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