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