2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/core_api.c
23 * @brief core service; this is the main API for encrypted P2P
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
34 * Information we track for each peer.
40 * We generally do NOT keep peer records in a DLL; this
41 * DLL is only used IF this peer's 'pending_head' message
42 * is ready for transmission.
44 struct PeerRecord *prev;
47 * We generally do NOT keep peer records in a DLL; this
48 * DLL is only used IF this peer's 'pending_head' message
49 * is ready for transmission.
51 struct PeerRecord *next;
54 * Peer the record is about.
56 struct GNUNET_PeerIdentity peer;
59 * Corresponding core handle.
61 struct GNUNET_CORE_Handle *ch;
64 * Head of doubly-linked list of pending requests.
65 * Requests are sorted by deadline *except* for HEAD,
66 * which is only modified upon transmission to core.
68 struct GNUNET_CORE_TransmitHandle *pending_head;
71 * Tail of doubly-linked list of pending requests.
73 struct GNUNET_CORE_TransmitHandle *pending_tail;
76 * Pending callback waiting for peer information, or NULL for none.
78 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
86 * Pointer to free when we call pcic and to use to cancel
87 * preference change on disconnect.
89 struct GNUNET_CORE_InformationRequestContext *pcic_ptr;
92 * Request information ID for the given pcic (needed in case a
93 * request is cancelled after being submitted to core and a new
94 * one is generated; in this case, we need to avoid matching the
95 * reply to the first (cancelled) request to the second request).
100 * ID of timeout task for the 'pending_head' handle
101 * which is the one with the smallest timeout.
103 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
106 * ID of task to run 'next_request_transmission'.
108 GNUNET_SCHEDULER_TaskIdentifier ntr_task;
111 * Current size of the queue of pending requests.
113 unsigned int queue_size;
116 * SendMessageRequest ID generator for this peer.
124 * Type of function called upon completion.
127 * @param success GNUNET_OK on success (which for request_connect
128 * ONLY means that we transmitted the connect request to CORE,
129 * it does not mean that we are actually now connected!);
130 * GNUNET_NO on timeout,
131 * GNUNET_SYSERR if core was shut down
133 typedef void (*GNUNET_CORE_ControlContinuation) (void *cls, int success);
137 * Entry in a doubly-linked list of control messages to be transmitted
138 * to the core service. Control messages include traffic allocation,
139 * connection requests and of course our initial 'init' request.
141 * The actual message is allocated at the end of this struct.
143 struct ControlMessage
146 * This is a doubly-linked list.
148 struct ControlMessage *next;
151 * This is a doubly-linked list.
153 struct ControlMessage *prev;
156 * Function to run after transmission failed/succeeded.
158 GNUNET_CORE_ControlContinuation cont;
161 * Closure for 'cont'.
166 * Transmit handle (if one is associated with this ControlMessage), or NULL.
168 struct GNUNET_CORE_TransmitHandle *th;
174 * Context for the core service connection.
176 struct GNUNET_CORE_Handle
180 * Configuration we're using.
182 const struct GNUNET_CONFIGURATION_Handle *cfg;
185 * Closure for the various callbacks.
190 * Function to call once we've handshaked with the core service.
192 GNUNET_CORE_StartupCallback init;
195 * Function to call whenever we're notified about a peer connecting.
197 GNUNET_CORE_ConnectEventHandler connects;
200 * Function to call whenever we're notified about a peer disconnecting.
202 GNUNET_CORE_DisconnectEventHandler disconnects;
205 * Function to call whenever we're notified about a peer changing status.
207 GNUNET_CORE_PeerStatusEventHandler status_events;
210 * Function to call whenever we receive an inbound message.
212 GNUNET_CORE_MessageCallback inbound_notify;
215 * Function to call whenever we receive an outbound message.
217 GNUNET_CORE_MessageCallback outbound_notify;
220 * Function handlers for messages of particular type.
222 const struct GNUNET_CORE_MessageHandler *handlers;
225 * Our connection to the service.
227 struct GNUNET_CLIENT_Connection *client;
230 * Handle for our current transmission request.
232 struct GNUNET_CLIENT_TransmitHandle *cth;
235 * Head of doubly-linked list of pending requests.
237 struct ControlMessage *control_pending_head;
240 * Tail of doubly-linked list of pending requests.
242 struct ControlMessage *control_pending_tail;
245 * Head of doubly-linked list of peers that are core-approved
246 * to send their next message.
248 struct PeerRecord *ready_peer_head;
251 * Tail of doubly-linked list of peers that are core-approved
252 * to send their next message.
254 struct PeerRecord *ready_peer_tail;
257 * Hash map listing all of the peers that we are currently
260 struct GNUNET_CONTAINER_MultiHashMap *peers;
263 * Identity of this peer.
265 struct GNUNET_PeerIdentity me;
268 * ID of reconnect task (if any).
270 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
273 * Current delay we use for re-trying to connect to core.
275 struct GNUNET_TIME_Relative retry_backoff;
278 * Request information ID generator.
283 * Number of messages we are allowed to queue per target.
285 unsigned int queue_size;
288 * Number of entries in the handlers array.
293 * For inbound notifications without a specific handler, do
294 * we expect to only receive headers?
296 int inbound_hdr_only;
299 * For outbound notifications without a specific handler, do
300 * we expect to only receive headers?
302 int outbound_hdr_only;
305 * Are we currently disconnected and hence unable to forward
314 * Handle for a transmission request.
316 struct GNUNET_CORE_TransmitHandle
320 * We keep active transmit handles in a doubly-linked list.
322 struct GNUNET_CORE_TransmitHandle *next;
325 * We keep active transmit handles in a doubly-linked list.
327 struct GNUNET_CORE_TransmitHandle *prev;
330 * Corresponding peer record.
332 struct PeerRecord *peer;
335 * Corresponding SEND_REQUEST message. Only non-NULL
336 * while SEND_REQUEST message is pending.
338 struct ControlMessage *cm;
341 * Function that will be called to get the actual request
342 * (once we are ready to transmit this request to the core).
343 * The function will be called with a NULL buffer to signal
346 GNUNET_CONNECTION_TransmitReadyNotify get_message;
349 * Closure for get_message.
351 void *get_message_cls;
354 * Timeout for this handle.
356 struct GNUNET_TIME_Absolute timeout;
359 * How important is this message?
364 * Size of this request.
369 * Send message request ID for this request.
374 * Is corking allowed?
382 * Our current client connection went down. Clean it up
383 * and try to reconnect!
385 * @param h our handle to the core service
388 reconnect (struct GNUNET_CORE_Handle *h);
392 * Task schedule to try to re-connect to core.
394 * @param cls the 'struct GNUNET_CORE_Handle'
395 * @param tc task context
398 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
400 struct GNUNET_CORE_Handle *h = cls;
402 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Connecting to CORE service after delay\n");
412 * Notify clients about disconnect and free
413 * the entry for connected peer.
415 * @param cls the 'struct GNUNET_CORE_Handle*'
416 * @param key the peer identity (not used)
417 * @param value the 'struct PeerRecord' to free.
418 * @return GNUNET_YES (continue)
421 disconnect_and_free_peer_entry (void *cls, const GNUNET_HashCode * key,
424 struct GNUNET_CORE_Handle *h = cls;
425 struct GNUNET_CORE_TransmitHandle *th;
426 struct PeerRecord *pr = value;
427 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
430 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
432 GNUNET_SCHEDULER_cancel (pr->timeout_task);
433 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
435 if (pr->ntr_task != GNUNET_SCHEDULER_NO_TASK)
437 GNUNET_SCHEDULER_cancel (pr->ntr_task);
438 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
440 if ((pr->prev != NULL) || (pr->next != NULL) || (h->ready_peer_head == pr))
441 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
442 if (h->disconnects != NULL)
443 h->disconnects (h->cls, &pr->peer);
444 /* all requests should have been cancelled, clean up anyway, just in case */
445 GNUNET_break (pr->queue_size == 0);
446 if (NULL != (pcic = pr->pcic))
449 pcic_cls = pr->pcic_cls;
450 GNUNET_CORE_peer_change_preference_cancel (pr->pcic_ptr);
451 pcic (pcic_cls, &pr->peer, 0, GNUNET_TIME_UNIT_FOREVER_REL);
453 while (NULL != (th = pr->pending_head))
456 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
462 /* done with 'voluntary' cleanups, now on to normal freeing */
463 GNUNET_assert (GNUNET_YES ==
464 GNUNET_CONTAINER_multihashmap_remove (h->peers, key, pr));
465 GNUNET_assert (pr->pending_head == NULL);
466 GNUNET_assert (pr->pending_tail == NULL);
467 GNUNET_assert (pr->ch = h);
468 GNUNET_assert (pr->queue_size == 0);
469 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
470 GNUNET_assert (pr->ntr_task == GNUNET_SCHEDULER_NO_TASK);
477 * Close down any existing connection to the CORE service and
478 * try re-establishing it later.
480 * @param h our handle
483 reconnect_later (struct GNUNET_CORE_Handle *h)
485 struct ControlMessage *cm;
486 struct PeerRecord *pr;
488 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
491 GNUNET_CLIENT_notify_transmit_ready_cancel (h->cth);
494 if (h->client != NULL)
496 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
499 h->currently_down = GNUNET_YES;
500 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
502 GNUNET_SCHEDULER_add_delayed (h->retry_backoff, &reconnect_task, h);
503 while (NULL != (cm = h->control_pending_head))
505 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
506 h->control_pending_tail, cm);
509 if (cm->cont != NULL)
510 cm->cont (cm->cont_cls, GNUNET_NO);
513 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
514 &disconnect_and_free_peer_entry, h);
515 while (NULL != (pr = h->ready_peer_head))
516 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
517 GNUNET_assert (h->control_pending_head == NULL);
519 GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, h->retry_backoff);
520 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
525 * Check the list of pending requests, send the next
528 * @param h core handle
529 * @param ignore_currently_down transmit message even if not initialized?
532 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down);
536 * The given request hit its timeout. Remove from the
537 * doubly-linked list and call the respective continuation.
539 * @param cls the transmit handle of the request that timed out
540 * @param tc context, can be NULL (!)
543 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
547 * Send a control message to the peer asking for transmission
548 * of the message in the given peer record.
550 * @param pr peer to request transmission to
553 request_next_transmission (struct PeerRecord *pr)
555 struct GNUNET_CORE_Handle *h = pr->ch;
556 struct ControlMessage *cm;
557 struct SendMessageRequest *smr;
558 struct GNUNET_CORE_TransmitHandle *th;
560 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
562 GNUNET_SCHEDULER_cancel (pr->timeout_task);
563 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
565 if (NULL == (th = pr->pending_head))
567 trigger_next_request (h, GNUNET_NO);
571 return; /* already done */
572 GNUNET_assert (pr->prev == NULL);
573 GNUNET_assert (pr->next == NULL);
575 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
576 (th->timeout), &transmission_timeout, pr);
577 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
578 sizeof (struct SendMessageRequest));
581 smr = (struct SendMessageRequest *) &cm[1];
582 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
583 smr->header.size = htons (sizeof (struct SendMessageRequest));
584 smr->priority = htonl (th->priority);
585 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
586 smr->peer = pr->peer;
587 smr->queue_size = htonl (pr->queue_size);
588 smr->size = htons (th->msize);
589 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
590 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
591 h->control_pending_tail, cm);
593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594 "Adding SEND REQUEST for peer `%s' to message queue\n",
595 GNUNET_i2s (&pr->peer));
597 trigger_next_request (h, GNUNET_NO);
602 * The given request hit its timeout. Remove from the
603 * doubly-linked list and call the respective continuation.
605 * @param cls the transmit handle of the request that timed out
606 * @param tc context, can be NULL (!)
609 transmission_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
611 struct PeerRecord *pr = cls;
612 struct GNUNET_CORE_Handle *h = pr->ch;
613 struct GNUNET_CORE_TransmitHandle *th;
615 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
616 th = pr->pending_head;
617 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
619 if ((pr->prev != NULL) || (pr->next != NULL) || (pr == h->ready_peer_head))
621 /* the request that was 'approved' by core was
622 * canceled before it could be transmitted; remove
623 * us from the 'ready' list */
624 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628 "Signalling timeout of request for transmission to CORE service\n");
630 request_next_transmission (pr);
631 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
637 * Transmit the next message to the core service.
640 transmit_message (void *cls, size_t size, void *buf)
642 struct GNUNET_CORE_Handle *h = cls;
643 struct ControlMessage *cm;
644 struct GNUNET_CORE_TransmitHandle *th;
645 struct PeerRecord *pr;
646 struct SendMessage *sm;
647 const struct GNUNET_MessageHeader *hdr;
651 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Transmission failed, initiating reconnect\n");
662 /* first check for control messages */
663 if (NULL != (cm = h->control_pending_head))
665 hdr = (const struct GNUNET_MessageHeader *) &cm[1];
666 msize = ntohs (hdr->size);
669 trigger_next_request (h, GNUNET_NO);
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Transmitting control message with %u bytes of type %u to core.\n",
675 (unsigned int) msize, (unsigned int) ntohs (hdr->type));
677 memcpy (buf, hdr, msize);
678 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
679 h->control_pending_tail, cm);
682 if (NULL != cm->cont)
683 cm->cont (cm->cont_cls, GNUNET_OK);
685 trigger_next_request (h, GNUNET_NO);
688 /* now check for 'ready' P2P messages */
689 if (NULL != (pr = h->ready_peer_head))
691 GNUNET_assert (pr->pending_head != NULL);
692 th = pr->pending_head;
693 if (size < th->msize + sizeof (struct SendMessage))
695 trigger_next_request (h, GNUNET_NO);
698 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
699 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
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), (unsigned int) th->msize);
711 sm = (struct SendMessage *) buf;
712 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
713 sm->priority = htonl (th->priority);
714 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
716 sm->cork = htonl ((uint32_t) th->cork);
717 sm->reserved = htonl (0);
719 th->get_message (th->get_message_cls,
720 size - sizeof (struct SendMessage), &sm[1]);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Transmitting SEND request to `%s' yielded %u bytes.\n",
725 GNUNET_i2s (&pr->peer), ret);
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Size of clients message to peer %s is 0!\n",
733 GNUNET_i2s (&pr->peer));
735 /* client decided to send nothing! */
736 request_next_transmission (pr);
740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
741 "Produced SEND message to core with %u bytes payload\n",
744 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
745 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
748 request_next_transmission (pr);
751 ret += sizeof (struct SendMessage);
752 sm->header.size = htons (ret);
753 GNUNET_assert (ret <= size);
754 request_next_transmission (pr);
762 * Check the list of pending requests, send the next
765 * @param h core handle
766 * @param ignore_currently_down transmit message even if not initialized?
769 trigger_next_request (struct GNUNET_CORE_Handle *h, int ignore_currently_down)
773 if ((GNUNET_YES == h->currently_down) && (ignore_currently_down == GNUNET_NO))
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
777 "Core connection down, not processing queue\n");
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
785 "Request pending, not processing queue\n");
789 if (h->control_pending_head != NULL)
791 ntohs (((struct GNUNET_MessageHeader *) &h->
792 control_pending_head[1])->size);
793 else if (h->ready_peer_head != NULL)
795 h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Request queue empty, not processing queue\n");
802 return; /* no pending message */
805 GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
806 GNUNET_TIME_UNIT_FOREVER_REL,
807 GNUNET_NO, &transmit_message, h);
812 * Handler for notification messages received from the core.
814 * @param cls our "struct GNUNET_CORE_Handle"
815 * @param msg the message received from the core service
818 main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg)
820 struct GNUNET_CORE_Handle *h = cls;
821 const struct InitReplyMessage *m;
822 const struct ConnectNotifyMessage *cnm;
823 const struct DisconnectNotifyMessage *dnm;
824 const struct NotifyTrafficMessage *ntm;
825 const struct GNUNET_MessageHeader *em;
826 const struct ConfigurationInfoMessage *cim;
827 const struct PeerStatusNotifyMessage *psnm;
828 const struct SendMessageReady *smr;
829 const struct GNUNET_CORE_MessageHandler *mh;
830 GNUNET_CORE_StartupCallback init;
831 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
832 struct PeerRecord *pr;
833 struct GNUNET_CORE_TransmitHandle *th;
842 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
844 ("Client was disconnected from core service, trying to reconnect.\n"));
848 msize = ntohs (msg->size);
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Processing message of type %u and size %u from core service\n",
852 ntohs (msg->type), msize);
854 switch (ntohs (msg->type))
856 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
857 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
863 m = (const struct InitReplyMessage *) msg;
864 GNUNET_break (0 == ntohl (m->reserved));
865 /* start our message processing loop */
866 if (GNUNET_YES == h->currently_down)
868 h->currently_down = GNUNET_NO;
869 trigger_next_request (h, GNUNET_NO);
871 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
872 h->me = m->my_identity;
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);
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, &h->me.hashPubKey);
893 GNUNET_assert (pr == NULL);
894 pr = GNUNET_malloc (sizeof (struct PeerRecord));
897 GNUNET_assert (GNUNET_YES ==
898 GNUNET_CONTAINER_multihashmap_put (h->peers,
899 &h->me.hashPubKey, pr,
900 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
901 if (NULL != h->connects)
902 h->connects (h->cls, &h->me, NULL);
904 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
905 if (msize < sizeof (struct ConnectNotifyMessage))
911 cnm = (const struct ConnectNotifyMessage *) msg;
912 ats_count = ntohl (cnm->ats_count);
914 sizeof (struct ConnectNotifyMessage) +
915 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
916 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
917 ntohl ((&cnm->ats)[ats_count].type)))
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 "Received notification about connection from `%s'.\n",
926 GNUNET_i2s (&cnm->peer));
928 if (0 == memcmp (&h->me, &cnm->peer, sizeof (struct GNUNET_PeerIdentity)))
930 /* connect to self!? */
934 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &cnm->peer.hashPubKey);
941 pr = GNUNET_malloc (sizeof (struct PeerRecord));
942 pr->peer = cnm->peer;
944 GNUNET_assert (GNUNET_YES ==
945 GNUNET_CONTAINER_multihashmap_put (h->peers,
946 &cnm->peer.hashPubKey, pr,
947 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
948 if (NULL != h->connects)
949 h->connects (h->cls, &cnm->peer, &cnm->ats);
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, &dnm->peer, 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, &dnm->peer.hashPubKey);
978 trigger = ((pr->prev != NULL) || (pr->next != NULL) ||
979 (h->ready_peer_head == pr));
980 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
982 trigger_next_request (h, GNUNET_NO);
984 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
985 if (NULL == h->status_events)
990 if (msize < sizeof (struct PeerStatusNotifyMessage))
996 psnm = (const struct PeerStatusNotifyMessage *) msg;
997 if (0 == memcmp (&h->me, &psnm->peer, sizeof (struct GNUNET_PeerIdentity)))
1003 ats_count = ntohl (psnm->ats_count);
1005 sizeof (struct PeerStatusNotifyMessage) +
1006 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1007 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
1008 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, &psnm->peer.hashPubKey);
1023 reconnect_later (h);
1026 h->status_events (h->cls, &psnm->peer, psnm->bandwidth_in,
1027 psnm->bandwidth_out,
1028 GNUNET_TIME_absolute_ntoh (psnm->timeout), &psnm->ats);
1030 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1031 if (msize < sizeof (struct NotifyTrafficMessage))
1034 reconnect_later (h);
1037 ntm = (const struct NotifyTrafficMessage *) msg;
1039 ats_count = ntohl (ntm->ats_count);
1041 sizeof (struct NotifyTrafficMessage) +
1042 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
1043 sizeof (struct GNUNET_MessageHeader)) ||
1044 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
1045 ntohl ((&ntm->ats)[ats_count].type)))
1048 reconnect_later (h);
1051 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count + 1];
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "Received message of type %u and size %u from peer `%4s'\n",
1055 ntohs (em->type), ntohs (em->size), GNUNET_i2s (&ntm->peer));
1057 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &ntm->peer.hashPubKey);
1061 reconnect_later (h);
1064 if ((GNUNET_NO == h->inbound_hdr_only) &&
1066 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1067 +ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)))
1070 reconnect_later (h);
1073 et = ntohs (em->type);
1074 for (hpos = 0; hpos < h->hcnt; hpos++)
1076 mh = &h->handlers[hpos];
1079 if ((mh->expected_size != ntohs (em->size)) && (mh->expected_size != 0))
1085 h->handlers[hpos].callback (h->cls, &ntm->peer, em, &ntm->ats))
1087 /* error in processing, do not process other messages! */
1091 if (NULL != h->inbound_notify)
1092 h->inbound_notify (h->cls, &ntm->peer, em, &ntm->ats);
1094 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1095 if (msize < sizeof (struct NotifyTrafficMessage))
1098 reconnect_later (h);
1101 ntm = (const struct NotifyTrafficMessage *) msg;
1102 if (0 == memcmp (&h->me, &ntm->peer, sizeof (struct GNUNET_PeerIdentity)))
1108 ats_count = ntohl (ntm->ats_count);
1110 sizeof (struct NotifyTrafficMessage) +
1111 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information) +
1112 sizeof (struct GNUNET_MessageHeader)) ||
1113 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR !=
1114 ntohl ((&ntm->ats)[ats_count].type)))
1117 reconnect_later (h);
1120 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count + 1];
1121 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &ntm->peer.hashPubKey);
1125 reconnect_later (h);
1129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130 "Received notification about transmission to `%s'.\n",
1131 GNUNET_i2s (&ntm->peer));
1133 if ((GNUNET_NO == h->outbound_hdr_only) &&
1135 ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1136 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)))
1139 reconnect_later (h);
1142 if (NULL == h->outbound_notify)
1147 h->outbound_notify (h->cls, &ntm->peer, em, &ntm->ats);
1149 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1150 if (msize != sizeof (struct SendMessageReady))
1153 reconnect_later (h);
1156 smr = (const struct SendMessageReady *) msg;
1157 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &smr->peer.hashPubKey);
1161 reconnect_later (h);
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1166 "Received notification about transmission readiness to `%s'.\n",
1167 GNUNET_i2s (&smr->peer));
1169 if (pr->pending_head == NULL)
1171 /* request must have been cancelled between the original request
1172 * and the response from core, ignore core's readiness */
1176 th = pr->pending_head;
1177 if (ntohs (smr->smr_id) != th->smr_id)
1179 /* READY message is for expired or cancelled message,
1180 * ignore! (we should have already sent another request) */
1183 if ((pr->prev != NULL) || (pr->next != NULL) || (h->ready_peer_head == pr))
1185 /* we should not already be on the ready list... */
1187 reconnect_later (h);
1190 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head, h->ready_peer_tail, pr);
1191 trigger_next_request (h, GNUNET_NO);
1193 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1194 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1197 reconnect_later (h);
1200 cim = (const struct ConfigurationInfoMessage *) msg;
1201 if (0 == memcmp (&h->me, &cim->peer, sizeof (struct GNUNET_PeerIdentity)))
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Received notification about configuration update for `%s' with RIM %u.\n",
1210 GNUNET_i2s (&cim->peer), (unsigned int) ntohl (cim->rim_id));
1212 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &cim->peer.hashPubKey);
1216 reconnect_later (h);
1219 if (pr->rim_id != ntohl (cim->rim_id))
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Reservation ID mismatch in notification...\n");
1229 GNUNET_free_non_null (pr->pcic_ptr);
1230 pr->pcic_ptr = NULL;
1232 pcic (pr->pcic_cls, &pr->peer, ntohl (cim->reserved_amount),
1233 GNUNET_TIME_relative_ntoh (cim->reserve_delay));
1236 reconnect_later (h);
1239 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1240 GNUNET_TIME_UNIT_FOREVER_REL);
1245 * Task executed once we are done transmitting the INIT message.
1246 * Starts our 'receive' loop.
1248 * @param cls the 'struct GNUNET_CORE_Handle'
1249 * @param success were we successful
1252 init_done_task (void *cls, int success)
1254 struct GNUNET_CORE_Handle *h = cls;
1256 if (success == GNUNET_SYSERR)
1257 return; /* shutdown */
1258 if (success == GNUNET_NO)
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Failed to exchange INIT with core, retrying\n");
1264 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1265 reconnect_later (h);
1268 GNUNET_CLIENT_receive (h->client, &main_notify_handler, h,
1269 GNUNET_TIME_UNIT_FOREVER_REL);
1274 * Our current client connection went down. Clean it up
1275 * and try to reconnect!
1277 * @param h our handle to the core service
1280 reconnect (struct GNUNET_CORE_Handle *h)
1282 struct ControlMessage *cm;
1283 struct InitMessage *init;
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting to CORE service\n");
1292 GNUNET_assert (h->client == NULL);
1293 GNUNET_assert (h->currently_down == GNUNET_YES);
1294 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1295 if (h->client == NULL)
1297 reconnect_later (h);
1300 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1301 cm = GNUNET_malloc (sizeof (struct ControlMessage) + msize);
1302 cm->cont = &init_done_task;
1304 init = (struct InitMessage *) &cm[1];
1305 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1306 init->header.size = htons (msize);
1307 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1308 if (h->status_events != NULL)
1309 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1310 if (h->inbound_notify != NULL)
1312 if (h->inbound_hdr_only)
1313 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1315 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1317 if (h->outbound_notify != NULL)
1319 if (h->outbound_hdr_only)
1320 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1322 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1324 init->options = htonl (opt);
1325 ts = (uint16_t *) & init[1];
1326 for (hpos = 0; hpos < h->hcnt; hpos++)
1327 ts[hpos] = htons (h->handlers[hpos].type);
1328 GNUNET_CONTAINER_DLL_insert (h->control_pending_head, h->control_pending_tail,
1330 trigger_next_request (h, GNUNET_YES);
1336 * Connect to the core service. Note that the connection may
1337 * complete (or fail) asynchronously.
1339 * @param cfg configuration to use
1340 * @param queue_size size of the per-peer message queue
1341 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1342 * @param init callback to call on timeout or once we have successfully
1343 * connected to the core service; note that timeout is only meaningful if init is not NULL
1344 * @param connects function to call on peer connect, can be NULL
1345 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1346 * @param status_events function to call on changes to peer connection status, can be NULL
1347 * @param inbound_notify function to call for all inbound messages, can be NULL
1348 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1349 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1350 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1351 * @param outbound_notify function to call for all outbound messages, can be NULL
1352 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1353 * GNUNET_MessageHeader and hence we do not need to give it the full message
1354 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1355 * @param handlers callbacks for messages we care about, NULL-terminated
1356 * @return handle to the core service (only useful for disconnect until 'init' is called);
1357 * NULL on error (in this case, init is never called)
1359 struct GNUNET_CORE_Handle *
1360 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1361 unsigned int queue_size, void *cls,
1362 GNUNET_CORE_StartupCallback init,
1363 GNUNET_CORE_ConnectEventHandler connects,
1364 GNUNET_CORE_DisconnectEventHandler disconnects,
1365 GNUNET_CORE_PeerStatusEventHandler status_events,
1366 GNUNET_CORE_MessageCallback inbound_notify,
1367 int inbound_hdr_only,
1368 GNUNET_CORE_MessageCallback outbound_notify,
1369 int outbound_hdr_only,
1370 const struct GNUNET_CORE_MessageHandler *handlers)
1372 struct GNUNET_CORE_Handle *h;
1374 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1376 h->queue_size = queue_size;
1379 h->connects = connects;
1380 h->disconnects = disconnects;
1381 h->status_events = status_events;
1382 h->inbound_notify = inbound_notify;
1383 h->outbound_notify = outbound_notify;
1384 h->inbound_hdr_only = inbound_hdr_only;
1385 h->outbound_hdr_only = outbound_hdr_only;
1386 h->handlers = handlers;
1388 h->currently_down = GNUNET_YES;
1389 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1390 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1391 if (NULL != handlers)
1392 while (handlers[h->hcnt].callback != NULL)
1394 GNUNET_assert (h->hcnt <
1395 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1396 sizeof (struct InitMessage)) / sizeof (uint16_t));
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CORE service\n");
1406 * Disconnect from the core service. This function can only
1407 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1408 * requests have been explicitly canceled.
1410 * @param handle connection to core to disconnect
1413 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1415 struct ControlMessage *cm;
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from CORE service\n");
1420 if (handle->cth != NULL)
1422 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1425 while (NULL != (cm = handle->control_pending_head))
1427 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1428 handle->control_pending_tail, cm);
1431 if (cm->cont != NULL)
1432 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1435 if (handle->client != NULL)
1437 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1438 handle->client = NULL;
1440 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1441 &disconnect_and_free_peer_entry,
1443 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1445 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1446 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1448 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1449 handle->peers = NULL;
1450 GNUNET_break (handle->ready_peer_head == NULL);
1451 GNUNET_free (handle);
1456 * Task that calls 'request_next_transmission'.
1458 * @param cls the 'struct PeerRecord*'
1459 * @param tc scheduler context
1462 run_request_next_transmission (void *cls,
1463 const struct GNUNET_SCHEDULER_TaskContext *tc)
1465 struct PeerRecord *pr = cls;
1467 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1468 request_next_transmission (pr);
1473 * Ask the core to call "notify" once it is ready to transmit the
1474 * given number of bytes to the specified "target". Must only be
1475 * called after a connection to the respective peer has been
1476 * established (and the client has been informed about this).
1478 * @param handle connection to core service
1479 * @param cork is corking allowed for this transmission?
1480 * @param priority how important is the message?
1481 * @param maxdelay how long can the message wait?
1482 * @param target who should receive the message,
1483 * use NULL for this peer (loopback)
1484 * @param notify_size how many bytes of buffer space does notify want?
1485 * @param notify function to call when buffer space is available
1486 * @param notify_cls closure for notify
1487 * @return non-NULL if the notify callback was queued,
1488 * NULL if we can not even queue the request (insufficient
1489 * memory); if NULL is returned, "notify" will NOT be called.
1491 struct GNUNET_CORE_TransmitHandle *
1492 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle, int cork,
1494 struct GNUNET_TIME_Relative maxdelay,
1495 const struct GNUNET_PeerIdentity *target,
1497 GNUNET_CONNECTION_TransmitReadyNotify notify,
1500 struct PeerRecord *pr;
1501 struct GNUNET_CORE_TransmitHandle *th;
1502 struct GNUNET_CORE_TransmitHandle *pos;
1503 struct GNUNET_CORE_TransmitHandle *prev;
1504 struct GNUNET_CORE_TransmitHandle *minp;
1506 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers, &target->hashPubKey);
1509 /* attempt to send to peer that is not connected */
1510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1511 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1512 GNUNET_i2s (target), GNUNET_h2s (&handle->me.hashPubKey));
1516 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1517 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1518 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1520 GNUNET_assert (NULL != notify);
1521 th->get_message = notify;
1522 th->get_message_cls = notify_cls;
1523 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1524 th->priority = priority;
1525 th->msize = notify_size;
1527 /* bound queue size */
1528 if (pr->queue_size == handle->queue_size)
1530 /* find lowest-priority entry, but skip the head of the list */
1531 minp = pr->pending_head->next;
1533 while (prev != NULL)
1535 if (prev->priority < minp->priority)
1541 GNUNET_break (handle->queue_size != 0);
1542 GNUNET_break (pr->queue_size == 1);
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1546 "Dropping transmission request: cannot drop queue head and limit is one\n");
1550 if (priority <= minp->priority)
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "Dropping transmission request: priority too low\n");
1557 return NULL; /* priority too low */
1559 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, minp);
1561 GNUNET_assert (0 == minp->get_message (minp->get_message_cls, 0, NULL));
1565 /* Order entries by deadline, but SKIP 'HEAD' if
1566 * we're in the 'ready_peer_*' DLL */
1567 pos = pr->pending_head;
1568 if ((pr->prev != NULL) || (pr->next != NULL) ||
1569 (pr == handle->ready_peer_head))
1571 GNUNET_assert (pos != NULL);
1572 pos = pos->next; /* skip head */
1575 /* insertion sort */
1577 while ((pos != NULL) && (pos->timeout.abs_value < th->timeout.abs_value))
1582 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head, pr->pending_tail, prev,
1585 /* was the request queue previously empty? */
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission request added to queue\n");
1589 if ((pr->pending_head == th) && (pr->ntr_task == GNUNET_SCHEDULER_NO_TASK) &&
1590 (pr->next == NULL) && (pr->prev == NULL) &&
1591 (handle->ready_peer_head != pr))
1593 GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1599 * Cancel the specified transmission-ready notification.
1601 * @param th handle that was returned by "notify_transmit_ready".
1604 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1606 struct PeerRecord *pr = th->peer;
1607 struct GNUNET_CORE_Handle *h = pr->ch;
1610 was_head = (pr->pending_head == th);
1611 GNUNET_CONTAINER_DLL_remove (pr->pending_head, pr->pending_tail, th);
1615 /* we're currently in the control queue, remove */
1616 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1617 h->control_pending_tail, th->cm);
1618 GNUNET_free (th->cm);
1623 if ((pr->prev != NULL) || (pr->next != NULL) || (pr == h->ready_peer_head))
1625 /* the request that was 'approved' by core was
1626 * canceled before it could be transmitted; remove
1627 * us from the 'ready' list */
1628 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head, h->ready_peer_tail, pr);
1630 request_next_transmission (pr);
1635 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1638 struct GNUNET_CORE_InformationRequestContext
1642 * Our connection to the service.
1644 struct GNUNET_CORE_Handle *h;
1647 * Link to control message, NULL if CM was sent.
1649 struct ControlMessage *cm;
1652 * Link to peer record.
1654 struct PeerRecord *pr;
1659 * CM was sent, remove link so we don't double-free.
1661 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1662 * @param success were we successful?
1665 change_preference_send_continuation (void *cls, int success)
1667 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1674 * Obtain statistics and/or change preferences for the given peer.
1676 * @param h core handle
1677 * @param peer identifies the peer
1678 * @param amount reserve N bytes for receiving, negative
1679 * amounts can be used to undo a (recent) reservation;
1680 * @param preference increase incoming traffic share preference by this amount;
1681 * in the absence of "amount" reservations, we use this
1682 * preference value to assign proportional bandwidth shares
1683 * to all connected peers
1684 * @param info function to call with the resulting configuration information
1685 * @param info_cls closure for info
1686 * @return NULL on error
1688 struct GNUNET_CORE_InformationRequestContext *
1689 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1690 const struct GNUNET_PeerIdentity *peer,
1691 int32_t amount, uint64_t preference,
1692 GNUNET_CORE_PeerConfigurationInfoCallback
1693 info, void *info_cls)
1695 struct GNUNET_CORE_InformationRequestContext *irc;
1696 struct PeerRecord *pr;
1697 struct RequestInfoMessage *rim;
1698 struct ControlMessage *cm;
1700 pr = GNUNET_CONTAINER_multihashmap_get (h->peers, &peer->hashPubKey);
1703 /* attempt to change preference on peer that is not connected */
1707 if (pr->pcic != NULL)
1709 /* second change before first one is done */
1713 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1716 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1717 sizeof (struct RequestInfoMessage));
1718 cm->cont = &change_preference_send_continuation;
1721 rim = (struct RequestInfoMessage *) &cm[1];
1722 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1723 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1724 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1725 rim->limit_outbound = GNUNET_BANDWIDTH_VALUE_MAX; // FIXME: remove entirely soon...
1726 rim->reserve_inbound = htonl (amount);
1727 rim->preference_change = GNUNET_htonll (preference);
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "Queueing CHANGE PREFERENCE request for peer `%s' with RIM %u\n",
1732 GNUNET_i2s (peer), (unsigned int) pr->rim_id);
1734 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1735 h->control_pending_tail, cm);
1737 pr->pcic_cls = info_cls;
1738 pr->pcic_ptr = irc; /* for free'ing irc */
1739 if (NULL != h->client)
1740 trigger_next_request (h, GNUNET_NO);
1746 * Cancel request for getting information about a peer.
1747 * Note that an eventual change in preference, trust or bandwidth
1748 * assignment MAY have already been committed at the time,
1749 * so cancelling a request is NOT sure to undo the original
1750 * request. The original request may or may not still commit.
1751 * The only thing cancellation ensures is that the callback
1752 * from the original request will no longer be called.
1754 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1757 GNUNET_CORE_peer_change_preference_cancel (struct
1758 GNUNET_CORE_InformationRequestContext
1761 struct GNUNET_CORE_Handle *h = irc->h;
1762 struct PeerRecord *pr = irc->pr;
1764 GNUNET_assert (pr->pcic_ptr == irc);
1765 if (irc->cm != NULL)
1767 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1768 h->control_pending_tail, irc->cm);
1769 GNUNET_free (irc->cm);
1772 pr->pcic_cls = NULL;
1773 pr->pcic_ptr = NULL;
1778 /* end of core_api.c */