2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/core_api.c
23 * @brief core service; this is the main API for encrypted P2P
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
34 * Information we track for each peer.
40 * We generally do NOT keep peer records in a DLL; this
41 * DLL is only used IF this peer's 'pending_head' message
42 * is ready for transmission.
44 struct PeerRecord *prev;
47 * We generally do NOT keep peer records in a DLL; this
48 * DLL is only used IF this peer's 'pending_head' message
49 * is ready for transmission.
51 struct PeerRecord *next;
54 * Peer the record is about.
56 struct GNUNET_PeerIdentity peer;
59 * Corresponding core handle.
61 struct GNUNET_CORE_Handle *ch;
64 * Head of doubly-linked list of pending requests.
65 * Requests are sorted by deadline *except* for HEAD,
66 * which is only modified upon transmission to core.
68 struct GNUNET_CORE_TransmitHandle *pending_head;
71 * Tail of doubly-linked list of pending requests.
73 struct GNUNET_CORE_TransmitHandle *pending_tail;
76 * Pending callback waiting for peer information, or NULL for none.
78 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
86 * Request information ID for the given pcic (needed in case a
87 * request is cancelled after being submitted to core and a new
88 * one is generated; in this case, we need to avoid matching the
89 * reply to the first (cancelled) request to the second request).
94 * ID of timeout task for the 'pending_head' handle
95 * which is the one with the smallest timeout.
97 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
100 * Current size of the queue of pending requests.
102 unsigned int queue_size;
105 * SendMessageRequest ID generator for this peer.
113 * Entry in a doubly-linked list of control messages to be transmitted
114 * to the core service. Control messages include traffic allocation,
115 * connection requests and of course our initial 'init' request.
117 * The actual message is allocated at the end of this struct.
119 struct ControlMessage
122 * This is a doubly-linked list.
124 struct ControlMessage *next;
127 * This is a doubly-linked list.
129 struct ControlMessage *prev;
132 * Function to run after successful transmission (or call with
133 * reason 'TIMEOUT' on error); called with scheduler context 'NULL'
136 GNUNET_SCHEDULER_Task cont;
139 * Closure for 'cont'.
144 * Transmit handle (if one is associated with this ControlMessage), or NULL.
146 struct GNUNET_CORE_TransmitHandle *th;
152 * Context for the core service connection.
154 struct GNUNET_CORE_Handle
158 * Configuration we're using.
160 const struct GNUNET_CONFIGURATION_Handle *cfg;
163 * Closure for the various callbacks.
168 * Function to call once we've handshaked with the core service.
170 GNUNET_CORE_StartupCallback init;
173 * Function to call whenever we're notified about a peer connecting.
175 GNUNET_CORE_ConnectEventHandler connects;
178 * Function to call whenever we're notified about a peer disconnecting.
180 GNUNET_CORE_DisconnectEventHandler disconnects;
183 * Function to call whenever we're notified about a peer changing status.
185 GNUNET_CORE_PeerStatusEventHandler status_events;
188 * Function to call whenever we receive an inbound message.
190 GNUNET_CORE_MessageCallback inbound_notify;
193 * Function to call whenever we receive an outbound message.
195 GNUNET_CORE_MessageCallback outbound_notify;
198 * Function handlers for messages of particular type.
200 const struct GNUNET_CORE_MessageHandler *handlers;
203 * Our connection to the service.
205 struct GNUNET_CLIENT_Connection *client;
208 * Handle for our current transmission request.
210 struct GNUNET_CLIENT_TransmitHandle *cth;
213 * Head of doubly-linked list of pending requests.
215 struct ControlMessage *pending_head;
218 * Tail of doubly-linked list of pending requests.
220 struct ControlMessage *pending_tail;
223 * Head of doubly-linked list of peers that are core-approved
224 * to send their next message.
226 struct PeerRecord *ready_peer_head;
229 * Tail of doubly-linked list of peers that are core-approved
230 * to send their next message.
232 struct PeerRecord *ready_peer_tail;
235 * Hash map listing all of the peers that we are currently
238 struct GNUNET_CONTAINER_MultiHashMap *peers;
241 * Identity of this peer.
243 struct GNUNET_PeerIdentity me;
246 * ID of reconnect task (if any).
248 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
251 * Current delay we use for re-trying to connect to core.
253 struct GNUNET_TIME_Relative retry_backoff;
256 * Request information ID generator.
261 * Number of messages we are allowed to queue per target.
263 unsigned int queue_size;
266 * Number of entries in the handlers array.
271 * For inbound notifications without a specific handler, do
272 * we expect to only receive headers?
274 int inbound_hdr_only;
277 * For outbound notifications without a specific handler, do
278 * we expect to only receive headers?
280 int outbound_hdr_only;
283 * Are we currently disconnected and hence unable to forward
292 * Handle for a transmission request.
294 struct GNUNET_CORE_TransmitHandle
298 * We keep active transmit handles in a doubly-linked list.
300 struct GNUNET_CORE_TransmitHandle *next;
303 * We keep active transmit handles in a doubly-linked list.
305 struct GNUNET_CORE_TransmitHandle *prev;
308 * Corresponding peer record.
310 struct PeerRecord *peer;
313 * Corresponding SEND_REQUEST message. Only non-NULL
314 * while SEND_REQUEST message is pending.
316 struct ControlMessage *cm;
319 * Function that will be called to get the actual request
320 * (once we are ready to transmit this request to the core).
321 * The function will be called with a NULL buffer to signal
324 GNUNET_CONNECTION_TransmitReadyNotify get_message;
327 * Closure for get_message.
329 void *get_message_cls;
332 * Timeout for this handle.
334 struct GNUNET_TIME_Absolute timeout;
337 * How important is this message?
342 * Size of this request.
347 * Send message request ID for this request.
355 * Our current client connection went down. Clean it up
356 * and try to reconnect!
358 * @param h our handle to the core service
361 reconnect (struct GNUNET_CORE_Handle *h);
365 * Task schedule to try to re-connect to core.
367 * @param cls the 'struct GNUNET_CORE_Handle'
368 * @param tc task context
371 reconnect_task (void *cls,
372 const struct GNUNET_SCHEDULER_TaskContext *tc)
374 struct GNUNET_CORE_Handle *h = cls;
376 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
379 "Connecting to CORE service after delay\n");
386 * Notify clients about disconnect and free
387 * the entry for connected peer.
389 * @param cls the 'struct GNUNET_CORE_Handle*'
390 * @param key the peer identity (not used)
391 * @param value the 'struct PeerRecord' to free.
392 * @return GNUNET_YES (continue)
395 disconnect_and_free_peer_entry (void *cls,
396 const GNUNET_HashCode *key,
399 static struct GNUNET_BANDWIDTH_Value32NBO zero;
400 struct GNUNET_CORE_Handle *h = cls;
401 struct GNUNET_CORE_TransmitHandle *th;
402 struct PeerRecord *pr = value;
403 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
405 while (NULL != (th = pr->pending_head))
407 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
412 th->get_message (th->get_message_cls,
416 if (NULL != (pcic = pr->pcic))
424 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
426 GNUNET_SCHEDULER_cancel (pr->timeout_task);
427 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
429 GNUNET_assert (pr->queue_size == 0);
430 if ( (pr->prev != NULL) ||
431 (pr->next != NULL) ||
432 (h->ready_peer_head == pr) )
433 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
436 if (h->disconnects != NULL)
437 h->disconnects (h->cls,
439 GNUNET_assert (GNUNET_YES ==
440 GNUNET_CONTAINER_multihashmap_remove (h->peers,
443 GNUNET_assert (pr->pending_head == NULL);
444 GNUNET_assert (pr->pending_tail == NULL);
445 GNUNET_assert (pr->ch = h);
446 GNUNET_assert (pr->queue_size == 0);
447 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
454 * Close down any existing connection to the CORE service and
455 * try re-establishing it later.
457 * @param h our handle
460 reconnect_later (struct GNUNET_CORE_Handle *h)
462 struct ControlMessage *cm;
463 struct PeerRecord *pr;
465 while (NULL != (cm = h->pending_head))
467 GNUNET_CONTAINER_DLL_remove (h->pending_head,
472 if (cm->cont != NULL)
473 cm->cont (cm->cont_cls, NULL);
476 if (h->client != NULL)
478 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
481 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
482 &disconnect_and_free_peer_entry,
485 while (NULL != (pr = h->ready_peer_head))
486 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
490 GNUNET_assert (h->pending_head == NULL);
491 h->currently_down = GNUNET_YES;
492 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
493 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
495 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
498 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
503 * Check the list of pending requests, send the next
506 * @param h core handle
507 * @param ignore_currently_down transmit message even if not initialized?
510 trigger_next_request (struct GNUNET_CORE_Handle *h,
511 int ignore_currently_down);
515 * The given request hit its timeout. Remove from the
516 * doubly-linked list and call the respective continuation.
518 * @param cls the transmit handle of the request that timed out
519 * @param tc context, can be NULL (!)
522 transmission_timeout (void *cls,
523 const struct GNUNET_SCHEDULER_TaskContext *tc);
527 * Send a control message to the peer asking for transmission
528 * of the message in the given peer record.
530 * @param pr peer to request transmission to
533 request_next_transmission (struct PeerRecord *pr)
535 struct GNUNET_CORE_Handle *h = pr->ch;
536 struct ControlMessage *cm;
537 struct SendMessageRequest *smr;
538 struct GNUNET_CORE_TransmitHandle *th;
540 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
542 GNUNET_SCHEDULER_cancel (pr->timeout_task);
543 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
545 if (NULL == (th = pr->pending_head))
547 trigger_next_request (h, GNUNET_NO);
550 GNUNET_assert (pr->prev == NULL);
551 GNUNET_assert (pr->next == NULL);
552 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
553 &transmission_timeout,
555 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
556 sizeof (struct SendMessageRequest));
559 smr = (struct SendMessageRequest*) &cm[1];
560 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
561 smr->header.size = htons (sizeof (struct SendMessageRequest));
562 smr->priority = htonl (th->priority);
563 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
564 smr->peer = pr->peer;
565 smr->queue_size = htonl (pr->queue_size);
566 smr->size = htons (th->msize);
567 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
568 GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Adding SEND REQUEST for peer `%s' to message queue\n",
575 GNUNET_i2s (&pr->peer));
577 trigger_next_request (h, GNUNET_NO);
582 * The given request hit its timeout. Remove from the
583 * doubly-linked list and call the respective continuation.
585 * @param cls the transmit handle of the request that timed out
586 * @param tc context, can be NULL (!)
589 transmission_timeout (void *cls,
590 const struct GNUNET_SCHEDULER_TaskContext *tc)
592 struct PeerRecord *pr = cls;
593 struct GNUNET_CORE_Handle *h = pr->ch;
594 struct GNUNET_CORE_TransmitHandle *th;
596 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
597 th = pr->pending_head;
598 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
602 if ( (pr->prev != NULL) ||
603 (pr->next != NULL) ||
604 (pr == h->ready_peer_head) )
606 /* the request that was 'approved' by core was
607 canceled before it could be transmitted; remove
608 us from the 'ready' list */
609 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Signalling timeout of request for transmission to CORE service\n");
617 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
618 request_next_transmission (pr);
623 * Transmit the next message to the core service.
626 transmit_message (void *cls,
630 struct GNUNET_CORE_Handle *h = cls;
631 struct ControlMessage *cm;
632 struct GNUNET_CORE_TransmitHandle *th;
633 struct PeerRecord *pr;
634 struct SendMessage *sm;
635 const struct GNUNET_MessageHeader *hdr;
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Transmission failed, initiating reconnect\n");
649 /* first check for control messages */
650 if (NULL != (cm = h->pending_head))
652 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
653 msize = ntohs (hdr->size);
656 trigger_next_request (h, GNUNET_NO);
660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661 "Transmitting control message with %u bytes of type %u to core.\n",
662 (unsigned int) msize,
663 (unsigned int) ntohs (hdr->type));
665 memcpy (buf, hdr, msize);
666 GNUNET_CONTAINER_DLL_remove (h->pending_head,
671 if (NULL != cm->cont)
672 GNUNET_SCHEDULER_add_continuation (cm->cont,
674 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
676 trigger_next_request (h, GNUNET_NO);
679 /* now check for 'ready' P2P messages */
680 if (NULL != (pr = h->ready_peer_head))
682 GNUNET_assert (pr->pending_head != NULL);
683 th = pr->pending_head;
684 if (size < th->msize + sizeof (struct SendMessage))
686 trigger_next_request (h, GNUNET_NO);
689 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
692 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
696 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
698 GNUNET_SCHEDULER_cancel (pr->timeout_task);
699 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
703 "Transmitting SEND request to `%s' with %u bytes.\n",
704 GNUNET_i2s (&pr->peer),
705 (unsigned int) th->msize);
707 sm = (struct SendMessage *) buf;
708 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
709 sm->priority = htonl (th->priority);
710 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
712 ret = th->get_message (th->get_message_cls,
713 size - sizeof (struct SendMessage),
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720 "Size of clients message to peer %s is 0!\n",
721 GNUNET_i2s(&pr->peer));
723 /* client decided to send nothing! */
724 request_next_transmission (pr);
728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 "Produced SEND message to core with %u bytes payload\n",
732 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
733 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
736 request_next_transmission (pr);
739 ret += sizeof (struct SendMessage);
740 sm->header.size = htons (ret);
741 GNUNET_assert (ret <= size);
743 request_next_transmission (pr);
751 * Check the list of pending requests, send the next
754 * @param h core handle
755 * @param ignore_currently_down transmit message even if not initialized?
758 trigger_next_request (struct GNUNET_CORE_Handle *h,
759 int ignore_currently_down)
763 if ( (GNUNET_YES == h->currently_down) &&
764 (ignore_currently_down == GNUNET_NO) )
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Core connection down, not processing queue\n");
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Request pending, not processing queue\n");
780 if (h->pending_head != NULL)
781 msize = ntohs (((struct GNUNET_MessageHeader*) &h->pending_head[1])->size);
782 else if (h->ready_peer_head != NULL)
783 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Request queue empty, not processing queue\n");
790 return; /* no pending message */
792 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
794 GNUNET_TIME_UNIT_FOREVER_REL,
796 &transmit_message, h);
801 * Handler for notification messages received from the core.
803 * @param cls our "struct GNUNET_CORE_Handle"
804 * @param msg the message received from the core service
807 main_notify_handler (void *cls,
808 const struct GNUNET_MessageHeader *msg)
810 struct GNUNET_CORE_Handle *h = cls;
811 const struct InitReplyMessage *m;
812 const struct ConnectNotifyMessage *cnm;
813 const struct DisconnectNotifyMessage *dnm;
814 const struct NotifyTrafficMessage *ntm;
815 const struct GNUNET_MessageHeader *em;
816 const struct ConfigurationInfoMessage *cim;
817 const struct PeerStatusNotifyMessage *psnm;
818 const struct SendMessageReady *smr;
819 const struct GNUNET_CORE_MessageHandler *mh;
820 GNUNET_CORE_StartupCallback init;
821 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
822 struct PeerRecord *pr;
823 struct GNUNET_CORE_TransmitHandle *th;
832 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
834 ("Client was disconnected from core service, trying to reconnect.\n"));
838 msize = ntohs (msg->size);
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
841 "Processing message of type %u and size %u from core service\n",
842 ntohs (msg->type), msize);
844 switch (ntohs (msg->type))
846 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
847 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
853 m = (const struct InitReplyMessage *) msg;
854 GNUNET_break (0 == ntohl (m->reserved));
855 /* start our message processing loop */
856 if (GNUNET_YES == h->currently_down)
858 h->currently_down = GNUNET_NO;
859 trigger_next_request (h, GNUNET_NO);
861 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
862 GNUNET_CRYPTO_hash (&m->publicKey,
864 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
866 if (NULL != (init = h->init))
868 /* mark so we don't call init on reconnect */
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Connected to core service of peer `%s'.\n",
873 GNUNET_i2s (&h->me));
875 init (h->cls, h, &h->me, &m->publicKey);
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881 "Successfully reconnected to core service.\n");
884 /* fake 'connect to self' */
885 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
887 GNUNET_assert (pr == NULL);
888 pr = GNUNET_malloc (sizeof (struct PeerRecord));
891 GNUNET_assert (GNUNET_YES ==
892 GNUNET_CONTAINER_multihashmap_put (h->peers,
895 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
896 if (NULL != h->connects)
901 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
902 if (msize < sizeof (struct ConnectNotifyMessage))
908 cnm = (const struct ConnectNotifyMessage *) msg;
909 ats_count = ntohl (cnm->ats_count);
910 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
911 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919 "Received notification about connection from `%s'.\n",
920 GNUNET_i2s (&cnm->peer));
922 if (0 == memcmp (&h->me,
924 sizeof (struct GNUNET_PeerIdentity)))
926 /* disconnect from self!? */
930 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
931 &cnm->peer.hashPubKey);
938 pr = GNUNET_malloc (sizeof (struct PeerRecord));
939 pr->peer = cnm->peer;
941 GNUNET_assert (GNUNET_YES ==
942 GNUNET_CONTAINER_multihashmap_put (h->peers,
943 &cnm->peer.hashPubKey,
945 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
946 if (NULL != h->connects)
951 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
952 if (msize != sizeof (struct DisconnectNotifyMessage))
958 dnm = (const struct DisconnectNotifyMessage *) msg;
959 if (0 == memcmp (&h->me,
961 sizeof (struct GNUNET_PeerIdentity)))
963 /* connection to self!? */
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "Received notification about disconnect from `%s'.\n",
970 GNUNET_i2s (&dnm->peer));
972 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
973 &dnm->peer.hashPubKey);
980 trigger = ( (pr->prev != NULL) ||
981 (pr->next != NULL) ||
982 (h->ready_peer_head == pr) );
983 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
985 trigger_next_request (h, GNUNET_NO);
987 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
988 if (NULL == h->status_events)
993 if (msize < sizeof (struct PeerStatusNotifyMessage))
999 psnm = (const struct PeerStatusNotifyMessage *) msg;
1000 if (0 == memcmp (&h->me,
1002 sizeof (struct GNUNET_PeerIdentity)))
1008 ats_count = ntohl (psnm->ats_count);
1009 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1010 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1013 reconnect_later (h);
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Received notification about status change by `%s'.\n",
1019 GNUNET_i2s (&psnm->peer));
1021 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1022 &psnm->peer.hashPubKey);
1026 reconnect_later (h);
1029 h->status_events (h->cls,
1032 psnm->bandwidth_out,
1033 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1036 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1037 if (msize < sizeof (struct NotifyTrafficMessage))
1040 reconnect_later (h);
1043 ntm = (const struct NotifyTrafficMessage *) msg;
1044 if (0 == memcmp (&h->me,
1046 sizeof (struct GNUNET_PeerIdentity)))
1052 ats_count = ntohl (ntm->ats_count);
1053 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1054 + sizeof (struct GNUNET_MessageHeader)) ||
1055 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1058 reconnect_later (h);
1061 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Received message of type %u and size %u from peer `%4s'\n",
1067 GNUNET_i2s (&ntm->peer));
1069 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1070 &ntm->peer.hashPubKey);
1074 reconnect_later (h);
1077 if ((GNUNET_NO == h->inbound_hdr_only) &&
1078 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1079 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1082 reconnect_later (h);
1085 et = ntohs (em->type);
1086 for (hpos = 0; hpos < h->hcnt; hpos++)
1088 mh = &h->handlers[hpos];
1091 if ((mh->expected_size != ntohs (em->size)) &&
1092 (mh->expected_size != 0))
1098 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1101 /* error in processing, do not process other messages! */
1105 if (NULL != h->inbound_notify)
1106 h->inbound_notify (h->cls, &ntm->peer, em,
1109 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1110 if (msize < sizeof (struct NotifyTrafficMessage))
1113 reconnect_later (h);
1116 ntm = (const struct NotifyTrafficMessage *) msg;
1117 if (0 == memcmp (&h->me,
1119 sizeof (struct GNUNET_PeerIdentity)))
1125 ats_count = ntohl (ntm->ats_count);
1126 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1127 + sizeof (struct GNUNET_MessageHeader)) ||
1128 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1131 reconnect_later (h);
1134 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1135 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1136 &ntm->peer.hashPubKey);
1140 reconnect_later (h);
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Received notification about transmission to `%s'.\n",
1146 GNUNET_i2s (&ntm->peer));
1148 if ((GNUNET_NO == h->outbound_hdr_only) &&
1149 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1150 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1153 reconnect_later (h);
1156 if (NULL == h->outbound_notify)
1161 h->outbound_notify (h->cls, &ntm->peer, em,
1164 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1165 if (msize != sizeof (struct SendMessageReady))
1168 reconnect_later (h);
1171 smr = (const struct SendMessageReady *) msg;
1172 if (0 == memcmp (&h->me,
1174 sizeof (struct GNUNET_PeerIdentity)))
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))
1258 ntohl (cim->reserved_amount),
1259 GNUNET_ntohll (cim->preference));
1262 reconnect_later (h);
1265 GNUNET_CLIENT_receive (h->client,
1266 &main_notify_handler, h,
1267 GNUNET_TIME_UNIT_FOREVER_REL);
1272 * Task executed once we are done transmitting the INIT message.
1273 * Starts our 'receive' loop.
1275 * @param cls the 'struct GNUNET_CORE_Handle'
1276 * @param tc task context
1279 init_done_task (void *cls,
1280 const struct GNUNET_SCHEDULER_TaskContext *tc)
1282 struct GNUNET_CORE_Handle *h = cls;
1286 if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Failed to exchange INIT with core, retrying\n");
1292 reconnect_later (h);
1295 GNUNET_CLIENT_receive (h->client,
1296 &main_notify_handler,
1298 GNUNET_TIME_UNIT_FOREVER_REL);
1303 * Our current client connection went down. Clean it up
1304 * and try to reconnect!
1306 * @param h our handle to the core service
1309 reconnect (struct GNUNET_CORE_Handle *h)
1311 struct ControlMessage *cm;
1312 struct InitMessage *init;
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Reconnecting to CORE service\n");
1322 GNUNET_assert (h->client == NULL);
1323 GNUNET_assert (h->currently_down == GNUNET_YES);
1324 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1325 if (h->client == NULL)
1327 reconnect_later (h);
1330 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1331 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1333 cm->cont = &init_done_task;
1335 init = (struct InitMessage*) &cm[1];
1336 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1337 init->header.size = htons (msize);
1338 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1339 if (h->status_events != NULL)
1340 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1341 if (h->inbound_notify != NULL)
1343 if (h->inbound_hdr_only)
1344 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1346 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1348 if (h->outbound_notify != NULL)
1350 if (h->outbound_hdr_only)
1351 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1353 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1355 init->options = htonl (opt);
1356 ts = (uint16_t *) &init[1];
1357 for (hpos = 0; hpos < h->hcnt; hpos++)
1358 ts[hpos] = htons (h->handlers[hpos].type);
1359 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1362 trigger_next_request (h, GNUNET_YES);
1368 * Connect to the core service. Note that the connection may
1369 * complete (or fail) asynchronously.
1371 * @param cfg configuration to use
1372 * @param queue_size size of the per-peer message queue
1373 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1374 * @param init callback to call on timeout or once we have successfully
1375 * connected to the core service; note that timeout is only meaningful if init is not NULL
1376 * @param connects function to call on peer connect, can be NULL
1377 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1378 * @param status_events function to call on changes to peer connection status, can be NULL
1379 * @param inbound_notify function to call for all inbound messages, can be NULL
1380 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1381 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1382 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1383 * @param outbound_notify function to call for all outbound messages, can be NULL
1384 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1385 * GNUNET_MessageHeader and hence we do not need to give it the full message
1386 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1387 * @param handlers callbacks for messages we care about, NULL-terminated
1388 * @return handle to the core service (only useful for disconnect until 'init' is called);
1389 * NULL on error (in this case, init is never called)
1391 struct GNUNET_CORE_Handle *
1392 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1393 unsigned int queue_size,
1395 GNUNET_CORE_StartupCallback init,
1396 GNUNET_CORE_ConnectEventHandler connects,
1397 GNUNET_CORE_DisconnectEventHandler disconnects,
1398 GNUNET_CORE_PeerStatusEventHandler status_events,
1399 GNUNET_CORE_MessageCallback inbound_notify,
1400 int inbound_hdr_only,
1401 GNUNET_CORE_MessageCallback outbound_notify,
1402 int outbound_hdr_only,
1403 const struct GNUNET_CORE_MessageHandler *handlers)
1405 struct GNUNET_CORE_Handle *h;
1407 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1409 h->queue_size = queue_size;
1412 h->connects = connects;
1413 h->disconnects = disconnects;
1414 h->status_events = status_events;
1415 h->inbound_notify = inbound_notify;
1416 h->outbound_notify = outbound_notify;
1417 h->inbound_hdr_only = inbound_hdr_only;
1418 h->outbound_hdr_only = outbound_hdr_only;
1419 h->handlers = handlers;
1421 h->currently_down = GNUNET_YES;
1422 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1423 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1424 while (handlers[h->hcnt].callback != NULL)
1426 GNUNET_assert (h->hcnt <
1427 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1428 sizeof (struct InitMessage)) / sizeof (uint16_t));
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Connecting to CORE service\n");
1439 * Disconnect from the core service. This function can only
1440 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1441 * requests have been explicitly canceled.
1443 * @param handle connection to core to disconnect
1446 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1448 struct ControlMessage *cm;
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1452 "Disconnecting from CORE service\n");
1454 if (handle->cth != NULL)
1456 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1459 if (handle->client != NULL)
1461 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1462 handle->client = NULL;
1464 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1466 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1467 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1469 while (NULL != (cm = handle->pending_head))
1471 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
1472 handle->pending_tail,
1476 if (cm->cont != NULL)
1477 cm->cont (cm->cont_cls, NULL);
1480 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1481 &disconnect_and_free_peer_entry,
1483 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1484 GNUNET_break (handle->ready_peer_head == NULL);
1485 GNUNET_free (handle);
1490 * Ask the core to call "notify" once it is ready to transmit the
1491 * given number of bytes to the specified "target". If we are not yet
1492 * connected to the specified peer, a call to this function will cause
1493 * us to try to establish a connection.
1495 * @param handle connection to core service
1496 * @param priority how important is the message?
1497 * @param maxdelay how long can the message wait?
1498 * @param target who should receive the message,
1499 * use NULL for this peer (loopback)
1500 * @param notify_size how many bytes of buffer space does notify want?
1501 * @param notify function to call when buffer space is available
1502 * @param notify_cls closure for notify
1503 * @return non-NULL if the notify callback was queued,
1504 * NULL if we can not even queue the request (insufficient
1505 * memory); if NULL is returned, "notify" will NOT be called.
1507 struct GNUNET_CORE_TransmitHandle *
1508 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1510 struct GNUNET_TIME_Relative maxdelay,
1511 const struct GNUNET_PeerIdentity *target,
1513 GNUNET_CONNECTION_TransmitReadyNotify notify,
1516 struct PeerRecord *pr;
1517 struct GNUNET_CORE_TransmitHandle *th;
1518 struct GNUNET_CORE_TransmitHandle *pos;
1519 struct GNUNET_CORE_TransmitHandle *prev;
1520 struct GNUNET_CORE_TransmitHandle *minp;
1522 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1523 &target->hashPubKey);
1526 /* attempt to send to peer that is not connected */
1527 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1528 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1529 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1533 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1534 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1535 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1537 th->get_message = notify;
1538 th->get_message_cls = notify_cls;
1539 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1540 th->priority = priority;
1541 th->msize = notify_size;
1542 /* bound queue size */
1543 if (pr->queue_size == handle->queue_size)
1545 /* find lowest-priority entry, but skip the head of the list */
1546 minp = pr->pending_head->next;
1548 while (prev != NULL)
1550 if (prev->priority < minp->priority)
1556 GNUNET_break (handle->queue_size != 0);
1557 GNUNET_break (pr->queue_size == 0);
1561 if (priority <= minp->priority)
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 "Dropping transmission request: priority too low\n");
1567 return NULL; /* priority too low */
1569 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1574 minp->get_message (minp->get_message_cls,
1579 /* Order entries by deadline, but SKIP 'HEAD' if
1580 we're in the 'ready_peer_*' DLL */
1581 pos = pr->pending_head;
1582 if ( (pr->prev != NULL) ||
1583 (pr->next != NULL) ||
1584 (pr == handle->ready_peer_head) )
1586 GNUNET_assert (pos != NULL);
1587 pos = pos->next; /* skip head */
1590 /* insertion sort */
1592 while ( (pos != NULL) &&
1593 (pos->timeout.abs_value < th->timeout.abs_value) )
1598 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1603 /* was the request queue previously empty? */
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606 "Transmission request added to queue\n");
1608 if (pr->pending_head == th)
1609 request_next_transmission (pr);
1615 * Cancel the specified transmission-ready notification.
1617 * @param th handle that was returned by "notify_transmit_ready".
1620 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1623 struct PeerRecord *pr = th->peer;
1624 struct GNUNET_CORE_Handle *h = pr->ch;
1627 was_head = (pr->pending_head == th);
1628 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1634 /* we're currently in the control queue, remove */
1635 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1638 GNUNET_free (th->cm);
1643 if ( (pr->prev != NULL) ||
1644 (pr->next != NULL) ||
1645 (pr == h->ready_peer_head) )
1647 /* the request that was 'approved' by core was
1648 canceled before it could be transmitted; remove
1649 us from the 'ready' list */
1650 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1654 request_next_transmission (pr);
1659 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1662 * Handle for a request to the core to connect to
1663 * a particular peer. Can be used to cancel the request
1664 * (before the 'cont'inuation is called).
1666 struct GNUNET_CORE_PeerRequestHandle
1670 * Link to control message.
1672 struct ControlMessage *cm;
1677 struct GNUNET_CORE_Handle *h;
1680 * Continuation to run when done.
1682 GNUNET_SCHEDULER_Task cont;
1685 * Closure for 'cont'.
1693 * Continuation called when the control message was transmitted.
1694 * Calls the original continuation and frees the remaining
1697 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1698 * @param tc scheduler context
1701 peer_request_connect_cont (void *cls,
1702 const struct GNUNET_SCHEDULER_TaskContext *tc)
1704 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1706 if (ret->cont != NULL)
1709 GNUNET_SCHEDULER_add_now (ret->cont,
1712 ret->cont (ret->cont_cls, tc);
1719 * Request that the core should try to connect to a particular peer.
1720 * Once the request has been transmitted to the core, the continuation
1721 * function will be called. Note that this does NOT mean that a
1722 * connection was successfully established -- it only means that the
1723 * core will now try. Successful establishment of the connection
1724 * will be signalled to the 'connects' callback argument of
1725 * 'GNUNET_CORE_connect' only. If the core service does not respond
1726 * to our connection attempt within the given time frame, 'cont' will
1727 * be called with the TIMEOUT reason code.
1729 * @param h core handle
1730 * @param timeout how long to try to talk to core
1731 * @param peer who should we connect to
1732 * @param cont function to call once the request has been completed (or timed out)
1733 * @param cont_cls closure for cont
1734 * @return NULL on error (cont will not be called), otherwise handle for cancellation
1736 struct GNUNET_CORE_PeerRequestHandle *
1737 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1738 struct GNUNET_TIME_Relative timeout,
1739 const struct GNUNET_PeerIdentity * peer,
1740 GNUNET_SCHEDULER_Task cont,
1743 struct GNUNET_CORE_PeerRequestHandle *ret;
1744 struct ControlMessage *cm;
1745 struct ConnectMessage *msg;
1747 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1748 sizeof (struct ConnectMessage));
1749 msg = (struct ConnectMessage*) &cm[1];
1750 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1751 msg->header.size = htons (sizeof (struct ConnectMessage));
1752 msg->reserved = htonl (0);
1753 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1755 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1758 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1762 ret->cont_cls = cont_cls;
1763 cm->cont = &peer_request_connect_cont;
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 "Queueing REQUEST_CONNECT request\n");
1769 if (h->pending_head == cm)
1770 trigger_next_request (h, GNUNET_NO);
1776 * Cancel a pending request to connect to a particular peer. Must not
1777 * be called after the 'cont' function was invoked.
1779 * @param req request handle that was returned for the original request
1782 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1784 struct GNUNET_CORE_Handle *h = req->h;
1785 struct ControlMessage *cm = req->cm;
1787 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1795 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1798 struct GNUNET_CORE_InformationRequestContext
1802 * Our connection to the service.
1804 struct GNUNET_CORE_Handle *h;
1807 * Function to call with the information.
1809 GNUNET_CORE_PeerConfigurationInfoCallback info;
1817 * Link to control message, NULL if CM was sent.
1819 struct ControlMessage *cm;
1822 * Link to peer record.
1824 struct PeerRecord *pr;
1829 * CM was sent, remove link so we don't double-free.
1831 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1832 * @param tc scheduler context
1835 change_preference_send_continuation (void *cls,
1836 const struct GNUNET_SCHEDULER_TaskContext *tc)
1838 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1845 * Obtain statistics and/or change preferences for the given peer.
1847 * @param h core handle
1848 * @param peer identifies the peer
1849 * @param timeout after how long should we give up (and call "info" with NULL
1850 * for "peer" to signal an error)?
1851 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1852 * caller should set "bw_out" to "-1" to avoid changing
1853 * the current value; otherwise "bw_out" will be lowered to
1854 * the specified value; passing a pointer to "0" can be used to force
1855 * us to disconnect from the peer; "bw_out" might not increase
1856 * as specified since the upper bound is generally
1857 * determined by the other peer!
1858 * @param amount reserve N bytes for receiving, negative
1859 * amounts can be used to undo a (recent) reservation;
1860 * @param preference increase incoming traffic share preference by this amount;
1861 * in the absence of "amount" reservations, we use this
1862 * preference value to assign proportional bandwidth shares
1863 * to all connected peers
1864 * @param info function to call with the resulting configuration information
1865 * @param info_cls closure for info
1866 * @return NULL on error
1868 struct GNUNET_CORE_InformationRequestContext *
1869 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1870 const struct GNUNET_PeerIdentity *peer,
1871 struct GNUNET_TIME_Relative timeout,
1872 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1874 uint64_t preference,
1875 GNUNET_CORE_PeerConfigurationInfoCallback info,
1878 struct GNUNET_CORE_InformationRequestContext *irc;
1879 struct PeerRecord *pr;
1880 struct RequestInfoMessage *rim;
1881 struct ControlMessage *cm;
1883 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1887 /* attempt to change preference on peer that is not connected */
1891 if (pr->pcic != NULL)
1893 /* second change before first one is done */
1897 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1901 irc->info_cls = info_cls;
1902 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1903 sizeof (struct RequestInfoMessage));
1904 cm->cont = &change_preference_send_continuation;
1907 rim = (struct RequestInfoMessage*) &cm[1];
1908 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1909 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1910 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1911 rim->limit_outbound = bw_out;
1912 rim->reserve_inbound = htonl (amount);
1913 rim->preference_change = GNUNET_htonll(preference);
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "Queueing CHANGE PREFERENCE request\n");
1919 GNUNET_CONTAINER_DLL_insert (h->pending_head,
1923 pr->pcic_cls = info_cls;
1924 if (h->pending_head == cm)
1925 trigger_next_request (h, GNUNET_NO);
1931 * Cancel request for getting information about a peer.
1932 * Note that an eventual change in preference, trust or bandwidth
1933 * assignment MAY have already been committed at the time,
1934 * so cancelling a request is NOT sure to undo the original
1935 * request. The original request may or may not still commit.
1936 * The only thing cancellation ensures is that the callback
1937 * from the original request will no longer be called.
1939 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1942 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1944 struct GNUNET_CORE_Handle *h = irc->h;
1945 struct PeerRecord *pr = irc->pr;
1947 if (irc->cm != NULL)
1949 GNUNET_CONTAINER_DLL_remove (h->pending_head,
1952 GNUNET_free (irc->cm);
1955 pr->pcic_cls = NULL;
1960 /* end of core_api.c */