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_after (h->control_pending_head,
582 h->control_pending_tail,
583 h->control_pending_tail,
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Adding SEND REQUEST for peer `%s' to message queue\n",
588 GNUNET_i2s (&pr->peer));
590 trigger_next_request (h, GNUNET_NO);
595 * The given request hit its timeout. Remove from the
596 * doubly-linked list and call the respective continuation.
598 * @param cls the transmit handle of the request that timed out
599 * @param tc context, can be NULL (!)
602 transmission_timeout (void *cls,
603 const struct GNUNET_SCHEDULER_TaskContext *tc)
605 struct PeerRecord *pr = cls;
606 struct GNUNET_CORE_Handle *h = pr->ch;
607 struct GNUNET_CORE_TransmitHandle *th;
609 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
610 th = pr->pending_head;
611 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
615 if ( (pr->prev != NULL) ||
616 (pr->next != NULL) ||
617 (pr == h->ready_peer_head) )
619 /* the request that was 'approved' by core was
620 canceled before it could be transmitted; remove
621 us from the 'ready' list */
622 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Signalling timeout of request for transmission to CORE service\n");
630 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
631 request_next_transmission (pr);
636 * Transmit the next message to the core service.
639 transmit_message (void *cls,
643 struct GNUNET_CORE_Handle *h = cls;
644 struct ControlMessage *cm;
645 struct GNUNET_CORE_TransmitHandle *th;
646 struct PeerRecord *pr;
647 struct SendMessage *sm;
648 const struct GNUNET_MessageHeader *hdr;
652 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Transmission failed, initiating reconnect\n");
663 /* first check for control messages */
664 if (NULL != (cm = h->control_pending_head))
666 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
667 msize = ntohs (hdr->size);
670 trigger_next_request (h, GNUNET_NO);
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Transmitting control message with %u bytes of type %u to core.\n",
676 (unsigned int) msize,
677 (unsigned int) ntohs (hdr->type));
679 memcpy (buf, hdr, msize);
680 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
681 h->control_pending_tail,
685 if (NULL != cm->cont)
686 cm->cont (cm->cont_cls, GNUNET_OK);
688 trigger_next_request (h, GNUNET_NO);
691 /* now check for 'ready' P2P messages */
692 if (NULL != (pr = h->ready_peer_head))
694 GNUNET_assert (pr->pending_head != NULL);
695 th = pr->pending_head;
696 if (size < th->msize + sizeof (struct SendMessage))
698 trigger_next_request (h, GNUNET_NO);
701 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
704 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
708 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
710 GNUNET_SCHEDULER_cancel (pr->timeout_task);
711 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
715 "Transmitting SEND request to `%s' with %u bytes.\n",
716 GNUNET_i2s (&pr->peer),
717 (unsigned int) th->msize);
719 sm = (struct SendMessage *) buf;
720 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
721 sm->priority = htonl (th->priority);
722 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
724 sm->cork = htonl ((uint32_t) th->cork);
725 sm->reserved = htonl (0);
726 ret = th->get_message (th->get_message_cls,
727 size - sizeof (struct SendMessage),
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "Size of clients message to peer %s is 0!\n",
735 GNUNET_i2s(&pr->peer));
737 /* client decided to send nothing! */
738 request_next_transmission (pr);
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Produced SEND message to core with %u bytes payload\n",
746 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
747 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
750 request_next_transmission (pr);
753 ret += sizeof (struct SendMessage);
754 sm->header.size = htons (ret);
755 GNUNET_assert (ret <= size);
757 request_next_transmission (pr);
765 * Check the list of pending requests, send the next
768 * @param h core handle
769 * @param ignore_currently_down transmit message even if not initialized?
772 trigger_next_request (struct GNUNET_CORE_Handle *h,
773 int ignore_currently_down)
777 if ( (GNUNET_YES == h->currently_down) &&
778 (ignore_currently_down == GNUNET_NO) )
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
782 "Core connection down, not processing queue\n");
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Request pending, not processing queue\n");
794 if (h->control_pending_head != NULL)
795 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
796 else if (h->ready_peer_head != NULL)
797 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Request queue empty, not processing queue\n");
804 return; /* no pending message */
806 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
808 GNUNET_TIME_UNIT_FOREVER_REL,
810 &transmit_message, h);
815 * Handler for notification messages received from the core.
817 * @param cls our "struct GNUNET_CORE_Handle"
818 * @param msg the message received from the core service
821 main_notify_handler (void *cls,
822 const struct GNUNET_MessageHeader *msg)
824 struct GNUNET_CORE_Handle *h = cls;
825 const struct InitReplyMessage *m;
826 const struct ConnectNotifyMessage *cnm;
827 const struct DisconnectNotifyMessage *dnm;
828 const struct NotifyTrafficMessage *ntm;
829 const struct GNUNET_MessageHeader *em;
830 const struct ConfigurationInfoMessage *cim;
831 const struct PeerStatusNotifyMessage *psnm;
832 const struct SendMessageReady *smr;
833 const struct GNUNET_CORE_MessageHandler *mh;
834 GNUNET_CORE_StartupCallback init;
835 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
836 struct PeerRecord *pr;
837 struct GNUNET_CORE_TransmitHandle *th;
846 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
848 ("Client was disconnected from core service, trying to reconnect.\n"));
852 msize = ntohs (msg->size);
854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
855 "Processing message of type %u and size %u from core service\n",
856 ntohs (msg->type), msize);
858 switch (ntohs (msg->type))
860 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
861 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
867 m = (const struct InitReplyMessage *) msg;
868 GNUNET_break (0 == ntohl (m->reserved));
869 /* start our message processing loop */
870 if (GNUNET_YES == h->currently_down)
872 h->currently_down = GNUNET_NO;
873 trigger_next_request (h, GNUNET_NO);
875 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
876 GNUNET_CRYPTO_hash (&m->publicKey,
878 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
880 if (NULL != (init = h->init))
882 /* mark so we don't call init on reconnect */
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886 "Connected to core service of peer `%s'.\n",
887 GNUNET_i2s (&h->me));
889 init (h->cls, h, &h->me, &m->publicKey);
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Successfully reconnected to core service.\n");
898 /* fake 'connect to self' */
899 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
901 GNUNET_assert (pr == NULL);
902 pr = GNUNET_malloc (sizeof (struct PeerRecord));
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_CONTAINER_multihashmap_put (h->peers,
909 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
910 if (NULL != h->connects)
915 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
916 if (msize < sizeof (struct ConnectNotifyMessage))
922 cnm = (const struct ConnectNotifyMessage *) msg;
923 ats_count = ntohl (cnm->ats_count);
924 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
925 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 "Received notification about connection from `%s'.\n",
934 GNUNET_i2s (&cnm->peer));
936 if (0 == memcmp (&h->me,
938 sizeof (struct GNUNET_PeerIdentity)))
940 /* connect to self!? */
944 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
945 &cnm->peer.hashPubKey);
952 pr = GNUNET_malloc (sizeof (struct PeerRecord));
953 pr->peer = cnm->peer;
955 GNUNET_assert (GNUNET_YES ==
956 GNUNET_CONTAINER_multihashmap_put (h->peers,
957 &cnm->peer.hashPubKey,
959 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
960 if (NULL != h->connects)
965 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
966 if (msize != sizeof (struct DisconnectNotifyMessage))
972 dnm = (const struct DisconnectNotifyMessage *) msg;
973 if (0 == memcmp (&h->me,
975 sizeof (struct GNUNET_PeerIdentity)))
977 /* connection to self!? */
981 GNUNET_break (0 == ntohl (dnm->reserved));
983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984 "Received notification about disconnect from `%s'.\n",
985 GNUNET_i2s (&dnm->peer));
987 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
988 &dnm->peer.hashPubKey);
995 trigger = ( (pr->prev != NULL) ||
996 (pr->next != NULL) ||
997 (h->ready_peer_head == pr) );
998 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
1000 trigger_next_request (h, GNUNET_NO);
1002 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
1003 if (NULL == h->status_events)
1008 if (msize < sizeof (struct PeerStatusNotifyMessage))
1011 reconnect_later (h);
1014 psnm = (const struct PeerStatusNotifyMessage *) msg;
1015 if (0 == memcmp (&h->me,
1017 sizeof (struct GNUNET_PeerIdentity)))
1023 ats_count = ntohl (psnm->ats_count);
1024 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1025 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1028 reconnect_later (h);
1032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1033 "Received notification about status change by `%s'.\n",
1034 GNUNET_i2s (&psnm->peer));
1036 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1037 &psnm->peer.hashPubKey);
1041 reconnect_later (h);
1044 h->status_events (h->cls,
1047 psnm->bandwidth_out,
1048 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1051 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1052 if (msize < sizeof (struct NotifyTrafficMessage))
1055 reconnect_later (h);
1058 ntm = (const struct NotifyTrafficMessage *) msg;
1060 ats_count = ntohl (ntm->ats_count);
1061 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1062 + sizeof (struct GNUNET_MessageHeader)) ||
1063 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1066 reconnect_later (h);
1069 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "Received message of type %u and size %u from peer `%4s'\n",
1075 GNUNET_i2s (&ntm->peer));
1077 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1078 &ntm->peer.hashPubKey);
1082 reconnect_later (h);
1085 if ((GNUNET_NO == h->inbound_hdr_only) &&
1086 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1087 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1090 reconnect_later (h);
1093 et = ntohs (em->type);
1094 for (hpos = 0; hpos < h->hcnt; hpos++)
1096 mh = &h->handlers[hpos];
1099 if ((mh->expected_size != ntohs (em->size)) &&
1100 (mh->expected_size != 0))
1106 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1109 /* error in processing, do not process other messages! */
1113 if (NULL != h->inbound_notify)
1114 h->inbound_notify (h->cls, &ntm->peer, em,
1117 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1118 if (msize < sizeof (struct NotifyTrafficMessage))
1121 reconnect_later (h);
1124 ntm = (const struct NotifyTrafficMessage *) msg;
1125 if (0 == memcmp (&h->me,
1127 sizeof (struct GNUNET_PeerIdentity)))
1133 ats_count = ntohl (ntm->ats_count);
1134 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1135 + sizeof (struct GNUNET_MessageHeader)) ||
1136 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1139 reconnect_later (h);
1142 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1143 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1144 &ntm->peer.hashPubKey);
1148 reconnect_later (h);
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Received notification about transmission to `%s'.\n",
1154 GNUNET_i2s (&ntm->peer));
1156 if ((GNUNET_NO == h->outbound_hdr_only) &&
1157 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1158 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1161 reconnect_later (h);
1164 if (NULL == h->outbound_notify)
1169 h->outbound_notify (h->cls, &ntm->peer, em,
1172 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1173 if (msize != sizeof (struct SendMessageReady))
1176 reconnect_later (h);
1179 smr = (const struct SendMessageReady *) msg;
1180 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1181 &smr->peer.hashPubKey);
1185 reconnect_later (h);
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Received notification about transmission readiness to `%s'.\n",
1191 GNUNET_i2s (&smr->peer));
1193 if (pr->pending_head == NULL)
1195 /* request must have been cancelled between the original request
1196 and the response from core, ignore core's readiness */
1200 th = pr->pending_head;
1201 if (ntohs (smr->smr_id) != th->smr_id)
1203 /* READY message is for expired or cancelled message,
1204 ignore! (we should have already sent another request) */
1207 if ( (pr->prev != NULL) ||
1208 (pr->next != NULL) ||
1209 (h->ready_peer_head == pr) )
1211 /* we should not already be on the ready list... */
1213 reconnect_later (h);
1216 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1219 trigger_next_request (h, GNUNET_NO);
1221 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1222 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1225 reconnect_later (h);
1228 cim = (const struct ConfigurationInfoMessage*) msg;
1229 if (0 == memcmp (&h->me,
1231 sizeof (struct GNUNET_PeerIdentity)))
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Received notification about configuration update for `%s'.\n",
1240 GNUNET_i2s (&cim->peer));
1242 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1243 &cim->peer.hashPubKey);
1247 reconnect_later (h);
1250 if (pr->rim_id != ntohl (cim->rim_id))
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Reservation ID mismatch in notification...\n");
1260 GNUNET_free_non_null (pr->pcic_ptr);
1261 pr->pcic_ptr = NULL;
1266 ntohl (cim->reserved_amount),
1267 GNUNET_TIME_relative_ntoh (cim->reserve_delay),
1268 GNUNET_ntohll (cim->preference));
1271 reconnect_later (h);
1274 GNUNET_CLIENT_receive (h->client,
1275 &main_notify_handler, h,
1276 GNUNET_TIME_UNIT_FOREVER_REL);
1281 * Task executed once we are done transmitting the INIT message.
1282 * Starts our 'receive' loop.
1284 * @param cls the 'struct GNUNET_CORE_Handle'
1285 * @param success were we successful
1288 init_done_task (void *cls,
1291 struct GNUNET_CORE_Handle *h = cls;
1293 if (success == GNUNET_SYSERR)
1294 return; /* shutdown */
1295 if (success == GNUNET_NO)
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "Failed to exchange INIT with core, retrying\n");
1301 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1302 reconnect_later (h);
1305 GNUNET_CLIENT_receive (h->client,
1306 &main_notify_handler,
1308 GNUNET_TIME_UNIT_FOREVER_REL);
1313 * Our current client connection went down. Clean it up
1314 * and try to reconnect!
1316 * @param h our handle to the core service
1319 reconnect (struct GNUNET_CORE_Handle *h)
1321 struct ControlMessage *cm;
1322 struct InitMessage *init;
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Reconnecting to CORE service\n");
1332 GNUNET_assert (h->client == NULL);
1333 GNUNET_assert (h->currently_down == GNUNET_YES);
1334 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1335 if (h->client == NULL)
1337 reconnect_later (h);
1340 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1341 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1343 cm->cont = &init_done_task;
1345 init = (struct InitMessage*) &cm[1];
1346 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1347 init->header.size = htons (msize);
1348 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1349 if (h->status_events != NULL)
1350 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1351 if (h->inbound_notify != NULL)
1353 if (h->inbound_hdr_only)
1354 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1356 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1358 if (h->outbound_notify != NULL)
1360 if (h->outbound_hdr_only)
1361 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1363 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1365 init->options = htonl (opt);
1366 ts = (uint16_t *) &init[1];
1367 for (hpos = 0; hpos < h->hcnt; hpos++)
1368 ts[hpos] = htons (h->handlers[hpos].type);
1369 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1370 h->control_pending_tail,
1372 trigger_next_request (h, GNUNET_YES);
1378 * Connect to the core service. Note that the connection may
1379 * complete (or fail) asynchronously.
1381 * @param cfg configuration to use
1382 * @param queue_size size of the per-peer message queue
1383 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1384 * @param init callback to call on timeout or once we have successfully
1385 * connected to the core service; note that timeout is only meaningful if init is not NULL
1386 * @param connects function to call on peer connect, can be NULL
1387 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1388 * @param status_events function to call on changes to peer connection status, can be NULL
1389 * @param inbound_notify function to call for all inbound messages, can be NULL
1390 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1391 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1392 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1393 * @param outbound_notify function to call for all outbound messages, can be NULL
1394 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1395 * GNUNET_MessageHeader and hence we do not need to give it the full message
1396 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1397 * @param handlers callbacks for messages we care about, NULL-terminated
1398 * @return handle to the core service (only useful for disconnect until 'init' is called);
1399 * NULL on error (in this case, init is never called)
1401 struct GNUNET_CORE_Handle *
1402 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1403 unsigned int queue_size,
1405 GNUNET_CORE_StartupCallback init,
1406 GNUNET_CORE_ConnectEventHandler connects,
1407 GNUNET_CORE_DisconnectEventHandler disconnects,
1408 GNUNET_CORE_PeerStatusEventHandler status_events,
1409 GNUNET_CORE_MessageCallback inbound_notify,
1410 int inbound_hdr_only,
1411 GNUNET_CORE_MessageCallback outbound_notify,
1412 int outbound_hdr_only,
1413 const struct GNUNET_CORE_MessageHandler *handlers)
1415 struct GNUNET_CORE_Handle *h;
1417 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1419 h->queue_size = queue_size;
1422 h->connects = connects;
1423 h->disconnects = disconnects;
1424 h->status_events = status_events;
1425 h->inbound_notify = inbound_notify;
1426 h->outbound_notify = outbound_notify;
1427 h->inbound_hdr_only = inbound_hdr_only;
1428 h->outbound_hdr_only = outbound_hdr_only;
1429 h->handlers = handlers;
1431 h->currently_down = GNUNET_YES;
1432 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1433 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1434 while (handlers[h->hcnt].callback != NULL)
1436 GNUNET_assert (h->hcnt <
1437 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1438 sizeof (struct InitMessage)) / sizeof (uint16_t));
1440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1441 "Connecting to CORE service\n");
1449 * Disconnect from the core service. This function can only
1450 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1451 * requests have been explicitly canceled.
1453 * @param handle connection to core to disconnect
1456 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1458 struct ControlMessage *cm;
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462 "Disconnecting from CORE service\n");
1464 if (handle->cth != NULL)
1466 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1469 if (handle->client != NULL)
1471 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1472 handle->client = NULL;
1474 while (NULL != (cm = handle->control_pending_head))
1476 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1477 handle->control_pending_tail,
1481 if (cm->cont != NULL)
1482 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1485 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1487 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1488 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1490 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1491 &disconnect_and_free_peer_entry,
1493 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1494 GNUNET_break (handle->ready_peer_head == NULL);
1495 GNUNET_free (handle);
1500 * Ask the core to call "notify" once it is ready to transmit the
1501 * given number of bytes to the specified "target". Must only be
1502 * called after a connection to the respective peer has been
1503 * established (and the client has been informed about this).
1505 * @param handle connection to core service
1506 * @param cork is corking allowed for this transmission?
1507 * @param priority how important is the message?
1508 * @param maxdelay how long can the message wait?
1509 * @param target who should receive the message,
1510 * use NULL for this peer (loopback)
1511 * @param notify_size how many bytes of buffer space does notify want?
1512 * @param notify function to call when buffer space is available
1513 * @param notify_cls closure for notify
1514 * @return non-NULL if the notify callback was queued,
1515 * NULL if we can not even queue the request (insufficient
1516 * memory); if NULL is returned, "notify" will NOT be called.
1518 struct GNUNET_CORE_TransmitHandle *
1519 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1522 struct GNUNET_TIME_Relative maxdelay,
1523 const struct GNUNET_PeerIdentity *target,
1525 GNUNET_CONNECTION_TransmitReadyNotify notify,
1528 struct PeerRecord *pr;
1529 struct GNUNET_CORE_TransmitHandle *th;
1530 struct GNUNET_CORE_TransmitHandle *pos;
1531 struct GNUNET_CORE_TransmitHandle *prev;
1532 struct GNUNET_CORE_TransmitHandle *minp;
1534 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1535 &target->hashPubKey);
1538 /* attempt to send to peer that is not connected */
1539 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1540 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1541 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1545 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1546 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1547 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1549 GNUNET_assert(NULL != notify);
1550 th->get_message = notify;
1551 th->get_message_cls = notify_cls;
1552 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1553 th->priority = priority;
1554 th->msize = notify_size;
1556 /* bound queue size */
1557 if (pr->queue_size == handle->queue_size)
1559 /* find lowest-priority entry, but skip the head of the list */
1560 minp = pr->pending_head->next;
1562 while (prev != NULL)
1564 if (prev->priority < minp->priority)
1570 GNUNET_break (handle->queue_size != 0);
1571 GNUNET_break (pr->queue_size == 1);
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 "Dropping transmission request: cannot drop queue head and limit is one\n");
1579 if (priority <= minp->priority)
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "Dropping transmission request: priority too low\n");
1585 return NULL; /* priority too low */
1587 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1592 minp->get_message (minp->get_message_cls,
1597 /* Order entries by deadline, but SKIP 'HEAD' if
1598 we're in the 'ready_peer_*' DLL */
1599 pos = pr->pending_head;
1600 if ( (pr->prev != NULL) ||
1601 (pr->next != NULL) ||
1602 (pr == handle->ready_peer_head) )
1604 GNUNET_assert (pos != NULL);
1605 pos = pos->next; /* skip head */
1608 /* insertion sort */
1610 while ( (pos != NULL) &&
1611 (pos->timeout.abs_value < th->timeout.abs_value) )
1616 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1621 /* was the request queue previously empty? */
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624 "Transmission request added to queue\n");
1626 if (pr->pending_head == th)
1627 request_next_transmission (pr);
1633 * Cancel the specified transmission-ready notification.
1635 * @param th handle that was returned by "notify_transmit_ready".
1638 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1641 struct PeerRecord *pr = th->peer;
1642 struct GNUNET_CORE_Handle *h = pr->ch;
1645 was_head = (pr->pending_head == th);
1646 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1652 /* we're currently in the control queue, remove */
1653 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1654 h->control_pending_tail,
1656 GNUNET_free (th->cm);
1661 if ( (pr->prev != NULL) ||
1662 (pr->next != NULL) ||
1663 (pr == h->ready_peer_head) )
1665 /* the request that was 'approved' by core was
1666 canceled before it could be transmitted; remove
1667 us from the 'ready' list */
1668 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1672 request_next_transmission (pr);
1677 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1680 * Handle for a request to the core to connect to
1681 * a particular peer. Can be used to cancel the request
1682 * (before the 'cont'inuation is called).
1684 struct GNUNET_CORE_PeerRequestHandle
1688 * Link to control message.
1690 struct ControlMessage *cm;
1695 struct GNUNET_CORE_Handle *h;
1698 * Continuation to run when done.
1700 GNUNET_CORE_ControlContinuation cont;
1703 * Closure for 'cont'.
1711 * Continuation called when the control message was transmitted.
1712 * Calls the original continuation and frees the remaining
1715 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1716 * @param success was the request transmitted?
1719 peer_request_connect_cont (void *cls,
1722 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1724 if (ret->cont != NULL)
1725 ret->cont (ret->cont_cls, success);
1731 * Request that the core should try to connect to a particular peer.
1732 * Once the request has been transmitted to the core, the continuation
1733 * function will be called. Note that this does NOT mean that a
1734 * connection was successfully established -- it only means that the
1735 * core will now try. Successful establishment of the connection
1736 * will be signalled to the 'connects' callback argument of
1737 * 'GNUNET_CORE_connect' only. If the core service does not respond
1738 * to our connection attempt within the given time frame, 'cont' will
1739 * be called with the TIMEOUT reason code.
1741 * @param h core handle
1742 * @param timeout how long to try to talk to core
1743 * @param peer who should we connect to
1744 * @param cont function to call once the request has been completed (or timed out)
1745 * @param cont_cls closure for cont
1747 * @return NULL on error or already connected,
1748 * otherwise handle for cancellation
1750 struct GNUNET_CORE_PeerRequestHandle *
1751 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1752 struct GNUNET_TIME_Relative timeout,
1753 const struct GNUNET_PeerIdentity * peer,
1754 GNUNET_CORE_ControlContinuation cont,
1757 struct GNUNET_CORE_PeerRequestHandle *ret;
1758 struct ControlMessage *cm;
1759 struct ConnectMessage *msg;
1761 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1765 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peers are already connected!\n");
1770 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1771 sizeof (struct ConnectMessage));
1772 msg = (struct ConnectMessage*) &cm[1];
1773 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1774 msg->header.size = htons (sizeof (struct ConnectMessage));
1775 msg->reserved = htonl (0);
1776 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1778 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1779 h->control_pending_tail,
1781 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1785 ret->cont_cls = cont_cls;
1786 cm->cont = &peer_request_connect_cont;
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "Queueing REQUEST_CONNECT request\n");
1792 if (h->control_pending_head == cm)
1793 trigger_next_request (h, GNUNET_NO);
1799 * Cancel a pending request to connect to a particular peer. Must not
1800 * be called after the 'cont' function was invoked.
1802 * @param req request handle that was returned for the original request
1805 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1807 struct GNUNET_CORE_Handle *h = req->h;
1808 struct ControlMessage *cm = req->cm;
1810 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1811 h->control_pending_tail,
1818 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1821 struct GNUNET_CORE_InformationRequestContext
1825 * Our connection to the service.
1827 struct GNUNET_CORE_Handle *h;
1830 * Link to control message, NULL if CM was sent.
1832 struct ControlMessage *cm;
1835 * Link to peer record.
1837 struct PeerRecord *pr;
1842 * CM was sent, remove link so we don't double-free.
1844 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1845 * @param success were we successful?
1848 change_preference_send_continuation (void *cls,
1851 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1854 // FIXME: who frees 'irc'?
1859 * Obtain statistics and/or change preferences for the given peer.
1861 * @param h core handle
1862 * @param peer identifies the peer
1863 * @param timeout after how long should we give up (and call "info" with NULL
1864 * for "peer" to signal an error)?
1865 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1866 * caller should set "bw_out" to "-1" to avoid changing
1867 * the current value; otherwise "bw_out" will be lowered to
1868 * the specified value; passing a pointer to "0" can be used to force
1869 * us to disconnect from the peer; "bw_out" might not increase
1870 * as specified since the upper bound is generally
1871 * determined by the other peer!
1872 * @param amount reserve N bytes for receiving, negative
1873 * amounts can be used to undo a (recent) reservation;
1874 * @param preference increase incoming traffic share preference by this amount;
1875 * in the absence of "amount" reservations, we use this
1876 * preference value to assign proportional bandwidth shares
1877 * to all connected peers
1878 * @param info function to call with the resulting configuration information
1879 * @param info_cls closure for info
1880 * @return NULL on error
1882 struct GNUNET_CORE_InformationRequestContext *
1883 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1884 const struct GNUNET_PeerIdentity *peer,
1885 struct GNUNET_TIME_Relative timeout,
1886 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1888 uint64_t preference,
1889 GNUNET_CORE_PeerConfigurationInfoCallback info,
1892 struct GNUNET_CORE_InformationRequestContext *irc;
1893 struct PeerRecord *pr;
1894 struct RequestInfoMessage *rim;
1895 struct ControlMessage *cm;
1897 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1901 /* attempt to change preference on peer that is not connected */
1905 if (pr->pcic != NULL)
1907 /* second change before first one is done */
1911 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1914 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1915 sizeof (struct RequestInfoMessage));
1916 cm->cont = &change_preference_send_continuation;
1919 rim = (struct RequestInfoMessage*) &cm[1];
1920 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1921 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1922 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1923 rim->limit_outbound = bw_out;
1924 rim->reserve_inbound = htonl (amount);
1925 rim->preference_change = GNUNET_htonll(preference);
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 "Queueing CHANGE PREFERENCE request\n");
1931 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1932 h->control_pending_tail,
1935 pr->pcic_cls = info_cls;
1936 pr->pcic_ptr = irc; /* for free'ing irc */
1937 if (h->control_pending_head == cm)
1938 trigger_next_request (h, GNUNET_NO);
1944 * Cancel request for getting information about a peer.
1945 * Note that an eventual change in preference, trust or bandwidth
1946 * assignment MAY have already been committed at the time,
1947 * so cancelling a request is NOT sure to undo the original
1948 * request. The original request may or may not still commit.
1949 * The only thing cancellation ensures is that the callback
1950 * from the original request will no longer be called.
1952 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1955 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1957 struct GNUNET_CORE_Handle *h = irc->h;
1958 struct PeerRecord *pr = irc->pr;
1960 GNUNET_assert (pr->pcic_ptr == irc);
1961 if (irc->cm != NULL)
1963 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1964 h->control_pending_tail,
1966 GNUNET_free (irc->cm);
1969 pr->pcic_cls = NULL;
1970 pr->pcic_ptr = NULL;
1975 /* end of core_api.c */