- fix
[oweals/gnunet.git] / src / transport / plugin_transport_udp_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.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 int
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 GNUNET_OK;
108   LOG (GNUNET_ERROR_TYPE_DEBUG,
109        "Received beacon with %u bytes from peer `%s' via address `%s'\n",
110        ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
111        udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
112   struct GNUNET_ATS_Information atsi[2];
113
114   /* setup ATS */
115   atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
116   atsi[0].value = htonl (1);
117   atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
118   atsi[1].value = mc->ats_address_network_type;
119   GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
120
121   hello = (struct GNUNET_MessageHeader *) &msg[1];
122   plugin->env->receive (plugin->env->cls,
123                                                                                         &msg->sender,
124                                                                                         hello,
125                         NULL,
126                         (const char *) &mc->addr,
127                         sizeof (mc->addr));
128   plugin->env->update_address_metrics (plugin->env->cls,
129                 &msg->sender,
130                 (const char *) &mc->addr,
131                 sizeof (mc->addr),
132       NULL,
133       (struct GNUNET_ATS_Information *) &atsi, 2);
134
135   GNUNET_STATISTICS_update (plugin->env->stats,
136                             _
137                             ("# IPv6 multicast HELLO beacons received via udp"),
138                             1, GNUNET_NO);
139   GNUNET_free (mc);
140   return GNUNET_OK;
141 }
142
143 int
144 broadcast_ipv4_mst_cb (void *cls, void *client,
145                        const struct GNUNET_MessageHeader *message)
146 {
147   struct Plugin *plugin = cls;
148   struct Mstv4Context *mc = client;
149   const struct GNUNET_MessageHeader *hello;
150   struct UDP_Beacon_Message *msg;
151
152   msg = (struct UDP_Beacon_Message *) message;
153
154   if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
155       ntohs (msg->header.type))
156     return GNUNET_OK;
157   LOG (GNUNET_ERROR_TYPE_DEBUG,
158        "Received beacon with %u bytes from peer `%s' via address `%s'\n",
159        ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
160        udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
161
162   struct GNUNET_ATS_Information atsi[2];
163
164   /* setup ATS */
165   atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
166   atsi[0].value = htonl (1);
167   atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
168   atsi[1].value = mc->ats_address_network_type;
169   GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
170
171   hello = (struct GNUNET_MessageHeader *) &msg[1];
172   plugin->env->receive (plugin->env->cls,
173                                                                                         &msg->sender,
174                                                                                         hello,
175                         NULL,
176                         (const char *) &mc->addr,
177                         sizeof (mc->addr));
178
179   plugin->env->update_address_metrics (plugin->env->cls,
180                 &msg->sender,
181                 (const char *) &mc->addr,
182                 sizeof (mc->addr),
183       NULL,
184       (struct GNUNET_ATS_Information *) &atsi, 2);
185
186   GNUNET_STATISTICS_update (plugin->env->stats,
187                             _
188                             ("# IPv4 broadcast HELLO beacons received via udp"),
189                             1, GNUNET_NO);
190   GNUNET_free (mc);
191   return GNUNET_OK;
192 }
193
194 void
195 udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
196 {
197   struct GNUNET_ATS_Information ats;
198
199   if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in)))
200   {
201     LOG (GNUNET_ERROR_TYPE_DEBUG,
202          "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
203          size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
204     struct Mstv4Context *mc;
205
206     mc = GNUNET_malloc (sizeof (struct Mstv4Context));
207     struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
208
209     mc->addr.ipv4_addr = av4->sin_addr.s_addr;
210     mc->addr.u4_port = av4->sin_port;
211     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
212     mc->ats_address_network_type = ats.value;
213
214     GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
215     if (GNUNET_OK !=
216         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
217                                    GNUNET_NO, GNUNET_NO))
218       GNUNET_free (mc);
219   }
220   else if ((GNUNET_YES == plugin->broadcast_ipv4) && (addrlen == sizeof (struct sockaddr_in6)))
221   {
222     LOG (GNUNET_ERROR_TYPE_DEBUG,
223          "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
224          size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
225     struct Mstv6Context *mc;
226
227     mc = GNUNET_malloc (sizeof (struct Mstv6Context));
228     struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
229
230     mc->addr.ipv6_addr = av6->sin6_addr;
231     mc->addr.u6_port = av6->sin6_port;
232     ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
233     mc->ats_address_network_type = ats.value;
234     GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
235     if (GNUNET_OK !=
236         GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
237                                    GNUNET_NO, GNUNET_NO))
238       GNUNET_free (mc);
239   }
240 }
241
242 static unsigned int
243 prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg)
244 {
245   uint16_t hello_size;
246   uint16_t msg_size;
247
248   const struct GNUNET_MessageHeader *hello;
249   hello = plugin->env->get_our_hello ();
250   if (NULL == hello)
251     return 0;
252   hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
253   msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
254
255   if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
256       (msg_size > (UDP_MTU)))
257     return 0;
258
259   msg->sender = *(plugin->env->my_identity);
260   msg->header.size = htons (msg_size);
261   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
262   memcpy (&msg[1], hello, hello_size);
263   return msg_size;
264 }
265
266 static void
267 udp_ipv4_broadcast_send (void *cls,
268                          const struct GNUNET_SCHEDULER_TaskContext *tc)
269 {
270   struct Plugin *plugin = cls;
271   int sent;
272   uint16_t msg_size;
273   char buf[65536] GNUNET_ALIGN;
274   struct BroadcastAddress *baddr;
275
276   plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
277
278   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
279   sent = 0;
280   baddr = plugin->ipv4_broadcast_head;
281   /* just IPv4 */
282   while ((msg_size > 0) && (baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
283   {
284     struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
285
286     addr->sin_port = htons (plugin->port);
287
288     sent = GNUNET_NETWORK_socket_sendto (plugin->sockv4, &buf, msg_size,
289                                       (const struct sockaddr *) addr,
290                                       baddr->addrlen);
291     if (sent == GNUNET_SYSERR)
292     {
293       if ((ENETUNREACH == errno) || (ENETDOWN == errno))
294       {
295         /* "Network unreachable" or "Network down"
296          *
297          * This indicates that we just do not have network connectivity
298          */
299         GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
300             "Network connectivity is down, cannot send beacon!\n");
301       }
302       else
303         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
304     }
305     else
306     {
307       LOG (GNUNET_ERROR_TYPE_DEBUG,
308            "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
309            GNUNET_a2s (baddr->addr, baddr->addrlen));
310     }
311     baddr = baddr->next;
312   }
313
314   plugin->send_ipv4_broadcast_task =
315       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
316                                     &udp_ipv4_broadcast_send, plugin);
317 }
318
319 static void
320 udp_ipv6_broadcast_send (void *cls,
321                          const struct GNUNET_SCHEDULER_TaskContext *tc)
322 {
323   struct Plugin *plugin = cls;
324   int sent;
325   uint16_t msg_size;
326   char buf[65536] GNUNET_ALIGN;
327
328   plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
329
330   msg_size = prepare_beacon(plugin, (struct UDP_Beacon_Message *) &buf);
331   sent = 0;
332   sent = GNUNET_NETWORK_socket_sendto (plugin->sockv6, &buf, msg_size,
333                                     (const struct sockaddr *)
334                                     &plugin->ipv6_multicast_address,
335                                     sizeof (struct sockaddr_in6));
336   if (sent == GNUNET_SYSERR)
337   {
338     if ((ENETUNREACH == errno) || (ENETDOWN == errno))
339     {
340       /* "Network unreachable" or "Network down"
341        *
342        * This indicates that this system is IPv6 enabled, but does not
343        * have a valid global IPv6 address assigned
344        */
345       GNUNET_log (GNUNET_ERROR_TYPE_BULK | GNUNET_ERROR_TYPE_WARNING,
346           "Network connectivity is down, cannot send beacon!\n");
347     }
348     else
349       GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
350   }
351   else
352   {
353     LOG (GNUNET_ERROR_TYPE_DEBUG,
354          "Sending IPv6 HELLO beacon broadcast with  %i bytes to address %s\n",
355          sent,
356          GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
357                      sizeof (struct sockaddr_in6)));
358   }
359   plugin->send_ipv6_broadcast_task =
360       GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
361                                     &udp_ipv6_broadcast_send, plugin);
362 }
363
364
365 static int
366 iface_proc (void *cls, const char *name, int isDefault,
367             const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
368             const struct sockaddr *netmask, socklen_t addrlen)
369 {
370   struct Plugin *plugin = cls;
371
372   if (addr != NULL)
373   {
374     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
375                 GNUNET_a2s (addr, addrlen), name, addr);
376     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377                 "broadcast address %s for interface %s %p\n ",
378                 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
379     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
380                 GNUNET_a2s (netmask, addrlen), name, netmask);
381
382     /* Collecting broadcast addresses */
383     if (broadcast_addr != NULL)
384     {
385       struct BroadcastAddress *ba =
386           GNUNET_malloc (sizeof (struct BroadcastAddress));
387       ba->addr = GNUNET_malloc (addrlen);
388       memcpy (ba->addr, broadcast_addr, addrlen);
389       ba->addrlen = addrlen;
390       GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
391                                    plugin->ipv4_broadcast_tail, ba);
392     }
393   }
394   return GNUNET_OK;
395 }
396
397
398 void
399 setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
400 {
401   /* create IPv4 broadcast socket */
402   plugin->broadcast_ipv4 = GNUNET_NO;
403   if (plugin->sockv4 != NULL)
404   {
405     int yes = 1;
406
407     if (GNUNET_NETWORK_socket_setsockopt
408         (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
409          sizeof (int)) != GNUNET_OK)
410     {
411       LOG (GNUNET_ERROR_TYPE_WARNING,
412            _
413            ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
414            ntohs (serverAddrv4->sin_port));
415     }
416     else
417     {
418       GNUNET_OS_network_interfaces_list (iface_proc, plugin);
419       plugin->send_ipv4_broadcast_task =
420           GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
421
422       plugin->broadcast_ipv4_mst =
423           GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
424
425       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
426       plugin->broadcast_ipv4 = GNUNET_YES;
427     }
428   }
429
430   plugin->broadcast_ipv6 = GNUNET_NO;
431   if (plugin->sockv6 != NULL)
432   {
433     memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
434     GNUNET_assert (1 ==
435                    inet_pton (AF_INET6, "FF05::13B",
436                               &plugin->ipv6_multicast_address.sin6_addr));
437
438     plugin->ipv6_multicast_address.sin6_family = AF_INET6;
439     plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
440
441     plugin->broadcast_ipv6_mst =
442         GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
443
444     /* Create IPv6 multicast request */
445     struct ipv6_mreq multicastRequest;
446
447     multicastRequest.ipv6mr_multiaddr =
448         plugin->ipv6_multicast_address.sin6_addr;
449     /* TODO: 0 selects the "best" interface, tweak to use all interfaces
450      *
451      * http://tools.ietf.org/html/rfc2553#section-5.2:
452      *
453      * IPV6_JOIN_GROUP
454      *
455      * Join a multicast group on a specified local interface.  If the
456      * interface index is specified as 0, the kernel chooses the local
457      * interface.  For example, some kernels look up the multicast
458      * group in the normal IPv6 routing table and using the resulting
459      * interface.
460      * */
461     multicastRequest.ipv6mr_interface = 0;
462
463     /* Join the multicast group */
464     if (GNUNET_NETWORK_socket_setsockopt
465         (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
466          (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
467     {
468       LOG (GNUNET_ERROR_TYPE_WARNING,
469       "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
470     }
471     else
472     {
473       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
474       plugin->send_ipv6_broadcast_task =
475           GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
476       plugin->broadcast_ipv6 = GNUNET_YES;
477     }
478   }
479 }
480
481 void
482 stop_broadcast (struct Plugin *plugin)
483 {
484   if (plugin->broadcast_ipv4)
485   {
486     if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
487     {
488       GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
489       plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
490     }
491
492     if (plugin->broadcast_ipv4_mst != NULL)
493       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
494
495     while (plugin->ipv4_broadcast_head != NULL)
496     {
497       struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
498
499       GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
500                                    plugin->ipv4_broadcast_tail, p);
501       GNUNET_free (p->addr);
502       GNUNET_free (p);
503     }
504   }
505
506   if (plugin->broadcast_ipv6)
507   {
508     /* Create IPv6 multicast request */
509     struct ipv6_mreq multicastRequest;
510
511     multicastRequest.ipv6mr_multiaddr =
512         plugin->ipv6_multicast_address.sin6_addr;
513     multicastRequest.ipv6mr_interface = 0;
514
515     /* Join the multicast address */
516     if (GNUNET_NETWORK_socket_setsockopt
517         (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
518         (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
519     {
520        GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
521     }
522     else
523     {
524       LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
525     }
526
527     if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
528     {
529       GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
530       plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
531     }
532     if (plugin->broadcast_ipv6_mst != NULL)
533       GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
534   }
535
536 }
537
538 /* end of plugin_transport_udp_broadcasting.c */