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"
34 * Information we track for each peer.
40 * We generally do NOT keep peer records in a DLL; this
41 * DLL is only used IF this peer's 'pending_head' message
42 * is ready for transmission.
44 struct PeerRecord *prev;
47 * We generally do NOT keep peer records in a DLL; this
48 * DLL is only used IF this peer's 'pending_head' message
49 * is ready for transmission.
51 struct PeerRecord *next;
54 * Peer the record is about.
56 struct GNUNET_PeerIdentity peer;
59 * Corresponding core handle.
61 struct GNUNET_CORE_Handle *ch;
64 * Head of doubly-linked list of pending requests.
65 * Requests are sorted by deadline *except* for HEAD,
66 * which is only modified upon transmission to core.
68 struct GNUNET_CORE_TransmitHandle *pending_head;
71 * Tail of doubly-linked list of pending requests.
73 struct GNUNET_CORE_TransmitHandle *pending_tail;
76 * ID of timeout task for the 'pending_head' handle
77 * which is the one with the smallest timeout.
79 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
82 * ID of task to run 'next_request_transmission'.
84 GNUNET_SCHEDULER_TaskIdentifier ntr_task;
87 * Current size of the queue of pending requests.
89 unsigned int queue_size;
92 * SendMessageRequest ID generator for this peer.
100 * Type of function called upon completion.
103 * @param success GNUNET_OK on success (which for request_connect
104 * ONLY means that we transmitted the connect request to CORE,
105 * it does not mean that we are actually now connected!);
106 * GNUNET_NO on timeout,
107 * GNUNET_SYSERR if core was shut down
109 typedef void (*GNUNET_CORE_ControlContinuation) (void *cls, int success);
113 * Entry in a doubly-linked list of control messages to be transmitted
114 * to the core service. Control messages include traffic allocation,
115 * connection requests and of course our initial 'init' request.
117 * The actual message is allocated at the end of this struct.
119 struct ControlMessage
122 * This is a doubly-linked list.
124 struct ControlMessage *next;
127 * This is a doubly-linked list.
129 struct ControlMessage *prev;
132 * Function to run after transmission failed/succeeded.
134 GNUNET_CORE_ControlContinuation cont;
137 * Closure for 'cont'.
142 * Transmit handle (if one is associated with this ControlMessage), or NULL.
144 struct GNUNET_CORE_TransmitHandle *th;
150 * Context for the core service connection.
152 struct GNUNET_CORE_Handle
156 * Configuration we're using.
158 const struct GNUNET_CONFIGURATION_Handle *cfg;
161 * Closure for the various callbacks.
166 * Function to call once we've handshaked with the core service.
168 GNUNET_CORE_StartupCallback init;
171 * Function to call whenever we're notified about a peer connecting.
173 GNUNET_CORE_ConnectEventHandler connects;
176 * Function to call whenever we're notified about a peer disconnecting.
178 GNUNET_CORE_DisconnectEventHandler disconnects;
181 * Function to call whenever we're notified about a peer changing status.
183 GNUNET_CORE_PeerStatusEventHandler status_events;
186 * Function to call whenever we receive an inbound message.
188 GNUNET_CORE_MessageCallback inbound_notify;
191 * Function to call whenever we receive an outbound message.
193 GNUNET_CORE_MessageCallback outbound_notify;
196 * Function handlers for messages of particular type.
198 const struct GNUNET_CORE_MessageHandler *handlers;
201 * Our connection to the service.
203 struct GNUNET_CLIENT_Connection *client;
206 * Handle for our current transmission request.
208 struct GNUNET_CLIENT_TransmitHandle *cth;
211 * Head of doubly-linked list of pending requests.
213 struct ControlMessage *control_pending_head;
216 * Tail of doubly-linked list of pending requests.
218 struct ControlMessage *control_pending_tail;
221 * Head of doubly-linked list of peers that are core-approved
222 * to send their next message.
224 struct PeerRecord *ready_peer_head;
227 * Tail of doubly-linked list of peers that are core-approved
228 * to send their next message.
230 struct PeerRecord *ready_peer_tail;
233 * Hash map listing all of the peers that we are currently
236 struct GNUNET_CONTAINER_MultiHashMap *peers;
239 * Identity of this peer.
241 struct GNUNET_PeerIdentity me;
244 * ID of reconnect task (if any).
246 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
249 * Current delay we use for re-trying to connect to core.
251 struct GNUNET_TIME_Relative retry_backoff;
254 * Number of messages we are allowed to queue per target.
256 unsigned int queue_size;
259 * Number of entries in the handlers array.
264 * For inbound notifications without a specific handler, do
265 * we expect to only receive headers?
267 int inbound_hdr_only;
270 * For outbound notifications without a specific handler, do
271 * we expect to only receive headers?
273 int outbound_hdr_only;
276 * Are we currently disconnected and hence unable to forward
285 * Handle for a transmission request.
287 struct GNUNET_CORE_TransmitHandle
291 * We keep active transmit handles in a doubly-linked list.
293 struct GNUNET_CORE_TransmitHandle *next;
296 * We keep active transmit handles in a doubly-linked list.
298 struct GNUNET_CORE_TransmitHandle *prev;
301 * Corresponding peer record.
303 struct PeerRecord *peer;
306 * Corresponding SEND_REQUEST message. Only non-NULL
307 * while SEND_REQUEST message is pending.
309 struct ControlMessage *cm;
312 * Function that will be called to get the actual request
313 * (once we are ready to transmit this request to the core).
314 * The function will be called with a NULL buffer to signal
317 GNUNET_CONNECTION_TransmitReadyNotify get_message;
320 * Closure for get_message.
322 void *get_message_cls;
325 * Timeout for this handle.
327 struct GNUNET_TIME_Absolute timeout;
330 * How important is this message?
335 * Size of this request.
340 * Send message request ID for this request.
345 * Is corking allowed?
353 * Our current client connection went down. Clean it up
354 * and try to reconnect!
356 * @param h our handle to the core service
359 reconnect (struct GNUNET_CORE_Handle *h);
363 * Task schedule to try to re-connect to core.
365 * @param cls the 'struct GNUNET_CORE_Handle'
366 * @param tc task context
369 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
371 struct GNUNET_CORE_Handle *h = cls;
373 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Connecting to CORE service after delay\n");
383 * Notify clients about disconnect and free
384 * the entry for connected peer.
386 * @param cls the 'struct GNUNET_CORE_Handle*'
387 * @param key the peer identity (not used)
388 * @param value the 'struct PeerRecord' to free.
389 * @return GNUNET_YES (continue)
392 disconnect_and_free_peer_entry (void *cls, const GNUNET_HashCode * key,
395 struct GNUNET_CORE_Handle *h = cls;
396 struct GNUNET_CORE_TransmitHandle *th;
397 struct PeerRecord *pr = value;
399 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
401 GNUNET_SCHEDULER_cancel (pr->timeout_task);
402 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
404 if (pr->ntr_task != GNUNET_SCHEDULER_NO_TASK)
406 GNUNET_SCHEDULER_cancel (pr->ntr_task);
407 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
409 if ((pr->prev != NULL) || (pr->next != NULL) || (h->ready_peer_head == pr))
410 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
411 if (h->disconnects != NULL)
412 h->disconnects (h->cls, &pr->peer);
413 /* all requests should have been cancelled, clean up anyway, just in case */
414 GNUNET_break (pr->queue_size == 0);
415 while (NULL != (th = pr->pending_head))
418 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
424 /* done with 'voluntary' cleanups, now on to normal freeing */
425 GNUNET_assert (GNUNET_YES ==
426 GNUNET_CONTAINER_multihashmap_remove (h->peers, key, pr));
427 GNUNET_assert (pr->pending_head == NULL);
428 GNUNET_assert (pr->pending_tail == NULL);
429 GNUNET_assert (pr->ch = h);
430 GNUNET_assert (pr->queue_size == 0);
431 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
432 GNUNET_assert (pr->ntr_task == GNUNET_SCHEDULER_NO_TASK);
439 * Close down any existing connection to the CORE service and
440 * try re-establishing it later.
442 * @param h our handle
445 reconnect_later (struct GNUNET_CORE_Handle *h)
447 struct ControlMessage *cm;
448 struct PeerRecord *pr;
450 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
453 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
456 if (h->client != NULL)
458 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
461 h->currently_down = GNUNET_YES;
462 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
464 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_task, h);
465 while (NULL != (cm = h->control_pending_head))
467 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
468 h->control_pending_tail, cm);
471 if (cm->cont != NULL)
472 cm->cont (cm->cont_cls, GNUNET_NO);
475 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
476 &disconnect_and_free_peer_entry, h);
477 while (NULL != (pr = h->ready_peer_head))
478 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
479 GNUNET_assert (h->control_pending_head == NULL);
481 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->retry_backoff);
482 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
487 * Check the list of pending requests, send the next
490 * @param h core handle
491 * @param ignore_currently_down transmit message even if not initialized?
494 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
498 * The given request hit its timeout. Remove from the
499 * doubly-linked list and call the respective continuation.
501 * @param cls the transmit handle of the request that timed out
502 * @param tc context, can be NULL (!)
505 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
509 * Send a control message to the peer asking for transmission
510 * of the message in the given peer record.
512 * @param pr peer to request transmission to
515 request_next_transmission (struct PeerRecord *pr)
517 struct GNUNET_CORE_Handle *h = pr->ch;
518 struct ControlMessage *cm;
519 struct SendMessageRequest *smr;
520 struct GNUNET_CORE_TransmitHandle *th;
522 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
524 GNUNET_SCHEDULER_cancel (pr->timeout_task);
525 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
527 if (NULL == (th = pr->pending_head))
529 trigger_next_request (h, GNUNET_NO);
533 return; /* already done */
534 GNUNET_assert (pr->prev == NULL);
535 GNUNET_assert (pr->next == NULL);
537 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
538 (th->timeout), &transmission_timeout, pr);
539 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
540 sizeof (struct SendMessageRequest));
543 smr = (struct SendMessageRequest *) &cm[1];
544 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
545 smr->header.size = htons (sizeof (struct SendMessageRequest));
546 smr->priority = htonl (th->priority);
547 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
548 smr->peer = pr->peer;
549 smr->queue_size = htonl (pr->queue_size);
550 smr->size = htons (th->msize);
551 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
552 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
553 h->control_pending_tail, cm);
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Adding SEND REQUEST for peer `%s' to message queue\n",
557 GNUNET_i2s (&pr->peer));
559 trigger_next_request (h, GNUNET_NO);
564 * The given request hit its timeout. Remove from the
565 * doubly-linked list and call the respective continuation.
567 * @param cls the transmit handle of the request that timed out
568 * @param tc context, can be NULL (!)
571 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
573 struct PeerRecord *pr = cls;
574 struct GNUNET_CORE_Handle *h = pr->ch;
575 struct GNUNET_CORE_TransmitHandle *th;
577 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
578 th = pr->pending_head;
579 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
581 if ((pr->prev != NULL) || (pr->next != NULL) || (pr == h->ready_peer_head))
583 /* the request that was 'approved' by core was
584 * canceled before it could be transmitted; remove
585 * us from the 'ready' list */
586 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Signalling timeout of request for transmission to CORE service\n");
592 request_next_transmission (pr);
593 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
599 * Transmit the next message to the core service.
602 transmit_message (void *cls, size_t size, void *buf)
604 struct GNUNET_CORE_Handle *h = cls;
605 struct ControlMessage *cm;
606 struct GNUNET_CORE_TransmitHandle *th;
607 struct PeerRecord *pr;
608 struct SendMessage *sm;
609 const struct GNUNET_MessageHeader *hdr;
613 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Transmission failed, initiating reconnect\n");
624 /* first check for control messages */
625 if (NULL != (cm = h->control_pending_head))
627 hdr = (const struct GNUNET_MessageHeader *) &cm[1];
628 msize = ntohs (hdr->size);
631 trigger_next_request (h, GNUNET_NO);
635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
636 "Transmitting control message with %u bytes of type %u to core.\n",
637 (unsigned int) msize, (unsigned int) ntohs (hdr->type));
639 memcpy (buf, hdr, msize);
640 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
641 h->control_pending_tail, cm);
644 if (NULL != cm->cont)
645 cm->cont (cm->cont_cls, GNUNET_OK);
647 trigger_next_request (h, GNUNET_NO);
650 /* now check for 'ready' P2P messages */
651 if (NULL != (pr = h->ready_peer_head))
653 GNUNET_assert (pr->pending_head != NULL);
654 th = pr->pending_head;
655 if (size < th->msize + sizeof (struct SendMessage))
657 trigger_next_request (h, GNUNET_NO);
660 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
661 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
663 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
665 GNUNET_SCHEDULER_cancel (pr->timeout_task);
666 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Transmitting SEND request to `%s' with %u bytes.\n",
671 GNUNET_i2s (&pr->peer), (unsigned int) th->msize);
673 sm = (struct SendMessage *) buf;
674 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
675 sm->priority = htonl (th->priority);
676 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
678 sm->cork = htonl ((uint32_t) th->cork);
679 sm->reserved = htonl (0);
681 th->get_message (th->get_message_cls,
682 size - sizeof (struct SendMessage), &sm[1]);
685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686 "Transmitting SEND request to `%s' yielded %u bytes.\n",
687 GNUNET_i2s (&pr->peer), ret);
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Size of clients message to peer %s is 0!\n",
695 GNUNET_i2s (&pr->peer));
697 /* client decided to send nothing! */
698 request_next_transmission (pr);
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703 "Produced SEND message to core with %u bytes payload\n",
706 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
707 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
710 request_next_transmission (pr);
713 ret += sizeof (struct SendMessage);
714 sm->header.size = htons (ret);
715 GNUNET_assert (ret <= size);
716 request_next_transmission (pr);
724 * Check the list of pending requests, send the next
727 * @param h core handle
728 * @param ignore_currently_down transmit message even if not initialized?
731 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down)
735 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Core connection down, not processing queue\n");
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "Request pending, not processing queue\n");
751 if (h->control_pending_head != NULL)
753 ntohs (((struct GNUNET_MessageHeader *) &h->
754 control_pending_head[1])->size);
755 else if (h->ready_peer_head != NULL)
757 h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Request queue empty, not processing queue\n");
764 return; /* no pending message */
767 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
768 GNUNET_TIME_UNIT_FOREVER_REL,
769 GNUNET_NO, &transmit_message, h);
774 * Handler for notification messages received from the core.
776 * @param cls our "struct GNUNET_CORE_Handle"
777 * @param msg the message received from the core service
780 main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
782 struct GNUNET_CORE_Handle *h = cls;
783 const struct InitReplyMessage *m;
784 const struct ConnectNotifyMessage *cnm;
785 const struct DisconnectNotifyMessage *dnm;
786 const struct NotifyTrafficMessage *ntm;
787 const struct GNUNET_MessageHeader *em;
788 const struct PeerStatusNotifyMessage *psnm;
789 const struct SendMessageReady *smr;
790 const struct GNUNET_CORE_MessageHandler *mh;
791 GNUNET_CORE_StartupCallback init;
792 struct PeerRecord *pr;
793 struct GNUNET_CORE_TransmitHandle *th;
802 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
804 ("Client was disconnected from core service, trying to reconnect.\n"));
808 msize = ntohs (msg->size);
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Processing message of type %u and size %u from core service\n",
812 ntohs (msg->type), msize);
814 switch (ntohs (msg->type))
816 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
817 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
823 m = (const struct InitReplyMessage *) msg;
824 GNUNET_break (0 == ntohl (m->reserved));
825 /* start our message processing loop */
826 if (GNUNET_YES == h->currently_down)
828 h->currently_down = GNUNET_NO;
829 trigger_next_request (h, GNUNET_NO);
831 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
832 h->me = m->my_identity;
833 if (NULL != (init = h->init))
835 /* mark so we don't call init on reconnect */
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 "Connected to core service of peer `%s'.\n",
840 GNUNET_i2s (&h->me));
842 init (h->cls, h, &h->me);
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Successfully reconnected to core service.\n");
851 /* fake 'connect to self' */
852 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &h->me.hashPubKey);
853 GNUNET_assert (pr == NULL);
854 pr = GNUNET_malloc (sizeof (struct PeerRecord));
857 GNUNET_assert (GNUNET_YES ==
858 GNUNET_CONTAINER_multihashmap_put (h->peers,
859 &h->me.hashPubKey, pr,
860 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
861 if (NULL != h->connects)
862 h->connects (h->cls, &h->me, NULL);
864 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
865 if (msize < sizeof (struct ConnectNotifyMessage))
871 cnm = (const struct ConnectNotifyMessage *) msg;
872 ats_count = ntohl (cnm->ats_count);
874 sizeof (struct ConnectNotifyMessage) +
875 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
876 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
877 ntohl ((&cnm->ats)[ats_count].type)))
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "Received notification about connection from `%s'.\n",
886 GNUNET_i2s (&cnm->peer));
888 if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
890 /* connect to self!? */
894 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &cnm->peer.hashPubKey);
901 pr = GNUNET_malloc (sizeof (struct PeerRecord));
902 pr->peer = cnm->peer;
904 GNUNET_assert (GNUNET_YES ==
905 GNUNET_CONTAINER_multihashmap_put (h->peers,
906 &cnm->peer.hashPubKey, pr,
907 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
908 if (NULL != h->connects)
909 h->connects (h->cls, &cnm->peer, &cnm->ats);
911 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
912 if (msize != sizeof (struct DisconnectNotifyMessage))
918 dnm = (const struct DisconnectNotifyMessage *) msg;
919 if (0 == memcmp (&h->me, &dnm->peer, sizeof (struct GNUNET_PeerIdentity)))
921 /* connection to self!? */
925 GNUNET_break (0 == ntohl (dnm->reserved));
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Received notification about disconnect from `%s'.\n",
929 GNUNET_i2s (&dnm->peer));
931 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &dnm->peer.hashPubKey);
938 trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
939 (h->ready_peer_head == pr));
940 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
942 trigger_next_request (h, GNUNET_NO);
944 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
945 if (NULL == h->status_events)
950 if (msize < sizeof (struct PeerStatusNotifyMessage))
956 psnm = (const struct PeerStatusNotifyMessage *) msg;
957 if (0 == memcmp (&h->me, &psnm->peer, sizeof (struct GNUNET_PeerIdentity)))
963 ats_count = ntohl (psnm->ats_count);
965 sizeof (struct PeerStatusNotifyMessage) +
966 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
967 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
968 ntohl ((&psnm->ats)[ats_count].type)))
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 "Received notification about status change by `%s'.\n",
977 GNUNET_i2s (&psnm->peer));
979 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &psnm->peer.hashPubKey);
986 h->status_events (h->cls, &psnm->peer, psnm->bandwidth_in,
988 GNUNET_TIME_absolute_ntoh (psnm->timeout), &psnm->ats);
990 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
991 if (msize < sizeof (struct NotifyTrafficMessage))
997 ntm = (const struct NotifyTrafficMessage *) msg;
999 ats_count = ntohl (ntm->ats_count);
1001 sizeof (struct NotifyTrafficMessage) +
1002 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
1003 sizeof (struct GNUNET_MessageHeader)) ||
1004 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
1005 ntohl ((&ntm->ats)[ats_count].type)))
1008 reconnect_later (h);
1011 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count + 1];
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1014 "Received message of type %u and size %u from peer `%4s'\n",
1015 ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
1017 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &ntm->peer.hashPubKey);
1021 reconnect_later (h);
1024 if ((GNUNET_NO == h->inbound_hdr_only) &&
1026 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1027 +ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)))
1030 reconnect_later (h);
1033 et = ntohs (em->type);
1034 for (hpos = 0; hpos < h->hcnt; hpos++)
1036 mh = &h->handlers[hpos];
1039 if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
1045 h->handlers[hpos].callback (h->cls, &ntm->peer, em, &ntm->ats))
1047 /* error in processing, do not process other messages! */
1051 if (NULL != h->inbound_notify)
1052 h->inbound_notify (h->cls, &ntm->peer, em, &ntm->ats);
1054 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1055 if (msize < sizeof (struct NotifyTrafficMessage))
1058 reconnect_later (h);
1061 ntm = (const struct NotifyTrafficMessage *) msg;
1062 if (0 == memcmp (&h->me, &ntm->peer, sizeof (struct GNUNET_PeerIdentity)))
1068 ats_count = ntohl (ntm->ats_count);
1070 sizeof (struct NotifyTrafficMessage) +
1071 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
1072 sizeof (struct GNUNET_MessageHeader)) ||
1073 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
1074 ntohl ((&ntm->ats)[ats_count].type)))
1077 reconnect_later (h);
1080 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count + 1];
1081 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &ntm->peer.hashPubKey);
1085 reconnect_later (h);
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Received notification about transmission to `%s'.\n",
1091 GNUNET_i2s (&ntm->peer));
1093 if ((GNUNET_NO == h->outbound_hdr_only) &&
1095 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1096 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)))
1099 reconnect_later (h);
1102 if (NULL == h->outbound_notify)
1107 h->outbound_notify (h->cls, &ntm->peer, em, &ntm->ats);
1109 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1110 if (msize != sizeof (struct SendMessageReady))
1113 reconnect_later (h);
1116 smr = (const struct SendMessageReady *) msg;
1117 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &smr->peer.hashPubKey);
1121 reconnect_later (h);
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "Received notification about transmission readiness to `%s'.\n",
1127 GNUNET_i2s (&smr->peer));
1129 if (pr->pending_head == NULL)
1131 /* request must have been cancelled between the original request
1132 * and the response from core, ignore core's readiness */
1136 th = pr->pending_head;
1137 if (ntohs (smr->smr_id) != th->smr_id)
1139 /* READY message is for expired or cancelled message,
1140 * ignore! (we should have already sent another request) */
1143 if ((pr->prev != NULL) || (pr->next != NULL) || (h->ready_peer_head == pr))
1145 /* we should not already be on the ready list... */
1147 reconnect_later (h);
1150 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head, h->ready_peer_tail, pr);
1151 trigger_next_request (h, GNUNET_NO);
1154 reconnect_later (h);
1157 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1158 GNUNET_TIME_UNIT_FOREVER_REL);
1163 * Task executed once we are done transmitting the INIT message.
1164 * Starts our 'receive' loop.
1166 * @param cls the 'struct GNUNET_CORE_Handle'
1167 * @param success were we successful
1170 init_done_task (void *cls, int success)
1172 struct GNUNET_CORE_Handle *h = cls;
1174 if (success == GNUNET_SYSERR)
1175 return; /* shutdown */
1176 if (success == GNUNET_NO)
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Failed to exchange INIT with core, retrying\n");
1182 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1183 reconnect_later (h);
1186 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1187 GNUNET_TIME_UNIT_FOREVER_REL);
1192 * Our current client connection went down. Clean it up
1193 * and try to reconnect!
1195 * @param h our handle to the core service
1198 reconnect (struct GNUNET_CORE_Handle *h)
1200 struct ControlMessage *cm;
1201 struct InitMessage *init;
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting to CORE service\n");
1210 GNUNET_assert (h->client == NULL);
1211 GNUNET_assert (h->currently_down == GNUNET_YES);
1212 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1213 if (h->client == NULL)
1215 reconnect_later (h);
1218 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1219 cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1220 cm->cont = &init_done_task;
1222 init = (struct InitMessage *) &cm[1];
1223 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1224 init->header.size = htons (msize);
1225 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1226 if (h->status_events != NULL)
1227 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1228 if (h->inbound_notify != NULL)
1230 if (h->inbound_hdr_only)
1231 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1233 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1235 if (h->outbound_notify != NULL)
1237 if (h->outbound_hdr_only)
1238 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1240 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1242 init->options = htonl (opt);
1243 ts = (uint16_t *) & init[1];
1244 for (hpos = 0; hpos < h->hcnt; hpos++)
1245 ts[hpos] = htons (h->handlers[hpos].type);
1246 GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1248 trigger_next_request (h, GNUNET_YES);
1254 * Connect to the core service. Note that the connection may
1255 * complete (or fail) asynchronously.
1257 * @param cfg configuration to use
1258 * @param queue_size size of the per-peer message queue
1259 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1260 * @param init callback to call on timeout or once we have successfully
1261 * connected to the core service; note that timeout is only meaningful if init is not NULL
1262 * @param connects function to call on peer connect, can be NULL
1263 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1264 * @param status_events function to call on changes to peer connection status, can be NULL
1265 * @param inbound_notify function to call for all inbound messages, can be NULL
1266 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1267 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1268 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1269 * @param outbound_notify function to call for all outbound messages, can be NULL
1270 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1271 * GNUNET_MessageHeader and hence we do not need to give it the full message
1272 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1273 * @param handlers callbacks for messages we care about, NULL-terminated
1274 * @return handle to the core service (only useful for disconnect until 'init' is called);
1275 * NULL on error (in this case, init is never called)
1277 struct GNUNET_CORE_Handle *
1278 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1279 unsigned int queue_size, void *cls,
1280 GNUNET_CORE_StartupCallback init,
1281 GNUNET_CORE_ConnectEventHandler connects,
1282 GNUNET_CORE_DisconnectEventHandler disconnects,
1283 GNUNET_CORE_PeerStatusEventHandler status_events,
1284 GNUNET_CORE_MessageCallback inbound_notify,
1285 int inbound_hdr_only,
1286 GNUNET_CORE_MessageCallback outbound_notify,
1287 int outbound_hdr_only,
1288 const struct GNUNET_CORE_MessageHandler *handlers)
1290 struct GNUNET_CORE_Handle *h;
1292 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1294 h->queue_size = queue_size;
1297 h->connects = connects;
1298 h->disconnects = disconnects;
1299 h->status_events = status_events;
1300 h->inbound_notify = inbound_notify;
1301 h->outbound_notify = outbound_notify;
1302 h->inbound_hdr_only = inbound_hdr_only;
1303 h->outbound_hdr_only = outbound_hdr_only;
1304 h->handlers = handlers;
1306 h->currently_down = GNUNET_YES;
1307 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1308 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1309 if (NULL != handlers)
1310 while (handlers[h->hcnt].callback != NULL)
1312 GNUNET_assert (h->hcnt <
1313 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1314 sizeof (struct InitMessage)) / sizeof (uint16_t));
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
1324 * Disconnect from the core service. This function can only
1325 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1326 * requests have been explicitly canceled.
1328 * @param handle connection to core to disconnect
1331 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1333 struct ControlMessage *cm;
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1338 if (handle->cth != NULL)
1340 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1343 while (NULL != (cm = handle->control_pending_head))
1345 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1346 handle->control_pending_tail, cm);
1349 if (cm->cont != NULL)
1350 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1353 if (handle->client != NULL)
1355 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1356 handle->client = NULL;
1358 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1359 &disconnect_and_free_peer_entry,
1361 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1363 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1364 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1366 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1367 handle->peers = NULL;
1368 GNUNET_break (handle->ready_peer_head == NULL);
1369 GNUNET_free (handle);
1374 * Task that calls 'request_next_transmission'.
1376 * @param cls the 'struct PeerRecord*'
1377 * @param tc scheduler context
1380 run_request_next_transmission (void *cls,
1381 const struct GNUNET_SCHEDULER_TaskContext *tc)
1383 struct PeerRecord *pr = cls;
1385 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1386 request_next_transmission (pr);
1391 * Ask the core to call "notify" once it is ready to transmit the
1392 * given number of bytes to the specified "target". Must only be
1393 * called after a connection to the respective peer has been
1394 * established (and the client has been informed about this).
1396 * @param handle connection to core service
1397 * @param cork is corking allowed for this transmission?
1398 * @param priority how important is the message?
1399 * @param maxdelay how long can the message wait?
1400 * @param target who should receive the message,
1401 * use NULL for this peer (loopback)
1402 * @param notify_size how many bytes of buffer space does notify want?
1403 * @param notify function to call when buffer space is available
1404 * @param notify_cls closure for notify
1405 * @return non-NULL if the notify callback was queued,
1406 * NULL if we can not even queue the request (insufficient
1407 * memory); if NULL is returned, "notify" will NOT be called.
1409 struct GNUNET_CORE_TransmitHandle *
1410 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
1412 struct GNUNET_TIME_Relative maxdelay,
1413 const struct GNUNET_PeerIdentity *target,
1415 GNUNET_CONNECTION_TransmitReadyNotify notify,
1418 struct PeerRecord *pr;
1419 struct GNUNET_CORE_TransmitHandle *th;
1420 struct GNUNET_CORE_TransmitHandle *pos;
1421 struct GNUNET_CORE_TransmitHandle *prev;
1422 struct GNUNET_CORE_TransmitHandle *minp;
1424 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers, &target->hashPubKey);
1427 /* attempt to send to peer that is not connected */
1428 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1429 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1430 GNUNET_i2s (target), GNUNET_h2s (&handle->me.hashPubKey));
1434 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1435 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1436 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1438 GNUNET_assert (NULL != notify);
1439 th->get_message = notify;
1440 th->get_message_cls = notify_cls;
1441 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1442 th->priority = priority;
1443 th->msize = notify_size;
1445 /* bound queue size */
1446 if (pr->queue_size == handle->queue_size)
1448 /* find lowest-priority entry, but skip the head of the list */
1449 minp = pr->pending_head->next;
1451 while (prev != NULL)
1453 if (prev->priority < minp->priority)
1459 GNUNET_break (handle->queue_size != 0);
1460 GNUNET_break (pr->queue_size == 1);
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "Dropping transmission request: cannot drop queue head and limit is one\n");
1468 if (priority <= minp->priority)
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Dropping transmission request: priority too low\n");
1475 return NULL; /* priority too low */
1477 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, minp);
1479 GNUNET_assert (0 == minp->get_message (minp->get_message_cls, 0, NULL));
1483 /* Order entries by deadline, but SKIP 'HEAD' if
1484 * we're in the 'ready_peer_*' DLL */
1485 pos = pr->pending_head;
1486 if ((pr->prev != NULL) || (pr->next != NULL) ||
1487 (pr == handle->ready_peer_head))
1489 GNUNET_assert (pos != NULL);
1490 pos = pos->next; /* skip head */
1493 /* insertion sort */
1495 while ((pos != NULL) && (pos->timeout.abs_value < th->timeout.abs_value))
1500 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head, pr->pending_tail, prev,
1503 /* was the request queue previously empty? */
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission request added to queue\n");
1507 if ((pr->pending_head == th) && (pr->ntr_task == GNUNET_SCHEDULER_NO_TASK) &&
1508 (pr->next == NULL) && (pr->prev == NULL) &&
1509 (handle->ready_peer_head != pr))
1511 GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1517 * Cancel the specified transmission-ready notification.
1519 * @param th handle that was returned by "notify_transmit_ready".
1522 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1524 struct PeerRecord *pr = th->peer;
1525 struct GNUNET_CORE_Handle *h = pr->ch;
1528 was_head = (pr->pending_head == th);
1529 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
1533 /* we're currently in the control queue, remove */
1534 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1535 h->control_pending_tail, th->cm);
1536 GNUNET_free (th->cm);
1541 if ((pr->prev != NULL) || (pr->next != NULL) || (pr == h->ready_peer_head))
1543 /* the request that was 'approved' by core was
1544 * canceled before it could be transmitted; remove
1545 * us from the 'ready' list */
1546 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1548 request_next_transmission (pr);
1553 /* end of core_api.c */