diff options
Diffstat (limited to 'src/cadet/gnunet-service-cadet.c')
-rw-r--r-- | src/cadet/gnunet-service-cadet.c | 1496 |
1 files changed, 1496 insertions, 0 deletions
diff --git a/src/cadet/gnunet-service-cadet.c b/src/cadet/gnunet-service-cadet.c new file mode 100644 index 000000000..a7e1fca47 --- /dev/null +++ b/src/cadet/gnunet-service-cadet.c | |||
@@ -0,0 +1,1496 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2001-2013, 2017 GNUnet e.V. | ||
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., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file cadet/gnunet-service-cadet.c | ||
23 | * @brief GNUnet CADET service with encryption | ||
24 | * @author Bartlomiej Polot | ||
25 | * @author Christian Grothoff | ||
26 | * | ||
27 | * Dictionary: | ||
28 | * - peer: other cadet instance. If there is direct connection it's a neighbor. | ||
29 | * - path: series of directly connected peer from one peer to another. | ||
30 | * - connection: path which is being used in a tunnel. | ||
31 | * - tunnel: encrypted connection to a peer, neighbor or not. | ||
32 | * - channel: logical link between two clients, on the same or different peers. | ||
33 | * have properties like reliability. | ||
34 | */ | ||
35 | |||
36 | #include "platform.h" | ||
37 | #include "gnunet_util_lib.h" | ||
38 | #include "cadet.h" | ||
39 | #include "gnunet_statistics_service.h" | ||
40 | #include "gnunet-service-cadet.h" | ||
41 | #include "gnunet-service-cadet_channel.h" | ||
42 | #include "gnunet-service-cadet_connection.h" | ||
43 | #include "gnunet-service-cadet_core.h" | ||
44 | #include "gnunet-service-cadet_dht.h" | ||
45 | #include "gnunet-service-cadet_hello.h" | ||
46 | #include "gnunet-service-cadet_tunnels.h" | ||
47 | #include "gnunet-service-cadet_peer.h" | ||
48 | #include "gnunet-service-cadet_paths.h" | ||
49 | |||
50 | #define LOG(level, ...) GNUNET_log (level,__VA_ARGS__) | ||
51 | |||
52 | |||
53 | /** | ||
54 | * Struct containing information about a client of the service | ||
55 | */ | ||
56 | struct CadetClient | ||
57 | { | ||
58 | /** | ||
59 | * Linked list next | ||
60 | */ | ||
61 | struct CadetClient *next; | ||
62 | |||
63 | /** | ||
64 | * Linked list prev | ||
65 | */ | ||
66 | struct CadetClient *prev; | ||
67 | |||
68 | /** | ||
69 | * Tunnels that belong to this client, indexed by local id, | ||
70 | * value is a `struct CadetChannel`. | ||
71 | */ | ||
72 | struct GNUNET_CONTAINER_MultiHashMap32 *channels; | ||
73 | |||
74 | /** | ||
75 | * Handle to communicate with the client | ||
76 | */ | ||
77 | struct GNUNET_MQ_Handle *mq; | ||
78 | |||
79 | /** | ||
80 | * Client handle. | ||
81 | */ | ||
82 | struct GNUNET_SERVICE_Client *client; | ||
83 | |||
84 | /** | ||
85 | * Ports that this client has declared interest in. | ||
86 | * Indexed by port, contains *Client. | ||
87 | */ | ||
88 | struct GNUNET_CONTAINER_MultiHashMap *ports; | ||
89 | |||
90 | /** | ||
91 | * Channel ID to use for the next incoming channel for this client. | ||
92 | * Wraps around (in theory). | ||
93 | */ | ||
94 | struct GNUNET_CADET_ClientChannelNumber next_ccn; | ||
95 | |||
96 | /** | ||
97 | * ID of the client, mainly for debug messages. Purely internal to this file. | ||
98 | */ | ||
99 | unsigned int id; | ||
100 | }; | ||
101 | |||
102 | /******************************************************************************/ | ||
103 | /*********************** GLOBAL VARIABLES ****************************/ | ||
104 | /******************************************************************************/ | ||
105 | |||
106 | /****************************** Global variables ******************************/ | ||
107 | |||
108 | /** | ||
109 | * Handle to our configuration. | ||
110 | */ | ||
111 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
112 | |||
113 | /** | ||
114 | * Handle to the statistics service. | ||
115 | */ | ||
116 | struct GNUNET_STATISTICS_Handle *stats; | ||
117 | |||
118 | /** | ||
119 | * Handle to communicate with ATS. | ||
120 | */ | ||
121 | struct GNUNET_ATS_ConnectivityHandle *ats_ch; | ||
122 | |||
123 | /** | ||
124 | * Local peer own ID. | ||
125 | */ | ||
126 | struct GNUNET_PeerIdentity my_full_id; | ||
127 | |||
128 | /** | ||
129 | * Own private key. | ||
130 | */ | ||
131 | struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key; | ||
132 | |||
133 | /** | ||
134 | * Signal that shutdown is happening: prevent recovery measures. | ||
135 | */ | ||
136 | int shutting_down; | ||
137 | |||
138 | /** | ||
139 | * DLL with all the clients, head. | ||
140 | */ | ||
141 | static struct CadetClient *clients_head; | ||
142 | |||
143 | /** | ||
144 | * DLL with all the clients, tail. | ||
145 | */ | ||
146 | static struct CadetClient *clients_tail; | ||
147 | |||
148 | /** | ||
149 | * Next ID to assign to a client. | ||
150 | */ | ||
151 | static unsigned int next_client_id; | ||
152 | |||
153 | /** | ||
154 | * All ports clients of this peer have opened. | ||
155 | */ | ||
156 | struct GNUNET_CONTAINER_MultiHashMap *open_ports; | ||
157 | |||
158 | /** | ||
159 | * Map from ports to channels where the ports were closed at the | ||
160 | * time we got the inbound connection. | ||
161 | * Indexed by port, contains `struct CadetChannel`. | ||
162 | */ | ||
163 | struct GNUNET_CONTAINER_MultiHashMap *loose_channels; | ||
164 | |||
165 | /** | ||
166 | * Map from PIDs to `struct CadetPeer` entries. | ||
167 | */ | ||
168 | struct GNUNET_CONTAINER_MultiPeerMap *peers; | ||
169 | |||
170 | /** | ||
171 | * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier` | ||
172 | * hash codes to `struct CadetConnection` objects. | ||
173 | */ | ||
174 | struct GNUNET_CONTAINER_MultiShortmap *connections; | ||
175 | |||
176 | /** | ||
177 | * How many messages are needed to trigger an AXOLOTL ratchet advance. | ||
178 | */ | ||
179 | unsigned long long ratchet_messages; | ||
180 | |||
181 | /** | ||
182 | * How long until we trigger a ratched advance due to time. | ||
183 | */ | ||
184 | struct GNUNET_TIME_Relative ratchet_time; | ||
185 | |||
186 | /** | ||
187 | * How frequently do we send KEEPALIVE messages on idle connections? | ||
188 | */ | ||
189 | struct GNUNET_TIME_Relative keepalive_period; | ||
190 | |||
191 | /** | ||
192 | * Set to non-zero values to create random drops to test retransmissions. | ||
193 | */ | ||
194 | unsigned long long drop_percent; | ||
195 | |||
196 | |||
197 | /** | ||
198 | * Send a message to a client. | ||
199 | * | ||
200 | * @param c client to get the message | ||
201 | * @param env envelope with the message | ||
202 | */ | ||
203 | void | ||
204 | GSC_send_to_client (struct CadetClient *c, | ||
205 | struct GNUNET_MQ_Envelope *env) | ||
206 | { | ||
207 | GNUNET_MQ_send (c->mq, | ||
208 | env); | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Return identifier for a client as a string. | ||
214 | * | ||
215 | * @param c client to identify | ||
216 | * @return string for debugging | ||
217 | */ | ||
218 | const char * | ||
219 | GSC_2s (struct CadetClient *c) | ||
220 | { | ||
221 | static char buf[32]; | ||
222 | |||
223 | GNUNET_snprintf (buf, | ||
224 | sizeof (buf), | ||
225 | "Client(%u)", | ||
226 | c->id); | ||
227 | return buf; | ||
228 | } | ||
229 | |||
230 | |||
231 | /** | ||
232 | * Lookup channel of client @a c by @a ccn. | ||
233 | * | ||
234 | * @param c client to look in | ||
235 | * @param ccn channel ID to look up | ||
236 | * @return NULL if no such channel exists | ||
237 | */ | ||
238 | static struct CadetChannel * | ||
239 | lookup_channel (struct CadetClient *c, | ||
240 | struct GNUNET_CADET_ClientChannelNumber ccn) | ||
241 | { | ||
242 | return GNUNET_CONTAINER_multihashmap32_get (c->channels, | ||
243 | ntohl (ccn.channel_of_client)); | ||
244 | } | ||
245 | |||
246 | |||
247 | /** | ||
248 | * Obtain the next LID to use for incoming connections to | ||
249 | * the given client. | ||
250 | * | ||
251 | * @param c client handle | ||
252 | */ | ||
253 | static struct GNUNET_CADET_ClientChannelNumber | ||
254 | client_get_next_ccn (struct CadetClient *c) | ||
255 | { | ||
256 | struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn; | ||
257 | |||
258 | /* increment until we have a free one... */ | ||
259 | while (NULL != | ||
260 | lookup_channel (c, | ||
261 | ccn)) | ||
262 | { | ||
263 | ccn.channel_of_client | ||
264 | = htonl (1 + (ntohl (ccn.channel_of_client))); | ||
265 | if (ntohl (ccn.channel_of_client) >= | ||
266 | GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) | ||
267 | ccn.channel_of_client = htonl (0); | ||
268 | } | ||
269 | c->next_ccn.channel_of_client | ||
270 | = htonl (1 + (ntohl (ccn.channel_of_client))); | ||
271 | return ccn; | ||
272 | } | ||
273 | |||
274 | |||
275 | /** | ||
276 | * Bind incoming channel to this client, and notify client about | ||
277 | * incoming connection. Caller is responsible for notifying the other | ||
278 | * peer about our acceptance of the channel. | ||
279 | * | ||
280 | * @param c client to bind to | ||
281 | * @param ch channel to be bound | ||
282 | * @param dest peer that establishes the connection | ||
283 | * @param port port number | ||
284 | * @param options options | ||
285 | * @return local channel number assigned to the new client | ||
286 | */ | ||
287 | struct GNUNET_CADET_ClientChannelNumber | ||
288 | GSC_bind (struct CadetClient *c, | ||
289 | struct CadetChannel *ch, | ||
290 | struct CadetPeer *dest, | ||
291 | const struct GNUNET_HashCode *port, | ||
292 | uint32_t options) | ||
293 | { | ||
294 | struct GNUNET_MQ_Envelope *env; | ||
295 | struct GNUNET_CADET_LocalChannelCreateMessage *cm; | ||
296 | struct GNUNET_CADET_ClientChannelNumber ccn; | ||
297 | |||
298 | ccn = client_get_next_ccn (c); | ||
299 | GNUNET_assert (GNUNET_YES == | ||
300 | GNUNET_CONTAINER_multihashmap32_put (c->channels, | ||
301 | ntohl (ccn.channel_of_client), | ||
302 | ch, | ||
303 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
304 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
305 | "Accepting incoming %s from %s on open port %s (%u), assigning ccn %X\n", | ||
306 | GCCH_2s (ch), | ||
307 | GCP_2s (dest), | ||
308 | GNUNET_h2s (port), | ||
309 | (uint32_t) ntohl (options), | ||
310 | (uint32_t) ntohl (ccn.channel_of_client)); | ||
311 | /* notify local client about incoming connection! */ | ||
312 | env = GNUNET_MQ_msg (cm, | ||
313 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE); | ||
314 | cm->ccn = ccn; | ||
315 | cm->port = *port; | ||
316 | cm->opt = htonl (options); | ||
317 | cm->peer = *GCP_get_id (dest); | ||
318 | GSC_send_to_client (c, | ||
319 | env); | ||
320 | return ccn; | ||
321 | } | ||
322 | |||
323 | |||
324 | /** | ||
325 | * Callback invoked on all peers to destroy all tunnels | ||
326 | * that may still exist. | ||
327 | * | ||
328 | * @param cls NULL | ||
329 | * @param pid identify of a peer | ||
330 | * @param value a `struct CadetPeer` that may still have a tunnel | ||
331 | * @return #GNUNET_OK (iterate over all entries) | ||
332 | */ | ||
333 | static int | ||
334 | destroy_tunnels_now (void *cls, | ||
335 | const struct GNUNET_PeerIdentity *pid, | ||
336 | void *value) | ||
337 | { | ||
338 | struct CadetPeer *cp = value; | ||
339 | struct CadetTunnel *t = GCP_get_tunnel (cp, | ||
340 | GNUNET_NO); | ||
341 | |||
342 | if (NULL != t) | ||
343 | GCT_destroy_tunnel_now (t); | ||
344 | return GNUNET_OK; | ||
345 | } | ||
346 | |||
347 | |||
348 | /** | ||
349 | * Callback invoked on all peers to destroy all tunnels | ||
350 | * that may still exist. | ||
351 | * | ||
352 | * @param cls NULL | ||
353 | * @param pid identify of a peer | ||
354 | * @param value a `struct CadetPeer` that may still have a tunnel | ||
355 | * @return #GNUNET_OK (iterate over all entries) | ||
356 | */ | ||
357 | static int | ||
358 | destroy_paths_now (void *cls, | ||
359 | const struct GNUNET_PeerIdentity *pid, | ||
360 | void *value) | ||
361 | { | ||
362 | struct CadetPeer *cp = value; | ||
363 | |||
364 | GCP_drop_owned_paths (cp); | ||
365 | return GNUNET_OK; | ||
366 | } | ||
367 | |||
368 | |||
369 | /** | ||
370 | * Shutdown everything once the clients have disconnected. | ||
371 | */ | ||
372 | static void | ||
373 | shutdown_rest () | ||
374 | { | ||
375 | if (NULL != stats) | ||
376 | { | ||
377 | GNUNET_STATISTICS_destroy (stats, | ||
378 | GNUNET_NO); | ||
379 | stats = NULL; | ||
380 | } | ||
381 | if (NULL != open_ports) | ||
382 | { | ||
383 | GNUNET_CONTAINER_multihashmap_destroy (open_ports); | ||
384 | open_ports = NULL; | ||
385 | } | ||
386 | if (NULL != loose_channels) | ||
387 | { | ||
388 | GNUNET_CONTAINER_multihashmap_destroy (loose_channels); | ||
389 | loose_channels = NULL; | ||
390 | } | ||
391 | /* Destroy tunnels. Note that all channels must be destroyed first! */ | ||
392 | GCP_iterate_all (&destroy_tunnels_now, | ||
393 | NULL); | ||
394 | /* All tunnels, channels, connections and CORE must be down before this point. */ | ||
395 | GCP_iterate_all (&destroy_paths_now, | ||
396 | NULL); | ||
397 | /* All paths, tunnels, channels, connections and CORE must be down before this point. */ | ||
398 | GCP_destroy_all_peers (); | ||
399 | if (NULL != peers) | ||
400 | { | ||
401 | GNUNET_CONTAINER_multipeermap_destroy (peers); | ||
402 | peers = NULL; | ||
403 | } | ||
404 | if (NULL != connections) | ||
405 | { | ||
406 | GNUNET_CONTAINER_multishortmap_destroy (connections); | ||
407 | connections = NULL; | ||
408 | } | ||
409 | if (NULL != ats_ch) | ||
410 | { | ||
411 | GNUNET_ATS_connectivity_done (ats_ch); | ||
412 | ats_ch = NULL; | ||
413 | } | ||
414 | GCD_shutdown (); | ||
415 | GCH_shutdown (); | ||
416 | GNUNET_free_non_null (my_private_key); | ||
417 | my_private_key = NULL; | ||
418 | } | ||
419 | |||
420 | |||
421 | /** | ||
422 | * Task run during shutdown. | ||
423 | * | ||
424 | * @param cls unused | ||
425 | */ | ||
426 | static void | ||
427 | shutdown_task (void *cls) | ||
428 | { | ||
429 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
430 | "Shutting down\n"); | ||
431 | shutting_down = GNUNET_YES; | ||
432 | GCO_shutdown (); | ||
433 | if (NULL == clients_head) | ||
434 | shutdown_rest (); | ||
435 | } | ||
436 | |||
437 | |||
438 | /** | ||
439 | * We had a remote connection @a value to port @a port before | ||
440 | * client @a cls opened port @a port. Bind them now. | ||
441 | * | ||
442 | * @param cls the `struct CadetClient` | ||
443 | * @param port the port | ||
444 | * @param value the `struct CadetChannel` | ||
445 | * @return #GNUNET_YES (iterate over all such channels) | ||
446 | */ | ||
447 | static int | ||
448 | bind_loose_channel (void *cls, | ||
449 | const struct GNUNET_HashCode *port, | ||
450 | void *value) | ||
451 | { | ||
452 | struct CadetClient *c = cls; | ||
453 | struct CadetChannel *ch = value; | ||
454 | |||
455 | GCCH_bind (ch, | ||
456 | c); | ||
457 | GNUNET_assert (GNUNET_YES == | ||
458 | GNUNET_CONTAINER_multihashmap_remove (loose_channels, | ||
459 | port, | ||
460 | value)); | ||
461 | return GNUNET_YES; | ||
462 | } | ||
463 | |||
464 | |||
465 | /** | ||
466 | * Handle port open request. Creates a mapping from the | ||
467 | * port to the respective client and checks whether we have | ||
468 | * loose channels trying to bind to the port. If so, those | ||
469 | * are bound. | ||
470 | * | ||
471 | * @param cls Identification of the client. | ||
472 | * @param pmsg The actual message. | ||
473 | */ | ||
474 | static void | ||
475 | handle_port_open (void *cls, | ||
476 | const struct GNUNET_CADET_PortMessage *pmsg) | ||
477 | { | ||
478 | struct CadetClient *c = cls; | ||
479 | |||
480 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
481 | "Open port %s requested by %s\n", | ||
482 | GNUNET_h2s (&pmsg->port), | ||
483 | GSC_2s (c)); | ||
484 | if (NULL == c->ports) | ||
485 | c->ports = GNUNET_CONTAINER_multihashmap_create (4, | ||
486 | GNUNET_NO); | ||
487 | if (GNUNET_OK != | ||
488 | GNUNET_CONTAINER_multihashmap_put (c->ports, | ||
489 | &pmsg->port, | ||
490 | c, | ||
491 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) | ||
492 | { | ||
493 | GNUNET_break (0); | ||
494 | GNUNET_SERVICE_client_drop (c->client); | ||
495 | return; | ||
496 | } | ||
497 | (void) GNUNET_CONTAINER_multihashmap_put (open_ports, | ||
498 | &pmsg->port, | ||
499 | c, | ||
500 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
501 | GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels, | ||
502 | &pmsg->port, | ||
503 | &bind_loose_channel, | ||
504 | c); | ||
505 | GNUNET_SERVICE_client_continue (c->client); | ||
506 | } | ||
507 | |||
508 | |||
509 | /** | ||
510 | * Handler for port close requests. Marks this port as closed | ||
511 | * (unless of course we have another client with the same port | ||
512 | * open). Note that existing channels accepted on the port are | ||
513 | * not affected. | ||
514 | * | ||
515 | * @param cls Identification of the client. | ||
516 | * @param pmsg The actual message. | ||
517 | */ | ||
518 | static void | ||
519 | handle_port_close (void *cls, | ||
520 | const struct GNUNET_CADET_PortMessage *pmsg) | ||
521 | { | ||
522 | struct CadetClient *c = cls; | ||
523 | |||
524 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
525 | "Closing port %s as requested by %s\n", | ||
526 | GNUNET_h2s (&pmsg->port), | ||
527 | GSC_2s (c)); | ||
528 | if (GNUNET_YES != | ||
529 | GNUNET_CONTAINER_multihashmap_remove (c->ports, | ||
530 | &pmsg->port, | ||
531 | c)) | ||
532 | { | ||
533 | GNUNET_break (0); | ||
534 | GNUNET_SERVICE_client_drop (c->client); | ||
535 | return; | ||
536 | } | ||
537 | GNUNET_assert (GNUNET_YES == | ||
538 | GNUNET_CONTAINER_multihashmap_remove (open_ports, | ||
539 | &pmsg->port, | ||
540 | c)); | ||
541 | GNUNET_SERVICE_client_continue (c->client); | ||
542 | } | ||
543 | |||
544 | |||
545 | /** | ||
546 | * Handler for requests for us creating a new channel to another peer and port. | ||
547 | * | ||
548 | * @param cls Identification of the client. | ||
549 | * @param tcm The actual message. | ||
550 | */ | ||
551 | static void | ||
552 | handle_channel_create (void *cls, | ||
553 | const struct GNUNET_CADET_LocalChannelCreateMessage *tcm) | ||
554 | { | ||
555 | struct CadetClient *c = cls; | ||
556 | struct CadetChannel *ch; | ||
557 | |||
558 | if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) | ||
559 | { | ||
560 | /* Channel ID not in allowed range. */ | ||
561 | GNUNET_break (0); | ||
562 | GNUNET_SERVICE_client_drop (c->client); | ||
563 | return; | ||
564 | } | ||
565 | ch = lookup_channel (c, | ||
566 | tcm->ccn); | ||
567 | if (NULL != ch) | ||
568 | { | ||
569 | /* Channel ID already in use. Not allowed. */ | ||
570 | GNUNET_break (0); | ||
571 | GNUNET_SERVICE_client_drop (c->client); | ||
572 | return; | ||
573 | } | ||
574 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
575 | "New channel to %s at port %s requested by %s\n", | ||
576 | GNUNET_i2s (&tcm->peer), | ||
577 | GNUNET_h2s (&tcm->port), | ||
578 | GSC_2s (c)); | ||
579 | |||
580 | /* Create channel */ | ||
581 | ch = GCCH_channel_local_new (c, | ||
582 | tcm->ccn, | ||
583 | GCP_get (&tcm->peer, | ||
584 | GNUNET_YES), | ||
585 | &tcm->port, | ||
586 | ntohl (tcm->opt)); | ||
587 | if (NULL == ch) | ||
588 | { | ||
589 | GNUNET_break (0); | ||
590 | GNUNET_SERVICE_client_drop (c->client); | ||
591 | return; | ||
592 | } | ||
593 | GNUNET_assert (GNUNET_YES == | ||
594 | GNUNET_CONTAINER_multihashmap32_put (c->channels, | ||
595 | ntohl (tcm->ccn.channel_of_client), | ||
596 | ch, | ||
597 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
598 | |||
599 | GNUNET_SERVICE_client_continue (c->client); | ||
600 | } | ||
601 | |||
602 | |||
603 | /** | ||
604 | * Handler for requests of destroying an existing channel. | ||
605 | * | ||
606 | * @param cls client identification of the client | ||
607 | * @param msg the actual message | ||
608 | */ | ||
609 | static void | ||
610 | handle_channel_destroy (void *cls, | ||
611 | const struct GNUNET_CADET_LocalChannelDestroyMessage *msg) | ||
612 | { | ||
613 | struct CadetClient *c = cls; | ||
614 | struct CadetChannel *ch; | ||
615 | |||
616 | ch = lookup_channel (c, | ||
617 | msg->ccn); | ||
618 | if (NULL == ch) | ||
619 | { | ||
620 | /* Client attempted to destroy unknown channel. | ||
621 | Can happen if the other side went down at the same time.*/ | ||
622 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
623 | "%s tried to destroy unknown channel %X\n", | ||
624 | GSC_2s(c), | ||
625 | (uint32_t) ntohl (msg->ccn.channel_of_client)); | ||
626 | return; | ||
627 | } | ||
628 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
629 | "%s is destroying %s\n", | ||
630 | GSC_2s(c), | ||
631 | GCCH_2s (ch)); | ||
632 | GNUNET_assert (GNUNET_YES == | ||
633 | GNUNET_CONTAINER_multihashmap32_remove (c->channels, | ||
634 | ntohl (msg->ccn.channel_of_client), | ||
635 | ch)); | ||
636 | GCCH_channel_local_destroy (ch, | ||
637 | c, | ||
638 | msg->ccn); | ||
639 | GNUNET_SERVICE_client_continue (c->client); | ||
640 | } | ||
641 | |||
642 | |||
643 | /** | ||
644 | * Check for client traffic data message is well-formed. | ||
645 | * | ||
646 | * @param cls identification of the client | ||
647 | * @param msg the actual message | ||
648 | * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not | ||
649 | */ | ||
650 | static int | ||
651 | check_local_data (void *cls, | ||
652 | const struct GNUNET_CADET_LocalData *msg) | ||
653 | { | ||
654 | size_t payload_size; | ||
655 | size_t payload_claimed_size; | ||
656 | const char *buf; | ||
657 | struct GNUNET_MessageHeader pa; | ||
658 | |||
659 | /* FIXME: what is the format we shall allow for @a msg? | ||
660 | ONE payload item or multiple? Seems current cadet_api | ||
661 | at least in theory allows more than one. Next-gen | ||
662 | cadet_api will likely no more, so we could then | ||
663 | simplify this mess again. */ | ||
664 | /* Sanity check for message size */ | ||
665 | payload_size = ntohs (msg->header.size) - sizeof (*msg); | ||
666 | buf = (const char *) &msg[1]; | ||
667 | while (payload_size >= sizeof (struct GNUNET_MessageHeader)) | ||
668 | { | ||
669 | /* need to memcpy() for alignment */ | ||
670 | GNUNET_memcpy (&pa, | ||
671 | buf, | ||
672 | sizeof (pa)); | ||
673 | payload_claimed_size = ntohs (pa.size); | ||
674 | if ( (payload_size < payload_claimed_size) || | ||
675 | (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) || | ||
676 | (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) ) | ||
677 | { | ||
678 | GNUNET_break (0); | ||
679 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
680 | "Local data of %u total size had sub-message %u at %u with %u bytes\n", | ||
681 | ntohs (msg->header.size), | ||
682 | ntohs (pa.type), | ||
683 | (unsigned int) (buf - (const char *) &msg[1]), | ||
684 | (unsigned int) payload_claimed_size); | ||
685 | return GNUNET_SYSERR; | ||
686 | } | ||
687 | payload_size -= payload_claimed_size; | ||
688 | buf += payload_claimed_size; | ||
689 | } | ||
690 | if (0 != payload_size) | ||
691 | { | ||
692 | GNUNET_break_op (0); | ||
693 | return GNUNET_SYSERR; | ||
694 | } | ||
695 | return GNUNET_OK; | ||
696 | } | ||
697 | |||
698 | |||
699 | /** | ||
700 | * Handler for client payload traffic to be send on a channel to | ||
701 | * another peer. | ||
702 | * | ||
703 | * @param cls identification of the client | ||
704 | * @param msg the actual message | ||
705 | */ | ||
706 | static void | ||
707 | handle_local_data (void *cls, | ||
708 | const struct GNUNET_CADET_LocalData *msg) | ||
709 | { | ||
710 | struct CadetClient *c = cls; | ||
711 | struct CadetChannel *ch; | ||
712 | size_t payload_size; | ||
713 | const char *buf; | ||
714 | |||
715 | ch = lookup_channel (c, | ||
716 | msg->ccn); | ||
717 | if (NULL == ch) | ||
718 | { | ||
719 | /* Channel does not exist (anymore) */ | ||
720 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
721 | "Dropping payload for channel %u from client (channel unknown, other endpoint may have disconnected)\n", | ||
722 | (unsigned int) ntohl (msg->ccn.channel_of_client)); | ||
723 | GNUNET_SERVICE_client_continue (c->client); | ||
724 | return; | ||
725 | } | ||
726 | payload_size = ntohs (msg->header.size) - sizeof (*msg); | ||
727 | GNUNET_STATISTICS_update (stats, | ||
728 | "# payload received from clients", | ||
729 | payload_size, | ||
730 | GNUNET_NO); | ||
731 | buf = (const char *) &msg[1]; | ||
732 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
733 | "Received %u bytes payload from %s for %s\n", | ||
734 | (unsigned int) payload_size, | ||
735 | GSC_2s (c), | ||
736 | GCCH_2s (ch)); | ||
737 | if (GNUNET_OK != | ||
738 | GCCH_handle_local_data (ch, | ||
739 | msg->ccn, | ||
740 | buf, | ||
741 | payload_size)) | ||
742 | { | ||
743 | GNUNET_SERVICE_client_drop (c->client); | ||
744 | return; | ||
745 | } | ||
746 | GNUNET_SERVICE_client_continue (c->client); | ||
747 | } | ||
748 | |||
749 | |||
750 | /** | ||
751 | * Handler for client's ACKs for payload traffic. | ||
752 | * | ||
753 | * @param cls identification of the client. | ||
754 | * @param msg The actual message. | ||
755 | */ | ||
756 | static void | ||
757 | handle_local_ack (void *cls, | ||
758 | const struct GNUNET_CADET_LocalAck *msg) | ||
759 | { | ||
760 | struct CadetClient *c = cls; | ||
761 | struct CadetChannel *ch; | ||
762 | |||
763 | ch = lookup_channel (c, | ||
764 | msg->ccn); | ||
765 | if (NULL == ch) | ||
766 | { | ||
767 | /* Channel does not exist (anymore) */ | ||
768 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
769 | "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n", | ||
770 | (unsigned int) ntohl (msg->ccn.channel_of_client)); | ||
771 | GNUNET_SERVICE_client_continue (c->client); | ||
772 | return; | ||
773 | } | ||
774 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
775 | "Got a local ACK from %s for %s\n", | ||
776 | GSC_2s(c), | ||
777 | GCCH_2s (ch)); | ||
778 | GCCH_handle_local_ack (ch, | ||
779 | msg->ccn); | ||
780 | GNUNET_SERVICE_client_continue (c->client); | ||
781 | } | ||
782 | |||
783 | |||
784 | /** | ||
785 | * Iterator over all peers to send a monitoring client info about each peer. | ||
786 | * | ||
787 | * @param cls Closure (). | ||
788 | * @param peer Peer ID (tunnel remote peer). | ||
789 | * @param value Peer info. | ||
790 | * @return #GNUNET_YES, to keep iterating. | ||
791 | */ | ||
792 | static int | ||
793 | get_all_peers_iterator (void *cls, | ||
794 | const struct GNUNET_PeerIdentity *peer, | ||
795 | void *value) | ||
796 | { | ||
797 | struct CadetClient *c = cls; | ||
798 | struct CadetPeer *p = value; | ||
799 | struct GNUNET_MQ_Envelope *env; | ||
800 | struct GNUNET_CADET_LocalInfoPeer *msg; | ||
801 | |||
802 | env = GNUNET_MQ_msg (msg, | ||
803 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS); | ||
804 | msg->destination = *peer; | ||
805 | msg->paths = htons (GCP_count_paths (p)); | ||
806 | msg->tunnel = htons (NULL != GCP_get_tunnel (p, | ||
807 | GNUNET_NO)); | ||
808 | GNUNET_MQ_send (c->mq, | ||
809 | env); | ||
810 | return GNUNET_YES; | ||
811 | } | ||
812 | |||
813 | |||
814 | /** | ||
815 | * Handler for client's INFO PEERS request. | ||
816 | * | ||
817 | * @param cls Identification of the client. | ||
818 | * @param message The actual message. | ||
819 | */ | ||
820 | static void | ||
821 | handle_get_peers (void *cls, | ||
822 | const struct GNUNET_MessageHeader *message) | ||
823 | { | ||
824 | struct CadetClient *c = cls; | ||
825 | struct GNUNET_MQ_Envelope *env; | ||
826 | struct GNUNET_MessageHeader *reply; | ||
827 | |||
828 | GCP_iterate_all (&get_all_peers_iterator, | ||
829 | c); | ||
830 | env = GNUNET_MQ_msg (reply, | ||
831 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS); | ||
832 | GNUNET_MQ_send (c->mq, | ||
833 | env); | ||
834 | GNUNET_SERVICE_client_continue (c->client); | ||
835 | } | ||
836 | |||
837 | |||
838 | /** | ||
839 | * Iterator over all paths of a peer to build an InfoPeer message. | ||
840 | * Message contains blocks of peers, first not included. | ||
841 | * | ||
842 | * @param cls message queue for transmission | ||
843 | * @param path Path itself | ||
844 | * @param off offset of the peer on @a path | ||
845 | * @return #GNUNET_YES if should keep iterating. | ||
846 | * #GNUNET_NO otherwise. | ||
847 | */ | ||
848 | static int | ||
849 | path_info_iterator (void *cls, | ||
850 | struct CadetPeerPath *path, | ||
851 | unsigned int off) | ||
852 | { | ||
853 | struct GNUNET_MQ_Handle *mq = cls; | ||
854 | struct GNUNET_MQ_Envelope *env; | ||
855 | struct GNUNET_MessageHeader *resp; | ||
856 | struct GNUNET_PeerIdentity *id; | ||
857 | uint16_t path_size; | ||
858 | unsigned int i; | ||
859 | unsigned int path_length; | ||
860 | |||
861 | path_length = GCPP_get_length (path); | ||
862 | path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1); | ||
863 | if (sizeof (*resp) + path_size > UINT16_MAX) | ||
864 | { | ||
865 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
866 | "Path of %u entries is too long for info message\n", | ||
867 | path_length); | ||
868 | return GNUNET_YES; | ||
869 | } | ||
870 | env = GNUNET_MQ_msg_extra (resp, | ||
871 | path_size, | ||
872 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER); | ||
873 | id = (struct GNUNET_PeerIdentity *) &resp[1]; | ||
874 | |||
875 | /* Don't copy first peer. First peer is always the local one. Last | ||
876 | * peer is always the destination (leave as 0, EOL). | ||
877 | */ | ||
878 | for (i = 0; i < off; i++) | ||
879 | id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path, | ||
880 | i + 1)); | ||
881 | GNUNET_MQ_send (mq, | ||
882 | env); | ||
883 | return GNUNET_YES; | ||
884 | } | ||
885 | |||
886 | |||
887 | /** | ||
888 | * Handler for client's SHOW_PEER request. | ||
889 | * | ||
890 | * @param cls Identification of the client. | ||
891 | * @param msg The actual message. | ||
892 | */ | ||
893 | static void | ||
894 | handle_show_peer (void *cls, | ||
895 | const struct GNUNET_CADET_LocalInfo *msg) | ||
896 | { | ||
897 | struct CadetClient *c = cls; | ||
898 | struct CadetPeer *p; | ||
899 | struct GNUNET_MQ_Envelope *env; | ||
900 | struct GNUNET_MessageHeader *resp; | ||
901 | |||
902 | p = GCP_get (&msg->peer, | ||
903 | GNUNET_NO); | ||
904 | if (NULL != p) | ||
905 | GCP_iterate_paths (p, | ||
906 | &path_info_iterator, | ||
907 | c->mq); | ||
908 | /* Send message with 0/0 to indicate the end */ | ||
909 | env = GNUNET_MQ_msg (resp, | ||
910 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END); | ||
911 | GNUNET_MQ_send (c->mq, | ||
912 | env); | ||
913 | GNUNET_SERVICE_client_continue (c->client); | ||
914 | } | ||
915 | |||
916 | |||
917 | /** | ||
918 | * Iterator over all tunnels to send a monitoring client info about each tunnel. | ||
919 | * | ||
920 | * @param cls Closure (). | ||
921 | * @param peer Peer ID (tunnel remote peer). | ||
922 | * @param value a `struct CadetPeer` | ||
923 | * @return #GNUNET_YES, to keep iterating. | ||
924 | */ | ||
925 | static int | ||
926 | get_all_tunnels_iterator (void *cls, | ||
927 | const struct GNUNET_PeerIdentity *peer, | ||
928 | void *value) | ||
929 | { | ||
930 | struct CadetClient *c = cls; | ||
931 | struct CadetPeer *p = value; | ||
932 | struct GNUNET_MQ_Envelope *env; | ||
933 | struct GNUNET_CADET_LocalInfoTunnel *msg; | ||
934 | struct CadetTunnel *t; | ||
935 | |||
936 | t = GCP_get_tunnel (p, | ||
937 | GNUNET_NO); | ||
938 | if (NULL == t) | ||
939 | return GNUNET_YES; | ||
940 | env = GNUNET_MQ_msg (msg, | ||
941 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS); | ||
942 | msg->destination = *peer; | ||
943 | msg->channels = htonl (GCT_count_channels (t)); | ||
944 | msg->connections = htonl (GCT_count_any_connections (t)); | ||
945 | msg->cstate = htons (0); | ||
946 | msg->estate = htons ((uint16_t) GCT_get_estate (t)); | ||
947 | GNUNET_MQ_send (c->mq, | ||
948 | env); | ||
949 | return GNUNET_YES; | ||
950 | } | ||
951 | |||
952 | |||
953 | /** | ||
954 | * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS request. | ||
955 | * | ||
956 | * @param cls client Identification of the client. | ||
957 | * @param message The actual message. | ||
958 | */ | ||
959 | static void | ||
960 | handle_info_tunnels (void *cls, | ||
961 | const struct GNUNET_MessageHeader *message) | ||
962 | { | ||
963 | struct CadetClient *c = cls; | ||
964 | struct GNUNET_MQ_Envelope *env; | ||
965 | struct GNUNET_MessageHeader *reply; | ||
966 | |||
967 | GCP_iterate_all (&get_all_tunnels_iterator, | ||
968 | c); | ||
969 | env = GNUNET_MQ_msg (reply, | ||
970 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS); | ||
971 | GNUNET_MQ_send (c->mq, | ||
972 | env); | ||
973 | GNUNET_SERVICE_client_continue (c->client); | ||
974 | } | ||
975 | |||
976 | |||
977 | /** | ||
978 | * Update the message with information about the connection. | ||
979 | * | ||
980 | * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update | ||
981 | * @param ct a connection about which we should store information in @a cls | ||
982 | */ | ||
983 | static void | ||
984 | iter_connection (void *cls, | ||
985 | struct CadetTConnection *ct) | ||
986 | { | ||
987 | struct GNUNET_CADET_LocalInfoTunnel *msg = cls; | ||
988 | struct CadetConnection *cc = ct->cc; | ||
989 | struct GNUNET_CADET_ConnectionTunnelIdentifier *h; | ||
990 | |||
991 | h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1]; | ||
992 | h[msg->connections++] = *(GCC_get_id (cc)); | ||
993 | } | ||
994 | |||
995 | |||
996 | /** | ||
997 | * Update the message with information about the channel. | ||
998 | * | ||
999 | * @param cls a `struct GNUNET_CADET_LocalInfoTunnel` message to update | ||
1000 | * @param ch a channel about which we should store information in @a cls | ||
1001 | */ | ||
1002 | static void | ||
1003 | iter_channel (void *cls, | ||
1004 | struct CadetChannel *ch) | ||
1005 | { | ||
1006 | struct GNUNET_CADET_LocalInfoTunnel *msg = cls; | ||
1007 | struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1]; | ||
1008 | struct GNUNET_CADET_ChannelTunnelNumber *chn | ||
1009 | = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections]; | ||
1010 | |||
1011 | chn[msg->channels++] = GCCH_get_id (ch); | ||
1012 | } | ||
1013 | |||
1014 | |||
1015 | /** | ||
1016 | * Handler for client's #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL request. | ||
1017 | * | ||
1018 | * @param cls Identification of the client. | ||
1019 | * @param msg The actual message. | ||
1020 | */ | ||
1021 | static void | ||
1022 | handle_info_tunnel (void *cls, | ||
1023 | const struct GNUNET_CADET_LocalInfo *msg) | ||
1024 | { | ||
1025 | struct CadetClient *c = cls; | ||
1026 | struct GNUNET_MQ_Envelope *env; | ||
1027 | struct GNUNET_CADET_LocalInfoTunnel *resp; | ||
1028 | struct CadetTunnel *t; | ||
1029 | struct CadetPeer *p; | ||
1030 | unsigned int ch_n; | ||
1031 | unsigned int c_n; | ||
1032 | |||
1033 | p = GCP_get (&msg->peer, | ||
1034 | GNUNET_NO); | ||
1035 | t = GCP_get_tunnel (p, | ||
1036 | GNUNET_NO); | ||
1037 | if (NULL == t) | ||
1038 | { | ||
1039 | /* We don't know the tunnel */ | ||
1040 | struct GNUNET_MQ_Envelope *env; | ||
1041 | struct GNUNET_CADET_LocalInfoTunnel *warn; | ||
1042 | |||
1043 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
1044 | "Tunnel to %s unknown\n", | ||
1045 | GNUNET_i2s_full (&msg->peer)); | ||
1046 | env = GNUNET_MQ_msg (warn, | ||
1047 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL); | ||
1048 | warn->destination = msg->peer; | ||
1049 | GNUNET_MQ_send (c->mq, | ||
1050 | env); | ||
1051 | GNUNET_SERVICE_client_continue (c->client); | ||
1052 | return; | ||
1053 | } | ||
1054 | |||
1055 | /* Initialize context */ | ||
1056 | ch_n = GCT_count_channels (t); | ||
1057 | c_n = GCT_count_any_connections (t); | ||
1058 | env = GNUNET_MQ_msg_extra (resp, | ||
1059 | c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) + | ||
1060 | ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber), | ||
1061 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL); | ||
1062 | resp->destination = msg->peer; | ||
1063 | /* Do not reorder! #iter_channel needs counters in HBO! */ | ||
1064 | GCT_iterate_connections (t, | ||
1065 | &iter_connection, | ||
1066 | resp); | ||
1067 | GCT_iterate_channels (t, | ||
1068 | &iter_channel, | ||
1069 | resp); | ||
1070 | resp->connections = htonl (resp->connections); | ||
1071 | resp->channels = htonl (resp->channels); | ||
1072 | resp->cstate = htons (0); | ||
1073 | resp->estate = htons (GCT_get_estate (t)); | ||
1074 | GNUNET_MQ_send (c->mq, | ||
1075 | env); | ||
1076 | GNUNET_SERVICE_client_continue (c->client); | ||
1077 | } | ||
1078 | |||
1079 | |||
1080 | /** | ||
1081 | * Iterator over all peers to dump info for each peer. | ||
1082 | * | ||
1083 | * @param cls Closure (unused). | ||
1084 | * @param peer Peer ID (tunnel remote peer). | ||
1085 | * @param value Peer info. | ||
1086 | * | ||
1087 | * @return #GNUNET_YES, to keep iterating. | ||
1088 | */ | ||
1089 | static int | ||
1090 | show_peer_iterator (void *cls, | ||
1091 | const struct GNUNET_PeerIdentity *peer, | ||
1092 | void *value) | ||
1093 | { | ||
1094 | struct CadetPeer *p = value; | ||
1095 | struct CadetTunnel *t; | ||
1096 | |||
1097 | t = GCP_get_tunnel (p, | ||
1098 | GNUNET_NO); | ||
1099 | if (NULL != t) | ||
1100 | GCT_debug (t, | ||
1101 | GNUNET_ERROR_TYPE_ERROR); | ||
1102 | LOG (GNUNET_ERROR_TYPE_ERROR, "\n"); | ||
1103 | return GNUNET_YES; | ||
1104 | } | ||
1105 | |||
1106 | |||
1107 | /** | ||
1108 | * Handler for client's INFO_DUMP request. | ||
1109 | * | ||
1110 | * @param cls Identification of the client. | ||
1111 | * @param message The actual message. | ||
1112 | */ | ||
1113 | static void | ||
1114 | handle_info_dump (void *cls, | ||
1115 | const struct GNUNET_MessageHeader *message) | ||
1116 | { | ||
1117 | struct CadetClient *c = cls; | ||
1118 | |||
1119 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
1120 | "Received dump info request from client %u\n", | ||
1121 | c->id); | ||
1122 | |||
1123 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
1124 | "*************************** DUMP START ***************************\n"); | ||
1125 | for (struct CadetClient *ci = clients_head; | ||
1126 | NULL != ci; | ||
1127 | ci = ci->next) | ||
1128 | { | ||
1129 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
1130 | "Client %u (%p), handle: %p, ports: %u, channels: %u\n", | ||
1131 | ci->id, | ||
1132 | ci, | ||
1133 | ci->client, | ||
1134 | (NULL != c->ports) | ||
1135 | ? GNUNET_CONTAINER_multihashmap_size (ci->ports) | ||
1136 | : 0, | ||
1137 | GNUNET_CONTAINER_multihashmap32_size (ci->channels)); | ||
1138 | } | ||
1139 | LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n"); | ||
1140 | GCP_iterate_all (&show_peer_iterator, | ||
1141 | NULL); | ||
1142 | |||
1143 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
1144 | "**************************** DUMP END ****************************\n"); | ||
1145 | |||
1146 | GNUNET_SERVICE_client_continue (c->client); | ||
1147 | } | ||
1148 | |||
1149 | |||
1150 | |||
1151 | /** | ||
1152 | * Callback called when a client connects to the service. | ||
1153 | * | ||
1154 | * @param cls closure for the service | ||
1155 | * @param client the new client that connected to the service | ||
1156 | * @param mq the message queue used to send messages to the client | ||
1157 | * @return @a c | ||
1158 | */ | ||
1159 | static void * | ||
1160 | client_connect_cb (void *cls, | ||
1161 | struct GNUNET_SERVICE_Client *client, | ||
1162 | struct GNUNET_MQ_Handle *mq) | ||
1163 | { | ||
1164 | struct CadetClient *c; | ||
1165 | |||
1166 | c = GNUNET_new (struct CadetClient); | ||
1167 | c->client = client; | ||
1168 | c->mq = mq; | ||
1169 | c->id = next_client_id++; /* overflow not important: just for debug */ | ||
1170 | c->channels | ||
1171 | = GNUNET_CONTAINER_multihashmap32_create (32); | ||
1172 | GNUNET_CONTAINER_DLL_insert (clients_head, | ||
1173 | clients_tail, | ||
1174 | c); | ||
1175 | GNUNET_STATISTICS_update (stats, | ||
1176 | "# clients", | ||
1177 | +1, | ||
1178 | GNUNET_NO); | ||
1179 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1180 | "%s connected\n", | ||
1181 | GSC_2s (c)); | ||
1182 | return c; | ||
1183 | } | ||
1184 | |||
1185 | |||
1186 | /** | ||
1187 | * A channel was destroyed by the other peer. Tell our client. | ||
1188 | * | ||
1189 | * @param c client that lost a channel | ||
1190 | * @param ccn channel identification number for the client | ||
1191 | * @param ch the channel object | ||
1192 | */ | ||
1193 | void | ||
1194 | GSC_handle_remote_channel_destroy (struct CadetClient *c, | ||
1195 | struct GNUNET_CADET_ClientChannelNumber ccn, | ||
1196 | struct CadetChannel *ch) | ||
1197 | { | ||
1198 | struct GNUNET_MQ_Envelope *env; | ||
1199 | struct GNUNET_CADET_LocalChannelDestroyMessage *tdm; | ||
1200 | |||
1201 | env = GNUNET_MQ_msg (tdm, | ||
1202 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY); | ||
1203 | tdm->ccn = ccn; | ||
1204 | GSC_send_to_client (c, | ||
1205 | env); | ||
1206 | GNUNET_assert (GNUNET_YES == | ||
1207 | GNUNET_CONTAINER_multihashmap32_remove (c->channels, | ||
1208 | ntohl (ccn.channel_of_client), | ||
1209 | ch)); | ||
1210 | } | ||
1211 | |||
1212 | |||
1213 | /** | ||
1214 | * A client that created a loose channel that was not bound to a port | ||
1215 | * disconnected, drop it from the #loose_channels list. | ||
1216 | * | ||
1217 | * @param port the port the channel was trying to bind to | ||
1218 | * @param ch the channel that was lost | ||
1219 | */ | ||
1220 | void | ||
1221 | GSC_drop_loose_channel (const struct GNUNET_HashCode *port, | ||
1222 | struct CadetChannel *ch) | ||
1223 | { | ||
1224 | GNUNET_assert (GNUNET_YES == | ||
1225 | GNUNET_CONTAINER_multihashmap_remove (loose_channels, | ||
1226 | port, | ||
1227 | ch)); | ||
1228 | } | ||
1229 | |||
1230 | |||
1231 | /** | ||
1232 | * Iterator for deleting each channel whose client endpoint disconnected. | ||
1233 | * | ||
1234 | * @param cls Closure (client that has disconnected). | ||
1235 | * @param key The local channel id in host byte order | ||
1236 | * @param value The value stored at the key (channel to destroy). | ||
1237 | * @return #GNUNET_OK, keep iterating. | ||
1238 | */ | ||
1239 | static int | ||
1240 | channel_destroy_iterator (void *cls, | ||
1241 | uint32_t key, | ||
1242 | void *value) | ||
1243 | { | ||
1244 | struct CadetClient *c = cls; | ||
1245 | struct GNUNET_CADET_ClientChannelNumber ccn; | ||
1246 | struct CadetChannel *ch = value; | ||
1247 | |||
1248 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1249 | "Destroying %s, due to %s disconnecting.\n", | ||
1250 | GCCH_2s (ch), | ||
1251 | GSC_2s (c)); | ||
1252 | ccn.channel_of_client = htonl (key); | ||
1253 | GCCH_channel_local_destroy (ch, | ||
1254 | c, | ||
1255 | ccn); | ||
1256 | GNUNET_assert (GNUNET_YES == | ||
1257 | GNUNET_CONTAINER_multihashmap32_remove (c->channels, | ||
1258 | key, | ||
1259 | ch)); | ||
1260 | return GNUNET_OK; | ||
1261 | } | ||
1262 | |||
1263 | |||
1264 | /** | ||
1265 | * Remove client's ports from the global hashmap on disconnect. | ||
1266 | * | ||
1267 | * @param cls Closure (unused). | ||
1268 | * @param key the port. | ||
1269 | * @param value the `struct CadetClient` to remove | ||
1270 | * @return #GNUNET_OK, keep iterating. | ||
1271 | */ | ||
1272 | static int | ||
1273 | client_release_ports (void *cls, | ||
1274 | const struct GNUNET_HashCode *key, | ||
1275 | void *value) | ||
1276 | { | ||
1277 | struct CadetClient *c = value; | ||
1278 | |||
1279 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1280 | "Closing port %s due to %s disconnect.\n", | ||
1281 | GNUNET_h2s (key), | ||
1282 | GSC_2s (c)); | ||
1283 | GNUNET_assert (GNUNET_YES == | ||
1284 | GNUNET_CONTAINER_multihashmap_remove (open_ports, | ||
1285 | key, | ||
1286 | value)); | ||
1287 | GNUNET_assert (GNUNET_YES == | ||
1288 | GNUNET_CONTAINER_multihashmap_remove (c->ports, | ||
1289 | key, | ||
1290 | value)); | ||
1291 | return GNUNET_OK; | ||
1292 | } | ||
1293 | |||
1294 | |||
1295 | /** | ||
1296 | * Callback called when a client disconnected from the service | ||
1297 | * | ||
1298 | * @param cls closure for the service | ||
1299 | * @param client the client that disconnected | ||
1300 | * @param internal_cls should be equal to @a c | ||
1301 | */ | ||
1302 | static void | ||
1303 | client_disconnect_cb (void *cls, | ||
1304 | struct GNUNET_SERVICE_Client *client, | ||
1305 | void *internal_cls) | ||
1306 | { | ||
1307 | struct CadetClient *c = internal_cls; | ||
1308 | |||
1309 | GNUNET_assert (c->client == client); | ||
1310 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1311 | "%s is disconnecting.\n", | ||
1312 | GSC_2s (c)); | ||
1313 | if (NULL != c->channels) | ||
1314 | { | ||
1315 | GNUNET_CONTAINER_multihashmap32_iterate (c->channels, | ||
1316 | &channel_destroy_iterator, | ||
1317 | c); | ||
1318 | GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels)); | ||
1319 | GNUNET_CONTAINER_multihashmap32_destroy (c->channels); | ||
1320 | } | ||
1321 | if (NULL != c->ports) | ||
1322 | { | ||
1323 | GNUNET_CONTAINER_multihashmap_iterate (c->ports, | ||
1324 | &client_release_ports, | ||
1325 | c); | ||
1326 | GNUNET_CONTAINER_multihashmap_destroy (c->ports); | ||
1327 | } | ||
1328 | GNUNET_CONTAINER_DLL_remove (clients_head, | ||
1329 | clients_tail, | ||
1330 | c); | ||
1331 | GNUNET_STATISTICS_update (stats, | ||
1332 | "# clients", | ||
1333 | -1, | ||
1334 | GNUNET_NO); | ||
1335 | GNUNET_free (c); | ||
1336 | if ( (NULL == clients_head) && | ||
1337 | (GNUNET_YES == shutting_down) ) | ||
1338 | shutdown_rest (); | ||
1339 | } | ||
1340 | |||
1341 | |||
1342 | /** | ||
1343 | * Setup CADET internals. | ||
1344 | * | ||
1345 | * @param cls closure | ||
1346 | * @param server the initialized server | ||
1347 | * @param c configuration to use | ||
1348 | */ | ||
1349 | static void | ||
1350 | run (void *cls, | ||
1351 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
1352 | struct GNUNET_SERVICE_Handle *service) | ||
1353 | { | ||
1354 | cfg = c; | ||
1355 | if (GNUNET_OK != | ||
1356 | GNUNET_CONFIGURATION_get_value_number (c, | ||
1357 | "CADET", | ||
1358 | "RATCHET_MESSAGES", | ||
1359 | &ratchet_messages)) | ||
1360 | { | ||
1361 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, | ||
1362 | "CADET", | ||
1363 | "RATCHET_MESSAGES", | ||
1364 | "needs to be a number"); | ||
1365 | ratchet_messages = 64; | ||
1366 | } | ||
1367 | if (GNUNET_OK != | ||
1368 | GNUNET_CONFIGURATION_get_value_time (c, | ||
1369 | "CADET", | ||
1370 | "RATCHET_TIME", | ||
1371 | &ratchet_time)) | ||
1372 | { | ||
1373 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, | ||
1374 | "CADET", | ||
1375 | "RATCHET_TIME", | ||
1376 | "need delay value"); | ||
1377 | ratchet_time = GNUNET_TIME_UNIT_HOURS; | ||
1378 | } | ||
1379 | if (GNUNET_OK != | ||
1380 | GNUNET_CONFIGURATION_get_value_time (c, | ||
1381 | "CADET", | ||
1382 | "REFRESH_CONNECTION_TIME", | ||
1383 | &keepalive_period)) | ||
1384 | { | ||
1385 | GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, | ||
1386 | "CADET", | ||
1387 | "REFRESH_CONNECTION_TIME", | ||
1388 | "need delay value"); | ||
1389 | keepalive_period = GNUNET_TIME_UNIT_MINUTES; | ||
1390 | } | ||
1391 | if (GNUNET_OK != | ||
1392 | GNUNET_CONFIGURATION_get_value_number (c, | ||
1393 | "CADET", | ||
1394 | "DROP_PERCENT", | ||
1395 | &drop_percent)) | ||
1396 | { | ||
1397 | drop_percent = 0; | ||
1398 | } | ||
1399 | else | ||
1400 | { | ||
1401 | LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n"); | ||
1402 | LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n"); | ||
1403 | LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n"); | ||
1404 | LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n"); | ||
1405 | LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n"); | ||
1406 | } | ||
1407 | my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c); | ||
1408 | if (NULL == my_private_key) | ||
1409 | { | ||
1410 | GNUNET_break (0); | ||
1411 | GNUNET_SCHEDULER_shutdown (); | ||
1412 | return; | ||
1413 | } | ||
1414 | GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, | ||
1415 | &my_full_id.public_key); | ||
1416 | stats = GNUNET_STATISTICS_create ("cadet", | ||
1417 | c); | ||
1418 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, | ||
1419 | NULL); | ||
1420 | ats_ch = GNUNET_ATS_connectivity_init (c); | ||
1421 | /* FIXME: optimize code to allow GNUNET_YES here! */ | ||
1422 | open_ports = GNUNET_CONTAINER_multihashmap_create (16, | ||
1423 | GNUNET_NO); | ||
1424 | loose_channels = GNUNET_CONTAINER_multihashmap_create (16, | ||
1425 | GNUNET_NO); | ||
1426 | peers = GNUNET_CONTAINER_multipeermap_create (16, | ||
1427 | GNUNET_YES); | ||
1428 | connections = GNUNET_CONTAINER_multishortmap_create (256, | ||
1429 | GNUNET_YES); | ||
1430 | GCH_init (c); | ||
1431 | GCD_init (c); | ||
1432 | GCO_init (c); | ||
1433 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
1434 | "CADET started for peer %s\n", | ||
1435 | GNUNET_i2s (&my_full_id)); | ||
1436 | |||
1437 | } | ||
1438 | |||
1439 | |||
1440 | /** | ||
1441 | * Define "main" method using service macro. | ||
1442 | */ | ||
1443 | GNUNET_SERVICE_MAIN | ||
1444 | ("cadet", | ||
1445 | GNUNET_SERVICE_OPTION_NONE, | ||
1446 | &run, | ||
1447 | &client_connect_cb, | ||
1448 | &client_disconnect_cb, | ||
1449 | NULL, | ||
1450 | GNUNET_MQ_hd_fixed_size (port_open, | ||
1451 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN, | ||
1452 | struct GNUNET_CADET_PortMessage, | ||
1453 | NULL), | ||
1454 | GNUNET_MQ_hd_fixed_size (port_close, | ||
1455 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE, | ||
1456 | struct GNUNET_CADET_PortMessage, | ||
1457 | NULL), | ||
1458 | GNUNET_MQ_hd_fixed_size (channel_create, | ||
1459 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE, | ||
1460 | struct GNUNET_CADET_LocalChannelCreateMessage, | ||
1461 | NULL), | ||
1462 | GNUNET_MQ_hd_fixed_size (channel_destroy, | ||
1463 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY, | ||
1464 | struct GNUNET_CADET_LocalChannelDestroyMessage, | ||
1465 | NULL), | ||
1466 | GNUNET_MQ_hd_var_size (local_data, | ||
1467 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, | ||
1468 | struct GNUNET_CADET_LocalData, | ||
1469 | NULL), | ||
1470 | GNUNET_MQ_hd_fixed_size (local_ack, | ||
1471 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK, | ||
1472 | struct GNUNET_CADET_LocalAck, | ||
1473 | NULL), | ||
1474 | GNUNET_MQ_hd_fixed_size (get_peers, | ||
1475 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS, | ||
1476 | struct GNUNET_MessageHeader, | ||
1477 | NULL), | ||
1478 | GNUNET_MQ_hd_fixed_size (show_peer, | ||
1479 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER, | ||
1480 | struct GNUNET_CADET_LocalInfo, | ||
1481 | NULL), | ||
1482 | GNUNET_MQ_hd_fixed_size (info_tunnels, | ||
1483 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS, | ||
1484 | struct GNUNET_MessageHeader, | ||
1485 | NULL), | ||
1486 | GNUNET_MQ_hd_fixed_size (info_tunnel, | ||
1487 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL, | ||
1488 | struct GNUNET_CADET_LocalInfo, | ||
1489 | NULL), | ||
1490 | GNUNET_MQ_hd_fixed_size (info_dump, | ||
1491 | GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP, | ||
1492 | struct GNUNET_MessageHeader, | ||
1493 | NULL), | ||
1494 | GNUNET_MQ_handler_end ()); | ||
1495 | |||
1496 | /* end of gnunet-service-cadet-new.c */ | ||