Verify that GCD(m,n) != 1 when n is an RSA modulus
[oweals/gnunet.git] / src / core / core_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2014 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  * @file core/core_api.c
22  * @brief core service; this is the main API for encrypted P2P
23  *        communications
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
30 #include "core.h"
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "core-api",__VA_ARGS__)
33
34
35 /**
36  * Handle for a transmission request.
37  */
38 struct GNUNET_CORE_TransmitHandle
39 {
40
41   /**
42    * Corresponding peer record.
43    */
44   struct PeerRecord *peer;
45
46   /**
47    * Corresponding SEND_REQUEST message.  Only non-NULL
48    * while SEND_REQUEST message is pending.
49    */
50   struct ControlMessage *cm;
51
52   /**
53    * Function that will be called to get the actual request
54    * (once we are ready to transmit this request to the core).
55    * The function will be called with a NULL buffer to signal
56    * timeout.
57    */
58   GNUNET_CONNECTION_TransmitReadyNotify get_message;
59
60   /**
61    * Closure for @e get_message.
62    */
63   void *get_message_cls;
64
65   /**
66    * Deadline for the transmission (the request does not get cancelled
67    * at this time, this is merely how soon the application wants this out).
68    */
69   struct GNUNET_TIME_Absolute deadline;
70
71   /**
72    * When did this request get queued?
73    */
74   struct GNUNET_TIME_Absolute request_time;
75
76   /**
77    * How important is this message?
78    */
79   enum GNUNET_CORE_Priority priority;
80
81   /**
82    * Is corking allowed?
83    */
84   int cork;
85
86   /**
87    * Size of this request.
88    */
89   uint16_t msize;
90
91   /**
92    * Send message request ID for this request.
93    */
94   uint16_t smr_id;
95
96 };
97
98
99 /**
100  * Information we track for each peer.
101  */
102 struct PeerRecord
103 {
104
105   /**
106    * We generally do NOT keep peer records in a DLL; this
107    * DLL is only used IF this peer's 'pending_head' message
108    * is ready for transmission.
109    */
110   struct PeerRecord *prev;
111
112   /**
113    * We generally do NOT keep peer records in a DLL; this
114    * DLL is only used IF this peer's 'pending_head' message
115    * is ready for transmission.
116    */
117   struct PeerRecord *next;
118
119   /**
120    * Corresponding core handle.
121    */
122   struct GNUNET_CORE_Handle *ch;
123
124   /**
125    * Pending request, if any.  'th->peer' is set to NULL if the
126    * request is not active.
127    */
128   struct GNUNET_CORE_TransmitHandle th;
129
130   /**
131    * Peer the record is about.
132    */
133   struct GNUNET_PeerIdentity peer;
134
135   /**
136    * ID of task to run #run_request_next_transmission().
137    */
138   struct GNUNET_SCHEDULER_Task *ntr_task;
139
140   /**
141    * SendMessageRequest ID generator for this peer.
142    */
143   uint16_t smr_id_gen;
144
145 };
146
147
148 /**
149  * Type of function called upon completion.
150  *
151  * @param cls closure
152  * @param success #GNUNET_OK on success (which for request_connect
153  *        ONLY means that we transmitted the connect request to CORE,
154  *        it does not mean that we are actually now connected!);
155  *        #GNUNET_NO on timeout,
156  *        #GNUNET_SYSERR if core was shut down
157  */
158 typedef void
159 (*GNUNET_CORE_ControlContinuation) (void *cls,
160                                     int success);
161
162
163 /**
164  * Entry in a doubly-linked list of control messages to be transmitted
165  * to the core service.  Control messages include traffic allocation,
166  * connection requests and of course our initial 'init' request.
167  *
168  * The actual message is allocated at the end of this struct.
169  */
170 struct ControlMessage
171 {
172   /**
173    * This is a doubly-linked list.
174    */
175   struct ControlMessage *next;
176
177   /**
178    * This is a doubly-linked list.
179    */
180   struct ControlMessage *prev;
181
182   /**
183    * Function to run after transmission failed/succeeded.
184    */
185   GNUNET_CORE_ControlContinuation cont;
186
187   /**
188    * Closure for @e cont.
189    */
190   void *cont_cls;
191
192   /**
193    * Transmit handle (if one is associated with this ControlMessage), or NULL.
194    */
195   struct GNUNET_CORE_TransmitHandle *th;
196 };
197
198
199
200 /**
201  * Context for the core service connection.
202  */
203 struct GNUNET_CORE_Handle
204 {
205
206   /**
207    * Configuration we're using.
208    */
209   const struct GNUNET_CONFIGURATION_Handle *cfg;
210
211   /**
212    * Closure for the various callbacks.
213    */
214   void *cls;
215
216   /**
217    * Function to call once we've handshaked with the core service.
218    */
219   GNUNET_CORE_StartupCallback init;
220
221   /**
222    * Function to call whenever we're notified about a peer connecting.
223    */
224   GNUNET_CORE_ConnectEventHandler connects;
225
226   /**
227    * Function to call whenever we're notified about a peer disconnecting.
228    */
229   GNUNET_CORE_DisconnectEventHandler disconnects;
230
231   /**
232    * Function to call whenever we receive an inbound message.
233    */
234   GNUNET_CORE_MessageCallback inbound_notify;
235
236   /**
237    * Function to call whenever we receive an outbound message.
238    */
239   GNUNET_CORE_MessageCallback outbound_notify;
240
241   /**
242    * Function handlers for messages of particular type.
243    */
244   const struct GNUNET_CORE_MessageHandler *handlers;
245
246   /**
247    * Our connection to the service.
248    */
249   struct GNUNET_CLIENT_Connection *client;
250
251   /**
252    * Handle for our current transmission request.
253    */
254   struct GNUNET_CLIENT_TransmitHandle *cth;
255
256   /**
257    * Head of doubly-linked list of pending requests.
258    */
259   struct ControlMessage *control_pending_head;
260
261   /**
262    * Tail of doubly-linked list of pending requests.
263    */
264   struct ControlMessage *control_pending_tail;
265
266   /**
267    * Head of doubly-linked list of peers that are core-approved
268    * to send their next message.
269    */
270   struct PeerRecord *ready_peer_head;
271
272   /**
273    * Tail of doubly-linked list of peers that are core-approved
274    * to send their next message.
275    */
276   struct PeerRecord *ready_peer_tail;
277
278   /**
279    * Hash map listing all of the peers that we are currently
280    * connected to.
281    */
282   struct GNUNET_CONTAINER_MultiPeerMap *peers;
283
284   /**
285    * Identity of this peer.
286    */
287   struct GNUNET_PeerIdentity me;
288
289   /**
290    * ID of reconnect task (if any).
291    */
292   struct GNUNET_SCHEDULER_Task * reconnect_task;
293
294   /**
295    * Current delay we use for re-trying to connect to core.
296    */
297   struct GNUNET_TIME_Relative retry_backoff;
298
299   /**
300    * Number of entries in the handlers array.
301    */
302   unsigned int hcnt;
303
304   /**
305    * For inbound notifications without a specific handler, do
306    * we expect to only receive headers?
307    */
308   int inbound_hdr_only;
309
310   /**
311    * For outbound notifications without a specific handler, do
312    * we expect to only receive headers?
313    */
314   int outbound_hdr_only;
315
316   /**
317    * Are we currently disconnected and hence unable to forward
318    * requests?
319    */
320   int currently_down;
321
322 };
323
324
325 /**
326  * Our current client connection went down.  Clean it up
327  * and try to reconnect!
328  *
329  * @param h our handle to the core service
330  */
331 static void
332 reconnect (struct GNUNET_CORE_Handle *h);
333
334
335 /**
336  * Task schedule to try to re-connect to core.
337  *
338  * @param cls the `struct GNUNET_CORE_Handle`
339  * @param tc task context
340  */
341 static void
342 reconnect_task (void *cls)
343 {
344   struct GNUNET_CORE_Handle *h = cls;
345
346   h->reconnect_task = NULL;
347   LOG (GNUNET_ERROR_TYPE_DEBUG,
348        "Connecting to CORE service after delay\n");
349   reconnect (h);
350 }
351
352
353 /**
354  * Notify clients about disconnect and free
355  * the entry for connected peer.
356  *
357  * @param cls the `struct GNUNET_CORE_Handle *`
358  * @param key the peer identity (not used)
359  * @param value the `struct PeerRecord` to free.
360  * @return #GNUNET_YES (continue)
361  */
362 static int
363 disconnect_and_free_peer_entry (void *cls,
364                                 const struct GNUNET_PeerIdentity *key,
365                                 void *value)
366 {
367   struct GNUNET_CORE_Handle *h = cls;
368   struct GNUNET_CORE_TransmitHandle *th;
369   struct PeerRecord *pr = value;
370
371   if (NULL != pr->ntr_task)
372   {
373     GNUNET_SCHEDULER_cancel (pr->ntr_task);
374     pr->ntr_task = NULL;
375   }
376   if ( (NULL != pr->prev) ||
377        (NULL != pr->next) ||
378        (h->ready_peer_head == pr) )
379     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
380                                  h->ready_peer_tail,
381                                  pr);
382   if (NULL != h->disconnects)
383     h->disconnects (h->cls,
384                     &pr->peer);
385   /* all requests should have been cancelled, clean up anyway, just in case */
386   th = &pr->th;
387   if (NULL != th->peer)
388   {
389     GNUNET_break (0);
390     th->peer = NULL;
391     if (NULL != th->cm)
392       th->cm->th = NULL;
393   }
394   /* done with 'voluntary' cleanups, now on to normal freeing */
395   GNUNET_assert (GNUNET_YES ==
396                  GNUNET_CONTAINER_multipeermap_remove (h->peers, key, pr));
397   GNUNET_assert (pr->ch == h);
398   GNUNET_assert (NULL == pr->ntr_task);
399   GNUNET_free (pr);
400   return GNUNET_YES;
401 }
402
403
404 /**
405  * Close down any existing connection to the CORE service and
406  * try re-establishing it later.
407  *
408  * @param h our handle
409  */
410 static void
411 reconnect_later (struct GNUNET_CORE_Handle *h)
412 {
413   struct ControlMessage *cm;
414   struct PeerRecord *pr;
415
416   GNUNET_assert (NULL == h->reconnect_task);
417   if (NULL != h->cth)
418   {
419     GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
420     h->cth = NULL;
421   }
422   if (NULL != h->client)
423   {
424     GNUNET_CLIENT_disconnect (h->client);
425     h->client = NULL;
426   }
427   h->currently_down = GNUNET_YES;
428   GNUNET_assert (h->reconnect_task == NULL);
429   h->reconnect_task =
430       GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
431                                     &reconnect_task, h);
432   while (NULL != (cm = h->control_pending_head))
433   {
434     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
435                                  h->control_pending_tail,
436                                  cm);
437     if (NULL != cm->th)
438       cm->th->cm = NULL;
439     if (NULL != cm->cont)
440       cm->cont (cm->cont_cls, GNUNET_NO);
441     GNUNET_free (cm);
442   }
443   GNUNET_CONTAINER_multipeermap_iterate (h->peers,
444                                          &disconnect_and_free_peer_entry, h);
445   while (NULL != (pr = h->ready_peer_head))
446     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
447                                  h->ready_peer_tail,
448                                  pr);
449   GNUNET_assert (NULL == h->control_pending_head);
450   h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
451 }
452
453
454 /**
455  * Check the list of pending requests, send the next
456  * one to the core.
457  *
458  * @param h core handle
459  * @param ignore_currently_down transmit message even if not initialized?
460  */
461 static void
462 trigger_next_request (struct GNUNET_CORE_Handle *h,
463                       int ignore_currently_down);
464
465
466 /**
467  * Send a control message to the peer asking for transmission
468  * of the message in the given peer record.
469  *
470  * @param pr peer to request transmission to
471  */
472 static void
473 request_next_transmission (struct PeerRecord *pr)
474 {
475   struct GNUNET_CORE_Handle *h = pr->ch;
476   struct ControlMessage *cm;
477   struct SendMessageRequest *smr;
478   struct GNUNET_CORE_TransmitHandle *th;
479
480   th = &pr->th;
481   if (NULL == th->peer)
482   {
483     trigger_next_request (h, GNUNET_NO);
484     return;
485   }
486   if (NULL != th->cm)
487     return;                     /* already done */
488   GNUNET_assert (NULL == pr->prev);
489   GNUNET_assert (NULL == pr->next);
490   cm = GNUNET_malloc (sizeof (struct ControlMessage) +
491                       sizeof (struct SendMessageRequest));
492   th->cm = cm;
493   cm->th = th;
494   smr = (struct SendMessageRequest *) &cm[1];
495   smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
496   smr->header.size = htons (sizeof (struct SendMessageRequest));
497   smr->priority = htonl ((uint32_t) th->priority);
498   smr->deadline = GNUNET_TIME_absolute_hton (th->deadline);
499   smr->peer = pr->peer;
500   smr->reserved = htonl (0);
501   smr->size = htons (th->msize);
502   smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
503   GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
504                                     h->control_pending_tail, cm);
505   LOG (GNUNET_ERROR_TYPE_DEBUG,
506        "Adding SEND REQUEST for peer `%s' to message queue\n",
507        GNUNET_i2s (&pr->peer));
508   trigger_next_request (h, GNUNET_NO);
509 }
510
511
512 /**
513  * Transmit the next message to the core service.
514  *
515  * @param cls closure with the `struct GNUNET_CORE_Handle`
516  * @param size number of bytes available in @a buf
517  * @param buf where the callee should write the message
518  * @return number of bytes written to @a buf
519  */
520 static size_t
521 transmit_message (void *cls,
522                   size_t size,
523                   void *buf)
524 {
525   struct GNUNET_CORE_Handle *h = cls;
526   struct ControlMessage *cm;
527   struct GNUNET_CORE_TransmitHandle *th;
528   struct GNUNET_TIME_Relative delay;
529   struct GNUNET_TIME_Relative overdue;
530   struct PeerRecord *pr;
531   struct SendMessage *sm;
532   const struct GNUNET_MessageHeader *hdr;
533   uint16_t msize;
534   size_t ret;
535
536   GNUNET_assert (h->reconnect_task == NULL);
537   h->cth = NULL;
538   if (NULL == buf)
539   {
540     LOG (GNUNET_ERROR_TYPE_DEBUG,
541          "Transmission failed, initiating reconnect\n");
542     reconnect_later (h);
543     return 0;
544   }
545   /* first check for control messages */
546   if (NULL != (cm = h->control_pending_head))
547   {
548     hdr = (const struct GNUNET_MessageHeader *) &cm[1];
549     msize = ntohs (hdr->size);
550     if (size < msize)
551     {
552       trigger_next_request (h, GNUNET_NO);
553       return 0;
554     }
555     LOG (GNUNET_ERROR_TYPE_DEBUG,
556          "Transmitting control message with %u bytes of type %u to core.\n",
557          (unsigned int) msize,
558          (unsigned int) ntohs (hdr->type));
559     memcpy (buf, hdr, msize);
560     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
561                                  h->control_pending_tail, cm);
562     if (NULL != cm->th)
563       cm->th->cm = NULL;
564     if (NULL != cm->cont)
565       cm->cont (cm->cont_cls, GNUNET_OK);
566     GNUNET_free (cm);
567     trigger_next_request (h, GNUNET_NO);
568     return msize;
569   }
570   /* now check for 'ready' P2P messages */
571   if (NULL == (pr = h->ready_peer_head))
572     return 0;
573   GNUNET_assert (NULL != pr->th.peer);
574   th = &pr->th;
575   if (size < th->msize + sizeof (struct SendMessage))
576   {
577     trigger_next_request (h, GNUNET_NO);
578     return 0;
579   }
580   GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
581                                h->ready_peer_tail,
582                                pr);
583   th->peer = NULL;
584   LOG (GNUNET_ERROR_TYPE_DEBUG,
585        "Transmitting SEND request to `%s' with %u bytes.\n",
586        GNUNET_i2s (&pr->peer),
587        (unsigned int) th->msize);
588   sm = (struct SendMessage *) buf;
589   sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
590   sm->priority = htonl ((uint32_t) th->priority);
591   sm->deadline = GNUNET_TIME_absolute_hton (th->deadline);
592   sm->peer = pr->peer;
593   sm->cork = htonl ((uint32_t) th->cork);
594   sm->reserved = htonl (0);
595   ret =
596     th->get_message (th->get_message_cls,
597                      size - sizeof (struct SendMessage),
598                      &sm[1]);
599   delay = GNUNET_TIME_absolute_get_duration (th->request_time);
600   overdue = GNUNET_TIME_absolute_get_duration (th->deadline);
601   if (overdue.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
602     LOG (GNUNET_ERROR_TYPE_WARNING,
603          "Transmitting overdue %u bytes to `%s' at priority %u with %s delay%s\n",
604          ret,
605          GNUNET_i2s (&pr->peer),
606          (unsigned int) th->priority,
607          GNUNET_STRINGS_relative_time_to_string (delay,
608                                                  GNUNET_YES),
609          (th->cork) ? " (corked)" : "");
610   else
611     LOG (GNUNET_ERROR_TYPE_DEBUG,
612          "Transmitting %u bytes to `%s' at priority %u with %s delay%s\n",
613          ret,
614          GNUNET_i2s (&pr->peer),
615          (unsigned int) th->priority,
616          GNUNET_STRINGS_relative_time_to_string (delay,
617                                                  GNUNET_YES),
618          (th->cork) ? " (corked)" : "");
619   if ( (0 == ret) &&
620        (GNUNET_CORE_PRIO_BACKGROUND == th->priority) )
621   {
622     /* client decided to send nothing; as the priority was
623        BACKGROUND, we can just not send anything to core.
624        For higher-priority messages, we must give an
625        empty message to CORE so that it knows that this
626        message is no longer pending. */
627     LOG (GNUNET_ERROR_TYPE_DEBUG,
628          "Size of clients message to peer %s is 0!\n",
629          GNUNET_i2s (&pr->peer));
630     request_next_transmission (pr);
631     return 0;
632   }
633   LOG (GNUNET_ERROR_TYPE_DEBUG,
634        "Produced SEND message to core with %u bytes payload\n",
635        (unsigned int) ret);
636   if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
637   {
638     GNUNET_break (0);
639     request_next_transmission (pr);
640     return 0;
641   }
642   ret += sizeof (struct SendMessage);
643   sm->header.size = htons (ret);
644   GNUNET_assert (ret <= size);
645   request_next_transmission (pr);
646   return ret;
647 }
648
649
650 /**
651  * Check the list of pending requests, send the next one to the core.
652  *
653  * @param h core handle
654  * @param ignore_currently_down transmit message even if not initialized?
655  */
656 static void
657 trigger_next_request (struct GNUNET_CORE_Handle *h,
658                       int ignore_currently_down)
659 {
660   uint16_t msize;
661
662   if ( (GNUNET_YES == h->currently_down) &&
663        (GNUNET_NO == ignore_currently_down) )
664   {
665     LOG (GNUNET_ERROR_TYPE_DEBUG,
666          "Core connection down, not processing queue\n");
667     return;
668   }
669   if (NULL != h->cth)
670   {
671     LOG (GNUNET_ERROR_TYPE_DEBUG,
672          "Request pending, not processing queue\n");
673     return;
674   }
675   if (NULL != h->control_pending_head)
676     msize =
677         ntohs (((struct GNUNET_MessageHeader *) &h->
678                 control_pending_head[1])->size);
679   else if (h->ready_peer_head != NULL)
680     msize =
681       h->ready_peer_head->th.msize + sizeof (struct SendMessage);
682   else
683   {
684     LOG (GNUNET_ERROR_TYPE_DEBUG,
685          "Request queue empty, not processing queue\n");
686     return;                     /* no pending message */
687   }
688   h->cth =
689       GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
690                                            GNUNET_TIME_UNIT_FOREVER_REL,
691                                            GNUNET_NO,
692                                            &transmit_message, h);
693 }
694
695
696 /**
697  * Handler for notification messages received from the core.
698  *
699  * @param cls our `struct GNUNET_CORE_Handle`
700  * @param msg the message received from the core service
701  */
702 static void
703 main_notify_handler (void *cls,
704                      const struct GNUNET_MessageHeader *msg)
705 {
706   struct GNUNET_CORE_Handle *h = cls;
707   const struct InitReplyMessage *m;
708   const struct ConnectNotifyMessage *cnm;
709   const struct DisconnectNotifyMessage *dnm;
710   const struct NotifyTrafficMessage *ntm;
711   const struct GNUNET_MessageHeader *em;
712   const struct SendMessageReady *smr;
713   const struct GNUNET_CORE_MessageHandler *mh;
714   GNUNET_CORE_StartupCallback init;
715   struct PeerRecord *pr;
716   struct GNUNET_CORE_TransmitHandle *th;
717   unsigned int hpos;
718   int trigger;
719   uint16_t msize;
720   uint16_t et;
721
722   if (NULL == msg)
723   {
724     LOG (GNUNET_ERROR_TYPE_INFO,
725          _("Client was disconnected from core service, trying to reconnect.\n"));
726     reconnect_later (h);
727     return;
728   }
729   msize = ntohs (msg->size);
730   LOG (GNUNET_ERROR_TYPE_DEBUG,
731        "Processing message of type %u and size %u from core service\n",
732        ntohs (msg->type), msize);
733   switch (ntohs (msg->type))
734   {
735   case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
736     if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
737     {
738       GNUNET_break (0);
739       reconnect_later (h);
740       return;
741     }
742     m = (const struct InitReplyMessage *) msg;
743     GNUNET_break (0 == ntohl (m->reserved));
744     /* start our message processing loop */
745     if (GNUNET_YES == h->currently_down)
746     {
747       h->currently_down = GNUNET_NO;
748       trigger_next_request (h, GNUNET_NO);
749     }
750     h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
751     h->me = m->my_identity;
752     if (NULL != (init = h->init))
753     {
754       /* mark so we don't call init on reconnect */
755       h->init = NULL;
756       LOG (GNUNET_ERROR_TYPE_DEBUG,
757            "Connected to core service of peer `%s'.\n",
758            GNUNET_i2s (&h->me));
759       init (h->cls, &h->me);
760     }
761     else
762     {
763       LOG (GNUNET_ERROR_TYPE_DEBUG,
764            "Successfully reconnected to core service.\n");
765     }
766     /* fake 'connect to self' */
767     pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &h->me);
768     GNUNET_assert (NULL == pr);
769     pr = GNUNET_new (struct PeerRecord);
770     pr->peer = h->me;
771     pr->ch = h;
772     GNUNET_assert (GNUNET_YES ==
773                    GNUNET_CONTAINER_multipeermap_put (h->peers,
774                                                       &h->me, pr,
775                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
776     if (NULL != h->connects)
777       h->connects (h->cls, &pr->peer);
778     break;
779   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
780     if (msize < sizeof (struct ConnectNotifyMessage))
781     {
782       GNUNET_break (0);
783       reconnect_later (h);
784       return;
785     }
786     cnm = (const struct ConnectNotifyMessage *) msg;
787     if (msize !=
788         sizeof (struct ConnectNotifyMessage))
789     {
790       GNUNET_break (0);
791       reconnect_later (h);
792       return;
793     }
794     LOG (GNUNET_ERROR_TYPE_DEBUG,
795          "Received notification about connection from `%s'.\n",
796          GNUNET_i2s (&cnm->peer));
797     if (0 == memcmp (&h->me,
798                      &cnm->peer,
799                      sizeof (struct GNUNET_PeerIdentity)))
800     {
801       /* connect to self!? */
802       GNUNET_break (0);
803       return;
804     }
805     pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &cnm->peer);
806     if (NULL != pr)
807     {
808       GNUNET_break (0);
809       reconnect_later (h);
810       return;
811     }
812     pr = GNUNET_new (struct PeerRecord);
813     pr->peer = cnm->peer;
814     pr->ch = h;
815     GNUNET_assert (GNUNET_YES ==
816                    GNUNET_CONTAINER_multipeermap_put (h->peers,
817                                                       &cnm->peer, pr,
818                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
819     if (NULL != h->connects)
820       h->connects (h->cls, &pr->peer);
821     break;
822   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
823     if (msize != sizeof (struct DisconnectNotifyMessage))
824     {
825       GNUNET_break (0);
826       reconnect_later (h);
827       return;
828     }
829     dnm = (const struct DisconnectNotifyMessage *) msg;
830     if (0 == memcmp (&h->me,
831                      &dnm->peer,
832                      sizeof (struct GNUNET_PeerIdentity)))
833     {
834       /* connection to self!? */
835       GNUNET_break (0);
836       return;
837     }
838     GNUNET_break (0 == ntohl (dnm->reserved));
839     LOG (GNUNET_ERROR_TYPE_DEBUG,
840          "Received notification about disconnect from `%s'.\n",
841          GNUNET_i2s (&dnm->peer));
842     pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &dnm->peer);
843     if (NULL == pr)
844     {
845       GNUNET_break (0);
846       reconnect_later (h);
847       return;
848     }
849     trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
850                (h->ready_peer_head == pr));
851     disconnect_and_free_peer_entry (h, &dnm->peer, pr);
852     if (trigger)
853       trigger_next_request (h, GNUNET_NO);
854     break;
855   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
856     if (msize < sizeof (struct NotifyTrafficMessage))
857     {
858       GNUNET_break (0);
859       reconnect_later (h);
860       return;
861     }
862     ntm = (const struct NotifyTrafficMessage *) msg;
863     if ((msize <
864          sizeof (struct NotifyTrafficMessage) +
865          sizeof (struct GNUNET_MessageHeader)) )
866     {
867       GNUNET_break (0);
868       reconnect_later (h);
869       return;
870     }
871     em = (const struct GNUNET_MessageHeader *) &ntm[1];
872     LOG (GNUNET_ERROR_TYPE_DEBUG,
873          "Received message of type %u and size %u from peer `%s'\n",
874          ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
875     if ((GNUNET_NO == h->inbound_hdr_only) &&
876         (msize !=
877          ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
878     {
879       GNUNET_break (0);
880       reconnect_later (h);
881       return;
882     }
883     et = ntohs (em->type);
884     for (hpos = 0; hpos < h->hcnt; hpos++)
885     {
886       mh = &h->handlers[hpos];
887       if (mh->type != et)
888         continue;
889       if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
890       {
891         LOG (GNUNET_ERROR_TYPE_ERROR,
892              "Unexpected message size %u for message of type %u from peer `%s'\n",
893              htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
894         GNUNET_break_op (0);
895         continue;
896       }
897       pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &ntm->peer);
898       if (NULL == pr)
899       {
900         GNUNET_break (0);
901         reconnect_later (h);
902         return;
903       }
904       if (GNUNET_OK !=
905           h->handlers[hpos].callback (h->cls, &ntm->peer, em))
906       {
907         /* error in processing, do not process other messages! */
908         break;
909       }
910     }
911     if (NULL != h->inbound_notify)
912       h->inbound_notify (h->cls, &ntm->peer, em);
913     break;
914   case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
915     if (msize < sizeof (struct NotifyTrafficMessage))
916     {
917       GNUNET_break (0);
918       reconnect_later (h);
919       return;
920     }
921     ntm = (const struct NotifyTrafficMessage *) msg;
922     if ((msize <
923          sizeof (struct NotifyTrafficMessage) +
924          sizeof (struct GNUNET_MessageHeader)) )
925     {
926       GNUNET_break (0);
927       reconnect_later (h);
928       return;
929     }
930     em = (const struct GNUNET_MessageHeader *) &ntm[1];
931     LOG (GNUNET_ERROR_TYPE_DEBUG,
932          "Received notification about transmission to `%s'.\n",
933          GNUNET_i2s (&ntm->peer));
934     if ((GNUNET_NO == h->outbound_hdr_only) &&
935         (msize !=
936          ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
937     {
938       GNUNET_break (0);
939       reconnect_later (h);
940       return;
941     }
942     if (NULL == h->outbound_notify)
943     {
944       GNUNET_break (0);
945       break;
946     }
947     h->outbound_notify (h->cls, &ntm->peer, em);
948     break;
949   case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
950     if (msize != sizeof (struct SendMessageReady))
951     {
952       GNUNET_break (0);
953       reconnect_later (h);
954       return;
955     }
956     smr = (const struct SendMessageReady *) msg;
957     pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
958                                             &smr->peer);
959     if (NULL == pr)
960     {
961       GNUNET_break (0);
962       reconnect_later (h);
963       return;
964     }
965     LOG (GNUNET_ERROR_TYPE_DEBUG,
966          "Received notification about transmission readiness to `%s'.\n",
967          GNUNET_i2s (&smr->peer));
968     if (NULL == pr->th.peer)
969     {
970       /* request must have been cancelled between the original request
971        * and the response from core, ignore core's readiness */
972       break;
973     }
974
975     th = &pr->th;
976     if (ntohs (smr->smr_id) != th->smr_id)
977     {
978       /* READY message is for expired or cancelled message,
979        * ignore! (we should have already sent another request) */
980       break;
981     }
982     if ( (NULL != pr->prev) ||
983          (NULL != pr->next) ||
984          (h->ready_peer_head == pr) )
985     {
986       /* we should not already be on the ready list... */
987       GNUNET_break (0);
988       reconnect_later (h);
989       return;
990     }
991     GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
992                                  h->ready_peer_tail,
993                                  pr);
994     trigger_next_request (h, GNUNET_NO);
995     break;
996   default:
997     reconnect_later (h);
998     return;
999   }
1000   GNUNET_CLIENT_receive (h->client,
1001                          &main_notify_handler, h,
1002                          GNUNET_TIME_UNIT_FOREVER_REL);
1003 }
1004
1005
1006 /**
1007  * Task executed once we are done transmitting the INIT message.
1008  * Starts our 'receive' loop.
1009  *
1010  * @param cls the 'struct GNUNET_CORE_Handle'
1011  * @param success were we successful
1012  */
1013 static void
1014 init_done_task (void *cls, int success)
1015 {
1016   struct GNUNET_CORE_Handle *h = cls;
1017
1018   if (GNUNET_SYSERR == success)
1019     return;                     /* shutdown */
1020   if (GNUNET_NO == success)
1021   {
1022     LOG (GNUNET_ERROR_TYPE_DEBUG,
1023          "Failed to exchange INIT with core, retrying\n");
1024     if (h->reconnect_task == NULL)
1025       reconnect_later (h);
1026     return;
1027   }
1028   GNUNET_CLIENT_receive (h->client,
1029                          &main_notify_handler, h,
1030                          GNUNET_TIME_UNIT_FOREVER_REL);
1031 }
1032
1033
1034 /**
1035  * Our current client connection went down.  Clean it up
1036  * and try to reconnect!
1037  *
1038  * @param h our handle to the core service
1039  */
1040 static void
1041 reconnect (struct GNUNET_CORE_Handle *h)
1042 {
1043   struct ControlMessage *cm;
1044   struct InitMessage *init;
1045   uint32_t opt;
1046   uint16_t msize;
1047   uint16_t *ts;
1048   unsigned int hpos;
1049
1050   GNUNET_assert (NULL == h->client);
1051   GNUNET_assert (GNUNET_YES == h->currently_down);
1052   GNUNET_assert (NULL != h->cfg);
1053   h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1054   if (NULL == h->client)
1055   {
1056     reconnect_later (h);
1057     return;
1058   }
1059   msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1060   cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1061   cm->cont = &init_done_task;
1062   cm->cont_cls = h;
1063   init = (struct InitMessage *) &cm[1];
1064   init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1065   init->header.size = htons (msize);
1066   opt = 0;
1067   if (NULL != h->inbound_notify)
1068   {
1069     if (h->inbound_hdr_only)
1070       opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1071     else
1072       opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1073   }
1074   if (NULL != h->outbound_notify)
1075   {
1076     if (h->outbound_hdr_only)
1077       opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1078     else
1079       opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1080   }
1081   LOG (GNUNET_ERROR_TYPE_INFO,
1082        "(Re)connecting to CORE service, monitoring messages of type %u\n",
1083        opt);
1084
1085   init->options = htonl (opt);
1086   ts = (uint16_t *) & init[1];
1087   for (hpos = 0; hpos < h->hcnt; hpos++)
1088     ts[hpos] = htons (h->handlers[hpos].type);
1089   GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1090                                h->control_pending_tail,
1091                                cm);
1092   trigger_next_request (h, GNUNET_YES);
1093 }
1094
1095
1096
1097 /**
1098  * Connect to the core service.  Note that the connection may
1099  * complete (or fail) asynchronously.
1100  *
1101  * @param cfg configuration to use
1102  * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1103  * @param init callback to call once we have successfully
1104  *        connected to the core service
1105  * @param connects function to call on peer connect, can be NULL
1106  * @param disconnects function to call on peer disconnect / timeout, can be NULL
1107  * @param inbound_notify function to call for all inbound messages, can be NULL
1108  * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
1109  *                GNUNET_MessageHeader and hence we do not need to give it the full message;
1110  *                can be used to improve efficiency, ignored if @a inbound_notify is NULLL
1111  * @param outbound_notify function to call for all outbound messages, can be NULL
1112  * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
1113  *                GNUNET_MessageHeader and hence we do not need to give it the full message
1114  *                can be used to improve efficiency, ignored if @a outbound_notify is NULLL
1115  * @param handlers callbacks for messages we care about, NULL-terminated
1116  * @return handle to the core service (only useful for disconnect until 'init' is called);
1117  *                NULL on error (in this case, init is never called)
1118  */
1119 struct GNUNET_CORE_Handle *
1120 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1121                      void *cls,
1122                      GNUNET_CORE_StartupCallback init,
1123                      GNUNET_CORE_ConnectEventHandler connects,
1124                      GNUNET_CORE_DisconnectEventHandler disconnects,
1125                      GNUNET_CORE_MessageCallback inbound_notify,
1126                      int inbound_hdr_only,
1127                      GNUNET_CORE_MessageCallback outbound_notify,
1128                      int outbound_hdr_only,
1129                      const struct GNUNET_CORE_MessageHandler *handlers)
1130 {
1131   struct GNUNET_CORE_Handle *h;
1132
1133   GNUNET_assert (NULL != cfg);
1134   h = GNUNET_new (struct GNUNET_CORE_Handle);
1135   h->cfg = cfg;
1136   h->cls = cls;
1137   h->init = init;
1138   h->connects = connects;
1139   h->disconnects = disconnects;
1140   h->inbound_notify = inbound_notify;
1141   h->outbound_notify = outbound_notify;
1142   h->inbound_hdr_only = inbound_hdr_only;
1143   h->outbound_hdr_only = outbound_hdr_only;
1144   h->handlers = handlers;
1145   h->hcnt = 0;
1146   h->currently_down = GNUNET_YES;
1147   h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1148   if (NULL != handlers)
1149     while (NULL != handlers[h->hcnt].callback)
1150       h->hcnt++;
1151   GNUNET_assert (h->hcnt <
1152                  (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1153                   sizeof (struct InitMessage)) / sizeof (uint16_t));
1154   LOG (GNUNET_ERROR_TYPE_DEBUG,
1155        "Connecting to CORE service\n");
1156   reconnect (h);
1157   return h;
1158 }
1159
1160
1161 /**
1162  * Disconnect from the core service.  This function can only
1163  * be called *after* all pending #GNUNET_CORE_notify_transmit_ready()
1164  * requests have been explicitly canceled.
1165  *
1166  * @param handle connection to core to disconnect
1167  */
1168 void
1169 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1170 {
1171   struct ControlMessage *cm;
1172
1173   GNUNET_assert (NULL != handle);
1174   LOG (GNUNET_ERROR_TYPE_DEBUG,
1175        "Disconnecting from CORE service\n");
1176   if (NULL != handle->cth)
1177   {
1178     GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1179     handle->cth = NULL;
1180   }
1181   while (NULL != (cm = handle->control_pending_head))
1182   {
1183     GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1184                                  handle->control_pending_tail,
1185                                  cm);
1186     if (NULL != cm->th)
1187       cm->th->cm = NULL;
1188     if (NULL != cm->cont)
1189       cm->cont (cm->cont_cls, GNUNET_SYSERR);
1190     GNUNET_free (cm);
1191   }
1192   if (NULL != handle->client)
1193   {
1194     GNUNET_CLIENT_disconnect (handle->client);
1195     handle->client = NULL;
1196   }
1197   GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
1198                                          &disconnect_and_free_peer_entry,
1199                                          handle);
1200   if (NULL != handle->reconnect_task)
1201   {
1202     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1203     handle->reconnect_task = NULL;
1204   }
1205   GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
1206   handle->peers = NULL;
1207   GNUNET_break (NULL == handle->ready_peer_head);
1208   GNUNET_free (handle);
1209 }
1210
1211
1212 /**
1213  * Task that calls #request_next_transmission().
1214  *
1215  * @param cls the `struct PeerRecord *`
1216  */
1217 static void
1218 run_request_next_transmission (void *cls)
1219 {
1220   struct PeerRecord *pr = cls;
1221
1222   pr->ntr_task = NULL;
1223   request_next_transmission (pr);
1224 }
1225
1226
1227 /**
1228  * Ask the core to call @a notify once it is ready to transmit the
1229  * given number of bytes to the specified @a target.  Must only be
1230  * called after a connection to the respective peer has been
1231  * established (and the client has been informed about this).  You may
1232  * have one request of this type pending for each connected peer at
1233  * any time.  If a peer disconnects, the application MUST call
1234  * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
1235  * transmission request, if one such request is pending.
1236  *
1237  * @param handle connection to core service
1238  * @param cork is corking allowed for this transmission?
1239  * @param priority how important is the message?
1240  * @param maxdelay how long can the message wait? Only effective if @a cork is #GNUNET_YES
1241  * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1242  * @param notify_size how many bytes of buffer space does @a notify want?
1243  * @param notify function to call when buffer space is available;
1244  *        will be called with NULL on timeout; clients MUST cancel
1245  *        all pending transmission requests DURING the disconnect
1246  *        handler
1247  * @param notify_cls closure for @a notify
1248  * @return non-NULL if the notify callback was queued,
1249  *         NULL if we can not even queue the request (request already pending);
1250  *         if NULL is returned, @a notify will NOT be called.
1251  */
1252 struct GNUNET_CORE_TransmitHandle *
1253 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1254                                    int cork,
1255                                    enum GNUNET_CORE_Priority priority,
1256                                    struct GNUNET_TIME_Relative maxdelay,
1257                                    const struct GNUNET_PeerIdentity *target,
1258                                    size_t notify_size,
1259                                    GNUNET_CONNECTION_TransmitReadyNotify notify,
1260                                    void *notify_cls)
1261 {
1262   struct PeerRecord *pr;
1263   struct GNUNET_CORE_TransmitHandle *th;
1264
1265   if (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1266   {
1267      GNUNET_break (0);
1268      return NULL;
1269   }
1270   GNUNET_assert (NULL != notify);
1271   LOG (GNUNET_ERROR_TYPE_DEBUG,
1272        "Asking core for transmission of %u bytes to `%s'\n",
1273        (unsigned int) notify_size,
1274        GNUNET_i2s (target));
1275   pr = GNUNET_CONTAINER_multipeermap_get (handle->peers,
1276                                           target);
1277   if (NULL == pr)
1278   {
1279     /* attempt to send to peer that is not connected */
1280     GNUNET_break (0);
1281     return NULL;
1282   }
1283   if (NULL != pr->th.peer)
1284   {
1285     /* attempting to queue a second request for the same destination */
1286     GNUNET_break (0);
1287     return NULL;
1288   }
1289   GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1290                  GNUNET_SERVER_MAX_MESSAGE_SIZE);
1291   th = &pr->th;
1292   memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1293   th->peer = pr;
1294   th->get_message = notify;
1295   th->get_message_cls = notify_cls;
1296   th->request_time = GNUNET_TIME_absolute_get ();
1297   if (GNUNET_YES == cork)
1298     th->deadline = GNUNET_TIME_relative_to_absolute (maxdelay);
1299   else
1300     th->deadline = th->request_time;
1301   th->priority = priority;
1302   th->msize = notify_size;
1303   th->cork = cork;
1304   GNUNET_assert (NULL == pr->ntr_task);
1305   pr->ntr_task =
1306     GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1307   LOG (GNUNET_ERROR_TYPE_DEBUG,
1308        "Transmission request added to queue\n");
1309   return th;
1310 }
1311
1312
1313 /**
1314  * Cancel the specified transmission-ready notification.
1315  *
1316  * @param th handle that was returned by #GNUNET_CORE_notify_transmit_ready().
1317  */
1318 void
1319 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1320 {
1321   struct PeerRecord *pr = th->peer;
1322   struct GNUNET_CORE_Handle *h;
1323
1324   GNUNET_assert (NULL != th);
1325   GNUNET_assert (NULL != pr);
1326   LOG (GNUNET_ERROR_TYPE_DEBUG,
1327        "Aborting transmission request to core for %u bytes to `%s'\n",
1328        (unsigned int) th->msize,
1329        GNUNET_i2s (&pr->peer));
1330   th->peer = NULL;
1331   h = pr->ch;
1332   if (NULL != th->cm)
1333   {
1334     /* we're currently in the control queue, remove */
1335     GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1336                                  h->control_pending_tail,
1337                                  th->cm);
1338     GNUNET_free (th->cm);
1339     th->cm = NULL;
1340   }
1341   if ( (NULL != pr->prev) ||
1342        (NULL != pr->next) ||
1343        (pr == h->ready_peer_head) )
1344   {
1345     /* the request that was 'approved' by core was
1346      * canceled before it could be transmitted; remove
1347      * us from the 'ready' list */
1348     GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1349                                  h->ready_peer_tail,
1350                                  pr);
1351   }
1352   if (NULL != pr->ntr_task)
1353   {
1354     GNUNET_SCHEDULER_cancel (pr->ntr_task);
1355     pr->ntr_task = NULL;
1356   }
1357 }
1358
1359
1360 /**
1361  * Check if the given peer is currently connected. This function is for special
1362  * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1363  * expected to track which peers are connected based on the connect/disconnect
1364  * callbacks from #GNUNET_CORE_connect().  This function is NOT part of the
1365  * 'versioned', 'official' API. The difference between this function and the
1366  * function GNUNET_CORE_is_peer_connected() is that this one returns
1367  * synchronously after looking in the CORE API cache. The function
1368  * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1369  * its response is given asynchronously.
1370  *
1371  * @param h the core handle
1372  * @param pid the identity of the peer to check if it has been connected to us
1373  * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
1374  */
1375 int
1376 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1377                                     const struct GNUNET_PeerIdentity *pid)
1378 {
1379   GNUNET_assert (NULL != h);
1380   GNUNET_assert (NULL != pid);
1381   return GNUNET_CONTAINER_multipeermap_contains (h->peers, pid);
1382 }
1383
1384
1385 /* end of core_api.c */