aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_udp_new_broadcasting.c
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2012-02-13 11:57:40 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2012-02-13 11:57:40 +0000
commitf89a942814ab1eb72334dafb80120ee146e42783 (patch)
treee30ffca8c2f9dea385cc9892dee44378bcd4a077 /src/transport/plugin_transport_udp_new_broadcasting.c
parent7eaa96c073b94a0f0d0968c578d493db75a0e020 (diff)
downloadgnunet-f89a942814ab1eb72334dafb80120ee146e42783.tar.gz
gnunet-f89a942814ab1eb72334dafb80120ee146e42783.zip
adding rewritten udp plugin
Diffstat (limited to 'src/transport/plugin_transport_udp_new_broadcasting.c')
-rw-r--r--src/transport/plugin_transport_udp_new_broadcasting.c522
1 files changed, 0 insertions, 522 deletions
diff --git a/src/transport/plugin_transport_udp_new_broadcasting.c b/src/transport/plugin_transport_udp_new_broadcasting.c
deleted file mode 100644
index 3c2d82675..000000000
--- a/src/transport/plugin_transport_udp_new_broadcasting.c
+++ /dev/null
@@ -1,522 +0,0 @@
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
45struct 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
59struct BroadcastAddress
60{
61 struct BroadcastAddress *next;
62 struct BroadcastAddress *prev;
63
64 void *addr;
65 socklen_t addrlen;
66};
67
68
69struct 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
80struct 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
93void
94broadcast_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#if DEBUG_UDP_BROADCASTING
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#endif
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
135void
136broadcast_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#if DEBUG_UDP_BROADCASTING
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#endif
155
156 struct GNUNET_ATS_Information atsi[2];
157
158 /* setup ATS */
159 atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
160 atsi[0].value = htonl (1);
161 atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
162 atsi[1].value = mc->ats_address_network_type;
163 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
164
165 hello = (struct GNUNET_MessageHeader *) &msg[1];
166 plugin->env->receive (plugin->env->cls, &msg->sender, hello,
167 (const struct GNUNET_ATS_Information *) &atsi, 2, NULL,
168 (const char *) &mc->addr, sizeof (mc->addr));
169
170 GNUNET_STATISTICS_update (plugin->env->stats,
171 _
172 ("# IPv4 broadcast HELLO beacons received via udp"),
173 1, GNUNET_NO);
174 GNUNET_free (mc);
175}
176
177void
178udp_broadcast_receive (struct Plugin *plugin, const char * buf, ssize_t size, struct sockaddr *addr, size_t addrlen)
179{
180 struct GNUNET_ATS_Information ats;
181
182 if (addrlen == sizeof (struct sockaddr_in))
183 {
184#if DEBUG_UDP_BROADCASTING
185 LOG (GNUNET_ERROR_TYPE_DEBUG,
186 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n",
187 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen));
188#endif
189 struct Mstv4Context *mc;
190
191 mc = GNUNET_malloc (sizeof (struct Mstv4Context));
192 struct sockaddr_in *av4 = (struct sockaddr_in *) addr;
193
194 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
195 mc->addr.u4_port = av4->sin_port;
196 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
197 mc->ats_address_network_type = ats.value;
198 if (GNUNET_OK !=
199 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
200 GNUNET_NO, GNUNET_NO))
201 GNUNET_free (mc);
202 }
203 else if (addrlen == sizeof (struct sockaddr_in6))
204 {
205#if DEBUG_UDP_BROADCASTING
206 LOG (GNUNET_ERROR_TYPE_DEBUG,
207 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
208 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
209#endif
210 struct Mstv6Context *mc;
211
212 mc = GNUNET_malloc (sizeof (struct Mstv6Context));
213 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
214
215 mc->addr.ipv6_addr = av6->sin6_addr;
216 mc->addr.u6_port = av6->sin6_port;
217 ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
218 mc->ats_address_network_type = ats.value;
219
220 if (GNUNET_OK !=
221 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
222 GNUNET_NO, GNUNET_NO))
223 GNUNET_free (mc);
224 }
225}
226
227static void
228udp_ipv4_broadcast_send (void *cls,
229 const struct GNUNET_SCHEDULER_TaskContext *tc)
230{
231 struct Plugin *plugin = cls;
232 int sent;
233 uint16_t msg_size;
234 uint16_t hello_size;
235 char buf[65536];
236
237 const struct GNUNET_MessageHeader *hello;
238 struct UDP_Beacon_Message *msg;
239 struct BroadcastAddress *baddr;
240
241 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
242
243 hello = plugin->env->get_our_hello ();
244 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
245 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
246
247 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
248 (msg_size > (UDP_MTU)))
249 return;
250
251 msg = (struct UDP_Beacon_Message *) buf;
252 msg->sender = *(plugin->env->my_identity);
253 msg->header.size = ntohs (msg_size);
254 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
255 memcpy (&msg[1], hello, hello_size);
256 sent = 0;
257
258 baddr = plugin->ipv4_broadcast_head;
259 /* just IPv4 */
260 while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in)))
261 {
262 struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr;
263
264 addr->sin_port = htons (plugin->port);
265
266 sent =
267 GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, msg_size,
268 (const struct sockaddr *) addr,
269 baddr->addrlen);
270 if (sent == GNUNET_SYSERR)
271 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
272 else
273 {
274#if DEBUG_UDP_BROADCASTING
275 LOG (GNUNET_ERROR_TYPE_DEBUG,
276 "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent,
277 GNUNET_a2s (baddr->addr, baddr->addrlen));
278#endif
279 }
280 baddr = baddr->next;
281 }
282
283 plugin->send_ipv4_broadcast_task =
284 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
285 &udp_ipv4_broadcast_send, plugin);
286}
287
288static void
289udp_ipv6_broadcast_send (void *cls,
290 const struct GNUNET_SCHEDULER_TaskContext *tc)
291{
292 struct Plugin *plugin = cls;
293 int sent;
294 uint16_t msg_size;
295 uint16_t hello_size;
296 char buf[65536];
297
298 const struct GNUNET_MessageHeader *hello;
299 struct UDP_Beacon_Message *msg;
300
301 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
302
303 hello = plugin->env->get_our_hello ();
304 hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello);
305 msg_size = hello_size + sizeof (struct UDP_Beacon_Message);
306
307 if (hello_size < (sizeof (struct GNUNET_MessageHeader)) ||
308 (msg_size > (UDP_MTU)))
309 return;
310
311 msg = (struct UDP_Beacon_Message *) buf;
312 msg->sender = *(plugin->env->my_identity);
313 msg->header.size = ntohs (msg_size);
314 msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON);
315 memcpy (&msg[1], hello, hello_size);
316 sent = 0;
317
318 sent =
319 GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, msg_size,
320 (const struct sockaddr *)
321 &plugin->ipv6_multicast_address,
322 sizeof (struct sockaddr_in6));
323 if (sent == GNUNET_SYSERR)
324 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
325 else
326 {
327#if DEBUG_UDP_BROADCASTING
328 LOG (GNUNET_ERROR_TYPE_DEBUG,
329 "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n",
330 sent,
331 GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address,
332 sizeof (struct sockaddr_in6)));
333#endif
334 }
335
336
337 plugin->send_ipv6_broadcast_task =
338 GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval,
339 &udp_ipv6_broadcast_send, plugin);
340}
341
342
343static int
344iface_proc (void *cls, const char *name, int isDefault,
345 const struct sockaddr *addr, const struct sockaddr *broadcast_addr,
346 const struct sockaddr *netmask, socklen_t addrlen)
347{
348 struct Plugin *plugin = cls;
349
350 if (addr != NULL)
351 {
352#if DEBUG_UDP_BROADCASTING
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ",
354 GNUNET_a2s (addr, addrlen), name, addr);
355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356 "broadcast address %s for interface %s %p\n ",
357 GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ",
359 GNUNET_a2s (netmask, addrlen), name, netmask);
360#endif
361
362 /* Collecting broadcast addresses */
363 if (broadcast_addr != NULL)
364 {
365 struct BroadcastAddress *ba =
366 GNUNET_malloc (sizeof (struct BroadcastAddress));
367 ba->addr = GNUNET_malloc (addrlen);
368 memcpy (ba->addr, broadcast_addr, addrlen);
369 ba->addrlen = addrlen;
370 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head,
371 plugin->ipv4_broadcast_tail, ba);
372 }
373 }
374 return GNUNET_OK;
375}
376
377
378void
379setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
380{
381 /* create IPv4 broadcast socket */
382 plugin->broadcast_ipv4 = GNUNET_NO;
383 if (plugin->sockv4 != NULL)
384 {
385 int yes = 1;
386
387 if (GNUNET_NETWORK_socket_setsockopt
388 (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes,
389 sizeof (int)) != GNUNET_OK)
390 {
391 LOG (GNUNET_ERROR_TYPE_WARNING,
392 _
393 ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"),
394 ntohs (serverAddrv4->sin_port));
395 }
396 else
397 {
398 GNUNET_OS_network_interfaces_list (iface_proc, plugin);
399 plugin->send_ipv4_broadcast_task =
400 GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin);
401
402 plugin->broadcast_ipv4_mst =
403 GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin);
404
405 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n");
406 plugin->broadcast_ipv4 = GNUNET_YES;
407 }
408 }
409
410 plugin->broadcast_ipv6 = GNUNET_NO;
411 if (plugin->sockv6 != NULL)
412 {
413 memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6));
414 GNUNET_assert (1 ==
415 inet_pton (AF_INET6, "FF05::13B",
416 &plugin->ipv6_multicast_address.sin6_addr));
417
418 plugin->ipv6_multicast_address.sin6_family = AF_INET6;
419 plugin->ipv6_multicast_address.sin6_port = htons (plugin->port);
420
421 plugin->broadcast_ipv6_mst =
422 GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin);
423
424 /* Create IPv6 multicast request */
425 struct ipv6_mreq multicastRequest;
426
427 multicastRequest.ipv6mr_multiaddr =
428 plugin->ipv6_multicast_address.sin6_addr;
429 /* TODO: 0 selects the "best" interface, tweak to use all interfaces
430 *
431 * http://tools.ietf.org/html/rfc2553#section-5.2:
432 *
433 * IPV6_JOIN_GROUP
434 *
435 * Join a multicast group on a specified local interface. If the
436 * interface index is specified as 0, the kernel chooses the local
437 * interface. For example, some kernels look up the multicast
438 * group in the normal IPv6 routing table and using the resulting
439 * interface.
440 * */
441 multicastRequest.ipv6mr_interface = 0;
442
443 /* Join the multicast group */
444 if (GNUNET_NETWORK_socket_setsockopt
445 (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP,
446 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
447 {
448 LOG (GNUNET_ERROR_TYPE_WARNING,
449 "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n");
450 }
451 else
452 {
453#if DEBUG_UDP
454 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n");
455#endif
456 plugin->send_ipv6_broadcast_task =
457 GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin);
458 plugin->broadcast_ipv6 = GNUNET_YES;
459 }
460 }
461}
462
463void
464stop_broadcast (struct Plugin *plugin)
465{
466 if (plugin->broadcast_ipv4)
467 {
468 if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
469 {
470 GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task);
471 plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
472 }
473
474 if (plugin->broadcast_ipv4_mst != NULL)
475 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
476
477 while (plugin->ipv4_broadcast_head != NULL)
478 {
479 struct BroadcastAddress *p = plugin->ipv4_broadcast_head;
480
481 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head,
482 plugin->ipv4_broadcast_tail, p);
483 GNUNET_free (p->addr);
484 GNUNET_free (p);
485 }
486 }
487
488 if (plugin->broadcast_ipv6)
489 {
490 /* Create IPv6 multicast request */
491 struct ipv6_mreq multicastRequest;
492
493 multicastRequest.ipv6mr_multiaddr =
494 plugin->ipv6_multicast_address.sin6_addr;
495 multicastRequest.ipv6mr_interface = 0;
496
497 /* Join the multicast address */
498 if (GNUNET_NETWORK_socket_setsockopt
499 (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
500 (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK)
501 {
502 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt);
503 }
504 else
505 {
506#if DEBUG_UDP
507 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n");
508#endif
509 }
510
511 if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK)
512 {
513 GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task);
514 plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK;
515 }
516 if (plugin->broadcast_ipv6_mst != NULL)
517 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst);
518 }
519
520}
521
522/* end of plugin_transport_udp_broadcasting.c */