nicer order of LOG statements
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet-new_core.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 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_core.c
23  * @brief cadet service; interaction with CORE service
24  * @author Bartlomiej Polot
25  * @author Christian Grothoff
26  *
27  * All functions in this file should use the prefix GCO (Gnunet Cadet cOre (bottom))
28  *
29  * TODO:
30  * - Optimization: given BROKEN messages, destroy paths (?)
31  */
32 #include "platform.h"
33 #include "gnunet-service-cadet-new_core.h"
34 #include "gnunet-service-cadet-new_paths.h"
35 #include "gnunet-service-cadet-new_peer.h"
36 #include "gnunet-service-cadet-new_connection.h"
37 #include "gnunet-service-cadet-new_tunnels.h"
38 #include "gnunet_core_service.h"
39 #include "cadet_protocol.h"
40
41
42 #define LOG(level, ...) GNUNET_log_from(level,"cadet-cor",__VA_ARGS__)
43
44
45 /**
46  * Number of messages we are willing to buffer per route.
47  */
48 #define ROUTE_BUFFER_SIZE 8
49
50
51 /**
52  * Information we keep per direction for a route.
53  */
54 struct RouteDirection
55 {
56   /**
57    * Target peer.
58    */
59   struct CadetPeer *hop;
60
61   /**
62    * Route this direction is part of.
63    */
64   struct CadetRoute *my_route;
65
66   /**
67    * Message queue manager for @e hop.
68    */
69   struct GCP_MessageQueueManager *mqm;
70
71   /**
72    * Cyclic message buffer to @e hop.
73    */
74   struct GNUNET_MQ_Envelope *out_buffer[ROUTE_BUFFER_SIZE];
75
76   /**
77    * Next write offset to use to append messages to @e out_buffer.
78    */
79   unsigned int out_wpos;
80
81   /**
82    * Next read offset to use to retrieve messages from @e out_buffer.
83    */
84   unsigned int out_rpos;
85
86   /**
87    * Is @e mqm currently ready for transmission?
88    */
89   int is_ready;
90
91 };
92
93
94 /**
95  * Description of a segment of a `struct CadetConnection` at the
96  * intermediate peers.  Routes are basically entries in a peer's
97  * routing table for forwarding traffic.  At both endpoints, the
98  * routes are terminated by a `struct CadetConnection`, which knows
99  * the complete `struct CadetPath` that is formed by the individual
100  * routes.
101  */
102 struct CadetRoute
103 {
104
105   /**
106    * Information about the next hop on this route.
107    */
108   struct RouteDirection next;
109
110   /**
111    * Information about the previous hop on this route.
112    */
113   struct RouteDirection prev;
114
115   /**
116    * Unique identifier for the connection that uses this route.
117    */
118   struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
119
120   /**
121    * When was this route last in use?
122    */
123   struct GNUNET_TIME_Absolute last_use;
124
125 };
126
127
128 /**
129  * Handle to the CORE service.
130  */
131 static struct GNUNET_CORE_Handle *core;
132
133 /**
134  * Routes on which this peer is an intermediate.
135  */
136 static struct GNUNET_CONTAINER_MultiShortmap *routes;
137
138
139 /**
140  * Get the route corresponding to a hash.
141  *
142  * @param cid hash generated from the connection identifier
143  */
144 static struct CadetRoute *
145 get_route (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
146 {
147   return GNUNET_CONTAINER_multishortmap_get (routes,
148                                              &cid->connection_of_tunnel);
149 }
150
151
152 /**
153  * We message @a msg from @a prev.  Find its route by @a cid and
154  * forward to the next hop.  Drop and signal broken route if we do not
155  * have a route.
156  *
157  * @param prev previous hop (sender)
158  * @param cid connection identifier, tells us which route to use
159  * @param msg the message to forward
160  */
161 static void
162 route_message (struct CadetPeer *prev,
163                const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
164                const struct GNUNET_MessageHeader *msg)
165 {
166   struct CadetRoute *route;
167   struct RouteDirection *dir;
168   struct GNUNET_MQ_Envelope *env;
169
170   route = get_route (cid);
171   if (NULL == route)
172   {
173     struct GNUNET_MQ_Envelope *env;
174     struct GNUNET_CADET_ConnectionBrokenMessage *bm;
175
176     LOG (GNUNET_ERROR_TYPE_DEBUG,
177          "Failed to route message of type %u from %s on connection %s: no route\n",
178          ntohs (msg->type),
179          GCP_2s (prev),
180          GNUNET_sh2s (&cid->connection_of_tunnel));
181     env = GNUNET_MQ_msg (bm,
182                          GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
183     bm->cid = *cid;
184     bm->peer1 = my_full_id;
185     GCP_send_ooo (prev,
186                   env);
187     return;
188   }
189   dir = (prev == route->prev.hop) ? &route->next : &route->prev;
190   if (GNUNET_YES == dir->is_ready)
191   {
192     LOG (GNUNET_ERROR_TYPE_DEBUG,
193          "Routing message of type %u from %s to %s on connection %s\n",
194          ntohs (msg->type),
195          GCP_2s (prev),
196          GNUNET_i2s (GCP_get_id (dir->hop)),
197          GNUNET_sh2s (&cid->connection_of_tunnel));
198     dir->is_ready = GNUNET_NO;
199     GCP_send (dir->mqm,
200               GNUNET_MQ_msg_copy (msg));
201     return;
202   }
203   env = dir->out_buffer[dir->out_wpos];
204   if (NULL != env)
205   {
206     /* Queue full, drop earliest message in queue */
207     LOG (GNUNET_ERROR_TYPE_DEBUG,
208          "Queue full due to new message of type %u from %s to %s on connection %s, dropping old message\n",
209          ntohs (msg->type),
210          GCP_2s (prev),
211          GNUNET_i2s (GCP_get_id (dir->hop)),
212          GNUNET_sh2s (&cid->connection_of_tunnel));
213     GNUNET_assert (dir->out_rpos == dir->out_wpos);
214     GNUNET_MQ_discard (env);
215     dir->out_rpos++;
216     if (ROUTE_BUFFER_SIZE == dir->out_rpos)
217       dir->out_rpos = 0;
218   }
219   LOG (GNUNET_ERROR_TYPE_DEBUG,
220        "Queueing new message of type %u from %s to %s on connection %s\n",
221        ntohs (msg->type),
222        GCP_2s (prev),
223        GNUNET_i2s (GCP_get_id (dir->hop)),
224        GNUNET_sh2s (&cid->connection_of_tunnel));
225   env = GNUNET_MQ_msg_copy (msg);
226   dir->out_buffer[dir->out_wpos] = env;
227   dir->out_wpos++;
228   if (ROUTE_BUFFER_SIZE == dir->out_wpos)
229     dir->out_wpos = 0;
230 }
231
232
233 /**
234  * Check if the create_connection message has the appropriate size.
235  *
236  * @param cls Closure (unused).
237  * @param msg Message to check.
238  *
239  * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
240  */
241 static int
242 check_connection_create (void *cls,
243                          const struct GNUNET_CADET_ConnectionCreateMessage *msg)
244 {
245   uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
246
247   if (0 != (size % sizeof (struct GNUNET_PeerIdentity)))
248   {
249     GNUNET_break_op (0);
250     return GNUNET_NO;
251   }
252   return GNUNET_YES;
253 }
254
255
256 /**
257  * Free internal data of a route direction.
258  *
259  * @param dir direction to destroy (do NOT free memory of 'dir' itself)
260  */
261 static void
262 destroy_direction (struct RouteDirection *dir)
263 {
264   for (unsigned int i=0;i<ROUTE_BUFFER_SIZE;i++)
265     if (NULL != dir->out_buffer[i])
266     {
267       GNUNET_MQ_discard (dir->out_buffer[i]);
268       dir->out_buffer[i] = NULL;
269     }
270   if (NULL != dir->mqm)
271   {
272     GCP_request_mq_cancel (dir->mqm,
273                            NULL);
274     dir->mqm = NULL;
275   }
276 }
277
278
279 /**
280  * Destroy our state for @a route.
281  *
282  * @param route route to destroy
283  */
284 static void
285 destroy_route (struct CadetRoute *route)
286 {
287   LOG (GNUNET_ERROR_TYPE_DEBUG,
288        "Destroying route from %s to %s of connection %s\n",
289        GNUNET_i2s  (GCP_get_id (route->prev.hop)),
290        GNUNET_i2s2 (GCP_get_id (route->next.hop)),
291        GNUNET_sh2s (&route->cid.connection_of_tunnel));
292   destroy_direction (&route->prev);
293   destroy_direction (&route->next);
294   GNUNET_free (route);
295 }
296
297
298 /**
299  * Send message that a route is broken between @a peer1 and @a peer2.
300  *
301  * @param target where to send the message
302  * @param cid connection identifier to use
303  * @param peer1 one of the peers where a link is broken
304  * @param peer2 another one of the peers where a link is broken
305  */
306 static void
307 send_broken (struct RouteDirection *target,
308              const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
309              const struct GNUNET_PeerIdentity *peer1,
310              const struct GNUNET_PeerIdentity *peer2)
311 {
312   struct GNUNET_MQ_Envelope *env;
313   struct GNUNET_CADET_ConnectionBrokenMessage *bm;
314
315   LOG (GNUNET_ERROR_TYPE_DEBUG,
316        "Notifying %s about BROKEN route at %s-%s of connection %s\n",
317        GCP_2s (target->hop),
318        GNUNET_i2s (peer1),
319        GNUNET_i2s2 (peer2),
320        GNUNET_sh2s (&cid->connection_of_tunnel));
321
322   env = GNUNET_MQ_msg (bm,
323                        GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
324   bm->cid = *cid;
325   if (NULL != peer1)
326     bm->peer1 = *peer1;
327   if (NULL != peer2)
328     bm->peer2 = *peer2;
329   GCP_request_mq_cancel (target->mqm,
330                          env);
331   target->mqm = NULL;
332 }
333
334
335 /**
336  * Function called when the message queue to the previous hop
337  * becomes available/unavailable.  We expect this function to
338  * be called immediately when we register, and then again
339  * later if the connection ever goes down.
340  *
341  * @param cls the `struct RouteDirection`
342  * @param available #GNUNET_YES if sending is now possible,
343  *                  #GNUNET_NO if sending is no longer possible
344  *                  #GNUNET_SYSERR if sending is no longer possible
345  *                                 and the last envelope was discarded
346  */
347 static void
348 dir_ready_cb (void *cls,
349               int ready)
350 {
351   struct RouteDirection *dir = cls;
352   struct CadetRoute *route = dir->my_route;
353   struct RouteDirection *odir;
354
355   if (GNUNET_YES == ready)
356   {
357     struct GNUNET_MQ_Envelope *env;
358
359     dir->is_ready = GNUNET_YES;
360     if (NULL != (env = dir->out_buffer[dir->out_rpos]))
361     {
362       dir->out_buffer[dir->out_rpos] = NULL;
363       dir->out_rpos++;
364       if (ROUTE_BUFFER_SIZE == dir->out_rpos)
365         dir->out_rpos = 0;
366       dir->is_ready = GNUNET_NO;
367       GCP_send (dir->mqm,
368                 env);
369     }
370     return;
371   }
372   odir = (dir == &route->next) ? &route->prev : &route->next;
373   send_broken (&route->next,
374                &route->cid,
375                GCP_get_id (odir->hop),
376                &my_full_id);
377   destroy_route (route);
378 }
379
380
381 /**
382  * Initialize one of the directions of a route.
383  *
384  * @param route route the direction belongs to
385  * @param dir direction to initialize
386  * @param hop next hop on in the @a dir
387  */
388 static void
389 dir_init (struct RouteDirection *dir,
390           struct CadetRoute *route,
391           struct CadetPeer *hop)
392 {
393   dir->hop = hop;
394   dir->my_route = route;
395   dir->mqm = GCP_request_mq (hop,
396                              &dir_ready_cb,
397                              dir);
398   GNUNET_assert (GNUNET_YES == dir->is_ready);
399 }
400
401
402 /**
403  * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
404  *
405  * @param cls Closure (CadetPeer for neighbor that sent the message).
406  * @param msg Message itself.
407  */
408 static void
409 handle_connection_create (void *cls,
410                           const struct GNUNET_CADET_ConnectionCreateMessage *msg)
411 {
412   struct CadetPeer *sender = cls;
413   struct CadetPeer *next;
414   const struct GNUNET_PeerIdentity *pids = (const struct GNUNET_PeerIdentity *) &msg[1];
415   struct CadetRoute *route;
416   uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
417   unsigned int path_length;
418   unsigned int off;
419
420   path_length = size / sizeof (struct GNUNET_PeerIdentity);
421   /* Initiator is at offset 0. */
422   for (off=1;off<path_length;off++)
423     if (0 == memcmp (&my_full_id,
424                      &pids[off],
425                      sizeof (struct GNUNET_PeerIdentity)))
426       break;
427   if (off == path_length)
428   {
429     /* We are not on the path, bogus request */
430     GNUNET_break_op (0);
431     return;
432   }
433   /* Check previous hop */
434   if (sender != GCP_get (&pids[off - 1],
435                          GNUNET_NO))
436   {
437     /* sender is not on the path, not allowed */
438     GNUNET_break_op (0);
439     return;
440   }
441   if (NULL !=
442       get_route (&msg->cid))
443   {
444     /* Duplicate CREATE, pass it on, previous one might have been lost! */
445     LOG (GNUNET_ERROR_TYPE_DEBUG,
446          "Passing on duplicate CREATE message on connection %s\n",
447          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
448     route_message (sender,
449                    &msg->cid,
450                    &msg->header);
451     return;
452   }
453   if (off == path_length - 1)
454   {
455     /* We are the destination, create connection */
456     struct CadetConnection *cc;
457     struct CadetPeerPath *path;
458     struct CadetPeer *origin;
459
460     cc = GNUNET_CONTAINER_multishortmap_get (connections,
461                                              &msg->cid.connection_of_tunnel);
462     if (NULL != cc)
463     {
464       LOG (GNUNET_ERROR_TYPE_DEBUG,
465            "Received duplicate CREATE message on connection %s\n",
466            GNUNET_sh2s (&msg->cid.connection_of_tunnel));
467       GCC_handle_duplicate_create (cc);
468       return;
469     }
470
471     origin = GCP_get (&pids[0],
472                       GNUNET_YES);
473     LOG (GNUNET_ERROR_TYPE_DEBUG,
474          "Received CREATE message from %s for connection %s, building inverse path\n",
475          GCP_2s (origin),
476          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
477     path = GCPP_get_path_from_route (path_length - 1,
478                                      pids);
479     GCT_add_inbound_connection (GCT_create_tunnel (origin),
480                                 &msg->cid,
481                                 path);
482     return;
483   }
484   /* We are merely a hop on the way, check if we can support the route */
485   next = GCP_get (&pids[off + 1],
486                   GNUNET_NO);
487   if ( (NULL == next) ||
488        (GNUNET_NO == GCP_has_core_connection (next)) )
489   {
490     /* unworkable, send back BROKEN notification */
491     struct GNUNET_MQ_Envelope *env;
492     struct GNUNET_CADET_ConnectionBrokenMessage *bm;
493
494     LOG (GNUNET_ERROR_TYPE_DEBUG,
495          "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
496          GCP_2s (sender),
497          GNUNET_sh2s (&msg->cid.connection_of_tunnel),
498          GNUNET_i2s (&pids[off + 1]),
499          off + 1);
500     env = GNUNET_MQ_msg (bm,
501                          GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
502     bm->cid = msg->cid;
503     bm->peer1 = pids[off + 1];
504     bm->peer2 = my_full_id;
505     GCP_send_ooo (sender,
506                   env);
507     return;
508   }
509
510   /* Workable route, create routing entry */
511   LOG (GNUNET_ERROR_TYPE_DEBUG,
512        "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
513        GCP_2s (sender),
514        GNUNET_sh2s (&msg->cid.connection_of_tunnel),
515        GNUNET_i2s (&pids[off + 1]),
516        off + 1);
517   route = GNUNET_new (struct CadetRoute);
518   route->cid = msg->cid;
519   dir_init (&route->prev,
520             route,
521             sender);
522   dir_init (&route->next,
523             route,
524             next);
525   GNUNET_assert (GNUNET_OK ==
526                  GNUNET_CONTAINER_multishortmap_put (routes,
527                                                      &route->cid.connection_of_tunnel,
528                                                      route,
529                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
530 }
531
532
533 /**
534  * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
535  *
536  * @param cls Closure (CadetPeer for neighbor that sent the message).
537  * @param msg Message itself.
538  */
539 static void
540 handle_connection_create_ack (void *cls,
541                               const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
542 {
543   struct CadetPeer *peer = cls;
544   struct CadetConnection *cc;
545
546   /* First, check if ACK belongs to a connection that ends here. */
547   cc = GNUNET_CONTAINER_multishortmap_get (connections,
548                                            &msg->cid.connection_of_tunnel);
549   if (NULL != cc)
550   {
551     /* verify ACK came from the right direction */
552     struct CadetPeerPath *path = GCC_get_path (cc);
553
554     if (peer !=
555         GCPP_get_peer_at_offset (path,
556                                  0))
557     {
558       /* received ACK from unexpected direction, ignore! */
559       GNUNET_break_op (0);
560       return;
561     }
562     LOG (GNUNET_ERROR_TYPE_DEBUG,
563          "Received CONNECTION_CREATE_ACK for connection %s.\n",
564          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
565     GCC_handle_connection_create_ack (cc);
566     return;
567   }
568
569   /* We're just an intermediary peer, route the message along its path */
570   route_message (peer,
571                  &msg->cid,
572                  &msg->header);
573 }
574
575
576 /**
577  * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
578  *
579  * @param cls Closure (CadetPeer for neighbor that sent the message).
580  * @param msg Message itself.
581  * @deprecated duplicate logic with #handle_destroy(); dedup!
582  */
583 static void
584 handle_connection_broken (void *cls,
585                           const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
586 {
587   struct CadetPeer *peer = cls;
588   struct CadetConnection *cc;
589   struct CadetRoute *route;
590
591   /* First, check if message belongs to a connection that ends here. */
592   cc = GNUNET_CONTAINER_multishortmap_get (connections,
593                                            &msg->cid.connection_of_tunnel);
594   if (NULL != cc)
595   {
596     /* verify message came from the right direction */
597     struct CadetPeerPath *path = GCC_get_path (cc);
598
599     if (peer !=
600         GCPP_get_peer_at_offset (path,
601                                  0))
602     {
603       /* received message from unexpected direction, ignore! */
604       GNUNET_break_op (0);
605       return;
606     }
607     LOG (GNUNET_ERROR_TYPE_DEBUG,
608          "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
609          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
610     GCC_destroy (cc);
611
612     /* FIXME: also destroy the path up to the specified link! */
613     return;
614   }
615
616   /* We're just an intermediary peer, route the message along its path */
617   route = get_route (&msg->cid);
618   route_message (peer,
619                  &msg->cid,
620                  &msg->header);
621   destroy_route (route);
622   /* FIXME: also destroy paths we MAY have up to the specified link! */
623 }
624
625
626 /**
627  * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
628  *
629  * @param cls Closure (CadetPeer for neighbor that sent the message).
630  * @param msg Message itself.
631  */
632 static void
633 handle_connection_destroy (void *cls,
634                            const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
635 {
636   struct CadetPeer *peer = cls;
637   struct CadetConnection *cc;
638   struct CadetRoute *route;
639
640   /* First, check if message belongs to a connection that ends here. */
641   cc = GNUNET_CONTAINER_multishortmap_get (connections,
642                                            &msg->cid.connection_of_tunnel);
643   if (NULL != cc)
644   {
645     /* verify message came from the right direction */
646     struct CadetPeerPath *path = GCC_get_path (cc);
647
648     if (peer !=
649         GCPP_get_peer_at_offset (path,
650                                  0))
651     {
652       /* received message from unexpected direction, ignore! */
653       GNUNET_break_op (0);
654       return;
655     }
656     LOG (GNUNET_ERROR_TYPE_DEBUG,
657          "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
658          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
659
660     GCC_destroy (cc);
661     return;
662   }
663
664   /* We're just an intermediary peer, route the message along its path */
665   LOG (GNUNET_ERROR_TYPE_DEBUG,
666        "Received CONNECTION_DESTROY for connection %s. Destroying route.\n",
667        GNUNET_sh2s (&msg->cid.connection_of_tunnel));
668   route = get_route (&msg->cid);
669   route_message (peer,
670                  &msg->cid,
671                  &msg->header);
672   destroy_route (route);
673 }
674
675
676 /**
677  * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
678  *
679  * @param cls Closure (CadetPeer for neighbor that sent the message).
680  * @param msg Message itself.
681  */
682 static void
683 handle_tunnel_kx (void *cls,
684                   const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
685 {
686   struct CadetPeer *peer = cls;
687   struct CadetConnection *cc;
688
689   /* First, check if message belongs to a connection that ends here. */
690   cc = GNUNET_CONTAINER_multishortmap_get (connections,
691                                            &msg->cid.connection_of_tunnel);
692   if (NULL != cc)
693   {
694     /* verify message came from the right direction */
695     struct CadetPeerPath *path = GCC_get_path (cc);
696
697     if (peer !=
698         GCPP_get_peer_at_offset (path,
699                                  0))
700     {
701       /* received message from unexpected direction, ignore! */
702       GNUNET_break_op (0);
703       return;
704     }
705     GCC_handle_kx (cc,
706                    msg);
707     return;
708   }
709
710   /* We're just an intermediary peer, route the message along its path */
711   route_message (peer,
712                  &msg->cid,
713                  &msg->header);
714 }
715
716
717 /**
718  * Check if the encrypted message has the appropriate size.
719  *
720  * @param cls Closure (unused).
721  * @param msg Message to check.
722  *
723  * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
724  */
725 static int
726 check_tunnel_encrypted (void *cls,
727                         const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
728 {
729   return GNUNET_YES;
730 }
731
732
733 /**
734  * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
735  *
736  * @param cls Closure (CadetPeer for neighbor that sent the message).
737  * @param msg Message itself.
738  */
739 static void
740 handle_tunnel_encrypted (void *cls,
741                          const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
742 {
743   struct CadetPeer *peer = cls;
744   struct CadetConnection *cc;
745
746   /* First, check if message belongs to a connection that ends here. */
747   cc = GNUNET_CONTAINER_multishortmap_get (connections,
748                                            &msg->cid.connection_of_tunnel);
749   if (NULL != cc)
750   {
751     /* verify message came from the right direction */
752     struct CadetPeerPath *path = GCC_get_path (cc);
753
754     if (peer !=
755         GCPP_get_peer_at_offset (path,
756                                  0))
757     {
758       /* received message from unexpected direction, ignore! */
759       GNUNET_break_op (0);
760       return;
761     }
762     GCC_handle_encrypted (cc,
763                           msg);
764     return;
765   }
766   /* We're just an intermediary peer, route the message along its path */
767   route_message (peer,
768                  &msg->cid,
769                  &msg->header);
770 }
771
772
773 /**
774  * Function called after #GNUNET_CORE_connect has succeeded (or failed
775  * for good).  Note that the private key of the peer is intentionally
776  * not exposed here; if you need it, your process should try to read
777  * the private key file directly (which should work if you are
778  * authorized...).  Implementations of this function must not call
779  * #GNUNET_CORE_disconnect (other than by scheduling a new task to
780  * do this later).
781  *
782  * @param cls closure
783  * @param my_identity ID of this peer, NULL if we failed
784  */
785 static void
786 core_init_cb (void *cls,
787               const struct GNUNET_PeerIdentity *my_identity)
788 {
789   if (NULL == my_identity)
790   {
791     GNUNET_break (0);
792     return;
793   }
794   GNUNET_break (0 ==
795                 memcmp (my_identity,
796                         &my_full_id,
797                         sizeof (struct GNUNET_PeerIdentity)));
798 }
799
800
801 /**
802  * Method called whenever a given peer connects.
803  *
804  * @param cls closure
805  * @param peer peer identity this notification is about
806  */
807 static void *
808 core_connect_cb (void *cls,
809                  const struct GNUNET_PeerIdentity *peer,
810                  struct GNUNET_MQ_Handle *mq)
811 {
812   struct CadetPeer *cp;
813
814   LOG (GNUNET_ERROR_TYPE_DEBUG,
815        "CORE connection to peer %s was established.\n",
816        GNUNET_i2s (peer));
817   cp = GCP_get (peer,
818                 GNUNET_YES);
819   GCP_set_mq (cp,
820               mq);
821   return cp;
822 }
823
824
825 /**
826  * Method called whenever a peer disconnects.
827  *
828  * @param cls closure
829  * @param peer peer identity this notification is about
830  */
831 static void
832 core_disconnect_cb (void *cls,
833                     const struct GNUNET_PeerIdentity *peer,
834                     void *peer_cls)
835 {
836   struct CadetPeer *cp = peer_cls;
837
838   LOG (GNUNET_ERROR_TYPE_DEBUG,
839        "CORE connection to peer %s went down.\n",
840        GNUNET_i2s (peer));
841   GCP_set_mq (cp,
842               NULL);
843 }
844
845
846 /**
847  * Initialize the CORE subsystem.
848  *
849  * @param c Configuration.
850  */
851 void
852 GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
853 {
854   struct GNUNET_MQ_MessageHandler handlers[] = {
855     GNUNET_MQ_hd_var_size (connection_create,
856                            GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
857                            struct GNUNET_CADET_ConnectionCreateMessage,
858                            NULL),
859     GNUNET_MQ_hd_fixed_size (connection_create_ack,
860                              GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
861                              struct GNUNET_CADET_ConnectionCreateAckMessage,
862                              NULL),
863     GNUNET_MQ_hd_fixed_size (connection_broken,
864                              GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
865                              struct GNUNET_CADET_ConnectionBrokenMessage,
866                              NULL),
867     GNUNET_MQ_hd_fixed_size (connection_destroy,
868                              GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
869                              struct GNUNET_CADET_ConnectionDestroyMessage,
870                              NULL),
871     GNUNET_MQ_hd_fixed_size (tunnel_kx,
872                              GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
873                              struct GNUNET_CADET_TunnelKeyExchangeMessage,
874                              NULL),
875     GNUNET_MQ_hd_var_size (tunnel_encrypted,
876                            GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
877                            struct GNUNET_CADET_TunnelEncryptedMessage,
878                            NULL),
879     GNUNET_MQ_handler_end ()
880   };
881
882   routes = GNUNET_CONTAINER_multishortmap_create (1024,
883                                                   GNUNET_NO);
884   core = GNUNET_CORE_connect (c,
885                               NULL,
886                               &core_init_cb,
887                               &core_connect_cb,
888                               &core_disconnect_cb,
889                               handlers);
890 }
891
892
893 /**
894  * Shut down the CORE subsystem.
895  */
896 void
897 GCO_shutdown ()
898 {
899   if (NULL != core)
900   {
901     GNUNET_CORE_disconnect (core);
902     core = NULL;
903   }
904   GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
905   GNUNET_CONTAINER_multishortmap_destroy (routes);
906 }
907
908 /* end of gnunet-cadet-service_core.c */