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 * - implement atsi parsing and passing
31 #include "gnunet_constants.h"
32 #include "gnunet_core_service.h"
37 * Information we track for each peer.
43 * We generally do NOT keep peer records in a DLL; this
44 * DLL is only used IF this peer's 'pending_head' message
45 * is ready for transmission.
47 struct PeerRecord *prev;
50 * We generally do NOT keep peer records in a DLL; this
51 * DLL is only used IF this peer's 'pending_head' message
52 * is ready for transmission.
54 struct PeerRecord *next;
57 * Peer the record is about.
59 struct GNUNET_PeerIdentity peer;
62 * Corresponding core handle.
64 struct GNUNET_CORE_Handle *ch;
67 * Head of doubly-linked list of pending requests.
68 * Requests are sorted by deadline *except* for HEAD,
69 * which is only modified upon transmission to core.
71 struct GNUNET_CORE_TransmitHandle *pending_head;
74 * Tail of doubly-linked list of pending requests.
76 struct GNUNET_CORE_TransmitHandle *pending_tail;
79 * Pending callback waiting for peer information, or NULL for none.
81 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
89 * Request information ID for the given pcic (needed in case a
90 * request is cancelled after being submitted to core and a new
91 * one is generated; in this case, we need to avoid matching the
92 * reply to the first (cancelled) request to the second request).
97 * ID of timeout task for the 'pending_head' handle
98 * which is the one with the smallest timeout.
100 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
103 * Current size of the queue of pending requests.
105 unsigned int queue_size;
108 * SendMessageRequest ID generator for this peer.
116 * Entry in a doubly-linked list of control messages to be transmitted
117 * to the core service. Control messages include traffic allocation,
118 * connection requests and of course our initial 'init' request.
120 * The actual message is allocated at the end of this struct.
122 struct ControlMessage
125 * This is a doubly-linked list.
127 struct ControlMessage *next;
130 * This is a doubly-linked list.
132 struct ControlMessage *prev;
135 * Function to run after successful transmission (or call with
136 * reason 'TIMEOUT' on error); called with scheduler context 'NULL'
139 GNUNET_SCHEDULER_Task cont;
142 * Closure for 'cont'.
151 * Context for the core service connection.
153 struct GNUNET_CORE_Handle
157 * Configuration we're using.
159 const struct GNUNET_CONFIGURATION_Handle *cfg;
162 * Closure for the various callbacks.
167 * Function to call once we've handshaked with the core service.
169 GNUNET_CORE_StartupCallback init;
172 * Function to call whenever we're notified about a peer connecting.
174 GNUNET_CORE_ConnectEventHandler connects;
177 * Function to call whenever we're notified about a peer disconnecting.
179 GNUNET_CORE_DisconnectEventHandler disconnects;
182 * Function to call whenever we're notified about a peer changing status.
184 GNUNET_CORE_PeerStatusEventHandler status_events;
187 * Function to call whenever we receive an inbound message.
189 GNUNET_CORE_MessageCallback inbound_notify;
192 * Function to call whenever we receive an outbound message.
194 GNUNET_CORE_MessageCallback outbound_notify;
197 * Function handlers for messages of particular type.
199 const struct GNUNET_CORE_MessageHandler *handlers;
202 * Our connection to the service.
204 struct GNUNET_CLIENT_Connection *client;
207 * Handle for our current transmission request.
209 struct GNUNET_CLIENT_TransmitHandle *cth;
212 * Head of doubly-linked list of pending requests.
214 struct ControlMessage *pending_head;
217 * Tail of doubly-linked list of pending requests.
219 struct ControlMessage *pending_tail;
222 * Head of doubly-linked list of peers that are core-approved
223 * to send their next message.
225 struct PeerRecord *ready_peer_head;
228 * Tail of doubly-linked list of peers that are core-approved
229 * to send their next message.
231 struct PeerRecord *ready_peer_tail;
234 * Hash map listing all of the peers that we are currently
237 struct GNUNET_CONTAINER_MultiHashMap *peers;
240 * Identity of this peer.
242 struct GNUNET_PeerIdentity me;
245 * ID of reconnect task (if any).
247 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
250 * Current delay we use for re-trying to connect to core.
252 struct GNUNET_TIME_Relative retry_backoff;
255 * Request information ID generator.
260 * Number of messages we are allowed to queue per target.
262 unsigned int queue_size;
265 * Number of entries in the handlers array.
270 * For inbound notifications without a specific handler, do
271 * we expect to only receive headers?
273 int inbound_hdr_only;
276 * For outbound notifications without a specific handler, do
277 * we expect to only receive headers?
279 int outbound_hdr_only;
282 * Are we currently disconnected and hence unable to forward
291 * Handle for a transmission request.
293 struct GNUNET_CORE_TransmitHandle
297 * We keep active transmit handles in a doubly-linked list.
299 struct GNUNET_CORE_TransmitHandle *next;
302 * We keep active transmit handles in a doubly-linked list.
304 struct GNUNET_CORE_TransmitHandle *prev;
307 * Corresponding peer record.
309 struct PeerRecord *peer;
312 * Corresponding SEND_REQUEST message. Only non-NULL
313 * while SEND_REQUEST message is pending.
315 struct ControlMessage *cm;
318 * Function that will be called to get the actual request
319 * (once we are ready to transmit this request to the core).
320 * The function will be called with a NULL buffer to signal
323 GNUNET_CONNECTION_TransmitReadyNotify get_message;
326 * Closure for get_message.
328 void *get_message_cls;
331 * Timeout for this handle.
333 struct GNUNET_TIME_Absolute timeout;
336 * How important is this message?
341 * Size of this request.
346 * Send message request ID for this request.
354 * Our current client connection went down. Clean it up
355 * and try to reconnect!
357 * @param h our handle to the core service
360 reconnect (struct GNUNET_CORE_Handle *h);
364 * Task schedule to try to re-connect to core.
366 * @param cls the 'struct GNUNET_CORE_Handle'
367 * @param tc task context
370 reconnect_task (void *cls,
371 const struct GNUNET_SCHEDULER_TaskContext *tc)
373 struct GNUNET_CORE_Handle *h = cls;
375 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Connecting to CORE service after delay\n");
385 * Notify clients about disconnect and free
386 * the entry for connected peer.
388 * @param cls the 'struct GNUNET_CORE_Handle*'
389 * @param key the peer identity (not used)
390 * @param value the 'struct PeerRecord' to free.
391 * @return GNUNET_YES (continue)
394 disconnect_and_free_peer_entry (void *cls,
395 const GNUNET_HashCode *key,
398 static struct GNUNET_BANDWIDTH_Value32NBO zero;
399 struct GNUNET_CORE_Handle *h = cls;
400 struct GNUNET_CORE_TransmitHandle *th;
401 struct PeerRecord *pr = value;
402 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
404 while (NULL != (th = pr->pending_head))
406 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
411 th->get_message (th->get_message_cls,
415 if (NULL != (pcic = pr->pcic))
423 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
425 GNUNET_SCHEDULER_cancel (pr->timeout_task);
426 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
428 GNUNET_assert (pr->queue_size == 0);
429 if ( (pr->prev != NULL) ||
430 (pr->next != NULL) ||
431 (h->ready_peer_head == pr) )
432 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
435 if (h->disconnects != NULL)
436 h->disconnects (h->cls,
438 GNUNET_assert (GNUNET_YES ==
439 GNUNET_CONTAINER_multihashmap_remove (h->peers,
442 GNUNET_assert (pr->pending_head == NULL);
443 GNUNET_assert (pr->pending_tail == NULL);
444 GNUNET_assert (pr->ch = h);
445 GNUNET_assert (pr->queue_size == 0);
446 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
453 * Close down any existing connection to the CORE service and
454 * try re-establishing it later.
456 * @param h our handle
459 reconnect_later (struct GNUNET_CORE_Handle *h)
461 struct ControlMessage *cm;
463 if (h->client != NULL)
465 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
467 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
468 &disconnect_and_free_peer_entry,
471 h->currently_down = GNUNET_YES;
472 while (NULL != (cm = h->pending_head))
474 GNUNET_CONTAINER_DLL_remove (h->pending_head,
477 cm->cont (cm->cont_cls, NULL);
480 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
481 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
483 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
486 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
491 * Check the list of pending requests, send the next
494 * @param h core handle
495 * @param ignore_currently_down transmit message even if not initialized?
498 trigger_next_request (struct GNUNET_CORE_Handle *h,
499 int ignore_currently_down);
503 * The given request hit its timeout. Remove from the
504 * doubly-linked list and call the respective continuation.
506 * @param cls the transmit handle of the request that timed out
507 * @param tc context, can be NULL (!)
510 transmission_timeout (void *cls,
511 const struct GNUNET_SCHEDULER_TaskContext *tc);
515 * Control message was sent, mark it as such.
517 * @param cls the 'struct GNUNET_CORE_TransmitHandle*'
518 * @param tc scheduler context
521 mark_control_message_sent (void *cls,
522 const struct GNUNET_SCHEDULER_TaskContext *tc)
524 struct GNUNET_CORE_TransmitHandle *th = cls;
531 * Send a control message to the peer asking for transmission
532 * of the message in the given peer record.
534 * @param pr peer to request transmission to
537 request_next_transmission (struct PeerRecord *pr)
539 struct GNUNET_CORE_Handle *h = pr->ch;
540 struct ControlMessage *cm;
541 struct SendMessageRequest *smr;
542 struct GNUNET_CORE_TransmitHandle *th;
544 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
546 GNUNET_SCHEDULER_cancel (pr->timeout_task);
547 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
549 if (NULL == (th = pr->pending_head))
551 trigger_next_request (h, GNUNET_NO);
554 GNUNET_assert (pr->prev == NULL);
555 GNUNET_assert (pr->next == NULL);
556 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
557 &transmission_timeout,
559 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
560 sizeof (struct SendMessageRequest));
561 cm->cont = &mark_control_message_sent;
564 smr = (struct SendMessageRequest*) &cm[1];
565 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
566 smr->header.size = htons (sizeof (struct SendMessageRequest));
567 smr->priority = htonl (th->priority);
568 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
569 smr->peer = pr->peer;
570 smr->queue_size = htonl (pr->queue_size);
571 smr->size = htons (th->msize);
572 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
573 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
577 trigger_next_request (h, GNUNET_NO);
582 * The given request hit its timeout. Remove from the
583 * doubly-linked list and call the respective continuation.
585 * @param cls the transmit handle of the request that timed out
586 * @param tc context, can be NULL (!)
589 transmission_timeout (void *cls,
590 const struct GNUNET_SCHEDULER_TaskContext *tc)
592 struct PeerRecord *pr = cls;
593 struct GNUNET_CORE_TransmitHandle *th;
595 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
596 th = pr->pending_head;
597 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Signalling timeout of request for transmission to CORE service\n");
605 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
606 request_next_transmission (pr);
611 * Transmit the next message to the core service.
614 transmit_message (void *cls,
618 struct GNUNET_CORE_Handle *h = cls;
619 struct ControlMessage *cm;
620 struct GNUNET_CORE_TransmitHandle *th;
621 struct PeerRecord *pr;
622 struct SendMessage *sm;
623 const struct GNUNET_MessageHeader *hdr;
631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632 "Transmission failed, initiating reconnect\n");
637 /* first check for control messages */
638 if (NULL != (cm = h->pending_head))
640 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
641 msize = ntohs (hdr->size);
644 trigger_next_request (h, GNUNET_NO);
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "Transmitting control message with %u bytes of type %u to core.\n",
650 (unsigned int) msize,
651 (unsigned int) ntohs (hdr->type));
653 memcpy (buf, hdr, msize);
654 GNUNET_CONTAINER_DLL_remove (h->pending_head,
657 if (NULL != cm->cont)
658 GNUNET_SCHEDULER_add_continuation (cm->cont,
660 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
662 trigger_next_request (h, GNUNET_NO);
665 /* now check for 'ready' P2P messages */
666 if (NULL != (pr = h->ready_peer_head))
668 th = pr->pending_head;
669 if (size < th->msize + sizeof (struct SendMessage))
671 trigger_next_request (h, GNUNET_NO);
674 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
677 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
681 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
683 GNUNET_SCHEDULER_cancel (pr->timeout_task);
684 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Transmitting SEND request to `%s' with %u bytes.\n",
689 GNUNET_i2s (&pr->peer),
690 (unsigned int) th->msize);
692 sm = (struct SendMessage *) buf;
693 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
694 sm->priority = htonl (th->priority);
695 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
697 ret = th->get_message (th->get_message_cls,
698 size - sizeof (struct SendMessage),
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Size of clients message to peer %s is 0!\n",
706 GNUNET_i2s(&pr->peer));
708 /* client decided to send nothing! */
709 request_next_transmission (pr);
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Produced SEND message to core with %u bytes payload\n",
717 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
718 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
721 request_next_transmission (pr);
724 ret += sizeof (struct SendMessage);
725 sm->header.size = htons (ret);
726 GNUNET_assert (ret <= size);
728 request_next_transmission (pr);
736 * Check the list of pending requests, send the next
739 * @param h core handle
740 * @param ignore_currently_down transmit message even if not initialized?
743 trigger_next_request (struct GNUNET_CORE_Handle *h,
744 int ignore_currently_down)
748 if ( (GNUNET_YES == h->currently_down) &&
749 (ignore_currently_down == GNUNET_NO) )
753 if (h->pending_head != NULL)
754 msize = ntohs (((struct GNUNET_MessageHeader*) &h->pending_head[1])->size);
755 else if (h->ready_peer_head != NULL)
756 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
758 return; /* no pending message */
759 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
761 GNUNET_TIME_UNIT_FOREVER_REL,
763 &transmit_message, h);
768 * Handler for notification messages received from the core.
770 * @param cls our "struct GNUNET_CORE_Handle"
771 * @param msg the message received from the core service
774 main_notify_handler (void *cls,
775 const struct GNUNET_MessageHeader *msg)
777 struct GNUNET_CORE_Handle *h = cls;
778 const struct InitReplyMessage *m;
779 const struct ConnectNotifyMessage *cnm;
780 const struct DisconnectNotifyMessage *dnm;
781 const struct NotifyTrafficMessage *ntm;
782 const struct GNUNET_MessageHeader *em;
783 const struct ConfigurationInfoMessage *cim;
784 const struct PeerStatusNotifyMessage *psnm;
785 const struct SendMessageReady *smr;
786 const struct GNUNET_CORE_MessageHandler *mh;
787 GNUNET_CORE_StartupCallback init;
788 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
789 struct PeerRecord *pr;
790 struct GNUNET_CORE_TransmitHandle *th;
798 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
800 ("Client was disconnected from core service, trying to reconnect.\n"));
804 msize = ntohs (msg->size);
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Processing message of type %u and size %u from core service\n",
808 ntohs (msg->type), msize);
810 switch (ntohs (msg->type))
812 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
813 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
819 m = (const struct InitReplyMessage *) msg;
820 GNUNET_break (0 == ntohl (m->reserved));
821 /* start our message processing loop */
822 if (GNUNET_YES == h->currently_down)
824 h->currently_down = GNUNET_NO;
825 trigger_next_request (h, GNUNET_NO);
827 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
828 if (NULL != (init = h->init))
830 /* mark so we don't call init on reconnect */
832 GNUNET_CRYPTO_hash (&m->publicKey,
834 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
838 "Connected to core service of peer `%s'.\n",
839 GNUNET_i2s (&h->me));
841 init (h->cls, h, &h->me, &m->publicKey);
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "Successfully reconnected to core service.\n");
851 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
852 if (msize != sizeof (struct ConnectNotifyMessage))
858 cnm = (const struct ConnectNotifyMessage *) msg;
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Received notification about connection from `%s'.\n",
862 GNUNET_i2s (&cnm->peer));
864 if (0 == memcmp (&h->me,
866 sizeof (struct GNUNET_PeerIdentity)))
868 /* disconnect from self!? */
872 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
873 &cnm->peer.hashPubKey);
880 pr = GNUNET_malloc (sizeof (struct PeerRecord));
881 pr->peer = cnm->peer;
883 GNUNET_assert (GNUNET_YES ==
884 GNUNET_CONTAINER_multihashmap_put (h->peers,
885 &cnm->peer.hashPubKey,
887 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
888 if (NULL != h->connects)
891 NULL /* FIXME: atsi! */);
893 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
894 if (msize != sizeof (struct DisconnectNotifyMessage))
900 dnm = (const struct DisconnectNotifyMessage *) msg;
901 if (0 == memcmp (&h->me,
903 sizeof (struct GNUNET_PeerIdentity)))
905 /* connection to self!? */
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "Received notification about disconnect from `%s'.\n",
912 GNUNET_i2s (&dnm->peer));
914 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
915 &dnm->peer.hashPubKey);
922 trigger = ( (pr->prev != NULL) ||
923 (pr->next != NULL) ||
924 (h->ready_peer_head == pr) );
925 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
927 trigger_next_request (h, GNUNET_NO);
929 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
930 if (NULL == h->status_events)
935 if (msize != sizeof (struct PeerStatusNotifyMessage))
941 psnm = (const struct PeerStatusNotifyMessage *) msg;
942 if (0 == memcmp (&h->me,
944 sizeof (struct GNUNET_PeerIdentity)))
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Received notification about status change by `%s'.\n",
953 GNUNET_i2s (&psnm->peer));
955 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
956 &psnm->peer.hashPubKey);
963 h->status_events (h->cls,
967 GNUNET_TIME_absolute_ntoh (psnm->timeout),
968 NULL /* FIXME: atsi */);
970 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
972 sizeof (struct NotifyTrafficMessage) +
973 sizeof (struct GNUNET_MessageHeader))
979 ntm = (const struct NotifyTrafficMessage *) msg;
980 if (0 == memcmp (&h->me,
982 sizeof (struct GNUNET_PeerIdentity)))
988 em = (const struct GNUNET_MessageHeader *) &ntm[1];
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Received message of type %u and size %u from peer `%4s'\n",
994 GNUNET_i2s (&ntm->peer));
996 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
997 &ntm->peer.hashPubKey);
1001 reconnect_later (h);
1004 if ((GNUNET_NO == h->inbound_hdr_only) &&
1005 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
1008 reconnect_later (h);
1011 et = ntohs (em->type);
1012 for (hpos = 0; hpos < h->hcnt; hpos++)
1014 mh = &h->handlers[hpos];
1017 if ((mh->expected_size != ntohs (em->size)) &&
1018 (mh->expected_size != 0))
1024 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1025 NULL /* FIXME: atsi */))
1027 /* error in processing, do not process other messages! */
1031 if (NULL != h->inbound_notify)
1032 h->inbound_notify (h->cls, &ntm->peer, em,
1033 NULL /* FIXME: atsi */);
1035 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1037 sizeof (struct NotifyTrafficMessage) +
1038 sizeof (struct GNUNET_MessageHeader))
1041 reconnect_later (h);
1044 ntm = (const struct NotifyTrafficMessage *) msg;
1045 if (0 == memcmp (&h->me,
1047 sizeof (struct GNUNET_PeerIdentity)))
1053 em = (const struct GNUNET_MessageHeader *) &ntm[1];
1054 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1055 &ntm->peer.hashPubKey);
1059 reconnect_later (h);
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Received notification about transmission to `%s'.\n",
1065 GNUNET_i2s (&ntm->peer));
1067 if ((GNUNET_NO == h->outbound_hdr_only) &&
1068 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
1071 reconnect_later (h);
1074 if (NULL == h->outbound_notify)
1079 h->outbound_notify (h->cls, &ntm->peer, em,
1080 NULL /* FIXME: atsi? */);
1082 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1083 if (msize != sizeof (struct SendMessageReady))
1086 reconnect_later (h);
1089 smr = (const struct SendMessageReady *) msg;
1090 if (0 == memcmp (&h->me,
1092 sizeof (struct GNUNET_PeerIdentity)))
1098 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1099 &smr->peer.hashPubKey);
1103 reconnect_later (h);
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "Received notification about transmission readiness to `%s'.\n",
1109 GNUNET_i2s (&smr->peer));
1111 th = pr->pending_head;
1112 if (ntohs (smr->smr_id) != th->smr_id)
1114 /* READY message is for expired or cancelled message,
1115 ignore! (we should have already sent another request) */
1118 if ( (pr->prev != NULL) ||
1119 (pr->next != NULL) ||
1120 (h->ready_peer_head == pr) )
1122 /* we should not already be on the ready list... */
1124 reconnect_later (h);
1127 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1130 trigger_next_request (h, GNUNET_NO);
1132 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1133 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1136 reconnect_later (h);
1139 cim = (const struct ConfigurationInfoMessage*) msg;
1140 if (0 == memcmp (&h->me,
1142 sizeof (struct GNUNET_PeerIdentity)))
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "Received notification about configuration update for `%s'.\n",
1151 GNUNET_i2s (&cim->peer));
1153 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1154 &cim->peer.hashPubKey);
1158 reconnect_later (h);
1161 if (pr->rim_id != ntohl (cim->rim_id))
1169 ntohl (cim->reserved_amount),
1170 GNUNET_ntohll (cim->preference));
1173 reconnect_later (h);
1176 GNUNET_CLIENT_receive (h->client,
1177 &main_notify_handler, h,
1178 GNUNET_TIME_UNIT_FOREVER_REL);
1183 * Task executed once we are done transmitting the INIT message.
1184 * Starts our 'receive' loop.
1186 * @param cls the 'struct GNUNET_CORE_Handle'
1187 * @param tc task context
1190 init_done_task (void *cls,
1191 const struct GNUNET_SCHEDULER_TaskContext *tc)
1193 struct GNUNET_CORE_Handle *h = cls;
1197 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Failed to exchange INIT with core, retrying\n");
1203 reconnect_later (h);
1206 GNUNET_CLIENT_receive (h->client,
1207 &main_notify_handler,
1209 GNUNET_TIME_UNIT_FOREVER_REL);
1214 * Our current client connection went down. Clean it up
1215 * and try to reconnect!
1217 * @param h our handle to the core service
1220 reconnect (struct GNUNET_CORE_Handle *h)
1222 struct ControlMessage *cm;
1223 struct InitMessage *init;
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Reconnecting to CORE service\n");
1233 GNUNET_assert (h->client == NULL);
1234 GNUNET_assert (h->currently_down == GNUNET_YES);
1235 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1236 if (h->client == NULL)
1238 reconnect_later (h);
1241 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1242 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1244 cm->cont = &init_done_task;
1246 init = (struct InitMessage*) &cm[1];
1247 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1248 init->header.size = htons (msize);
1249 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1250 if (h->status_events != NULL)
1251 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1252 if (h->inbound_notify != NULL)
1254 if (h->inbound_hdr_only)
1255 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1257 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1259 if (h->outbound_notify != NULL)
1261 if (h->outbound_hdr_only)
1262 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1264 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1266 init->options = htonl (opt);
1267 ts = (uint16_t *) &init[1];
1268 for (hpos = 0; hpos < h->hcnt; hpos++)
1269 ts[hpos] = htons (h->handlers[hpos].type);
1270 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1273 trigger_next_request (h, GNUNET_YES);
1279 * Connect to the core service. Note that the connection may
1280 * complete (or fail) asynchronously.
1282 * @param cfg configuration to use
1283 * @param queue_size size of the per-peer message queue
1284 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1285 * @param init callback to call on timeout or once we have successfully
1286 * connected to the core service; note that timeout is only meaningful if init is not NULL
1287 * @param connects function to call on peer connect, can be NULL
1288 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1289 * @param status_events function to call on changes to peer connection status, can be NULL
1290 * @param inbound_notify function to call for all inbound messages, can be NULL
1291 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1292 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1293 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1294 * @param outbound_notify function to call for all outbound messages, can be NULL
1295 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1296 * GNUNET_MessageHeader and hence we do not need to give it the full message
1297 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1298 * @param handlers callbacks for messages we care about, NULL-terminated
1299 * @return handle to the core service (only useful for disconnect until 'init' is called);
1300 * NULL on error (in this case, init is never called)
1302 struct GNUNET_CORE_Handle *
1303 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1304 unsigned int queue_size,
1306 GNUNET_CORE_StartupCallback init,
1307 GNUNET_CORE_ConnectEventHandler connects,
1308 GNUNET_CORE_DisconnectEventHandler disconnects,
1309 GNUNET_CORE_PeerStatusEventHandler status_events,
1310 GNUNET_CORE_MessageCallback inbound_notify,
1311 int inbound_hdr_only,
1312 GNUNET_CORE_MessageCallback outbound_notify,
1313 int outbound_hdr_only,
1314 const struct GNUNET_CORE_MessageHandler *handlers)
1316 struct GNUNET_CORE_Handle *h;
1318 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1320 h->queue_size = queue_size;
1323 h->connects = connects;
1324 h->disconnects = disconnects;
1325 h->status_events = status_events;
1326 h->inbound_notify = inbound_notify;
1327 h->outbound_notify = outbound_notify;
1328 h->inbound_hdr_only = inbound_hdr_only;
1329 h->outbound_hdr_only = outbound_hdr_only;
1330 h->handlers = handlers;
1332 h->currently_down = GNUNET_YES;
1333 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1334 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1335 while (handlers[h->hcnt].callback != NULL)
1337 GNUNET_assert (h->hcnt <
1338 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1339 sizeof (struct InitMessage)) / sizeof (uint16_t));
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1342 "Connecting to CORE service\n");
1350 * Disconnect from the core service. This function can only
1351 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1352 * requests have been explicitly cancelled.
1354 * @param handle connection to core to disconnect
1357 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1359 struct ControlMessage *cm;
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Disconnecting from CORE service\n");
1365 if (handle->cth != NULL)
1367 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1370 if (handle->client != NULL)
1372 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1373 handle->client = NULL;
1375 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1377 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1378 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1380 while (NULL != (cm = handle->pending_head))
1382 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1383 handle->pending_tail,
1385 cm->cont (cm->cont_cls, NULL);
1388 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1389 &disconnect_and_free_peer_entry,
1391 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1392 GNUNET_break (handle->ready_peer_head == NULL);
1393 GNUNET_free (handle);
1398 * Ask the core to call "notify" once it is ready to transmit the
1399 * given number of bytes to the specified "target". If we are not yet
1400 * connected to the specified peer, a call to this function will cause
1401 * us to try to establish a connection.
1403 * @param handle connection to core service
1404 * @param priority how important is the message?
1405 * @param maxdelay how long can the message wait?
1406 * @param target who should receive the message,
1407 * use NULL for this peer (loopback)
1408 * @param notify_size how many bytes of buffer space does notify want?
1409 * @param notify function to call when buffer space is available
1410 * @param notify_cls closure for notify
1411 * @return non-NULL if the notify callback was queued,
1412 * NULL if we can not even queue the request (insufficient
1413 * memory); if NULL is returned, "notify" will NOT be called.
1415 struct GNUNET_CORE_TransmitHandle *
1416 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1418 struct GNUNET_TIME_Relative maxdelay,
1419 const struct GNUNET_PeerIdentity *target,
1421 GNUNET_CONNECTION_TransmitReadyNotify notify,
1424 struct PeerRecord *pr;
1425 struct GNUNET_CORE_TransmitHandle *th;
1426 struct GNUNET_CORE_TransmitHandle *pos;
1427 struct GNUNET_CORE_TransmitHandle *prev;
1428 struct GNUNET_CORE_TransmitHandle *minp;
1430 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1431 &target->hashPubKey);
1434 /* attempt to send to peer that is not connected */
1435 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1436 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1437 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1441 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1442 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1443 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1445 th->get_message = notify;
1446 th->get_message_cls = notify_cls;
1447 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1448 th->priority = priority;
1449 th->msize = notify_size;
1450 /* bound queue size */
1451 if (pr->queue_size == handle->queue_size)
1453 /* find lowest-priority entry */
1454 minp = pr->pending_head;
1456 while (prev != NULL)
1458 if (prev->priority < minp->priority)
1464 GNUNET_break (handle->queue_size != 0);
1465 GNUNET_break (pr->queue_size == 0);
1468 if (priority <= minp->priority)
1469 return NULL; /* priority too low */
1470 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1475 minp->get_message (minp->get_message_cls,
1480 /* Order entries by deadline, but SKIP 'HEAD' if
1481 we're in the 'ready_peer_*' DLL */
1482 pos = pr->pending_head;
1483 if ( (pr->prev != NULL) ||
1484 (pr->next != NULL) ||
1485 (pr == handle->ready_peer_head) )
1487 GNUNET_assert (pos != NULL);
1488 pos = pos->next; /* skip head */
1491 /* insertion sort */
1493 while ( (pos != NULL) &&
1494 (pos->timeout.abs_value < th->timeout.abs_value) )
1499 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1504 /* was the request queue previously empty? */
1505 if (pr->pending_head == th)
1506 request_next_transmission (pr);
1512 * Cancel the specified transmission-ready notification.
1514 * @param th handle that was returned by "notify_transmit_ready".
1517 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1520 struct PeerRecord *pr = th->peer;
1521 struct GNUNET_CORE_Handle *h = pr->ch;
1524 was_head = (pr->pending_head == th);
1525 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1530 /* we're currently in the control queue, remove */
1531 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1534 GNUNET_free (th->cm);
1539 if ( (pr->prev != NULL) ||
1540 (pr->next != NULL) ||
1541 (pr == h->ready_peer_head) )
1543 /* the request that was 'approved' by core was
1544 cancelled before it could be transmitted; remove
1545 us from the 'ready' list */
1546 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1550 request_next_transmission (pr);
1555 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1558 * Handle for a request to the core to connect to
1559 * a particular peer. Can be used to cancel the request
1560 * (before the 'cont'inuation is called).
1562 struct GNUNET_CORE_PeerRequestHandle
1566 * Link to control message.
1568 struct ControlMessage *cm;
1573 struct GNUNET_CORE_Handle *h;
1576 * Continuation to run when done.
1578 GNUNET_SCHEDULER_Task cont;
1581 * Closure for 'cont'.
1589 * Continuation called when the control message was transmitted.
1590 * Calls the original continuation and frees the remaining
1593 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1594 * @param tc scheduler context
1597 peer_request_connect_cont (void *cls,
1598 const struct GNUNET_SCHEDULER_TaskContext *tc)
1600 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1602 if (ret->cont != NULL)
1605 GNUNET_SCHEDULER_add_now (ret->cont,
1608 ret->cont (ret->cont_cls, tc);
1615 * Request that the core should try to connect to a particular peer.
1616 * Once the request has been transmitted to the core, the continuation
1617 * function will be called. Note that this does NOT mean that a
1618 * connection was successfully established -- it only means that the
1619 * core will now try. Successful establishment of the connection
1620 * will be signalled to the 'connects' callback argument of
1621 * 'GNUNET_CORE_connect' only. If the core service does not respond
1622 * to our connection attempt within the given time frame, 'cont' will
1623 * be called with the TIMEOUT reason code.
1625 * @param h core handle
1626 * @param timeout how long to try to talk to core
1627 * @param peer who should we connect to
1628 * @param cont function to call once the request has been completed (or timed out)
1629 * @param cont_cls closure for cont
1630 * @return NULL on error (cont will not be called), otherwise handle for cancellation
1632 struct GNUNET_CORE_PeerRequestHandle *
1633 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1634 struct GNUNET_TIME_Relative timeout,
1635 const struct GNUNET_PeerIdentity * peer,
1636 GNUNET_SCHEDULER_Task cont,
1639 struct GNUNET_CORE_PeerRequestHandle *ret;
1640 struct ControlMessage *cm;
1641 struct ConnectMessage *msg;
1643 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1644 sizeof (struct ConnectMessage));
1645 msg = (struct ConnectMessage*) &cm[1];
1646 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1647 msg->header.size = htons (sizeof (struct ConnectMessage));
1648 msg->reserved = htonl (0);
1649 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1651 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1654 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1658 ret->cont_cls = cont_cls;
1659 cm->cont = &peer_request_connect_cont;
1661 if (h->pending_head == cm)
1662 trigger_next_request (h, GNUNET_NO);
1668 * Cancel a pending request to connect to a particular peer. Must not
1669 * be called after the 'cont' function was invoked.
1671 * @param req request handle that was returned for the original request
1674 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1676 struct GNUNET_CORE_Handle *h = req->h;
1677 struct ControlMessage *cm = req->cm;
1679 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1687 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1690 struct GNUNET_CORE_InformationRequestContext
1694 * Our connection to the service.
1696 struct GNUNET_CORE_Handle *h;
1699 * Function to call with the information.
1701 GNUNET_CORE_PeerConfigurationInfoCallback info;
1709 * Link to control message, NULL if CM was sent.
1711 struct ControlMessage *cm;
1714 * Link to peer record.
1716 struct PeerRecord *pr;
1721 * CM was sent, remove link so we don't double-free.
1723 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1724 * @param tc scheduler context
1727 change_preference_send_continuation (void *cls,
1728 const struct GNUNET_SCHEDULER_TaskContext *tc)
1730 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1737 * Obtain statistics and/or change preferences for the given peer.
1739 * @param h core handle
1740 * @param peer identifies the peer
1741 * @param timeout after how long should we give up (and call "info" with NULL
1742 * for "peer" to signal an error)?
1743 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1744 * caller should set "bw_out" to "-1" to avoid changing
1745 * the current value; otherwise "bw_out" will be lowered to
1746 * the specified value; passing a pointer to "0" can be used to force
1747 * us to disconnect from the peer; "bw_out" might not increase
1748 * as specified since the upper bound is generally
1749 * determined by the other peer!
1750 * @param amount reserve N bytes for receiving, negative
1751 * amounts can be used to undo a (recent) reservation;
1752 * @param preference increase incoming traffic share preference by this amount;
1753 * in the absence of "amount" reservations, we use this
1754 * preference value to assign proportional bandwidth shares
1755 * to all connected peers
1756 * @param info function to call with the resulting configuration information
1757 * @param info_cls closure for info
1758 * @return NULL on error
1760 struct GNUNET_CORE_InformationRequestContext *
1761 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1762 const struct GNUNET_PeerIdentity *peer,
1763 struct GNUNET_TIME_Relative timeout,
1764 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1766 uint64_t preference,
1767 GNUNET_CORE_PeerConfigurationInfoCallback info,
1770 struct GNUNET_CORE_InformationRequestContext *irc;
1771 struct PeerRecord *pr;
1772 struct RequestInfoMessage *rim;
1773 struct ControlMessage *cm;
1775 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1779 /* attempt to change preference on peer that is not connected */
1783 if (pr->pcic != NULL)
1785 /* second change before first one is done */
1789 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1793 irc->info_cls = info_cls;
1794 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1795 sizeof (struct RequestInfoMessage));
1796 cm->cont = &change_preference_send_continuation;
1799 rim = (struct RequestInfoMessage*) &cm[1];
1800 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1801 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1802 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1803 rim->limit_outbound = bw_out;
1804 rim->reserve_inbound = htonl (amount);
1805 rim->preference_change = GNUNET_htonll(preference);
1807 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1811 pr->pcic_cls = info_cls;
1817 * Cancel request for getting information about a peer.
1818 * Note that an eventual change in preference, trust or bandwidth
1819 * assignment MAY have already been committed at the time,
1820 * so cancelling a request is NOT sure to undo the original
1821 * request. The original request may or may not still commit.
1822 * The only thing cancellation ensures is that the callback
1823 * from the original request will no longer be called.
1825 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1828 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1830 struct GNUNET_CORE_Handle *h = irc->h;
1831 struct PeerRecord *pr = irc->pr;
1833 if (irc->cm != NULL)
1835 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1838 GNUNET_free (irc->cm);
1841 pr->pcic_cls = NULL;
1847 /* ********************* GNUNET_CORE_iterate_peers *********************** */
1850 * Context for 'iterate_peers' helper function.
1852 struct IterationContext
1857 GNUNET_CORE_ConnectEventHandler peer_cb;
1860 * Closure for 'peer_cb'.
1867 * Call callback for each peer.
1869 * @param cls the 'struct IterationContext'
1870 * @param hc peer identity, not used
1871 * @param value the 'struct PeerRecord'
1872 * @return GNUNET_YES (continue iteration)
1875 iterate_peers (void *cls,
1876 const GNUNET_HashCode *hc,
1879 struct IterationContext *ic = cls;
1880 struct PeerRecord *pr = value;
1882 ic->peer_cb (ic->cb_cls,
1884 NULL /* FIXME: pass atsi? */);
1890 * Obtain statistics and/or change preferences for the given peer.
1892 * @param h handle to core
1893 * @param peer_cb function to call with the peer information
1894 * @param cb_cls closure for peer_cb
1895 * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
1898 GNUNET_CORE_iterate_peers (struct GNUNET_CORE_Handle *h,
1899 GNUNET_CORE_ConnectEventHandler peer_cb,
1902 struct IterationContext ic;
1904 ic.peer_cb = peer_cb;
1906 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
1913 /* end of core_api.c */