- changes
[oweals/gnunet.git] / src / transport / plugin_transport_udp_new_broadcasting.c
1 /*
2      This file is part of GNUnet
3      (C) 2010, 2011 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 3, 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_broadcasting.c
23  * @brief Neighbour discovery with UDP
24  * @author Christian Grothoff
25  * @author Matthias Wachs
26  */
27 #include "platform.h"
28 #include "plugin_transport_udp_new.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_fragmentation_lib.h"
32 #include "gnunet_nat_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_resolver_service.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_constants.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
41
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
43
44
45 struct UDP_Beacon_Message
46 {
47  /**
48   * Message header.
49   */
50   struct GNUNET_MessageHeader header;
51
52  /**
53   * What is the identity of the sender
54   */
55   struct GNUNET_PeerIdentity sender;
56 };
57
58
59 struct BroadcastAddress
60 {
61   struct BroadcastAddress *next;
62   struct BroadcastAddress *prev;
63
64   void *addr;
65   socklen_t addrlen;
66 };
67
68
69 struct Mstv4Context
70 {
71   struct Plugin *plugin;
72
73   struct IPv4UdpAddress addr;
74   /**
75    * ATS network type in NBO
76    */
77   uint32_t ats_address_network_type;
78 };
79
80 struct Mstv6Context
81 {
82   struct Plugin *plugin;
83
84   struct IPv6UdpAddress addr;
85   /**
86    * ATS network type in NBO
87    */
88   uint32_t ats_address_network_type;
89 };
90
91
92
93 void
94 broadcast_ipv6_mst_cb (void *cls, void *client,
95                        const struct GNUNET_MessageHeader *message)
96 {
97
98   struct Plugin *plugin = cls;
99   struct Mstv6Context *mc = client;
100   const struct GNUNET_MessageHeader *hello;
101   struct UDP_Beacon_Message *msg;
102
103   msg = (struct UDP_Beacon_Message *) message;
104
105   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
106       ntohs (msg->header.type))
107     return;
108
109   LOG (GNUNET_ERROR_TYPE_DEBUG,
110        "Received beacon with %u bytes from peer `%s' via address `%s'\n",
111        ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
112        udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
113
114   struct GNUNET_ATS_Information atsi[2];
115
116   /* setup ATS */
117   atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
118   atsi[0].value = htonl (1);
119   atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
120   atsi[1].value = mc->ats_address_network_type;
121   GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
122
123   hello = (struct GNUNET_MessageHeader *) &msg[1];
124   plugin->env->receive (plugin->env->cls, &msg->sender, hello,
125                         (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
126                         (const char *) &mc->addr, sizeof (mc->addr));
127
128   GNUNET_STATISTICS_update (plugin->env->stats,
129                             _
130                             ("# IPv6 multicast HELLO beacons received via udp"),
131                             1, GNUNET_NO);
132   GNUNET_free (mc);
133 }
134
135 void
136 broadcast_ipv4_mst_cb (void *cls, void *client,
137                        const struct GNUNET_MessageHeader *message)
138 {
139   struct Plugin *plugin = cls;
140   struct Mstv4Context *mc = client;
141   const struct GNUNET_MessageHeader *hello;
142   struct UDP_Beacon_Message *msg;
143
144   msg = (struct UDP_Beacon_Message *) message;
145
146   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
147       ntohs (msg->header.type))
148     return;
149
150   LOG (GNUNET_ERROR_TYPE_DEBUG,
151        "Received beacon with %u bytes from peer `%s' via address `%s'\n",
152        ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
153        udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
154
155   struct GNUNET_ATS_Information atsi[2];
156
157   /* setup ATS */
158   atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
159   atsi[0].value = htonl (1);
160   atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
161   atsi[1].value = mc->ats_address_network_type;
162   GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
163
164   hello = (struct GNUNET_MessageHeader *) &msg[1];
165   plugin->env->receive (plugin->env->cls, &msg->sender, hello,
166                         (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
167                         (const char *) &mc->addr, sizeof (mc->addr));
168
169   GNUNET_STATISTICS_update (plugin->env->stats,
170                             _
171                             ("# IPv4 broadcast HELLO beacons received via udp"),
172                             1, GNUNET_NO);
173   GNUNET_free (mc);
174 }
175
176 void
177 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
178 {
179   struct GNUNET_ATS_Information ats;
180
181   if (addrlen == sizeof (struct sockaddr_in))
182   {
183     LOG (GNUNET_ERROR_TYPE_DEBUG,
184          "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
185          size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
186
187     struct Mstv4Context *mc;
188
189     mc = GNUNET_malloc (sizeof (struct Mstv4Context));
190     struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
191
192     mc->addr.ipv4_addr = av4->sin_addr.s_addr;
193     mc->addr.u4_port = av4->sin_port;
194     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
195     mc->ats_address_network_type = ats.value;
196     if (GNUNET_OK !=
197         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
198                                    GNUNET_NO, GNUNET_NO))
199       GNUNET_free (mc);
200   }
201   else if (addrlen == sizeof (struct sockaddr_in6))
202   {
203     LOG (GNUNET_ERROR_TYPE_DEBUG,
204          "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
205          size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
206
207     struct Mstv6Context *mc;
208
209     mc = GNUNET_malloc (sizeof (struct Mstv6Context));
210     struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
211
212     mc->addr.ipv6_addr = av6->sin6_addr;
213     mc->addr.u6_port = av6->sin6_port;
214     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
215     mc->ats_address_network_type = ats.value;
216
217     if (GNUNET_OK !=
218         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
219                                    GNUNET_NO, GNUNET_NO))
220       GNUNET_free (mc);
221   }
222 }
223
224 static void
225 udp_ipv4_broadcast_send (void *cls,
226                          const struct GNUNET_SCHEDULER_TaskContext *tc)
227 {
228   struct Plugin *plugin = cls;
229   int sent;
230   uint16_t msg_size;
231   uint16_t hello_size;
232   char buf[65536];
233
234   const struct GNUNET_MessageHeader *hello;
235   struct UDP_Beacon_Message *msg;
236   struct BroadcastAddress *baddr;
237
238   plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
239
240   hello = plugin->env->get_our_hello ();
241   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
242   msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
243
244   if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
245       (msg_size > (UDP_MTU)))
246     return;
247
248   msg = (struct UDP_Beacon_Message *) buf;
249   msg->sender = *(plugin->env->my_identity);
250   msg->header.size = ntohs (msg_size);
251   msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
252   memcpy (&msg[1], hello, hello_size);
253   sent = 0;
254
255   baddr = plugin->ipv4_broadcast_head;
256   /* just IPv4 */
257   while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
258   {
259     struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
260
261     addr->sin_port = htons (plugin->port);
262
263     sent =
264         GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, msg_size,
265                                       (const struct sockaddr *) addr,
266                                       baddr->addrlen);
267     if (sent == GNUNET_SYSERR)
268       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
269     else
270       LOG (GNUNET_ERROR_TYPE_DEBUG,
271            "Sent HELLO beacon broadcast with  %i bytes to address %s\n", sent,
272            GNUNET_a2s (baddr->addr, baddr->addrlen));
273     baddr = baddr->next;
274   }
275
276   plugin->send_ipv4_broadcast_task =
277       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
278                                     &udp_ipv4_broadcast_send, plugin);
279 }
280
281 static void
282 udp_ipv6_broadcast_send (void *cls,
283                          const struct GNUNET_SCHEDULER_TaskContext *tc)
284 {
285   struct Plugin *plugin = cls;
286   int sent;
287   uint16_t msg_size;
288   uint16_t hello_size;
289   char buf[65536];
290
291   const struct GNUNET_MessageHeader *hello;
292   struct UDP_Beacon_Message *msg;
293
294   plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
295
296   hello = plugin->env->get_our_hello ();
297   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
298   msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
299
300   if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
301       (msg_size > (UDP_MTU)))
302     return;
303
304   msg = (struct UDP_Beacon_Message *) buf;
305   msg->sender = *(plugin->env->my_identity);
306   msg->header.size = ntohs (msg_size);
307   msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
308   memcpy (&msg[1], hello, hello_size);
309   sent = 0;
310
311   sent =
312       GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, msg_size,
313                                     (const struct sockaddr *)
314                                     &plugin->ipv6_multicast_address,
315                                     sizeof (struct sockaddr_in6));
316   if (sent == GNUNET_SYSERR)
317     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
318   else
319     LOG (GNUNET_ERROR_TYPE_DEBUG,
320          "Sending IPv6 HELLO beacon broadcast with  %i bytes to address %s\n",
321          sent,
322          GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
323                      sizeof (struct sockaddr_in6)));
324
325
326
327   plugin->send_ipv6_broadcast_task =
328       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
329                                     &udp_ipv6_broadcast_send, plugin);
330 }
331
332
333 static int
334 iface_proc (void *cls, const char *name, int isDefault,
335             const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
336             const struct sockaddr *netmask, socklen_t addrlen)
337 {
338   struct Plugin *plugin = cls;
339
340   if (addr != NULL)
341   {
342     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
343                 GNUNET_a2s (addr, addrlen), name, addr);
344     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345                 "broadcast address %s for interface %s %p\n ",
346                 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
347     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
348                 GNUNET_a2s (netmask, addrlen), name, netmask);
349
350
351     /* Collecting broadcast addresses */
352     if (broadcast_addr != NULL)
353     {
354       struct BroadcastAddress *ba =
355           GNUNET_malloc (sizeof (struct BroadcastAddress));
356       ba->addr = GNUNET_malloc (addrlen);
357       memcpy (ba->addr, broadcast_addr, addrlen);
358       ba->addrlen = addrlen;
359       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
360                                    plugin->ipv4_broadcast_tail, ba);
361     }
362   }
363   return GNUNET_OK;
364 }
365
366
367 void
368 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
369 {
370   /* create IPv4 broadcast socket */
371   plugin->broadcast_ipv4 = GNUNET_NO;
372   if (plugin->sockv4 != NULL)
373   {
374     int yes = 1;
375
376     if (GNUNET_NETWORK_socket_setsockopt
377         (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
378          sizeof (int)) != GNUNET_OK)
379     {
380       LOG (GNUNET_ERROR_TYPE_WARNING,
381            _
382            ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
383            ntohs (serverAddrv4->sin_port));
384     }
385     else
386     {
387       GNUNET_OS_network_interfaces_list (iface_proc, plugin);
388       plugin->send_ipv4_broadcast_task =
389           GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
390
391       plugin->broadcast_ipv4_mst =
392           GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
393
394       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
395       plugin->broadcast_ipv4 = GNUNET_YES;
396     }
397   }
398
399   plugin->broadcast_ipv6 = GNUNET_NO;
400   if (plugin->sockv6 != NULL)
401   {
402     memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
403     GNUNET_assert (1 ==
404                    inet_pton (AF_INET6, "FF05::13B",
405                               &plugin->ipv6_multicast_address.sin6_addr));
406
407     plugin->ipv6_multicast_address.sin6_family = AF_INET6;
408     plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
409
410     plugin->broadcast_ipv6_mst =
411         GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
412
413     /* Create IPv6 multicast request */
414     struct ipv6_mreq multicastRequest;
415
416     multicastRequest.ipv6mr_multiaddr =
417         plugin->ipv6_multicast_address.sin6_addr;
418     /* TODO: 0 selects the "best" interface, tweak to use all interfaces
419      *
420      * http://tools.ietf.org/html/rfc2553#section-5.2:
421      *
422      * IPV6_JOIN_GROUP
423      *
424      * Join a multicast group on a specified local interface.  If the
425      * interface index is specified as 0, the kernel chooses the local
426      * interface.  For example, some kernels look up the multicast
427      * group in the normal IPv6 routing table and using the resulting
428      * interface.
429      * */
430     multicastRequest.ipv6mr_interface = 0;
431
432     /* Join the multicast group */
433     if (GNUNET_NETWORK_socket_setsockopt
434         (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
435          (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
436     {
437       LOG (GNUNET_ERROR_TYPE_WARNING,
438       "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
439     }
440     else
441     {
442 #if DEBUG_UDP
443       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
444 #endif
445       plugin->send_ipv6_broadcast_task =
446           GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
447       plugin->broadcast_ipv6 = GNUNET_YES;
448     }
449   }
450 }
451
452 void
453 stop_broadcast (struct Plugin *plugin)
454 {
455   if (plugin->broadcast_ipv4)
456   {
457     if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
458     {
459       GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
460       plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
461     }
462
463     if (plugin->broadcast_ipv4_mst != NULL)
464       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
465
466     while (plugin->ipv4_broadcast_head != NULL)
467     {
468       struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
469
470       GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
471                                    plugin->ipv4_broadcast_tail, p);
472       GNUNET_free (p->addr);
473       GNUNET_free (p);
474     }
475   }
476
477   if (plugin->broadcast_ipv6)
478   {
479     /* Create IPv6 multicast request */
480     struct ipv6_mreq multicastRequest;
481
482     multicastRequest.ipv6mr_multiaddr =
483         plugin->ipv6_multicast_address.sin6_addr;
484     multicastRequest.ipv6mr_interface = 0;
485
486     /* Join the multicast address */
487     if (GNUNET_NETWORK_socket_setsockopt
488         (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
489         (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
490     {
491        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
492     }
493     else
494     {
495 #if DEBUG_UDP
496       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
497 #endif
498     }
499
500     if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
501     {
502       GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
503       plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
504     }
505     if (plugin->broadcast_ipv6_mst != NULL)
506       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
507   }
508
509 }
510
511 /* end of plugin_transport_udp_broadcasting.c */