2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/core_api.c
23 * @brief core service; this is the main API for encrypted P2P
25 * @author Christian Grothoff
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
34 * Information we track for each peer.
40 * We generally do NOT keep peer records in a DLL; this
41 * DLL is only used IF this peer's 'pending_head' message
42 * is ready for transmission.
44 struct PeerRecord *prev;
47 * We generally do NOT keep peer records in a DLL; this
48 * DLL is only used IF this peer's 'pending_head' message
49 * is ready for transmission.
51 struct PeerRecord *next;
54 * Peer the record is about.
56 struct GNUNET_PeerIdentity peer;
59 * Corresponding core handle.
61 struct GNUNET_CORE_Handle *ch;
64 * Head of doubly-linked list of pending requests.
65 * Requests are sorted by deadline *except* for HEAD,
66 * which is only modified upon transmission to core.
68 struct GNUNET_CORE_TransmitHandle *pending_head;
71 * Tail of doubly-linked list of pending requests.
73 struct GNUNET_CORE_TransmitHandle *pending_tail;
76 * Pending callback waiting for peer information, or NULL for none.
78 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
86 * Pointer to free when we call pcic.
91 * Request information ID for the given pcic (needed in case a
92 * request is cancelled after being submitted to core and a new
93 * one is generated; in this case, we need to avoid matching the
94 * reply to the first (cancelled) request to the second request).
99 * ID of timeout task for the 'pending_head' handle
100 * which is the one with the smallest timeout.
102 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
105 * ID of task to run 'next_request_transmission'.
107 GNUNET_SCHEDULER_TaskIdentifier ntr_task;
110 * Current size of the queue of pending requests.
112 unsigned int queue_size;
115 * SendMessageRequest ID generator for this peer.
123 * Entry in a doubly-linked list of control messages to be transmitted
124 * to the core service. Control messages include traffic allocation,
125 * connection requests and of course our initial 'init' request.
127 * The actual message is allocated at the end of this struct.
129 struct ControlMessage
132 * This is a doubly-linked list.
134 struct ControlMessage *next;
137 * This is a doubly-linked list.
139 struct ControlMessage *prev;
142 * Function to run after transmission failed/succeeded.
144 GNUNET_CORE_ControlContinuation cont;
147 * Closure for 'cont'.
152 * Transmit handle (if one is associated with this ControlMessage), or NULL.
154 struct GNUNET_CORE_TransmitHandle *th;
160 * Context for the core service connection.
162 struct GNUNET_CORE_Handle
166 * Configuration we're using.
168 const struct GNUNET_CONFIGURATION_Handle *cfg;
171 * Closure for the various callbacks.
176 * Function to call once we've handshaked with the core service.
178 GNUNET_CORE_StartupCallback init;
181 * Function to call whenever we're notified about a peer connecting.
183 GNUNET_CORE_ConnectEventHandler connects;
186 * Function to call whenever we're notified about a peer disconnecting.
188 GNUNET_CORE_DisconnectEventHandler disconnects;
191 * Function to call whenever we're notified about a peer changing status.
193 GNUNET_CORE_PeerStatusEventHandler status_events;
196 * Function to call whenever we receive an inbound message.
198 GNUNET_CORE_MessageCallback inbound_notify;
201 * Function to call whenever we receive an outbound message.
203 GNUNET_CORE_MessageCallback outbound_notify;
206 * Function handlers for messages of particular type.
208 const struct GNUNET_CORE_MessageHandler *handlers;
211 * Our connection to the service.
213 struct GNUNET_CLIENT_Connection *client;
216 * Handle for our current transmission request.
218 struct GNUNET_CLIENT_TransmitHandle *cth;
221 * Head of doubly-linked list of pending requests.
223 struct ControlMessage *control_pending_head;
226 * Tail of doubly-linked list of pending requests.
228 struct ControlMessage *control_pending_tail;
231 * Head of doubly-linked list of peers that are core-approved
232 * to send their next message.
234 struct PeerRecord *ready_peer_head;
237 * Tail of doubly-linked list of peers that are core-approved
238 * to send their next message.
240 struct PeerRecord *ready_peer_tail;
243 * Hash map listing all of the peers that we are currently
246 struct GNUNET_CONTAINER_MultiHashMap *peers;
249 * Identity of this peer.
251 struct GNUNET_PeerIdentity me;
254 * ID of reconnect task (if any).
256 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
259 * Current delay we use for re-trying to connect to core.
261 struct GNUNET_TIME_Relative retry_backoff;
264 * Request information ID generator.
269 * Number of messages we are allowed to queue per target.
271 unsigned int queue_size;
274 * Number of entries in the handlers array.
279 * For inbound notifications without a specific handler, do
280 * we expect to only receive headers?
282 int inbound_hdr_only;
285 * For outbound notifications without a specific handler, do
286 * we expect to only receive headers?
288 int outbound_hdr_only;
291 * Are we currently disconnected and hence unable to forward
300 * Handle for a transmission request.
302 struct GNUNET_CORE_TransmitHandle
306 * We keep active transmit handles in a doubly-linked list.
308 struct GNUNET_CORE_TransmitHandle *next;
311 * We keep active transmit handles in a doubly-linked list.
313 struct GNUNET_CORE_TransmitHandle *prev;
316 * Corresponding peer record.
318 struct PeerRecord *peer;
321 * Corresponding SEND_REQUEST message. Only non-NULL
322 * while SEND_REQUEST message is pending.
324 struct ControlMessage *cm;
327 * Function that will be called to get the actual request
328 * (once we are ready to transmit this request to the core).
329 * The function will be called with a NULL buffer to signal
332 GNUNET_CONNECTION_TransmitReadyNotify get_message;
335 * Closure for get_message.
337 void *get_message_cls;
340 * Timeout for this handle.
342 struct GNUNET_TIME_Absolute timeout;
345 * How important is this message?
350 * Size of this request.
355 * Send message request ID for this request.
360 * Is corking allowed?
368 * Our current client connection went down. Clean it up
369 * and try to reconnect!
371 * @param h our handle to the core service
374 reconnect (struct GNUNET_CORE_Handle *h);
378 * Task schedule to try to re-connect to core.
380 * @param cls the 'struct GNUNET_CORE_Handle'
381 * @param tc task context
384 reconnect_task (void *cls,
385 const struct GNUNET_SCHEDULER_TaskContext *tc)
387 struct GNUNET_CORE_Handle *h = cls;
389 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Connecting to CORE service after delay\n");
399 * Notify clients about disconnect and free
400 * the entry for connected peer.
402 * @param cls the 'struct GNUNET_CORE_Handle*'
403 * @param key the peer identity (not used)
404 * @param value the 'struct PeerRecord' to free.
405 * @return GNUNET_YES (continue)
408 disconnect_and_free_peer_entry (void *cls,
409 const GNUNET_HashCode *key,
412 static struct GNUNET_BANDWIDTH_Value32NBO zero;
413 struct GNUNET_CORE_Handle *h = cls;
414 struct GNUNET_CORE_TransmitHandle *th;
415 struct PeerRecord *pr = value;
416 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
418 while (NULL != (th = pr->pending_head))
420 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
425 th->get_message (th->get_message_cls,
431 if (NULL != (pcic = pr->pcic))
434 GNUNET_free_non_null (pr->pcic_ptr);
440 GNUNET_TIME_UNIT_FOREVER_REL,
443 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
445 GNUNET_SCHEDULER_cancel (pr->timeout_task);
446 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
448 if (pr->ntr_task != GNUNET_SCHEDULER_NO_TASK)
450 GNUNET_SCHEDULER_cancel (pr->ntr_task);
451 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
453 GNUNET_assert (pr->queue_size == 0);
454 if ( (pr->prev != NULL) ||
455 (pr->next != NULL) ||
456 (h->ready_peer_head == pr) )
457 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
460 if (h->disconnects != NULL)
461 h->disconnects (h->cls,
463 GNUNET_assert (GNUNET_YES ==
464 GNUNET_CONTAINER_multihashmap_remove (h->peers,
467 GNUNET_assert (pr->pending_head == NULL);
468 GNUNET_assert (pr->pending_tail == NULL);
469 GNUNET_assert (pr->ch = h);
470 GNUNET_assert (pr->queue_size == 0);
471 GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
472 GNUNET_assert (pr->ntr_task == GNUNET_SCHEDULER_NO_TASK);
479 * Close down any existing connection to the CORE service and
480 * try re-establishing it later.
482 * @param h our handle
485 reconnect_later (struct GNUNET_CORE_Handle *h)
487 struct ControlMessage *cm;
488 struct PeerRecord *pr;
490 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
491 if (h->client != NULL)
493 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
496 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
497 &disconnect_and_free_peer_entry,
500 while (NULL != (pr = h->ready_peer_head))
501 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
504 h->currently_down = GNUNET_YES;
505 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
508 while (NULL != (cm = h->control_pending_head))
510 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
511 h->control_pending_tail,
515 if (cm->cont != NULL)
516 cm->cont (cm->cont_cls, GNUNET_NO);
519 GNUNET_assert (h->control_pending_head == NULL);
520 h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
522 h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
527 * Check the list of pending requests, send the next
530 * @param h core handle
531 * @param ignore_currently_down transmit message even if not initialized?
534 trigger_next_request (struct GNUNET_CORE_Handle *h,
535 int ignore_currently_down);
539 * The given request hit its timeout. Remove from the
540 * doubly-linked list and call the respective continuation.
542 * @param cls the transmit handle of the request that timed out
543 * @param tc context, can be NULL (!)
546 transmission_timeout (void *cls,
547 const struct GNUNET_SCHEDULER_TaskContext *tc);
551 * Send a control message to the peer asking for transmission
552 * of the message in the given peer record.
554 * @param pr peer to request transmission to
557 request_next_transmission (struct PeerRecord *pr)
559 struct GNUNET_CORE_Handle *h = pr->ch;
560 struct ControlMessage *cm;
561 struct SendMessageRequest *smr;
562 struct GNUNET_CORE_TransmitHandle *th;
564 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
566 GNUNET_SCHEDULER_cancel (pr->timeout_task);
567 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
569 if (NULL == (th = pr->pending_head))
571 trigger_next_request (h, GNUNET_NO);
575 return; /* already done */
576 GNUNET_assert (pr->prev == NULL);
577 GNUNET_assert (pr->next == NULL);
578 pr->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (th->timeout),
579 &transmission_timeout,
581 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
582 sizeof (struct SendMessageRequest));
585 smr = (struct SendMessageRequest*) &cm[1];
586 smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
587 smr->header.size = htons (sizeof (struct SendMessageRequest));
588 smr->priority = htonl (th->priority);
589 smr->deadline = GNUNET_TIME_absolute_hton (th->timeout);
590 smr->peer = pr->peer;
591 smr->queue_size = htonl (pr->queue_size);
592 smr->size = htons (th->msize);
593 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
594 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
595 h->control_pending_tail,
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Adding SEND REQUEST for peer `%s' to message queue\n",
600 GNUNET_i2s (&pr->peer));
602 trigger_next_request (h, GNUNET_NO);
607 * The given request hit its timeout. Remove from the
608 * doubly-linked list and call the respective continuation.
610 * @param cls the transmit handle of the request that timed out
611 * @param tc context, can be NULL (!)
614 transmission_timeout (void *cls,
615 const struct GNUNET_SCHEDULER_TaskContext *tc)
617 struct PeerRecord *pr = cls;
618 struct GNUNET_CORE_Handle *h = pr->ch;
619 struct GNUNET_CORE_TransmitHandle *th;
621 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
622 th = pr->pending_head;
623 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
627 if ( (pr->prev != NULL) ||
628 (pr->next != NULL) ||
629 (pr == h->ready_peer_head) )
631 /* the request that was 'approved' by core was
632 canceled before it could be transmitted; remove
633 us from the 'ready' list */
634 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Signalling timeout of request for transmission to CORE service\n");
642 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
643 request_next_transmission (pr);
648 * Transmit the next message to the core service.
651 transmit_message (void *cls,
655 struct GNUNET_CORE_Handle *h = cls;
656 struct ControlMessage *cm;
657 struct GNUNET_CORE_TransmitHandle *th;
658 struct PeerRecord *pr;
659 struct SendMessage *sm;
660 const struct GNUNET_MessageHeader *hdr;
664 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Transmission failed, initiating reconnect\n");
675 /* first check for control messages */
676 if (NULL != (cm = h->control_pending_head))
678 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
679 msize = ntohs (hdr->size);
682 trigger_next_request (h, GNUNET_NO);
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Transmitting control message with %u bytes of type %u to core.\n",
688 (unsigned int) msize,
689 (unsigned int) ntohs (hdr->type));
691 memcpy (buf, hdr, msize);
692 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
693 h->control_pending_tail,
697 if (NULL != cm->cont)
698 cm->cont (cm->cont_cls, GNUNET_OK);
700 trigger_next_request (h, GNUNET_NO);
703 /* now check for 'ready' P2P messages */
704 if (NULL != (pr = h->ready_peer_head))
706 GNUNET_assert (pr->pending_head != NULL);
707 th = pr->pending_head;
708 if (size < th->msize + sizeof (struct SendMessage))
710 trigger_next_request (h, GNUNET_NO);
713 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
716 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
720 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
722 GNUNET_SCHEDULER_cancel (pr->timeout_task);
723 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Transmitting SEND request to `%s' with %u bytes.\n",
728 GNUNET_i2s (&pr->peer),
729 (unsigned int) th->msize);
731 sm = (struct SendMessage *) buf;
732 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
733 sm->priority = htonl (th->priority);
734 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
736 sm->cork = htonl ((uint32_t) th->cork);
737 sm->reserved = htonl (0);
738 ret = th->get_message (th->get_message_cls,
739 size - sizeof (struct SendMessage),
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "Transmitting SEND request to `%s' yielded %u bytes.\n",
745 GNUNET_i2s (&pr->peer),
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
753 "Size of clients message to peer %s is 0!\n",
754 GNUNET_i2s(&pr->peer));
756 /* client decided to send nothing! */
757 request_next_transmission (pr);
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Produced SEND message to core with %u bytes payload\n",
765 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
766 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
769 request_next_transmission (pr);
772 ret += sizeof (struct SendMessage);
773 sm->header.size = htons (ret);
774 GNUNET_assert (ret <= size);
775 request_next_transmission (pr);
783 * Check the list of pending requests, send the next
786 * @param h core handle
787 * @param ignore_currently_down transmit message even if not initialized?
790 trigger_next_request (struct GNUNET_CORE_Handle *h,
791 int ignore_currently_down)
795 if ( (GNUNET_YES == h->currently_down) &&
796 (ignore_currently_down == GNUNET_NO) )
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Core connection down, not processing queue\n");
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Request pending, not processing queue\n");
812 if (h->control_pending_head != NULL)
813 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
814 else if (h->ready_peer_head != NULL)
815 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 "Request queue empty, not processing queue\n");
822 return; /* no pending message */
824 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
826 GNUNET_TIME_UNIT_FOREVER_REL,
828 &transmit_message, h);
833 * Handler for notification messages received from the core.
835 * @param cls our "struct GNUNET_CORE_Handle"
836 * @param msg the message received from the core service
839 main_notify_handler (void *cls,
840 const struct GNUNET_MessageHeader *msg)
842 struct GNUNET_CORE_Handle *h = cls;
843 const struct InitReplyMessage *m;
844 const struct ConnectNotifyMessage *cnm;
845 const struct DisconnectNotifyMessage *dnm;
846 const struct NotifyTrafficMessage *ntm;
847 const struct GNUNET_MessageHeader *em;
848 const struct ConfigurationInfoMessage *cim;
849 const struct PeerStatusNotifyMessage *psnm;
850 const struct SendMessageReady *smr;
851 const struct GNUNET_CORE_MessageHandler *mh;
852 GNUNET_CORE_StartupCallback init;
853 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
854 struct PeerRecord *pr;
855 struct GNUNET_CORE_TransmitHandle *th;
864 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
866 ("Client was disconnected from core service, trying to reconnect.\n"));
870 msize = ntohs (msg->size);
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Processing message of type %u and size %u from core service\n",
874 ntohs (msg->type), msize);
876 switch (ntohs (msg->type))
878 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
879 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
885 m = (const struct InitReplyMessage *) msg;
886 GNUNET_break (0 == ntohl (m->reserved));
887 /* start our message processing loop */
888 if (GNUNET_YES == h->currently_down)
890 h->currently_down = GNUNET_NO;
891 trigger_next_request (h, GNUNET_NO);
893 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
894 GNUNET_CRYPTO_hash (&m->publicKey,
896 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
898 if (NULL != (init = h->init))
900 /* mark so we don't call init on reconnect */
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Connected to core service of peer `%s'.\n",
905 GNUNET_i2s (&h->me));
907 init (h->cls, h, &h->me, &m->publicKey);
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Successfully reconnected to core service.\n");
916 /* fake 'connect to self' */
917 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
919 GNUNET_assert (pr == NULL);
920 pr = GNUNET_malloc (sizeof (struct PeerRecord));
923 GNUNET_assert (GNUNET_YES ==
924 GNUNET_CONTAINER_multihashmap_put (h->peers,
927 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
928 if (NULL != h->connects)
933 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
934 if (msize < sizeof (struct ConnectNotifyMessage))
940 cnm = (const struct ConnectNotifyMessage *) msg;
941 ats_count = ntohl (cnm->ats_count);
942 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
943 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 "Received notification about connection from `%s'.\n",
952 GNUNET_i2s (&cnm->peer));
954 if (0 == memcmp (&h->me,
956 sizeof (struct GNUNET_PeerIdentity)))
958 /* connect to self!? */
962 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
963 &cnm->peer.hashPubKey);
970 pr = GNUNET_malloc (sizeof (struct PeerRecord));
971 pr->peer = cnm->peer;
973 GNUNET_assert (GNUNET_YES ==
974 GNUNET_CONTAINER_multihashmap_put (h->peers,
975 &cnm->peer.hashPubKey,
977 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
978 if (NULL != h->connects)
983 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
984 if (msize != sizeof (struct DisconnectNotifyMessage))
990 dnm = (const struct DisconnectNotifyMessage *) msg;
991 if (0 == memcmp (&h->me,
993 sizeof (struct GNUNET_PeerIdentity)))
995 /* connection to self!? */
999 GNUNET_break (0 == ntohl (dnm->reserved));
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 "Received notification about disconnect from `%s'.\n",
1003 GNUNET_i2s (&dnm->peer));
1005 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1006 &dnm->peer.hashPubKey);
1010 reconnect_later (h);
1013 trigger = ( (pr->prev != NULL) ||
1014 (pr->next != NULL) ||
1015 (h->ready_peer_head == pr) );
1016 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
1018 trigger_next_request (h, GNUNET_NO);
1020 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
1021 if (NULL == h->status_events)
1026 if (msize < sizeof (struct PeerStatusNotifyMessage))
1029 reconnect_later (h);
1032 psnm = (const struct PeerStatusNotifyMessage *) msg;
1033 if (0 == memcmp (&h->me,
1035 sizeof (struct GNUNET_PeerIdentity)))
1041 ats_count = ntohl (psnm->ats_count);
1042 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1043 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1046 reconnect_later (h);
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Received notification about status change by `%s'.\n",
1052 GNUNET_i2s (&psnm->peer));
1054 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1055 &psnm->peer.hashPubKey);
1059 reconnect_later (h);
1062 h->status_events (h->cls,
1065 psnm->bandwidth_out,
1066 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1069 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1070 if (msize < sizeof (struct NotifyTrafficMessage))
1073 reconnect_later (h);
1076 ntm = (const struct NotifyTrafficMessage *) msg;
1078 ats_count = ntohl (ntm->ats_count);
1079 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1080 + sizeof (struct GNUNET_MessageHeader)) ||
1081 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1084 reconnect_later (h);
1087 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Received message of type %u and size %u from peer `%4s'\n",
1093 GNUNET_i2s (&ntm->peer));
1095 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1096 &ntm->peer.hashPubKey);
1100 reconnect_later (h);
1103 if ((GNUNET_NO == h->inbound_hdr_only) &&
1104 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1105 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1108 reconnect_later (h);
1111 et = ntohs (em->type);
1112 for (hpos = 0; hpos < h->hcnt; hpos++)
1114 mh = &h->handlers[hpos];
1117 if ((mh->expected_size != ntohs (em->size)) &&
1118 (mh->expected_size != 0))
1124 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1127 /* error in processing, do not process other messages! */
1131 if (NULL != h->inbound_notify)
1132 h->inbound_notify (h->cls, &ntm->peer, em,
1135 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1136 if (msize < sizeof (struct NotifyTrafficMessage))
1139 reconnect_later (h);
1142 ntm = (const struct NotifyTrafficMessage *) msg;
1143 if (0 == memcmp (&h->me,
1145 sizeof (struct GNUNET_PeerIdentity)))
1151 ats_count = ntohl (ntm->ats_count);
1152 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1153 + sizeof (struct GNUNET_MessageHeader)) ||
1154 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1157 reconnect_later (h);
1160 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1161 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1162 &ntm->peer.hashPubKey);
1166 reconnect_later (h);
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "Received notification about transmission to `%s'.\n",
1172 GNUNET_i2s (&ntm->peer));
1174 if ((GNUNET_NO == h->outbound_hdr_only) &&
1175 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1176 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1179 reconnect_later (h);
1182 if (NULL == h->outbound_notify)
1187 h->outbound_notify (h->cls, &ntm->peer, em,
1190 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1191 if (msize != sizeof (struct SendMessageReady))
1194 reconnect_later (h);
1197 smr = (const struct SendMessageReady *) msg;
1198 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1199 &smr->peer.hashPubKey);
1203 reconnect_later (h);
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Received notification about transmission readiness to `%s'.\n",
1209 GNUNET_i2s (&smr->peer));
1211 if (pr->pending_head == NULL)
1213 /* request must have been cancelled between the original request
1214 and the response from core, ignore core's readiness */
1218 th = pr->pending_head;
1219 if (ntohs (smr->smr_id) != th->smr_id)
1221 /* READY message is for expired or cancelled message,
1222 ignore! (we should have already sent another request) */
1225 if ( (pr->prev != NULL) ||
1226 (pr->next != NULL) ||
1227 (h->ready_peer_head == pr) )
1229 /* we should not already be on the ready list... */
1231 reconnect_later (h);
1234 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1237 trigger_next_request (h, GNUNET_NO);
1239 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1240 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1243 reconnect_later (h);
1246 cim = (const struct ConfigurationInfoMessage*) msg;
1247 if (0 == memcmp (&h->me,
1249 sizeof (struct GNUNET_PeerIdentity)))
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Received notification about configuration update for `%s' with RIM %u.\n",
1258 GNUNET_i2s (&cim->peer),
1259 (unsigned int) ntohl (cim->rim_id));
1261 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1262 &cim->peer.hashPubKey);
1266 reconnect_later (h);
1269 if (pr->rim_id != ntohl (cim->rim_id))
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Reservation ID mismatch in notification...\n");
1279 GNUNET_free_non_null (pr->pcic_ptr);
1280 pr->pcic_ptr = NULL;
1285 ntohl (cim->reserved_amount),
1286 GNUNET_TIME_relative_ntoh (cim->reserve_delay),
1287 GNUNET_ntohll (cim->preference));
1290 reconnect_later (h);
1293 GNUNET_CLIENT_receive (h->client,
1294 &main_notify_handler, h,
1295 GNUNET_TIME_UNIT_FOREVER_REL);
1300 * Task executed once we are done transmitting the INIT message.
1301 * Starts our 'receive' loop.
1303 * @param cls the 'struct GNUNET_CORE_Handle'
1304 * @param success were we successful
1307 init_done_task (void *cls,
1310 struct GNUNET_CORE_Handle *h = cls;
1312 if (success == GNUNET_SYSERR)
1313 return; /* shutdown */
1314 if (success == GNUNET_NO)
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318 "Failed to exchange INIT with core, retrying\n");
1320 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1321 reconnect_later (h);
1324 GNUNET_CLIENT_receive (h->client,
1325 &main_notify_handler,
1327 GNUNET_TIME_UNIT_FOREVER_REL);
1332 * Our current client connection went down. Clean it up
1333 * and try to reconnect!
1335 * @param h our handle to the core service
1338 reconnect (struct GNUNET_CORE_Handle *h)
1340 struct ControlMessage *cm;
1341 struct InitMessage *init;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Reconnecting to CORE service\n");
1351 GNUNET_assert (h->client == NULL);
1352 GNUNET_assert (h->currently_down == GNUNET_YES);
1353 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1354 if (h->client == NULL)
1356 reconnect_later (h);
1359 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1360 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1362 cm->cont = &init_done_task;
1364 init = (struct InitMessage*) &cm[1];
1365 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1366 init->header.size = htons (msize);
1367 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1368 if (h->status_events != NULL)
1369 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1370 if (h->inbound_notify != NULL)
1372 if (h->inbound_hdr_only)
1373 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1375 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1377 if (h->outbound_notify != NULL)
1379 if (h->outbound_hdr_only)
1380 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1382 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1384 init->options = htonl (opt);
1385 ts = (uint16_t *) &init[1];
1386 for (hpos = 0; hpos < h->hcnt; hpos++)
1387 ts[hpos] = htons (h->handlers[hpos].type);
1388 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1389 h->control_pending_tail,
1391 trigger_next_request (h, GNUNET_YES);
1397 * Connect to the core service. Note that the connection may
1398 * complete (or fail) asynchronously.
1400 * @param cfg configuration to use
1401 * @param queue_size size of the per-peer message queue
1402 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1403 * @param init callback to call on timeout or once we have successfully
1404 * connected to the core service; note that timeout is only meaningful if init is not NULL
1405 * @param connects function to call on peer connect, can be NULL
1406 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1407 * @param status_events function to call on changes to peer connection status, can be NULL
1408 * @param inbound_notify function to call for all inbound messages, can be NULL
1409 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1410 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1411 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1412 * @param outbound_notify function to call for all outbound messages, can be NULL
1413 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1414 * GNUNET_MessageHeader and hence we do not need to give it the full message
1415 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1416 * @param handlers callbacks for messages we care about, NULL-terminated
1417 * @return handle to the core service (only useful for disconnect until 'init' is called);
1418 * NULL on error (in this case, init is never called)
1420 struct GNUNET_CORE_Handle *
1421 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1422 unsigned int queue_size,
1424 GNUNET_CORE_StartupCallback init,
1425 GNUNET_CORE_ConnectEventHandler connects,
1426 GNUNET_CORE_DisconnectEventHandler disconnects,
1427 GNUNET_CORE_PeerStatusEventHandler status_events,
1428 GNUNET_CORE_MessageCallback inbound_notify,
1429 int inbound_hdr_only,
1430 GNUNET_CORE_MessageCallback outbound_notify,
1431 int outbound_hdr_only,
1432 const struct GNUNET_CORE_MessageHandler *handlers)
1434 struct GNUNET_CORE_Handle *h;
1436 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1438 h->queue_size = queue_size;
1441 h->connects = connects;
1442 h->disconnects = disconnects;
1443 h->status_events = status_events;
1444 h->inbound_notify = inbound_notify;
1445 h->outbound_notify = outbound_notify;
1446 h->inbound_hdr_only = inbound_hdr_only;
1447 h->outbound_hdr_only = outbound_hdr_only;
1448 h->handlers = handlers;
1450 h->currently_down = GNUNET_YES;
1451 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1452 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1453 while (handlers[h->hcnt].callback != NULL)
1455 GNUNET_assert (h->hcnt <
1456 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1457 sizeof (struct InitMessage)) / sizeof (uint16_t));
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1460 "Connecting to CORE service\n");
1468 * Disconnect from the core service. This function can only
1469 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1470 * requests have been explicitly canceled.
1472 * @param handle connection to core to disconnect
1475 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1477 struct ControlMessage *cm;
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Disconnecting from CORE service\n");
1483 if (handle->cth != NULL)
1485 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1488 if (handle->client != NULL)
1490 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1491 handle->client = NULL;
1493 while (NULL != (cm = handle->control_pending_head))
1495 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1496 handle->control_pending_tail,
1500 if (cm->cont != NULL)
1501 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1504 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1506 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1507 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1509 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1510 &disconnect_and_free_peer_entry,
1512 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1513 GNUNET_break (handle->ready_peer_head == NULL);
1514 GNUNET_free (handle);
1519 * Task that calls 'request_next_transmission'.
1521 * @param cls the 'struct PeerRecord*'
1522 * @param tc scheduler context
1525 run_request_next_transmission (void *cls,
1526 const struct GNUNET_SCHEDULER_TaskContext *tc)
1528 struct PeerRecord *pr = cls;
1530 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1531 request_next_transmission (pr);
1536 * Ask the core to call "notify" once it is ready to transmit the
1537 * given number of bytes to the specified "target". Must only be
1538 * called after a connection to the respective peer has been
1539 * established (and the client has been informed about this).
1541 * @param handle connection to core service
1542 * @param cork is corking allowed for this transmission?
1543 * @param priority how important is the message?
1544 * @param maxdelay how long can the message wait?
1545 * @param target who should receive the message,
1546 * use NULL for this peer (loopback)
1547 * @param notify_size how many bytes of buffer space does notify want?
1548 * @param notify function to call when buffer space is available
1549 * @param notify_cls closure for notify
1550 * @return non-NULL if the notify callback was queued,
1551 * NULL if we can not even queue the request (insufficient
1552 * memory); if NULL is returned, "notify" will NOT be called.
1554 struct GNUNET_CORE_TransmitHandle *
1555 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1558 struct GNUNET_TIME_Relative maxdelay,
1559 const struct GNUNET_PeerIdentity *target,
1561 GNUNET_CONNECTION_TransmitReadyNotify notify,
1564 struct PeerRecord *pr;
1565 struct GNUNET_CORE_TransmitHandle *th;
1566 struct GNUNET_CORE_TransmitHandle *pos;
1567 struct GNUNET_CORE_TransmitHandle *prev;
1568 struct GNUNET_CORE_TransmitHandle *minp;
1570 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1571 &target->hashPubKey);
1574 /* attempt to send to peer that is not connected */
1575 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1576 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1577 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1581 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1582 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1583 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1585 GNUNET_assert(NULL != notify);
1586 th->get_message = notify;
1587 th->get_message_cls = notify_cls;
1588 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1589 th->priority = priority;
1590 th->msize = notify_size;
1592 /* bound queue size */
1593 if (pr->queue_size == handle->queue_size)
1595 /* find lowest-priority entry, but skip the head of the list */
1596 minp = pr->pending_head->next;
1598 while (prev != NULL)
1600 if (prev->priority < minp->priority)
1606 GNUNET_break (handle->queue_size != 0);
1607 GNUNET_break (pr->queue_size == 1);
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "Dropping transmission request: cannot drop queue head and limit is one\n");
1615 if (priority <= minp->priority)
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1619 "Dropping transmission request: priority too low\n");
1622 return NULL; /* priority too low */
1624 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1629 minp->get_message (minp->get_message_cls,
1634 /* Order entries by deadline, but SKIP 'HEAD' if
1635 we're in the 'ready_peer_*' DLL */
1636 pos = pr->pending_head;
1637 if ( (pr->prev != NULL) ||
1638 (pr->next != NULL) ||
1639 (pr == handle->ready_peer_head) )
1641 GNUNET_assert (pos != NULL);
1642 pos = pos->next; /* skip head */
1645 /* insertion sort */
1647 while ( (pos != NULL) &&
1648 (pos->timeout.abs_value < th->timeout.abs_value) )
1653 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1658 /* was the request queue previously empty? */
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "Transmission request added to queue\n");
1663 if ( (pr->pending_head == th) &&
1664 (pr->ntr_task == GNUNET_SCHEDULER_NO_TASK) )
1665 pr->ntr_task = GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1671 * Cancel the specified transmission-ready notification.
1673 * @param th handle that was returned by "notify_transmit_ready".
1676 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1679 struct PeerRecord *pr = th->peer;
1680 struct GNUNET_CORE_Handle *h = pr->ch;
1683 was_head = (pr->pending_head == th);
1684 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1690 /* we're currently in the control queue, remove */
1691 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1692 h->control_pending_tail,
1694 GNUNET_free (th->cm);
1699 if ( (pr->prev != NULL) ||
1700 (pr->next != NULL) ||
1701 (pr == h->ready_peer_head) )
1703 /* the request that was 'approved' by core was
1704 canceled before it could be transmitted; remove
1705 us from the 'ready' list */
1706 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1710 request_next_transmission (pr);
1715 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1718 * Handle for a request to the core to connect to
1719 * a particular peer. Can be used to cancel the request
1720 * (before the 'cont'inuation is called).
1722 struct GNUNET_CORE_PeerRequestHandle
1726 * Link to control message.
1728 struct ControlMessage *cm;
1733 struct GNUNET_CORE_Handle *h;
1736 * Continuation to run when done.
1738 GNUNET_CORE_ControlContinuation cont;
1741 * Closure for 'cont'.
1749 * Continuation called when the control message was transmitted.
1750 * Calls the original continuation and frees the remaining
1753 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1754 * @param success was the request transmitted?
1757 peer_request_connect_cont (void *cls,
1760 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1762 if (ret->cont != NULL)
1763 ret->cont (ret->cont_cls, success);
1769 * Request that the core should try to connect to a particular peer.
1770 * Once the request has been transmitted to the core, the continuation
1771 * function will be called. Note that this does NOT mean that a
1772 * connection was successfully established -- it only means that the
1773 * core will now try. Successful establishment of the connection
1774 * will be signalled to the 'connects' callback argument of
1775 * 'GNUNET_CORE_connect' only. If the core service does not respond
1776 * to our connection attempt within the given time frame, 'cont' will
1777 * be called with the TIMEOUT reason code.
1779 * @param h core handle
1780 * @param timeout how long to try to talk to core
1781 * @param peer who should we connect to
1782 * @param cont function to call once the request has been completed (or timed out)
1783 * @param cont_cls closure for cont
1785 * @return NULL on error or already connected,
1786 * otherwise handle for cancellation
1788 struct GNUNET_CORE_PeerRequestHandle *
1789 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1790 struct GNUNET_TIME_Relative timeout,
1791 const struct GNUNET_PeerIdentity * peer,
1792 GNUNET_CORE_ControlContinuation cont,
1795 struct GNUNET_CORE_PeerRequestHandle *ret;
1796 struct ControlMessage *cm;
1797 struct ConnectMessage *msg;
1799 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1803 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1804 "Peers are already connected!\n");
1809 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1810 sizeof (struct ConnectMessage));
1811 msg = (struct ConnectMessage*) &cm[1];
1812 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1813 msg->header.size = htons (sizeof (struct ConnectMessage));
1814 msg->reserved = htonl (0);
1815 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1817 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1818 h->control_pending_tail,
1820 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1824 ret->cont_cls = cont_cls;
1825 cm->cont = &peer_request_connect_cont;
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "Queueing REQUEST_CONNECT request\n");
1831 trigger_next_request (h, GNUNET_NO);
1837 * Cancel a pending request to connect to a particular peer. Must not
1838 * be called after the 'cont' function was invoked.
1840 * @param req request handle that was returned for the original request
1843 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1845 struct GNUNET_CORE_Handle *h = req->h;
1846 struct ControlMessage *cm = req->cm;
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "A CHANGE PREFERENCE request was cancelled!\n");
1852 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1853 h->control_pending_tail,
1860 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1863 struct GNUNET_CORE_InformationRequestContext
1867 * Our connection to the service.
1869 struct GNUNET_CORE_Handle *h;
1872 * Link to control message, NULL if CM was sent.
1874 struct ControlMessage *cm;
1877 * Link to peer record.
1879 struct PeerRecord *pr;
1884 * CM was sent, remove link so we don't double-free.
1886 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1887 * @param success were we successful?
1890 change_preference_send_continuation (void *cls,
1893 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1900 * Obtain statistics and/or change preferences for the given peer.
1902 * @param h core handle
1903 * @param peer identifies the peer
1904 * @param timeout after how long should we give up (and call "info" with NULL
1905 * for "peer" to signal an error)?
1906 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1907 * caller should set "bw_out" to "-1" to avoid changing
1908 * the current value; otherwise "bw_out" will be lowered to
1909 * the specified value; passing a pointer to "0" can be used to force
1910 * us to disconnect from the peer; "bw_out" might not increase
1911 * as specified since the upper bound is generally
1912 * determined by the other peer!
1913 * @param amount reserve N bytes for receiving, negative
1914 * amounts can be used to undo a (recent) reservation;
1915 * @param preference increase incoming traffic share preference by this amount;
1916 * in the absence of "amount" reservations, we use this
1917 * preference value to assign proportional bandwidth shares
1918 * to all connected peers
1919 * @param info function to call with the resulting configuration information
1920 * @param info_cls closure for info
1921 * @return NULL on error
1923 struct GNUNET_CORE_InformationRequestContext *
1924 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1925 const struct GNUNET_PeerIdentity *peer,
1926 struct GNUNET_TIME_Relative timeout,
1927 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1929 uint64_t preference,
1930 GNUNET_CORE_PeerConfigurationInfoCallback info,
1933 struct GNUNET_CORE_InformationRequestContext *irc;
1934 struct PeerRecord *pr;
1935 struct RequestInfoMessage *rim;
1936 struct ControlMessage *cm;
1938 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1942 /* attempt to change preference on peer that is not connected */
1946 if (pr->pcic != NULL)
1948 /* second change before first one is done */
1952 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1955 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1956 sizeof (struct RequestInfoMessage));
1957 cm->cont = &change_preference_send_continuation;
1960 rim = (struct RequestInfoMessage*) &cm[1];
1961 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1962 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1963 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1964 rim->limit_outbound = bw_out;
1965 rim->reserve_inbound = htonl (amount);
1966 rim->preference_change = GNUNET_htonll(preference);
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "Queueing CHANGE PREFERENCE request for peer `%s' with RIM %u\n",
1972 (unsigned int) pr->rim_id);
1974 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1975 h->control_pending_tail,
1978 pr->pcic_cls = info_cls;
1979 pr->pcic_ptr = irc; /* for free'ing irc */
1980 if (NULL != h->client)
1981 trigger_next_request (h, GNUNET_NO);
1987 * Cancel request for getting information about a peer.
1988 * Note that an eventual change in preference, trust or bandwidth
1989 * assignment MAY have already been committed at the time,
1990 * so cancelling a request is NOT sure to undo the original
1991 * request. The original request may or may not still commit.
1992 * The only thing cancellation ensures is that the callback
1993 * from the original request will no longer be called.
1995 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
1998 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
2000 struct GNUNET_CORE_Handle *h = irc->h;
2001 struct PeerRecord *pr = irc->pr;
2003 GNUNET_assert (pr->pcic_ptr == irc);
2004 if (irc->cm != NULL)
2006 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
2007 h->control_pending_tail,
2009 GNUNET_free (irc->cm);
2012 pr->pcic_cls = NULL;
2013 pr->pcic_ptr = NULL;
2018 /* end of core_api.c */