File force_bind.c changed (mode: 100644) (index a5207a6..2e5a072) |
28 |
28 |
#include <fcntl.h> |
#include <fcntl.h> |
29 |
29 |
#include <sys/socket.h> |
#include <sys/socket.h> |
30 |
30 |
#include <arpa/inet.h> |
#include <arpa/inet.h> |
|
31 |
|
#include <netinet/in.h> |
|
32 |
|
#include <netinet/tcp.h> |
31 |
33 |
|
|
32 |
34 |
|
|
33 |
35 |
static int (*old_bind)(int sockfd, const struct sockaddr *addr, socklen_t addrlen) = NULL; |
static int (*old_bind)(int sockfd, const struct sockaddr *addr, socklen_t addrlen) = NULL; |
|
... |
... |
static int (*old_setsockopt)(int sockfd, int level, int optname, const void *op |
35 |
37 |
static int (*old_socket)(int domain, int type, int protocol); |
static int (*old_socket)(int domain, int type, int protocol); |
36 |
38 |
static char *force_address = NULL; |
static char *force_address = NULL; |
37 |
39 |
static int force_port = -1; |
static int force_port = -1; |
38 |
|
static char set_tos = 0; |
|
39 |
|
static unsigned char tos; |
|
|
40 |
|
static unsigned int set_tos = 0, tos; |
|
41 |
|
static unsigned int set_keepalive = 0, keepalive; |
40 |
42 |
|
|
41 |
43 |
/* Functions */ |
/* Functions */ |
42 |
44 |
|
|
|
45 |
|
static void set_ka(int sock) |
|
46 |
|
{ |
|
47 |
|
int flag, ret; |
|
48 |
|
|
|
49 |
|
flag = (keepalive > 0) ? 1 : 0; |
|
50 |
|
ret = old_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(flag)); |
|
51 |
|
syslog(LOG_INFO, "force_bind: changing SO_KEEPALIVE to 1 (ret=%d).\n", ret); |
|
52 |
|
} |
|
53 |
|
|
|
54 |
|
static void set_ka_idle(int sock) |
|
55 |
|
{ |
|
56 |
|
int ret; |
|
57 |
|
|
|
58 |
|
ret = old_setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepalive, sizeof(keepalive)); |
|
59 |
|
syslog(LOG_INFO, "force_bind: changing TCP_KEEPIDLE to %us (ret=%d).\n", |
|
60 |
|
keepalive, ret); |
|
61 |
|
} |
|
62 |
|
|
43 |
63 |
void init(void) |
void init(void) |
44 |
64 |
{ |
{ |
45 |
65 |
static unsigned char inited = 0; |
static unsigned char inited = 0; |
|
... |
... |
void init(void) |
73 |
93 |
tos); |
tos); |
74 |
94 |
} |
} |
75 |
95 |
|
|
|
96 |
|
/* keep alive */ |
|
97 |
|
x = getenv("FORCE_NET_KA"); |
|
98 |
|
if (x != NULL) { |
|
99 |
|
set_keepalive = 1; |
|
100 |
|
keepalive = strtoul(x, NULL, 0); |
|
101 |
|
syslog(LOG_INFO, "force_bind: Force KA to %u.\n", |
|
102 |
|
keepalive); |
|
103 |
|
} |
|
104 |
|
|
76 |
105 |
old_bind = dlsym(RTLD_NEXT, "bind"); |
old_bind = dlsym(RTLD_NEXT, "bind"); |
77 |
106 |
if (old_bind == NULL) { |
if (old_bind == NULL) { |
78 |
107 |
syslog(LOG_ERR, "force_bind: Cannot resolve 'bind'!\n"); |
syslog(LOG_ERR, "force_bind: Cannot resolve 'bind'!\n"); |
|
... |
... |
int setsockopt(int sockfd, int level, int optname, const void *optval, |
145 |
174 |
{ |
{ |
146 |
175 |
init(); |
init(); |
147 |
176 |
|
|
148 |
|
switch (optname) { |
|
149 |
|
case IP_TOS: |
|
150 |
|
if (set_tos == 1) { |
|
151 |
|
syslog(LOG_INFO, "force_bind: changing TOS from %hhu to %hhu.\n", |
|
152 |
|
*(char *)optval, tos); |
|
153 |
|
optval = &tos; |
|
154 |
|
} |
|
155 |
|
break; |
|
|
177 |
|
if (level == SOL_SOCKET) { |
|
178 |
|
if ((optname == SO_KEEPALIVE) && (set_keepalive == 1)) { |
|
179 |
|
set_ka(sockfd); |
|
180 |
|
return 0; |
|
181 |
|
} |
|
182 |
|
} |
|
183 |
|
|
|
184 |
|
if (level == IPPROTO_IP) { |
|
185 |
|
if ((optname == IP_TOS) && (set_tos == 1)) { |
|
186 |
|
syslog(LOG_INFO, "force_bind: changing TOS from %hhu to %hhu.\n", |
|
187 |
|
*(char *)optval, tos); |
|
188 |
|
optval = &tos; |
|
189 |
|
optlen = sizeof(tos); |
|
190 |
|
} |
156 |
191 |
} |
} |
157 |
192 |
|
|
|
193 |
|
if (level == IPPROTO_TCP) { |
|
194 |
|
if ((optname == TCP_KEEPIDLE) && (set_keepalive == 1)) { |
|
195 |
|
set_ka_idle(sockfd); |
|
196 |
|
return 0; |
|
197 |
|
} |
|
198 |
|
} |
158 |
199 |
|
|
159 |
200 |
return old_setsockopt(sockfd, level, optname, optval, optlen); |
return old_setsockopt(sockfd, level, optname, optval, optlen); |
160 |
201 |
} |
} |
|
... |
... |
int socket(int domain, int type, int protocol) |
173 |
214 |
return -1; |
return -1; |
174 |
215 |
|
|
175 |
216 |
if (set_tos == 1) |
if (set_tos == 1) |
176 |
|
setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, 1); |
|
|
217 |
|
setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
|
218 |
|
|
|
219 |
|
if (set_keepalive == 1) { |
|
220 |
|
set_ka(sock); |
|
221 |
|
|
|
222 |
|
if (type == SOCK_STREAM) |
|
223 |
|
set_ka_idle(sock); |
|
224 |
|
} |
177 |
225 |
|
|
178 |
226 |
return sock; |
return sock; |
179 |
227 |
} |
} |