2 This file is part of GNUnet
3 (C) 2001, 2002, 2003, 2004, 2005, 2008 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transports/udp.c
23 * @brief Implementation of the UDP transport service
24 * @author Christian Grothoff
28 #include "gnunet_util.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_transport.h"
31 #include "gnunet_stats_service.h"
32 #include "gnunet_upnp_service.h"
35 #define DEBUG_UDP GNUNET_YES
38 * The default maximum size of each outbound UDP message,
39 * optimal value for Ethernet (10 or 100 MBit).
41 #define MESSAGE_SIZE 1472
44 * Message-Packet header.
49 * size of the message, in bytes, including this header.
51 GNUNET_MessageHeader header;
54 * What is the identity of the sender (GNUNET_hash of public key)
56 GNUNET_PeerIdentity sender;
60 #define MY_TRANSPORT_NAME "UDP"
63 /* *********** globals ************* */
65 static int stat_bytesReceived;
67 static int stat_bytesSent;
69 static int stat_bytesDropped;
71 static int stat_udpConnected;
74 * thread that listens for inbound messages
76 static struct GNUNET_SelectHandle *selector;
79 * the socket that we transmit all data with
81 static struct GNUNET_SocketHandle *udp_sock;
83 static struct GNUNET_LoadMonitor *load_monitor;
87 * The socket of session has data waiting, process!
89 * This function may only be called if the tcplock is
90 * already held by the caller.
93 select_message_handler (void *mh_cls,
94 struct GNUNET_SelectHandle *sh,
95 struct GNUNET_SocketHandle *sock,
96 void *sock_ctx, const GNUNET_MessageHeader * msg)
99 GNUNET_TransportPacket *mp;
100 const UDPMessage *um;
102 len = ntohs (msg->size);
103 if (len <= sizeof (UDPMessage))
105 GNUNET_GE_LOG (coreAPI->ectx,
106 GNUNET_GE_WARNING | GNUNET_GE_USER | GNUNET_GE_BULK,
107 _("Received malformed message via %s. Ignored.\n"),
109 return GNUNET_SYSERR;
111 um = (const UDPMessage *) msg;
112 mp = GNUNET_malloc (sizeof (GNUNET_TransportPacket));
113 mp->msg = GNUNET_malloc (len - sizeof (UDPMessage));
114 memcpy (mp->msg, &um[1], len - sizeof (UDPMessage));
115 mp->sender = um->sender;
116 mp->size = len - sizeof (UDPMessage);
118 coreAPI->receive (mp);
120 stats->change (stat_bytesReceived, len);
125 select_accept_handler (void *ah_cls,
126 struct GNUNET_SelectHandle *sh,
127 struct GNUNET_SocketHandle *sock,
128 const void *addr, unsigned int addr_len)
130 static int nonnullpointer;
132 if (GNUNET_NO != is_rejected_tester (addr, addr_len))
134 return &nonnullpointer;
138 * Select has been forced to close a connection.
139 * Free the associated context.
142 select_close_handler (void *ch_cls,
143 struct GNUNET_SelectHandle *sh,
144 struct GNUNET_SocketHandle *sock, void *sock_ctx)
150 * Establish a connection to a remote node.
152 * @param hello the hello-Message for the target node
153 * @param tsessionPtr the session handle that is to be set
154 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
157 udp_connect (const GNUNET_MessageHello * hello,
158 GNUNET_TSession ** tsessionPtr, int may_reuse)
160 GNUNET_TSession *tsession;
162 tsession = GNUNET_malloc (sizeof (GNUNET_TSession));
163 memset (tsession, 0, sizeof (GNUNET_TSession));
164 tsession->internal = GNUNET_malloc (GNUNET_sizeof_hello (hello));
165 memcpy (tsession->internal, hello, GNUNET_sizeof_hello (hello));
166 tsession->ttype = myAPI.protocol_number;
167 tsession->peer = hello->senderIdentity;
168 *tsessionPtr = tsession;
170 stats->change (stat_udpConnected, 1);
175 * A (core) Session is to be associated with a transport session. The
176 * transport service may want to know in order to call back on the
177 * core if the connection is being closed.
179 * @param tsession the session handle passed along
180 * from the call to receive that was made by the transport
182 * @return GNUNET_OK if the session could be associated,
183 * GNUNET_SYSERR if not.
186 udp_associate (GNUNET_TSession * tsession)
188 return GNUNET_SYSERR; /* UDP connections can never be associated */
192 * Disconnect from a remote node.
194 * @param tsession the session that is closed
195 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
198 udp_disconnect (GNUNET_TSession * tsession)
200 if (tsession != NULL)
202 if (tsession->internal != NULL)
203 GNUNET_free (tsession->internal);
204 GNUNET_free (tsession);
206 stats->change (stat_udpConnected, -1);
212 * Shutdown the server process (stop receiving inbound traffic). Maybe
216 udp_transport_server_stop ()
218 GNUNET_GE_ASSERT (coreAPI->ectx, udp_sock != NULL);
219 if (selector != NULL)
221 GNUNET_select_destroy (selector);
224 GNUNET_socket_destroy (udp_sock);
230 * Test if the transport would even try to send
231 * a message of the given size and importance
232 * for the given session.<br>
233 * This function is used to check if the core should
234 * even bother to construct (and encrypt) this kind
237 * @return GNUNET_YES if the transport would try (i.e. queue
238 * the message or call the OS to send),
239 * GNUNET_NO if the transport would just drop the message,
240 * GNUNET_SYSERR if the size/session is invalid
243 udp_test_would_try (GNUNET_TSession * tsession, unsigned int size,
246 const GNUNET_MessageHello *hello;
248 if (udp_sock == NULL)
249 return GNUNET_SYSERR;
252 GNUNET_GE_BREAK (coreAPI->ectx, 0);
253 return GNUNET_SYSERR;
255 if (size > myAPI.mtu)
257 GNUNET_GE_BREAK (coreAPI->ectx, 0);
258 return GNUNET_SYSERR;
260 hello = (const GNUNET_MessageHello *) tsession->internal;
262 return GNUNET_SYSERR;
267 * Create a UDP socket. If possible, use IPv6, otherwise
268 * try IPv4. Update available_protocols accordingly.
270 static struct GNUNET_NETWORK_Handle *
273 struct GNUNET_NETWORK_Handle *desc;
275 available_protocols = VERSION_AVAILABLE_NONE;
278 GNUNET_GC_get_configuration_value_yesno (cfg, "GNUNETD", "DISABLE-IPV6",
281 desc = GNUNET_net_socket (PF_INET6, SOCK_DGRAM, 17);
285 desc = GNUNET_net_socket (PF_INET, SOCK_DGRAM, 17);
288 GNUNET_GE_LOG_STRERROR (coreAPI->ectx,
289 GNUNET_GE_ERROR | GNUNET_GE_ADMIN |
290 GNUNET_GE_BULK, "socket");
291 return GNUNET_SYSERR;
293 available_protocols = VERSION_AVAILABLE_IPV4;
297 available_protocols = VERSION_AVAILABLE_IPV6 | VERSION_AVAILABLE_IPV4;
303 * Send a message to the specified remote node.
305 * @param tsession the GNUNET_MessageHello identifying the remote node
306 * @param message what to send
307 * @param size the size of the message
308 * @return GNUNET_SYSERR on error, GNUNET_OK on success
311 udp_send (GNUNET_TSession * tsession,
312 const void *message, const unsigned int size, int important)
314 const GNUNET_MessageHello *hello;
315 const HostAddress *haddr;
317 struct sockaddr_in serverAddrv4;
318 struct sockaddr_in6 serverAddrv6;
319 struct sockaddr *serverAddr;
321 unsigned short available;
326 GNUNET_GE_ASSERT (NULL, tsession != NULL);
327 if (udp_sock == NULL)
328 return GNUNET_SYSERR;
331 GNUNET_GE_BREAK (coreAPI->ectx, 0);
332 return GNUNET_SYSERR;
334 if (size > myAPI.mtu)
336 GNUNET_GE_BREAK (coreAPI->ectx, 0);
337 return GNUNET_SYSERR;
339 hello = (const GNUNET_MessageHello *) tsession->internal;
341 return GNUNET_SYSERR;
343 haddr = (const HostAddress *) &hello[1];
344 available = ntohs (haddr->availability) & available_protocols;
345 if (available == VERSION_AVAILABLE_NONE)
346 return GNUNET_SYSERR;
347 if (available == (VERSION_AVAILABLE_IPV4 | VERSION_AVAILABLE_IPV6))
349 if (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 2) == 0)
350 available = VERSION_AVAILABLE_IPV4;
352 available = VERSION_AVAILABLE_IPV6;
354 ssize = size + sizeof (UDPMessage);
355 mp = GNUNET_malloc (ssize);
356 mp->header.size = htons (ssize);
358 mp->sender = *(coreAPI->my_identity);
359 memcpy (&mp[1], message, size);
362 if ((available & VERSION_AVAILABLE_IPV4) > 0)
364 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
365 serverAddrv4.sin_family = AF_INET;
366 serverAddrv4.sin_port = haddr->port;
367 memcpy (&serverAddrv4.sin_addr, &haddr->ipv4, sizeof (struct in_addr));
368 addrlen = sizeof (serverAddrv4);
369 serverAddr = (struct sockaddr *) &serverAddrv4;
373 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
374 serverAddrv6.sin6_family = AF_INET;
375 serverAddrv6.sin6_port = haddr->port;
376 memcpy (&serverAddrv6.sin6_addr, &haddr->ipv6,
377 sizeof (struct in6_addr));
378 addrlen = sizeof (serverAddrv6);
379 serverAddr = (struct sockaddr *) &serverAddrv6;
382 if (GNUNET_YES == GNUNET_socket_send_to (udp_sock,
383 GNUNET_NC_NONBLOCKING,
386 (const char *) serverAddr,
390 win_ols_sendto (udp_sock, mp, ssize, (const char *) serverAddr, addrlen);
391 if (sent != SOCKET_ERROR)
396 stats->change (stat_bytesSent, sent);
401 stats->change (stat_bytesDropped, ssize);
408 * Start the server process to receive inbound traffic.
410 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
413 udp_transport_server_start ()
415 struct sockaddr_in serverAddrv4;
416 struct sockaddr_in6 serverAddrv6;
417 struct sockaddr *serverAddr;
419 GNUNET_NETWORK_Handle *desc;
423 GNUNET_GE_ASSERT (coreAPI->ectx, selector == NULL);
424 /* initialize UDP network */
428 desc = udp_create_socket ();
430 return GNUNET_SYSERR;
431 if (GNUNET_net_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0)
433 GNUNET_GE_DIE_STRERROR (coreAPI->ectx,
434 GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
435 GNUNET_GE_IMMEDIATE, "setsockopt");
436 return GNUNET_SYSERR;
438 if (available_protocols == VERSION_AVAILABLE_IPV4)
440 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
441 serverAddrv4.sin_family = AF_INET;
442 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
443 serverAddrv4.sin_port = htons (port);
444 addrlen = sizeof (serverAddrv4);
445 serverAddr = (struct sockaddr *) &serverAddrv4;
449 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
450 serverAddrv6.sin6_family = AF_INET6;
451 serverAddrv6.sin6_addr = in6addr_any;
452 serverAddrv6.sin6_port = htons (port);
453 addrlen = sizeof (serverAddrv6);
454 serverAddr = (struct sockaddr *) &serverAddrv6;
456 if (GNUNET_net_bind (desc, serverAddr, addrlen) < 0)
458 GNUNET_GE_LOG_STRERROR (coreAPI->ectx,
459 GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
460 GNUNET_GE_IMMEDIATE, "bind");
461 GNUNET_GE_LOG (coreAPI->ectx,
462 GNUNET_GE_FATAL | GNUNET_GE_ADMIN |
464 _("Failed to bind to %s port %d.\n"),
465 MY_TRANSPORT_NAME, port);
466 if (0 != GNUNET_net_close (&desc))
467 GNUNET_GE_LOG_STRERROR (coreAPI->ectx,
468 GNUNET_GE_ERROR | GNUNET_GE_USER |
469 GNUNET_GE_ADMIN | GNUNET_GE_BULK,
471 return GNUNET_SYSERR;
473 selector = GNUNET_select_create ("udp", GNUNET_YES, coreAPI->ectx, load_monitor, desc, addrlen, 0, /* timeout */
474 &select_message_handler,
476 &select_accept_handler,
478 &select_close_handler,
480 16 /* max sockets */ );
481 if (selector == NULL)
482 return GNUNET_SYSERR;
484 desc = udp_create_socket ();
487 GNUNET_GE_LOG_STRERROR (coreAPI->ectx,
488 GNUNET_GE_ERROR | GNUNET_GE_ADMIN |
489 GNUNET_GE_BULK, "socket");
490 GNUNET_select_destroy (selector);
492 return GNUNET_SYSERR;
494 udp_sock = GNUNET_socket_create (coreAPI->ectx, load_monitor, desc);
495 GNUNET_GE_ASSERT (coreAPI->ectx, udp_sock != NULL);
500 * The exported method. Makes the core api available via a global and
501 * returns the udp transport API.
503 GNUNET_TransportAPI *
504 inittransport_udp (GNUNET_CoreAPIForTransport * core)
506 unsigned long long mtu;
509 load_monitor = core->load_monitor;
510 GNUNET_GE_ASSERT (coreAPI->ectx, sizeof (UDPMessage) == 68);
511 GNUNET_GE_ASSERT (coreAPI->ectx, sizeof (HostAddress) == 24);
513 if (-1 == GNUNET_GC_get_configuration_value_number (cfg,
518 GNUNET_P2P_MESSAGE_OVERHEAD
521 (GNUNET_MessageHeader) +
528 GNUNET_GE_LOG (coreAPI->ectx,
529 GNUNET_GE_ERROR | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE,
530 _("MTU %llu for `%s' is probably too low!\n"), mtu, "UDP");
531 lock = GNUNET_mutex_create (GNUNET_NO);
533 GNUNET_GC_attach_change_listener (cfg, &reload_configuration, NULL))
535 GNUNET_mutex_destroy (lock);
539 if (GNUNET_GC_get_configuration_value_yesno (cfg, "UDP", "UPNP", GNUNET_YES)
542 upnp = coreAPI->service_request ("upnp");
544 GNUNET_GE_LOG (coreAPI->ectx,
545 GNUNET_GE_ERROR | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE,
546 "The UPnP service could not be loaded. To disable UPnP, set the "
547 "configuration option \"UPNP\" in section \"%s\" to \"NO\"\n",
550 stats = coreAPI->service_request ("stats");
554 = stats->create (gettext_noop ("# bytes received via UDP"));
555 stat_bytesSent = stats->create (gettext_noop ("# bytes sent via UDP"));
557 = stats->create (gettext_noop ("# bytes dropped by UDP (outgoing)"));
559 = stats->create (gettext_noop ("# UDP connections (right now)"));
561 myAPI.protocol_number = GNUNET_TRANSPORT_PROTOCOL_NUMBER_UDP;
562 myAPI.mtu = mtu - sizeof (UDPMessage);
564 myAPI.hello_verify = &verify_hello;
565 myAPI.hello_create = &create_hello;
566 myAPI.connect = &udp_connect;
567 myAPI.send = &udp_send;
568 myAPI.associate = &udp_associate;
569 myAPI.disconnect = &udp_disconnect;
570 myAPI.server_start = &udp_transport_server_start;
571 myAPI.server_stop = &udp_transport_server_stop;
572 myAPI.hello_to_address = &hello_to_address;
573 myAPI.send_now_test = &udp_test_would_try;