remove debug
[oweals/gnunet.git] / src / transport / plugin_transport_udp.c
1 /*
2      This file is part of GNUnet
3      (C) 2010 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 2, 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 service
24  * @author Christian Grothoff
25  * @author Nathan Evans
26  */
27
28 #include "platform.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_service_lib.h"
37 #include "gnunet_signatures.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "plugin_transport.h"
41 #include "transport.h"
42
43 #define DEBUG_UDP GNUNET_NO
44
45 /*
46  * Transport cost to peer, always 1 for UDP (direct connection)
47  */
48 #define UDP_DIRECT_DISTANCE 1
49
50 /**
51  * Handle for request of hostname resolution, non-NULL if pending.
52  */
53 static struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
54
55 /**
56  * How long until we give up on transmitting the welcome message?
57  */
58 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
59
60
61 /**
62  * Message-Packet header.
63  */
64 struct UDPMessage
65 {
66   /**
67    * size of the message, in bytes, including this header.
68    */
69   struct GNUNET_MessageHeader header;
70
71   /**
72    * What is the identity of the sender (GNUNET_hash of public key)
73    */
74   struct GNUNET_PeerIdentity sender;
75
76 };
77
78
79 struct PrettyPrinterContext
80 {
81   GNUNET_TRANSPORT_AddressStringCallback asc;
82   void *asc_cls;
83   uint16_t port;
84 };
85
86 /**
87  * Encapsulation of all of the state of the plugin.
88  */
89 struct Plugin
90 {
91   /**
92    * Our environment.
93    */
94   struct GNUNET_TRANSPORT_PluginEnvironment *env;
95
96   /**
97    * Handle for the statistics service.
98    */
99   struct GNUNET_STATISTICS_Handle *statistics;
100
101   /**
102    * Handle to the network service.
103    */
104   struct GNUNET_SERVICE_Context *service;
105
106   /**
107    * Handle for request of hostname resolution, non-NULL if pending.
108    */
109   struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
110
111   /**
112    * FD Read set
113    */
114   struct GNUNET_NETWORK_FDSet *rs;
115
116   /**
117    * ID of task used to update our addresses when one expires.
118    */
119   GNUNET_SCHEDULER_TaskIdentifier address_update_task;
120
121   /**
122    * ID of select task
123    */
124   GNUNET_SCHEDULER_TaskIdentifier select_task;
125
126   /**
127    * Port that we are actually listening on.
128    */
129   uint16_t open_port;
130
131   /**
132    * Port that the user said we would have visible to the
133    * rest of the world.
134    */
135   uint16_t adv_port;
136
137 };
138
139 /* *********** globals ************* */
140
141 /**
142  * the socket that we transmit all data with
143  */
144 static struct GNUNET_NETWORK_Handle *udp_sock;
145
146 /**
147  * Disconnect from a remote node.
148  *
149  * @param cls closure ('struct Plugin'), unused
150  * @param target peer do disconnect
151  * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
152  */
153 void
154 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
155 {
156   /* nothing to do, UDP is stateless */
157 }
158
159 /**
160  * Shutdown the server process (stop receiving inbound traffic). Maybe
161  * restarted later!
162  */
163 static int
164 udp_transport_server_stop (void *cls)
165 {
166   struct Plugin *plugin = cls;
167   int ret;
168
169   GNUNET_assert (udp_sock != NULL);
170   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
171     {
172       GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
173       plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
174     }
175
176   ret = GNUNET_NETWORK_socket_close (udp_sock);
177   if (ret != GNUNET_SYSERR)
178     udp_sock = NULL;
179   return ret;
180 }
181
182 /**
183  * Function that can be used by the transport service to transmit
184  * a message using the plugin.
185  *
186  * @param cls closure
187  * @param target who should receive this message (ignored by UDP)
188  * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
189  * @param msgbuf_size the size of the msgbuf to send
190  * @param priority how important is the message (ignored by UDP)
191  * @param timeout when should we time out (give up) if we can not transmit?
192  * @param addr the addr to send the message to, needs to be a sockaddr for us
193  * @param addrlen the len of addr
194  * @param force_address not used, we had better have an address to send to
195  *        because we are stateless!!
196  * @param cont continuation to call once the message has
197  *        been transmitted (or if the transport is ready
198  *        for the next transmission call; or if the
199  *        peer disconnected...)
200  * @param cont_cls closure for cont
201  *
202  * @return the number of bytes written
203  */
204
205 static ssize_t
206 udp_plugin_send (void *cls,
207                  const struct GNUNET_PeerIdentity *target,
208                  const char *msgbuf,
209                  size_t msgbuf_size,
210                  unsigned int priority,
211                  struct GNUNET_TIME_Relative timeout,
212                  const void *addr,
213                  size_t addrlen,
214                  int force_address,
215                  GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
216 {
217   struct Plugin *plugin = cls;
218   struct UDPMessage *message;
219   int ssize;
220   ssize_t sent;
221
222   GNUNET_assert(udp_sock != NULL);
223
224   if ((addr == NULL) || (addrlen == 0))
225     {
226 #if DEBUG_UDP
227   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
228                    ("udp_plugin_send called without address, returning!\n"));
229 #endif
230       cont (cont_cls, target, GNUNET_OK);
231       return 0; /* Can never send if we don't have an address!! */
232     }
233
234   /* Build the message to be sent */
235   message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
236   ssize = sizeof (struct UDPMessage) + msgbuf_size;
237
238 #if DEBUG_UDP
239   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
240                    ("In udp_send, ssize is %d, sending message to %s\n"), ssize, GNUNET_a2s((const struct sockaddr *)addr, addrlen));
241 #endif
242   message->header.size = htons (ssize);
243   message->header.type = htons (0);
244   memcpy (&message->sender, plugin->env->my_identity,
245           sizeof (struct GNUNET_PeerIdentity));
246   memcpy (&message[1], msgbuf, msgbuf_size);
247
248   /* Actually send the message */
249   sent =
250     GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
251                                   addr,
252                                   addrlen);
253
254   if (cont != NULL)
255     {
256       if (sent == GNUNET_SYSERR)
257         cont (cont_cls, target, GNUNET_SYSERR);
258       else
259         {
260 #if DEBUG_UDP
261   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
262                    ("Sucessfully sent message, calling transmit continuation!\n"));
263 #endif
264           cont (cont_cls, target, GNUNET_OK);
265         }
266     }
267   GNUNET_free (message);
268   return sent;
269 }
270
271
272 /**
273  * Add the IP of our network interface to the list of
274  * our external IP addresses.
275  */
276 static int
277 process_interfaces (void *cls,
278                     const char *name,
279                     int isDefault,
280                     const struct sockaddr *addr, socklen_t addrlen)
281 {
282   struct Plugin *plugin = cls;
283   int af;
284   struct sockaddr_in *v4;
285   struct sockaddr_in6 *v6;
286
287   af = addr->sa_family;
288   if (af == AF_INET)
289     {
290       v4 = (struct sockaddr_in *) addr;
291       v4->sin_port = htons (plugin->adv_port);
292     }
293   else
294     {
295       GNUNET_assert (af == AF_INET6);
296       v6 = (struct sockaddr_in6 *) addr;
297       v6->sin6_port = htons (plugin->adv_port);
298     }
299   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
300                    GNUNET_ERROR_TYPE_BULK,
301                    "udp", _("Found address `%s' (%s)\n"),
302                    GNUNET_a2s (addr, addrlen), name);
303   plugin->env->notify_address (plugin->env->cls,
304                                "udp",
305                                addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
306
307   return GNUNET_OK;
308 }
309
310
311 /**
312  * Function called by the resolver for each address obtained from DNS
313  * for our own hostname.  Add the addresses to the list of our
314  * external IP addresses.
315  *
316  * @param cls closure
317  * @param addr one of the addresses of the host, NULL for the last address
318  * @param addrlen length of the address
319  */
320 static void
321 process_hostname_ips (void *cls,
322                       const struct sockaddr *addr, socklen_t addrlen)
323 {
324   struct Plugin *plugin = cls;
325
326   if (addr == NULL)
327     {
328       plugin->hostname_dns = NULL;
329       return;
330     }
331   process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
332 }
333
334
335 /*
336  * @param cls the plugin handle
337  * @param tc the scheduling context (for rescheduling this function again)
338  *
339  * We have been notified that our writeset has something to read.  Presumably
340  * select has been called already, so we can go ahead and start reading from
341  * the socket immediately.  Then we check if there is more to be read by
342  * calling select ourselves while there is stuff on the wire.  Then reschedule
343  * this function to be called again once more is available.
344  *
345  */
346 static void
347 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348 {
349   struct Plugin *plugin = cls;
350   char *buf;
351   struct UDPMessage *msg;
352   struct GNUNET_PeerIdentity *sender;
353   unsigned int buflen;
354   socklen_t fromlen;
355   struct sockaddr_storage addr;
356   ssize_t ret;
357   int offset;
358   int count;
359   int tsize;
360   char *msgbuf;
361   const struct GNUNET_MessageHeader *currhdr;
362
363 #if DEBUG_UDP
364       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
365                        ("entered select...\n"));
366 #endif
367
368       buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
369
370 #if DEBUG_UDP
371       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
372                        ("we expect to read %u bytes\n"), buflen);
373 #endif
374
375     if (buflen == GNUNET_NO)
376       return;
377
378     buf = GNUNET_malloc (buflen);
379     fromlen = sizeof (addr);
380
381     memset (&addr, 0, fromlen);
382     ret =
383       GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
384                                       (struct sockaddr *) &addr, &fromlen);
385
386 #if DEBUG_UDP
387     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
388                      ("socket_recv returned %u, src_addr_len is %u\n"), ret,
389                      fromlen);
390 #endif
391
392     if (ret <= 0)
393       {
394         GNUNET_free (buf);
395         return;
396       }
397     msg = (struct UDPMessage *) buf;
398
399 #if DEBUG_UDP
400     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
401                      ("header reports message size of %d\n"),
402                      ntohs (msg->header.size));
403
404     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
405                      ("header reports message type of %d\n"),
406                      ntohs (msg->header.type));
407 #endif
408     if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
409       {
410         GNUNET_free (buf);
411         GNUNET_NETWORK_fdset_zero (plugin->rs);
412         GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock);
413         return;
414       }
415     msgbuf = (char *)&msg[1];
416     sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
417     memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
418
419     offset = 0;
420     count = 0;
421     tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
422 #if DEBUG_UDP
423     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UDP", _
424                      ("offset is %d, tsize is %d (UDPMessage size is %d)\n"),
425                      offset, tsize, sizeof(struct UDPMessage));
426 #endif
427     while (offset < tsize)
428       {
429         currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
430 #if DEBUG_UDP
431     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
432                      ("processing msg %d: type %d, size %d at offset %d\n"),
433                      count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
434 #endif
435         plugin->env->receive (plugin->env->cls,
436             sender, currhdr, UDP_DIRECT_DISTANCE, (char *)&addr, fromlen);
437         offset += ntohs(currhdr->size);
438 #if DEBUG_UDP
439     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
440                      ("offset now %d, tsize %d\n"),
441                      offset, tsize);
442 #endif
443         count++;
444       }
445
446     GNUNET_free (sender);
447     GNUNET_free (buf);
448
449   plugin->select_task =
450     GNUNET_SCHEDULER_add_select (plugin->env->sched,
451                                  GNUNET_SCHEDULER_PRIORITY_DEFAULT,
452                                  GNUNET_SCHEDULER_NO_TASK,
453                                  GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
454                                  NULL, &udp_plugin_select, plugin);
455
456 }
457
458 /**
459  * Create a UDP socket.  If possible, use IPv6, otherwise
460  * try IPv4.
461  */
462 static struct GNUNET_NETWORK_Handle *
463 udp_transport_server_start (void *cls)
464 {
465   struct Plugin *plugin = cls;
466   struct GNUNET_NETWORK_Handle *desc;
467   struct sockaddr_in serverAddrv4;
468   struct sockaddr_in6 serverAddrv6;
469   struct sockaddr *serverAddr;
470   socklen_t addrlen;
471
472   desc = NULL;
473   if (GNUNET_YES !=
474       GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
475                                             "DISABLE-IPV6"))
476     {
477       desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
478       if (desc != NULL)
479         {
480           memset (&serverAddrv6, 0, sizeof (serverAddrv6));
481 #if HAVE_SOCKADDR_IN_SIN_LEN
482           serverAddrv6.sin6_len = sizeof (serverAddrv6);
483 #endif
484           serverAddrv6.sin6_family = AF_INET6;
485           serverAddrv6.sin6_addr = in6addr_any;
486           serverAddrv6.sin6_port = htons (plugin->open_port);
487           addrlen = sizeof (serverAddrv6);
488           serverAddr = (struct sockaddr *) &serverAddrv6;
489         }
490     }
491   if (NULL == desc)
492     {
493       desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
494       if (NULL == desc)
495         {
496           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
497           return NULL;
498         }
499       else
500         {
501           memset (&serverAddrv4, 0, sizeof (serverAddrv4));
502 #if HAVE_SOCKADDR_IN_SIN_LEN
503           serverAddrv4.sin_len = sizeof (serverAddrv4);
504 #endif
505           serverAddrv4.sin_family = AF_INET;
506           serverAddrv4.sin_addr.s_addr = INADDR_ANY;
507           serverAddrv4.sin_port = htons (plugin->open_port);
508           addrlen = sizeof (serverAddrv4);
509           serverAddr = (struct sockaddr *) &serverAddrv4;
510         }
511     }
512
513   if (desc != NULL)
514     {
515       GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
516                      GNUNET_OK);
517     }
518
519   plugin->rs = GNUNET_NETWORK_fdset_create ();
520
521   GNUNET_NETWORK_fdset_zero (plugin->rs);
522   GNUNET_NETWORK_fdset_set (plugin->rs, desc);
523
524   plugin->select_task =
525     GNUNET_SCHEDULER_add_select (plugin->env->sched,
526                                  GNUNET_SCHEDULER_PRIORITY_DEFAULT,
527                                  GNUNET_SCHEDULER_NO_TASK,
528                                  GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
529                                  NULL, &udp_plugin_select, plugin);
530
531   return desc;
532 }
533
534
535 /**
536  * Check if the given port is plausible (must be either
537  * our listen port or our advertised port).  If it is
538  * neither, we return one of these two ports at random.
539  *
540  * @return either in_port or a more plausible port
541  */
542 static uint16_t
543 check_port (struct Plugin *plugin, uint16_t in_port)
544 {
545   if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
546     return in_port;
547   return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
548                                     2) == 0)
549     ? plugin->open_port : plugin->adv_port;
550 }
551
552
553 /**
554  * Another peer has suggested an address for this peer and transport
555  * plugin.  Check that this could be a valid address.  This function
556  * is not expected to 'validate' the address in the sense of trying to
557  * connect to it but simply to see if the binary format is technically
558  * legal for establishing a connection.
559  *
560  * @param cls handle to Plugin
561  * @param addr address to check
562  * @param addrlen length of addr
563  * @return GNUNET_OK if this is a plausible address for this peer
564  *         and transport, GNUNET_SYSERR if not
565  *
566  * TODO: perhaps make everything work with sockaddr_storage, it may
567  *       be a cleaner way to handle addresses in UDP
568  */
569 static int
570 udp_check_address (void *cls, void *addr, size_t addrlen)
571 {
572   struct Plugin *plugin = cls;
573   char buf[sizeof (struct sockaddr_in6)];
574
575   struct sockaddr_in *v4;
576   struct sockaddr_in6 *v6;
577
578   if ((addrlen != sizeof (struct sockaddr_in)) &&
579       (addrlen != sizeof (struct sockaddr_in6)))
580     {
581       GNUNET_break_op (0);
582       return GNUNET_SYSERR;
583     }
584   memcpy (buf, addr, sizeof (struct sockaddr_in6));
585   if (addrlen == sizeof (struct sockaddr_in))
586     {
587       v4 = (struct sockaddr_in *) buf;
588       v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
589     }
590   else
591     {
592       v6 = (struct sockaddr_in6 *) buf;
593       v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
594     }
595 #if DEBUG_UDP
596   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
597                    "tcp",
598                    "Informing transport service about my address `%s'.\n",
599                    GNUNET_a2s (addr, addrlen));
600 #endif
601   return GNUNET_OK;
602   return GNUNET_OK;
603 }
604
605
606 /**
607  * Append our port and forward the result.
608  */
609 static void
610 append_port (void *cls, const char *hostname)
611 {
612   struct PrettyPrinterContext *ppc = cls;
613   char *ret;
614
615   if (hostname == NULL)
616     {
617       ppc->asc (ppc->asc_cls, NULL);
618       GNUNET_free (ppc);
619       return;
620     }
621   GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
622   ppc->asc (ppc->asc_cls, ret);
623   GNUNET_free (ret);
624 }
625
626
627 /**
628  * Convert the transports address to a nice, human-readable
629  * format.
630  *
631  * @param cls closure
632  * @param type name of the transport that generated the address
633  * @param addr one of the addresses of the host, NULL for the last address
634  *        the specific address format depends on the transport
635  * @param addrlen length of the address
636  * @param numeric should (IP) addresses be displayed in numeric form?
637  * @param timeout after how long should we give up?
638  * @param asc function to call on each string
639  * @param asc_cls closure for asc
640  */
641 static void
642 udp_plugin_address_pretty_printer (void *cls,
643                                    const char *type,
644                                    const void *addr,
645                                    size_t addrlen,
646                                    int numeric,
647                                    struct GNUNET_TIME_Relative timeout,
648                                    GNUNET_TRANSPORT_AddressStringCallback asc,
649                                    void *asc_cls)
650 {
651   struct Plugin *plugin = cls;
652   const struct sockaddr_in *v4;
653   const struct sockaddr_in6 *v6;
654   struct PrettyPrinterContext *ppc;
655
656   if ((addrlen != sizeof (struct sockaddr_in)) &&
657       (addrlen != sizeof (struct sockaddr_in6)))
658     {
659       /* invalid address */
660       GNUNET_break_op (0);
661       asc (asc_cls, NULL);
662       return;
663     }
664   ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
665   ppc->asc = asc;
666   ppc->asc_cls = asc_cls;
667   if (addrlen == sizeof (struct sockaddr_in))
668     {
669       v4 = (const struct sockaddr_in *) addr;
670       ppc->port = ntohs (v4->sin_port);
671     }
672   else
673     {
674       v6 = (const struct sockaddr_in6 *) addr;
675       ppc->port = ntohs (v6->sin6_port);
676
677     }
678   GNUNET_RESOLVER_hostname_get (plugin->env->sched,
679                                 plugin->env->cfg,
680                                 addr,
681                                 addrlen,
682                                 !numeric, timeout, &append_port, ppc);
683 }
684
685
686 /**
687  * The exported method. Makes the core api available via a global and
688  * returns the udp transport API.
689  */
690 void *
691 libgnunet_plugin_transport_udp_init (void *cls)
692 {
693   unsigned long long mtu;
694
695   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
696   struct GNUNET_TRANSPORT_PluginFunctions *api;
697   struct Plugin *plugin;
698   struct GNUNET_SERVICE_Context *service;
699   unsigned long long aport;
700   unsigned long long bport;
701
702   service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
703   if (service == NULL)
704     {
705       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
706                        ("Failed to start service for `%s' transport plugin.\n"),
707                        "udp");
708       return NULL;
709     }
710   aport = 0;
711   if ((GNUNET_OK !=
712        GNUNET_CONFIGURATION_get_value_number (env->cfg,
713                                               "transport-udp",
714                                               "PORT",
715                                               &bport)) ||
716       (bport > 65535) ||
717       ((GNUNET_OK ==
718         GNUNET_CONFIGURATION_get_value_number (env->cfg,
719                                                "transport-udp",
720                                                "ADVERTISED-PORT",
721                                                &aport)) && (aport > 65535)))
722     {
723       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
724                        "udp",
725                        _
726                        ("Require valid port number for service `%s' in configuration!\n"),
727                        "transport-udp");
728       GNUNET_SERVICE_stop (service);
729       return NULL;
730     }
731   if (aport == 0)
732     aport = bport;
733
734   mtu = 1240;
735   if (mtu < 1200)
736     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
737                      "udp",
738                      _("MTU %llu for `%s' is probably too low!\n"), mtu,
739                      "UDP");
740
741   plugin = GNUNET_malloc (sizeof (struct Plugin));
742   plugin->open_port = bport;
743   plugin->adv_port = aport;
744   plugin->env = env;
745   plugin->statistics = NULL;
746   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
747   api->cls = plugin;
748
749   api->send = &udp_plugin_send;
750   api->disconnect = &udp_disconnect;
751   api->address_pretty_printer = &udp_plugin_address_pretty_printer;
752   api->check_address = &udp_check_address;
753
754   plugin->service = service;
755
756   /* FIXME: do the two calls below periodically again and
757      not just once (since the info we get might change...) */
758   GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
759   plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
760                                                            env->cfg,
761                                                            AF_UNSPEC,
762                                                            HOSTNAME_RESOLVE_TIMEOUT,
763                                                            &process_hostname_ips,
764                                                            plugin);
765
766   udp_sock = udp_transport_server_start (plugin);
767
768   GNUNET_assert (udp_sock != NULL);
769
770   return api;
771 }
772
773 void *
774 libgnunet_plugin_transport_udp_done (void *cls)
775 {
776   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
777   struct Plugin *plugin = api->cls;
778
779   udp_transport_server_stop (plugin);
780   if (NULL != hostname_dns)
781     {
782       GNUNET_RESOLVER_request_cancel (hostname_dns);
783       hostname_dns = NULL;
784     }
785   GNUNET_SERVICE_stop (plugin->service);
786
787   GNUNET_NETWORK_fdset_destroy (plugin->rs);
788   GNUNET_free (plugin);
789   GNUNET_free (api);
790   return NULL;
791 }
792
793 /* end of plugin_transport_udp.c */