6b5a02dc15da46ea157bf22667888c40beeb9b17
[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     path = GCPP_get_path_from_route (path_length - 1,
472                                      pids);
473     origin = GCP_get (&pids[0],
474                       GNUNET_YES);
475     LOG (GNUNET_ERROR_TYPE_DEBUG,
476          "Received CREATE message from %s via path %s for connection %s\n",
477          GCP_2s (origin),
478          GCPP_2s (path),
479          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
480     GCT_add_inbound_connection (GCT_create_tunnel (origin),
481                                 &msg->cid,
482                                 path);
483     return;
484   }
485   /* We are merely a hop on the way, check if we can support the route */
486   next = GCP_get (&pids[off + 1],
487                   GNUNET_NO);
488   if ( (NULL == next) ||
489        (GNUNET_NO == GCP_has_core_connection (next)) )
490   {
491     /* unworkable, send back BROKEN notification */
492     struct GNUNET_MQ_Envelope *env;
493     struct GNUNET_CADET_ConnectionBrokenMessage *bm;
494
495     LOG (GNUNET_ERROR_TYPE_DEBUG,
496          "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
497          GCP_2s (sender),
498          GNUNET_sh2s (&msg->cid.connection_of_tunnel),
499          GNUNET_i2s (&pids[off + 1]),
500          off + 1);
501     env = GNUNET_MQ_msg (bm,
502                          GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
503     bm->cid = msg->cid;
504     bm->peer1 = pids[off + 1];
505     bm->peer2 = my_full_id;
506     GCP_send_ooo (sender,
507                   env);
508     return;
509   }
510
511   /* Workable route, create routing entry */
512   LOG (GNUNET_ERROR_TYPE_DEBUG,
513        "Received CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
514        GCP_2s (sender),
515        GNUNET_sh2s (&msg->cid.connection_of_tunnel),
516        GNUNET_i2s (&pids[off + 1]),
517        off + 1);
518   route = GNUNET_new (struct CadetRoute);
519   route->cid = msg->cid;
520   dir_init (&route->prev,
521             route,
522             sender);
523   dir_init (&route->next,
524             route,
525             next);
526   GNUNET_assert (GNUNET_OK ==
527                  GNUNET_CONTAINER_multishortmap_put (routes,
528                                                      &route->cid.connection_of_tunnel,
529                                                      route,
530                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
531 }
532
533
534 /**
535  * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
536  *
537  * @param cls Closure (CadetPeer for neighbor that sent the message).
538  * @param msg Message itself.
539  */
540 static void
541 handle_connection_create_ack (void *cls,
542                               const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
543 {
544   struct CadetPeer *peer = cls;
545   struct CadetConnection *cc;
546
547   /* First, check if ACK belongs to a connection that ends here. */
548   cc = GNUNET_CONTAINER_multishortmap_get (connections,
549                                            &msg->cid.connection_of_tunnel);
550   if (NULL != cc)
551   {
552     /* verify ACK came from the right direction */
553     struct CadetPeerPath *path = GCC_get_path (cc);
554
555     if (peer !=
556         GCPP_get_peer_at_offset (path,
557                                  0))
558     {
559       /* received ACK from unexpected direction, ignore! */
560       GNUNET_break_op (0);
561       return;
562     }
563     LOG (GNUNET_ERROR_TYPE_DEBUG,
564          "Received CONNECTION_CREATE_ACK for connection %s.\n",
565          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
566     GCC_handle_connection_create_ack (cc);
567     return;
568   }
569
570   /* We're just an intermediary peer, route the message along its path */
571   route_message (peer,
572                  &msg->cid,
573                  &msg->header);
574 }
575
576
577 /**
578  * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
579  *
580  * @param cls Closure (CadetPeer for neighbor that sent the message).
581  * @param msg Message itself.
582  * @deprecated duplicate logic with #handle_destroy(); dedup!
583  */
584 static void
585 handle_connection_broken (void *cls,
586                           const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
587 {
588   struct CadetPeer *peer = cls;
589   struct CadetConnection *cc;
590   struct CadetRoute *route;
591
592   /* First, check if message belongs to a connection that ends here. */
593   cc = GNUNET_CONTAINER_multishortmap_get (connections,
594                                            &msg->cid.connection_of_tunnel);
595   if (NULL != cc)
596   {
597     /* verify message came from the right direction */
598     struct CadetPeerPath *path = GCC_get_path (cc);
599
600     if (peer !=
601         GCPP_get_peer_at_offset (path,
602                                  0))
603     {
604       /* received message from unexpected direction, ignore! */
605       GNUNET_break_op (0);
606       return;
607     }
608     LOG (GNUNET_ERROR_TYPE_DEBUG,
609          "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
610          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
611     GCC_destroy (cc);
612
613     /* FIXME: also destroy the path up to the specified link! */
614     return;
615   }
616
617   /* We're just an intermediary peer, route the message along its path */
618   route = get_route (&msg->cid);
619   route_message (peer,
620                  &msg->cid,
621                  &msg->header);
622   destroy_route (route);
623   /* FIXME: also destroy paths we MAY have up to the specified link! */
624 }
625
626
627 /**
628  * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
629  *
630  * @param cls Closure (CadetPeer for neighbor that sent the message).
631  * @param msg Message itself.
632  */
633 static void
634 handle_connection_destroy (void *cls,
635                            const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
636 {
637   struct CadetPeer *peer = cls;
638   struct CadetConnection *cc;
639   struct CadetRoute *route;
640
641   /* First, check if message belongs to a connection that ends here. */
642   cc = GNUNET_CONTAINER_multishortmap_get (connections,
643                                            &msg->cid.connection_of_tunnel);
644   if (NULL != cc)
645   {
646     /* verify message came from the right direction */
647     struct CadetPeerPath *path = GCC_get_path (cc);
648
649     if (peer !=
650         GCPP_get_peer_at_offset (path,
651                                  0))
652     {
653       /* received message from unexpected direction, ignore! */
654       GNUNET_break_op (0);
655       return;
656     }
657     LOG (GNUNET_ERROR_TYPE_DEBUG,
658          "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
659          GNUNET_sh2s (&msg->cid.connection_of_tunnel));
660
661     GCC_destroy (cc);
662     return;
663   }
664
665   /* We're just an intermediary peer, route the message along its path */
666   LOG (GNUNET_ERROR_TYPE_DEBUG,
667        "Received CONNECTION_DESTROY for connection %s. Destroying route.\n",
668        GNUNET_sh2s (&msg->cid.connection_of_tunnel));
669   route = get_route (&msg->cid);
670   route_message (peer,
671                  &msg->cid,
672                  &msg->header);
673   destroy_route (route);
674 }
675
676
677 /**
678  * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
679  *
680  * @param cls Closure (CadetPeer for neighbor that sent the message).
681  * @param msg Message itself.
682  */
683 static void
684 handle_tunnel_kx (void *cls,
685                   const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
686 {
687   struct CadetPeer *peer = cls;
688   struct CadetConnection *cc;
689
690   /* First, check if message belongs to a connection that ends here. */
691   cc = GNUNET_CONTAINER_multishortmap_get (connections,
692                                            &msg->cid.connection_of_tunnel);
693   if (NULL != cc)
694   {
695     /* verify message came from the right direction */
696     struct CadetPeerPath *path = GCC_get_path (cc);
697
698     if (peer !=
699         GCPP_get_peer_at_offset (path,
700                                  0))
701     {
702       /* received message from unexpected direction, ignore! */
703       GNUNET_break_op (0);
704       return;
705     }
706     GCC_handle_kx (cc,
707                    msg);
708     return;
709   }
710
711   /* We're just an intermediary peer, route the message along its path */
712   route_message (peer,
713                  &msg->cid,
714                  &msg->header);
715 }
716
717
718 /**
719  * Check if the encrypted message has the appropriate size.
720  *
721  * @param cls Closure (unused).
722  * @param msg Message to check.
723  *
724  * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
725  */
726 static int
727 check_tunnel_encrypted (void *cls,
728                         const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
729 {
730   return GNUNET_YES;
731 }
732
733
734 /**
735  * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
736  *
737  * @param cls Closure (CadetPeer for neighbor that sent the message).
738  * @param msg Message itself.
739  */
740 static void
741 handle_tunnel_encrypted (void *cls,
742                          const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
743 {
744   struct CadetPeer *peer = cls;
745   struct CadetConnection *cc;
746
747   /* First, check if message belongs to a connection that ends here. */
748   cc = GNUNET_CONTAINER_multishortmap_get (connections,
749                                            &msg->cid.connection_of_tunnel);
750   if (NULL != cc)
751   {
752     /* verify message came from the right direction */
753     struct CadetPeerPath *path = GCC_get_path (cc);
754
755     if (peer !=
756         GCPP_get_peer_at_offset (path,
757                                  0))
758     {
759       /* received message from unexpected direction, ignore! */
760       GNUNET_break_op (0);
761       return;
762     }
763     GCC_handle_encrypted (cc,
764                           msg);
765     return;
766   }
767   /* We're just an intermediary peer, route the message along its path */
768   route_message (peer,
769                  &msg->cid,
770                  &msg->header);
771 }
772
773
774 /**
775  * Function called after #GNUNET_CORE_connect has succeeded (or failed
776  * for good).  Note that the private key of the peer is intentionally
777  * not exposed here; if you need it, your process should try to read
778  * the private key file directly (which should work if you are
779  * authorized...).  Implementations of this function must not call
780  * #GNUNET_CORE_disconnect (other than by scheduling a new task to
781  * do this later).
782  *
783  * @param cls closure
784  * @param my_identity ID of this peer, NULL if we failed
785  */
786 static void
787 core_init_cb (void *cls,
788               const struct GNUNET_PeerIdentity *my_identity)
789 {
790   if (NULL == my_identity)
791   {
792     GNUNET_break (0);
793     return;
794   }
795   GNUNET_break (0 ==
796                 memcmp (my_identity,
797                         &my_full_id,
798                         sizeof (struct GNUNET_PeerIdentity)));
799 }
800
801
802 /**
803  * Method called whenever a given peer connects.
804  *
805  * @param cls closure
806  * @param peer peer identity this notification is about
807  */
808 static void *
809 core_connect_cb (void *cls,
810                  const struct GNUNET_PeerIdentity *peer,
811                  struct GNUNET_MQ_Handle *mq)
812 {
813   struct CadetPeer *cp;
814
815   LOG (GNUNET_ERROR_TYPE_DEBUG,
816        "CORE connection to peer %s was established.\n",
817        GNUNET_i2s (peer));
818   cp = GCP_get (peer,
819                 GNUNET_YES);
820   GCP_set_mq (cp,
821               mq);
822   return cp;
823 }
824
825
826 /**
827  * Method called whenever a peer disconnects.
828  *
829  * @param cls closure
830  * @param peer peer identity this notification is about
831  */
832 static void
833 core_disconnect_cb (void *cls,
834                     const struct GNUNET_PeerIdentity *peer,
835                     void *peer_cls)
836 {
837   struct CadetPeer *cp = peer_cls;
838
839   LOG (GNUNET_ERROR_TYPE_DEBUG,
840        "CORE connection to peer %s went down.\n",
841        GNUNET_i2s (peer));
842   GCP_set_mq (cp,
843               NULL);
844 }
845
846
847 /**
848  * Initialize the CORE subsystem.
849  *
850  * @param c Configuration.
851  */
852 void
853 GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
854 {
855   struct GNUNET_MQ_MessageHandler handlers[] = {
856     GNUNET_MQ_hd_var_size (connection_create,
857                            GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
858                            struct GNUNET_CADET_ConnectionCreateMessage,
859                            NULL),
860     GNUNET_MQ_hd_fixed_size (connection_create_ack,
861                              GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
862                              struct GNUNET_CADET_ConnectionCreateAckMessage,
863                              NULL),
864     GNUNET_MQ_hd_fixed_size (connection_broken,
865                              GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
866                              struct GNUNET_CADET_ConnectionBrokenMessage,
867                              NULL),
868     GNUNET_MQ_hd_fixed_size (connection_destroy,
869                              GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
870                              struct GNUNET_CADET_ConnectionDestroyMessage,
871                              NULL),
872     GNUNET_MQ_hd_fixed_size (tunnel_kx,
873                              GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
874                              struct GNUNET_CADET_TunnelKeyExchangeMessage,
875                              NULL),
876     GNUNET_MQ_hd_var_size (tunnel_encrypted,
877                            GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
878                            struct GNUNET_CADET_TunnelEncryptedMessage,
879                            NULL),
880     GNUNET_MQ_handler_end ()
881   };
882
883   routes = GNUNET_CONTAINER_multishortmap_create (1024,
884                                                   GNUNET_NO);
885   core = GNUNET_CORE_connect (c,
886                               NULL,
887                               &core_init_cb,
888                               &core_connect_cb,
889                               &core_disconnect_cb,
890                               handlers);
891 }
892
893
894 /**
895  * Shut down the CORE subsystem.
896  */
897 void
898 GCO_shutdown ()
899 {
900   if (NULL != core)
901   {
902     GNUNET_CORE_disconnect (core);
903     core = NULL;
904   }
905   GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
906   GNUNET_CONTAINER_multishortmap_destroy (routes);
907 }
908
909 /* end of gnunet-cadet-service_core.c */