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'.
147 * Transmit handle (if one is associated with this ControlMessage), or NULL.
149 struct GNUNET_CORE_TransmitHandle *th;
155 * Context for the core service connection.
157 struct GNUNET_CORE_Handle
161 * Configuration we're using.
163 const struct GNUNET_CONFIGURATION_Handle *cfg;
166 * Closure for the various callbacks.
171 * Function to call once we've handshaked with the core service.
173 GNUNET_CORE_StartupCallback init;
176 * Function to call whenever we're notified about a peer connecting.
178 GNUNET_CORE_ConnectEventHandler connects;
181 * Function to call whenever we're notified about a peer disconnecting.
183 GNUNET_CORE_DisconnectEventHandler disconnects;
186 * Function to call whenever we're notified about a peer changing status.
188 GNUNET_CORE_PeerStatusEventHandler status_events;
191 * Function to call whenever we receive an inbound message.
193 GNUNET_CORE_MessageCallback inbound_notify;
196 * Function to call whenever we receive an outbound message.
198 GNUNET_CORE_MessageCallback outbound_notify;
201 * Function handlers for messages of particular type.
203 const struct GNUNET_CORE_MessageHandler *handlers;
206 * Our connection to the service.
208 struct GNUNET_CLIENT_Connection *client;
211 * Handle for our current transmission request.
213 struct GNUNET_CLIENT_TransmitHandle *cth;
216 * Head of doubly-linked list of pending requests.
218 struct ControlMessage *pending_head;
221 * Tail of doubly-linked list of pending requests.
223 struct ControlMessage *pending_tail;
226 * Head of doubly-linked list of peers that are core-approved
227 * to send their next message.
229 struct PeerRecord *ready_peer_head;
232 * Tail of doubly-linked list of peers that are core-approved
233 * to send their next message.
235 struct PeerRecord *ready_peer_tail;
238 * Hash map listing all of the peers that we are currently
241 struct GNUNET_CONTAINER_MultiHashMap *peers;
244 * Identity of this peer.
246 struct GNUNET_PeerIdentity me;
249 * ID of reconnect task (if any).
251 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
254 * Current delay we use for re-trying to connect to core.
256 struct GNUNET_TIME_Relative retry_backoff;
259 * Request information ID generator.
264 * Number of messages we are allowed to queue per target.
266 unsigned int queue_size;
269 * Number of entries in the handlers array.
274 * For inbound notifications without a specific handler, do
275 * we expect to only receive headers?
277 int inbound_hdr_only;
280 * For outbound notifications without a specific handler, do
281 * we expect to only receive headers?
283 int outbound_hdr_only;
286 * Are we currently disconnected and hence unable to forward
295 * Handle for a transmission request.
297 struct GNUNET_CORE_TransmitHandle
301 * We keep active transmit handles in a doubly-linked list.
303 struct GNUNET_CORE_TransmitHandle *next;
306 * We keep active transmit handles in a doubly-linked list.
308 struct GNUNET_CORE_TransmitHandle *prev;
311 * Corresponding peer record.
313 struct PeerRecord *peer;
316 * Corresponding SEND_REQUEST message. Only non-NULL
317 * while SEND_REQUEST message is pending.
319 struct ControlMessage *cm;
322 * Function that will be called to get the actual request
323 * (once we are ready to transmit this request to the core).
324 * The function will be called with a NULL buffer to signal
327 GNUNET_CONNECTION_TransmitReadyNotify get_message;
330 * Closure for get_message.
332 void *get_message_cls;
335 * Timeout for this handle.
337 struct GNUNET_TIME_Absolute timeout;
340 * How important is this message?
345 * Size of this request.
350 * Send message request ID for this request.
358 * Our current client connection went down. Clean it up
359 * and try to reconnect!
361 * @param h our handle to the core service
364 reconnect (struct GNUNET_CORE_Handle *h);
368 * Task schedule to try to re-connect to core.
370 * @param cls the 'struct GNUNET_CORE_Handle'
371 * @param tc task context
374 reconnect_task (void *cls,
375 const struct GNUNET_SCHEDULER_TaskContext *tc)
377 struct GNUNET_CORE_Handle *h = cls;
379 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Connecting to CORE service after delay\n");
389 * Notify clients about disconnect and free
390 * the entry for connected peer.
392 * @param cls the 'struct GNUNET_CORE_Handle*'
393 * @param key the peer identity (not used)
394 * @param value the 'struct PeerRecord' to free.
395 * @return GNUNET_YES (continue)
398 disconnect_and_free_peer_entry (void *cls,
399 const GNUNET_HashCode *key,
402 static struct GNUNET_BANDWIDTH_Value32NBO zero;
403 struct GNUNET_CORE_Handle *h = cls;
404 struct GNUNET_CORE_TransmitHandle *th;
405 struct PeerRecord *pr = value;
406 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
408 while (NULL != (th = pr->pending_head))
410 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
415 th->get_message (th->get_message_cls,
419 if (NULL != (pcic = pr->pcic))
427 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
429 GNUNET_SCHEDULER_cancel (pr->timeout_task);
430 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
432 GNUNET_assert (pr->queue_size == 0);
433 if ( (pr->prev != NULL) ||
434 (pr->next != NULL) ||
435 (h->ready_peer_head == pr) )
436 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
439 if (h->disconnects != NULL)
440 h->disconnects (h->cls,
442 GNUNET_assert (GNUNET_YES ==
443 GNUNET_CONTAINER_multihashmap_remove (h->peers,
446 GNUNET_assert (pr->pending_head == NULL);
447 GNUNET_assert (pr->pending_tail == NULL);
448 GNUNET_assert (pr->ch = h);
449 GNUNET_assert (pr->queue_size == 0);
450 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
457 * Close down any existing connection to the CORE service and
458 * try re-establishing it later.
460 * @param h our handle
463 reconnect_later (struct GNUNET_CORE_Handle *h)
465 struct ControlMessage *cm;
467 while (NULL != (cm = h->pending_head))
469 GNUNET_CONTAINER_DLL_remove (h->pending_head,
474 if (cm->cont != NULL)
475 cm->cont (cm->cont_cls, NULL);
478 if (h->client != NULL)
480 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
482 /* FIXME: is this right? GNUNET_CLIENT_disconnect frees all outstanding transmission handles, so h->cth is invalid! */
483 /* Otherwise a subsequent call to GNUNET_CORE_disconnect tries to cancel this task!!! */
485 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
486 &disconnect_and_free_peer_entry,
489 GNUNET_assert (h->pending_head == NULL);
490 h->currently_down = GNUNET_YES;
491 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
492 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
494 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
497 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
502 * Check the list of pending requests, send the next
505 * @param h core handle
506 * @param ignore_currently_down transmit message even if not initialized?
509 trigger_next_request (struct GNUNET_CORE_Handle *h,
510 int ignore_currently_down);
514 * The given request hit its timeout. Remove from the
515 * doubly-linked list and call the respective continuation.
517 * @param cls the transmit handle of the request that timed out
518 * @param tc context, can be NULL (!)
521 transmission_timeout (void *cls,
522 const struct GNUNET_SCHEDULER_TaskContext *tc);
526 * Send a control message to the peer asking for transmission
527 * of the message in the given peer record.
529 * @param pr peer to request transmission to
532 request_next_transmission (struct PeerRecord *pr)
534 struct GNUNET_CORE_Handle *h = pr->ch;
535 struct ControlMessage *cm;
536 struct SendMessageRequest *smr;
537 struct GNUNET_CORE_TransmitHandle *th;
539 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
541 GNUNET_SCHEDULER_cancel (pr->timeout_task);
542 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
544 if (NULL == (th = pr->pending_head))
546 trigger_next_request (h, GNUNET_NO);
549 GNUNET_assert (pr->prev == NULL);
550 GNUNET_assert (pr->next == NULL);
551 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
552 &transmission_timeout,
554 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
555 sizeof (struct SendMessageRequest));
558 smr = (struct SendMessageRequest*) &cm[1];
559 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
560 smr->header.size = htons (sizeof (struct SendMessageRequest));
561 smr->priority = htonl (th->priority);
562 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
563 smr->peer = pr->peer;
564 smr->queue_size = htonl (pr->queue_size);
565 smr->size = htons (th->msize);
566 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
567 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Adding SEND REQUEST for peer `%s' to message queue\n",
574 GNUNET_i2s (&pr->peer));
576 trigger_next_request (h, GNUNET_NO);
581 * The given request hit its timeout. Remove from the
582 * doubly-linked list and call the respective continuation.
584 * @param cls the transmit handle of the request that timed out
585 * @param tc context, can be NULL (!)
588 transmission_timeout (void *cls,
589 const struct GNUNET_SCHEDULER_TaskContext *tc)
591 struct PeerRecord *pr = cls;
592 struct GNUNET_CORE_TransmitHandle *th;
594 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
595 th = pr->pending_head;
596 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
602 "Signalling timeout of request for transmission to CORE service\n");
604 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
605 request_next_transmission (pr);
610 * Transmit the next message to the core service.
613 transmit_message (void *cls,
617 struct GNUNET_CORE_Handle *h = cls;
618 struct ControlMessage *cm;
619 struct GNUNET_CORE_TransmitHandle *th;
620 struct PeerRecord *pr;
621 struct SendMessage *sm;
622 const struct GNUNET_MessageHeader *hdr;
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Transmission failed, initiating reconnect\n");
636 /* first check for control messages */
637 if (NULL != (cm = h->pending_head))
639 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
640 msize = ntohs (hdr->size);
643 trigger_next_request (h, GNUNET_NO);
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "Transmitting control message with %u bytes of type %u to core.\n",
649 (unsigned int) msize,
650 (unsigned int) ntohs (hdr->type));
652 memcpy (buf, hdr, msize);
653 GNUNET_CONTAINER_DLL_remove (h->pending_head,
658 if (NULL != cm->cont)
659 GNUNET_SCHEDULER_add_continuation (cm->cont,
661 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
663 trigger_next_request (h, GNUNET_NO);
666 /* now check for 'ready' P2P messages */
667 if (NULL != (pr = h->ready_peer_head))
669 /* FIXME: If a reconnect_later call happened, this can be NULL! */
670 if (pr->pending_head == NULL)
675 th = pr->pending_head;
676 if (size < th->msize + sizeof (struct SendMessage))
678 trigger_next_request (h, GNUNET_NO);
681 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
684 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
688 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
690 GNUNET_SCHEDULER_cancel (pr->timeout_task);
691 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 "Transmitting SEND request to `%s' with %u bytes.\n",
696 GNUNET_i2s (&pr->peer),
697 (unsigned int) th->msize);
699 sm = (struct SendMessage *) buf;
700 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
701 sm->priority = htonl (th->priority);
702 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
704 ret = th->get_message (th->get_message_cls,
705 size - sizeof (struct SendMessage),
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Size of clients message to peer %s is 0!\n",
713 GNUNET_i2s(&pr->peer));
715 /* client decided to send nothing! */
716 request_next_transmission (pr);
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Produced SEND message to core with %u bytes payload\n",
724 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
725 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
728 request_next_transmission (pr);
731 ret += sizeof (struct SendMessage);
732 sm->header.size = htons (ret);
733 GNUNET_assert (ret <= size);
735 request_next_transmission (pr);
743 * Check the list of pending requests, send the next
746 * @param h core handle
747 * @param ignore_currently_down transmit message even if not initialized?
750 trigger_next_request (struct GNUNET_CORE_Handle *h,
751 int ignore_currently_down)
755 if ( (GNUNET_YES == h->currently_down) &&
756 (ignore_currently_down == GNUNET_NO) )
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Core connection down, not processing queue\n");
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Request pending, not processing queue\n");
772 if (h->pending_head != NULL)
773 msize = ntohs (((struct GNUNET_MessageHeader*) &h->pending_head[1])->size);
774 else if ((h->ready_peer_head != NULL) && (h->ready_peer_head->pending_head != NULL)) /* FIXME: h->ready_peer_head->pending_head check necessary? */
775 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Request queue empty, not processing queue\n");
782 return; /* no pending message */
784 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
786 GNUNET_TIME_UNIT_FOREVER_REL,
788 &transmit_message, h);
793 * Handler for notification messages received from the core.
795 * @param cls our "struct GNUNET_CORE_Handle"
796 * @param msg the message received from the core service
799 main_notify_handler (void *cls,
800 const struct GNUNET_MessageHeader *msg)
802 struct GNUNET_CORE_Handle *h = cls;
803 const struct InitReplyMessage *m;
804 const struct ConnectNotifyMessage *cnm;
805 const struct DisconnectNotifyMessage *dnm;
806 const struct NotifyTrafficMessage *ntm;
807 const struct GNUNET_MessageHeader *em;
808 const struct ConfigurationInfoMessage *cim;
809 const struct PeerStatusNotifyMessage *psnm;
810 const struct SendMessageReady *smr;
811 const struct GNUNET_CORE_MessageHandler *mh;
812 GNUNET_CORE_StartupCallback init;
813 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
814 struct PeerRecord *pr;
815 struct GNUNET_CORE_TransmitHandle *th;
823 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
825 ("Client was disconnected from core service, trying to reconnect.\n"));
829 msize = ntohs (msg->size);
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Processing message of type %u and size %u from core service\n",
833 ntohs (msg->type), msize);
835 switch (ntohs (msg->type))
837 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
838 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
844 m = (const struct InitReplyMessage *) msg;
845 GNUNET_break (0 == ntohl (m->reserved));
846 /* start our message processing loop */
847 if (GNUNET_YES == h->currently_down)
849 h->currently_down = GNUNET_NO;
850 trigger_next_request (h, GNUNET_NO);
852 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
853 if (NULL != (init = h->init))
855 /* mark so we don't call init on reconnect */
857 GNUNET_CRYPTO_hash (&m->publicKey,
859 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Connected to core service of peer `%s'.\n",
864 GNUNET_i2s (&h->me));
866 init (h->cls, h, &h->me, &m->publicKey);
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Successfully reconnected to core service.\n");
876 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
877 if (msize != sizeof (struct ConnectNotifyMessage))
883 cnm = (const struct ConnectNotifyMessage *) msg;
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Received notification about connection from `%s'.\n",
887 GNUNET_i2s (&cnm->peer));
889 if (0 == memcmp (&h->me,
891 sizeof (struct GNUNET_PeerIdentity)))
893 /* disconnect from self!? */
897 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
898 &cnm->peer.hashPubKey);
905 pr = GNUNET_malloc (sizeof (struct PeerRecord));
906 pr->peer = cnm->peer;
908 GNUNET_assert (GNUNET_YES ==
909 GNUNET_CONTAINER_multihashmap_put (h->peers,
910 &cnm->peer.hashPubKey,
912 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
913 if (NULL != h->connects)
916 NULL /* FIXME: atsi! */);
918 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
919 if (msize != sizeof (struct DisconnectNotifyMessage))
925 dnm = (const struct DisconnectNotifyMessage *) msg;
926 if (0 == memcmp (&h->me,
928 sizeof (struct GNUNET_PeerIdentity)))
930 /* connection to self!? */
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936 "Received notification about disconnect from `%s'.\n",
937 GNUNET_i2s (&dnm->peer));
939 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
940 &dnm->peer.hashPubKey);
947 trigger = ( (pr->prev != NULL) ||
948 (pr->next != NULL) ||
949 (h->ready_peer_head == pr) );
950 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
952 trigger_next_request (h, GNUNET_NO);
954 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
955 if (NULL == h->status_events)
960 if (msize != sizeof (struct PeerStatusNotifyMessage))
966 psnm = (const struct PeerStatusNotifyMessage *) msg;
967 if (0 == memcmp (&h->me,
969 sizeof (struct GNUNET_PeerIdentity)))
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Received notification about status change by `%s'.\n",
978 GNUNET_i2s (&psnm->peer));
980 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
981 &psnm->peer.hashPubKey);
988 h->status_events (h->cls,
992 GNUNET_TIME_absolute_ntoh (psnm->timeout),
993 NULL /* FIXME: atsi */);
995 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
997 sizeof (struct NotifyTrafficMessage) +
998 sizeof (struct GNUNET_MessageHeader))
1001 reconnect_later (h);
1004 ntm = (const struct NotifyTrafficMessage *) msg;
1005 if (0 == memcmp (&h->me,
1007 sizeof (struct GNUNET_PeerIdentity)))
1013 em = (const struct GNUNET_MessageHeader *) &ntm[1];
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "Received message of type %u and size %u from peer `%4s'\n",
1019 GNUNET_i2s (&ntm->peer));
1021 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1022 &ntm->peer.hashPubKey);
1026 reconnect_later (h);
1029 if ((GNUNET_NO == h->inbound_hdr_only) &&
1030 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
1033 reconnect_later (h);
1036 et = ntohs (em->type);
1037 for (hpos = 0; hpos < h->hcnt; hpos++)
1039 mh = &h->handlers[hpos];
1042 if ((mh->expected_size != ntohs (em->size)) &&
1043 (mh->expected_size != 0))
1049 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1050 NULL /* FIXME: atsi */))
1052 /* error in processing, do not process other messages! */
1056 if (NULL != h->inbound_notify)
1057 h->inbound_notify (h->cls, &ntm->peer, em,
1058 NULL /* FIXME: atsi */);
1060 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1062 sizeof (struct NotifyTrafficMessage) +
1063 sizeof (struct GNUNET_MessageHeader))
1066 reconnect_later (h);
1069 ntm = (const struct NotifyTrafficMessage *) msg;
1070 if (0 == memcmp (&h->me,
1072 sizeof (struct GNUNET_PeerIdentity)))
1078 em = (const struct GNUNET_MessageHeader *) &ntm[1];
1079 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1080 &ntm->peer.hashPubKey);
1084 reconnect_later (h);
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "Received notification about transmission to `%s'.\n",
1090 GNUNET_i2s (&ntm->peer));
1092 if ((GNUNET_NO == h->outbound_hdr_only) &&
1093 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
1096 reconnect_later (h);
1099 if (NULL == h->outbound_notify)
1104 h->outbound_notify (h->cls, &ntm->peer, em,
1105 NULL /* FIXME: atsi? */);
1107 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1108 if (msize != sizeof (struct SendMessageReady))
1111 reconnect_later (h);
1114 smr = (const struct SendMessageReady *) msg;
1115 if (0 == memcmp (&h->me,
1117 sizeof (struct GNUNET_PeerIdentity)))
1123 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1124 &smr->peer.hashPubKey);
1128 reconnect_later (h);
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1133 "Received notification about transmission readiness to `%s'.\n",
1134 GNUNET_i2s (&smr->peer));
1136 /* FIXME: pr->pending_head is sometimes NULL here... Safe to just return? Or does this indicate something is out of sync somewhere else? */
1137 if (pr->pending_head == NULL)
1143 th = pr->pending_head;
1144 if (ntohs (smr->smr_id) != th->smr_id)
1146 /* READY message is for expired or cancelled message,
1147 ignore! (we should have already sent another request) */
1150 if ( (pr->prev != NULL) ||
1151 (pr->next != NULL) ||
1152 (h->ready_peer_head == pr) )
1154 /* we should not already be on the ready list... */
1156 reconnect_later (h);
1159 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1162 trigger_next_request (h, GNUNET_NO);
1164 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1165 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1168 reconnect_later (h);
1171 cim = (const struct ConfigurationInfoMessage*) msg;
1172 if (0 == memcmp (&h->me,
1174 sizeof (struct GNUNET_PeerIdentity)))
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Received notification about configuration update for `%s'.\n",
1183 GNUNET_i2s (&cim->peer));
1185 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1186 &cim->peer.hashPubKey);
1190 reconnect_later (h);
1193 if (pr->rim_id != ntohl (cim->rim_id))
1201 ntohl (cim->reserved_amount),
1202 GNUNET_ntohll (cim->preference));
1205 reconnect_later (h);
1208 GNUNET_CLIENT_receive (h->client,
1209 &main_notify_handler, h,
1210 GNUNET_TIME_UNIT_FOREVER_REL);
1215 * Task executed once we are done transmitting the INIT message.
1216 * Starts our 'receive' loop.
1218 * @param cls the 'struct GNUNET_CORE_Handle'
1219 * @param tc task context
1222 init_done_task (void *cls,
1223 const struct GNUNET_SCHEDULER_TaskContext *tc)
1225 struct GNUNET_CORE_Handle *h = cls;
1229 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "Failed to exchange INIT with core, retrying\n");
1235 reconnect_later (h);
1238 GNUNET_CLIENT_receive (h->client,
1239 &main_notify_handler,
1241 GNUNET_TIME_UNIT_FOREVER_REL);
1246 * Our current client connection went down. Clean it up
1247 * and try to reconnect!
1249 * @param h our handle to the core service
1252 reconnect (struct GNUNET_CORE_Handle *h)
1254 struct ControlMessage *cm;
1255 struct InitMessage *init;
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 "Reconnecting to CORE service\n");
1265 GNUNET_assert (h->client == NULL);
1266 GNUNET_assert (h->currently_down == GNUNET_YES);
1267 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1268 if (h->client == NULL)
1270 reconnect_later (h);
1273 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1274 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1276 cm->cont = &init_done_task;
1278 init = (struct InitMessage*) &cm[1];
1279 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1280 init->header.size = htons (msize);
1281 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1282 if (h->status_events != NULL)
1283 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1284 if (h->inbound_notify != NULL)
1286 if (h->inbound_hdr_only)
1287 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1289 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1291 if (h->outbound_notify != NULL)
1293 if (h->outbound_hdr_only)
1294 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1296 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1298 init->options = htonl (opt);
1299 ts = (uint16_t *) &init[1];
1300 for (hpos = 0; hpos < h->hcnt; hpos++)
1301 ts[hpos] = htons (h->handlers[hpos].type);
1302 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1305 trigger_next_request (h, GNUNET_YES);
1311 * Connect to the core service. Note that the connection may
1312 * complete (or fail) asynchronously.
1314 * @param cfg configuration to use
1315 * @param queue_size size of the per-peer message queue
1316 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1317 * @param init callback to call on timeout or once we have successfully
1318 * connected to the core service; note that timeout is only meaningful if init is not NULL
1319 * @param connects function to call on peer connect, can be NULL
1320 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1321 * @param status_events function to call on changes to peer connection status, can be NULL
1322 * @param inbound_notify function to call for all inbound messages, can be NULL
1323 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1324 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1325 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1326 * @param outbound_notify function to call for all outbound messages, can be NULL
1327 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1328 * GNUNET_MessageHeader and hence we do not need to give it the full message
1329 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1330 * @param handlers callbacks for messages we care about, NULL-terminated
1331 * @return handle to the core service (only useful for disconnect until 'init' is called);
1332 * NULL on error (in this case, init is never called)
1334 struct GNUNET_CORE_Handle *
1335 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1336 unsigned int queue_size,
1338 GNUNET_CORE_StartupCallback init,
1339 GNUNET_CORE_ConnectEventHandler connects,
1340 GNUNET_CORE_DisconnectEventHandler disconnects,
1341 GNUNET_CORE_PeerStatusEventHandler status_events,
1342 GNUNET_CORE_MessageCallback inbound_notify,
1343 int inbound_hdr_only,
1344 GNUNET_CORE_MessageCallback outbound_notify,
1345 int outbound_hdr_only,
1346 const struct GNUNET_CORE_MessageHandler *handlers)
1348 struct GNUNET_CORE_Handle *h;
1350 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1352 h->queue_size = queue_size;
1355 h->connects = connects;
1356 h->disconnects = disconnects;
1357 h->status_events = status_events;
1358 h->inbound_notify = inbound_notify;
1359 h->outbound_notify = outbound_notify;
1360 h->inbound_hdr_only = inbound_hdr_only;
1361 h->outbound_hdr_only = outbound_hdr_only;
1362 h->handlers = handlers;
1364 h->currently_down = GNUNET_YES;
1365 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1366 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1367 while (handlers[h->hcnt].callback != NULL)
1369 GNUNET_assert (h->hcnt <
1370 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1371 sizeof (struct InitMessage)) / sizeof (uint16_t));
1373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374 "Connecting to CORE service\n");
1382 * Disconnect from the core service. This function can only
1383 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1384 * requests have been explicitly canceled.
1386 * @param handle connection to core to disconnect
1389 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1391 struct ControlMessage *cm;
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Disconnecting from CORE service\n");
1397 if (handle->cth != NULL)
1399 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1402 if (handle->client != NULL)
1404 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1405 handle->client = NULL;
1407 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1409 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1410 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1412 while (NULL != (cm = handle->pending_head))
1414 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1415 handle->pending_tail,
1419 if (cm->cont != NULL)
1420 cm->cont (cm->cont_cls, NULL);
1423 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1424 &disconnect_and_free_peer_entry,
1426 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1427 GNUNET_break (handle->ready_peer_head == NULL);
1428 GNUNET_free (handle);
1433 * Ask the core to call "notify" once it is ready to transmit the
1434 * given number of bytes to the specified "target". If we are not yet
1435 * connected to the specified peer, a call to this function will cause
1436 * us to try to establish a connection.
1438 * @param handle connection to core service
1439 * @param priority how important is the message?
1440 * @param maxdelay how long can the message wait?
1441 * @param target who should receive the message,
1442 * use NULL for this peer (loopback)
1443 * @param notify_size how many bytes of buffer space does notify want?
1444 * @param notify function to call when buffer space is available
1445 * @param notify_cls closure for notify
1446 * @return non-NULL if the notify callback was queued,
1447 * NULL if we can not even queue the request (insufficient
1448 * memory); if NULL is returned, "notify" will NOT be called.
1450 struct GNUNET_CORE_TransmitHandle *
1451 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1453 struct GNUNET_TIME_Relative maxdelay,
1454 const struct GNUNET_PeerIdentity *target,
1456 GNUNET_CONNECTION_TransmitReadyNotify notify,
1459 struct PeerRecord *pr;
1460 struct GNUNET_CORE_TransmitHandle *th;
1461 struct GNUNET_CORE_TransmitHandle *pos;
1462 struct GNUNET_CORE_TransmitHandle *prev;
1463 struct GNUNET_CORE_TransmitHandle *minp;
1465 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1466 &target->hashPubKey);
1469 /* attempt to send to peer that is not connected */
1470 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1471 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1472 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1476 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1477 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1478 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1480 th->get_message = notify;
1481 th->get_message_cls = notify_cls;
1482 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1483 th->priority = priority;
1484 th->msize = notify_size;
1485 /* bound queue size */
1486 if (pr->queue_size == handle->queue_size)
1488 /* find lowest-priority entry */
1489 minp = pr->pending_head;
1491 while (prev != NULL)
1493 if (prev->priority < minp->priority)
1499 GNUNET_break (handle->queue_size != 0);
1500 GNUNET_break (pr->queue_size == 0);
1503 if (priority <= minp->priority)
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Dropping transmission request: priority too low\n");
1509 return NULL; /* priority too low */
1511 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1516 minp->get_message (minp->get_message_cls,
1521 /* Order entries by deadline, but SKIP 'HEAD' if
1522 we're in the 'ready_peer_*' DLL */
1523 /* FIXME: again, pr->pending_head is NULL after a reconnect_later call */
1524 pos = pr->pending_head;
1525 if (pr->pending_head != NULL)
1527 if ( (pr->prev != NULL) ||
1528 (pr->next != NULL) ||
1529 (pr == handle->ready_peer_head) )
1531 GNUNET_assert (pos != NULL);
1532 pos = pos->next; /* skip head */
1540 /* insertion sort */
1542 while ( (pos != NULL) &&
1543 (pos->timeout.abs_value < th->timeout.abs_value) )
1548 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1553 /* was the request queue previously empty? */
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Transmission request added to queue\n");
1558 if (pr->pending_head == th)
1559 request_next_transmission (pr);
1565 * Cancel the specified transmission-ready notification.
1567 * @param th handle that was returned by "notify_transmit_ready".
1570 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1573 struct PeerRecord *pr = th->peer;
1574 struct GNUNET_CORE_Handle *h = pr->ch;
1577 was_head = (pr->pending_head == th);
1578 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1584 /* we're currently in the control queue, remove */
1585 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1588 GNUNET_free (th->cm);
1593 if ( (pr->prev != NULL) ||
1594 (pr->next != NULL) ||
1595 (pr == h->ready_peer_head) )
1597 /* the request that was 'approved' by core was
1598 canceled before it could be transmitted; remove
1599 us from the 'ready' list */
1600 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1604 request_next_transmission (pr);
1609 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1612 * Handle for a request to the core to connect to
1613 * a particular peer. Can be used to cancel the request
1614 * (before the 'cont'inuation is called).
1616 struct GNUNET_CORE_PeerRequestHandle
1620 * Link to control message.
1622 struct ControlMessage *cm;
1627 struct GNUNET_CORE_Handle *h;
1630 * Continuation to run when done.
1632 GNUNET_SCHEDULER_Task cont;
1635 * Closure for 'cont'.
1643 * Continuation called when the control message was transmitted.
1644 * Calls the original continuation and frees the remaining
1647 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1648 * @param tc scheduler context
1651 peer_request_connect_cont (void *cls,
1652 const struct GNUNET_SCHEDULER_TaskContext *tc)
1654 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1656 if (ret->cont != NULL)
1659 GNUNET_SCHEDULER_add_now (ret->cont,
1662 ret->cont (ret->cont_cls, tc);
1669 * Request that the core should try to connect to a particular peer.
1670 * Once the request has been transmitted to the core, the continuation
1671 * function will be called. Note that this does NOT mean that a
1672 * connection was successfully established -- it only means that the
1673 * core will now try. Successful establishment of the connection
1674 * will be signalled to the 'connects' callback argument of
1675 * 'GNUNET_CORE_connect' only. If the core service does not respond
1676 * to our connection attempt within the given time frame, 'cont' will
1677 * be called with the TIMEOUT reason code.
1679 * @param h core handle
1680 * @param timeout how long to try to talk to core
1681 * @param peer who should we connect to
1682 * @param cont function to call once the request has been completed (or timed out)
1683 * @param cont_cls closure for cont
1684 * @return NULL on error (cont will not be called), otherwise handle for cancellation
1686 struct GNUNET_CORE_PeerRequestHandle *
1687 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1688 struct GNUNET_TIME_Relative timeout,
1689 const struct GNUNET_PeerIdentity * peer,
1690 GNUNET_SCHEDULER_Task cont,
1693 struct GNUNET_CORE_PeerRequestHandle *ret;
1694 struct ControlMessage *cm;
1695 struct ConnectMessage *msg;
1697 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1698 sizeof (struct ConnectMessage));
1699 msg = (struct ConnectMessage*) &cm[1];
1700 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1701 msg->header.size = htons (sizeof (struct ConnectMessage));
1702 msg->reserved = htonl (0);
1703 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1705 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1708 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1712 ret->cont_cls = cont_cls;
1713 cm->cont = &peer_request_connect_cont;
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "Queueing REQUEST_CONNECT request\n");
1719 if (h->pending_head == cm)
1720 trigger_next_request (h, GNUNET_NO);
1726 * Cancel a pending request to connect to a particular peer. Must not
1727 * be called after the 'cont' function was invoked.
1729 * @param req request handle that was returned for the original request
1732 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1734 struct GNUNET_CORE_Handle *h = req->h;
1735 struct ControlMessage *cm = req->cm;
1737 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1745 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1748 struct GNUNET_CORE_InformationRequestContext
1752 * Our connection to the service.
1754 struct GNUNET_CORE_Handle *h;
1757 * Function to call with the information.
1759 GNUNET_CORE_PeerConfigurationInfoCallback info;
1767 * Link to control message, NULL if CM was sent.
1769 struct ControlMessage *cm;
1772 * Link to peer record.
1774 struct PeerRecord *pr;
1779 * CM was sent, remove link so we don't double-free.
1781 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1782 * @param tc scheduler context
1785 change_preference_send_continuation (void *cls,
1786 const struct GNUNET_SCHEDULER_TaskContext *tc)
1788 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1795 * Obtain statistics and/or change preferences for the given peer.
1797 * @param h core handle
1798 * @param peer identifies the peer
1799 * @param timeout after how long should we give up (and call "info" with NULL
1800 * for "peer" to signal an error)?
1801 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1802 * caller should set "bw_out" to "-1" to avoid changing
1803 * the current value; otherwise "bw_out" will be lowered to
1804 * the specified value; passing a pointer to "0" can be used to force
1805 * us to disconnect from the peer; "bw_out" might not increase
1806 * as specified since the upper bound is generally
1807 * determined by the other peer!
1808 * @param amount reserve N bytes for receiving, negative
1809 * amounts can be used to undo a (recent) reservation;
1810 * @param preference increase incoming traffic share preference by this amount;
1811 * in the absence of "amount" reservations, we use this
1812 * preference value to assign proportional bandwidth shares
1813 * to all connected peers
1814 * @param info function to call with the resulting configuration information
1815 * @param info_cls closure for info
1816 * @return NULL on error
1818 struct GNUNET_CORE_InformationRequestContext *
1819 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1820 const struct GNUNET_PeerIdentity *peer,
1821 struct GNUNET_TIME_Relative timeout,
1822 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1824 uint64_t preference,
1825 GNUNET_CORE_PeerConfigurationInfoCallback info,
1828 struct GNUNET_CORE_InformationRequestContext *irc;
1829 struct PeerRecord *pr;
1830 struct RequestInfoMessage *rim;
1831 struct ControlMessage *cm;
1833 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1837 /* attempt to change preference on peer that is not connected */
1841 if (pr->pcic != NULL)
1843 /* second change before first one is done */
1847 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1851 irc->info_cls = info_cls;
1852 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1853 sizeof (struct RequestInfoMessage));
1854 cm->cont = &change_preference_send_continuation;
1857 rim = (struct RequestInfoMessage*) &cm[1];
1858 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1859 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1860 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1861 rim->limit_outbound = bw_out;
1862 rim->reserve_inbound = htonl (amount);
1863 rim->preference_change = GNUNET_htonll(preference);
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "Queueing CHANGE PREFERENCE request\n");
1869 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1873 pr->pcic_cls = info_cls;
1874 if (h->pending_head == cm)
1875 trigger_next_request (h, GNUNET_NO);
1881 * Cancel request for getting information about a peer.
1882 * Note that an eventual change in preference, trust or bandwidth
1883 * assignment MAY have already been committed at the time,
1884 * so cancelling a request is NOT sure to undo the original
1885 * request. The original request may or may not still commit.
1886 * The only thing cancellation ensures is that the callback
1887 * from the original request will no longer be called.
1889 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1892 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1894 struct GNUNET_CORE_Handle *h = irc->h;
1895 struct PeerRecord *pr = irc->pr;
1897 if (irc->cm != NULL)
1899 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1902 GNUNET_free (irc->cm);
1905 pr->pcic_cls = NULL;
1910 /* end of core_api.c */