2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/core_api.c
23 * @brief core service; this is the main API for encrypted P2P
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
34 * Information we track for each peer.
40 * We generally do NOT keep peer records in a DLL; this
41 * DLL is only used IF this peer's 'pending_head' message
42 * is ready for transmission.
44 struct PeerRecord *prev;
47 * We generally do NOT keep peer records in a DLL; this
48 * DLL is only used IF this peer's 'pending_head' message
49 * is ready for transmission.
51 struct PeerRecord *next;
54 * Peer the record is about.
56 struct GNUNET_PeerIdentity peer;
59 * Corresponding core handle.
61 struct GNUNET_CORE_Handle *ch;
64 * Head of doubly-linked list of pending requests.
65 * Requests are sorted by deadline *except* for HEAD,
66 * which is only modified upon transmission to core.
68 struct GNUNET_CORE_TransmitHandle *pending_head;
71 * Tail of doubly-linked list of pending requests.
73 struct GNUNET_CORE_TransmitHandle *pending_tail;
76 * Pending callback waiting for peer information, or NULL for none.
78 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
86 * Request information ID for the given pcic (needed in case a
87 * request is cancelled after being submitted to core and a new
88 * one is generated; in this case, we need to avoid matching the
89 * reply to the first (cancelled) request to the second request).
94 * ID of timeout task for the 'pending_head' handle
95 * which is the one with the smallest timeout.
97 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
100 * Current size of the queue of pending requests.
102 unsigned int queue_size;
105 * SendMessageRequest ID generator for this peer.
113 * Entry in a doubly-linked list of control messages to be transmitted
114 * to the core service. Control messages include traffic allocation,
115 * connection requests and of course our initial 'init' request.
117 * The actual message is allocated at the end of this struct.
119 struct ControlMessage
122 * This is a doubly-linked list.
124 struct ControlMessage *next;
127 * This is a doubly-linked list.
129 struct ControlMessage *prev;
132 * Function to run after transmission failed/succeeded.
134 GNUNET_CORE_ControlContinuation cont;
137 * Closure for 'cont'.
142 * Transmit handle (if one is associated with this ControlMessage), or NULL.
144 struct GNUNET_CORE_TransmitHandle *th;
150 * Context for the core service connection.
152 struct GNUNET_CORE_Handle
156 * Configuration we're using.
158 const struct GNUNET_CONFIGURATION_Handle *cfg;
161 * Closure for the various callbacks.
166 * Function to call once we've handshaked with the core service.
168 GNUNET_CORE_StartupCallback init;
171 * Function to call whenever we're notified about a peer connecting.
173 GNUNET_CORE_ConnectEventHandler connects;
176 * Function to call whenever we're notified about a peer disconnecting.
178 GNUNET_CORE_DisconnectEventHandler disconnects;
181 * Function to call whenever we're notified about a peer changing status.
183 GNUNET_CORE_PeerStatusEventHandler status_events;
186 * Function to call whenever we receive an inbound message.
188 GNUNET_CORE_MessageCallback inbound_notify;
191 * Function to call whenever we receive an outbound message.
193 GNUNET_CORE_MessageCallback outbound_notify;
196 * Function handlers for messages of particular type.
198 const struct GNUNET_CORE_MessageHandler *handlers;
201 * Our connection to the service.
203 struct GNUNET_CLIENT_Connection *client;
206 * Handle for our current transmission request.
208 struct GNUNET_CLIENT_TransmitHandle *cth;
211 * Head of doubly-linked list of pending requests.
213 struct ControlMessage *control_pending_head;
216 * Tail of doubly-linked list of pending requests.
218 struct ControlMessage *control_pending_tail;
221 * Head of doubly-linked list of peers that are core-approved
222 * to send their next message.
224 struct PeerRecord *ready_peer_head;
227 * Tail of doubly-linked list of peers that are core-approved
228 * to send their next message.
230 struct PeerRecord *ready_peer_tail;
233 * Hash map listing all of the peers that we are currently
236 struct GNUNET_CONTAINER_MultiHashMap *peers;
239 * Identity of this peer.
241 struct GNUNET_PeerIdentity me;
244 * ID of reconnect task (if any).
246 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
249 * Current delay we use for re-trying to connect to core.
251 struct GNUNET_TIME_Relative retry_backoff;
254 * Request information ID generator.
259 * Number of messages we are allowed to queue per target.
261 unsigned int queue_size;
264 * Number of entries in the handlers array.
269 * For inbound notifications without a specific handler, do
270 * we expect to only receive headers?
272 int inbound_hdr_only;
275 * For outbound notifications without a specific handler, do
276 * we expect to only receive headers?
278 int outbound_hdr_only;
281 * Are we currently disconnected and hence unable to forward
290 * Handle for a transmission request.
292 struct GNUNET_CORE_TransmitHandle
296 * We keep active transmit handles in a doubly-linked list.
298 struct GNUNET_CORE_TransmitHandle *next;
301 * We keep active transmit handles in a doubly-linked list.
303 struct GNUNET_CORE_TransmitHandle *prev;
306 * Corresponding peer record.
308 struct PeerRecord *peer;
311 * Corresponding SEND_REQUEST message. Only non-NULL
312 * while SEND_REQUEST message is pending.
314 struct ControlMessage *cm;
317 * Function that will be called to get the actual request
318 * (once we are ready to transmit this request to the core).
319 * The function will be called with a NULL buffer to signal
322 GNUNET_CONNECTION_TransmitReadyNotify get_message;
325 * Closure for get_message.
327 void *get_message_cls;
330 * Timeout for this handle.
332 struct GNUNET_TIME_Absolute timeout;
335 * How important is this message?
340 * Size of this request.
345 * Send message request ID for this request.
350 * Is corking allowed?
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))
426 GNUNET_TIME_UNIT_FOREVER_REL,
429 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
431 GNUNET_SCHEDULER_cancel (pr->timeout_task);
432 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
434 GNUNET_assert (pr->queue_size == 0);
435 if ( (pr->prev != NULL) ||
436 (pr->next != NULL) ||
437 (h->ready_peer_head == pr) )
438 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
441 if (h->disconnects != NULL)
442 h->disconnects (h->cls,
444 GNUNET_assert (GNUNET_YES ==
445 GNUNET_CONTAINER_multihashmap_remove (h->peers,
448 GNUNET_assert (pr->pending_head == NULL);
449 GNUNET_assert (pr->pending_tail == NULL);
450 GNUNET_assert (pr->ch = h);
451 GNUNET_assert (pr->queue_size == 0);
452 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
459 * Close down any existing connection to the CORE service and
460 * try re-establishing it later.
462 * @param h our handle
465 reconnect_later (struct GNUNET_CORE_Handle *h)
467 struct ControlMessage *cm;
468 struct PeerRecord *pr;
470 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
471 if (h->client != NULL)
473 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
476 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
477 &disconnect_and_free_peer_entry,
480 while (NULL != (pr = h->ready_peer_head))
481 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
484 h->currently_down = GNUNET_YES;
485 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
488 while (NULL != (cm = h->control_pending_head))
490 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
491 h->control_pending_tail,
495 if (cm->cont != NULL)
496 cm->cont (cm->cont_cls, GNUNET_NO);
499 GNUNET_assert (h->control_pending_head == NULL);
500 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
502 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
507 * Check the list of pending requests, send the next
510 * @param h core handle
511 * @param ignore_currently_down transmit message even if not initialized?
514 trigger_next_request (struct GNUNET_CORE_Handle *h,
515 int ignore_currently_down);
519 * The given request hit its timeout. Remove from the
520 * doubly-linked list and call the respective continuation.
522 * @param cls the transmit handle of the request that timed out
523 * @param tc context, can be NULL (!)
526 transmission_timeout (void *cls,
527 const struct GNUNET_SCHEDULER_TaskContext *tc);
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);
555 return; /* already done */
556 GNUNET_assert (pr->prev == NULL);
557 GNUNET_assert (pr->next == NULL);
558 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
559 &transmission_timeout,
561 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
562 sizeof (struct SendMessageRequest));
565 smr = (struct SendMessageRequest*) &cm[1];
566 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
567 smr->header.size = htons (sizeof (struct SendMessageRequest));
568 smr->priority = htonl (th->priority);
569 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
570 smr->peer = pr->peer;
571 smr->queue_size = htonl (pr->queue_size);
572 smr->size = htons (th->msize);
573 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
574 GNUNET_CONTAINER_DLL_insert_after (h->control_pending_head,
575 h->control_pending_tail,
576 h->control_pending_tail,
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Adding SEND REQUEST for peer `%s' to message queue\n",
581 GNUNET_i2s (&pr->peer));
583 trigger_next_request (h, GNUNET_NO);
588 * The given request hit its timeout. Remove from the
589 * doubly-linked list and call the respective continuation.
591 * @param cls the transmit handle of the request that timed out
592 * @param tc context, can be NULL (!)
595 transmission_timeout (void *cls,
596 const struct GNUNET_SCHEDULER_TaskContext *tc)
598 struct PeerRecord *pr = cls;
599 struct GNUNET_CORE_Handle *h = pr->ch;
600 struct GNUNET_CORE_TransmitHandle *th;
602 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
603 th = pr->pending_head;
604 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
608 if ( (pr->prev != NULL) ||
609 (pr->next != NULL) ||
610 (pr == h->ready_peer_head) )
612 /* the request that was 'approved' by core was
613 canceled before it could be transmitted; remove
614 us from the 'ready' list */
615 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Signalling timeout of request for transmission to CORE service\n");
623 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
624 request_next_transmission (pr);
629 * Transmit the next message to the core service.
632 transmit_message (void *cls,
636 struct GNUNET_CORE_Handle *h = cls;
637 struct ControlMessage *cm;
638 struct GNUNET_CORE_TransmitHandle *th;
639 struct PeerRecord *pr;
640 struct SendMessage *sm;
641 const struct GNUNET_MessageHeader *hdr;
645 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Transmission failed, initiating reconnect\n");
656 /* first check for control messages */
657 if (NULL != (cm = h->control_pending_head))
659 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
660 msize = ntohs (hdr->size);
663 trigger_next_request (h, GNUNET_NO);
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Transmitting control message with %u bytes of type %u to core.\n",
669 (unsigned int) msize,
670 (unsigned int) ntohs (hdr->type));
672 memcpy (buf, hdr, msize);
673 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
674 h->control_pending_tail,
678 if (NULL != cm->cont)
679 cm->cont (cm->cont_cls, GNUNET_OK);
681 trigger_next_request (h, GNUNET_NO);
684 /* now check for 'ready' P2P messages */
685 if (NULL != (pr = h->ready_peer_head))
687 GNUNET_assert (pr->pending_head != NULL);
688 th = pr->pending_head;
689 if (size < th->msize + sizeof (struct SendMessage))
691 trigger_next_request (h, GNUNET_NO);
694 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
697 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
701 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
703 GNUNET_SCHEDULER_cancel (pr->timeout_task);
704 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Transmitting SEND request to `%s' with %u bytes.\n",
709 GNUNET_i2s (&pr->peer),
710 (unsigned int) th->msize);
712 sm = (struct SendMessage *) buf;
713 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
714 sm->priority = htonl (th->priority);
715 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
717 sm->cork = htonl ((uint32_t) th->cork);
718 sm->reserved = htonl (0);
719 ret = th->get_message (th->get_message_cls,
720 size - sizeof (struct SendMessage),
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Size of clients message to peer %s is 0!\n",
728 GNUNET_i2s(&pr->peer));
730 /* client decided to send nothing! */
731 request_next_transmission (pr);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Produced SEND message to core with %u bytes payload\n",
739 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
740 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
743 request_next_transmission (pr);
746 ret += sizeof (struct SendMessage);
747 sm->header.size = htons (ret);
748 GNUNET_assert (ret <= size);
750 request_next_transmission (pr);
758 * Check the list of pending requests, send the next
761 * @param h core handle
762 * @param ignore_currently_down transmit message even if not initialized?
765 trigger_next_request (struct GNUNET_CORE_Handle *h,
766 int ignore_currently_down)
770 if ( (GNUNET_YES == h->currently_down) &&
771 (ignore_currently_down == GNUNET_NO) )
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Core connection down, not processing queue\n");
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Request pending, not processing queue\n");
787 if (h->control_pending_head != NULL)
788 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
789 else if (h->ready_peer_head != NULL)
790 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 "Request queue empty, not processing queue\n");
797 return; /* no pending message */
799 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
801 GNUNET_TIME_UNIT_FOREVER_REL,
803 &transmit_message, h);
808 * Handler for notification messages received from the core.
810 * @param cls our "struct GNUNET_CORE_Handle"
811 * @param msg the message received from the core service
814 main_notify_handler (void *cls,
815 const struct GNUNET_MessageHeader *msg)
817 struct GNUNET_CORE_Handle *h = cls;
818 const struct InitReplyMessage *m;
819 const struct ConnectNotifyMessage *cnm;
820 const struct DisconnectNotifyMessage *dnm;
821 const struct NotifyTrafficMessage *ntm;
822 const struct GNUNET_MessageHeader *em;
823 const struct ConfigurationInfoMessage *cim;
824 const struct PeerStatusNotifyMessage *psnm;
825 const struct SendMessageReady *smr;
826 const struct GNUNET_CORE_MessageHandler *mh;
827 GNUNET_CORE_StartupCallback init;
828 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
829 struct PeerRecord *pr;
830 struct GNUNET_CORE_TransmitHandle *th;
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
841 ("Client was disconnected from core service, trying to reconnect.\n"));
845 msize = ntohs (msg->size);
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Processing message of type %u and size %u from core service\n",
849 ntohs (msg->type), msize);
851 switch (ntohs (msg->type))
853 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
854 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
860 m = (const struct InitReplyMessage *) msg;
861 GNUNET_break (0 == ntohl (m->reserved));
862 /* start our message processing loop */
863 if (GNUNET_YES == h->currently_down)
865 h->currently_down = GNUNET_NO;
866 trigger_next_request (h, GNUNET_NO);
868 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
869 GNUNET_CRYPTO_hash (&m->publicKey,
871 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
873 if (NULL != (init = h->init))
875 /* mark so we don't call init on reconnect */
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Connected to core service of peer `%s'.\n",
880 GNUNET_i2s (&h->me));
882 init (h->cls, h, &h->me, &m->publicKey);
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "Successfully reconnected to core service.\n");
891 /* fake 'connect to self' */
892 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
894 GNUNET_assert (pr == NULL);
895 pr = GNUNET_malloc (sizeof (struct PeerRecord));
898 GNUNET_assert (GNUNET_YES ==
899 GNUNET_CONTAINER_multihashmap_put (h->peers,
902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
903 if (NULL != h->connects)
908 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
909 if (msize < sizeof (struct ConnectNotifyMessage))
915 cnm = (const struct ConnectNotifyMessage *) msg;
916 ats_count = ntohl (cnm->ats_count);
917 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
918 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Received notification about connection from `%s'.\n",
927 GNUNET_i2s (&cnm->peer));
929 if (0 == memcmp (&h->me,
931 sizeof (struct GNUNET_PeerIdentity)))
933 /* connect to self!? */
937 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
938 &cnm->peer.hashPubKey);
945 pr = GNUNET_malloc (sizeof (struct PeerRecord));
946 pr->peer = cnm->peer;
948 GNUNET_assert (GNUNET_YES ==
949 GNUNET_CONTAINER_multihashmap_put (h->peers,
950 &cnm->peer.hashPubKey,
952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
953 if (NULL != h->connects)
958 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
959 if (msize != sizeof (struct DisconnectNotifyMessage))
965 dnm = (const struct DisconnectNotifyMessage *) msg;
966 if (0 == memcmp (&h->me,
968 sizeof (struct GNUNET_PeerIdentity)))
970 /* connection to self!? */
974 GNUNET_break (0 == ntohl (dnm->reserved));
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Received notification about disconnect from `%s'.\n",
978 GNUNET_i2s (&dnm->peer));
980 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
981 &dnm->peer.hashPubKey);
988 trigger = ( (pr->prev != NULL) ||
989 (pr->next != NULL) ||
990 (h->ready_peer_head == pr) );
991 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
993 trigger_next_request (h, GNUNET_NO);
995 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
996 if (NULL == h->status_events)
1000 if (msize < sizeof (struct PeerStatusNotifyMessage))
1003 reconnect_later (h);
1006 psnm = (const struct PeerStatusNotifyMessage *) msg;
1007 if (0 == memcmp (&h->me,
1009 sizeof (struct GNUNET_PeerIdentity)))
1015 ats_count = ntohl (psnm->ats_count);
1016 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1017 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1020 reconnect_later (h);
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "Received notification about status change by `%s'.\n",
1026 GNUNET_i2s (&psnm->peer));
1028 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1029 &psnm->peer.hashPubKey);
1033 reconnect_later (h);
1036 h->status_events (h->cls,
1039 psnm->bandwidth_out,
1040 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1043 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1044 if (msize < sizeof (struct NotifyTrafficMessage))
1047 reconnect_later (h);
1050 ntm = (const struct NotifyTrafficMessage *) msg;
1052 ats_count = ntohl (ntm->ats_count);
1053 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1054 + sizeof (struct GNUNET_MessageHeader)) ||
1055 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1058 reconnect_later (h);
1061 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Received message of type %u and size %u from peer `%4s'\n",
1067 GNUNET_i2s (&ntm->peer));
1069 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1070 &ntm->peer.hashPubKey);
1074 reconnect_later (h);
1077 if ((GNUNET_NO == h->inbound_hdr_only) &&
1078 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1079 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1082 reconnect_later (h);
1085 et = ntohs (em->type);
1086 for (hpos = 0; hpos < h->hcnt; hpos++)
1088 mh = &h->handlers[hpos];
1091 if ((mh->expected_size != ntohs (em->size)) &&
1092 (mh->expected_size != 0))
1098 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1101 /* error in processing, do not process other messages! */
1105 if (NULL != h->inbound_notify)
1106 h->inbound_notify (h->cls, &ntm->peer, em,
1109 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1110 if (msize < sizeof (struct NotifyTrafficMessage))
1113 reconnect_later (h);
1116 ntm = (const struct NotifyTrafficMessage *) msg;
1117 if (0 == memcmp (&h->me,
1119 sizeof (struct GNUNET_PeerIdentity)))
1125 ats_count = ntohl (ntm->ats_count);
1126 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1127 + sizeof (struct GNUNET_MessageHeader)) ||
1128 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1131 reconnect_later (h);
1134 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1135 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1136 &ntm->peer.hashPubKey);
1140 reconnect_later (h);
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Received notification about transmission to `%s'.\n",
1146 GNUNET_i2s (&ntm->peer));
1148 if ((GNUNET_NO == h->outbound_hdr_only) &&
1149 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1150 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1153 reconnect_later (h);
1156 if (NULL == h->outbound_notify)
1161 h->outbound_notify (h->cls, &ntm->peer, em,
1164 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1165 if (msize != sizeof (struct SendMessageReady))
1168 reconnect_later (h);
1171 smr = (const struct SendMessageReady *) msg;
1172 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1173 &smr->peer.hashPubKey);
1177 reconnect_later (h);
1181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1182 "Received notification about transmission readiness to `%s'.\n",
1183 GNUNET_i2s (&smr->peer));
1185 if (pr->pending_head == NULL)
1187 /* request must have been cancelled between the original request
1188 and the response from core, ignore core's readiness */
1192 th = pr->pending_head;
1193 if (ntohs (smr->smr_id) != th->smr_id)
1195 /* READY message is for expired or cancelled message,
1196 ignore! (we should have already sent another request) */
1199 if ( (pr->prev != NULL) ||
1200 (pr->next != NULL) ||
1201 (h->ready_peer_head == pr) )
1203 /* we should not already be on the ready list... */
1205 reconnect_later (h);
1208 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1211 trigger_next_request (h, GNUNET_NO);
1213 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1214 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1217 reconnect_later (h);
1220 cim = (const struct ConfigurationInfoMessage*) msg;
1221 if (0 == memcmp (&h->me,
1223 sizeof (struct GNUNET_PeerIdentity)))
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Received notification about configuration update for `%s'.\n",
1232 GNUNET_i2s (&cim->peer));
1234 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1235 &cim->peer.hashPubKey);
1239 reconnect_later (h);
1242 if (pr->rim_id != ntohl (cim->rim_id))
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Reservation ID mismatch in notification...\n");
1256 ntohl (cim->reserved_amount),
1257 GNUNET_TIME_relative_ntoh (cim->reserve_delay),
1258 GNUNET_ntohll (cim->preference));
1261 reconnect_later (h);
1264 GNUNET_CLIENT_receive (h->client,
1265 &main_notify_handler, h,
1266 GNUNET_TIME_UNIT_FOREVER_REL);
1271 * Task executed once we are done transmitting the INIT message.
1272 * Starts our 'receive' loop.
1274 * @param cls the 'struct GNUNET_CORE_Handle'
1275 * @param success were we successful
1278 init_done_task (void *cls,
1281 struct GNUNET_CORE_Handle *h = cls;
1283 if (success == GNUNET_SYSERR)
1284 return; /* shutdown */
1285 if (success == GNUNET_NO)
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289 "Failed to exchange INIT with core, retrying\n");
1291 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1292 reconnect_later (h);
1295 GNUNET_CLIENT_receive (h->client,
1296 &main_notify_handler,
1298 GNUNET_TIME_UNIT_FOREVER_REL);
1303 * Our current client connection went down. Clean it up
1304 * and try to reconnect!
1306 * @param h our handle to the core service
1309 reconnect (struct GNUNET_CORE_Handle *h)
1311 struct ControlMessage *cm;
1312 struct InitMessage *init;
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Reconnecting to CORE service\n");
1322 GNUNET_assert (h->client == NULL);
1323 GNUNET_assert (h->currently_down == GNUNET_YES);
1324 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1325 if (h->client == NULL)
1327 reconnect_later (h);
1330 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1331 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1333 cm->cont = &init_done_task;
1335 init = (struct InitMessage*) &cm[1];
1336 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1337 init->header.size = htons (msize);
1338 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1339 if (h->status_events != NULL)
1340 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1341 if (h->inbound_notify != NULL)
1343 if (h->inbound_hdr_only)
1344 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1346 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1348 if (h->outbound_notify != NULL)
1350 if (h->outbound_hdr_only)
1351 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1353 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1355 init->options = htonl (opt);
1356 ts = (uint16_t *) &init[1];
1357 for (hpos = 0; hpos < h->hcnt; hpos++)
1358 ts[hpos] = htons (h->handlers[hpos].type);
1359 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1360 h->control_pending_tail,
1362 trigger_next_request (h, GNUNET_YES);
1368 * Connect to the core service. Note that the connection may
1369 * complete (or fail) asynchronously.
1371 * @param cfg configuration to use
1372 * @param queue_size size of the per-peer message queue
1373 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1374 * @param init callback to call on timeout or once we have successfully
1375 * connected to the core service; note that timeout is only meaningful if init is not NULL
1376 * @param connects function to call on peer connect, can be NULL
1377 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1378 * @param status_events function to call on changes to peer connection status, can be NULL
1379 * @param inbound_notify function to call for all inbound messages, can be NULL
1380 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1381 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1382 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1383 * @param outbound_notify function to call for all outbound messages, can be NULL
1384 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1385 * GNUNET_MessageHeader and hence we do not need to give it the full message
1386 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1387 * @param handlers callbacks for messages we care about, NULL-terminated
1388 * @return handle to the core service (only useful for disconnect until 'init' is called);
1389 * NULL on error (in this case, init is never called)
1391 struct GNUNET_CORE_Handle *
1392 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1393 unsigned int queue_size,
1395 GNUNET_CORE_StartupCallback init,
1396 GNUNET_CORE_ConnectEventHandler connects,
1397 GNUNET_CORE_DisconnectEventHandler disconnects,
1398 GNUNET_CORE_PeerStatusEventHandler status_events,
1399 GNUNET_CORE_MessageCallback inbound_notify,
1400 int inbound_hdr_only,
1401 GNUNET_CORE_MessageCallback outbound_notify,
1402 int outbound_hdr_only,
1403 const struct GNUNET_CORE_MessageHandler *handlers)
1405 struct GNUNET_CORE_Handle *h;
1407 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1409 h->queue_size = queue_size;
1412 h->connects = connects;
1413 h->disconnects = disconnects;
1414 h->status_events = status_events;
1415 h->inbound_notify = inbound_notify;
1416 h->outbound_notify = outbound_notify;
1417 h->inbound_hdr_only = inbound_hdr_only;
1418 h->outbound_hdr_only = outbound_hdr_only;
1419 h->handlers = handlers;
1421 h->currently_down = GNUNET_YES;
1422 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1423 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1424 while (handlers[h->hcnt].callback != NULL)
1426 GNUNET_assert (h->hcnt <
1427 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1428 sizeof (struct InitMessage)) / sizeof (uint16_t));
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Connecting to CORE service\n");
1439 * Disconnect from the core service. This function can only
1440 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1441 * requests have been explicitly canceled.
1443 * @param handle connection to core to disconnect
1446 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1448 struct ControlMessage *cm;
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1452 "Disconnecting from CORE service\n");
1454 if (handle->cth != NULL)
1456 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1459 if (handle->client != NULL)
1461 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1462 handle->client = NULL;
1464 while (NULL != (cm = handle->control_pending_head))
1466 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1467 handle->control_pending_tail,
1471 if (cm->cont != NULL)
1472 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1475 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1477 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1478 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1480 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1481 &disconnect_and_free_peer_entry,
1483 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1484 GNUNET_break (handle->ready_peer_head == NULL);
1485 GNUNET_free (handle);
1490 * Ask the core to call "notify" once it is ready to transmit the
1491 * given number of bytes to the specified "target". Must only be
1492 * called after a connection to the respective peer has been
1493 * established (and the client has been informed about this).
1495 * @param handle connection to core service
1496 * @param cork is corking allowed for this transmission?
1497 * @param priority how important is the message?
1498 * @param maxdelay how long can the message wait?
1499 * @param target who should receive the message,
1500 * use NULL for this peer (loopback)
1501 * @param notify_size how many bytes of buffer space does notify want?
1502 * @param notify function to call when buffer space is available
1503 * @param notify_cls closure for notify
1504 * @return non-NULL if the notify callback was queued,
1505 * NULL if we can not even queue the request (insufficient
1506 * memory); if NULL is returned, "notify" will NOT be called.
1508 struct GNUNET_CORE_TransmitHandle *
1509 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1512 struct GNUNET_TIME_Relative maxdelay,
1513 const struct GNUNET_PeerIdentity *target,
1515 GNUNET_CONNECTION_TransmitReadyNotify notify,
1518 struct PeerRecord *pr;
1519 struct GNUNET_CORE_TransmitHandle *th;
1520 struct GNUNET_CORE_TransmitHandle *pos;
1521 struct GNUNET_CORE_TransmitHandle *prev;
1522 struct GNUNET_CORE_TransmitHandle *minp;
1524 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1525 &target->hashPubKey);
1528 /* attempt to send to peer that is not connected */
1529 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1530 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1531 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1535 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1536 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1537 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1539 GNUNET_assert(NULL != notify);
1540 th->get_message = notify;
1541 th->get_message_cls = notify_cls;
1542 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1543 th->priority = priority;
1544 th->msize = notify_size;
1546 /* bound queue size */
1547 if (pr->queue_size == handle->queue_size)
1549 /* find lowest-priority entry, but skip the head of the list */
1550 minp = pr->pending_head->next;
1552 while (prev != NULL)
1554 if (prev->priority < minp->priority)
1560 GNUNET_break (handle->queue_size != 0);
1561 GNUNET_break (pr->queue_size == 0);
1565 if (priority <= minp->priority)
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Dropping transmission request: priority too low\n");
1571 return NULL; /* priority too low */
1573 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1578 minp->get_message (minp->get_message_cls,
1583 /* Order entries by deadline, but SKIP 'HEAD' if
1584 we're in the 'ready_peer_*' DLL */
1585 pos = pr->pending_head;
1586 if ( (pr->prev != NULL) ||
1587 (pr->next != NULL) ||
1588 (pr == handle->ready_peer_head) )
1590 GNUNET_assert (pos != NULL);
1591 pos = pos->next; /* skip head */
1594 /* insertion sort */
1596 while ( (pos != NULL) &&
1597 (pos->timeout.abs_value < th->timeout.abs_value) )
1602 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1607 /* was the request queue previously empty? */
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "Transmission request added to queue\n");
1612 if (pr->pending_head == th)
1613 request_next_transmission (pr);
1619 * Cancel the specified transmission-ready notification.
1621 * @param th handle that was returned by "notify_transmit_ready".
1624 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1627 struct PeerRecord *pr = th->peer;
1628 struct GNUNET_CORE_Handle *h = pr->ch;
1631 was_head = (pr->pending_head == th);
1632 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1638 /* we're currently in the control queue, remove */
1639 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1640 h->control_pending_tail,
1642 GNUNET_free (th->cm);
1647 if ( (pr->prev != NULL) ||
1648 (pr->next != NULL) ||
1649 (pr == h->ready_peer_head) )
1651 /* the request that was 'approved' by core was
1652 canceled before it could be transmitted; remove
1653 us from the 'ready' list */
1654 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1658 request_next_transmission (pr);
1663 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1666 * Handle for a request to the core to connect to
1667 * a particular peer. Can be used to cancel the request
1668 * (before the 'cont'inuation is called).
1670 struct GNUNET_CORE_PeerRequestHandle
1674 * Link to control message.
1676 struct ControlMessage *cm;
1681 struct GNUNET_CORE_Handle *h;
1684 * Continuation to run when done.
1686 GNUNET_CORE_ControlContinuation cont;
1689 * Closure for 'cont'.
1697 * Continuation called when the control message was transmitted.
1698 * Calls the original continuation and frees the remaining
1701 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1702 * @param success was the request transmitted?
1705 peer_request_connect_cont (void *cls,
1708 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1710 if (ret->cont != NULL)
1711 ret->cont (ret->cont_cls, success);
1717 * Request that the core should try to connect to a particular peer.
1718 * Once the request has been transmitted to the core, the continuation
1719 * function will be called. Note that this does NOT mean that a
1720 * connection was successfully established -- it only means that the
1721 * core will now try. Successful establishment of the connection
1722 * will be signalled to the 'connects' callback argument of
1723 * 'GNUNET_CORE_connect' only. If the core service does not respond
1724 * to our connection attempt within the given time frame, 'cont' will
1725 * be called with the TIMEOUT reason code.
1727 * @param h core handle
1728 * @param timeout how long to try to talk to core
1729 * @param peer who should we connect to
1730 * @param cont function to call once the request has been completed (or timed out)
1731 * @param cont_cls closure for cont
1733 * @return NULL on error or already connected,
1734 * otherwise handle for cancellation
1736 struct GNUNET_CORE_PeerRequestHandle *
1737 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1738 struct GNUNET_TIME_Relative timeout,
1739 const struct GNUNET_PeerIdentity * peer,
1740 GNUNET_CORE_ControlContinuation cont,
1743 struct GNUNET_CORE_PeerRequestHandle *ret;
1744 struct ControlMessage *cm;
1745 struct ConnectMessage *msg;
1747 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1751 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peers are already connected!\n");
1756 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1757 sizeof (struct ConnectMessage));
1758 msg = (struct ConnectMessage*) &cm[1];
1759 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1760 msg->header.size = htons (sizeof (struct ConnectMessage));
1761 msg->reserved = htonl (0);
1762 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1764 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1765 h->control_pending_tail,
1767 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1771 ret->cont_cls = cont_cls;
1772 cm->cont = &peer_request_connect_cont;
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Queueing REQUEST_CONNECT request\n");
1778 if (h->control_pending_head == cm)
1779 trigger_next_request (h, GNUNET_NO);
1785 * Cancel a pending request to connect to a particular peer. Must not
1786 * be called after the 'cont' function was invoked.
1788 * @param req request handle that was returned for the original request
1791 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1793 struct GNUNET_CORE_Handle *h = req->h;
1794 struct ControlMessage *cm = req->cm;
1796 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1797 h->control_pending_tail,
1804 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1807 struct GNUNET_CORE_InformationRequestContext
1811 * Our connection to the service.
1813 struct GNUNET_CORE_Handle *h;
1816 * Link to control message, NULL if CM was sent.
1818 struct ControlMessage *cm;
1821 * Link to peer record.
1823 struct PeerRecord *pr;
1828 * CM was sent, remove link so we don't double-free.
1830 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1831 * @param success were we successful?
1834 change_preference_send_continuation (void *cls,
1837 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1844 * Obtain statistics and/or change preferences for the given peer.
1846 * @param h core handle
1847 * @param peer identifies the peer
1848 * @param timeout after how long should we give up (and call "info" with NULL
1849 * for "peer" to signal an error)?
1850 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1851 * caller should set "bw_out" to "-1" to avoid changing
1852 * the current value; otherwise "bw_out" will be lowered to
1853 * the specified value; passing a pointer to "0" can be used to force
1854 * us to disconnect from the peer; "bw_out" might not increase
1855 * as specified since the upper bound is generally
1856 * determined by the other peer!
1857 * @param amount reserve N bytes for receiving, negative
1858 * amounts can be used to undo a (recent) reservation;
1859 * @param preference increase incoming traffic share preference by this amount;
1860 * in the absence of "amount" reservations, we use this
1861 * preference value to assign proportional bandwidth shares
1862 * to all connected peers
1863 * @param info function to call with the resulting configuration information
1864 * @param info_cls closure for info
1865 * @return NULL on error
1867 struct GNUNET_CORE_InformationRequestContext *
1868 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1869 const struct GNUNET_PeerIdentity *peer,
1870 struct GNUNET_TIME_Relative timeout,
1871 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1873 uint64_t preference,
1874 GNUNET_CORE_PeerConfigurationInfoCallback info,
1877 struct GNUNET_CORE_InformationRequestContext *irc;
1878 struct PeerRecord *pr;
1879 struct RequestInfoMessage *rim;
1880 struct ControlMessage *cm;
1882 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1886 /* attempt to change preference on peer that is not connected */
1890 if (pr->pcic != NULL)
1892 /* second change before first one is done */
1896 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1899 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1900 sizeof (struct RequestInfoMessage));
1901 cm->cont = &change_preference_send_continuation;
1904 rim = (struct RequestInfoMessage*) &cm[1];
1905 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1906 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1907 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1908 rim->limit_outbound = bw_out;
1909 rim->reserve_inbound = htonl (amount);
1910 rim->preference_change = GNUNET_htonll(preference);
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Queueing CHANGE PREFERENCE request\n");
1916 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1917 h->control_pending_tail,
1920 pr->pcic_cls = info_cls;
1921 if (h->control_pending_head == cm)
1922 trigger_next_request (h, GNUNET_NO);
1928 * Cancel request for getting information about a peer.
1929 * Note that an eventual change in preference, trust or bandwidth
1930 * assignment MAY have already been committed at the time,
1931 * so cancelling a request is NOT sure to undo the original
1932 * request. The original request may or may not still commit.
1933 * The only thing cancellation ensures is that the callback
1934 * from the original request will no longer be called.
1936 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1939 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1941 struct GNUNET_CORE_Handle *h = irc->h;
1942 struct PeerRecord *pr = irc->pr;
1944 if (irc->cm != NULL)
1946 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1947 h->control_pending_tail,
1949 GNUNET_free (irc->cm);
1952 pr->pcic_cls = NULL;
1957 /* end of core_api.c */