diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2012-01-30 10:24:20 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2012-01-30 10:24:20 +0000 |
commit | 2480cd03d241eec2448017997b5b8c056ee3caf5 (patch) | |
tree | d691ece20bdb710e6ffd2b6f596b0ac02c3c30ae /src/transport/plugin_transport_udp_new_broadcasting.c | |
parent | 335fc575c8bb19428507ea218fb0ff68d970a46c (diff) | |
download | gnunet-2480cd03d241eec2448017997b5b8c056ee3caf5.tar.gz gnunet-2480cd03d241eec2448017997b5b8c056ee3caf5.zip |
- new udp implementation: start and stop
Diffstat (limited to 'src/transport/plugin_transport_udp_new_broadcasting.c')
-rw-r--r-- | src/transport/plugin_transport_udp_new_broadcasting.c | 463 |
1 files changed, 463 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_udp_new_broadcasting.c b/src/transport/plugin_transport_udp_new_broadcasting.c new file mode 100644 index 000000000..7513cb626 --- /dev/null +++ b/src/transport/plugin_transport_udp_new_broadcasting.c | |||
@@ -0,0 +1,463 @@ | |||
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 | static void | ||
177 | udp_ipv4_broadcast_send (void *cls, | ||
178 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
179 | { | ||
180 | struct Plugin *plugin = cls; | ||
181 | int sent; | ||
182 | uint16_t msg_size; | ||
183 | uint16_t hello_size; | ||
184 | char buf[65536]; | ||
185 | |||
186 | const struct GNUNET_MessageHeader *hello; | ||
187 | struct UDP_Beacon_Message *msg; | ||
188 | struct BroadcastAddress *baddr; | ||
189 | |||
190 | plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK; | ||
191 | |||
192 | hello = plugin->env->get_our_hello (); | ||
193 | hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello); | ||
194 | msg_size = hello_size + sizeof (struct UDP_Beacon_Message); | ||
195 | |||
196 | if (hello_size < (sizeof (struct GNUNET_MessageHeader)) || | ||
197 | (msg_size > (UDP_MTU))) | ||
198 | return; | ||
199 | |||
200 | msg = (struct UDP_Beacon_Message *) buf; | ||
201 | msg->sender = *(plugin->env->my_identity); | ||
202 | msg->header.size = ntohs (msg_size); | ||
203 | msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON); | ||
204 | memcpy (&msg[1], hello, hello_size); | ||
205 | sent = 0; | ||
206 | |||
207 | baddr = plugin->ipv4_broadcast_head; | ||
208 | /* just IPv4 */ | ||
209 | while ((baddr != NULL) && (baddr->addrlen == sizeof (struct sockaddr_in))) | ||
210 | { | ||
211 | struct sockaddr_in *addr = (struct sockaddr_in *) baddr->addr; | ||
212 | |||
213 | addr->sin_port = htons (plugin->port); | ||
214 | |||
215 | sent = | ||
216 | GNUNET_NETWORK_socket_sendto (plugin->sockv4, msg, msg_size, | ||
217 | (const struct sockaddr *) addr, | ||
218 | baddr->addrlen); | ||
219 | if (sent == GNUNET_SYSERR) | ||
220 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto"); | ||
221 | else | ||
222 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
223 | "Sent HELLO beacon broadcast with %i bytes to address %s\n", sent, | ||
224 | GNUNET_a2s (baddr->addr, baddr->addrlen)); | ||
225 | baddr = baddr->next; | ||
226 | } | ||
227 | |||
228 | plugin->send_ipv4_broadcast_task = | ||
229 | GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval, | ||
230 | &udp_ipv4_broadcast_send, plugin); | ||
231 | } | ||
232 | |||
233 | static void | ||
234 | udp_ipv6_broadcast_send (void *cls, | ||
235 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
236 | { | ||
237 | struct Plugin *plugin = cls; | ||
238 | int sent; | ||
239 | uint16_t msg_size; | ||
240 | uint16_t hello_size; | ||
241 | char buf[65536]; | ||
242 | |||
243 | const struct GNUNET_MessageHeader *hello; | ||
244 | struct UDP_Beacon_Message *msg; | ||
245 | |||
246 | plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK; | ||
247 | |||
248 | hello = plugin->env->get_our_hello (); | ||
249 | hello_size = GNUNET_HELLO_size ((struct GNUNET_HELLO_Message *) hello); | ||
250 | msg_size = hello_size + sizeof (struct UDP_Beacon_Message); | ||
251 | |||
252 | if (hello_size < (sizeof (struct GNUNET_MessageHeader)) || | ||
253 | (msg_size > (UDP_MTU))) | ||
254 | return; | ||
255 | |||
256 | msg = (struct UDP_Beacon_Message *) buf; | ||
257 | msg->sender = *(plugin->env->my_identity); | ||
258 | msg->header.size = ntohs (msg_size); | ||
259 | msg->header.type = ntohs (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON); | ||
260 | memcpy (&msg[1], hello, hello_size); | ||
261 | sent = 0; | ||
262 | |||
263 | sent = | ||
264 | GNUNET_NETWORK_socket_sendto (plugin->sockv6, msg, msg_size, | ||
265 | (const struct sockaddr *) | ||
266 | &plugin->ipv6_multicast_address, | ||
267 | sizeof (struct sockaddr_in6)); | ||
268 | if (sent == GNUNET_SYSERR) | ||
269 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto"); | ||
270 | else | ||
271 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
272 | "Sending IPv6 HELLO beacon broadcast with %i bytes to address %s\n", | ||
273 | sent, | ||
274 | GNUNET_a2s ((const struct sockaddr *) &plugin->ipv6_multicast_address, | ||
275 | sizeof (struct sockaddr_in6))); | ||
276 | |||
277 | |||
278 | |||
279 | plugin->send_ipv6_broadcast_task = | ||
280 | GNUNET_SCHEDULER_add_delayed (plugin->broadcast_interval, | ||
281 | &udp_ipv6_broadcast_send, plugin); | ||
282 | } | ||
283 | |||
284 | |||
285 | static int | ||
286 | iface_proc (void *cls, const char *name, int isDefault, | ||
287 | const struct sockaddr *addr, const struct sockaddr *broadcast_addr, | ||
288 | const struct sockaddr *netmask, socklen_t addrlen) | ||
289 | { | ||
290 | struct Plugin *plugin = cls; | ||
291 | |||
292 | if (addr != NULL) | ||
293 | { | ||
294 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "address %s for interface %s %p\n ", | ||
295 | GNUNET_a2s (addr, addrlen), name, addr); | ||
296 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
297 | "broadcast address %s for interface %s %p\n ", | ||
298 | GNUNET_a2s (broadcast_addr, addrlen), name, broadcast_addr); | ||
299 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "netmask %s for interface %s %p\n ", | ||
300 | GNUNET_a2s (netmask, addrlen), name, netmask); | ||
301 | |||
302 | |||
303 | /* Collecting broadcast addresses */ | ||
304 | if (broadcast_addr != NULL) | ||
305 | { | ||
306 | struct BroadcastAddress *ba = | ||
307 | GNUNET_malloc (sizeof (struct BroadcastAddress)); | ||
308 | ba->addr = GNUNET_malloc (addrlen); | ||
309 | memcpy (ba->addr, broadcast_addr, addrlen); | ||
310 | ba->addrlen = addrlen; | ||
311 | GNUNET_CONTAINER_DLL_insert (plugin->ipv4_broadcast_head, | ||
312 | plugin->ipv4_broadcast_tail, ba); | ||
313 | } | ||
314 | } | ||
315 | return GNUNET_OK; | ||
316 | } | ||
317 | |||
318 | |||
319 | void | ||
320 | setup_broadcast (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4) | ||
321 | { | ||
322 | /* create IPv4 broadcast socket */ | ||
323 | plugin->broadcast_ipv4 = GNUNET_NO; | ||
324 | if (plugin->sockv4 != NULL) | ||
325 | { | ||
326 | int yes = 1; | ||
327 | |||
328 | if (GNUNET_NETWORK_socket_setsockopt | ||
329 | (plugin->sockv4, SOL_SOCKET, SO_BROADCAST, &yes, | ||
330 | sizeof (int)) != GNUNET_OK) | ||
331 | { | ||
332 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
333 | _ | ||
334 | ("Failed to set IPv4 broadcast option for broadcast socket on port %d\n"), | ||
335 | ntohs (serverAddrv4->sin_port)); | ||
336 | } | ||
337 | else | ||
338 | { | ||
339 | GNUNET_OS_network_interfaces_list (iface_proc, plugin); | ||
340 | plugin->send_ipv4_broadcast_task = | ||
341 | GNUNET_SCHEDULER_add_now (&udp_ipv4_broadcast_send, plugin); | ||
342 | |||
343 | plugin->broadcast_ipv4_mst = | ||
344 | GNUNET_SERVER_mst_create (broadcast_ipv4_mst_cb, plugin); | ||
345 | |||
346 | LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Broadcasting running\n"); | ||
347 | plugin->broadcast_ipv4 = GNUNET_YES; | ||
348 | } | ||
349 | } | ||
350 | |||
351 | plugin->broadcast_ipv6 = GNUNET_NO; | ||
352 | if (plugin->sockv6 != NULL) | ||
353 | { | ||
354 | memset (&plugin->ipv6_multicast_address, 0, sizeof (struct sockaddr_in6)); | ||
355 | GNUNET_assert (1 == | ||
356 | inet_pton (AF_INET6, "FF05::13B", | ||
357 | &plugin->ipv6_multicast_address.sin6_addr)); | ||
358 | |||
359 | plugin->ipv6_multicast_address.sin6_family = AF_INET6; | ||
360 | plugin->ipv6_multicast_address.sin6_port = htons (plugin->port); | ||
361 | |||
362 | plugin->broadcast_ipv6_mst = | ||
363 | GNUNET_SERVER_mst_create (broadcast_ipv6_mst_cb, plugin); | ||
364 | |||
365 | /* Create IPv6 multicast request */ | ||
366 | struct ipv6_mreq multicastRequest; | ||
367 | |||
368 | multicastRequest.ipv6mr_multiaddr = | ||
369 | plugin->ipv6_multicast_address.sin6_addr; | ||
370 | /* TODO: 0 selects the "best" interface, tweak to use all interfaces | ||
371 | * | ||
372 | * http://tools.ietf.org/html/rfc2553#section-5.2: | ||
373 | * | ||
374 | * IPV6_JOIN_GROUP | ||
375 | * | ||
376 | * Join a multicast group on a specified local interface. If the | ||
377 | * interface index is specified as 0, the kernel chooses the local | ||
378 | * interface. For example, some kernels look up the multicast | ||
379 | * group in the normal IPv6 routing table and using the resulting | ||
380 | * interface. | ||
381 | * */ | ||
382 | multicastRequest.ipv6mr_interface = 0; | ||
383 | |||
384 | /* Join the multicast group */ | ||
385 | if (GNUNET_NETWORK_socket_setsockopt | ||
386 | (plugin->sockv6, IPPROTO_IPV6, IPV6_JOIN_GROUP, | ||
387 | (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK) | ||
388 | { | ||
389 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
390 | "Failed to join IPv6 multicast group: IPv6 broadcasting not running\n"); | ||
391 | } | ||
392 | else | ||
393 | { | ||
394 | #if DEBUG_UDP | ||
395 | LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 broadcasting running\n"); | ||
396 | #endif | ||
397 | plugin->send_ipv6_broadcast_task = | ||
398 | GNUNET_SCHEDULER_add_now (&udp_ipv6_broadcast_send, plugin); | ||
399 | plugin->broadcast_ipv6 = GNUNET_YES; | ||
400 | } | ||
401 | } | ||
402 | } | ||
403 | |||
404 | void | ||
405 | stop_broadcast (struct Plugin *plugin) | ||
406 | { | ||
407 | if (plugin->broadcast_ipv4) | ||
408 | { | ||
409 | if (plugin->send_ipv4_broadcast_task != GNUNET_SCHEDULER_NO_TASK) | ||
410 | { | ||
411 | GNUNET_SCHEDULER_cancel (plugin->send_ipv4_broadcast_task); | ||
412 | plugin->send_ipv4_broadcast_task = GNUNET_SCHEDULER_NO_TASK; | ||
413 | } | ||
414 | |||
415 | if (plugin->broadcast_ipv4_mst != NULL) | ||
416 | GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst); | ||
417 | |||
418 | while (plugin->ipv4_broadcast_head != NULL) | ||
419 | { | ||
420 | struct BroadcastAddress *p = plugin->ipv4_broadcast_head; | ||
421 | |||
422 | GNUNET_CONTAINER_DLL_remove (plugin->ipv4_broadcast_head, | ||
423 | plugin->ipv4_broadcast_tail, p); | ||
424 | GNUNET_free (p->addr); | ||
425 | GNUNET_free (p); | ||
426 | } | ||
427 | } | ||
428 | |||
429 | if (plugin->broadcast_ipv6) | ||
430 | { | ||
431 | /* Create IPv6 multicast request */ | ||
432 | struct ipv6_mreq multicastRequest; | ||
433 | |||
434 | multicastRequest.ipv6mr_multiaddr = | ||
435 | plugin->ipv6_multicast_address.sin6_addr; | ||
436 | multicastRequest.ipv6mr_interface = 0; | ||
437 | |||
438 | /* Join the multicast address */ | ||
439 | if (GNUNET_NETWORK_socket_setsockopt | ||
440 | (plugin->sockv6, IPPROTO_IPV6, IPV6_LEAVE_GROUP, | ||
441 | (char *) &multicastRequest, sizeof (multicastRequest)) != GNUNET_OK) | ||
442 | { | ||
443 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, setsockopt); | ||
444 | } | ||
445 | else | ||
446 | { | ||
447 | #if DEBUG_UDP | ||
448 | LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv6 Broadcasting stopped\n"); | ||
449 | #endif | ||
450 | } | ||
451 | |||
452 | if (plugin->send_ipv6_broadcast_task != GNUNET_SCHEDULER_NO_TASK) | ||
453 | { | ||
454 | GNUNET_SCHEDULER_cancel (plugin->send_ipv6_broadcast_task); | ||
455 | plugin->send_ipv6_broadcast_task = GNUNET_SCHEDULER_NO_TASK; | ||
456 | } | ||
457 | if (plugin->broadcast_ipv6_mst != NULL) | ||
458 | GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst); | ||
459 | } | ||
460 | |||
461 | } | ||
462 | |||
463 | /* end of plugin_transport_udp_broadcasting.c */ | ||