diff options
Diffstat (limited to 'src/nat-auto/gnunet-nat-server.c')
-rw-r--r-- | src/nat-auto/gnunet-nat-server.c | 300 |
1 files changed, 149 insertions, 151 deletions
diff --git a/src/nat-auto/gnunet-nat-server.c b/src/nat-auto/gnunet-nat-server.c index c58768982..db44d7629 100644 --- a/src/nat-auto/gnunet-nat-server.c +++ b/src/nat-auto/gnunet-nat-server.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 15 | You should have received a copy of the GNU Affero General Public License |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file src/nat/gnunet-nat-server.c | 22 | * @file src/nat/gnunet-nat-server.c |
@@ -33,8 +33,7 @@ | |||
33 | /** | 33 | /** |
34 | * Information we track per client. | 34 | * Information we track per client. |
35 | */ | 35 | */ |
36 | struct ClientData | 36 | struct ClientData { |
37 | { | ||
38 | /** | 37 | /** |
39 | * Timeout task. | 38 | * Timeout task. |
40 | */ | 39 | */ |
@@ -61,9 +60,9 @@ static const struct GNUNET_CONFIGURATION_Handle *cfg; | |||
61 | * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO) | 60 | * @param is_tcp mark for TCP (#GNUNET_YES) or UDP (#GNUNET_NO) |
62 | */ | 61 | */ |
63 | static void | 62 | static void |
64 | try_anat (uint32_t dst_ipv4, | 63 | try_anat(uint32_t dst_ipv4, |
65 | uint16_t dport, | 64 | uint16_t dport, |
66 | int is_tcp) | 65 | int is_tcp) |
67 | { | 66 | { |
68 | struct GNUNET_NAT_Handle *h; | 67 | struct GNUNET_NAT_Handle *h; |
69 | struct sockaddr_in lsa; | 68 | struct sockaddr_in lsa; |
@@ -71,45 +70,44 @@ try_anat (uint32_t dst_ipv4, | |||
71 | const struct sockaddr *sa; | 70 | const struct sockaddr *sa; |
72 | socklen_t sa_len; | 71 | socklen_t sa_len; |
73 | 72 | ||
74 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 73 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
75 | "Asking for connection reversal with %x and code %u\n", | 74 | "Asking for connection reversal with %x and code %u\n", |
76 | (unsigned int) dst_ipv4, | 75 | (unsigned int)dst_ipv4, |
77 | (unsigned int) dport); | 76 | (unsigned int)dport); |
78 | memset (&lsa, 0, sizeof (lsa)); | 77 | memset(&lsa, 0, sizeof(lsa)); |
79 | lsa.sin_family = AF_INET; | 78 | lsa.sin_family = AF_INET; |
80 | #if HAVE_SOCKADDR_IN_SIN_LEN | 79 | #if HAVE_SOCKADDR_IN_SIN_LEN |
81 | lsa.sin_len = sizeof (sa); | 80 | lsa.sin_len = sizeof(sa); |
82 | #endif | 81 | #endif |
83 | lsa.sin_addr.s_addr = 0; | 82 | lsa.sin_addr.s_addr = 0; |
84 | lsa.sin_port = htons (dport); | 83 | lsa.sin_port = htons(dport); |
85 | memset (&rsa, 0, sizeof (rsa)); | 84 | memset(&rsa, 0, sizeof(rsa)); |
86 | rsa.sin_family = AF_INET; | 85 | rsa.sin_family = AF_INET; |
87 | #if HAVE_SOCKADDR_IN_SIN_LEN | 86 | #if HAVE_SOCKADDR_IN_SIN_LEN |
88 | rsa.sin_len = sizeof (sa); | 87 | rsa.sin_len = sizeof(sa); |
89 | #endif | 88 | #endif |
90 | rsa.sin_addr.s_addr = dst_ipv4; | 89 | rsa.sin_addr.s_addr = dst_ipv4; |
91 | rsa.sin_port = htons (dport); | 90 | rsa.sin_port = htons(dport); |
92 | sa_len = sizeof (lsa); | 91 | sa_len = sizeof(lsa); |
93 | sa = (const struct sockaddr *) &lsa; | 92 | sa = (const struct sockaddr *)&lsa; |
94 | h = GNUNET_NAT_register (cfg, | 93 | h = GNUNET_NAT_register(cfg, |
95 | "none", | 94 | "none", |
96 | is_tcp ? IPPROTO_TCP : IPPROTO_UDP, | 95 | is_tcp ? IPPROTO_TCP : IPPROTO_UDP, |
97 | 1, | 96 | 1, |
98 | &sa, | 97 | &sa, |
99 | &sa_len, | 98 | &sa_len, |
100 | NULL, NULL, NULL); | 99 | NULL, NULL, NULL); |
101 | GNUNET_NAT_request_reversal (h, | 100 | GNUNET_NAT_request_reversal(h, |
102 | &lsa, | 101 | &lsa, |
103 | &rsa); | 102 | &rsa); |
104 | GNUNET_NAT_unregister (h); | 103 | GNUNET_NAT_unregister(h); |
105 | } | 104 | } |
106 | 105 | ||
107 | 106 | ||
108 | /** | 107 | /** |
109 | * Closure for #tcp_send. | 108 | * Closure for #tcp_send. |
110 | */ | 109 | */ |
111 | struct TcpContext | 110 | struct TcpContext { |
112 | { | ||
113 | /** | 111 | /** |
114 | * TCP socket. | 112 | * TCP socket. |
115 | */ | 113 | */ |
@@ -129,24 +127,24 @@ struct TcpContext | |||
129 | * @param cls the `struct TcpContext` | 127 | * @param cls the `struct TcpContext` |
130 | */ | 128 | */ |
131 | static void | 129 | static void |
132 | tcp_send (void *cls) | 130 | tcp_send(void *cls) |
133 | { | 131 | { |
134 | struct TcpContext *ctx = cls; | 132 | struct TcpContext *ctx = cls; |
135 | const struct GNUNET_SCHEDULER_TaskContext *tc; | 133 | const struct GNUNET_SCHEDULER_TaskContext *tc; |
136 | 134 | ||
137 | tc = GNUNET_SCHEDULER_get_task_context (); | 135 | tc = GNUNET_SCHEDULER_get_task_context(); |
138 | if ((NULL != tc->write_ready) && | 136 | if ((NULL != tc->write_ready) && |
139 | (GNUNET_NETWORK_fdset_isset (tc->write_ready, ctx->s))) | 137 | (GNUNET_NETWORK_fdset_isset(tc->write_ready, ctx->s))) |
140 | { | ||
141 | if (-1 == | ||
142 | GNUNET_NETWORK_socket_send (ctx->s, &ctx->data, sizeof (ctx->data))) | ||
143 | { | 138 | { |
144 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "send"); | 139 | if (-1 == |
140 | GNUNET_NETWORK_socket_send(ctx->s, &ctx->data, sizeof(ctx->data))) | ||
141 | { | ||
142 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "send"); | ||
143 | } | ||
144 | GNUNET_NETWORK_socket_shutdown(ctx->s, SHUT_RDWR); | ||
145 | } | 145 | } |
146 | GNUNET_NETWORK_socket_shutdown (ctx->s, SHUT_RDWR); | 146 | GNUNET_NETWORK_socket_close(ctx->s); |
147 | } | 147 | GNUNET_free(ctx); |
148 | GNUNET_NETWORK_socket_close (ctx->s); | ||
149 | GNUNET_free (ctx); | ||
150 | } | 148 | } |
151 | 149 | ||
152 | 150 | ||
@@ -159,52 +157,52 @@ tcp_send (void *cls) | |||
159 | * @param data data to send | 157 | * @param data data to send |
160 | */ | 158 | */ |
161 | static void | 159 | static void |
162 | try_send_tcp (uint32_t dst_ipv4, | 160 | try_send_tcp(uint32_t dst_ipv4, |
163 | uint16_t dport, | 161 | uint16_t dport, |
164 | uint16_t data) | 162 | uint16_t data) |
165 | { | 163 | { |
166 | struct GNUNET_NETWORK_Handle *s; | 164 | struct GNUNET_NETWORK_Handle *s; |
167 | struct sockaddr_in sa; | 165 | struct sockaddr_in sa; |
168 | struct TcpContext *ctx; | 166 | struct TcpContext *ctx; |
169 | 167 | ||
170 | s = GNUNET_NETWORK_socket_create (AF_INET, | 168 | s = GNUNET_NETWORK_socket_create(AF_INET, |
171 | SOCK_STREAM, | 169 | SOCK_STREAM, |
172 | 0); | 170 | 0); |
173 | if (NULL == s) | 171 | if (NULL == s) |
174 | { | 172 | { |
175 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | 173 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, |
176 | "socket"); | 174 | "socket"); |
177 | return; | 175 | return; |
178 | } | 176 | } |
179 | memset (&sa, 0, sizeof (sa)); | 177 | memset(&sa, 0, sizeof(sa)); |
180 | sa.sin_family = AF_INET; | 178 | sa.sin_family = AF_INET; |
181 | #if HAVE_SOCKADDR_IN_SIN_LEN | 179 | #if HAVE_SOCKADDR_IN_SIN_LEN |
182 | sa.sin_len = sizeof (sa); | 180 | sa.sin_len = sizeof(sa); |
183 | #endif | 181 | #endif |
184 | sa.sin_addr.s_addr = dst_ipv4; | 182 | sa.sin_addr.s_addr = dst_ipv4; |
185 | sa.sin_port = htons (dport); | 183 | sa.sin_port = htons(dport); |
186 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 184 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
187 | "Sending TCP message to `%s'\n", | 185 | "Sending TCP message to `%s'\n", |
188 | GNUNET_a2s ((struct sockaddr *) &sa, | 186 | GNUNET_a2s((struct sockaddr *)&sa, |
189 | sizeof (sa))); | 187 | sizeof(sa))); |
190 | if ( (GNUNET_OK != | 188 | if ((GNUNET_OK != |
191 | GNUNET_NETWORK_socket_connect (s, | 189 | GNUNET_NETWORK_socket_connect(s, |
192 | (const struct sockaddr *) &sa, | 190 | (const struct sockaddr *)&sa, |
193 | sizeof (sa))) && | 191 | sizeof(sa))) && |
194 | (errno != EINPROGRESS) ) | 192 | (errno != EINPROGRESS)) |
195 | { | 193 | { |
196 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | 194 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, |
197 | "connect"); | 195 | "connect"); |
198 | GNUNET_NETWORK_socket_close (s); | 196 | GNUNET_NETWORK_socket_close(s); |
199 | return; | 197 | return; |
200 | } | 198 | } |
201 | ctx = GNUNET_new (struct TcpContext); | 199 | ctx = GNUNET_new(struct TcpContext); |
202 | ctx->s = s; | 200 | ctx->s = s; |
203 | ctx->data = data; | 201 | ctx->data = data; |
204 | GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_SECONDS, | 202 | GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_SECONDS, |
205 | s, | 203 | s, |
206 | &tcp_send, | 204 | &tcp_send, |
207 | ctx); | 205 | ctx); |
208 | } | 206 | } |
209 | 207 | ||
210 | 208 | ||
@@ -217,42 +215,42 @@ try_send_tcp (uint32_t dst_ipv4, | |||
217 | * @param data data to send | 215 | * @param data data to send |
218 | */ | 216 | */ |
219 | static void | 217 | static void |
220 | try_send_udp (uint32_t dst_ipv4, | 218 | try_send_udp(uint32_t dst_ipv4, |
221 | uint16_t dport, | 219 | uint16_t dport, |
222 | uint16_t data) | 220 | uint16_t data) |
223 | { | 221 | { |
224 | struct GNUNET_NETWORK_Handle *s; | 222 | struct GNUNET_NETWORK_Handle *s; |
225 | struct sockaddr_in sa; | 223 | struct sockaddr_in sa; |
226 | 224 | ||
227 | s = GNUNET_NETWORK_socket_create (AF_INET, | 225 | s = GNUNET_NETWORK_socket_create(AF_INET, |
228 | SOCK_DGRAM, | 226 | SOCK_DGRAM, |
229 | 0); | 227 | 0); |
230 | if (NULL == s) | 228 | if (NULL == s) |
231 | { | 229 | { |
232 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | 230 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, |
233 | "socket"); | 231 | "socket"); |
234 | return; | 232 | return; |
235 | } | 233 | } |
236 | memset (&sa, 0, sizeof (sa)); | 234 | memset(&sa, 0, sizeof(sa)); |
237 | sa.sin_family = AF_INET; | 235 | sa.sin_family = AF_INET; |
238 | #if HAVE_SOCKADDR_IN_SIN_LEN | 236 | #if HAVE_SOCKADDR_IN_SIN_LEN |
239 | sa.sin_len = sizeof (sa); | 237 | sa.sin_len = sizeof(sa); |
240 | #endif | 238 | #endif |
241 | sa.sin_addr.s_addr = dst_ipv4; | 239 | sa.sin_addr.s_addr = dst_ipv4; |
242 | sa.sin_port = htons (dport); | 240 | sa.sin_port = htons(dport); |
243 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 241 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
244 | "Sending UDP packet to `%s'\n", | 242 | "Sending UDP packet to `%s'\n", |
245 | GNUNET_a2s ((struct sockaddr *) &sa, | 243 | GNUNET_a2s((struct sockaddr *)&sa, |
246 | sizeof (sa))); | 244 | sizeof(sa))); |
247 | if (-1 == | 245 | if (-1 == |
248 | GNUNET_NETWORK_socket_sendto (s, | 246 | GNUNET_NETWORK_socket_sendto(s, |
249 | &data, | 247 | &data, |
250 | sizeof (data), | 248 | sizeof(data), |
251 | (const struct sockaddr *) &sa, | 249 | (const struct sockaddr *)&sa, |
252 | sizeof (sa))) | 250 | sizeof(sa))) |
253 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | 251 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, |
254 | "sendto"); | 252 | "sendto"); |
255 | GNUNET_NETWORK_socket_close (s); | 253 | GNUNET_NETWORK_socket_close(s); |
256 | } | 254 | } |
257 | 255 | ||
258 | 256 | ||
@@ -264,28 +262,28 @@ try_send_udp (uint32_t dst_ipv4, | |||
264 | * @param msg message with details about what to test | 262 | * @param msg message with details about what to test |
265 | */ | 263 | */ |
266 | static void | 264 | static void |
267 | handle_test (void *cls, | 265 | handle_test(void *cls, |
268 | const struct GNUNET_NAT_AUTO_TestMessage *tm) | 266 | const struct GNUNET_NAT_AUTO_TestMessage *tm) |
269 | { | 267 | { |
270 | struct ClientData *cd = cls; | 268 | struct ClientData *cd = cls; |
271 | uint16_t dport; | 269 | uint16_t dport; |
272 | 270 | ||
273 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 271 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
274 | "Received test request\n"); | 272 | "Received test request\n"); |
275 | dport = ntohs (tm->dport); | 273 | dport = ntohs(tm->dport); |
276 | if (0 == dport) | 274 | if (0 == dport) |
277 | try_anat (tm->dst_ipv4, | 275 | try_anat(tm->dst_ipv4, |
278 | ntohs (tm->data), | 276 | ntohs(tm->data), |
279 | (int) ntohl (tm->is_tcp)); | 277 | (int)ntohl(tm->is_tcp)); |
280 | else if (GNUNET_YES == ntohl (tm->is_tcp)) | 278 | else if (GNUNET_YES == ntohl(tm->is_tcp)) |
281 | try_send_tcp (tm->dst_ipv4, | 279 | try_send_tcp(tm->dst_ipv4, |
282 | dport, | 280 | dport, |
283 | tm->data); | 281 | tm->data); |
284 | else | 282 | else |
285 | try_send_udp (tm->dst_ipv4, | 283 | try_send_udp(tm->dst_ipv4, |
286 | dport, | 284 | dport, |
287 | tm->data); | 285 | tm->data); |
288 | GNUNET_SERVICE_client_drop (cd->client); | 286 | GNUNET_SERVICE_client_drop(cd->client); |
289 | } | 287 | } |
290 | 288 | ||
291 | 289 | ||
@@ -297,9 +295,9 @@ handle_test (void *cls, | |||
297 | * @param srv service handle | 295 | * @param srv service handle |
298 | */ | 296 | */ |
299 | static void | 297 | static void |
300 | run (void *cls, | 298 | run(void *cls, |
301 | const struct GNUNET_CONFIGURATION_Handle *c, | 299 | const struct GNUNET_CONFIGURATION_Handle *c, |
302 | struct GNUNET_SERVICE_Handle *srv) | 300 | struct GNUNET_SERVICE_Handle *srv) |
303 | { | 301 | { |
304 | cfg = c; | 302 | cfg = c; |
305 | } | 303 | } |
@@ -311,12 +309,12 @@ run (void *cls, | |||
311 | * @param cls our `struct ClientData` of a client to drop | 309 | * @param cls our `struct ClientData` of a client to drop |
312 | */ | 310 | */ |
313 | static void | 311 | static void |
314 | force_timeout (void *cls) | 312 | force_timeout(void *cls) |
315 | { | 313 | { |
316 | struct ClientData *cd = cls; | 314 | struct ClientData *cd = cls; |
317 | 315 | ||
318 | cd->tt = NULL; | 316 | cd->tt = NULL; |
319 | GNUNET_SERVICE_client_drop (cd->client); | 317 | GNUNET_SERVICE_client_drop(cd->client); |
320 | } | 318 | } |
321 | 319 | ||
322 | 320 | ||
@@ -330,17 +328,17 @@ force_timeout (void *cls) | |||
330 | * @return our `struct ClientData` | 328 | * @return our `struct ClientData` |
331 | */ | 329 | */ |
332 | static void * | 330 | static void * |
333 | client_connect_cb (void *cls, | 331 | client_connect_cb(void *cls, |
334 | struct GNUNET_SERVICE_Client *c, | 332 | struct GNUNET_SERVICE_Client *c, |
335 | struct GNUNET_MQ_Handle *mq) | 333 | struct GNUNET_MQ_Handle *mq) |
336 | { | 334 | { |
337 | struct ClientData *cd; | 335 | struct ClientData *cd; |
338 | 336 | ||
339 | cd = GNUNET_new (struct ClientData); | 337 | cd = GNUNET_new(struct ClientData); |
340 | cd->client = c; | 338 | cd->client = c; |
341 | cd->tt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | 339 | cd->tt = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, |
342 | &force_timeout, | 340 | &force_timeout, |
343 | cd); | 341 | cd); |
344 | return cd; | 342 | return cd; |
345 | } | 343 | } |
346 | 344 | ||
@@ -353,15 +351,15 @@ client_connect_cb (void *cls, | |||
353 | * @param internal_cls our `struct ClientData` | 351 | * @param internal_cls our `struct ClientData` |
354 | */ | 352 | */ |
355 | static void | 353 | static void |
356 | client_disconnect_cb (void *cls, | 354 | client_disconnect_cb(void *cls, |
357 | struct GNUNET_SERVICE_Client *c, | 355 | struct GNUNET_SERVICE_Client *c, |
358 | void *internal_cls) | 356 | void *internal_cls) |
359 | { | 357 | { |
360 | struct ClientData *cd = internal_cls; | 358 | struct ClientData *cd = internal_cls; |
361 | 359 | ||
362 | if (NULL != cd->tt) | 360 | if (NULL != cd->tt) |
363 | GNUNET_SCHEDULER_cancel (cd->tt); | 361 | GNUNET_SCHEDULER_cancel(cd->tt); |
364 | GNUNET_free (cd); | 362 | GNUNET_free(cd); |
365 | } | 363 | } |
366 | 364 | ||
367 | 365 | ||
@@ -369,17 +367,17 @@ client_disconnect_cb (void *cls, | |||
369 | * Define "main" method using service macro. | 367 | * Define "main" method using service macro. |
370 | */ | 368 | */ |
371 | GNUNET_SERVICE_MAIN | 369 | GNUNET_SERVICE_MAIN |
372 | ("nat-server", | 370 | ("nat-server", |
373 | GNUNET_SERVICE_OPTION_NONE, | 371 | GNUNET_SERVICE_OPTION_NONE, |
374 | &run, | 372 | &run, |
375 | &client_connect_cb, | 373 | &client_connect_cb, |
376 | &client_disconnect_cb, | 374 | &client_disconnect_cb, |
377 | NULL, | 375 | NULL, |
378 | GNUNET_MQ_hd_fixed_size (test, | 376 | GNUNET_MQ_hd_fixed_size(test, |
379 | GNUNET_MESSAGE_TYPE_NAT_TEST, | 377 | GNUNET_MESSAGE_TYPE_NAT_TEST, |
380 | struct GNUNET_NAT_AUTO_TestMessage, | 378 | struct GNUNET_NAT_AUTO_TestMessage, |
381 | NULL), | 379 | NULL), |
382 | GNUNET_MQ_handler_end ()); | 380 | GNUNET_MQ_handler_end()); |
383 | 381 | ||
384 | 382 | ||
385 | #if defined(LINUX) && defined(__GLIBC__) | 383 | #if defined(LINUX) && defined(__GLIBC__) |
@@ -389,11 +387,11 @@ GNUNET_SERVICE_MAIN | |||
389 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. | 387 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. |
390 | */ | 388 | */ |
391 | void __attribute__ ((constructor)) | 389 | void __attribute__ ((constructor)) |
392 | GNUNET_ARM_memory_init () | 390 | GNUNET_ARM_memory_init() |
393 | { | 391 | { |
394 | mallopt (M_TRIM_THRESHOLD, 4 * 1024); | 392 | mallopt(M_TRIM_THRESHOLD, 4 * 1024); |
395 | mallopt (M_TOP_PAD, 1 * 1024); | 393 | mallopt(M_TOP_PAD, 1 * 1024); |
396 | malloc_trim (0); | 394 | malloc_trim(0); |
397 | } | 395 | } |
398 | #endif | 396 | #endif |
399 | 397 | ||