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_MultiPeerMap *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,
361 const struct GNUNET_PeerIdentity *key,
364 struct GNUNET_CORE_Handle *h = cls;
365 struct GNUNET_CORE_TransmitHandle *th;
366 struct PeerRecord *pr = value;
368 if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
370 GNUNET_SCHEDULER_cancel (pr->timeout_task);
371 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
373 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
375 GNUNET_SCHEDULER_cancel (pr->ntr_task);
376 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
378 if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
379 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
380 if (NULL != h->disconnects)
381 h->disconnects (h->cls, &pr->peer);
382 /* all requests should have been cancelled, clean up anyway, just in case */
384 if (NULL != th->peer)
391 /* done with 'voluntary' cleanups, now on to normal freeing */
392 GNUNET_assert (GNUNET_YES ==
393 GNUNET_CONTAINER_multipeermap_remove (h->peers, key, pr));
394 GNUNET_assert (pr->ch == h);
395 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->timeout_task);
396 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
403 * Close down any existing connection to the CORE service and
404 * try re-establishing it later.
406 * @param h our handle
409 reconnect_later (struct GNUNET_CORE_Handle *h)
411 struct ControlMessage *cm;
412 struct PeerRecord *pr;
414 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == h->reconnect_task);
417 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
420 if (NULL != h->client)
422 GNUNET_CLIENT_disconnect (h->client);
425 h->currently_down = GNUNET_YES;
426 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
428 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_task, h);
429 while (NULL != (cm = h->control_pending_head))
431 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
432 h->control_pending_tail, cm);
435 if (NULL != cm->cont)
436 cm->cont (cm->cont_cls, GNUNET_NO);
439 GNUNET_CONTAINER_multipeermap_iterate (h->peers,
440 &disconnect_and_free_peer_entry, h);
441 while (NULL != (pr = h->ready_peer_head))
442 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
443 GNUNET_assert (h->control_pending_head == NULL);
444 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
449 * Check the list of pending requests, send the next
452 * @param h core handle
453 * @param ignore_currently_down transmit message even if not initialized?
456 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
460 * The given request hit its timeout. Remove from the
461 * doubly-linked list and call the respective continuation.
463 * @param cls the transmit handle of the request that timed out
464 * @param tc context, can be NULL (!)
467 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
471 * Send a control message to the peer asking for transmission
472 * of the message in the given peer record.
474 * @param pr peer to request transmission to
477 request_next_transmission (struct PeerRecord *pr)
479 struct GNUNET_CORE_Handle *h = pr->ch;
480 struct ControlMessage *cm;
481 struct SendMessageRequest *smr;
482 struct GNUNET_CORE_TransmitHandle *th;
484 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
486 GNUNET_SCHEDULER_cancel (pr->timeout_task);
487 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
490 if (NULL == th->peer)
492 trigger_next_request (h, GNUNET_NO);
496 return; /* already done */
497 GNUNET_assert (pr->prev == NULL);
498 GNUNET_assert (pr->next == NULL);
500 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
501 (th->timeout), &transmission_timeout, pr);
502 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
503 sizeof (struct SendMessageRequest));
506 smr = (struct SendMessageRequest *) &cm[1];
507 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
508 smr->header.size = htons (sizeof (struct SendMessageRequest));
509 smr->priority = htonl (th->priority);
510 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
511 smr->peer = pr->peer;
512 smr->reserved = htonl (0);
513 smr->size = htons (th->msize);
514 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
515 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
516 h->control_pending_tail, cm);
517 LOG (GNUNET_ERROR_TYPE_DEBUG,
518 "Adding SEND REQUEST for peer `%s' to message queue\n",
519 GNUNET_i2s (&pr->peer));
520 trigger_next_request (h, GNUNET_NO);
525 * The given request hit its timeout. Remove from the
526 * doubly-linked list and call the respective continuation.
528 * @param cls the transmit handle of the request that timed out
529 * @param tc context, can be NULL (!)
532 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
534 struct PeerRecord *pr = cls;
535 struct GNUNET_CORE_Handle *h = pr->ch;
536 struct GNUNET_CORE_TransmitHandle *th;
538 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
539 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
541 GNUNET_SCHEDULER_cancel (pr->ntr_task);
542 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
546 if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
548 /* the request that was 'approved' by core was
549 * canceled before it could be transmitted; remove
550 * us from the 'ready' list */
551 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
555 /* we're currently in the control queue, remove */
556 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
557 h->control_pending_tail, th->cm);
558 GNUNET_free (th->cm);
560 LOG (GNUNET_ERROR_TYPE_DEBUG,
561 "Signalling timeout of request for transmission to peer `%s' via CORE\n",
562 GNUNET_i2s (&pr->peer));
563 trigger_next_request (h, GNUNET_NO);
565 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
570 * Transmit the next message to the core service.
572 * @param cls closure with the 'struct GNUNET_CORE_Handle'
573 * @param size number of bytes available in @a buf
574 * @param buf where the callee should write the message
575 * @return number of bytes written to buf
578 transmit_message (void *cls, size_t size, void *buf)
580 struct GNUNET_CORE_Handle *h = cls;
581 struct ControlMessage *cm;
582 struct GNUNET_CORE_TransmitHandle *th;
583 struct PeerRecord *pr;
584 struct SendMessage *sm;
585 const struct GNUNET_MessageHeader *hdr;
589 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Transmission failed, initiating reconnect\n");
598 /* first check for control messages */
599 if (NULL != (cm = h->control_pending_head))
601 hdr = (const struct GNUNET_MessageHeader *) &cm[1];
602 msize = ntohs (hdr->size);
605 trigger_next_request (h, GNUNET_NO);
608 LOG (GNUNET_ERROR_TYPE_DEBUG,
609 "Transmitting control message with %u bytes of type %u to core.\n",
610 (unsigned int) msize, (unsigned int) ntohs (hdr->type));
611 memcpy (buf, hdr, msize);
612 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
613 h->control_pending_tail, cm);
616 if (NULL != cm->cont)
617 cm->cont (cm->cont_cls, GNUNET_OK);
619 trigger_next_request (h, GNUNET_NO);
622 /* now check for 'ready' P2P messages */
623 if (NULL == (pr = h->ready_peer_head))
625 GNUNET_assert (NULL != pr->th.peer);
627 if (size < th->msize + sizeof (struct SendMessage))
629 trigger_next_request (h, GNUNET_NO);
632 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
634 if (GNUNET_SCHEDULER_NO_TASK != pr->timeout_task)
636 GNUNET_SCHEDULER_cancel (pr->timeout_task);
637 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
639 LOG (GNUNET_ERROR_TYPE_DEBUG,
640 "Transmitting SEND request to `%s' with %u bytes.\n",
641 GNUNET_i2s (&pr->peer), (unsigned int) th->msize);
642 sm = (struct SendMessage *) buf;
643 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
644 sm->priority = htonl (th->priority);
645 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
647 sm->cork = htonl ((uint32_t) th->cork);
648 sm->reserved = htonl (0);
650 th->get_message (th->get_message_cls,
651 size - sizeof (struct SendMessage), &sm[1]);
653 LOG (GNUNET_ERROR_TYPE_DEBUG,
654 "Transmitting SEND request to `%s' yielded %u bytes.\n",
655 GNUNET_i2s (&pr->peer), ret);
658 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Size of clients message to peer %s is 0!\n",
660 GNUNET_i2s (&pr->peer));
661 /* client decided to send nothing! */
662 request_next_transmission (pr);
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Produced SEND message to core with %u bytes payload\n",
668 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
669 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
672 request_next_transmission (pr);
675 ret += sizeof (struct SendMessage);
676 sm->header.size = htons (ret);
677 GNUNET_assert (ret <= size);
678 request_next_transmission (pr);
684 * Check the list of pending requests, send the next
687 * @param h core handle
688 * @param ignore_currently_down transmit message even if not initialized?
691 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down)
695 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
697 LOG (GNUNET_ERROR_TYPE_DEBUG,
698 "Core connection down, not processing queue\n");
703 LOG (GNUNET_ERROR_TYPE_DEBUG, "Request pending, not processing queue\n");
706 if (NULL != h->control_pending_head)
708 ntohs (((struct GNUNET_MessageHeader *) &h->
709 control_pending_head[1])->size);
710 else if (h->ready_peer_head != NULL)
712 h->ready_peer_head->th.msize + sizeof (struct SendMessage);
715 LOG (GNUNET_ERROR_TYPE_DEBUG,
716 "Request queue empty, not processing queue\n");
717 return; /* no pending message */
720 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
721 GNUNET_TIME_UNIT_FOREVER_REL,
722 GNUNET_NO, &transmit_message, h);
727 * Handler for notification messages received from the core.
729 * @param cls our "struct GNUNET_CORE_Handle"
730 * @param msg the message received from the core service
733 main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
735 struct GNUNET_CORE_Handle *h = cls;
736 const struct InitReplyMessage *m;
737 const struct ConnectNotifyMessage *cnm;
738 const struct DisconnectNotifyMessage *dnm;
739 const struct NotifyTrafficMessage *ntm;
740 const struct GNUNET_MessageHeader *em;
741 const struct SendMessageReady *smr;
742 const struct GNUNET_CORE_MessageHandler *mh;
743 GNUNET_CORE_StartupCallback init;
744 struct PeerRecord *pr;
745 struct GNUNET_CORE_TransmitHandle *th;
752 LOG (GNUNET_ERROR_TYPE_INFO,
753 _("Client was disconnected from core service, trying to reconnect.\n"));
757 msize = ntohs (msg->size);
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Processing message of type %u and size %u from core service\n",
760 ntohs (msg->type), msize);
761 switch (ntohs (msg->type))
763 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
764 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
770 m = (const struct InitReplyMessage *) msg;
771 GNUNET_break (0 == ntohl (m->reserved));
772 /* start our message processing loop */
773 if (GNUNET_YES == h->currently_down)
775 h->currently_down = GNUNET_NO;
776 trigger_next_request (h, GNUNET_NO);
778 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
779 h->me = m->my_identity;
780 if (NULL != (init = h->init))
782 /* mark so we don't call init on reconnect */
784 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to core service of peer `%s'.\n",
785 GNUNET_i2s (&h->me));
786 init (h->cls, &h->me);
790 LOG (GNUNET_ERROR_TYPE_DEBUG,
791 "Successfully reconnected to core service.\n");
793 /* fake 'connect to self' */
794 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &h->me);
795 GNUNET_assert (NULL == pr);
796 pr = GNUNET_new (struct PeerRecord);
799 GNUNET_assert (GNUNET_YES ==
800 GNUNET_CONTAINER_multipeermap_put (h->peers,
802 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
803 if (NULL != h->connects)
804 h->connects (h->cls, &h->me);
806 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
807 if (msize < sizeof (struct ConnectNotifyMessage))
813 cnm = (const struct ConnectNotifyMessage *) msg;
815 sizeof (struct ConnectNotifyMessage))
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Received notification about connection from `%s'.\n",
823 GNUNET_i2s (&cnm->peer));
824 if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
826 /* connect to self!? */
830 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &cnm->peer);
837 pr = GNUNET_new (struct PeerRecord);
838 pr->peer = cnm->peer;
840 GNUNET_assert (GNUNET_YES ==
841 GNUNET_CONTAINER_multipeermap_put (h->peers,
843 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
844 if (NULL != h->connects)
845 h->connects (h->cls, &cnm->peer);
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_multipeermap_get (h->peers, &dnm->peer);
872 trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
873 (h->ready_peer_head == pr));
874 disconnect_and_free_peer_entry (h, &dnm->peer, 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;
887 sizeof (struct NotifyTrafficMessage) +
888 sizeof (struct GNUNET_MessageHeader)) )
894 em = (const struct GNUNET_MessageHeader *) &ntm[1];
895 LOG (GNUNET_ERROR_TYPE_DEBUG,
896 "Received message of type %u and size %u from peer `%4s'\n",
897 ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
898 if ((GNUNET_NO == h->inbound_hdr_only) &&
900 ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
906 et = ntohs (em->type);
907 for (hpos = 0; hpos < h->hcnt; hpos++)
909 mh = &h->handlers[hpos];
912 if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
914 LOG (GNUNET_ERROR_TYPE_ERROR,
915 "Unexpected message size %u for message of type %u from peer `%4s'\n",
916 htons (em->size), mh->type, GNUNET_i2s (&ntm->peer));
920 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &ntm->peer);
928 h->handlers[hpos].callback (h->cls, &ntm->peer, em))
930 /* error in processing, do not process other messages! */
934 if (NULL != h->inbound_notify)
935 h->inbound_notify (h->cls, &ntm->peer, em);
937 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
938 if (msize < sizeof (struct NotifyTrafficMessage))
944 ntm = (const struct NotifyTrafficMessage *) msg;
946 sizeof (struct NotifyTrafficMessage) +
947 sizeof (struct GNUNET_MessageHeader)) )
953 em = (const struct GNUNET_MessageHeader *) &ntm[1];
954 LOG (GNUNET_ERROR_TYPE_DEBUG,
955 "Received notification about transmission to `%s'.\n",
956 GNUNET_i2s (&ntm->peer));
957 if ((GNUNET_NO == h->outbound_hdr_only) &&
959 ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
965 if (NULL == h->outbound_notify)
970 h->outbound_notify (h->cls, &ntm->peer, em);
972 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
973 if (msize != sizeof (struct SendMessageReady))
979 smr = (const struct SendMessageReady *) msg;
980 pr = GNUNET_CONTAINER_multipeermap_get (h->peers, &smr->peer);
987 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 "Received notification about transmission readiness to `%s'.\n",
989 GNUNET_i2s (&smr->peer));
990 if (NULL == pr->th.peer)
992 /* request must have been cancelled between the original request
993 * and the response from core, ignore core's readiness */
998 if (ntohs (smr->smr_id) != th->smr_id)
1000 /* READY message is for expired or cancelled message,
1001 * ignore! (we should have already sent another request) */
1004 if ((NULL != pr->prev) || (NULL != pr->next) || (h->ready_peer_head == pr))
1006 /* we should not already be on the ready list... */
1008 reconnect_later (h);
1011 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head, h->ready_peer_tail, pr);
1012 trigger_next_request (h, GNUNET_NO);
1015 reconnect_later (h);
1018 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1019 GNUNET_TIME_UNIT_FOREVER_REL);
1024 * Task executed once we are done transmitting the INIT message.
1025 * Starts our 'receive' loop.
1027 * @param cls the 'struct GNUNET_CORE_Handle'
1028 * @param success were we successful
1031 init_done_task (void *cls, int success)
1033 struct GNUNET_CORE_Handle *h = cls;
1035 if (GNUNET_SYSERR == success)
1036 return; /* shutdown */
1037 if (GNUNET_NO == success)
1039 LOG (GNUNET_ERROR_TYPE_DEBUG,
1040 "Failed to exchange INIT with core, retrying\n");
1041 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1042 reconnect_later (h);
1045 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1046 GNUNET_TIME_UNIT_FOREVER_REL);
1051 * Our current client connection went down. Clean it up
1052 * and try to reconnect!
1054 * @param h our handle to the core service
1057 reconnect (struct GNUNET_CORE_Handle *h)
1059 struct ControlMessage *cm;
1060 struct InitMessage *init;
1066 GNUNET_assert (NULL == h->client);
1067 GNUNET_assert (h->currently_down == GNUNET_YES);
1068 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1069 if (NULL == h->client)
1071 reconnect_later (h);
1074 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1075 cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1076 cm->cont = &init_done_task;
1078 init = (struct InitMessage *) &cm[1];
1079 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1080 init->header.size = htons (msize);
1082 if (h->inbound_notify != NULL)
1084 if (h->inbound_hdr_only)
1085 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1087 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1089 if (h->outbound_notify != NULL)
1091 if (h->outbound_hdr_only)
1092 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1094 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1096 LOG (GNUNET_ERROR_TYPE_INFO,
1097 "(Re)connecting to CORE service, monitoring messages of type %u\n",
1100 init->options = htonl (opt);
1101 ts = (uint16_t *) & init[1];
1102 for (hpos = 0; hpos < h->hcnt; hpos++)
1103 ts[hpos] = htons (h->handlers[hpos].type);
1104 GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1106 trigger_next_request (h, GNUNET_YES);
1112 * Connect to the core service. Note that the connection may
1113 * complete (or fail) asynchronously.
1115 * @param cfg configuration to use
1116 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1117 * @param init callback to call once we have successfully
1118 * connected to the core service
1119 * @param connects function to call on peer connect, can be NULL
1120 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1121 * @param inbound_notify function to call for all inbound messages, can be NULL
1122 * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
1123 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1124 * can be used to improve efficiency, ignored if @a inbound_notify is NULLL
1125 * @param outbound_notify function to call for all outbound messages, can be NULL
1126 * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
1127 * GNUNET_MessageHeader and hence we do not need to give it the full message
1128 * can be used to improve efficiency, ignored if @a outbound_notify is NULLL
1129 * @param handlers callbacks for messages we care about, NULL-terminated
1130 * @return handle to the core service (only useful for disconnect until 'init' is called);
1131 * NULL on error (in this case, init is never called)
1133 struct GNUNET_CORE_Handle *
1134 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1136 GNUNET_CORE_StartupCallback init,
1137 GNUNET_CORE_ConnectEventHandler connects,
1138 GNUNET_CORE_DisconnectEventHandler disconnects,
1139 GNUNET_CORE_MessageCallback inbound_notify,
1140 int inbound_hdr_only,
1141 GNUNET_CORE_MessageCallback outbound_notify,
1142 int outbound_hdr_only,
1143 const struct GNUNET_CORE_MessageHandler *handlers)
1145 struct GNUNET_CORE_Handle *h;
1147 GNUNET_assert (NULL != cfg);
1148 h = GNUNET_new (struct GNUNET_CORE_Handle);
1152 h->connects = connects;
1153 h->disconnects = disconnects;
1154 h->inbound_notify = inbound_notify;
1155 h->outbound_notify = outbound_notify;
1156 h->inbound_hdr_only = inbound_hdr_only;
1157 h->outbound_hdr_only = outbound_hdr_only;
1158 h->handlers = handlers;
1160 h->currently_down = GNUNET_YES;
1161 h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
1162 if (NULL != handlers)
1163 while (handlers[h->hcnt].callback != NULL)
1165 GNUNET_assert (h->hcnt <
1166 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1167 sizeof (struct InitMessage)) / sizeof (uint16_t));
1168 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
1175 * Disconnect from the core service. This function can only
1176 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1177 * requests have been explicitly canceled.
1179 * @param handle connection to core to disconnect
1182 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1184 struct ControlMessage *cm;
1186 GNUNET_assert (NULL != handle);
1188 LOG (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1189 if (NULL != handle->cth)
1191 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1194 while (NULL != (cm = handle->control_pending_head))
1196 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1197 handle->control_pending_tail, cm);
1200 if (NULL != cm->cont)
1201 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1204 if (NULL != handle->client)
1206 GNUNET_CLIENT_disconnect (handle->client);
1207 handle->client = NULL;
1209 GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
1210 &disconnect_and_free_peer_entry,
1212 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1214 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1215 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1217 GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
1218 handle->peers = NULL;
1219 GNUNET_break (handle->ready_peer_head == NULL);
1220 GNUNET_free (handle);
1225 * Task that calls 'request_next_transmission'.
1227 * @param cls the 'struct PeerRecord *'
1228 * @param tc scheduler context
1231 run_request_next_transmission (void *cls,
1232 const struct GNUNET_SCHEDULER_TaskContext *tc)
1234 struct PeerRecord *pr = cls;
1236 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1237 request_next_transmission (pr);
1242 * Ask the core to call @a notify once it is ready to transmit the
1243 * given number of bytes to the specified @a target. Must only be
1244 * called after a connection to the respective peer has been
1245 * established (and the client has been informed about this). You may
1246 * have one request of this type pending for each connected peer at
1247 * any time. If a peer disconnects, the application MUST call
1248 * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
1249 * transmission request, if one such request is pending.
1251 * @param handle connection to core service
1252 * @param cork is corking allowed for this transmission?
1253 * @param priority how important is the message?
1254 * @param maxdelay how long can the message wait?
1255 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
1256 * @param notify_size how many bytes of buffer space does @a notify want?
1257 * @param notify function to call when buffer space is available;
1258 * will be called with NULL on timeout; clients MUST cancel
1259 * all pending transmission requests DURING the disconnect
1261 * @param notify_cls closure for notify
1262 * @return non-NULL if the notify callback was queued,
1263 * NULL if we can not even queue the request (request already pending);
1264 * if NULL is returned, @a notify will NOT be called.
1266 struct GNUNET_CORE_TransmitHandle *
1267 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
1269 struct GNUNET_TIME_Relative maxdelay,
1270 const struct GNUNET_PeerIdentity *target,
1272 GNUNET_CONNECTION_TransmitReadyNotify notify,
1275 struct PeerRecord *pr;
1276 struct GNUNET_CORE_TransmitHandle *th;
1278 GNUNET_assert (NULL != handle);
1279 GNUNET_assert (NULL != target);
1281 if (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
1286 GNUNET_assert (NULL != notify);
1287 LOG (GNUNET_ERROR_TYPE_DEBUG,
1288 "Asking core for transmission of %u bytes to `%s'\n",
1289 (unsigned int) notify_size,
1290 GNUNET_i2s (target));
1291 pr = GNUNET_CONTAINER_multipeermap_get (handle->peers, target);
1294 /* attempt to send to peer that is not connected */
1298 if (NULL != pr->th.peer)
1300 /* attempting to queue a second request for the same destination */
1304 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1305 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1307 memset (th, 0, sizeof (struct GNUNET_CORE_TransmitHandle));
1309 th->get_message = notify;
1310 th->get_message_cls = notify_cls;
1311 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1312 th->priority = priority;
1313 th->msize = notify_size;
1315 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == pr->ntr_task);
1317 GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1318 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmission request added to queue\n");
1324 * Cancel the specified transmission-ready notification.
1326 * @param th handle that was returned by "notify_transmit_ready".
1329 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1331 struct PeerRecord *pr = th->peer;
1332 struct GNUNET_CORE_Handle *h;
1334 GNUNET_assert (NULL != th);
1335 GNUNET_assert (NULL != pr);
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "Aborting transmission request to core for %u bytes to `%s'\n",
1338 (unsigned int) th->msize,
1339 GNUNET_i2s (&pr->peer));
1344 /* we're currently in the control queue, remove */
1345 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1346 h->control_pending_tail, th->cm);
1347 GNUNET_free (th->cm);
1350 if ((NULL != pr->prev) || (NULL != pr->next) || (pr == h->ready_peer_head))
1352 /* the request that was 'approved' by core was
1353 * canceled before it could be transmitted; remove
1354 * us from the 'ready' list */
1355 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1357 if (GNUNET_SCHEDULER_NO_TASK != pr->ntr_task)
1359 GNUNET_SCHEDULER_cancel (pr->ntr_task);
1360 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1366 * Check if the given peer is currently connected. This function is for special
1367 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1368 * expected to track which peers are connected based on the connect/disconnect
1369 * callbacks from GNUNET_CORE_connect. This function is NOT part of the
1370 * 'versioned', 'official' API. The difference between this function and the
1371 * function GNUNET_CORE_is_peer_connected() is that this one returns
1372 * synchronously after looking in the CORE API cache. The function
1373 * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1374 * its response is given asynchronously.
1376 * @param h the core handle
1377 * @param pid the identity of the peer to check if it has been connected to us
1378 * @return GNUNET_YES if the peer is connected to us; GNUNET_NO if not
1381 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1382 const struct GNUNET_PeerIdentity *pid)
1384 GNUNET_assert (NULL != h);
1385 GNUNET_assert (NULL != pid);
1386 return GNUNET_CONTAINER_multipeermap_contains (h->peers, pid);
1390 /* end of core_api.c */