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