2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/core_api.c
23 * @brief core service; this is the main API for encrypted P2P
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_core_service.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "core-api",__VA_ARGS__)
37 * Handle for a transmission request.
39 struct GNUNET_CORE_TransmitHandle
43 * Corresponding peer record.
45 struct PeerRecord *peer;
48 * Corresponding SEND_REQUEST message. Only non-NULL
49 * while SEND_REQUEST message is pending.
51 struct ControlMessage *cm;
54 * Function that will be called to get the actual request
55 * (once we are ready to transmit this request to the core).
56 * The function will be called with a NULL buffer to signal
59 GNUNET_CONNECTION_TransmitReadyNotify get_message;
62 * Closure for get_message.
64 void *get_message_cls;
67 * Timeout for this handle.
69 struct GNUNET_TIME_Absolute timeout;
72 * How important is this message?
74 enum GNUNET_CORE_Priority priority;
77 * Size of this request.
82 * Send message request ID for this request.
95 * Information we track for each peer.
101 * We generally do NOT keep peer records in a DLL; this
102 * DLL is only used IF this peer's 'pending_head' message
103 * is ready for transmission.
105 struct PeerRecord *prev;
108 * We generally do NOT keep peer records in a DLL; this
109 * DLL is only used IF this peer's 'pending_head' message
110 * is ready for transmission.
112 struct PeerRecord *next;
115 * Peer the record is about.
117 struct GNUNET_PeerIdentity peer;
120 * Corresponding core handle.
122 struct GNUNET_CORE_Handle *ch;
125 * Pending request, if any. 'th->peer' is set to NULL if the
126 * request is not active.
128 struct GNUNET_CORE_TransmitHandle th;
131 * ID of timeout task for the 'pending_head' handle
132 * which is the one with the smallest timeout.
134 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
137 * ID of task to run 'next_request_transmission'.
139 GNUNET_SCHEDULER_TaskIdentifier ntr_task;
142 * SendMessageRequest ID generator for this peer.
150 struct GNUNET_PeerIdentity target;
151 struct GNUNET_CORE_Handle *core;
152 struct GNUNET_CORE_TransmitHandle *th;
157 * Type of function called upon completion.
160 * @param success GNUNET_OK on success (which for request_connect
161 * ONLY means that we transmitted the connect request to CORE,
162 * it does not mean that we are actually now connected!);
163 * GNUNET_NO on timeout,
164 * GNUNET_SYSERR if core was shut down
166 typedef void (*GNUNET_CORE_ControlContinuation) (void *cls, int success);
170 * Entry in a doubly-linked list of control messages to be transmitted
171 * to the core service. Control messages include traffic allocation,
172 * connection requests and of course our initial 'init' request.
174 * The actual message is allocated at the end of this struct.
176 struct ControlMessage
179 * This is a doubly-linked list.
181 struct ControlMessage *next;
184 * This is a doubly-linked list.
186 struct ControlMessage *prev;
189 * Function to run after transmission failed/succeeded.
191 GNUNET_CORE_ControlContinuation cont;
194 * Closure for 'cont'.
199 * Transmit handle (if one is associated with this ControlMessage), or NULL.
201 struct GNUNET_CORE_TransmitHandle *th;
207 * Context for the core service connection.
209 struct GNUNET_CORE_Handle
213 * Configuration we're using.
215 const struct GNUNET_CONFIGURATION_Handle *cfg;
218 * Closure for the various callbacks.
223 * Function to call once we've handshaked with the core service.
225 GNUNET_CORE_StartupCallback init;
228 * Function to call whenever we're notified about a peer connecting.
230 GNUNET_CORE_ConnectEventHandler connects;
233 * Function to call whenever we're notified about a peer disconnecting.
235 GNUNET_CORE_DisconnectEventHandler disconnects;
238 * Function to call whenever we receive an inbound message.
240 GNUNET_CORE_MessageCallback inbound_notify;
243 * Function to call whenever we receive an outbound message.
245 GNUNET_CORE_MessageCallback outbound_notify;
248 * Function handlers for messages of particular type.
250 const struct GNUNET_CORE_MessageHandler *handlers;
253 * Our connection to the service.
255 struct GNUNET_CLIENT_Connection *client;
258 * Handle for our current transmission request.
260 struct GNUNET_CLIENT_TransmitHandle *cth;
263 * Head of doubly-linked list of pending requests.
265 struct ControlMessage *control_pending_head;
268 * Tail of doubly-linked list of pending requests.
270 struct ControlMessage *control_pending_tail;
273 * Head of doubly-linked list of peers that are core-approved
274 * to send their next message.
276 struct PeerRecord *ready_peer_head;
279 * Tail of doubly-linked list of peers that are core-approved
280 * to send their next message.
282 struct PeerRecord *ready_peer_tail;
285 * Hash map listing all of the peers that we are currently
288 struct GNUNET_CONTAINER_MultiPeerMap *peers;
291 * Identity of this peer.
293 struct GNUNET_PeerIdentity me;
296 * ID of reconnect task (if any).
298 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
301 * Current delay we use for re-trying to connect to core.
303 struct GNUNET_TIME_Relative retry_backoff;
306 * Number of entries in the handlers array.
311 * For inbound notifications without a specific handler, do
312 * we expect to only receive headers?
314 int inbound_hdr_only;
317 * For outbound notifications without a specific handler, do
318 * we expect to only receive headers?
320 int outbound_hdr_only;
323 * Are we currently disconnected and hence unable to forward
332 * Our current client connection went down. Clean it up
333 * and try to reconnect!
335 * @param h our handle to the core service
338 reconnect (struct GNUNET_CORE_Handle *h);
342 * Task schedule to try to re-connect to core.
344 * @param cls the `struct GNUNET_CORE_Handle`
345 * @param tc task context
348 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
350 struct GNUNET_CORE_Handle *h = cls;
352 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
353 LOG (GNUNET_ERROR_TYPE_DEBUG,
354 "Connecting to CORE service after delay\n");
360 * Notify clients about disconnect and free
361 * the entry for connected peer.
363 * @param cls the `struct GNUNET_CORE_Handle *`
364 * @param key the peer identity (not used)
365 * @param value the `struct PeerRecord` to free.
366 * @return #GNUNET_YES (continue)
369 disconnect_and_free_peer_entry (void *cls,
370 const struct GNUNET_PeerIdentity *key,
373 struct GNUNET_CORE_Handle *h = cls;
374 struct GNUNET_CORE_TransmitHandle *th;
375 struct PeerRecord *pr = value;
377 if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
379 GNUNET_SCHEDULER_cancel (pr->timeout_task);
380 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
382 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
384 GNUNET_SCHEDULER_cancel (pr->ntr_task);
385 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
387 if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
388 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
389 if (NULL != h->disconnects)
390 h->disconnects (h->cls, &pr->peer);
391 /* all requests should have been cancelled, clean up anyway, just in case */
393 if (NULL != th->peer)
400 /* done with 'voluntary' cleanups, now on to normal freeing */
401 GNUNET_assert (GNUNET_YES ==
402 GNUNET_CONTAINER_multipeermap_remove (h->peers, key, pr));
403 GNUNET_assert (pr->ch == h);
404 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->timeout_task);
405 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
412 * Close down any existing connection to the CORE service and
413 * try re-establishing it later.
415 * @param h our handle
418 reconnect_later (struct GNUNET_CORE_Handle *h)
420 struct ControlMessage *cm;
421 struct PeerRecord *pr;
423 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
426 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
429 if (NULL != h->client)
431 GNUNET_CLIENT_disconnect (h->client);
434 h->currently_down = GNUNET_YES;
435 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
437 GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
439 while (NULL != (cm = h->control_pending_head))
441 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
442 h->control_pending_tail, cm);
445 if (NULL != cm->cont)
446 cm->cont (cm->cont_cls, GNUNET_NO);
449 GNUNET_CONTAINER_multipeermap_iterate (h->peers,
450 &disconnect_and_free_peer_entry, h);
451 while (NULL != (pr = h->ready_peer_head))
452 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
453 GNUNET_assert (h->control_pending_head == NULL);
454 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
459 * Check the list of pending requests, send the next
462 * @param h core handle
463 * @param ignore_currently_down transmit message even if not initialized?
466 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
470 * The given request hit its timeout. Remove from the
471 * doubly-linked list and call the respective continuation.
473 * @param cls the transmit handle of the request that timed out
474 * @param tc context, can be NULL (!)
477 transmission_timeout (void *cls,
478 const struct GNUNET_SCHEDULER_TaskContext *tc);
482 * Send a control message to the peer asking for transmission
483 * of the message in the given peer record.
485 * @param pr peer to request transmission to
488 request_next_transmission (struct PeerRecord *pr)
490 struct GNUNET_CORE_Handle *h = pr->ch;
491 struct ControlMessage *cm;
492 struct SendMessageRequest *smr;
493 struct GNUNET_CORE_TransmitHandle *th;
495 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
497 GNUNET_SCHEDULER_cancel (pr->timeout_task);
498 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
501 if (NULL == th->peer)
503 trigger_next_request (h, GNUNET_NO);
507 return; /* already done */
508 GNUNET_assert (pr->prev == NULL);
509 GNUNET_assert (pr->next == NULL);
511 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
512 (th->timeout), &transmission_timeout, pr);
513 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
514 sizeof (struct SendMessageRequest));
517 smr = (struct SendMessageRequest *) &cm[1];
518 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
519 smr->header.size = htons (sizeof (struct SendMessageRequest));
520 smr->priority = htonl ((uint32_t) th->priority);
521 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
522 smr->peer = pr->peer;
523 smr->reserved = htonl (0);
524 smr->size = htons (th->msize);
525 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
526 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
527 h->control_pending_tail, cm);
528 LOG (GNUNET_ERROR_TYPE_DEBUG,
529 "Adding SEND REQUEST for peer `%s' to message queue\n",
530 GNUNET_i2s (&pr->peer));
531 trigger_next_request (h, GNUNET_NO);
536 * The given request hit its timeout. Remove from the
537 * doubly-linked list and call the respective continuation.
539 * @param cls the transmit handle of the request that timed out
540 * @param tc context, can be NULL (!)
543 transmission_timeout (void *cls,
544 const struct GNUNET_SCHEDULER_TaskContext *tc)
546 struct PeerRecord *pr = cls;
547 struct GNUNET_CORE_Handle *h = pr->ch;
548 struct GNUNET_CORE_TransmitHandle *th;
550 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
551 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
553 GNUNET_SCHEDULER_cancel (pr->ntr_task);
554 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
558 if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
560 /* the request that was 'approved' by core was
561 * canceled before it could be transmitted; remove
562 * us from the 'ready' list */
563 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
567 /* we're currently in the control queue, remove */
568 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
569 h->control_pending_tail, th->cm);
570 GNUNET_free (th->cm);
572 LOG (GNUNET_ERROR_TYPE_DEBUG,
573 "Signalling timeout of request for transmission to peer `%s' via CORE\n",
574 GNUNET_i2s (&pr->peer));
575 trigger_next_request (h, GNUNET_NO);
577 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
582 * Transmit the next message to the core service.
584 * @param cls closure with the `struct GNUNET_CORE_Handle`
585 * @param size number of bytes available in @a buf
586 * @param buf where the callee should write the message
587 * @return number of bytes written to @a buf
590 transmit_message (void *cls, size_t size, void *buf)
592 struct GNUNET_CORE_Handle *h = cls;
593 struct ControlMessage *cm;
594 struct GNUNET_CORE_TransmitHandle *th;
595 struct PeerRecord *pr;
596 struct SendMessage *sm;
597 const struct GNUNET_MessageHeader *hdr;
601 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
605 LOG (GNUNET_ERROR_TYPE_DEBUG,
606 "Transmission failed, initiating reconnect\n");
610 /* first check for control messages */
611 if (NULL != (cm = h->control_pending_head))
613 hdr = (const struct GNUNET_MessageHeader *) &cm[1];
614 msize = ntohs (hdr->size);
617 trigger_next_request (h, GNUNET_NO);
620 LOG (GNUNET_ERROR_TYPE_DEBUG,
621 "Transmitting control message with %u bytes of type %u to core.\n",
622 (unsigned int) msize, (unsigned int) ntohs (hdr->type));
623 memcpy (buf, hdr, msize);
624 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
625 h->control_pending_tail, cm);
628 if (NULL != cm->cont)
629 cm->cont (cm->cont_cls, GNUNET_OK);
631 trigger_next_request (h, GNUNET_NO);
634 /* now check for 'ready' P2P messages */
635 if (NULL == (pr = h->ready_peer_head))
637 GNUNET_assert (NULL != pr->th.peer);
639 if (size < th->msize + sizeof (struct SendMessage))
641 trigger_next_request (h, GNUNET_NO);
644 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
646 if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
648 GNUNET_SCHEDULER_cancel (pr->timeout_task);
649 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
651 LOG (GNUNET_ERROR_TYPE_DEBUG,
652 "Transmitting SEND request to `%s' with %u bytes.\n",
653 GNUNET_i2s (&pr->peer), (unsigned int) th->msize);
654 sm = (struct SendMessage *) buf;
655 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
656 sm->priority = htonl ((uint32_t) th->priority);
657 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
659 sm->cork = htonl ((uint32_t) th->cork);
660 sm->reserved = htonl (0);
662 th->get_message (th->get_message_cls,
663 size - sizeof (struct SendMessage), &sm[1]);
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Transmitting SEND request to `%s' yielded %u bytes.\n",
667 GNUNET_i2s (&pr->peer), ret);
670 LOG (GNUNET_ERROR_TYPE_DEBUG,
671 "Size of clients message to peer %s is 0!\n",
672 GNUNET_i2s (&pr->peer));
673 /* client decided to send nothing! */
674 request_next_transmission (pr);
677 LOG (GNUNET_ERROR_TYPE_DEBUG,
678 "Produced SEND message to core with %u bytes payload\n",
680 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
681 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
684 request_next_transmission (pr);
687 ret += sizeof (struct SendMessage);
688 sm->header.size = htons (ret);
689 GNUNET_assert (ret <= size);
690 request_next_transmission (pr);
696 * Check the list of pending requests, send the next
699 * @param h core handle
700 * @param ignore_currently_down transmit message even if not initialized?
703 trigger_next_request (struct GNUNET_CORE_Handle *h,
704 int ignore_currently_down)
708 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
710 LOG (GNUNET_ERROR_TYPE_DEBUG,
711 "Core connection down, not processing queue\n");
716 LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
719 if (NULL != h->control_pending_head)
721 ntohs (((struct GNUNET_MessageHeader *) &h->
722 control_pending_head[1])->size);
723 else if (h->ready_peer_head != NULL)
725 h->ready_peer_head->th.msize + sizeof (struct SendMessage);
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Request queue empty, not processing queue\n");
730 return; /* no pending message */
733 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
734 GNUNET_TIME_UNIT_FOREVER_REL,
735 GNUNET_NO, &transmit_message, h);
740 * Handler for notification messages received from the core.
742 * @param cls our `struct GNUNET_CORE_Handle`
743 * @param msg the message received from the core service
746 main_notify_handler (void *cls,
747 const struct GNUNET_MessageHeader *msg)
749 struct GNUNET_CORE_Handle *h = cls;
750 const struct InitReplyMessage *m;
751 const struct ConnectNotifyMessage *cnm;
752 const struct DisconnectNotifyMessage *dnm;
753 const struct NotifyTrafficMessage *ntm;
754 const struct GNUNET_MessageHeader *em;
755 const struct SendMessageReady *smr;
756 const struct GNUNET_CORE_MessageHandler *mh;
757 GNUNET_CORE_StartupCallback init;
758 struct PeerRecord *pr;
759 struct GNUNET_CORE_TransmitHandle *th;
766 LOG (GNUNET_ERROR_TYPE_INFO,
767 _("Client was disconnected from core service, trying to reconnect.\n"));
771 msize = ntohs (msg->size);
772 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "Processing message of type %u and size %u from core service\n",
774 ntohs (msg->type), msize);
775 switch (ntohs (msg->type))
777 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
778 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
784 m = (const struct InitReplyMessage *) msg;
785 GNUNET_break (0 == ntohl (m->reserved));
786 /* start our message processing loop */
787 if (GNUNET_YES == h->currently_down)
789 h->currently_down = GNUNET_NO;
790 trigger_next_request (h, GNUNET_NO);
792 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
793 h->me = m->my_identity;
794 if (NULL != (init = h->init))
796 /* mark so we don't call init on reconnect */
798 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to core service of peer `%s'.\n",
799 GNUNET_i2s (&h->me));
800 init (h->cls, &h->me);
804 LOG (GNUNET_ERROR_TYPE_DEBUG,
805 "Successfully reconnected to core service.\n");
807 /* fake 'connect to self' */
808 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &h->me);
809 GNUNET_assert (NULL == pr);
810 pr = GNUNET_new (struct PeerRecord);
813 GNUNET_assert (GNUNET_YES ==
814 GNUNET_CONTAINER_multipeermap_put (h->peers,
816 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
817 if (NULL != h->connects)
818 h->connects (h->cls, &h->me);
820 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
821 if (msize < sizeof (struct ConnectNotifyMessage))
827 cnm = (const struct ConnectNotifyMessage *) msg;
829 sizeof (struct ConnectNotifyMessage))
835 LOG (GNUNET_ERROR_TYPE_DEBUG,
836 "Received notification about connection from `%s'.\n",
837 GNUNET_i2s (&cnm->peer));
838 if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
840 /* connect to self!? */
844 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &cnm->peer);
851 pr = GNUNET_new (struct PeerRecord);
852 pr->peer = cnm->peer;
854 GNUNET_assert (GNUNET_YES ==
855 GNUNET_CONTAINER_multipeermap_put (h->peers,
857 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
858 if (NULL != h->connects)
859 h->connects (h->cls, &cnm->peer);
861 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
862 if (msize != sizeof (struct DisconnectNotifyMessage))
868 dnm = (const struct DisconnectNotifyMessage *) msg;
869 if (0 == memcmp (&h->me, &dnm->peer, sizeof (struct GNUNET_PeerIdentity)))
871 /* connection to self!? */
875 GNUNET_break (0 == ntohl (dnm->reserved));
876 LOG (GNUNET_ERROR_TYPE_DEBUG,
877 "Received notification about disconnect from `%s'.\n",
878 GNUNET_i2s (&dnm->peer));
879 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &dnm->peer);
886 trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
887 (h->ready_peer_head == pr));
888 disconnect_and_free_peer_entry (h, &dnm->peer, pr);
890 trigger_next_request (h, GNUNET_NO);
892 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
893 if (msize < sizeof (struct NotifyTrafficMessage))
899 ntm = (const struct NotifyTrafficMessage *) msg;
901 sizeof (struct NotifyTrafficMessage) +
902 sizeof (struct GNUNET_MessageHeader)) )
908 em = (const struct GNUNET_MessageHeader *) &ntm[1];
909 LOG (GNUNET_ERROR_TYPE_DEBUG,
910 "Received message of type %u and size %u from peer `%4s'\n",
911 ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
912 if ((GNUNET_NO == h->inbound_hdr_only) &&
914 ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
920 et = ntohs (em->type);
921 for (hpos = 0; hpos < h->hcnt; hpos++)
923 mh = &h->handlers[hpos];
926 if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
928 LOG (GNUNET_ERROR_TYPE_ERROR,
929 "Unexpected message size %u for message of type %u from peer `%4s'\n",
930 htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
934 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &ntm->peer);
942 h->handlers[hpos].callback (h->cls, &ntm->peer, em))
944 /* error in processing, do not process other messages! */
948 if (NULL != h->inbound_notify)
949 h->inbound_notify (h->cls, &ntm->peer, em);
951 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
952 if (msize < sizeof (struct NotifyTrafficMessage))
958 ntm = (const struct NotifyTrafficMessage *) msg;
960 sizeof (struct NotifyTrafficMessage) +
961 sizeof (struct GNUNET_MessageHeader)) )
967 em = (const struct GNUNET_MessageHeader *) &ntm[1];
968 LOG (GNUNET_ERROR_TYPE_DEBUG,
969 "Received notification about transmission to `%s'.\n",
970 GNUNET_i2s (&ntm->peer));
971 if ((GNUNET_NO == h->outbound_hdr_only) &&
973 ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
979 if (NULL == h->outbound_notify)
984 h->outbound_notify (h->cls, &ntm->peer, em);
986 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
987 if (msize != sizeof (struct SendMessageReady))
993 smr = (const struct SendMessageReady *) msg;
994 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &smr->peer);
1001 LOG (GNUNET_ERROR_TYPE_DEBUG,
1002 "Received notification about transmission readiness to `%s'.\n",
1003 GNUNET_i2s (&smr->peer));
1004 if (NULL == pr->th.peer)
1006 /* request must have been cancelled between the original request
1007 * and the response from core, ignore core's readiness */
1012 if (ntohs (smr->smr_id) != th->smr_id)
1014 /* READY message is for expired or cancelled message,
1015 * ignore! (we should have already sent another request) */
1018 if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
1020 /* we should not already be on the ready list... */
1022 reconnect_later (h);
1025 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head, h->ready_peer_tail, pr);
1026 trigger_next_request (h, GNUNET_NO);
1029 reconnect_later (h);
1032 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1033 GNUNET_TIME_UNIT_FOREVER_REL);
1038 * Task executed once we are done transmitting the INIT message.
1039 * Starts our 'receive' loop.
1041 * @param cls the 'struct GNUNET_CORE_Handle'
1042 * @param success were we successful
1045 init_done_task (void *cls, int success)
1047 struct GNUNET_CORE_Handle *h = cls;
1049 if (GNUNET_SYSERR == success)
1050 return; /* shutdown */
1051 if (GNUNET_NO == success)
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 "Failed to exchange INIT with core, retrying\n");
1055 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1056 reconnect_later (h);
1059 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1060 GNUNET_TIME_UNIT_FOREVER_REL);
1065 * Our current client connection went down. Clean it up
1066 * and try to reconnect!
1068 * @param h our handle to the core service
1071 reconnect (struct GNUNET_CORE_Handle *h)
1073 struct ControlMessage *cm;
1074 struct InitMessage *init;
1080 GNUNET_assert (NULL == h->client);
1081 GNUNET_assert (GNUNET_YES == h->currently_down);
1082 GNUNET_assert (NULL != h->cfg);
1083 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1084 if (NULL == h->client)
1086 reconnect_later (h);
1089 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1090 cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1091 cm->cont = &init_done_task;
1093 init = (struct InitMessage *) &cm[1];
1094 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1095 init->header.size = htons (msize);
1097 if (h->inbound_notify != NULL)
1099 if (h->inbound_hdr_only)
1100 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1102 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1104 if (h->outbound_notify != NULL)
1106 if (h->outbound_hdr_only)
1107 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1109 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1111 LOG (GNUNET_ERROR_TYPE_INFO,
1112 "(Re)connecting to CORE service, monitoring messages of type %u\n",
1115 init->options = htonl (opt);
1116 ts = (uint16_t *) & init[1];
1117 for (hpos = 0; hpos < h->hcnt; hpos++)
1118 ts[hpos] = htons (h->handlers[hpos].type);
1119 GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1121 trigger_next_request (h, GNUNET_YES);
1127 * Connect to the core service. Note that the connection may
1128 * complete (or fail) asynchronously.
1130 * @param cfg configuration to use
1131 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1132 * @param init callback to call once we have successfully
1133 * connected to the core service
1134 * @param connects function to call on peer connect, can be NULL
1135 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1136 * @param inbound_notify function to call for all inbound messages, can be NULL
1137 * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
1138 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1139 * can be used to improve efficiency, ignored if @a inbound_notify is NULLL
1140 * @param outbound_notify function to call for all outbound messages, can be NULL
1141 * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
1142 * GNUNET_MessageHeader and hence we do not need to give it the full message
1143 * can be used to improve efficiency, ignored if @a outbound_notify is NULLL
1144 * @param handlers callbacks for messages we care about, NULL-terminated
1145 * @return handle to the core service (only useful for disconnect until 'init' is called);
1146 * NULL on error (in this case, init is never called)
1148 struct GNUNET_CORE_Handle *
1149 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1151 GNUNET_CORE_StartupCallback init,
1152 GNUNET_CORE_ConnectEventHandler connects,
1153 GNUNET_CORE_DisconnectEventHandler disconnects,
1154 GNUNET_CORE_MessageCallback inbound_notify,
1155 int inbound_hdr_only,
1156 GNUNET_CORE_MessageCallback outbound_notify,
1157 int outbound_hdr_only,
1158 const struct GNUNET_CORE_MessageHandler *handlers)
1160 struct GNUNET_CORE_Handle *h;
1162 GNUNET_assert (NULL != cfg);
1163 h = GNUNET_new (struct GNUNET_CORE_Handle);
1167 h->connects = connects;
1168 h->disconnects = disconnects;
1169 h->inbound_notify = inbound_notify;
1170 h->outbound_notify = outbound_notify;
1171 h->inbound_hdr_only = inbound_hdr_only;
1172 h->outbound_hdr_only = outbound_hdr_only;
1173 h->handlers = handlers;
1175 h->currently_down = GNUNET_YES;
1176 h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1177 if (NULL != handlers)
1178 while (handlers[h->hcnt].callback != NULL)
1180 GNUNET_assert (h->hcnt <
1181 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1182 sizeof (struct InitMessage)) / sizeof (uint16_t));
1183 LOG (GNUNET_ERROR_TYPE_DEBUG,
1184 "Connecting to CORE service\n");
1191 * Disconnect from the core service. This function can only
1192 * be called *after* all pending #GNUNET_CORE_notify_transmit_ready()
1193 * requests have been explicitly canceled.
1195 * @param handle connection to core to disconnect
1198 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1200 struct ControlMessage *cm;
1202 GNUNET_assert (NULL != handle);
1204 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1205 if (NULL != handle->cth)
1207 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1210 while (NULL != (cm = handle->control_pending_head))
1212 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1213 handle->control_pending_tail, cm);
1216 if (NULL != cm->cont)
1217 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1220 if (NULL != handle->client)
1222 GNUNET_CLIENT_disconnect (handle->client);
1223 handle->client = NULL;
1225 GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
1226 &disconnect_and_free_peer_entry,
1228 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1230 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1231 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1233 GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
1234 handle->peers = NULL;
1235 GNUNET_break (handle->ready_peer_head == NULL);
1236 GNUNET_free (handle);
1241 * Task that calls 'request_next_transmission'.
1243 * @param cls the 'struct PeerRecord *'
1244 * @param tc scheduler context
1247 run_request_next_transmission (void *cls,
1248 const struct GNUNET_SCHEDULER_TaskContext *tc)
1250 struct PeerRecord *pr = cls;
1252 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1253 request_next_transmission (pr);
1258 * Ask the core to call @a notify once it is ready to transmit the
1259 * given number of bytes to the specified @a target. Must only be
1260 * called after a connection to the respective peer has been
1261 * established (and the client has been informed about this). You may
1262 * have one request of this type pending for each connected peer at
1263 * any time. If a peer disconnects, the application MUST call
1264 * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
1265 * transmission request, if one such request is pending.
1267 * @param handle connection to core service
1268 * @param cork is corking allowed for this transmission?
1269 * @param priority how important is the message?
1270 * @param maxdelay how long can the message wait? Only effective if @a cork is #GNUNET_YES
1271 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1272 * @param notify_size how many bytes of buffer space does @a notify want?
1273 * @param notify function to call when buffer space is available;
1274 * will be called with NULL on timeout; clients MUST cancel
1275 * all pending transmission requests DURING the disconnect
1277 * @param notify_cls closure for notify
1278 * @return non-NULL if the notify callback was queued,
1279 * NULL if we can not even queue the request (request already pending);
1280 * if NULL is returned, @a notify will NOT be called.
1282 struct GNUNET_CORE_TransmitHandle *
1283 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1285 enum GNUNET_CORE_Priority priority,
1286 struct GNUNET_TIME_Relative maxdelay,
1287 const struct GNUNET_PeerIdentity *target,
1289 GNUNET_CONNECTION_TransmitReadyNotify notify,
1292 struct PeerRecord *pr;
1293 struct GNUNET_CORE_TransmitHandle *th;
1295 if (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1300 GNUNET_assert (NULL != notify);
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 "Asking core for transmission of %u bytes to `%s'\n",
1303 (unsigned int) notify_size,
1304 GNUNET_i2s (target));
1305 pr = GNUNET_CONTAINER_multipeermap_get (handle->peers, target);
1308 /* attempt to send to peer that is not connected */
1312 if (NULL != pr->th.peer)
1314 /* attempting to queue a second request for the same destination */
1318 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1319 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1321 memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1323 th->get_message = notify;
1324 th->get_message_cls = notify_cls;
1325 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1326 th->priority = priority;
1327 th->msize = notify_size;
1329 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
1331 GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "Transmission request added to queue\n");
1339 * Cancel the specified transmission-ready notification.
1341 * @param th handle that was returned by "notify_transmit_ready".
1344 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1346 struct PeerRecord *pr = th->peer;
1347 struct GNUNET_CORE_Handle *h;
1349 GNUNET_assert (NULL != th);
1350 GNUNET_assert (NULL != pr);
1351 LOG (GNUNET_ERROR_TYPE_DEBUG,
1352 "Aborting transmission request to core for %u bytes to `%s'\n",
1353 (unsigned int) th->msize,
1354 GNUNET_i2s (&pr->peer));
1359 /* we're currently in the control queue, remove */
1360 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1361 h->control_pending_tail, th->cm);
1362 GNUNET_free (th->cm);
1365 if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
1367 /* the request that was 'approved' by core was
1368 * canceled before it could be transmitted; remove
1369 * us from the 'ready' list */
1370 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1372 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
1374 GNUNET_SCHEDULER_cancel (pr->ntr_task);
1375 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1381 * Check if the given peer is currently connected. This function is for special
1382 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1383 * expected to track which peers are connected based on the connect/disconnect
1384 * callbacks from GNUNET_CORE_connect. This function is NOT part of the
1385 * 'versioned', 'official' API. The difference between this function and the
1386 * function GNUNET_CORE_is_peer_connected() is that this one returns
1387 * synchronously after looking in the CORE API cache. The function
1388 * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1389 * its response is given asynchronously.
1391 * @param h the core handle
1392 * @param pid the identity of the peer to check if it has been connected to us
1393 * @return GNUNET_YES if the peer is connected to us; GNUNET_NO if not
1396 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1397 const struct GNUNET_PeerIdentity *pid)
1399 GNUNET_assert (NULL != h);
1400 GNUNET_assert (NULL != pid);
1401 return GNUNET_CONTAINER_multipeermap_contains (h->peers, pid);
1406 * Function called to notify a client about the connection
1407 * begin ready to queue more data. "buf" will be
1408 * NULL and "size" zero if the connection was closed for
1409 * writing in the meantime.
1411 * @param cls closure
1412 * @param size number of bytes available in buf
1413 * @param buf where the callee should write the message
1414 * @return number of bytes written to buf
1417 core_mq_ntr (void *cls, size_t size,
1420 struct GNUNET_MQ_Handle *mq = cls;
1421 struct CoreMQState *mqs = GNUNET_MQ_impl_state (mq);
1422 const struct GNUNET_MessageHeader *mh = GNUNET_MQ_impl_current (mq);
1423 size_t msg_size = ntohs (mh->size);
1424 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "core-mq", "ntr called (size %u, type %u)\n",
1425 msg_size, ntohs (mh->type));
1429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "core-mq", "send error\n");
1430 GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
1433 memcpy (buf, mh, msg_size);
1434 GNUNET_MQ_impl_send_continue (mq);
1440 * Signature of functions implementing the
1441 * sending functionality of a message queue.
1443 * @param mq the message queue
1444 * @param msg the message to send
1445 * @param impl_state state of the implementation
1448 core_mq_send (struct GNUNET_MQ_Handle *mq,
1449 const struct GNUNET_MessageHeader *msg,
1452 struct CoreMQState *mqs = impl_state;
1453 GNUNET_assert (NULL == mqs->th);
1454 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "core-mq", "Sending queued message (size %u)\n",
1456 mqs->th = GNUNET_CORE_notify_transmit_ready (mqs->core, GNUNET_YES, 0,
1457 GNUNET_TIME_UNIT_FOREVER_REL,
1459 ntohs (msg->size), core_mq_ntr, mq);
1464 * Signature of functions implementing the
1465 * destruction of a message queue.
1466 * Implementations must not free @a mq, but should
1467 * take care of @a impl_state.
1469 * @param mq the message queue to destroy
1470 * @param impl_state state of the implementation
1473 core_mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
1475 struct CoreMQState *mqs = impl_state;
1476 if (NULL != mqs->th)
1478 GNUNET_CORE_notify_transmit_ready_cancel (mqs->th);
1486 * Implementation function that cancels the currently sent message.
1488 * @param mq message queue
1489 * @param impl_state state specific to the implementation
1492 core_mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
1494 struct CoreMQState *mqs = impl_state;
1495 GNUNET_assert (NULL != mqs->th);
1496 GNUNET_CORE_notify_transmit_ready_cancel (mqs->th);
1501 * Create a message queue for sending messages to a peer with CORE.
1502 * Messages may only be queued with #GNUNET_MQ_send once the init callback has
1503 * been called for the given handle.
1504 * There must only be one queue per peer for each core handle.
1505 * The message queue can only be used to transmit messages,
1506 * not to receive them.
1508 * @param h the core handle
1509 * @param target the target peer for this queue, may not be NULL
1510 * @return a message queue for sending messages over the core handle
1511 * to the target peer
1513 struct GNUNET_MQ_Handle *
1514 GNUNET_CORE_mq_create (struct GNUNET_CORE_Handle *h,
1515 const struct GNUNET_PeerIdentity *target)
1517 struct CoreMQState *mqs = GNUNET_new (struct CoreMQState);
1519 mqs->target = *target;
1520 return GNUNET_MQ_queue_for_callbacks (core_mq_send, core_mq_destroy,
1521 core_mq_cancel, mqs,
1525 /* end of core_api.c */