diff options
Diffstat (limited to 'src/nat-auto/gnunet-service-nat-auto.c')
-rw-r--r-- | src/nat-auto/gnunet-service-nat-auto.c | 289 |
1 files changed, 147 insertions, 142 deletions
diff --git a/src/nat-auto/gnunet-service-nat-auto.c b/src/nat-auto/gnunet-service-nat-auto.c index 199a759ea..2c7faae2e 100644 --- a/src/nat-auto/gnunet-service-nat-auto.c +++ b/src/nat-auto/gnunet-service-nat-auto.c | |||
@@ -45,13 +45,15 @@ | |||
45 | /** | 45 | /** |
46 | * How long do we wait until we forcefully terminate autoconfiguration? | 46 | * How long do we wait until we forcefully terminate autoconfiguration? |
47 | */ | 47 | */ |
48 | #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5) | 48 | #define AUTOCONFIG_TIMEOUT GNUNET_TIME_relative_multiply ( \ |
49 | GNUNET_TIME_UNIT_SECONDS, 5) | ||
49 | 50 | ||
50 | 51 | ||
51 | /** | 52 | /** |
52 | * Internal data structure we track for each of our clients. | 53 | * Internal data structure we track for each of our clients. |
53 | */ | 54 | */ |
54 | struct ClientHandle { | 55 | struct ClientHandle |
56 | { | ||
55 | /** | 57 | /** |
56 | * Kept in a DLL. | 58 | * Kept in a DLL. |
57 | */ | 59 | */ |
@@ -77,7 +79,8 @@ struct ClientHandle { | |||
77 | /** | 79 | /** |
78 | * Context for autoconfiguration operations. | 80 | * Context for autoconfiguration operations. |
79 | */ | 81 | */ |
80 | struct AutoconfigContext { | 82 | struct AutoconfigContext |
83 | { | ||
81 | /** | 84 | /** |
82 | * Kept in a DLL. | 85 | * Kept in a DLL. |
83 | */ | 86 | */ |
@@ -167,8 +170,9 @@ static struct GNUNET_STATISTICS_Handle *stats; | |||
167 | * @return #GNUNET_OK if message is well-formed | 170 | * @return #GNUNET_OK if message is well-formed |
168 | */ | 171 | */ |
169 | static int | 172 | static int |
170 | check_autoconfig_request(void *cls, | 173 | check_autoconfig_request (void *cls, |
171 | const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) | 174 | const struct |
175 | GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) | ||
172 | { | 176 | { |
173 | return GNUNET_OK; /* checked later */ | 177 | return GNUNET_OK; /* checked later */ |
174 | } | 178 | } |
@@ -180,13 +184,13 @@ check_autoconfig_request(void *cls, | |||
180 | * @param ac autoconfiguration to terminate activities for | 184 | * @param ac autoconfiguration to terminate activities for |
181 | */ | 185 | */ |
182 | static void | 186 | static void |
183 | terminate_ac_activities(struct AutoconfigContext *ac) | 187 | terminate_ac_activities (struct AutoconfigContext *ac) |
184 | { | 188 | { |
185 | if (NULL != ac->timeout_task) | 189 | if (NULL != ac->timeout_task) |
186 | { | 190 | { |
187 | GNUNET_SCHEDULER_cancel(ac->timeout_task); | 191 | GNUNET_SCHEDULER_cancel (ac->timeout_task); |
188 | ac->timeout_task = NULL; | 192 | ac->timeout_task = NULL; |
189 | } | 193 | } |
190 | } | 194 | } |
191 | 195 | ||
192 | 196 | ||
@@ -197,7 +201,7 @@ terminate_ac_activities(struct AutoconfigContext *ac) | |||
197 | * @param cls the `struct AutoconfigContext` to conclude | 201 | * @param cls the `struct AutoconfigContext` to conclude |
198 | */ | 202 | */ |
199 | static void | 203 | static void |
200 | conclude_autoconfig_request(void *cls) | 204 | conclude_autoconfig_request (void *cls) |
201 | { | 205 | { |
202 | struct AutoconfigContext *ac = cls; | 206 | struct AutoconfigContext *ac = cls; |
203 | struct ClientHandle *ch = ac->ch; | 207 | struct ClientHandle *ch = ac->ch; |
@@ -208,34 +212,34 @@ conclude_autoconfig_request(void *cls) | |||
208 | struct GNUNET_CONFIGURATION_Handle *diff; | 212 | struct GNUNET_CONFIGURATION_Handle *diff; |
209 | 213 | ||
210 | ac->timeout_task = NULL; | 214 | ac->timeout_task = NULL; |
211 | terminate_ac_activities(ac); | 215 | terminate_ac_activities (ac); |
212 | 216 | ||
213 | /* Send back response */ | 217 | /* Send back response */ |
214 | diff = GNUNET_CONFIGURATION_get_diff(ac->orig, | 218 | diff = GNUNET_CONFIGURATION_get_diff (ac->orig, |
215 | ac->c); | 219 | ac->c); |
216 | buf = GNUNET_CONFIGURATION_serialize(diff, | 220 | buf = GNUNET_CONFIGURATION_serialize (diff, |
217 | &c_size); | 221 | &c_size); |
218 | GNUNET_CONFIGURATION_destroy(diff); | 222 | GNUNET_CONFIGURATION_destroy (diff); |
219 | env = GNUNET_MQ_msg_extra(arm, | 223 | env = GNUNET_MQ_msg_extra (arm, |
220 | c_size, | 224 | c_size, |
221 | GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT); | 225 | GNUNET_MESSAGE_TYPE_NAT_AUTO_CFG_RESULT); |
222 | arm->status_code = htonl((uint32_t)ac->status_code); | 226 | arm->status_code = htonl ((uint32_t) ac->status_code); |
223 | arm->type = htonl((uint32_t)ac->type); | 227 | arm->type = htonl ((uint32_t) ac->type); |
224 | GNUNET_memcpy(&arm[1], | 228 | GNUNET_memcpy (&arm[1], |
225 | buf, | 229 | buf, |
226 | c_size); | 230 | c_size); |
227 | GNUNET_free(buf); | 231 | GNUNET_free (buf); |
228 | GNUNET_MQ_send(ch->mq, | 232 | GNUNET_MQ_send (ch->mq, |
229 | env); | 233 | env); |
230 | 234 | ||
231 | /* clean up */ | 235 | /* clean up */ |
232 | GNUNET_CONFIGURATION_destroy(ac->orig); | 236 | GNUNET_CONFIGURATION_destroy (ac->orig); |
233 | GNUNET_CONFIGURATION_destroy(ac->c); | 237 | GNUNET_CONFIGURATION_destroy (ac->c); |
234 | GNUNET_CONTAINER_DLL_remove(ac_head, | 238 | GNUNET_CONTAINER_DLL_remove (ac_head, |
235 | ac_tail, | 239 | ac_tail, |
236 | ac); | 240 | ac); |
237 | GNUNET_free(ac); | 241 | GNUNET_free (ac); |
238 | GNUNET_SERVICE_client_continue(ch->client); | 242 | GNUNET_SERVICE_client_continue (ch->client); |
239 | } | 243 | } |
240 | 244 | ||
241 | 245 | ||
@@ -246,12 +250,12 @@ conclude_autoconfig_request(void *cls) | |||
246 | * @param ac autoconfiguation context to check | 250 | * @param ac autoconfiguation context to check |
247 | */ | 251 | */ |
248 | static void | 252 | static void |
249 | check_autoconfig_finished(struct AutoconfigContext *ac) | 253 | check_autoconfig_finished (struct AutoconfigContext *ac) |
250 | { | 254 | { |
251 | GNUNET_SCHEDULER_cancel(ac->timeout_task); | 255 | GNUNET_SCHEDULER_cancel (ac->timeout_task); |
252 | ac->timeout_task | 256 | ac->timeout_task |
253 | = GNUNET_SCHEDULER_add_now(&conclude_autoconfig_request, | 257 | = GNUNET_SCHEDULER_add_now (&conclude_autoconfig_request, |
254 | ac); | 258 | ac); |
255 | } | 259 | } |
256 | 260 | ||
257 | 261 | ||
@@ -261,28 +265,28 @@ check_autoconfig_finished(struct AutoconfigContext *ac) | |||
261 | * @param ac autoconfiguration to update | 265 | * @param ac autoconfiguration to update |
262 | */ | 266 | */ |
263 | static void | 267 | static void |
264 | update_enable_upnpc_option(struct AutoconfigContext *ac) | 268 | update_enable_upnpc_option (struct AutoconfigContext *ac) |
265 | { | 269 | { |
266 | switch (ac->enable_upnpc) | 270 | switch (ac->enable_upnpc) |
267 | { | 271 | { |
268 | case GNUNET_YES: | 272 | case GNUNET_YES: |
269 | GNUNET_CONFIGURATION_set_value_string(ac->c, | 273 | GNUNET_CONFIGURATION_set_value_string (ac->c, |
270 | "NAT", | 274 | "NAT", |
271 | "ENABLE_UPNP", | 275 | "ENABLE_UPNP", |
272 | "YES"); | 276 | "YES"); |
273 | break; | 277 | break; |
274 | 278 | ||
275 | case GNUNET_NO: | 279 | case GNUNET_NO: |
276 | GNUNET_CONFIGURATION_set_value_string(ac->c, | 280 | GNUNET_CONFIGURATION_set_value_string (ac->c, |
277 | "NAT", | 281 | "NAT", |
278 | "ENABLE_UPNP", | 282 | "ENABLE_UPNP", |
279 | "NO"); | 283 | "NO"); |
280 | break; | 284 | break; |
281 | 285 | ||
282 | case GNUNET_SYSERR: | 286 | case GNUNET_SYSERR: |
283 | /* We are unsure, do not change option */ | 287 | /* We are unsure, do not change option */ |
284 | break; | 288 | break; |
285 | } | 289 | } |
286 | } | 290 | } |
287 | 291 | ||
288 | 292 | ||
@@ -294,62 +298,63 @@ update_enable_upnpc_option(struct AutoconfigContext *ac) | |||
294 | * @param message the message received | 298 | * @param message the message received |
295 | */ | 299 | */ |
296 | static void | 300 | static void |
297 | handle_autoconfig_request(void *cls, | 301 | handle_autoconfig_request (void *cls, |
298 | const struct GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) | 302 | const struct |
303 | GNUNET_NAT_AUTO_AutoconfigRequestMessage *message) | ||
299 | { | 304 | { |
300 | struct ClientHandle *ch = cls; | 305 | struct ClientHandle *ch = cls; |
301 | size_t left = ntohs(message->header.size) - sizeof(*message); | 306 | size_t left = ntohs (message->header.size) - sizeof(*message); |
302 | struct AutoconfigContext *ac; | 307 | struct AutoconfigContext *ac; |
303 | 308 | ||
304 | ac = GNUNET_new(struct AutoconfigContext); | 309 | ac = GNUNET_new (struct AutoconfigContext); |
305 | ac->status_code = GNUNET_NAT_ERROR_SUCCESS; | 310 | ac->status_code = GNUNET_NAT_ERROR_SUCCESS; |
306 | ac->ch = ch; | 311 | ac->ch = ch; |
307 | ac->c = GNUNET_CONFIGURATION_create(); | 312 | ac->c = GNUNET_CONFIGURATION_create (); |
308 | if (GNUNET_OK != | 313 | if (GNUNET_OK != |
309 | GNUNET_CONFIGURATION_deserialize(ac->c, | 314 | GNUNET_CONFIGURATION_deserialize (ac->c, |
310 | (const char *)&message[1], | 315 | (const char *) &message[1], |
311 | left, | 316 | left, |
312 | NULL)) | 317 | NULL)) |
313 | { | 318 | { |
314 | GNUNET_break(0); | 319 | GNUNET_break (0); |
315 | GNUNET_SERVICE_client_drop(ch->client); | 320 | GNUNET_SERVICE_client_drop (ch->client); |
316 | GNUNET_CONFIGURATION_destroy(ac->c); | 321 | GNUNET_CONFIGURATION_destroy (ac->c); |
317 | GNUNET_free(ac); | 322 | GNUNET_free (ac); |
318 | return; | 323 | return; |
319 | } | 324 | } |
320 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 325 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
321 | "Received REQUEST_AUTO_CONFIG message from client\n"); | 326 | "Received REQUEST_AUTO_CONFIG message from client\n"); |
322 | 327 | ||
323 | GNUNET_CONTAINER_DLL_insert(ac_head, | 328 | GNUNET_CONTAINER_DLL_insert (ac_head, |
324 | ac_tail, | 329 | ac_tail, |
325 | ac); | 330 | ac); |
326 | ac->orig | 331 | ac->orig |
327 | = GNUNET_CONFIGURATION_dup(ac->c); | 332 | = GNUNET_CONFIGURATION_dup (ac->c); |
328 | ac->timeout_task | 333 | ac->timeout_task |
329 | = GNUNET_SCHEDULER_add_delayed(AUTOCONFIG_TIMEOUT, | 334 | = GNUNET_SCHEDULER_add_delayed (AUTOCONFIG_TIMEOUT, |
330 | &conclude_autoconfig_request, | 335 | &conclude_autoconfig_request, |
331 | ac); | 336 | ac); |
332 | ac->enable_upnpc = GNUNET_SYSERR; /* undecided */ | 337 | ac->enable_upnpc = GNUNET_SYSERR; /* undecided */ |
333 | 338 | ||
334 | /* Probe for upnpc */ | 339 | /* Probe for upnpc */ |
335 | if (GNUNET_SYSERR == | 340 | if (GNUNET_SYSERR == |
336 | GNUNET_OS_check_helper_binary("upnpc", | 341 | GNUNET_OS_check_helper_binary ("upnpc", |
337 | GNUNET_NO, | 342 | GNUNET_NO, |
338 | NULL)) | 343 | NULL)) |
339 | { | 344 | { |
340 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, | 345 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
341 | _("UPnP client `upnpc` command not found, disabling UPnP\n")); | 346 | _ ("UPnP client `upnpc` command not found, disabling UPnP\n")); |
342 | ac->enable_upnpc = GNUNET_NO; | 347 | ac->enable_upnpc = GNUNET_NO; |
343 | } | 348 | } |
344 | else | 349 | else |
345 | { | 350 | { |
346 | /* We might at some point be behind NAT, try upnpc */ | 351 | /* We might at some point be behind NAT, try upnpc */ |
347 | ac->enable_upnpc = GNUNET_YES; | 352 | ac->enable_upnpc = GNUNET_YES; |
348 | } | 353 | } |
349 | update_enable_upnpc_option(ac); | 354 | update_enable_upnpc_option (ac); |
350 | 355 | ||
351 | /* Finally, check if we are already done */ | 356 | /* Finally, check if we are already done */ |
352 | check_autoconfig_finished(ac); | 357 | check_autoconfig_finished (ac); |
353 | } | 358 | } |
354 | 359 | ||
355 | 360 | ||
@@ -359,24 +364,24 @@ handle_autoconfig_request(void *cls, | |||
359 | * @param cls unused | 364 | * @param cls unused |
360 | */ | 365 | */ |
361 | static void | 366 | static void |
362 | shutdown_task(void *cls) | 367 | shutdown_task (void *cls) |
363 | { | 368 | { |
364 | struct AutoconfigContext *ac; | 369 | struct AutoconfigContext *ac; |
365 | 370 | ||
366 | while (NULL != (ac = ac_head)) | 371 | while (NULL != (ac = ac_head)) |
367 | { | 372 | { |
368 | GNUNET_CONTAINER_DLL_remove(ac_head, | 373 | GNUNET_CONTAINER_DLL_remove (ac_head, |
369 | ac_tail, | 374 | ac_tail, |
370 | ac); | 375 | ac); |
371 | terminate_ac_activities(ac); | 376 | terminate_ac_activities (ac); |
372 | GNUNET_free(ac); | 377 | GNUNET_free (ac); |
373 | } | 378 | } |
374 | if (NULL != stats) | 379 | if (NULL != stats) |
375 | { | 380 | { |
376 | GNUNET_STATISTICS_destroy(stats, | 381 | GNUNET_STATISTICS_destroy (stats, |
377 | GNUNET_NO); | 382 | GNUNET_NO); |
378 | stats = NULL; | 383 | stats = NULL; |
379 | } | 384 | } |
380 | } | 385 | } |
381 | 386 | ||
382 | 387 | ||
@@ -388,15 +393,15 @@ shutdown_task(void *cls) | |||
388 | * @param service the initialized service | 393 | * @param service the initialized service |
389 | */ | 394 | */ |
390 | static void | 395 | static void |
391 | run(void *cls, | 396 | run (void *cls, |
392 | const struct GNUNET_CONFIGURATION_Handle *c, | 397 | const struct GNUNET_CONFIGURATION_Handle *c, |
393 | struct GNUNET_SERVICE_Handle *service) | 398 | struct GNUNET_SERVICE_Handle *service) |
394 | { | 399 | { |
395 | cfg = c; | 400 | cfg = c; |
396 | GNUNET_SCHEDULER_add_shutdown(&shutdown_task, | 401 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, |
397 | NULL); | 402 | NULL); |
398 | stats = GNUNET_STATISTICS_create("nat-auto", | 403 | stats = GNUNET_STATISTICS_create ("nat-auto", |
399 | cfg); | 404 | cfg); |
400 | } | 405 | } |
401 | 406 | ||
402 | 407 | ||
@@ -409,18 +414,18 @@ run(void *cls, | |||
409 | * @return a `struct ClientHandle` | 414 | * @return a `struct ClientHandle` |
410 | */ | 415 | */ |
411 | static void * | 416 | static void * |
412 | client_connect_cb(void *cls, | 417 | client_connect_cb (void *cls, |
413 | struct GNUNET_SERVICE_Client *c, | 418 | struct GNUNET_SERVICE_Client *c, |
414 | struct GNUNET_MQ_Handle *mq) | 419 | struct GNUNET_MQ_Handle *mq) |
415 | { | 420 | { |
416 | struct ClientHandle *ch; | 421 | struct ClientHandle *ch; |
417 | 422 | ||
418 | ch = GNUNET_new(struct ClientHandle); | 423 | ch = GNUNET_new (struct ClientHandle); |
419 | ch->mq = mq; | 424 | ch->mq = mq; |
420 | ch->client = c; | 425 | ch->client = c; |
421 | GNUNET_CONTAINER_DLL_insert(ch_head, | 426 | GNUNET_CONTAINER_DLL_insert (ch_head, |
422 | ch_tail, | 427 | ch_tail, |
423 | ch); | 428 | ch); |
424 | return ch; | 429 | return ch; |
425 | } | 430 | } |
426 | 431 | ||
@@ -433,16 +438,16 @@ client_connect_cb(void *cls, | |||
433 | * @param internal_cls a `struct ClientHandle *` | 438 | * @param internal_cls a `struct ClientHandle *` |
434 | */ | 439 | */ |
435 | static void | 440 | static void |
436 | client_disconnect_cb(void *cls, | 441 | client_disconnect_cb (void *cls, |
437 | struct GNUNET_SERVICE_Client *c, | 442 | struct GNUNET_SERVICE_Client *c, |
438 | void *internal_cls) | 443 | void *internal_cls) |
439 | { | 444 | { |
440 | struct ClientHandle *ch = internal_cls; | 445 | struct ClientHandle *ch = internal_cls; |
441 | 446 | ||
442 | GNUNET_CONTAINER_DLL_remove(ch_head, | 447 | GNUNET_CONTAINER_DLL_remove (ch_head, |
443 | ch_tail, | 448 | ch_tail, |
444 | ch); | 449 | ch); |
445 | GNUNET_free(ch); | 450 | GNUNET_free (ch); |
446 | } | 451 | } |
447 | 452 | ||
448 | 453 | ||
@@ -456,11 +461,11 @@ GNUNET_SERVICE_MAIN | |||
456 | &client_connect_cb, | 461 | &client_connect_cb, |
457 | &client_disconnect_cb, | 462 | &client_disconnect_cb, |
458 | NULL, | 463 | NULL, |
459 | GNUNET_MQ_hd_var_size(autoconfig_request, | 464 | GNUNET_MQ_hd_var_size (autoconfig_request, |
460 | GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG, | 465 | GNUNET_MESSAGE_TYPE_NAT_AUTO_REQUEST_CFG, |
461 | struct GNUNET_NAT_AUTO_AutoconfigRequestMessage, | 466 | struct GNUNET_NAT_AUTO_AutoconfigRequestMessage, |
462 | NULL), | 467 | NULL), |
463 | GNUNET_MQ_handler_end()); | 468 | GNUNET_MQ_handler_end ()); |
464 | 469 | ||
465 | 470 | ||
466 | #if defined(LINUX) && defined(__GLIBC__) | 471 | #if defined(LINUX) && defined(__GLIBC__) |
@@ -470,11 +475,11 @@ GNUNET_SERVICE_MAIN | |||
470 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. | 475 | * MINIMIZE heap size (way below 128k) since this process doesn't need much. |
471 | */ | 476 | */ |
472 | void __attribute__ ((constructor)) | 477 | void __attribute__ ((constructor)) |
473 | GNUNET_ARM_memory_init() | 478 | GNUNET_ARM_memory_init () |
474 | { | 479 | { |
475 | mallopt(M_TRIM_THRESHOLD, 4 * 1024); | 480 | mallopt (M_TRIM_THRESHOLD, 4 * 1024); |
476 | mallopt(M_TOP_PAD, 1 * 1024); | 481 | mallopt (M_TOP_PAD, 1 * 1024); |
477 | malloc_trim(0); | 482 | malloc_trim (0); |
478 | } | 483 | } |
479 | #endif | 484 | #endif |
480 | 485 | ||