summaryrefslogtreecommitdiff
path: root/ot_socket.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_socket.c')
-rw-r--r--ot_socket.c26
1 files changed, 25 insertions, 1 deletions
diff --git a/ot_socket.c b/ot_socket.c
index 576087f..158459f 100644
--- a/ot_socket.c
+++ b/ot_socket.c
@@ -75,13 +75,37 @@ static int socket_v6(int type) {
75 return s; 75 return s;
76} 76}
77 77
78/* SO_REUSEPORT means three different things. Linux spreads datagrams over
79 the group. FreeBSD permits the bind and spreads nothing, which is what
80 SO_REUSEPORT_LB was added for. Darwin hands every datagram to whichever
81 socket bound last, so a group there starves all but one reader. */
82#if defined(SO_REUSEPORT_LB)
83#define OT_BALANCE_OPTION SO_REUSEPORT_LB
84#elif defined(SO_REUSEPORT) && !defined(__APPLE__)
85#define OT_BALANCE_OPTION SO_REUSEPORT
86#endif
87
88int socket_can_balance(void) {
89#ifdef OT_BALANCE_OPTION
90 return 1;
91#else
92 return 0;
93#endif
94}
95
78/* The only place in the tree that needs to know what a kernel calls this */ 96/* The only place in the tree that needs to know what a kernel calls this */
79static void socket_set_sharing(int s, socket_sharing sharing) { 97static void socket_set_sharing(int s, socket_sharing sharing) {
80 int one = 1; 98 int one = 1;
81 99
82 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); 100 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
101#ifdef OT_BALANCE_OPTION
102 if (sharing == SOCKET_BALANCED) {
103 setsockopt(s, SOL_SOCKET, OT_BALANCE_OPTION, &one, sizeof(one));
104 return;
105 }
106#endif
83#ifdef SO_REUSEPORT 107#ifdef SO_REUSEPORT
84 if (sharing == SOCKET_SHARED) 108 if (sharing != SOCKET_EXCLUSIVE)
85 setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); 109 setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
86#endif 110#endif
87} 111}