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_constants.h"
29 #include "gnunet_core_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "core-api",__VA_ARGS__)
36 * Handle for a transmission request.
38 struct GNUNET_CORE_TransmitHandle
42 * Corresponding peer record.
44 struct PeerRecord *peer;
47 * Corresponding SEND_REQUEST message. Only non-NULL
48 * while SEND_REQUEST message is pending.
50 struct ControlMessage *cm;
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
58 GNUNET_CONNECTION_TransmitReadyNotify get_message;
61 * Closure for get_message.
63 void *get_message_cls;
66 * Timeout for this handle.
68 struct GNUNET_TIME_Absolute timeout;
71 * How important is this message?
76 * Size of this request.
81 * Send message request ID for this request.
94 * Information we track for each peer.
100 * We generally do NOT keep peer records in a DLL; this
101 * DLL is only used IF this peer's 'pending_head' message
102 * is ready for transmission.
104 struct PeerRecord *prev;
107 * We generally do NOT keep peer records in a DLL; this
108 * DLL is only used IF this peer's 'pending_head' message
109 * is ready for transmission.
111 struct PeerRecord *next;
114 * Peer the record is about.
116 struct GNUNET_PeerIdentity peer;
119 * Corresponding core handle.
121 struct GNUNET_CORE_Handle *ch;
124 * Pending request, if any. 'th->peer' is set to NULL if the
125 * request is not active.
127 struct GNUNET_CORE_TransmitHandle th;
130 * ID of timeout task for the 'pending_head' handle
131 * which is the one with the smallest timeout.
133 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
136 * ID of task to run 'next_request_transmission'.
138 GNUNET_SCHEDULER_TaskIdentifier ntr_task;
141 * SendMessageRequest ID generator for this peer.
149 * Type of function called upon completion.
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
158 typedef void (*GNUNET_CORE_ControlContinuation) (void *cls, int success);
162 * Entry in a doubly-linked list of control messages to be transmitted
163 * to the core service. Control messages include traffic allocation,
164 * connection requests and of course our initial 'init' request.
166 * The actual message is allocated at the end of this struct.
168 struct ControlMessage
171 * This is a doubly-linked list.
173 struct ControlMessage *next;
176 * This is a doubly-linked list.
178 struct ControlMessage *prev;
181 * Function to run after transmission failed/succeeded.
183 GNUNET_CORE_ControlContinuation cont;
186 * Closure for 'cont'.
191 * Transmit handle (if one is associated with this ControlMessage), or NULL.
193 struct GNUNET_CORE_TransmitHandle *th;
199 * Context for the core service connection.
201 struct GNUNET_CORE_Handle
205 * Configuration we're using.
207 const struct GNUNET_CONFIGURATION_Handle *cfg;
210 * Closure for the various callbacks.
215 * Function to call once we've handshaked with the core service.
217 GNUNET_CORE_StartupCallback init;
220 * Function to call whenever we're notified about a peer connecting.
222 GNUNET_CORE_ConnectEventHandler connects;
225 * Function to call whenever we're notified about a peer disconnecting.
227 GNUNET_CORE_DisconnectEventHandler disconnects;
230 * Function to call whenever we receive an inbound message.
232 GNUNET_CORE_MessageCallback inbound_notify;
235 * Function to call whenever we receive an outbound message.
237 GNUNET_CORE_MessageCallback outbound_notify;
240 * Function handlers for messages of particular type.
242 const struct GNUNET_CORE_MessageHandler *handlers;
245 * Our connection to the service.
247 struct GNUNET_CLIENT_Connection *client;
250 * Handle for our current transmission request.
252 struct GNUNET_CLIENT_TransmitHandle *cth;
255 * Head of doubly-linked list of pending requests.
257 struct ControlMessage *control_pending_head;
260 * Tail of doubly-linked list of pending requests.
262 struct ControlMessage *control_pending_tail;
265 * Head of doubly-linked list of peers that are core-approved
266 * to send their next message.
268 struct PeerRecord *ready_peer_head;
271 * Tail of doubly-linked list of peers that are core-approved
272 * to send their next message.
274 struct PeerRecord *ready_peer_tail;
277 * Hash map listing all of the peers that we are currently
280 struct GNUNET_CONTAINER_MultiHashMap *peers;
283 * Identity of this peer.
285 struct GNUNET_PeerIdentity me;
288 * ID of reconnect task (if any).
290 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
293 * Current delay we use for re-trying to connect to core.
295 struct GNUNET_TIME_Relative retry_backoff;
298 * Number of entries in the handlers array.
303 * For inbound notifications without a specific handler, do
304 * we expect to only receive headers?
306 int inbound_hdr_only;
309 * For outbound notifications without a specific handler, do
310 * we expect to only receive headers?
312 int outbound_hdr_only;
315 * Are we currently disconnected and hence unable to forward
324 * Our current client connection went down. Clean it up
325 * and try to reconnect!
327 * @param h our handle to the core service
330 reconnect (struct GNUNET_CORE_Handle *h);
334 * Task schedule to try to re-connect to core.
336 * @param cls the 'struct GNUNET_CORE_Handle'
337 * @param tc task context
340 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct GNUNET_CORE_Handle *h = cls;
344 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
345 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service after delay\n");
351 * Notify clients about disconnect and free
352 * the entry for connected peer.
354 * @param cls the 'struct GNUNET_CORE_Handle*'
355 * @param key the peer identity (not used)
356 * @param value the 'struct PeerRecord' to free.
357 * @return GNUNET_YES (continue)
360 disconnect_and_free_peer_entry (void *cls, const struct GNUNET_HashCode * key,
363 struct GNUNET_CORE_Handle *h = cls;
364 struct GNUNET_CORE_TransmitHandle *th;
365 struct PeerRecord *pr = value;
367 if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
369 GNUNET_SCHEDULER_cancel (pr->timeout_task);
370 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
372 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
374 GNUNET_SCHEDULER_cancel (pr->ntr_task);
375 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
377 if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
378 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
379 if (NULL != h->disconnects)
380 h->disconnects (h->cls, &pr->peer);
381 /* all requests should have been cancelled, clean up anyway, just in case */
383 if (NULL != th->peer)
390 /* done with 'voluntary' cleanups, now on to normal freeing */
391 GNUNET_assert (GNUNET_YES ==
392 GNUNET_CONTAINER_multihashmap_remove (h->peers, key, pr));
393 GNUNET_assert (pr->ch == h);
394 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->timeout_task);
395 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
402 * Close down any existing connection to the CORE service and
403 * try re-establishing it later.
405 * @param h our handle
408 reconnect_later (struct GNUNET_CORE_Handle *h)
410 struct ControlMessage *cm;
411 struct PeerRecord *pr;
413 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
416 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
419 if (NULL != h->client)
421 GNUNET_CLIENT_disconnect (h->client);
424 h->currently_down = GNUNET_YES;
425 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
427 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_task, h);
428 while (NULL != (cm = h->control_pending_head))
430 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
431 h->control_pending_tail, cm);
434 if (NULL != cm->cont)
435 cm->cont (cm->cont_cls, GNUNET_NO);
438 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
439 &disconnect_and_free_peer_entry, h);
440 while (NULL != (pr = h->ready_peer_head))
441 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
442 GNUNET_assert (h->control_pending_head == NULL);
443 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
448 * Check the list of pending requests, send the next
451 * @param h core handle
452 * @param ignore_currently_down transmit message even if not initialized?
455 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
459 * The given request hit its timeout. Remove from the
460 * doubly-linked list and call the respective continuation.
462 * @param cls the transmit handle of the request that timed out
463 * @param tc context, can be NULL (!)
466 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
470 * Send a control message to the peer asking for transmission
471 * of the message in the given peer record.
473 * @param pr peer to request transmission to
476 request_next_transmission (struct PeerRecord *pr)
478 struct GNUNET_CORE_Handle *h = pr->ch;
479 struct ControlMessage *cm;
480 struct SendMessageRequest *smr;
481 struct GNUNET_CORE_TransmitHandle *th;
483 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
485 GNUNET_SCHEDULER_cancel (pr->timeout_task);
486 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
489 if (NULL == th->peer)
491 trigger_next_request (h, GNUNET_NO);
495 return; /* already done */
496 GNUNET_assert (pr->prev == NULL);
497 GNUNET_assert (pr->next == NULL);
499 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
500 (th->timeout), &transmission_timeout, pr);
501 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
502 sizeof (struct SendMessageRequest));
505 smr = (struct SendMessageRequest *) &cm[1];
506 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
507 smr->header.size = htons (sizeof (struct SendMessageRequest));
508 smr->priority = htonl (th->priority);
509 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
510 smr->peer = pr->peer;
511 smr->reserved = htonl (0);
512 smr->size = htons (th->msize);
513 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
514 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
515 h->control_pending_tail, cm);
516 LOG (GNUNET_ERROR_TYPE_DEBUG,
517 "Adding SEND REQUEST for peer `%s' to message queue\n",
518 GNUNET_i2s (&pr->peer));
519 trigger_next_request (h, GNUNET_NO);
524 * The given request hit its timeout. Remove from the
525 * doubly-linked list and call the respective continuation.
527 * @param cls the transmit handle of the request that timed out
528 * @param tc context, can be NULL (!)
531 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
533 struct PeerRecord *pr = cls;
534 struct GNUNET_CORE_Handle *h = pr->ch;
535 struct GNUNET_CORE_TransmitHandle *th;
537 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
538 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
540 GNUNET_SCHEDULER_cancel (pr->ntr_task);
541 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
545 if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
547 /* the request that was 'approved' by core was
548 * canceled before it could be transmitted; remove
549 * us from the 'ready' list */
550 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
554 /* we're currently in the control queue, remove */
555 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
556 h->control_pending_tail, th->cm);
557 GNUNET_free (th->cm);
559 LOG (GNUNET_ERROR_TYPE_DEBUG,
560 "Signalling timeout of request for transmission to peer `%s' via CORE\n",
561 GNUNET_i2s (&pr->peer));
562 trigger_next_request (h, GNUNET_NO);
564 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
569 * Transmit the next message to the core service.
571 * @param cls closure with the 'struct GNUNET_CORE_Handle'
572 * @param size number of bytes available in buf
573 * @param buf where the callee should write the message
574 * @return number of bytes written to buf
577 transmit_message (void *cls, size_t size, void *buf)
579 struct GNUNET_CORE_Handle *h = cls;
580 struct ControlMessage *cm;
581 struct GNUNET_CORE_TransmitHandle *th;
582 struct PeerRecord *pr;
583 struct SendMessage *sm;
584 const struct GNUNET_MessageHeader *hdr;
588 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
592 LOG (GNUNET_ERROR_TYPE_DEBUG,
593 "Transmission failed, initiating reconnect\n");
597 /* first check for control messages */
598 if (NULL != (cm = h->control_pending_head))
600 hdr = (const struct GNUNET_MessageHeader *) &cm[1];
601 msize = ntohs (hdr->size);
604 trigger_next_request (h, GNUNET_NO);
607 LOG (GNUNET_ERROR_TYPE_DEBUG,
608 "Transmitting control message with %u bytes of type %u to core.\n",
609 (unsigned int) msize, (unsigned int) ntohs (hdr->type));
610 memcpy (buf, hdr, msize);
611 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
612 h->control_pending_tail, cm);
615 if (NULL != cm->cont)
616 cm->cont (cm->cont_cls, GNUNET_OK);
618 trigger_next_request (h, GNUNET_NO);
621 /* now check for 'ready' P2P messages */
622 if (NULL == (pr = h->ready_peer_head))
624 GNUNET_assert (NULL != pr->th.peer);
626 if (size < th->msize + sizeof (struct SendMessage))
628 trigger_next_request (h, GNUNET_NO);
631 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
633 if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
635 GNUNET_SCHEDULER_cancel (pr->timeout_task);
636 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
638 LOG (GNUNET_ERROR_TYPE_DEBUG,
639 "Transmitting SEND request to `%s' with %u bytes.\n",
640 GNUNET_i2s (&pr->peer), (unsigned int) th->msize);
641 sm = (struct SendMessage *) buf;
642 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
643 sm->priority = htonl (th->priority);
644 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
646 sm->cork = htonl ((uint32_t) th->cork);
647 sm->reserved = htonl (0);
649 th->get_message (th->get_message_cls,
650 size - sizeof (struct SendMessage), &sm[1]);
652 LOG (GNUNET_ERROR_TYPE_DEBUG,
653 "Transmitting SEND request to `%s' yielded %u bytes.\n",
654 GNUNET_i2s (&pr->peer), ret);
657 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 "Size of clients message to peer %s is 0!\n",
659 GNUNET_i2s (&pr->peer));
660 /* client decided to send nothing! */
661 request_next_transmission (pr);
664 LOG (GNUNET_ERROR_TYPE_DEBUG,
665 "Produced SEND message to core with %u bytes payload\n",
667 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
668 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
671 request_next_transmission (pr);
674 ret += sizeof (struct SendMessage);
675 sm->header.size = htons (ret);
676 GNUNET_assert (ret <= size);
677 request_next_transmission (pr);
683 * Check the list of pending requests, send the next
686 * @param h core handle
687 * @param ignore_currently_down transmit message even if not initialized?
690 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down)
694 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
696 LOG (GNUNET_ERROR_TYPE_DEBUG,
697 "Core connection down, not processing queue\n");
702 LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
705 if (NULL != h->control_pending_head)
707 ntohs (((struct GNUNET_MessageHeader *) &h->
708 control_pending_head[1])->size);
709 else if (h->ready_peer_head != NULL)
711 h->ready_peer_head->th.msize + sizeof (struct SendMessage);
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "Request queue empty, not processing queue\n");
716 return; /* no pending message */
719 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
720 GNUNET_TIME_UNIT_FOREVER_REL,
721 GNUNET_NO, &transmit_message, h);
726 * Handler for notification messages received from the core.
728 * @param cls our "struct GNUNET_CORE_Handle"
729 * @param msg the message received from the core service
732 main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
734 struct GNUNET_CORE_Handle *h = cls;
735 const struct InitReplyMessage *m;
736 const struct ConnectNotifyMessage *cnm;
737 const struct DisconnectNotifyMessage *dnm;
738 const struct NotifyTrafficMessage *ntm;
739 const struct GNUNET_MessageHeader *em;
740 const struct SendMessageReady *smr;
741 const struct GNUNET_CORE_MessageHandler *mh;
742 const struct GNUNET_ATS_Information *ats;
743 GNUNET_CORE_StartupCallback init;
744 struct PeerRecord *pr;
745 struct GNUNET_CORE_TransmitHandle *th;
754 LOG (GNUNET_ERROR_TYPE_INFO,
755 _("Client was disconnected from core service, trying to reconnect.\n"));
759 msize = ntohs (msg->size);
760 LOG (GNUNET_ERROR_TYPE_DEBUG,
761 "Processing message of type %u and size %u from core service\n",
762 ntohs (msg->type), msize);
763 switch (ntohs (msg->type))
765 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
766 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
772 m = (const struct InitReplyMessage *) msg;
773 GNUNET_break (0 == ntohl (m->reserved));
774 /* start our message processing loop */
775 if (GNUNET_YES == h->currently_down)
777 h->currently_down = GNUNET_NO;
778 trigger_next_request (h, GNUNET_NO);
780 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
781 h->me = m->my_identity;
782 if (NULL != (init = h->init))
784 /* mark so we don't call init on reconnect */
786 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to core service of peer `%s'.\n",
787 GNUNET_i2s (&h->me));
788 init (h->cls, h, &h->me);
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Successfully reconnected to core service.\n");
795 /* fake 'connect to self' */
796 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &h->me.hashPubKey);
797 GNUNET_assert (NULL == pr);
798 pr = GNUNET_malloc (sizeof (struct PeerRecord));
801 GNUNET_assert (GNUNET_YES ==
802 GNUNET_CONTAINER_multihashmap_put (h->peers,
803 &h->me.hashPubKey, pr,
804 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
805 if (NULL != h->connects)
806 h->connects (h->cls, &h->me, NULL, 0);
808 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
809 if (msize < sizeof (struct ConnectNotifyMessage))
815 cnm = (const struct ConnectNotifyMessage *) msg;
816 ats_count = ntohl (cnm->ats_count);
818 sizeof (struct ConnectNotifyMessage) +
819 ats_count * sizeof (struct GNUNET_ATS_Information))
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Received notification about connection from `%s'.\n",
827 GNUNET_i2s (&cnm->peer));
828 if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
830 /* connect to self!? */
834 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &cnm->peer.hashPubKey);
841 pr = GNUNET_malloc (sizeof (struct PeerRecord));
842 pr->peer = cnm->peer;
844 GNUNET_assert (GNUNET_YES ==
845 GNUNET_CONTAINER_multihashmap_put (h->peers,
846 &cnm->peer.hashPubKey, pr,
847 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
848 ats = (const struct GNUNET_ATS_Information *) &cnm[1];
849 if (NULL != h->connects)
850 h->connects (h->cls, &cnm->peer, ats, ats_count);
852 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
853 if (msize != sizeof (struct DisconnectNotifyMessage))
859 dnm = (const struct DisconnectNotifyMessage *) msg;
860 if (0 == memcmp (&h->me, &dnm->peer, sizeof (struct GNUNET_PeerIdentity)))
862 /* connection to self!? */
866 GNUNET_break (0 == ntohl (dnm->reserved));
867 LOG (GNUNET_ERROR_TYPE_DEBUG,
868 "Received notification about disconnect from `%s'.\n",
869 GNUNET_i2s (&dnm->peer));
870 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &dnm->peer.hashPubKey);
877 trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
878 (h->ready_peer_head == pr));
879 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
881 trigger_next_request (h, GNUNET_NO);
883 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
884 if (msize < sizeof (struct NotifyTrafficMessage))
890 ntm = (const struct NotifyTrafficMessage *) msg;
891 ats_count = ntohl (ntm->ats_count);
893 sizeof (struct NotifyTrafficMessage) +
894 ats_count * sizeof (struct GNUNET_ATS_Information) +
895 sizeof (struct GNUNET_MessageHeader)) )
901 ats = (const struct GNUNET_ATS_Information*) &ntm[1];
902 em = (const struct GNUNET_MessageHeader *) &ats[ats_count];
903 LOG (GNUNET_ERROR_TYPE_DEBUG,
904 "Received message of type %u and size %u from peer `%4s'\n",
905 ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
906 if ((GNUNET_NO == h->inbound_hdr_only) &&
908 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
909 +ats_count * sizeof (struct GNUNET_ATS_Information)))
915 et = ntohs (em->type);
916 for (hpos = 0; hpos < h->hcnt; hpos++)
918 mh = &h->handlers[hpos];
921 if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
923 LOG (GNUNET_ERROR_TYPE_ERROR,
924 "Unexpected message size %u for message of type %u from peer `%4s'\n",
925 htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
929 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &ntm->peer.hashPubKey);
937 h->handlers[hpos].callback (h->cls, &ntm->peer, em, ats,
940 /* error in processing, do not process other messages! */
944 if (NULL != h->inbound_notify)
945 h->inbound_notify (h->cls, &ntm->peer, em, ats, ats_count);
947 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
948 if (msize < sizeof (struct NotifyTrafficMessage))
954 ntm = (const struct NotifyTrafficMessage *) msg;
955 ats_count = ntohl (ntm->ats_count);
957 sizeof (struct NotifyTrafficMessage) +
958 ats_count * sizeof (struct GNUNET_ATS_Information) +
959 sizeof (struct GNUNET_MessageHeader)) )
965 ats = (const struct GNUNET_ATS_Information*) &ntm[1];
966 em = (const struct GNUNET_MessageHeader *) &ats[ats_count];
967 LOG (GNUNET_ERROR_TYPE_DEBUG,
968 "Received notification about transmission to `%s'.\n",
969 GNUNET_i2s (&ntm->peer));
970 if ((GNUNET_NO == h->outbound_hdr_only) &&
972 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
973 ats_count * sizeof (struct GNUNET_ATS_Information)))
979 if (NULL == h->outbound_notify)
984 h->outbound_notify (h->cls, &ntm->peer, em, ats, ats_count);
986 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
987 if (msize != sizeof (struct SendMessageReady))
993 smr = (const struct SendMessageReady *) msg;
994 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &smr->peer.hashPubKey);
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 (h->currently_down == GNUNET_YES);
1082 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1083 if (NULL == h->client)
1085 reconnect_later (h);
1088 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1089 cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1090 cm->cont = &init_done_task;
1092 init = (struct InitMessage *) &cm[1];
1093 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1094 init->header.size = htons (msize);
1096 if (h->inbound_notify != NULL)
1098 if (h->inbound_hdr_only)
1099 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1101 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1103 if (h->outbound_notify != NULL)
1105 if (h->outbound_hdr_only)
1106 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1108 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1110 LOG (GNUNET_ERROR_TYPE_INFO,
1111 "(Re)connecting to CORE service, monitoring messages of type %u\n",
1114 init->options = htonl (opt);
1115 ts = (uint16_t *) & init[1];
1116 for (hpos = 0; hpos < h->hcnt; hpos++)
1117 ts[hpos] = htons (h->handlers[hpos].type);
1118 GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1120 trigger_next_request (h, GNUNET_YES);
1126 * Connect to the core service. Note that the connection may
1127 * complete (or fail) asynchronously.
1129 * @param cfg configuration to use
1130 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1131 * @param init callback to call once we have successfully
1132 * connected to the core service
1133 * @param connects function to call on peer connect, can be NULL
1134 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1135 * @param inbound_notify function to call for all inbound messages, can be NULL
1136 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1137 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1138 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1139 * @param outbound_notify function to call for all outbound messages, can be NULL
1140 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1141 * GNUNET_MessageHeader and hence we do not need to give it the full message
1142 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1143 * @param handlers callbacks for messages we care about, NULL-terminated
1144 * @return handle to the core service (only useful for disconnect until 'init' is called);
1145 * NULL on error (in this case, init is never called)
1147 struct GNUNET_CORE_Handle *
1148 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1150 GNUNET_CORE_StartupCallback init,
1151 GNUNET_CORE_ConnectEventHandler connects,
1152 GNUNET_CORE_DisconnectEventHandler disconnects,
1153 GNUNET_CORE_MessageCallback inbound_notify,
1154 int inbound_hdr_only,
1155 GNUNET_CORE_MessageCallback outbound_notify,
1156 int outbound_hdr_only,
1157 const struct GNUNET_CORE_MessageHandler *handlers)
1159 struct GNUNET_CORE_Handle *h;
1161 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1165 h->connects = connects;
1166 h->disconnects = disconnects;
1167 h->inbound_notify = inbound_notify;
1168 h->outbound_notify = outbound_notify;
1169 h->inbound_hdr_only = inbound_hdr_only;
1170 h->outbound_hdr_only = outbound_hdr_only;
1171 h->handlers = handlers;
1173 h->currently_down = GNUNET_YES;
1174 h->peers = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1175 if (NULL != handlers)
1176 while (handlers[h->hcnt].callback != NULL)
1178 GNUNET_assert (h->hcnt <
1179 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1180 sizeof (struct InitMessage)) / sizeof (uint16_t));
1181 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
1188 * Disconnect from the core service. This function can only
1189 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1190 * requests have been explicitly canceled.
1192 * @param handle connection to core to disconnect
1195 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1197 struct ControlMessage *cm;
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1200 if (NULL != handle->cth)
1202 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1205 while (NULL != (cm = handle->control_pending_head))
1207 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1208 handle->control_pending_tail, cm);
1211 if (NULL != cm->cont)
1212 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1215 if (NULL != handle->client)
1217 GNUNET_CLIENT_disconnect (handle->client);
1218 handle->client = NULL;
1220 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1221 &disconnect_and_free_peer_entry,
1223 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1225 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1226 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1228 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1229 handle->peers = NULL;
1230 GNUNET_break (handle->ready_peer_head == NULL);
1231 GNUNET_free (handle);
1236 * Task that calls 'request_next_transmission'.
1238 * @param cls the 'struct PeerRecord*'
1239 * @param tc scheduler context
1242 run_request_next_transmission (void *cls,
1243 const struct GNUNET_SCHEDULER_TaskContext *tc)
1245 struct PeerRecord *pr = cls;
1247 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1248 request_next_transmission (pr);
1253 * Ask the core to call "notify" once it is ready to transmit the
1254 * given number of bytes to the specified "target". Must only be
1255 * called after a connection to the respective peer has been
1256 * established (and the client has been informed about this). You may
1257 * have one request of this type pending for each connected peer at
1258 * any time. If a peer disconnects, the application MUST call
1259 * "GNUNET_CORE_notify_transmit_ready_cancel" on the respective
1260 * transmission request, if one such request is pending.
1262 * @param handle connection to core service
1263 * @param cork is corking allowed for this transmission?
1264 * @param priority how important is the message?
1265 * @param maxdelay how long can the message wait?
1266 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1267 * @param notify_size how many bytes of buffer space does notify want?
1268 * @param notify function to call when buffer space is available;
1269 * will be called with NULL on timeout; clients MUST cancel
1270 * all pending transmission requests DURING the disconnect
1272 * @param notify_cls closure for notify
1273 * @return non-NULL if the notify callback was queued,
1274 * NULL if we can not even queue the request (request already pending);
1275 * if NULL is returned, "notify" will NOT be called.
1277 struct GNUNET_CORE_TransmitHandle *
1278 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
1280 struct GNUNET_TIME_Relative maxdelay,
1281 const struct GNUNET_PeerIdentity *target,
1283 GNUNET_CONNECTION_TransmitReadyNotify notify,
1286 struct PeerRecord *pr;
1287 struct GNUNET_CORE_TransmitHandle *th;
1289 if (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1294 GNUNET_assert (NULL != notify);
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Asking core for transmission of %u bytes to `%s'\n",
1297 (unsigned int) notify_size,
1298 GNUNET_i2s (target));
1299 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers, &target->hashPubKey);
1302 /* attempt to send to peer that is not connected */
1306 if (NULL != pr->th.peer)
1308 /* attempting to queue a second request for the same destination */
1312 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1313 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1315 memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1317 th->get_message = notify;
1318 th->get_message_cls = notify_cls;
1319 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1320 th->priority = priority;
1321 th->msize = notify_size;
1323 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
1325 GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1326 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission request added to queue\n");
1332 * Cancel the specified transmission-ready notification.
1334 * @param th handle that was returned by "notify_transmit_ready".
1337 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1339 struct PeerRecord *pr = th->peer;
1340 struct GNUNET_CORE_Handle *h;
1342 GNUNET_assert (NULL != pr);
1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1344 "Aborting transmission request to core for %u bytes to `%s'\n",
1345 (unsigned int) th->msize,
1346 GNUNET_i2s (&pr->peer));
1351 /* we're currently in the control queue, remove */
1352 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1353 h->control_pending_tail, th->cm);
1354 GNUNET_free (th->cm);
1357 if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
1359 /* the request that was 'approved' by core was
1360 * canceled before it could be transmitted; remove
1361 * us from the 'ready' list */
1362 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1364 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
1366 GNUNET_SCHEDULER_cancel (pr->ntr_task);
1367 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1373 * Check if the given peer is currently connected. This function is for special
1374 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1375 * expected to track which peers are connected based on the connect/disconnect
1376 * callbacks from GNUNET_CORE_connect. This function is NOT part of the
1377 * 'versioned', 'official' API. The difference between this function and the
1378 * function GNUNET_CORE_is_peer_connected() is that this one returns
1379 * synchronously after looking in the CORE API cache. The function
1380 * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1381 * its response is given asynchronously.
1383 * @param h the core handle
1384 * @param pid the identity of the peer to check if it has been connected to us
1385 * @return GNUNET_YES if the peer is connected to us; GNUNET_NO if not
1388 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1389 const struct GNUNET_PeerIdentity *pid)
1391 GNUNET_assert (NULL != h);
1392 GNUNET_assert (NULL != pid);
1393 return GNUNET_CONTAINER_multihashmap_contains (h->peers, &pid->hashPubKey);
1397 /* end of core_api.c */