- changes
[oweals/gnunet.git] / src / transport / plugin_transport_udp_new.c
1 /*
2      This file is part of GNUnet
3      (C) 2010, 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file transport/plugin_transport_udp.c
23  * @brief Implementation of the UDP transport protocol
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  * @author Matthias Wachs
27  */
28 #include "platform.h"
29 #include "plugin_transport_udp_new.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_fragmentation_lib.h"
33 #include "gnunet_nat_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_transport_plugin.h"
41 #include "transport.h"
42
43 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
44
45
46 /**
47  * Closure for 'append_port'.
48  */
49 struct PrettyPrinterContext
50 {
51   /**
52    * Function to call with the result.
53    */
54   GNUNET_TRANSPORT_AddressStringCallback asc;
55
56   /**
57    * Clsoure for 'asc'.
58    */
59   void *asc_cls;
60
61   /**
62    * Port to add after the IP address.
63    */
64   uint16_t port;
65 };
66
67
68 /**
69  * Function called for a quick conversion of the binary address to
70  * a numeric address.  Note that the caller must not free the
71  * address and that the next call to this function is allowed
72  * to override the address again.
73  *
74  * @param cls closure
75  * @param addr binary address
76  * @param addrlen length of the address
77  * @return string representing the same address
78  */
79 const char *
80 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
81 {
82   static char rbuf[INET6_ADDRSTRLEN + 10];
83   char buf[INET6_ADDRSTRLEN];
84   const void *sb;
85   struct in_addr a4;
86   struct in6_addr a6;
87   const struct IPv4UdpAddress *t4;
88   const struct IPv6UdpAddress *t6;
89   int af;
90   uint16_t port;
91
92   if (addrlen == sizeof (struct IPv6UdpAddress))
93   {
94     t6 = addr;
95     af = AF_INET6;
96     port = ntohs (t6->u6_port);
97     memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
98     sb = &a6;
99   }
100   else if (addrlen == sizeof (struct IPv4UdpAddress))
101   {
102     t4 = addr;
103     af = AF_INET;
104     port = ntohs (t4->u4_port);
105     memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
106     sb = &a4;
107   }
108   else
109   {
110     GNUNET_break_op (0);
111     return NULL;
112   }
113   inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
114   GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
115                    buf, port);
116   return rbuf;
117 }
118
119
120 /**
121  * Append our port and forward the result.
122  *
123  * @param cls a 'struct PrettyPrinterContext'
124  * @param hostname result from DNS resolver
125  */
126 static void
127 append_port (void *cls, const char *hostname)
128 {
129   struct PrettyPrinterContext *ppc = cls;
130   char *ret;
131
132   if (hostname == NULL)
133   {
134     ppc->asc (ppc->asc_cls, NULL);
135     GNUNET_free (ppc);
136     return;
137   }
138   GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
139   ppc->asc (ppc->asc_cls, ret);
140   GNUNET_free (ret);
141 }
142
143
144 /**
145  * Convert the transports address to a nice, human-readable
146  * format.
147  *
148  * @param cls closure
149  * @param type name of the transport that generated the address
150  * @param addr one of the addresses of the host, NULL for the last address
151  *        the specific address format depends on the transport
152  * @param addrlen length of the address
153  * @param numeric should (IP) addresses be displayed in numeric form?
154  * @param timeout after how long should we give up?
155  * @param asc function to call on each string
156  * @param asc_cls closure for asc
157  */
158 static void
159 udp_plugin_address_pretty_printer (void *cls, const char *type,
160                                    const void *addr, size_t addrlen,
161                                    int numeric,
162                                    struct GNUNET_TIME_Relative timeout,
163                                    GNUNET_TRANSPORT_AddressStringCallback asc,
164                                    void *asc_cls)
165 {
166   struct PrettyPrinterContext *ppc;
167   const void *sb;
168   size_t sbs;
169   struct sockaddr_in a4;
170   struct sockaddr_in6 a6;
171   const struct IPv4UdpAddress *u4;
172   const struct IPv6UdpAddress *u6;
173   uint16_t port;
174
175   if (addrlen == sizeof (struct IPv6UdpAddress))
176   {
177     u6 = addr;
178     memset (&a6, 0, sizeof (a6));
179     a6.sin6_family = AF_INET6;
180 #if HAVE_SOCKADDR_IN_SIN_LEN
181     a6.sin6_len = sizeof (a6);
182 #endif
183     a6.sin6_port = u6->u6_port;
184     memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
185     port = ntohs (u6->u6_port);
186     sb = &a6;
187     sbs = sizeof (a6);
188   }
189   else if (addrlen == sizeof (struct IPv4UdpAddress))
190   {
191     u4 = addr;
192     memset (&a4, 0, sizeof (a4));
193     a4.sin_family = AF_INET;
194 #if HAVE_SOCKADDR_IN_SIN_LEN
195     a4.sin_len = sizeof (a4);
196 #endif
197     a4.sin_port = u4->u4_port;
198     a4.sin_addr.s_addr = u4->ipv4_addr;
199     port = ntohs (u4->u4_port);
200     sb = &a4;
201     sbs = sizeof (a4);
202   }
203   else
204   {
205     /* invalid address */
206     GNUNET_break_op (0);
207     asc (asc_cls, NULL);
208     return;
209   }
210   ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
211   ppc->asc = asc;
212   ppc->asc_cls = asc_cls;
213   ppc->port = port;
214   GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
215 }
216
217
218 /**
219  * Check if the given port is plausible (must be either our listen
220  * port or our advertised port).  If it is neither, we return
221  * GNUNET_SYSERR.
222  *
223  * @param plugin global variables
224  * @param in_port port number to check
225  * @return GNUNET_OK if port is either open_port or adv_port
226  */
227 static int
228 check_port (struct Plugin *plugin, uint16_t in_port)
229 {
230   if ((in_port == plugin->port) || (in_port == plugin->aport))
231     return GNUNET_OK;
232   return GNUNET_SYSERR;
233 }
234
235
236
237 /**
238  * Function that will be called to check if a binary address for this
239  * plugin is well-formed and corresponds to an address for THIS peer
240  * (as per our configuration).  Naturally, if absolutely necessary,
241  * plugins can be a bit conservative in their answer, but in general
242  * plugins should make sure that the address does not redirect
243  * traffic to a 3rd party that might try to man-in-the-middle our
244  * traffic.
245  *
246  * @param cls closure, should be our handle to the Plugin
247  * @param addr pointer to the address
248  * @param addrlen length of addr
249  * @return GNUNET_OK if this is a plausible address for this peer
250  *         and transport, GNUNET_SYSERR if not
251  *
252  */
253 static int
254 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
255 {
256   struct Plugin *plugin = cls;
257   struct IPv4UdpAddress *v4;
258   struct IPv6UdpAddress *v6;
259
260   if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
261       (addrlen != sizeof (struct IPv6UdpAddress)))
262   {
263     GNUNET_break_op (0);
264     return GNUNET_SYSERR;
265   }
266   if (addrlen == sizeof (struct IPv4UdpAddress))
267   {
268     v4 = (struct IPv4UdpAddress *) addr;
269     if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
270       return GNUNET_SYSERR;
271     if (GNUNET_OK !=
272         GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
273                                  sizeof (struct in_addr)))
274       return GNUNET_SYSERR;
275   }
276   else
277   {
278     v6 = (struct IPv6UdpAddress *) addr;
279     if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
280     {
281       GNUNET_break_op (0);
282       return GNUNET_SYSERR;
283     }
284     if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
285       return GNUNET_SYSERR;
286     if (GNUNET_OK !=
287         GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
288                                  sizeof (struct in6_addr)))
289       return GNUNET_SYSERR;
290   }
291   return GNUNET_OK;
292 }
293
294
295 /**
296  * Disconnect from a remote node.  Clean up session if we have one for this peer
297  *
298  * @param cls closure for this call (should be handle to Plugin)
299  * @param target the peeridentity of the peer to disconnect
300  * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
301  */
302 static void
303 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
304 {
305
306 }
307
308
309 /**
310  * Creates a new outbound session the transport service will use to send data to the
311  * peer
312  *
313  * @param cls the plugin
314  * @param address the address
315  * @return the session or NULL of max connections exceeded
316  */
317
318 static struct Session *
319 udp_plugin_get_session (void *cls,
320                   const struct GNUNET_HELLO_Address *address)
321 {
322   struct Session * s = NULL;
323   //struct Plugin * plugin = cls;
324
325   return s;
326 }
327
328
329 /**
330  * Function that can be used by the transport service to transmit
331  * a message using the plugin.   Note that in the case of a
332  * peer disconnecting, the continuation MUST be called
333  * prior to the disconnect notification itself.  This function
334  * will be called with this peer's HELLO message to initiate
335  * a fresh connection to another peer.
336  *
337  * @param cls closure
338  * @param session which session must be used
339  * @param msgbuf the message to transmit
340  * @param msgbuf_size number of bytes in 'msgbuf'
341  * @param priority how important is the message (most plugins will
342  *                 ignore message priority and just FIFO)
343  * @param to how long to wait at most for the transmission (does not
344  *                require plugins to discard the message after the timeout,
345  *                just advisory for the desired delay; most plugins will ignore
346  *                this as well)
347  * @param cont continuation to call once the message has
348  *        been transmitted (or if the transport is ready
349  *        for the next transmission call; or if the
350  *        peer disconnected...); can be NULL
351  * @param cont_cls closure for cont
352  * @return number of bytes used (on the physical network, with overheads);
353  *         -1 on hard errors (i.e. address invalid); 0 is a legal value
354  *         and does NOT mean that the message was not transmitted (DV)
355  */
356 static ssize_t
357 udp_plugin_send (void *cls,
358                   struct Session *session,
359                   const char *msgbuf, size_t msgbuf_size,
360                   unsigned int priority,
361                   struct GNUNET_TIME_Relative to,
362                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
363 {
364
365   return 0;
366
367 }
368
369
370 /**
371  * Our external IP address/port mapping has changed.
372  *
373  * @param cls closure, the 'struct LocalAddrList'
374  * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
375  *     the previous (now invalid) one
376  * @param addr either the previous or the new public IP address
377  * @param addrlen actual lenght of the address
378  */
379 static void
380 udp_nat_port_map_callback (void *cls, int add_remove,
381                            const struct sockaddr *addr, socklen_t addrlen)
382 {
383   struct Plugin *plugin = cls;
384   struct IPv4UdpAddress u4;
385   struct IPv6UdpAddress u6;
386   void *arg;
387   size_t args;
388
389   /* convert 'addr' to our internal format */
390   switch (addr->sa_family)
391   {
392   case AF_INET:
393     GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
394     u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
395     u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
396     arg = &u4;
397     args = sizeof (u4);
398     break;
399   case AF_INET6:
400     GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
401     memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
402             sizeof (struct in6_addr));
403     u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
404     arg = &u6;
405     args = sizeof (u6);
406     break;
407   default:
408     GNUNET_break (0);
409     return;
410   }
411   /* modify our published address list */
412   plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
413 }
414
415
416 /**
417  * Read and process a message from the given socket.
418  *
419  * @param plugin the overall plugin
420  * @param rsock socket to read from
421  */
422 static void
423 udp_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
424 {
425   socklen_t fromlen;
426   char addr[32];
427   char buf[65536];
428   ssize_t size;
429   const struct GNUNET_MessageHeader *msg;
430   //const struct GNUNET_MessageHeader *ack;
431   //struct Session *peer_session;
432   //const struct UDP_ACK_Message *udp_ack;
433   //struct ReceiveContext *rc;
434   //struct GNUNET_TIME_Absolute now;
435   //struct FindReceiveContext frc;
436   //struct Session *s = NULL;
437   //struct GNUNET_TIME_Relative flow_delay;
438   //struct GNUNET_ATS_Information ats;
439
440   fromlen = sizeof (addr);
441   memset (&addr, 0, sizeof (addr));
442   size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
443                                       (struct sockaddr *) &addr, &fromlen);
444   if (size < sizeof (struct GNUNET_MessageHeader))
445   {
446     GNUNET_break_op (0);
447     return;
448   }
449   msg = (const struct GNUNET_MessageHeader *) buf;
450
451   LOG (GNUNET_ERROR_TYPE_DEBUG,
452        "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
453        GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
454
455   if (size != ntohs (msg->size))
456   {
457     GNUNET_break_op (0);
458     return;
459   }
460   switch (ntohs (msg->type))
461   {
462   case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
463   {
464     udp_broadcast_receive(plugin, &buf, size, addr, fromlen);
465     return;
466   }
467   case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
468     if (ntohs (msg->size) < sizeof (struct UDPMessage))
469     {
470       GNUNET_break_op (0);
471       return;
472     }
473     /*
474     process_udp_message (plugin, (const struct UDPMessage *) msg,
475                          (const struct sockaddr *) addr, fromlen);
476      */
477     return;
478   default:
479     GNUNET_break_op (0);
480     return;
481   }
482 }
483
484 /**
485  * We have been notified that our readset has something to read.  We don't
486  * know which socket needs to be read, so we have to check each one
487  * Then reschedule this function to be called again once more is available.
488  *
489  * @param cls the plugin handle
490  * @param tc the scheduling context (for rescheduling this function again)
491  */
492 static void
493 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
494 {
495   struct Plugin *plugin = cls;
496
497   plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
498   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
499     return;
500
501   if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
502   {
503     if ((NULL != plugin->sockv4) &&
504       (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
505         udp_read (plugin, plugin->sockv4);
506     if ((NULL != plugin->sockv6) &&
507       (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
508         udp_read (plugin, plugin->sockv6);
509   }
510
511   if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
512   {
513     /* TODO */
514   }
515
516   plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
517                                    GNUNET_SCHEDULER_NO_TASK,
518                                    GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
519                                    plugin->ws, &udp_plugin_select, plugin);
520
521 }
522
523
524 static int
525 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
526 {
527   int tries;
528   int sockets_created = 0;
529   struct sockaddr *serverAddr;
530   struct sockaddr *addrs[2];
531   socklen_t addrlens[2];
532   socklen_t addrlen;
533
534   /* Create IPv6 socket */
535   if (plugin->enable_ipv6 == GNUNET_YES)
536   {
537     plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
538     if (NULL == plugin->sockv6)
539     {
540       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
541     }
542     else
543     {
544 #if HAVE_SOCKADDR_IN_SIN_LEN
545       serverAddrv6.sin6_len = sizeof (serverAddrv6);
546 #endif
547       serverAddrv6->sin6_family = AF_INET6;
548       serverAddrv6->sin6_addr = in6addr_any;
549       serverAddrv6->sin6_port = htons (plugin->port);
550       addrlen = sizeof (struct sockaddr_in6);
551       serverAddr = (struct sockaddr *) serverAddrv6;
552 #if DEBUG_UDP
553       LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
554            ntohs (serverAddrv6->sin6_port));
555 #endif
556       tries = 0;
557       while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
558              GNUNET_OK)
559       {
560         serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);        /* Find a good, non-root port */
561 #if DEBUG_UDP
562         LOG (GNUNET_ERROR_TYPE_DEBUG,
563              "IPv6 Binding failed, trying new port %d\n",
564              ntohs (serverAddrv6->sin6_port));
565 #endif
566         tries++;
567         if (tries > 10)
568         {
569           GNUNET_NETWORK_socket_close (plugin->sockv6);
570           plugin->sockv6 = NULL;
571           break;
572         }
573       }
574       if (plugin->sockv6 != NULL)
575       {
576 #if DEBUG_UDP
577         LOG (GNUNET_ERROR_TYPE_DEBUG,
578              "IPv6 socket created on port %d\n",
579              ntohs (serverAddrv6->sin6_port));
580 #endif
581         addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
582         addrlens[sockets_created] = sizeof (struct sockaddr_in6);
583         sockets_created++;
584       }
585     }
586   }
587
588   /* Create IPv4 socket */
589   plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
590   if (NULL == plugin->sockv4)
591   {
592     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
593   }
594   else
595   {
596 #if HAVE_SOCKADDR_IN_SIN_LEN
597     serverAddrv4.sin_len = sizeof (serverAddrv4);
598 #endif
599     serverAddrv4->sin_family = AF_INET;
600     serverAddrv4->sin_addr.s_addr = INADDR_ANY;
601     serverAddrv4->sin_port = htons (plugin->port);
602     addrlen = sizeof (struct sockaddr_in);
603     serverAddr = (struct sockaddr *) serverAddrv4;
604
605 #if DEBUG_UDP
606     LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
607          ntohs (serverAddrv4->sin_port));
608 #endif
609     tries = 0;
610     while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
611            GNUNET_OK)
612     {
613       serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);   /* Find a good, non-root port */
614 #if DEBUG_UDP
615       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
616            ntohs (serverAddrv4->sin_port));
617 #endif
618       tries++;
619       if (tries > 10)
620       {
621         GNUNET_NETWORK_socket_close (plugin->sockv4);
622         plugin->sockv4 = NULL;
623         break;
624       }
625     }
626     if (plugin->sockv4 != NULL)
627     {
628       addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
629       addrlens[sockets_created] = sizeof (struct sockaddr_in);
630       sockets_created++;
631     }
632   }
633
634   /* Create file descriptors */
635   plugin->rs = GNUNET_NETWORK_fdset_create ();
636   plugin->ws = GNUNET_NETWORK_fdset_create ();
637   GNUNET_NETWORK_fdset_zero (plugin->rs);
638   GNUNET_NETWORK_fdset_zero (plugin->ws);
639   if (NULL != plugin->sockv4)
640   {
641     GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
642     GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv4);
643   }
644   if (NULL != plugin->sockv6)
645   {
646     GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
647     GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv6);
648   }
649
650   if (sockets_created == 0)
651     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
652
653   plugin->select_task =
654       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
655                                    GNUNET_SCHEDULER_NO_TASK,
656                                    GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
657                                    plugin->ws, &udp_plugin_select, plugin);
658
659   plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
660                            GNUNET_NO, plugin->port,
661                            sockets_created,
662                            (const struct sockaddr **) addrs, addrlens,
663                            &udp_nat_port_map_callback, NULL, plugin);
664
665   return sockets_created;
666 }
667
668
669 /**
670  * The exported method. Makes the core api available via a global and
671  * returns the udp transport API.
672  *
673  * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
674  * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
675  */
676 void *
677 libgnunet_plugin_transport_udp_init (void *cls)
678 {
679   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
680   struct GNUNET_TRANSPORT_PluginFunctions *api;
681   struct Plugin *plugin;
682
683   unsigned long long port;
684   unsigned long long aport;
685   unsigned long long broadcast;
686   unsigned long long udp_max_bps;
687   unsigned long long enable_v6;
688   char * bind4_address;
689   char * bind6_address;
690   struct GNUNET_TIME_Relative interval;
691
692   struct sockaddr_in serverAddrv4;
693   struct sockaddr_in6 serverAddrv6;
694
695   int res;
696
697   /* Get port number */
698   if (GNUNET_OK !=
699       GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
700                                              &port))
701     port = 2086;
702   if (GNUNET_OK !=
703       GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
704                                              "ADVERTISED_PORT", &aport))
705     aport = port;
706   if (port > 65535)
707   {
708     LOG (GNUNET_ERROR_TYPE_WARNING,
709          _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
710          65535);
711     return NULL;
712   }
713
714   /* Protocols */
715   if ((GNUNET_YES ==
716        GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
717                                              "DISABLEV6")))
718   {
719     enable_v6 = GNUNET_NO;
720   }
721   else
722     enable_v6 = GNUNET_YES;
723
724
725   /* Addresses */
726   memset (&serverAddrv6, 0, sizeof (serverAddrv6));
727   memset (&serverAddrv4, 0, sizeof (serverAddrv4));
728
729   if (GNUNET_YES ==
730       GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
731                                              "BINDTO", &bind4_address))
732   {
733     LOG (GNUNET_ERROR_TYPE_DEBUG,
734          "Binding udp plugin to specific address: `%s'\n",
735          bind4_address);
736     if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
737     {
738       GNUNET_free (bind4_address);
739       return NULL;
740     }
741   }
742
743   if (GNUNET_YES ==
744       GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
745                                              "BINDTO6", &bind6_address))
746   {
747     LOG (GNUNET_ERROR_TYPE_DEBUG,
748          "Binding udp plugin to specific address: `%s'\n",
749          bind6_address);
750     if (1 !=
751         inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
752     {
753       LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
754            bind6_address);
755       GNUNET_free_non_null (bind4_address);
756       GNUNET_free (bind6_address);
757       return NULL;
758     }
759   }
760
761
762   /* Enable neighbour discovery */
763   broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
764                                             "BROADCAST");
765   if (broadcast == GNUNET_SYSERR)
766     broadcast = GNUNET_NO;
767
768   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
769                                            "BROADCAST_INTERVAL", &interval))
770   {
771     interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
772   }
773
774   /* Maximum datarate */
775   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
776                                              "MAX_BPS", &udp_max_bps))
777   {
778     udp_max_bps = 1024 * 1024 * 50;     /* 50 MB/s == infinity for practical purposes */
779   }
780
781   plugin = GNUNET_malloc (sizeof (struct Plugin));
782   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
783
784   GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
785                                  GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
786
787   plugin->port = port;
788   plugin->aport = aport;
789   plugin->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
790   plugin->broadcast_interval = interval;
791   plugin->enable_ipv6 = enable_v6;
792   plugin->env = env;
793
794   api->cls = plugin;
795   api->send = NULL;
796   api->disconnect = &udp_disconnect;
797   api->address_pretty_printer = &udp_plugin_address_pretty_printer;
798   api->address_to_string = &udp_address_to_string;
799   api->check_address = &udp_plugin_check_address;
800   api->get_session = &udp_plugin_get_session;
801   api->send_with_session = &udp_plugin_send;
802
803   LOG (GNUNET_ERROR_TYPE_ERROR, "Setting up sockets\n");
804   res = setup_sockets (plugin, &serverAddrv6, &serverAddrv4);
805   if ((res == 0) || ((plugin->sockv4 == NULL) && (plugin->sockv6 == NULL)))
806   {
807     LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
808     GNUNET_free (plugin);
809     GNUNET_free (api);
810     return NULL;
811   }
812
813   LOG (GNUNET_ERROR_TYPE_ERROR, "Starting broadcasting\n");
814   setup_broadcast (plugin, &serverAddrv6, &serverAddrv4);
815
816
817   GNUNET_free_non_null (bind4_address);
818   GNUNET_free_non_null (bind6_address);
819   return api;
820 }
821
822
823 /**
824  * The exported method. Makes the core api available via a global and
825  * returns the udp transport API.
826  *
827  * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
828  * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
829  */
830 void *
831 libgnunet_plugin_transport_udp_done (void *cls)
832 {
833   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
834   struct Plugin *plugin = api->cls;
835
836   stop_broadcast (plugin);
837
838   /* Closing sockets */
839   if (plugin->sockv4 != NULL)
840   {
841     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
842     plugin->sockv4 = NULL;
843   }
844   if (plugin->sockv6 != NULL)
845   {
846     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
847     plugin->sockv6 = NULL;
848   }
849   GNUNET_NETWORK_fdset_destroy (plugin->rs);
850   GNUNET_NETWORK_fdset_destroy (plugin->ws);
851
852   GNUNET_NAT_unregister (plugin->nat);
853   plugin->nat = NULL;
854   GNUNET_free (plugin);
855   GNUNET_free (api);
856   return NULL;
857 }
858
859
860 /* end of plugin_transport_udp.c */