| 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 | 22035 | inline int OpenSocket(int domain, int type, int protocol) { | |
| 13 | #ifdef SOCK_CLOEXEC | ||
| 14 | 22035 | 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 | 249465 | inline int AcceptSocket(int s, struct sockaddr* addr, socklen_t* len) { | |
| 25 | #ifdef SOCK_CLOEXEC | ||
| 26 | 249465 | 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 | #include <fcntl.h> | ||
| 41 | //# define SOCK_NONBLOCK O_NONBLOCK | ||
| 42 | #define SOCK_NONBLOCK 0 | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #include <libkern/OSByteOrder.h> | ||
| 46 | |||
| 47 | #define htobe16(x) OSSwapHostToBigInt16(x) | ||
| 48 | #define htole16(x) OSSwapHostToLittleInt16(x) | ||
| 49 | #define be16toh(x) OSSwapBigToHostInt16(x) | ||
| 50 | #define le16toh(x) OSSwapLittleToHostInt16(x) | ||
| 51 | |||
| 52 | #define htobe32(x) OSSwapHostToBigInt32(x) | ||
| 53 | #define htole32(x) OSSwapHostToLittleInt32(x) | ||
| 54 | #define be32toh(x) OSSwapBigToHostInt32(x) | ||
| 55 | #define le32toh(x) OSSwapLittleToHostInt32(x) | ||
| 56 | |||
| 57 | #define htobe64(x) OSSwapHostToBigInt64(x) | ||
| 58 | #define htole64(x) OSSwapHostToLittleInt64(x) | ||
| 59 | #define be64toh(x) OSSwapBigToHostInt64(x) | ||
| 60 | #define le64toh(x) OSSwapLittleToHostInt64(x) | ||
| 61 | |||
| 62 | struct mmsghdr { | ||
| 63 | struct msghdr msg_hdr; | ||
| 64 | unsigned int msg_len; | ||
| 65 | }; | ||
| 66 | |||
| 67 | struct sockaddr_ll { | ||
| 68 | unsigned short sll_family; /* Always AF_PACKET */ | ||
| 69 | unsigned short sll_protocol; /* Physical-layer protocol */ | ||
| 70 | int sll_ifindex; /* Interface number */ | ||
| 71 | unsigned short sll_hatype; /* ARP hardware type */ | ||
| 72 | unsigned char sll_pkttype; /* Packet type */ | ||
| 73 | unsigned char sll_halen; /* Length of address */ | ||
| 74 | unsigned char sll_addr[8]; /* Physical-layer address */ | ||
| 75 | }; | ||
| 76 | |||
| 77 | struct ether_header { | ||
| 78 | u_char ether_dhost[6]; | ||
| 79 | u_char ether_shost[6]; | ||
| 80 | u_short ether_type; | ||
| 81 | }; | ||
| 82 | |||
| 83 | inline int recvmmsg(int fd, struct mmsghdr *msgs, unsigned int n, | ||
| 84 | unsigned int flags, struct timespec *timeout) | ||
| 85 | { | ||
| 86 | for (unsigned int i = 0; i < n; i++) { | ||
| 87 | ssize_t retval = recvmsg(fd, &msgs[i].msg_hdr, flags); | ||
| 88 | if (retval < 0) { | ||
| 89 | return i ? i : retval; | ||
| 90 | } | ||
| 91 | msgs[i].msg_len = retval; | ||
| 92 | } | ||
| 93 | return n; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | inline int sendmmsg(int fd, struct mmsghdr *msgs, | ||
| 98 | unsigned int n, unsigned int flags) | ||
| 99 | { | ||
| 100 | for (unsigned int i = 0; i < n; i++) { | ||
| 101 | ssize_t retval = sendmsg(fd, &msgs[i].msg_hdr, flags); | ||
| 102 | if (retval < 0) { | ||
| 103 | return i ? i : retval; | ||
| 104 | } | ||
| 105 | msgs[i].msg_len = retval; | ||
| 106 | } | ||
| 107 | return n; | ||
| 108 | } | ||
| 109 | |||
| 110 | #endif | ||
| 111 |