| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <fcntl.h> | ||
| 4 | #include <sys/socket.h> | ||
| 5 | |||
| 6 | // Every socket fppd opens goes through these, so none survives into a child. | ||
| 7 | // The shells and tools fppd runs (system(), popen()) inherit every descriptor | ||
| 8 | // that is not close-on-exec, and one still running when fppd exits -- a pactl | ||
| 9 | // waiting on a wedged PipeWire, say -- kept fppd's ports: the restarted fppd | ||
| 10 | // bound MultiSync's 32320 again beside it (SO_REUSEPORT), and the kernel | ||
| 11 | // handed part of the incoming packets to the orphan. | ||
| 12 | 29815 | inline int OpenSocket(int domain, int type, int protocol) { | |
| 13 | #ifdef SOCK_CLOEXEC | ||
| 14 | 29815 | return ::socket(domain, type | SOCK_CLOEXEC, protocol); | |
| 15 | #else | ||
| 16 | int s = ::socket(domain, type, protocol); | ||
| 17 | if (s >= 0) { | ||
| 18 | fcntl(s, F_SETFD, FD_CLOEXEC); | ||
| 19 | } | ||
| 20 | return s; | ||
| 21 | #endif | ||
| 22 | } | ||
| 23 | |||
| 24 | 264902 | inline int AcceptSocket(int s, struct sockaddr* addr, socklen_t* len) { | |
| 25 | #ifdef SOCK_CLOEXEC | ||
| 26 | 264902 | return ::accept4(s, addr, len, SOCK_CLOEXEC); | |
| 27 | #else | ||
| 28 | int c = ::accept(s, addr, len); | ||
| 29 | if (c >= 0) { | ||
| 30 | fcntl(c, F_SETFD, FD_CLOEXEC); | ||
| 31 | } | ||
| 32 | return c; | ||
| 33 | #endif | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | #ifdef PLATFORM_OSX | ||
| 38 | |||
| 39 | #ifndef SOCK_NONBLOCK | ||
| 40 | // A no-op here: macOS has no such socket() flag, and plugins pass this to a raw | ||
| 41 | // socket(), so a private bit would fail with EINVAL. A socket opened with it is | ||
| 42 | // still BLOCKING on macOS, so a reader on the main loop must use MSG_DONTWAIT. | ||
| 43 | #define SOCK_NONBLOCK 0 | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #include <libkern/OSByteOrder.h> | ||
| 47 | |||
| 48 | #define htobe16(x) OSSwapHostToBigInt16(x) | ||
| 49 | #define htole16(x) OSSwapHostToLittleInt16(x) | ||
| 50 | #define be16toh(x) OSSwapBigToHostInt16(x) | ||
| 51 | #define le16toh(x) OSSwapLittleToHostInt16(x) | ||
| 52 | |||
| 53 | #define htobe32(x) OSSwapHostToBigInt32(x) | ||
| 54 | #define htole32(x) OSSwapHostToLittleInt32(x) | ||
| 55 | #define be32toh(x) OSSwapBigToHostInt32(x) | ||
| 56 | #define le32toh(x) OSSwapLittleToHostInt32(x) | ||
| 57 | |||
| 58 | #define htobe64(x) OSSwapHostToBigInt64(x) | ||
| 59 | #define htole64(x) OSSwapHostToLittleInt64(x) | ||
| 60 | #define be64toh(x) OSSwapBigToHostInt64(x) | ||
| 61 | #define le64toh(x) OSSwapLittleToHostInt64(x) | ||
| 62 | |||
| 63 | struct mmsghdr { | ||
| 64 | struct msghdr msg_hdr; | ||
| 65 | unsigned int msg_len; | ||
| 66 | }; | ||
| 67 | |||
| 68 | struct sockaddr_ll { | ||
| 69 | unsigned short sll_family; /* Always AF_PACKET */ | ||
| 70 | unsigned short sll_protocol; /* Physical-layer protocol */ | ||
| 71 | int sll_ifindex; /* Interface number */ | ||
| 72 | unsigned short sll_hatype; /* ARP hardware type */ | ||
| 73 | unsigned char sll_pkttype; /* Packet type */ | ||
| 74 | unsigned char sll_halen; /* Length of address */ | ||
| 75 | unsigned char sll_addr[8]; /* Physical-layer address */ | ||
| 76 | }; | ||
| 77 | |||
| 78 | struct ether_header { | ||
| 79 | u_char ether_dhost[6]; | ||
| 80 | u_char ether_shost[6]; | ||
| 81 | u_short ether_type; | ||
| 82 | }; | ||
| 83 | |||
| 84 | inline int recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, | ||
| 85 | unsigned int flags, struct timespec *timeout) | ||
| 86 | { | ||
| 87 | for (unsigned int i = 0; i < n; i++) { | ||
| 88 | ssize_t retval = recvmsg(fd, &msgs[i].msg_hdr, flags); | ||
| 89 | if (retval < 0) { | ||
| 90 | return i ? i : retval; | ||
| 91 | } | ||
| 92 | msgs[i].msg_len = retval; | ||
| 93 | } | ||
| 94 | return n; | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | inline int sendmmsg(int fd, struct mmsghdr *msgs, | ||
| 99 | unsigned int n, unsigned int flags) | ||
| 100 | { | ||
| 101 | for (unsigned int i = 0; i < n; i++) { | ||
| 102 | ssize_t retval = sendmsg(fd, &msgs[i].msg_hdr, flags); | ||
| 103 | if (retval < 0) { | ||
| 104 | return i ? i : retval; | ||
| 105 | } | ||
| 106 | msgs[i].msg_len = retval; | ||
| 107 | } | ||
| 108 | return n; | ||
| 109 | } | ||
| 110 | |||
| 111 | #endif | ||
| 112 |