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.
350 * Is corking allowed?
358 * Our current client connection went down. Clean it up
359 * and try to reconnect!
361 * @param h our handle to the core service
364 reconnect (struct GNUNET_CORE_Handle *h);
368 * Task schedule to try to re-connect to core.
370 * @param cls the 'struct GNUNET_CORE_Handle'
371 * @param tc task context
374 reconnect_task (void *cls,
375 const struct GNUNET_SCHEDULER_TaskContext *tc)
377 struct GNUNET_CORE_Handle *h = cls;
379 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Connecting to CORE service after delay\n");
389 * Notify clients about disconnect and free
390 * the entry for connected peer.
392 * @param cls the 'struct GNUNET_CORE_Handle*'
393 * @param key the peer identity (not used)
394 * @param value the 'struct PeerRecord' to free.
395 * @return GNUNET_YES (continue)
398 disconnect_and_free_peer_entry (void *cls,
399 const GNUNET_HashCode *key,
402 static struct GNUNET_BANDWIDTH_Value32NBO zero;
403 struct GNUNET_CORE_Handle *h = cls;
404 struct GNUNET_CORE_TransmitHandle *th;
405 struct PeerRecord *pr = value;
406 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
408 while (NULL != (th = pr->pending_head))
410 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
415 th->get_message (th->get_message_cls,
419 if (NULL != (pcic = pr->pcic))
426 GNUNET_TIME_UNIT_FOREVER_REL,
429 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
431 GNUNET_SCHEDULER_cancel (pr->timeout_task);
432 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
434 GNUNET_assert (pr->queue_size == 0);
435 if ( (pr->prev != NULL) ||
436 (pr->next != NULL) ||
437 (h->ready_peer_head == pr) )
438 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
441 if (h->disconnects != NULL)
442 h->disconnects (h->cls,
444 GNUNET_assert (GNUNET_YES ==
445 GNUNET_CONTAINER_multihashmap_remove (h->peers,
448 GNUNET_assert (pr->pending_head == NULL);
449 GNUNET_assert (pr->pending_tail == NULL);
450 GNUNET_assert (pr->ch = h);
451 GNUNET_assert (pr->queue_size == 0);
452 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
459 * Close down any existing connection to the CORE service and
460 * try re-establishing it later.
462 * @param h our handle
465 reconnect_later (struct GNUNET_CORE_Handle *h)
467 struct ControlMessage *cm;
468 struct PeerRecord *pr;
470 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
471 if (h->client != NULL)
473 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
476 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
477 &disconnect_and_free_peer_entry,
480 while (NULL != (pr = h->ready_peer_head))
481 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
484 h->currently_down = GNUNET_YES;
485 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
488 while (NULL != (cm = h->control_pending_head))
490 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
491 h->control_pending_tail,
495 if (cm->cont != NULL)
496 cm->cont (cm->cont_cls, GNUNET_NO);
499 GNUNET_assert (h->control_pending_head == NULL);
500 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
502 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
507 * Check the list of pending requests, send the next
510 * @param h core handle
511 * @param ignore_currently_down transmit message even if not initialized?
514 trigger_next_request (struct GNUNET_CORE_Handle *h,
515 int ignore_currently_down);
519 * The given request hit its timeout. Remove from the
520 * doubly-linked list and call the respective continuation.
522 * @param cls the transmit handle of the request that timed out
523 * @param tc context, can be NULL (!)
526 transmission_timeout (void *cls,
527 const struct GNUNET_SCHEDULER_TaskContext *tc);
531 * Send a control message to the peer asking for transmission
532 * of the message in the given peer record.
534 * @param pr peer to request transmission to
537 request_next_transmission (struct PeerRecord *pr)
539 struct GNUNET_CORE_Handle *h = pr->ch;
540 struct ControlMessage *cm;
541 struct SendMessageRequest *smr;
542 struct GNUNET_CORE_TransmitHandle *th;
544 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
546 GNUNET_SCHEDULER_cancel (pr->timeout_task);
547 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
549 if (NULL == (th = pr->pending_head))
551 trigger_next_request (h, GNUNET_NO);
555 return; /* already done */
556 GNUNET_assert (pr->prev == NULL);
557 GNUNET_assert (pr->next == NULL);
558 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
559 &transmission_timeout,
561 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
562 sizeof (struct SendMessageRequest));
565 smr = (struct SendMessageRequest*) &cm[1];
566 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
567 smr->header.size = htons (sizeof (struct SendMessageRequest));
568 smr->priority = htonl (th->priority);
569 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
570 smr->peer = pr->peer;
571 smr->queue_size = htonl (pr->queue_size);
572 smr->size = htons (th->msize);
573 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
574 GNUNET_CONTAINER_DLL_insert_after (h->control_pending_head,
575 h->control_pending_tail,
576 h->control_pending_tail,
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Adding SEND REQUEST for peer `%s' to message queue\n",
581 GNUNET_i2s (&pr->peer));
583 trigger_next_request (h, GNUNET_NO);
588 * The given request hit its timeout. Remove from the
589 * doubly-linked list and call the respective continuation.
591 * @param cls the transmit handle of the request that timed out
592 * @param tc context, can be NULL (!)
595 transmission_timeout (void *cls,
596 const struct GNUNET_SCHEDULER_TaskContext *tc)
598 struct PeerRecord *pr = cls;
599 struct GNUNET_CORE_Handle *h = pr->ch;
600 struct GNUNET_CORE_TransmitHandle *th;
602 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
603 th = pr->pending_head;
604 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
608 if ( (pr->prev != NULL) ||
609 (pr->next != NULL) ||
610 (pr == h->ready_peer_head) )
612 /* the request that was 'approved' by core was
613 canceled before it could be transmitted; remove
614 us from the 'ready' list */
615 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
621 "Signalling timeout of request for transmission to CORE service\n");
623 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
624 request_next_transmission (pr);
629 * Transmit the next message to the core service.
632 transmit_message (void *cls,
636 struct GNUNET_CORE_Handle *h = cls;
637 struct ControlMessage *cm;
638 struct GNUNET_CORE_TransmitHandle *th;
639 struct PeerRecord *pr;
640 struct SendMessage *sm;
641 const struct GNUNET_MessageHeader *hdr;
645 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Transmission failed, initiating reconnect\n");
656 /* first check for control messages */
657 if (NULL != (cm = h->control_pending_head))
659 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
660 msize = ntohs (hdr->size);
663 trigger_next_request (h, GNUNET_NO);
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Transmitting control message with %u bytes of type %u to core.\n",
669 (unsigned int) msize,
670 (unsigned int) ntohs (hdr->type));
672 memcpy (buf, hdr, msize);
673 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
674 h->control_pending_tail,
678 if (NULL != cm->cont)
679 cm->cont (cm->cont_cls, GNUNET_OK);
681 trigger_next_request (h, GNUNET_NO);
684 /* now check for 'ready' P2P messages */
685 if (NULL != (pr = h->ready_peer_head))
687 GNUNET_assert (pr->pending_head != NULL);
688 th = pr->pending_head;
689 if (size < th->msize + sizeof (struct SendMessage))
691 trigger_next_request (h, GNUNET_NO);
694 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
697 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
701 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
703 GNUNET_SCHEDULER_cancel (pr->timeout_task);
704 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Transmitting SEND request to `%s' with %u bytes.\n",
709 GNUNET_i2s (&pr->peer),
710 (unsigned int) th->msize);
712 sm = (struct SendMessage *) buf;
713 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
714 sm->priority = htonl (th->priority);
715 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
717 sm->cork = htonl ((uint32_t) th->cork);
718 sm->reserved = htonl (0);
719 ret = th->get_message (th->get_message_cls,
720 size - sizeof (struct SendMessage),
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Size of clients message to peer %s is 0!\n",
728 GNUNET_i2s(&pr->peer));
730 /* client decided to send nothing! */
731 request_next_transmission (pr);
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Produced SEND message to core with %u bytes payload\n",
739 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
740 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
743 request_next_transmission (pr);
746 ret += sizeof (struct SendMessage);
747 sm->header.size = htons (ret);
748 GNUNET_assert (ret <= size);
750 request_next_transmission (pr);
758 * Check the list of pending requests, send the next
761 * @param h core handle
762 * @param ignore_currently_down transmit message even if not initialized?
765 trigger_next_request (struct GNUNET_CORE_Handle *h,
766 int ignore_currently_down)
770 if ( (GNUNET_YES == h->currently_down) &&
771 (ignore_currently_down == GNUNET_NO) )
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Core connection down, not processing queue\n");
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
783 "Request pending, not processing queue\n");
787 if (h->control_pending_head != NULL)
788 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
789 else if (h->ready_peer_head != NULL)
790 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 "Request queue empty, not processing queue\n");
797 return; /* no pending message */
799 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
801 GNUNET_TIME_UNIT_FOREVER_REL,
803 &transmit_message, h);
808 * Handler for notification messages received from the core.
810 * @param cls our "struct GNUNET_CORE_Handle"
811 * @param msg the message received from the core service
814 main_notify_handler (void *cls,
815 const struct GNUNET_MessageHeader *msg)
817 struct GNUNET_CORE_Handle *h = cls;
818 const struct InitReplyMessage *m;
819 const struct ConnectNotifyMessage *cnm;
820 const struct DisconnectNotifyMessage *dnm;
821 const struct NotifyTrafficMessage *ntm;
822 const struct GNUNET_MessageHeader *em;
823 const struct ConfigurationInfoMessage *cim;
824 const struct PeerStatusNotifyMessage *psnm;
825 const struct SendMessageReady *smr;
826 const struct GNUNET_CORE_MessageHandler *mh;
827 GNUNET_CORE_StartupCallback init;
828 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
829 struct PeerRecord *pr;
830 struct GNUNET_CORE_TransmitHandle *th;
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
841 ("Client was disconnected from core service, trying to reconnect.\n"));
845 msize = ntohs (msg->size);
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Processing message of type %u and size %u from core service\n",
849 ntohs (msg->type), msize);
851 switch (ntohs (msg->type))
853 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
854 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
860 m = (const struct InitReplyMessage *) msg;
861 GNUNET_break (0 == ntohl (m->reserved));
862 /* start our message processing loop */
863 if (GNUNET_YES == h->currently_down)
865 h->currently_down = GNUNET_NO;
866 trigger_next_request (h, GNUNET_NO);
868 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
869 GNUNET_CRYPTO_hash (&m->publicKey,
871 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
873 if (NULL != (init = h->init))
875 /* mark so we don't call init on reconnect */
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Connected to core service of peer `%s'.\n",
880 GNUNET_i2s (&h->me));
882 init (h->cls, h, &h->me, &m->publicKey);
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "Successfully reconnected to core service.\n");
891 /* fake 'connect to self' */
892 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
894 GNUNET_assert (pr == NULL);
895 pr = GNUNET_malloc (sizeof (struct PeerRecord));
898 GNUNET_assert (GNUNET_YES ==
899 GNUNET_CONTAINER_multihashmap_put (h->peers,
902 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
903 if (NULL != h->connects)
908 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
909 if (msize < sizeof (struct ConnectNotifyMessage))
915 cnm = (const struct ConnectNotifyMessage *) msg;
916 ats_count = ntohl (cnm->ats_count);
917 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
918 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Received notification about connection from `%s'.\n",
927 GNUNET_i2s (&cnm->peer));
929 if (0 == memcmp (&h->me,
931 sizeof (struct GNUNET_PeerIdentity)))
933 /* connect to self!? */
937 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
938 &cnm->peer.hashPubKey);
945 pr = GNUNET_malloc (sizeof (struct PeerRecord));
946 pr->peer = cnm->peer;
948 GNUNET_assert (GNUNET_YES ==
949 GNUNET_CONTAINER_multihashmap_put (h->peers,
950 &cnm->peer.hashPubKey,
952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
953 if (NULL != h->connects)
958 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
959 if (msize != sizeof (struct DisconnectNotifyMessage))
965 dnm = (const struct DisconnectNotifyMessage *) msg;
966 if (0 == memcmp (&h->me,
968 sizeof (struct GNUNET_PeerIdentity)))
970 /* connection to self!? */
974 GNUNET_break (0 == ntohl (dnm->reserved));
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Received notification about disconnect from `%s'.\n",
978 GNUNET_i2s (&dnm->peer));
980 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
981 &dnm->peer.hashPubKey);
988 trigger = ( (pr->prev != NULL) ||
989 (pr->next != NULL) ||
990 (h->ready_peer_head == pr) );
991 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
993 trigger_next_request (h, GNUNET_NO);
995 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
996 if (NULL == h->status_events)
1001 if (msize < sizeof (struct PeerStatusNotifyMessage))
1004 reconnect_later (h);
1007 psnm = (const struct PeerStatusNotifyMessage *) msg;
1008 if (0 == memcmp (&h->me,
1010 sizeof (struct GNUNET_PeerIdentity)))
1016 ats_count = ntohl (psnm->ats_count);
1017 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1018 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1021 reconnect_later (h);
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "Received notification about status change by `%s'.\n",
1027 GNUNET_i2s (&psnm->peer));
1029 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1030 &psnm->peer.hashPubKey);
1034 reconnect_later (h);
1037 h->status_events (h->cls,
1040 psnm->bandwidth_out,
1041 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1044 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1045 if (msize < sizeof (struct NotifyTrafficMessage))
1048 reconnect_later (h);
1051 ntm = (const struct NotifyTrafficMessage *) msg;
1053 ats_count = ntohl (ntm->ats_count);
1054 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1055 + sizeof (struct GNUNET_MessageHeader)) ||
1056 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1059 reconnect_later (h);
1062 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065 "Received message of type %u and size %u from peer `%4s'\n",
1068 GNUNET_i2s (&ntm->peer));
1070 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1071 &ntm->peer.hashPubKey);
1075 reconnect_later (h);
1078 if ((GNUNET_NO == h->inbound_hdr_only) &&
1079 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1080 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1083 reconnect_later (h);
1086 et = ntohs (em->type);
1087 for (hpos = 0; hpos < h->hcnt; hpos++)
1089 mh = &h->handlers[hpos];
1092 if ((mh->expected_size != ntohs (em->size)) &&
1093 (mh->expected_size != 0))
1099 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1102 /* error in processing, do not process other messages! */
1106 if (NULL != h->inbound_notify)
1107 h->inbound_notify (h->cls, &ntm->peer, em,
1110 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1111 if (msize < sizeof (struct NotifyTrafficMessage))
1114 reconnect_later (h);
1117 ntm = (const struct NotifyTrafficMessage *) msg;
1118 if (0 == memcmp (&h->me,
1120 sizeof (struct GNUNET_PeerIdentity)))
1126 ats_count = ntohl (ntm->ats_count);
1127 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1128 + sizeof (struct GNUNET_MessageHeader)) ||
1129 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1132 reconnect_later (h);
1135 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1136 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1137 &ntm->peer.hashPubKey);
1141 reconnect_later (h);
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "Received notification about transmission to `%s'.\n",
1147 GNUNET_i2s (&ntm->peer));
1149 if ((GNUNET_NO == h->outbound_hdr_only) &&
1150 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1151 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1154 reconnect_later (h);
1157 if (NULL == h->outbound_notify)
1162 h->outbound_notify (h->cls, &ntm->peer, em,
1165 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1166 if (msize != sizeof (struct SendMessageReady))
1169 reconnect_later (h);
1172 smr = (const struct SendMessageReady *) msg;
1173 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1174 &smr->peer.hashPubKey);
1178 reconnect_later (h);
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Received notification about transmission readiness to `%s'.\n",
1184 GNUNET_i2s (&smr->peer));
1186 if (pr->pending_head == NULL)
1188 /* request must have been cancelled between the original request
1189 and the response from core, ignore core's readiness */
1193 th = pr->pending_head;
1194 if (ntohs (smr->smr_id) != th->smr_id)
1196 /* READY message is for expired or cancelled message,
1197 ignore! (we should have already sent another request) */
1200 if ( (pr->prev != NULL) ||
1201 (pr->next != NULL) ||
1202 (h->ready_peer_head == pr) )
1204 /* we should not already be on the ready list... */
1206 reconnect_later (h);
1209 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1212 trigger_next_request (h, GNUNET_NO);
1214 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1215 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1218 reconnect_later (h);
1221 cim = (const struct ConfigurationInfoMessage*) msg;
1222 if (0 == memcmp (&h->me,
1224 sizeof (struct GNUNET_PeerIdentity)))
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Received notification about configuration update for `%s'.\n",
1233 GNUNET_i2s (&cim->peer));
1235 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1236 &cim->peer.hashPubKey);
1240 reconnect_later (h);
1243 if (pr->rim_id != ntohl (cim->rim_id))
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Reservation ID mismatch in notification...\n");
1257 ntohl (cim->reserved_amount),
1258 GNUNET_TIME_relative_ntoh (cim->reserve_delay),
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 success were we successful
1279 init_done_task (void *cls,
1282 struct GNUNET_CORE_Handle *h = cls;
1284 if (success == GNUNET_SYSERR)
1285 return; /* shutdown */
1286 if (success == GNUNET_NO)
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "Failed to exchange INIT with core, retrying\n");
1292 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1293 reconnect_later (h);
1296 GNUNET_CLIENT_receive (h->client,
1297 &main_notify_handler,
1299 GNUNET_TIME_UNIT_FOREVER_REL);
1304 * Our current client connection went down. Clean it up
1305 * and try to reconnect!
1307 * @param h our handle to the core service
1310 reconnect (struct GNUNET_CORE_Handle *h)
1312 struct ControlMessage *cm;
1313 struct InitMessage *init;
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321 "Reconnecting to CORE service\n");
1323 GNUNET_assert (h->client == NULL);
1324 GNUNET_assert (h->currently_down == GNUNET_YES);
1325 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1326 if (h->client == NULL)
1328 reconnect_later (h);
1331 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1332 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1334 cm->cont = &init_done_task;
1336 init = (struct InitMessage*) &cm[1];
1337 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1338 init->header.size = htons (msize);
1339 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1340 if (h->status_events != NULL)
1341 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1342 if (h->inbound_notify != NULL)
1344 if (h->inbound_hdr_only)
1345 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1347 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1349 if (h->outbound_notify != NULL)
1351 if (h->outbound_hdr_only)
1352 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1354 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1356 init->options = htonl (opt);
1357 ts = (uint16_t *) &init[1];
1358 for (hpos = 0; hpos < h->hcnt; hpos++)
1359 ts[hpos] = htons (h->handlers[hpos].type);
1360 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1361 h->control_pending_tail,
1363 trigger_next_request (h, GNUNET_YES);
1369 * Connect to the core service. Note that the connection may
1370 * complete (or fail) asynchronously.
1372 * @param cfg configuration to use
1373 * @param queue_size size of the per-peer message queue
1374 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1375 * @param init callback to call on timeout or once we have successfully
1376 * connected to the core service; note that timeout is only meaningful if init is not NULL
1377 * @param connects function to call on peer connect, can be NULL
1378 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1379 * @param status_events function to call on changes to peer connection status, can be NULL
1380 * @param inbound_notify function to call for all inbound messages, can be NULL
1381 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1382 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1383 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1384 * @param outbound_notify function to call for all outbound messages, can be NULL
1385 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1386 * GNUNET_MessageHeader and hence we do not need to give it the full message
1387 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1388 * @param handlers callbacks for messages we care about, NULL-terminated
1389 * @return handle to the core service (only useful for disconnect until 'init' is called);
1390 * NULL on error (in this case, init is never called)
1392 struct GNUNET_CORE_Handle *
1393 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1394 unsigned int queue_size,
1396 GNUNET_CORE_StartupCallback init,
1397 GNUNET_CORE_ConnectEventHandler connects,
1398 GNUNET_CORE_DisconnectEventHandler disconnects,
1399 GNUNET_CORE_PeerStatusEventHandler status_events,
1400 GNUNET_CORE_MessageCallback inbound_notify,
1401 int inbound_hdr_only,
1402 GNUNET_CORE_MessageCallback outbound_notify,
1403 int outbound_hdr_only,
1404 const struct GNUNET_CORE_MessageHandler *handlers)
1406 struct GNUNET_CORE_Handle *h;
1408 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1410 h->queue_size = queue_size;
1413 h->connects = connects;
1414 h->disconnects = disconnects;
1415 h->status_events = status_events;
1416 h->inbound_notify = inbound_notify;
1417 h->outbound_notify = outbound_notify;
1418 h->inbound_hdr_only = inbound_hdr_only;
1419 h->outbound_hdr_only = outbound_hdr_only;
1420 h->handlers = handlers;
1422 h->currently_down = GNUNET_YES;
1423 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1424 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1425 while (handlers[h->hcnt].callback != NULL)
1427 GNUNET_assert (h->hcnt <
1428 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1429 sizeof (struct InitMessage)) / sizeof (uint16_t));
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "Connecting to CORE service\n");
1440 * Disconnect from the core service. This function can only
1441 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1442 * requests have been explicitly canceled.
1444 * @param handle connection to core to disconnect
1447 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1449 struct ControlMessage *cm;
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453 "Disconnecting from CORE service\n");
1455 if (handle->cth != NULL)
1457 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1460 if (handle->client != NULL)
1462 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1463 handle->client = NULL;
1465 while (NULL != (cm = handle->control_pending_head))
1467 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1468 handle->control_pending_tail,
1472 if (cm->cont != NULL)
1473 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1476 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1478 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1479 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1481 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1482 &disconnect_and_free_peer_entry,
1484 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1485 GNUNET_break (handle->ready_peer_head == NULL);
1486 GNUNET_free (handle);
1491 * Ask the core to call "notify" once it is ready to transmit the
1492 * given number of bytes to the specified "target". Must only be
1493 * called after a connection to the respective peer has been
1494 * established (and the client has been informed about this).
1496 * @param handle connection to core service
1497 * @param cork is corking allowed for this transmission?
1498 * @param priority how important is the message?
1499 * @param maxdelay how long can the message wait?
1500 * @param target who should receive the message,
1501 * use NULL for this peer (loopback)
1502 * @param notify_size how many bytes of buffer space does notify want?
1503 * @param notify function to call when buffer space is available
1504 * @param notify_cls closure for notify
1505 * @return non-NULL if the notify callback was queued,
1506 * NULL if we can not even queue the request (insufficient
1507 * memory); if NULL is returned, "notify" will NOT be called.
1509 struct GNUNET_CORE_TransmitHandle *
1510 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1513 struct GNUNET_TIME_Relative maxdelay,
1514 const struct GNUNET_PeerIdentity *target,
1516 GNUNET_CONNECTION_TransmitReadyNotify notify,
1519 struct PeerRecord *pr;
1520 struct GNUNET_CORE_TransmitHandle *th;
1521 struct GNUNET_CORE_TransmitHandle *pos;
1522 struct GNUNET_CORE_TransmitHandle *prev;
1523 struct GNUNET_CORE_TransmitHandle *minp;
1525 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1526 &target->hashPubKey);
1529 /* attempt to send to peer that is not connected */
1530 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1531 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1532 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1536 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1537 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1538 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1540 GNUNET_assert(NULL != notify);
1541 th->get_message = notify;
1542 th->get_message_cls = notify_cls;
1543 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1544 th->priority = priority;
1545 th->msize = notify_size;
1547 /* bound queue size */
1548 if (pr->queue_size == handle->queue_size)
1550 /* find lowest-priority entry, but skip the head of the list */
1551 minp = pr->pending_head->next;
1553 while (prev != NULL)
1555 if (prev->priority < minp->priority)
1561 GNUNET_break (handle->queue_size != 0);
1562 GNUNET_break (pr->queue_size == 1);
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 "Dropping transmission request: cannot drop queue head and limit is one\n");
1570 if (priority <= minp->priority)
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1574 "Dropping transmission request: priority too low\n");
1576 return NULL; /* priority too low */
1578 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1583 minp->get_message (minp->get_message_cls,
1588 /* Order entries by deadline, but SKIP 'HEAD' if
1589 we're in the 'ready_peer_*' DLL */
1590 pos = pr->pending_head;
1591 if ( (pr->prev != NULL) ||
1592 (pr->next != NULL) ||
1593 (pr == handle->ready_peer_head) )
1595 GNUNET_assert (pos != NULL);
1596 pos = pos->next; /* skip head */
1599 /* insertion sort */
1601 while ( (pos != NULL) &&
1602 (pos->timeout.abs_value < th->timeout.abs_value) )
1607 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1612 /* was the request queue previously empty? */
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "Transmission request added to queue\n");
1617 if (pr->pending_head == th)
1618 request_next_transmission (pr);
1624 * Cancel the specified transmission-ready notification.
1626 * @param th handle that was returned by "notify_transmit_ready".
1629 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1632 struct PeerRecord *pr = th->peer;
1633 struct GNUNET_CORE_Handle *h = pr->ch;
1636 was_head = (pr->pending_head == th);
1637 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1643 /* we're currently in the control queue, remove */
1644 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1645 h->control_pending_tail,
1647 GNUNET_free (th->cm);
1652 if ( (pr->prev != NULL) ||
1653 (pr->next != NULL) ||
1654 (pr == h->ready_peer_head) )
1656 /* the request that was 'approved' by core was
1657 canceled before it could be transmitted; remove
1658 us from the 'ready' list */
1659 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1663 request_next_transmission (pr);
1668 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1671 * Handle for a request to the core to connect to
1672 * a particular peer. Can be used to cancel the request
1673 * (before the 'cont'inuation is called).
1675 struct GNUNET_CORE_PeerRequestHandle
1679 * Link to control message.
1681 struct ControlMessage *cm;
1686 struct GNUNET_CORE_Handle *h;
1689 * Continuation to run when done.
1691 GNUNET_CORE_ControlContinuation cont;
1694 * Closure for 'cont'.
1702 * Continuation called when the control message was transmitted.
1703 * Calls the original continuation and frees the remaining
1706 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1707 * @param success was the request transmitted?
1710 peer_request_connect_cont (void *cls,
1713 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1715 if (ret->cont != NULL)
1716 ret->cont (ret->cont_cls, success);
1722 * Request that the core should try to connect to a particular peer.
1723 * Once the request has been transmitted to the core, the continuation
1724 * function will be called. Note that this does NOT mean that a
1725 * connection was successfully established -- it only means that the
1726 * core will now try. Successful establishment of the connection
1727 * will be signalled to the 'connects' callback argument of
1728 * 'GNUNET_CORE_connect' only. If the core service does not respond
1729 * to our connection attempt within the given time frame, 'cont' will
1730 * be called with the TIMEOUT reason code.
1732 * @param h core handle
1733 * @param timeout how long to try to talk to core
1734 * @param peer who should we connect to
1735 * @param cont function to call once the request has been completed (or timed out)
1736 * @param cont_cls closure for cont
1738 * @return NULL on error or already connected,
1739 * otherwise handle for cancellation
1741 struct GNUNET_CORE_PeerRequestHandle *
1742 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1743 struct GNUNET_TIME_Relative timeout,
1744 const struct GNUNET_PeerIdentity * peer,
1745 GNUNET_CORE_ControlContinuation cont,
1748 struct GNUNET_CORE_PeerRequestHandle *ret;
1749 struct ControlMessage *cm;
1750 struct ConnectMessage *msg;
1752 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1756 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peers are already connected!\n");
1761 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1762 sizeof (struct ConnectMessage));
1763 msg = (struct ConnectMessage*) &cm[1];
1764 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1765 msg->header.size = htons (sizeof (struct ConnectMessage));
1766 msg->reserved = htonl (0);
1767 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1769 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1770 h->control_pending_tail,
1772 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1776 ret->cont_cls = cont_cls;
1777 cm->cont = &peer_request_connect_cont;
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "Queueing REQUEST_CONNECT request\n");
1783 if (h->control_pending_head == cm)
1784 trigger_next_request (h, GNUNET_NO);
1790 * Cancel a pending request to connect to a particular peer. Must not
1791 * be called after the 'cont' function was invoked.
1793 * @param req request handle that was returned for the original request
1796 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1798 struct GNUNET_CORE_Handle *h = req->h;
1799 struct ControlMessage *cm = req->cm;
1801 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1802 h->control_pending_tail,
1809 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1812 struct GNUNET_CORE_InformationRequestContext
1816 * Our connection to the service.
1818 struct GNUNET_CORE_Handle *h;
1821 * Link to control message, NULL if CM was sent.
1823 struct ControlMessage *cm;
1826 * Link to peer record.
1828 struct PeerRecord *pr;
1833 * CM was sent, remove link so we don't double-free.
1835 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1836 * @param success were we successful?
1839 change_preference_send_continuation (void *cls,
1842 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1849 * Obtain statistics and/or change preferences for the given peer.
1851 * @param h core handle
1852 * @param peer identifies the peer
1853 * @param timeout after how long should we give up (and call "info" with NULL
1854 * for "peer" to signal an error)?
1855 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1856 * caller should set "bw_out" to "-1" to avoid changing
1857 * the current value; otherwise "bw_out" will be lowered to
1858 * the specified value; passing a pointer to "0" can be used to force
1859 * us to disconnect from the peer; "bw_out" might not increase
1860 * as specified since the upper bound is generally
1861 * determined by the other peer!
1862 * @param amount reserve N bytes for receiving, negative
1863 * amounts can be used to undo a (recent) reservation;
1864 * @param preference increase incoming traffic share preference by this amount;
1865 * in the absence of "amount" reservations, we use this
1866 * preference value to assign proportional bandwidth shares
1867 * to all connected peers
1868 * @param info function to call with the resulting configuration information
1869 * @param info_cls closure for info
1870 * @return NULL on error
1872 struct GNUNET_CORE_InformationRequestContext *
1873 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1874 const struct GNUNET_PeerIdentity *peer,
1875 struct GNUNET_TIME_Relative timeout,
1876 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1878 uint64_t preference,
1879 GNUNET_CORE_PeerConfigurationInfoCallback info,
1882 struct GNUNET_CORE_InformationRequestContext *irc;
1883 struct PeerRecord *pr;
1884 struct RequestInfoMessage *rim;
1885 struct ControlMessage *cm;
1887 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1891 /* attempt to change preference on peer that is not connected */
1895 if (pr->pcic != NULL)
1897 /* second change before first one is done */
1901 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1904 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1905 sizeof (struct RequestInfoMessage));
1906 cm->cont = &change_preference_send_continuation;
1909 rim = (struct RequestInfoMessage*) &cm[1];
1910 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1911 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1912 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1913 rim->limit_outbound = bw_out;
1914 rim->reserve_inbound = htonl (amount);
1915 rim->preference_change = GNUNET_htonll(preference);
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 "Queueing CHANGE PREFERENCE request\n");
1921 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1922 h->control_pending_tail,
1925 pr->pcic_cls = info_cls;
1926 if (h->control_pending_head == cm)
1927 trigger_next_request (h, GNUNET_NO);
1933 * Cancel request for getting information about a peer.
1934 * Note that an eventual change in preference, trust or bandwidth
1935 * assignment MAY have already been committed at the time,
1936 * so cancelling a request is NOT sure to undo the original
1937 * request. The original request may or may not still commit.
1938 * The only thing cancellation ensures is that the callback
1939 * from the original request will no longer be called.
1941 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1944 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
1946 struct GNUNET_CORE_Handle *h = irc->h;
1947 struct PeerRecord *pr = irc->pr;
1949 if (irc->cm != NULL)
1951 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1952 h->control_pending_tail,
1954 GNUNET_free (irc->cm);
1957 pr->pcic_cls = NULL;
1962 /* end of core_api.c */