summaryrefslogtreecommitdiff
path: root/src/dns/gnunet-service-dns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dns/gnunet-service-dns.c')
-rw-r--r--src/dns/gnunet-service-dns.c1192
1 files changed, 607 insertions, 585 deletions
diff --git a/src/dns/gnunet-service-dns.c b/src/dns/gnunet-service-dns.c
index 9a2293b2d..e1cb7a6ff 100644
--- a/src/dns/gnunet-service-dns.c
+++ b/src/dns/gnunet-service-dns.c
@@ -59,13 +59,14 @@
59 * Generic logging shorthand 59 * Generic logging shorthand
60 */ 60 */
61#define LOG(kind, ...) \ 61#define LOG(kind, ...) \
62 GNUNET_log_from(kind, "dns", __VA_ARGS__); 62 GNUNET_log_from (kind, "dns", __VA_ARGS__);
63 63
64 64
65/** 65/**
66 * Phases each request goes through. 66 * Phases each request goes through.
67 */ 67 */
68enum RequestPhase { 68enum RequestPhase
69{
69 /** 70 /**
70 * Request has just been received. 71 * Request has just been received.
71 */ 72 */
@@ -111,7 +112,8 @@ enum RequestPhase {
111/** 112/**
112 * Entry we keep for each client. 113 * Entry we keep for each client.
113 */ 114 */
114struct ClientRecord { 115struct ClientRecord
116{
115 /** 117 /**
116 * Kept in doubly-linked list. 118 * Kept in doubly-linked list.
117 */ 119 */
@@ -142,7 +144,8 @@ struct ClientRecord {
142/** 144/**
143 * Entry we keep for each active request. 145 * Entry we keep for each active request.
144 */ 146 */
145struct RequestRecord { 147struct RequestRecord
148{
146 /** 149 /**
147 * List of clients that still need to see this request (each entry 150 * List of clients that still need to see this request (each entry
148 * is set to NULL when the client is done). 151 * is set to NULL when the client is done).
@@ -252,14 +255,14 @@ static struct GNUNET_DNSSTUB_Context *dnsstub;
252 * @param rr request to clean up 255 * @param rr request to clean up
253 */ 256 */
254static void 257static void
255cleanup_rr(struct RequestRecord *rr) 258cleanup_rr (struct RequestRecord *rr)
256{ 259{
257 GNUNET_free_non_null(rr->payload); 260 GNUNET_free_non_null (rr->payload);
258 rr->payload = NULL; 261 rr->payload = NULL;
259 rr->payload_length = 0; 262 rr->payload_length = 0;
260 GNUNET_array_grow(rr->client_wait_list, 263 GNUNET_array_grow (rr->client_wait_list,
261 rr->client_wait_list_length, 264 rr->client_wait_list_length,
262 0); 265 0);
263} 266}
264 267
265 268
@@ -269,28 +272,28 @@ cleanup_rr(struct RequestRecord *rr)
269 * @param cls unused 272 * @param cls unused
270 */ 273 */
271static void 274static void
272cleanup_task(void *cls GNUNET_UNUSED) 275cleanup_task (void *cls GNUNET_UNUSED)
273{ 276{
274 if (NULL != hijacker) 277 if (NULL != hijacker)
275 { 278 {
276 GNUNET_HELPER_stop(hijacker, GNUNET_NO); 279 GNUNET_HELPER_stop (hijacker, GNUNET_NO);
277 hijacker = NULL; 280 hijacker = NULL;
278 } 281 }
279 for (unsigned int i = 0; i < 8; i++) 282 for (unsigned int i = 0; i < 8; i++)
280 GNUNET_free_non_null(helper_argv[i]); 283 GNUNET_free_non_null (helper_argv[i]);
281 for (unsigned int i = 0; i <= UINT16_MAX; i++) 284 for (unsigned int i = 0; i <= UINT16_MAX; i++)
282 cleanup_rr(&requests[i]); 285 cleanup_rr (&requests[i]);
283 if (NULL != stats) 286 if (NULL != stats)
284 { 287 {
285 GNUNET_STATISTICS_destroy(stats, 288 GNUNET_STATISTICS_destroy (stats,
286 GNUNET_NO); 289 GNUNET_NO);
287 stats = NULL; 290 stats = NULL;
288 } 291 }
289 if (NULL != dnsstub) 292 if (NULL != dnsstub)
290 { 293 {
291 GNUNET_DNSSTUB_stop(dnsstub); 294 GNUNET_DNSSTUB_stop (dnsstub);
292 dnsstub = NULL; 295 dnsstub = NULL;
293 } 296 }
294} 297}
295 298
296 299
@@ -300,56 +303,56 @@ cleanup_task(void *cls GNUNET_UNUSED)
300 * @param rr request send to the network or just clean up. 303 * @param rr request send to the network or just clean up.
301 */ 304 */
302static void 305static void
303request_done(struct RequestRecord *rr) 306request_done (struct RequestRecord *rr)
304{ 307{
305 struct GNUNET_MessageHeader *hdr; 308 struct GNUNET_MessageHeader *hdr;
306 size_t reply_len; 309 size_t reply_len;
307 uint16_t source_port; 310 uint16_t source_port;
308 uint16_t destination_port; 311 uint16_t destination_port;
309 312
310 GNUNET_array_grow(rr->client_wait_list, 313 GNUNET_array_grow (rr->client_wait_list,
311 rr->client_wait_list_length, 314 rr->client_wait_list_length,
312 0); 315 0);
313 if (RP_RESPONSE_MONITOR != rr->phase) 316 if (RP_RESPONSE_MONITOR != rr->phase)
314 { 317 {
315 /* no response, drop */ 318 /* no response, drop */
316 LOG(GNUNET_ERROR_TYPE_DEBUG, 319 LOG (GNUNET_ERROR_TYPE_DEBUG,
317 "Got no response for request %llu, dropping\n", 320 "Got no response for request %llu, dropping\n",
318 (unsigned long long)rr->request_id); 321 (unsigned long long) rr->request_id);
319 cleanup_rr(rr); 322 cleanup_rr (rr);
320 return; 323 return;
321 } 324 }
322 325
323 LOG(GNUNET_ERROR_TYPE_DEBUG, 326 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "Transmitting response for request %llu\n", 327 "Transmitting response for request %llu\n",
325 (unsigned long long)rr->request_id); 328 (unsigned long long) rr->request_id);
326 /* send response via hijacker */ 329 /* send response via hijacker */
327 reply_len = sizeof(struct GNUNET_MessageHeader); 330 reply_len = sizeof(struct GNUNET_MessageHeader);
328 reply_len += sizeof(struct GNUNET_TUN_Layer2PacketHeader); 331 reply_len += sizeof(struct GNUNET_TUN_Layer2PacketHeader);
329 switch (rr->src_addr.ss_family) 332 switch (rr->src_addr.ss_family)
330 { 333 {
331 case AF_INET: 334 case AF_INET:
332 reply_len += sizeof(struct GNUNET_TUN_IPv4Header); 335 reply_len += sizeof(struct GNUNET_TUN_IPv4Header);
333 break; 336 break;
334 337
335 case AF_INET6: 338 case AF_INET6:
336 reply_len += sizeof(struct GNUNET_TUN_IPv6Header); 339 reply_len += sizeof(struct GNUNET_TUN_IPv6Header);
337 break; 340 break;
338 341
339 default: 342 default:
340 GNUNET_break(0); 343 GNUNET_break (0);
341 cleanup_rr(rr); 344 cleanup_rr (rr);
342 return; 345 return;
343 } 346 }
344 reply_len += sizeof(struct GNUNET_TUN_UdpHeader); 347 reply_len += sizeof(struct GNUNET_TUN_UdpHeader);
345 reply_len += rr->payload_length; 348 reply_len += rr->payload_length;
346 if (reply_len >= GNUNET_MAX_MESSAGE_SIZE) 349 if (reply_len >= GNUNET_MAX_MESSAGE_SIZE)
347 { 350 {
348 /* response too big, drop */ 351 /* response too big, drop */
349 GNUNET_break(0); /* how can this be? */ 352 GNUNET_break (0); /* how can this be? */
350 cleanup_rr(rr); 353 cleanup_rr (rr);
351 return; 354 return;
352 } 355 }
353 { 356 {
354 char buf[reply_len] GNUNET_ALIGN; 357 char buf[reply_len] GNUNET_ALIGN;
355 size_t off; 358 size_t off;
@@ -357,70 +360,72 @@ request_done(struct RequestRecord *rr)
357 struct GNUNET_TUN_IPv6Header ip6; 360 struct GNUNET_TUN_IPv6Header ip6;
358 361
359 /* first, GNUnet message header */ 362 /* first, GNUnet message header */
360 hdr = (struct GNUNET_MessageHeader*)buf; 363 hdr = (struct GNUNET_MessageHeader*) buf;
361 hdr->type = htons(GNUNET_MESSAGE_TYPE_DNS_HELPER); 364 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
362 hdr->size = htons((uint16_t)reply_len); 365 hdr->size = htons ((uint16_t) reply_len);
363 off = sizeof(struct GNUNET_MessageHeader); 366 off = sizeof(struct GNUNET_MessageHeader);
364 367
365 /* first, TUN header */ 368 /* first, TUN header */
366 { 369 {
367 struct GNUNET_TUN_Layer2PacketHeader tun; 370 struct GNUNET_TUN_Layer2PacketHeader tun;
368 371
369 tun.flags = htons(0); 372 tun.flags = htons (0);
370 if (rr->src_addr.ss_family == AF_INET) 373 if (rr->src_addr.ss_family == AF_INET)
371 tun.proto = htons(ETH_P_IPV4); 374 tun.proto = htons (ETH_P_IPV4);
372 else 375 else
373 tun.proto = htons(ETH_P_IPV6); 376 tun.proto = htons (ETH_P_IPV6);
374 GNUNET_memcpy(&buf[off], 377 GNUNET_memcpy (&buf[off],
375 &tun, 378 &tun,
376 sizeof(struct GNUNET_TUN_Layer2PacketHeader)); 379 sizeof(struct GNUNET_TUN_Layer2PacketHeader));
377 off += sizeof(struct GNUNET_TUN_Layer2PacketHeader); 380 off += sizeof(struct GNUNET_TUN_Layer2PacketHeader);
378 } 381 }
379 382
380 /* now IP header */ 383 /* now IP header */
381 switch (rr->src_addr.ss_family) 384 switch (rr->src_addr.ss_family)
385 {
386 case AF_INET:
382 { 387 {
383 case AF_INET: 388 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr;
384 { 389 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr;
385 struct sockaddr_in *src = (struct sockaddr_in *)&rr->src_addr;
386 struct sockaddr_in *dst = (struct sockaddr_in *)&rr->dst_addr;
387 390
388 source_port = dst->sin_port; 391 source_port = dst->sin_port;
389 destination_port = src->sin_port; 392 destination_port = src->sin_port;
390 GNUNET_TUN_initialize_ipv4_header(&ip4, 393 GNUNET_TUN_initialize_ipv4_header (&ip4,
391 IPPROTO_UDP, 394 IPPROTO_UDP,
392 reply_len - off - sizeof(struct GNUNET_TUN_IPv4Header), 395 reply_len - off - sizeof(struct
393 &dst->sin_addr, 396 GNUNET_TUN_IPv4Header),
394 &src->sin_addr); 397 &dst->sin_addr,
395 GNUNET_memcpy(&buf[off], 398 &src->sin_addr);
396 &ip4, 399 GNUNET_memcpy (&buf[off],
397 sizeof(ip4)); 400 &ip4,
401 sizeof(ip4));
398 off += sizeof(ip4); 402 off += sizeof(ip4);
399 } 403 }
400 break; 404 break;
401 405
402 case AF_INET6: 406 case AF_INET6:
403 { 407 {
404 struct sockaddr_in6 *src = (struct sockaddr_in6 *)&rr->src_addr; 408 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr;
405 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&rr->dst_addr; 409 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr;
406 410
407 source_port = dst->sin6_port; 411 source_port = dst->sin6_port;
408 destination_port = src->sin6_port; 412 destination_port = src->sin6_port;
409 GNUNET_TUN_initialize_ipv6_header(&ip6, 413 GNUNET_TUN_initialize_ipv6_header (&ip6,
410 IPPROTO_UDP, 414 IPPROTO_UDP,
411 reply_len - off - sizeof(struct GNUNET_TUN_IPv6Header), 415 reply_len - off - sizeof(struct
412 &dst->sin6_addr, 416 GNUNET_TUN_IPv6Header),
413 &src->sin6_addr); 417 &dst->sin6_addr,
414 GNUNET_memcpy(&buf[off], 418 &src->sin6_addr);
415 &ip6, 419 GNUNET_memcpy (&buf[off],
416 sizeof(ip6)); 420 &ip6,
421 sizeof(ip6));
417 off += sizeof(ip6); 422 off += sizeof(ip6);
418 } 423 }
419 break; 424 break;
420 425
421 default: 426 default:
422 GNUNET_assert(0); 427 GNUNET_assert (0);
423 } 428 }
424 429
425 /* now UDP header */ 430 /* now UDP header */
426 { 431 {
@@ -428,40 +433,41 @@ request_done(struct RequestRecord *rr)
428 433
429 udp.source_port = source_port; 434 udp.source_port = source_port;
430 udp.destination_port = destination_port; 435 udp.destination_port = destination_port;
431 udp.len = htons(reply_len - off); 436 udp.len = htons (reply_len - off);
432 if (AF_INET == rr->src_addr.ss_family) 437 if (AF_INET == rr->src_addr.ss_family)
433 GNUNET_TUN_calculate_udp4_checksum(&ip4, 438 GNUNET_TUN_calculate_udp4_checksum (&ip4,
434 &udp, 439 &udp,
435 rr->payload, 440 rr->payload,
436 rr->payload_length); 441 rr->payload_length);
437 else 442 else
438 GNUNET_TUN_calculate_udp6_checksum(&ip6, 443 GNUNET_TUN_calculate_udp6_checksum (&ip6,
439 &udp, 444 &udp,
440 rr->payload, 445 rr->payload,
441 rr->payload_length); 446 rr->payload_length);
442 GNUNET_memcpy(&buf[off], 447 GNUNET_memcpy (&buf[off],
443 &udp, 448 &udp,
444 sizeof(udp)); 449 sizeof(udp));
445 off += sizeof(udp); 450 off += sizeof(udp);
446 } 451 }
447 452
448 /* now DNS payload */ 453 /* now DNS payload */
449 { 454 {
450 GNUNET_memcpy(&buf[off], rr->payload, rr->payload_length); 455 GNUNET_memcpy (&buf[off], rr->payload, rr->payload_length);
451 off += rr->payload_length; 456 off += rr->payload_length;
452 } 457 }
453 /* final checks & sending */ 458 /* final checks & sending */
454 GNUNET_assert(off == reply_len); 459 GNUNET_assert (off == reply_len);
455 (void)GNUNET_HELPER_send(hijacker, 460 (void) GNUNET_HELPER_send (hijacker,
456 hdr, 461 hdr,
457 GNUNET_YES, 462 GNUNET_YES,
458 NULL, NULL); 463 NULL, NULL);
459 GNUNET_STATISTICS_update(stats, 464 GNUNET_STATISTICS_update (stats,
460 gettext_noop("# DNS requests answered via TUN interface"), 465 gettext_noop (
461 1, GNUNET_NO); 466 "# DNS requests answered via TUN interface"),
467 1, GNUNET_NO);
462 } 468 }
463 /* clean up, we're done */ 469 /* clean up, we're done */
464 cleanup_rr(rr); 470 cleanup_rr (rr);
465} 471}
466 472
467 473
@@ -473,31 +479,32 @@ request_done(struct RequestRecord *rr)
473 * @param cr client to send the response to 479 * @param cr client to send the response to
474 */ 480 */
475static void 481static void
476send_request_to_client(struct RequestRecord *rr, 482send_request_to_client (struct RequestRecord *rr,
477 struct ClientRecord *cr) 483 struct ClientRecord *cr)
478{ 484{
479 struct GNUNET_MQ_Envelope *env; 485 struct GNUNET_MQ_Envelope *env;
480 struct GNUNET_DNS_Request *req; 486 struct GNUNET_DNS_Request *req;
481 487
482 if (sizeof(struct GNUNET_DNS_Request) + rr->payload_length >= GNUNET_MAX_MESSAGE_SIZE) 488 if (sizeof(struct GNUNET_DNS_Request) + rr->payload_length >=
483 { 489 GNUNET_MAX_MESSAGE_SIZE)
484 GNUNET_break(0); 490 {
485 cleanup_rr(rr); 491 GNUNET_break (0);
486 return; 492 cleanup_rr (rr);
487 } 493 return;
488 LOG(GNUNET_ERROR_TYPE_DEBUG, 494 }
489 "Sending information about request %llu to local client\n", 495 LOG (GNUNET_ERROR_TYPE_DEBUG,
490 (unsigned long long)rr->request_id); 496 "Sending information about request %llu to local client\n",
491 env = GNUNET_MQ_msg_extra(req, 497 (unsigned long long) rr->request_id);
492 rr->payload_length, 498 env = GNUNET_MQ_msg_extra (req,
493 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST); 499 rr->payload_length,
494 req->reserved = htonl(0); 500 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
501 req->reserved = htonl (0);
495 req->request_id = rr->request_id; 502 req->request_id = rr->request_id;
496 GNUNET_memcpy(&req[1], 503 GNUNET_memcpy (&req[1],
497 rr->payload, 504 rr->payload,
498 rr->payload_length); 505 rr->payload_length);
499 GNUNET_MQ_send(cr->mq, 506 GNUNET_MQ_send (cr->mq,
500 env); 507 env);
501} 508}
502 509
503 510
@@ -510,9 +517,9 @@ send_request_to_client(struct RequestRecord *rr,
510 * @param r number of bytes in dns 517 * @param r number of bytes in dns
511 */ 518 */
512static void 519static void
513process_dns_result(void *cls, 520process_dns_result (void *cls,
514 const struct GNUNET_TUN_DnsHeader *dns, 521 const struct GNUNET_TUN_DnsHeader *dns,
515 size_t r); 522 size_t r);
516 523
517 524
518/** 525/**
@@ -522,138 +529,139 @@ process_dns_result(void *cls,
522 * @param rr request to process further 529 * @param rr request to process further
523 */ 530 */
524static void 531static void
525next_phase(struct RequestRecord *rr) 532next_phase (struct RequestRecord *rr)
526{ 533{
527 struct ClientRecord *cr; 534 struct ClientRecord *cr;
528 int nz; 535 int nz;
529 536
530 if (rr->phase == RP_DROP) 537 if (rr->phase == RP_DROP)
531 { 538 {
532 cleanup_rr(rr); 539 cleanup_rr (rr);
533 return; 540 return;
534 } 541 }
535 nz = -1; 542 nz = -1;
536 for (unsigned int j = 0; j < rr->client_wait_list_length; j++) 543 for (unsigned int j = 0; j < rr->client_wait_list_length; j++)
544 {
545 if (NULL != rr->client_wait_list[j])
537 { 546 {
538 if (NULL != rr->client_wait_list[j]) 547 nz = (int) j;
539 { 548 break;
540 nz = (int)j;
541 break;
542 }
543 } 549 }
550 }
544 if (-1 != nz) 551 if (-1 != nz)
545 { 552 {
546 send_request_to_client(rr, 553 send_request_to_client (rr,
547 rr->client_wait_list[nz]); 554 rr->client_wait_list[nz]);
548 return; 555 return;
549 } 556 }
550 /* done with current phase, advance! */ 557 /* done with current phase, advance! */
551 LOG(GNUNET_ERROR_TYPE_DEBUG, 558 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Request %llu now in phase %d\n", 559 "Request %llu now in phase %d\n",
553 rr->request_id, 560 rr->request_id,
554 rr->phase); 561 rr->phase);
555 switch (rr->phase) 562 switch (rr->phase)
563 {
564 case RP_INIT:
565 rr->phase = RP_REQUEST_MONITOR;
566 for (cr = clients_head; NULL != cr; cr = cr->next)
556 { 567 {
557 case RP_INIT: 568 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
558 rr->phase = RP_REQUEST_MONITOR; 569 GNUNET_array_append (rr->client_wait_list,
559 for (cr = clients_head; NULL != cr; cr = cr->next) 570 rr->client_wait_list_length,
560 { 571 cr);
561 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR)) 572 }
562 GNUNET_array_append(rr->client_wait_list, 573 next_phase (rr);
563 rr->client_wait_list_length, 574 return;
564 cr);
565 }
566 next_phase(rr);
567 return;
568 575
569 case RP_REQUEST_MONITOR: 576 case RP_REQUEST_MONITOR:
570 rr->phase = RP_QUERY; 577 rr->phase = RP_QUERY;
571 for (cr = clients_head; NULL != cr; cr = cr->next) 578 for (cr = clients_head; NULL != cr; cr = cr->next)
572 { 579 {
573 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION)) 580 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
574 GNUNET_array_append(rr->client_wait_list, 581 GNUNET_array_append (rr->client_wait_list,
575 rr->client_wait_list_length, 582 rr->client_wait_list_length,
576 cr); 583 cr);
577 } 584 }
578 next_phase(rr); 585 next_phase (rr);
579 return; 586 return;
580 587
581 case RP_QUERY: 588 case RP_QUERY:
582#if 0 589#if 0
583 /* TODO: optionally, use this to forward DNS requests to the 590 /* TODO: optionally, use this to forward DNS requests to the
584 * original* DNS server instead of the one we have configured... 591 * original* DNS server instead of the one we have configured...
585 (but then we need to create a fresh dnsstub for each request 592 (but then we need to create a fresh dnsstub for each request
586 * and* manage the timeout) */ 593 * and* manage the timeout) */
587 switch (rr->dst_addr.ss_family) 594 switch (rr->dst_addr.ss_family)
588 { 595 {
589 case AF_INET: 596 case AF_INET:
590 salen = sizeof(struct sockaddr_in); 597 salen = sizeof(struct sockaddr_in);
591 sa = (const struct sockaddr *)&rr->dst_addr; 598 sa = (const struct sockaddr *) &rr->dst_addr;
592 break; 599 break;
593 600
594 case AF_INET6: 601 case AF_INET6:
595 salen = sizeof(struct sockaddr_in6); 602 salen = sizeof(struct sockaddr_in6);
596 sa = (const struct sockaddr *)&rr->dst_addr; 603 sa = (const struct sockaddr *) &rr->dst_addr;
597 break; 604 break;
598 605
599 default: 606 default:
600 GNUNET_assert(0); 607 GNUNET_assert (0);
601 } 608 }
602#endif 609#endif
603 rr->phase = RP_INTERNET_DNS; 610 rr->phase = RP_INTERNET_DNS;
604 rr->rs = GNUNET_DNSSTUB_resolve(dnsstub, 611 rr->rs = GNUNET_DNSSTUB_resolve (dnsstub,
605 rr->payload, 612 rr->payload,
606 rr->payload_length, 613 rr->payload_length,
607 &process_dns_result, 614 &process_dns_result,
608 NULL); 615 NULL);
609 if (NULL == rr->rs) 616 if (NULL == rr->rs)
610 { 617 {
611 GNUNET_STATISTICS_update(stats, 618 GNUNET_STATISTICS_update (stats,
612 gettext_noop("# DNS exit failed (failed to open socket)"), 619 gettext_noop (
613 1, 620 "# DNS exit failed (failed to open socket)"),
614 GNUNET_NO); 621 1,
615 cleanup_rr(rr); 622 GNUNET_NO);
616 return; 623 cleanup_rr (rr);
617 }
618 return; 624 return;
625 }
626 return;
619 627
620 case RP_INTERNET_DNS: 628 case RP_INTERNET_DNS:
621 rr->phase = RP_MODIFY; 629 rr->phase = RP_MODIFY;
622 for (cr = clients_head; NULL != cr; cr = cr->next) 630 for (cr = clients_head; NULL != cr; cr = cr->next)
623 { 631 {
624 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION)) 632 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
625 GNUNET_array_append(rr->client_wait_list, 633 GNUNET_array_append (rr->client_wait_list,
626 rr->client_wait_list_length, 634 rr->client_wait_list_length,
627 cr); 635 cr);
628 } 636 }
629 next_phase(rr); 637 next_phase (rr);
630 return; 638 return;
631 639
632 case RP_MODIFY: 640 case RP_MODIFY:
633 rr->phase = RP_RESPONSE_MONITOR; 641 rr->phase = RP_RESPONSE_MONITOR;
634 for (cr = clients_head; NULL != cr; cr = cr->next) 642 for (cr = clients_head; NULL != cr; cr = cr->next)
635 { 643 {
636 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR)) 644 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
637 GNUNET_array_append(rr->client_wait_list, 645 GNUNET_array_append (rr->client_wait_list,
638 rr->client_wait_list_length, 646 rr->client_wait_list_length,
639 cr); 647 cr);
640 } 648 }
641 next_phase(rr); 649 next_phase (rr);
642 return; 650 return;
643 651
644 case RP_RESPONSE_MONITOR: 652 case RP_RESPONSE_MONITOR:
645 request_done(rr); 653 request_done (rr);
646 break; 654 break;
647 655
648 case RP_DROP: 656 case RP_DROP:
649 cleanup_rr(rr); 657 cleanup_rr (rr);
650 break; 658 break;
651 659
652 default: 660 default:
653 GNUNET_break(0); 661 GNUNET_break (0);
654 cleanup_rr(rr); 662 cleanup_rr (rr);
655 break; 663 break;
656 } 664 }
657} 665}
658 666
659 667
@@ -666,18 +674,18 @@ next_phase(struct RequestRecord *rr)
666 * @return our `struct ClientRecord` 674 * @return our `struct ClientRecord`
667 */ 675 */
668static void * 676static void *
669client_connect_cb(void *cls, 677client_connect_cb (void *cls,
670 struct GNUNET_SERVICE_Client *client, 678 struct GNUNET_SERVICE_Client *client,
671 struct GNUNET_MQ_Handle *mq) 679 struct GNUNET_MQ_Handle *mq)
672{ 680{
673 struct ClientRecord *cr = cls; 681 struct ClientRecord *cr = cls;
674 682
675 cr = GNUNET_new(struct ClientRecord); 683 cr = GNUNET_new (struct ClientRecord);
676 cr->client = client; 684 cr->client = client;
677 cr->mq = mq; 685 cr->mq = mq;
678 GNUNET_CONTAINER_DLL_insert(clients_head, 686 GNUNET_CONTAINER_DLL_insert (clients_head,
679 clients_tail, 687 clients_tail,
680 cr); 688 cr);
681 return cr; 689 return cr;
682} 690}
683 691
@@ -690,31 +698,31 @@ client_connect_cb(void *cls,
690 * @param app_ctx our `struct ClientRecord` 698 * @param app_ctx our `struct ClientRecord`
691 */ 699 */
692static void 700static void
693client_disconnect_cb(void *cls, 701client_disconnect_cb (void *cls,
694 struct GNUNET_SERVICE_Client *client, 702 struct GNUNET_SERVICE_Client *client,
695 void *app_ctx) 703 void *app_ctx)
696{ 704{
697 struct ClientRecord *cr = app_ctx; 705 struct ClientRecord *cr = app_ctx;
698 struct RequestRecord *rr; 706 struct RequestRecord *rr;
699 707
700 GNUNET_CONTAINER_DLL_remove(clients_head, 708 GNUNET_CONTAINER_DLL_remove (clients_head,
701 clients_tail, 709 clients_tail,
702 cr); 710 cr);
703 for (unsigned int i = 0; i < UINT16_MAX; i++) 711 for (unsigned int i = 0; i < UINT16_MAX; i++)
712 {
713 rr = &requests[i];
714 if (0 == rr->client_wait_list_length)
715 continue; /* not in use */
716 for (unsigned int j = 0; j < rr->client_wait_list_length; j++)
704 { 717 {
705 rr = &requests[i]; 718 if (rr->client_wait_list[j] == cr)
706 if (0 == rr->client_wait_list_length) 719 {
707 continue; /* not in use */ 720 rr->client_wait_list[j] = NULL;
708 for (unsigned int j = 0; j < rr->client_wait_list_length; j++) 721 next_phase (rr);
709 { 722 }
710 if (rr->client_wait_list[j] == cr)
711 {
712 rr->client_wait_list[j] = NULL;
713 next_phase(rr);
714 }
715 }
716 } 723 }
717 GNUNET_free(cr); 724 }
725 GNUNET_free (cr);
718} 726}
719 727
720 728
@@ -727,39 +735,40 @@ client_disconnect_cb(void *cls,
727 * @param r number of bytes in dns 735 * @param r number of bytes in dns
728 */ 736 */
729static void 737static void
730process_dns_result(void *cls, 738process_dns_result (void *cls,
731 const struct GNUNET_TUN_DnsHeader *dns, 739 const struct GNUNET_TUN_DnsHeader *dns,
732 size_t r) 740 size_t r)
733{ 741{
734 struct RequestRecord *rr; 742 struct RequestRecord *rr;
735 743
736 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Processing DNS result from stub resolver\n"); 745 "Processing DNS result from stub resolver\n");
738 GNUNET_assert(NULL == cls); 746 GNUNET_assert (NULL == cls);
739 if (NULL == dns) 747 if (NULL == dns)
740 return; /* ignore */ 748 return; /* ignore */
741 749
742 rr = &requests[dns->id]; 750 rr = &requests[dns->id];
743 if (rr->phase != RP_INTERNET_DNS) 751 if (rr->phase != RP_INTERNET_DNS)
744 { 752 {
745 /* unexpected / bogus reply */ 753 /* unexpected / bogus reply */
746 GNUNET_STATISTICS_update(stats, 754 GNUNET_STATISTICS_update (stats,
747 gettext_noop("# External DNS response discarded (no matching request)"), 755 gettext_noop (
748 1, GNUNET_NO); 756 "# External DNS response discarded (no matching request)"),
749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 757 1, GNUNET_NO);
750 "Received DNS reply that does not match any pending request. Dropping.\n"); 758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 return; 759 "Received DNS reply that does not match any pending request. Dropping.\n");
752 } 760 return;
753 LOG(GNUNET_ERROR_TYPE_DEBUG, 761 }
754 "Got a response from the stub resolver for DNS request %llu intercepted locally!\n", 762 LOG (GNUNET_ERROR_TYPE_DEBUG,
755 (unsigned long long)rr->request_id); 763 "Got a response from the stub resolver for DNS request %llu intercepted locally!\n",
756 GNUNET_free_non_null(rr->payload); 764 (unsigned long long) rr->request_id);
757 rr->payload = GNUNET_malloc(r); 765 GNUNET_free_non_null (rr->payload);
758 GNUNET_memcpy(rr->payload, 766 rr->payload = GNUNET_malloc (r);
759 dns, 767 GNUNET_memcpy (rr->payload,
760 r); 768 dns,
769 r);
761 rr->payload_length = r; 770 rr->payload_length = r;
762 next_phase(rr); 771 next_phase (rr);
763} 772}
764 773
765 774
@@ -770,13 +779,13 @@ process_dns_result(void *cls,
770 * @param reg the init message 779 * @param reg the init message
771 */ 780 */
772static void 781static void
773handle_client_init(void *cls, 782handle_client_init (void *cls,
774 const struct GNUNET_DNS_Register *reg) 783 const struct GNUNET_DNS_Register *reg)
775{ 784{
776 struct ClientRecord *cr = cls; 785 struct ClientRecord *cr = cls;
777 786
778 cr->flags = (enum GNUNET_DNS_Flags)ntohl(reg->flags); 787 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
779 GNUNET_SERVICE_client_continue(cr->client); 788 GNUNET_SERVICE_client_continue (cr->client);
780} 789}
781 790
782 791
@@ -788,8 +797,8 @@ handle_client_init(void *cls,
788 * @return #GNUNET_OK (always fine) 797 * @return #GNUNET_OK (always fine)
789 */ 798 */
790static int 799static int
791check_client_response(void *cls, 800check_client_response (void *cls,
792 const struct GNUNET_DNS_Response *resp) 801 const struct GNUNET_DNS_Response *resp)
793{ 802{
794 return GNUNET_OK; /* any payload is acceptable */ 803 return GNUNET_OK; /* any payload is acceptable */
795} 804}
@@ -802,89 +811,93 @@ check_client_response(void *cls,
802 * @param resp the response 811 * @param resp the response
803 */ 812 */
804static void 813static void
805handle_client_response(void *cls, 814handle_client_response (void *cls,
806 const struct GNUNET_DNS_Response *resp) 815 const struct GNUNET_DNS_Response *resp)
807{ 816{
808 struct ClientRecord *cr = cls; 817 struct ClientRecord *cr = cls;
809 struct RequestRecord *rr; 818 struct RequestRecord *rr;
810 uint16_t msize; 819 uint16_t msize;
811 uint16_t off; 820 uint16_t off;
812 821
813 msize = ntohs(resp->header.size); 822 msize = ntohs (resp->header.size);
814 off = (uint16_t)resp->request_id; 823 off = (uint16_t) resp->request_id;
815 rr = &requests[off]; 824 rr = &requests[off];
816 LOG(GNUNET_ERROR_TYPE_DEBUG, 825 LOG (GNUNET_ERROR_TYPE_DEBUG,
817 "Received DNS response with ID %llu from local client!\n", 826 "Received DNS response with ID %llu from local client!\n",
818 (unsigned long long)resp->request_id); 827 (unsigned long long) resp->request_id);
819 if (rr->request_id != resp->request_id) 828 if (rr->request_id != resp->request_id)
820 { 829 {
821 GNUNET_STATISTICS_update(stats, 830 GNUNET_STATISTICS_update (stats,
822 gettext_noop("# Client response discarded (no matching request)"), 831 gettext_noop (
823 1, 832 "# Client response discarded (no matching request)"),
824 GNUNET_NO); 833 1,
825 GNUNET_SERVICE_client_continue(cr->client); 834 GNUNET_NO);
826 return; 835 GNUNET_SERVICE_client_continue (cr->client);
827 } 836 return;
837 }
828 for (unsigned int i = 0; i < rr->client_wait_list_length; i++) 838 for (unsigned int i = 0; i < rr->client_wait_list_length; i++)
839 {
840 if (NULL == rr->client_wait_list[i])
841 continue;
842 if (rr->client_wait_list[i] != cr)
843 continue;
844 rr->client_wait_list[i] = NULL;
845 switch (ntohl (resp->drop_flag))
829 { 846 {
830 if (NULL == rr->client_wait_list[i]) 847 case 0: /* drop */
831 continue; 848 rr->phase = RP_DROP;
832 if (rr->client_wait_list[i] != cr) 849 break;
833 continue; 850
834 rr->client_wait_list[i] = NULL; 851 case 1: /* no change */
835 switch (ntohl(resp->drop_flag)) 852 break;
836 { 853
837 case 0: /* drop */ 854 case 2: /* update */
838 rr->phase = RP_DROP; 855 msize -= sizeof(struct GNUNET_DNS_Response);
839 break; 856 if ((sizeof(struct GNUNET_TUN_DnsHeader) > msize) ||
840 857 (RP_REQUEST_MONITOR == rr->phase) ||
841 case 1: /* no change */ 858 (RP_RESPONSE_MONITOR == rr->phase))
842 break; 859 {
843 860 GNUNET_break (0);
844 case 2: /* update */ 861 GNUNET_SERVICE_client_drop (cr->client);
845 msize -= sizeof(struct GNUNET_DNS_Response); 862 next_phase (rr);
846 if ((sizeof(struct GNUNET_TUN_DnsHeader) > msize) || 863 return;
847 (RP_REQUEST_MONITOR == rr->phase) || 864 }
848 (RP_RESPONSE_MONITOR == rr->phase)) 865 GNUNET_free_non_null (rr->payload);
849 { 866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 GNUNET_break(0); 867 "Changing DNS reply according to client specifications\n");
851 GNUNET_SERVICE_client_drop(cr->client); 868 rr->payload = GNUNET_malloc (msize);
852 next_phase(rr); 869 rr->payload_length = msize;
853 return; 870 GNUNET_memcpy (rr->payload, &resp[1], msize);
854 } 871 if (rr->phase == RP_QUERY)
855 GNUNET_free_non_null(rr->payload); 872 {
856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 873 /* clear wait list, we're moving to MODIFY phase next */
857 "Changing DNS reply according to client specifications\n"); 874 GNUNET_array_grow (rr->client_wait_list,
858 rr->payload = GNUNET_malloc(msize); 875 rr->client_wait_list_length,
859 rr->payload_length = msize; 876 0);
860 GNUNET_memcpy(rr->payload, &resp[1], msize); 877 }
861 if (rr->phase == RP_QUERY) 878 /* if query changed to answer, move past DNS resolution phase... */
862 { 879 if ((RP_QUERY == rr->phase) &&
863 /* clear wait list, we're moving to MODIFY phase next */ 880 (rr->payload_length > sizeof(struct GNUNET_TUN_DnsHeader)) &&
864 GNUNET_array_grow(rr->client_wait_list, 881 ( ((struct GNUNET_TUN_DnsFlags*) &(((struct
865 rr->client_wait_list_length, 882 GNUNET_TUN_DnsHeader*) rr->
866 0); 883 payload)->flags))->
867 } 884 query_or_response == 1) )
868 /* if query changed to answer, move past DNS resolution phase... */ 885 {
869 if ((RP_QUERY == rr->phase) && 886 rr->phase = RP_INTERNET_DNS;
870 (rr->payload_length > sizeof(struct GNUNET_TUN_DnsHeader)) && 887 GNUNET_array_grow (rr->client_wait_list,
871 ((struct GNUNET_TUN_DnsFlags*)&(((struct GNUNET_TUN_DnsHeader*)rr->payload)->flags))->query_or_response == 1) 888 rr->client_wait_list_length,
872 { 889 0);
873 rr->phase = RP_INTERNET_DNS; 890 }
874 GNUNET_array_grow(rr->client_wait_list, 891 break;
875 rr->client_wait_list_length,
876 0);
877 }
878 break;
879 }
880 next_phase(rr);
881 GNUNET_SERVICE_client_continue(cr->client);
882 return;
883 } 892 }
893 next_phase (rr);
894 GNUNET_SERVICE_client_continue (cr->client);
895 return;
896 }
884 /* odd, client was not on our list for the request, that ought 897 /* odd, client was not on our list for the request, that ought
885 to be an error */ 898 to be an error */
886 GNUNET_break(0); 899 GNUNET_break (0);
887 GNUNET_SERVICE_client_drop(cr->client); 900 GNUNET_SERVICE_client_drop (cr->client);
888} 901}
889 902
890 903
@@ -896,8 +909,8 @@ handle_client_response(void *cls,
896 * @param message the actual message, a DNS request we should handle 909 * @param message the actual message, a DNS request we should handle
897 */ 910 */
898static int 911static int
899process_helper_messages(void *cls, 912process_helper_messages (void *cls,
900 const struct GNUNET_MessageHeader *message) 913 const struct GNUNET_MessageHeader *message)
901{ 914{
902 uint16_t msize; 915 uint16_t msize;
903 const struct GNUNET_TUN_Layer2PacketHeader *tun; 916 const struct GNUNET_TUN_Layer2PacketHeader *tun;
@@ -911,95 +924,103 @@ process_helper_messages(void *cls,
911 struct sockaddr_in *dsta4; 924 struct sockaddr_in *dsta4;
912 struct sockaddr_in6 *dsta6; 925 struct sockaddr_in6 *dsta6;
913 926
914 LOG(GNUNET_ERROR_TYPE_DEBUG, 927 LOG (GNUNET_ERROR_TYPE_DEBUG,
915 "Intercepted message via DNS hijacker\n"); 928 "Intercepted message via DNS hijacker\n");
916 msize = ntohs(message->size); 929 msize = ntohs (message->size);
917 if (msize < sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_TUN_Layer2PacketHeader) + sizeof(struct GNUNET_TUN_IPv4Header)) 930 if (msize < sizeof(struct GNUNET_MessageHeader) + sizeof(struct
918 { 931 GNUNET_TUN_Layer2PacketHeader)
919 /* non-IP packet received on TUN!? */ 932 + sizeof(struct GNUNET_TUN_IPv4Header))
920 GNUNET_break(0); 933 {
921 return GNUNET_OK; 934 /* non-IP packet received on TUN!? */
922 } 935 GNUNET_break (0);
936 return GNUNET_OK;
937 }
923 msize -= sizeof(struct GNUNET_MessageHeader); 938 msize -= sizeof(struct GNUNET_MessageHeader);
924 tun = (const struct GNUNET_TUN_Layer2PacketHeader *)&message[1]; 939 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
925 msize -= sizeof(struct GNUNET_TUN_Layer2PacketHeader); 940 msize -= sizeof(struct GNUNET_TUN_Layer2PacketHeader);
926 switch (ntohs(tun->proto)) 941 switch (ntohs (tun->proto))
942 {
943 case ETH_P_IPV4:
944 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
945 ip6 = NULL; /* make compiler happy */
946 if ((msize < sizeof(struct GNUNET_TUN_IPv4Header)) ||
947 (ip4->version != 4) ||
948 (ip4->header_length != sizeof(struct GNUNET_TUN_IPv4Header) / 4) ||
949 (ntohs (ip4->total_length) != msize) ||
950 (ip4->protocol != IPPROTO_UDP))
927 { 951 {
928 case ETH_P_IPV4: 952 /* non-IP/UDP packet received on TUN (or with options) */
929 ip4 = (const struct GNUNET_TUN_IPv4Header *)&tun[1]; 953 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
930 ip6 = NULL; /* make compiler happy */ 954 _ ("Received malformed IPv4-UDP packet on TUN interface.\n"));
931 if ((msize < sizeof(struct GNUNET_TUN_IPv4Header)) ||
932 (ip4->version != 4) ||
933 (ip4->header_length != sizeof(struct GNUNET_TUN_IPv4Header) / 4) ||
934 (ntohs(ip4->total_length) != msize) ||
935 (ip4->protocol != IPPROTO_UDP))
936 {
937 /* non-IP/UDP packet received on TUN (or with options) */
938 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
939 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
940 return GNUNET_OK;
941 }
942 udp = (const struct GNUNET_TUN_UdpHeader*)&ip4[1];
943 msize -= sizeof(struct GNUNET_TUN_IPv4Header);
944 break;
945
946 case ETH_P_IPV6:
947 ip4 = NULL; /* make compiler happy */
948 ip6 = (const struct GNUNET_TUN_IPv6Header *)&tun[1];
949 if ((msize < sizeof(struct GNUNET_TUN_IPv6Header)) ||
950 (ip6->version != 6) ||
951 (ntohs(ip6->payload_length) != msize - sizeof(struct GNUNET_TUN_IPv6Header)) ||
952 (ip6->next_header != IPPROTO_UDP))
953 {
954 /* non-IP/UDP packet received on TUN (or with extensions) */
955 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
956 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
957 return GNUNET_OK;
958 }
959 udp = (const struct GNUNET_TUN_UdpHeader *)&ip6[1];
960 msize -= sizeof(struct GNUNET_TUN_IPv6Header);
961 break;
962
963 default:
964 /* non-IP packet received on TUN!? */
965 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
966 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
967 (unsigned int)msize,
968 ntohs(tun->proto));
969 return GNUNET_OK; 955 return GNUNET_OK;
970 } 956 }
971 if ((msize <= sizeof(struct GNUNET_TUN_UdpHeader) + sizeof(struct GNUNET_TUN_DnsHeader)) || 957 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1];
972 (DNS_PORT != ntohs(udp->destination_port))) 958 msize -= sizeof(struct GNUNET_TUN_IPv4Header);
959 break;
960
961 case ETH_P_IPV6:
962 ip4 = NULL; /* make compiler happy */
963 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
964 if ((msize < sizeof(struct GNUNET_TUN_IPv6Header)) ||
965 (ip6->version != 6) ||
966 (ntohs (ip6->payload_length) != msize - sizeof(struct
967 GNUNET_TUN_IPv6Header))
968 ||
969 (ip6->next_header != IPPROTO_UDP))
973 { 970 {
974 /* non-DNS packet received on TUN, ignore */ 971 /* non-IP/UDP packet received on TUN (or with extensions) */
975 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
976 _("DNS interceptor got non-DNS packet (dropped)\n")); 973 _ ("Received malformed IPv6-UDP packet on TUN interface.\n"));
977 GNUNET_STATISTICS_update(stats,
978 gettext_noop("# Non-DNS UDP packet received via TUN interface"),
979 1, GNUNET_NO);
980 return GNUNET_OK; 974 return GNUNET_OK;
981 } 975 }
976 udp = (const struct GNUNET_TUN_UdpHeader *) &ip6[1];
977 msize -= sizeof(struct GNUNET_TUN_IPv6Header);
978 break;
979
980 default:
981 /* non-IP packet received on TUN!? */
982 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
983 _ (
984 "Got non-IP packet with %u bytes and protocol %u from TUN\n"),
985 (unsigned int) msize,
986 ntohs (tun->proto));
987 return GNUNET_OK;
988 }
989 if ((msize <= sizeof(struct GNUNET_TUN_UdpHeader) + sizeof(struct
990 GNUNET_TUN_DnsHeader))
991 ||
992 (DNS_PORT != ntohs (udp->destination_port)))
993 {
994 /* non-DNS packet received on TUN, ignore */
995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
996 _ ("DNS interceptor got non-DNS packet (dropped)\n"));
997 GNUNET_STATISTICS_update (stats,
998 gettext_noop (
999 "# Non-DNS UDP packet received via TUN interface"),
1000 1, GNUNET_NO);
1001 return GNUNET_OK;
1002 }
982 msize -= sizeof(struct GNUNET_TUN_UdpHeader); 1003 msize -= sizeof(struct GNUNET_TUN_UdpHeader);
983 dns = (const struct GNUNET_TUN_DnsHeader*)&udp[1]; 1004 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
984 rr = &requests[dns->id]; 1005 rr = &requests[dns->id];
985 1006
986 /* clean up from previous request */ 1007 /* clean up from previous request */
987 GNUNET_free_non_null(rr->payload); 1008 GNUNET_free_non_null (rr->payload);
988 rr->payload = NULL; 1009 rr->payload = NULL;
989 GNUNET_array_grow(rr->client_wait_list, 1010 GNUNET_array_grow (rr->client_wait_list,
990 rr->client_wait_list_length, 1011 rr->client_wait_list_length,
991 0); 1012 0);
992 1013
993 /* setup new request */ 1014 /* setup new request */
994 rr->phase = RP_INIT; 1015 rr->phase = RP_INIT;
995 switch (ntohs(tun->proto)) 1016 switch (ntohs (tun->proto))
996 { 1017 {
997 case ETH_P_IPV4: 1018 case ETH_P_IPV4:
998 { 1019 {
999 srca4 = (struct sockaddr_in*)&rr->src_addr; 1020 srca4 = (struct sockaddr_in*) &rr->src_addr;
1000 dsta4 = (struct sockaddr_in*)&rr->dst_addr; 1021 dsta4 = (struct sockaddr_in*) &rr->dst_addr;
1001 memset(srca4, 0, sizeof(struct sockaddr_in)); 1022 memset (srca4, 0, sizeof(struct sockaddr_in));
1002 memset(dsta4, 0, sizeof(struct sockaddr_in)); 1023 memset (dsta4, 0, sizeof(struct sockaddr_in));
1003 srca4->sin_family = AF_INET; 1024 srca4->sin_family = AF_INET;
1004 dsta4->sin_family = AF_INET; 1025 dsta4->sin_family = AF_INET;
1005 srca4->sin_addr = ip4->source_address; 1026 srca4->sin_addr = ip4->source_address;
@@ -1013,12 +1034,12 @@ process_helper_messages(void *cls,
1013 } 1034 }
1014 break; 1035 break;
1015 1036
1016 case ETH_P_IPV6: 1037 case ETH_P_IPV6:
1017 { 1038 {
1018 srca6 = (struct sockaddr_in6*)&rr->src_addr; 1039 srca6 = (struct sockaddr_in6*) &rr->src_addr;
1019 dsta6 = (struct sockaddr_in6*)&rr->dst_addr; 1040 dsta6 = (struct sockaddr_in6*) &rr->dst_addr;
1020 memset(srca6, 0, sizeof(struct sockaddr_in6)); 1041 memset (srca6, 0, sizeof(struct sockaddr_in6));
1021 memset(dsta6, 0, sizeof(struct sockaddr_in6)); 1042 memset (dsta6, 0, sizeof(struct sockaddr_in6));
1022 srca6->sin6_family = AF_INET6; 1043 srca6->sin6_family = AF_INET6;
1023 dsta6->sin6_family = AF_INET6; 1044 dsta6->sin6_family = AF_INET6;
1024 srca6->sin6_addr = ip6->source_address; 1045 srca6->sin6_addr = ip6->source_address;
@@ -1032,22 +1053,23 @@ process_helper_messages(void *cls,
1032 } 1053 }
1033 break; 1054 break;
1034 1055
1035 default: 1056 default:
1036 GNUNET_assert(0); 1057 GNUNET_assert (0);
1037 } 1058 }
1038 rr->payload = GNUNET_malloc(msize); 1059 rr->payload = GNUNET_malloc (msize);
1039 rr->payload_length = msize; 1060 rr->payload_length = msize;
1040 GNUNET_memcpy(rr->payload, dns, msize); 1061 GNUNET_memcpy (rr->payload, dns, msize);
1041 rr->request_id = dns->id | (request_id_gen << 16); 1062 rr->request_id = dns->id | (request_id_gen << 16);
1042 request_id_gen++; 1063 request_id_gen++;
1043 LOG(GNUNET_ERROR_TYPE_DEBUG, 1064 LOG (GNUNET_ERROR_TYPE_DEBUG,
1044 "Creating new DNS request %llu\n", 1065 "Creating new DNS request %llu\n",
1045 (unsigned long long)rr->request_id); 1066 (unsigned long long) rr->request_id);
1046 GNUNET_STATISTICS_update(stats, 1067 GNUNET_STATISTICS_update (stats,
1047 gettext_noop("# DNS requests received via TUN interface"), 1068 gettext_noop (
1048 1, GNUNET_NO); 1069 "# DNS requests received via TUN interface"),
1070 1, GNUNET_NO);
1049 /* start request processing state machine */ 1071 /* start request processing state machine */
1050 next_phase(rr); 1072 next_phase (rr);
1051 return GNUNET_OK; 1073 return GNUNET_OK;
1052} 1074}
1053 1075
@@ -1058,9 +1080,9 @@ process_helper_messages(void *cls,
1058 * @param service the initialized service 1080 * @param service the initialized service
1059 */ 1081 */
1060static void 1082static void
1061run(void *cls, 1083run (void *cls,
1062 const struct GNUNET_CONFIGURATION_Handle *cfg_, 1084 const struct GNUNET_CONFIGURATION_Handle *cfg_,
1063 struct GNUNET_SERVICE_Handle *service) 1085 struct GNUNET_SERVICE_Handle *service)
1064{ 1086{
1065 char *ifc_name; 1087 char *ifc_name;
1066 char *ipv4addr; 1088 char *ipv4addr;
@@ -1072,126 +1094,126 @@ run(void *cls,
1072 int nortsetup; 1094 int nortsetup;
1073 1095
1074 cfg = cfg_; 1096 cfg = cfg_;
1075 stats = GNUNET_STATISTICS_create("dns", cfg); 1097 stats = GNUNET_STATISTICS_create ("dns", cfg);
1076 GNUNET_SCHEDULER_add_shutdown(&cleanup_task, 1098 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
1077 cls); 1099 cls);
1078 dnsstub = GNUNET_DNSSTUB_start(128); 1100 dnsstub = GNUNET_DNSSTUB_start (128);
1079 /* TODO: support multiple DNS_EXIT servers being configured */ 1101 /* TODO: support multiple DNS_EXIT servers being configured */
1080 /* TODO: see above TODO on using DNS server from original packet. 1102 /* TODO: see above TODO on using DNS server from original packet.
1081 Not sure which is best... */ 1103 Not sure which is best... */
1082 dns_exit = NULL; 1104 dns_exit = NULL;
1083 if ((GNUNET_OK != 1105 if ((GNUNET_OK !=
1084 GNUNET_CONFIGURATION_get_value_string(cfg, 1106 GNUNET_CONFIGURATION_get_value_string (cfg,
1085 "dns", 1107 "dns",
1086 "DNS_EXIT", 1108 "DNS_EXIT",
1087 &dns_exit)) || 1109 &dns_exit)) ||
1088 (GNUNET_OK != 1110 (GNUNET_OK !=
1089 GNUNET_DNSSTUB_add_dns_ip(dnsstub, 1111 GNUNET_DNSSTUB_add_dns_ip (dnsstub,
1090 dns_exit))) 1112 dns_exit)))
1091 { 1113 {
1092 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR, 1114 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1093 "dns", 1115 "dns",
1094 "DNS_EXIT", 1116 "DNS_EXIT",
1095 _("need a valid IPv4 or IPv6 address\n")); 1117 _ ("need a valid IPv4 or IPv6 address\n"));
1096 GNUNET_free_non_null(dns_exit); 1118 GNUNET_free_non_null (dns_exit);
1097 } 1119 }
1098 binary = GNUNET_OS_get_suid_binary_path(cfg, "gnunet-helper-dns"); 1120 binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-dns");
1099 1121
1100 if (GNUNET_YES != 1122 if (GNUNET_YES !=
1101 GNUNET_OS_check_helper_binary(binary, 1123 GNUNET_OS_check_helper_binary (binary,
1102 GNUNET_YES, 1124 GNUNET_YES,
1103 NULL)) // TODO: once we have a windows-testcase, add test parameters here 1125 NULL)) // TODO: once we have a windows-testcase, add test parameters here
1104 { 1126 {
1105 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1127 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1106 _("`%s' is not SUID or the path is invalid, " 1128 _ ("`%s' is not SUID or the path is invalid, "
1107 "will not run DNS interceptor\n"), 1129 "will not run DNS interceptor\n"),
1108 binary); 1130 binary);
1109 global_ret = 1; 1131 global_ret = 1;
1110 GNUNET_free(binary); 1132 GNUNET_free (binary);
1111 return; 1133 return;
1112 } 1134 }
1113 GNUNET_free(binary); 1135 GNUNET_free (binary);
1114 1136
1115 helper_argv[0] = GNUNET_strdup("gnunet-dns"); 1137 helper_argv[0] = GNUNET_strdup ("gnunet-dns");
1116 if (GNUNET_SYSERR == 1138 if (GNUNET_SYSERR ==
1117 GNUNET_CONFIGURATION_get_value_string(cfg, 1139 GNUNET_CONFIGURATION_get_value_string (cfg,
1118 "dns", 1140 "dns",
1119 "IFNAME", 1141 "IFNAME",
1120 &ifc_name)) 1142 &ifc_name))
1121 { 1143 {
1122 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1123 "No entry 'IFNAME' in configuration!\n"); 1145 "No entry 'IFNAME' in configuration!\n");
1124 GNUNET_free(binary); 1146 GNUNET_free (binary);
1125 GNUNET_SCHEDULER_shutdown(); 1147 GNUNET_SCHEDULER_shutdown ();
1126 return; 1148 return;
1127 } 1149 }
1128 helper_argv[1] = ifc_name; 1150 helper_argv[1] = ifc_name;
1129 if ((GNUNET_SYSERR == 1151 if ((GNUNET_SYSERR ==
1130 GNUNET_CONFIGURATION_get_value_string(cfg, 1152 GNUNET_CONFIGURATION_get_value_string (cfg,
1131 "dns", 1153 "dns",
1132 "IPV6ADDR", 1154 "IPV6ADDR",
1133 &ipv6addr))) 1155 &ipv6addr)))
1134 { 1156 {
1135 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1136 "No entry 'IPV6ADDR' in configuration!\n"); 1158 "No entry 'IPV6ADDR' in configuration!\n");
1137 GNUNET_free(binary); 1159 GNUNET_free (binary);
1138 GNUNET_SCHEDULER_shutdown(); 1160 GNUNET_SCHEDULER_shutdown ();
1139 return; 1161 return;
1140 } 1162 }
1141 helper_argv[2] = ipv6addr; 1163 helper_argv[2] = ipv6addr;
1142 if (GNUNET_SYSERR == 1164 if (GNUNET_SYSERR ==
1143 GNUNET_CONFIGURATION_get_value_string(cfg, 1165 GNUNET_CONFIGURATION_get_value_string (cfg,
1144 "dns", 1166 "dns",
1145 "IPV6PREFIX", 1167 "IPV6PREFIX",
1146 &ipv6prefix)) 1168 &ipv6prefix))
1147 { 1169 {
1148 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1149 "No entry 'IPV6PREFIX' in configuration!\n"); 1171 "No entry 'IPV6PREFIX' in configuration!\n");
1150 GNUNET_free(binary); 1172 GNUNET_free (binary);
1151 GNUNET_SCHEDULER_shutdown(); 1173 GNUNET_SCHEDULER_shutdown ();
1152 return; 1174 return;
1153 } 1175 }
1154 helper_argv[3] = ipv6prefix; 1176 helper_argv[3] = ipv6prefix;
1155 1177
1156 if (GNUNET_SYSERR == 1178 if (GNUNET_SYSERR ==
1157 GNUNET_CONFIGURATION_get_value_string(cfg, 1179 GNUNET_CONFIGURATION_get_value_string (cfg,
1158 "dns", 1180 "dns",
1159 "IPV4ADDR", 1181 "IPV4ADDR",
1160 &ipv4addr)) 1182 &ipv4addr))
1161 { 1183 {
1162 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1163 "No entry 'IPV4ADDR' in configuration!\n"); 1185 "No entry 'IPV4ADDR' in configuration!\n");
1164 GNUNET_free(binary); 1186 GNUNET_free (binary);
1165 GNUNET_SCHEDULER_shutdown(); 1187 GNUNET_SCHEDULER_shutdown ();
1166 return; 1188 return;
1167 } 1189 }
1168 helper_argv[4] = ipv4addr; 1190 helper_argv[4] = ipv4addr;
1169 if (GNUNET_SYSERR == 1191 if (GNUNET_SYSERR ==
1170 GNUNET_CONFIGURATION_get_value_string(cfg, "dns", "IPV4MASK", 1192 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK",
1171 &ipv4mask)) 1193 &ipv4mask))
1172 { 1194 {
1173 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1174 "No entry 'IPV4MASK' in configuration!\n"); 1196 "No entry 'IPV4MASK' in configuration!\n");
1175 GNUNET_free(binary); 1197 GNUNET_free (binary);
1176 GNUNET_SCHEDULER_shutdown(); 1198 GNUNET_SCHEDULER_shutdown ();
1177 return; 1199 return;
1178 } 1200 }
1179 helper_argv[5] = ipv4mask; 1201 helper_argv[5] = ipv4mask;
1180 1202
1181 nortsetup = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dns", 1203 nortsetup = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dns",
1182 "SKIP_ROUTING_SETUP"); 1204 "SKIP_ROUTING_SETUP");
1183 if (GNUNET_YES == nortsetup) 1205 if (GNUNET_YES == nortsetup)
1184 helper_argv[6] = GNUNET_strdup("1"); 1206 helper_argv[6] = GNUNET_strdup ("1");
1185 else 1207 else
1186 helper_argv[6] = GNUNET_strdup("0"); 1208 helper_argv[6] = GNUNET_strdup ("0");
1187 1209
1188 helper_argv[7] = NULL; 1210 helper_argv[7] = NULL;
1189 hijacker = GNUNET_HELPER_start(GNUNET_NO, 1211 hijacker = GNUNET_HELPER_start (GNUNET_NO,
1190 binary, 1212 binary,
1191 helper_argv, 1213 helper_argv,
1192 &process_helper_messages, 1214 &process_helper_messages,
1193 NULL, NULL); 1215 NULL, NULL);
1194 GNUNET_free(binary); 1216 GNUNET_free (binary);
1195} 1217}
1196 1218
1197 1219
@@ -1205,15 +1227,15 @@ GNUNET_SERVICE_MAIN
1205 &client_connect_cb, 1227 &client_connect_cb,
1206 &client_disconnect_cb, 1228 &client_disconnect_cb,
1207 NULL, 1229 NULL,
1208 GNUNET_MQ_hd_fixed_size(client_init, 1230 GNUNET_MQ_hd_fixed_size (client_init,
1209 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, 1231 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1210 struct GNUNET_DNS_Register, 1232 struct GNUNET_DNS_Register,
1211 NULL), 1233 NULL),
1212 GNUNET_MQ_hd_var_size(client_response, 1234 GNUNET_MQ_hd_var_size (client_response,
1213 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 1235 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE,
1214 struct GNUNET_DNS_Response, 1236 struct GNUNET_DNS_Response,
1215 NULL), 1237 NULL),
1216 GNUNET_MQ_handler_end()); 1238 GNUNET_MQ_handler_end ());
1217 1239
1218 1240
1219/* FIXME: this might need a port on systems without 'getresgid' */ 1241/* FIXME: this might need a port on systems without 'getresgid' */
@@ -1222,28 +1244,28 @@ GNUNET_SERVICE_MAIN
1222 * Enable use of SGID capabilities on POSIX 1244 * Enable use of SGID capabilities on POSIX
1223 */ 1245 */
1224void __attribute__ ((constructor)) 1246void __attribute__ ((constructor))
1225GNUNET_DNS_init() 1247GNUNET_DNS_init ()
1226{ 1248{
1227 gid_t rgid; 1249 gid_t rgid;
1228 gid_t egid; 1250 gid_t egid;
1229 gid_t sgid; 1251 gid_t sgid;
1230 1252
1231 if (-1 == getresgid(&rgid, 1253 if (-1 == getresgid (&rgid,
1232 &egid, 1254 &egid,
1233 &sgid)) 1255 &sgid))
1234 { 1256 {
1235 fprintf(stderr, 1257 fprintf (stderr,
1236 "getresgid failed: %s\n", 1258 "getresgid failed: %s\n",
1237 strerror(errno)); 1259 strerror (errno));
1238 } 1260 }
1239 else if (sgid != rgid) 1261 else if (sgid != rgid)
1240 { 1262 {
1241 if (-1 == setregid(sgid, 1263 if (-1 == setregid (sgid,
1242 sgid)) 1264 sgid))
1243 fprintf(stderr, 1265 fprintf (stderr,
1244 "setregid failed: %s\n", 1266 "setregid failed: %s\n",
1245 strerror(errno)); 1267 strerror (errno));
1246 } 1268 }
1247} 1269}
1248#endif 1270#endif
1249 1271