diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-07-14 15:30:07 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-07-14 15:30:07 +0000 |
commit | e053b5cd2bf4c8fa6ec2b6b0afca3da1cd7d6170 (patch) | |
tree | 71bf7e3a925b34b589c8befd641b6dc794bb70ca /src/transport/plugin_transport_udp_new.c | |
parent | b1dbcfcf51167b5eb15596cec02561a5be84cef2 (diff) | |
download | gnunet-e053b5cd2bf4c8fa6ec2b6b0afca3da1cd7d6170.tar.gz gnunet-e053b5cd2bf4c8fa6ec2b6b0afca3da1cd7d6170.zip |
new udp code
Diffstat (limited to 'src/transport/plugin_transport_udp_new.c')
-rw-r--r-- | src/transport/plugin_transport_udp_new.c | 1599 |
1 files changed, 1599 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_udp_new.c b/src/transport/plugin_transport_udp_new.c new file mode 100644 index 000000000..c5b14813b --- /dev/null +++ b/src/transport/plugin_transport_udp_new.c | |||
@@ -0,0 +1,1599 @@ | |||
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.c | ||
23 | * @brief Implementation of the UDP NAT punching | ||
24 | * transport service | ||
25 | * @author Christian Grothoff | ||
26 | * @author Nathan Evans | ||
27 | */ | ||
28 | #include "platform.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_statistics_service.h" | ||
37 | #include "gnunet_transport_service.h" | ||
38 | #include "gnunet_transport_plugin.h" | ||
39 | #include "transport.h" | ||
40 | |||
41 | #define DEBUG_UDP GNUNET_NO | ||
42 | |||
43 | /** | ||
44 | * MTU for fragmentation subsystem. Should be conservative since | ||
45 | * all communicating peers MUST work with this MTU. | ||
46 | */ | ||
47 | #define UDP_MTU 1400 | ||
48 | |||
49 | /** | ||
50 | * Number of messages we can defragment in parallel. We only really | ||
51 | * defragment 1 message at a time, but if messages get re-ordered, we | ||
52 | * may want to keep knowledge about the previous message to avoid | ||
53 | * discarding the current message in favor of a single fragment of a | ||
54 | * previous message. 3 should be good since we don't expect massive | ||
55 | * message reorderings with UDP. | ||
56 | */ | ||
57 | #define UDP_MAX_MESSAGES_IN_DEFRAG 3 | ||
58 | |||
59 | /** | ||
60 | * We keep a defragmentation queue per sender address. How many | ||
61 | * sender addresses do we support at the same time? Memory consumption | ||
62 | * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this | ||
63 | * value. (So 128 corresponds to 12 MB and should suffice for | ||
64 | * connecting to roughly 128 peers via UDP). | ||
65 | */ | ||
66 | #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128 | ||
67 | |||
68 | |||
69 | /** | ||
70 | * UDP Message-Packet header (after defragmentation). | ||
71 | */ | ||
72 | struct UDPMessage | ||
73 | { | ||
74 | /** | ||
75 | * Message header. | ||
76 | */ | ||
77 | struct GNUNET_MessageHeader header; | ||
78 | |||
79 | /** | ||
80 | * Always zero for now. | ||
81 | */ | ||
82 | uint32_t reserved; | ||
83 | |||
84 | /** | ||
85 | * What is the identity of the sender | ||
86 | */ | ||
87 | struct GNUNET_PeerIdentity sender; | ||
88 | |||
89 | }; | ||
90 | |||
91 | |||
92 | /** | ||
93 | * Network format for IPv4 addresses. | ||
94 | */ | ||
95 | struct IPv4UdpAddress | ||
96 | { | ||
97 | /** | ||
98 | * IPv4 address, in network byte order. | ||
99 | */ | ||
100 | uint32_t ipv4_addr GNUNET_PACKED; | ||
101 | |||
102 | /** | ||
103 | * Port number, in network byte order. | ||
104 | */ | ||
105 | uint16_t u4_port GNUNET_PACKED; | ||
106 | }; | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Network format for IPv6 addresses. | ||
111 | */ | ||
112 | struct IPv6UdpAddress | ||
113 | { | ||
114 | |||
115 | /** | ||
116 | * IPv6 address. | ||
117 | */ | ||
118 | struct in6_addr ipv6_addr GNUNET_PACKED; | ||
119 | |||
120 | /** | ||
121 | * Port number, in network byte order. | ||
122 | */ | ||
123 | uint16_t u6_port GNUNET_PACKED; | ||
124 | }; | ||
125 | |||
126 | |||
127 | /* Forward definition */ | ||
128 | struct Plugin; | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Session with another peer. | ||
133 | */ | ||
134 | struct PeerSession | ||
135 | { | ||
136 | |||
137 | /** | ||
138 | * Which peer is this session for? | ||
139 | */ | ||
140 | struct GNUNET_PeerIdentity target; | ||
141 | |||
142 | /** | ||
143 | * Pointer to the global plugin struct. | ||
144 | */ | ||
145 | struct Plugin *plugin; | ||
146 | |||
147 | /** | ||
148 | * Address of the other peer | ||
149 | */ | ||
150 | const struct sockaddr *sock_addr; | ||
151 | |||
152 | /** | ||
153 | * Function to call upon completion of the transmission. | ||
154 | */ | ||
155 | GNUNET_TRANSPORT_TransmitContinuation cont; | ||
156 | |||
157 | /** | ||
158 | * Closure for 'cont'. | ||
159 | */ | ||
160 | void *cont_cls; | ||
161 | |||
162 | /** | ||
163 | * Current outgoing message to this peer. | ||
164 | */ | ||
165 | struct GNUNET_FRAGMENT_Context *frag; | ||
166 | |||
167 | }; | ||
168 | |||
169 | |||
170 | /** | ||
171 | * Data structure to track defragmentation contexts based | ||
172 | * on the source of the UDP traffic. | ||
173 | */ | ||
174 | struct ReceiveContext | ||
175 | { | ||
176 | |||
177 | /** | ||
178 | * Defragmentation context. | ||
179 | */ | ||
180 | struct GNUNET_DEFRAGMENT_Context *defrag; | ||
181 | |||
182 | /** | ||
183 | * Source address this receive context is for (allocated at the | ||
184 | * end of the struct). | ||
185 | */ | ||
186 | const struct sockaddr *src_addr; | ||
187 | |||
188 | /** | ||
189 | * Reference to master plugin struct. | ||
190 | */ | ||
191 | struct Plugin *plugin; | ||
192 | |||
193 | /** | ||
194 | * Node in the defrag heap. | ||
195 | */ | ||
196 | struct GNUNET_CONTAINER_HeapNode *hnode; | ||
197 | |||
198 | /** | ||
199 | * Length of 'src_addr' | ||
200 | */ | ||
201 | size_t addr_len; | ||
202 | |||
203 | }; | ||
204 | |||
205 | |||
206 | /** | ||
207 | * Encapsulation of all of the state of the plugin. | ||
208 | */ | ||
209 | struct Plugin | ||
210 | { | ||
211 | |||
212 | /** | ||
213 | * Our environment. | ||
214 | */ | ||
215 | struct GNUNET_TRANSPORT_PluginEnvironment *env; | ||
216 | |||
217 | /** | ||
218 | * Session of peers with whom we are currently connected, | ||
219 | * map of peer identity to 'struct PeerSession'. | ||
220 | */ | ||
221 | struct GNUNET_CONTAINER_MultiHashMap *sessions; | ||
222 | |||
223 | /** | ||
224 | * Heap with all of our defragmentation activities. | ||
225 | */ | ||
226 | struct GNUNET_CONTAINER_Heap *defrags; | ||
227 | |||
228 | /** | ||
229 | * ID of select task | ||
230 | */ | ||
231 | GNUNET_SCHEDULER_TaskIdentifier select_task; | ||
232 | |||
233 | /** | ||
234 | * Tokenizer for inbound messages. | ||
235 | */ | ||
236 | struct GNUNET_SERVER_MessageStreamTokenizer *mst; | ||
237 | |||
238 | /** | ||
239 | * Address we were told to bind to exclusively (IPv4). | ||
240 | */ | ||
241 | char *bind4_address; | ||
242 | |||
243 | /** | ||
244 | * Address we were told to bind to exclusively (IPv6). | ||
245 | */ | ||
246 | char *bind6_address; | ||
247 | |||
248 | /** | ||
249 | * Handle to NAT traversal support. | ||
250 | */ | ||
251 | struct GNUNET_NAT_Handle *nat; | ||
252 | |||
253 | /** | ||
254 | * FD Read set | ||
255 | */ | ||
256 | struct GNUNET_NETWORK_FDSet *rs; | ||
257 | |||
258 | /** | ||
259 | * The read socket for IPv4 | ||
260 | */ | ||
261 | struct GNUNET_NETWORK_Handle *sockv4; | ||
262 | |||
263 | /** | ||
264 | * The read socket for IPv6 | ||
265 | */ | ||
266 | struct GNUNET_NETWORK_Handle *sockv6; | ||
267 | |||
268 | /** | ||
269 | * Port we listen on. | ||
270 | */ | ||
271 | uint16_t port; | ||
272 | |||
273 | /** | ||
274 | * Port we advertise on. | ||
275 | */ | ||
276 | uint16_t aport; | ||
277 | |||
278 | }; | ||
279 | |||
280 | |||
281 | /** | ||
282 | * Lookup the session for the given peer. | ||
283 | * | ||
284 | * @param plugin the plugin | ||
285 | * @param peer peer's identity | ||
286 | * @return NULL if we have no session | ||
287 | */ | ||
288 | struct PeerSession * | ||
289 | find_session (struct Plugin *plugin, | ||
290 | const struct GNUNET_PeerIdentity *peer) | ||
291 | { | ||
292 | return GNUNET_CONTAINER_multihashmap_get (plugin->sessions, | ||
293 | &peer->hashPubKey); | ||
294 | } | ||
295 | |||
296 | |||
297 | /** | ||
298 | * Disconnect from a remote node. Clean up session if we have one for this peer | ||
299 | * | ||
300 | * @param cls closure for this call (should be handle to Plugin) | ||
301 | * @param target the peeridentity of the peer to disconnect | ||
302 | * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed | ||
303 | */ | ||
304 | static void | ||
305 | udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target) | ||
306 | { | ||
307 | struct Plugin *plugin = cls; | ||
308 | struct PeerSession *session; | ||
309 | |||
310 | session = find_session (plugin, target); | ||
311 | if (NULL == session) | ||
312 | return; | ||
313 | GNUNET_assert (GNUNET_OK == | ||
314 | GNUNET_CONTAINER_multihashmap_remove (plugin->sessions, | ||
315 | &target->hashPubKey, | ||
316 | session)); | ||
317 | (void) GNUNET_FRAGMENT_context_destroy (session->frag); | ||
318 | session->cont (session->cont_cls, target, GNUNET_SYSERR); | ||
319 | GNUNET_free (session); | ||
320 | } | ||
321 | |||
322 | |||
323 | /** | ||
324 | * Actually send out the message. | ||
325 | * | ||
326 | * @param plugin the plugin | ||
327 | * @param send_handle which handle to send message on | ||
328 | * @param target who should receive this message (ignored by UDP) | ||
329 | * @param msgbuf one or more GNUNET_MessageHeader(s) strung together | ||
330 | * @param msgbuf_size the size of the msgbuf to send | ||
331 | * @param priority how important is the message (ignored by UDP) | ||
332 | * @param timeout when should we time out (give up) if we can not transmit? | ||
333 | * @param addr the addr to send the message to, needs to be a sockaddr for us | ||
334 | * @param addrlen the len of addr | ||
335 | * @param cont continuation to call once the message has | ||
336 | * been transmitted (or if the transport is ready | ||
337 | * for the next transmission call; or if the | ||
338 | * peer disconnected...) | ||
339 | * @param cont_cls closure for cont | ||
340 | * @return the number of bytes written | ||
341 | */ | ||
342 | static ssize_t | ||
343 | udp_send (struct Plugin *plugin, | ||
344 | const struct sockaddr *sa, | ||
345 | const struct GNUNET_MessageHeader *msg) | ||
346 | { | ||
347 | ssize_t sent; | ||
348 | size_t slen; | ||
349 | |||
350 | switch (sa->sa_family) | ||
351 | { | ||
352 | case AF_INET: | ||
353 | sent = | ||
354 | GNUNET_NETWORK_socket_sendto (plugin->sockv4, | ||
355 | msg, | ||
356 | ntohs (msg->size), | ||
357 | sa, | ||
358 | slen = sizeof (struct sockaddr_in)); | ||
359 | break; | ||
360 | case AF_INET6: | ||
361 | sent = | ||
362 | GNUNET_NETWORK_socket_sendto (plugin->sockv6, | ||
363 | msg, | ||
364 | ntohs (msg->size), | ||
365 | sa, | ||
366 | slen = sizeof (struct sockaddr_in6)); | ||
367 | break; | ||
368 | default: | ||
369 | GNUNET_break (0); | ||
370 | return 0; | ||
371 | } | ||
372 | if (GNUNET_SYSERR == sent) | ||
373 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, | ||
374 | "sendto"); | ||
375 | #if DEBUG_UDP | ||
376 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
377 | "UDP transmited %u-byte message to %s (%d: %s)\n", | ||
378 | (unsigned int) ntohs (msg->size), | ||
379 | GNUNET_a2s (sa, slen), | ||
380 | (int) sent, | ||
381 | (sent < 0) ? STRERROR (errno) : "ok"); | ||
382 | #endif | ||
383 | return sent; | ||
384 | } | ||
385 | |||
386 | |||
387 | /** | ||
388 | * Function that is called with messages created by the fragmentation | ||
389 | * module. In the case of the 'proc' callback of the | ||
390 | * GNUNET_FRAGMENT_context_create function, this function must | ||
391 | * eventually call 'GNUNET_FRAGMENT_context_transmission_done'. | ||
392 | * | ||
393 | * @param cls closure, the 'struct PeerSession' | ||
394 | * @param msg the message that was created | ||
395 | */ | ||
396 | static void | ||
397 | send_fragment (void *cls, | ||
398 | const struct GNUNET_MessageHeader *msg) | ||
399 | { | ||
400 | struct PeerSession *session = cls; | ||
401 | |||
402 | udp_send (session->plugin, | ||
403 | session->sock_addr, | ||
404 | msg); | ||
405 | GNUNET_FRAGMENT_context_transmission_done (session->frag); | ||
406 | } | ||
407 | |||
408 | |||
409 | /** | ||
410 | * Function that can be used by the transport service to transmit | ||
411 | * a message using the plugin. | ||
412 | * | ||
413 | * @param cls closure | ||
414 | * @param target who should receive this message (ignored by UDP) | ||
415 | * @param msgbuf one or more GNUNET_MessageHeader(s) strung together | ||
416 | * @param msgbuf_size the size of the msgbuf to send | ||
417 | * @param priority how important is the message (ignored by UDP) | ||
418 | * @param timeout when should we time out (give up) if we can not transmit? | ||
419 | * @param session identifier used for this session (NULL for UDP) | ||
420 | * @param addr the addr to send the message to | ||
421 | * @param addrlen the len of addr | ||
422 | * @param force_address not used, we had better have an address to send to | ||
423 | * because we are stateless!! | ||
424 | * @param cont continuation to call once the message has | ||
425 | * been transmitted (or if the transport is ready | ||
426 | * for the next transmission call; or if the | ||
427 | * peer disconnected...) | ||
428 | * @param cont_cls closure for cont | ||
429 | * | ||
430 | * @return the number of bytes written (may return 0 and the message can | ||
431 | * still be transmitted later!) | ||
432 | */ | ||
433 | static ssize_t | ||
434 | udp_plugin_send (void *cls, | ||
435 | const struct GNUNET_PeerIdentity *target, | ||
436 | const char *msgbuf, | ||
437 | size_t msgbuf_size, | ||
438 | unsigned int priority, | ||
439 | struct GNUNET_TIME_Relative timeout, | ||
440 | struct Session *session, | ||
441 | const void *addr, | ||
442 | size_t addrlen, | ||
443 | int force_address, | ||
444 | GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls) | ||
445 | { | ||
446 | struct Plugin *plugin = cls; | ||
447 | struct PeerSession *peer_session; | ||
448 | const struct IPv4UdpAddress *t4; | ||
449 | const struct IPv6UdpAddress *t6; | ||
450 | struct sockaddr_in *v4; | ||
451 | struct sockaddr_in6 *v6; | ||
452 | size_t mlen = msgbuf_size + sizeof (struct UDPMessage); | ||
453 | char mbuf[mlen]; | ||
454 | struct UDPMessage *udp; | ||
455 | |||
456 | if (force_address == GNUNET_SYSERR) | ||
457 | return GNUNET_SYSERR; | ||
458 | GNUNET_assert (NULL == session); | ||
459 | if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
460 | { | ||
461 | GNUNET_break (0); | ||
462 | return GNUNET_SYSERR; | ||
463 | } | ||
464 | switch (addrlen) | ||
465 | { | ||
466 | case sizeof(struct IPv4UdpAddress): | ||
467 | t4 = addr; | ||
468 | peer_session = GNUNET_malloc (sizeof (struct PeerSession) + sizeof (struct sockaddr_in)); | ||
469 | v4 = (struct sockaddr_in*) &peer_session[1]; | ||
470 | v4->sin_family = AF_INET; | ||
471 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
472 | v4->sin_len = sizeof (struct sockaddr_in); | ||
473 | #endif | ||
474 | v4->sin_port = t4->u4_port; | ||
475 | v4->sin_addr.s_addr = t4->ipv4_addr; | ||
476 | break; | ||
477 | case sizeof(struct IPv6UdpAddress): | ||
478 | t6 = addr; | ||
479 | peer_session = GNUNET_malloc (sizeof (struct PeerSession) + sizeof (struct sockaddr_in6)); | ||
480 | v6 = (struct sockaddr_in6*) &peer_session[1]; | ||
481 | v6->sin6_family = AF_INET6; | ||
482 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
483 | v6->sin6_len = sizeof (struct sockaddr_in6); | ||
484 | #endif | ||
485 | v6->sin6_port = t6->u6_port; | ||
486 | v6->sin6_addr = t6->ipv6_addr; | ||
487 | break; | ||
488 | default: | ||
489 | /* Must have a valid address to send to */ | ||
490 | GNUNET_break_op(0); | ||
491 | return GNUNET_SYSERR; | ||
492 | } | ||
493 | udp = (struct UDPMessage*) mbuf; | ||
494 | udp->header.size = htons (mlen); | ||
495 | udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE); | ||
496 | udp->reserved = htonl (0); | ||
497 | udp->sender = *plugin->env->my_identity; | ||
498 | memcpy (&udp[1], msgbuf, msgbuf_size); | ||
499 | peer_session->target = *target; | ||
500 | peer_session->plugin = plugin; | ||
501 | peer_session->sock_addr = (const struct sockaddr*) &peer_session[1]; | ||
502 | peer_session->cont = cont; | ||
503 | peer_session->cont_cls = cont_cls; | ||
504 | if (mlen <= UDP_MTU) | ||
505 | { | ||
506 | mlen = udp_send (plugin, | ||
507 | peer_session->sock_addr, | ||
508 | &udp->header); | ||
509 | cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR); | ||
510 | GNUNET_free (peer_session); | ||
511 | } | ||
512 | else | ||
513 | { | ||
514 | GNUNET_assert (GNUNET_OK == | ||
515 | GNUNET_CONTAINER_multihashmap_put (plugin->sessions, | ||
516 | &target->hashPubKey, | ||
517 | peer_session, | ||
518 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
519 | peer_session->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats, | ||
520 | UDP_MTU, | ||
521 | NULL /* tracker; FIXME: add later to limit send rate... */, | ||
522 | GNUNET_TIME_UNIT_SECONDS /* expected delay for ACKs */, | ||
523 | &udp->header, | ||
524 | &send_fragment, | ||
525 | peer_session); | ||
526 | } | ||
527 | return mlen; | ||
528 | } | ||
529 | |||
530 | |||
531 | /** | ||
532 | * Closure for 'process_inbound_tokenized_messages' | ||
533 | */ | ||
534 | struct SourceInformation | ||
535 | { | ||
536 | /** | ||
537 | * Sender identity. | ||
538 | */ | ||
539 | struct GNUNET_PeerIdentity sender; | ||
540 | |||
541 | /** | ||
542 | * Source address. | ||
543 | */ | ||
544 | const void *arg; | ||
545 | |||
546 | /** | ||
547 | * Number of bytes in source address. | ||
548 | */ | ||
549 | size_t args; | ||
550 | }; | ||
551 | |||
552 | |||
553 | /** | ||
554 | * Message tokenizer has broken up an incomming message. Pass it on | ||
555 | * to the service. | ||
556 | * | ||
557 | * @param cls the 'struct Plugin' | ||
558 | * @param client the 'struct SourceInformation' | ||
559 | * @param hdr the actual message | ||
560 | */ | ||
561 | static void | ||
562 | process_inbound_tokenized_messages (void *cls, | ||
563 | void *client, | ||
564 | const struct GNUNET_MessageHeader *hdr) | ||
565 | { | ||
566 | struct Plugin *plugin = cls; | ||
567 | struct SourceInformation* si = client; | ||
568 | struct GNUNET_TRANSPORT_ATS_Information distance[2]; | ||
569 | |||
570 | /* setup ATS */ | ||
571 | distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE); | ||
572 | distance[0].value = htonl (1); | ||
573 | distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR); | ||
574 | distance[1].value = htonl (0); | ||
575 | |||
576 | plugin->env->receive (plugin->env->cls, | ||
577 | &si->sender, | ||
578 | hdr, | ||
579 | distance, 2, | ||
580 | NULL, | ||
581 | si->arg, si->args); | ||
582 | } | ||
583 | |||
584 | |||
585 | /** | ||
586 | * We've received a UDP Message. Process it (pass contents to main service). | ||
587 | * | ||
588 | * @param plugin plugin context | ||
589 | * @param msg the message | ||
590 | * @param sender_addr sender address | ||
591 | * @param sender_addr_len number of bytes in sender_addr | ||
592 | */ | ||
593 | static void | ||
594 | process_udp_message (struct Plugin *plugin, | ||
595 | const struct UDPMessage *msg, | ||
596 | const struct sockaddr *sender_addr, | ||
597 | socklen_t sender_addr_len) | ||
598 | { | ||
599 | struct SourceInformation si; | ||
600 | struct IPv4UdpAddress u4; | ||
601 | struct IPv6UdpAddress u6; | ||
602 | const void *arg; | ||
603 | size_t args; | ||
604 | |||
605 | if (0 != ntohl (msg->reserved)) | ||
606 | { | ||
607 | GNUNET_break_op (0); | ||
608 | return; | ||
609 | } | ||
610 | if (ntohs (msg->header.size) < sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage)) | ||
611 | { | ||
612 | GNUNET_break_op (0); | ||
613 | return; | ||
614 | } | ||
615 | |||
616 | /* convert address */ | ||
617 | switch (sender_addr->sa_family) | ||
618 | { | ||
619 | case AF_INET: | ||
620 | GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in)); | ||
621 | u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr; | ||
622 | u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port; | ||
623 | arg = &u4; | ||
624 | args = sizeof (u4); | ||
625 | break; | ||
626 | case AF_INET6: | ||
627 | GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6)); | ||
628 | u6.ipv6_addr = ((struct sockaddr_in6*) sender_addr)->sin6_addr; | ||
629 | u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port; | ||
630 | arg = &u6; | ||
631 | args = sizeof (u6); | ||
632 | break; | ||
633 | default: | ||
634 | GNUNET_break (0); | ||
635 | return; | ||
636 | } | ||
637 | #if DEBUG_UDP | ||
638 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
639 | "udp", | ||
640 | "Received message with %u bytes from peer `%s' at `%s'\n", | ||
641 | (unsigned int) ntohs (msg->header.size), | ||
642 | GNUNET_i2s (&msg->sender), | ||
643 | GNUNET_a2s (sender_addr, sender_addr_len)); | ||
644 | #endif | ||
645 | |||
646 | /* iterate over all embedded messages */ | ||
647 | si.sender = msg->sender; | ||
648 | si.arg = arg; | ||
649 | si.args = args; | ||
650 | GNUNET_SERVER_mst_receive (plugin->mst, | ||
651 | &si, | ||
652 | (const char*) &msg[1], | ||
653 | ntohs (msg->header.size) - sizeof (struct UDPMessage), | ||
654 | GNUNET_YES, | ||
655 | GNUNET_NO); | ||
656 | } | ||
657 | |||
658 | |||
659 | /** | ||
660 | * Process a defragmented message. | ||
661 | * | ||
662 | * @param cls the 'struct ReceiveContext' | ||
663 | * @param msg the message | ||
664 | */ | ||
665 | static void | ||
666 | fragment_msg_proc (void *cls, | ||
667 | const struct GNUNET_MessageHeader *msg) | ||
668 | { | ||
669 | struct ReceiveContext *rc = cls; | ||
670 | |||
671 | if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE) | ||
672 | { | ||
673 | GNUNET_break (0); | ||
674 | return; | ||
675 | } | ||
676 | if (ntohs (msg->size) < sizeof(struct UDPMessage)) | ||
677 | { | ||
678 | GNUNET_break (0); | ||
679 | return; | ||
680 | } | ||
681 | process_udp_message (rc->plugin, | ||
682 | (const struct UDPMessage*) msg, | ||
683 | rc->src_addr, | ||
684 | rc->addr_len); | ||
685 | } | ||
686 | |||
687 | |||
688 | /** | ||
689 | * Transmit an acknowledgement. | ||
690 | * | ||
691 | * @param cls the 'struct ReceiveContext' | ||
692 | * @param id message ID (unused) | ||
693 | * @param msg ack to transmit | ||
694 | */ | ||
695 | static void | ||
696 | ack_proc (void *cls, | ||
697 | uint32_t id, | ||
698 | const struct GNUNET_MessageHeader *msg) | ||
699 | { | ||
700 | struct ReceiveContext *rc = cls; | ||
701 | size_t msize = sizeof (struct UDPMessage) + ntohs (msg->size); | ||
702 | char buf[msize]; | ||
703 | struct UDPMessage *udp; | ||
704 | |||
705 | #if DEBUG_UDP | ||
706 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
707 | "udp", | ||
708 | "Sending ACK to `%s'\n", | ||
709 | GNUNET_a2s (rc->src_addr, | ||
710 | (rc->src_addr->sa_family == AF_INET) | ||
711 | ? sizeof (struct sockaddr_in) | ||
712 | : sizeof (struct sockaddr_in6))); | ||
713 | #endif | ||
714 | udp = (struct UDPMessage*) buf; | ||
715 | udp->header.size = htons ((uint16_t) msize); | ||
716 | udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK); | ||
717 | udp->reserved = htonl (0); | ||
718 | udp->sender = *rc->plugin->env->my_identity; | ||
719 | memcpy (&udp[1], msg, ntohs (msg->size)); | ||
720 | (void) udp_send (rc->plugin, | ||
721 | rc->src_addr, | ||
722 | &udp->header); | ||
723 | } | ||
724 | |||
725 | |||
726 | /** | ||
727 | * Closure for 'find_receive_context'. | ||
728 | */ | ||
729 | struct FindReceiveContext | ||
730 | { | ||
731 | /** | ||
732 | * Where to store the result. | ||
733 | */ | ||
734 | struct ReceiveContext *rc; | ||
735 | |||
736 | /** | ||
737 | * Address to find. | ||
738 | */ | ||
739 | const struct sockaddr *addr; | ||
740 | |||
741 | /** | ||
742 | * Number of bytes in 'addr'. | ||
743 | */ | ||
744 | socklen_t addr_len; | ||
745 | }; | ||
746 | |||
747 | |||
748 | /** | ||
749 | * Scan the heap for a receive context with the given address. | ||
750 | * | ||
751 | * @param cls the 'struct FindReceiveContext' | ||
752 | * @param node internal node of the heap | ||
753 | * @param element value stored at the node (a 'struct ReceiveContext') | ||
754 | * @param cost cost associated with the node | ||
755 | * @return GNUNET_YES if we should continue to iterate, | ||
756 | * GNUNET_NO if not. | ||
757 | */ | ||
758 | static int | ||
759 | find_receive_context (void *cls, | ||
760 | struct GNUNET_CONTAINER_HeapNode *node, | ||
761 | void *element, | ||
762 | GNUNET_CONTAINER_HeapCostType cost) | ||
763 | { | ||
764 | struct FindReceiveContext *frc = cls; | ||
765 | struct ReceiveContext *e = element; | ||
766 | |||
767 | if ( (frc->addr_len == e->addr_len) && | ||
768 | (0 == memcmp (frc->addr, | ||
769 | e->src_addr, | ||
770 | frc->addr_len) ) ) | ||
771 | { | ||
772 | frc->rc = e; | ||
773 | return GNUNET_NO; | ||
774 | } | ||
775 | return GNUNET_YES; | ||
776 | } | ||
777 | |||
778 | |||
779 | /** | ||
780 | * Read and process a message from the given socket. | ||
781 | * | ||
782 | * @param plugin the overall plugin | ||
783 | * @param rsock socket to read from | ||
784 | */ | ||
785 | static void | ||
786 | udp_read (struct Plugin *plugin, | ||
787 | struct GNUNET_NETWORK_Handle *rsock) | ||
788 | { | ||
789 | socklen_t fromlen; | ||
790 | char addr[32]; | ||
791 | char buf[65536]; | ||
792 | ssize_t ret; | ||
793 | const struct GNUNET_MessageHeader *msg; | ||
794 | const struct GNUNET_MessageHeader *ack; | ||
795 | struct PeerSession *peer_session; | ||
796 | const struct UDPMessage *udp; | ||
797 | struct ReceiveContext *rc; | ||
798 | struct GNUNET_TIME_Absolute now; | ||
799 | struct FindReceiveContext frc; | ||
800 | |||
801 | fromlen = sizeof (addr); | ||
802 | memset (&addr, 0, sizeof(addr)); | ||
803 | ret = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf), | ||
804 | (struct sockaddr *)&addr, &fromlen); | ||
805 | if (ret < sizeof (struct GNUNET_MessageHeader)) | ||
806 | { | ||
807 | GNUNET_break_op (0); | ||
808 | return; | ||
809 | } | ||
810 | #if DEBUG_UDP | ||
811 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
812 | "UDP received %u-byte message from `%s'\n", | ||
813 | (unsigned int) ret, | ||
814 | GNUNET_a2s ((const struct sockaddr*) addr, fromlen)); | ||
815 | #endif | ||
816 | msg = (const struct GNUNET_MessageHeader *) buf; | ||
817 | if (ret != ntohs (msg->size)) | ||
818 | { | ||
819 | GNUNET_break_op (0); | ||
820 | return; | ||
821 | } | ||
822 | switch (ntohs (msg->type)) | ||
823 | { | ||
824 | case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE: | ||
825 | if (ntohs (msg->size) < sizeof (struct UDPMessage)) | ||
826 | { | ||
827 | GNUNET_break_op (0); | ||
828 | return; | ||
829 | } | ||
830 | process_udp_message (plugin, | ||
831 | (const struct UDPMessage *) msg, | ||
832 | (const struct sockaddr*) addr, | ||
833 | fromlen); | ||
834 | return; | ||
835 | case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK: | ||
836 | if (ntohs (msg->size) < sizeof (struct UDPMessage) + sizeof (struct GNUNET_MessageHeader)) | ||
837 | { | ||
838 | GNUNET_break_op (0); | ||
839 | return; | ||
840 | } | ||
841 | udp = (const struct UDPMessage *) msg; | ||
842 | if (ntohl (udp->reserved) != 0) | ||
843 | { | ||
844 | GNUNET_break_op (0); | ||
845 | return; | ||
846 | } | ||
847 | ack = (const struct GNUNET_MessageHeader*) &udp[1]; | ||
848 | if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDPMessage)) | ||
849 | { | ||
850 | GNUNET_break_op (0); | ||
851 | return; | ||
852 | } | ||
853 | #if DEBUG_UDP | ||
854 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
855 | "UDP processes %u-byte acknowledgement from `%s' at `%s'\n", | ||
856 | (unsigned int) ntohs (msg->size), | ||
857 | GNUNET_i2s (&udp->sender), | ||
858 | GNUNET_a2s ((const struct sockaddr*) addr, fromlen)); | ||
859 | #endif | ||
860 | |||
861 | peer_session = find_session (plugin, &udp->sender); | ||
862 | if (NULL == peer_session) | ||
863 | { | ||
864 | #if DEBUG_UDP | ||
865 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
866 | "Session for ACK not found, dropping ACK!\n"); | ||
867 | #endif | ||
868 | return; | ||
869 | } | ||
870 | if (GNUNET_OK != | ||
871 | GNUNET_FRAGMENT_process_ack (peer_session->frag, | ||
872 | ack)) | ||
873 | return; | ||
874 | GNUNET_assert (GNUNET_OK == | ||
875 | GNUNET_CONTAINER_multihashmap_remove (plugin->sessions, | ||
876 | &udp->sender.hashPubKey, | ||
877 | peer_session)); | ||
878 | GNUNET_FRAGMENT_context_destroy (peer_session->frag); | ||
879 | peer_session->cont (peer_session->cont_cls, | ||
880 | &udp->sender, | ||
881 | GNUNET_OK); | ||
882 | GNUNET_free (peer_session); | ||
883 | return; | ||
884 | case GNUNET_MESSAGE_TYPE_FRAGMENT: | ||
885 | frc.rc = NULL; | ||
886 | frc.addr = (const struct sockaddr*) addr; | ||
887 | frc.addr_len = fromlen; | ||
888 | GNUNET_CONTAINER_heap_iterate (plugin->defrags, | ||
889 | &find_receive_context, | ||
890 | &frc); | ||
891 | now = GNUNET_TIME_absolute_get (); | ||
892 | rc = frc.rc; | ||
893 | if (rc == NULL) | ||
894 | { | ||
895 | /* need to create a new RC */ | ||
896 | rc = GNUNET_malloc (sizeof (struct ReceiveContext) + fromlen); | ||
897 | memcpy (&rc[1], addr, fromlen); | ||
898 | rc->src_addr = (const struct sockaddr*) &rc[1]; | ||
899 | rc->addr_len = fromlen; | ||
900 | rc->plugin = plugin; | ||
901 | rc->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats, | ||
902 | UDP_MTU, | ||
903 | UDP_MAX_MESSAGES_IN_DEFRAG, | ||
904 | rc, | ||
905 | &fragment_msg_proc, | ||
906 | &ack_proc); | ||
907 | rc->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrags, | ||
908 | rc, | ||
909 | (GNUNET_CONTAINER_HeapCostType) now.abs_value); | ||
910 | } | ||
911 | #if DEBUG_UDP | ||
912 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
913 | "UDP processes %u-byte fragment from `%s'\n", | ||
914 | (unsigned int) ntohs (msg->size), | ||
915 | GNUNET_a2s ((const struct sockaddr*) addr, fromlen)); | ||
916 | #endif | ||
917 | |||
918 | if (GNUNET_OK == | ||
919 | GNUNET_DEFRAGMENT_process_fragment (rc->defrag, | ||
920 | msg)) | ||
921 | { | ||
922 | /* keep this 'rc' from expiring */ | ||
923 | GNUNET_CONTAINER_heap_update_cost (plugin->defrags, | ||
924 | rc->hnode, | ||
925 | (GNUNET_CONTAINER_HeapCostType) now.abs_value); | ||
926 | } | ||
927 | if (GNUNET_CONTAINER_heap_get_size (plugin->defrags) > UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG) | ||
928 | { | ||
929 | /* remove 'rc' that was inactive the longest */ | ||
930 | rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags); | ||
931 | GNUNET_DEFRAGMENT_context_destroy (rc->defrag); | ||
932 | GNUNET_free (rc); | ||
933 | } | ||
934 | return; | ||
935 | default: | ||
936 | GNUNET_break_op (0); | ||
937 | return; | ||
938 | } | ||
939 | } | ||
940 | |||
941 | |||
942 | /** | ||
943 | * We have been notified that our writeset has something to read. We don't | ||
944 | * know which socket needs to be read, so we have to check each one | ||
945 | * Then reschedule this function to be called again once more is available. | ||
946 | * | ||
947 | * @param cls the plugin handle | ||
948 | * @param tc the scheduling context (for rescheduling this function again) | ||
949 | */ | ||
950 | static void | ||
951 | udp_plugin_select (void *cls, | ||
952 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
953 | { | ||
954 | struct Plugin *plugin = cls; | ||
955 | |||
956 | plugin->select_task = GNUNET_SCHEDULER_NO_TASK; | ||
957 | if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
958 | return; | ||
959 | if (GNUNET_NETWORK_fdset_isset (tc->read_ready, | ||
960 | plugin->sockv4)) | ||
961 | udp_read (plugin, plugin->sockv4); | ||
962 | if (GNUNET_NETWORK_fdset_isset (tc->read_ready, | ||
963 | plugin->sockv6)) | ||
964 | udp_read (plugin, plugin->sockv6); | ||
965 | plugin->select_task = | ||
966 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
967 | GNUNET_SCHEDULER_NO_TASK, | ||
968 | GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs, | ||
969 | NULL, &udp_plugin_select, plugin); | ||
970 | |||
971 | } | ||
972 | |||
973 | |||
974 | /** | ||
975 | * Check if the given port is plausible (must be either our listen | ||
976 | * port or our advertised port). If it is neither, we return | ||
977 | * GNUNET_SYSERR. | ||
978 | * | ||
979 | * @param plugin global variables | ||
980 | * @param in_port port number to check | ||
981 | * @return GNUNET_OK if port is either open_port or adv_port | ||
982 | */ | ||
983 | static int | ||
984 | check_port (struct Plugin *plugin, uint16_t in_port) | ||
985 | { | ||
986 | if ( (in_port == plugin->port) || | ||
987 | (in_port == plugin->aport) ) | ||
988 | return GNUNET_OK; | ||
989 | return GNUNET_SYSERR; | ||
990 | } | ||
991 | |||
992 | |||
993 | /** | ||
994 | * Function that will be called to check if a binary address for this | ||
995 | * plugin is well-formed and corresponds to an address for THIS peer | ||
996 | * (as per our configuration). Naturally, if absolutely necessary, | ||
997 | * plugins can be a bit conservative in their answer, but in general | ||
998 | * plugins should make sure that the address does not redirect | ||
999 | * traffic to a 3rd party that might try to man-in-the-middle our | ||
1000 | * traffic. | ||
1001 | * | ||
1002 | * @param cls closure, should be our handle to the Plugin | ||
1003 | * @param addr pointer to the address | ||
1004 | * @param addrlen length of addr | ||
1005 | * @return GNUNET_OK if this is a plausible address for this peer | ||
1006 | * and transport, GNUNET_SYSERR if not | ||
1007 | * | ||
1008 | */ | ||
1009 | static int | ||
1010 | udp_plugin_check_address (void *cls, | ||
1011 | const void *addr, | ||
1012 | size_t addrlen) | ||
1013 | { | ||
1014 | struct Plugin *plugin = cls; | ||
1015 | struct IPv4UdpAddress *v4; | ||
1016 | struct IPv6UdpAddress *v6; | ||
1017 | |||
1018 | if ((addrlen != sizeof (struct IPv4UdpAddress)) && | ||
1019 | (addrlen != sizeof (struct IPv6UdpAddress))) | ||
1020 | { | ||
1021 | GNUNET_break_op (0); | ||
1022 | return GNUNET_SYSERR; | ||
1023 | } | ||
1024 | if (addrlen == sizeof (struct IPv4UdpAddress)) | ||
1025 | { | ||
1026 | v4 = (struct IPv4UdpAddress *) addr; | ||
1027 | if (GNUNET_OK != | ||
1028 | check_port (plugin, ntohs (v4->u4_port))) | ||
1029 | return GNUNET_SYSERR; | ||
1030 | if (GNUNET_OK != | ||
1031 | GNUNET_NAT_test_address (plugin->nat, | ||
1032 | &v4->ipv4_addr, sizeof (struct in_addr))) | ||
1033 | return GNUNET_SYSERR; | ||
1034 | } | ||
1035 | else | ||
1036 | { | ||
1037 | v6 = (struct IPv6UdpAddress *) addr; | ||
1038 | if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr)) | ||
1039 | { | ||
1040 | GNUNET_break_op (0); | ||
1041 | return GNUNET_SYSERR; | ||
1042 | } | ||
1043 | if (GNUNET_OK != | ||
1044 | check_port (plugin, ntohs (v6->u6_port))) | ||
1045 | return GNUNET_SYSERR; | ||
1046 | if (GNUNET_OK != | ||
1047 | GNUNET_NAT_test_address (plugin->nat, | ||
1048 | &v6->ipv6_addr, sizeof (struct in6_addr))) | ||
1049 | return GNUNET_SYSERR; | ||
1050 | } | ||
1051 | return GNUNET_OK; | ||
1052 | } | ||
1053 | |||
1054 | |||
1055 | /** | ||
1056 | * Function called for a quick conversion of the binary address to | ||
1057 | * a numeric address. Note that the caller must not free the | ||
1058 | * address and that the next call to this function is allowed | ||
1059 | * to override the address again. | ||
1060 | * | ||
1061 | * @param cls closure | ||
1062 | * @param addr binary address | ||
1063 | * @param addrlen length of the address | ||
1064 | * @return string representing the same address | ||
1065 | */ | ||
1066 | static const char* | ||
1067 | udp_address_to_string (void *cls, | ||
1068 | const void *addr, | ||
1069 | size_t addrlen) | ||
1070 | { | ||
1071 | static char rbuf[INET6_ADDRSTRLEN + 10]; | ||
1072 | char buf[INET6_ADDRSTRLEN]; | ||
1073 | const void *sb; | ||
1074 | struct in_addr a4; | ||
1075 | struct in6_addr a6; | ||
1076 | const struct IPv4UdpAddress *t4; | ||
1077 | const struct IPv6UdpAddress *t6; | ||
1078 | int af; | ||
1079 | uint16_t port; | ||
1080 | |||
1081 | if (addrlen == sizeof (struct IPv6UdpAddress)) | ||
1082 | { | ||
1083 | t6 = addr; | ||
1084 | af = AF_INET6; | ||
1085 | port = ntohs (t6->u6_port); | ||
1086 | memcpy (&a6, &t6->ipv6_addr, sizeof (a6)); | ||
1087 | sb = &a6; | ||
1088 | } | ||
1089 | else if (addrlen == sizeof (struct IPv4UdpAddress)) | ||
1090 | { | ||
1091 | t4 = addr; | ||
1092 | af = AF_INET; | ||
1093 | port = ntohs (t4->u4_port); | ||
1094 | memcpy (&a4, &t4->ipv4_addr, sizeof (a4)); | ||
1095 | sb = &a4; | ||
1096 | } | ||
1097 | else | ||
1098 | { | ||
1099 | GNUNET_break_op (0); | ||
1100 | return NULL; | ||
1101 | } | ||
1102 | inet_ntop (af, sb, buf, INET6_ADDRSTRLEN); | ||
1103 | GNUNET_snprintf (rbuf, | ||
1104 | sizeof (rbuf), | ||
1105 | "%s:%u", | ||
1106 | buf, | ||
1107 | port); | ||
1108 | return rbuf; | ||
1109 | } | ||
1110 | |||
1111 | |||
1112 | /** | ||
1113 | * Closure for 'append_port'. | ||
1114 | */ | ||
1115 | struct PrettyPrinterContext | ||
1116 | { | ||
1117 | /** | ||
1118 | * Function to call with the result. | ||
1119 | */ | ||
1120 | GNUNET_TRANSPORT_AddressStringCallback asc; | ||
1121 | |||
1122 | /** | ||
1123 | * Clsoure for 'asc'. | ||
1124 | */ | ||
1125 | void *asc_cls; | ||
1126 | |||
1127 | /** | ||
1128 | * Port to add after the IP address. | ||
1129 | */ | ||
1130 | uint16_t port; | ||
1131 | }; | ||
1132 | |||
1133 | |||
1134 | /** | ||
1135 | * Append our port and forward the result. | ||
1136 | * | ||
1137 | * @param cls a 'struct PrettyPrinterContext' | ||
1138 | * @param hostname result from DNS resolver | ||
1139 | */ | ||
1140 | static void | ||
1141 | append_port (void *cls, const char *hostname) | ||
1142 | { | ||
1143 | struct PrettyPrinterContext *ppc = cls; | ||
1144 | char *ret; | ||
1145 | |||
1146 | if (hostname == NULL) | ||
1147 | { | ||
1148 | ppc->asc (NULL, ret); | ||
1149 | GNUNET_free (ppc); | ||
1150 | return; | ||
1151 | } | ||
1152 | GNUNET_asprintf (&ret, | ||
1153 | "%s:%d", | ||
1154 | hostname, | ||
1155 | ppc->port); | ||
1156 | ppc->asc (ppc->asc_cls, ret); | ||
1157 | GNUNET_free (ret); | ||
1158 | } | ||
1159 | |||
1160 | |||
1161 | /** | ||
1162 | * Convert the transports address to a nice, human-readable | ||
1163 | * format. | ||
1164 | * | ||
1165 | * @param cls closure | ||
1166 | * @param type name of the transport that generated the address | ||
1167 | * @param addr one of the addresses of the host, NULL for the last address | ||
1168 | * the specific address format depends on the transport | ||
1169 | * @param addrlen length of the address | ||
1170 | * @param numeric should (IP) addresses be displayed in numeric form? | ||
1171 | * @param timeout after how long should we give up? | ||
1172 | * @param asc function to call on each string | ||
1173 | * @param asc_cls closure for asc | ||
1174 | */ | ||
1175 | static void | ||
1176 | udp_plugin_address_pretty_printer (void *cls, | ||
1177 | const char *type, | ||
1178 | const void *addr, | ||
1179 | size_t addrlen, | ||
1180 | int numeric, | ||
1181 | struct GNUNET_TIME_Relative timeout, | ||
1182 | GNUNET_TRANSPORT_AddressStringCallback asc, | ||
1183 | void *asc_cls) | ||
1184 | { | ||
1185 | struct PrettyPrinterContext *ppc; | ||
1186 | const void *sb; | ||
1187 | size_t sbs; | ||
1188 | struct sockaddr_in a4; | ||
1189 | struct sockaddr_in6 a6; | ||
1190 | const struct IPv4UdpAddress *u4; | ||
1191 | const struct IPv6UdpAddress *u6; | ||
1192 | uint16_t port; | ||
1193 | |||
1194 | if (addrlen == sizeof (struct IPv6UdpAddress)) | ||
1195 | { | ||
1196 | u6 = addr; | ||
1197 | memset (&a6, 0, sizeof (a6)); | ||
1198 | a6.sin6_family = AF_INET6; | ||
1199 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
1200 | a6.sin6_len = sizeof (a6); | ||
1201 | #endif | ||
1202 | a6.sin6_port = u6->u6_port; | ||
1203 | memcpy (&a6.sin6_addr, | ||
1204 | &u6->ipv6_addr, | ||
1205 | sizeof (struct in6_addr)); | ||
1206 | port = ntohs (u6->u6_port); | ||
1207 | sb = &a6; | ||
1208 | sbs = sizeof (a6); | ||
1209 | } | ||
1210 | else if (addrlen == sizeof (struct IPv4UdpAddress)) | ||
1211 | { | ||
1212 | u4 = addr; | ||
1213 | memset (&a4, 0, sizeof (a4)); | ||
1214 | a4.sin_family = AF_INET; | ||
1215 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
1216 | a4.sin_len = sizeof (a4); | ||
1217 | #endif | ||
1218 | a4.sin_port = u4->u4_port; | ||
1219 | a4.sin_addr.s_addr = u4->ipv4_addr; | ||
1220 | port = ntohs (u4->u4_port); | ||
1221 | sb = &a4; | ||
1222 | sbs = sizeof (a4); | ||
1223 | } | ||
1224 | else | ||
1225 | { | ||
1226 | /* invalid address */ | ||
1227 | GNUNET_break_op (0); | ||
1228 | asc (asc_cls, NULL); | ||
1229 | return; | ||
1230 | } | ||
1231 | ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext)); | ||
1232 | ppc->asc = asc; | ||
1233 | ppc->asc_cls = asc_cls; | ||
1234 | ppc->port = port; | ||
1235 | GNUNET_RESOLVER_hostname_get (sb, | ||
1236 | sbs, | ||
1237 | !numeric, timeout, | ||
1238 | &append_port, ppc); | ||
1239 | } | ||
1240 | |||
1241 | |||
1242 | /** | ||
1243 | * Our external IP address/port mapping has changed. | ||
1244 | * | ||
1245 | * @param cls closure, the 'struct LocalAddrList' | ||
1246 | * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean | ||
1247 | * the previous (now invalid) one | ||
1248 | * @param addr either the previous or the new public IP address | ||
1249 | * @param addrlen actual lenght of the address | ||
1250 | */ | ||
1251 | static void | ||
1252 | udp_nat_port_map_callback (void *cls, | ||
1253 | int add_remove, | ||
1254 | const struct sockaddr *addr, | ||
1255 | socklen_t addrlen) | ||
1256 | { | ||
1257 | struct Plugin *plugin = cls; | ||
1258 | struct IPv4UdpAddress u4; | ||
1259 | struct IPv6UdpAddress u6; | ||
1260 | void *arg; | ||
1261 | size_t args; | ||
1262 | |||
1263 | /* convert 'addr' to our internal format */ | ||
1264 | switch (addr->sa_family) | ||
1265 | { | ||
1266 | case AF_INET: | ||
1267 | GNUNET_assert (addrlen == sizeof (struct sockaddr_in)); | ||
1268 | u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr; | ||
1269 | u4.u4_port = ((struct sockaddr_in *) addr)->sin_port; | ||
1270 | arg = &u4; | ||
1271 | args = sizeof (u4); | ||
1272 | break; | ||
1273 | case AF_INET6: | ||
1274 | GNUNET_assert (addrlen == sizeof (struct sockaddr_in6)); | ||
1275 | memcpy (&u6.ipv6_addr, | ||
1276 | &((struct sockaddr_in6 *) addr)->sin6_addr, | ||
1277 | sizeof (struct in6_addr)); | ||
1278 | u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port; | ||
1279 | arg = &u6; | ||
1280 | args = sizeof (u6); | ||
1281 | break; | ||
1282 | default: | ||
1283 | GNUNET_break (0); | ||
1284 | return; | ||
1285 | } | ||
1286 | /* modify our published address list */ | ||
1287 | plugin->env->notify_address (plugin->env->cls, | ||
1288 | add_remove, | ||
1289 | arg, args); | ||
1290 | } | ||
1291 | |||
1292 | |||
1293 | /** | ||
1294 | * The exported method. Makes the core api available via a global and | ||
1295 | * returns the udp transport API. | ||
1296 | * | ||
1297 | * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment' | ||
1298 | * @return our 'struct GNUNET_TRANSPORT_PluginFunctions' | ||
1299 | */ | ||
1300 | void * | ||
1301 | libgnunet_plugin_transport_udp_init (void *cls) | ||
1302 | { | ||
1303 | struct GNUNET_TRANSPORT_PluginEnvironment *env = cls; | ||
1304 | unsigned long long port; | ||
1305 | unsigned long long aport; | ||
1306 | struct GNUNET_TRANSPORT_PluginFunctions *api; | ||
1307 | struct Plugin *plugin; | ||
1308 | int sockets_created; | ||
1309 | struct sockaddr_in serverAddrv4; | ||
1310 | struct sockaddr_in6 serverAddrv6; | ||
1311 | struct sockaddr *serverAddr; | ||
1312 | struct sockaddr *addrs[2]; | ||
1313 | socklen_t addrlens[2]; | ||
1314 | socklen_t addrlen; | ||
1315 | unsigned int tries; | ||
1316 | |||
1317 | if (GNUNET_OK != | ||
1318 | GNUNET_CONFIGURATION_get_value_number (env->cfg, | ||
1319 | "transport-udp", | ||
1320 | "PORT", | ||
1321 | &port)) | ||
1322 | port = 2086; | ||
1323 | if (GNUNET_OK != | ||
1324 | GNUNET_CONFIGURATION_get_value_number (env->cfg, | ||
1325 | "transport-udp", | ||
1326 | "ADVERTISED_PORT", | ||
1327 | &aport)) | ||
1328 | aport = port; | ||
1329 | if (port > 65535) | ||
1330 | { | ||
1331 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1332 | _("Given `%s' option is out of range: %llu > %u\n"), | ||
1333 | "PORT", | ||
1334 | port, | ||
1335 | 65535); | ||
1336 | return NULL; | ||
1337 | } | ||
1338 | memset (&serverAddrv6, 0, sizeof (serverAddrv6)); | ||
1339 | memset (&serverAddrv4, 0, sizeof (serverAddrv4)); | ||
1340 | |||
1341 | plugin = GNUNET_malloc (sizeof (struct Plugin)); | ||
1342 | plugin->port = port; | ||
1343 | plugin->aport = aport; | ||
1344 | plugin->env = env; | ||
1345 | api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions)); | ||
1346 | api->cls = plugin; | ||
1347 | |||
1348 | api->send = &udp_plugin_send; | ||
1349 | api->disconnect = &udp_disconnect; | ||
1350 | api->address_pretty_printer = &udp_plugin_address_pretty_printer; | ||
1351 | api->address_to_string = &udp_address_to_string; | ||
1352 | api->check_address = &udp_plugin_check_address; | ||
1353 | |||
1354 | if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg, | ||
1355 | "transport-udp", | ||
1356 | "BINDTO", | ||
1357 | &plugin->bind4_address)) | ||
1358 | { | ||
1359 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
1360 | "Binding udp plugin to specific address: `%s'\n", | ||
1361 | plugin->bind4_address); | ||
1362 | if (1 != inet_pton(AF_INET, | ||
1363 | plugin->bind4_address, | ||
1364 | &serverAddrv4.sin_addr)) | ||
1365 | { | ||
1366 | GNUNET_free (plugin->bind4_address); | ||
1367 | GNUNET_free (plugin); | ||
1368 | return NULL; | ||
1369 | } | ||
1370 | } | ||
1371 | |||
1372 | if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string(env->cfg, | ||
1373 | "transport-udp", | ||
1374 | "BINDTO6", | ||
1375 | &plugin->bind6_address)) | ||
1376 | { | ||
1377 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
1378 | "Binding udp plugin to specific address: `%s'\n", | ||
1379 | plugin->bind6_address); | ||
1380 | if (1 != inet_pton(AF_INET6, | ||
1381 | plugin->bind6_address, | ||
1382 | &serverAddrv6.sin6_addr)) | ||
1383 | { | ||
1384 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
1385 | _("Invalid IPv6 address: `%s'\n"), | ||
1386 | plugin->bind6_address); | ||
1387 | GNUNET_free_non_null (plugin->bind4_address); | ||
1388 | GNUNET_free (plugin->bind6_address); | ||
1389 | GNUNET_free (plugin); | ||
1390 | return NULL; | ||
1391 | } | ||
1392 | } | ||
1393 | |||
1394 | plugin->defrags = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); | ||
1395 | plugin->sessions = GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG * 2); | ||
1396 | sockets_created = 0; | ||
1397 | if ( (GNUNET_YES != | ||
1398 | GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, | ||
1399 | "nat", | ||
1400 | "DISABLEV6"))) | ||
1401 | { | ||
1402 | plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0); | ||
1403 | if (NULL == plugin->sockv6) | ||
1404 | { | ||
1405 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); | ||
1406 | } | ||
1407 | else | ||
1408 | { | ||
1409 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
1410 | serverAddrv6.sin6_len = sizeof (serverAddrv6); | ||
1411 | #endif | ||
1412 | serverAddrv6.sin6_family = AF_INET6; | ||
1413 | serverAddrv6.sin6_addr = in6addr_any; | ||
1414 | serverAddrv6.sin6_port = htons (plugin->port); | ||
1415 | addrlen = sizeof (serverAddrv6); | ||
1416 | serverAddr = (struct sockaddr *) &serverAddrv6; | ||
1417 | #if DEBUG_UDP | ||
1418 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1419 | "Binding to IPv6 port %d\n", | ||
1420 | ntohs(serverAddrv6.sin6_port)); | ||
1421 | #endif | ||
1422 | tries = 0; | ||
1423 | while (GNUNET_NETWORK_socket_bind (plugin->sockv6, | ||
1424 | serverAddr, addrlen) != | ||
1425 | GNUNET_OK) | ||
1426 | { | ||
1427 | serverAddrv6.sin6_port | ||
1428 | = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */ | ||
1429 | #if DEBUG_UDP | ||
1430 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1431 | "IPv6 Binding failed, trying new port %d\n", | ||
1432 | ntohs(serverAddrv6.sin6_port)); | ||
1433 | #endif | ||
1434 | tries++; | ||
1435 | if (tries > 10) | ||
1436 | { | ||
1437 | GNUNET_NETWORK_socket_close (plugin->sockv6); | ||
1438 | plugin->sockv6 = NULL; | ||
1439 | break; | ||
1440 | } | ||
1441 | } | ||
1442 | if (plugin->sockv6 != NULL) | ||
1443 | { | ||
1444 | addrs[sockets_created] = (struct sockaddr*) &serverAddrv6; | ||
1445 | addrlens[sockets_created] = sizeof (serverAddrv6); | ||
1446 | sockets_created++; | ||
1447 | } | ||
1448 | } | ||
1449 | } | ||
1450 | |||
1451 | plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, | ||
1452 | plugin); | ||
1453 | plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0); | ||
1454 | if (NULL == plugin->sockv4) | ||
1455 | { | ||
1456 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket"); | ||
1457 | } | ||
1458 | else | ||
1459 | { | ||
1460 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
1461 | serverAddrv4.sin_len = sizeof (serverAddrv4); | ||
1462 | #endif | ||
1463 | serverAddrv4.sin_family = AF_INET; | ||
1464 | serverAddrv4.sin_addr.s_addr = INADDR_ANY; | ||
1465 | serverAddrv4.sin_port = htons (plugin->port); | ||
1466 | addrlen = sizeof (serverAddrv4); | ||
1467 | serverAddr = (struct sockaddr *) &serverAddrv4; | ||
1468 | #if DEBUG_UDP | ||
1469 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1470 | "Binding to IPv4 port %d\n", | ||
1471 | ntohs(serverAddrv4.sin_port)); | ||
1472 | #endif | ||
1473 | tries = 0; | ||
1474 | while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) != | ||
1475 | GNUNET_OK) | ||
1476 | { | ||
1477 | serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */ | ||
1478 | #if DEBUG_UDP | ||
1479 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1480 | "IPv4 Binding failed, trying new port %d\n", | ||
1481 | ntohs(serverAddrv4.sin_port)); | ||
1482 | #endif | ||
1483 | tries++; | ||
1484 | if (tries > 10) | ||
1485 | { | ||
1486 | GNUNET_NETWORK_socket_close (plugin->sockv4); | ||
1487 | plugin->sockv4 = NULL; | ||
1488 | break; | ||
1489 | } | ||
1490 | } | ||
1491 | if (plugin->sockv4 != NULL) | ||
1492 | { | ||
1493 | addrs[sockets_created] = (struct sockaddr*) &serverAddrv4; | ||
1494 | addrlens[sockets_created] = sizeof (serverAddrv4); | ||
1495 | sockets_created++; | ||
1496 | } | ||
1497 | } | ||
1498 | |||
1499 | plugin->rs = GNUNET_NETWORK_fdset_create (); | ||
1500 | GNUNET_NETWORK_fdset_zero (plugin->rs); | ||
1501 | if (NULL != plugin->sockv4) | ||
1502 | GNUNET_NETWORK_fdset_set (plugin->rs, | ||
1503 | plugin->sockv4); | ||
1504 | if (NULL != plugin->sockv6) | ||
1505 | GNUNET_NETWORK_fdset_set (plugin->rs, | ||
1506 | plugin->sockv6); | ||
1507 | |||
1508 | plugin->select_task = | ||
1509 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
1510 | GNUNET_SCHEDULER_NO_TASK, | ||
1511 | GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs, | ||
1512 | NULL, &udp_plugin_select, plugin); | ||
1513 | if (sockets_created == 0) | ||
1514 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1515 | _("Failed to open UDP sockets\n")); | ||
1516 | plugin->nat = GNUNET_NAT_register (env->cfg, | ||
1517 | GNUNET_NO, | ||
1518 | port, | ||
1519 | sockets_created, | ||
1520 | (const struct sockaddr**) addrs, addrlens, | ||
1521 | &udp_nat_port_map_callback, | ||
1522 | NULL, | ||
1523 | plugin); | ||
1524 | return api; | ||
1525 | } | ||
1526 | |||
1527 | |||
1528 | /** | ||
1529 | * Destroy a session, plugin is being unloaded. | ||
1530 | * | ||
1531 | * @param cls unused | ||
1532 | * @param key hash of public key of target peer | ||
1533 | * @param value a 'struct PeerSession*' to clean up | ||
1534 | * @return GNUNET_OK (continue to iterate) | ||
1535 | */ | ||
1536 | static int | ||
1537 | destroy_session (void *cls, | ||
1538 | const GNUNET_HashCode *key, | ||
1539 | void *value) | ||
1540 | { | ||
1541 | struct PeerSession *peer_session = value; | ||
1542 | |||
1543 | GNUNET_FRAGMENT_context_destroy (peer_session->frag); | ||
1544 | GNUNET_free (peer_session); | ||
1545 | return GNUNET_OK; | ||
1546 | } | ||
1547 | |||
1548 | |||
1549 | /** | ||
1550 | * Shutdown the plugin. | ||
1551 | * | ||
1552 | * @param cls our 'struct GNUNET_TRANSPORT_PluginFunctions' | ||
1553 | * @return NULL | ||
1554 | */ | ||
1555 | void * | ||
1556 | libgnunet_plugin_transport_udp_done (void *cls) | ||
1557 | { | ||
1558 | struct GNUNET_TRANSPORT_PluginFunctions *api = cls; | ||
1559 | struct Plugin *plugin = api->cls; | ||
1560 | struct ReceiveContext *rc; | ||
1561 | |||
1562 | /* FIXME: clean up heap and hashmap */ | ||
1563 | GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, | ||
1564 | &destroy_session, | ||
1565 | NULL); | ||
1566 | GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions); | ||
1567 | plugin->sessions = NULL; | ||
1568 | while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags))) | ||
1569 | { | ||
1570 | GNUNET_DEFRAGMENT_context_destroy (rc->defrag); | ||
1571 | GNUNET_free (rc); | ||
1572 | } | ||
1573 | GNUNET_CONTAINER_heap_destroy (plugin->defrags); | ||
1574 | |||
1575 | if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK) | ||
1576 | { | ||
1577 | GNUNET_SCHEDULER_cancel (plugin->select_task); | ||
1578 | plugin->select_task = GNUNET_SCHEDULER_NO_TASK; | ||
1579 | } | ||
1580 | if (plugin->sockv4 != NULL) | ||
1581 | { | ||
1582 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4)); | ||
1583 | plugin->sockv4 = NULL; | ||
1584 | } | ||
1585 | if (plugin->sockv6 != NULL) | ||
1586 | { | ||
1587 | GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6)); | ||
1588 | plugin->sockv6 = NULL; | ||
1589 | } | ||
1590 | GNUNET_SERVER_mst_destroy (plugin->mst); | ||
1591 | GNUNET_NETWORK_fdset_destroy (plugin->rs); | ||
1592 | GNUNET_NAT_unregister (plugin->nat); | ||
1593 | plugin->nat = NULL; | ||
1594 | GNUNET_free (plugin); | ||
1595 | GNUNET_free (api); | ||
1596 | return NULL; | ||
1597 | } | ||
1598 | |||
1599 | /* end of plugin_transport_udp.c */ | ||