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