2 This file is part of GNUnet.
3 Copyright (C) 2009-2014 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.
21 * @file core/core_api.c
22 * @brief core service; this is the main API for encrypted P2P
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
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 @e get_message.
63 void *get_message_cls;
66 * Timeout for this handle.
68 struct GNUNET_TIME_Absolute timeout;
71 * How important is this message?
73 enum GNUNET_CORE_Priority priority;
81 * Size of this request.
86 * 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 * Corresponding core handle.
116 struct GNUNET_CORE_Handle *ch;
119 * Pending request, if any. 'th->peer' is set to NULL if the
120 * request is not active.
122 struct GNUNET_CORE_TransmitHandle th;
125 * Peer the record is about.
127 struct GNUNET_PeerIdentity peer;
130 * ID of timeout task for the 'pending_head' handle
131 * which is the one with the smallest timeout.
133 struct GNUNET_SCHEDULER_Task * timeout_task;
136 * ID of task to run 'next_request_transmission'.
138 struct GNUNET_SCHEDULER_Task * 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
159 (*GNUNET_CORE_ControlContinuation) (void *cls,
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.
168 * The actual message is allocated at the end of this struct.
170 struct ControlMessage
173 * This is a doubly-linked list.
175 struct ControlMessage *next;
178 * This is a doubly-linked list.
180 struct ControlMessage *prev;
183 * Function to run after transmission failed/succeeded.
185 GNUNET_CORE_ControlContinuation cont;
188 * Closure for @e cont.
193 * Transmit handle (if one is associated with this ControlMessage), or NULL.
195 struct GNUNET_CORE_TransmitHandle *th;
201 * Context for the core service connection.
203 struct GNUNET_CORE_Handle
207 * Configuration we're using.
209 const struct GNUNET_CONFIGURATION_Handle *cfg;
212 * Closure for the various callbacks.
217 * Function to call once we've handshaked with the core service.
219 GNUNET_CORE_StartupCallback init;
222 * Function to call whenever we're notified about a peer connecting.
224 GNUNET_CORE_ConnectEventHandler connects;
227 * Function to call whenever we're notified about a peer disconnecting.
229 GNUNET_CORE_DisconnectEventHandler disconnects;
232 * Function to call whenever we receive an inbound message.
234 GNUNET_CORE_MessageCallback inbound_notify;
237 * Function to call whenever we receive an outbound message.
239 GNUNET_CORE_MessageCallback outbound_notify;
242 * Function handlers for messages of particular type.
244 const struct GNUNET_CORE_MessageHandler *handlers;
247 * Our connection to the service.
249 struct GNUNET_CLIENT_Connection *client;
252 * Handle for our current transmission request.
254 struct GNUNET_CLIENT_TransmitHandle *cth;
257 * Head of doubly-linked list of pending requests.
259 struct ControlMessage *control_pending_head;
262 * Tail of doubly-linked list of pending requests.
264 struct ControlMessage *control_pending_tail;
267 * Head of doubly-linked list of peers that are core-approved
268 * to send their next message.
270 struct PeerRecord *ready_peer_head;
273 * Tail of doubly-linked list of peers that are core-approved
274 * to send their next message.
276 struct PeerRecord *ready_peer_tail;
279 * Hash map listing all of the peers that we are currently
282 struct GNUNET_CONTAINER_MultiPeerMap *peers;
285 * Identity of this peer.
287 struct GNUNET_PeerIdentity me;
290 * ID of reconnect task (if any).
292 struct GNUNET_SCHEDULER_Task * reconnect_task;
295 * Current delay we use for re-trying to connect to core.
297 struct GNUNET_TIME_Relative retry_backoff;
300 * Number of entries in the handlers array.
305 * For inbound notifications without a specific handler, do
306 * we expect to only receive headers?
308 int inbound_hdr_only;
311 * For outbound notifications without a specific handler, do
312 * we expect to only receive headers?
314 int outbound_hdr_only;
317 * Are we currently disconnected and hence unable to forward
326 * Our current client connection went down. Clean it up
327 * and try to reconnect!
329 * @param h our handle to the core service
332 reconnect (struct GNUNET_CORE_Handle *h);
336 * Task schedule to try to re-connect to core.
338 * @param cls the `struct GNUNET_CORE_Handle`
339 * @param tc task context
342 reconnect_task (void *cls,
343 const struct GNUNET_SCHEDULER_TaskContext *tc)
345 struct GNUNET_CORE_Handle *h = cls;
347 h->reconnect_task = NULL;
348 LOG (GNUNET_ERROR_TYPE_DEBUG,
349 "Connecting to CORE service after delay\n");
355 * Notify clients about disconnect and free
356 * the entry for connected peer.
358 * @param cls the `struct GNUNET_CORE_Handle *`
359 * @param key the peer identity (not used)
360 * @param value the `struct PeerRecord` to free.
361 * @return #GNUNET_YES (continue)
364 disconnect_and_free_peer_entry (void *cls,
365 const struct GNUNET_PeerIdentity *key,
368 struct GNUNET_CORE_Handle *h = cls;
369 struct GNUNET_CORE_TransmitHandle *th;
370 struct PeerRecord *pr = value;
372 if (NULL != pr->timeout_task)
374 GNUNET_SCHEDULER_cancel (pr->timeout_task);
375 pr->timeout_task = NULL;
377 if (NULL != pr->ntr_task)
379 GNUNET_SCHEDULER_cancel (pr->ntr_task);
382 if ( (NULL != pr->prev) ||
383 (NULL != pr->next) ||
384 (h->ready_peer_head == pr) )
385 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
388 if (NULL != h->disconnects)
389 h->disconnects (h->cls,
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 (NULL == pr->timeout_task);
405 GNUNET_assert (NULL == 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 (NULL == 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 == NULL);
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,
446 if (NULL != cm->cont)
447 cm->cont (cm->cont_cls, GNUNET_NO);
450 GNUNET_CONTAINER_multipeermap_iterate (h->peers,
451 &disconnect_and_free_peer_entry, h);
452 while (NULL != (pr = h->ready_peer_head))
453 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
456 GNUNET_assert (NULL == h->control_pending_head);
457 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
462 * Check the list of pending requests, send the next
465 * @param h core handle
466 * @param ignore_currently_down transmit message even if not initialized?
469 trigger_next_request (struct GNUNET_CORE_Handle *h,
470 int ignore_currently_down);
474 * The given request hit its timeout. Remove from the
475 * doubly-linked list and call the respective continuation.
477 * @param cls the transmit handle of the request that timed out
478 * @param tc context, can be NULL (!)
481 transmission_timeout (void *cls,
482 const struct GNUNET_SCHEDULER_TaskContext *tc);
486 * Send a control message to the peer asking for transmission
487 * of the message in the given peer record.
489 * @param pr peer to request transmission to
492 request_next_transmission (struct PeerRecord *pr)
494 struct GNUNET_CORE_Handle *h = pr->ch;
495 struct ControlMessage *cm;
496 struct SendMessageRequest *smr;
497 struct GNUNET_CORE_TransmitHandle *th;
499 if (pr->timeout_task != NULL)
501 GNUNET_SCHEDULER_cancel (pr->timeout_task);
502 pr->timeout_task = NULL;
505 if (NULL == th->peer)
507 trigger_next_request (h, GNUNET_NO);
511 return; /* already done */
512 GNUNET_assert (pr->prev == NULL);
513 GNUNET_assert (pr->next == NULL);
515 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
516 &transmission_timeout,
518 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
519 sizeof (struct SendMessageRequest));
522 smr = (struct SendMessageRequest *) &cm[1];
523 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
524 smr->header.size = htons (sizeof (struct SendMessageRequest));
525 smr->priority = htonl ((uint32_t) th->priority);
526 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
527 smr->peer = pr->peer;
528 smr->reserved = htonl (0);
529 smr->size = htons (th->msize);
530 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
531 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
532 h->control_pending_tail, cm);
533 LOG (GNUNET_ERROR_TYPE_DEBUG,
534 "Adding SEND REQUEST for peer `%s' to message queue\n",
535 GNUNET_i2s (&pr->peer));
536 trigger_next_request (h, GNUNET_NO);
541 * The given request hit its timeout. Remove from the
542 * doubly-linked list and call the respective continuation.
544 * @param cls the transmit handle of the request that timed out
545 * @param tc context, can be NULL (!)
548 transmission_timeout (void *cls,
549 const struct GNUNET_SCHEDULER_TaskContext *tc)
551 struct PeerRecord *pr = cls;
552 struct GNUNET_CORE_Handle *h = pr->ch;
553 struct GNUNET_CORE_TransmitHandle *th;
555 pr->timeout_task = NULL;
556 if (NULL != pr->ntr_task)
558 GNUNET_SCHEDULER_cancel (pr->ntr_task);
563 if ( (NULL != pr->prev) ||
564 (NULL != pr->next) ||
565 (pr == h->ready_peer_head) )
567 /* the request that was 'approved' by core was
568 * canceled before it could be transmitted; remove
569 * us from the 'ready' list */
570 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
576 /* we're currently in the control queue, remove */
577 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
578 h->control_pending_tail, th->cm);
579 GNUNET_free (th->cm);
581 LOG (GNUNET_ERROR_TYPE_DEBUG,
582 "Signalling timeout of request for transmission to peer `%s' via CORE\n",
583 GNUNET_i2s (&pr->peer));
584 trigger_next_request (h, GNUNET_NO);
586 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
591 * Transmit the next message to the core service.
593 * @param cls closure with the `struct GNUNET_CORE_Handle`
594 * @param size number of bytes available in @a buf
595 * @param buf where the callee should write the message
596 * @return number of bytes written to @a buf
599 transmit_message (void *cls,
603 struct GNUNET_CORE_Handle *h = cls;
604 struct ControlMessage *cm;
605 struct GNUNET_CORE_TransmitHandle *th;
606 struct PeerRecord *pr;
607 struct SendMessage *sm;
608 const struct GNUNET_MessageHeader *hdr;
612 GNUNET_assert (h->reconnect_task == NULL);
616 LOG (GNUNET_ERROR_TYPE_DEBUG,
617 "Transmission failed, initiating reconnect\n");
621 /* first check for control messages */
622 if (NULL != (cm = h->control_pending_head))
624 hdr = (const struct GNUNET_MessageHeader *) &cm[1];
625 msize = ntohs (hdr->size);
628 trigger_next_request (h, GNUNET_NO);
631 LOG (GNUNET_ERROR_TYPE_DEBUG,
632 "Transmitting control message with %u bytes of type %u to core.\n",
633 (unsigned int) msize,
634 (unsigned int) ntohs (hdr->type));
635 memcpy (buf, hdr, msize);
636 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
637 h->control_pending_tail, cm);
640 if (NULL != cm->cont)
641 cm->cont (cm->cont_cls, GNUNET_OK);
643 trigger_next_request (h, GNUNET_NO);
646 /* now check for 'ready' P2P messages */
647 if (NULL == (pr = h->ready_peer_head))
649 GNUNET_assert (NULL != pr->th.peer);
651 if (size < th->msize + sizeof (struct SendMessage))
653 trigger_next_request (h, GNUNET_NO);
656 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
660 if (NULL != pr->timeout_task)
662 GNUNET_SCHEDULER_cancel (pr->timeout_task);
663 pr->timeout_task = NULL;
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Transmitting SEND request to `%s' with %u bytes.\n",
667 GNUNET_i2s (&pr->peer),
668 (unsigned int) th->msize);
669 sm = (struct SendMessage *) buf;
670 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
671 sm->priority = htonl ((uint32_t) th->priority);
672 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
674 sm->cork = htonl ((uint32_t) th->cork);
675 sm->reserved = htonl (0);
677 th->get_message (th->get_message_cls,
678 size - sizeof (struct SendMessage),
681 LOG (GNUNET_ERROR_TYPE_DEBUG,
682 "Transmitting SEND request to `%s' yielded %u bytes.\n",
683 GNUNET_i2s (&pr->peer),
687 LOG (GNUNET_ERROR_TYPE_DEBUG,
688 "Size of clients message to peer %s is 0!\n",
689 GNUNET_i2s (&pr->peer));
690 /* client decided to send nothing! */
691 request_next_transmission (pr);
694 LOG (GNUNET_ERROR_TYPE_DEBUG,
695 "Produced SEND message to core with %u bytes payload\n",
697 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
698 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
701 request_next_transmission (pr);
704 ret += sizeof (struct SendMessage);
705 sm->header.size = htons (ret);
706 GNUNET_assert (ret <= size);
707 request_next_transmission (pr);
713 * Check the list of pending requests, send the next one to the core.
715 * @param h core handle
716 * @param ignore_currently_down transmit message even if not initialized?
719 trigger_next_request (struct GNUNET_CORE_Handle *h,
720 int ignore_currently_down)
724 if ( (GNUNET_YES == h->currently_down) &&
725 (GNUNET_NO == ignore_currently_down) )
727 LOG (GNUNET_ERROR_TYPE_DEBUG,
728 "Core connection down, not processing queue\n");
733 LOG (GNUNET_ERROR_TYPE_DEBUG,
734 "Request pending, not processing queue\n");
737 if (NULL != h->control_pending_head)
739 ntohs (((struct GNUNET_MessageHeader *) &h->
740 control_pending_head[1])->size);
741 else if (h->ready_peer_head != NULL)
743 h->ready_peer_head->th.msize + sizeof (struct SendMessage);
746 LOG (GNUNET_ERROR_TYPE_DEBUG,
747 "Request queue empty, not processing queue\n");
748 return; /* no pending message */
751 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
752 GNUNET_TIME_UNIT_FOREVER_REL,
754 &transmit_message, h);
759 * Handler for notification messages received from the core.
761 * @param cls our `struct GNUNET_CORE_Handle`
762 * @param msg the message received from the core service
765 main_notify_handler (void *cls,
766 const struct GNUNET_MessageHeader *msg)
768 struct GNUNET_CORE_Handle *h = cls;
769 const struct InitReplyMessage *m;
770 const struct ConnectNotifyMessage *cnm;
771 const struct DisconnectNotifyMessage *dnm;
772 const struct NotifyTrafficMessage *ntm;
773 const struct GNUNET_MessageHeader *em;
774 const struct SendMessageReady *smr;
775 const struct GNUNET_CORE_MessageHandler *mh;
776 GNUNET_CORE_StartupCallback init;
777 struct PeerRecord *pr;
778 struct GNUNET_CORE_TransmitHandle *th;
786 LOG (GNUNET_ERROR_TYPE_INFO,
787 _("Client was disconnected from core service, trying to reconnect.\n"));
791 msize = ntohs (msg->size);
792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 "Processing message of type %u and size %u from core service\n",
794 ntohs (msg->type), msize);
795 switch (ntohs (msg->type))
797 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
798 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
804 m = (const struct InitReplyMessage *) msg;
805 GNUNET_break (0 == ntohl (m->reserved));
806 /* start our message processing loop */
807 if (GNUNET_YES == h->currently_down)
809 h->currently_down = GNUNET_NO;
810 trigger_next_request (h, GNUNET_NO);
812 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
813 h->me = m->my_identity;
814 if (NULL != (init = h->init))
816 /* mark so we don't call init on reconnect */
818 LOG (GNUNET_ERROR_TYPE_DEBUG,
819 "Connected to core service of peer `%s'.\n",
820 GNUNET_i2s (&h->me));
821 init (h->cls, &h->me);
825 LOG (GNUNET_ERROR_TYPE_DEBUG,
826 "Successfully reconnected to core service.\n");
828 /* fake 'connect to self' */
829 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &h->me);
830 GNUNET_assert (NULL == pr);
831 pr = GNUNET_new (struct PeerRecord);
834 GNUNET_assert (GNUNET_YES ==
835 GNUNET_CONTAINER_multipeermap_put (h->peers,
837 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
838 if (NULL != h->connects)
839 h->connects (h->cls, &pr->peer);
841 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
842 if (msize < sizeof (struct ConnectNotifyMessage))
848 cnm = (const struct ConnectNotifyMessage *) msg;
850 sizeof (struct ConnectNotifyMessage))
856 LOG (GNUNET_ERROR_TYPE_DEBUG,
857 "Received notification about connection from `%s'.\n",
858 GNUNET_i2s (&cnm->peer));
859 if (0 == memcmp (&h->me,
861 sizeof (struct GNUNET_PeerIdentity)))
863 /* connect to self!? */
867 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &cnm->peer);
874 pr = GNUNET_new (struct PeerRecord);
875 pr->peer = cnm->peer;
877 GNUNET_assert (GNUNET_YES ==
878 GNUNET_CONTAINER_multipeermap_put (h->peers,
880 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
881 if (NULL != h->connects)
882 h->connects (h->cls, &pr->peer);
884 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
885 if (msize != sizeof (struct DisconnectNotifyMessage))
891 dnm = (const struct DisconnectNotifyMessage *) msg;
892 if (0 == memcmp (&h->me,
894 sizeof (struct GNUNET_PeerIdentity)))
896 /* connection to self!? */
900 GNUNET_break (0 == ntohl (dnm->reserved));
901 LOG (GNUNET_ERROR_TYPE_DEBUG,
902 "Received notification about disconnect from `%s'.\n",
903 GNUNET_i2s (&dnm->peer));
904 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &dnm->peer);
911 trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
912 (h->ready_peer_head == pr));
913 disconnect_and_free_peer_entry (h, &dnm->peer, pr);
915 trigger_next_request (h, GNUNET_NO);
917 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
918 if (msize < sizeof (struct NotifyTrafficMessage))
924 ntm = (const struct NotifyTrafficMessage *) msg;
926 sizeof (struct NotifyTrafficMessage) +
927 sizeof (struct GNUNET_MessageHeader)) )
933 em = (const struct GNUNET_MessageHeader *) &ntm[1];
934 LOG (GNUNET_ERROR_TYPE_DEBUG,
935 "Received message of type %u and size %u from peer `%4s'\n",
936 ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
937 if ((GNUNET_NO == h->inbound_hdr_only) &&
939 ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
945 et = ntohs (em->type);
946 for (hpos = 0; hpos < h->hcnt; hpos++)
948 mh = &h->handlers[hpos];
951 if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
953 LOG (GNUNET_ERROR_TYPE_ERROR,
954 "Unexpected message size %u for message of type %u from peer `%4s'\n",
955 htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
959 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &ntm->peer);
967 h->handlers[hpos].callback (h->cls, &ntm->peer, em))
969 /* error in processing, do not process other messages! */
973 if (NULL != h->inbound_notify)
974 h->inbound_notify (h->cls, &ntm->peer, em);
976 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
977 if (msize < sizeof (struct NotifyTrafficMessage))
983 ntm = (const struct NotifyTrafficMessage *) msg;
985 sizeof (struct NotifyTrafficMessage) +
986 sizeof (struct GNUNET_MessageHeader)) )
992 em = (const struct GNUNET_MessageHeader *) &ntm[1];
993 LOG (GNUNET_ERROR_TYPE_DEBUG,
994 "Received notification about transmission to `%s'.\n",
995 GNUNET_i2s (&ntm->peer));
996 if ((GNUNET_NO == h->outbound_hdr_only) &&
998 ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
1001 reconnect_later (h);
1004 if (NULL == h->outbound_notify)
1009 h->outbound_notify (h->cls, &ntm->peer, em);
1011 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1012 if (msize != sizeof (struct SendMessageReady))
1015 reconnect_later (h);
1018 smr = (const struct SendMessageReady *) msg;
1019 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
1024 reconnect_later (h);
1027 LOG (GNUNET_ERROR_TYPE_DEBUG,
1028 "Received notification about transmission readiness to `%s'.\n",
1029 GNUNET_i2s (&smr->peer));
1030 if (NULL == pr->th.peer)
1032 /* request must have been cancelled between the original request
1033 * and the response from core, ignore core's readiness */
1038 if (ntohs (smr->smr_id) != th->smr_id)
1040 /* READY message is for expired or cancelled message,
1041 * ignore! (we should have already sent another request) */
1044 if ( (NULL != pr->prev) ||
1045 (NULL != pr->next) ||
1046 (h->ready_peer_head == pr) )
1048 /* we should not already be on the ready list... */
1050 reconnect_later (h);
1053 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1056 trigger_next_request (h, GNUNET_NO);
1059 reconnect_later (h);
1062 GNUNET_CLIENT_receive (h->client,
1063 &main_notify_handler, h,
1064 GNUNET_TIME_UNIT_FOREVER_REL);
1069 * Task executed once we are done transmitting the INIT message.
1070 * Starts our 'receive' loop.
1072 * @param cls the 'struct GNUNET_CORE_Handle'
1073 * @param success were we successful
1076 init_done_task (void *cls, int success)
1078 struct GNUNET_CORE_Handle *h = cls;
1080 if (GNUNET_SYSERR == success)
1081 return; /* shutdown */
1082 if (GNUNET_NO == success)
1084 LOG (GNUNET_ERROR_TYPE_DEBUG,
1085 "Failed to exchange INIT with core, retrying\n");
1086 if (h->reconnect_task == NULL)
1087 reconnect_later (h);
1090 GNUNET_CLIENT_receive (h->client,
1091 &main_notify_handler, h,
1092 GNUNET_TIME_UNIT_FOREVER_REL);
1097 * Our current client connection went down. Clean it up
1098 * and try to reconnect!
1100 * @param h our handle to the core service
1103 reconnect (struct GNUNET_CORE_Handle *h)
1105 struct ControlMessage *cm;
1106 struct InitMessage *init;
1112 GNUNET_assert (NULL == h->client);
1113 GNUNET_assert (GNUNET_YES == h->currently_down);
1114 GNUNET_assert (NULL != h->cfg);
1115 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1116 if (NULL == h->client)
1118 reconnect_later (h);
1121 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1122 cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1123 cm->cont = &init_done_task;
1125 init = (struct InitMessage *) &cm[1];
1126 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1127 init->header.size = htons (msize);
1129 if (NULL != h->inbound_notify)
1131 if (h->inbound_hdr_only)
1132 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1134 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1136 if (NULL != h->outbound_notify)
1138 if (h->outbound_hdr_only)
1139 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1141 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1143 LOG (GNUNET_ERROR_TYPE_INFO,
1144 "(Re)connecting to CORE service, monitoring messages of type %u\n",
1147 init->options = htonl (opt);
1148 ts = (uint16_t *) & init[1];
1149 for (hpos = 0; hpos < h->hcnt; hpos++)
1150 ts[hpos] = htons (h->handlers[hpos].type);
1151 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1152 h->control_pending_tail,
1154 trigger_next_request (h, GNUNET_YES);
1160 * Connect to the core service. Note that the connection may
1161 * complete (or fail) asynchronously.
1163 * @param cfg configuration to use
1164 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1165 * @param init callback to call once we have successfully
1166 * connected to the core service
1167 * @param connects function to call on peer connect, can be NULL
1168 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1169 * @param inbound_notify function to call for all inbound messages, can be NULL
1170 * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
1171 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1172 * can be used to improve efficiency, ignored if @a inbound_notify is NULLL
1173 * @param outbound_notify function to call for all outbound messages, can be NULL
1174 * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
1175 * GNUNET_MessageHeader and hence we do not need to give it the full message
1176 * can be used to improve efficiency, ignored if @a outbound_notify is NULLL
1177 * @param handlers callbacks for messages we care about, NULL-terminated
1178 * @return handle to the core service (only useful for disconnect until 'init' is called);
1179 * NULL on error (in this case, init is never called)
1181 struct GNUNET_CORE_Handle *
1182 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1184 GNUNET_CORE_StartupCallback init,
1185 GNUNET_CORE_ConnectEventHandler connects,
1186 GNUNET_CORE_DisconnectEventHandler disconnects,
1187 GNUNET_CORE_MessageCallback inbound_notify,
1188 int inbound_hdr_only,
1189 GNUNET_CORE_MessageCallback outbound_notify,
1190 int outbound_hdr_only,
1191 const struct GNUNET_CORE_MessageHandler *handlers)
1193 struct GNUNET_CORE_Handle *h;
1195 GNUNET_assert (NULL != cfg);
1196 h = GNUNET_new (struct GNUNET_CORE_Handle);
1200 h->connects = connects;
1201 h->disconnects = disconnects;
1202 h->inbound_notify = inbound_notify;
1203 h->outbound_notify = outbound_notify;
1204 h->inbound_hdr_only = inbound_hdr_only;
1205 h->outbound_hdr_only = outbound_hdr_only;
1206 h->handlers = handlers;
1208 h->currently_down = GNUNET_YES;
1209 h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1210 if (NULL != handlers)
1211 while (handlers[h->hcnt].callback != NULL)
1213 GNUNET_assert (h->hcnt <
1214 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1215 sizeof (struct InitMessage)) / sizeof (uint16_t));
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "Connecting to CORE service\n");
1224 * Disconnect from the core service. This function can only
1225 * be called *after* all pending #GNUNET_CORE_notify_transmit_ready()
1226 * requests have been explicitly canceled.
1228 * @param handle connection to core to disconnect
1231 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1233 struct ControlMessage *cm;
1235 GNUNET_assert (NULL != handle);
1236 LOG (GNUNET_ERROR_TYPE_DEBUG,
1237 "Disconnecting from CORE service\n");
1238 if (NULL != handle->cth)
1240 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1243 while (NULL != (cm = handle->control_pending_head))
1245 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1246 handle->control_pending_tail,
1250 if (NULL != cm->cont)
1251 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1254 if (NULL != handle->client)
1256 GNUNET_CLIENT_disconnect (handle->client);
1257 handle->client = NULL;
1259 GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
1260 &disconnect_and_free_peer_entry,
1262 if (handle->reconnect_task != NULL)
1264 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1265 handle->reconnect_task = NULL;
1267 GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
1268 handle->peers = NULL;
1269 GNUNET_break (handle->ready_peer_head == NULL);
1270 GNUNET_free (handle);
1275 * Task that calls 'request_next_transmission'.
1277 * @param cls the 'struct PeerRecord *'
1278 * @param tc scheduler context
1281 run_request_next_transmission (void *cls,
1282 const struct GNUNET_SCHEDULER_TaskContext *tc)
1284 struct PeerRecord *pr = cls;
1286 pr->ntr_task = NULL;
1287 request_next_transmission (pr);
1292 * Ask the core to call @a notify once it is ready to transmit the
1293 * given number of bytes to the specified @a target. Must only be
1294 * called after a connection to the respective peer has been
1295 * established (and the client has been informed about this). You may
1296 * have one request of this type pending for each connected peer at
1297 * any time. If a peer disconnects, the application MUST call
1298 * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
1299 * transmission request, if one such request is pending.
1301 * @param handle connection to core service
1302 * @param cork is corking allowed for this transmission?
1303 * @param priority how important is the message?
1304 * @param maxdelay how long can the message wait? Only effective if @a cork is #GNUNET_YES
1305 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1306 * @param notify_size how many bytes of buffer space does @a notify want?
1307 * @param notify function to call when buffer space is available;
1308 * will be called with NULL on timeout; clients MUST cancel
1309 * all pending transmission requests DURING the disconnect
1311 * @param notify_cls closure for @a notify
1312 * @return non-NULL if the notify callback was queued,
1313 * NULL if we can not even queue the request (request already pending);
1314 * if NULL is returned, @a notify will NOT be called.
1316 struct GNUNET_CORE_TransmitHandle *
1317 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1319 enum GNUNET_CORE_Priority priority,
1320 struct GNUNET_TIME_Relative maxdelay,
1321 const struct GNUNET_PeerIdentity *target,
1323 GNUNET_CONNECTION_TransmitReadyNotify notify,
1326 struct PeerRecord *pr;
1327 struct GNUNET_CORE_TransmitHandle *th;
1329 if (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1334 GNUNET_assert (NULL != notify);
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "Asking core for transmission of %u bytes to `%s'\n",
1337 (unsigned int) notify_size,
1338 GNUNET_i2s (target));
1339 pr = GNUNET_CONTAINER_multipeermap_get (handle->peers,
1343 /* attempt to send to peer that is not connected */
1347 if (NULL != pr->th.peer)
1349 /* attempting to queue a second request for the same destination */
1353 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1354 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1356 memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1358 th->get_message = notify;
1359 th->get_message_cls = notify_cls;
1360 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1361 th->priority = priority;
1362 th->msize = notify_size;
1364 GNUNET_assert (NULL == pr->ntr_task);
1366 GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1367 LOG (GNUNET_ERROR_TYPE_DEBUG,
1368 "Transmission request added to queue\n");
1374 * Cancel the specified transmission-ready notification.
1376 * @param th handle that was returned by "notify_transmit_ready".
1379 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1381 struct PeerRecord *pr = th->peer;
1382 struct GNUNET_CORE_Handle *h;
1384 GNUNET_assert (NULL != th);
1385 GNUNET_assert (NULL != pr);
1386 LOG (GNUNET_ERROR_TYPE_DEBUG,
1387 "Aborting transmission request to core for %u bytes to `%s'\n",
1388 (unsigned int) th->msize,
1389 GNUNET_i2s (&pr->peer));
1394 /* we're currently in the control queue, remove */
1395 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1396 h->control_pending_tail,
1398 GNUNET_free (th->cm);
1401 if ( (NULL != pr->prev) ||
1402 (NULL != pr->next) ||
1403 (pr == h->ready_peer_head) )
1405 /* the request that was 'approved' by core was
1406 * canceled before it could be transmitted; remove
1407 * us from the 'ready' list */
1408 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1412 if (NULL != pr->ntr_task)
1414 GNUNET_SCHEDULER_cancel (pr->ntr_task);
1415 pr->ntr_task = NULL;
1421 * Check if the given peer is currently connected. This function is for special
1422 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1423 * expected to track which peers are connected based on the connect/disconnect
1424 * callbacks from #GNUNET_CORE_connect(). This function is NOT part of the
1425 * 'versioned', 'official' API. The difference between this function and the
1426 * function GNUNET_CORE_is_peer_connected() is that this one returns
1427 * synchronously after looking in the CORE API cache. The function
1428 * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1429 * its response is given asynchronously.
1431 * @param h the core handle
1432 * @param pid the identity of the peer to check if it has been connected to us
1433 * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
1436 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1437 const struct GNUNET_PeerIdentity *pid)
1439 GNUNET_assert (NULL != h);
1440 GNUNET_assert (NULL != pid);
1441 return GNUNET_CONTAINER_multipeermap_contains (h->peers, pid);
1445 /* end of core_api.c */