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,
426 if (NULL != (pcic = pr->pcic))
429 GNUNET_free_non_null (pr->pcic_ptr);
435 GNUNET_TIME_UNIT_FOREVER_REL,
438 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
440 GNUNET_SCHEDULER_cancel (pr->timeout_task);
441 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
443 GNUNET_assert (pr->queue_size == 0);
444 if ( (pr->prev != NULL) ||
445 (pr->next != NULL) ||
446 (h->ready_peer_head == pr) )
447 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
450 if (h->disconnects != NULL)
451 h->disconnects (h->cls,
453 GNUNET_assert (GNUNET_YES ==
454 GNUNET_CONTAINER_multihashmap_remove (h->peers,
457 GNUNET_assert (pr->pending_head == NULL);
458 GNUNET_assert (pr->pending_tail == NULL);
459 GNUNET_assert (pr->ch = h);
460 GNUNET_assert (pr->queue_size == 0);
461 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
468 * Close down any existing connection to the CORE service and
469 * try re-establishing it later.
471 * @param h our handle
474 reconnect_later (struct GNUNET_CORE_Handle *h)
476 struct ControlMessage *cm;
477 struct PeerRecord *pr;
479 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
480 if (h->client != NULL)
482 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
485 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
486 &disconnect_and_free_peer_entry,
489 while (NULL != (pr = h->ready_peer_head))
490 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
493 h->currently_down = GNUNET_YES;
494 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
497 while (NULL != (cm = h->control_pending_head))
499 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
500 h->control_pending_tail,
504 if (cm->cont != NULL)
505 cm->cont (cm->cont_cls, GNUNET_NO);
508 GNUNET_assert (h->control_pending_head == NULL);
509 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
511 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
516 * Check the list of pending requests, send the next
519 * @param h core handle
520 * @param ignore_currently_down transmit message even if not initialized?
523 trigger_next_request (struct GNUNET_CORE_Handle *h,
524 int ignore_currently_down);
528 * The given request hit its timeout. Remove from the
529 * doubly-linked list and call the respective continuation.
531 * @param cls the transmit handle of the request that timed out
532 * @param tc context, can be NULL (!)
535 transmission_timeout (void *cls,
536 const struct GNUNET_SCHEDULER_TaskContext *tc);
540 * Send a control message to the peer asking for transmission
541 * of the message in the given peer record.
543 * @param pr peer to request transmission to
546 request_next_transmission (struct PeerRecord *pr)
548 struct GNUNET_CORE_Handle *h = pr->ch;
549 struct ControlMessage *cm;
550 struct SendMessageRequest *smr;
551 struct GNUNET_CORE_TransmitHandle *th;
553 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
555 GNUNET_SCHEDULER_cancel (pr->timeout_task);
556 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
558 if (NULL == (th = pr->pending_head))
560 trigger_next_request (h, GNUNET_NO);
564 return; /* already done */
565 GNUNET_assert (pr->prev == NULL);
566 GNUNET_assert (pr->next == NULL);
567 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
568 &transmission_timeout,
570 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
571 sizeof (struct SendMessageRequest));
574 smr = (struct SendMessageRequest*) &cm[1];
575 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
576 smr->header.size = htons (sizeof (struct SendMessageRequest));
577 smr->priority = htonl (th->priority);
578 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
579 smr->peer = pr->peer;
580 smr->queue_size = htonl (pr->queue_size);
581 smr->size = htons (th->msize);
582 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
583 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
584 h->control_pending_tail,
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Adding SEND REQUEST for peer `%s' to message queue\n",
589 GNUNET_i2s (&pr->peer));
591 trigger_next_request (h, GNUNET_NO);
596 * The given request hit its timeout. Remove from the
597 * doubly-linked list and call the respective continuation.
599 * @param cls the transmit handle of the request that timed out
600 * @param tc context, can be NULL (!)
603 transmission_timeout (void *cls,
604 const struct GNUNET_SCHEDULER_TaskContext *tc)
606 struct PeerRecord *pr = cls;
607 struct GNUNET_CORE_Handle *h = pr->ch;
608 struct GNUNET_CORE_TransmitHandle *th;
610 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
611 th = pr->pending_head;
612 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
616 if ( (pr->prev != NULL) ||
617 (pr->next != NULL) ||
618 (pr == h->ready_peer_head) )
620 /* the request that was 'approved' by core was
621 canceled before it could be transmitted; remove
622 us from the 'ready' list */
623 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Signalling timeout of request for transmission to CORE service\n");
631 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
632 request_next_transmission (pr);
637 * Transmit the next message to the core service.
640 transmit_message (void *cls,
644 struct GNUNET_CORE_Handle *h = cls;
645 struct ControlMessage *cm;
646 struct GNUNET_CORE_TransmitHandle *th;
647 struct PeerRecord *pr;
648 struct SendMessage *sm;
649 const struct GNUNET_MessageHeader *hdr;
653 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
659 "Transmission failed, initiating reconnect\n");
664 /* first check for control messages */
665 if (NULL != (cm = h->control_pending_head))
667 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
668 msize = ntohs (hdr->size);
671 trigger_next_request (h, GNUNET_NO);
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Transmitting control message with %u bytes of type %u to core.\n",
677 (unsigned int) msize,
678 (unsigned int) ntohs (hdr->type));
680 memcpy (buf, hdr, msize);
681 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
682 h->control_pending_tail,
686 if (NULL != cm->cont)
687 cm->cont (cm->cont_cls, GNUNET_OK);
689 trigger_next_request (h, GNUNET_NO);
692 /* now check for 'ready' P2P messages */
693 if (NULL != (pr = h->ready_peer_head))
695 GNUNET_assert (pr->pending_head != NULL);
696 th = pr->pending_head;
697 if (size < th->msize + sizeof (struct SendMessage))
699 trigger_next_request (h, GNUNET_NO);
702 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
705 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
709 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
711 GNUNET_SCHEDULER_cancel (pr->timeout_task);
712 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Transmitting SEND request to `%s' with %u bytes.\n",
717 GNUNET_i2s (&pr->peer),
718 (unsigned int) th->msize);
720 sm = (struct SendMessage *) buf;
721 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
722 sm->priority = htonl (th->priority);
723 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
725 sm->cork = htonl ((uint32_t) th->cork);
726 sm->reserved = htonl (0);
727 ret = th->get_message (th->get_message_cls,
728 size - sizeof (struct SendMessage),
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Transmitting SEND request to `%s' yielded %u bytes.\n",
734 GNUNET_i2s (&pr->peer),
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Size of clients message to peer %s is 0!\n",
742 GNUNET_i2s(&pr->peer));
744 /* client decided to send nothing! */
745 request_next_transmission (pr);
750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
751 "Produced SEND message to core with %u bytes payload\n",
754 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
755 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
758 request_next_transmission (pr);
762 ret += sizeof (struct SendMessage);
763 sm->header.size = htons (ret);
764 GNUNET_assert (ret <= size);
766 request_next_transmission (pr);
774 * Check the list of pending requests, send the next
777 * @param h core handle
778 * @param ignore_currently_down transmit message even if not initialized?
781 trigger_next_request (struct GNUNET_CORE_Handle *h,
782 int ignore_currently_down)
786 if ( (GNUNET_YES == h->currently_down) &&
787 (ignore_currently_down == GNUNET_NO) )
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Core connection down, not processing queue\n");
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Request pending, not processing queue\n");
803 if (h->control_pending_head != NULL)
804 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
805 else if (h->ready_peer_head != NULL)
806 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Request queue empty, not processing queue\n");
813 return; /* no pending message */
815 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
817 GNUNET_TIME_UNIT_FOREVER_REL,
819 &transmit_message, h);
824 * Handler for notification messages received from the core.
826 * @param cls our "struct GNUNET_CORE_Handle"
827 * @param msg the message received from the core service
830 main_notify_handler (void *cls,
831 const struct GNUNET_MessageHeader *msg)
833 struct GNUNET_CORE_Handle *h = cls;
834 const struct InitReplyMessage *m;
835 const struct ConnectNotifyMessage *cnm;
836 const struct DisconnectNotifyMessage *dnm;
837 const struct NotifyTrafficMessage *ntm;
838 const struct GNUNET_MessageHeader *em;
839 const struct ConfigurationInfoMessage *cim;
840 const struct PeerStatusNotifyMessage *psnm;
841 const struct SendMessageReady *smr;
842 const struct GNUNET_CORE_MessageHandler *mh;
843 GNUNET_CORE_StartupCallback init;
844 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
845 struct PeerRecord *pr;
846 struct GNUNET_CORE_TransmitHandle *th;
855 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
857 ("Client was disconnected from core service, trying to reconnect.\n"));
861 msize = ntohs (msg->size);
863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
864 "Processing message of type %u and size %u from core service\n",
865 ntohs (msg->type), msize);
867 switch (ntohs (msg->type))
869 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
870 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
876 m = (const struct InitReplyMessage *) msg;
877 GNUNET_break (0 == ntohl (m->reserved));
878 /* start our message processing loop */
879 if (GNUNET_YES == h->currently_down)
881 h->currently_down = GNUNET_NO;
882 trigger_next_request (h, GNUNET_NO);
884 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
885 GNUNET_CRYPTO_hash (&m->publicKey,
887 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
889 if (NULL != (init = h->init))
891 /* mark so we don't call init on reconnect */
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Connected to core service of peer `%s'.\n",
896 GNUNET_i2s (&h->me));
898 init (h->cls, h, &h->me, &m->publicKey);
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Successfully reconnected to core service.\n");
907 /* fake 'connect to self' */
908 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
910 GNUNET_assert (pr == NULL);
911 pr = GNUNET_malloc (sizeof (struct PeerRecord));
914 GNUNET_assert (GNUNET_YES ==
915 GNUNET_CONTAINER_multihashmap_put (h->peers,
918 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
919 if (NULL != h->connects)
924 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
925 if (msize < sizeof (struct ConnectNotifyMessage))
931 cnm = (const struct ConnectNotifyMessage *) msg;
932 ats_count = ntohl (cnm->ats_count);
933 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
934 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Received notification about connection from `%s'.\n",
943 GNUNET_i2s (&cnm->peer));
945 if (0 == memcmp (&h->me,
947 sizeof (struct GNUNET_PeerIdentity)))
949 /* connect to self!? */
953 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
954 &cnm->peer.hashPubKey);
961 pr = GNUNET_malloc (sizeof (struct PeerRecord));
962 pr->peer = cnm->peer;
964 GNUNET_assert (GNUNET_YES ==
965 GNUNET_CONTAINER_multihashmap_put (h->peers,
966 &cnm->peer.hashPubKey,
968 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
969 if (NULL != h->connects)
974 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
975 if (msize != sizeof (struct DisconnectNotifyMessage))
981 dnm = (const struct DisconnectNotifyMessage *) msg;
982 if (0 == memcmp (&h->me,
984 sizeof (struct GNUNET_PeerIdentity)))
986 /* connection to self!? */
990 GNUNET_break (0 == ntohl (dnm->reserved));
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Received notification about disconnect from `%s'.\n",
994 GNUNET_i2s (&dnm->peer));
996 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
997 &dnm->peer.hashPubKey);
1001 reconnect_later (h);
1004 trigger = ( (pr->prev != NULL) ||
1005 (pr->next != NULL) ||
1006 (h->ready_peer_head == pr) );
1007 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
1009 trigger_next_request (h, GNUNET_NO);
1011 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
1012 if (NULL == h->status_events)
1017 if (msize < sizeof (struct PeerStatusNotifyMessage))
1020 reconnect_later (h);
1023 psnm = (const struct PeerStatusNotifyMessage *) msg;
1024 if (0 == memcmp (&h->me,
1026 sizeof (struct GNUNET_PeerIdentity)))
1032 ats_count = ntohl (psnm->ats_count);
1033 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1034 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1037 reconnect_later (h);
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Received notification about status change by `%s'.\n",
1043 GNUNET_i2s (&psnm->peer));
1045 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1046 &psnm->peer.hashPubKey);
1050 reconnect_later (h);
1053 h->status_events (h->cls,
1056 psnm->bandwidth_out,
1057 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1060 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1061 if (msize < sizeof (struct NotifyTrafficMessage))
1064 reconnect_later (h);
1067 ntm = (const struct NotifyTrafficMessage *) msg;
1069 ats_count = ntohl (ntm->ats_count);
1070 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1071 + sizeof (struct GNUNET_MessageHeader)) ||
1072 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1075 reconnect_later (h);
1078 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "Received message of type %u and size %u from peer `%4s'\n",
1084 GNUNET_i2s (&ntm->peer));
1086 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1087 &ntm->peer.hashPubKey);
1091 reconnect_later (h);
1094 if ((GNUNET_NO == h->inbound_hdr_only) &&
1095 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1096 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1099 reconnect_later (h);
1102 et = ntohs (em->type);
1103 for (hpos = 0; hpos < h->hcnt; hpos++)
1105 mh = &h->handlers[hpos];
1108 if ((mh->expected_size != ntohs (em->size)) &&
1109 (mh->expected_size != 0))
1115 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1118 /* error in processing, do not process other messages! */
1122 if (NULL != h->inbound_notify)
1123 h->inbound_notify (h->cls, &ntm->peer, em,
1126 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1127 if (msize < sizeof (struct NotifyTrafficMessage))
1130 reconnect_later (h);
1133 ntm = (const struct NotifyTrafficMessage *) msg;
1134 if (0 == memcmp (&h->me,
1136 sizeof (struct GNUNET_PeerIdentity)))
1142 ats_count = ntohl (ntm->ats_count);
1143 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1144 + sizeof (struct GNUNET_MessageHeader)) ||
1145 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1148 reconnect_later (h);
1151 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1152 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1153 &ntm->peer.hashPubKey);
1157 reconnect_later (h);
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Received notification about transmission to `%s'.\n",
1163 GNUNET_i2s (&ntm->peer));
1165 if ((GNUNET_NO == h->outbound_hdr_only) &&
1166 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1167 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1170 reconnect_later (h);
1173 if (NULL == h->outbound_notify)
1178 h->outbound_notify (h->cls, &ntm->peer, em,
1181 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1182 if (msize != sizeof (struct SendMessageReady))
1185 reconnect_later (h);
1188 smr = (const struct SendMessageReady *) msg;
1189 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1190 &smr->peer.hashPubKey);
1194 reconnect_later (h);
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "Received notification about transmission readiness to `%s'.\n",
1200 GNUNET_i2s (&smr->peer));
1202 if (pr->pending_head == NULL)
1204 /* request must have been cancelled between the original request
1205 and the response from core, ignore core's readiness */
1209 th = pr->pending_head;
1210 if (ntohs (smr->smr_id) != th->smr_id)
1212 /* READY message is for expired or cancelled message,
1213 ignore! (we should have already sent another request) */
1216 if ( (pr->prev != NULL) ||
1217 (pr->next != NULL) ||
1218 (h->ready_peer_head == pr) )
1220 /* we should not already be on the ready list... */
1222 reconnect_later (h);
1225 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1228 trigger_next_request (h, GNUNET_NO);
1230 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1231 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1234 reconnect_later (h);
1237 cim = (const struct ConfigurationInfoMessage*) msg;
1238 if (0 == memcmp (&h->me,
1240 sizeof (struct GNUNET_PeerIdentity)))
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1248 "Received notification about configuration update for `%s' with RIM %u.\n",
1249 GNUNET_i2s (&cim->peer),
1250 (unsigned int) ntohl (cim->rim_id));
1252 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1253 &cim->peer.hashPubKey);
1257 reconnect_later (h);
1260 if (pr->rim_id != ntohl (cim->rim_id))
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "Reservation ID mismatch in notification...\n");
1270 GNUNET_free_non_null (pr->pcic_ptr);
1271 pr->pcic_ptr = NULL;
1276 ntohl (cim->reserved_amount),
1277 GNUNET_TIME_relative_ntoh (cim->reserve_delay),
1278 GNUNET_ntohll (cim->preference));
1281 reconnect_later (h);
1284 GNUNET_CLIENT_receive (h->client,
1285 &main_notify_handler, h,
1286 GNUNET_TIME_UNIT_FOREVER_REL);
1291 * Task executed once we are done transmitting the INIT message.
1292 * Starts our 'receive' loop.
1294 * @param cls the 'struct GNUNET_CORE_Handle'
1295 * @param success were we successful
1298 init_done_task (void *cls,
1301 struct GNUNET_CORE_Handle *h = cls;
1303 if (success == GNUNET_SYSERR)
1304 return; /* shutdown */
1305 if (success == GNUNET_NO)
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309 "Failed to exchange INIT with core, retrying\n");
1311 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1312 reconnect_later (h);
1315 GNUNET_CLIENT_receive (h->client,
1316 &main_notify_handler,
1318 GNUNET_TIME_UNIT_FOREVER_REL);
1323 * Our current client connection went down. Clean it up
1324 * and try to reconnect!
1326 * @param h our handle to the core service
1329 reconnect (struct GNUNET_CORE_Handle *h)
1331 struct ControlMessage *cm;
1332 struct InitMessage *init;
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1340 "Reconnecting to CORE service\n");
1342 GNUNET_assert (h->client == NULL);
1343 GNUNET_assert (h->currently_down == GNUNET_YES);
1344 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1345 if (h->client == NULL)
1347 reconnect_later (h);
1350 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1351 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1353 cm->cont = &init_done_task;
1355 init = (struct InitMessage*) &cm[1];
1356 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1357 init->header.size = htons (msize);
1358 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1359 if (h->status_events != NULL)
1360 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1361 if (h->inbound_notify != NULL)
1363 if (h->inbound_hdr_only)
1364 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1366 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1368 if (h->outbound_notify != NULL)
1370 if (h->outbound_hdr_only)
1371 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1373 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1375 init->options = htonl (opt);
1376 ts = (uint16_t *) &init[1];
1377 for (hpos = 0; hpos < h->hcnt; hpos++)
1378 ts[hpos] = htons (h->handlers[hpos].type);
1379 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1380 h->control_pending_tail,
1382 trigger_next_request (h, GNUNET_YES);
1388 * Connect to the core service. Note that the connection may
1389 * complete (or fail) asynchronously.
1391 * @param cfg configuration to use
1392 * @param queue_size size of the per-peer message queue
1393 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1394 * @param init callback to call on timeout or once we have successfully
1395 * connected to the core service; note that timeout is only meaningful if init is not NULL
1396 * @param connects function to call on peer connect, can be NULL
1397 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1398 * @param status_events function to call on changes to peer connection status, can be NULL
1399 * @param inbound_notify function to call for all inbound messages, can be NULL
1400 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1401 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1402 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1403 * @param outbound_notify function to call for all outbound messages, can be NULL
1404 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1405 * GNUNET_MessageHeader and hence we do not need to give it the full message
1406 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1407 * @param handlers callbacks for messages we care about, NULL-terminated
1408 * @return handle to the core service (only useful for disconnect until 'init' is called);
1409 * NULL on error (in this case, init is never called)
1411 struct GNUNET_CORE_Handle *
1412 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1413 unsigned int queue_size,
1415 GNUNET_CORE_StartupCallback init,
1416 GNUNET_CORE_ConnectEventHandler connects,
1417 GNUNET_CORE_DisconnectEventHandler disconnects,
1418 GNUNET_CORE_PeerStatusEventHandler status_events,
1419 GNUNET_CORE_MessageCallback inbound_notify,
1420 int inbound_hdr_only,
1421 GNUNET_CORE_MessageCallback outbound_notify,
1422 int outbound_hdr_only,
1423 const struct GNUNET_CORE_MessageHandler *handlers)
1425 struct GNUNET_CORE_Handle *h;
1427 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1429 h->queue_size = queue_size;
1432 h->connects = connects;
1433 h->disconnects = disconnects;
1434 h->status_events = status_events;
1435 h->inbound_notify = inbound_notify;
1436 h->outbound_notify = outbound_notify;
1437 h->inbound_hdr_only = inbound_hdr_only;
1438 h->outbound_hdr_only = outbound_hdr_only;
1439 h->handlers = handlers;
1441 h->currently_down = GNUNET_YES;
1442 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1443 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1444 while (handlers[h->hcnt].callback != NULL)
1446 GNUNET_assert (h->hcnt <
1447 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1448 sizeof (struct InitMessage)) / sizeof (uint16_t));
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1451 "Connecting to CORE service\n");
1459 * Disconnect from the core service. This function can only
1460 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1461 * requests have been explicitly canceled.
1463 * @param handle connection to core to disconnect
1466 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1468 struct ControlMessage *cm;
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Disconnecting from CORE service\n");
1474 if (handle->cth != NULL)
1476 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1479 if (handle->client != NULL)
1481 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1482 handle->client = NULL;
1484 while (NULL != (cm = handle->control_pending_head))
1486 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1487 handle->control_pending_tail,
1491 if (cm->cont != NULL)
1492 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1495 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1497 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1498 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1500 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1501 &disconnect_and_free_peer_entry,
1503 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1504 GNUNET_break (handle->ready_peer_head == NULL);
1505 GNUNET_free (handle);
1510 * Ask the core to call "notify" once it is ready to transmit the
1511 * given number of bytes to the specified "target". Must only be
1512 * called after a connection to the respective peer has been
1513 * established (and the client has been informed about this).
1515 * @param handle connection to core service
1516 * @param cork is corking allowed for this transmission?
1517 * @param priority how important is the message?
1518 * @param maxdelay how long can the message wait?
1519 * @param target who should receive the message,
1520 * use NULL for this peer (loopback)
1521 * @param notify_size how many bytes of buffer space does notify want?
1522 * @param notify function to call when buffer space is available
1523 * @param notify_cls closure for notify
1524 * @return non-NULL if the notify callback was queued,
1525 * NULL if we can not even queue the request (insufficient
1526 * memory); if NULL is returned, "notify" will NOT be called.
1528 struct GNUNET_CORE_TransmitHandle *
1529 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1532 struct GNUNET_TIME_Relative maxdelay,
1533 const struct GNUNET_PeerIdentity *target,
1535 GNUNET_CONNECTION_TransmitReadyNotify notify,
1538 struct PeerRecord *pr;
1539 struct GNUNET_CORE_TransmitHandle *th;
1540 struct GNUNET_CORE_TransmitHandle *pos;
1541 struct GNUNET_CORE_TransmitHandle *prev;
1542 struct GNUNET_CORE_TransmitHandle *minp;
1544 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1545 &target->hashPubKey);
1548 /* attempt to send to peer that is not connected */
1549 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1550 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1551 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1555 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1556 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1557 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1559 GNUNET_assert(NULL != notify);
1560 th->get_message = notify;
1561 th->get_message_cls = notify_cls;
1562 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1563 th->priority = priority;
1564 th->msize = notify_size;
1566 /* bound queue size */
1567 if (pr->queue_size == handle->queue_size)
1569 /* find lowest-priority entry, but skip the head of the list */
1570 minp = pr->pending_head->next;
1572 while (prev != NULL)
1574 if (prev->priority < minp->priority)
1580 GNUNET_break (handle->queue_size != 0);
1581 GNUNET_break (pr->queue_size == 1);
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "Dropping transmission request: cannot drop queue head and limit is one\n");
1589 if (priority <= minp->priority)
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1593 "Dropping transmission request: priority too low\n");
1596 return NULL; /* priority too low */
1598 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1603 minp->get_message (minp->get_message_cls,
1608 /* Order entries by deadline, but SKIP 'HEAD' if
1609 we're in the 'ready_peer_*' DLL */
1610 pos = pr->pending_head;
1611 if ( (pr->prev != NULL) ||
1612 (pr->next != NULL) ||
1613 (pr == handle->ready_peer_head) )
1615 GNUNET_assert (pos != NULL);
1616 pos = pos->next; /* skip head */
1619 /* insertion sort */
1621 while ( (pos != NULL) &&
1622 (pos->timeout.abs_value < th->timeout.abs_value) )
1627 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1632 /* was the request queue previously empty? */
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Transmission request added to queue\n");
1637 if (pr->pending_head == th)
1638 request_next_transmission (pr);
1644 * Cancel the specified transmission-ready notification.
1646 * @param th handle that was returned by "notify_transmit_ready".
1649 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1652 struct PeerRecord *pr = th->peer;
1653 struct GNUNET_CORE_Handle *h = pr->ch;
1656 was_head = (pr->pending_head == th);
1657 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1663 /* we're currently in the control queue, remove */
1664 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1665 h->control_pending_tail,
1667 GNUNET_free (th->cm);
1672 if ( (pr->prev != NULL) ||
1673 (pr->next != NULL) ||
1674 (pr == h->ready_peer_head) )
1676 /* the request that was 'approved' by core was
1677 canceled before it could be transmitted; remove
1678 us from the 'ready' list */
1679 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1683 request_next_transmission (pr);
1688 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1691 * Handle for a request to the core to connect to
1692 * a particular peer. Can be used to cancel the request
1693 * (before the 'cont'inuation is called).
1695 struct GNUNET_CORE_PeerRequestHandle
1699 * Link to control message.
1701 struct ControlMessage *cm;
1706 struct GNUNET_CORE_Handle *h;
1709 * Continuation to run when done.
1711 GNUNET_CORE_ControlContinuation cont;
1714 * Closure for 'cont'.
1722 * Continuation called when the control message was transmitted.
1723 * Calls the original continuation and frees the remaining
1726 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1727 * @param success was the request transmitted?
1730 peer_request_connect_cont (void *cls,
1733 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1735 if (ret->cont != NULL)
1736 ret->cont (ret->cont_cls, success);
1742 * Request that the core should try to connect to a particular peer.
1743 * Once the request has been transmitted to the core, the continuation
1744 * function will be called. Note that this does NOT mean that a
1745 * connection was successfully established -- it only means that the
1746 * core will now try. Successful establishment of the connection
1747 * will be signalled to the 'connects' callback argument of
1748 * 'GNUNET_CORE_connect' only. If the core service does not respond
1749 * to our connection attempt within the given time frame, 'cont' will
1750 * be called with the TIMEOUT reason code.
1752 * @param h core handle
1753 * @param timeout how long to try to talk to core
1754 * @param peer who should we connect to
1755 * @param cont function to call once the request has been completed (or timed out)
1756 * @param cont_cls closure for cont
1758 * @return NULL on error or already connected,
1759 * otherwise handle for cancellation
1761 struct GNUNET_CORE_PeerRequestHandle *
1762 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1763 struct GNUNET_TIME_Relative timeout,
1764 const struct GNUNET_PeerIdentity * peer,
1765 GNUNET_CORE_ControlContinuation cont,
1768 struct GNUNET_CORE_PeerRequestHandle *ret;
1769 struct ControlMessage *cm;
1770 struct ConnectMessage *msg;
1772 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1776 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1777 "Peers are already connected!\n");
1782 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1783 sizeof (struct ConnectMessage));
1784 msg = (struct ConnectMessage*) &cm[1];
1785 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1786 msg->header.size = htons (sizeof (struct ConnectMessage));
1787 msg->reserved = htonl (0);
1788 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1790 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1791 h->control_pending_tail,
1793 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1797 ret->cont_cls = cont_cls;
1798 cm->cont = &peer_request_connect_cont;
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1802 "Queueing REQUEST_CONNECT request\n");
1804 trigger_next_request (h, GNUNET_NO);
1810 * Cancel a pending request to connect to a particular peer. Must not
1811 * be called after the 'cont' function was invoked.
1813 * @param req request handle that was returned for the original request
1816 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1818 struct GNUNET_CORE_Handle *h = req->h;
1819 struct ControlMessage *cm = req->cm;
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "A CHANGE PREFERENCE request was cancelled!\n");
1825 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1826 h->control_pending_tail,
1833 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1836 struct GNUNET_CORE_InformationRequestContext
1840 * Our connection to the service.
1842 struct GNUNET_CORE_Handle *h;
1845 * Link to control message, NULL if CM was sent.
1847 struct ControlMessage *cm;
1850 * Link to peer record.
1852 struct PeerRecord *pr;
1857 * CM was sent, remove link so we don't double-free.
1859 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1860 * @param success were we successful?
1863 change_preference_send_continuation (void *cls,
1866 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1873 * Obtain statistics and/or change preferences for the given peer.
1875 * @param h core handle
1876 * @param peer identifies the peer
1877 * @param timeout after how long should we give up (and call "info" with NULL
1878 * for "peer" to signal an error)?
1879 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1880 * caller should set "bw_out" to "-1" to avoid changing
1881 * the current value; otherwise "bw_out" will be lowered to
1882 * the specified value; passing a pointer to "0" can be used to force
1883 * us to disconnect from the peer; "bw_out" might not increase
1884 * as specified since the upper bound is generally
1885 * determined by the other peer!
1886 * @param amount reserve N bytes for receiving, negative
1887 * amounts can be used to undo a (recent) reservation;
1888 * @param preference increase incoming traffic share preference by this amount;
1889 * in the absence of "amount" reservations, we use this
1890 * preference value to assign proportional bandwidth shares
1891 * to all connected peers
1892 * @param info function to call with the resulting configuration information
1893 * @param info_cls closure for info
1894 * @return NULL on error
1896 struct GNUNET_CORE_InformationRequestContext *
1897 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1898 const struct GNUNET_PeerIdentity *peer,
1899 struct GNUNET_TIME_Relative timeout,
1900 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1902 uint64_t preference,
1903 GNUNET_CORE_PeerConfigurationInfoCallback info,
1906 struct GNUNET_CORE_InformationRequestContext *irc;
1907 struct PeerRecord *pr;
1908 struct RequestInfoMessage *rim;
1909 struct ControlMessage *cm;
1911 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1915 /* attempt to change preference on peer that is not connected */
1919 if (pr->pcic != NULL)
1921 /* second change before first one is done */
1925 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1928 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1929 sizeof (struct RequestInfoMessage));
1930 cm->cont = &change_preference_send_continuation;
1933 rim = (struct RequestInfoMessage*) &cm[1];
1934 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1935 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1936 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1937 rim->limit_outbound = bw_out;
1938 rim->reserve_inbound = htonl (amount);
1939 rim->preference_change = GNUNET_htonll(preference);
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1943 "Queueing CHANGE PREFERENCE request for peer `%s' with RIM %u\n",
1945 (unsigned int) pr->rim_id);
1947 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1948 h->control_pending_tail,
1951 pr->pcic_cls = info_cls;
1952 pr->pcic_ptr = irc; /* for free'ing irc */
1953 trigger_next_request (h, GNUNET_NO);
1959 * Cancel request for getting information about a peer.
1960 * Note that an eventual change in preference, trust or bandwidth
1961 * assignment MAY have already been committed at the time,
1962 * so cancelling a request is NOT sure to undo the original
1963 * request. The original request may or may not still commit.
1964 * The only thing cancellation ensures is that the callback
1965 * from the original request will no longer be called.
1967 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1970 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1972 struct GNUNET_CORE_Handle *h = irc->h;
1973 struct PeerRecord *pr = irc->pr;
1975 GNUNET_assert (pr->pcic_ptr == irc);
1976 if (irc->cm != NULL)
1978 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1979 h->control_pending_tail,
1981 GNUNET_free (irc->cm);
1984 pr->pcic_cls = NULL;
1985 pr->pcic_ptr = NULL;
1990 /* end of core_api.c */