15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
24#define SOCK_INVALID_INDEX -1
36 template <
typename NativeHandle>
42 const int flags = fcntl(
fd, F_GETFD, 0);
45 fcntl(
fd, F_SETFD, flags | FD_CLOEXEC);
62 DIR *FdDir = opendir(
"/proc/self/fd");
68 while ((Entry = readdir(FdDir)) !=
nullptr)
70 char *EndPtr =
nullptr;
71 const long Fd = std::strtol(Entry->d_name, &EndPtr, 10);
72 if (EndPtr == Entry->d_name || *EndPtr !=
'\0' || Fd < 0)
76 struct sockaddr_storage Addr;
77 socklen_t AddrLen =
sizeof(Addr);
78 if (getsockname(
static_cast<int>(Fd),
reinterpret_cast<struct sockaddr *
>(&Addr), &AddrLen) != 0)
82 uint16_t BoundPort = 0;
83 if (Addr.ss_family == AF_INET)
85 BoundPort = ntohs(
reinterpret_cast<struct sockaddr_in *
>(&Addr)->sin_port);
87 else if (Addr.ss_family == AF_INET6)
89 BoundPort = ntohs(
reinterpret_cast<struct sockaddr_in6 *
>(&Addr)->sin6_port);
91 if (BoundPort == Port)
#define SOCK_INVALID_INDEX
< socket sockaddr_in
This file contains definitions of common data structures used in traffic manager.
void SetSocketCloseOnExec(NativeHandle fd)
Marks a socket file descriptor close-on-exec, so it is not inherited by child processes started via f...
void SetCloseOnExecForListeningPort(uint16_t Port)
Fallback for listening sockets we have no direct handle to (e.g.