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