diff options
Diffstat (limited to 'src/service/dns/gnunet-service-dns.c')
-rw-r--r-- | src/service/dns/gnunet-service-dns.c | 1272 |
1 files changed, 1272 insertions, 0 deletions
diff --git a/src/service/dns/gnunet-service-dns.c b/src/service/dns/gnunet-service-dns.c new file mode 100644 index 000000000..24566ae21 --- /dev/null +++ b/src/service/dns/gnunet-service-dns.c | |||
@@ -0,0 +1,1272 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dns/gnunet-service-dns.c | ||
23 | * @brief service to intercept and modify DNS queries (and replies) of this system | ||
24 | * @author Christian Grothoff | ||
25 | * | ||
26 | * For "secure" interaction with the legacy DNS system, we permit | ||
27 | * replies only to arrive within a 5s window (and they must match | ||
28 | * ports, IPs and request IDs). Furthermore, we let the OS pick a | ||
29 | * source port, opening up to 128 sockets per address family (IPv4 or | ||
30 | * IPv6). Those sockets are closed if they are not in use for 5s | ||
31 | * (which means they will be freshly randomized afterwards). For new | ||
32 | * requests, we pick a random slot in the array with 128 socket slots | ||
33 | * (and re-use an existing socket if the slot is still in use). Thus | ||
34 | * each request will be given one of 128 random source ports, and the | ||
35 | * 128 random source ports will also change "often" (less often if the | ||
36 | * system is very busy, each time if we are mostly idle). At the same | ||
37 | * time, the system will never use more than 256 UDP sockets. | ||
38 | */ | ||
39 | #include "platform.h" | ||
40 | #include "gnunet_util_lib.h" | ||
41 | #include "gnunet_applications.h" | ||
42 | #include "gnunet_constants.h" | ||
43 | #include "gnunet_protocols.h" | ||
44 | #include "gnunet_signatures.h" | ||
45 | #include "dns.h" | ||
46 | #include "gnunet_dns_service.h" | ||
47 | #include "gnunet_statistics_service.h" | ||
48 | |||
49 | /** | ||
50 | * Port number for DNS | ||
51 | */ | ||
52 | #define DNS_PORT 53 | ||
53 | |||
54 | |||
55 | /** | ||
56 | * Generic logging shorthand | ||
57 | */ | ||
58 | #define LOG(kind, ...) \ | ||
59 | GNUNET_log_from (kind, "dns", __VA_ARGS__); | ||
60 | |||
61 | |||
62 | /** | ||
63 | * Phases each request goes through. | ||
64 | */ | ||
65 | enum RequestPhase | ||
66 | { | ||
67 | /** | ||
68 | * Request has just been received. | ||
69 | */ | ||
70 | RP_INIT, | ||
71 | |||
72 | /** | ||
73 | * Showing the request to all monitor clients. If | ||
74 | * client list is empty, will enter QUERY phase. | ||
75 | */ | ||
76 | RP_REQUEST_MONITOR, | ||
77 | |||
78 | /** | ||
79 | * Showing the request to PRE-RESOLUTION clients to find an answer. | ||
80 | * If client list is empty, will trigger global DNS request. | ||
81 | */ | ||
82 | RP_QUERY, | ||
83 | |||
84 | /** | ||
85 | * Global Internet query is now pending. | ||
86 | */ | ||
87 | RP_INTERNET_DNS, | ||
88 | |||
89 | /** | ||
90 | * Client (or global DNS request) has resulted in a response. | ||
91 | * Forward to all POST-RESOLUTION clients. If client list is empty, | ||
92 | * will enter RESPONSE_MONITOR phase. | ||
93 | */ | ||
94 | RP_MODIFY, | ||
95 | |||
96 | /** | ||
97 | * Showing the request to all monitor clients. If | ||
98 | * client list is empty, give the result to the hijacker (and be done). | ||
99 | */ | ||
100 | RP_RESPONSE_MONITOR, | ||
101 | |||
102 | /** | ||
103 | * Some client has told us to drop the request. | ||
104 | */ | ||
105 | RP_DROP | ||
106 | }; | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Entry we keep for each client. | ||
111 | */ | ||
112 | struct ClientRecord | ||
113 | { | ||
114 | /** | ||
115 | * Kept in doubly-linked list. | ||
116 | */ | ||
117 | struct ClientRecord *next; | ||
118 | |||
119 | /** | ||
120 | * Kept in doubly-linked list. | ||
121 | */ | ||
122 | struct ClientRecord *prev; | ||
123 | |||
124 | /** | ||
125 | * Handle to the client. | ||
126 | */ | ||
127 | struct GNUNET_SERVICE_Client *client; | ||
128 | |||
129 | /** | ||
130 | * Message queue to talk to @a client. | ||
131 | */ | ||
132 | struct GNUNET_MQ_Handle *mq; | ||
133 | |||
134 | /** | ||
135 | * Flags for the client. | ||
136 | */ | ||
137 | enum GNUNET_DNS_Flags flags; | ||
138 | }; | ||
139 | |||
140 | |||
141 | /** | ||
142 | * Entry we keep for each active request. | ||
143 | */ | ||
144 | struct RequestRecord | ||
145 | { | ||
146 | /** | ||
147 | * List of clients that still need to see this request (each entry | ||
148 | * is set to NULL when the client is done). | ||
149 | */ | ||
150 | struct ClientRecord **client_wait_list; | ||
151 | |||
152 | /** | ||
153 | * Payload of the UDP packet (the UDP payload), can be either query | ||
154 | * or already the response. | ||
155 | */ | ||
156 | char *payload; | ||
157 | |||
158 | /** | ||
159 | * Socket we are using to transmit this request (must match if we receive | ||
160 | * a response). | ||
161 | */ | ||
162 | struct GNUNET_DNSSTUB_RequestSocket *rs; | ||
163 | |||
164 | /** | ||
165 | * Source address of the original request (for sending response). | ||
166 | */ | ||
167 | struct sockaddr_storage src_addr; | ||
168 | |||
169 | /** | ||
170 | * Destination address of the original request (for potential use as exit). | ||
171 | */ | ||
172 | struct sockaddr_storage dst_addr; | ||
173 | |||
174 | /** | ||
175 | * ID of this request, also basis for hashing. Lowest 16 bit will | ||
176 | * be our message ID when doing a global DNS request and our index | ||
177 | * into the 'requests' array. | ||
178 | */ | ||
179 | uint64_t request_id; | ||
180 | |||
181 | /** | ||
182 | * Number of bytes in payload. | ||
183 | */ | ||
184 | size_t payload_length; | ||
185 | |||
186 | /** | ||
187 | * Length of the @e client_wait_list. | ||
188 | */ | ||
189 | unsigned int client_wait_list_length; | ||
190 | |||
191 | /** | ||
192 | * In which phase this this request? | ||
193 | */ | ||
194 | enum RequestPhase phase; | ||
195 | }; | ||
196 | |||
197 | |||
198 | /** | ||
199 | * Global return value from 'main'. | ||
200 | */ | ||
201 | static int global_ret; | ||
202 | |||
203 | /** | ||
204 | * The configuration to use | ||
205 | */ | ||
206 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
207 | |||
208 | /** | ||
209 | * Statistics. | ||
210 | */ | ||
211 | static struct GNUNET_STATISTICS_Handle *stats; | ||
212 | |||
213 | /** | ||
214 | * Handle to DNS hijacker helper process ("gnunet-helper-dns"). | ||
215 | */ | ||
216 | static struct GNUNET_HELPER_Handle *hijacker; | ||
217 | |||
218 | /** | ||
219 | * Command-line arguments we are giving to the hijacker process. | ||
220 | */ | ||
221 | static char *helper_argv[8]; | ||
222 | |||
223 | /** | ||
224 | * Head of DLL of clients we consult. | ||
225 | */ | ||
226 | static struct ClientRecord *clients_head; | ||
227 | |||
228 | /** | ||
229 | * Tail of DLL of clients we consult. | ||
230 | */ | ||
231 | static struct ClientRecord *clients_tail; | ||
232 | |||
233 | /** | ||
234 | * Array of all open requests. | ||
235 | */ | ||
236 | static struct RequestRecord requests[UINT16_MAX + 1]; | ||
237 | |||
238 | /** | ||
239 | * Generator for unique request IDs. | ||
240 | */ | ||
241 | static uint64_t request_id_gen; | ||
242 | |||
243 | /** | ||
244 | * Handle to the DNS Stub resolver. | ||
245 | */ | ||
246 | static struct GNUNET_DNSSTUB_Context *dnsstub; | ||
247 | |||
248 | |||
249 | /** | ||
250 | * We're done processing a DNS request, free associated memory. | ||
251 | * | ||
252 | * @param rr request to clean up | ||
253 | */ | ||
254 | static void | ||
255 | cleanup_rr (struct RequestRecord *rr) | ||
256 | { | ||
257 | GNUNET_free (rr->payload); | ||
258 | rr->payload = NULL; | ||
259 | rr->payload_length = 0; | ||
260 | GNUNET_array_grow (rr->client_wait_list, | ||
261 | rr->client_wait_list_length, | ||
262 | 0); | ||
263 | } | ||
264 | |||
265 | |||
266 | /** | ||
267 | * Task run during shutdown. | ||
268 | * | ||
269 | * @param cls unused | ||
270 | */ | ||
271 | static void | ||
272 | cleanup_task (void *cls GNUNET_UNUSED) | ||
273 | { | ||
274 | if (NULL != hijacker) | ||
275 | { | ||
276 | GNUNET_HELPER_stop (hijacker, GNUNET_NO); | ||
277 | hijacker = NULL; | ||
278 | } | ||
279 | for (unsigned int i = 0; i < 8; i++) | ||
280 | GNUNET_free (helper_argv[i]); | ||
281 | for (unsigned int i = 0; i <= UINT16_MAX; i++) | ||
282 | cleanup_rr (&requests[i]); | ||
283 | if (NULL != stats) | ||
284 | { | ||
285 | GNUNET_STATISTICS_destroy (stats, | ||
286 | GNUNET_NO); | ||
287 | stats = NULL; | ||
288 | } | ||
289 | if (NULL != dnsstub) | ||
290 | { | ||
291 | GNUNET_DNSSTUB_stop (dnsstub); | ||
292 | dnsstub = NULL; | ||
293 | } | ||
294 | } | ||
295 | |||
296 | |||
297 | /** | ||
298 | * We're done with some request, finish processing. | ||
299 | * | ||
300 | * @param rr request send to the network or just clean up. | ||
301 | */ | ||
302 | static void | ||
303 | request_done (struct RequestRecord *rr) | ||
304 | { | ||
305 | struct GNUNET_MessageHeader *hdr; | ||
306 | size_t reply_len; | ||
307 | uint16_t source_port; | ||
308 | uint16_t destination_port; | ||
309 | |||
310 | GNUNET_array_grow (rr->client_wait_list, | ||
311 | rr->client_wait_list_length, | ||
312 | 0); | ||
313 | if (RP_RESPONSE_MONITOR != rr->phase) | ||
314 | { | ||
315 | /* no response, drop */ | ||
316 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
317 | "Got no response for request %llu, dropping\n", | ||
318 | (unsigned long long) rr->request_id); | ||
319 | cleanup_rr (rr); | ||
320 | return; | ||
321 | } | ||
322 | |||
323 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
324 | "Transmitting response for request %llu\n", | ||
325 | (unsigned long long) rr->request_id); | ||
326 | /* send response via hijacker */ | ||
327 | reply_len = sizeof(struct GNUNET_MessageHeader); | ||
328 | reply_len += sizeof(struct GNUNET_TUN_Layer2PacketHeader); | ||
329 | switch (rr->src_addr.ss_family) | ||
330 | { | ||
331 | case AF_INET: | ||
332 | reply_len += sizeof(struct GNUNET_TUN_IPv4Header); | ||
333 | break; | ||
334 | |||
335 | case AF_INET6: | ||
336 | reply_len += sizeof(struct GNUNET_TUN_IPv6Header); | ||
337 | break; | ||
338 | |||
339 | default: | ||
340 | GNUNET_break (0); | ||
341 | cleanup_rr (rr); | ||
342 | return; | ||
343 | } | ||
344 | reply_len += sizeof(struct GNUNET_TUN_UdpHeader); | ||
345 | reply_len += rr->payload_length; | ||
346 | if (reply_len >= GNUNET_MAX_MESSAGE_SIZE) | ||
347 | { | ||
348 | /* response too big, drop */ | ||
349 | GNUNET_break (0); /* how can this be? */ | ||
350 | cleanup_rr (rr); | ||
351 | return; | ||
352 | } | ||
353 | { | ||
354 | char buf[reply_len] GNUNET_ALIGN; | ||
355 | size_t off; | ||
356 | struct GNUNET_TUN_IPv4Header ip4; | ||
357 | struct GNUNET_TUN_IPv6Header ip6; | ||
358 | |||
359 | /* first, GNUnet message header */ | ||
360 | hdr = (struct GNUNET_MessageHeader*) buf; | ||
361 | hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER); | ||
362 | hdr->size = htons ((uint16_t) reply_len); | ||
363 | off = sizeof(struct GNUNET_MessageHeader); | ||
364 | |||
365 | /* first, TUN header */ | ||
366 | { | ||
367 | struct GNUNET_TUN_Layer2PacketHeader tun; | ||
368 | |||
369 | tun.flags = htons (0); | ||
370 | if (rr->src_addr.ss_family == AF_INET) | ||
371 | tun.proto = htons (ETH_P_IPV4); | ||
372 | else | ||
373 | tun.proto = htons (ETH_P_IPV6); | ||
374 | GNUNET_memcpy (&buf[off], | ||
375 | &tun, | ||
376 | sizeof(struct GNUNET_TUN_Layer2PacketHeader)); | ||
377 | off += sizeof(struct GNUNET_TUN_Layer2PacketHeader); | ||
378 | } | ||
379 | |||
380 | /* now IP header */ | ||
381 | switch (rr->src_addr.ss_family) | ||
382 | { | ||
383 | case AF_INET: | ||
384 | { | ||
385 | struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr; | ||
386 | struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr; | ||
387 | |||
388 | source_port = dst->sin_port; | ||
389 | destination_port = src->sin_port; | ||
390 | GNUNET_TUN_initialize_ipv4_header (&ip4, | ||
391 | IPPROTO_UDP, | ||
392 | reply_len - off - sizeof(struct | ||
393 | GNUNET_TUN_IPv4Header), | ||
394 | &dst->sin_addr, | ||
395 | &src->sin_addr); | ||
396 | GNUNET_memcpy (&buf[off], | ||
397 | &ip4, | ||
398 | sizeof(ip4)); | ||
399 | off += sizeof(ip4); | ||
400 | } | ||
401 | break; | ||
402 | |||
403 | case AF_INET6: | ||
404 | { | ||
405 | struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr; | ||
406 | struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr; | ||
407 | |||
408 | source_port = dst->sin6_port; | ||
409 | destination_port = src->sin6_port; | ||
410 | GNUNET_TUN_initialize_ipv6_header (&ip6, | ||
411 | IPPROTO_UDP, | ||
412 | reply_len - off - sizeof(struct | ||
413 | GNUNET_TUN_IPv6Header), | ||
414 | &dst->sin6_addr, | ||
415 | &src->sin6_addr); | ||
416 | GNUNET_memcpy (&buf[off], | ||
417 | &ip6, | ||
418 | sizeof(ip6)); | ||
419 | off += sizeof(ip6); | ||
420 | } | ||
421 | break; | ||
422 | |||
423 | default: | ||
424 | GNUNET_assert (0); | ||
425 | } | ||
426 | |||
427 | /* now UDP header */ | ||
428 | { | ||
429 | struct GNUNET_TUN_UdpHeader udp; | ||
430 | |||
431 | udp.source_port = source_port; | ||
432 | udp.destination_port = destination_port; | ||
433 | udp.len = htons (reply_len - off); | ||
434 | if (AF_INET == rr->src_addr.ss_family) | ||
435 | GNUNET_TUN_calculate_udp4_checksum (&ip4, | ||
436 | &udp, | ||
437 | rr->payload, | ||
438 | rr->payload_length); | ||
439 | else | ||
440 | GNUNET_TUN_calculate_udp6_checksum (&ip6, | ||
441 | &udp, | ||
442 | rr->payload, | ||
443 | rr->payload_length); | ||
444 | GNUNET_memcpy (&buf[off], | ||
445 | &udp, | ||
446 | sizeof(udp)); | ||
447 | off += sizeof(udp); | ||
448 | } | ||
449 | |||
450 | /* now DNS payload */ | ||
451 | { | ||
452 | GNUNET_memcpy (&buf[off], rr->payload, rr->payload_length); | ||
453 | off += rr->payload_length; | ||
454 | } | ||
455 | /* final checks & sending */ | ||
456 | GNUNET_assert (off == reply_len); | ||
457 | (void) GNUNET_HELPER_send (hijacker, | ||
458 | hdr, | ||
459 | GNUNET_YES, | ||
460 | NULL, NULL); | ||
461 | GNUNET_STATISTICS_update (stats, | ||
462 | gettext_noop ( | ||
463 | "# DNS requests answered via TUN interface"), | ||
464 | 1, GNUNET_NO); | ||
465 | } | ||
466 | /* clean up, we're done */ | ||
467 | cleanup_rr (rr); | ||
468 | } | ||
469 | |||
470 | |||
471 | /** | ||
472 | * Show the payload of the given request record to the client | ||
473 | * (and wait for a response). | ||
474 | * | ||
475 | * @param rr request to send to client | ||
476 | * @param cr client to send the response to | ||
477 | */ | ||
478 | static void | ||
479 | send_request_to_client (struct RequestRecord *rr, | ||
480 | struct ClientRecord *cr) | ||
481 | { | ||
482 | struct GNUNET_MQ_Envelope *env; | ||
483 | struct GNUNET_DNS_Request *req; | ||
484 | |||
485 | if (sizeof(struct GNUNET_DNS_Request) + rr->payload_length >= | ||
486 | GNUNET_MAX_MESSAGE_SIZE) | ||
487 | { | ||
488 | GNUNET_break (0); | ||
489 | cleanup_rr (rr); | ||
490 | return; | ||
491 | } | ||
492 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
493 | "Sending information about request %llu to local client\n", | ||
494 | (unsigned long long) rr->request_id); | ||
495 | env = GNUNET_MQ_msg_extra (req, | ||
496 | rr->payload_length, | ||
497 | GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST); | ||
498 | req->reserved = htonl (0); | ||
499 | req->request_id = rr->request_id; | ||
500 | GNUNET_memcpy (&req[1], | ||
501 | rr->payload, | ||
502 | rr->payload_length); | ||
503 | GNUNET_MQ_send (cr->mq, | ||
504 | env); | ||
505 | } | ||
506 | |||
507 | |||
508 | /** | ||
509 | * Callback called from DNSSTUB resolver when a resolution | ||
510 | * succeeded. | ||
511 | * | ||
512 | * @param cls NULL | ||
513 | * @param dns the response itself | ||
514 | * @param r number of bytes in dns | ||
515 | */ | ||
516 | static void | ||
517 | process_dns_result (void *cls, | ||
518 | const struct GNUNET_TUN_DnsHeader *dns, | ||
519 | size_t r); | ||
520 | |||
521 | |||
522 | /** | ||
523 | * A client has completed its processing for this | ||
524 | * request. Move on. | ||
525 | * | ||
526 | * @param rr request to process further | ||
527 | */ | ||
528 | static void | ||
529 | next_phase (struct RequestRecord *rr) | ||
530 | { | ||
531 | struct ClientRecord *cr; | ||
532 | int nz; | ||
533 | |||
534 | if (rr->phase == RP_DROP) | ||
535 | { | ||
536 | cleanup_rr (rr); | ||
537 | return; | ||
538 | } | ||
539 | nz = -1; | ||
540 | for (unsigned int j = 0; j < rr->client_wait_list_length; j++) | ||
541 | { | ||
542 | if (NULL != rr->client_wait_list[j]) | ||
543 | { | ||
544 | nz = (int) j; | ||
545 | break; | ||
546 | } | ||
547 | } | ||
548 | if (-1 != nz) | ||
549 | { | ||
550 | send_request_to_client (rr, | ||
551 | rr->client_wait_list[nz]); | ||
552 | return; | ||
553 | } | ||
554 | /* done with current phase, advance! */ | ||
555 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
556 | "Request %llu now in phase %d\n", | ||
557 | (unsigned long long) rr->request_id, | ||
558 | rr->phase); | ||
559 | switch (rr->phase) | ||
560 | { | ||
561 | case RP_INIT: | ||
562 | rr->phase = RP_REQUEST_MONITOR; | ||
563 | for (cr = clients_head; NULL != cr; cr = cr->next) | ||
564 | { | ||
565 | if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR)) | ||
566 | GNUNET_array_append (rr->client_wait_list, | ||
567 | rr->client_wait_list_length, | ||
568 | cr); | ||
569 | } | ||
570 | next_phase (rr); | ||
571 | return; | ||
572 | |||
573 | case RP_REQUEST_MONITOR: | ||
574 | rr->phase = RP_QUERY; | ||
575 | for (cr = clients_head; NULL != cr; cr = cr->next) | ||
576 | { | ||
577 | if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION)) | ||
578 | GNUNET_array_append (rr->client_wait_list, | ||
579 | rr->client_wait_list_length, | ||
580 | cr); | ||
581 | } | ||
582 | next_phase (rr); | ||
583 | return; | ||
584 | |||
585 | case RP_QUERY: | ||
586 | #if 0 | ||
587 | /* TODO: optionally, use this to forward DNS requests to the | ||
588 | * original* DNS server instead of the one we have configured... | ||
589 | (but then we need to create a fresh dnsstub for each request | ||
590 | * and* manage the timeout) */ | ||
591 | switch (rr->dst_addr.ss_family) | ||
592 | { | ||
593 | case AF_INET: | ||
594 | salen = sizeof(struct sockaddr_in); | ||
595 | sa = (const struct sockaddr *) &rr->dst_addr; | ||
596 | break; | ||
597 | |||
598 | case AF_INET6: | ||
599 | salen = sizeof(struct sockaddr_in6); | ||
600 | sa = (const struct sockaddr *) &rr->dst_addr; | ||
601 | break; | ||
602 | |||
603 | default: | ||
604 | GNUNET_assert (0); | ||
605 | } | ||
606 | #endif | ||
607 | rr->phase = RP_INTERNET_DNS; | ||
608 | rr->rs = GNUNET_DNSSTUB_resolve (dnsstub, | ||
609 | rr->payload, | ||
610 | rr->payload_length, | ||
611 | &process_dns_result, | ||
612 | NULL); | ||
613 | if (NULL == rr->rs) | ||
614 | { | ||
615 | GNUNET_STATISTICS_update (stats, | ||
616 | gettext_noop ( | ||
617 | "# DNS exit failed (failed to open socket)"), | ||
618 | 1, | ||
619 | GNUNET_NO); | ||
620 | cleanup_rr (rr); | ||
621 | return; | ||
622 | } | ||
623 | return; | ||
624 | |||
625 | case RP_INTERNET_DNS: | ||
626 | rr->phase = RP_MODIFY; | ||
627 | for (cr = clients_head; NULL != cr; cr = cr->next) | ||
628 | { | ||
629 | if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION)) | ||
630 | GNUNET_array_append (rr->client_wait_list, | ||
631 | rr->client_wait_list_length, | ||
632 | cr); | ||
633 | } | ||
634 | next_phase (rr); | ||
635 | return; | ||
636 | |||
637 | case RP_MODIFY: | ||
638 | rr->phase = RP_RESPONSE_MONITOR; | ||
639 | for (cr = clients_head; NULL != cr; cr = cr->next) | ||
640 | { | ||
641 | if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR)) | ||
642 | GNUNET_array_append (rr->client_wait_list, | ||
643 | rr->client_wait_list_length, | ||
644 | cr); | ||
645 | } | ||
646 | next_phase (rr); | ||
647 | return; | ||
648 | |||
649 | case RP_RESPONSE_MONITOR: | ||
650 | request_done (rr); | ||
651 | break; | ||
652 | |||
653 | case RP_DROP: | ||
654 | cleanup_rr (rr); | ||
655 | break; | ||
656 | |||
657 | default: | ||
658 | GNUNET_break (0); | ||
659 | cleanup_rr (rr); | ||
660 | break; | ||
661 | } | ||
662 | } | ||
663 | |||
664 | |||
665 | /** | ||
666 | * A client connected, setup our data structures. | ||
667 | * | ||
668 | * @param cls unused | ||
669 | * @param client handle of client that connected | ||
670 | * @param mq message queue to talk to @a client | ||
671 | * @return our `struct ClientRecord` | ||
672 | */ | ||
673 | static void * | ||
674 | client_connect_cb (void *cls, | ||
675 | struct GNUNET_SERVICE_Client *client, | ||
676 | struct GNUNET_MQ_Handle *mq) | ||
677 | { | ||
678 | struct ClientRecord *cr = cls; | ||
679 | |||
680 | cr = GNUNET_new (struct ClientRecord); | ||
681 | cr->client = client; | ||
682 | cr->mq = mq; | ||
683 | GNUNET_CONTAINER_DLL_insert (clients_head, | ||
684 | clients_tail, | ||
685 | cr); | ||
686 | return cr; | ||
687 | } | ||
688 | |||
689 | |||
690 | /** | ||
691 | * A client disconnected, clean up after it. | ||
692 | * | ||
693 | * @param cls unused | ||
694 | * @param client handle of client that disconnected | ||
695 | * @param app_ctx our `struct ClientRecord` | ||
696 | */ | ||
697 | static void | ||
698 | client_disconnect_cb (void *cls, | ||
699 | struct GNUNET_SERVICE_Client *client, | ||
700 | void *app_ctx) | ||
701 | { | ||
702 | struct ClientRecord *cr = app_ctx; | ||
703 | struct RequestRecord *rr; | ||
704 | |||
705 | GNUNET_CONTAINER_DLL_remove (clients_head, | ||
706 | clients_tail, | ||
707 | cr); | ||
708 | for (unsigned int i = 0; i < UINT16_MAX; i++) | ||
709 | { | ||
710 | rr = &requests[i]; | ||
711 | if (0 == rr->client_wait_list_length) | ||
712 | continue; /* not in use */ | ||
713 | for (unsigned int j = 0; j < rr->client_wait_list_length; j++) | ||
714 | { | ||
715 | if (rr->client_wait_list[j] == cr) | ||
716 | { | ||
717 | rr->client_wait_list[j] = NULL; | ||
718 | next_phase (rr); | ||
719 | } | ||
720 | } | ||
721 | } | ||
722 | GNUNET_free (cr); | ||
723 | } | ||
724 | |||
725 | |||
726 | /** | ||
727 | * Callback called from DNSSTUB resolver when a resolution | ||
728 | * succeeded. | ||
729 | * | ||
730 | * @param cls NULL | ||
731 | * @param dns the response itself | ||
732 | * @param r number of bytes in dns | ||
733 | */ | ||
734 | static void | ||
735 | process_dns_result (void *cls, | ||
736 | const struct GNUNET_TUN_DnsHeader *dns, | ||
737 | size_t r) | ||
738 | { | ||
739 | struct RequestRecord *rr; | ||
740 | |||
741 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
742 | "Processing DNS result from stub resolver\n"); | ||
743 | GNUNET_assert (NULL == cls); | ||
744 | if (NULL == dns) | ||
745 | return; /* ignore */ | ||
746 | |||
747 | rr = &requests[dns->id]; | ||
748 | if (rr->phase != RP_INTERNET_DNS) | ||
749 | { | ||
750 | /* unexpected / bogus reply */ | ||
751 | GNUNET_STATISTICS_update (stats, | ||
752 | gettext_noop ( | ||
753 | "# External DNS response discarded (no matching request)"), | ||
754 | 1, GNUNET_NO); | ||
755 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
756 | "Received DNS reply that does not match any pending request. Dropping.\n"); | ||
757 | return; | ||
758 | } | ||
759 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
760 | "Got a response from the stub resolver for DNS request %llu intercepted locally!\n", | ||
761 | (unsigned long long) rr->request_id); | ||
762 | GNUNET_free (rr->payload); | ||
763 | rr->payload = GNUNET_malloc (r); | ||
764 | GNUNET_memcpy (rr->payload, | ||
765 | dns, | ||
766 | r); | ||
767 | rr->payload_length = r; | ||
768 | next_phase (rr); | ||
769 | } | ||
770 | |||
771 | |||
772 | /** | ||
773 | * We got a new client. Make sure all new DNS requests pass by its desk. | ||
774 | * | ||
775 | * @param cls the client | ||
776 | * @param reg the init message | ||
777 | */ | ||
778 | static void | ||
779 | handle_client_init (void *cls, | ||
780 | const struct GNUNET_DNS_Register *reg) | ||
781 | { | ||
782 | struct ClientRecord *cr = cls; | ||
783 | |||
784 | cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags); | ||
785 | GNUNET_SERVICE_client_continue (cr->client); | ||
786 | } | ||
787 | |||
788 | |||
789 | /** | ||
790 | * Check a response from a client. | ||
791 | * | ||
792 | * @param cls the client | ||
793 | * @param resp the response | ||
794 | * @return #GNUNET_OK (always fine) | ||
795 | */ | ||
796 | static int | ||
797 | check_client_response (void *cls, | ||
798 | const struct GNUNET_DNS_Response *resp) | ||
799 | { | ||
800 | return GNUNET_OK; /* any payload is acceptable */ | ||
801 | } | ||
802 | |||
803 | |||
804 | /** | ||
805 | * Handle a response from a client. | ||
806 | * | ||
807 | * @param cls the client | ||
808 | * @param resp the response | ||
809 | */ | ||
810 | static void | ||
811 | handle_client_response (void *cls, | ||
812 | const struct GNUNET_DNS_Response *resp) | ||
813 | { | ||
814 | struct ClientRecord *cr = cls; | ||
815 | struct RequestRecord *rr; | ||
816 | uint16_t msize; | ||
817 | uint16_t off; | ||
818 | |||
819 | msize = ntohs (resp->header.size); | ||
820 | off = (uint16_t) resp->request_id; | ||
821 | rr = &requests[off]; | ||
822 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
823 | "Received DNS response with ID %llu from local client!\n", | ||
824 | (unsigned long long) resp->request_id); | ||
825 | if (rr->request_id != resp->request_id) | ||
826 | { | ||
827 | GNUNET_STATISTICS_update (stats, | ||
828 | gettext_noop ( | ||
829 | "# Client response discarded (no matching request)"), | ||
830 | 1, | ||
831 | GNUNET_NO); | ||
832 | GNUNET_SERVICE_client_continue (cr->client); | ||
833 | return; | ||
834 | } | ||
835 | for (unsigned int i = 0; i < rr->client_wait_list_length; i++) | ||
836 | { | ||
837 | if (NULL == rr->client_wait_list[i]) | ||
838 | continue; | ||
839 | if (rr->client_wait_list[i] != cr) | ||
840 | continue; | ||
841 | rr->client_wait_list[i] = NULL; | ||
842 | switch (ntohl (resp->drop_flag)) | ||
843 | { | ||
844 | case 0: /* drop */ | ||
845 | rr->phase = RP_DROP; | ||
846 | break; | ||
847 | |||
848 | case 1: /* no change */ | ||
849 | break; | ||
850 | |||
851 | case 2: /* update */ | ||
852 | msize -= sizeof(struct GNUNET_DNS_Response); | ||
853 | if ((sizeof(struct GNUNET_TUN_DnsHeader) > msize) || | ||
854 | (RP_REQUEST_MONITOR == rr->phase) || | ||
855 | (RP_RESPONSE_MONITOR == rr->phase)) | ||
856 | { | ||
857 | GNUNET_break (0); | ||
858 | GNUNET_SERVICE_client_drop (cr->client); | ||
859 | next_phase (rr); | ||
860 | return; | ||
861 | } | ||
862 | GNUNET_free (rr->payload); | ||
863 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
864 | "Changing DNS reply according to client specifications\n"); | ||
865 | rr->payload = GNUNET_malloc (msize); | ||
866 | rr->payload_length = msize; | ||
867 | GNUNET_memcpy (rr->payload, &resp[1], msize); | ||
868 | if (rr->phase == RP_QUERY) | ||
869 | { | ||
870 | /* clear wait list, we're moving to MODIFY phase next */ | ||
871 | GNUNET_array_grow (rr->client_wait_list, | ||
872 | rr->client_wait_list_length, | ||
873 | 0); | ||
874 | } | ||
875 | /* if query changed to answer, move past DNS resolution phase... */ | ||
876 | if ((RP_QUERY == rr->phase) && | ||
877 | (rr->payload_length > sizeof(struct GNUNET_TUN_DnsHeader)) && | ||
878 | ( ((struct GNUNET_TUN_DnsFlags*) &(((struct | ||
879 | GNUNET_TUN_DnsHeader*) rr-> | ||
880 | payload)->flags))-> | ||
881 | query_or_response == 1) ) | ||
882 | { | ||
883 | rr->phase = RP_INTERNET_DNS; | ||
884 | GNUNET_array_grow (rr->client_wait_list, | ||
885 | rr->client_wait_list_length, | ||
886 | 0); | ||
887 | } | ||
888 | break; | ||
889 | } | ||
890 | next_phase (rr); | ||
891 | GNUNET_SERVICE_client_continue (cr->client); | ||
892 | return; | ||
893 | } | ||
894 | /* odd, client was not on our list for the request, that ought | ||
895 | to be an error */ | ||
896 | GNUNET_break (0); | ||
897 | GNUNET_SERVICE_client_drop (cr->client); | ||
898 | } | ||
899 | |||
900 | |||
901 | /** | ||
902 | * Functions with this signature are called whenever a complete | ||
903 | * message is received by the tokenizer from the DNS hijack process. | ||
904 | * | ||
905 | * @param cls closure | ||
906 | * @param message the actual message, a DNS request we should handle | ||
907 | */ | ||
908 | static int | ||
909 | process_helper_messages (void *cls, | ||
910 | const struct GNUNET_MessageHeader *message) | ||
911 | { | ||
912 | uint16_t msize; | ||
913 | const struct GNUNET_TUN_Layer2PacketHeader *tun; | ||
914 | const struct GNUNET_TUN_IPv4Header *ip4; | ||
915 | const struct GNUNET_TUN_IPv6Header *ip6; | ||
916 | const struct GNUNET_TUN_UdpHeader *udp; | ||
917 | const struct GNUNET_TUN_DnsHeader *dns; | ||
918 | struct RequestRecord *rr; | ||
919 | struct sockaddr_in *srca4; | ||
920 | struct sockaddr_in6 *srca6; | ||
921 | struct sockaddr_in *dsta4; | ||
922 | struct sockaddr_in6 *dsta6; | ||
923 | |||
924 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
925 | "Intercepted message via DNS hijacker\n"); | ||
926 | msize = ntohs (message->size); | ||
927 | if (msize < sizeof(struct GNUNET_MessageHeader) + sizeof(struct | ||
928 | GNUNET_TUN_Layer2PacketHeader) | ||
929 | + sizeof(struct GNUNET_TUN_IPv4Header)) | ||
930 | { | ||
931 | /* non-IP packet received on TUN!? */ | ||
932 | GNUNET_break (0); | ||
933 | return GNUNET_OK; | ||
934 | } | ||
935 | msize -= sizeof(struct GNUNET_MessageHeader); | ||
936 | tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1]; | ||
937 | msize -= sizeof(struct GNUNET_TUN_Layer2PacketHeader); | ||
938 | switch (ntohs (tun->proto)) | ||
939 | { | ||
940 | case ETH_P_IPV4: | ||
941 | ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1]; | ||
942 | ip6 = NULL; /* make compiler happy */ | ||
943 | if ((msize < sizeof(struct GNUNET_TUN_IPv4Header)) || | ||
944 | (ip4->version != 4) || | ||
945 | (ip4->header_length != sizeof(struct GNUNET_TUN_IPv4Header) / 4) || | ||
946 | (ntohs (ip4->total_length) != msize) || | ||
947 | (ip4->protocol != IPPROTO_UDP)) | ||
948 | { | ||
949 | /* non-IP/UDP packet received on TUN (or with options) */ | ||
950 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
951 | _ ("Received malformed IPv4-UDP packet on TUN interface.\n")); | ||
952 | return GNUNET_OK; | ||
953 | } | ||
954 | udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1]; | ||
955 | msize -= sizeof(struct GNUNET_TUN_IPv4Header); | ||
956 | break; | ||
957 | |||
958 | case ETH_P_IPV6: | ||
959 | ip4 = NULL; /* make compiler happy */ | ||
960 | ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1]; | ||
961 | if ((msize < sizeof(struct GNUNET_TUN_IPv6Header)) || | ||
962 | (ip6->version != 6) || | ||
963 | (ntohs (ip6->payload_length) != msize - sizeof(struct | ||
964 | GNUNET_TUN_IPv6Header)) | ||
965 | || | ||
966 | (ip6->next_header != IPPROTO_UDP)) | ||
967 | { | ||
968 | /* non-IP/UDP packet received on TUN (or with extensions) */ | ||
969 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
970 | _ ("Received malformed IPv6-UDP packet on TUN interface.\n")); | ||
971 | return GNUNET_OK; | ||
972 | } | ||
973 | udp = (const struct GNUNET_TUN_UdpHeader *) &ip6[1]; | ||
974 | msize -= sizeof(struct GNUNET_TUN_IPv6Header); | ||
975 | break; | ||
976 | |||
977 | default: | ||
978 | /* non-IP packet received on TUN!? */ | ||
979 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
980 | _ ( | ||
981 | "Got non-IP packet with %u bytes and protocol %u from TUN\n"), | ||
982 | (unsigned int) msize, | ||
983 | ntohs (tun->proto)); | ||
984 | return GNUNET_OK; | ||
985 | } | ||
986 | if ((msize <= sizeof(struct GNUNET_TUN_UdpHeader) + sizeof(struct | ||
987 | GNUNET_TUN_DnsHeader)) | ||
988 | || | ||
989 | (DNS_PORT != ntohs (udp->destination_port))) | ||
990 | { | ||
991 | /* non-DNS packet received on TUN, ignore */ | ||
992 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
993 | _ ("DNS interceptor got non-DNS packet (dropped)\n")); | ||
994 | GNUNET_STATISTICS_update (stats, | ||
995 | gettext_noop ( | ||
996 | "# Non-DNS UDP packet received via TUN interface"), | ||
997 | 1, GNUNET_NO); | ||
998 | return GNUNET_OK; | ||
999 | } | ||
1000 | msize -= sizeof(struct GNUNET_TUN_UdpHeader); | ||
1001 | dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1]; | ||
1002 | rr = &requests[dns->id]; | ||
1003 | |||
1004 | /* clean up from previous request */ | ||
1005 | GNUNET_free (rr->payload); | ||
1006 | rr->payload = NULL; | ||
1007 | GNUNET_array_grow (rr->client_wait_list, | ||
1008 | rr->client_wait_list_length, | ||
1009 | 0); | ||
1010 | |||
1011 | /* setup new request */ | ||
1012 | rr->phase = RP_INIT; | ||
1013 | switch (ntohs (tun->proto)) | ||
1014 | { | ||
1015 | case ETH_P_IPV4: | ||
1016 | { | ||
1017 | srca4 = (struct sockaddr_in*) &rr->src_addr; | ||
1018 | dsta4 = (struct sockaddr_in*) &rr->dst_addr; | ||
1019 | memset (srca4, 0, sizeof(struct sockaddr_in)); | ||
1020 | memset (dsta4, 0, sizeof(struct sockaddr_in)); | ||
1021 | srca4->sin_family = AF_INET; | ||
1022 | dsta4->sin_family = AF_INET; | ||
1023 | srca4->sin_addr = ip4->source_address; | ||
1024 | dsta4->sin_addr = ip4->destination_address; | ||
1025 | srca4->sin_port = udp->source_port; | ||
1026 | dsta4->sin_port = udp->destination_port; | ||
1027 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
1028 | srca4->sin_len = sizeof(struct sockaddr_in); | ||
1029 | dsta4->sin_len = sizeof(struct sockaddr_in); | ||
1030 | #endif | ||
1031 | } | ||
1032 | break; | ||
1033 | |||
1034 | case ETH_P_IPV6: | ||
1035 | { | ||
1036 | srca6 = (struct sockaddr_in6*) &rr->src_addr; | ||
1037 | dsta6 = (struct sockaddr_in6*) &rr->dst_addr; | ||
1038 | memset (srca6, 0, sizeof(struct sockaddr_in6)); | ||
1039 | memset (dsta6, 0, sizeof(struct sockaddr_in6)); | ||
1040 | srca6->sin6_family = AF_INET6; | ||
1041 | dsta6->sin6_family = AF_INET6; | ||
1042 | srca6->sin6_addr = ip6->source_address; | ||
1043 | dsta6->sin6_addr = ip6->destination_address; | ||
1044 | srca6->sin6_port = udp->source_port; | ||
1045 | dsta6->sin6_port = udp->destination_port; | ||
1046 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
1047 | srca6->sin6_len = sizeof(struct sockaddr_in6); | ||
1048 | dsta6->sin6_len = sizeof(struct sockaddr_in6); | ||
1049 | #endif | ||
1050 | } | ||
1051 | break; | ||
1052 | |||
1053 | default: | ||
1054 | GNUNET_assert (0); | ||
1055 | } | ||
1056 | rr->payload = GNUNET_malloc (msize); | ||
1057 | rr->payload_length = msize; | ||
1058 | GNUNET_memcpy (rr->payload, dns, msize); | ||
1059 | rr->request_id = dns->id | (request_id_gen << 16); | ||
1060 | request_id_gen++; | ||
1061 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1062 | "Creating new DNS request %llu\n", | ||
1063 | (unsigned long long) rr->request_id); | ||
1064 | GNUNET_STATISTICS_update (stats, | ||
1065 | gettext_noop ( | ||
1066 | "# DNS requests received via TUN interface"), | ||
1067 | 1, GNUNET_NO); | ||
1068 | /* start request processing state machine */ | ||
1069 | next_phase (rr); | ||
1070 | return GNUNET_OK; | ||
1071 | } | ||
1072 | |||
1073 | |||
1074 | /** | ||
1075 | * @param cls closure | ||
1076 | * @param cfg_ configuration to use | ||
1077 | * @param service the initialized service | ||
1078 | */ | ||
1079 | static void | ||
1080 | run (void *cls, | ||
1081 | const struct GNUNET_CONFIGURATION_Handle *cfg_, | ||
1082 | struct GNUNET_SERVICE_Handle *service) | ||
1083 | { | ||
1084 | char *ifc_name; | ||
1085 | char *ipv4addr; | ||
1086 | char *ipv4mask; | ||
1087 | char *ipv6addr; | ||
1088 | char *ipv6prefix; | ||
1089 | char *dns_exit; | ||
1090 | char *binary; | ||
1091 | int nortsetup; | ||
1092 | |||
1093 | cfg = cfg_; | ||
1094 | stats = GNUNET_STATISTICS_create ("dns", cfg); | ||
1095 | GNUNET_SCHEDULER_add_shutdown (&cleanup_task, | ||
1096 | cls); | ||
1097 | dnsstub = GNUNET_DNSSTUB_start (128); | ||
1098 | /* TODO: support multiple DNS_EXIT servers being configured */ | ||
1099 | /* TODO: see above TODO on using DNS server from original packet. | ||
1100 | Not sure which is best... */ | ||
1101 | dns_exit = NULL; | ||
1102 | if ((GNUNET_OK != | ||
1103 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
1104 | "dns", | ||
1105 | "DNS_EXIT", | ||
1106 | &dns_exit)) || | ||
1107 | (GNUNET_OK != | ||
1108 | GNUNET_DNSSTUB_add_dns_ip (dnsstub, | ||
1109 | dns_exit))) | ||
1110 | { | ||
1111 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, | ||
1112 | "dns", | ||
1113 | "DNS_EXIT", | ||
1114 | _ ("need a valid IPv4 or IPv6 address\n")); | ||
1115 | GNUNET_free (dns_exit); | ||
1116 | } | ||
1117 | binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-dns"); | ||
1118 | |||
1119 | if (GNUNET_YES != | ||
1120 | GNUNET_OS_check_helper_binary (binary, | ||
1121 | GNUNET_YES, | ||
1122 | NULL)) // TODO: once we have a windows-testcase, add test parameters here | ||
1123 | { | ||
1124 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1125 | _ ("`%s' is not SUID or the path is invalid, " | ||
1126 | "will not run DNS interceptor\n"), | ||
1127 | binary); | ||
1128 | global_ret = 1; | ||
1129 | GNUNET_free (binary); | ||
1130 | return; | ||
1131 | } | ||
1132 | GNUNET_free (binary); | ||
1133 | |||
1134 | helper_argv[0] = GNUNET_strdup ("gnunet-dns"); | ||
1135 | if (GNUNET_SYSERR == | ||
1136 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
1137 | "dns", | ||
1138 | "IFNAME", | ||
1139 | &ifc_name)) | ||
1140 | { | ||
1141 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1142 | "No entry 'IFNAME' in configuration!\n"); | ||
1143 | GNUNET_free (binary); | ||
1144 | GNUNET_SCHEDULER_shutdown (); | ||
1145 | return; | ||
1146 | } | ||
1147 | helper_argv[1] = ifc_name; | ||
1148 | if ((GNUNET_SYSERR == | ||
1149 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
1150 | "dns", | ||
1151 | "IPV6ADDR", | ||
1152 | &ipv6addr))) | ||
1153 | { | ||
1154 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1155 | "No entry 'IPV6ADDR' in configuration!\n"); | ||
1156 | GNUNET_free (binary); | ||
1157 | GNUNET_SCHEDULER_shutdown (); | ||
1158 | return; | ||
1159 | } | ||
1160 | helper_argv[2] = ipv6addr; | ||
1161 | if (GNUNET_SYSERR == | ||
1162 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
1163 | "dns", | ||
1164 | "IPV6PREFIX", | ||
1165 | &ipv6prefix)) | ||
1166 | { | ||
1167 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1168 | "No entry 'IPV6PREFIX' in configuration!\n"); | ||
1169 | GNUNET_free (binary); | ||
1170 | GNUNET_SCHEDULER_shutdown (); | ||
1171 | return; | ||
1172 | } | ||
1173 | helper_argv[3] = ipv6prefix; | ||
1174 | |||
1175 | if (GNUNET_SYSERR == | ||
1176 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
1177 | "dns", | ||
1178 | "IPV4ADDR", | ||
1179 | &ipv4addr)) | ||
1180 | { | ||
1181 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1182 | "No entry 'IPV4ADDR' in configuration!\n"); | ||
1183 | GNUNET_free (binary); | ||
1184 | GNUNET_SCHEDULER_shutdown (); | ||
1185 | return; | ||
1186 | } | ||
1187 | helper_argv[4] = ipv4addr; | ||
1188 | if (GNUNET_SYSERR == | ||
1189 | GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK", | ||
1190 | &ipv4mask)) | ||
1191 | { | ||
1192 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1193 | "No entry 'IPV4MASK' in configuration!\n"); | ||
1194 | GNUNET_free (binary); | ||
1195 | GNUNET_SCHEDULER_shutdown (); | ||
1196 | return; | ||
1197 | } | ||
1198 | helper_argv[5] = ipv4mask; | ||
1199 | |||
1200 | nortsetup = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dns", | ||
1201 | "SKIP_ROUTING_SETUP"); | ||
1202 | if (GNUNET_YES == nortsetup) | ||
1203 | helper_argv[6] = GNUNET_strdup ("1"); | ||
1204 | else | ||
1205 | helper_argv[6] = GNUNET_strdup ("0"); | ||
1206 | |||
1207 | helper_argv[7] = NULL; | ||
1208 | hijacker = GNUNET_HELPER_start (GNUNET_NO, | ||
1209 | binary, | ||
1210 | helper_argv, | ||
1211 | &process_helper_messages, | ||
1212 | NULL, NULL); | ||
1213 | GNUNET_free (binary); | ||
1214 | } | ||
1215 | |||
1216 | |||
1217 | /** | ||
1218 | * Define "main" method using service macro. | ||
1219 | */ | ||
1220 | GNUNET_SERVICE_MAIN | ||
1221 | ("dns", | ||
1222 | GNUNET_SERVICE_OPTION_NONE, | ||
1223 | &run, | ||
1224 | &client_connect_cb, | ||
1225 | &client_disconnect_cb, | ||
1226 | NULL, | ||
1227 | GNUNET_MQ_hd_fixed_size (client_init, | ||
1228 | GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, | ||
1229 | struct GNUNET_DNS_Register, | ||
1230 | NULL), | ||
1231 | GNUNET_MQ_hd_var_size (client_response, | ||
1232 | GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, | ||
1233 | struct GNUNET_DNS_Response, | ||
1234 | NULL), | ||
1235 | GNUNET_MQ_handler_end ()); | ||
1236 | |||
1237 | |||
1238 | /* FIXME: this might need a port on systems without 'getresgid' */ | ||
1239 | #if HAVE_GETRESGID | ||
1240 | /** | ||
1241 | * Enable use of SGID capabilities on POSIX | ||
1242 | */ | ||
1243 | void __attribute__ ((constructor)) | ||
1244 | GNUNET_DNS_init () | ||
1245 | { | ||
1246 | gid_t rgid; | ||
1247 | gid_t egid; | ||
1248 | gid_t sgid; | ||
1249 | |||
1250 | if (-1 == getresgid (&rgid, | ||
1251 | &egid, | ||
1252 | &sgid)) | ||
1253 | { | ||
1254 | fprintf (stderr, | ||
1255 | "getresgid failed: %s\n", | ||
1256 | strerror (errno)); | ||
1257 | } | ||
1258 | else if (sgid != rgid) | ||
1259 | { | ||
1260 | if (-1 == setregid (sgid, | ||
1261 | sgid)) | ||
1262 | fprintf (stderr, | ||
1263 | "setregid failed: %s\n", | ||
1264 | strerror (errno)); | ||
1265 | } | ||
1266 | } | ||
1267 | |||
1268 | |||
1269 | #endif | ||
1270 | |||
1271 | |||
1272 | /* end of gnunet-service-dns.c */ | ||