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 GNUNET_HashCode * key,
363 struct GNUNET_CORE_Handle *h = cls;
364 struct GNUNET_CORE_TransmitHandle *th;
365 struct PeerRecord *pr = value;
367 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
369 GNUNET_SCHEDULER_cancel (pr->timeout_task);
370 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
372 if (pr->ntr_task != GNUNET_SCHEDULER_NO_TASK)
374 GNUNET_SCHEDULER_cancel (pr->ntr_task);
375 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
377 if ((pr->prev != NULL) || (pr->next != NULL) || (h->ready_peer_head == pr))
378 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
379 if (h->disconnects != NULL)
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 (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
395 GNUNET_assert (pr->ntr_task == GNUNET_SCHEDULER_NO_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 (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
416 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
419 if (h->client != NULL)
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 (cm->cont != NULL)
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);
444 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->retry_backoff);
445 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
450 * Check the list of pending requests, send the next
453 * @param h core handle
454 * @param ignore_currently_down transmit message even if not initialized?
457 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
461 * The given request hit its timeout. Remove from the
462 * doubly-linked list and call the respective continuation.
464 * @param cls the transmit handle of the request that timed out
465 * @param tc context, can be NULL (!)
468 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
472 * Send a control message to the peer asking for transmission
473 * of the message in the given peer record.
475 * @param pr peer to request transmission to
478 request_next_transmission (struct PeerRecord *pr)
480 struct GNUNET_CORE_Handle *h = pr->ch;
481 struct ControlMessage *cm;
482 struct SendMessageRequest *smr;
483 struct GNUNET_CORE_TransmitHandle *th;
485 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
487 GNUNET_SCHEDULER_cancel (pr->timeout_task);
488 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
491 if (NULL == th->peer)
493 trigger_next_request (h, GNUNET_NO);
497 return; /* already done */
498 GNUNET_assert (pr->prev == NULL);
499 GNUNET_assert (pr->next == NULL);
501 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
502 (th->timeout), &transmission_timeout, pr);
503 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
504 sizeof (struct SendMessageRequest));
507 smr = (struct SendMessageRequest *) &cm[1];
508 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
509 smr->header.size = htons (sizeof (struct SendMessageRequest));
510 smr->priority = htonl (th->priority);
511 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
512 smr->peer = pr->peer;
513 smr->reserved = htonl (0);
514 smr->size = htons (th->msize);
515 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
516 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
517 h->control_pending_tail, cm);
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Adding SEND REQUEST for peer `%s' to message queue\n",
520 GNUNET_i2s (&pr->peer));
521 trigger_next_request (h, GNUNET_NO);
526 * The given request hit its timeout. Remove from the
527 * doubly-linked list and call the respective continuation.
529 * @param cls the transmit handle of the request that timed out
530 * @param tc context, can be NULL (!)
533 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
535 struct PeerRecord *pr = cls;
536 struct GNUNET_CORE_Handle *h = pr->ch;
537 struct GNUNET_CORE_TransmitHandle *th;
539 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
542 if ((pr->prev != NULL) || (pr->next != NULL) || (pr == h->ready_peer_head))
544 /* the request that was 'approved' by core was
545 * canceled before it could be transmitted; remove
546 * us from the 'ready' list */
547 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
551 /* we're currently in the control queue, remove */
552 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
553 h->control_pending_tail, th->cm);
554 GNUNET_free (th->cm);
556 LOG (GNUNET_ERROR_TYPE_DEBUG,
557 "Signalling timeout of request for transmission to CORE service\n");
558 trigger_next_request (h, GNUNET_NO);
559 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
564 * Transmit the next message to the core service.
566 * @param cls closure with the 'struct GNUNET_CORE_Handle'
567 * @param size number of bytes available in buf
568 * @param buf where the callee should write the message
569 * @return number of bytes written to buf
572 transmit_message (void *cls, size_t size, void *buf)
574 struct GNUNET_CORE_Handle *h = cls;
575 struct ControlMessage *cm;
576 struct GNUNET_CORE_TransmitHandle *th;
577 struct PeerRecord *pr;
578 struct SendMessage *sm;
579 const struct GNUNET_MessageHeader *hdr;
583 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
587 LOG (GNUNET_ERROR_TYPE_DEBUG,
588 "Transmission failed, initiating reconnect\n");
592 /* first check for control messages */
593 if (NULL != (cm = h->control_pending_head))
595 hdr = (const struct GNUNET_MessageHeader *) &cm[1];
596 msize = ntohs (hdr->size);
599 trigger_next_request (h, GNUNET_NO);
602 LOG (GNUNET_ERROR_TYPE_DEBUG,
603 "Transmitting control message with %u bytes of type %u to core.\n",
604 (unsigned int) msize, (unsigned int) ntohs (hdr->type));
605 memcpy (buf, hdr, msize);
606 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
607 h->control_pending_tail, cm);
610 if (NULL != cm->cont)
611 cm->cont (cm->cont_cls, GNUNET_OK);
613 trigger_next_request (h, GNUNET_NO);
616 /* now check for 'ready' P2P messages */
617 if (NULL == (pr = h->ready_peer_head))
619 GNUNET_assert (NULL != pr->th.peer);
621 if (size < th->msize + sizeof (struct SendMessage))
623 trigger_next_request (h, GNUNET_NO);
626 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
628 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
630 GNUNET_SCHEDULER_cancel (pr->timeout_task);
631 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
633 LOG (GNUNET_ERROR_TYPE_DEBUG,
634 "Transmitting SEND request to `%s' with %u bytes.\n",
635 GNUNET_i2s (&pr->peer), (unsigned int) th->msize);
636 sm = (struct SendMessage *) buf;
637 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
638 sm->priority = htonl (th->priority);
639 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
641 sm->cork = htonl ((uint32_t) th->cork);
642 sm->reserved = htonl (0);
644 th->get_message (th->get_message_cls,
645 size - sizeof (struct SendMessage), &sm[1]);
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "Transmitting SEND request to `%s' yielded %u bytes.\n",
649 GNUNET_i2s (&pr->peer), ret);
652 LOG (GNUNET_ERROR_TYPE_DEBUG,
653 "Size of clients message to peer %s is 0!\n",
654 GNUNET_i2s (&pr->peer));
655 /* client decided to send nothing! */
656 request_next_transmission (pr);
659 LOG (GNUNET_ERROR_TYPE_DEBUG,
660 "Produced SEND message to core with %u bytes payload\n",
662 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
663 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
666 request_next_transmission (pr);
669 ret += sizeof (struct SendMessage);
670 sm->header.size = htons (ret);
671 GNUNET_assert (ret <= size);
672 request_next_transmission (pr);
678 * Check the list of pending requests, send the next
681 * @param h core handle
682 * @param ignore_currently_down transmit message even if not initialized?
685 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down)
689 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
691 LOG (GNUNET_ERROR_TYPE_DEBUG,
692 "Core connection down, not processing queue\n");
697 LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
700 if (h->control_pending_head != NULL)
702 ntohs (((struct GNUNET_MessageHeader *) &h->
703 control_pending_head[1])->size);
704 else if (h->ready_peer_head != NULL)
706 h->ready_peer_head->th.msize + sizeof (struct SendMessage);
709 LOG (GNUNET_ERROR_TYPE_DEBUG,
710 "Request queue empty, not processing queue\n");
711 return; /* no pending message */
714 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
715 GNUNET_TIME_UNIT_FOREVER_REL,
716 GNUNET_NO, &transmit_message, h);
721 * Handler for notification messages received from the core.
723 * @param cls our "struct GNUNET_CORE_Handle"
724 * @param msg the message received from the core service
727 main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
729 struct GNUNET_CORE_Handle *h = cls;
730 const struct InitReplyMessage *m;
731 const struct ConnectNotifyMessage *cnm;
732 const struct DisconnectNotifyMessage *dnm;
733 const struct NotifyTrafficMessage *ntm;
734 const struct GNUNET_MessageHeader *em;
735 const struct SendMessageReady *smr;
736 const struct GNUNET_CORE_MessageHandler *mh;
737 const struct GNUNET_ATS_Information *ats;
738 GNUNET_CORE_StartupCallback init;
739 struct PeerRecord *pr;
740 struct GNUNET_CORE_TransmitHandle *th;
749 LOG (GNUNET_ERROR_TYPE_INFO,
750 _("Client was disconnected from core service, trying to reconnect.\n"));
754 msize = ntohs (msg->size);
755 LOG (GNUNET_ERROR_TYPE_DEBUG,
756 "Processing message of type %u and size %u from core service\n",
757 ntohs (msg->type), msize);
758 switch (ntohs (msg->type))
760 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
761 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
767 m = (const struct InitReplyMessage *) msg;
768 GNUNET_break (0 == ntohl (m->reserved));
769 /* start our message processing loop */
770 if (GNUNET_YES == h->currently_down)
772 h->currently_down = GNUNET_NO;
773 trigger_next_request (h, GNUNET_NO);
775 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
776 h->me = m->my_identity;
777 if (NULL != (init = h->init))
779 /* mark so we don't call init on reconnect */
781 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to core service of peer `%s'.\n",
782 GNUNET_i2s (&h->me));
783 init (h->cls, h, &h->me);
787 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 "Successfully reconnected to core service.\n");
790 /* fake 'connect to self' */
791 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &h->me.hashPubKey);
792 GNUNET_assert (NULL == pr);
793 pr = GNUNET_malloc (sizeof (struct PeerRecord));
796 GNUNET_assert (GNUNET_YES ==
797 GNUNET_CONTAINER_multihashmap_put (h->peers,
798 &h->me.hashPubKey, pr,
799 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
800 if (NULL != h->connects)
801 h->connects (h->cls, &h->me, NULL, 0);
803 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
804 if (msize < sizeof (struct ConnectNotifyMessage))
810 cnm = (const struct ConnectNotifyMessage *) msg;
811 ats_count = ntohl (cnm->ats_count);
813 sizeof (struct ConnectNotifyMessage) +
814 ats_count * sizeof (struct GNUNET_ATS_Information))
820 LOG (GNUNET_ERROR_TYPE_DEBUG,
821 "Received notification about connection from `%s'.\n",
822 GNUNET_i2s (&cnm->peer));
823 if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
825 /* connect to self!? */
829 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &cnm->peer.hashPubKey);
836 pr = GNUNET_malloc (sizeof (struct PeerRecord));
837 pr->peer = cnm->peer;
839 GNUNET_assert (GNUNET_YES ==
840 GNUNET_CONTAINER_multihashmap_put (h->peers,
841 &cnm->peer.hashPubKey, pr,
842 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
843 ats = (const struct GNUNET_ATS_Information *) &cnm[1];
844 if (NULL != h->connects)
845 h->connects (h->cls, &cnm->peer, ats, ats_count);
847 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
848 if (msize != sizeof (struct DisconnectNotifyMessage))
854 dnm = (const struct DisconnectNotifyMessage *) msg;
855 if (0 == memcmp (&h->me, &dnm->peer, sizeof (struct GNUNET_PeerIdentity)))
857 /* connection to self!? */
861 GNUNET_break (0 == ntohl (dnm->reserved));
862 LOG (GNUNET_ERROR_TYPE_DEBUG,
863 "Received notification about disconnect from `%s'.\n",
864 GNUNET_i2s (&dnm->peer));
865 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &dnm->peer.hashPubKey);
872 trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
873 (h->ready_peer_head == pr));
874 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
876 trigger_next_request (h, GNUNET_NO);
878 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
879 if (msize < sizeof (struct NotifyTrafficMessage))
885 ntm = (const struct NotifyTrafficMessage *) msg;
886 ats_count = ntohl (ntm->ats_count);
888 sizeof (struct NotifyTrafficMessage) +
889 ats_count * sizeof (struct GNUNET_ATS_Information) +
890 sizeof (struct GNUNET_MessageHeader)) )
896 ats = (const struct GNUNET_ATS_Information*) &ntm[1];
897 em = (const struct GNUNET_MessageHeader *) &ats[ats_count];
898 LOG (GNUNET_ERROR_TYPE_DEBUG,
899 "Received message of type %u and size %u from peer `%4s'\n",
900 ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
901 if ((GNUNET_NO == h->inbound_hdr_only) &&
903 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
904 +ats_count * sizeof (struct GNUNET_ATS_Information)))
910 et = ntohs (em->type);
911 for (hpos = 0; hpos < h->hcnt; hpos++)
913 mh = &h->handlers[hpos];
916 if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
919 "Unexpected message size %u for message of type %u from peer `%4s'\n",
920 htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
924 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &ntm->peer.hashPubKey);
932 h->handlers[hpos].callback (h->cls, &ntm->peer, em, ats,
935 /* error in processing, do not process other messages! */
939 if (NULL != h->inbound_notify)
940 h->inbound_notify (h->cls, &ntm->peer, em, ats, ats_count);
942 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
943 if (msize < sizeof (struct NotifyTrafficMessage))
949 ntm = (const struct NotifyTrafficMessage *) msg;
950 ats_count = ntohl (ntm->ats_count);
952 sizeof (struct NotifyTrafficMessage) +
953 ats_count * sizeof (struct GNUNET_ATS_Information) +
954 sizeof (struct GNUNET_MessageHeader)) )
960 ats = (const struct GNUNET_ATS_Information*) &ntm[1];
961 em = (const struct GNUNET_MessageHeader *) &ats[ats_count];
962 LOG (GNUNET_ERROR_TYPE_DEBUG,
963 "Received notification about transmission to `%s'.\n",
964 GNUNET_i2s (&ntm->peer));
965 if ((GNUNET_NO == h->outbound_hdr_only) &&
967 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
968 ats_count * sizeof (struct GNUNET_ATS_Information)))
974 if (NULL == h->outbound_notify)
979 h->outbound_notify (h->cls, &ntm->peer, em, ats, ats_count);
981 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
982 if (msize != sizeof (struct SendMessageReady))
988 smr = (const struct SendMessageReady *) msg;
989 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &smr->peer.hashPubKey);
996 LOG (GNUNET_ERROR_TYPE_DEBUG,
997 "Received notification about transmission readiness to `%s'.\n",
998 GNUNET_i2s (&smr->peer));
999 if (NULL == pr->th.peer)
1001 /* request must have been cancelled between the original request
1002 * and the response from core, ignore core's readiness */
1007 if (ntohs (smr->smr_id) != th->smr_id)
1009 /* READY message is for expired or cancelled message,
1010 * ignore! (we should have already sent another request) */
1013 if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
1015 /* we should not already be on the ready list... */
1017 reconnect_later (h);
1020 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head, h->ready_peer_tail, pr);
1021 trigger_next_request (h, GNUNET_NO);
1024 reconnect_later (h);
1027 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1028 GNUNET_TIME_UNIT_FOREVER_REL);
1033 * Task executed once we are done transmitting the INIT message.
1034 * Starts our 'receive' loop.
1036 * @param cls the 'struct GNUNET_CORE_Handle'
1037 * @param success were we successful
1040 init_done_task (void *cls, int success)
1042 struct GNUNET_CORE_Handle *h = cls;
1044 if (GNUNET_SYSERR == success)
1045 return; /* shutdown */
1046 if (GNUNET_NO == success)
1048 LOG (GNUNET_ERROR_TYPE_DEBUG,
1049 "Failed to exchange INIT with core, retrying\n");
1050 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1051 reconnect_later (h);
1054 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1055 GNUNET_TIME_UNIT_FOREVER_REL);
1060 * Our current client connection went down. Clean it up
1061 * and try to reconnect!
1063 * @param h our handle to the core service
1066 reconnect (struct GNUNET_CORE_Handle *h)
1068 struct ControlMessage *cm;
1069 struct InitMessage *init;
1075 GNUNET_assert (NULL == h->client);
1076 GNUNET_assert (h->currently_down == GNUNET_YES);
1077 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1078 if (NULL == h->client)
1080 reconnect_later (h);
1083 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1084 cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1085 cm->cont = &init_done_task;
1087 init = (struct InitMessage *) &cm[1];
1088 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1089 init->header.size = htons (msize);
1091 if (h->inbound_notify != NULL)
1093 if (h->inbound_hdr_only)
1094 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1096 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1098 if (h->outbound_notify != NULL)
1100 if (h->outbound_hdr_only)
1101 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1103 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1105 LOG (GNUNET_ERROR_TYPE_INFO,
1106 "(Re)connecting to CORE service, monitoring messages of type %u\n",
1109 init->options = htonl (opt);
1110 ts = (uint16_t *) & init[1];
1111 for (hpos = 0; hpos < h->hcnt; hpos++)
1112 ts[hpos] = htons (h->handlers[hpos].type);
1113 GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1115 trigger_next_request (h, GNUNET_YES);
1121 * Connect to the core service. Note that the connection may
1122 * complete (or fail) asynchronously.
1124 * @param cfg configuration to use
1125 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1126 * @param init callback to call once we have successfully
1127 * connected to the core service
1128 * @param connects function to call on peer connect, can be NULL
1129 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1130 * @param inbound_notify function to call for all inbound messages, can be NULL
1131 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1132 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1133 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1134 * @param outbound_notify function to call for all outbound messages, can be NULL
1135 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1136 * GNUNET_MessageHeader and hence we do not need to give it the full message
1137 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1138 * @param handlers callbacks for messages we care about, NULL-terminated
1139 * @return handle to the core service (only useful for disconnect until 'init' is called);
1140 * NULL on error (in this case, init is never called)
1142 struct GNUNET_CORE_Handle *
1143 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1145 GNUNET_CORE_StartupCallback init,
1146 GNUNET_CORE_ConnectEventHandler connects,
1147 GNUNET_CORE_DisconnectEventHandler disconnects,
1148 GNUNET_CORE_MessageCallback inbound_notify,
1149 int inbound_hdr_only,
1150 GNUNET_CORE_MessageCallback outbound_notify,
1151 int outbound_hdr_only,
1152 const struct GNUNET_CORE_MessageHandler *handlers)
1154 struct GNUNET_CORE_Handle *h;
1156 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1160 h->connects = connects;
1161 h->disconnects = disconnects;
1162 h->inbound_notify = inbound_notify;
1163 h->outbound_notify = outbound_notify;
1164 h->inbound_hdr_only = inbound_hdr_only;
1165 h->outbound_hdr_only = outbound_hdr_only;
1166 h->handlers = handlers;
1168 h->currently_down = GNUNET_YES;
1169 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1170 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1171 if (NULL != handlers)
1172 while (handlers[h->hcnt].callback != NULL)
1174 GNUNET_assert (h->hcnt <
1175 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1176 sizeof (struct InitMessage)) / sizeof (uint16_t));
1177 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
1184 * Disconnect from the core service. This function can only
1185 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1186 * requests have been explicitly canceled.
1188 * @param handle connection to core to disconnect
1191 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1193 struct ControlMessage *cm;
1195 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1196 if (NULL != handle->cth)
1198 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1201 while (NULL != (cm = handle->control_pending_head))
1203 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1204 handle->control_pending_tail, cm);
1207 if (NULL != cm->cont)
1208 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1211 if (NULL != handle->client)
1213 GNUNET_CLIENT_disconnect (handle->client);
1214 handle->client = NULL;
1216 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1217 &disconnect_and_free_peer_entry,
1219 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1221 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1222 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1224 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1225 handle->peers = NULL;
1226 GNUNET_break (handle->ready_peer_head == NULL);
1227 GNUNET_free (handle);
1232 * Task that calls 'request_next_transmission'.
1234 * @param cls the 'struct PeerRecord*'
1235 * @param tc scheduler context
1238 run_request_next_transmission (void *cls,
1239 const struct GNUNET_SCHEDULER_TaskContext *tc)
1241 struct PeerRecord *pr = cls;
1243 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1244 request_next_transmission (pr);
1249 * Ask the core to call "notify" once it is ready to transmit the
1250 * given number of bytes to the specified "target". Must only be
1251 * called after a connection to the respective peer has been
1252 * established (and the client has been informed about this). You may
1253 * have one request of this type pending for each connected peer at
1254 * any time. If a peer disconnects, the application MUST call
1255 * "GNUNET_CORE_notify_transmit_ready_cancel" on the respective
1256 * transmission request, if one such request is pending.
1258 * @param handle connection to core service
1259 * @param cork is corking allowed for this transmission?
1260 * @param priority how important is the message?
1261 * @param maxdelay how long can the message wait?
1262 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1263 * @param notify_size how many bytes of buffer space does notify want?
1264 * @param notify function to call when buffer space is available;
1265 * will be called with NULL on timeout; clients MUST cancel
1266 * all pending transmission requests DURING the disconnect
1268 * @param notify_cls closure for notify
1269 * @return non-NULL if the notify callback was queued,
1270 * NULL if we can not even queue the request (request already pending);
1271 * if NULL is returned, "notify" will NOT be called.
1273 struct GNUNET_CORE_TransmitHandle *
1274 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
1276 struct GNUNET_TIME_Relative maxdelay,
1277 const struct GNUNET_PeerIdentity *target,
1279 GNUNET_CONNECTION_TransmitReadyNotify notify,
1282 struct PeerRecord *pr;
1283 struct GNUNET_CORE_TransmitHandle *th;
1285 GNUNET_assert (NULL != notify);
1286 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers, &target->hashPubKey);
1289 /* attempt to send to peer that is not connected */
1293 if (NULL != pr->th.peer)
1295 /* attempting to queue a second request for the same destination */
1299 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1300 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1302 memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1304 th->get_message = notify;
1305 th->get_message_cls = notify_cls;
1306 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1307 th->priority = priority;
1308 th->msize = notify_size;
1311 GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1312 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission request added to queue\n");
1318 * Cancel the specified transmission-ready notification.
1320 * @param th handle that was returned by "notify_transmit_ready".
1323 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1325 struct PeerRecord *pr = th->peer;
1326 struct GNUNET_CORE_Handle *h;
1328 GNUNET_assert (NULL != pr);
1333 /* we're currently in the control queue, remove */
1334 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1335 h->control_pending_tail, th->cm);
1336 GNUNET_free (th->cm);
1339 if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
1341 /* the request that was 'approved' by core was
1342 * canceled before it could be transmitted; remove
1343 * us from the 'ready' list */
1344 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1349 /* end of core_api.c */