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 while (NULL != (cm = h->control_pending_head))
493 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
494 h->control_pending_tail,
498 if (cm->cont != NULL)
499 cm->cont (cm->cont_cls, GNUNET_NO);
502 if (h->client != NULL)
504 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
507 GNUNET_CONTAINER_multihashmap_iterate (h->peers,
508 &disconnect_and_free_peer_entry,
511 while (NULL != (pr = h->ready_peer_head))
512 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
515 h->currently_down = GNUNET_YES;
516 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
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 request_next_transmission (pr);
643 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
649 * Transmit the next message to the core service.
652 transmit_message (void *cls,
656 struct GNUNET_CORE_Handle *h = cls;
657 struct ControlMessage *cm;
658 struct GNUNET_CORE_TransmitHandle *th;
659 struct PeerRecord *pr;
660 struct SendMessage *sm;
661 const struct GNUNET_MessageHeader *hdr;
665 GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671 "Transmission failed, initiating reconnect\n");
676 /* first check for control messages */
677 if (NULL != (cm = h->control_pending_head))
679 hdr = (const struct GNUNET_MessageHeader*) &cm[1];
680 msize = ntohs (hdr->size);
683 trigger_next_request (h, GNUNET_NO);
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Transmitting control message with %u bytes of type %u to core.\n",
689 (unsigned int) msize,
690 (unsigned int) ntohs (hdr->type));
692 memcpy (buf, hdr, msize);
693 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
694 h->control_pending_tail,
698 if (NULL != cm->cont)
699 cm->cont (cm->cont_cls, GNUNET_OK);
701 trigger_next_request (h, GNUNET_NO);
704 /* now check for 'ready' P2P messages */
705 if (NULL != (pr = h->ready_peer_head))
707 GNUNET_assert (pr->pending_head != NULL);
708 th = pr->pending_head;
709 if (size < th->msize + sizeof (struct SendMessage))
711 trigger_next_request (h, GNUNET_NO);
714 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
717 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
721 if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
723 GNUNET_SCHEDULER_cancel (pr->timeout_task);
724 pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Transmitting SEND request to `%s' with %u bytes.\n",
729 GNUNET_i2s (&pr->peer),
730 (unsigned int) th->msize);
732 sm = (struct SendMessage *) buf;
733 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
734 sm->priority = htonl (th->priority);
735 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
737 sm->cork = htonl ((uint32_t) th->cork);
738 sm->reserved = htonl (0);
739 ret = th->get_message (th->get_message_cls,
740 size - sizeof (struct SendMessage),
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Transmitting SEND request to `%s' yielded %u bytes.\n",
746 GNUNET_i2s (&pr->peer),
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "Size of clients message to peer %s is 0!\n",
755 GNUNET_i2s(&pr->peer));
757 /* client decided to send nothing! */
758 request_next_transmission (pr);
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
763 "Produced SEND message to core with %u bytes payload\n",
766 GNUNET_assert (ret >= sizeof (struct GNUNET_MessageHeader));
767 if (ret + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
770 request_next_transmission (pr);
773 ret += sizeof (struct SendMessage);
774 sm->header.size = htons (ret);
775 GNUNET_assert (ret <= size);
776 request_next_transmission (pr);
784 * Check the list of pending requests, send the next
787 * @param h core handle
788 * @param ignore_currently_down transmit message even if not initialized?
791 trigger_next_request (struct GNUNET_CORE_Handle *h,
792 int ignore_currently_down)
796 if ( (GNUNET_YES == h->currently_down) &&
797 (ignore_currently_down == GNUNET_NO) )
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Core connection down, not processing queue\n");
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Request pending, not processing queue\n");
813 if (h->control_pending_head != NULL)
814 msize = ntohs (((struct GNUNET_MessageHeader*) &h->control_pending_head[1])->size);
815 else if (h->ready_peer_head != NULL)
816 msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Request queue empty, not processing queue\n");
823 return; /* no pending message */
825 h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
827 GNUNET_TIME_UNIT_FOREVER_REL,
829 &transmit_message, h);
834 * Handler for notification messages received from the core.
836 * @param cls our "struct GNUNET_CORE_Handle"
837 * @param msg the message received from the core service
840 main_notify_handler (void *cls,
841 const struct GNUNET_MessageHeader *msg)
843 struct GNUNET_CORE_Handle *h = cls;
844 const struct InitReplyMessage *m;
845 const struct ConnectNotifyMessage *cnm;
846 const struct DisconnectNotifyMessage *dnm;
847 const struct NotifyTrafficMessage *ntm;
848 const struct GNUNET_MessageHeader *em;
849 const struct ConfigurationInfoMessage *cim;
850 const struct PeerStatusNotifyMessage *psnm;
851 const struct SendMessageReady *smr;
852 const struct GNUNET_CORE_MessageHandler *mh;
853 GNUNET_CORE_StartupCallback init;
854 GNUNET_CORE_PeerConfigurationInfoCallback pcic;
855 struct PeerRecord *pr;
856 struct GNUNET_CORE_TransmitHandle *th;
865 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
867 ("Client was disconnected from core service, trying to reconnect.\n"));
871 msize = ntohs (msg->size);
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Processing message of type %u and size %u from core service\n",
875 ntohs (msg->type), msize);
877 switch (ntohs (msg->type))
879 case GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY:
880 if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
886 m = (const struct InitReplyMessage *) msg;
887 GNUNET_break (0 == ntohl (m->reserved));
888 /* start our message processing loop */
889 if (GNUNET_YES == h->currently_down)
891 h->currently_down = GNUNET_NO;
892 trigger_next_request (h, GNUNET_NO);
894 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
895 GNUNET_CRYPTO_hash (&m->publicKey,
897 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
899 if (NULL != (init = h->init))
901 /* mark so we don't call init on reconnect */
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "Connected to core service of peer `%s'.\n",
906 GNUNET_i2s (&h->me));
908 init (h->cls, h, &h->me, &m->publicKey);
913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914 "Successfully reconnected to core service.\n");
917 /* fake 'connect to self' */
918 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
920 GNUNET_assert (pr == NULL);
921 pr = GNUNET_malloc (sizeof (struct PeerRecord));
924 GNUNET_assert (GNUNET_YES ==
925 GNUNET_CONTAINER_multihashmap_put (h->peers,
928 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
929 if (NULL != h->connects)
934 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
935 if (msize < sizeof (struct ConnectNotifyMessage))
941 cnm = (const struct ConnectNotifyMessage *) msg;
942 ats_count = ntohl (cnm->ats_count);
943 if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
944 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Received notification about connection from `%s'.\n",
953 GNUNET_i2s (&cnm->peer));
955 if (0 == memcmp (&h->me,
957 sizeof (struct GNUNET_PeerIdentity)))
959 /* connect to self!? */
963 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
964 &cnm->peer.hashPubKey);
971 pr = GNUNET_malloc (sizeof (struct PeerRecord));
972 pr->peer = cnm->peer;
974 GNUNET_assert (GNUNET_YES ==
975 GNUNET_CONTAINER_multihashmap_put (h->peers,
976 &cnm->peer.hashPubKey,
978 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
979 if (NULL != h->connects)
984 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
985 if (msize != sizeof (struct DisconnectNotifyMessage))
991 dnm = (const struct DisconnectNotifyMessage *) msg;
992 if (0 == memcmp (&h->me,
994 sizeof (struct GNUNET_PeerIdentity)))
996 /* connection to self!? */
1000 GNUNET_break (0 == ntohl (dnm->reserved));
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Received notification about disconnect from `%s'.\n",
1004 GNUNET_i2s (&dnm->peer));
1006 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1007 &dnm->peer.hashPubKey);
1011 reconnect_later (h);
1014 trigger = ( (pr->prev != NULL) ||
1015 (pr->next != NULL) ||
1016 (h->ready_peer_head == pr) );
1017 disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
1019 trigger_next_request (h, GNUNET_NO);
1021 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
1022 if (NULL == h->status_events)
1027 if (msize < sizeof (struct PeerStatusNotifyMessage))
1030 reconnect_later (h);
1033 psnm = (const struct PeerStatusNotifyMessage *) msg;
1034 if (0 == memcmp (&h->me,
1036 sizeof (struct GNUNET_PeerIdentity)))
1042 ats_count = ntohl (psnm->ats_count);
1043 if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
1044 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
1047 reconnect_later (h);
1051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1052 "Received notification about status change by `%s'.\n",
1053 GNUNET_i2s (&psnm->peer));
1055 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1056 &psnm->peer.hashPubKey);
1060 reconnect_later (h);
1063 h->status_events (h->cls,
1066 psnm->bandwidth_out,
1067 GNUNET_TIME_absolute_ntoh (psnm->timeout),
1070 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
1071 if (msize < sizeof (struct NotifyTrafficMessage))
1074 reconnect_later (h);
1077 ntm = (const struct NotifyTrafficMessage *) msg;
1079 ats_count = ntohl (ntm->ats_count);
1080 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1081 + sizeof (struct GNUNET_MessageHeader)) ||
1082 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1085 reconnect_later (h);
1088 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1091 "Received message of type %u and size %u from peer `%4s'\n",
1094 GNUNET_i2s (&ntm->peer));
1096 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1097 &ntm->peer.hashPubKey);
1101 reconnect_later (h);
1104 if ((GNUNET_NO == h->inbound_hdr_only) &&
1105 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) +
1106 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1109 reconnect_later (h);
1112 et = ntohs (em->type);
1113 for (hpos = 0; hpos < h->hcnt; hpos++)
1115 mh = &h->handlers[hpos];
1118 if ((mh->expected_size != ntohs (em->size)) &&
1119 (mh->expected_size != 0))
1125 h->handlers[hpos].callback (h->cls, &ntm->peer, em,
1128 /* error in processing, do not process other messages! */
1132 if (NULL != h->inbound_notify)
1133 h->inbound_notify (h->cls, &ntm->peer, em,
1136 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
1137 if (msize < sizeof (struct NotifyTrafficMessage))
1140 reconnect_later (h);
1143 ntm = (const struct NotifyTrafficMessage *) msg;
1144 if (0 == memcmp (&h->me,
1146 sizeof (struct GNUNET_PeerIdentity)))
1152 ats_count = ntohl (ntm->ats_count);
1153 if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
1154 + sizeof (struct GNUNET_MessageHeader)) ||
1155 (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
1158 reconnect_later (h);
1161 em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
1162 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1163 &ntm->peer.hashPubKey);
1167 reconnect_later (h);
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172 "Received notification about transmission to `%s'.\n",
1173 GNUNET_i2s (&ntm->peer));
1175 if ((GNUNET_NO == h->outbound_hdr_only) &&
1176 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)
1177 + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
1180 reconnect_later (h);
1183 if (NULL == h->outbound_notify)
1188 h->outbound_notify (h->cls, &ntm->peer, em,
1191 case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
1192 if (msize != sizeof (struct SendMessageReady))
1195 reconnect_later (h);
1198 smr = (const struct SendMessageReady *) msg;
1199 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1200 &smr->peer.hashPubKey);
1204 reconnect_later (h);
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Received notification about transmission readiness to `%s'.\n",
1210 GNUNET_i2s (&smr->peer));
1212 if (pr->pending_head == NULL)
1214 /* request must have been cancelled between the original request
1215 and the response from core, ignore core's readiness */
1219 th = pr->pending_head;
1220 if (ntohs (smr->smr_id) != th->smr_id)
1222 /* READY message is for expired or cancelled message,
1223 ignore! (we should have already sent another request) */
1226 if ( (pr->prev != NULL) ||
1227 (pr->next != NULL) ||
1228 (h->ready_peer_head == pr) )
1230 /* we should not already be on the ready list... */
1232 reconnect_later (h);
1235 GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
1238 trigger_next_request (h, GNUNET_NO);
1240 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
1241 if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
1244 reconnect_later (h);
1247 cim = (const struct ConfigurationInfoMessage*) msg;
1248 if (0 == memcmp (&h->me,
1250 sizeof (struct GNUNET_PeerIdentity)))
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Received notification about configuration update for `%s' with RIM %u.\n",
1259 GNUNET_i2s (&cim->peer),
1260 (unsigned int) ntohl (cim->rim_id));
1262 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1263 &cim->peer.hashPubKey);
1267 reconnect_later (h);
1270 if (pr->rim_id != ntohl (cim->rim_id))
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274 "Reservation ID mismatch in notification...\n");
1280 GNUNET_free_non_null (pr->pcic_ptr);
1281 pr->pcic_ptr = NULL;
1286 ntohl (cim->reserved_amount),
1287 GNUNET_TIME_relative_ntoh (cim->reserve_delay),
1288 GNUNET_ntohll (cim->preference));
1291 reconnect_later (h);
1294 GNUNET_CLIENT_receive (h->client,
1295 &main_notify_handler, h,
1296 GNUNET_TIME_UNIT_FOREVER_REL);
1301 * Task executed once we are done transmitting the INIT message.
1302 * Starts our 'receive' loop.
1304 * @param cls the 'struct GNUNET_CORE_Handle'
1305 * @param success were we successful
1308 init_done_task (void *cls,
1311 struct GNUNET_CORE_Handle *h = cls;
1313 if (success == GNUNET_SYSERR)
1314 return; /* shutdown */
1315 if (success == GNUNET_NO)
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Failed to exchange INIT with core, retrying\n");
1321 if (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK)
1322 reconnect_later (h);
1325 GNUNET_CLIENT_receive (h->client,
1326 &main_notify_handler,
1328 GNUNET_TIME_UNIT_FOREVER_REL);
1333 * Our current client connection went down. Clean it up
1334 * and try to reconnect!
1336 * @param h our handle to the core service
1339 reconnect (struct GNUNET_CORE_Handle *h)
1341 struct ControlMessage *cm;
1342 struct InitMessage *init;
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 "Reconnecting to CORE service\n");
1352 GNUNET_assert (h->client == NULL);
1353 GNUNET_assert (h->currently_down == GNUNET_YES);
1354 h->client = GNUNET_CLIENT_connect ("core", h->cfg);
1355 if (h->client == NULL)
1357 reconnect_later (h);
1360 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
1361 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1363 cm->cont = &init_done_task;
1365 init = (struct InitMessage*) &cm[1];
1366 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
1367 init->header.size = htons (msize);
1368 opt = GNUNET_CORE_OPTION_SEND_CONNECT | GNUNET_CORE_OPTION_SEND_DISCONNECT;
1369 if (h->status_events != NULL)
1370 opt |= GNUNET_CORE_OPTION_SEND_STATUS_CHANGE;
1371 if (h->inbound_notify != NULL)
1373 if (h->inbound_hdr_only)
1374 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
1376 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
1378 if (h->outbound_notify != NULL)
1380 if (h->outbound_hdr_only)
1381 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
1383 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
1385 init->options = htonl (opt);
1386 ts = (uint16_t *) &init[1];
1387 for (hpos = 0; hpos < h->hcnt; hpos++)
1388 ts[hpos] = htons (h->handlers[hpos].type);
1389 GNUNET_CONTAINER_DLL_insert (h->control_pending_head,
1390 h->control_pending_tail,
1392 trigger_next_request (h, GNUNET_YES);
1398 * Connect to the core service. Note that the connection may
1399 * complete (or fail) asynchronously.
1401 * @param cfg configuration to use
1402 * @param queue_size size of the per-peer message queue
1403 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
1404 * @param init callback to call on timeout or once we have successfully
1405 * connected to the core service; note that timeout is only meaningful if init is not NULL
1406 * @param connects function to call on peer connect, can be NULL
1407 * @param disconnects function to call on peer disconnect / timeout, can be NULL
1408 * @param status_events function to call on changes to peer connection status, can be NULL
1409 * @param inbound_notify function to call for all inbound messages, can be NULL
1410 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
1411 * GNUNET_MessageHeader and hence we do not need to give it the full message;
1412 * can be used to improve efficiency, ignored if inbound_notify is NULLL
1413 * @param outbound_notify function to call for all outbound messages, can be NULL
1414 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
1415 * GNUNET_MessageHeader and hence we do not need to give it the full message
1416 * can be used to improve efficiency, ignored if outbound_notify is NULLL
1417 * @param handlers callbacks for messages we care about, NULL-terminated
1418 * @return handle to the core service (only useful for disconnect until 'init' is called);
1419 * NULL on error (in this case, init is never called)
1421 struct GNUNET_CORE_Handle *
1422 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1423 unsigned int queue_size,
1425 GNUNET_CORE_StartupCallback init,
1426 GNUNET_CORE_ConnectEventHandler connects,
1427 GNUNET_CORE_DisconnectEventHandler disconnects,
1428 GNUNET_CORE_PeerStatusEventHandler status_events,
1429 GNUNET_CORE_MessageCallback inbound_notify,
1430 int inbound_hdr_only,
1431 GNUNET_CORE_MessageCallback outbound_notify,
1432 int outbound_hdr_only,
1433 const struct GNUNET_CORE_MessageHandler *handlers)
1435 struct GNUNET_CORE_Handle *h;
1437 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
1439 h->queue_size = queue_size;
1442 h->connects = connects;
1443 h->disconnects = disconnects;
1444 h->status_events = status_events;
1445 h->inbound_notify = inbound_notify;
1446 h->outbound_notify = outbound_notify;
1447 h->inbound_hdr_only = inbound_hdr_only;
1448 h->outbound_hdr_only = outbound_hdr_only;
1449 h->handlers = handlers;
1451 h->currently_down = GNUNET_YES;
1452 h->peers = GNUNET_CONTAINER_multihashmap_create (128);
1453 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
1454 while (handlers[h->hcnt].callback != NULL)
1456 GNUNET_assert (h->hcnt <
1457 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
1458 sizeof (struct InitMessage)) / sizeof (uint16_t));
1460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1461 "Connecting to CORE service\n");
1469 * Disconnect from the core service. This function can only
1470 * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
1471 * requests have been explicitly canceled.
1473 * @param handle connection to core to disconnect
1476 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
1478 struct ControlMessage *cm;
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1482 "Disconnecting from CORE service\n");
1484 if (handle->cth != NULL)
1486 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
1489 while (NULL != (cm = handle->control_pending_head))
1491 GNUNET_CONTAINER_DLL_remove (handle->control_pending_head,
1492 handle->control_pending_tail,
1496 if (cm->cont != NULL)
1497 cm->cont (cm->cont_cls, GNUNET_SYSERR);
1500 if (handle->client != NULL)
1502 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
1503 handle->client = NULL;
1505 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
1507 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
1508 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
1510 GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
1511 &disconnect_and_free_peer_entry,
1513 GNUNET_CONTAINER_multihashmap_destroy (handle->peers);
1514 GNUNET_break (handle->ready_peer_head == NULL);
1515 GNUNET_free (handle);
1520 * Task that calls 'request_next_transmission'.
1522 * @param cls the 'struct PeerRecord*'
1523 * @param tc scheduler context
1526 run_request_next_transmission (void *cls,
1527 const struct GNUNET_SCHEDULER_TaskContext *tc)
1529 struct PeerRecord *pr = cls;
1531 pr->ntr_task = GNUNET_SCHEDULER_NO_TASK;
1532 request_next_transmission (pr);
1537 * Ask the core to call "notify" once it is ready to transmit the
1538 * given number of bytes to the specified "target". Must only be
1539 * called after a connection to the respective peer has been
1540 * established (and the client has been informed about this).
1542 * @param handle connection to core service
1543 * @param cork is corking allowed for this transmission?
1544 * @param priority how important is the message?
1545 * @param maxdelay how long can the message wait?
1546 * @param target who should receive the message,
1547 * use NULL for this peer (loopback)
1548 * @param notify_size how many bytes of buffer space does notify want?
1549 * @param notify function to call when buffer space is available
1550 * @param notify_cls closure for notify
1551 * @return non-NULL if the notify callback was queued,
1552 * NULL if we can not even queue the request (insufficient
1553 * memory); if NULL is returned, "notify" will NOT be called.
1555 struct GNUNET_CORE_TransmitHandle *
1556 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1559 struct GNUNET_TIME_Relative maxdelay,
1560 const struct GNUNET_PeerIdentity *target,
1562 GNUNET_CONNECTION_TransmitReadyNotify notify,
1565 struct PeerRecord *pr;
1566 struct GNUNET_CORE_TransmitHandle *th;
1567 struct GNUNET_CORE_TransmitHandle *pos;
1568 struct GNUNET_CORE_TransmitHandle *prev;
1569 struct GNUNET_CORE_TransmitHandle *minp;
1571 pr = GNUNET_CONTAINER_multihashmap_get (handle->peers,
1572 &target->hashPubKey);
1575 /* attempt to send to peer that is not connected */
1576 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1577 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
1578 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
1582 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1583 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1584 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1586 GNUNET_assert(NULL != notify);
1587 th->get_message = notify;
1588 th->get_message_cls = notify_cls;
1589 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1590 th->priority = priority;
1591 th->msize = notify_size;
1593 /* bound queue size */
1594 if (pr->queue_size == handle->queue_size)
1596 /* find lowest-priority entry, but skip the head of the list */
1597 minp = pr->pending_head->next;
1599 while (prev != NULL)
1601 if (prev->priority < minp->priority)
1607 GNUNET_break (handle->queue_size != 0);
1608 GNUNET_break (pr->queue_size == 1);
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Dropping transmission request: cannot drop queue head and limit is one\n");
1616 if (priority <= minp->priority)
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 "Dropping transmission request: priority too low\n");
1623 return NULL; /* priority too low */
1625 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1630 minp->get_message (minp->get_message_cls,
1635 /* Order entries by deadline, but SKIP 'HEAD' if
1636 we're in the 'ready_peer_*' DLL */
1637 pos = pr->pending_head;
1638 if ( (pr->prev != NULL) ||
1639 (pr->next != NULL) ||
1640 (pr == handle->ready_peer_head) )
1642 GNUNET_assert (pos != NULL);
1643 pos = pos->next; /* skip head */
1646 /* insertion sort */
1648 while ( (pos != NULL) &&
1649 (pos->timeout.abs_value < th->timeout.abs_value) )
1654 GNUNET_CONTAINER_DLL_insert_after (pr->pending_head,
1659 /* was the request queue previously empty? */
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 "Transmission request added to queue\n");
1664 if ( (pr->pending_head == th) &&
1665 (pr->ntr_task == GNUNET_SCHEDULER_NO_TASK) &&
1666 (pr->next == NULL) &&
1667 (pr->prev == NULL) &&
1668 (handle->ready_peer_head != pr) )
1669 pr->ntr_task = GNUNET_SCHEDULER_add_now (&run_request_next_transmission, pr);
1675 * Cancel the specified transmission-ready notification.
1677 * @param th handle that was returned by "notify_transmit_ready".
1680 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1683 struct PeerRecord *pr = th->peer;
1684 struct GNUNET_CORE_Handle *h = pr->ch;
1687 was_head = (pr->pending_head == th);
1688 GNUNET_CONTAINER_DLL_remove (pr->pending_head,
1694 /* we're currently in the control queue, remove */
1695 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1696 h->control_pending_tail,
1698 GNUNET_free (th->cm);
1703 if ( (pr->prev != NULL) ||
1704 (pr->next != NULL) ||
1705 (pr == h->ready_peer_head) )
1707 /* the request that was 'approved' by core was
1708 canceled before it could be transmitted; remove
1709 us from the 'ready' list */
1710 GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
1714 request_next_transmission (pr);
1719 /* ****************** GNUNET_CORE_peer_request_connect ******************** */
1722 * Handle for a request to the core to connect to
1723 * a particular peer. Can be used to cancel the request
1724 * (before the 'cont'inuation is called).
1726 struct GNUNET_CORE_PeerRequestHandle
1730 * Link to control message.
1732 struct ControlMessage *cm;
1737 struct GNUNET_CORE_Handle *h;
1740 * Continuation to run when done.
1742 GNUNET_CORE_ControlContinuation cont;
1745 * Closure for 'cont'.
1753 * Continuation called when the control message was transmitted.
1754 * Calls the original continuation and frees the remaining
1757 * @param cls the 'struct GNUNET_CORE_PeerRequestHandle'
1758 * @param success was the request transmitted?
1761 peer_request_connect_cont (void *cls,
1764 struct GNUNET_CORE_PeerRequestHandle *ret = cls;
1766 if (ret->cont != NULL)
1767 ret->cont (ret->cont_cls, success);
1773 * Request that the core should try to connect to a particular peer.
1774 * Once the request has been transmitted to the core, the continuation
1775 * function will be called. Note that this does NOT mean that a
1776 * connection was successfully established -- it only means that the
1777 * core will now try. Successful establishment of the connection
1778 * will be signalled to the 'connects' callback argument of
1779 * 'GNUNET_CORE_connect' only. If the core service does not respond
1780 * to our connection attempt within the given time frame, 'cont' will
1781 * be called with the TIMEOUT reason code.
1783 * @param h core handle
1784 * @param timeout how long to try to talk to core
1785 * @param peer who should we connect to
1786 * @param cont function to call once the request has been completed (or timed out)
1787 * @param cont_cls closure for cont
1789 * @return NULL on error or already connected,
1790 * otherwise handle for cancellation
1792 struct GNUNET_CORE_PeerRequestHandle *
1793 GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
1794 struct GNUNET_TIME_Relative timeout,
1795 const struct GNUNET_PeerIdentity * peer,
1796 GNUNET_CORE_ControlContinuation cont,
1799 struct GNUNET_CORE_PeerRequestHandle *ret;
1800 struct ControlMessage *cm;
1801 struct ConnectMessage *msg;
1803 if (NULL != GNUNET_CONTAINER_multihashmap_get (h->peers,
1807 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1808 "Peers are already connected!\n");
1813 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1814 sizeof (struct ConnectMessage));
1815 msg = (struct ConnectMessage*) &cm[1];
1816 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT);
1817 msg->header.size = htons (sizeof (struct ConnectMessage));
1818 msg->reserved = htonl (0);
1819 msg->timeout = GNUNET_TIME_relative_hton (timeout);
1821 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1822 h->control_pending_tail,
1824 ret = GNUNET_malloc (sizeof (struct GNUNET_CORE_PeerRequestHandle));
1828 ret->cont_cls = cont_cls;
1829 cm->cont = &peer_request_connect_cont;
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Queueing REQUEST_CONNECT request\n");
1835 trigger_next_request (h, GNUNET_NO);
1841 * Cancel a pending request to connect to a particular peer. Must not
1842 * be called after the 'cont' function was invoked.
1844 * @param req request handle that was returned for the original request
1847 GNUNET_CORE_peer_request_connect_cancel (struct GNUNET_CORE_PeerRequestHandle *req)
1849 struct GNUNET_CORE_Handle *h = req->h;
1850 struct ControlMessage *cm = req->cm;
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1854 "A CHANGE PREFERENCE request was cancelled!\n");
1856 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
1857 h->control_pending_tail,
1864 /* ****************** GNUNET_CORE_peer_change_preference ******************** */
1867 struct GNUNET_CORE_InformationRequestContext
1871 * Our connection to the service.
1873 struct GNUNET_CORE_Handle *h;
1876 * Link to control message, NULL if CM was sent.
1878 struct ControlMessage *cm;
1881 * Link to peer record.
1883 struct PeerRecord *pr;
1888 * CM was sent, remove link so we don't double-free.
1890 * @param cls the 'struct GNUNET_CORE_InformationRequestContext'
1891 * @param success were we successful?
1894 change_preference_send_continuation (void *cls,
1897 struct GNUNET_CORE_InformationRequestContext *irc = cls;
1904 * Obtain statistics and/or change preferences for the given peer.
1906 * @param h core handle
1907 * @param peer identifies the peer
1908 * @param timeout after how long should we give up (and call "info" with NULL
1909 * for "peer" to signal an error)?
1910 * @param bw_out set to the current bandwidth limit (sending) for this peer,
1911 * caller should set "bw_out" to "-1" to avoid changing
1912 * the current value; otherwise "bw_out" will be lowered to
1913 * the specified value; passing a pointer to "0" can be used to force
1914 * us to disconnect from the peer; "bw_out" might not increase
1915 * as specified since the upper bound is generally
1916 * determined by the other peer!
1917 * @param amount reserve N bytes for receiving, negative
1918 * amounts can be used to undo a (recent) reservation;
1919 * @param preference increase incoming traffic share preference by this amount;
1920 * in the absence of "amount" reservations, we use this
1921 * preference value to assign proportional bandwidth shares
1922 * to all connected peers
1923 * @param info function to call with the resulting configuration information
1924 * @param info_cls closure for info
1925 * @return NULL on error
1927 struct GNUNET_CORE_InformationRequestContext *
1928 GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
1929 const struct GNUNET_PeerIdentity *peer,
1930 struct GNUNET_TIME_Relative timeout,
1931 struct GNUNET_BANDWIDTH_Value32NBO bw_out,
1933 uint64_t preference,
1934 GNUNET_CORE_PeerConfigurationInfoCallback info,
1937 struct GNUNET_CORE_InformationRequestContext *irc;
1938 struct PeerRecord *pr;
1939 struct RequestInfoMessage *rim;
1940 struct ControlMessage *cm;
1942 pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
1946 /* attempt to change preference on peer that is not connected */
1950 if (pr->pcic != NULL)
1952 /* second change before first one is done */
1956 irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
1959 cm = GNUNET_malloc (sizeof (struct ControlMessage) +
1960 sizeof (struct RequestInfoMessage));
1961 cm->cont = &change_preference_send_continuation;
1964 rim = (struct RequestInfoMessage*) &cm[1];
1965 rim->header.size = htons (sizeof (struct RequestInfoMessage));
1966 rim->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO);
1967 rim->rim_id = htonl (pr->rim_id = h->rim_id_gen++);
1968 rim->limit_outbound = bw_out;
1969 rim->reserve_inbound = htonl (amount);
1970 rim->preference_change = GNUNET_htonll(preference);
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1974 "Queueing CHANGE PREFERENCE request for peer `%s' with RIM %u\n",
1976 (unsigned int) pr->rim_id);
1978 GNUNET_CONTAINER_DLL_insert_tail (h->control_pending_head,
1979 h->control_pending_tail,
1982 pr->pcic_cls = info_cls;
1983 pr->pcic_ptr = irc; /* for free'ing irc */
1984 if (NULL != h->client)
1985 trigger_next_request (h, GNUNET_NO);
1991 * Cancel request for getting information about a peer.
1992 * Note that an eventual change in preference, trust or bandwidth
1993 * assignment MAY have already been committed at the time,
1994 * so cancelling a request is NOT sure to undo the original
1995 * request. The original request may or may not still commit.
1996 * The only thing cancellation ensures is that the callback
1997 * from the original request will no longer be called.
1999 * @param irc context returned by the original GNUNET_CORE_peer_get_info call
2002 GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequestContext *irc)
2004 struct GNUNET_CORE_Handle *h = irc->h;
2005 struct PeerRecord *pr = irc->pr;
2007 GNUNET_assert (pr->pcic_ptr == irc);
2008 if (irc->cm != NULL)
2010 GNUNET_CONTAINER_DLL_remove (h->control_pending_head,
2011 h->control_pending_tail,
2013 GNUNET_free (irc->cm);
2016 pr->pcic_cls = NULL;
2017 pr->pcic_ptr = NULL;
2022 /* end of core_api.c */