diff options
Diffstat (limited to 'src/mesh/gnunet-service-mesh.c')
-rw-r--r-- | src/mesh/gnunet-service-mesh.c | 5875 |
1 files changed, 44 insertions, 5831 deletions
diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c index d63576b4b..d187c93af 100644 --- a/src/mesh/gnunet-service-mesh.c +++ b/src/mesh/gnunet-service-mesh.c | |||
@@ -20,5696 +20,74 @@ | |||
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file mesh/gnunet-service-mesh.c | 22 | * @file mesh/gnunet-service-mesh.c |
23 | * @brief GNUnet MESH service | 23 | * @brief GNUnet MESH service with encryption |
24 | * @author Bartlomiej Polot | 24 | * @author Bartlomiej Polot |
25 | * | 25 | * |
26 | * FIXME in progress: | 26 | * FIXME in progress: |
27 | * - when sending in-order buffered data, wait for client ACKs | 27 | * - rekey - reliability interaction |
28 | * - channel retransmit timing | ||
28 | * | 29 | * |
29 | * TODO: | 30 | * TODO: |
30 | * - relay corking down to core | 31 | * - relay corking down to core |
31 | * - set ttl relative to path length | 32 | * - set ttl relative to path length |
32 | * - add signatures | ||
33 | * - add encryption | ||
34 | * TODO END | 33 | * TODO END |
34 | * | ||
35 | * Dictionary: | ||
36 | * - peer: other mesh instance. If there is direct connection it's a neighbor. | ||
37 | * - tunnel: encrypted connection to a peer, neighbor or not. | ||
38 | * - channel: connection between two clients, on the same or different peers. | ||
39 | * have properties like reliability. | ||
40 | * - path: series of directly connected peer from one peer to another. | ||
41 | * - connection: path which is being used in a tunnel. | ||
35 | */ | 42 | */ |
36 | 43 | ||
37 | #include "platform.h" | 44 | #include "platform.h" |
45 | #include "gnunet_util_lib.h" | ||
38 | #include "mesh.h" | 46 | #include "mesh.h" |
39 | #include "mesh_protocol.h" | ||
40 | #include "mesh_path.h" | ||
41 | #include "block_mesh.h" | ||
42 | #include "gnunet_dht_service.h" | ||
43 | #include "gnunet_statistics_service.h" | 47 | #include "gnunet_statistics_service.h" |
44 | 48 | ||
45 | #define MESH_BLOOM_SIZE 128 | 49 | #include "gnunet-service-mesh_local.h" |
46 | 50 | #include "gnunet-service-mesh_channel.h" | |
47 | #define MESH_DEBUG_DHT GNUNET_NO | 51 | #include "gnunet-service-mesh_connection.h" |
48 | #define MESH_DEBUG_CONNECTION GNUNET_NO | 52 | #include "gnunet-service-mesh_tunnel.h" |
49 | #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO | 53 | #include "gnunet-service-mesh_dht.h" |
50 | 54 | #include "gnunet-service-mesh_peer.h" | |
51 | #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\ | ||
52 | GNUNET_TIME_UNIT_MINUTES,\ | ||
53 | 10) | ||
54 | #define MESH_RETRANSMIT_TIME GNUNET_TIME_UNIT_SECONDS | ||
55 | #define MESH_RETRANSMIT_MARGIN 4 | ||
56 | |||
57 | #if MESH_DEBUG_CONNECTION | ||
58 | #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) | ||
59 | #else | ||
60 | #define DEBUG_CONN(...) | ||
61 | #endif | ||
62 | |||
63 | #if MESH_DEBUG_DHT | ||
64 | #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__) | ||
65 | #else | ||
66 | #define DEBUG_DHT(...) | ||
67 | #endif | ||
68 | |||
69 | #if MESH_DEBUG_TIMING | ||
70 | #include <time.h> | ||
71 | double __sum; | ||
72 | uint64_t __count; | ||
73 | struct timespec __mesh_start; | ||
74 | struct timespec __mesh_end; | ||
75 | #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start)) | ||
76 | #define INTERVAL_END \ | ||
77 | do {\ | ||
78 | clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\ | ||
79 | double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\ | ||
80 | if (__diff < 0) __diff += 1000000000;\ | ||
81 | __sum += __diff;\ | ||
82 | __count++;\ | ||
83 | } while (0) | ||
84 | #define INTERVAL_SHOW \ | ||
85 | if (0 < __count)\ | ||
86 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count) | ||
87 | #else | ||
88 | #define INTERVAL_START | ||
89 | #define INTERVAL_END | ||
90 | #define INTERVAL_SHOW | ||
91 | #endif | ||
92 | |||
93 | /******************************************************************************/ | ||
94 | /************************ DATA STRUCTURES ****************************/ | ||
95 | /******************************************************************************/ | ||
96 | |||
97 | /** FWD declaration */ | ||
98 | struct MeshPeer; | ||
99 | struct MeshClient; | ||
100 | |||
101 | |||
102 | /** | ||
103 | * Struct containing info about a queued transmission to this peer | ||
104 | */ | ||
105 | struct MeshPeerQueue | ||
106 | { | ||
107 | /** | ||
108 | * DLL next | ||
109 | */ | ||
110 | struct MeshPeerQueue *next; | ||
111 | |||
112 | /** | ||
113 | * DLL previous | ||
114 | */ | ||
115 | struct MeshPeerQueue *prev; | ||
116 | |||
117 | /** | ||
118 | * Peer this transmission is directed to. | ||
119 | */ | ||
120 | struct MeshPeer *peer; | ||
121 | |||
122 | /** | ||
123 | * Tunnel this message belongs to. | ||
124 | */ | ||
125 | struct MeshTunnel *tunnel; | ||
126 | |||
127 | /** | ||
128 | * Pointer to info stucture used as cls. | ||
129 | */ | ||
130 | void *cls; | ||
131 | |||
132 | /** | ||
133 | * Type of message | ||
134 | */ | ||
135 | uint16_t type; | ||
136 | |||
137 | /** | ||
138 | * Size of the message | ||
139 | */ | ||
140 | size_t size; | ||
141 | }; | ||
142 | |||
143 | |||
144 | /** | ||
145 | * Struct containing all information regarding a given peer | ||
146 | */ | ||
147 | struct MeshPeer | ||
148 | { | ||
149 | /** | ||
150 | * ID of the peer | ||
151 | */ | ||
152 | GNUNET_PEER_Id id; | ||
153 | |||
154 | /** | ||
155 | * Last time we heard from this peer | ||
156 | */ | ||
157 | struct GNUNET_TIME_Absolute last_contact; | ||
158 | |||
159 | /** | ||
160 | * Number of attempts to reconnect so far | ||
161 | */ | ||
162 | int n_reconnect_attempts; | ||
163 | |||
164 | /** | ||
165 | * Paths to reach the peer, ordered by ascending hop count | ||
166 | */ | ||
167 | struct MeshPeerPath *path_head; | ||
168 | |||
169 | /** | ||
170 | * Paths to reach the peer, ordered by ascending hop count | ||
171 | */ | ||
172 | struct MeshPeerPath *path_tail; | ||
173 | |||
174 | /** | ||
175 | * Handle to stop the DHT search for a path to this peer | ||
176 | */ | ||
177 | struct GNUNET_DHT_GetHandle *dhtget; | ||
178 | |||
179 | /** | ||
180 | * Array of tunnels this peer is the target of. | ||
181 | * Most probably a small amount, therefore not a hashmap. | ||
182 | * When the path to the peer changes, notify these tunnels to let them | ||
183 | * re-adjust their path trees. | ||
184 | */ | ||
185 | struct MeshTunnel **tunnels; | ||
186 | |||
187 | /** | ||
188 | * Number of tunnels this peers participates in | ||
189 | */ | ||
190 | unsigned int ntunnels; | ||
191 | |||
192 | /** | ||
193 | * Transmission queue to core DLL head | ||
194 | */ | ||
195 | struct MeshPeerQueue *queue_head; | ||
196 | |||
197 | /** | ||
198 | * Transmission queue to core DLL tail | ||
199 | */ | ||
200 | struct MeshPeerQueue *queue_tail; | ||
201 | |||
202 | /** | ||
203 | * How many messages are in the queue to this peer. | ||
204 | */ | ||
205 | unsigned int queue_n; | ||
206 | |||
207 | /** | ||
208 | * Handle for queued transmissions | ||
209 | */ | ||
210 | struct GNUNET_CORE_TransmitHandle *core_transmit; | ||
211 | }; | ||
212 | |||
213 | |||
214 | /** | ||
215 | * Struct to encapsulate all the Flow Control information to a peer in the | ||
216 | * context of a tunnel: Same peer in different tunnels will have independent | ||
217 | * flow control structures, allowing to choke/free tunnels according to its | ||
218 | * own criteria. | ||
219 | */ | ||
220 | struct MeshFlowControl | ||
221 | { | ||
222 | /** | ||
223 | * ID of the last packet sent towards the peer. | ||
224 | */ | ||
225 | uint32_t last_pid_sent; | ||
226 | |||
227 | /** | ||
228 | * ID of the last packet received from the peer. | ||
229 | */ | ||
230 | uint32_t last_pid_recv; | ||
231 | |||
232 | /** | ||
233 | * Last ACK sent to the peer (peer can't send more than this PID). | ||
234 | */ | ||
235 | uint32_t last_ack_sent; | ||
236 | |||
237 | /** | ||
238 | * Last ACK sent towards the origin (for traffic towards leaf node). | ||
239 | */ | ||
240 | uint32_t last_ack_recv; | ||
241 | |||
242 | /** | ||
243 | * How many payload messages are in the queue towards this peer. | ||
244 | */ | ||
245 | uint32_t queue_n; | ||
246 | |||
247 | /** | ||
248 | * Task to poll the peer in case of a lost ACK causes stall. | ||
249 | */ | ||
250 | GNUNET_SCHEDULER_TaskIdentifier poll_task; | ||
251 | |||
252 | /** | ||
253 | * How frequently to poll for ACKs. | ||
254 | */ | ||
255 | struct GNUNET_TIME_Relative poll_time; | ||
256 | |||
257 | /** | ||
258 | * On which tunnel to poll. | ||
259 | * Using an explicit poll_ctx would not help memory wise, | ||
260 | * since the allocated context would have to be stored in the | ||
261 | * fc struct in order to free it upon cancelling poll_task. | ||
262 | */ | ||
263 | struct MeshTunnel *t; | ||
264 | }; | ||
265 | |||
266 | |||
267 | /** | ||
268 | * Globally unique tunnel identification (owner + number) | ||
269 | * DO NOT USE OVER THE NETWORK | ||
270 | */ | ||
271 | struct MESH_TunnelID | ||
272 | { | ||
273 | /** | ||
274 | * Node that owns the tunnel | ||
275 | */ | ||
276 | GNUNET_PEER_Id oid; | ||
277 | |||
278 | /** | ||
279 | * Tunnel number to differentiate all the tunnels owned by the node oid | ||
280 | * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI ) | ||
281 | */ | ||
282 | MESH_TunnelNumber tid; | ||
283 | }; | ||
284 | |||
285 | |||
286 | /** | ||
287 | * Data needed for reliable tunnel endpoint retransmission management. | ||
288 | */ | ||
289 | struct MeshTunnelReliability; | ||
290 | |||
291 | |||
292 | /** | ||
293 | * Info needed to retry a message in case it gets lost. | ||
294 | */ | ||
295 | struct MeshReliableMessage | ||
296 | { | ||
297 | /** | ||
298 | * Double linked list, FIFO style | ||
299 | */ | ||
300 | struct MeshReliableMessage *next; | ||
301 | struct MeshReliableMessage *prev; | ||
302 | |||
303 | /** | ||
304 | * Tunnel Reliability queue this message is in. | ||
305 | */ | ||
306 | struct MeshTunnelReliability *rel; | ||
307 | |||
308 | /** | ||
309 | * ID of the message (ACK needed to free) | ||
310 | */ | ||
311 | uint32_t mid; | ||
312 | |||
313 | /** | ||
314 | * When was this message issued (to calculate ACK delay) | ||
315 | */ | ||
316 | struct GNUNET_TIME_Absolute timestamp; | ||
317 | |||
318 | /* struct GNUNET_MESH_Data with payload */ | ||
319 | }; | ||
320 | |||
321 | |||
322 | struct MeshTunnelReliability | ||
323 | { | ||
324 | /** | ||
325 | * Tunnel this is about. | ||
326 | */ | ||
327 | struct MeshTunnel *t; | ||
328 | |||
329 | /** | ||
330 | * DLL of messages sent and not yet ACK'd. | ||
331 | */ | ||
332 | struct MeshReliableMessage *head_sent; | ||
333 | struct MeshReliableMessage *tail_sent; | ||
334 | |||
335 | /** | ||
336 | * Messages pending | ||
337 | */ | ||
338 | unsigned int n_sent; | ||
339 | |||
340 | /** | ||
341 | * Next MID to use. | ||
342 | */ | ||
343 | uint32_t mid_sent; | ||
344 | |||
345 | /** | ||
346 | * DLL of messages received out of order. | ||
347 | */ | ||
348 | struct MeshReliableMessage *head_recv; | ||
349 | struct MeshReliableMessage *tail_recv; | ||
350 | |||
351 | /** | ||
352 | * Next MID expected. | ||
353 | */ | ||
354 | uint32_t mid_recv; | ||
355 | |||
356 | /** | ||
357 | * Task to resend/poll in case no ACK is received. | ||
358 | */ | ||
359 | GNUNET_SCHEDULER_TaskIdentifier retry_task; | ||
360 | |||
361 | /** | ||
362 | * Counter for exponential backoff. | ||
363 | */ | ||
364 | struct GNUNET_TIME_Relative retry_timer; | ||
365 | |||
366 | /** | ||
367 | * How long does it usually take to get an ACK. | ||
368 | */ | ||
369 | struct GNUNET_TIME_Relative expected_delay; | ||
370 | }; | ||
371 | |||
372 | |||
373 | /** | ||
374 | * Struct containing all information regarding a tunnel | ||
375 | * For an intermediate node the improtant info used will be: | ||
376 | * - id Tunnel unique identification | ||
377 | * - paths[0] To know where to send it next | ||
378 | * - metainfo: ready, speeds, accounting | ||
379 | */ | ||
380 | struct MeshTunnel | ||
381 | { | ||
382 | /** | ||
383 | * Tunnel ID | ||
384 | */ | ||
385 | struct MESH_TunnelID id; | ||
386 | |||
387 | /** | ||
388 | * Port of the tunnel. | ||
389 | */ | ||
390 | uint32_t port; | ||
391 | |||
392 | /** | ||
393 | * State of the tunnel. | ||
394 | */ | ||
395 | enum MeshTunnelState state; | ||
396 | |||
397 | /** | ||
398 | * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 ) | ||
399 | */ | ||
400 | MESH_TunnelNumber local_tid; | ||
401 | |||
402 | /** | ||
403 | * Local tunnel number for local destination clients (incoming number) | ||
404 | * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same | ||
405 | * number. | ||
406 | */ | ||
407 | MESH_TunnelNumber local_tid_dest; | ||
408 | 55 | ||
409 | /** | ||
410 | * Is the tunnel bufferless (minimum latency)? | ||
411 | */ | ||
412 | int nobuffer; | ||
413 | |||
414 | /** | ||
415 | * Is the tunnel reliable? | ||
416 | */ | ||
417 | int reliable; | ||
418 | |||
419 | /** | ||
420 | * Force sending ACK? Flag to allow duplicate ACK on POLL. | ||
421 | */ | ||
422 | int force_ack; | ||
423 | |||
424 | /** | ||
425 | * How many messages do we accept in the forward queue. | ||
426 | */ | ||
427 | unsigned int queue_max; | ||
428 | |||
429 | /** | ||
430 | * Last time the tunnel was used | ||
431 | */ | ||
432 | struct GNUNET_TIME_Absolute timestamp; | ||
433 | |||
434 | /** | ||
435 | * Destination of the tunnel. | ||
436 | */ | ||
437 | GNUNET_PEER_Id dest; | ||
438 | |||
439 | /** | ||
440 | * Next hop in the tunnel. If 0, @c client must be set. | ||
441 | */ | ||
442 | GNUNET_PEER_Id next_hop; | ||
443 | |||
444 | /** | ||
445 | * Previous hop in the tunnel. If 0, @c owner must be set. | ||
446 | */ | ||
447 | GNUNET_PEER_Id prev_hop; | ||
448 | |||
449 | /** | ||
450 | * Flow control information about @c next_hop or @c client. | ||
451 | */ | ||
452 | struct MeshFlowControl next_fc; | ||
453 | |||
454 | /** | ||
455 | * Flow control information about @c prev_hop or @c owner. | ||
456 | */ | ||
457 | struct MeshFlowControl prev_fc; | ||
458 | |||
459 | /** | ||
460 | * Client owner of the tunnel, if any | ||
461 | */ | ||
462 | struct MeshClient *owner; | ||
463 | |||
464 | /** | ||
465 | * Client destination of the tunnel, if any. | ||
466 | */ | ||
467 | struct MeshClient *client; | ||
468 | |||
469 | /** | ||
470 | * Task to keep the used paths alive at the owner, | ||
471 | * time tunnel out on all the other peers. | ||
472 | */ | ||
473 | GNUNET_SCHEDULER_TaskIdentifier fwd_maintenance_task; | ||
474 | |||
475 | /** | ||
476 | * Task to keep the used paths alive at the destination, | ||
477 | * time tunnel out on all the other peers. | ||
478 | */ | ||
479 | GNUNET_SCHEDULER_TaskIdentifier bck_maintenance_task; | ||
480 | |||
481 | /** | ||
482 | * Path being used for the tunnel. | ||
483 | */ | ||
484 | struct MeshPeerPath *path; | ||
485 | |||
486 | /** | ||
487 | * Flag to signal the destruction of the tunnel. | ||
488 | * If this is set GNUNET_YES the tunnel will be destroyed | ||
489 | * when the queue is empty. | ||
490 | */ | ||
491 | int destroy; | ||
492 | |||
493 | /** | ||
494 | * Total messages pending for this tunnels, payload or not. | ||
495 | */ | ||
496 | unsigned int pending_messages; | ||
497 | |||
498 | /** | ||
499 | * Reliability data. | ||
500 | * Only present (non-NULL) at the owner of a tunnel. | ||
501 | */ | ||
502 | struct MeshTunnelReliability *fwd_rel; | ||
503 | |||
504 | /** | ||
505 | * Reliability data. | ||
506 | * Only present (non-NULL) at the destination of a tunnel. | ||
507 | */ | ||
508 | struct MeshTunnelReliability *bck_rel; | ||
509 | }; | ||
510 | |||
511 | |||
512 | /** | ||
513 | * Struct containing information about a client of the service | ||
514 | * | ||
515 | * TODO: add a list of 'waiting' ports | ||
516 | */ | ||
517 | struct MeshClient | ||
518 | { | ||
519 | /** | ||
520 | * Linked list next | ||
521 | */ | ||
522 | struct MeshClient *next; | ||
523 | |||
524 | /** | ||
525 | * Linked list prev | ||
526 | */ | ||
527 | struct MeshClient *prev; | ||
528 | |||
529 | /** | ||
530 | * Tunnels that belong to this client, indexed by local id | ||
531 | */ | ||
532 | struct GNUNET_CONTAINER_MultiHashMap32 *own_tunnels; | ||
533 | |||
534 | /** | ||
535 | * Tunnels this client has accepted, indexed by incoming local id | ||
536 | */ | ||
537 | struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels; | ||
538 | |||
539 | /** | ||
540 | * Handle to communicate with the client | ||
541 | */ | ||
542 | struct GNUNET_SERVER_Client *handle; | ||
543 | |||
544 | /** | ||
545 | * Ports that this client has declared interest in. | ||
546 | * Indexed by port, contains *Client. | ||
547 | */ | ||
548 | struct GNUNET_CONTAINER_MultiHashMap32 *ports; | ||
549 | |||
550 | /** | ||
551 | * Whether the client is active or shutting down (don't send confirmations | ||
552 | * to a client that is shutting down. | ||
553 | */ | ||
554 | int shutting_down; | ||
555 | |||
556 | /** | ||
557 | * ID of the client, mainly for debug messages | ||
558 | */ | ||
559 | unsigned int id; | ||
560 | |||
561 | }; | ||
562 | |||
563 | |||
564 | /******************************************************************************/ | ||
565 | /************************ DEBUG FUNCTIONS ****************************/ | ||
566 | /******************************************************************************/ | ||
567 | |||
568 | #if MESH_DEBUG | ||
569 | /** | ||
570 | * GNUNET_SCHEDULER_Task for printing a message after some operation is done | ||
571 | * @param cls string to print | ||
572 | * @param success GNUNET_OK if the PUT was transmitted, | ||
573 | * GNUNET_NO on timeout, | ||
574 | * GNUNET_SYSERR on disconnect from service | ||
575 | * after the PUT message was transmitted | ||
576 | * (so we don't know if it was received or not) | ||
577 | */ | ||
578 | |||
579 | #if 0 | ||
580 | static void | ||
581 | mesh_debug (void *cls, int success) | ||
582 | { | ||
583 | char *s = cls; | ||
584 | |||
585 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success); | ||
586 | } | ||
587 | #endif | ||
588 | |||
589 | #endif | ||
590 | 56 | ||
591 | /******************************************************************************/ | 57 | /******************************************************************************/ |
592 | /*********************** GLOBAL VARIABLES ****************************/ | 58 | /*********************** GLOBAL VARIABLES ****************************/ |
593 | /******************************************************************************/ | 59 | /******************************************************************************/ |
594 | 60 | ||
595 | /************************** Configuration parameters **************************/ | 61 | /****************************** Global variables ******************************/ |
596 | |||
597 | /** | ||
598 | * How often to send tunnel keepalives. Tunnels timeout after 4 missed. | ||
599 | */ | ||
600 | static struct GNUNET_TIME_Relative refresh_path_time; | ||
601 | |||
602 | /** | ||
603 | * How often to PUT own ID in the DHT. | ||
604 | */ | ||
605 | static struct GNUNET_TIME_Relative id_announce_time; | ||
606 | |||
607 | /** | ||
608 | * Maximum time allowed to connect to a peer found by string. | ||
609 | */ | ||
610 | static struct GNUNET_TIME_Relative connect_timeout; | ||
611 | |||
612 | /** | ||
613 | * Default TTL for payload packets. | ||
614 | */ | ||
615 | static unsigned long long default_ttl; | ||
616 | |||
617 | /** | ||
618 | * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put. | ||
619 | */ | ||
620 | static unsigned long long dht_replication_level; | ||
621 | |||
622 | /** | ||
623 | * How many tunnels are we willing to maintain. | ||
624 | * Local tunnels are always allowed, even if there are more tunnels than max. | ||
625 | */ | ||
626 | static unsigned long long max_tunnels; | ||
627 | |||
628 | /** | ||
629 | * How many messages *in total* are we willing to queue, divided by number of | ||
630 | * tunnels to get tunnel queue size. | ||
631 | */ | ||
632 | static unsigned long long max_msgs_queue; | ||
633 | |||
634 | /** | ||
635 | * How many peers do we want to remember? | ||
636 | */ | ||
637 | static unsigned long long max_peers; | ||
638 | |||
639 | /** | ||
640 | * Percentage of messages that will be dropped (for test purposes only). | ||
641 | */ | ||
642 | static unsigned long long drop_percent; | ||
643 | |||
644 | /*************************** Static global variables **************************/ | ||
645 | |||
646 | /** | ||
647 | * DLL with all the clients, head. | ||
648 | */ | ||
649 | static struct MeshClient *clients_head; | ||
650 | |||
651 | /** | ||
652 | * DLL with all the clients, tail. | ||
653 | */ | ||
654 | static struct MeshClient *clients_tail; | ||
655 | |||
656 | /** | ||
657 | * Tunnels known, indexed by MESH_TunnelID (MeshTunnel). | ||
658 | */ | ||
659 | static struct GNUNET_CONTAINER_MultiHashMap *tunnels; | ||
660 | |||
661 | /** | ||
662 | * Number of tunnels known. | ||
663 | */ | ||
664 | static unsigned long long n_tunnels; | ||
665 | |||
666 | /** | ||
667 | * Tunnels incoming, indexed by MESH_TunnelNumber | ||
668 | * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV). | ||
669 | */ | ||
670 | static struct GNUNET_CONTAINER_MultiHashMap32 *incoming_tunnels; | ||
671 | |||
672 | /** | ||
673 | * Peers known, indexed by PeerIdentity (MeshPeer). | ||
674 | */ | ||
675 | static struct GNUNET_CONTAINER_MultiPeerMap *peers; | ||
676 | |||
677 | /* | ||
678 | * Handle to communicate with transport | ||
679 | */ | ||
680 | // static struct GNUNET_TRANSPORT_Handle *transport_handle; | ||
681 | |||
682 | /** | ||
683 | * Handle to communicate with core. | ||
684 | */ | ||
685 | static struct GNUNET_CORE_Handle *core_handle; | ||
686 | |||
687 | /** | ||
688 | * Handle to use DHT. | ||
689 | */ | ||
690 | static struct GNUNET_DHT_Handle *dht_handle; | ||
691 | |||
692 | /** | ||
693 | * Handle to server. | ||
694 | */ | ||
695 | static struct GNUNET_SERVER_Handle *server_handle; | ||
696 | 62 | ||
697 | /** | 63 | /** |
698 | * Handle to the statistics service. | 64 | * Handle to the statistics service. |
699 | */ | 65 | */ |
700 | static struct GNUNET_STATISTICS_Handle *stats; | 66 | struct GNUNET_STATISTICS_Handle *stats; |
701 | |||
702 | /** | ||
703 | * Notification context, to send messages to local clients. | ||
704 | */ | ||
705 | static struct GNUNET_SERVER_NotificationContext *nc; | ||
706 | 67 | ||
707 | /** | 68 | /** |
708 | * Local peer own ID (memory efficient handle). | 69 | * Local peer own ID (memory efficient handle). |
709 | */ | 70 | */ |
710 | static GNUNET_PEER_Id myid; | 71 | GNUNET_PEER_Id myid; |
711 | 72 | ||
712 | /** | 73 | /** |
713 | * Local peer own ID (full value). | 74 | * Local peer own ID (full value). |
714 | */ | 75 | */ |
715 | static struct GNUNET_PeerIdentity my_full_id; | 76 | struct GNUNET_PeerIdentity my_full_id; |
77 | |||
78 | /*************************** Static global variables **************************/ | ||
716 | 79 | ||
717 | /** | 80 | /** |
718 | * Own private key. | 81 | * Own private key. |
719 | */ | 82 | */ |
720 | static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key; | 83 | static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key; |
721 | 84 | ||
722 | /** | ||
723 | * Tunnel ID for the next created tunnel (global tunnel number). | ||
724 | */ | ||
725 | static MESH_TunnelNumber next_tid; | ||
726 | |||
727 | /** | ||
728 | * Tunnel ID for the next incoming tunnel (local tunnel number). | ||
729 | */ | ||
730 | static MESH_TunnelNumber next_local_tid; | ||
731 | |||
732 | /** | ||
733 | * All ports clients of this peer have opened. | ||
734 | */ | ||
735 | static struct GNUNET_CONTAINER_MultiHashMap32 *ports; | ||
736 | |||
737 | /** | ||
738 | * Task to periodically announce itself in the network. | ||
739 | */ | ||
740 | GNUNET_SCHEDULER_TaskIdentifier announce_id_task; | ||
741 | |||
742 | /** | ||
743 | * Next ID to assign to a client. | ||
744 | */ | ||
745 | unsigned int next_client_id; | ||
746 | |||
747 | |||
748 | /******************************************************************************/ | ||
749 | /*********************** DECLARATIONS **************************/ | ||
750 | /******************************************************************************/ | ||
751 | |||
752 | /** | ||
753 | * Function to process paths received for a new peer addition. The recorded | ||
754 | * paths form the initial tunnel, which can be optimized later. | ||
755 | * Called on each result obtained for the DHT search. | ||
756 | * | ||
757 | * @param cls closure | ||
758 | * @param exp when will this value expire | ||
759 | * @param key key of the result | ||
760 | * @param type type of the result | ||
761 | * @param size number of bytes in data | ||
762 | * @param data pointer to the result data | ||
763 | */ | ||
764 | static void | ||
765 | dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, | ||
766 | const struct GNUNET_HashCode * key, | ||
767 | const struct GNUNET_PeerIdentity *get_path, | ||
768 | unsigned int get_path_length, | ||
769 | const struct GNUNET_PeerIdentity *put_path, | ||
770 | unsigned int put_path_length, enum GNUNET_BLOCK_Type type, | ||
771 | size_t size, const void *data); | ||
772 | |||
773 | |||
774 | /** | ||
775 | * Retrieve the MeshPeer stucture associated with the peer, create one | ||
776 | * and insert it in the appropriate structures if the peer is not known yet. | ||
777 | * | ||
778 | * @param peer_id Full identity of the peer. | ||
779 | * | ||
780 | * @return Existing or newly created peer info. | ||
781 | */ | ||
782 | static struct MeshPeer * | ||
783 | peer_get (const struct GNUNET_PeerIdentity *peer_id); | ||
784 | |||
785 | |||
786 | /** | ||
787 | * Retrieve the MeshPeer stucture associated with the peer, create one | ||
788 | * and insert it in the appropriate structures if the peer is not known yet. | ||
789 | * | ||
790 | * @param peer Short identity of the peer. | ||
791 | * | ||
792 | * @return Existing or newly created peer info. | ||
793 | */ | ||
794 | static struct MeshPeer * | ||
795 | peer_get_short (const GNUNET_PEER_Id peer); | ||
796 | |||
797 | |||
798 | /** | ||
799 | * Build a PeerPath from the paths returned from the DHT, reversing the paths | ||
800 | * to obtain a local peer -> destination path and interning the peer ids. | ||
801 | * | ||
802 | * @return Newly allocated and created path | ||
803 | */ | ||
804 | static struct MeshPeerPath * | ||
805 | path_build_from_dht (const struct GNUNET_PeerIdentity *get_path, | ||
806 | unsigned int get_path_length, | ||
807 | const struct GNUNET_PeerIdentity *put_path, | ||
808 | unsigned int put_path_length); | ||
809 | |||
810 | |||
811 | /** | ||
812 | * Adds a path to the peer_infos of all the peers in the path | ||
813 | * | ||
814 | * @param p Path to process. | ||
815 | * @param confirmed Whether we know if the path works or not. | ||
816 | */ | ||
817 | static void | ||
818 | path_add_to_peers (struct MeshPeerPath *p, int confirmed); | ||
819 | |||
820 | |||
821 | |||
822 | /** | ||
823 | * Search for a tunnel by global ID using full PeerIdentities. | ||
824 | * | ||
825 | * @param oid owner of the tunnel. | ||
826 | * @param tid global tunnel number. | ||
827 | * | ||
828 | * @return tunnel handler, NULL if doesn't exist. | ||
829 | */ | ||
830 | static struct MeshTunnel * | ||
831 | tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid); | ||
832 | |||
833 | |||
834 | /** | ||
835 | * Change the tunnel state. | ||
836 | * | ||
837 | * @param t Tunnel whose ttate to change. | ||
838 | * @param state New state. | ||
839 | */ | ||
840 | static void | ||
841 | tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state); | ||
842 | |||
843 | |||
844 | /** | ||
845 | * Notify a tunnel that a connection has broken that affects at least | ||
846 | * some of its peers. | ||
847 | * | ||
848 | * @param t Tunnel affected. | ||
849 | * @param p1 Peer that got disconnected from p2. | ||
850 | * @param p2 Peer that got disconnected from p1. | ||
851 | * | ||
852 | * @return Short ID of the peer disconnected (either p1 or p2). | ||
853 | * 0 if the tunnel remained unaffected. | ||
854 | */ | ||
855 | static GNUNET_PEER_Id | ||
856 | tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1, | ||
857 | GNUNET_PEER_Id p2); | ||
858 | |||
859 | |||
860 | /** | ||
861 | * Send FWD keepalive packets for a tunnel. | ||
862 | * | ||
863 | * @param cls Closure (tunnel for which to send the keepalive). | ||
864 | * @param tc Notification context. | ||
865 | */ | ||
866 | static void | ||
867 | tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
868 | |||
869 | /** | ||
870 | * Send BCK keepalive packets for a tunnel. | ||
871 | * | ||
872 | * @param cls Closure (tunnel for which to send the keepalive). | ||
873 | * @param tc Notification context. | ||
874 | */ | ||
875 | static void | ||
876 | tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); | ||
877 | |||
878 | /** | ||
879 | * @brief Use the given path for the tunnel. | ||
880 | * Update the next and prev hops (and RCs). | ||
881 | * (Re)start the path refresh in case the tunnel is locally owned. | ||
882 | * | ||
883 | * @param t Tunnel to update. | ||
884 | * @param p Path to use. | ||
885 | */ | ||
886 | static void | ||
887 | tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p); | ||
888 | |||
889 | /** | ||
890 | * Tunnel is empty: destroy it. | ||
891 | * | ||
892 | * Notifies all participants (peers, cleints) about the destruction. | ||
893 | * | ||
894 | * @param t Tunnel to destroy. | ||
895 | */ | ||
896 | static void | ||
897 | tunnel_destroy_empty (struct MeshTunnel *t); | ||
898 | |||
899 | /** | ||
900 | * Destroy the tunnel. | ||
901 | * | ||
902 | * This function does not generate any warning traffic to clients or peers. | ||
903 | * | ||
904 | * Tasks: | ||
905 | * Remove the tunnel from peer_info's and clients' hashmaps. | ||
906 | * Cancel messages belonging to this tunnel queued to neighbors. | ||
907 | * Free any allocated resources linked to the tunnel. | ||
908 | * | ||
909 | * @param t the tunnel to destroy | ||
910 | * | ||
911 | * @return GNUNET_OK on success | ||
912 | */ | ||
913 | static int | ||
914 | tunnel_destroy (struct MeshTunnel *t); | ||
915 | |||
916 | /** | ||
917 | * @brief Queue and pass message to core when possible. | ||
918 | * | ||
919 | * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status | ||
920 | * and accounts for it. In case the queue is full, the message is dropped and | ||
921 | * a break issued. | ||
922 | * | ||
923 | * Otherwise, message is treated as internal and allowed to go regardless of | ||
924 | * queue status. | ||
925 | * | ||
926 | * @param cls Closure (@c type dependant). It will be used by queue_send to | ||
927 | * build the message to be sent if not already prebuilt. | ||
928 | * @param type Type of the message, 0 for a raw message. | ||
929 | * @param size Size of the message. | ||
930 | * @param dst Neighbor to send message to. | ||
931 | * @param t Tunnel this message belongs to. | ||
932 | */ | ||
933 | static void | ||
934 | queue_add (void *cls, uint16_t type, size_t size, | ||
935 | struct MeshPeer *dst, struct MeshTunnel *t); | ||
936 | |||
937 | |||
938 | /** | ||
939 | * Free a transmission that was already queued with all resources | ||
940 | * associated to the request. | ||
941 | * | ||
942 | * @param queue Queue handler to cancel. | ||
943 | * @param clear_cls Is it necessary to free associated cls? | ||
944 | */ | ||
945 | static void | ||
946 | queue_destroy (struct MeshPeerQueue *queue, int clear_cls); | ||
947 | |||
948 | |||
949 | /** | ||
950 | * @brief Get the next transmittable message from the queue. | ||
951 | * | ||
952 | * This will be the head, except in the case of being a data packet | ||
953 | * not allowed by the destination peer. | ||
954 | * | ||
955 | * @param peer Destination peer. | ||
956 | * | ||
957 | * @return The next viable MeshPeerQueue element to send to that peer. | ||
958 | * NULL when there are no transmittable messages. | ||
959 | */ | ||
960 | struct MeshPeerQueue * | ||
961 | queue_get_next (const struct MeshPeer *peer); | ||
962 | |||
963 | |||
964 | /** | ||
965 | * Core callback to write a queued packet to core buffer | ||
966 | * | ||
967 | * @param cls Closure (peer info). | ||
968 | * @param size Number of bytes available in buf. | ||
969 | * @param buf Where the to write the message. | ||
970 | * | ||
971 | * @return number of bytes written to buf | ||
972 | */ | ||
973 | static size_t | ||
974 | queue_send (void *cls, size_t size, void *buf); | ||
975 | |||
976 | |||
977 | /** | ||
978 | * Dummy function to separate declarations from definitions in function list. | ||
979 | */ | ||
980 | void | ||
981 | __mesh_divider______________________________________________________________(); | ||
982 | |||
983 | |||
984 | static const char * | ||
985 | GNUNET_MESH_DEBUG_S2S (enum MeshTunnelState s) | ||
986 | { | ||
987 | static char buf[128]; | ||
988 | |||
989 | switch (s) | ||
990 | { | ||
991 | /** | ||
992 | * Uninitialized status, should never appear in operation. | ||
993 | */ | ||
994 | case MESH_TUNNEL_NEW: return "MESH_TUNNEL_NEW"; | ||
995 | |||
996 | /** | ||
997 | * Path to the peer not known yet | ||
998 | */ | ||
999 | case MESH_TUNNEL_SEARCHING: return "MESH_TUNNEL_SEARCHING"; | ||
1000 | |||
1001 | /** | ||
1002 | * Request sent, not yet answered. | ||
1003 | */ | ||
1004 | case MESH_TUNNEL_WAITING: return "MESH_TUNNEL_WAITING"; | ||
1005 | |||
1006 | /** | ||
1007 | * Peer connected and ready to accept data | ||
1008 | */ | ||
1009 | case MESH_TUNNEL_READY: return "MESH_TUNNEL_READY"; | ||
1010 | |||
1011 | /** | ||
1012 | * Peer connected previosly but not responding | ||
1013 | */ | ||
1014 | case MESH_TUNNEL_RECONNECTING: return "MESH_TUNNEL_RECONNECTING"; | ||
1015 | |||
1016 | default: | ||
1017 | sprintf (buf, "%u (UNKNOWN STATE)", s); | ||
1018 | return buf; | ||
1019 | } | ||
1020 | } | ||
1021 | |||
1022 | |||
1023 | /******************************************************************************/ | ||
1024 | /************************ PERIODIC FUNCTIONS ****************************/ | ||
1025 | /******************************************************************************/ | ||
1026 | |||
1027 | /** | ||
1028 | * Periodically announce self id in the DHT | ||
1029 | * | ||
1030 | * @param cls closure | ||
1031 | * @param tc task context | ||
1032 | */ | ||
1033 | static void | ||
1034 | announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
1035 | { | ||
1036 | struct PBlock block; | ||
1037 | struct GNUNET_HashCode phash; | ||
1038 | |||
1039 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
1040 | { | ||
1041 | announce_id_task = GNUNET_SCHEDULER_NO_TASK; | ||
1042 | return; | ||
1043 | } | ||
1044 | /* TODO | ||
1045 | * - Set data expiration in function of X | ||
1046 | * - Adapt X to churn | ||
1047 | */ | ||
1048 | DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id)); | ||
1049 | GNUNET_CRYPTO_hash (&my_full_id, sizeof (my_full_id), &phash); | ||
1050 | block.id = my_full_id; | ||
1051 | GNUNET_DHT_put (dht_handle, /* DHT handle */ | ||
1052 | &phash, | ||
1053 | dht_replication_level, /* Replication level */ | ||
1054 | GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */ | ||
1055 | GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */ | ||
1056 | sizeof (block), /* Size of the data */ | ||
1057 | (const char *) &block, /* Data itself */ | ||
1058 | GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */ | ||
1059 | GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */ | ||
1060 | NULL, /* Continuation */ | ||
1061 | NULL); /* Continuation closure */ | ||
1062 | announce_id_task = | ||
1063 | GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls); | ||
1064 | } | ||
1065 | |||
1066 | |||
1067 | /******************************************************************************/ | ||
1068 | /****************** GENERAL HELPER FUNCTIONS ************************/ | ||
1069 | /******************************************************************************/ | ||
1070 | |||
1071 | |||
1072 | /** | ||
1073 | * Check if client has registered with the service and has not disconnected | ||
1074 | * | ||
1075 | * @param client the client to check | ||
1076 | * | ||
1077 | * @return non-NULL if client exists in the global DLL | ||
1078 | */ | ||
1079 | static struct MeshClient * | ||
1080 | client_get (struct GNUNET_SERVER_Client *client) | ||
1081 | { | ||
1082 | return GNUNET_SERVER_client_get_user_context (client, struct MeshClient); | ||
1083 | } | ||
1084 | |||
1085 | |||
1086 | /** | ||
1087 | * Deletes a tunnel from a client (either owner or destination). To be used on | ||
1088 | * tunnel destroy. | ||
1089 | * | ||
1090 | * @param c Client whose tunnel to delete. | ||
1091 | * @param t Tunnel which should be deleted. | ||
1092 | */ | ||
1093 | static void | ||
1094 | client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t) | ||
1095 | { | ||
1096 | int res; | ||
1097 | |||
1098 | if (c == t->owner) | ||
1099 | { | ||
1100 | res = GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, | ||
1101 | t->local_tid, t); | ||
1102 | if (GNUNET_YES != res) | ||
1103 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel owner KO\n"); | ||
1104 | } | ||
1105 | if (c == t->client) | ||
1106 | { | ||
1107 | res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels, | ||
1108 | t->local_tid_dest, t); | ||
1109 | if (GNUNET_YES != res) | ||
1110 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel client KO\n"); | ||
1111 | } | ||
1112 | } | ||
1113 | |||
1114 | |||
1115 | /** | ||
1116 | * Notify the appropriate client that a new incoming tunnel was created. | ||
1117 | * | ||
1118 | * @param t Tunnel that was created. | ||
1119 | */ | ||
1120 | static void | ||
1121 | send_local_tunnel_create (struct MeshTunnel *t) | ||
1122 | { | ||
1123 | struct GNUNET_MESH_TunnelMessage msg; | ||
1124 | |||
1125 | if (NULL == t->client) | ||
1126 | return; | ||
1127 | msg.header.size = htons (sizeof (msg)); | ||
1128 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE); | ||
1129 | msg.tunnel_id = htonl (t->local_tid_dest); | ||
1130 | msg.port = htonl (t->port); | ||
1131 | msg.opt = 0; | ||
1132 | msg.opt |= GNUNET_YES == t->reliable ? GNUNET_MESH_OPTION_RELIABLE : 0; | ||
1133 | msg.opt |= GNUNET_YES == t->nobuffer ? GNUNET_MESH_OPTION_NOBUFFER : 0; | ||
1134 | msg.opt = htonl (msg.opt); | ||
1135 | GNUNET_PEER_resolve (t->id.oid, &msg.peer); | ||
1136 | GNUNET_SERVER_notification_context_unicast (nc, t->client->handle, | ||
1137 | &msg.header, GNUNET_NO); | ||
1138 | } | ||
1139 | |||
1140 | |||
1141 | /** | ||
1142 | * Notify a client that the incoming tunnel is no longer valid. | ||
1143 | * | ||
1144 | * @param t Tunnel that is destroyed. | ||
1145 | * @param fwd Forward notification (owner->dest)? | ||
1146 | */ | ||
1147 | static void | ||
1148 | send_local_tunnel_destroy (struct MeshTunnel *t, int fwd) | ||
1149 | { | ||
1150 | struct GNUNET_MESH_TunnelMessage msg; | ||
1151 | struct MeshClient *c; | ||
1152 | |||
1153 | c = fwd ? t->client : t->owner; | ||
1154 | if (NULL == c) | ||
1155 | { | ||
1156 | GNUNET_break (0); | ||
1157 | return; | ||
1158 | } | ||
1159 | msg.header.size = htons (sizeof (msg)); | ||
1160 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); | ||
1161 | msg.tunnel_id = htonl (fwd ? t->local_tid_dest : t->local_tid); | ||
1162 | msg.port = htonl (0); | ||
1163 | memset (&msg.peer, 0, sizeof (msg.peer)); | ||
1164 | msg.opt = htonl (0); | ||
1165 | GNUNET_SERVER_notification_context_unicast (nc, c->handle, | ||
1166 | &msg.header, GNUNET_NO); | ||
1167 | } | ||
1168 | |||
1169 | |||
1170 | /** | ||
1171 | * Build a local ACK message and send it to a local client. | ||
1172 | * | ||
1173 | * @param t Tunnel on which to send the ACK. | ||
1174 | * @param c Client to whom send the ACK. | ||
1175 | * @param is_fwd Set to GNUNET_YES for FWD ACK (dest->owner) | ||
1176 | */ | ||
1177 | static void | ||
1178 | send_local_ack (struct MeshTunnel *t, | ||
1179 | struct MeshClient *c, | ||
1180 | int is_fwd) | ||
1181 | { | ||
1182 | struct GNUNET_MESH_LocalAck msg; | ||
1183 | |||
1184 | msg.header.size = htons (sizeof (msg)); | ||
1185 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK); | ||
1186 | msg.tunnel_id = htonl (is_fwd ? t->local_tid : t->local_tid_dest); | ||
1187 | GNUNET_SERVER_notification_context_unicast (nc, | ||
1188 | c->handle, | ||
1189 | &msg.header, | ||
1190 | GNUNET_NO); | ||
1191 | } | ||
1192 | |||
1193 | |||
1194 | |||
1195 | /** | ||
1196 | * Sends an already built message to a peer, properly registrating | ||
1197 | * all used resources. | ||
1198 | * | ||
1199 | * @param message Message to send. Function makes a copy of it. | ||
1200 | * @param peer Short ID of the neighbor whom to send the message. | ||
1201 | * @param t Tunnel on which this message is transmitted. | ||
1202 | */ | ||
1203 | static void | ||
1204 | send_prebuilt_message (const struct GNUNET_MessageHeader *message, | ||
1205 | GNUNET_PEER_Id peer, | ||
1206 | struct MeshTunnel *t) | ||
1207 | { | ||
1208 | struct GNUNET_PeerIdentity id; | ||
1209 | struct MeshPeer *neighbor; | ||
1210 | struct MeshPeerPath *p; | ||
1211 | void *data; | ||
1212 | size_t size; | ||
1213 | uint16_t type; | ||
1214 | |||
1215 | // GNUNET_TRANSPORT_try_connect(); FIXME use? | ||
1216 | |||
1217 | if (0 == peer) | ||
1218 | return; | ||
1219 | |||
1220 | size = ntohs (message->size); | ||
1221 | data = GNUNET_malloc (size); | ||
1222 | memcpy (data, message, size); | ||
1223 | type = ntohs(message->type); | ||
1224 | if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type || | ||
1225 | GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type) | ||
1226 | { | ||
1227 | struct GNUNET_MESH_Data *u; | ||
1228 | |||
1229 | u = (struct GNUNET_MESH_Data *) data; | ||
1230 | u->ttl = htonl (ntohl (u->ttl) - 1); | ||
1231 | } | ||
1232 | GNUNET_PEER_resolve (peer, &id); | ||
1233 | neighbor = peer_get (&id); | ||
1234 | for (p = neighbor->path_head; NULL != p; p = p->next) | ||
1235 | { | ||
1236 | if (2 >= p->length) | ||
1237 | { | ||
1238 | break; | ||
1239 | } | ||
1240 | } | ||
1241 | if (NULL == p) | ||
1242 | { | ||
1243 | #if MESH_DEBUG | ||
1244 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1245 | " %s IS NOT DIRECTLY CONNECTED\n", | ||
1246 | GNUNET_i2s(&id)); | ||
1247 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1248 | " PATHS TO %s:\n", | ||
1249 | GNUNET_i2s(&id)); | ||
1250 | for (p = neighbor->path_head; NULL != p; p = p->next) | ||
1251 | { | ||
1252 | struct GNUNET_PeerIdentity debug_id; | ||
1253 | unsigned int i; | ||
1254 | |||
1255 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1256 | " path with %u hops through:\n", | ||
1257 | p->length); | ||
1258 | for (i = 0; i < p->length; i++) | ||
1259 | { | ||
1260 | GNUNET_PEER_resolve(p->peers[i], &debug_id); | ||
1261 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1262 | " hop %u: %s\n", | ||
1263 | i, GNUNET_i2s(&debug_id)); | ||
1264 | } | ||
1265 | } | ||
1266 | #endif | ||
1267 | GNUNET_break (0); | ||
1268 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1269 | " no direct connection to %s\n", | ||
1270 | GNUNET_i2s (&id)); | ||
1271 | GNUNET_free (data); | ||
1272 | return; | ||
1273 | } | ||
1274 | if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type) | ||
1275 | type = 0; | ||
1276 | queue_add (data, | ||
1277 | type, | ||
1278 | size, | ||
1279 | neighbor, | ||
1280 | t); | ||
1281 | } | ||
1282 | |||
1283 | |||
1284 | /** | ||
1285 | * Sends a CREATE PATH message for a path to a peer, properly registrating | ||
1286 | * all used resources. | ||
1287 | * | ||
1288 | * @param t Tunnel for which the path is created. | ||
1289 | */ | ||
1290 | static void | ||
1291 | send_path_create (struct MeshTunnel *t) | ||
1292 | { | ||
1293 | struct MeshPeer *neighbor; | ||
1294 | |||
1295 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send create path\n"); | ||
1296 | neighbor = peer_get_short (t->next_hop); | ||
1297 | queue_add (t, | ||
1298 | GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, | ||
1299 | sizeof (struct GNUNET_MESH_CreateTunnel) + | ||
1300 | (t->path->length * sizeof (struct GNUNET_PeerIdentity)), | ||
1301 | neighbor, | ||
1302 | t); | ||
1303 | tunnel_change_state (t, MESH_TUNNEL_WAITING); | ||
1304 | } | ||
1305 | |||
1306 | |||
1307 | /** | ||
1308 | * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us. | ||
1309 | * | ||
1310 | * @param t Tunnel which to confirm. | ||
1311 | */ | ||
1312 | static void | ||
1313 | send_path_ack (struct MeshTunnel *t) | ||
1314 | { | ||
1315 | struct MeshPeer *neighbor; | ||
1316 | |||
1317 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Send path ack\n"); | ||
1318 | neighbor = peer_get_short (t->prev_hop); | ||
1319 | queue_add (t, | ||
1320 | GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, | ||
1321 | sizeof (struct GNUNET_MESH_PathACK), | ||
1322 | neighbor, | ||
1323 | t); | ||
1324 | tunnel_change_state (t, MESH_TUNNEL_WAITING); | ||
1325 | } | ||
1326 | |||
1327 | |||
1328 | /** | ||
1329 | * Build an ACK message and queue it to send to the given peer. | ||
1330 | * | ||
1331 | * @param t Tunnel on which to send the ACK. | ||
1332 | * @param peer Peer to whom send the ACK. | ||
1333 | * @param ack Value of the ACK. | ||
1334 | */ | ||
1335 | static void | ||
1336 | send_ack (struct MeshTunnel *t, GNUNET_PEER_Id peer, uint32_t ack) | ||
1337 | { | ||
1338 | struct GNUNET_MESH_ACK msg; | ||
1339 | |||
1340 | GNUNET_PEER_resolve (t->id.oid, &msg.oid); | ||
1341 | msg.header.size = htons (sizeof (msg)); | ||
1342 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK); | ||
1343 | msg.pid = htonl (ack); | ||
1344 | msg.tid = htonl (t->id.tid); | ||
1345 | |||
1346 | send_prebuilt_message (&msg.header, peer, t); | ||
1347 | } | ||
1348 | |||
1349 | |||
1350 | /** | ||
1351 | * Core callback to write a pre-constructed data packet to core buffer | ||
1352 | * | ||
1353 | * @param cls Closure (MeshTransmissionDescriptor with data in "data" member). | ||
1354 | * @param size Number of bytes available in buf. | ||
1355 | * @param buf Where the to write the message. | ||
1356 | * | ||
1357 | * @return number of bytes written to buf | ||
1358 | */ | ||
1359 | static size_t | ||
1360 | send_core_data_raw (void *cls, size_t size, void *buf) | ||
1361 | { | ||
1362 | struct GNUNET_MessageHeader *msg = cls; | ||
1363 | size_t total_size; | ||
1364 | |||
1365 | GNUNET_assert (NULL != msg); | ||
1366 | total_size = ntohs (msg->size); | ||
1367 | |||
1368 | if (total_size > size) | ||
1369 | { | ||
1370 | GNUNET_break (0); | ||
1371 | return 0; | ||
1372 | } | ||
1373 | memcpy (buf, msg, total_size); | ||
1374 | GNUNET_free (cls); | ||
1375 | return total_size; | ||
1376 | } | ||
1377 | |||
1378 | |||
1379 | /** | ||
1380 | * Function to send a create path packet to a peer. | ||
1381 | * | ||
1382 | * @param cls closure | ||
1383 | * @param size number of bytes available in buf | ||
1384 | * @param buf where the callee should write the message | ||
1385 | * @return number of bytes written to buf | ||
1386 | */ | ||
1387 | static size_t | ||
1388 | send_core_path_create (void *cls, size_t size, void *buf) | ||
1389 | { | ||
1390 | struct MeshTunnel *t = cls; | ||
1391 | struct GNUNET_MESH_CreateTunnel *msg; | ||
1392 | struct GNUNET_PeerIdentity *peer_ptr; | ||
1393 | struct MeshPeerPath *p = t->path; | ||
1394 | size_t size_needed; | ||
1395 | uint32_t opt; | ||
1396 | int i; | ||
1397 | |||
1398 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n"); | ||
1399 | size_needed = | ||
1400 | sizeof (struct GNUNET_MESH_CreateTunnel) + | ||
1401 | p->length * sizeof (struct GNUNET_PeerIdentity); | ||
1402 | |||
1403 | if (size < size_needed || NULL == buf) | ||
1404 | { | ||
1405 | GNUNET_break (0); | ||
1406 | return 0; | ||
1407 | } | ||
1408 | msg = (struct GNUNET_MESH_CreateTunnel *) buf; | ||
1409 | msg->header.size = htons (size_needed); | ||
1410 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE); | ||
1411 | msg->tid = ntohl (t->id.tid); | ||
1412 | |||
1413 | opt = 0; | ||
1414 | if (GNUNET_YES == t->nobuffer) | ||
1415 | opt |= GNUNET_MESH_OPTION_NOBUFFER; | ||
1416 | if (GNUNET_YES == t->reliable) | ||
1417 | opt |= GNUNET_MESH_OPTION_RELIABLE; | ||
1418 | msg->opt = htonl (opt); | ||
1419 | msg->port = htonl (t->port); | ||
1420 | |||
1421 | peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1]; | ||
1422 | for (i = 0; i < p->length; i++) | ||
1423 | { | ||
1424 | GNUNET_PEER_resolve (p->peers[i], peer_ptr++); | ||
1425 | } | ||
1426 | |||
1427 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1428 | "CREATE PATH (%u bytes long) sent!\n", size_needed); | ||
1429 | return size_needed; | ||
1430 | } | ||
1431 | |||
1432 | |||
1433 | /** | ||
1434 | * Creates a path ack message in buf and frees all unused resources. | ||
1435 | * | ||
1436 | * @param cls closure (MeshTransmissionDescriptor) | ||
1437 | * @param size number of bytes available in buf | ||
1438 | * @param buf where the callee should write the message | ||
1439 | * @return number of bytes written to buf | ||
1440 | */ | ||
1441 | static size_t | ||
1442 | send_core_path_ack (void *cls, size_t size, void *buf) | ||
1443 | { | ||
1444 | struct MeshTunnel *t = cls; | ||
1445 | struct GNUNET_MESH_PathACK *msg = buf; | ||
1446 | |||
1447 | GNUNET_assert (NULL != t); | ||
1448 | if (sizeof (struct GNUNET_MESH_PathACK) > size) | ||
1449 | { | ||
1450 | GNUNET_break (0); | ||
1451 | return 0; | ||
1452 | } | ||
1453 | t->prev_fc.last_ack_sent = t->nobuffer ? 0 : t->queue_max - 1; | ||
1454 | msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK)); | ||
1455 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK); | ||
1456 | GNUNET_PEER_resolve (t->id.oid, &msg->oid); | ||
1457 | msg->tid = htonl (t->id.tid); | ||
1458 | msg->peer_id = my_full_id; | ||
1459 | msg->ack = htonl (t->prev_fc.last_ack_sent); | ||
1460 | |||
1461 | /* TODO add signature */ | ||
1462 | |||
1463 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n"); | ||
1464 | return sizeof (struct GNUNET_MESH_PathACK); | ||
1465 | } | ||
1466 | |||
1467 | |||
1468 | /** | ||
1469 | * Iterator over all the peers to remove the oldest not-used entry. | ||
1470 | * | ||
1471 | * @param cls Closure (unsued). | ||
1472 | * @param key ID of the peer. | ||
1473 | * @param value Peer_Info of the peer. | ||
1474 | * | ||
1475 | * FIXME implement | ||
1476 | */ | ||
1477 | static int | ||
1478 | peer_timeout (void *cls, | ||
1479 | const struct GNUNET_PeerIdentity *key, | ||
1480 | void *value) | ||
1481 | { | ||
1482 | return GNUNET_YES; | ||
1483 | } | ||
1484 | |||
1485 | |||
1486 | /** | ||
1487 | * Retrieve the MeshPeer stucture associated with the peer, create one | ||
1488 | * and insert it in the appropriate structures if the peer is not known yet. | ||
1489 | * | ||
1490 | * @param peer_id Full identity of the peer. | ||
1491 | * | ||
1492 | * @return Existing or newly created peer info. | ||
1493 | */ | ||
1494 | static struct MeshPeer * | ||
1495 | peer_get (const struct GNUNET_PeerIdentity *peer_id) | ||
1496 | { | ||
1497 | struct MeshPeer *peer; | ||
1498 | |||
1499 | peer = GNUNET_CONTAINER_multipeermap_get (peers, peer_id); | ||
1500 | if (NULL == peer) | ||
1501 | { | ||
1502 | peer = GNUNET_new (struct MeshPeer); | ||
1503 | if (GNUNET_CONTAINER_multipeermap_size (peers) > max_peers) | ||
1504 | { | ||
1505 | GNUNET_CONTAINER_multipeermap_iterate (peers, | ||
1506 | &peer_timeout, | ||
1507 | NULL); | ||
1508 | } | ||
1509 | GNUNET_CONTAINER_multipeermap_put (peers, peer_id, peer, | ||
1510 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
1511 | peer->id = GNUNET_PEER_intern (peer_id); | ||
1512 | } | ||
1513 | peer->last_contact = GNUNET_TIME_absolute_get(); | ||
1514 | |||
1515 | return peer; | ||
1516 | } | ||
1517 | |||
1518 | |||
1519 | /** | ||
1520 | * Retrieve the MeshPeer stucture associated with the peer, create one | ||
1521 | * and insert it in the appropriate structures if the peer is not known yet. | ||
1522 | * | ||
1523 | * @param peer Short identity of the peer. | ||
1524 | * | ||
1525 | * @return Existing or newly created peer info. | ||
1526 | */ | ||
1527 | static struct MeshPeer * | ||
1528 | peer_get_short (const GNUNET_PEER_Id peer) | ||
1529 | { | ||
1530 | return peer_get (GNUNET_PEER_resolve2 (peer)); | ||
1531 | } | ||
1532 | |||
1533 | |||
1534 | /** | ||
1535 | * Select which PID to POLL for, to compensate for lost messages. | ||
1536 | * | ||
1537 | * @param p Peer we want to poll. | ||
1538 | * @param t Tunnel about which we want to poll. | ||
1539 | * | ||
1540 | * @return PID to use, either last sent or first_in_queue - 1 | ||
1541 | */ | ||
1542 | static uint32_t | ||
1543 | peer_get_first_payload_pid (struct MeshPeer *p, struct MeshTunnel *t) | ||
1544 | { | ||
1545 | struct MeshPeerQueue *q; | ||
1546 | uint16_t type; | ||
1547 | |||
1548 | type = p->id == t->next_hop ? GNUNET_MESSAGE_TYPE_MESH_UNICAST : | ||
1549 | GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN; | ||
1550 | |||
1551 | for (q = p->queue_head; NULL != q; q = q->next) | ||
1552 | { | ||
1553 | if (q->type == type && q->tunnel == t) | ||
1554 | { | ||
1555 | struct GNUNET_MESH_Data *msg = q->cls; | ||
1556 | |||
1557 | /* Pretend that the last one sent was the previous to this */ | ||
1558 | return ntohl (msg->pid) - 1; | ||
1559 | } | ||
1560 | } | ||
1561 | |||
1562 | /* No data in queue, use last sent */ | ||
1563 | { | ||
1564 | struct MeshFlowControl *fc; | ||
1565 | |||
1566 | fc = p->id == t->next_hop ? &t->next_fc : &t->prev_fc; | ||
1567 | return fc->last_pid_sent; | ||
1568 | } | ||
1569 | } | ||
1570 | |||
1571 | |||
1572 | /** | ||
1573 | * Choose the best path towards a peer considering the tunnel properties. | ||
1574 | * | ||
1575 | * @param peer The destination peer. | ||
1576 | * @param t The tunnel the path is for. | ||
1577 | * | ||
1578 | * @return Best current known path towards the peer, if any. | ||
1579 | */ | ||
1580 | static struct MeshPeerPath * | ||
1581 | peer_get_best_path (const struct MeshPeer *peer, const struct MeshTunnel *t) | ||
1582 | { | ||
1583 | struct MeshPeerPath *best_p; | ||
1584 | struct MeshPeerPath *p; | ||
1585 | unsigned int best_cost; | ||
1586 | unsigned int cost; | ||
1587 | |||
1588 | best_p = p = peer->path_head; | ||
1589 | best_cost = cost = p->length; | ||
1590 | while (NULL != p) | ||
1591 | { | ||
1592 | if ((cost = p->length) < best_cost) | ||
1593 | { | ||
1594 | best_cost = cost; | ||
1595 | best_p = p; | ||
1596 | } | ||
1597 | p = p->next; | ||
1598 | } | ||
1599 | return best_p; | ||
1600 | } | ||
1601 | |||
1602 | |||
1603 | /** | ||
1604 | * Try to establish a new connection to this peer in the given tunnel. | ||
1605 | * If the peer doesn't have any path to it yet, try to get one. | ||
1606 | * If the peer already has some path, send a CREATE PATH towards it. | ||
1607 | * | ||
1608 | * @param peer PeerInfo of the peer. | ||
1609 | * @param t Tunnel for which to create the path, if possible. | ||
1610 | */ | ||
1611 | static void | ||
1612 | peer_connect (struct MeshPeer *peer, struct MeshTunnel *t) | ||
1613 | { | ||
1614 | struct MeshPeerPath *p; | ||
1615 | struct GNUNET_HashCode phash; | ||
1616 | |||
1617 | if (NULL != peer->path_head) | ||
1618 | { | ||
1619 | p = peer_get_best_path (peer, t); | ||
1620 | tunnel_use_path (t, p); | ||
1621 | send_path_create (t); | ||
1622 | } | ||
1623 | else if (NULL == peer->dhtget) | ||
1624 | { | ||
1625 | struct GNUNET_PeerIdentity id; | ||
1626 | |||
1627 | GNUNET_PEER_resolve (peer->id, &id); | ||
1628 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1629 | "Starting DHT GET for peer %s\n", | ||
1630 | GNUNET_i2s (&id)); | ||
1631 | GNUNET_CRYPTO_hash (&id, sizeof (my_full_id), &phash); | ||
1632 | peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */ | ||
1633 | GNUNET_BLOCK_TYPE_MESH_PEER, /* type */ | ||
1634 | &phash, /* key to search */ | ||
1635 | dht_replication_level, /* replication level */ | ||
1636 | GNUNET_DHT_RO_RECORD_ROUTE | | ||
1637 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, | ||
1638 | NULL, /* xquery */ | ||
1639 | 0, /* xquery bits */ | ||
1640 | &dht_get_id_handler, peer); | ||
1641 | tunnel_change_state (t, MESH_TUNNEL_SEARCHING); | ||
1642 | } | ||
1643 | else | ||
1644 | { | ||
1645 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1646 | "There is no path but the DHT GET is already started.\n"); | ||
1647 | } | ||
1648 | } | ||
1649 | |||
1650 | |||
1651 | /** | ||
1652 | * @brief Re-initiate traffic to this peer if necessary. | ||
1653 | * | ||
1654 | * Check if there is traffic queued towards this peer | ||
1655 | * and the core transmit handle is NULL (traffic was stalled). | ||
1656 | * If so, call core tmt rdy. | ||
1657 | * | ||
1658 | * @param peer_id Short ID of peer to which initiate traffic. | ||
1659 | */ | ||
1660 | static void | ||
1661 | peer_unlock_queue (GNUNET_PEER_Id peer_id) | ||
1662 | { | ||
1663 | struct MeshPeer *peer; | ||
1664 | struct MeshPeerQueue *q; | ||
1665 | size_t size; | ||
1666 | |||
1667 | peer = peer_get_short (peer_id); | ||
1668 | if (NULL != peer->core_transmit) | ||
1669 | return; | ||
1670 | |||
1671 | q = queue_get_next (peer); | ||
1672 | if (NULL == q) | ||
1673 | return; | ||
1674 | |||
1675 | size = q->size; | ||
1676 | peer->core_transmit = | ||
1677 | GNUNET_CORE_notify_transmit_ready (core_handle, | ||
1678 | GNUNET_NO, | ||
1679 | 0, | ||
1680 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
1681 | GNUNET_PEER_resolve2 (peer->id), | ||
1682 | size, | ||
1683 | &queue_send, | ||
1684 | peer); | ||
1685 | } | ||
1686 | |||
1687 | |||
1688 | /** | ||
1689 | * Cancel all transmissions towards a neighbor that belong to a certain tunnel. | ||
1690 | * | ||
1691 | * @param neighbor Short ID of the neighbor to whom cancel the transmissions. | ||
1692 | * @param t Tunnel which to cancel. | ||
1693 | */ | ||
1694 | static void | ||
1695 | peer_cancel_queues (GNUNET_PEER_Id neighbor, struct MeshTunnel *t) | ||
1696 | { | ||
1697 | struct MeshPeer *peer; | ||
1698 | struct MeshPeerQueue *q; | ||
1699 | struct MeshPeerQueue *next; | ||
1700 | struct MeshFlowControl *fc; | ||
1701 | |||
1702 | if (0 == neighbor) | ||
1703 | return; /* Was local peer, 0'ed in tunnel_destroy_iterator */ | ||
1704 | peer = peer_get_short (neighbor); | ||
1705 | for (q = peer->queue_head; NULL != q; q = next) | ||
1706 | { | ||
1707 | next = q->next; | ||
1708 | if (q->tunnel == t) | ||
1709 | { | ||
1710 | if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == q->type || | ||
1711 | GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == q->type) | ||
1712 | { | ||
1713 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1714 | "peer_cancel_queues %s\n", | ||
1715 | GNUNET_MESH_DEBUG_M2S (q->type)); | ||
1716 | } | ||
1717 | queue_destroy (q, GNUNET_YES); | ||
1718 | } | ||
1719 | } | ||
1720 | if (NULL == peer->queue_head && NULL != peer->core_transmit) | ||
1721 | { | ||
1722 | GNUNET_CORE_notify_transmit_ready_cancel (peer->core_transmit); | ||
1723 | peer->core_transmit = NULL; | ||
1724 | } | ||
1725 | fc = neighbor == t->next_hop ? &t->next_fc : &t->prev_fc; | ||
1726 | if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task) | ||
1727 | { | ||
1728 | GNUNET_SCHEDULER_cancel (fc->poll_task); | ||
1729 | fc->poll_task = GNUNET_SCHEDULER_NO_TASK; | ||
1730 | } | ||
1731 | } | ||
1732 | |||
1733 | |||
1734 | /** | ||
1735 | * Destroy the peer_info and free any allocated resources linked to it | ||
1736 | * | ||
1737 | * @param peer The peer_info to destroy. | ||
1738 | * | ||
1739 | * @return GNUNET_OK on success | ||
1740 | */ | ||
1741 | static int | ||
1742 | peer_destroy (struct MeshPeer *peer) | ||
1743 | { | ||
1744 | struct GNUNET_PeerIdentity id; | ||
1745 | struct MeshPeerPath *p; | ||
1746 | struct MeshPeerPath *nextp; | ||
1747 | unsigned int i; | ||
1748 | |||
1749 | GNUNET_PEER_resolve (peer->id, &id); | ||
1750 | GNUNET_PEER_change_rc (peer->id, -1); | ||
1751 | |||
1752 | if (GNUNET_YES != | ||
1753 | GNUNET_CONTAINER_multipeermap_remove (peers, &id, peer)) | ||
1754 | { | ||
1755 | GNUNET_break (0); | ||
1756 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1757 | "removing peer %s, not in hashmap\n", GNUNET_i2s (&id)); | ||
1758 | } | ||
1759 | if (NULL != peer->dhtget) | ||
1760 | { | ||
1761 | GNUNET_DHT_get_stop (peer->dhtget); | ||
1762 | } | ||
1763 | p = peer->path_head; | ||
1764 | while (NULL != p) | ||
1765 | { | ||
1766 | nextp = p->next; | ||
1767 | GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p); | ||
1768 | path_destroy (p); | ||
1769 | p = nextp; | ||
1770 | } | ||
1771 | for (i = 0; i < peer->ntunnels; i++) | ||
1772 | tunnel_destroy_empty (peer->tunnels[i]); | ||
1773 | GNUNET_array_grow (peer->tunnels, peer->ntunnels, 0); | ||
1774 | GNUNET_free (peer); | ||
1775 | return GNUNET_OK; | ||
1776 | } | ||
1777 | |||
1778 | |||
1779 | /** | ||
1780 | * Remove all paths that rely on a direct connection between p1 and p2 | ||
1781 | * from the peer itself and notify all tunnels about it. | ||
1782 | * | ||
1783 | * @param peer PeerInfo of affected peer. | ||
1784 | * @param p1 GNUNET_PEER_Id of one peer. | ||
1785 | * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and | ||
1786 | * no longer is. | ||
1787 | * | ||
1788 | * TODO: optimize (see below) | ||
1789 | */ | ||
1790 | static void | ||
1791 | peer_remove_path (struct MeshPeer *peer, GNUNET_PEER_Id p1, | ||
1792 | GNUNET_PEER_Id p2) | ||
1793 | { | ||
1794 | struct MeshPeerPath *p; | ||
1795 | struct MeshPeerPath *next; | ||
1796 | struct MeshPeer *peer_d; | ||
1797 | GNUNET_PEER_Id d; | ||
1798 | unsigned int destroyed; | ||
1799 | unsigned int i; | ||
1800 | |||
1801 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n"); | ||
1802 | destroyed = 0; | ||
1803 | for (p = peer->path_head; NULL != p; p = next) | ||
1804 | { | ||
1805 | next = p->next; | ||
1806 | for (i = 0; i < (p->length - 1); i++) | ||
1807 | { | ||
1808 | if ((p->peers[i] == p1 && p->peers[i + 1] == p2) || | ||
1809 | (p->peers[i] == p2 && p->peers[i + 1] == p1)) | ||
1810 | { | ||
1811 | GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p); | ||
1812 | path_destroy (p); | ||
1813 | destroyed++; | ||
1814 | break; | ||
1815 | } | ||
1816 | } | ||
1817 | } | ||
1818 | if (0 == destroyed) | ||
1819 | return; | ||
1820 | |||
1821 | for (i = 0; i < peer->ntunnels; i++) | ||
1822 | { | ||
1823 | d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2); | ||
1824 | if (0 == d) | ||
1825 | continue; | ||
1826 | |||
1827 | peer_d = peer_get_short (d); | ||
1828 | next = peer_get_best_path (peer_d, peer->tunnels[i]); | ||
1829 | tunnel_use_path (peer->tunnels[i], next); | ||
1830 | peer_connect (peer_d, peer->tunnels[i]); | ||
1831 | } | ||
1832 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n"); | ||
1833 | } | ||
1834 | |||
1835 | |||
1836 | /** | ||
1837 | * Add the path to the peer and update the path used to reach it in case this | ||
1838 | * is the shortest. | ||
1839 | * | ||
1840 | * @param peer_info Destination peer to add the path to. | ||
1841 | * @param path New path to add. Last peer must be the peer in arg 1. | ||
1842 | * Path will be either used of freed if already known. | ||
1843 | * @param trusted Do we trust that this path is real? | ||
1844 | */ | ||
1845 | void | ||
1846 | peer_add_path (struct MeshPeer *peer_info, struct MeshPeerPath *path, | ||
1847 | int trusted) | ||
1848 | { | ||
1849 | struct MeshPeerPath *aux; | ||
1850 | unsigned int l; | ||
1851 | unsigned int l2; | ||
1852 | |||
1853 | if ((NULL == peer_info) || (NULL == path)) | ||
1854 | { | ||
1855 | GNUNET_break (0); | ||
1856 | path_destroy (path); | ||
1857 | return; | ||
1858 | } | ||
1859 | if (path->peers[path->length - 1] != peer_info->id) | ||
1860 | { | ||
1861 | GNUNET_break (0); | ||
1862 | path_destroy (path); | ||
1863 | return; | ||
1864 | } | ||
1865 | if (2 >= path->length && GNUNET_NO == trusted) | ||
1866 | { | ||
1867 | /* Only allow CORE to tell us about direct paths */ | ||
1868 | path_destroy (path); | ||
1869 | return; | ||
1870 | } | ||
1871 | for (l = 1; l < path->length; l++) | ||
1872 | { | ||
1873 | if (path->peers[l] == myid) | ||
1874 | { | ||
1875 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l); | ||
1876 | for (l2 = 0; l2 < path->length - l; l2++) | ||
1877 | { | ||
1878 | path->peers[l2] = path->peers[l + l2]; | ||
1879 | } | ||
1880 | path->length -= l; | ||
1881 | l = 1; | ||
1882 | path->peers = | ||
1883 | GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id)); | ||
1884 | } | ||
1885 | } | ||
1886 | #if MESH_DEBUG | ||
1887 | { | ||
1888 | struct GNUNET_PeerIdentity id; | ||
1889 | |||
1890 | GNUNET_PEER_resolve (peer_info->id, &id); | ||
1891 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n", | ||
1892 | path->length, GNUNET_i2s (&id)); | ||
1893 | } | ||
1894 | #endif | ||
1895 | l = path_get_length (path); | ||
1896 | if (0 == l) | ||
1897 | { | ||
1898 | path_destroy (path); | ||
1899 | return; | ||
1900 | } | ||
1901 | |||
1902 | GNUNET_assert (peer_info->id == path->peers[path->length - 1]); | ||
1903 | for (aux = peer_info->path_head; aux != NULL; aux = aux->next) | ||
1904 | { | ||
1905 | l2 = path_get_length (aux); | ||
1906 | if (l2 > l) | ||
1907 | { | ||
1908 | GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head, | ||
1909 | peer_info->path_tail, aux, path); | ||
1910 | return; | ||
1911 | } | ||
1912 | else | ||
1913 | { | ||
1914 | if (l2 == l && memcmp (path->peers, aux->peers, l) == 0) | ||
1915 | { | ||
1916 | path_destroy (path); | ||
1917 | return; | ||
1918 | } | ||
1919 | } | ||
1920 | } | ||
1921 | GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail, | ||
1922 | path); | ||
1923 | return; | ||
1924 | } | ||
1925 | |||
1926 | |||
1927 | /** | ||
1928 | * Add the path to the origin peer and update the path used to reach it in case | ||
1929 | * this is the shortest. | ||
1930 | * The path is given in peer_info -> destination, therefore we turn the path | ||
1931 | * upside down first. | ||
1932 | * | ||
1933 | * @param peer_info Peer to add the path to, being the origin of the path. | ||
1934 | * @param path New path to add after being inversed. | ||
1935 | * Path will be either used or freed. | ||
1936 | * @param trusted Do we trust that this path is real? | ||
1937 | */ | ||
1938 | static void | ||
1939 | peer_add_path_to_origin (struct MeshPeer *peer_info, | ||
1940 | struct MeshPeerPath *path, int trusted) | ||
1941 | { | ||
1942 | path_invert (path); | ||
1943 | peer_add_path (peer_info, path, trusted); | ||
1944 | } | ||
1945 | |||
1946 | |||
1947 | /** | ||
1948 | * Add a tunnel to the list of tunnels a peer participates in. | ||
1949 | * Update the tunnel's destination. | ||
1950 | * | ||
1951 | * @param p Peer to add to. | ||
1952 | * @param t Tunnel to add. | ||
1953 | */ | ||
1954 | static void | ||
1955 | peer_add_tunnel (struct MeshPeer *p, struct MeshTunnel *t) | ||
1956 | { | ||
1957 | if (0 != t->dest) | ||
1958 | { | ||
1959 | GNUNET_break (t->dest == p->id); | ||
1960 | return; | ||
1961 | } | ||
1962 | t->dest = p->id; | ||
1963 | GNUNET_PEER_change_rc (t->dest, 1); | ||
1964 | GNUNET_array_append (p->tunnels, p->ntunnels, t); | ||
1965 | } | ||
1966 | |||
1967 | |||
1968 | /** | ||
1969 | * Remove a tunnel from the list of tunnels a peer participates in. | ||
1970 | * Free the tunnel's destination. | ||
1971 | * | ||
1972 | * @param p Peer to clean. | ||
1973 | * @param t Tunnel to remove. | ||
1974 | */ | ||
1975 | static void | ||
1976 | peer_remove_tunnel (struct MeshPeer *p, struct MeshTunnel *t) | ||
1977 | { | ||
1978 | unsigned int i; | ||
1979 | |||
1980 | if (t->dest == p->id) | ||
1981 | { | ||
1982 | GNUNET_PEER_change_rc (t->dest, -1); | ||
1983 | t->dest = 0; | ||
1984 | } | ||
1985 | for (i = 0; i < p->ntunnels; i++) | ||
1986 | { | ||
1987 | if (p->tunnels[i] == t) | ||
1988 | { | ||
1989 | p->tunnels[i] = p->tunnels[p->ntunnels - 1]; | ||
1990 | GNUNET_array_grow (p->tunnels, p->ntunnels, p->ntunnels - 1); | ||
1991 | return; | ||
1992 | } | ||
1993 | } | ||
1994 | } | ||
1995 | |||
1996 | |||
1997 | /** | ||
1998 | * Function called if the connection to the peer has been stalled for a while, | ||
1999 | * possibly due to a missed ACK. Poll the peer about its ACK status. | ||
2000 | * | ||
2001 | * @param cls Closure (poll ctx). | ||
2002 | * @param tc TaskContext. | ||
2003 | */ | ||
2004 | static void | ||
2005 | tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
2006 | { | ||
2007 | struct MeshFlowControl *fc = cls; | ||
2008 | struct GNUNET_MESH_Poll msg; | ||
2009 | struct MeshTunnel *t = fc->t; | ||
2010 | GNUNET_PEER_Id peer; | ||
2011 | |||
2012 | fc->poll_task = GNUNET_SCHEDULER_NO_TASK; | ||
2013 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
2014 | { | ||
2015 | return; | ||
2016 | } | ||
2017 | |||
2018 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** Polling!\n"); | ||
2019 | |||
2020 | GNUNET_PEER_resolve (t->id.oid, &msg.oid); | ||
2021 | |||
2022 | if (fc == &t->prev_fc) | ||
2023 | { | ||
2024 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** prev peer!\n"); | ||
2025 | peer = t->prev_hop; | ||
2026 | } | ||
2027 | else if (fc == &t->next_fc) | ||
2028 | { | ||
2029 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** next peer!\n"); | ||
2030 | peer = t->next_hop; | ||
2031 | } | ||
2032 | else | ||
2033 | { | ||
2034 | GNUNET_break (0); | ||
2035 | return; | ||
2036 | } | ||
2037 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** peer: %s!\n", | ||
2038 | GNUNET_i2s(GNUNET_PEER_resolve2 (peer))); | ||
2039 | if (0 == peer) | ||
2040 | { | ||
2041 | if (GNUNET_YES == t->destroy) | ||
2042 | tunnel_destroy (t); | ||
2043 | else | ||
2044 | GNUNET_break (0); | ||
2045 | |||
2046 | return; | ||
2047 | } | ||
2048 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL); | ||
2049 | msg.header.size = htons (sizeof (msg)); | ||
2050 | msg.tid = htonl (t->id.tid); | ||
2051 | msg.pid = htonl (peer_get_first_payload_pid (peer_get_short (peer), t)); | ||
2052 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " *** pid (%u)!\n", ntohl (msg.pid)); | ||
2053 | send_prebuilt_message (&msg.header, peer, t); | ||
2054 | fc->poll_time = GNUNET_TIME_STD_BACKOFF (fc->poll_time); | ||
2055 | fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, | ||
2056 | &tunnel_poll, fc); | ||
2057 | } | ||
2058 | |||
2059 | |||
2060 | /** | ||
2061 | * Build a PeerPath from the paths returned from the DHT, reversing the paths | ||
2062 | * to obtain a local peer -> destination path and interning the peer ids. | ||
2063 | * | ||
2064 | * @return Newly allocated and created path | ||
2065 | */ | ||
2066 | static struct MeshPeerPath * | ||
2067 | path_build_from_dht (const struct GNUNET_PeerIdentity *get_path, | ||
2068 | unsigned int get_path_length, | ||
2069 | const struct GNUNET_PeerIdentity *put_path, | ||
2070 | unsigned int put_path_length) | ||
2071 | { | ||
2072 | struct MeshPeerPath *p; | ||
2073 | GNUNET_PEER_Id id; | ||
2074 | int i; | ||
2075 | |||
2076 | p = path_new (1); | ||
2077 | p->peers[0] = myid; | ||
2078 | GNUNET_PEER_change_rc (myid, 1); | ||
2079 | i = get_path_length; | ||
2080 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i); | ||
2081 | for (i--; i >= 0; i--) | ||
2082 | { | ||
2083 | id = GNUNET_PEER_intern (&get_path[i]); | ||
2084 | if (p->length > 0 && id == p->peers[p->length - 1]) | ||
2085 | { | ||
2086 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n"); | ||
2087 | GNUNET_PEER_change_rc (id, -1); | ||
2088 | } | ||
2089 | else | ||
2090 | { | ||
2091 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n", | ||
2092 | GNUNET_i2s (&get_path[i])); | ||
2093 | p->length++; | ||
2094 | p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length); | ||
2095 | p->peers[p->length - 1] = id; | ||
2096 | } | ||
2097 | } | ||
2098 | i = put_path_length; | ||
2099 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i); | ||
2100 | for (i--; i >= 0; i--) | ||
2101 | { | ||
2102 | id = GNUNET_PEER_intern (&put_path[i]); | ||
2103 | if (id == myid) | ||
2104 | { | ||
2105 | /* PUT path went through us, so discard the path up until now and start | ||
2106 | * from here to get a much shorter (and loop-free) path. | ||
2107 | */ | ||
2108 | path_destroy (p); | ||
2109 | p = path_new (0); | ||
2110 | } | ||
2111 | if (p->length > 0 && id == p->peers[p->length - 1]) | ||
2112 | { | ||
2113 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n"); | ||
2114 | GNUNET_PEER_change_rc (id, -1); | ||
2115 | } | ||
2116 | else | ||
2117 | { | ||
2118 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n", | ||
2119 | GNUNET_i2s (&put_path[i])); | ||
2120 | p->length++; | ||
2121 | p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length); | ||
2122 | p->peers[p->length - 1] = id; | ||
2123 | } | ||
2124 | } | ||
2125 | #if MESH_DEBUG | ||
2126 | if (get_path_length > 0) | ||
2127 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n", | ||
2128 | GNUNET_i2s (&get_path[0])); | ||
2129 | if (put_path_length > 0) | ||
2130 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n", | ||
2131 | GNUNET_i2s (&put_path[0])); | ||
2132 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n", | ||
2133 | p->length); | ||
2134 | for (i = 0; i < p->length; i++) | ||
2135 | { | ||
2136 | struct GNUNET_PeerIdentity peer_id; | ||
2137 | |||
2138 | GNUNET_PEER_resolve (p->peers[i], &peer_id); | ||
2139 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i], | ||
2140 | GNUNET_i2s (&peer_id)); | ||
2141 | } | ||
2142 | #endif | ||
2143 | return p; | ||
2144 | } | ||
2145 | |||
2146 | |||
2147 | /** | ||
2148 | * Adds a path to the peer_infos of all the peers in the path | ||
2149 | * | ||
2150 | * @param p Path to process. | ||
2151 | * @param confirmed Whether we know if the path works or not. | ||
2152 | */ | ||
2153 | static void | ||
2154 | path_add_to_peers (struct MeshPeerPath *p, int confirmed) | ||
2155 | { | ||
2156 | unsigned int i; | ||
2157 | |||
2158 | /* TODO: invert and add */ | ||
2159 | for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ; | ||
2160 | for (i++; i < p->length; i++) | ||
2161 | { | ||
2162 | struct MeshPeer *aux; | ||
2163 | struct MeshPeerPath *copy; | ||
2164 | |||
2165 | aux = peer_get_short (p->peers[i]); | ||
2166 | copy = path_duplicate (p); | ||
2167 | copy->length = i + 1; | ||
2168 | peer_add_path (aux, copy, p->length < 3 ? GNUNET_NO : confirmed); | ||
2169 | } | ||
2170 | } | ||
2171 | |||
2172 | |||
2173 | /** | ||
2174 | * Search for a tunnel among the incoming tunnels | ||
2175 | * | ||
2176 | * @param tid the local id of the tunnel | ||
2177 | * | ||
2178 | * @return tunnel handler, NULL if doesn't exist | ||
2179 | */ | ||
2180 | static struct MeshTunnel * | ||
2181 | tunnel_get_incoming (MESH_TunnelNumber tid) | ||
2182 | { | ||
2183 | GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV); | ||
2184 | return GNUNET_CONTAINER_multihashmap32_get (incoming_tunnels, tid); | ||
2185 | } | ||
2186 | |||
2187 | |||
2188 | /** | ||
2189 | * Search for a tunnel among the tunnels for a client | ||
2190 | * | ||
2191 | * @param c the client whose tunnels to search in | ||
2192 | * @param tid the local id of the tunnel | ||
2193 | * | ||
2194 | * @return tunnel handler, NULL if doesn't exist | ||
2195 | */ | ||
2196 | static struct MeshTunnel * | ||
2197 | tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid) | ||
2198 | { | ||
2199 | if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI)) | ||
2200 | { | ||
2201 | GNUNET_break_op (0); | ||
2202 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "TID %X not a local tid\n", tid); | ||
2203 | return NULL; | ||
2204 | } | ||
2205 | if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) | ||
2206 | { | ||
2207 | return tunnel_get_incoming (tid); | ||
2208 | } | ||
2209 | else | ||
2210 | { | ||
2211 | return GNUNET_CONTAINER_multihashmap32_get (c->own_tunnels, tid); | ||
2212 | } | ||
2213 | } | ||
2214 | |||
2215 | |||
2216 | /** | ||
2217 | * Search for a tunnel by global ID using PEER_ID | ||
2218 | * | ||
2219 | * @param pi owner of the tunnel | ||
2220 | * @param tid global tunnel number | ||
2221 | * | ||
2222 | * @return tunnel handler, NULL if doesn't exist | ||
2223 | */ | ||
2224 | static struct MeshTunnel * | ||
2225 | tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid) | ||
2226 | { | ||
2227 | struct MESH_TunnelID id; | ||
2228 | struct GNUNET_HashCode hash; | ||
2229 | |||
2230 | id.oid = pi; | ||
2231 | id.tid = tid; | ||
2232 | |||
2233 | GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash); | ||
2234 | return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash); | ||
2235 | } | ||
2236 | |||
2237 | |||
2238 | /** | ||
2239 | * Search for a tunnel by global ID using full PeerIdentities | ||
2240 | * | ||
2241 | * @param oid owner of the tunnel | ||
2242 | * @param tid global tunnel number | ||
2243 | * | ||
2244 | * @return tunnel handler, NULL if doesn't exist | ||
2245 | */ | ||
2246 | static struct MeshTunnel * | ||
2247 | tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid) | ||
2248 | { | ||
2249 | return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid); | ||
2250 | } | ||
2251 | |||
2252 | |||
2253 | /** | ||
2254 | * Change the tunnel state. | ||
2255 | * | ||
2256 | * @param t Tunnel whose ttate to change. | ||
2257 | * @param state New state. | ||
2258 | */ | ||
2259 | static void | ||
2260 | tunnel_change_state (struct MeshTunnel *t, enum MeshTunnelState state) | ||
2261 | { | ||
2262 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2263 | "Tunnel %s[%X] state was %s\n", | ||
2264 | GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid, | ||
2265 | GNUNET_MESH_DEBUG_S2S (t->state)); | ||
2266 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2267 | "Tunnel %s[%X] state is now %s\n", | ||
2268 | GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid, | ||
2269 | GNUNET_MESH_DEBUG_S2S (state)); | ||
2270 | t->state = state; | ||
2271 | } | ||
2272 | |||
2273 | |||
2274 | |||
2275 | /** | ||
2276 | * Add a client to a tunnel, initializing all needed data structures. | ||
2277 | * | ||
2278 | * @param t Tunnel to which add the client. | ||
2279 | * @param c Client which to add to the tunnel. | ||
2280 | */ | ||
2281 | static void | ||
2282 | tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c) | ||
2283 | { | ||
2284 | if (NULL != t->client) | ||
2285 | { | ||
2286 | GNUNET_break(0); | ||
2287 | return; | ||
2288 | } | ||
2289 | if (GNUNET_OK != | ||
2290 | GNUNET_CONTAINER_multihashmap32_put (c->incoming_tunnels, | ||
2291 | t->local_tid_dest, t, | ||
2292 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) | ||
2293 | { | ||
2294 | GNUNET_break (0); | ||
2295 | return; | ||
2296 | } | ||
2297 | if (GNUNET_OK != | ||
2298 | GNUNET_CONTAINER_multihashmap32_put (incoming_tunnels, | ||
2299 | t->local_tid_dest, t, | ||
2300 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) | ||
2301 | { | ||
2302 | GNUNET_break (0); | ||
2303 | return; | ||
2304 | } | ||
2305 | t->client = c; | ||
2306 | } | ||
2307 | |||
2308 | |||
2309 | static void | ||
2310 | tunnel_use_path (struct MeshTunnel *t, struct MeshPeerPath *p) | ||
2311 | { | ||
2312 | unsigned int own_pos; | ||
2313 | |||
2314 | for (own_pos = 0; own_pos < p->length; own_pos++) | ||
2315 | { | ||
2316 | if (p->peers[own_pos] == myid) | ||
2317 | break; | ||
2318 | } | ||
2319 | if (own_pos > p->length - 1) | ||
2320 | { | ||
2321 | GNUNET_break (0); | ||
2322 | return; | ||
2323 | } | ||
2324 | |||
2325 | if (own_pos < p->length - 1) | ||
2326 | t->next_hop = p->peers[own_pos + 1]; | ||
2327 | else | ||
2328 | t->next_hop = p->peers[own_pos]; | ||
2329 | GNUNET_PEER_change_rc (t->next_hop, 1); | ||
2330 | if (0 < own_pos) | ||
2331 | t->prev_hop = p->peers[own_pos - 1]; | ||
2332 | else | ||
2333 | t->prev_hop = p->peers[0]; | ||
2334 | GNUNET_PEER_change_rc (t->prev_hop, 1); | ||
2335 | |||
2336 | if (NULL != t->path) | ||
2337 | path_destroy (t->path); | ||
2338 | t->path = path_duplicate (p); | ||
2339 | if (0 == own_pos) | ||
2340 | { | ||
2341 | if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task) | ||
2342 | GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task); | ||
2343 | t->fwd_maintenance_task = | ||
2344 | GNUNET_SCHEDULER_add_delayed (refresh_path_time, | ||
2345 | &tunnel_fwd_keepalive, t); | ||
2346 | } | ||
2347 | } | ||
2348 | |||
2349 | |||
2350 | /** | ||
2351 | * Notifies a tunnel that a connection has broken that affects at least | ||
2352 | * some of its peers. Sends a notification towards the root of the tree. | ||
2353 | * In case the peer is the owner of the tree, notifies the client that owns | ||
2354 | * the tunnel and tries to reconnect. | ||
2355 | * | ||
2356 | * @param t Tunnel affected. | ||
2357 | * @param p1 Peer that got disconnected from p2. | ||
2358 | * @param p2 Peer that got disconnected from p1. | ||
2359 | * | ||
2360 | * @return Short ID of the peer disconnected (either p1 or p2). | ||
2361 | * 0 if the tunnel remained unaffected. | ||
2362 | */ | ||
2363 | static GNUNET_PEER_Id | ||
2364 | tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1, | ||
2365 | GNUNET_PEER_Id p2) | ||
2366 | { | ||
2367 | // if (myid != p1 && myid != p2) FIXME | ||
2368 | // { | ||
2369 | // return; | ||
2370 | // } | ||
2371 | // | ||
2372 | // if (tree_get_predecessor (t->tree) != 0) | ||
2373 | // { | ||
2374 | // /* We are the peer still connected, notify owner of the disconnection. */ | ||
2375 | // struct GNUNET_MESH_PathBroken msg; | ||
2376 | // struct GNUNET_PeerIdentity neighbor; | ||
2377 | // | ||
2378 | // msg.header.size = htons (sizeof (msg)); | ||
2379 | // msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN); | ||
2380 | // GNUNET_PEER_resolve (t->id.oid, &msg.oid); | ||
2381 | // msg.tid = htonl (t->id.tid); | ||
2382 | // msg.peer1 = my_full_id; | ||
2383 | // GNUNET_PEER_resolve (pid, &msg.peer2); | ||
2384 | // GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor); | ||
2385 | // send_prebuilt_message (&msg.header, &neighbor, t); | ||
2386 | // } | ||
2387 | return 0; | ||
2388 | } | ||
2389 | |||
2390 | |||
2391 | /** | ||
2392 | * Send an end-to-end FWD ACK message for the most recent in-sequence payload. | ||
2393 | * | ||
2394 | * @param t Tunnel this is about. | ||
2395 | * @param fwd Is for FWD traffic? (ACK dest->owner) | ||
2396 | */ | ||
2397 | static void | ||
2398 | tunnel_send_data_ack (struct MeshTunnel *t, int fwd) | ||
2399 | { | ||
2400 | struct GNUNET_MESH_DataACK msg; | ||
2401 | struct MeshTunnelReliability *rel; | ||
2402 | struct MeshReliableMessage *copy; | ||
2403 | GNUNET_PEER_Id hop; | ||
2404 | uint64_t mask; | ||
2405 | unsigned int delta; | ||
2406 | |||
2407 | rel = fwd ? t->bck_rel : t->fwd_rel; | ||
2408 | hop = fwd ? t->prev_hop : t->next_hop; | ||
2409 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2410 | "send_data_ack for %u\n", | ||
2411 | rel->mid_recv - 1); | ||
2412 | |||
2413 | if (GNUNET_NO == t->reliable) | ||
2414 | { | ||
2415 | GNUNET_break_op (0); | ||
2416 | return; | ||
2417 | } | ||
2418 | msg.header.type = htons (fwd ? GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK : | ||
2419 | GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK); | ||
2420 | msg.header.size = htons (sizeof (msg)); | ||
2421 | msg.tid = htonl (t->id.tid); | ||
2422 | GNUNET_PEER_resolve (t->id.oid, &msg.oid); | ||
2423 | msg.mid = htonl (rel->mid_recv - 1); | ||
2424 | msg.futures = 0; | ||
2425 | for (copy = rel->head_recv; NULL != copy; copy = copy->next) | ||
2426 | { | ||
2427 | delta = copy->mid - rel->mid_recv; | ||
2428 | if (63 < delta) | ||
2429 | break; | ||
2430 | mask = 0x1LL << delta; | ||
2431 | msg.futures |= mask; | ||
2432 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2433 | " setting bit for %u (delta %u) (%llX) -> %llX\n", | ||
2434 | copy->mid, delta, mask, msg.futures); | ||
2435 | } | ||
2436 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " final futures %llX\n", msg.futures); | ||
2437 | |||
2438 | send_prebuilt_message (&msg.header, hop, t); | ||
2439 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_data_ack END\n"); | ||
2440 | } | ||
2441 | |||
2442 | |||
2443 | /** | ||
2444 | * Send an ACK informing the predecessor about the available buffer space. | ||
2445 | * In case there is no predecessor, inform the owning client. | ||
2446 | * If buffering is off, send only on behalf of children or self if endpoint. | ||
2447 | * If buffering is on, send when sent to children and buffer space is free. | ||
2448 | * Note that although the name is fwd_ack, the FWD mean forward *traffic*, | ||
2449 | * the ACK itself goes "back" (towards root). | ||
2450 | * | ||
2451 | * @param t Tunnel on which to send the ACK. | ||
2452 | * @param type Type of message that triggered the ACK transmission. | ||
2453 | * @param fwd Is this FWD ACK? (Going dest->owner) | ||
2454 | */ | ||
2455 | static void | ||
2456 | tunnel_send_ack (struct MeshTunnel *t, uint16_t type, int fwd) | ||
2457 | { | ||
2458 | struct MeshTunnelReliability *rel; | ||
2459 | struct MeshFlowControl *next_fc; | ||
2460 | struct MeshFlowControl *prev_fc; | ||
2461 | struct MeshClient *c; | ||
2462 | struct MeshClient *o; | ||
2463 | GNUNET_PEER_Id hop; | ||
2464 | uint32_t delta_mid; | ||
2465 | uint32_t ack; | ||
2466 | int delta; | ||
2467 | |||
2468 | rel = fwd ? t->fwd_rel : t->bck_rel; | ||
2469 | c = fwd ? t->client : t->owner; | ||
2470 | o = fwd ? t->owner : t->client; | ||
2471 | next_fc = fwd ? &t->next_fc : &t->prev_fc; | ||
2472 | prev_fc = fwd ? &t->prev_fc : &t->next_fc; | ||
2473 | hop = fwd ? t->prev_hop : t->next_hop; | ||
2474 | |||
2475 | switch (type) | ||
2476 | { | ||
2477 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST: | ||
2478 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN: | ||
2479 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2480 | "ACK due to %s\n", | ||
2481 | GNUNET_MESH_DEBUG_M2S (type)); | ||
2482 | if (GNUNET_YES == t->nobuffer && (GNUNET_NO == t->reliable || NULL == c)) | ||
2483 | { | ||
2484 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n"); | ||
2485 | return; | ||
2486 | } | ||
2487 | if (GNUNET_YES == t->reliable && NULL != c) | ||
2488 | tunnel_send_data_ack (t, fwd); | ||
2489 | break; | ||
2490 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK: | ||
2491 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK: | ||
2492 | case GNUNET_MESSAGE_TYPE_MESH_ACK: | ||
2493 | case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK: | ||
2494 | break; | ||
2495 | case GNUNET_MESSAGE_TYPE_MESH_POLL: | ||
2496 | case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK: | ||
2497 | t->force_ack = GNUNET_YES; | ||
2498 | break; | ||
2499 | default: | ||
2500 | GNUNET_break (0); | ||
2501 | } | ||
2502 | |||
2503 | /* Check if we need to transmit the ACK */ | ||
2504 | if (NULL == o && | ||
2505 | prev_fc->last_ack_sent - prev_fc->last_pid_recv > 3 && | ||
2506 | GNUNET_NO == t->force_ack) | ||
2507 | { | ||
2508 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n"); | ||
2509 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2510 | " last pid recv: %u, last ack sent: %u\n", | ||
2511 | prev_fc->last_pid_recv, prev_fc->last_ack_sent); | ||
2512 | return; | ||
2513 | } | ||
2514 | |||
2515 | /* Ok, ACK might be necessary, what PID to ACK? */ | ||
2516 | delta = t->queue_max - next_fc->queue_n; | ||
2517 | if (NULL != o && GNUNET_YES == t->reliable && NULL != rel->head_sent) | ||
2518 | delta_mid = rel->mid_sent - rel->head_sent->mid; | ||
2519 | else | ||
2520 | delta_mid = 0; | ||
2521 | if (0 > delta || (GNUNET_YES == t->reliable && | ||
2522 | NULL != o && | ||
2523 | (10 < rel->n_sent || 64 <= delta_mid))) | ||
2524 | delta = 0; | ||
2525 | if (NULL != o && delta > 1) | ||
2526 | delta = 1; | ||
2527 | ack = prev_fc->last_pid_recv + delta; | ||
2528 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack); | ||
2529 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2530 | " last pid %u, last ack %u, qmax %u, q %u\n", | ||
2531 | prev_fc->last_pid_recv, prev_fc->last_ack_sent, | ||
2532 | t->queue_max, next_fc->queue_n); | ||
2533 | if (ack == prev_fc->last_ack_sent && GNUNET_NO == t->force_ack) | ||
2534 | { | ||
2535 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n"); | ||
2536 | return; | ||
2537 | } | ||
2538 | |||
2539 | prev_fc->last_ack_sent = ack; | ||
2540 | if (NULL != o) | ||
2541 | send_local_ack (t, o, fwd); | ||
2542 | else if (0 != hop) | ||
2543 | send_ack (t, hop, ack); | ||
2544 | else | ||
2545 | GNUNET_break (GNUNET_YES == t->destroy); | ||
2546 | t->force_ack = GNUNET_NO; | ||
2547 | } | ||
2548 | |||
2549 | |||
2550 | /** | ||
2551 | * Modify the mesh message TID from global to local and send to client. | ||
2552 | * | ||
2553 | * @param t Tunnel on which to send the message. | ||
2554 | * @param msg Message to modify and send. | ||
2555 | * @param c Client to send to. | ||
2556 | * @param tid Tunnel ID to use (c can be both owner and client). | ||
2557 | */ | ||
2558 | static void | ||
2559 | tunnel_send_client_to_tid (struct MeshTunnel *t, | ||
2560 | const struct GNUNET_MESH_Data *msg, | ||
2561 | struct MeshClient *c, MESH_TunnelNumber tid) | ||
2562 | { | ||
2563 | struct GNUNET_MESH_LocalData *copy; | ||
2564 | uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_MESH_Data); | ||
2565 | char cbuf[size + sizeof (struct GNUNET_MESH_LocalData)]; | ||
2566 | |||
2567 | if (size < sizeof (struct GNUNET_MessageHeader)) | ||
2568 | { | ||
2569 | GNUNET_break_op (0); | ||
2570 | return; | ||
2571 | } | ||
2572 | if (NULL == c) | ||
2573 | { | ||
2574 | GNUNET_break (0); | ||
2575 | return; | ||
2576 | } | ||
2577 | copy = (struct GNUNET_MESH_LocalData *) cbuf; | ||
2578 | memcpy (©[1], &msg[1], size); | ||
2579 | copy->header.size = htons (sizeof (struct GNUNET_MESH_LocalData) + size); | ||
2580 | copy->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA); | ||
2581 | copy->tid = htonl (tid); | ||
2582 | GNUNET_SERVER_notification_context_unicast (nc, c->handle, | ||
2583 | ©->header, GNUNET_NO); | ||
2584 | } | ||
2585 | |||
2586 | /** | ||
2587 | * Modify the unicast message TID from global to local and send to client. | ||
2588 | * | ||
2589 | * @param t Tunnel on which to send the message. | ||
2590 | * @param msg Message to modify and send. | ||
2591 | * @param fwd Forward? | ||
2592 | */ | ||
2593 | static void | ||
2594 | tunnel_send_client_data (struct MeshTunnel *t, | ||
2595 | const struct GNUNET_MESH_Data *msg, | ||
2596 | int fwd) | ||
2597 | { | ||
2598 | if (fwd) | ||
2599 | tunnel_send_client_to_tid (t, msg, t->client, t->local_tid_dest); | ||
2600 | else | ||
2601 | tunnel_send_client_to_tid (t, msg, t->owner, t->local_tid); | ||
2602 | } | ||
2603 | |||
2604 | |||
2605 | /** | ||
2606 | * Send up to 64 buffered messages to the client for in order delivery. | ||
2607 | * | ||
2608 | * @param t Tunnel on which to empty the message buffer. | ||
2609 | * @param c Client to send to. | ||
2610 | * @param rel Reliability structure to corresponding peer. | ||
2611 | * If rel == t->bck_rel, this is FWD data. | ||
2612 | */ | ||
2613 | static void | ||
2614 | tunnel_send_client_buffered_data (struct MeshTunnel *t, struct MeshClient *c, | ||
2615 | struct MeshTunnelReliability *rel) | ||
2616 | { | ||
2617 | ; | ||
2618 | struct MeshReliableMessage *copy; | ||
2619 | struct MeshReliableMessage *next; | ||
2620 | |||
2621 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data\n"); | ||
2622 | for (copy = rel->head_recv; NULL != copy; copy = next) | ||
2623 | { | ||
2624 | next = copy->next; | ||
2625 | if (copy->mid == rel->mid_recv) | ||
2626 | { | ||
2627 | struct GNUNET_MESH_Data *msg = (struct GNUNET_MESH_Data *) ©[1]; | ||
2628 | |||
2629 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2630 | " have %u! now expecting %u\n", | ||
2631 | copy->mid, rel->mid_recv + 1); | ||
2632 | tunnel_send_client_data (t, msg, (rel == t->bck_rel)); | ||
2633 | rel->mid_recv++; | ||
2634 | GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy); | ||
2635 | GNUNET_free (copy); | ||
2636 | } | ||
2637 | else | ||
2638 | { | ||
2639 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2640 | " don't have %u, next is %u\n", | ||
2641 | rel->mid_recv, | ||
2642 | copy->mid); | ||
2643 | return; | ||
2644 | } | ||
2645 | } | ||
2646 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "send_buffered_data END\n"); | ||
2647 | } | ||
2648 | |||
2649 | |||
2650 | /** | ||
2651 | * We have received a message out of order, buffer it until we receive | ||
2652 | * the missing one and we can feed the rest to the client. | ||
2653 | * | ||
2654 | * @param t Tunnel to add to. | ||
2655 | * @param msg Message to buffer. | ||
2656 | * @param rel Reliability data to the corresponding direction. | ||
2657 | */ | ||
2658 | static void | ||
2659 | tunnel_add_buffered_data (struct MeshTunnel *t, | ||
2660 | const struct GNUNET_MESH_Data *msg, | ||
2661 | struct MeshTunnelReliability *rel) | ||
2662 | { | ||
2663 | struct MeshReliableMessage *copy; | ||
2664 | struct MeshReliableMessage *prev; | ||
2665 | uint32_t mid; | ||
2666 | uint16_t size; | ||
2667 | |||
2668 | size = ntohs (msg->header.size); | ||
2669 | mid = ntohl (msg->mid); | ||
2670 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data %u\n", mid); | ||
2671 | |||
2672 | copy = GNUNET_malloc (sizeof (*copy) + size); | ||
2673 | copy->mid = mid; | ||
2674 | copy->rel = rel; | ||
2675 | memcpy (©[1], msg, size); | ||
2676 | |||
2677 | // FIXME do something better than O(n), although n < 64... | ||
2678 | // FIXME start from the end (most messages are the latest ones) | ||
2679 | for (prev = rel->head_recv; NULL != prev; prev = prev->next) | ||
2680 | { | ||
2681 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " prev %u\n", prev->mid); | ||
2682 | if (GMC_is_pid_bigger (prev->mid, mid)) | ||
2683 | { | ||
2684 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " bingo!\n"); | ||
2685 | GNUNET_CONTAINER_DLL_insert_before (rel->head_recv, rel->tail_recv, | ||
2686 | prev, copy); | ||
2687 | return; | ||
2688 | } | ||
2689 | } | ||
2690 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " insert at tail!\n"); | ||
2691 | GNUNET_CONTAINER_DLL_insert_tail (rel->head_recv, rel->tail_recv, copy); | ||
2692 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add_buffered_data END\n"); | ||
2693 | } | ||
2694 | |||
2695 | |||
2696 | /** | ||
2697 | * Destroy a reliable message after it has been acknowledged, either by | ||
2698 | * direct mid ACK or bitfield. Updates the appropriate data structures and | ||
2699 | * timers and frees all memory. | ||
2700 | * | ||
2701 | * @param copy Message that is no longer needed: remote peer got it. | ||
2702 | */ | ||
2703 | static void | ||
2704 | tunnel_free_reliable_message (struct MeshReliableMessage *copy) | ||
2705 | { | ||
2706 | struct MeshTunnelReliability *rel; | ||
2707 | struct GNUNET_TIME_Relative time; | ||
2708 | |||
2709 | rel = copy->rel; | ||
2710 | time = GNUNET_TIME_absolute_get_duration (copy->timestamp); | ||
2711 | rel->expected_delay.rel_value_us *= 7; | ||
2712 | rel->expected_delay.rel_value_us += time.rel_value_us; | ||
2713 | rel->expected_delay.rel_value_us /= 8; | ||
2714 | rel->n_sent--; | ||
2715 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Freeing %u\n", copy->mid); | ||
2716 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent); | ||
2717 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! took %s\n", | ||
2718 | GNUNET_STRINGS_relative_time_to_string (time, GNUNET_NO)); | ||
2719 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! new expected delay %s\n", | ||
2720 | GNUNET_STRINGS_relative_time_to_string (rel->expected_delay, | ||
2721 | GNUNET_NO)); | ||
2722 | rel->retry_timer = rel->expected_delay; | ||
2723 | GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy); | ||
2724 | GNUNET_free (copy); | ||
2725 | } | ||
2726 | |||
2727 | |||
2728 | /** | ||
2729 | * Destroy all reliable messages queued for a tunnel, | ||
2730 | * during a tunnel destruction. | ||
2731 | * Frees the reliability structure itself. | ||
2732 | * | ||
2733 | * @param rel Reliability data for a tunnel. | ||
2734 | */ | ||
2735 | static void | ||
2736 | tunnel_free_reliable_all (struct MeshTunnelReliability *rel) | ||
2737 | { | ||
2738 | struct MeshReliableMessage *copy; | ||
2739 | struct MeshReliableMessage *next; | ||
2740 | |||
2741 | if (NULL == rel) | ||
2742 | return; | ||
2743 | |||
2744 | for (copy = rel->head_recv; NULL != copy; copy = next) | ||
2745 | { | ||
2746 | next = copy->next; | ||
2747 | GNUNET_CONTAINER_DLL_remove (rel->head_recv, rel->tail_recv, copy); | ||
2748 | GNUNET_free (copy); | ||
2749 | } | ||
2750 | for (copy = rel->head_sent; NULL != copy; copy = next) | ||
2751 | { | ||
2752 | next = copy->next; | ||
2753 | GNUNET_CONTAINER_DLL_remove (rel->head_sent, rel->tail_sent, copy); | ||
2754 | GNUNET_free (copy); | ||
2755 | } | ||
2756 | if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task) | ||
2757 | GNUNET_SCHEDULER_cancel (rel->retry_task); | ||
2758 | GNUNET_free (rel); | ||
2759 | } | ||
2760 | |||
2761 | |||
2762 | /** | ||
2763 | * Mark future messages as ACK'd. | ||
2764 | * | ||
2765 | * @param t Tunnel whose sent buffer to clean. | ||
2766 | * @param msg DataACK message with a bitfield of future ACK'd messages. | ||
2767 | * @param rel Reliability data. | ||
2768 | */ | ||
2769 | static void | ||
2770 | tunnel_free_sent_reliable (struct MeshTunnel *t, | ||
2771 | const struct GNUNET_MESH_DataACK *msg, | ||
2772 | struct MeshTunnelReliability *rel) | ||
2773 | { | ||
2774 | struct MeshReliableMessage *copy; | ||
2775 | struct MeshReliableMessage *next; | ||
2776 | uint64_t bitfield; | ||
2777 | uint64_t mask; | ||
2778 | uint32_t mid; | ||
2779 | uint32_t target; | ||
2780 | unsigned int i; | ||
2781 | |||
2782 | bitfield = msg->futures; | ||
2783 | mid = ntohl (msg->mid); | ||
2784 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2785 | "free_sent_reliable %u %llX\n", | ||
2786 | mid, bitfield); | ||
2787 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2788 | " rel %p, head %p\n", | ||
2789 | rel, rel->head_sent); | ||
2790 | for (i = 0, copy = rel->head_sent; | ||
2791 | i < 64 && NULL != copy && 0 != bitfield; | ||
2792 | i++) | ||
2793 | { | ||
2794 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2795 | " trying bit %u (mid %u)\n", | ||
2796 | i, mid + i + 1); | ||
2797 | mask = 0x1LL << i; | ||
2798 | if (0 == (bitfield & mask)) | ||
2799 | continue; | ||
2800 | |||
2801 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " set!\n"); | ||
2802 | /* Bit was set, clear the bit from the bitfield */ | ||
2803 | bitfield &= ~mask; | ||
2804 | |||
2805 | /* The i-th bit was set. Do we have that copy? */ | ||
2806 | /* Skip copies with mid < target */ | ||
2807 | target = mid + i + 1; | ||
2808 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " target %u\n", target); | ||
2809 | while (NULL != copy && GMC_is_pid_bigger (target, copy->mid)) | ||
2810 | copy = copy->next; | ||
2811 | |||
2812 | /* Did we run out of copies? (previously freed, it's ok) */ | ||
2813 | if (NULL == copy) | ||
2814 | { | ||
2815 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run out of copies...\n"); | ||
2816 | return; | ||
2817 | } | ||
2818 | |||
2819 | /* Did we overshoot the target? (previously freed, it's ok) */ | ||
2820 | if (GMC_is_pid_bigger (copy->mid, target)) | ||
2821 | { | ||
2822 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " next copy %u\n", copy->mid); | ||
2823 | continue; | ||
2824 | } | ||
2825 | |||
2826 | /* Now copy->mid == target, free it */ | ||
2827 | next = copy->next; | ||
2828 | tunnel_free_reliable_message (copy); | ||
2829 | copy = next; | ||
2830 | } | ||
2831 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "free_sent_reliable END\n"); | ||
2832 | } | ||
2833 | |||
2834 | |||
2835 | /** | ||
2836 | * We haven't received an ACK after a certain time: restransmit the message. | ||
2837 | * | ||
2838 | * @param cls Closure (MeshReliableMessage with the message to restransmit) | ||
2839 | * @param tc TaskContext. | ||
2840 | */ | ||
2841 | static void | ||
2842 | tunnel_retransmit_message (void *cls, | ||
2843 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
2844 | { | ||
2845 | struct MeshTunnelReliability *rel = cls; | ||
2846 | struct MeshReliableMessage *copy; | ||
2847 | struct MeshFlowControl *fc; | ||
2848 | struct MeshPeerQueue *q; | ||
2849 | struct MeshPeer *pi; | ||
2850 | struct MeshTunnel *t; | ||
2851 | struct GNUNET_MESH_Data *payload; | ||
2852 | GNUNET_PEER_Id hop; | ||
2853 | |||
2854 | rel->retry_task = GNUNET_SCHEDULER_NO_TASK; | ||
2855 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
2856 | return; | ||
2857 | |||
2858 | t = rel->t; | ||
2859 | copy = rel->head_sent; | ||
2860 | if (NULL == copy) | ||
2861 | { | ||
2862 | GNUNET_break (0); | ||
2863 | return; | ||
2864 | } | ||
2865 | |||
2866 | /* Search the message to be retransmitted in the outgoing queue */ | ||
2867 | payload = (struct GNUNET_MESH_Data *) ©[1]; | ||
2868 | hop = rel == t->fwd_rel ? t->next_hop : t->prev_hop; | ||
2869 | fc = rel == t->fwd_rel ? &t->prev_fc : &t->next_fc; | ||
2870 | pi = peer_get_short (hop); | ||
2871 | for (q = pi->queue_head; NULL != q; q = q->next) | ||
2872 | { | ||
2873 | if (ntohs (payload->header.type) == q->type) | ||
2874 | { | ||
2875 | struct GNUNET_MESH_Data *queued_data = q->cls; | ||
2876 | |||
2877 | if (queued_data->mid == payload->mid) | ||
2878 | break; | ||
2879 | } | ||
2880 | } | ||
2881 | |||
2882 | /* Message not found in the queue */ | ||
2883 | if (NULL == q) | ||
2884 | { | ||
2885 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! RETRANSMIT %u\n", copy->mid); | ||
2886 | |||
2887 | fc->last_ack_sent++; | ||
2888 | fc->last_pid_recv++; | ||
2889 | payload->pid = htonl (fc->last_pid_recv); | ||
2890 | send_prebuilt_message (&payload->header, hop, t); | ||
2891 | GNUNET_STATISTICS_update (stats, "# data retransmitted", 1, GNUNET_NO); | ||
2892 | } | ||
2893 | else | ||
2894 | { | ||
2895 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! STILL IN QUEUE %u\n", copy->mid); | ||
2896 | } | ||
2897 | |||
2898 | rel->retry_timer = GNUNET_TIME_STD_BACKOFF (rel->retry_timer); | ||
2899 | rel->retry_task = GNUNET_SCHEDULER_add_delayed (rel->retry_timer, | ||
2900 | &tunnel_retransmit_message, | ||
2901 | cls); | ||
2902 | } | ||
2903 | |||
2904 | |||
2905 | /** | ||
2906 | * Send keepalive packets for a tunnel. | ||
2907 | * | ||
2908 | * @param t Tunnel to keep alive.. | ||
2909 | * @param fwd Is this a FWD keepalive? (owner -> dest). | ||
2910 | */ | ||
2911 | static void | ||
2912 | tunnel_keepalive (struct MeshTunnel *t, int fwd) | ||
2913 | { | ||
2914 | struct GNUNET_MESH_TunnelKeepAlive *msg; | ||
2915 | size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive); | ||
2916 | char cbuf[size]; | ||
2917 | GNUNET_PEER_Id hop; | ||
2918 | uint16_t type; | ||
2919 | |||
2920 | type = fwd ? GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE : | ||
2921 | GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE; | ||
2922 | hop = fwd ? t->next_hop : t->prev_hop; | ||
2923 | |||
2924 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2925 | "sending %s keepalive for tunnel %d\n", | ||
2926 | fwd ? "FWD" : "BCK", t->id.tid); | ||
2927 | |||
2928 | msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf; | ||
2929 | msg->header.size = htons (size); | ||
2930 | msg->header.type = htons (type); | ||
2931 | msg->oid = *(GNUNET_PEER_resolve2 (t->id.oid)); | ||
2932 | msg->tid = htonl (t->id.tid); | ||
2933 | send_prebuilt_message (&msg->header, hop, t); | ||
2934 | } | ||
2935 | |||
2936 | |||
2937 | /** | ||
2938 | * Send create (PATH_CREATE/PATH_ACK) packets for a tunnel. | ||
2939 | * | ||
2940 | * @param t Tunnel for which to send the message. | ||
2941 | * @param fwd If GNUNET_YES, send CREATE, otherwise send ACK. | ||
2942 | */ | ||
2943 | static void | ||
2944 | tunnel_recreate (struct MeshTunnel *t, int fwd) | ||
2945 | { | ||
2946 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2947 | "sending path recreate for tunnel %s[%X]\n", | ||
2948 | GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid); | ||
2949 | if (fwd) | ||
2950 | send_path_create (t); | ||
2951 | else | ||
2952 | send_path_ack (t); | ||
2953 | } | ||
2954 | |||
2955 | |||
2956 | /** | ||
2957 | * Generic tunnel timer management. | ||
2958 | * Depending on the role of the peer in the tunnel will send the | ||
2959 | * appropriate message (build or keepalive) | ||
2960 | * | ||
2961 | * @param t Tunnel to maintain. | ||
2962 | * @param fwd Is FWD? | ||
2963 | */ | ||
2964 | static void | ||
2965 | tunnel_maintain (struct MeshTunnel *t, int fwd) | ||
2966 | { | ||
2967 | switch (t->state) | ||
2968 | { | ||
2969 | case MESH_TUNNEL_NEW: | ||
2970 | GNUNET_break (0); | ||
2971 | case MESH_TUNNEL_SEARCHING: | ||
2972 | /* TODO DHT GET with RO_BART */ | ||
2973 | break; | ||
2974 | case MESH_TUNNEL_WAITING: | ||
2975 | tunnel_recreate (t, fwd); | ||
2976 | break; | ||
2977 | case MESH_TUNNEL_READY: | ||
2978 | tunnel_keepalive (t, fwd); | ||
2979 | break; | ||
2980 | default: | ||
2981 | break; | ||
2982 | } | ||
2983 | } | ||
2984 | |||
2985 | |||
2986 | static void | ||
2987 | tunnel_fwd_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
2988 | { | ||
2989 | struct MeshTunnel *t = cls; | ||
2990 | |||
2991 | t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK; | ||
2992 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) || | ||
2993 | NULL == t->owner) | ||
2994 | return; | ||
2995 | |||
2996 | tunnel_maintain (t, GNUNET_YES); | ||
2997 | t->fwd_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time, | ||
2998 | &tunnel_fwd_keepalive, | ||
2999 | t); | ||
3000 | } | ||
3001 | |||
3002 | |||
3003 | static void | ||
3004 | tunnel_bck_keepalive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
3005 | { | ||
3006 | struct MeshTunnel *t = cls; | ||
3007 | |||
3008 | t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK; | ||
3009 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) || | ||
3010 | NULL == t->client) | ||
3011 | return; | ||
3012 | |||
3013 | tunnel_keepalive (t, GNUNET_NO); | ||
3014 | t->bck_maintenance_task = GNUNET_SCHEDULER_add_delayed (refresh_path_time, | ||
3015 | &tunnel_bck_keepalive, | ||
3016 | t); | ||
3017 | } | ||
3018 | |||
3019 | |||
3020 | /** | ||
3021 | * Send a message to all peers and clients in this tunnel that the tunnel | ||
3022 | * is no longer valid. If some peer or client should not receive the message, | ||
3023 | * should be zero'ed out before calling this function. | ||
3024 | * | ||
3025 | * @param t The tunnel whose peers and clients to notify. | ||
3026 | */ | ||
3027 | static void | ||
3028 | tunnel_send_destroy (struct MeshTunnel *t) | ||
3029 | { | ||
3030 | struct GNUNET_MESH_TunnelDestroy msg; | ||
3031 | struct GNUNET_PeerIdentity id; | ||
3032 | |||
3033 | msg.header.size = htons (sizeof (msg)); | ||
3034 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY); | ||
3035 | GNUNET_PEER_resolve (t->id.oid, &msg.oid); | ||
3036 | msg.tid = htonl (t->id.tid); | ||
3037 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3038 | " sending tunnel destroy for tunnel: %s [%X]\n", | ||
3039 | GNUNET_i2s (&msg.oid), t->id.tid); | ||
3040 | |||
3041 | if (NULL == t->client && 0 != t->next_hop) | ||
3042 | { | ||
3043 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " child: %u\n", t->next_hop); | ||
3044 | GNUNET_PEER_resolve (t->next_hop, &id); | ||
3045 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3046 | " sending forward to %s\n", | ||
3047 | GNUNET_i2s (&id)); | ||
3048 | send_prebuilt_message (&msg.header, t->next_hop, t); | ||
3049 | } | ||
3050 | if (NULL == t->owner && 0 != t->prev_hop) | ||
3051 | { | ||
3052 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", t->prev_hop); | ||
3053 | GNUNET_PEER_resolve (t->prev_hop, &id); | ||
3054 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3055 | " sending back to %s\n", | ||
3056 | GNUNET_i2s (&id)); | ||
3057 | send_prebuilt_message (&msg.header, t->prev_hop, t); | ||
3058 | } | ||
3059 | if (NULL != t->owner) | ||
3060 | { | ||
3061 | send_local_tunnel_destroy (t, GNUNET_NO); | ||
3062 | } | ||
3063 | if (NULL != t->client) | ||
3064 | { | ||
3065 | send_local_tunnel_destroy (t, GNUNET_YES); | ||
3066 | } | ||
3067 | } | ||
3068 | |||
3069 | static int | ||
3070 | tunnel_destroy (struct MeshTunnel *t) | ||
3071 | { | ||
3072 | struct MeshClient *c; | ||
3073 | struct GNUNET_HashCode hash; | ||
3074 | int r; | ||
3075 | |||
3076 | if (NULL == t) | ||
3077 | return GNUNET_OK; | ||
3078 | |||
3079 | r = GNUNET_OK; | ||
3080 | c = t->owner; | ||
3081 | |||
3082 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n", | ||
3083 | GNUNET_i2s (GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid); | ||
3084 | if (NULL != c) | ||
3085 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); | ||
3086 | |||
3087 | GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash); | ||
3088 | if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t)) | ||
3089 | { | ||
3090 | GNUNET_break (0); | ||
3091 | r = GNUNET_SYSERR; | ||
3092 | } | ||
3093 | |||
3094 | if (NULL != c) | ||
3095 | { | ||
3096 | if (GNUNET_YES != GNUNET_CONTAINER_multihashmap32_remove (c->own_tunnels, | ||
3097 | t->local_tid, t)) | ||
3098 | { | ||
3099 | GNUNET_break (0); | ||
3100 | r = GNUNET_SYSERR; | ||
3101 | } | ||
3102 | } | ||
3103 | |||
3104 | c = t->client; | ||
3105 | if (NULL != c) | ||
3106 | { | ||
3107 | if (GNUNET_YES != | ||
3108 | GNUNET_CONTAINER_multihashmap32_remove (c->incoming_tunnels, | ||
3109 | t->local_tid_dest, t)) | ||
3110 | { | ||
3111 | GNUNET_break (0); | ||
3112 | r = GNUNET_SYSERR; | ||
3113 | } | ||
3114 | if (GNUNET_YES != | ||
3115 | GNUNET_CONTAINER_multihashmap32_remove (incoming_tunnels, | ||
3116 | t->local_tid_dest, t)) | ||
3117 | { | ||
3118 | GNUNET_break (0); | ||
3119 | r = GNUNET_SYSERR; | ||
3120 | } | ||
3121 | } | ||
3122 | |||
3123 | if(GNUNET_YES == t->reliable) | ||
3124 | { | ||
3125 | tunnel_free_reliable_all (t->fwd_rel); | ||
3126 | tunnel_free_reliable_all (t->bck_rel); | ||
3127 | } | ||
3128 | if (0 != t->prev_hop) | ||
3129 | { | ||
3130 | peer_cancel_queues (t->prev_hop, t); | ||
3131 | GNUNET_PEER_change_rc (t->prev_hop, -1); | ||
3132 | } | ||
3133 | if (0 != t->next_hop) | ||
3134 | { | ||
3135 | peer_cancel_queues (t->next_hop, t); | ||
3136 | GNUNET_PEER_change_rc (t->next_hop, -1); | ||
3137 | } | ||
3138 | if (GNUNET_SCHEDULER_NO_TASK != t->next_fc.poll_task) | ||
3139 | { | ||
3140 | GNUNET_SCHEDULER_cancel (t->next_fc.poll_task); | ||
3141 | t->next_fc.poll_task = GNUNET_SCHEDULER_NO_TASK; | ||
3142 | } | ||
3143 | if (GNUNET_SCHEDULER_NO_TASK != t->prev_fc.poll_task) | ||
3144 | { | ||
3145 | GNUNET_SCHEDULER_cancel (t->prev_fc.poll_task); | ||
3146 | t->prev_fc.poll_task = GNUNET_SCHEDULER_NO_TASK; | ||
3147 | } | ||
3148 | if (0 != t->dest) { | ||
3149 | peer_remove_tunnel (peer_get_short (t->dest), t); | ||
3150 | } | ||
3151 | |||
3152 | if (GNUNET_SCHEDULER_NO_TASK != t->fwd_maintenance_task) | ||
3153 | GNUNET_SCHEDULER_cancel (t->fwd_maintenance_task); | ||
3154 | if (GNUNET_SCHEDULER_NO_TASK != t->bck_maintenance_task) | ||
3155 | GNUNET_SCHEDULER_cancel (t->bck_maintenance_task); | ||
3156 | |||
3157 | n_tunnels--; | ||
3158 | GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO); | ||
3159 | path_destroy (t->path); | ||
3160 | GNUNET_free (t); | ||
3161 | return r; | ||
3162 | } | ||
3163 | |||
3164 | /** | ||
3165 | * Tunnel is empty: destroy it. | ||
3166 | * | ||
3167 | * Notifies all participants (peers, cleints) about the destruction. | ||
3168 | * | ||
3169 | * @param t Tunnel to destroy. | ||
3170 | */ | ||
3171 | static void | ||
3172 | tunnel_destroy_empty (struct MeshTunnel *t) | ||
3173 | { | ||
3174 | #if MESH_DEBUG | ||
3175 | { | ||
3176 | struct GNUNET_PeerIdentity id; | ||
3177 | |||
3178 | GNUNET_PEER_resolve (t->id.oid, &id); | ||
3179 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3180 | "executing destruction of empty tunnel %s [%X]\n", | ||
3181 | GNUNET_i2s (&id), t->id.tid); | ||
3182 | } | ||
3183 | #endif | ||
3184 | |||
3185 | if (GNUNET_NO == t->destroy) | ||
3186 | tunnel_send_destroy (t); | ||
3187 | if (0 == t->pending_messages) | ||
3188 | tunnel_destroy (t); | ||
3189 | else | ||
3190 | t->destroy = GNUNET_YES; | ||
3191 | } | ||
3192 | |||
3193 | /** | ||
3194 | * Initialize a Flow Control structure to the initial state. | ||
3195 | * | ||
3196 | * @param fc Flow Control structure to initialize. | ||
3197 | */ | ||
3198 | static void | ||
3199 | fc_init (struct MeshFlowControl *fc) | ||
3200 | { | ||
3201 | fc->last_pid_sent = (uint32_t) -1; /* Next (expected) = 0 */ | ||
3202 | fc->last_pid_recv = (uint32_t) -1; | ||
3203 | fc->last_ack_sent = (uint32_t) -1; /* No traffic allowed yet */ | ||
3204 | fc->last_ack_recv = (uint32_t) -1; | ||
3205 | fc->poll_task = GNUNET_SCHEDULER_NO_TASK; | ||
3206 | fc->poll_time = GNUNET_TIME_UNIT_SECONDS; | ||
3207 | fc->queue_n = 0; | ||
3208 | } | ||
3209 | |||
3210 | /** | ||
3211 | * Create a new tunnel | ||
3212 | * | ||
3213 | * @param owner Who is the owner of the tunnel (short ID). | ||
3214 | * @param tid Tunnel Number of the tunnel. | ||
3215 | * @param client Clients that owns the tunnel, NULL for foreign tunnels. | ||
3216 | * @param local Tunnel Number for the tunnel, for the client point of view. | ||
3217 | * | ||
3218 | * @return A new initialized tunnel. NULL on error. | ||
3219 | */ | ||
3220 | static struct MeshTunnel * | ||
3221 | tunnel_new (GNUNET_PEER_Id owner, | ||
3222 | MESH_TunnelNumber tid, | ||
3223 | struct MeshClient *client, | ||
3224 | MESH_TunnelNumber local) | ||
3225 | { | ||
3226 | struct MeshTunnel *t; | ||
3227 | struct GNUNET_HashCode hash; | ||
3228 | |||
3229 | if (n_tunnels >= max_tunnels && NULL == client) | ||
3230 | return NULL; | ||
3231 | |||
3232 | t = GNUNET_malloc (sizeof (struct MeshTunnel)); | ||
3233 | t->id.oid = owner; | ||
3234 | t->id.tid = tid; | ||
3235 | t->queue_max = (max_msgs_queue / max_tunnels) + 1; | ||
3236 | t->owner = client; | ||
3237 | fc_init (&t->next_fc); | ||
3238 | fc_init (&t->prev_fc); | ||
3239 | t->next_fc.t = t; | ||
3240 | t->prev_fc.t = t; | ||
3241 | t->local_tid = local; | ||
3242 | n_tunnels++; | ||
3243 | GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO); | ||
3244 | |||
3245 | GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash); | ||
3246 | if (GNUNET_OK != | ||
3247 | GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t, | ||
3248 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) | ||
3249 | { | ||
3250 | GNUNET_break (0); | ||
3251 | tunnel_destroy (t); | ||
3252 | if (NULL != client) | ||
3253 | { | ||
3254 | GNUNET_break (0); | ||
3255 | GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR); | ||
3256 | } | ||
3257 | return NULL; | ||
3258 | } | ||
3259 | |||
3260 | if (NULL != client) | ||
3261 | { | ||
3262 | if (GNUNET_OK != | ||
3263 | GNUNET_CONTAINER_multihashmap32_put (client->own_tunnels, | ||
3264 | t->local_tid, t, | ||
3265 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) | ||
3266 | { | ||
3267 | tunnel_destroy (t); | ||
3268 | GNUNET_break (0); | ||
3269 | GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR); | ||
3270 | return NULL; | ||
3271 | } | ||
3272 | } | ||
3273 | |||
3274 | return t; | ||
3275 | } | ||
3276 | |||
3277 | |||
3278 | /** | ||
3279 | * Set options in a tunnel, extracted from a bit flag field | ||
3280 | * | ||
3281 | * @param t Tunnel to set options to. | ||
3282 | * @param options Bit array in host byte order. | ||
3283 | */ | ||
3284 | static void | ||
3285 | tunnel_set_options (struct MeshTunnel *t, uint32_t options) | ||
3286 | { | ||
3287 | t->nobuffer = (options & GNUNET_MESH_OPTION_NOBUFFER) != 0 ? | ||
3288 | GNUNET_YES : GNUNET_NO; | ||
3289 | t->reliable = (options & GNUNET_MESH_OPTION_RELIABLE) != 0 ? | ||
3290 | GNUNET_YES : GNUNET_NO; | ||
3291 | } | ||
3292 | |||
3293 | |||
3294 | /** | ||
3295 | * Iterator for deleting each tunnel whose client endpoint disconnected. | ||
3296 | * | ||
3297 | * @param cls Closure (client that has disconnected). | ||
3298 | * @param key The local tunnel id (used to access the hashmap). | ||
3299 | * @param value The value stored at the key (tunnel to destroy). | ||
3300 | * | ||
3301 | * @return GNUNET_OK, keep iterating. | ||
3302 | */ | ||
3303 | static int | ||
3304 | tunnel_destroy_iterator (void *cls, | ||
3305 | uint32_t key, | ||
3306 | void *value) | ||
3307 | { | ||
3308 | struct MeshTunnel *t = value; | ||
3309 | struct MeshClient *c = cls; | ||
3310 | |||
3311 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3312 | " Tunnel %X / %X destroy, due to client %u shutdown.\n", | ||
3313 | t->local_tid, t->local_tid_dest, c->id); | ||
3314 | client_delete_tunnel (c, t); | ||
3315 | if (c == t->client) | ||
3316 | { | ||
3317 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is destination.\n", c->id); | ||
3318 | t->client = NULL; | ||
3319 | if (0 != t->next_hop) { /* destroy could come before a path is used */ | ||
3320 | GNUNET_PEER_change_rc (t->next_hop, -1); | ||
3321 | t->next_hop = 0; | ||
3322 | } | ||
3323 | } | ||
3324 | if (c == t->owner) | ||
3325 | { | ||
3326 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Client %u is owner.\n", c->id); | ||
3327 | t->owner = NULL; | ||
3328 | if (0 != t->prev_hop) { /* destroy could come before a path is used */ | ||
3329 | GNUNET_PEER_change_rc (t->prev_hop, -1); | ||
3330 | t->prev_hop = 0; | ||
3331 | } | ||
3332 | } | ||
3333 | |||
3334 | tunnel_destroy_empty (t); | ||
3335 | |||
3336 | return GNUNET_OK; | ||
3337 | } | ||
3338 | |||
3339 | |||
3340 | /** | ||
3341 | * remove client's ports from the global hashmap on diconnect. | ||
3342 | * | ||
3343 | * @param cls Closure (unused). | ||
3344 | * @param key Port. | ||
3345 | * @param value ThClient structure. | ||
3346 | * | ||
3347 | * @return GNUNET_OK, keep iterating. | ||
3348 | */ | ||
3349 | static int | ||
3350 | client_release_ports (void *cls, | ||
3351 | uint32_t key, | ||
3352 | void *value) | ||
3353 | { | ||
3354 | int res; | ||
3355 | |||
3356 | res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value); | ||
3357 | if (GNUNET_YES != res) | ||
3358 | { | ||
3359 | GNUNET_break (0); | ||
3360 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
3361 | "Port %u by client %p was not registered.\n", | ||
3362 | key, value); | ||
3363 | } | ||
3364 | return GNUNET_OK; | ||
3365 | } | ||
3366 | |||
3367 | /** | ||
3368 | * Timeout function due to lack of keepalive/traffic from the owner. | ||
3369 | * Destroys tunnel if called. | ||
3370 | * | ||
3371 | * @param cls Closure (tunnel to destroy). | ||
3372 | * @param tc TaskContext | ||
3373 | */ | ||
3374 | static void | ||
3375 | tunnel_fwd_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
3376 | { | ||
3377 | struct MeshTunnel *t = cls; | ||
3378 | |||
3379 | t->fwd_maintenance_task = GNUNET_SCHEDULER_NO_TASK; | ||
3380 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
3381 | return; | ||
3382 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3383 | "Tunnel %s [%X] FWD timed out. Destroying.\n", | ||
3384 | GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid); | ||
3385 | if (NULL != t->client) | ||
3386 | send_local_tunnel_destroy (t, GNUNET_YES); | ||
3387 | tunnel_destroy (t); /* Do not notify other */ | ||
3388 | } | ||
3389 | |||
3390 | |||
3391 | /** | ||
3392 | * Timeout function due to lack of keepalive/traffic from the destination. | ||
3393 | * Destroys tunnel if called. | ||
3394 | * | ||
3395 | * @param cls Closure (tunnel to destroy). | ||
3396 | * @param tc TaskContext | ||
3397 | */ | ||
3398 | static void | ||
3399 | tunnel_bck_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
3400 | { | ||
3401 | struct MeshTunnel *t = cls; | ||
3402 | |||
3403 | t->bck_maintenance_task = GNUNET_SCHEDULER_NO_TASK; | ||
3404 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
3405 | return; | ||
3406 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3407 | "Tunnel %s [%X] BCK timed out. Destroying.\n", | ||
3408 | GNUNET_i2s(GNUNET_PEER_resolve2 (t->id.oid)), t->id.tid); | ||
3409 | if (NULL != t->owner) | ||
3410 | send_local_tunnel_destroy (t, GNUNET_NO); | ||
3411 | tunnel_destroy (t); /* Do not notify other */ | ||
3412 | } | ||
3413 | |||
3414 | |||
3415 | /** | ||
3416 | * Resets the tunnel timeout task, some other message has done the task's job. | ||
3417 | * - For the first peer on the direction this means to send | ||
3418 | * a keepalive or a path confirmation message (either create or ACK). | ||
3419 | * - For all other peers, this means to destroy the tunnel, | ||
3420 | * due to lack of activity. | ||
3421 | * Starts the tiemout if no timeout was running (tunnel just created). | ||
3422 | * | ||
3423 | * @param t Tunnel whose timeout to reset. | ||
3424 | * @param fwd Is this forward? | ||
3425 | * | ||
3426 | * TODO use heap to improve efficiency of scheduler. | ||
3427 | */ | ||
3428 | static void | ||
3429 | tunnel_reset_timeout (struct MeshTunnel *t, int fwd) | ||
3430 | { | ||
3431 | GNUNET_SCHEDULER_TaskIdentifier *ti; | ||
3432 | GNUNET_SCHEDULER_Task f; | ||
3433 | struct MeshClient *c; | ||
3434 | |||
3435 | ti = fwd ? &t->fwd_maintenance_task : &t->bck_maintenance_task; | ||
3436 | c = fwd ? t->owner : t->client; | ||
3437 | |||
3438 | if (GNUNET_SCHEDULER_NO_TASK != *ti) | ||
3439 | GNUNET_SCHEDULER_cancel (*ti); | ||
3440 | |||
3441 | if (NULL != c) | ||
3442 | { | ||
3443 | f = fwd ? &tunnel_fwd_keepalive : &tunnel_bck_keepalive; | ||
3444 | *ti = GNUNET_SCHEDULER_add_delayed (refresh_path_time, f, t); | ||
3445 | } | ||
3446 | else | ||
3447 | { | ||
3448 | f = fwd ? &tunnel_fwd_timeout : &tunnel_bck_timeout; | ||
3449 | *ti = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
3450 | (refresh_path_time, 4), | ||
3451 | f, t); | ||
3452 | } | ||
3453 | } | ||
3454 | |||
3455 | |||
3456 | /******************************************************************************/ | ||
3457 | /**************** MESH NETWORK HANDLER HELPERS ***********************/ | ||
3458 | /******************************************************************************/ | ||
3459 | |||
3460 | /** | ||
3461 | * Free a transmission that was already queued with all resources | ||
3462 | * associated to the request. | ||
3463 | * | ||
3464 | * @param queue Queue handler to cancel. | ||
3465 | * @param clear_cls Is it necessary to free associated cls? | ||
3466 | */ | ||
3467 | static void | ||
3468 | queue_destroy (struct MeshPeerQueue *queue, int clear_cls) | ||
3469 | { | ||
3470 | struct MeshFlowControl *fc; | ||
3471 | |||
3472 | if (GNUNET_YES == clear_cls) | ||
3473 | { | ||
3474 | switch (queue->type) | ||
3475 | { | ||
3476 | case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: | ||
3477 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n"); | ||
3478 | GNUNET_break (GNUNET_YES == queue->tunnel->destroy); | ||
3479 | /* fall through */ | ||
3480 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST: | ||
3481 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN: | ||
3482 | case GNUNET_MESSAGE_TYPE_MESH_ACK: | ||
3483 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK: | ||
3484 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK: | ||
3485 | case GNUNET_MESSAGE_TYPE_MESH_POLL: | ||
3486 | case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE: | ||
3487 | case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE: | ||
3488 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3489 | " prebuilt message\n"); | ||
3490 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3491 | " type %s\n", | ||
3492 | GNUNET_MESH_DEBUG_M2S (queue->type)); | ||
3493 | break; | ||
3494 | case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE: | ||
3495 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n"); | ||
3496 | break; | ||
3497 | default: | ||
3498 | GNUNET_break (0); | ||
3499 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3500 | " type %s unknown!\n", | ||
3501 | GNUNET_MESH_DEBUG_M2S (queue->type)); | ||
3502 | } | ||
3503 | GNUNET_free_non_null (queue->cls); | ||
3504 | } | ||
3505 | GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head, | ||
3506 | queue->peer->queue_tail, | ||
3507 | queue); | ||
3508 | |||
3509 | /* Delete from appropriate fc in the tunnel */ | ||
3510 | if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type || | ||
3511 | GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type ) | ||
3512 | { | ||
3513 | if (queue->peer->id == queue->tunnel->prev_hop) | ||
3514 | fc = &queue->tunnel->prev_fc; | ||
3515 | else if (queue->peer->id == queue->tunnel->next_hop) | ||
3516 | fc = &queue->tunnel->next_fc; | ||
3517 | else | ||
3518 | { | ||
3519 | GNUNET_break (0); | ||
3520 | GNUNET_free (queue); | ||
3521 | return; | ||
3522 | } | ||
3523 | fc->queue_n--; | ||
3524 | } | ||
3525 | GNUNET_free (queue); | ||
3526 | } | ||
3527 | |||
3528 | |||
3529 | /** | ||
3530 | * @brief Get the next transmittable message from the queue. | ||
3531 | * | ||
3532 | * This will be the head, except in the case of being a data packet | ||
3533 | * not allowed by the destination peer. | ||
3534 | * | ||
3535 | * @param peer Destination peer. | ||
3536 | * | ||
3537 | * @return The next viable MeshPeerQueue element to send to that peer. | ||
3538 | * NULL when there are no transmittable messages. | ||
3539 | */ | ||
3540 | struct MeshPeerQueue * | ||
3541 | queue_get_next (const struct MeshPeer *peer) | ||
3542 | { | ||
3543 | struct MeshPeerQueue *q; | ||
3544 | |||
3545 | struct GNUNET_MESH_Data *dmsg; | ||
3546 | struct MeshTunnel* t; | ||
3547 | uint32_t pid; | ||
3548 | uint32_t ack; | ||
3549 | |||
3550 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* selecting message\n"); | ||
3551 | for (q = peer->queue_head; NULL != q; q = q->next) | ||
3552 | { | ||
3553 | t = q->tunnel; | ||
3554 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3555 | "* %s\n", | ||
3556 | GNUNET_MESH_DEBUG_M2S (q->type)); | ||
3557 | dmsg = (struct GNUNET_MESH_Data *) q->cls; | ||
3558 | pid = ntohl (dmsg->pid); | ||
3559 | switch (q->type) | ||
3560 | { | ||
3561 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST: | ||
3562 | ack = t->next_fc.last_ack_recv; | ||
3563 | break; | ||
3564 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN: | ||
3565 | ack = t->prev_fc.last_ack_recv; | ||
3566 | break; | ||
3567 | default: | ||
3568 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3569 | "* OK!\n"); | ||
3570 | return q; | ||
3571 | } | ||
3572 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3573 | "* ACK: %u, PID: %u, MID: %u\n", | ||
3574 | ack, pid, ntohl (dmsg->mid)); | ||
3575 | if (GNUNET_NO == GMC_is_pid_bigger (pid, ack)) | ||
3576 | { | ||
3577 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3578 | "* OK!\n"); | ||
3579 | return q; | ||
3580 | } | ||
3581 | else | ||
3582 | { | ||
3583 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3584 | "* NEXT!\n"); | ||
3585 | } | ||
3586 | } | ||
3587 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3588 | "* nothing found\n"); | ||
3589 | return NULL; | ||
3590 | } | ||
3591 | |||
3592 | |||
3593 | static size_t | ||
3594 | queue_send (void *cls, size_t size, void *buf) | ||
3595 | { | ||
3596 | struct MeshPeer *peer = cls; | ||
3597 | struct GNUNET_MessageHeader *msg; | ||
3598 | struct MeshPeerQueue *queue; | ||
3599 | struct MeshTunnel *t; | ||
3600 | struct GNUNET_PeerIdentity dst_id; | ||
3601 | struct MeshFlowControl *fc; | ||
3602 | size_t data_size; | ||
3603 | uint32_t pid; | ||
3604 | uint16_t type; | ||
3605 | |||
3606 | peer->core_transmit = NULL; | ||
3607 | |||
3608 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Queue send\n"); | ||
3609 | |||
3610 | if (NULL == buf || 0 == size) | ||
3611 | { | ||
3612 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Buffer size 0.\n"); | ||
3613 | return 0; | ||
3614 | } | ||
3615 | queue = queue_get_next (peer); | ||
3616 | |||
3617 | /* Queue has no internal mesh traffic nor sendable payload */ | ||
3618 | if (NULL == queue) | ||
3619 | { | ||
3620 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not ready, return\n"); | ||
3621 | if (NULL == peer->queue_head) | ||
3622 | GNUNET_break (0); /* Core tmt_rdy should've been canceled */ | ||
3623 | return 0; | ||
3624 | } | ||
3625 | |||
3626 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* not empty\n"); | ||
3627 | |||
3628 | GNUNET_PEER_resolve (peer->id, &dst_id); | ||
3629 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3630 | "* towards %s\n", | ||
3631 | GNUNET_i2s (&dst_id)); | ||
3632 | /* Check if buffer size is enough for the message */ | ||
3633 | if (queue->size > size) | ||
3634 | { | ||
3635 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3636 | "* not enough room, reissue\n"); | ||
3637 | peer->core_transmit = | ||
3638 | GNUNET_CORE_notify_transmit_ready (core_handle, | ||
3639 | GNUNET_NO, | ||
3640 | 0, | ||
3641 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
3642 | &dst_id, | ||
3643 | queue->size, | ||
3644 | &queue_send, | ||
3645 | peer); | ||
3646 | return 0; | ||
3647 | } | ||
3648 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* size ok\n"); | ||
3649 | |||
3650 | t = queue->tunnel; | ||
3651 | GNUNET_assert (0 < t->pending_messages); | ||
3652 | t->pending_messages--; | ||
3653 | type = 0; | ||
3654 | |||
3655 | /* Fill buf */ | ||
3656 | switch (queue->type) | ||
3657 | { | ||
3658 | case 0: | ||
3659 | case GNUNET_MESSAGE_TYPE_MESH_ACK: | ||
3660 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK: | ||
3661 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK: | ||
3662 | case GNUNET_MESSAGE_TYPE_MESH_POLL: | ||
3663 | case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN: | ||
3664 | case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY: | ||
3665 | case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY: | ||
3666 | case GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE: | ||
3667 | case GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE: | ||
3668 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3669 | "* raw: %s\n", | ||
3670 | GNUNET_MESH_DEBUG_M2S (queue->type)); | ||
3671 | /* Fall through */ | ||
3672 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST: | ||
3673 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN: | ||
3674 | data_size = send_core_data_raw (queue->cls, size, buf); | ||
3675 | msg = (struct GNUNET_MessageHeader *) buf; | ||
3676 | type = ntohs (msg->type); | ||
3677 | break; | ||
3678 | case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE: | ||
3679 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path create\n"); | ||
3680 | data_size = send_core_path_create (queue->cls, size, buf); | ||
3681 | break; | ||
3682 | case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK: | ||
3683 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* path ack\n"); | ||
3684 | data_size = send_core_path_ack (queue->cls, size, buf); | ||
3685 | break; | ||
3686 | default: | ||
3687 | GNUNET_break (0); | ||
3688 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
3689 | "* type unknown: %u\n", | ||
3690 | queue->type); | ||
3691 | data_size = 0; | ||
3692 | } | ||
3693 | |||
3694 | if (0 < drop_percent && | ||
3695 | GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, 101) < drop_percent) | ||
3696 | { | ||
3697 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
3698 | "Dropping message of type %s\n", | ||
3699 | GNUNET_MESH_DEBUG_M2S(queue->type)); | ||
3700 | data_size = 0; | ||
3701 | } | ||
3702 | /* Free queue, but cls was freed by send_core_* */ | ||
3703 | queue_destroy (queue, GNUNET_NO); | ||
3704 | |||
3705 | /* Send ACK if needed, after accounting for sent ID in fc->queue_n */ | ||
3706 | pid = ((struct GNUNET_MESH_Data *) buf)->pid; | ||
3707 | pid = ntohl (pid); | ||
3708 | switch (type) | ||
3709 | { | ||
3710 | case GNUNET_MESSAGE_TYPE_MESH_UNICAST: | ||
3711 | t->next_fc.last_pid_sent = pid; | ||
3712 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST, GNUNET_YES); | ||
3713 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3714 | "!!! FWD %u\n", | ||
3715 | ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) ); | ||
3716 | break; | ||
3717 | case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN: | ||
3718 | t->prev_fc.last_pid_sent = pid; | ||
3719 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3720 | "!!! BCK %u\n", | ||
3721 | ntohl ( ((struct GNUNET_MESH_Data *) buf)->mid ) ); | ||
3722 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, GNUNET_NO); | ||
3723 | break; | ||
3724 | default: | ||
3725 | break; | ||
3726 | } | ||
3727 | |||
3728 | /* If more data in queue, send next */ | ||
3729 | queue = queue_get_next (peer); | ||
3730 | if (NULL != queue) | ||
3731 | { | ||
3732 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* more data!\n"); | ||
3733 | if (NULL == peer->core_transmit) { | ||
3734 | peer->core_transmit = | ||
3735 | GNUNET_CORE_notify_transmit_ready(core_handle, | ||
3736 | 0, | ||
3737 | 0, | ||
3738 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
3739 | &dst_id, | ||
3740 | queue->size, | ||
3741 | &queue_send, | ||
3742 | peer); | ||
3743 | } | ||
3744 | else | ||
3745 | { | ||
3746 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3747 | "* tmt rdy called somewhere else\n"); | ||
3748 | } | ||
3749 | } | ||
3750 | if (peer->id == t->next_hop) | ||
3751 | fc = &t->next_fc; | ||
3752 | else if (peer->id == t->prev_hop) | ||
3753 | fc = &t->prev_fc; | ||
3754 | else | ||
3755 | { | ||
3756 | GNUNET_break (0); | ||
3757 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id: %u, next: %u, prev: %u\n", | ||
3758 | peer->id, t->next_hop, t->prev_hop); | ||
3759 | return data_size; | ||
3760 | } | ||
3761 | if (NULL != peer->queue_head) | ||
3762 | { | ||
3763 | if (GNUNET_SCHEDULER_NO_TASK == fc->poll_task && fc->queue_n > 0) | ||
3764 | { | ||
3765 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
3766 | "* %s starting poll timeout\n", | ||
3767 | GNUNET_i2s (&my_full_id)); | ||
3768 | fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, | ||
3769 | &tunnel_poll, fc); | ||
3770 | } | ||
3771 | } | ||
3772 | else | ||
3773 | { | ||
3774 | if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task) | ||
3775 | { | ||
3776 | GNUNET_SCHEDULER_cancel (fc->poll_task); | ||
3777 | fc->poll_task = GNUNET_SCHEDULER_NO_TASK; | ||
3778 | } | ||
3779 | } | ||
3780 | if (GNUNET_YES == t->destroy && 0 == t->pending_messages) | ||
3781 | { | ||
3782 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* destroying tunnel!\n"); | ||
3783 | tunnel_destroy (t); | ||
3784 | } | ||
3785 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Return %d\n", data_size); | ||
3786 | return data_size; | ||
3787 | } | ||
3788 | |||
3789 | |||
3790 | /** | ||
3791 | * @brief Queue and pass message to core when possible. | ||
3792 | * | ||
3793 | * If type is payload (UNICAST, TO_ORIGIN) checks for queue status and | ||
3794 | * accounts for it. In case the queue is full, the message is dropped and | ||
3795 | * a break issued. | ||
3796 | * | ||
3797 | * Otherwise, message is treated as internal and allowed to go regardless of | ||
3798 | * queue status. | ||
3799 | * | ||
3800 | * @param cls Closure (@c type dependant). It will be used by queue_send to | ||
3801 | * build the message to be sent if not already prebuilt. | ||
3802 | * @param type Type of the message, 0 for a raw message. | ||
3803 | * @param size Size of the message. | ||
3804 | * @param dst Neighbor to send message to. | ||
3805 | * @param t Tunnel this message belongs to. | ||
3806 | */ | ||
3807 | static void | ||
3808 | queue_add (void *cls, uint16_t type, size_t size, | ||
3809 | struct MeshPeer *dst, struct MeshTunnel *t) | ||
3810 | { | ||
3811 | struct MeshPeerQueue *queue; | ||
3812 | struct MeshFlowControl *fc; | ||
3813 | int priority; | ||
3814 | |||
3815 | fc = NULL; | ||
3816 | priority = GNUNET_NO; | ||
3817 | if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) | ||
3818 | { | ||
3819 | fc = &t->next_fc; | ||
3820 | } | ||
3821 | else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type) | ||
3822 | { | ||
3823 | fc = &t->prev_fc; | ||
3824 | } | ||
3825 | if (NULL != fc) | ||
3826 | { | ||
3827 | if (fc->queue_n >= t->queue_max) | ||
3828 | { | ||
3829 | /* If this isn't a retransmission, drop the message */ | ||
3830 | if (GNUNET_NO == t->reliable || | ||
3831 | (NULL == t->owner && GNUNET_MESSAGE_TYPE_MESH_UNICAST == type) || | ||
3832 | (NULL == t->client && GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)) | ||
3833 | { | ||
3834 | GNUNET_STATISTICS_update (stats, "# messages dropped (buffer full)", | ||
3835 | 1, GNUNET_NO); | ||
3836 | GNUNET_break (0); | ||
3837 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3838 | "queue full: %u/%u\n", | ||
3839 | fc->queue_n, t->queue_max); | ||
3840 | return; /* Drop this message */ | ||
3841 | } | ||
3842 | priority = GNUNET_YES; | ||
3843 | } | ||
3844 | fc->queue_n++; | ||
3845 | if (GMC_is_pid_bigger(fc->last_pid_sent + 1, fc->last_ack_recv) && | ||
3846 | GNUNET_SCHEDULER_NO_TASK == fc->poll_task) | ||
3847 | fc->poll_task = GNUNET_SCHEDULER_add_delayed (fc->poll_time, | ||
3848 | &tunnel_poll, | ||
3849 | fc); | ||
3850 | } | ||
3851 | queue = GNUNET_malloc (sizeof (struct MeshPeerQueue)); | ||
3852 | queue->cls = cls; | ||
3853 | queue->type = type; | ||
3854 | queue->size = size; | ||
3855 | queue->peer = dst; | ||
3856 | queue->tunnel = t; | ||
3857 | if (GNUNET_YES == priority) | ||
3858 | { | ||
3859 | struct GNUNET_MESH_Data *d; | ||
3860 | uint32_t prev; | ||
3861 | uint32_t next; | ||
3862 | |||
3863 | GNUNET_CONTAINER_DLL_insert (dst->queue_head, dst->queue_tail, queue); | ||
3864 | d = (struct GNUNET_MESH_Data *) queue->cls; | ||
3865 | prev = d->pid; | ||
3866 | for (queue = dst->queue_tail; NULL != queue; queue = queue->prev) | ||
3867 | { | ||
3868 | if (queue->type != type) | ||
3869 | continue; | ||
3870 | d = (struct GNUNET_MESH_Data *) queue->cls; | ||
3871 | next = d->pid; | ||
3872 | d->pid = prev; | ||
3873 | prev = next; | ||
3874 | } | ||
3875 | } | ||
3876 | else | ||
3877 | GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue); | ||
3878 | |||
3879 | if (NULL == dst->core_transmit) | ||
3880 | { | ||
3881 | dst->core_transmit = | ||
3882 | GNUNET_CORE_notify_transmit_ready (core_handle, | ||
3883 | 0, | ||
3884 | 0, | ||
3885 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
3886 | GNUNET_PEER_resolve2 (dst->id), | ||
3887 | size, | ||
3888 | &queue_send, | ||
3889 | dst); | ||
3890 | } | ||
3891 | t->pending_messages++; | ||
3892 | } | ||
3893 | |||
3894 | |||
3895 | /******************************************************************************/ | ||
3896 | /******************** MESH NETWORK HANDLERS **************************/ | ||
3897 | /******************************************************************************/ | ||
3898 | |||
3899 | |||
3900 | /** | ||
3901 | * Core handler for path creation | ||
3902 | * | ||
3903 | * @param cls closure | ||
3904 | * @param message message | ||
3905 | * @param peer peer identity this notification is about | ||
3906 | * | ||
3907 | * @return GNUNET_OK to keep the connection open, | ||
3908 | * GNUNET_SYSERR to close it (signal serious error) | ||
3909 | */ | ||
3910 | static int | ||
3911 | handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
3912 | const struct GNUNET_MessageHeader *message) | ||
3913 | { | ||
3914 | unsigned int own_pos; | ||
3915 | uint16_t size; | ||
3916 | uint16_t i; | ||
3917 | MESH_TunnelNumber tid; | ||
3918 | struct GNUNET_MESH_CreateTunnel *msg; | ||
3919 | struct GNUNET_PeerIdentity *pi; | ||
3920 | struct MeshPeerPath *path; | ||
3921 | struct MeshPeer *dest_peer_info; | ||
3922 | struct MeshPeer *orig_peer_info; | ||
3923 | struct MeshTunnel *t; | ||
3924 | |||
3925 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3926 | "Received a path create msg [%s]\n", | ||
3927 | GNUNET_i2s (&my_full_id)); | ||
3928 | size = ntohs (message->size); | ||
3929 | if (size < sizeof (struct GNUNET_MESH_CreateTunnel)) | ||
3930 | { | ||
3931 | GNUNET_break_op (0); | ||
3932 | return GNUNET_OK; | ||
3933 | } | ||
3934 | |||
3935 | size -= sizeof (struct GNUNET_MESH_CreateTunnel); | ||
3936 | if (size % sizeof (struct GNUNET_PeerIdentity)) | ||
3937 | { | ||
3938 | GNUNET_break_op (0); | ||
3939 | return GNUNET_OK; | ||
3940 | } | ||
3941 | size /= sizeof (struct GNUNET_PeerIdentity); | ||
3942 | if (size < 1) | ||
3943 | { | ||
3944 | GNUNET_break_op (0); | ||
3945 | return GNUNET_OK; | ||
3946 | } | ||
3947 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size); | ||
3948 | msg = (struct GNUNET_MESH_CreateTunnel *) message; | ||
3949 | |||
3950 | tid = ntohl (msg->tid); | ||
3951 | pi = (struct GNUNET_PeerIdentity *) &msg[1]; | ||
3952 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3953 | " path is for tunnel %s[%X]:%u.\n", | ||
3954 | GNUNET_i2s (pi), tid, ntohl (msg->port)); | ||
3955 | t = tunnel_get (pi, tid); | ||
3956 | if (NULL == t) /* might be a local tunnel */ | ||
3957 | { | ||
3958 | uint32_t opt; | ||
3959 | |||
3960 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n"); | ||
3961 | t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0); | ||
3962 | if (NULL == t) | ||
3963 | { | ||
3964 | GNUNET_break (0); | ||
3965 | return GNUNET_OK; | ||
3966 | } | ||
3967 | t->port = ntohl (msg->port); | ||
3968 | opt = ntohl (msg->opt); | ||
3969 | if (0 != (opt & GNUNET_MESH_OPTION_NOBUFFER)) | ||
3970 | { | ||
3971 | t->nobuffer = GNUNET_YES; | ||
3972 | t->queue_max = 1; | ||
3973 | } | ||
3974 | if (0 != (opt & GNUNET_MESH_OPTION_RELIABLE)) | ||
3975 | { | ||
3976 | t->reliable = GNUNET_YES; | ||
3977 | } | ||
3978 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer); | ||
3979 | } | ||
3980 | tunnel_reset_timeout (t, GNUNET_YES); | ||
3981 | tunnel_change_state (t, MESH_TUNNEL_WAITING); | ||
3982 | dest_peer_info = | ||
3983 | GNUNET_CONTAINER_multipeermap_get (peers, &pi[size - 1]); | ||
3984 | if (NULL == dest_peer_info) | ||
3985 | { | ||
3986 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3987 | " Creating PeerInfo for destination.\n"); | ||
3988 | dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeer)); | ||
3989 | dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]); | ||
3990 | GNUNET_CONTAINER_multipeermap_put (peers, &pi[size - 1], | ||
3991 | dest_peer_info, | ||
3992 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
3993 | } | ||
3994 | orig_peer_info = GNUNET_CONTAINER_multipeermap_get (peers, pi); | ||
3995 | if (NULL == orig_peer_info) | ||
3996 | { | ||
3997 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3998 | " Creating PeerInfo for origin.\n"); | ||
3999 | orig_peer_info = GNUNET_new (struct MeshPeer); | ||
4000 | orig_peer_info->id = GNUNET_PEER_intern (pi); | ||
4001 | GNUNET_CONTAINER_multipeermap_put (peers, pi, orig_peer_info, | ||
4002 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
4003 | } | ||
4004 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n"); | ||
4005 | path = path_new (size); | ||
4006 | own_pos = 0; | ||
4007 | for (i = 0; i < size; i++) | ||
4008 | { | ||
4009 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n", | ||
4010 | GNUNET_i2s (&pi[i])); | ||
4011 | path->peers[i] = GNUNET_PEER_intern (&pi[i]); | ||
4012 | if (path->peers[i] == myid) | ||
4013 | own_pos = i; | ||
4014 | } | ||
4015 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos); | ||
4016 | if (own_pos == 0 && path->peers[own_pos] != myid) | ||
4017 | { | ||
4018 | /* create path: self not found in path through self */ | ||
4019 | GNUNET_break_op (0); | ||
4020 | path_destroy (path); | ||
4021 | tunnel_destroy (t); | ||
4022 | return GNUNET_OK; | ||
4023 | } | ||
4024 | path_add_to_peers (path, GNUNET_NO); | ||
4025 | tunnel_use_path (t, path); | ||
4026 | |||
4027 | peer_add_tunnel (dest_peer_info, t); | ||
4028 | |||
4029 | if (own_pos == size - 1) | ||
4030 | { | ||
4031 | struct MeshClient *c; | ||
4032 | |||
4033 | /* Find target client */ | ||
4034 | c = GNUNET_CONTAINER_multihashmap32_get (ports, t->port); | ||
4035 | if (NULL == c) | ||
4036 | { | ||
4037 | /* TODO send reject */ | ||
4038 | return GNUNET_OK; | ||
4039 | } | ||
4040 | |||
4041 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n"); | ||
4042 | peer_add_path_to_origin (orig_peer_info, path, GNUNET_YES); | ||
4043 | /* This can be a retransmission due to a lost PATH ACK. | ||
4044 | * Check if we already have a destination client for the tunnel. */ | ||
4045 | if (t->client != c) | ||
4046 | { | ||
4047 | /* Assign local tid */ | ||
4048 | while (NULL != tunnel_get_incoming (next_local_tid)) | ||
4049 | next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV; | ||
4050 | t->local_tid_dest = next_local_tid++; | ||
4051 | next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV; | ||
4052 | |||
4053 | if (GNUNET_YES == t->reliable) | ||
4054 | { | ||
4055 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n"); | ||
4056 | t->bck_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability)); | ||
4057 | t->bck_rel->t = t; | ||
4058 | t->bck_rel->expected_delay = MESH_RETRANSMIT_TIME; | ||
4059 | } | ||
4060 | |||
4061 | tunnel_add_client (t, c); | ||
4062 | send_local_tunnel_create (t); | ||
4063 | } | ||
4064 | send_path_ack (t); | ||
4065 | /* Eliminate tunnel when origin dies */ | ||
4066 | tunnel_reset_timeout (t, GNUNET_YES); | ||
4067 | /* Keep tunnel alive in direction dest->owner*/ | ||
4068 | tunnel_reset_timeout (t, GNUNET_NO); | ||
4069 | } | ||
4070 | else | ||
4071 | { | ||
4072 | struct MeshPeerPath *path2; | ||
4073 | |||
4074 | t->next_hop = path->peers[own_pos + 1]; | ||
4075 | GNUNET_PEER_change_rc(t->next_hop, 1); | ||
4076 | |||
4077 | /* It's for somebody else! Retransmit. */ | ||
4078 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n"); | ||
4079 | path2 = path_duplicate (path); | ||
4080 | peer_add_path (dest_peer_info, path2, GNUNET_NO); | ||
4081 | peer_add_path_to_origin (orig_peer_info, path, GNUNET_NO); | ||
4082 | send_path_create (t); | ||
4083 | } | ||
4084 | return GNUNET_OK; | ||
4085 | } | ||
4086 | |||
4087 | |||
4088 | |||
4089 | /** | ||
4090 | * Core handler for path ACKs | ||
4091 | * | ||
4092 | * @param cls closure | ||
4093 | * @param message message | ||
4094 | * @param peer peer identity this notification is about | ||
4095 | * | ||
4096 | * @return GNUNET_OK to keep the connection open, | ||
4097 | * GNUNET_SYSERR to close it (signal serious error) | ||
4098 | */ | ||
4099 | static int | ||
4100 | handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4101 | const struct GNUNET_MessageHeader *message) | ||
4102 | { | ||
4103 | struct GNUNET_MESH_PathACK *msg; | ||
4104 | struct MeshPeer *peer_info; | ||
4105 | struct MeshPeerPath *p; | ||
4106 | struct MeshTunnel *t; | ||
4107 | |||
4108 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n", | ||
4109 | GNUNET_i2s (&my_full_id)); | ||
4110 | msg = (struct GNUNET_MESH_PathACK *) message; | ||
4111 | t = tunnel_get (&msg->oid, ntohl(msg->tid)); | ||
4112 | if (NULL == t) | ||
4113 | { | ||
4114 | /* TODO notify that we don't know the tunnel */ | ||
4115 | GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO); | ||
4116 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n", | ||
4117 | GNUNET_i2s (&msg->oid), ntohl(msg->tid)); | ||
4118 | return GNUNET_OK; | ||
4119 | } | ||
4120 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n", | ||
4121 | GNUNET_i2s (&msg->oid), ntohl(msg->tid)); | ||
4122 | |||
4123 | peer_info = peer_get (&msg->peer_id); | ||
4124 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n", | ||
4125 | GNUNET_i2s (&msg->peer_id)); | ||
4126 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n", | ||
4127 | GNUNET_i2s (peer)); | ||
4128 | |||
4129 | /* Add path to peers? */ | ||
4130 | p = t->path; | ||
4131 | if (NULL != p) | ||
4132 | { | ||
4133 | path_add_to_peers (p, GNUNET_YES); | ||
4134 | } | ||
4135 | else | ||
4136 | { | ||
4137 | GNUNET_break (0); | ||
4138 | } | ||
4139 | tunnel_change_state (t, MESH_TUNNEL_READY); | ||
4140 | tunnel_reset_timeout (t, GNUNET_NO); | ||
4141 | t->next_fc.last_ack_recv = (NULL == t->client) ? ntohl (msg->ack) : 0; | ||
4142 | t->prev_fc.last_ack_sent = ntohl (msg->ack); | ||
4143 | |||
4144 | /* Message for us? */ | ||
4145 | if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity))) | ||
4146 | { | ||
4147 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n"); | ||
4148 | if (NULL == t->owner) | ||
4149 | { | ||
4150 | GNUNET_break_op (0); | ||
4151 | return GNUNET_OK; | ||
4152 | } | ||
4153 | if (NULL != peer_info->dhtget) | ||
4154 | { | ||
4155 | GNUNET_DHT_get_stop (peer_info->dhtget); | ||
4156 | peer_info->dhtget = NULL; | ||
4157 | } | ||
4158 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_YES); | ||
4159 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, GNUNET_NO); | ||
4160 | return GNUNET_OK; | ||
4161 | } | ||
4162 | |||
4163 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4164 | " not for us, retransmitting...\n"); | ||
4165 | send_prebuilt_message (message, t->prev_hop, t); | ||
4166 | return GNUNET_OK; | ||
4167 | } | ||
4168 | |||
4169 | |||
4170 | /** | ||
4171 | * Core handler for notifications of broken paths | ||
4172 | * | ||
4173 | * @param cls closure | ||
4174 | * @param message message | ||
4175 | * @param peer peer identity this notification is about | ||
4176 | * | ||
4177 | * @return GNUNET_OK to keep the connection open, | ||
4178 | * GNUNET_SYSERR to close it (signal serious error) | ||
4179 | */ | ||
4180 | static int | ||
4181 | handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4182 | const struct GNUNET_MessageHeader *message) | ||
4183 | { | ||
4184 | struct GNUNET_MESH_PathBroken *msg; | ||
4185 | struct MeshTunnel *t; | ||
4186 | |||
4187 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4188 | "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer)); | ||
4189 | msg = (struct GNUNET_MESH_PathBroken *) message; | ||
4190 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", | ||
4191 | GNUNET_i2s (&msg->peer1)); | ||
4192 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n", | ||
4193 | GNUNET_i2s (&msg->peer2)); | ||
4194 | t = tunnel_get (&msg->oid, ntohl (msg->tid)); | ||
4195 | if (NULL == t) | ||
4196 | { | ||
4197 | GNUNET_break_op (0); | ||
4198 | return GNUNET_OK; | ||
4199 | } | ||
4200 | tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1), | ||
4201 | GNUNET_PEER_search (&msg->peer2)); | ||
4202 | return GNUNET_OK; | ||
4203 | |||
4204 | } | ||
4205 | |||
4206 | |||
4207 | /** | ||
4208 | * Core handler for tunnel destruction | ||
4209 | * | ||
4210 | * @param cls closure | ||
4211 | * @param message message | ||
4212 | * @param peer peer identity this notification is about | ||
4213 | * | ||
4214 | * @return GNUNET_OK to keep the connection open, | ||
4215 | * GNUNET_SYSERR to close it (signal serious error) | ||
4216 | */ | ||
4217 | static int | ||
4218 | handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4219 | const struct GNUNET_MessageHeader *message) | ||
4220 | { | ||
4221 | struct GNUNET_MESH_TunnelDestroy *msg; | ||
4222 | struct MeshTunnel *t; | ||
4223 | |||
4224 | msg = (struct GNUNET_MESH_TunnelDestroy *) message; | ||
4225 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4226 | "Got a TUNNEL DESTROY packet from %s\n", | ||
4227 | GNUNET_i2s (peer)); | ||
4228 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4229 | " for tunnel %s [%u]\n", | ||
4230 | GNUNET_i2s (&msg->oid), ntohl (msg->tid)); | ||
4231 | t = tunnel_get (&msg->oid, ntohl (msg->tid)); | ||
4232 | if (NULL == t) | ||
4233 | { | ||
4234 | /* Probably already got the message from another path, | ||
4235 | * destroyed the tunnel and retransmitted to children. | ||
4236 | * Safe to ignore. | ||
4237 | */ | ||
4238 | GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", | ||
4239 | 1, GNUNET_NO); | ||
4240 | return GNUNET_OK; | ||
4241 | } | ||
4242 | if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) | ||
4243 | { | ||
4244 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n", | ||
4245 | t->local_tid, t->local_tid_dest); | ||
4246 | } | ||
4247 | if (GNUNET_PEER_search (peer) == t->prev_hop) | ||
4248 | { | ||
4249 | // TODO check owner's signature | ||
4250 | // TODO add owner's signatue to tunnel for retransmission | ||
4251 | peer_cancel_queues (t->prev_hop, t); | ||
4252 | GNUNET_PEER_change_rc (t->prev_hop, -1); | ||
4253 | t->prev_hop = 0; | ||
4254 | } | ||
4255 | else if (GNUNET_PEER_search (peer) == t->next_hop) | ||
4256 | { | ||
4257 | // TODO check dest's signature | ||
4258 | // TODO add dest's signatue to tunnel for retransmission | ||
4259 | peer_cancel_queues (t->next_hop, t); | ||
4260 | GNUNET_PEER_change_rc (t->next_hop, -1); | ||
4261 | t->next_hop = 0; | ||
4262 | } | ||
4263 | else | ||
4264 | { | ||
4265 | GNUNET_break_op (0); | ||
4266 | // TODO check both owner AND destination's signature to see which matches | ||
4267 | // TODO restransmit in appropriate direction | ||
4268 | return GNUNET_OK; | ||
4269 | } | ||
4270 | tunnel_destroy_empty (t); | ||
4271 | |||
4272 | // TODO: add timeout to destroy the tunnel anyway | ||
4273 | return GNUNET_OK; | ||
4274 | } | ||
4275 | |||
4276 | |||
4277 | /** | ||
4278 | * Generic handler for mesh network payload traffic. | ||
4279 | * | ||
4280 | * @param peer Peer identity this notification is about. | ||
4281 | * @param message Data message. | ||
4282 | * @param fwd Is this FWD traffic? GNUNET_YES : GNUNET_NO; | ||
4283 | * | ||
4284 | * @return GNUNET_OK to keep the connection open, | ||
4285 | * GNUNET_SYSERR to close it (signal serious error) | ||
4286 | */ | ||
4287 | static int | ||
4288 | handle_mesh_data (const struct GNUNET_PeerIdentity *peer, | ||
4289 | const struct GNUNET_MessageHeader *message, | ||
4290 | int fwd) | ||
4291 | { | ||
4292 | struct GNUNET_MESH_Data *msg; | ||
4293 | struct MeshFlowControl *fc; | ||
4294 | struct MeshTunnelReliability *rel; | ||
4295 | struct MeshTunnel *t; | ||
4296 | struct MeshClient *c; | ||
4297 | GNUNET_PEER_Id hop; | ||
4298 | uint32_t pid; | ||
4299 | uint32_t ttl; | ||
4300 | uint16_t type; | ||
4301 | size_t size; | ||
4302 | |||
4303 | /* Check size */ | ||
4304 | size = ntohs (message->size); | ||
4305 | if (size < | ||
4306 | sizeof (struct GNUNET_MESH_Data) + | ||
4307 | sizeof (struct GNUNET_MessageHeader)) | ||
4308 | { | ||
4309 | GNUNET_break (0); | ||
4310 | return GNUNET_OK; | ||
4311 | } | ||
4312 | type =ntohs (message->type); | ||
4313 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a %s message from %s\n", | ||
4314 | GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer)); | ||
4315 | msg = (struct GNUNET_MESH_Data *) message; | ||
4316 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " payload of type %s\n", | ||
4317 | GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type))); | ||
4318 | /* Check tunnel */ | ||
4319 | t = tunnel_get (&msg->oid, ntohl (msg->tid)); | ||
4320 | if (NULL == t) | ||
4321 | { | ||
4322 | /* TODO notify back: we don't know this tunnel */ | ||
4323 | GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO); | ||
4324 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "WARNING tunnel unknown\n"); | ||
4325 | return GNUNET_OK; | ||
4326 | } | ||
4327 | |||
4328 | /* Initialize FWD/BCK data */ | ||
4329 | pid = ntohl (msg->pid); | ||
4330 | fc = fwd ? &t->prev_fc : &t->next_fc; | ||
4331 | c = fwd ? t->client : t->owner; | ||
4332 | rel = fwd ? t->bck_rel : t->fwd_rel; | ||
4333 | hop = fwd ? t->next_hop : t->prev_hop; | ||
4334 | if (GMC_is_pid_bigger (pid, fc->last_ack_sent)) | ||
4335 | { | ||
4336 | GNUNET_STATISTICS_update (stats, "# unsolicited data", 1, GNUNET_NO); | ||
4337 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4338 | "WARNING Received PID %u, (prev %u), ACK %u\n", | ||
4339 | pid, fc->last_pid_recv, fc->last_ack_sent); | ||
4340 | return GNUNET_OK; | ||
4341 | } | ||
4342 | if (NULL != c) | ||
4343 | tunnel_change_state (t, MESH_TUNNEL_READY); | ||
4344 | tunnel_reset_timeout (t, fwd); | ||
4345 | if (NULL != c) | ||
4346 | { | ||
4347 | /* TODO signature verification */ | ||
4348 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " it's for us! sending to client\n"); | ||
4349 | GNUNET_STATISTICS_update (stats, "# data received", 1, GNUNET_NO); | ||
4350 | if (GMC_is_pid_bigger (pid, fc->last_pid_recv)) | ||
4351 | { | ||
4352 | uint32_t mid; | ||
4353 | |||
4354 | mid = ntohl (msg->mid); | ||
4355 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4356 | " pid %u (mid %u) not seen yet\n", pid, mid); | ||
4357 | fc->last_pid_recv = pid; | ||
4358 | |||
4359 | if (GNUNET_NO == t->reliable || | ||
4360 | ( !GMC_is_pid_bigger (rel->mid_recv, mid) && | ||
4361 | GMC_is_pid_bigger (rel->mid_recv + 64, mid) ) ) | ||
4362 | { | ||
4363 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4364 | "!!! RECV %u\n", ntohl (msg->mid)); | ||
4365 | if (GNUNET_YES == t->reliable) | ||
4366 | { | ||
4367 | /* Is this the exact next expected messasge? */ | ||
4368 | if (mid == rel->mid_recv) | ||
4369 | { | ||
4370 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "as expected\n"); | ||
4371 | rel->mid_recv++; | ||
4372 | tunnel_send_client_data (t, msg, fwd); | ||
4373 | tunnel_send_client_buffered_data (t, c, rel); | ||
4374 | } | ||
4375 | else | ||
4376 | { | ||
4377 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "save for later\n"); | ||
4378 | tunnel_add_buffered_data (t, msg, rel); | ||
4379 | } | ||
4380 | } | ||
4381 | else /* Tunnel unreliable, send to clients directly */ | ||
4382 | { | ||
4383 | tunnel_send_client_data (t, msg, fwd); | ||
4384 | } | ||
4385 | } | ||
4386 | else | ||
4387 | { | ||
4388 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4389 | " MID %u not expected (%u - %u), dropping!\n", | ||
4390 | ntohl (msg->mid), rel->mid_recv, rel->mid_recv + 64); | ||
4391 | } | ||
4392 | } | ||
4393 | else | ||
4394 | { | ||
4395 | // GNUNET_STATISTICS_update (stats, "# duplicate PID", 1, GNUNET_NO); | ||
4396 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
4397 | " Pid %u not expected (%u+), dropping!\n", | ||
4398 | pid, fc->last_pid_recv + 1); | ||
4399 | } | ||
4400 | tunnel_send_ack (t, type, fwd); | ||
4401 | return GNUNET_OK; | ||
4402 | } | ||
4403 | fc->last_pid_recv = pid; | ||
4404 | if (0 == hop) | ||
4405 | { | ||
4406 | GNUNET_STATISTICS_update (stats, "# data on dying tunnel", 1, GNUNET_NO); | ||
4407 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "data on dying tunnel %s[%X]\n", | ||
4408 | GNUNET_PEER_resolve2 (t->id.oid), ntohl (msg->tid)); | ||
4409 | return GNUNET_OK; /* Next hop has destoyed the tunnel, drop */ | ||
4410 | } | ||
4411 | ttl = ntohl (msg->ttl); | ||
4412 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl); | ||
4413 | if (ttl == 0) | ||
4414 | { | ||
4415 | GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO); | ||
4416 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n"); | ||
4417 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, fwd); | ||
4418 | return GNUNET_OK; | ||
4419 | } | ||
4420 | |||
4421 | if (myid != hop) | ||
4422 | { | ||
4423 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); | ||
4424 | send_prebuilt_message (message, hop, t); | ||
4425 | GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO); | ||
4426 | } | ||
4427 | return GNUNET_OK; | ||
4428 | } | ||
4429 | |||
4430 | |||
4431 | /** | ||
4432 | * Core handler for mesh network traffic going from the origin to a peer | ||
4433 | * | ||
4434 | * @param cls Closure (unused). | ||
4435 | * @param message Message received. | ||
4436 | * @param peer Peer who sent the message. | ||
4437 | * | ||
4438 | * @return GNUNET_OK to keep the connection open, | ||
4439 | * GNUNET_SYSERR to close it (signal serious error) | ||
4440 | */ | ||
4441 | static int | ||
4442 | handle_mesh_unicast (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4443 | const struct GNUNET_MessageHeader *message) | ||
4444 | { | ||
4445 | return handle_mesh_data (peer, message, GNUNET_YES); | ||
4446 | } | ||
4447 | |||
4448 | /** | ||
4449 | * Core handler for mesh network traffic towards the owner of a tunnel. | ||
4450 | * | ||
4451 | * @param cls Closure (unused). | ||
4452 | * @param message Message received. | ||
4453 | * @param peer Peer who sent the message. | ||
4454 | * | ||
4455 | * @return GNUNET_OK to keep the connection open, | ||
4456 | * GNUNET_SYSERR to close it (signal serious error) | ||
4457 | */ | ||
4458 | static int | ||
4459 | handle_mesh_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4460 | const struct GNUNET_MessageHeader *message) | ||
4461 | { | ||
4462 | return handle_mesh_data (peer, message, GNUNET_NO); | ||
4463 | } | ||
4464 | |||
4465 | |||
4466 | /** | ||
4467 | * Core handler for mesh network traffic end-to-end ACKs. | ||
4468 | * | ||
4469 | * @param cls Closure. | ||
4470 | * @param message Message. | ||
4471 | * @param peer Peer identity this notification is about. | ||
4472 | * | ||
4473 | * @return GNUNET_OK to keep the connection open, | ||
4474 | * GNUNET_SYSERR to close it (signal serious error) | ||
4475 | */ | ||
4476 | static int | ||
4477 | handle_mesh_data_ack (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4478 | const struct GNUNET_MessageHeader *message) | ||
4479 | { | ||
4480 | struct GNUNET_MESH_DataACK *msg; | ||
4481 | struct MeshTunnelReliability *rel; | ||
4482 | struct MeshReliableMessage *copy; | ||
4483 | struct MeshReliableMessage *next; | ||
4484 | struct MeshTunnel *t; | ||
4485 | GNUNET_PEER_Id id; | ||
4486 | uint32_t ack; | ||
4487 | uint16_t type; | ||
4488 | int work; | ||
4489 | |||
4490 | type = ntohs (message->type); | ||
4491 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a %s message from %s!\n", | ||
4492 | GNUNET_MESH_DEBUG_M2S (type), GNUNET_i2s (peer)); | ||
4493 | msg = (struct GNUNET_MESH_DataACK *) message; | ||
4494 | |||
4495 | t = tunnel_get (&msg->oid, ntohl (msg->tid)); | ||
4496 | if (NULL == t) | ||
4497 | { | ||
4498 | /* TODO notify that we dont know this tunnel (whom)? */ | ||
4499 | GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO); | ||
4500 | return GNUNET_OK; | ||
4501 | } | ||
4502 | ack = ntohl (msg->mid); | ||
4503 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack); | ||
4504 | |||
4505 | /* Is this a forward or backward ACK? */ | ||
4506 | id = GNUNET_PEER_search (peer); | ||
4507 | if (t->next_hop == id && GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type) | ||
4508 | { | ||
4509 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n"); | ||
4510 | if (NULL == t->owner) | ||
4511 | { | ||
4512 | send_prebuilt_message (message, t->prev_hop, t); | ||
4513 | return GNUNET_OK; | ||
4514 | } | ||
4515 | rel = t->fwd_rel; | ||
4516 | } | ||
4517 | else if (t->prev_hop == id && GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK == type) | ||
4518 | { | ||
4519 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n"); | ||
4520 | if (NULL == t->client) | ||
4521 | { | ||
4522 | send_prebuilt_message (message, t->next_hop, t); | ||
4523 | return GNUNET_OK; | ||
4524 | } | ||
4525 | rel = t->bck_rel; | ||
4526 | } | ||
4527 | else | ||
4528 | { | ||
4529 | GNUNET_break_op (0); | ||
4530 | return GNUNET_OK; | ||
4531 | } | ||
4532 | |||
4533 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! ACK %u\n", ack); | ||
4534 | for (work = GNUNET_NO, copy = rel->head_sent; copy != NULL; copy = next) | ||
4535 | { | ||
4536 | if (GMC_is_pid_bigger (copy->mid, ack)) | ||
4537 | { | ||
4538 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! head %u, out!\n", copy->mid); | ||
4539 | tunnel_free_sent_reliable (t, msg, rel); | ||
4540 | break; | ||
4541 | } | ||
4542 | work = GNUNET_YES; | ||
4543 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! id %u\n", copy->mid); | ||
4544 | next = copy->next; | ||
4545 | tunnel_free_reliable_message (copy); | ||
4546 | } | ||
4547 | /* Once buffers have been free'd, send ACK */ | ||
4548 | tunnel_send_ack (t, type, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK == type); | ||
4549 | |||
4550 | /* If some message was free'd, update the retransmission delay*/ | ||
4551 | if (GNUNET_YES == work) | ||
4552 | { | ||
4553 | if (GNUNET_SCHEDULER_NO_TASK != rel->retry_task) | ||
4554 | { | ||
4555 | GNUNET_SCHEDULER_cancel (rel->retry_task); | ||
4556 | if (NULL == rel->head_sent) | ||
4557 | { | ||
4558 | rel->retry_task = GNUNET_SCHEDULER_NO_TASK; | ||
4559 | } | ||
4560 | else | ||
4561 | { | ||
4562 | struct GNUNET_TIME_Absolute new_target; | ||
4563 | struct GNUNET_TIME_Relative delay; | ||
4564 | |||
4565 | delay = GNUNET_TIME_relative_multiply (rel->retry_timer, | ||
4566 | MESH_RETRANSMIT_MARGIN); | ||
4567 | new_target = GNUNET_TIME_absolute_add (rel->head_sent->timestamp, | ||
4568 | delay); | ||
4569 | delay = GNUNET_TIME_absolute_get_remaining (new_target); | ||
4570 | rel->retry_task = | ||
4571 | GNUNET_SCHEDULER_add_delayed (delay, | ||
4572 | &tunnel_retransmit_message, | ||
4573 | rel); | ||
4574 | } | ||
4575 | } | ||
4576 | else | ||
4577 | GNUNET_break (0); | ||
4578 | } | ||
4579 | return GNUNET_OK; | ||
4580 | } | ||
4581 | |||
4582 | /** | ||
4583 | * Core handler for mesh network traffic point-to-point acks. | ||
4584 | * | ||
4585 | * @param cls closure | ||
4586 | * @param message message | ||
4587 | * @param peer peer identity this notification is about | ||
4588 | * | ||
4589 | * @return GNUNET_OK to keep the connection open, | ||
4590 | * GNUNET_SYSERR to close it (signal serious error) | ||
4591 | */ | ||
4592 | static int | ||
4593 | handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4594 | const struct GNUNET_MessageHeader *message) | ||
4595 | { | ||
4596 | struct GNUNET_MESH_ACK *msg; | ||
4597 | struct MeshTunnel *t; | ||
4598 | struct MeshFlowControl *fc; | ||
4599 | GNUNET_PEER_Id id; | ||
4600 | uint32_t ack; | ||
4601 | |||
4602 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n", | ||
4603 | GNUNET_i2s (peer)); | ||
4604 | msg = (struct GNUNET_MESH_ACK *) message; | ||
4605 | |||
4606 | t = tunnel_get (&msg->oid, ntohl (msg->tid)); | ||
4607 | |||
4608 | if (NULL == t) | ||
4609 | { | ||
4610 | /* TODO notify that we dont know this tunnel (whom)? */ | ||
4611 | GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO); | ||
4612 | return GNUNET_OK; | ||
4613 | } | ||
4614 | ack = ntohl (msg->pid); | ||
4615 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack); | ||
4616 | |||
4617 | /* Is this a forward or backward ACK? */ | ||
4618 | id = GNUNET_PEER_search (peer); | ||
4619 | if (t->next_hop == id) | ||
4620 | { | ||
4621 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n"); | ||
4622 | fc = &t->next_fc; | ||
4623 | } | ||
4624 | else if (t->prev_hop == id) | ||
4625 | { | ||
4626 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n"); | ||
4627 | fc = &t->prev_fc; | ||
4628 | } | ||
4629 | else | ||
4630 | { | ||
4631 | GNUNET_break_op (0); | ||
4632 | return GNUNET_OK; | ||
4633 | } | ||
4634 | |||
4635 | if (GNUNET_SCHEDULER_NO_TASK != fc->poll_task && | ||
4636 | GMC_is_pid_bigger (ack, fc->last_ack_recv)) | ||
4637 | { | ||
4638 | GNUNET_SCHEDULER_cancel (fc->poll_task); | ||
4639 | fc->poll_task = GNUNET_SCHEDULER_NO_TASK; | ||
4640 | fc->poll_time = GNUNET_TIME_UNIT_SECONDS; | ||
4641 | } | ||
4642 | |||
4643 | fc->last_ack_recv = ack; | ||
4644 | peer_unlock_queue (id); | ||
4645 | tunnel_change_state (t, MESH_TUNNEL_READY); | ||
4646 | |||
4647 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK, t->next_hop == id); | ||
4648 | |||
4649 | return GNUNET_OK; | ||
4650 | } | ||
4651 | |||
4652 | |||
4653 | /** | ||
4654 | * Core handler for mesh network traffic point-to-point ack polls. | ||
4655 | * | ||
4656 | * @param cls closure | ||
4657 | * @param message message | ||
4658 | * @param peer peer identity this notification is about | ||
4659 | * | ||
4660 | * @return GNUNET_OK to keep the connection open, | ||
4661 | * GNUNET_SYSERR to close it (signal serious error) | ||
4662 | */ | ||
4663 | static int | ||
4664 | handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4665 | const struct GNUNET_MessageHeader *message) | ||
4666 | { | ||
4667 | struct GNUNET_MESH_Poll *msg; | ||
4668 | struct MeshTunnel *t; | ||
4669 | struct MeshFlowControl *fc; | ||
4670 | GNUNET_PEER_Id id; | ||
4671 | uint32_t pid; | ||
4672 | uint32_t old; | ||
4673 | |||
4674 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a POLL packet from %s!\n", | ||
4675 | GNUNET_i2s (peer)); | ||
4676 | |||
4677 | msg = (struct GNUNET_MESH_Poll *) message; | ||
4678 | |||
4679 | t = tunnel_get (&msg->oid, ntohl (msg->tid)); | ||
4680 | |||
4681 | if (NULL == t) | ||
4682 | { | ||
4683 | /* TODO notify that we dont know this tunnel (whom)? */ | ||
4684 | GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO); | ||
4685 | GNUNET_break_op (0); | ||
4686 | return GNUNET_OK; | ||
4687 | } | ||
4688 | |||
4689 | /* Is this a forward or backward ACK? */ | ||
4690 | id = GNUNET_PEER_search (peer); | ||
4691 | pid = ntohl (msg->pid); | ||
4692 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PID %u\n", pid); | ||
4693 | if (t->next_hop == id) | ||
4694 | { | ||
4695 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n"); | ||
4696 | fc = &t->next_fc; | ||
4697 | old = fc->last_pid_recv; | ||
4698 | } | ||
4699 | else if (t->prev_hop == id) | ||
4700 | { | ||
4701 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n"); | ||
4702 | fc = &t->prev_fc; | ||
4703 | old = fc->last_pid_recv; | ||
4704 | } | ||
4705 | else | ||
4706 | { | ||
4707 | GNUNET_break (0); | ||
4708 | return GNUNET_OK; | ||
4709 | } | ||
4710 | |||
4711 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " was %u\n", fc->last_pid_recv); | ||
4712 | fc->last_pid_recv = pid; | ||
4713 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL, t->prev_hop == id); | ||
4714 | |||
4715 | if (GNUNET_YES == t->reliable) | ||
4716 | fc->last_pid_recv = old; | ||
4717 | |||
4718 | return GNUNET_OK; | ||
4719 | } | ||
4720 | |||
4721 | |||
4722 | /** | ||
4723 | * Core handler for mesh keepalives. | ||
4724 | * | ||
4725 | * @param cls closure | ||
4726 | * @param message message | ||
4727 | * @param peer peer identity this notification is about | ||
4728 | * @return GNUNET_OK to keep the connection open, | ||
4729 | * GNUNET_SYSERR to close it (signal serious error) | ||
4730 | * | ||
4731 | * TODO: Check who we got this from, to validate route. | ||
4732 | */ | ||
4733 | static int | ||
4734 | handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
4735 | const struct GNUNET_MessageHeader *message) | ||
4736 | { | ||
4737 | struct GNUNET_MESH_TunnelKeepAlive *msg; | ||
4738 | struct MeshTunnel *t; | ||
4739 | struct MeshClient *c; | ||
4740 | GNUNET_PEER_Id hop; | ||
4741 | int fwd; | ||
4742 | |||
4743 | msg = (struct GNUNET_MESH_TunnelKeepAlive *) message; | ||
4744 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n", | ||
4745 | GNUNET_i2s (peer)); | ||
4746 | |||
4747 | t = tunnel_get (&msg->oid, ntohl (msg->tid)); | ||
4748 | if (NULL == t) | ||
4749 | { | ||
4750 | /* TODO notify that we dont know that tunnel */ | ||
4751 | GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, | ||
4752 | GNUNET_NO); | ||
4753 | return GNUNET_OK; | ||
4754 | } | ||
4755 | |||
4756 | fwd = GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE == ntohs (message->type) ? | ||
4757 | GNUNET_YES : GNUNET_NO; | ||
4758 | c = fwd ? t->client : t->owner; | ||
4759 | hop = fwd ? t->next_hop : t->prev_hop; | ||
4760 | |||
4761 | if (NULL != c) | ||
4762 | tunnel_change_state (t, MESH_TUNNEL_READY); | ||
4763 | tunnel_reset_timeout (t, fwd); | ||
4764 | if (NULL != c || 0 == hop || myid == hop) | ||
4765 | return GNUNET_OK; | ||
4766 | |||
4767 | GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO); | ||
4768 | send_prebuilt_message (message, hop, t); | ||
4769 | return GNUNET_OK; | ||
4770 | } | ||
4771 | |||
4772 | |||
4773 | |||
4774 | /** | ||
4775 | * Functions to handle messages from core | ||
4776 | */ | ||
4777 | static struct GNUNET_CORE_MessageHandler core_handlers[] = { | ||
4778 | {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0}, | ||
4779 | {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN, | ||
4780 | sizeof (struct GNUNET_MESH_PathBroken)}, | ||
4781 | {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY, | ||
4782 | sizeof (struct GNUNET_MESH_TunnelDestroy)}, | ||
4783 | {&handle_mesh_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0}, | ||
4784 | {&handle_mesh_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0}, | ||
4785 | {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_UNICAST_ACK, | ||
4786 | sizeof (struct GNUNET_MESH_DataACK)}, | ||
4787 | {&handle_mesh_data_ack, GNUNET_MESSAGE_TYPE_MESH_TO_ORIG_ACK, | ||
4788 | sizeof (struct GNUNET_MESH_DataACK)}, | ||
4789 | {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_FWD_KEEPALIVE, | ||
4790 | sizeof (struct GNUNET_MESH_TunnelKeepAlive)}, | ||
4791 | {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_BCK_KEEPALIVE, | ||
4792 | sizeof (struct GNUNET_MESH_TunnelKeepAlive)}, | ||
4793 | {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK, | ||
4794 | sizeof (struct GNUNET_MESH_ACK)}, | ||
4795 | {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL, | ||
4796 | sizeof (struct GNUNET_MESH_Poll)}, | ||
4797 | {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK, | ||
4798 | sizeof (struct GNUNET_MESH_PathACK)}, | ||
4799 | {NULL, 0, 0} | ||
4800 | }; | ||
4801 | |||
4802 | |||
4803 | /** | ||
4804 | * Function to process paths received for a new peer addition. The recorded | ||
4805 | * paths form the initial tunnel, which can be optimized later. | ||
4806 | * Called on each result obtained for the DHT search. | ||
4807 | * | ||
4808 | * @param cls closure | ||
4809 | * @param exp when will this value expire | ||
4810 | * @param key key of the result | ||
4811 | * @param get_path path of the get request | ||
4812 | * @param get_path_length lenght of get_path | ||
4813 | * @param put_path path of the put request | ||
4814 | * @param put_path_length length of the put_path | ||
4815 | * @param type type of the result | ||
4816 | * @param size number of bytes in data | ||
4817 | * @param data pointer to the result data | ||
4818 | */ | ||
4819 | static void | ||
4820 | dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, | ||
4821 | const struct GNUNET_HashCode * key, | ||
4822 | const struct GNUNET_PeerIdentity *get_path, | ||
4823 | unsigned int get_path_length, | ||
4824 | const struct GNUNET_PeerIdentity *put_path, | ||
4825 | unsigned int put_path_length, enum GNUNET_BLOCK_Type type, | ||
4826 | size_t size, const void *data) | ||
4827 | { | ||
4828 | struct MeshPeer *peer = cls; | ||
4829 | struct MeshPeerPath *p; | ||
4830 | struct GNUNET_PeerIdentity pi; | ||
4831 | int i; | ||
4832 | |||
4833 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n"); | ||
4834 | GNUNET_PEER_resolve (peer->id, &pi); | ||
4835 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi)); | ||
4836 | |||
4837 | p = path_build_from_dht (get_path, get_path_length, | ||
4838 | put_path, put_path_length); | ||
4839 | path_add_to_peers (p, GNUNET_NO); | ||
4840 | path_destroy (p); | ||
4841 | for (i = 0; i < peer->ntunnels; i++) | ||
4842 | { | ||
4843 | struct GNUNET_PeerIdentity id; | ||
4844 | |||
4845 | GNUNET_PEER_resolve (peer->tunnels[i]->id.oid, &id); | ||
4846 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... tunnel %s:%X (%X / %X)\n", | ||
4847 | GNUNET_i2s (&id), peer->tunnels[i]->id.tid, | ||
4848 | peer->tunnels[i]->local_tid, | ||
4849 | peer->tunnels[i]->local_tid_dest); | ||
4850 | if (peer->tunnels[i]->state == MESH_TUNNEL_SEARCHING) | ||
4851 | { | ||
4852 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... connect!\n"); | ||
4853 | peer_connect (peer, peer->tunnels[i]); | ||
4854 | } | ||
4855 | } | ||
4856 | |||
4857 | return; | ||
4858 | } | ||
4859 | |||
4860 | |||
4861 | /******************************************************************************/ | ||
4862 | /********************* MESH LOCAL HANDLES **************************/ | ||
4863 | /******************************************************************************/ | ||
4864 | |||
4865 | |||
4866 | /** | ||
4867 | * Handler for client connection. | ||
4868 | * | ||
4869 | * @param cls Closure (unused). | ||
4870 | * @param client Client handler. | ||
4871 | */ | ||
4872 | static void | ||
4873 | handle_local_client_connect (void *cls, struct GNUNET_SERVER_Client *client) | ||
4874 | { | ||
4875 | struct MeshClient *c; | ||
4876 | |||
4877 | if (NULL == client) | ||
4878 | return; | ||
4879 | c = GNUNET_malloc (sizeof (struct MeshClient)); | ||
4880 | c->handle = client; | ||
4881 | GNUNET_SERVER_client_keep (client); | ||
4882 | GNUNET_SERVER_client_set_user_context (client, c); | ||
4883 | GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c); | ||
4884 | } | ||
4885 | |||
4886 | |||
4887 | /** | ||
4888 | * Handler for client disconnection | ||
4889 | * | ||
4890 | * @param cls closure | ||
4891 | * @param client identification of the client; NULL | ||
4892 | * for the last call when the server is destroyed | ||
4893 | */ | ||
4894 | static void | ||
4895 | handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) | ||
4896 | { | ||
4897 | struct MeshClient *c; | ||
4898 | |||
4899 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected: %p\n", client); | ||
4900 | if (client == NULL) | ||
4901 | { | ||
4902 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n"); | ||
4903 | return; | ||
4904 | } | ||
4905 | |||
4906 | c = client_get (client); | ||
4907 | if (NULL != c) | ||
4908 | { | ||
4909 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u, %p)\n", | ||
4910 | c->id, c); | ||
4911 | GNUNET_SERVER_client_drop (c->handle); | ||
4912 | c->shutting_down = GNUNET_YES; | ||
4913 | if (NULL != c->own_tunnels) | ||
4914 | { | ||
4915 | GNUNET_CONTAINER_multihashmap32_iterate (c->own_tunnels, | ||
4916 | &tunnel_destroy_iterator, c); | ||
4917 | GNUNET_CONTAINER_multihashmap32_destroy (c->own_tunnels); | ||
4918 | } | ||
4919 | |||
4920 | if (NULL != c->incoming_tunnels) | ||
4921 | { | ||
4922 | GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_tunnels, | ||
4923 | &tunnel_destroy_iterator, c); | ||
4924 | GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_tunnels); | ||
4925 | } | ||
4926 | |||
4927 | if (NULL != c->ports) | ||
4928 | { | ||
4929 | GNUNET_CONTAINER_multihashmap32_iterate (c->ports, | ||
4930 | &client_release_ports, c); | ||
4931 | GNUNET_CONTAINER_multihashmap32_destroy (c->ports); | ||
4932 | } | ||
4933 | GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c); | ||
4934 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client free (%p)\n", c); | ||
4935 | GNUNET_free (c); | ||
4936 | GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO); | ||
4937 | } | ||
4938 | else | ||
4939 | { | ||
4940 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " context NULL!\n"); | ||
4941 | } | ||
4942 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "done!\n"); | ||
4943 | return; | ||
4944 | } | ||
4945 | |||
4946 | |||
4947 | /** | ||
4948 | * Handler for new clients | ||
4949 | * | ||
4950 | * @param cls closure | ||
4951 | * @param client identification of the client | ||
4952 | * @param message the actual message, which includes messages the client wants | ||
4953 | */ | ||
4954 | static void | ||
4955 | handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client, | ||
4956 | const struct GNUNET_MessageHeader *message) | ||
4957 | { | ||
4958 | struct GNUNET_MESH_ClientConnect *cc_msg; | ||
4959 | struct MeshClient *c; | ||
4960 | unsigned int size; | ||
4961 | uint32_t *p; | ||
4962 | unsigned int i; | ||
4963 | |||
4964 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected %p\n", client); | ||
4965 | |||
4966 | /* Check data sanity */ | ||
4967 | size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect); | ||
4968 | cc_msg = (struct GNUNET_MESH_ClientConnect *) message; | ||
4969 | if (0 != (size % sizeof (uint32_t))) | ||
4970 | { | ||
4971 | GNUNET_break (0); | ||
4972 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
4973 | return; | ||
4974 | } | ||
4975 | size /= sizeof (uint32_t); | ||
4976 | |||
4977 | /* Initialize new client structure */ | ||
4978 | c = GNUNET_SERVER_client_get_user_context (client, struct MeshClient); | ||
4979 | c->id = next_client_id++; /* overflow not important: just for debug */ | ||
4980 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client id %u\n", c->id); | ||
4981 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u ports\n", size); | ||
4982 | if (size > 0) | ||
4983 | { | ||
4984 | uint32_t u32; | ||
4985 | |||
4986 | p = (uint32_t *) &cc_msg[1]; | ||
4987 | c->ports = GNUNET_CONTAINER_multihashmap32_create (size); | ||
4988 | for (i = 0; i < size; i++) | ||
4989 | { | ||
4990 | u32 = ntohl (p[i]); | ||
4991 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32); | ||
4992 | |||
4993 | /* store in client's hashmap */ | ||
4994 | GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c, | ||
4995 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); | ||
4996 | /* store in global hashmap */ | ||
4997 | /* FIXME only allow one client to have the port open, | ||
4998 | * have a backup hashmap with waiting clients */ | ||
4999 | GNUNET_CONTAINER_multihashmap32_put (ports, u32, c, | ||
5000 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
5001 | } | ||
5002 | } | ||
5003 | |||
5004 | c->own_tunnels = GNUNET_CONTAINER_multihashmap32_create (32); | ||
5005 | c->incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32); | ||
5006 | GNUNET_SERVER_notification_context_add (nc, client); | ||
5007 | GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO); | ||
5008 | |||
5009 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5010 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n"); | ||
5011 | } | ||
5012 | |||
5013 | |||
5014 | /** | ||
5015 | * Handler for requests of new tunnels | ||
5016 | * | ||
5017 | * @param cls Closure. | ||
5018 | * @param client Identification of the client. | ||
5019 | * @param message The actual message. | ||
5020 | */ | ||
5021 | static void | ||
5022 | handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client, | ||
5023 | const struct GNUNET_MessageHeader *message) | ||
5024 | { | ||
5025 | struct GNUNET_MESH_TunnelMessage *t_msg; | ||
5026 | struct MeshPeer *peer_info; | ||
5027 | struct MeshTunnel *t; | ||
5028 | struct MeshClient *c; | ||
5029 | MESH_TunnelNumber tid; | ||
5030 | |||
5031 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n"); | ||
5032 | |||
5033 | /* Sanity check for client registration */ | ||
5034 | if (NULL == (c = client_get (client))) | ||
5035 | { | ||
5036 | GNUNET_break (0); | ||
5037 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5038 | return; | ||
5039 | } | ||
5040 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); | ||
5041 | |||
5042 | /* Message size sanity check */ | ||
5043 | if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size)) | ||
5044 | { | ||
5045 | GNUNET_break (0); | ||
5046 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5047 | return; | ||
5048 | } | ||
5049 | |||
5050 | t_msg = (struct GNUNET_MESH_TunnelMessage *) message; | ||
5051 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n", | ||
5052 | GNUNET_i2s (&t_msg->peer), ntohl (t_msg->port)); | ||
5053 | tid = ntohl (t_msg->tunnel_id); | ||
5054 | /* Sanity check for duplicate tunnel IDs */ | ||
5055 | if (NULL != tunnel_get_by_local_id (c, tid)) | ||
5056 | { | ||
5057 | GNUNET_break (0); | ||
5058 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5059 | return; | ||
5060 | } | ||
5061 | |||
5062 | /* Create tunnel */ | ||
5063 | while (NULL != tunnel_get_by_pi (myid, next_tid)) | ||
5064 | next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI; | ||
5065 | t = tunnel_new (myid, next_tid, c, tid); | ||
5066 | next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI; | ||
5067 | if (NULL == t) | ||
5068 | { | ||
5069 | GNUNET_break (0); | ||
5070 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5071 | return; | ||
5072 | } | ||
5073 | t->port = ntohl (t_msg->port); | ||
5074 | tunnel_set_options (t, ntohl (t_msg->opt)); | ||
5075 | if (GNUNET_YES == t->reliable) | ||
5076 | { | ||
5077 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! Reliable\n"); | ||
5078 | t->fwd_rel = GNUNET_malloc (sizeof (struct MeshTunnelReliability)); | ||
5079 | t->fwd_rel->t = t; | ||
5080 | t->fwd_rel->expected_delay = MESH_RETRANSMIT_TIME; | ||
5081 | } | ||
5082 | |||
5083 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s[%x]:%u (%x)\n", | ||
5084 | GNUNET_i2s (&my_full_id), t->id.tid, t->port, t->local_tid); | ||
5085 | |||
5086 | peer_info = peer_get (&t_msg->peer); | ||
5087 | peer_add_tunnel (peer_info, t); | ||
5088 | peer_connect (peer_info, t); | ||
5089 | tunnel_reset_timeout (t, GNUNET_YES); | ||
5090 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5091 | return; | ||
5092 | } | ||
5093 | |||
5094 | |||
5095 | /** | ||
5096 | * Handler for requests of deleting tunnels | ||
5097 | * | ||
5098 | * @param cls closure | ||
5099 | * @param client identification of the client | ||
5100 | * @param message the actual message | ||
5101 | */ | ||
5102 | static void | ||
5103 | handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client, | ||
5104 | const struct GNUNET_MessageHeader *message) | ||
5105 | { | ||
5106 | struct GNUNET_MESH_TunnelMessage *tunnel_msg; | ||
5107 | struct MeshClient *c; | ||
5108 | struct MeshTunnel *t; | ||
5109 | MESH_TunnelNumber tid; | ||
5110 | |||
5111 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
5112 | "Got a DESTROY TUNNEL from client!\n"); | ||
5113 | |||
5114 | /* Sanity check for client registration */ | ||
5115 | if (NULL == (c = client_get (client))) | ||
5116 | { | ||
5117 | GNUNET_break (0); | ||
5118 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5119 | return; | ||
5120 | } | ||
5121 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); | ||
5122 | |||
5123 | /* Message sanity check */ | ||
5124 | if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size)) | ||
5125 | { | ||
5126 | GNUNET_break (0); | ||
5127 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5128 | return; | ||
5129 | } | ||
5130 | |||
5131 | tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message; | ||
5132 | |||
5133 | /* Retrieve tunnel */ | ||
5134 | tid = ntohl (tunnel_msg->tunnel_id); | ||
5135 | t = tunnel_get_by_local_id (c, tid); | ||
5136 | if (NULL == t) | ||
5137 | { | ||
5138 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid); | ||
5139 | GNUNET_break (0); | ||
5140 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5141 | return; | ||
5142 | } | ||
5143 | |||
5144 | /* Cleanup after the tunnel */ | ||
5145 | client_delete_tunnel (c, t); | ||
5146 | if (c == t->client && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV <= tid) | ||
5147 | { | ||
5148 | t->client = NULL; | ||
5149 | } | ||
5150 | else if (c == t->owner && GNUNET_MESH_LOCAL_TUNNEL_ID_SERV > tid) | ||
5151 | { | ||
5152 | peer_remove_tunnel (peer_get_short (t->dest), t); | ||
5153 | t->owner = NULL; | ||
5154 | } | ||
5155 | else | ||
5156 | { | ||
5157 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5158 | " tunnel %X client %p (%p, %p)\n", | ||
5159 | tid, c, t->owner, t->client); | ||
5160 | GNUNET_break (0); | ||
5161 | } | ||
5162 | |||
5163 | /* The tunnel will be destroyed when the last message is transmitted. */ | ||
5164 | tunnel_destroy_empty (t); | ||
5165 | |||
5166 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5167 | return; | ||
5168 | } | ||
5169 | |||
5170 | |||
5171 | /** | ||
5172 | * Handler for client traffic | ||
5173 | * | ||
5174 | * @param cls closure | ||
5175 | * @param client identification of the client | ||
5176 | * @param message the actual message | ||
5177 | */ | ||
5178 | static void | ||
5179 | handle_local_data (void *cls, struct GNUNET_SERVER_Client *client, | ||
5180 | const struct GNUNET_MessageHeader *message) | ||
5181 | { | ||
5182 | struct GNUNET_MESH_LocalData *data_msg; | ||
5183 | struct MeshClient *c; | ||
5184 | struct MeshTunnel *t; | ||
5185 | struct MeshFlowControl *fc; | ||
5186 | MESH_TunnelNumber tid; | ||
5187 | size_t size; | ||
5188 | |||
5189 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
5190 | "Got data from a client!\n"); | ||
5191 | |||
5192 | /* Sanity check for client registration */ | ||
5193 | if (NULL == (c = client_get (client))) | ||
5194 | { | ||
5195 | GNUNET_break (0); | ||
5196 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5197 | return; | ||
5198 | } | ||
5199 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); | ||
5200 | |||
5201 | data_msg = (struct GNUNET_MESH_LocalData *) message; | ||
5202 | |||
5203 | /* Sanity check for message size */ | ||
5204 | size = ntohs (message->size) - sizeof (struct GNUNET_MESH_LocalData); | ||
5205 | if (size < sizeof (struct GNUNET_MessageHeader)) | ||
5206 | { | ||
5207 | GNUNET_break (0); | ||
5208 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5209 | return; | ||
5210 | } | ||
5211 | |||
5212 | /* Tunnel exists? */ | ||
5213 | tid = ntohl (data_msg->tid); | ||
5214 | t = tunnel_get_by_local_id (c, tid); | ||
5215 | if (NULL == t) | ||
5216 | { | ||
5217 | GNUNET_break (0); | ||
5218 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5219 | return; | ||
5220 | } | ||
5221 | |||
5222 | /* Is the client in the tunnel? */ | ||
5223 | if ( !( (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV && | ||
5224 | t->owner && | ||
5225 | t->owner->handle == client) | ||
5226 | || | ||
5227 | (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV && | ||
5228 | t->client && | ||
5229 | t->client->handle == client) ) ) | ||
5230 | { | ||
5231 | GNUNET_break (0); | ||
5232 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5233 | return; | ||
5234 | } | ||
5235 | |||
5236 | /* Ok, everything is correct, send the message | ||
5237 | * (pretend we got it from a mesh peer) | ||
5238 | */ | ||
5239 | { | ||
5240 | struct GNUNET_MESH_Data *payload; | ||
5241 | char cbuf[sizeof(struct GNUNET_MESH_Data) + size]; | ||
5242 | |||
5243 | fc = tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? &t->prev_fc : &t->next_fc; | ||
5244 | if (GNUNET_YES == t->reliable) | ||
5245 | { | ||
5246 | struct MeshTunnelReliability *rel; | ||
5247 | struct MeshReliableMessage *copy; | ||
5248 | |||
5249 | rel = (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) ? t->fwd_rel : t->bck_rel; | ||
5250 | copy = GNUNET_malloc (sizeof (struct MeshReliableMessage) | ||
5251 | + sizeof(struct GNUNET_MESH_Data) | ||
5252 | + size); | ||
5253 | copy->mid = rel->mid_sent++; | ||
5254 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "!!! DATA %u\n", copy->mid); | ||
5255 | copy->timestamp = GNUNET_TIME_absolute_get (); | ||
5256 | copy->rel = rel; | ||
5257 | rel->n_sent++; | ||
5258 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " n_sent %u\n", rel->n_sent); | ||
5259 | GNUNET_CONTAINER_DLL_insert_tail (rel->head_sent, rel->tail_sent, copy); | ||
5260 | if (GNUNET_SCHEDULER_NO_TASK == rel->retry_task) | ||
5261 | { | ||
5262 | rel->retry_timer = | ||
5263 | GNUNET_TIME_relative_multiply (rel->expected_delay, | ||
5264 | MESH_RETRANSMIT_MARGIN); | ||
5265 | rel->retry_task = | ||
5266 | GNUNET_SCHEDULER_add_delayed (rel->retry_timer, | ||
5267 | &tunnel_retransmit_message, | ||
5268 | rel); | ||
5269 | } | ||
5270 | payload = (struct GNUNET_MESH_Data *) ©[1]; | ||
5271 | payload->mid = htonl (copy->mid); | ||
5272 | } | ||
5273 | else | ||
5274 | { | ||
5275 | payload = (struct GNUNET_MESH_Data *) cbuf; | ||
5276 | payload->mid = htonl (fc->last_pid_recv + 1); | ||
5277 | } | ||
5278 | memcpy (&payload[1], &data_msg[1], size); | ||
5279 | payload->header.size = htons (sizeof (struct GNUNET_MESH_Data) + size); | ||
5280 | payload->header.type = htons (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV ? | ||
5281 | GNUNET_MESSAGE_TYPE_MESH_UNICAST : | ||
5282 | GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN); | ||
5283 | GNUNET_PEER_resolve(t->id.oid, &payload->oid);; | ||
5284 | payload->tid = htonl (t->id.tid); | ||
5285 | payload->ttl = htonl (default_ttl); | ||
5286 | payload->pid = htonl (fc->last_pid_recv + 1); | ||
5287 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
5288 | " calling generic handler...\n"); | ||
5289 | if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) | ||
5290 | handle_mesh_unicast (NULL, &my_full_id, &payload->header); | ||
5291 | else | ||
5292 | handle_mesh_to_orig (NULL, &my_full_id, &payload->header); | ||
5293 | } | ||
5294 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n"); | ||
5295 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5296 | |||
5297 | return; | ||
5298 | } | ||
5299 | |||
5300 | |||
5301 | /** | ||
5302 | * Handler for client's ACKs for payload traffic. | ||
5303 | * | ||
5304 | * @param cls Closure (unused). | ||
5305 | * @param client Identification of the client. | ||
5306 | * @param message The actual message. | ||
5307 | */ | ||
5308 | static void | ||
5309 | handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client, | ||
5310 | const struct GNUNET_MessageHeader *message) | ||
5311 | { | ||
5312 | struct GNUNET_MESH_LocalAck *msg; | ||
5313 | struct MeshTunnel *t; | ||
5314 | struct MeshClient *c; | ||
5315 | MESH_TunnelNumber tid; | ||
5316 | |||
5317 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n"); | ||
5318 | /* Sanity check for client registration */ | ||
5319 | if (NULL == (c = client_get (client))) | ||
5320 | { | ||
5321 | GNUNET_break (0); | ||
5322 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5323 | return; | ||
5324 | } | ||
5325 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); | ||
5326 | |||
5327 | msg = (struct GNUNET_MESH_LocalAck *) message; | ||
5328 | |||
5329 | /* Tunnel exists? */ | ||
5330 | tid = ntohl (msg->tunnel_id); | ||
5331 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid); | ||
5332 | t = tunnel_get_by_local_id (c, tid); | ||
5333 | if (NULL == t) | ||
5334 | { | ||
5335 | GNUNET_break (0); | ||
5336 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid); | ||
5337 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id); | ||
5338 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5339 | return; | ||
5340 | } | ||
5341 | |||
5342 | /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */ | ||
5343 | if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV) | ||
5344 | { | ||
5345 | /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */ | ||
5346 | t->prev_fc.last_ack_recv++; | ||
5347 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_NO); | ||
5348 | } | ||
5349 | else | ||
5350 | { | ||
5351 | /* The client doesn't own the tunnel, this ACK is for FWD traffic. */ | ||
5352 | t->next_fc.last_ack_recv++; | ||
5353 | tunnel_send_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, GNUNET_YES); | ||
5354 | } | ||
5355 | |||
5356 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5357 | |||
5358 | return; | ||
5359 | } | ||
5360 | |||
5361 | |||
5362 | |||
5363 | /** | ||
5364 | * Iterator over all tunnels to send a monitoring client info about each tunnel. | ||
5365 | * | ||
5366 | * @param cls Closure (client handle). | ||
5367 | * @param key Key (hashed tunnel ID, unused). | ||
5368 | * @param value Tunnel info. | ||
5369 | * | ||
5370 | * @return GNUNET_YES, to keep iterating. | ||
5371 | */ | ||
5372 | static int | ||
5373 | monitor_all_tunnels_iterator (void *cls, | ||
5374 | const struct GNUNET_HashCode * key, | ||
5375 | void *value) | ||
5376 | { | ||
5377 | struct GNUNET_SERVER_Client *client = cls; | ||
5378 | struct MeshTunnel *t = value; | ||
5379 | struct GNUNET_MESH_LocalMonitor *msg; | ||
5380 | |||
5381 | msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor)); | ||
5382 | GNUNET_PEER_resolve(t->id.oid, &msg->owner); | ||
5383 | msg->tunnel_id = htonl (t->id.tid); | ||
5384 | msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor)); | ||
5385 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS); | ||
5386 | GNUNET_PEER_resolve (t->dest, &msg->destination); | ||
5387 | |||
5388 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
5389 | "* sending info about tunnel %s [%u]\n", | ||
5390 | GNUNET_i2s (&msg->owner), t->id.tid); | ||
5391 | |||
5392 | GNUNET_SERVER_notification_context_unicast (nc, client, | ||
5393 | &msg->header, GNUNET_NO); | ||
5394 | return GNUNET_YES; | ||
5395 | } | ||
5396 | |||
5397 | |||
5398 | /** | ||
5399 | * Handler for client's MONITOR request. | ||
5400 | * | ||
5401 | * @param cls Closure (unused). | ||
5402 | * @param client Identification of the client. | ||
5403 | * @param message The actual message. | ||
5404 | */ | ||
5405 | static void | ||
5406 | handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client, | ||
5407 | const struct GNUNET_MessageHeader *message) | ||
5408 | { | ||
5409 | struct MeshClient *c; | ||
5410 | |||
5411 | /* Sanity check for client registration */ | ||
5412 | if (NULL == (c = client_get (client))) | ||
5413 | { | ||
5414 | GNUNET_break (0); | ||
5415 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5416 | return; | ||
5417 | } | ||
5418 | |||
5419 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
5420 | "Received get tunnels request from client %u\n", | ||
5421 | c->id); | ||
5422 | GNUNET_CONTAINER_multihashmap_iterate (tunnels, | ||
5423 | monitor_all_tunnels_iterator, | ||
5424 | client); | ||
5425 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
5426 | "Get tunnels request from client %u completed\n", | ||
5427 | c->id); | ||
5428 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5429 | } | ||
5430 | |||
5431 | |||
5432 | /** | ||
5433 | * Handler for client's MONITOR_TUNNEL request. | ||
5434 | * | ||
5435 | * @param cls Closure (unused). | ||
5436 | * @param client Identification of the client. | ||
5437 | * @param message The actual message. | ||
5438 | */ | ||
5439 | static void | ||
5440 | handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client, | ||
5441 | const struct GNUNET_MessageHeader *message) | ||
5442 | { | ||
5443 | const struct GNUNET_MESH_LocalMonitor *msg; | ||
5444 | struct GNUNET_MESH_LocalMonitor *resp; | ||
5445 | struct MeshClient *c; | ||
5446 | struct MeshTunnel *t; | ||
5447 | |||
5448 | /* Sanity check for client registration */ | ||
5449 | if (NULL == (c = client_get (client))) | ||
5450 | { | ||
5451 | GNUNET_break (0); | ||
5452 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
5453 | return; | ||
5454 | } | ||
5455 | |||
5456 | msg = (struct GNUNET_MESH_LocalMonitor *) message; | ||
5457 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
5458 | "Received tunnel info request from client %u for tunnel %s[%X]\n", | ||
5459 | c->id, | ||
5460 | &msg->owner, | ||
5461 | ntohl (msg->tunnel_id)); | ||
5462 | t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id)); | ||
5463 | if (NULL == t) | ||
5464 | { | ||
5465 | /* We don't know the tunnel FIXME */ | ||
5466 | struct GNUNET_MESH_LocalMonitor warn; | ||
5467 | |||
5468 | warn = *msg; | ||
5469 | GNUNET_SERVER_notification_context_unicast (nc, client, | ||
5470 | &warn.header, | ||
5471 | GNUNET_NO); | ||
5472 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5473 | return; | ||
5474 | } | ||
5475 | |||
5476 | /* Initialize context */ | ||
5477 | resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor)); | ||
5478 | *resp = *msg; | ||
5479 | GNUNET_PEER_resolve (t->dest, &resp->destination); | ||
5480 | resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor)); | ||
5481 | GNUNET_SERVER_notification_context_unicast (nc, c->handle, | ||
5482 | &resp->header, GNUNET_NO); | ||
5483 | GNUNET_free (resp); | ||
5484 | |||
5485 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
5486 | "Monitor tunnel request from client %u completed\n", | ||
5487 | c->id); | ||
5488 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
5489 | } | ||
5490 | |||
5491 | |||
5492 | /** | ||
5493 | * Functions to handle messages from clients | ||
5494 | */ | ||
5495 | static struct GNUNET_SERVER_MessageHandler client_handlers[] = { | ||
5496 | {&handle_local_new_client, NULL, | ||
5497 | GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0}, | ||
5498 | {&handle_local_tunnel_create, NULL, | ||
5499 | GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE, | ||
5500 | sizeof (struct GNUNET_MESH_TunnelMessage)}, | ||
5501 | {&handle_local_tunnel_destroy, NULL, | ||
5502 | GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY, | ||
5503 | sizeof (struct GNUNET_MESH_TunnelMessage)}, | ||
5504 | {&handle_local_data, NULL, | ||
5505 | GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA, 0}, | ||
5506 | {&handle_local_ack, NULL, | ||
5507 | GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK, | ||
5508 | sizeof (struct GNUNET_MESH_LocalAck)}, | ||
5509 | {&handle_local_get_tunnels, NULL, | ||
5510 | GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS, | ||
5511 | sizeof (struct GNUNET_MessageHeader)}, | ||
5512 | {&handle_local_show_tunnel, NULL, | ||
5513 | GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL, | ||
5514 | sizeof (struct GNUNET_MESH_LocalMonitor)}, | ||
5515 | {NULL, NULL, 0, 0} | ||
5516 | }; | ||
5517 | |||
5518 | |||
5519 | /** | ||
5520 | * Method called whenever a given peer connects. | ||
5521 | * | ||
5522 | * @param cls closure | ||
5523 | * @param peer peer identity this notification is about | ||
5524 | */ | ||
5525 | static void | ||
5526 | core_connect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
5527 | { | ||
5528 | struct MeshPeer *peer_info; | ||
5529 | struct MeshPeerPath *path; | ||
5530 | |||
5531 | DEBUG_CONN ("Peer connected\n"); | ||
5532 | DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id)); | ||
5533 | peer_info = peer_get (peer); | ||
5534 | if (myid == peer_info->id) | ||
5535 | { | ||
5536 | DEBUG_CONN (" (self)\n"); | ||
5537 | path = path_new (1); | ||
5538 | } | ||
5539 | else | ||
5540 | { | ||
5541 | DEBUG_CONN (" %s\n", GNUNET_i2s (peer)); | ||
5542 | path = path_new (2); | ||
5543 | path->peers[1] = peer_info->id; | ||
5544 | GNUNET_PEER_change_rc (peer_info->id, 1); | ||
5545 | GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO); | ||
5546 | } | ||
5547 | path->peers[0] = myid; | ||
5548 | GNUNET_PEER_change_rc (myid, 1); | ||
5549 | peer_add_path (peer_info, path, GNUNET_YES); | ||
5550 | return; | ||
5551 | } | ||
5552 | |||
5553 | |||
5554 | /** | ||
5555 | * Method called whenever a peer disconnects. | ||
5556 | * | ||
5557 | * @param cls closure | ||
5558 | * @param peer peer identity this notification is about | ||
5559 | */ | ||
5560 | static void | ||
5561 | core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
5562 | { | ||
5563 | struct MeshPeer *pi; | ||
5564 | struct MeshPeerQueue *q; | ||
5565 | struct MeshPeerQueue *n; | ||
5566 | |||
5567 | DEBUG_CONN ("Peer disconnected\n"); | ||
5568 | pi = GNUNET_CONTAINER_multipeermap_get (peers, peer); | ||
5569 | if (NULL == pi) | ||
5570 | { | ||
5571 | GNUNET_break (0); | ||
5572 | return; | ||
5573 | } | ||
5574 | q = pi->queue_head; | ||
5575 | while (NULL != q) | ||
5576 | { | ||
5577 | n = q->next; | ||
5578 | /* TODO try to reroute this traffic instead */ | ||
5579 | queue_destroy(q, GNUNET_YES); | ||
5580 | q = n; | ||
5581 | } | ||
5582 | if (NULL != pi->core_transmit) | ||
5583 | { | ||
5584 | GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit); | ||
5585 | pi->core_transmit = NULL; | ||
5586 | } | ||
5587 | peer_remove_path (pi, pi->id, myid); | ||
5588 | if (myid == pi->id) | ||
5589 | { | ||
5590 | DEBUG_CONN (" (self)\n"); | ||
5591 | } | ||
5592 | GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO); | ||
5593 | return; | ||
5594 | } | ||
5595 | |||
5596 | |||
5597 | /** | ||
5598 | * Install server (service) handlers and start listening to clients. | ||
5599 | */ | ||
5600 | static void | ||
5601 | server_init (void) | ||
5602 | { | ||
5603 | GNUNET_SERVER_add_handlers (server_handle, client_handlers); | ||
5604 | GNUNET_SERVER_connect_notify (server_handle, | ||
5605 | &handle_local_client_connect, NULL); | ||
5606 | GNUNET_SERVER_disconnect_notify (server_handle, | ||
5607 | &handle_local_client_disconnect, NULL); | ||
5608 | nc = GNUNET_SERVER_notification_context_create (server_handle, 1); | ||
5609 | |||
5610 | clients_head = NULL; | ||
5611 | clients_tail = NULL; | ||
5612 | next_client_id = 0; | ||
5613 | GNUNET_SERVER_resume (server_handle); | ||
5614 | } | ||
5615 | |||
5616 | |||
5617 | /** | ||
5618 | * To be called on core init/fail. | ||
5619 | * | ||
5620 | * @param cls Closure (config) | ||
5621 | * @param identity the public identity of this peer | ||
5622 | */ | ||
5623 | static void | ||
5624 | core_init (void *cls, | ||
5625 | const struct GNUNET_PeerIdentity *identity) | ||
5626 | { | ||
5627 | const struct GNUNET_CONFIGURATION_Handle *c = cls; | ||
5628 | static int i = 0; | ||
5629 | |||
5630 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n"); | ||
5631 | if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id))) | ||
5632 | { | ||
5633 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n")); | ||
5634 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5635 | " core id %s\n", | ||
5636 | GNUNET_i2s (identity)); | ||
5637 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5638 | " my id %s\n", | ||
5639 | GNUNET_i2s (&my_full_id)); | ||
5640 | GNUNET_CORE_disconnect (core_handle); | ||
5641 | core_handle = GNUNET_CORE_connect (c, /* Main configuration */ | ||
5642 | NULL, /* Closure passed to MESH functions */ | ||
5643 | &core_init, /* Call core_init once connected */ | ||
5644 | &core_connect, /* Handle connects */ | ||
5645 | &core_disconnect, /* remove peers on disconnects */ | ||
5646 | NULL, /* Don't notify about all incoming messages */ | ||
5647 | GNUNET_NO, /* For header only in notification */ | ||
5648 | NULL, /* Don't notify about all outbound messages */ | ||
5649 | GNUNET_NO, /* For header-only out notification */ | ||
5650 | core_handlers); /* Register these handlers */ | ||
5651 | if (10 < i++) | ||
5652 | GNUNET_abort(); | ||
5653 | } | ||
5654 | server_init (); | ||
5655 | return; | ||
5656 | } | ||
5657 | |||
5658 | 85 | ||
5659 | /******************************************************************************/ | 86 | /******************************************************************************/ |
5660 | /************************ MAIN FUNCTIONS ****************************/ | 87 | /************************ MAIN FUNCTIONS ****************************/ |
5661 | /******************************************************************************/ | 88 | /******************************************************************************/ |
5662 | 89 | ||
5663 | /** | 90 | /** |
5664 | * Iterator over tunnel hash map entries to destroy the tunnel during shutdown. | ||
5665 | * | ||
5666 | * @param cls closure | ||
5667 | * @param key current key code | ||
5668 | * @param value value in the hash map | ||
5669 | * @return GNUNET_YES if we should continue to iterate, | ||
5670 | * GNUNET_NO if not. | ||
5671 | */ | ||
5672 | static int | ||
5673 | shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value) | ||
5674 | { | ||
5675 | struct MeshTunnel *t = value; | ||
5676 | |||
5677 | tunnel_destroy (t); | ||
5678 | return GNUNET_YES; | ||
5679 | } | ||
5680 | |||
5681 | /** | ||
5682 | * Iterator over peer hash map entries to destroy the tunnel during shutdown. | ||
5683 | * | ||
5684 | * @param cls closure | ||
5685 | * @param key current key code | ||
5686 | * @param value value in the hash map | ||
5687 | * @return GNUNET_YES if we should continue to iterate, | ||
5688 | * GNUNET_NO if not. | ||
5689 | */ | ||
5690 | static int | ||
5691 | shutdown_peer (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | ||
5692 | { | ||
5693 | struct MeshPeer *p = value; | ||
5694 | struct MeshPeerQueue *q; | ||
5695 | struct MeshPeerQueue *n; | ||
5696 | |||
5697 | q = p->queue_head; | ||
5698 | while (NULL != q) | ||
5699 | { | ||
5700 | n = q->next; | ||
5701 | if (q->peer == p) | ||
5702 | { | ||
5703 | queue_destroy(q, GNUNET_YES); | ||
5704 | } | ||
5705 | q = n; | ||
5706 | } | ||
5707 | peer_destroy (p); | ||
5708 | return GNUNET_YES; | ||
5709 | } | ||
5710 | |||
5711 | |||
5712 | /** | ||
5713 | * Task run during shutdown. | 91 | * Task run during shutdown. |
5714 | * | 92 | * |
5715 | * @param cls unused | 93 | * @param cls unused |
@@ -5720,28 +98,12 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
5720 | { | 98 | { |
5721 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n"); | 99 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n"); |
5722 | 100 | ||
5723 | if (core_handle != NULL) | 101 | GML_shutdown (); |
5724 | { | 102 | GMD_shutdown (); |
5725 | GNUNET_CORE_disconnect (core_handle); | 103 | GMP_shutdown (); |
5726 | core_handle = NULL; | 104 | GMC_shutdown (); |
5727 | } | 105 | GMT_shutdown (); |
5728 | GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL); | 106 | |
5729 | GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_peer, NULL); | ||
5730 | if (dht_handle != NULL) | ||
5731 | { | ||
5732 | GNUNET_DHT_disconnect (dht_handle); | ||
5733 | dht_handle = NULL; | ||
5734 | } | ||
5735 | if (nc != NULL) | ||
5736 | { | ||
5737 | GNUNET_SERVER_notification_context_destroy (nc); | ||
5738 | nc = NULL; | ||
5739 | } | ||
5740 | if (GNUNET_SCHEDULER_NO_TASK != announce_id_task) | ||
5741 | { | ||
5742 | GNUNET_SCHEDULER_cancel (announce_id_task); | ||
5743 | announce_id_task = GNUNET_SCHEDULER_NO_TASK; | ||
5744 | } | ||
5745 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n"); | 107 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n"); |
5746 | } | 108 | } |
5747 | 109 | ||
@@ -5757,176 +119,28 @@ static void | |||
5757 | run (void *cls, struct GNUNET_SERVER_Handle *server, | 119 | run (void *cls, struct GNUNET_SERVER_Handle *server, |
5758 | const struct GNUNET_CONFIGURATION_Handle *c) | 120 | const struct GNUNET_CONFIGURATION_Handle *c) |
5759 | { | 121 | { |
5760 | char *keyfile; | ||
5761 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; | ||
5762 | |||
5763 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n"); | 122 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n"); |
5764 | server_handle = server; | ||
5765 | GNUNET_SERVER_suspend (server_handle); | ||
5766 | |||
5767 | if (GNUNET_OK != | ||
5768 | GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY", | ||
5769 | &keyfile)) | ||
5770 | { | ||
5771 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5772 | _ | ||
5773 | ("%s service is lacking key configuration settings (%s). Exiting.\n"), | ||
5774 | "mesh", "peer/privatekey"); | ||
5775 | GNUNET_SCHEDULER_shutdown (); | ||
5776 | return; | ||
5777 | } | ||
5778 | |||
5779 | if (GNUNET_OK != | ||
5780 | GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME", | ||
5781 | &refresh_path_time)) | ||
5782 | { | ||
5783 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5784 | _ | ||
5785 | ("%s service is lacking key configuration settings (%s). Exiting.\n"), | ||
5786 | "mesh", "refresh path time"); | ||
5787 | GNUNET_SCHEDULER_shutdown (); | ||
5788 | return; | ||
5789 | } | ||
5790 | |||
5791 | if (GNUNET_OK != | ||
5792 | GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME", | ||
5793 | &id_announce_time)) | ||
5794 | { | ||
5795 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5796 | _ | ||
5797 | ("%s service is lacking key configuration settings (%s). Exiting.\n"), | ||
5798 | "mesh", "id announce time"); | ||
5799 | GNUNET_SCHEDULER_shutdown (); | ||
5800 | return; | ||
5801 | } | ||
5802 | |||
5803 | if (GNUNET_OK != | ||
5804 | GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT", | ||
5805 | &connect_timeout)) | ||
5806 | { | ||
5807 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5808 | _ | ||
5809 | ("%s service is lacking key configuration settings (%s). Exiting.\n"), | ||
5810 | "mesh", "connect timeout"); | ||
5811 | GNUNET_SCHEDULER_shutdown (); | ||
5812 | return; | ||
5813 | } | ||
5814 | 123 | ||
5815 | if (GNUNET_OK != | ||
5816 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE", | ||
5817 | &max_msgs_queue)) | ||
5818 | { | ||
5819 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5820 | _ | ||
5821 | ("%s service is lacking key configuration settings (%s). Exiting.\n"), | ||
5822 | "mesh", "max msgs queue"); | ||
5823 | GNUNET_SCHEDULER_shutdown (); | ||
5824 | return; | ||
5825 | } | ||
5826 | |||
5827 | if (GNUNET_OK != | ||
5828 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS", | ||
5829 | &max_tunnels)) | ||
5830 | { | ||
5831 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
5832 | _ | ||
5833 | ("%s service is lacking key configuration settings (%s). Exiting.\n"), | ||
5834 | "mesh", "max tunnels"); | ||
5835 | GNUNET_SCHEDULER_shutdown (); | ||
5836 | return; | ||
5837 | } | ||
5838 | |||
5839 | if (GNUNET_OK != | ||
5840 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL", | ||
5841 | &default_ttl)) | ||
5842 | { | ||
5843 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
5844 | _ | ||
5845 | ("%s service is lacking key configuration settings (%s). Using default (%u).\n"), | ||
5846 | "mesh", "default ttl", 64); | ||
5847 | default_ttl = 64; | ||
5848 | } | ||
5849 | |||
5850 | if (GNUNET_OK != | ||
5851 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS", | ||
5852 | &max_peers)) | ||
5853 | { | ||
5854 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
5855 | _("%s service is lacking key configuration settings (%s). Using default (%u).\n"), | ||
5856 | "mesh", "max peers", 1000); | ||
5857 | max_peers = 1000; | ||
5858 | } | ||
5859 | |||
5860 | if (GNUNET_OK != | ||
5861 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DROP_PERCENT", | ||
5862 | &drop_percent)) | ||
5863 | { | ||
5864 | drop_percent = 0; | ||
5865 | } | ||
5866 | else | ||
5867 | { | ||
5868 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
5869 | "Mesh is running with drop mode enabled. " | ||
5870 | "This is NOT a good idea! " | ||
5871 | "Remove the DROP_PERCENT option from your configuration.\n"); | ||
5872 | } | ||
5873 | |||
5874 | if (GNUNET_OK != | ||
5875 | GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL", | ||
5876 | &dht_replication_level)) | ||
5877 | { | ||
5878 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
5879 | _ | ||
5880 | ("%s service is lacking key configuration settings (%s). Using default (%u).\n"), | ||
5881 | "mesh", "dht replication level", 3); | ||
5882 | dht_replication_level = 3; | ||
5883 | } | ||
5884 | |||
5885 | tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO); | ||
5886 | incoming_tunnels = GNUNET_CONTAINER_multihashmap32_create (32); | ||
5887 | peers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_NO); | ||
5888 | ports = GNUNET_CONTAINER_multihashmap32_create (32); | ||
5889 | |||
5890 | dht_handle = GNUNET_DHT_connect (c, 64); | ||
5891 | if (NULL == dht_handle) | ||
5892 | { | ||
5893 | GNUNET_break (0); | ||
5894 | } | ||
5895 | stats = GNUNET_STATISTICS_create ("mesh", c); | 124 | stats = GNUNET_STATISTICS_create ("mesh", c); |
5896 | 125 | ||
5897 | /* Scheduled the task to clean up when shutdown is called */ | 126 | /* Scheduled the task to clean up when shutdown is called */ |
5898 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, | 127 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, |
5899 | NULL); | 128 | NULL); |
5900 | pk = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile); | 129 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "reading key\n"); |
5901 | GNUNET_free (keyfile); | 130 | my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c); |
5902 | GNUNET_assert (NULL != pk); | 131 | GNUNET_assert (NULL != my_private_key); |
5903 | my_private_key = pk; | 132 | GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_full_id.public_key); |
5904 | GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, | ||
5905 | &my_full_id.public_key); | ||
5906 | myid = GNUNET_PEER_intern (&my_full_id); | 133 | myid = GNUNET_PEER_intern (&my_full_id); |
5907 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 134 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
5908 | "Mesh for peer [%s] starting\n", | 135 | "Mesh for peer [%s] starting\n", |
5909 | GNUNET_i2s(&my_full_id)); | 136 | GNUNET_i2s (&my_full_id)); |
137 | |||
138 | GML_init (server); /* Local clients */ | ||
139 | GMC_init (c); /* Connections */ | ||
140 | GMP_init (c); /* Peers */ | ||
141 | GMD_init (c); /* DHT */ | ||
142 | GMT_init (c, my_private_key); /* Tunnels */ | ||
5910 | 143 | ||
5911 | core_handle = GNUNET_CORE_connect (c, /* Main configuration */ | ||
5912 | NULL, /* Closure passed to MESH functions */ | ||
5913 | &core_init, /* Call core_init once connected */ | ||
5914 | &core_connect, /* Handle connects */ | ||
5915 | &core_disconnect, /* remove peers on disconnects */ | ||
5916 | NULL, /* Don't notify about all incoming messages */ | ||
5917 | GNUNET_NO, /* For header only in notification */ | ||
5918 | NULL, /* Don't notify about all outbound messages */ | ||
5919 | GNUNET_NO, /* For header-only out notification */ | ||
5920 | core_handlers); /* Register these handlers */ | ||
5921 | if (NULL == core_handle) | ||
5922 | { | ||
5923 | GNUNET_break (0); | ||
5924 | GNUNET_SCHEDULER_shutdown (); | ||
5925 | return; | ||
5926 | } | ||
5927 | next_tid = 0; | ||
5928 | next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV; | ||
5929 | announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls); | ||
5930 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n"); | 144 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n"); |
5931 | } | 145 | } |
5932 | 146 | ||
@@ -5944,13 +158,12 @@ main (int argc, char *const *argv) | |||
5944 | int ret; | 158 | int ret; |
5945 | int r; | 159 | int r; |
5946 | 160 | ||
5947 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n"); | 161 | fprintf (stderr, "main()\n"); |
5948 | r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run, | 162 | r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run, |
5949 | NULL); | 163 | NULL); |
164 | GNUNET_free (my_private_key); | ||
5950 | ret = (GNUNET_OK == r) ? 0 : 1; | 165 | ret = (GNUNET_OK == r) ? 0 : 1; |
5951 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n"); | 166 | fprintf (stderr, "main() END\n"); |
5952 | |||
5953 | INTERVAL_SHOW; | ||
5954 | 167 | ||
5955 | return ret; | 168 | return ret; |
5956 | } | 169 | } |