2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/core_api.c
23 * @brief core service; this is the main API for encrypted P2P
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
34 * Information we track for each peer.
40 * We generally do NOT keep peer records in a DLL; this
41 * DLL is only used IF this peer's 'pending_head' message
42 * is ready for transmission.
44 struct PeerRecord *prev;
47 * We generally do NOT keep peer records in a DLL; this
48 * DLL is only used IF this peer's 'pending_head' message
49 * is ready for transmission.
51 struct PeerRecord *next;
54 * Peer the record is about.
56 struct GNUNET_PeerIdentity peer;
59 * Corresponding core handle.
61 struct GNUNET_CORE_Handle *ch;
64 * Head of doubly-linked list of pending requests.
65 * Requests are sorted by deadline *except* for HEAD,
66 * which is only modified upon transmission to core.
68 struct GNUNET_CORE_TransmitHandle *pending_head;
71 * Tail of doubly-linked list of pending requests.
73 struct GNUNET_CORE_TransmitHandle *pending_tail;
76 * Pending callback waiting for peer information, or NULL for none.
78 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
86 * Request information ID for the given pcic (needed in case a
87 * request is cancelled after being submitted to core and a new
88 * one is generated; in this case, we need to avoid matching the
89 * reply to the first (cancelled) request to the second request).
94 * ID of timeout task for the 'pending_head' handle
95 * which is the one with the smallest timeout.
97 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
100 * Current size of the queue of pending requests.
102 unsigned int queue_size;
105 * SendMessageRequest ID generator for this peer.
113 * Entry in a doubly-linked list of control messages to be transmitted
114 * to the core service. Control messages include traffic allocation,
115 * connection requests and of course our initial 'init' request.
117 * The actual message is allocated at the end of this struct.
119 struct ControlMessage
122 * This is a doubly-linked list.
124 struct ControlMessage *next;
127 * This is a doubly-linked list.
129 struct ControlMessage *prev;
132 * Function to run after transmission failed/succeeded.
134 GNUNET_CORE_ControlContinuation cont;
137 * Closure for 'cont'.
142 * Transmit handle (if one is associated with this ControlMessage), or NULL.
144 struct GNUNET_CORE_TransmitHandle *th;
150 * Context for the core service connection.
152 struct GNUNET_CORE_Handle
156 * Configuration we're using.
158 const struct GNUNET_CONFIGURATION_Handle *cfg;
161 * Closure for the various callbacks.
166 * Function to call once we've handshaked with the core service.
168 GNUNET_CORE_StartupCallback init;
171 * Function to call whenever we're notified about a peer connecting.
173 GNUNET_CORE_ConnectEventHandler connects;
176 * Function to call whenever we're notified about a peer disconnecting.
178 GNUNET_CORE_DisconnectEventHandler disconnects;
181 * Function to call whenever we're notified about a peer changing status.
183 GNUNET_CORE_PeerStatusEventHandler status_events;
186 * Function to call whenever we receive an inbound message.
188 GNUNET_CORE_MessageCallback inbound_notify;
191 * Function to call whenever we receive an outbound message.
193 GNUNET_CORE_MessageCallback outbound_notify;
196 * Function handlers for messages of particular type.
198 const struct GNUNET_CORE_MessageHandler *handlers;
201 * Our connection to the service.
203 struct GNUNET_CLIENT_Connection *client;
206 * Handle for our current transmission request.
208 struct GNUNET_CLIENT_TransmitHandle *cth;
211 * Head of doubly-linked list of pending requests.
213 struct ControlMessage *control_pending_head;
216 * Tail of doubly-linked list of pending requests.
218 struct ControlMessage *control_pending_tail;
221 * Head of doubly-linked list of peers that are core-approved
222 * to send their next message.
224 struct PeerRecord *ready_peer_head;
227 * Tail of doubly-linked list of peers that are core-approved
228 * to send their next message.
230 struct PeerRecord *ready_peer_tail;
233 * Hash map listing all of the peers that we are currently
236 struct GNUNET_CONTAINER_MultiHashMap *peers;
239 * Identity of this peer.
241 struct GNUNET_PeerIdentity me;
244 * ID of reconnect task (if any).
246 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
249 * Current delay we use for re-trying to connect to core.
251 struct GNUNET_TIME_Relative retry_backoff;
254 * Request information ID generator.
259 * Number of messages we are allowed to queue per target.
261 unsigned int queue_size;
264 * Number of entries in the handlers array.
269 * For inbound notifications without a specific handler, do
270 * we expect to only receive headers?
272 int inbound_hdr_only;
275 * For outbound notifications without a specific handler, do
276 * we expect to only receive headers?
278 int outbound_hdr_only;
281 * Are we currently disconnected and hence unable to forward
290 * Handle for a transmission request.
292 struct GNUNET_CORE_TransmitHandle
296 * We keep active transmit handles in a doubly-linked list.
298 struct GNUNET_CORE_TransmitHandle *next;
301 * We keep active transmit handles in a doubly-linked list.
303 struct GNUNET_CORE_TransmitHandle *prev;
306 * Corresponding peer record.
308 struct PeerRecord *peer;
311 * Corresponding SEND_REQUEST message. Only non-NULL
312 * while SEND_REQUEST message is pending.
314 struct ControlMessage *cm;
317 * Function that will be called to get the actual request
318 * (once we are ready to transmit this request to the core).
319 * The function will be called with a NULL buffer to signal
322 GNUNET_CONNECTION_TransmitReadyNotify get_message;
325 * Closure for get_message.
327 void *get_message_cls;
330 * Timeout for this handle.
332 struct GNUNET_TIME_Absolute timeout;
335 * How important is this message?
340 * Size of this request.
345 * Send message request ID for this request.
353 * Our current client connection went down. Clean it up
354 * and try to reconnect!
356 * @param h our handle to the core service
359 reconnect (struct GNUNET_CORE_Handle *h);
363 * Task schedule to try to re-connect to core.
365 * @param cls the 'struct GNUNET_CORE_Handle'
366 * @param tc task context
369 reconnect_task (void *cls,
370 const struct GNUNET_SCHEDULER_TaskContext *tc)
372 struct GNUNET_CORE_Handle *h = cls;
374 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Connecting to CORE service after delay\n");
384 * Notify clients about disconnect and free
385 * the entry for connected peer.
387 * @param cls the 'struct GNUNET_CORE_Handle*'
388 * @param key the peer identity (not used)
389 * @param value the 'struct PeerRecord' to free.
390 * @return GNUNET_YES (continue)
393 disconnect_and_free_peer_entry (void *cls,
394 const GNUNET_HashCode *key,
397 static struct GNUNET_BANDWIDTH_Value32NBO zero;
398 struct GNUNET_CORE_Handle *h = cls;
399 struct GNUNET_CORE_TransmitHandle *th;
400 struct PeerRecord *pr = value;
401 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
403 while (NULL != (th = pr->pending_head))
405 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
410 th->get_message (th->get_message_cls,
414 if (NULL != (pcic = pr->pcic))
422 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
424 GNUNET_SCHEDULER_cancel (pr->timeout_task);
425 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
427 GNUNET_assert (pr->queue_size == 0);
428 if ( (pr->prev != NULL) ||
429 (pr->next != NULL) ||
430 (h->ready_peer_head == pr) )
431 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
434 if (h->disconnects != NULL)
435 h->disconnects (h->cls,
437 GNUNET_assert (GNUNET_YES ==
438 GNUNET_CONTAINER_multihashmap_remove (h->peers,
441 GNUNET_assert (pr->pending_head == NULL);
442 GNUNET_assert (pr->pending_tail == NULL);
443 GNUNET_assert (pr->ch = h);
444 GNUNET_assert (pr->queue_size == 0);
445 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
452 * Close down any existing connection to the CORE service and
453 * try re-establishing it later.
455 * @param h our handle
458 reconnect_later (struct GNUNET_CORE_Handle *h)
460 struct ControlMessage *cm;
461 struct PeerRecord *pr;
463 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
464 if (h->client != NULL)
466 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
469 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
470 &disconnect_and_free_peer_entry,
473 while (NULL != (pr = h->ready_peer_head))
474 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
477 h->currently_down = GNUNET_YES;
478 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
481 while (NULL != (cm = h->control_pending_head))
483 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
484 h->control_pending_tail,
488 if (cm->cont != NULL)
489 cm->cont (cm->cont_cls, GNUNET_NO);
492 GNUNET_assert (h->control_pending_head == NULL);
493 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
495 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
500 * Check the list of pending requests, send the next
503 * @param h core handle
504 * @param ignore_currently_down transmit message even if not initialized?
507 trigger_next_request (struct GNUNET_CORE_Handle *h,
508 int ignore_currently_down);
512 * The given request hit its timeout. Remove from the
513 * doubly-linked list and call the respective continuation.
515 * @param cls the transmit handle of the request that timed out
516 * @param tc context, can be NULL (!)
519 transmission_timeout (void *cls,
520 const struct GNUNET_SCHEDULER_TaskContext *tc);
524 * Send a control message to the peer asking for transmission
525 * of the message in the given peer record.
527 * @param pr peer to request transmission to
530 request_next_transmission (struct PeerRecord *pr)
532 struct GNUNET_CORE_Handle *h = pr->ch;
533 struct ControlMessage *cm;
534 struct SendMessageRequest *smr;
535 struct GNUNET_CORE_TransmitHandle *th;
537 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
539 GNUNET_SCHEDULER_cancel (pr->timeout_task);
540 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
542 if (NULL == (th = pr->pending_head))
544 trigger_next_request (h, GNUNET_NO);
548 return; /* already done */
549 GNUNET_assert (pr->prev == NULL);
550 GNUNET_assert (pr->next == NULL);
551 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
552 &transmission_timeout,
554 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
555 sizeof (struct SendMessageRequest));
558 smr = (struct SendMessageRequest*) &cm[1];
559 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
560 smr->header.size = htons (sizeof (struct SendMessageRequest));
561 smr->priority = htonl (th->priority);
562 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
563 smr->peer = pr->peer;
564 smr->queue_size = htonl (pr->queue_size);
565 smr->size = htons (th->msize);
566 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
567 GNUNET_CONTAINER_DLL_insert_after (h->control_pending_head,
568 h->control_pending_tail,
569 h->control_pending_tail,
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "Adding SEND REQUEST for peer `%s' to message queue\n",
574 GNUNET_i2s (&pr->peer));
576 trigger_next_request (h, GNUNET_NO);
581 * The given request hit its timeout. Remove from the
582 * doubly-linked list and call the respective continuation.
584 * @param cls the transmit handle of the request that timed out
585 * @param tc context, can be NULL (!)
588 transmission_timeout (void *cls,
589 const struct GNUNET_SCHEDULER_TaskContext *tc)
591 struct PeerRecord *pr = cls;
592 struct GNUNET_CORE_Handle *h = pr->ch;
593 struct GNUNET_CORE_TransmitHandle *th;
595 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
596 th = pr->pending_head;
597 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
601 if ( (pr->prev != NULL) ||
602 (pr->next != NULL) ||
603 (pr == h->ready_peer_head) )
605 /* the request that was 'approved' by core was
606 canceled before it could be transmitted; remove
607 us from the 'ready' list */
608 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614 "Signalling timeout of request for transmission to CORE service\n");
616 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
617 request_next_transmission (pr);
622 * Transmit the next message to the core service.
625 transmit_message (void *cls,
629 struct GNUNET_CORE_Handle *h = cls;
630 struct ControlMessage *cm;
631 struct GNUNET_CORE_TransmitHandle *th;
632 struct PeerRecord *pr;
633 struct SendMessage *sm;
634 const struct GNUNET_MessageHeader *hdr;
638 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Transmission failed, initiating reconnect\n");
649 /* first check for control messages */
650 if (NULL != (cm = h->control_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->control_pending_head,
667 h->control_pending_tail,
671 if (NULL != cm->cont)
672 cm->cont (cm->cont_cls, GNUNET_OK);
674 trigger_next_request (h, GNUNET_NO);
677 /* now check for 'ready' P2P messages */
678 if (NULL != (pr = h->ready_peer_head))
680 GNUNET_assert (pr->pending_head != NULL);
681 th = pr->pending_head;
682 if (size < th->msize + sizeof (struct SendMessage))
684 trigger_next_request (h, GNUNET_NO);
687 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
690 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
694 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
696 GNUNET_SCHEDULER_cancel (pr->timeout_task);
697 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "Transmitting SEND request to `%s' with %u bytes.\n",
702 GNUNET_i2s (&pr->peer),
703 (unsigned int) th->msize);
705 sm = (struct SendMessage *) buf;
706 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
707 sm->priority = htonl (th->priority);
708 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
710 ret = th->get_message (th->get_message_cls,
711 size - sizeof (struct SendMessage),
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Size of clients message to peer %s is 0!\n",
719 GNUNET_i2s(&pr->peer));
721 /* client decided to send nothing! */
722 request_next_transmission (pr);
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Produced SEND message to core with %u bytes payload\n",
730 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
731 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
734 request_next_transmission (pr);
737 ret += sizeof (struct SendMessage);
738 sm->header.size = htons (ret);
739 GNUNET_assert (ret <= size);
741 request_next_transmission (pr);
749 * Check the list of pending requests, send the next
752 * @param h core handle
753 * @param ignore_currently_down transmit message even if not initialized?
756 trigger_next_request (struct GNUNET_CORE_Handle *h,
757 int ignore_currently_down)
761 if ( (GNUNET_YES == h->currently_down) &&
762 (ignore_currently_down == GNUNET_NO) )
765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766 "Core connection down, not processing queue\n");
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774 "Request pending, not processing queue\n");
778 if (h->control_pending_head != NULL)
779 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
780 else if (h->ready_peer_head != NULL)
781 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Request queue empty, not processing queue\n");
788 return; /* no pending message */
790 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
792 GNUNET_TIME_UNIT_FOREVER_REL,
794 &transmit_message, h);
799 * Handler for notification messages received from the core.
801 * @param cls our "struct GNUNET_CORE_Handle"
802 * @param msg the message received from the core service
805 main_notify_handler (void *cls,
806 const struct GNUNET_MessageHeader *msg)
808 struct GNUNET_CORE_Handle *h = cls;
809 const struct InitReplyMessage *m;
810 const struct ConnectNotifyMessage *cnm;
811 const struct DisconnectNotifyMessage *dnm;
812 const struct NotifyTrafficMessage *ntm;
813 const struct GNUNET_MessageHeader *em;
814 const struct ConfigurationInfoMessage *cim;
815 const struct PeerStatusNotifyMessage *psnm;
816 const struct SendMessageReady *smr;
817 const struct GNUNET_CORE_MessageHandler *mh;
818 GNUNET_CORE_StartupCallback init;
819 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
820 struct PeerRecord *pr;
821 struct GNUNET_CORE_TransmitHandle *th;
830 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
832 ("Client was disconnected from core service, trying to reconnect.\n"));
836 msize = ntohs (msg->size);
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839 "Processing message of type %u and size %u from core service\n",
840 ntohs (msg->type), msize);
842 switch (ntohs (msg->type))
844 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
845 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
851 m = (const struct InitReplyMessage *) msg;
852 GNUNET_break (0 == ntohl (m->reserved));
853 /* start our message processing loop */
854 if (GNUNET_YES == h->currently_down)
856 h->currently_down = GNUNET_NO;
857 trigger_next_request (h, GNUNET_NO);
859 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
860 GNUNET_CRYPTO_hash (&m->publicKey,
862 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
864 if (NULL != (init = h->init))
866 /* mark so we don't call init on reconnect */
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Connected to core service of peer `%s'.\n",
871 GNUNET_i2s (&h->me));
873 init (h->cls, h, &h->me, &m->publicKey);
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Successfully reconnected to core service.\n");
882 /* fake 'connect to self' */
883 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
885 GNUNET_assert (pr == NULL);
886 pr = GNUNET_malloc (sizeof (struct PeerRecord));
889 GNUNET_assert (GNUNET_YES ==
890 GNUNET_CONTAINER_multihashmap_put (h->peers,
893 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
894 if (NULL != h->connects)
899 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
900 if (msize < sizeof (struct ConnectNotifyMessage))
906 cnm = (const struct ConnectNotifyMessage *) msg;
907 ats_count = ntohl (cnm->ats_count);
908 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
909 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Received notification about connection from `%s'.\n",
918 GNUNET_i2s (&cnm->peer));
920 if (0 == memcmp (&h->me,
922 sizeof (struct GNUNET_PeerIdentity)))
924 /* connect to self!? */
928 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
929 &cnm->peer.hashPubKey);
936 pr = GNUNET_malloc (sizeof (struct PeerRecord));
937 pr->peer = cnm->peer;
939 GNUNET_assert (GNUNET_YES ==
940 GNUNET_CONTAINER_multihashmap_put (h->peers,
941 &cnm->peer.hashPubKey,
943 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
944 if (NULL != h->connects)
949 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
950 if (msize != sizeof (struct DisconnectNotifyMessage))
956 dnm = (const struct DisconnectNotifyMessage *) msg;
957 if (0 == memcmp (&h->me,
959 sizeof (struct GNUNET_PeerIdentity)))
961 /* connection to self!? */
965 GNUNET_break (0 == ntohl (dnm->reserved));
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Received notification about disconnect from `%s'.\n",
969 GNUNET_i2s (&dnm->peer));
971 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
972 &dnm->peer.hashPubKey);
979 trigger = ( (pr->prev != NULL) ||
980 (pr->next != NULL) ||
981 (h->ready_peer_head == pr) );
982 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
984 trigger_next_request (h, GNUNET_NO);
986 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
987 if (NULL == h->status_events)
991 if (msize < sizeof (struct PeerStatusNotifyMessage))
997 psnm = (const struct PeerStatusNotifyMessage *) msg;
998 if (0 == memcmp (&h->me,
1000 sizeof (struct GNUNET_PeerIdentity)))
1006 ats_count = ntohl (psnm->ats_count);
1007 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1008 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1011 reconnect_later (h);
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "Received notification about status change by `%s'.\n",
1017 GNUNET_i2s (&psnm->peer));
1019 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1020 &psnm->peer.hashPubKey);
1024 reconnect_later (h);
1027 h->status_events (h->cls,
1030 psnm->bandwidth_out,
1031 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1034 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1035 if (msize < sizeof (struct NotifyTrafficMessage))
1038 reconnect_later (h);
1041 ntm = (const struct NotifyTrafficMessage *) msg;
1043 ats_count = ntohl (ntm->ats_count);
1044 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1045 + sizeof (struct GNUNET_MessageHeader)) ||
1046 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1049 reconnect_later (h);
1052 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Received message of type %u and size %u from peer `%4s'\n",
1058 GNUNET_i2s (&ntm->peer));
1060 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1061 &ntm->peer.hashPubKey);
1065 reconnect_later (h);
1068 if ((GNUNET_NO == h->inbound_hdr_only) &&
1069 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1070 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1073 reconnect_later (h);
1076 et = ntohs (em->type);
1077 for (hpos = 0; hpos < h->hcnt; hpos++)
1079 mh = &h->handlers[hpos];
1082 if ((mh->expected_size != ntohs (em->size)) &&
1083 (mh->expected_size != 0))
1089 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1092 /* error in processing, do not process other messages! */
1096 if (NULL != h->inbound_notify)
1097 h->inbound_notify (h->cls, &ntm->peer, em,
1100 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1101 if (msize < sizeof (struct NotifyTrafficMessage))
1104 reconnect_later (h);
1107 ntm = (const struct NotifyTrafficMessage *) msg;
1108 if (0 == memcmp (&h->me,
1110 sizeof (struct GNUNET_PeerIdentity)))
1116 ats_count = ntohl (ntm->ats_count);
1117 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1118 + sizeof (struct GNUNET_MessageHeader)) ||
1119 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1122 reconnect_later (h);
1125 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1126 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1127 &ntm->peer.hashPubKey);
1131 reconnect_later (h);
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "Received notification about transmission to `%s'.\n",
1137 GNUNET_i2s (&ntm->peer));
1139 if ((GNUNET_NO == h->outbound_hdr_only) &&
1140 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1141 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1144 reconnect_later (h);
1147 if (NULL == h->outbound_notify)
1152 h->outbound_notify (h->cls, &ntm->peer, em,
1155 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1156 if (msize != sizeof (struct SendMessageReady))
1159 reconnect_later (h);
1162 smr = (const struct SendMessageReady *) msg;
1163 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1164 &smr->peer.hashPubKey);
1168 reconnect_later (h);
1172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1173 "Received notification about transmission readiness to `%s'.\n",
1174 GNUNET_i2s (&smr->peer));
1176 if (pr->pending_head == NULL)
1178 /* request must have been cancelled between the original request
1179 and the response from core, ignore core's readiness */
1183 th = pr->pending_head;
1184 if (ntohs (smr->smr_id) != th->smr_id)
1186 /* READY message is for expired or cancelled message,
1187 ignore! (we should have already sent another request) */
1190 if ( (pr->prev != NULL) ||
1191 (pr->next != NULL) ||
1192 (h->ready_peer_head == pr) )
1194 /* we should not already be on the ready list... */
1196 reconnect_later (h);
1199 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1202 trigger_next_request (h, GNUNET_NO);
1204 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1205 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1208 reconnect_later (h);
1211 cim = (const struct ConfigurationInfoMessage*) msg;
1212 if (0 == memcmp (&h->me,
1214 sizeof (struct GNUNET_PeerIdentity)))
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222 "Received notification about configuration update for `%s'.\n",
1223 GNUNET_i2s (&cim->peer));
1225 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1226 &cim->peer.hashPubKey);
1230 reconnect_later (h);
1233 if (pr->rim_id != ntohl (cim->rim_id))
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Reservation ID mismatch in notification...\n");
1247 ntohl (cim->reserved_amount),
1248 GNUNET_ntohll (cim->preference));
1251 reconnect_later (h);
1254 GNUNET_CLIENT_receive (h->client,
1255 &main_notify_handler, h,
1256 GNUNET_TIME_UNIT_FOREVER_REL);
1261 * Task executed once we are done transmitting the INIT message.
1262 * Starts our 'receive' loop.
1264 * @param cls the 'struct GNUNET_CORE_Handle'
1265 * @param success were we successful
1268 init_done_task (void *cls,
1271 struct GNUNET_CORE_Handle *h = cls;
1273 if (success == GNUNET_SYSERR)
1274 return; /* shutdown */
1275 if (success == GNUNET_NO)
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Failed to exchange INIT with core, retrying\n");
1281 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1282 reconnect_later (h);
1285 GNUNET_CLIENT_receive (h->client,
1286 &main_notify_handler,
1288 GNUNET_TIME_UNIT_FOREVER_REL);
1293 * Our current client connection went down. Clean it up
1294 * and try to reconnect!
1296 * @param h our handle to the core service
1299 reconnect (struct GNUNET_CORE_Handle *h)
1301 struct ControlMessage *cm;
1302 struct InitMessage *init;
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "Reconnecting to CORE service\n");
1312 GNUNET_assert (h->client == NULL);
1313 GNUNET_assert (h->currently_down == GNUNET_YES);
1314 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1315 if (h->client == NULL)
1317 reconnect_later (h);
1320 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1321 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1323 cm->cont = &init_done_task;
1325 init = (struct InitMessage*) &cm[1];
1326 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1327 init->header.size = htons (msize);
1328 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1329 if (h->status_events != NULL)
1330 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1331 if (h->inbound_notify != NULL)
1333 if (h->inbound_hdr_only)
1334 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1336 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1338 if (h->outbound_notify != NULL)
1340 if (h->outbound_hdr_only)
1341 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1343 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1345 init->options = htonl (opt);
1346 ts = (uint16_t *) &init[1];
1347 for (hpos = 0; hpos < h->hcnt; hpos++)
1348 ts[hpos] = htons (h->handlers[hpos].type);
1349 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1350 h->control_pending_tail,
1352 trigger_next_request (h, GNUNET_YES);
1358 * Connect to the core service. Note that the connection may
1359 * complete (or fail) asynchronously.
1361 * @param cfg configuration to use
1362 * @param queue_size size of the per-peer message queue
1363 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1364 * @param init callback to call on timeout or once we have successfully
1365 * connected to the core service; note that timeout is only meaningful if init is not NULL
1366 * @param connects function to call on peer connect, can be NULL
1367 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1368 * @param status_events function to call on changes to peer connection status, can be NULL
1369 * @param inbound_notify function to call for all inbound messages, can be NULL
1370 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1371 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1372 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1373 * @param outbound_notify function to call for all outbound messages, can be NULL
1374 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1375 * GNUNET_MessageHeader and hence we do not need to give it the full message
1376 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1377 * @param handlers callbacks for messages we care about, NULL-terminated
1378 * @return handle to the core service (only useful for disconnect until 'init' is called);
1379 * NULL on error (in this case, init is never called)
1381 struct GNUNET_CORE_Handle *
1382 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1383 unsigned int queue_size,
1385 GNUNET_CORE_StartupCallback init,
1386 GNUNET_CORE_ConnectEventHandler connects,
1387 GNUNET_CORE_DisconnectEventHandler disconnects,
1388 GNUNET_CORE_PeerStatusEventHandler status_events,
1389 GNUNET_CORE_MessageCallback inbound_notify,
1390 int inbound_hdr_only,
1391 GNUNET_CORE_MessageCallback outbound_notify,
1392 int outbound_hdr_only,
1393 const struct GNUNET_CORE_MessageHandler *handlers)
1395 struct GNUNET_CORE_Handle *h;
1397 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1399 h->queue_size = queue_size;
1402 h->connects = connects;
1403 h->disconnects = disconnects;
1404 h->status_events = status_events;
1405 h->inbound_notify = inbound_notify;
1406 h->outbound_notify = outbound_notify;
1407 h->inbound_hdr_only = inbound_hdr_only;
1408 h->outbound_hdr_only = outbound_hdr_only;
1409 h->handlers = handlers;
1411 h->currently_down = GNUNET_YES;
1412 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1413 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1414 while (handlers[h->hcnt].callback != NULL)
1416 GNUNET_assert (h->hcnt <
1417 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1418 sizeof (struct InitMessage)) / sizeof (uint16_t));
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Connecting to CORE service\n");
1429 * Disconnect from the core service. This function can only
1430 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1431 * requests have been explicitly canceled.
1433 * @param handle connection to core to disconnect
1436 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1438 struct ControlMessage *cm;
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Disconnecting from CORE service\n");
1444 if (handle->cth != NULL)
1446 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1449 if (handle->client != NULL)
1451 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1452 handle->client = NULL;
1454 while (NULL != (cm = handle->control_pending_head))
1456 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1457 handle->control_pending_tail,
1461 if (cm->cont != NULL)
1462 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1465 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1467 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1468 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1470 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1471 &disconnect_and_free_peer_entry,
1473 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1474 GNUNET_break (handle->ready_peer_head == NULL);
1475 GNUNET_free (handle);
1480 * Ask the core to call "notify" once it is ready to transmit the
1481 * given number of bytes to the specified "target". Must only be
1482 * called after a connection to the respective peer has been
1483 * established (and the client has been informed about this).
1485 * @param handle connection to core service
1486 * @param priority how important is the message?
1487 * @param maxdelay how long can the message wait?
1488 * @param target who should receive the message,
1489 * use NULL for this peer (loopback)
1490 * @param notify_size how many bytes of buffer space does notify want?
1491 * @param notify function to call when buffer space is available
1492 * @param notify_cls closure for notify
1493 * @return non-NULL if the notify callback was queued,
1494 * NULL if we can not even queue the request (insufficient
1495 * memory); if NULL is returned, "notify" will NOT be called.
1497 struct GNUNET_CORE_TransmitHandle *
1498 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1500 struct GNUNET_TIME_Relative maxdelay,
1501 const struct GNUNET_PeerIdentity *target,
1503 GNUNET_CONNECTION_TransmitReadyNotify notify,
1506 struct PeerRecord *pr;
1507 struct GNUNET_CORE_TransmitHandle *th;
1508 struct GNUNET_CORE_TransmitHandle *pos;
1509 struct GNUNET_CORE_TransmitHandle *prev;
1510 struct GNUNET_CORE_TransmitHandle *minp;
1512 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1513 &target->hashPubKey);
1516 /* attempt to send to peer that is not connected */
1517 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1518 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1519 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1523 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1524 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1525 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1527 GNUNET_assert(NULL != notify);
1528 th->get_message = notify;
1529 th->get_message_cls = notify_cls;
1530 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1531 th->priority = priority;
1532 th->msize = notify_size;
1533 /* bound queue size */
1534 if (pr->queue_size == handle->queue_size)
1536 /* find lowest-priority entry, but skip the head of the list */
1537 minp = pr->pending_head->next;
1539 while (prev != NULL)
1541 if (prev->priority < minp->priority)
1547 GNUNET_break (handle->queue_size != 0);
1548 GNUNET_break (pr->queue_size == 0);
1552 if (priority <= minp->priority)
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Dropping transmission request: priority too low\n");
1558 return NULL; /* priority too low */
1560 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1565 minp->get_message (minp->get_message_cls,
1570 /* Order entries by deadline, but SKIP 'HEAD' if
1571 we're in the 'ready_peer_*' DLL */
1572 pos = pr->pending_head;
1573 if ( (pr->prev != NULL) ||
1574 (pr->next != NULL) ||
1575 (pr == handle->ready_peer_head) )
1577 GNUNET_assert (pos != NULL);
1578 pos = pos->next; /* skip head */
1581 /* insertion sort */
1583 while ( (pos != NULL) &&
1584 (pos->timeout.abs_value < th->timeout.abs_value) )
1589 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1594 /* was the request queue previously empty? */
1596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1597 "Transmission request added to queue\n");
1599 if (pr->pending_head == th)
1600 request_next_transmission (pr);
1606 * Cancel the specified transmission-ready notification.
1608 * @param th handle that was returned by "notify_transmit_ready".
1611 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1614 struct PeerRecord *pr = th->peer;
1615 struct GNUNET_CORE_Handle *h = pr->ch;
1618 was_head = (pr->pending_head == th);
1619 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1625 /* we're currently in the control queue, remove */
1626 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1627 h->control_pending_tail,
1629 GNUNET_free (th->cm);
1634 if ( (pr->prev != NULL) ||
1635 (pr->next != NULL) ||
1636 (pr == h->ready_peer_head) )
1638 /* the request that was 'approved' by core was
1639 canceled before it could be transmitted; remove
1640 us from the 'ready' list */
1641 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1645 request_next_transmission (pr);
1650 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1653 * Handle for a request to the core to connect to
1654 * a particular peer. Can be used to cancel the request
1655 * (before the 'cont'inuation is called).
1657 struct GNUNET_CORE_PeerRequestHandle
1661 * Link to control message.
1663 struct ControlMessage *cm;
1668 struct GNUNET_CORE_Handle *h;
1671 * Continuation to run when done.
1673 GNUNET_CORE_ControlContinuation cont;
1676 * Closure for 'cont'.
1684 * Continuation called when the control message was transmitted.
1685 * Calls the original continuation and frees the remaining
1688 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1689 * @param success was the request transmitted?
1692 peer_request_connect_cont (void *cls,
1695 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1697 if (ret->cont != NULL)
1698 ret->cont (ret->cont_cls, success);
1704 * Request that the core should try to connect to a particular peer.
1705 * Once the request has been transmitted to the core, the continuation
1706 * function will be called. Note that this does NOT mean that a
1707 * connection was successfully established -- it only means that the
1708 * core will now try. Successful establishment of the connection
1709 * will be signalled to the 'connects' callback argument of
1710 * 'GNUNET_CORE_connect' only. If the core service does not respond
1711 * to our connection attempt within the given time frame, 'cont' will
1712 * be called with the TIMEOUT reason code.
1714 * @param h core handle
1715 * @param timeout how long to try to talk to core
1716 * @param peer who should we connect to
1717 * @param cont function to call once the request has been completed (or timed out)
1718 * @param cont_cls closure for cont
1720 * @return NULL on error or already connected,
1721 * otherwise handle for cancellation
1723 struct GNUNET_CORE_PeerRequestHandle *
1724 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1725 struct GNUNET_TIME_Relative timeout,
1726 const struct GNUNET_PeerIdentity * peer,
1727 GNUNET_CORE_ControlContinuation cont,
1730 struct GNUNET_CORE_PeerRequestHandle *ret;
1731 struct ControlMessage *cm;
1732 struct ConnectMessage *msg;
1734 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1736 return NULL; /* Already connected, means callback should have happened already! */
1739 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1740 sizeof (struct ConnectMessage));
1741 msg = (struct ConnectMessage*) &cm[1];
1742 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1743 msg->header.size = htons (sizeof (struct ConnectMessage));
1744 msg->reserved = htonl (0);
1745 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1747 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1748 h->control_pending_tail,
1750 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1754 ret->cont_cls = cont_cls;
1755 cm->cont = &peer_request_connect_cont;
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "Queueing REQUEST_CONNECT request\n");
1761 if (h->control_pending_head == cm)
1762 trigger_next_request (h, GNUNET_NO);
1768 * Cancel a pending request to connect to a particular peer. Must not
1769 * be called after the 'cont' function was invoked.
1771 * @param req request handle that was returned for the original request
1774 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1776 struct GNUNET_CORE_Handle *h = req->h;
1777 struct ControlMessage *cm = req->cm;
1779 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1780 h->control_pending_tail,
1787 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1790 struct GNUNET_CORE_InformationRequestContext
1794 * Our connection to the service.
1796 struct GNUNET_CORE_Handle *h;
1799 * Function to call with the information.
1801 GNUNET_CORE_PeerConfigurationInfoCallback info;
1809 * Link to control message, NULL if CM was sent.
1811 struct ControlMessage *cm;
1814 * Link to peer record.
1816 struct PeerRecord *pr;
1821 * CM was sent, remove link so we don't double-free.
1823 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1824 * @param success were we successful?
1827 change_preference_send_continuation (void *cls,
1830 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1837 * Obtain statistics and/or change preferences for the given peer.
1839 * @param h core handle
1840 * @param peer identifies the peer
1841 * @param timeout after how long should we give up (and call "info" with NULL
1842 * for "peer" to signal an error)?
1843 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1844 * caller should set "bw_out" to "-1" to avoid changing
1845 * the current value; otherwise "bw_out" will be lowered to
1846 * the specified value; passing a pointer to "0" can be used to force
1847 * us to disconnect from the peer; "bw_out" might not increase
1848 * as specified since the upper bound is generally
1849 * determined by the other peer!
1850 * @param amount reserve N bytes for receiving, negative
1851 * amounts can be used to undo a (recent) reservation;
1852 * @param preference increase incoming traffic share preference by this amount;
1853 * in the absence of "amount" reservations, we use this
1854 * preference value to assign proportional bandwidth shares
1855 * to all connected peers
1856 * @param info function to call with the resulting configuration information
1857 * @param info_cls closure for info
1858 * @return NULL on error
1860 struct GNUNET_CORE_InformationRequestContext *
1861 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1862 const struct GNUNET_PeerIdentity *peer,
1863 struct GNUNET_TIME_Relative timeout,
1864 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1866 uint64_t preference,
1867 GNUNET_CORE_PeerConfigurationInfoCallback info,
1870 struct GNUNET_CORE_InformationRequestContext *irc;
1871 struct PeerRecord *pr;
1872 struct RequestInfoMessage *rim;
1873 struct ControlMessage *cm;
1875 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1879 /* attempt to change preference on peer that is not connected */
1883 if (pr->pcic != NULL)
1885 /* second change before first one is done */
1889 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1893 irc->info_cls = info_cls;
1894 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1895 sizeof (struct RequestInfoMessage));
1896 cm->cont = &change_preference_send_continuation;
1899 rim = (struct RequestInfoMessage*) &cm[1];
1900 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1901 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1902 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1903 rim->limit_outbound = bw_out;
1904 rim->reserve_inbound = htonl (amount);
1905 rim->preference_change = GNUNET_htonll(preference);
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "Queueing CHANGE PREFERENCE request\n");
1911 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1912 h->control_pending_tail,
1915 pr->pcic_cls = info_cls;
1916 if (h->control_pending_head == cm)
1917 trigger_next_request (h, GNUNET_NO);
1923 * Cancel request for getting information about a peer.
1924 * Note that an eventual change in preference, trust or bandwidth
1925 * assignment MAY have already been committed at the time,
1926 * so cancelling a request is NOT sure to undo the original
1927 * request. The original request may or may not still commit.
1928 * The only thing cancellation ensures is that the callback
1929 * from the original request will no longer be called.
1931 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1934 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1936 struct GNUNET_CORE_Handle *h = irc->h;
1937 struct PeerRecord *pr = irc->pr;
1939 if (irc->cm != NULL)
1941 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1942 h->control_pending_tail,
1944 GNUNET_free (irc->cm);
1947 pr->pcic_cls = NULL;
1952 /* end of core_api.c */