aboutsummaryrefslogtreecommitdiff
path: root/src/service/dht/plugin_dhtu_ip.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/dht/plugin_dhtu_ip.c')
-rw-r--r--src/service/dht/plugin_dhtu_ip.c1170
1 files changed, 1170 insertions, 0 deletions
diff --git a/src/service/dht/plugin_dhtu_ip.c b/src/service/dht/plugin_dhtu_ip.c
new file mode 100644
index 000000000..3c01257dc
--- /dev/null
+++ b/src/service/dht/plugin_dhtu_ip.c
@@ -0,0 +1,1170 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2021, 2022 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 * @author Christian Grothoff
23 *
24 * @file plugin_dhtu_ip.c
25 * @brief plain IP based DHT network underlay
26 */
27#include "platform.h"
28#include "gnunet_dhtu_plugin.h"
29#include "plugin_dhtu_ip.h"
30
31/**
32 * How frequently should we re-scan our local interfaces for IPs?
33 */
34#define SCAN_FREQ GNUNET_TIME_UNIT_MINUTES
35
36/**
37 * Maximum number of concurrently active destinations to support.
38 */
39#define MAX_DESTS 256
40
41
42/**
43 * Opaque handle that the underlay offers for our address to be used when
44 * sending messages to another peer.
45 */
46struct GNUNET_DHTU_Source
47{
48
49 /**
50 * Kept in a DLL.
51 */
52 struct GNUNET_DHTU_Source *next;
53
54 /**
55 * Kept in a DLL.
56 */
57 struct GNUNET_DHTU_Source *prev;
58
59 /**
60 * Application context for this source.
61 */
62 void *app_ctx;
63
64 /**
65 * Address in URL form ("ip+udp://$PID/$IP:$PORT")
66 */
67 char *address;
68
69 /**
70 * My actual address.
71 */
72 struct sockaddr_storage addr;
73
74 /**
75 * Number of bytes in @a addr.
76 */
77 socklen_t addrlen;
78
79 /**
80 * Last generation this address was observed.
81 */
82 unsigned int scan_generation;
83
84};
85
86
87/**
88 * Opaque handle that the underlay offers for the target peer when sending
89 * messages to another peer.
90 */
91struct GNUNET_DHTU_Target
92{
93
94 /**
95 * Kept in a DLL.
96 */
97 struct GNUNET_DHTU_Target *next;
98
99 /**
100 * Kept in a DLL.
101 */
102 struct GNUNET_DHTU_Target *prev;
103
104 /**
105 * Application context for this target.
106 */
107 void *app_ctx;
108
109 /**
110 * Head of preferences expressed for this target.
111 */
112 struct GNUNET_DHTU_PreferenceHandle *ph_head;
113
114 /**
115 * Tail of preferences expressed for this target.
116 */
117 struct GNUNET_DHTU_PreferenceHandle *ph_tail;
118
119 /**
120 * Peer's identity.
121 */
122 struct GNUNET_PeerIdentity pid;
123
124 /**
125 * Target IP address.
126 */
127 struct sockaddr_storage addr;
128
129 /**
130 * Number of bytes in @a addr.
131 */
132 socklen_t addrlen;
133
134 /**
135 * Preference counter, length of the @a ph_head DLL.
136 */
137 unsigned int ph_count;
138
139};
140
141/**
142 * Opaque handle expressing a preference of the DHT to
143 * keep a particular target connected.
144 */
145struct GNUNET_DHTU_PreferenceHandle
146{
147 /**
148 * Kept in a DLL.
149 */
150 struct GNUNET_DHTU_PreferenceHandle *next;
151
152 /**
153 * Kept in a DLL.
154 */
155 struct GNUNET_DHTU_PreferenceHandle *prev;
156
157 /**
158 * Target a preference was expressed for.
159 */
160 struct GNUNET_DHTU_Target *target;
161};
162
163
164/**
165 * Closure for all plugin functions.
166 */
167struct Plugin
168{
169 /**
170 * Callbacks into the DHT.
171 */
172 struct GNUNET_DHTU_PluginEnvironment *env;
173
174 /**
175 * Head of sources where we receive traffic.
176 */
177 struct GNUNET_DHTU_Source *src_head;
178
179 /**
180 * Tail of sources where we receive traffic.
181 */
182 struct GNUNET_DHTU_Source *src_tail;
183
184 /**
185 * Head of destinations that are active. Sorted by
186 * last use, with latest used at the head.
187 */
188 struct GNUNET_DHTU_Target *dst_head;
189
190 /**
191 * Tail of destinations that are active.
192 */
193 struct GNUNET_DHTU_Target *dst_tail;
194
195 /**
196 * Map from hashes of sockaddrs to targets.
197 */
198 struct GNUNET_CONTAINER_MultiHashMap *dsts;
199
200 /**
201 * Task that scans for IP address changes.
202 */
203 struct GNUNET_SCHEDULER_Task *scan_task;
204
205 /**
206 * Task that reads incoming UDP packets.
207 */
208 struct GNUNET_SCHEDULER_Task *read_task;
209
210 /**
211 * Port we bind to.
212 */
213 char *port;
214
215 /**
216 * My UDP socket.
217 */
218 struct GNUNET_NETWORK_Handle *sock;
219
220 /**
221 * My identity.
222 */
223 struct GNUNET_PeerIdentity my_id;
224
225 /**
226 * How often have we scanned for IPs?
227 */
228 unsigned int scan_generation;
229
230 /**
231 * Port as a 16-bit value.
232 */
233 uint16_t port16;
234};
235
236
237/**
238 * Create a target to which we may send traffic.
239 *
240 * @param plugin our plugin
241 * @param pid presumed identity of the target
242 * @param addr target address
243 * @param addrlen number of bytes in @a addr
244 * @return new target object
245 */
246static struct GNUNET_DHTU_Target *
247create_target (struct Plugin *plugin,
248 const struct GNUNET_PeerIdentity *pid,
249 const struct sockaddr *addr,
250 socklen_t addrlen)
251{
252 struct GNUNET_DHTU_Target *dst;
253
254 if (MAX_DESTS <=
255 GNUNET_CONTAINER_multihashmap_size (plugin->dsts))
256 {
257 struct GNUNET_HashCode key;
258
259 dst = NULL;
260 for (struct GNUNET_DHTU_Target *pos = plugin->dst_head;
261 NULL != pos;
262 pos = pos->next)
263 {
264 /* >= here assures we remove oldest entries first */
265 if ( (NULL == dst) ||
266 (dst->ph_count >= pos->ph_count) )
267 dst = pos;
268 }
269 GNUNET_assert (NULL != dst);
270 plugin->env->disconnect_cb (dst->app_ctx);
271 GNUNET_CRYPTO_hash (&dst->addr,
272 dst->addrlen,
273 &key);
274 GNUNET_assert (GNUNET_YES ==
275 GNUNET_CONTAINER_multihashmap_remove (plugin->dsts,
276 &key,
277 dst));
278 GNUNET_CONTAINER_DLL_remove (plugin->dst_head,
279 plugin->dst_tail,
280 dst);
281 GNUNET_assert (NULL == dst->ph_head);
282 GNUNET_free (dst);
283 }
284 dst = GNUNET_new (struct GNUNET_DHTU_Target);
285 dst->addrlen = addrlen;
286 dst->pid = *pid;
287 memcpy (&dst->addr,
288 addr,
289 addrlen);
290 GNUNET_CONTAINER_DLL_insert (plugin->dst_head,
291 plugin->dst_tail,
292 dst);
293 plugin->env->connect_cb (plugin->env->cls,
294 dst,
295 &dst->pid,
296 &dst->app_ctx);
297 return dst;
298}
299
300
301/**
302 * Find target matching @a addr. If none exists,
303 * create one!
304 *
305 * @param plugin the plugin handle
306 * @param pid presumed identity of the target
307 * @param addr socket address to find
308 * @param addrlen number of bytes in @a addr
309 * @return matching target object
310 */
311static struct GNUNET_DHTU_Target *
312find_target (struct Plugin *plugin,
313 const struct GNUNET_PeerIdentity *pid,
314 const void *addr,
315 size_t addrlen)
316{
317 struct GNUNET_HashCode key;
318 struct GNUNET_DHTU_Target *dst;
319
320 GNUNET_CRYPTO_hash (addr,
321 addrlen,
322 &key);
323 dst = GNUNET_CONTAINER_multihashmap_get (plugin->dsts,
324 &key);
325 if (NULL == dst)
326 {
327 dst = create_target (plugin,
328 pid,
329 (const struct sockaddr *) addr,
330 addrlen);
331 GNUNET_assert (GNUNET_YES ==
332 GNUNET_CONTAINER_multihashmap_put (
333 plugin->dsts,
334 &key,
335 dst,
336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
337 }
338 else
339 {
340 /* move to head of DLL */
341 GNUNET_CONTAINER_DLL_remove (plugin->dst_head,
342 plugin->dst_tail,
343 dst);
344 GNUNET_CONTAINER_DLL_insert (plugin->dst_head,
345 plugin->dst_tail,
346 dst);
347
348 }
349 return dst;
350}
351
352
353/**
354 * Request creation of a session with a peer at the given @a address.
355 *
356 * @param cls closure (internal context for the plugin)
357 * @param pid identity of the target peer
358 * @param address target address to connect to
359 */
360static void
361ip_try_connect (void *cls,
362 const struct GNUNET_PeerIdentity *pid,
363 const char *address)
364{
365 struct Plugin *plugin = cls;
366 char *colon;
367 const char *port;
368 char *addr;
369 struct addrinfo hints = {
370 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV
371 };
372 struct addrinfo *result = NULL;
373
374 if (0 !=
375 strncmp (address,
376 "ip+",
377 strlen ("ip+")))
378 return;
379 address += strlen ("ip+");
380 if (0 !=
381 strncmp (address,
382 "udp://",
383 strlen ("udp://")))
384 return;
385 address += strlen ("udp://");
386 addr = GNUNET_strdup (address);
387 colon = strchr (addr, ':');
388 if (NULL == colon)
389 {
390 port = plugin->port;
391 }
392 else
393 {
394 *colon = '\0';
395 port = colon + 1;
396 }
397 if (0 !=
398 getaddrinfo (addr,
399 port,
400 &hints,
401 &result))
402 {
403 GNUNET_break (0);
404 GNUNET_free (addr);
405 return;
406 }
407 GNUNET_free (addr);
408 (void) find_target (plugin,
409 pid,
410 result->ai_addr,
411 result->ai_addrlen);
412 freeaddrinfo (result);
413}
414
415
416/**
417 * Request underlay to keep the connection to @a target alive if possible.
418 * Hold may be called multiple times to express a strong preference to
419 * keep a connection, say because a @a target is in multiple tables.
420 *
421 * @param cls closure
422 * @param target connection to keep alive
423 */
424static struct GNUNET_DHTU_PreferenceHandle *
425ip_hold (void *cls,
426 struct GNUNET_DHTU_Target *target)
427{
428 struct GNUNET_DHTU_PreferenceHandle *ph;
429
430 ph = GNUNET_new (struct GNUNET_DHTU_PreferenceHandle);
431 ph->target = target;
432 GNUNET_CONTAINER_DLL_insert (target->ph_head,
433 target->ph_tail,
434 ph);
435 target->ph_count++;
436 return ph;
437}
438
439
440/**
441 * Do no long request underlay to keep the connection alive.
442 *
443 * @param cls closure
444 * @param target connection to keep alive
445 */
446static void
447ip_drop (struct GNUNET_DHTU_PreferenceHandle *ph)
448{
449 struct GNUNET_DHTU_Target *target = ph->target;
450
451 GNUNET_CONTAINER_DLL_remove (target->ph_head,
452 target->ph_tail,
453 ph);
454 target->ph_count--;
455 GNUNET_free (ph);
456}
457
458
459/**
460 * Send message to some other participant over the network. Note that
461 * sending is not guaranteeing that the other peer actually received the
462 * message. For any given @a target, the DHT must wait for the @a
463 * finished_cb to be called before calling send() again.
464 *
465 * @param cls closure (internal context for the plugin)
466 * @param target receiver identification
467 * @param msg message
468 * @param msg_size number of bytes in @a msg
469 * @param finished_cb function called once transmission is done
470 * (not called if @a target disconnects, then only the
471 * disconnect_cb is called).
472 * @param finished_cb_cls closure for @a finished_cb
473 */
474static void
475ip_send (void *cls,
476 struct GNUNET_DHTU_Target *target,
477 const void *msg,
478 size_t msg_size,
479 GNUNET_SCHEDULER_TaskCallback finished_cb,
480 void *finished_cb_cls)
481{
482 struct Plugin *plugin = cls;
483 char buf[sizeof (plugin->my_id) + msg_size];
484
485 memcpy (buf,
486 &plugin->my_id,
487 sizeof (plugin->my_id));
488 memcpy (&buf[sizeof (plugin->my_id)],
489 msg,
490 msg_size);
491 GNUNET_NETWORK_socket_sendto (plugin->sock,
492 buf,
493 sizeof (buf),
494 (const struct sockaddr *) &target->addr,
495 target->addrlen);
496 finished_cb (finished_cb_cls);
497}
498
499
500/**
501 * Create a new source on which we may be receiving traffic.
502 *
503 * @param plugin our plugin
504 * @param addr our address
505 * @param addrlen number of bytes in @a addr
506 * @return new source object
507 */
508static struct GNUNET_DHTU_Source *
509create_source (struct Plugin *plugin,
510 const struct sockaddr *addr,
511 socklen_t addrlen)
512{
513 struct GNUNET_DHTU_Source *src;
514
515 src = GNUNET_new (struct GNUNET_DHTU_Source);
516 src->addrlen = addrlen;
517 memcpy (&src->addr,
518 addr,
519 addrlen);
520 src->scan_generation = plugin->scan_generation;
521 switch (addr->sa_family)
522 {
523 case AF_INET:
524 {
525 const struct sockaddr_in *s4 = (const struct sockaddr_in *) addr;
526 char buf[INET_ADDRSTRLEN];
527
528 GNUNET_assert (sizeof (struct sockaddr_in) == addrlen);
529 GNUNET_asprintf (&src->address,
530 "ip+udp://%s:%u",
531 inet_ntop (AF_INET,
532 &s4->sin_addr,
533 buf,
534 sizeof (buf)),
535 ntohs (s4->sin_port));
536 }
537 break;
538 case AF_INET6:
539 {
540 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) addr;
541 char buf[INET6_ADDRSTRLEN];
542
543 GNUNET_assert (sizeof (struct sockaddr_in6) == addrlen);
544 GNUNET_asprintf (&src->address,
545 "ip+udp://[%s]:%u",
546 inet_ntop (AF_INET6,
547 &s6->sin6_addr,
548 buf,
549 sizeof (buf)),
550 ntohs (s6->sin6_port));
551 }
552 break;
553 default:
554 GNUNET_break (0);
555 GNUNET_free (src);
556 return NULL;
557 }
558 GNUNET_CONTAINER_DLL_insert (plugin->src_head,
559 plugin->src_tail,
560 src);
561 plugin->env->address_add_cb (plugin->env->cls,
562 src->address,
563 src,
564 &src->app_ctx);
565 return src;
566}
567
568
569/**
570 * Compare two addresses excluding the ports for equality. Only compares IP
571 * address. Must only be called on AF_INET or AF_INET6 addresses.
572 *
573 * @param a1 address to compare
574 * @param a2 address to compare
575 * @param alen number of bytes in @a a1 and @a a2
576 * @return 0 if @a a1 == @a a2.
577 */
578static int
579addrcmp_np (const struct sockaddr *a1,
580 const struct sockaddr *a2,
581 size_t alen)
582{
583 GNUNET_assert (a1->sa_family == a2->sa_family);
584 switch (a1->sa_family)
585 {
586 case AF_INET:
587 GNUNET_assert (sizeof (struct sockaddr_in) == alen);
588 {
589 const struct sockaddr_in *s1 = (const struct sockaddr_in *) a1;
590 const struct sockaddr_in *s2 = (const struct sockaddr_in *) a2;
591
592 if (s1->sin_addr.s_addr != s2->sin_addr.s_addr)
593 return 1;
594 break;
595 }
596 case AF_INET6:
597 GNUNET_assert (sizeof (struct sockaddr_in6) == alen);
598 {
599 const struct sockaddr_in6 *s1 = (const struct sockaddr_in6 *) a1;
600 const struct sockaddr_in6 *s2 = (const struct sockaddr_in6 *) a2;
601
602 if (0 != GNUNET_memcmp (&s1->sin6_addr,
603 &s2->sin6_addr))
604 return 1;
605 break;
606 }
607 default:
608 GNUNET_assert (0);
609 }
610 return 0;
611}
612
613
614/**
615 * Compare two addresses for equality. Only
616 * compares IP address and port. Must only be
617 * called on AF_INET or AF_INET6 addresses.
618 *
619 * @param a1 address to compare
620 * @param a2 address to compare
621 * @param alen number of bytes in @a a1 and @a a2
622 * @return 0 if @a a1 == @a a2.
623 */
624static int
625addrcmp (const struct sockaddr *a1,
626 const struct sockaddr *a2,
627 size_t alen)
628{
629 GNUNET_assert (a1->sa_family == a2->sa_family);
630 switch (a1->sa_family)
631 {
632 case AF_INET:
633 GNUNET_assert (sizeof (struct sockaddr_in) == alen);
634 {
635 const struct sockaddr_in *s1 = (const struct sockaddr_in *) a1;
636 const struct sockaddr_in *s2 = (const struct sockaddr_in *) a2;
637
638 if (s1->sin_port != s2->sin_port)
639 return 1;
640 if (s1->sin_addr.s_addr != s2->sin_addr.s_addr)
641 return 1;
642 break;
643 }
644 case AF_INET6:
645 GNUNET_assert (sizeof (struct sockaddr_in6) == alen);
646 {
647 const struct sockaddr_in6 *s1 = (const struct sockaddr_in6 *) a1;
648 const struct sockaddr_in6 *s2 = (const struct sockaddr_in6 *) a2;
649
650 if (s1->sin6_port != s2->sin6_port)
651 return 1;
652 if (0 != GNUNET_memcmp (&s1->sin6_addr,
653 &s2->sin6_addr))
654 return 1;
655 break;
656 }
657 default:
658 GNUNET_assert (0);
659 }
660 return 0;
661}
662
663
664/**
665 * Callback function invoked for each interface found.
666 *
667 * @param cls closure
668 * @param name name of the interface (can be NULL for unknown)
669 * @param isDefault is this presumably the default interface
670 * @param addr address of this interface (can be NULL for unknown or unassigned)
671 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
672 * @param netmask the network mask (can be NULL for unknown or unassigned)
673 * @param addrlen length of the address
674 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
675 */
676static enum GNUNET_GenericReturnValue
677process_ifcs (void *cls,
678 const char *name,
679 int isDefault,
680 const struct sockaddr *addr,
681 const struct sockaddr *broadcast_addr,
682 const struct sockaddr *netmask,
683 socklen_t addrlen)
684{
685 struct Plugin *plugin = cls;
686 struct GNUNET_DHTU_Source *src;
687
688 for (src = plugin->src_head;
689 NULL != src;
690 src = src->next)
691 {
692 if ( (addrlen == src->addrlen) &&
693 (0 == addrcmp_np (addr,
694 (const struct sockaddr *) &src->addr,
695 addrlen)) )
696 {
697 src->scan_generation = plugin->scan_generation;
698 return GNUNET_OK;
699 }
700 }
701 switch (addr->sa_family)
702 {
703 case AF_INET:
704 {
705 struct sockaddr_in v4;
706
707 GNUNET_assert (sizeof(v4) == addrlen);
708 memcpy (&v4,
709 addr,
710 addrlen);
711 v4.sin_port = htons (plugin->port16);
712 (void) create_source (plugin,
713 (const struct sockaddr *) &v4,
714 sizeof (v4));
715 break;
716 }
717 case AF_INET6:
718 {
719 struct sockaddr_in6 v6;
720
721 GNUNET_assert (sizeof(v6) == addrlen);
722 memcpy (&v6,
723 addr,
724 addrlen);
725 v6.sin6_port = htons (plugin->port16);
726 (void) create_source (plugin,
727 (const struct sockaddr *) &v6,
728 sizeof (v6));
729 break;
730 }
731 }
732 return GNUNET_OK;
733}
734
735
736/**
737 * Scan network interfaces for IP address changes.
738 *
739 * @param cls a `struct Plugin`
740 */
741static void
742scan (void *cls)
743{
744 struct Plugin *plugin = cls;
745 struct GNUNET_DHTU_Source *next;
746
747 plugin->scan_generation++;
748 GNUNET_OS_network_interfaces_list (&process_ifcs,
749 plugin);
750 for (struct GNUNET_DHTU_Source *src = plugin->src_head;
751 NULL != src;
752 src = next)
753 {
754 next = src->next;
755 if (src->scan_generation >= plugin->scan_generation)
756 continue;
757 GNUNET_CONTAINER_DLL_remove (plugin->src_head,
758 plugin->src_tail,
759 src);
760 plugin->env->address_del_cb (src->app_ctx);
761 GNUNET_free (src->address);
762 GNUNET_free (src);
763 }
764 plugin->scan_task = GNUNET_SCHEDULER_add_delayed (SCAN_FREQ,
765 &scan,
766 plugin);
767}
768
769
770/**
771 * Find our source matching @a addr. If none exists,
772 * create one!
773 *
774 * @param plugin the plugin handle
775 * @param addr socket address to find
776 * @param addrlen number of bytes in @a addr
777 * @return matching source object
778 */
779static struct GNUNET_DHTU_Source *
780find_source (struct Plugin *plugin,
781 const void *addr,
782 size_t addrlen)
783{
784 for (struct GNUNET_DHTU_Source *src = plugin->src_head;
785 NULL != src;
786 src = src->next)
787 {
788 if ( (addrlen == src->addrlen) &&
789 (0 == addrcmp (addr,
790 (const struct sockaddr *) &src->addr,
791 addrlen)) )
792 return src;
793 }
794
795 return create_source (plugin,
796 (const struct sockaddr *) addr,
797 addrlen);
798}
799
800
801/**
802 * UDP socket is ready to receive. Read.
803 *
804 * @param cls our `struct Plugin *`
805 */
806static void
807read_cb (void *cls)
808{
809 struct Plugin *plugin = cls;
810 ssize_t ret;
811 const struct GNUNET_PeerIdentity *pid;
812 char buf[65536] GNUNET_ALIGN;
813 struct sockaddr_storage sa;
814 struct iovec iov = {
815 .iov_base = buf,
816 .iov_len = sizeof (buf)
817 };
818 char ctl[128];
819 struct msghdr mh = {
820 .msg_name = &sa,
821 .msg_namelen = sizeof (sa),
822 .msg_iov = &iov,
823 .msg_iovlen = 1,
824 .msg_control = ctl,
825 .msg_controllen = sizeof (ctl)
826 };
827 struct GNUNET_DHTU_Target *dst = NULL;
828 struct GNUNET_DHTU_Source *src = NULL;
829
830 ret = recvmsg (GNUNET_NETWORK_get_fd (plugin->sock),
831 &mh,
832 MSG_DONTWAIT);
833 plugin->read_task = GNUNET_SCHEDULER_add_read_net (
834 GNUNET_TIME_UNIT_FOREVER_REL,
835 plugin->sock,
836 &read_cb,
837 plugin);
838 if (ret < 0)
839 return; /* read failure, hopefully EAGAIN */
840 if (ret < sizeof (*pid))
841 {
842 GNUNET_break_op (0);
843 return;
844 }
845 /* find IP where we received message */
846 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR (&mh);
847 NULL != cmsg;
848 cmsg = CMSG_NXTHDR (&mh,
849 cmsg))
850 {
851 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
852 "Got CMSG level %u (%d/%d), type %u (%d/%d)\n",
853 cmsg->cmsg_level,
854 (cmsg->cmsg_level == IPPROTO_IP),
855 (cmsg->cmsg_level == IPPROTO_IPV6),
856 cmsg->cmsg_type,
857 (cmsg->cmsg_type == IP_PKTINFO),
858 (cmsg->cmsg_type == IPV6_PKTINFO));
859 if ( (cmsg->cmsg_level == IPPROTO_IP) &&
860 (cmsg->cmsg_type == IP_PKTINFO) )
861 {
862 if (CMSG_LEN (sizeof (struct in_pktinfo)) ==
863 cmsg->cmsg_len)
864 {
865 struct in_pktinfo pi;
866
867 memcpy (&pi,
868 CMSG_DATA (cmsg),
869 sizeof (pi));
870 {
871 struct sockaddr_in sa = {
872 .sin_family = AF_INET,
873 .sin_addr = pi.ipi_addr,
874 .sin_port = htons (plugin->port16)
875 };
876
877 src = find_source (plugin,
878 &sa,
879 sizeof (sa));
880 /* For sources we discovered by reading,
881 force the generation far into the future */
882 src->scan_generation = plugin->scan_generation + 60;
883 }
884 break;
885 }
886 else
887 GNUNET_break (0);
888 }
889 if ( (cmsg->cmsg_level == IPPROTO_IPV6) &&
890 (cmsg->cmsg_type == IPV6_PKTINFO) )
891 {
892 if (CMSG_LEN (sizeof (struct in6_pktinfo)) ==
893 cmsg->cmsg_len)
894 {
895 struct in6_pktinfo pi;
896
897 memcpy (&pi,
898 CMSG_DATA (cmsg),
899 sizeof (pi));
900 {
901 struct sockaddr_in6 sa = {
902 .sin6_family = AF_INET6,
903 .sin6_addr = pi.ipi6_addr,
904 .sin6_port = htons (plugin->port16),
905 .sin6_scope_id = pi.ipi6_ifindex
906 };
907
908 src = find_source (plugin,
909 &sa,
910 sizeof (sa));
911 /* For sources we discovered by reading,
912 force the generation far into the future */
913 src->scan_generation = plugin->scan_generation + 60;
914 break;
915 }
916 }
917 else
918 GNUNET_break (0);
919 }
920 }
921 if (NULL == src)
922 {
923 GNUNET_break (0);
924 return;
925 }
926 pid = (const struct GNUNET_PeerIdentity *) buf;
927 dst = find_target (plugin,
928 pid,
929 &sa,
930 mh.msg_namelen);
931 if (NULL == dst)
932 {
933 GNUNET_break (0);
934 return;
935 }
936 plugin->env->receive_cb (plugin->env->cls,
937 &dst->app_ctx,
938 &src->app_ctx,
939 &buf[sizeof(*pid)],
940 ret - sizeof (*pid));
941}
942
943
944/**
945 * Entry point for the plugin.
946 *
947 * @param cls closure (the `struct GNUNET_DHTU_PluginEnvironment`)
948 * @return the plugin's API
949 */
950struct GNUNET_DHTU_PluginFunctions *
951DHTU_ip_init (struct GNUNET_DHTU_PluginEnvironment *env)
952{
953 struct GNUNET_DHTU_PluginFunctions *api;
954 struct Plugin *plugin;
955 char *port;
956 unsigned int nport;
957 int sock;
958 int af;
959 unsigned long long nse;
960
961 if (GNUNET_OK !=
962 GNUNET_CONFIGURATION_get_value_number (env->cfg,
963 "DHTU-IP",
964 "NSE",
965 &nse))
966 {
967 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
968 "DHTU-IP",
969 "NSE");
970 return NULL;
971 }
972 if (GNUNET_OK !=
973 GNUNET_CONFIGURATION_get_value_string (env->cfg,
974 "DHTU-IP",
975 "UDP_PORT",
976 &port))
977 {
978 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
979 "DHTU-IP",
980 "UDP_PORT");
981 return NULL;
982 }
983 {
984 char dummy;
985
986 if ( (1 != sscanf (port,
987 "%u%c",
988 &nport,
989 &dummy)) ||
990 (nport > UINT16_MAX) )
991 {
992 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
993 "DHTU-IP",
994 "UDP_PORT",
995 "must be number below 65536");
996 GNUNET_free (port);
997 return NULL;
998 }
999 }
1000 plugin = GNUNET_new (struct Plugin);
1001 plugin->env = env;
1002 plugin->port = port;
1003 plugin->port16 = (uint16_t) nport;
1004 if (GNUNET_OK !=
1005 GNUNET_CRYPTO_get_peer_identity (env->cfg,
1006 &plugin->my_id))
1007 {
1008 GNUNET_free (plugin);
1009 return NULL;
1010 }
1011 af = AF_INET6;
1012 sock = socket (af,
1013 SOCK_DGRAM,
1014 IPPROTO_UDP);
1015 if (-1 == sock)
1016 {
1017 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1018 "socket");
1019 GNUNET_free (plugin->port);
1020 GNUNET_free (plugin);
1021 return NULL;
1022 }
1023 switch (af)
1024 {
1025 case AF_INET:
1026 {
1027 int on = 1;
1028
1029 if (0 !=
1030 setsockopt (sock,
1031 IPPROTO_IP,
1032 IP_PKTINFO,
1033 &on,
1034 sizeof (on)))
1035 {
1036 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1037 "setsockopt");
1038 }
1039 }
1040 {
1041 struct sockaddr_in sa = {
1042 .sin_family = AF_INET,
1043 .sin_port = htons ((uint16_t) nport)
1044 };
1045
1046 if (0 !=
1047 bind (sock,
1048 (const struct sockaddr *) &sa,
1049 sizeof (sa)))
1050 {
1051 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1052 "socket");
1053 GNUNET_break (0 ==
1054 close (sock));
1055 GNUNET_free (plugin->port);
1056 GNUNET_free (plugin);
1057 return NULL;
1058 }
1059 }
1060 break;
1061 case AF_INET6:
1062 {
1063 int on = 1;
1064
1065 if (0 !=
1066 setsockopt (sock,
1067 IPPROTO_IPV6,
1068 IPV6_RECVPKTINFO,
1069 &on,
1070 sizeof (on)))
1071 {
1072 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1073 "setsockopt");
1074 }
1075 }
1076 {
1077 struct sockaddr_in6 sa = {
1078 .sin6_family = AF_INET6,
1079 .sin6_port = htons ((uint16_t) nport)
1080 };
1081
1082 if (0 !=
1083 bind (sock,
1084 (const struct sockaddr *) &sa,
1085 sizeof (sa)))
1086 {
1087 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
1088 "socket");
1089 GNUNET_break (0 ==
1090 close (sock));
1091 GNUNET_free (plugin->port);
1092 GNUNET_free (plugin);
1093 return NULL;
1094 }
1095 }
1096 break;
1097 }
1098 plugin->dsts = GNUNET_CONTAINER_multihashmap_create (128,
1099 GNUNET_NO);
1100 plugin->sock = GNUNET_NETWORK_socket_box_native (sock);
1101 plugin->read_task = GNUNET_SCHEDULER_add_read_net (
1102 GNUNET_TIME_UNIT_FOREVER_REL,
1103 plugin->sock,
1104 &read_cb,
1105 plugin);
1106 env->network_size_cb (env->cls,
1107 GNUNET_TIME_UNIT_ZERO_ABS,
1108 log (nse) / log (2),
1109 -1.0 /* stddev */);
1110 plugin->scan_task = GNUNET_SCHEDULER_add_now (&scan,
1111 plugin);
1112 api = GNUNET_new (struct GNUNET_DHTU_PluginFunctions);
1113 api->cls = plugin;
1114 api->try_connect = &ip_try_connect;
1115 api->hold = &ip_hold;
1116 api->drop = &ip_drop;
1117 api->send = &ip_send;
1118 return api;
1119}
1120
1121
1122/**
1123 * Exit point from the plugin.
1124 *
1125 * @param cls closure (our `struct Plugin`)
1126 * @return NULL
1127 */
1128void *
1129DHTU_ip_done (struct GNUNET_DHTU_PluginFunctions *api)
1130{
1131 struct Plugin *plugin = api->cls;
1132 struct GNUNET_DHTU_Source *src;
1133 struct GNUNET_DHTU_Target *dst;
1134
1135 while (NULL != (dst = plugin->dst_head))
1136 {
1137 plugin->env->disconnect_cb (dst->app_ctx);
1138 GNUNET_assert (NULL == dst->ph_head);
1139 GNUNET_CONTAINER_DLL_remove (plugin->dst_head,
1140 plugin->dst_tail,
1141 dst);
1142 GNUNET_free (dst);
1143 }
1144 while (NULL != (src = plugin->src_head))
1145 {
1146 plugin->env->address_del_cb (src->app_ctx);
1147 GNUNET_CONTAINER_DLL_remove (plugin->src_head,
1148 plugin->src_tail,
1149 src);
1150 GNUNET_free (src->address);
1151 GNUNET_free (src);
1152 }
1153 plugin->env->network_size_cb (plugin->env->cls,
1154 GNUNET_TIME_UNIT_FOREVER_ABS,
1155 0.0,
1156 0.0);
1157 GNUNET_CONTAINER_multihashmap_destroy (plugin->dsts);
1158 if (NULL != plugin->read_task)
1159 {
1160 GNUNET_SCHEDULER_cancel (plugin->read_task);
1161 plugin->read_task = NULL;
1162 }
1163 GNUNET_SCHEDULER_cancel (plugin->scan_task);
1164 GNUNET_break (GNUNET_OK ==
1165 GNUNET_NETWORK_socket_close (plugin->sock));
1166 GNUNET_free (plugin->port);
1167 GNUNET_free (plugin);
1168 GNUNET_free (api);
1169 return NULL;
1170}