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