fix
[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, -1 on error (in this case, cont is not called)
203  */
204 static ssize_t
205 udp_plugin_send (void *cls,
206                  const struct GNUNET_PeerIdentity *target,
207                  const char *msgbuf,
208                  size_t msgbuf_size,
209                  unsigned int priority,
210                  struct GNUNET_TIME_Relative timeout,
211                  const void *addr,
212                  size_t addrlen,
213                  int force_address,
214                  GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
215 {
216   struct Plugin *plugin = cls;
217   struct UDPMessage *message;
218   int ssize;
219   ssize_t sent;
220
221   GNUNET_assert(udp_sock != NULL);
222   if ( (addr == NULL) || (addrlen == 0) )
223     {
224 #if DEBUG_UDP
225   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
226                    ("udp_plugin_send called without address, returning!\n"));
227 #endif
228       return -1; /* Can never send if we don't have an address!! */
229     }
230
231   /* Build the message to be sent */
232   message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
233   ssize = sizeof (struct UDPMessage) + msgbuf_size;
234
235 #if DEBUG_UDP
236   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
237                    ("In udp_send, ssize is %d, sending message to %s\n"), ssize, GNUNET_a2s((const struct sockaddr *)addr, addrlen));
238 #endif
239   message->header.size = htons (ssize);
240   message->header.type = htons (0);
241   memcpy (&message->sender, plugin->env->my_identity,
242           sizeof (struct GNUNET_PeerIdentity));
243   memcpy (&message[1], msgbuf, msgbuf_size);
244   sent =
245     GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
246                                   addr,
247                                   addrlen);
248   if ( (cont != NULL) &&
249        (sent != -1) )
250     cont (cont_cls, target, GNUNET_OK);
251   GNUNET_free (message);
252   return sent;
253 }
254
255
256 /**
257  * Add the IP of our network interface to the list of
258  * our external IP addresses.
259  */
260 static int
261 process_interfaces (void *cls,
262                     const char *name,
263                     int isDefault,
264                     const struct sockaddr *addr, socklen_t addrlen)
265 {
266   struct Plugin *plugin = cls;
267   int af;
268   struct sockaddr_in *v4;
269   struct sockaddr_in6 *v6;
270
271   af = addr->sa_family;
272   if (af == AF_INET)
273     {
274       v4 = (struct sockaddr_in *) addr;
275       v4->sin_port = htons (plugin->adv_port);
276     }
277   else
278     {
279       GNUNET_assert (af == AF_INET6);
280       v6 = (struct sockaddr_in6 *) addr;
281       v6->sin6_port = htons (plugin->adv_port);
282     }
283   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
284                    GNUNET_ERROR_TYPE_BULK,
285                    "udp", _("Found address `%s' (%s)\n"),
286                    GNUNET_a2s (addr, addrlen), name);
287   plugin->env->notify_address (plugin->env->cls,
288                                "udp",
289                                addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
290
291   return GNUNET_OK;
292 }
293
294
295 /**
296  * Function called by the resolver for each address obtained from DNS
297  * for our own hostname.  Add the addresses to the list of our
298  * external IP addresses.
299  *
300  * @param cls closure
301  * @param addr one of the addresses of the host, NULL for the last address
302  * @param addrlen length of the address
303  */
304 static void
305 process_hostname_ips (void *cls,
306                       const struct sockaddr *addr, socklen_t addrlen)
307 {
308   struct Plugin *plugin = cls;
309
310   if (addr == NULL)
311     {
312       plugin->hostname_dns = NULL;
313       return;
314     }
315   process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
316 }
317
318
319 /*
320  * @param cls the plugin handle
321  * @param tc the scheduling context (for rescheduling this function again)
322  *
323  * We have been notified that our writeset has something to read.  Presumably
324  * select has been called already, so we can go ahead and start reading from
325  * the socket immediately.  Then we check if there is more to be read by
326  * calling select ourselves while there is stuff on the wire.  Then reschedule
327  * this function to be called again once more is available.
328  *
329  */
330 static void
331 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
332 {
333   struct Plugin *plugin = cls;
334   char *buf;
335   struct UDPMessage *msg;
336   struct GNUNET_PeerIdentity *sender;
337   unsigned int buflen;
338   socklen_t fromlen;
339   struct sockaddr_storage addr;
340   ssize_t ret;
341   int offset;
342   int count;
343   int tsize;
344   char *msgbuf;
345   const struct GNUNET_MessageHeader *currhdr;
346
347 #if DEBUG_UDP
348       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
349                        ("entered select...\n"));
350 #endif
351
352       buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
353
354 #if DEBUG_UDP
355       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
356                        ("we expect to read %u bytes\n"), buflen);
357 #endif
358
359     if (buflen == GNUNET_NO)
360       return;
361
362     buf = GNUNET_malloc (buflen);
363     fromlen = sizeof (addr);
364
365     memset (&addr, 0, fromlen);
366     ret =
367       GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
368                                       (struct sockaddr *) &addr, &fromlen);
369
370 #if DEBUG_UDP
371     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
372                      ("socket_recv returned %u, src_addr_len is %u\n"), ret,
373                      fromlen);
374 #endif
375
376     if (ret <= 0)
377       {
378         GNUNET_free (buf);
379         return;
380       }
381     msg = (struct UDPMessage *) buf;
382
383 #if DEBUG_UDP
384     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
385                      ("header reports message size of %d\n"),
386                      ntohs (msg->header.size));
387
388     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
389                      ("header reports message type of %d\n"),
390                      ntohs (msg->header.type));
391 #endif
392     if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
393       {
394         GNUNET_free (buf);
395         GNUNET_NETWORK_fdset_zero (plugin->rs);
396         GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock);
397         return;
398       }
399     msgbuf = (char *)&msg[1];
400     sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
401     memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
402
403     offset = 0;
404     count = 0;
405     tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
406 #if DEBUG_UDP
407     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "UDP", _
408                      ("offset is %d, tsize is %d (UDPMessage size is %d)\n"),
409                      offset, tsize, sizeof(struct UDPMessage));
410 #endif
411     while (offset < tsize)
412       {
413         currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
414 #if DEBUG_UDP
415     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
416                      ("processing msg %d: type %d, size %d at offset %d\n"),
417                      count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
418 #endif
419         plugin->env->receive (plugin->env->cls,
420             sender, currhdr, UDP_DIRECT_DISTANCE, (char *)&addr, fromlen);
421         offset += ntohs(currhdr->size);
422 #if DEBUG_UDP
423     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
424                      ("offset now %d, tsize %d\n"),
425                      offset, tsize);
426 #endif
427         count++;
428       }
429
430     GNUNET_free (sender);
431     GNUNET_free (buf);
432
433   plugin->select_task =
434     GNUNET_SCHEDULER_add_select (plugin->env->sched,
435                                  GNUNET_SCHEDULER_PRIORITY_DEFAULT,
436                                  GNUNET_SCHEDULER_NO_TASK,
437                                  GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
438                                  NULL, &udp_plugin_select, plugin);
439
440 }
441
442 /**
443  * Create a UDP socket.  If possible, use IPv6, otherwise
444  * try IPv4.
445  */
446 static struct GNUNET_NETWORK_Handle *
447 udp_transport_server_start (void *cls)
448 {
449   struct Plugin *plugin = cls;
450   struct GNUNET_NETWORK_Handle *desc;
451   struct sockaddr_in serverAddrv4;
452   struct sockaddr_in6 serverAddrv6;
453   struct sockaddr *serverAddr;
454   socklen_t addrlen;
455
456   desc = NULL;
457   if (GNUNET_YES !=
458       GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
459                                             "DISABLE-IPV6"))
460     {
461       desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
462       if (desc != NULL)
463         {
464           memset (&serverAddrv6, 0, sizeof (serverAddrv6));
465 #if HAVE_SOCKADDR_IN_SIN_LEN
466           serverAddrv6.sin6_len = sizeof (serverAddrv6);
467 #endif
468           serverAddrv6.sin6_family = AF_INET6;
469           serverAddrv6.sin6_addr = in6addr_any;
470           serverAddrv6.sin6_port = htons (plugin->open_port);
471           addrlen = sizeof (serverAddrv6);
472           serverAddr = (struct sockaddr *) &serverAddrv6;
473         }
474     }
475   if (NULL == desc)
476     {
477       desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
478       if (NULL == desc)
479         {
480           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
481           return NULL;
482         }
483       else
484         {
485           memset (&serverAddrv4, 0, sizeof (serverAddrv4));
486 #if HAVE_SOCKADDR_IN_SIN_LEN
487           serverAddrv4.sin_len = sizeof (serverAddrv4);
488 #endif
489           serverAddrv4.sin_family = AF_INET;
490           serverAddrv4.sin_addr.s_addr = INADDR_ANY;
491           serverAddrv4.sin_port = htons (plugin->open_port);
492           addrlen = sizeof (serverAddrv4);
493           serverAddr = (struct sockaddr *) &serverAddrv4;
494         }
495     }
496
497   if (desc != NULL)
498     {
499       GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
500                      GNUNET_OK);
501     }
502
503   plugin->rs = GNUNET_NETWORK_fdset_create ();
504
505   GNUNET_NETWORK_fdset_zero (plugin->rs);
506   GNUNET_NETWORK_fdset_set (plugin->rs, desc);
507
508   plugin->select_task =
509     GNUNET_SCHEDULER_add_select (plugin->env->sched,
510                                  GNUNET_SCHEDULER_PRIORITY_DEFAULT,
511                                  GNUNET_SCHEDULER_NO_TASK,
512                                  GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
513                                  NULL, &udp_plugin_select, plugin);
514
515   return desc;
516 }
517
518
519 /**
520  * Check if the given port is plausible (must be either
521  * our listen port or our advertised port).  If it is
522  * neither, we return one of these two ports at random.
523  *
524  * @return either in_port or a more plausible port
525  */
526 static uint16_t
527 check_port (struct Plugin *plugin, uint16_t in_port)
528 {
529   if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
530     return in_port;
531   return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
532                                     2) == 0)
533     ? plugin->open_port : plugin->adv_port;
534 }
535
536
537 /**
538  * Another peer has suggested an address for this peer and transport
539  * plugin.  Check that this could be a valid address.  This function
540  * is not expected to 'validate' the address in the sense of trying to
541  * connect to it but simply to see if the binary format is technically
542  * legal for establishing a connection.
543  *
544  * @param cls handle to Plugin
545  * @param addr address to check
546  * @param addrlen length of addr
547  * @return GNUNET_OK if this is a plausible address for this peer
548  *         and transport, GNUNET_SYSERR if not
549  *
550  * TODO: perhaps make everything work with sockaddr_storage, it may
551  *       be a cleaner way to handle addresses in UDP
552  */
553 static int
554 udp_check_address (void *cls, void *addr, size_t addrlen)
555 {
556   struct Plugin *plugin = cls;
557   char buf[sizeof (struct sockaddr_in6)];
558
559   struct sockaddr_in *v4;
560   struct sockaddr_in6 *v6;
561
562   if ((addrlen != sizeof (struct sockaddr_in)) &&
563       (addrlen != sizeof (struct sockaddr_in6)))
564     {
565       GNUNET_break_op (0);
566       return GNUNET_SYSERR;
567     }
568   memcpy (buf, addr, sizeof (struct sockaddr_in6));
569   if (addrlen == sizeof (struct sockaddr_in))
570     {
571       v4 = (struct sockaddr_in *) buf;
572       v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
573     }
574   else
575     {
576       v6 = (struct sockaddr_in6 *) buf;
577       v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
578     }
579 #if DEBUG_UDP
580   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
581                    "tcp",
582                    "Informing transport service about my address `%s'.\n",
583                    GNUNET_a2s (addr, addrlen));
584 #endif
585   return GNUNET_OK;
586   return GNUNET_OK;
587 }
588
589
590 /**
591  * Append our port and forward the result.
592  */
593 static void
594 append_port (void *cls, const char *hostname)
595 {
596   struct PrettyPrinterContext *ppc = cls;
597   char *ret;
598
599   if (hostname == NULL)
600     {
601       ppc->asc (ppc->asc_cls, NULL);
602       GNUNET_free (ppc);
603       return;
604     }
605   GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
606   ppc->asc (ppc->asc_cls, ret);
607   GNUNET_free (ret);
608 }
609
610
611 /**
612  * Convert the transports address to a nice, human-readable
613  * format.
614  *
615  * @param cls closure
616  * @param type name of the transport that generated the address
617  * @param addr one of the addresses of the host, NULL for the last address
618  *        the specific address format depends on the transport
619  * @param addrlen length of the address
620  * @param numeric should (IP) addresses be displayed in numeric form?
621  * @param timeout after how long should we give up?
622  * @param asc function to call on each string
623  * @param asc_cls closure for asc
624  */
625 static void
626 udp_plugin_address_pretty_printer (void *cls,
627                                    const char *type,
628                                    const void *addr,
629                                    size_t addrlen,
630                                    int numeric,
631                                    struct GNUNET_TIME_Relative timeout,
632                                    GNUNET_TRANSPORT_AddressStringCallback asc,
633                                    void *asc_cls)
634 {
635   struct Plugin *plugin = cls;
636   const struct sockaddr_in *v4;
637   const struct sockaddr_in6 *v6;
638   struct PrettyPrinterContext *ppc;
639
640   if ((addrlen != sizeof (struct sockaddr_in)) &&
641       (addrlen != sizeof (struct sockaddr_in6)))
642     {
643       /* invalid address */
644       GNUNET_break_op (0);
645       asc (asc_cls, NULL);
646       return;
647     }
648   ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
649   ppc->asc = asc;
650   ppc->asc_cls = asc_cls;
651   if (addrlen == sizeof (struct sockaddr_in))
652     {
653       v4 = (const struct sockaddr_in *) addr;
654       ppc->port = ntohs (v4->sin_port);
655     }
656   else
657     {
658       v6 = (const struct sockaddr_in6 *) addr;
659       ppc->port = ntohs (v6->sin6_port);
660
661     }
662   GNUNET_RESOLVER_hostname_get (plugin->env->sched,
663                                 plugin->env->cfg,
664                                 addr,
665                                 addrlen,
666                                 !numeric, timeout, &append_port, ppc);
667 }
668
669
670 /**
671  * The exported method. Makes the core api available via a global and
672  * returns the udp transport API.
673  */
674 void *
675 libgnunet_plugin_transport_udp_init (void *cls)
676 {
677   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
678   struct GNUNET_TRANSPORT_PluginFunctions *api;
679   struct Plugin *plugin;
680   struct GNUNET_SERVICE_Context *service;
681   unsigned long long aport;
682   unsigned long long bport;
683   unsigned long long mtu;
684
685   service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
686   if (service == NULL)
687     {
688       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
689                        ("Failed to start service for `%s' transport plugin.\n"),
690                        "udp");
691       return NULL;
692     }
693   aport = 0;
694   if ((GNUNET_OK !=
695        GNUNET_CONFIGURATION_get_value_number (env->cfg,
696                                               "transport-udp",
697                                               "PORT",
698                                               &bport)) ||
699       (bport > 65535) ||
700       ((GNUNET_OK ==
701         GNUNET_CONFIGURATION_get_value_number (env->cfg,
702                                                "transport-udp",
703                                                "ADVERTISED-PORT",
704                                                &aport)) && (aport > 65535)))
705     {
706       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
707                        "udp",
708                        _
709                        ("Require valid port number for service `%s' in configuration!\n"),
710                        "transport-udp");
711       GNUNET_SERVICE_stop (service);
712       return NULL;
713     }
714   if (aport == 0)
715     aport = bport;
716
717   mtu = 1240;
718   if (mtu < 1200)
719     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
720                      "udp",
721                      _("MTU %llu for `%s' is probably too low!\n"), mtu,
722                      "UDP");
723
724   plugin = GNUNET_malloc (sizeof (struct Plugin));
725   plugin->open_port = bport;
726   plugin->adv_port = aport;
727   plugin->env = env;
728   plugin->statistics = NULL;
729   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
730   api->cls = plugin;
731
732   api->send = &udp_plugin_send;
733   api->disconnect = &udp_disconnect;
734   api->address_pretty_printer = &udp_plugin_address_pretty_printer;
735   api->check_address = &udp_check_address;
736
737   plugin->service = service;
738
739   /* FIXME: do the two calls below periodically again and
740      not just once (since the info we get might change...) */
741   GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
742   plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
743                                                            env->cfg,
744                                                            AF_UNSPEC,
745                                                            HOSTNAME_RESOLVE_TIMEOUT,
746                                                            &process_hostname_ips,
747                                                            plugin);
748
749   udp_sock = udp_transport_server_start (plugin);
750
751   GNUNET_assert (udp_sock != NULL);
752
753   return api;
754 }
755
756 void *
757 libgnunet_plugin_transport_udp_done (void *cls)
758 {
759   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
760   struct Plugin *plugin = api->cls;
761
762   udp_transport_server_stop (plugin);
763   if (NULL != hostname_dns)
764     {
765       GNUNET_RESOLVER_request_cancel (hostname_dns);
766       hostname_dns = NULL;
767     }
768   GNUNET_SERVICE_stop (plugin->service);
769
770   GNUNET_NETWORK_fdset_destroy (plugin->rs);
771   GNUNET_free (plugin);
772   GNUNET_free (api);
773   return NULL;
774 }
775
776 /* end of plugin_transport_udp.c */