-
Notifications
You must be signed in to change notification settings - Fork 19
/
sockopt.c
156 lines (131 loc) · 3.21 KB
/
sockopt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#define EXTUNIX_WANT_SOCKOPT
#define EXTUNIX_WANT_TCP_KEEPIDLE
#define EXTUNIX_WANT_TCP_KEEPCNT
#define EXTUNIX_WANT_TCP_KEEPINTVL
#include "config.h"
#if defined(EXTUNIX_HAVE_SOCKOPT)
#ifndef TCP_KEEPCNT
#define TCP_KEEPCNT (-1)
#endif
#ifndef TCP_KEEPIDLE
#if defined(__APPLE__) && defined(TCP_KEEPALIVE)
#define TCP_KEEPIDLE (TCP_KEEPALIVE)
#else
#define TCP_KEEPIDLE (-1)
#endif
#endif
#ifndef TCP_KEEPINTVL
#define TCP_KEEPINTVL (-1)
#endif
#ifndef SO_REUSEPORT
#define SO_REUSEPORT (-1)
#endif
#ifndef SO_ATTACH_BPF
#define SO_ATTACH_BPF (-1)
#endif
#ifndef SO_ATTACH_REUSEPORT_EBPF
#define SO_ATTACH_REUSEPORT_EBPF (-1)
#endif
#ifndef SO_DETACH_FILTER
#define SO_DETACH_FILTER (-1)
#endif
#ifndef SO_DETACH_BPF
#define SO_DETACH_BPF (-1)
#endif
#ifndef SO_LOCK_FILTER
#define SO_LOCK_FILTER (-1)
#endif
struct option {
int opt;
int level;
};
static struct option tcp_options[] = {
{ TCP_KEEPCNT, IPPROTO_TCP },
{ TCP_KEEPIDLE, IPPROTO_TCP },
{ TCP_KEEPINTVL, IPPROTO_TCP },
{ SO_REUSEPORT, SOL_SOCKET },
{ SO_ATTACH_BPF, SOL_SOCKET },
{ SO_ATTACH_REUSEPORT_EBPF, SOL_SOCKET },
{ SO_DETACH_FILTER, SOL_SOCKET },
{ SO_DETACH_BPF, SOL_SOCKET },
{ SO_LOCK_FILTER, SOL_SOCKET },
};
CAMLprim value caml_extunix_have_sockopt(value k)
{
if (Int_val(k) < 0 || (unsigned int)Int_val(k) >= sizeof(tcp_options) / sizeof(tcp_options[0]))
{
caml_invalid_argument("have_sockopt");
}
return Val_bool(tcp_options[Int_val(k)].opt != -1);
}
CAMLprim value caml_extunix_setsockopt_int(value fd, value k, value v)
{
int optval = Int_val(v);
socklen_t optlen = sizeof(optval);
#ifdef _WIN32
SOCKET s = INVALID_SOCKET;
if (KIND_SOCKET != Descr_kind_val(fd))
caml_invalid_argument("setsockopt_int");
s = Socket_val(fd);
#else
int s = Int_val(fd);
#endif
if (Int_val(k) < 0 || (unsigned int)Int_val(k) >= sizeof(tcp_options) / sizeof(tcp_options[0]))
{
caml_invalid_argument("setsockopt_int");
}
if (tcp_options[Int_val(k)].opt == -1)
{
caml_raise_not_found();
assert(0);
}
if (0 != setsockopt(s, tcp_options[Int_val(k)].level, tcp_options[Int_val(k)].opt, (void *)&optval, optlen))
{
#ifdef _WIN32
if (WSAGetLastError() == WSAENOPROTOOPT) {
#else
if (errno == ENOPROTOOPT) {
#endif
caml_raise_not_found();
assert(0);
}
uerror("setsockopt_int", Nothing);
}
return Val_unit;
}
CAMLprim value caml_extunix_getsockopt_int(value fd, value k)
{
int optval;
socklen_t optlen = sizeof(optval);
#ifdef _WIN32
SOCKET s = INVALID_SOCKET;
if (KIND_SOCKET != Descr_kind_val(fd))
caml_invalid_argument("getsockopt_int");
s = Socket_val(fd);
#else
int s = Int_val(fd);
#endif
if (Int_val(k) < 0 || (unsigned int)Int_val(k) >= sizeof(tcp_options) / sizeof(tcp_options[0]))
{
caml_invalid_argument("getsockopt_int");
}
if (tcp_options[Int_val(k)].opt == -1)
{
caml_raise_not_found();
assert(0);
}
if (0 != getsockopt(s, tcp_options[Int_val(k)].level, tcp_options[Int_val(k)].opt, (void *)&optval, &optlen))
{
#ifdef _WIN32
if (WSAGetLastError() == WSAENOPROTOOPT) {
#else
if (errno == ENOPROTOOPT) {
#endif
caml_raise_not_found();
assert(0);
}
uerror("getsockopt_int", Nothing);
}
return Val_int(optval);
}
#endif