small changes
[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  * The default maximum size of each outbound UDP message,
47  * optimal value for Ethernet (10 or 100 MBit).
48  */
49 #define MESSAGE_SIZE 1472
50
51 /**
52  * Handle for request of hostname resolution, non-NULL if pending.
53  */
54 static struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
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 /* Forward definition */
74 struct Plugin;
75
76 /**
77  * Session handle for UDP connections.
78  */
79 struct Session
80 {
81
82   /**
83    * Stored in a linked list.
84    */
85   struct Session *next;
86
87   /**
88    * Pointer to the global plugin struct.
89    */
90   struct Plugin *plugin;
91
92   /**
93    * To whom are we talking to (set to our identity
94    */
95   struct GNUNET_PeerIdentity target;
96
97   /**
98    * Address of the other peer if WE initiated the connection
99    * (and hence can be sure what it is), otherwise NULL.
100    */
101   void *connect_addr;
102
103   /**
104    * Length of connect_addr, can be 0.
105    */
106   size_t connect_alen;
107
108   /*
109    * Random challenge number for validation
110    */
111   int challenge;
112
113   /*
114    * Have we received validation (performed ping/pong) from this peer?
115    */
116   unsigned int validated;
117
118 };
119
120 /**
121  * Encapsulation of all of the state of the plugin.
122  */
123 struct Plugin
124 {
125   /**
126    * Our environment.
127    */
128   struct GNUNET_TRANSPORT_PluginEnvironment *env;
129
130   /**
131    * List of open TCP sessions.
132    */
133   struct Session *sessions;
134
135   /**
136    * Handle for the statistics service.
137    */
138   struct GNUNET_STATISTICS_Handle *statistics;
139
140   /**
141    * Handle to the network service.
142    */
143   struct GNUNET_SERVICE_Context *service;
144
145   /**
146    * ID of task used to update our addresses when one expires.
147    */
148   GNUNET_SCHEDULER_TaskIdentifier address_update_task;
149
150   /**
151    * ID of select task
152    */
153   GNUNET_SCHEDULER_TaskIdentifier select_task;
154
155   /**
156    * Port that we are actually listening on.
157    */
158   uint16_t open_port;
159
160   /**
161    * Port that the user said we would have visible to the
162    * rest of the world.
163    */
164   uint16_t adv_port;
165
166   /*
167    * FD Read set
168    */
169   struct GNUNET_NETWORK_FDSet *rs;
170
171 };
172
173 /**
174  * Message used to ask a peer to validate receipt (to check an address
175  * from a HELLO).  Followed by the address used.  Note that the
176  * recipients response does not affirm that he has this address,
177  * only that he got the challenge message.
178  */
179 struct UDPPingMessage
180 {
181
182   /**
183    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING
184    */
185   struct GNUNET_MessageHeader header;
186
187   /**
188    * Random challenge number (in network byte order).
189    */
190   uint32_t challenge GNUNET_PACKED;
191
192
193
194 };
195
196
197 /**
198  * Message used to validate a HELLO.  The challenge is included in the
199  * confirmation to make matching of replies to requests possible.  The
200  * signature signs the original challenge number, our public key, the
201  * sender's address (so that the sender can check that the address we
202  * saw is plausible for him and possibly detect a MiM attack) and a
203  * timestamp (to limit replay).<p>
204  *
205  * This message is followed by the address of the
206  * client that we are observing (which is part of what
207  * is being signed).
208  */
209 struct UDPPongMessage
210 {
211   /**
212    * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG
213    */
214   struct GNUNET_MessageHeader header;
215
216   /**
217    * Random challenge number (in network byte order).
218    */
219   uint32_t challenge GNUNET_PACKED;
220
221   /* Length of addr, appended to end of message */
222   unsigned int addrlen;
223 };
224
225 /* *********** globals ************* */
226
227 /**
228  * the socket that we transmit all data with
229  */
230 static struct GNUNET_NETWORK_Handle *udp_sock;
231
232
233 /**
234  * A (core) Session is to be associated with a transport session. The
235  * transport service may want to know in order to call back on the
236  * core if the connection is being closed.
237  *
238  * @param session the session handle passed along
239  *   from the call to receive that was made by the transport
240  *   layer
241  * @return GNUNET_OK if the session could be associated,
242  *         GNUNET_SYSERR if not.
243  */
244 int
245 udp_associate (struct Session *session)
246 {
247   return GNUNET_SYSERR;         /* UDP connections can never be associated */
248 }
249
250 /**
251  * Disconnect from a remote node.
252  *
253  * @param tsession the session that is closed
254  * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
255  */
256 void
257 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
258 {
259   return;
260 }
261
262 /**
263  * Shutdown the server process (stop receiving inbound traffic). Maybe
264  * restarted later!
265  */
266 static int
267 udp_transport_server_stop (void *cls)
268 {
269   struct Plugin *plugin = cls;
270   GNUNET_assert (udp_sock != NULL);
271   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
272     {
273       GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
274       plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
275     }
276
277   GNUNET_NETWORK_socket_close (udp_sock);
278   udp_sock = NULL;
279   return GNUNET_OK;
280 }
281
282 static struct Session *
283 find_session (void *cls, struct Session *session_list,
284               const struct GNUNET_PeerIdentity *peer)
285 {
286   struct Plugin *plugin = cls;
287   struct Session *pos;
288   pos = session_list;
289
290   while (pos != NULL)
291     {
292       if (memcmp (peer, &pos->target, sizeof (struct GNUNET_PeerIdentity)) ==
293           0)
294         return pos;
295       pos = pos->next;
296     }
297
298   return NULL;
299 }
300
301 /**
302  * Function that can be used by the transport service to transmit
303  * a message using the plugin.
304  *
305  * @param cls closure
306  * @param service_context value passed to the transport-service
307  *        to identify the neighbour
308  * @param target who should receive this message
309  * @param priority how important is the message
310  * @param msg the message to transmit
311  * @param timeout when should we time out (give up) if we can not transmit?
312  * @param cont continuation to call once the message has
313  *        been transmitted (or if the transport is ready
314  *        for the next transmission call; or if the
315  *        peer disconnected...)
316  * @param cont_cls closure for cont
317  */
318 static void
319 udp_plugin_send (void *cls,
320                  const struct GNUNET_PeerIdentity *target,
321                  unsigned int priority,
322                  const struct GNUNET_MessageHeader *msg,
323                  struct GNUNET_TIME_Relative timeout,
324                  GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
325 {
326   struct Plugin *plugin = cls;
327   struct Session *session;
328   struct UDPMessage *message;
329   int ssize;
330   size_t sent;
331
332   session = find_session (plugin, plugin->sessions, target);
333
334   if ((session == NULL) || (udp_sock == NULL))
335     return;
336
337   /* Build the message to be sent */
338   message = GNUNET_malloc (sizeof (struct UDPMessage) + ntohs (msg->size));
339   ssize = sizeof (struct UDPMessage) + ntohs (msg->size);
340
341 #if DEBUG_UDP
342   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
343                    ("In udp_send, ssize is %d\n"), ssize);
344 #endif
345   message->header.size = htons (ssize);
346   message->header.type = htons (0);
347   memcpy (&message->sender, plugin->env->my_identity,
348           sizeof (struct GNUNET_PeerIdentity));
349   memcpy (&message[1], msg, ntohs (msg->size));
350
351   /* Actually send the message */
352   sent =
353     GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
354                                   session->connect_addr,
355                                   session->connect_alen);
356
357   if (cont != NULL)
358     {
359       if (sent == GNUNET_SYSERR)
360         cont (cont_cls, target, GNUNET_SYSERR);
361       else
362         cont (cont_cls, target, GNUNET_OK);
363     }
364   GNUNET_free(message);
365   return;
366 }
367
368 /**
369  * We've received a PING from this peer via UDP.
370  * Send back our PONG.
371  *
372  * @param cls closure
373  * @param sender the Identity of the sender
374  * @param message the actual message
375  */
376 static void
377 handle_udp_ping (void *cls,
378                  struct GNUNET_PeerIdentity *sender,
379                  struct sockaddr_storage *addr, size_t addrlen,
380                  const struct GNUNET_MessageHeader *message)
381 {
382   struct Plugin *plugin = cls;
383   struct Session *head = plugin->sessions;
384   const struct UDPPingMessage *ping = (const struct UDPPingMessage *) message;
385   struct UDPPongMessage *pong;
386   struct Session *found;
387
388 #if DEBUG_UDP
389   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
390                    ("handling ping, challenge is %d\n"),
391                    ntohs (ping->challenge));
392 #endif
393   found = find_session (plugin, head, sender);
394   if (found != NULL)
395     {
396       pong = GNUNET_malloc (sizeof (struct UDPPongMessage) + addrlen);
397       pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG);
398       pong->header.size = htons (sizeof (struct UDPPongMessage) + addrlen);
399       pong->challenge = ping->challenge;
400       memcpy (&pong[1], addr, addrlen);
401       pong->addrlen = htons (addrlen);
402
403       udp_plugin_send (plugin, sender, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
404                        &pong->header,
405                        GNUNET_TIME_relative_multiply
406                        (GNUNET_TIME_UNIT_SECONDS, 30), NULL, NULL);
407       GNUNET_free(pong);
408     }
409
410   return;
411
412 }
413
414 /**
415  * We've received a PONG from this peer via UDP.
416  * Great. Call validate func if we haven't already
417  * received a PONG.
418  *
419  * @param cls closure
420  * @param client identification of the client
421  * @param message the actual message
422  */
423 static void
424 handle_udp_pong (void *cls,
425                  struct GNUNET_PeerIdentity *sender,
426                  const struct GNUNET_MessageHeader *message)
427 {
428   struct Plugin *plugin = cls;
429   const struct UDPPongMessage *pong = (struct UDPPongMessage *) message;
430   struct Session *found;
431   unsigned int addr_len;
432   struct sockaddr_storage addr;
433
434 #if DEBUG_UDP
435   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _("handling pong\n"));
436 #endif
437   found = find_session (plugin, plugin->sessions, sender);
438 #if DEBUG_UDP
439   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
440                    ("found->challenge %d, pong->challenge %d\n"),
441                    found->challenge, ntohs (pong->challenge));
442 #endif
443   if ((found != NULL) && (found->challenge == ntohs (pong->challenge)))
444     {
445       found->validated = GNUNET_YES;
446       addr_len = ntohs (pong->addrlen);
447 #if DEBUG_UDP
448       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
449                        ("found associated ping, addr is %u bytes\n"),
450                        addr_len);
451 #endif
452       memcpy (&addr, &pong[1], addr_len);
453       plugin->env->notify_validation (plugin->env->cls, "udp", sender,
454                                       ntohs (pong->challenge),
455                                       (char *) &addr);
456     }
457   else
458     {
459
460 #if DEBUG_UDP
461       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
462                        ("Session not found!\n"));
463 #endif
464     }
465   return;
466 }
467
468 static void
469 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
470 {
471   struct Plugin *plugin = cls;
472   struct GNUNET_TIME_Relative timeout =
473     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
474   char *buf;
475   struct UDPMessage *msg;
476   const struct GNUNET_MessageHeader *hdr;
477   struct GNUNET_PeerIdentity *sender;
478   unsigned int buflen;
479   socklen_t fromlen;
480   struct sockaddr_storage addr;
481   ssize_t ret;
482
483   do
484     {
485       buflen = GNUNET_NETWORK_socket_recvfrom_amount (udp_sock);
486
487 #if DEBUG_UDP
488       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
489                        ("we expect to read %u bytes\n"), buflen);
490 #endif
491
492       if (buflen == GNUNET_NO)
493         return;
494
495       buf = GNUNET_malloc (buflen);
496       fromlen = sizeof (addr);
497
498 #if DEBUG_UDP
499       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
500                        ("src_addr_len is %u\n"), fromlen);
501 #endif
502
503       memset (&addr, 0, fromlen);
504       ret =
505         GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
506                                         (struct sockaddr *) &addr, &fromlen);
507
508 #if DEBUG_UDP
509       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
510                        ("socket_recv returned %u, src_addr_len is %u\n"), ret,
511                        fromlen);
512 #endif
513
514       if (ret <= 0)
515         {
516           GNUNET_free (buf);
517           return;
518         }
519
520       msg = (struct UDPMessage *) buf;
521
522 #if DEBUG_UDP
523       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
524                        ("header reports message size of %d\n"),
525                        ntohs (msg->header.size));
526
527       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
528                        ("header reports message type of %d\n"),
529                        ntohs (msg->header.type));
530 #endif
531       /*if (ntohs(hdr->size) < sizeof(struct UDPMessage))
532          {
533          GNUNET_free(buf);
534          GNUNET_NETWORK_fdset_zero(plugin->rs);
535          GNUNET_NETWORK_fdset_set(plugin->rs, udp_sock);
536          break;
537          } */
538       hdr = (const struct GNUNET_MessageHeader *) &msg[1];
539       sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
540       memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
541
542 #if DEBUG_UDP
543       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
544                        ("msg reports message size of %d\n"),
545                        ntohs (hdr->size));
546
547       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
548                        ("msg reports message type of %d\n"),
549                        ntohs (hdr->type));
550 #endif
551
552       if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING)
553         {
554           handle_udp_ping (plugin, sender, &addr, fromlen, hdr);
555         }
556
557       if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG)
558         {
559           handle_udp_pong (plugin, sender, hdr);
560         }
561       GNUNET_free(sender);
562       GNUNET_free (buf);
563
564     }
565   while (GNUNET_NETWORK_socket_select (plugin->rs,
566                                        NULL,
567                                        NULL,
568                                        timeout) > 0
569          && GNUNET_NETWORK_fdset_isset (plugin->rs, udp_sock));
570
571   plugin->select_task =
572     GNUNET_SCHEDULER_add_select (plugin->env->sched,
573                                  GNUNET_SCHEDULER_PRIORITY_DEFAULT,
574                                  GNUNET_SCHEDULER_NO_TASK,
575                                  GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
576                                  NULL, &udp_plugin_select, plugin);
577
578 }
579
580 /**
581  * Create a UDP socket.  If possible, use IPv6, otherwise
582  * try IPv4.
583  */
584 static struct GNUNET_NETWORK_Handle *
585 udp_transport_server_start (void *cls)
586 {
587   struct Plugin *plugin = cls;
588   struct GNUNET_NETWORK_Handle *desc;
589   struct sockaddr_in serverAddrv4;
590   struct sockaddr_in6 serverAddrv6;
591   struct sockaddr *serverAddr;
592   socklen_t addrlen;
593
594   desc = NULL;
595   if (GNUNET_YES !=
596       GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD",
597                                             "DISABLE-IPV6"))
598     {
599       desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
600       if (desc != NULL)
601         {
602           memset (&serverAddrv6, 0, sizeof (serverAddrv6));
603 #if HAVE_SOCKADDR_IN_SIN_LEN
604           serverAddrv6.sin6_len = sizeof (serverAddrv6);
605 #endif
606           serverAddrv6.sin6_family = AF_INET6;
607           serverAddrv6.sin6_addr = in6addr_any;
608           serverAddrv6.sin6_port = htons (plugin->open_port);
609           addrlen = sizeof (serverAddrv6);
610           serverAddr = (struct sockaddr *) &serverAddrv6;
611         }
612     }
613   if (NULL == desc)
614     {
615       desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
616       if (NULL == desc)
617         {
618           GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "socket");
619           return NULL;
620         }
621       else
622         {
623           memset (&serverAddrv4, 0, sizeof (serverAddrv4));
624 #if HAVE_SOCKADDR_IN_SIN_LEN
625           serverAddrv4.sin_len = sizeof (serverAddrv4);
626 #endif
627           serverAddrv4.sin_family = AF_INET;
628           serverAddrv4.sin_addr.s_addr = INADDR_ANY;
629           serverAddrv4.sin_port = htons (plugin->open_port);
630           addrlen = sizeof (serverAddrv4);
631           serverAddr = (struct sockaddr *) &serverAddrv4;
632         }
633     }
634
635   if (desc != NULL)
636     {
637       GNUNET_assert (GNUNET_NETWORK_socket_bind (desc, serverAddr, addrlen) ==
638                      GNUNET_OK);
639     }
640
641   plugin->rs = GNUNET_NETWORK_fdset_create ();
642
643   GNUNET_NETWORK_fdset_zero (plugin->rs);
644   GNUNET_NETWORK_fdset_set (plugin->rs, desc);
645
646   plugin->select_task =
647     GNUNET_SCHEDULER_add_select (plugin->env->sched,
648                                  GNUNET_SCHEDULER_PRIORITY_DEFAULT,
649                                  GNUNET_SCHEDULER_NO_TASK,
650                                  GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
651                                  NULL, &udp_plugin_select, plugin);
652
653   return desc;
654 }
655
656 /**
657  * Function that can be used by the transport service to validate that
658  * another peer is reachable at a particular address (even if we
659  * already have a connection to this peer, this function is required
660  * to establish a new one).
661  *
662  * @param cls closure
663  * @param target who should receive this message
664  * @param challenge challenge code to use
665  * @param addrlen length of the address
666  * @param addr the address
667  * @param timeout how long should we try to transmit these?
668  * @return GNUNET_OK if the transmission has been scheduled
669  */
670 static int
671 udp_plugin_validate (void *cls,
672                      const struct GNUNET_PeerIdentity *target,
673                      uint32_t challenge,
674                      struct GNUNET_TIME_Relative timeout,
675                      const void *addr, size_t addrlen)
676 {
677   struct Plugin *plugin = cls;
678   struct Session *new_session;
679   struct UDPPongMessage *msg;
680
681   if (addrlen <= 0)
682     return GNUNET_SYSERR;
683
684   new_session = GNUNET_malloc (sizeof (struct Session));
685   new_session->connect_addr = GNUNET_malloc (addrlen);
686   memcpy (new_session->connect_addr, addr, addrlen);
687   new_session->connect_alen = addrlen;
688 #if DEBUG_UDP
689   if (memcmp
690       (target, plugin->env->my_identity,
691        sizeof (struct GNUNET_PeerIdentity)) == 0)
692     {
693       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
694                        ("definitely adding self to session list... hmmm\n"));
695     }
696 #endif
697   memcpy (&new_session->target, target, sizeof (struct GNUNET_PeerIdentity));
698   new_session->challenge = challenge;
699   new_session->validated = GNUNET_NO;
700   new_session->next = plugin->sessions;
701   plugin->sessions = new_session;
702
703   msg = GNUNET_malloc (sizeof (struct UDPPongMessage));
704   msg->header.size = htons (sizeof (struct UDPPongMessage));
705   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING);
706   msg->challenge = htons (challenge);
707 #if DEBUG_UDP
708   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
709                    ("In validate, header size is %d, type %d, challenge %u\n"),
710                    ntohs (msg->header.size), ntohs (msg->header.type),
711                    ntohl (msg->challenge));
712 #endif
713   udp_plugin_send (plugin, target, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
714                    &msg->header, timeout, NULL, NULL);
715
716   GNUNET_free(msg);
717   return GNUNET_OK;
718 }
719
720 /**
721  * Convert the transports address to a nice, human-readable
722  * format.
723  *
724  * @param cls closure
725  * @param type name of the transport that generated the address
726  * @param addr one of the addresses of the host, NULL for the last address
727  *        the specific address format depends on the transport
728  * @param addrlen length of the address
729  * @param numeric should (IP) addresses be displayed in numeric form?
730  * @param timeout after how long should we give up?
731  * @param asc function to call on each string
732  * @param asc_cls closure for asc
733  */
734 static void
735 udp_plugin_address_pretty_printer (void *cls,
736                                    const char *type,
737                                    const void *addr,
738                                    size_t addrlen,
739                                    int numeric,
740                                    struct GNUNET_TIME_Relative timeout,
741                                    GNUNET_TRANSPORT_AddressStringCallback asc,
742                                    void *asc_cls)
743 {
744
745 }
746
747 /**
748  * Set a quota for receiving data from the given peer; this is a
749  * per-transport limit.  The transport should limit its read/select
750  * calls to stay below the quota (in terms of incoming data).
751  *
752  * @param cls closure
753  * @param target the peer for whom the quota is given
754  * @param quota_in quota for receiving/sending data in bytes per ms
755  */
756 static void
757 udp_plugin_set_receive_quota (void *cls,
758                               const struct GNUNET_PeerIdentity *target,
759                               uint32_t quota_in)
760 {
761
762 }
763
764 /**
765  * Another peer has suggested an address for this
766  * peer and transport plugin.  Check that this could be a valid
767  * address.  If so, consider adding it to the list
768  * of addresses.
769  *
770  * @param cls closure
771  * @param addr pointer to the address
772  * @param addrlen length of addr
773  * @return GNUNET_OK if this is a plausible address for this peer
774  *         and transport
775  */
776 static int
777 udp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
778 {
779
780   return GNUNET_SYSERR;
781 }
782
783
784 /**
785  * The exported method. Makes the core api available via a global and
786  * returns the udp transport API.
787  */
788 void *
789 libgnunet_plugin_transport_udp_init (void *cls)
790 {
791   unsigned long long mtu;
792
793   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
794   struct GNUNET_TRANSPORT_PluginFunctions *api;
795   struct Plugin *plugin;
796   struct GNUNET_SERVICE_Context *service;
797   unsigned long long aport;
798   unsigned long long bport;
799
800   service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
801   if (service == NULL)
802     {
803       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "udp", _
804                        ("Failed to start service for `%s' transport plugin.\n"),
805                        "udp");
806       return NULL;
807     }
808   aport = 0;
809   if ((GNUNET_OK !=
810        GNUNET_CONFIGURATION_get_value_number (env->cfg,
811                                               "transport-udp",
812                                               "PORT",
813                                               &bport)) ||
814       (bport > 65535) ||
815       ((GNUNET_OK ==
816         GNUNET_CONFIGURATION_get_value_number (env->cfg,
817                                                "transport-udp",
818                                                "ADVERTISED-PORT",
819                                                &aport)) && (aport > 65535)))
820     {
821       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
822                        "udp",
823                        _
824                        ("Require valid port number for service `%s' in configuration!\n"),
825                        "transport-udp");
826       GNUNET_SERVICE_stop (service);
827       return NULL;
828     }
829   if (aport == 0)
830     aport = bport;
831
832   mtu = 1240;
833   if (mtu < 1200)
834     GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
835                      "udp",
836                      _("MTU %llu for `%s' is probably too low!\n"), mtu,
837                      "UDP");
838
839   plugin = GNUNET_malloc (sizeof (struct Plugin));
840   plugin->open_port = bport;
841   plugin->adv_port = aport;
842   plugin->env = env;
843   plugin->statistics = NULL;
844   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
845   plugin->sessions = NULL;
846   api->cls = plugin;
847
848   api->validate = &udp_plugin_validate;
849   api->send = &udp_plugin_send;
850   api->disconnect = &udp_disconnect;
851   api->address_pretty_printer = &udp_plugin_address_pretty_printer;
852   api->set_receive_quota = &udp_plugin_set_receive_quota;
853   api->address_suggested = &udp_plugin_address_suggested;
854   api->cost_estimate = 17;      /* TODO: ATS */
855   plugin->service = service;
856
857   udp_sock = udp_transport_server_start (plugin);
858
859   GNUNET_assert (udp_sock != NULL);
860
861   return api;
862 }
863
864 void *
865 libgnunet_plugin_transport_udp_done (void *cls)
866 {
867   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
868   struct Plugin *plugin = api->cls;
869   struct Session *pos;
870   struct Session *oldpos;
871
872   udp_transport_server_stop (plugin);
873   if (NULL != hostname_dns)
874     {
875       GNUNET_RESOLVER_request_cancel (hostname_dns);
876       hostname_dns = NULL;
877     }
878   GNUNET_SERVICE_stop (plugin->service);
879
880   pos = plugin->sessions;
881   while (pos != NULL)
882     {
883       GNUNET_free(pos->connect_addr);
884       oldpos = pos;
885       pos = pos->next;
886       GNUNET_free(oldpos);
887     }
888
889   GNUNET_NETWORK_fdset_destroy(plugin->rs);
890   GNUNET_free (plugin);
891   GNUNET_free (api);
892   return NULL;
893 }
894
895 /* end of plugin_transport_udp.c */