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 * Pointer to free when we call pcic.
91 * Request information ID for the given pcic (needed in case a
92 * request is cancelled after being submitted to core and a new
93 * one is generated; in this case, we need to avoid matching the
94 * reply to the first (cancelled) request to the second request).
99 * ID of timeout task for the 'pending_head' handle
100 * which is the one with the smallest timeout.
102 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
105 * Current size of the queue of pending requests.
107 unsigned int queue_size;
110 * SendMessageRequest ID generator for this peer.
118 * Entry in a doubly-linked list of control messages to be transmitted
119 * to the core service. Control messages include traffic allocation,
120 * connection requests and of course our initial 'init' request.
122 * The actual message is allocated at the end of this struct.
124 struct ControlMessage
127 * This is a doubly-linked list.
129 struct ControlMessage *next;
132 * This is a doubly-linked list.
134 struct ControlMessage *prev;
137 * Function to run after transmission failed/succeeded.
139 GNUNET_CORE_ControlContinuation 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 *control_pending_head;
221 * Tail of doubly-linked list of pending requests.
223 struct ControlMessage *control_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.
355 * Is corking allowed?
363 * Our current client connection went down. Clean it up
364 * and try to reconnect!
366 * @param h our handle to the core service
369 reconnect (struct GNUNET_CORE_Handle *h);
373 * Task schedule to try to re-connect to core.
375 * @param cls the 'struct GNUNET_CORE_Handle'
376 * @param tc task context
379 reconnect_task (void *cls,
380 const struct GNUNET_SCHEDULER_TaskContext *tc)
382 struct GNUNET_CORE_Handle *h = cls;
384 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Connecting to CORE service after delay\n");
394 * Notify clients about disconnect and free
395 * the entry for connected peer.
397 * @param cls the 'struct GNUNET_CORE_Handle*'
398 * @param key the peer identity (not used)
399 * @param value the 'struct PeerRecord' to free.
400 * @return GNUNET_YES (continue)
403 disconnect_and_free_peer_entry (void *cls,
404 const GNUNET_HashCode *key,
407 static struct GNUNET_BANDWIDTH_Value32NBO zero;
408 struct GNUNET_CORE_Handle *h = cls;
409 struct GNUNET_CORE_TransmitHandle *th;
410 struct PeerRecord *pr = value;
411 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
413 while (NULL != (th = pr->pending_head))
415 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
420 th->get_message (th->get_message_cls,
424 if (NULL != (pcic = pr->pcic))
427 GNUNET_free_non_null (pr->pcic_ptr);
433 GNUNET_TIME_UNIT_FOREVER_REL,
436 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
438 GNUNET_SCHEDULER_cancel (pr->timeout_task);
439 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
441 GNUNET_assert (pr->queue_size == 0);
442 if ( (pr->prev != NULL) ||
443 (pr->next != NULL) ||
444 (h->ready_peer_head == pr) )
445 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
448 if (h->disconnects != NULL)
449 h->disconnects (h->cls,
451 GNUNET_assert (GNUNET_YES ==
452 GNUNET_CONTAINER_multihashmap_remove (h->peers,
455 GNUNET_assert (pr->pending_head == NULL);
456 GNUNET_assert (pr->pending_tail == NULL);
457 GNUNET_assert (pr->ch = h);
458 GNUNET_assert (pr->queue_size == 0);
459 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
466 * Close down any existing connection to the CORE service and
467 * try re-establishing it later.
469 * @param h our handle
472 reconnect_later (struct GNUNET_CORE_Handle *h)
474 struct ControlMessage *cm;
475 struct PeerRecord *pr;
477 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
478 if (h->client != NULL)
480 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
483 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
484 &disconnect_and_free_peer_entry,
487 while (NULL != (pr = h->ready_peer_head))
488 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
491 h->currently_down = GNUNET_YES;
492 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
495 while (NULL != (cm = h->control_pending_head))
497 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
498 h->control_pending_tail,
502 if (cm->cont != NULL)
503 cm->cont (cm->cont_cls, GNUNET_NO);
506 GNUNET_assert (h->control_pending_head == NULL);
507 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
509 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
514 * Check the list of pending requests, send the next
517 * @param h core handle
518 * @param ignore_currently_down transmit message even if not initialized?
521 trigger_next_request (struct GNUNET_CORE_Handle *h,
522 int ignore_currently_down);
526 * The given request hit its timeout. Remove from the
527 * doubly-linked list and call the respective continuation.
529 * @param cls the transmit handle of the request that timed out
530 * @param tc context, can be NULL (!)
533 transmission_timeout (void *cls,
534 const struct GNUNET_SCHEDULER_TaskContext *tc);
538 * Send a control message to the peer asking for transmission
539 * of the message in the given peer record.
541 * @param pr peer to request transmission to
544 request_next_transmission (struct PeerRecord *pr)
546 struct GNUNET_CORE_Handle *h = pr->ch;
547 struct ControlMessage *cm;
548 struct SendMessageRequest *smr;
549 struct GNUNET_CORE_TransmitHandle *th;
551 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
553 GNUNET_SCHEDULER_cancel (pr->timeout_task);
554 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
556 if (NULL == (th = pr->pending_head))
558 trigger_next_request (h, GNUNET_NO);
562 return; /* already done */
563 GNUNET_assert (pr->prev == NULL);
564 GNUNET_assert (pr->next == NULL);
565 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
566 &transmission_timeout,
568 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
569 sizeof (struct SendMessageRequest));
572 smr = (struct SendMessageRequest*) &cm[1];
573 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
574 smr->header.size = htons (sizeof (struct SendMessageRequest));
575 smr->priority = htonl (th->priority);
576 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
577 smr->peer = pr->peer;
578 smr->queue_size = htonl (pr->queue_size);
579 smr->size = htons (th->msize);
580 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
581 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
582 h->control_pending_tail,
585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
586 "Adding SEND REQUEST for peer `%s' to message queue\n",
587 GNUNET_i2s (&pr->peer));
589 trigger_next_request (h, GNUNET_NO);
594 * The given request hit its timeout. Remove from the
595 * doubly-linked list and call the respective continuation.
597 * @param cls the transmit handle of the request that timed out
598 * @param tc context, can be NULL (!)
601 transmission_timeout (void *cls,
602 const struct GNUNET_SCHEDULER_TaskContext *tc)
604 struct PeerRecord *pr = cls;
605 struct GNUNET_CORE_Handle *h = pr->ch;
606 struct GNUNET_CORE_TransmitHandle *th;
608 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
609 th = pr->pending_head;
610 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
614 if ( (pr->prev != NULL) ||
615 (pr->next != NULL) ||
616 (pr == h->ready_peer_head) )
618 /* the request that was 'approved' by core was
619 canceled before it could be transmitted; remove
620 us from the 'ready' list */
621 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
627 "Signalling timeout of request for transmission to CORE service\n");
629 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
630 request_next_transmission (pr);
635 * Transmit the next message to the core service.
638 transmit_message (void *cls,
642 struct GNUNET_CORE_Handle *h = cls;
643 struct ControlMessage *cm;
644 struct GNUNET_CORE_TransmitHandle *th;
645 struct PeerRecord *pr;
646 struct SendMessage *sm;
647 const struct GNUNET_MessageHeader *hdr;
651 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Transmission failed, initiating reconnect\n");
662 /* first check for control messages */
663 if (NULL != (cm = h->control_pending_head))
665 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
666 msize = ntohs (hdr->size);
669 trigger_next_request (h, GNUNET_NO);
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Transmitting control message with %u bytes of type %u to core.\n",
675 (unsigned int) msize,
676 (unsigned int) ntohs (hdr->type));
678 memcpy (buf, hdr, msize);
679 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
680 h->control_pending_tail,
684 if (NULL != cm->cont)
685 cm->cont (cm->cont_cls, GNUNET_OK);
687 trigger_next_request (h, GNUNET_NO);
690 /* now check for 'ready' P2P messages */
691 if (NULL != (pr = h->ready_peer_head))
693 GNUNET_assert (pr->pending_head != NULL);
694 th = pr->pending_head;
695 if (size < th->msize + sizeof (struct SendMessage))
697 trigger_next_request (h, GNUNET_NO);
700 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
703 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
707 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
709 GNUNET_SCHEDULER_cancel (pr->timeout_task);
710 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714 "Transmitting SEND request to `%s' with %u bytes.\n",
715 GNUNET_i2s (&pr->peer),
716 (unsigned int) th->msize);
718 sm = (struct SendMessage *) buf;
719 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
720 sm->priority = htonl (th->priority);
721 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
723 sm->cork = htonl ((uint32_t) th->cork);
724 sm->reserved = htonl (0);
725 ret = th->get_message (th->get_message_cls,
726 size - sizeof (struct SendMessage),
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
731 "Transmitting SEND request to `%s' yielded %u bytes.\n",
732 GNUNET_i2s (&pr->peer),
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Size of clients message to peer %s is 0!\n",
740 GNUNET_i2s(&pr->peer));
742 /* client decided to send nothing! */
743 request_next_transmission (pr);
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749 "Produced SEND message to core with %u bytes payload\n",
752 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
753 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
756 request_next_transmission (pr);
760 ret += sizeof (struct SendMessage);
761 sm->header.size = htons (ret);
762 GNUNET_assert (ret <= size);
764 request_next_transmission (pr);
772 * Check the list of pending requests, send the next
775 * @param h core handle
776 * @param ignore_currently_down transmit message even if not initialized?
779 trigger_next_request (struct GNUNET_CORE_Handle *h,
780 int ignore_currently_down)
784 if ( (GNUNET_YES == h->currently_down) &&
785 (ignore_currently_down == GNUNET_NO) )
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Core connection down, not processing queue\n");
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Request pending, not processing queue\n");
801 if (h->control_pending_head != NULL)
802 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
803 else if (h->ready_peer_head != NULL)
804 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Request queue empty, not processing queue\n");
811 return; /* no pending message */
813 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
815 GNUNET_TIME_UNIT_FOREVER_REL,
817 &transmit_message, h);
822 * Handler for notification messages received from the core.
824 * @param cls our "struct GNUNET_CORE_Handle"
825 * @param msg the message received from the core service
828 main_notify_handler (void *cls,
829 const struct GNUNET_MessageHeader *msg)
831 struct GNUNET_CORE_Handle *h = cls;
832 const struct InitReplyMessage *m;
833 const struct ConnectNotifyMessage *cnm;
834 const struct DisconnectNotifyMessage *dnm;
835 const struct NotifyTrafficMessage *ntm;
836 const struct GNUNET_MessageHeader *em;
837 const struct ConfigurationInfoMessage *cim;
838 const struct PeerStatusNotifyMessage *psnm;
839 const struct SendMessageReady *smr;
840 const struct GNUNET_CORE_MessageHandler *mh;
841 GNUNET_CORE_StartupCallback init;
842 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
843 struct PeerRecord *pr;
844 struct GNUNET_CORE_TransmitHandle *th;
853 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
855 ("Client was disconnected from core service, trying to reconnect.\n"));
859 msize = ntohs (msg->size);
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Processing message of type %u and size %u from core service\n",
863 ntohs (msg->type), msize);
865 switch (ntohs (msg->type))
867 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
868 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
874 m = (const struct InitReplyMessage *) msg;
875 GNUNET_break (0 == ntohl (m->reserved));
876 /* start our message processing loop */
877 if (GNUNET_YES == h->currently_down)
879 h->currently_down = GNUNET_NO;
880 trigger_next_request (h, GNUNET_NO);
882 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
883 GNUNET_CRYPTO_hash (&m->publicKey,
885 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
887 if (NULL != (init = h->init))
889 /* mark so we don't call init on reconnect */
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893 "Connected to core service of peer `%s'.\n",
894 GNUNET_i2s (&h->me));
896 init (h->cls, h, &h->me, &m->publicKey);
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "Successfully reconnected to core service.\n");
905 /* fake 'connect to self' */
906 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
908 GNUNET_assert (pr == NULL);
909 pr = GNUNET_malloc (sizeof (struct PeerRecord));
912 GNUNET_assert (GNUNET_YES ==
913 GNUNET_CONTAINER_multihashmap_put (h->peers,
916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
917 if (NULL != h->connects)
922 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
923 if (msize < sizeof (struct ConnectNotifyMessage))
929 cnm = (const struct ConnectNotifyMessage *) msg;
930 ats_count = ntohl (cnm->ats_count);
931 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
932 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "Received notification about connection from `%s'.\n",
941 GNUNET_i2s (&cnm->peer));
943 if (0 == memcmp (&h->me,
945 sizeof (struct GNUNET_PeerIdentity)))
947 /* connect to self!? */
951 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
952 &cnm->peer.hashPubKey);
959 pr = GNUNET_malloc (sizeof (struct PeerRecord));
960 pr->peer = cnm->peer;
962 GNUNET_assert (GNUNET_YES ==
963 GNUNET_CONTAINER_multihashmap_put (h->peers,
964 &cnm->peer.hashPubKey,
966 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
967 if (NULL != h->connects)
972 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
973 if (msize != sizeof (struct DisconnectNotifyMessage))
979 dnm = (const struct DisconnectNotifyMessage *) msg;
980 if (0 == memcmp (&h->me,
982 sizeof (struct GNUNET_PeerIdentity)))
984 /* connection to self!? */
988 GNUNET_break (0 == ntohl (dnm->reserved));
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Received notification about disconnect from `%s'.\n",
992 GNUNET_i2s (&dnm->peer));
994 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
995 &dnm->peer.hashPubKey);
1002 trigger = ( (pr->prev != NULL) ||
1003 (pr->next != NULL) ||
1004 (h->ready_peer_head == pr) );
1005 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
1007 trigger_next_request (h, GNUNET_NO);
1009 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
1010 if (NULL == h->status_events)
1015 if (msize < sizeof (struct PeerStatusNotifyMessage))
1018 reconnect_later (h);
1021 psnm = (const struct PeerStatusNotifyMessage *) msg;
1022 if (0 == memcmp (&h->me,
1024 sizeof (struct GNUNET_PeerIdentity)))
1030 ats_count = ntohl (psnm->ats_count);
1031 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1032 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1035 reconnect_later (h);
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "Received notification about status change by `%s'.\n",
1041 GNUNET_i2s (&psnm->peer));
1043 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1044 &psnm->peer.hashPubKey);
1048 reconnect_later (h);
1051 h->status_events (h->cls,
1054 psnm->bandwidth_out,
1055 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1058 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1059 if (msize < sizeof (struct NotifyTrafficMessage))
1062 reconnect_later (h);
1065 ntm = (const struct NotifyTrafficMessage *) msg;
1067 ats_count = ntohl (ntm->ats_count);
1068 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1069 + sizeof (struct GNUNET_MessageHeader)) ||
1070 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1073 reconnect_later (h);
1076 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Received message of type %u and size %u from peer `%4s'\n",
1082 GNUNET_i2s (&ntm->peer));
1084 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1085 &ntm->peer.hashPubKey);
1089 reconnect_later (h);
1092 if ((GNUNET_NO == h->inbound_hdr_only) &&
1093 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1094 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1097 reconnect_later (h);
1100 et = ntohs (em->type);
1101 for (hpos = 0; hpos < h->hcnt; hpos++)
1103 mh = &h->handlers[hpos];
1106 if ((mh->expected_size != ntohs (em->size)) &&
1107 (mh->expected_size != 0))
1113 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1116 /* error in processing, do not process other messages! */
1120 if (NULL != h->inbound_notify)
1121 h->inbound_notify (h->cls, &ntm->peer, em,
1124 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1125 if (msize < sizeof (struct NotifyTrafficMessage))
1128 reconnect_later (h);
1131 ntm = (const struct NotifyTrafficMessage *) msg;
1132 if (0 == memcmp (&h->me,
1134 sizeof (struct GNUNET_PeerIdentity)))
1140 ats_count = ntohl (ntm->ats_count);
1141 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1142 + sizeof (struct GNUNET_MessageHeader)) ||
1143 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1146 reconnect_later (h);
1149 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1150 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1151 &ntm->peer.hashPubKey);
1155 reconnect_later (h);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "Received notification about transmission to `%s'.\n",
1161 GNUNET_i2s (&ntm->peer));
1163 if ((GNUNET_NO == h->outbound_hdr_only) &&
1164 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1165 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1168 reconnect_later (h);
1171 if (NULL == h->outbound_notify)
1176 h->outbound_notify (h->cls, &ntm->peer, em,
1179 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1180 if (msize != sizeof (struct SendMessageReady))
1183 reconnect_later (h);
1186 smr = (const struct SendMessageReady *) msg;
1187 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1188 &smr->peer.hashPubKey);
1192 reconnect_later (h);
1196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1197 "Received notification about transmission readiness to `%s'.\n",
1198 GNUNET_i2s (&smr->peer));
1200 if (pr->pending_head == NULL)
1202 /* request must have been cancelled between the original request
1203 and the response from core, ignore core's readiness */
1207 th = pr->pending_head;
1208 if (ntohs (smr->smr_id) != th->smr_id)
1210 /* READY message is for expired or cancelled message,
1211 ignore! (we should have already sent another request) */
1214 if ( (pr->prev != NULL) ||
1215 (pr->next != NULL) ||
1216 (h->ready_peer_head == pr) )
1218 /* we should not already be on the ready list... */
1220 reconnect_later (h);
1223 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1226 trigger_next_request (h, GNUNET_NO);
1228 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1229 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1232 reconnect_later (h);
1235 cim = (const struct ConfigurationInfoMessage*) msg;
1236 if (0 == memcmp (&h->me,
1238 sizeof (struct GNUNET_PeerIdentity)))
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Received notification about configuration update for `%s' with RIM %u.\n",
1247 GNUNET_i2s (&cim->peer),
1248 (unsigned int) ntohl (cim->rim_id));
1250 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1251 &cim->peer.hashPubKey);
1255 reconnect_later (h);
1258 if (pr->rim_id != ntohl (cim->rim_id))
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Reservation ID mismatch in notification...\n");
1268 GNUNET_free_non_null (pr->pcic_ptr);
1269 pr->pcic_ptr = NULL;
1274 ntohl (cim->reserved_amount),
1275 GNUNET_TIME_relative_ntoh (cim->reserve_delay),
1276 GNUNET_ntohll (cim->preference));
1279 reconnect_later (h);
1282 GNUNET_CLIENT_receive (h->client,
1283 &main_notify_handler, h,
1284 GNUNET_TIME_UNIT_FOREVER_REL);
1289 * Task executed once we are done transmitting the INIT message.
1290 * Starts our 'receive' loop.
1292 * @param cls the 'struct GNUNET_CORE_Handle'
1293 * @param success were we successful
1296 init_done_task (void *cls,
1299 struct GNUNET_CORE_Handle *h = cls;
1301 if (success == GNUNET_SYSERR)
1302 return; /* shutdown */
1303 if (success == GNUNET_NO)
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Failed to exchange INIT with core, retrying\n");
1309 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1310 reconnect_later (h);
1313 GNUNET_CLIENT_receive (h->client,
1314 &main_notify_handler,
1316 GNUNET_TIME_UNIT_FOREVER_REL);
1321 * Our current client connection went down. Clean it up
1322 * and try to reconnect!
1324 * @param h our handle to the core service
1327 reconnect (struct GNUNET_CORE_Handle *h)
1329 struct ControlMessage *cm;
1330 struct InitMessage *init;
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Reconnecting to CORE service\n");
1340 GNUNET_assert (h->client == NULL);
1341 GNUNET_assert (h->currently_down == GNUNET_YES);
1342 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1343 if (h->client == NULL)
1345 reconnect_later (h);
1348 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1349 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1351 cm->cont = &init_done_task;
1353 init = (struct InitMessage*) &cm[1];
1354 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1355 init->header.size = htons (msize);
1356 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1357 if (h->status_events != NULL)
1358 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1359 if (h->inbound_notify != NULL)
1361 if (h->inbound_hdr_only)
1362 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1364 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1366 if (h->outbound_notify != NULL)
1368 if (h->outbound_hdr_only)
1369 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1371 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1373 init->options = htonl (opt);
1374 ts = (uint16_t *) &init[1];
1375 for (hpos = 0; hpos < h->hcnt; hpos++)
1376 ts[hpos] = htons (h->handlers[hpos].type);
1377 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1378 h->control_pending_tail,
1380 trigger_next_request (h, GNUNET_YES);
1386 * Connect to the core service. Note that the connection may
1387 * complete (or fail) asynchronously.
1389 * @param cfg configuration to use
1390 * @param queue_size size of the per-peer message queue
1391 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1392 * @param init callback to call on timeout or once we have successfully
1393 * connected to the core service; note that timeout is only meaningful if init is not NULL
1394 * @param connects function to call on peer connect, can be NULL
1395 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1396 * @param status_events function to call on changes to peer connection status, can be NULL
1397 * @param inbound_notify function to call for all inbound messages, can be NULL
1398 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1399 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1400 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1401 * @param outbound_notify function to call for all outbound messages, can be NULL
1402 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1403 * GNUNET_MessageHeader and hence we do not need to give it the full message
1404 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1405 * @param handlers callbacks for messages we care about, NULL-terminated
1406 * @return handle to the core service (only useful for disconnect until 'init' is called);
1407 * NULL on error (in this case, init is never called)
1409 struct GNUNET_CORE_Handle *
1410 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1411 unsigned int queue_size,
1413 GNUNET_CORE_StartupCallback init,
1414 GNUNET_CORE_ConnectEventHandler connects,
1415 GNUNET_CORE_DisconnectEventHandler disconnects,
1416 GNUNET_CORE_PeerStatusEventHandler status_events,
1417 GNUNET_CORE_MessageCallback inbound_notify,
1418 int inbound_hdr_only,
1419 GNUNET_CORE_MessageCallback outbound_notify,
1420 int outbound_hdr_only,
1421 const struct GNUNET_CORE_MessageHandler *handlers)
1423 struct GNUNET_CORE_Handle *h;
1425 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1427 h->queue_size = queue_size;
1430 h->connects = connects;
1431 h->disconnects = disconnects;
1432 h->status_events = status_events;
1433 h->inbound_notify = inbound_notify;
1434 h->outbound_notify = outbound_notify;
1435 h->inbound_hdr_only = inbound_hdr_only;
1436 h->outbound_hdr_only = outbound_hdr_only;
1437 h->handlers = handlers;
1439 h->currently_down = GNUNET_YES;
1440 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1441 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1442 while (handlers[h->hcnt].callback != NULL)
1444 GNUNET_assert (h->hcnt <
1445 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1446 sizeof (struct InitMessage)) / sizeof (uint16_t));
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 "Connecting to CORE service\n");
1457 * Disconnect from the core service. This function can only
1458 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1459 * requests have been explicitly canceled.
1461 * @param handle connection to core to disconnect
1464 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1466 struct ControlMessage *cm;
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Disconnecting from CORE service\n");
1472 if (handle->cth != NULL)
1474 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1477 if (handle->client != NULL)
1479 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1480 handle->client = NULL;
1482 while (NULL != (cm = handle->control_pending_head))
1484 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1485 handle->control_pending_tail,
1489 if (cm->cont != NULL)
1490 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1493 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1495 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1496 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1498 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1499 &disconnect_and_free_peer_entry,
1501 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1502 GNUNET_break (handle->ready_peer_head == NULL);
1503 GNUNET_free (handle);
1508 * Ask the core to call "notify" once it is ready to transmit the
1509 * given number of bytes to the specified "target". Must only be
1510 * called after a connection to the respective peer has been
1511 * established (and the client has been informed about this).
1513 * @param handle connection to core service
1514 * @param cork is corking allowed for this transmission?
1515 * @param priority how important is the message?
1516 * @param maxdelay how long can the message wait?
1517 * @param target who should receive the message,
1518 * use NULL for this peer (loopback)
1519 * @param notify_size how many bytes of buffer space does notify want?
1520 * @param notify function to call when buffer space is available
1521 * @param notify_cls closure for notify
1522 * @return non-NULL if the notify callback was queued,
1523 * NULL if we can not even queue the request (insufficient
1524 * memory); if NULL is returned, "notify" will NOT be called.
1526 struct GNUNET_CORE_TransmitHandle *
1527 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1530 struct GNUNET_TIME_Relative maxdelay,
1531 const struct GNUNET_PeerIdentity *target,
1533 GNUNET_CONNECTION_TransmitReadyNotify notify,
1536 struct PeerRecord *pr;
1537 struct GNUNET_CORE_TransmitHandle *th;
1538 struct GNUNET_CORE_TransmitHandle *pos;
1539 struct GNUNET_CORE_TransmitHandle *prev;
1540 struct GNUNET_CORE_TransmitHandle *minp;
1542 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1543 &target->hashPubKey);
1546 /* attempt to send to peer that is not connected */
1547 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1548 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1549 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1553 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1554 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1555 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1557 GNUNET_assert(NULL != notify);
1558 th->get_message = notify;
1559 th->get_message_cls = notify_cls;
1560 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1561 th->priority = priority;
1562 th->msize = notify_size;
1564 /* bound queue size */
1565 if (pr->queue_size == handle->queue_size)
1567 /* find lowest-priority entry, but skip the head of the list */
1568 minp = pr->pending_head->next;
1570 while (prev != NULL)
1572 if (prev->priority < minp->priority)
1578 GNUNET_break (handle->queue_size != 0);
1579 GNUNET_break (pr->queue_size == 1);
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "Dropping transmission request: cannot drop queue head and limit is one\n");
1587 if (priority <= minp->priority)
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Dropping transmission request: priority too low\n");
1594 return NULL; /* priority too low */
1596 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1601 minp->get_message (minp->get_message_cls,
1606 /* Order entries by deadline, but SKIP 'HEAD' if
1607 we're in the 'ready_peer_*' DLL */
1608 pos = pr->pending_head;
1609 if ( (pr->prev != NULL) ||
1610 (pr->next != NULL) ||
1611 (pr == handle->ready_peer_head) )
1613 GNUNET_assert (pos != NULL);
1614 pos = pos->next; /* skip head */
1617 /* insertion sort */
1619 while ( (pos != NULL) &&
1620 (pos->timeout.abs_value < th->timeout.abs_value) )
1625 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1630 /* was the request queue previously empty? */
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1633 "Transmission request added to queue\n");
1635 if (pr->pending_head == th)
1636 request_next_transmission (pr);
1642 * Cancel the specified transmission-ready notification.
1644 * @param th handle that was returned by "notify_transmit_ready".
1647 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1650 struct PeerRecord *pr = th->peer;
1651 struct GNUNET_CORE_Handle *h = pr->ch;
1654 was_head = (pr->pending_head == th);
1655 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1661 /* we're currently in the control queue, remove */
1662 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1663 h->control_pending_tail,
1665 GNUNET_free (th->cm);
1670 if ( (pr->prev != NULL) ||
1671 (pr->next != NULL) ||
1672 (pr == h->ready_peer_head) )
1674 /* the request that was 'approved' by core was
1675 canceled before it could be transmitted; remove
1676 us from the 'ready' list */
1677 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1681 request_next_transmission (pr);
1686 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1689 * Handle for a request to the core to connect to
1690 * a particular peer. Can be used to cancel the request
1691 * (before the 'cont'inuation is called).
1693 struct GNUNET_CORE_PeerRequestHandle
1697 * Link to control message.
1699 struct ControlMessage *cm;
1704 struct GNUNET_CORE_Handle *h;
1707 * Continuation to run when done.
1709 GNUNET_CORE_ControlContinuation cont;
1712 * Closure for 'cont'.
1720 * Continuation called when the control message was transmitted.
1721 * Calls the original continuation and frees the remaining
1724 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1725 * @param success was the request transmitted?
1728 peer_request_connect_cont (void *cls,
1731 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1733 if (ret->cont != NULL)
1734 ret->cont (ret->cont_cls, success);
1740 * Request that the core should try to connect to a particular peer.
1741 * Once the request has been transmitted to the core, the continuation
1742 * function will be called. Note that this does NOT mean that a
1743 * connection was successfully established -- it only means that the
1744 * core will now try. Successful establishment of the connection
1745 * will be signalled to the 'connects' callback argument of
1746 * 'GNUNET_CORE_connect' only. If the core service does not respond
1747 * to our connection attempt within the given time frame, 'cont' will
1748 * be called with the TIMEOUT reason code.
1750 * @param h core handle
1751 * @param timeout how long to try to talk to core
1752 * @param peer who should we connect to
1753 * @param cont function to call once the request has been completed (or timed out)
1754 * @param cont_cls closure for cont
1756 * @return NULL on error or already connected,
1757 * otherwise handle for cancellation
1759 struct GNUNET_CORE_PeerRequestHandle *
1760 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1761 struct GNUNET_TIME_Relative timeout,
1762 const struct GNUNET_PeerIdentity * peer,
1763 GNUNET_CORE_ControlContinuation cont,
1766 struct GNUNET_CORE_PeerRequestHandle *ret;
1767 struct ControlMessage *cm;
1768 struct ConnectMessage *msg;
1770 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1774 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1775 "Peers are already connected!\n");
1780 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1781 sizeof (struct ConnectMessage));
1782 msg = (struct ConnectMessage*) &cm[1];
1783 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1784 msg->header.size = htons (sizeof (struct ConnectMessage));
1785 msg->reserved = htonl (0);
1786 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1788 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1789 h->control_pending_tail,
1791 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1795 ret->cont_cls = cont_cls;
1796 cm->cont = &peer_request_connect_cont;
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1800 "Queueing REQUEST_CONNECT request\n");
1802 trigger_next_request (h, GNUNET_NO);
1808 * Cancel a pending request to connect to a particular peer. Must not
1809 * be called after the 'cont' function was invoked.
1811 * @param req request handle that was returned for the original request
1814 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1816 struct GNUNET_CORE_Handle *h = req->h;
1817 struct ControlMessage *cm = req->cm;
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1821 "A CHANGE PREFERENCE request was cancelled!\n");
1823 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1824 h->control_pending_tail,
1831 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1834 struct GNUNET_CORE_InformationRequestContext
1838 * Our connection to the service.
1840 struct GNUNET_CORE_Handle *h;
1843 * Link to control message, NULL if CM was sent.
1845 struct ControlMessage *cm;
1848 * Link to peer record.
1850 struct PeerRecord *pr;
1855 * CM was sent, remove link so we don't double-free.
1857 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1858 * @param success were we successful?
1861 change_preference_send_continuation (void *cls,
1864 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1871 * Obtain statistics and/or change preferences for the given peer.
1873 * @param h core handle
1874 * @param peer identifies the peer
1875 * @param timeout after how long should we give up (and call "info" with NULL
1876 * for "peer" to signal an error)?
1877 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1878 * caller should set "bw_out" to "-1" to avoid changing
1879 * the current value; otherwise "bw_out" will be lowered to
1880 * the specified value; passing a pointer to "0" can be used to force
1881 * us to disconnect from the peer; "bw_out" might not increase
1882 * as specified since the upper bound is generally
1883 * determined by the other peer!
1884 * @param amount reserve N bytes for receiving, negative
1885 * amounts can be used to undo a (recent) reservation;
1886 * @param preference increase incoming traffic share preference by this amount;
1887 * in the absence of "amount" reservations, we use this
1888 * preference value to assign proportional bandwidth shares
1889 * to all connected peers
1890 * @param info function to call with the resulting configuration information
1891 * @param info_cls closure for info
1892 * @return NULL on error
1894 struct GNUNET_CORE_InformationRequestContext *
1895 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1896 const struct GNUNET_PeerIdentity *peer,
1897 struct GNUNET_TIME_Relative timeout,
1898 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1900 uint64_t preference,
1901 GNUNET_CORE_PeerConfigurationInfoCallback info,
1904 struct GNUNET_CORE_InformationRequestContext *irc;
1905 struct PeerRecord *pr;
1906 struct RequestInfoMessage *rim;
1907 struct ControlMessage *cm;
1909 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1913 /* attempt to change preference on peer that is not connected */
1917 if (pr->pcic != NULL)
1919 /* second change before first one is done */
1923 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1926 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1927 sizeof (struct RequestInfoMessage));
1928 cm->cont = &change_preference_send_continuation;
1931 rim = (struct RequestInfoMessage*) &cm[1];
1932 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1933 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1934 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1935 rim->limit_outbound = bw_out;
1936 rim->reserve_inbound = htonl (amount);
1937 rim->preference_change = GNUNET_htonll(preference);
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "Queueing CHANGE PREFERENCE request for peer `%s' with RIM %u\n",
1943 (unsigned int) pr->rim_id);
1945 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1946 h->control_pending_tail,
1949 pr->pcic_cls = info_cls;
1950 pr->pcic_ptr = irc; /* for free'ing irc */
1951 trigger_next_request (h, GNUNET_NO);
1957 * Cancel request for getting information about a peer.
1958 * Note that an eventual change in preference, trust or bandwidth
1959 * assignment MAY have already been committed at the time,
1960 * so cancelling a request is NOT sure to undo the original
1961 * request. The original request may or may not still commit.
1962 * The only thing cancellation ensures is that the callback
1963 * from the original request will no longer be called.
1965 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1968 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1970 struct GNUNET_CORE_Handle *h = irc->h;
1971 struct PeerRecord *pr = irc->pr;
1973 GNUNET_assert (pr->pcic_ptr == irc);
1974 if (irc->cm != NULL)
1976 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1977 h->control_pending_tail,
1979 GNUNET_free (irc->cm);
1982 pr->pcic_cls = NULL;
1983 pr->pcic_ptr = NULL;
1988 /* end of core_api.c */