remove comments
[oweals/gnunet.git] / src / transport / plugin_transport_udp.c
1 /*
2      This file is part of GNUnet
3      (C) 2001, 2002, 2003, 2004, 2005, 2008 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  * 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,
258                 const struct GNUNET_PeerIdentity *
259                 target)
260 {
261   return;
262 }
263
264 /**
265  * Shutdown the server process (stop receiving inbound traffic). Maybe
266  * restarted later!
267  */
268 static int
269 udp_transport_server_stop (void *cls)
270 {
271   struct Plugin *plugin = cls;
272   GNUNET_assert (udp_sock != NULL);
273   if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
274     {
275       GNUNET_SCHEDULER_cancel (plugin->env->sched, plugin->select_task);
276       plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
277     }
278
279   GNUNET_NETWORK_socket_close (udp_sock);
280   udp_sock = NULL;
281   return GNUNET_OK;
282 }
283
284 static struct Session *
285 find_session(void *cls, struct Session * session_list, const struct GNUNET_PeerIdentity *peer)
286 {
287   struct Plugin *plugin = cls;
288   struct Session *pos;
289   pos = session_list;
290
291   while (pos != NULL)
292   {
293     if (memcmp(peer, &pos->target, sizeof(struct GNUNET_PeerIdentity)) == 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, sizeof(struct GNUNET_PeerIdentity));
348   memcpy (&message[1], msg, ntohs (msg->size));
349
350   /* Actually send the message */
351   sent = GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize, session->connect_addr,
352                                 session->connect_alen);
353
354   if (cont != NULL)
355     {
356       if (sent == GNUNET_SYSERR)
357         cont(cont_cls, target, GNUNET_SYSERR);
358       else
359         cont(cont_cls, target, GNUNET_OK);
360     }
361
362   return;
363 }
364
365 /**
366  * We've received a PING from this peer via UDP.
367  * Send back our PONG.
368  *
369  * @param cls closure
370  * @param sender the Identity of the sender
371  * @param message the actual message
372  */
373 static void
374 handle_udp_ping (void *cls,
375                  struct GNUNET_PeerIdentity *sender, struct sockaddr_storage * addr, size_t addrlen,
376                  const struct GNUNET_MessageHeader *message)
377 {
378   struct Plugin *plugin = cls;
379   struct Session *head = plugin->sessions;
380   const struct UDPPingMessage *ping = (const struct UDPPingMessage *)message;
381   struct UDPPongMessage *pong;
382   struct Session *found;
383
384 #if DEBUG_UDP
385       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
386             ("handling ping, challenge is %d\n"), ntohs(ping->challenge));
387 #endif
388   found = find_session(plugin, head, sender);
389   if (found != NULL)
390     {
391       pong = GNUNET_malloc(sizeof(struct UDPPongMessage) + addrlen);
392       pong->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG);
393       pong->header.size = htons(sizeof(struct UDPPongMessage) + addrlen);
394       pong->challenge = ping->challenge;
395       memcpy(&pong[1], addr, addrlen);
396       pong->addrlen = htons(addrlen);
397
398       udp_plugin_send(plugin, sender, GNUNET_SCHEDULER_PRIORITY_DEFAULT, &pong->header, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30), NULL, NULL);
399     }
400
401   return;
402
403 }
404
405 /**
406  * We've received a PONG from this peer via UDP.
407  * Great. Call validate func if we haven't already
408  * received a PONG.
409  *
410  * @param cls closure
411  * @param client identification of the client
412  * @param message the actual message
413  */
414 static void
415 handle_udp_pong (void *cls,
416                  struct GNUNET_PeerIdentity *sender,
417                  const struct GNUNET_MessageHeader *message)
418 {
419   struct Plugin *plugin = cls;
420   const struct UDPPongMessage *pong = (struct UDPPongMessage *)message;
421   struct Session *found;
422   unsigned int addr_len;
423   struct sockaddr_storage addr;
424
425 #if DEBUG_UDP
426       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
427             ("handling pong\n"));
428 #endif
429   found = find_session(plugin, plugin->sessions, sender);
430 #if DEBUG_UDP
431       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
432             ("found->challenge %d, pong->challenge %d\n"), found->challenge, ntohs(pong->challenge));
433 #endif
434   if ((found != NULL) && (found->challenge == ntohs(pong->challenge)))
435     {
436       found->validated = GNUNET_YES;
437       addr_len = ntohs(pong->addrlen);
438 #if DEBUG_UDP
439       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
440             ("found associated ping, addr is %u bytes\n"), addr_len);
441 #endif
442       memcpy(&addr, &pong[1], addr_len);
443       plugin->env->notify_validation(plugin->env->cls, "udp", sender, ntohs(pong->challenge), (char *)&addr);
444     }
445   else
446     {
447
448 #if DEBUG_UDP
449       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
450             ("Session not found!\n"));
451 #endif
452     }
453   return;
454 }
455
456 static void
457 udp_plugin_select (void *cls,
458                    const struct GNUNET_SCHEDULER_TaskContext *tc)
459 {
460   struct Plugin *plugin = cls;
461   struct GNUNET_TIME_Relative timeout  = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
462   char * buf;
463   struct UDPMessage *msg;
464   const struct GNUNET_MessageHeader *hdr;
465   struct GNUNET_PeerIdentity *sender;
466   unsigned int buflen;
467   socklen_t fromlen;
468   struct sockaddr_storage addr;
469   ssize_t ret;
470
471    do
472     {
473       buflen = GNUNET_NETWORK_socket_recvfrom_amount(udp_sock);
474
475 #if DEBUG_UDP
476       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
477             ("we expect to read %u bytes\n"), buflen);
478 #endif
479
480       if (buflen == GNUNET_NO)
481         return;
482
483       buf = GNUNET_malloc(buflen);
484       fromlen = sizeof(addr);
485
486 #if DEBUG_UDP
487       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
488             ("src_addr_len is %u\n"), fromlen);
489 #endif
490
491       memset(&addr, 0, fromlen);
492       ret = GNUNET_NETWORK_socket_recvfrom(udp_sock, buf, buflen, (struct sockaddr *)&addr, &fromlen);
493
494 #if DEBUG_UDP
495       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
496             ("socket_recv returned %u, src_addr_len is %u\n"), ret, fromlen);
497 #endif
498
499       if (ret <= 0)
500         {
501           GNUNET_free(buf);
502           return;
503         }
504
505       msg = (struct UDPMessage *)buf;
506
507 #if DEBUG_UDP
508       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
509             ("header reports message size of %d\n"), ntohs(msg->header.size));
510
511       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
512             ("header reports message type of %d\n"), ntohs(msg->header.type));
513 #endif
514       /*if (ntohs(hdr->size) < sizeof(struct UDPMessage))
515         {
516           GNUNET_free(buf);
517           GNUNET_NETWORK_fdset_zero(plugin->rs);
518           GNUNET_NETWORK_fdset_set(plugin->rs, udp_sock);
519           break;
520         }*/
521       hdr = (const struct GNUNET_MessageHeader *)&msg[1];
522       sender = GNUNET_malloc(sizeof(struct GNUNET_PeerIdentity));
523       memcpy(sender, &msg->sender, sizeof(struct GNUNET_PeerIdentity));
524
525 #if DEBUG_UDP
526       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
527             ("msg reports message size of %d\n"), ntohs(hdr->size));
528
529       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
530             ("msg reports message type of %d\n"), ntohs(hdr->type));
531 #endif
532
533       if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING)
534         {
535           handle_udp_ping(plugin, sender, &addr, fromlen, hdr);
536         }
537
538       if (ntohs(hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG)
539         {
540           handle_udp_pong(plugin, sender, hdr);
541         }
542
543       GNUNET_free(buf);
544
545     }
546     while (GNUNET_NETWORK_socket_select (plugin->rs,
547                                          NULL,
548                                          NULL,
549                                          timeout) > 0 && GNUNET_NETWORK_fdset_isset(plugin->rs, udp_sock));
550
551     plugin->select_task = GNUNET_SCHEDULER_add_select(plugin->env->sched, GNUNET_SCHEDULER_PRIORITY_DEFAULT, GNUNET_SCHEDULER_NO_TASK,
552       GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs, NULL, &udp_plugin_select, plugin);
553
554 }
555
556 /**
557  * Create a UDP socket.  If possible, use IPv6, otherwise
558  * try IPv4.
559  */
560 static struct GNUNET_NETWORK_Handle *
561 udp_transport_server_start (void *cls)
562 {
563   struct Plugin *plugin = cls;
564   struct GNUNET_NETWORK_Handle *desc;
565   struct sockaddr_in serverAddrv4;
566   struct sockaddr_in6 serverAddrv6;
567   struct sockaddr *serverAddr;
568   socklen_t addrlen;
569
570   desc = NULL;
571   if (GNUNET_YES !=
572       GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "GNUNETD", "DISABLE-IPV6"))
573     {
574       desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 17);
575       if (desc != NULL)
576         {
577           memset (&serverAddrv6, 0, sizeof (serverAddrv6));
578   #if HAVE_SOCKADDR_IN_SIN_LEN
579           serverAddrv6.sin6_len = sizeof (serverAddrv6);
580   #endif
581           serverAddrv6.sin6_family = AF_INET6;
582           serverAddrv6.sin6_addr = in6addr_any;
583           serverAddrv6.sin6_port = htons (plugin->open_port);
584           addrlen = sizeof (serverAddrv6);
585           serverAddr = (struct sockaddr *) &serverAddrv6;
586         }
587     }
588   if (NULL == desc)
589     {
590       desc = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 17);
591       if (NULL == desc)
592         {
593           GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG,
594                          "udp",
595                          "socket");
596           return NULL;
597         }
598       else
599         {
600           memset (&serverAddrv4, 0, sizeof (serverAddrv4));
601   #if HAVE_SOCKADDR_IN_SIN_LEN
602           serverAddrv4.sin_len = sizeof (serverAddrv4);
603   #endif
604           serverAddrv4.sin_family = AF_INET;
605           serverAddrv4.sin_addr.s_addr = INADDR_ANY;
606           serverAddrv4.sin_port = htons (plugin->open_port);
607           addrlen = sizeof (serverAddrv4);
608           serverAddr = (struct sockaddr *) &serverAddrv4;
609         }
610     }
611
612   if (desc != NULL)
613     {
614       GNUNET_assert(GNUNET_NETWORK_socket_bind(desc, serverAddr, addrlen) == GNUNET_OK);
615     }
616
617   plugin->rs = GNUNET_NETWORK_fdset_create ();
618
619   GNUNET_NETWORK_fdset_zero(plugin->rs);
620   GNUNET_NETWORK_fdset_set(plugin->rs, desc);
621
622   plugin->select_task = GNUNET_SCHEDULER_add_select(plugin->env->sched, GNUNET_SCHEDULER_PRIORITY_DEFAULT, GNUNET_SCHEDULER_NO_TASK,
623       GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs, NULL, &udp_plugin_select, plugin);
624
625   return desc;
626 }
627
628 /**
629  * Function that can be used by the transport service to validate that
630  * another peer is reachable at a particular address (even if we
631  * already have a connection to this peer, this function is required
632  * to establish a new one).
633  *
634  * @param cls closure
635  * @param target who should receive this message
636  * @param challenge challenge code to use
637  * @param addrlen length of the address
638  * @param addr the address
639  * @param timeout how long should we try to transmit these?
640  * @return GNUNET_OK if the transmission has been scheduled
641  */
642 static int
643 udp_plugin_validate (void *cls,
644                      const struct GNUNET_PeerIdentity *target,
645                      uint32_t challenge,
646                      struct GNUNET_TIME_Relative timeout,
647                      const void *addr, size_t addrlen)
648 {
649   struct Plugin *plugin = cls;
650   struct Session *new_session;
651   struct UDPPongMessage *msg;
652
653   if (addrlen <= 0)
654     return GNUNET_SYSERR;
655
656   new_session = GNUNET_malloc(sizeof(struct Session));
657   new_session->connect_addr = GNUNET_malloc(addrlen);
658   memcpy(new_session->connect_addr, addr, addrlen);
659   new_session->connect_alen = addrlen;
660 #if DEBUG_UDP
661   if (memcmp(target, plugin->env->my_identity, sizeof(struct GNUNET_PeerIdentity)) == 0)
662     {
663       GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
664               ("definitely adding self to session list... hmmm\n"));
665     }
666 #endif
667   memcpy(&new_session->target, target, sizeof(struct GNUNET_PeerIdentity));
668   new_session->challenge = challenge;
669   new_session->validated = GNUNET_NO;
670   new_session->next = plugin->sessions;
671   plugin->sessions = new_session;
672
673   msg = GNUNET_malloc (sizeof (struct UDPPongMessage));
674   msg->header.size = htons(sizeof(struct UDPPongMessage));
675   msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING);
676   msg->challenge = htons(challenge);
677 #if DEBUG_UDP
678   GNUNET_log_from(GNUNET_ERROR_TYPE_INFO, "udp", _
679                  ("In validate, header size is %d, type %d, challenge %u\n"), ntohs(msg->header.size), ntohs(msg->header.type), ntohl(msg->challenge));
680 #endif
681   udp_plugin_send(plugin, target, GNUNET_SCHEDULER_PRIORITY_DEFAULT, &msg->header, timeout, NULL, NULL);
682
683   return GNUNET_OK;
684 }
685
686 /**
687  * Convert the transports address to a nice, human-readable
688  * format.
689  *
690  * @param cls closure
691  * @param type name of the transport that generated the address
692  * @param addr one of the addresses of the host, NULL for the last address
693  *        the specific address format depends on the transport
694  * @param addrlen length of the address
695  * @param numeric should (IP) addresses be displayed in numeric form?
696  * @param timeout after how long should we give up?
697  * @param asc function to call on each string
698  * @param asc_cls closure for asc
699  */
700 static void
701 udp_plugin_address_pretty_printer (void *cls,
702                                    const char *type,
703                                    const void *addr,
704                                    size_t addrlen,
705                                    int numeric,
706                                    struct GNUNET_TIME_Relative timeout,
707                                    GNUNET_TRANSPORT_AddressStringCallback asc,
708                                    void *asc_cls)
709 {
710
711 }
712
713 /**
714  * Set a quota for receiving data from the given peer; this is a
715  * per-transport limit.  The transport should limit its read/select
716  * calls to stay below the quota (in terms of incoming data).
717  *
718  * @param cls closure
719  * @param target the peer for whom the quota is given
720  * @param quota_in quota for receiving/sending data in bytes per ms
721  */
722 static void
723 udp_plugin_set_receive_quota (void *cls,
724                               const struct GNUNET_PeerIdentity *target,
725                               uint32_t quota_in)
726 {
727
728 }
729
730 /**
731  * Another peer has suggested an address for this
732  * peer and transport plugin.  Check that this could be a valid
733  * address.  If so, consider adding it to the list
734  * of addresses.
735  *
736  * @param cls closure
737  * @param addr pointer to the address
738  * @param addrlen length of addr
739  * @return GNUNET_OK if this is a plausible address for this peer
740  *         and transport
741  */
742 static int
743 udp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
744 {
745
746   return GNUNET_SYSERR;
747 }
748
749
750 /**
751  * The exported method. Makes the core api available via a global and
752  * returns the udp transport API.
753  */
754 void *
755 libgnunet_plugin_transport_udp_init (void *cls)
756 {
757   unsigned long long mtu;
758
759   struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
760   struct GNUNET_TRANSPORT_PluginFunctions *api;
761   struct Plugin *plugin;
762   struct GNUNET_SERVICE_Context *service;
763   unsigned long long aport;
764   unsigned long long bport;
765
766   service = GNUNET_SERVICE_start ("transport-udp", env->sched, env->cfg);
767   if (service == NULL)
768     {
769       GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, "udp", _
770       ("Failed to start service for `%s' transport plugin.\n"), "udp");
771       return NULL;
772     }
773     aport = 0;
774     if ((GNUNET_OK !=
775          GNUNET_CONFIGURATION_get_value_number (env->cfg,
776                                                 "transport-udp",
777                                                 "PORT",
778                                                 &bport)) ||
779         (bport > 65535) ||
780         ((GNUNET_OK ==
781           GNUNET_CONFIGURATION_get_value_number (env->cfg,
782                                                  "transport-udp",
783                                                  "ADVERTISED-PORT",
784                                                  &aport)) && (aport > 65535)))
785       {
786         GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
787                          "udp",
788                          _
789                          ("Require valid port number for service `%s' in configuration!\n"),
790                          "transport-udp");
791         GNUNET_SERVICE_stop (service);
792         return NULL;
793       }
794     if (aport == 0)
795       aport = bport;
796
797   mtu = 1240;
798   if (mtu < 1200)
799     GNUNET_log_from(GNUNET_ERROR_TYPE_INFO,
800                     "udp",
801                     _("MTU %llu for `%s' is probably too low!\n"), mtu, "UDP");
802
803   plugin = GNUNET_malloc (sizeof (struct Plugin));
804   plugin->open_port = bport;
805   plugin->adv_port = aport;
806   plugin->env = env;
807   plugin->statistics = NULL;
808   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
809   plugin->sessions = NULL;
810   api->cls = plugin;
811
812   api->validate = &udp_plugin_validate;
813   api->send = &udp_plugin_send;
814   api->disconnect = &udp_disconnect;
815   api->address_pretty_printer = &udp_plugin_address_pretty_printer;
816   api->set_receive_quota = &udp_plugin_set_receive_quota;
817   api->address_suggested = &udp_plugin_address_suggested;
818   api->cost_estimate = 17;      /* TODO: ATS */
819   plugin->service = service;
820
821   udp_sock = udp_transport_server_start(plugin);
822
823   GNUNET_assert(udp_sock != NULL);
824
825   return api;
826 }
827
828 void *
829 libgnunet_plugin_transport_udp_done (void *cls)
830 {
831   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
832   struct Plugin *plugin = api->cls;
833
834   udp_transport_server_stop(plugin);
835   if (NULL != hostname_dns)
836     {
837       GNUNET_RESOLVER_request_cancel (hostname_dns);
838       hostname_dns = NULL;
839     }
840   GNUNET_SERVICE_stop (plugin->service);
841   GNUNET_free (plugin);
842   GNUNET_free (api);
843   return NULL;
844 }
845
846 /* end of plugin_transport_udp.c */