2 This file is part of GNUnet.
3 (C) 2009 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 2, 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_core_service.h"
33 * Context for the core service connection.
35 struct GNUNET_CORE_Handle
41 struct GNUNET_SCHEDULER_Handle *sched;
44 * Configuration we're using.
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 * Closure for the various callbacks.
54 * Function to call once we've handshaked with the core service.
56 GNUNET_CORE_StartupCallback init;
59 * Function to call whenever we're notified about a peer connecting.
61 GNUNET_CORE_ClientEventHandler connects;
64 * Function to call whenever we're notified about a peer disconnecting.
66 GNUNET_CORE_ClientEventHandler disconnects;
69 * Function to call whenever we're asked to generate traffic
70 * (data provided to be transmitted back to the service).
72 GNUNET_CORE_BufferFillCallback bfc;
75 * Function to call whenever we receive an inbound message.
77 GNUNET_CORE_MessageCallback inbound_notify;
80 * Function to call whenever we receive an outbound message.
82 GNUNET_CORE_MessageCallback outbound_notify;
85 * Function handlers for messages of particular type.
87 const struct GNUNET_CORE_MessageHandler *handlers;
90 * Our connection to the service.
92 struct GNUNET_CLIENT_Connection *client;
95 * Handle for our current transmission request.
97 struct GNUNET_CLIENT_TransmitHandle *th;
100 * Head of doubly-linked list of pending requests.
102 struct GNUNET_CORE_TransmitHandle *pending_head;
105 * Tail of doubly-linked list of pending requests.
107 struct GNUNET_CORE_TransmitHandle *pending_tail;
110 * Currently submitted request (or NULL)
112 struct GNUNET_CORE_TransmitHandle *submitted;
115 * Currently submitted request based on solicitation (or NULL)
117 struct GNUNET_CORE_TransmitHandle *solicit_transmit_req;
120 * Buffer where we store a message for transmission in response
121 * to a traffic solicitation (or NULL).
123 char *solicit_buffer;
126 * How long to wait until we time out the connection attempt?
128 struct GNUNET_TIME_Absolute startup_timeout;
131 * ID of reconnect task (if any).
133 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
136 * Number of entries in the handlers array.
141 * For inbound notifications without a specific handler, do
142 * we expect to only receive headers?
144 int inbound_hdr_only;
147 * For outbound notifications without a specific handler, do
148 * we expect to only receive headers?
150 int outbound_hdr_only;
153 * Are we currently disconnected and hence unable to forward
161 * Handle for a transmission request.
163 struct GNUNET_CORE_TransmitHandle
167 * We keep active transmit handles in a doubly-linked list.
169 struct GNUNET_CORE_TransmitHandle *next;
172 * We keep active transmit handles in a doubly-linked list.
174 struct GNUNET_CORE_TransmitHandle *prev;
177 * Corresponding core handle.
179 struct GNUNET_CORE_Handle *ch;
182 * Function that will be called to get the actual request
183 * (once we are ready to transmit this request to the core).
184 * The function will be called with a NULL buffer to signal
187 GNUNET_CONNECTION_TransmitReadyNotify get_message;
190 * Closure for get_message.
192 void *get_message_cls;
195 * If this entry is for a configuration request, pointer
196 * to the information callback; otherwise NULL.
198 GNUNET_CORE_PeerConfigurationInfoCallback info;
206 * If this entry is for a transmission request, pointer
207 * to the notify callback; otherwise NULL.
209 GNUNET_CONNECTION_TransmitReadyNotify notify;
212 * Closure for notify.
217 * Peer the request is about.
219 struct GNUNET_PeerIdentity peer;
222 * Timeout for this handle.
224 struct GNUNET_TIME_Absolute timeout;
227 * ID of timeout task.
229 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
232 * How important is this message?
237 * Size of this request.
246 * Function called when we are ready to transmit our
247 * "START" message (or when this operation timed out).
250 * @param size number of bytes available in buf
251 * @param buf where the callee should write the message
252 * @return number of bytes written to buf
254 static size_t transmit_start (void *cls, size_t size, void *buf);
258 * Our current client connection went down. Clean it up
259 * and try to reconnect!
261 * @param h our handle to the core service
264 reconnect (struct GNUNET_CORE_Handle *h)
266 GNUNET_CLIENT_disconnect (h->client);
267 h->currently_down = GNUNET_YES;
268 h->client = GNUNET_CLIENT_connect (h->sched, "core", h->cfg);
269 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
270 sizeof (struct InitMessage) +
271 sizeof (uint16_t) * h->hcnt,
272 GNUNET_TIME_UNIT_SECONDS,
279 * The given request hit its timeout. Remove from the
280 * doubly-linked list and call the respective continuation.
282 * @param cls the transmit handle of the request that timed out
283 * @param tc context, can be NULL (!)
286 timeout_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 struct GNUNET_CORE_TransmitHandle *th = cls;
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Transmission request timed out.\n");
292 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
293 GNUNET_assert (0 == th->get_message (th->get_message_cls, 0, NULL));
294 GNUNET_CORE_notify_transmit_ready_cancel (th);
299 * Function called when we are ready to transmit a request from our
300 * request list (or when this operation timed out).
303 * @param size number of bytes available in buf
304 * @param buf where the callee should write the message
305 * @return number of bytes written to buf
308 request_start (void *cls, size_t size, void *buf)
310 struct GNUNET_CORE_Handle *h = cls;
311 struct GNUNET_CORE_TransmitHandle *th;
315 th = h->pending_head;
318 timeout_request (th, NULL);
321 /* create new timeout task (in case core takes too long to respond!) */
322 th->timeout_task = GNUNET_SCHEDULER_add_delayed (h->sched,
323 GNUNET_TIME_absolute_get_remaining
325 &timeout_request, th);
326 /* remove th from doubly-linked pending list, move to submitted */
327 GNUNET_assert (th->prev == NULL);
328 h->pending_head = th->next;
329 if (th->next == NULL)
330 h->pending_tail = NULL;
332 th->next->prev = NULL;
333 GNUNET_assert (h->submitted == NULL);
335 GNUNET_assert (size >= th->msize);
336 ret = th->get_message (th->get_message_cls, size, buf);
337 GNUNET_assert (ret <= size);
343 * Check the list of pending requests, send the next
347 trigger_next_request (struct GNUNET_CORE_Handle *h)
349 struct GNUNET_CORE_TransmitHandle *th;
350 if (h->currently_down)
351 return; /* connection temporarily down */
352 if (NULL == (th = h->pending_head))
353 return; /* no requests pending */
354 GNUNET_assert (NULL == h->th);
355 GNUNET_SCHEDULER_cancel (h->sched, th->timeout_task);
356 th->timeout_task = GNUNET_SCHEDULER_NO_TASK;
357 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
359 GNUNET_TIME_absolute_get_remaining
368 * cls is a pointer to a 32 bit number followed by that
369 * amount of data. If possible, copy to buf and return
370 * number of bytes copied. Always free the buffer.
373 copy_and_free (void *cls, size_t size, void *buf)
375 struct GNUNET_CORE_Handle *h = cls;
376 char *cbuf = h->solicit_buffer;
379 h->solicit_transmit_req = NULL;
380 h->solicit_buffer = NULL;
381 memcpy (&have, cbuf, sizeof (uint32_t));
384 /* timeout / error case */
388 memcpy (buf, cbuf + sizeof (uint32_t), have);
395 * Call bfc callback to solicit traffic for the given peer.
397 * @param h our handle to the core service
398 * @param peer peer for which traffic is solicited
399 * @param amount number of bytes that are being solicited
402 solicit_traffic (struct GNUNET_CORE_Handle *h,
403 const struct GNUNET_PeerIdentity *peer, uint32_t amount)
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
410 "Core solicited %u bytes of traffic for `%s'!\n",
413 if (NULL != h->solicit_transmit_req)
415 /* more than one solicitation pending */
419 have = h->bfc (h->cls, peer, buf, amount);
422 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
423 "Can not help with traffic solicitation for `%s'!\n",
427 GNUNET_assert (have >= sizeof (struct GNUNET_MessageHeader));
428 cbuf = GNUNET_malloc (have + sizeof (uint32_t));
429 memcpy (cbuf, &have, sizeof (uint32_t));
430 memcpy (cbuf + sizeof (uint32_t), buf, have);
431 h->solicit_buffer = cbuf;
432 h->solicit_transmit_req
433 = GNUNET_CORE_notify_transmit_ready (h,
435 GNUNET_TIME_UNIT_SECONDS,
436 peer, have, ©_and_free, h);
437 if (h->solicit_transmit_req == NULL)
439 /* this should not happen */
442 h->solicit_buffer = NULL;
448 * Handler for most messages received from the core.
450 * @param cls our "struct GNUNET_CORE_Handle"
451 * @param msg the message received from the core service
454 main_handler (void *cls, const struct GNUNET_MessageHeader *msg)
456 struct GNUNET_CORE_Handle *h = cls;
458 const struct ConnectNotifyMessage *cnm;
459 const struct NotifyTrafficMessage *ntm;
460 const struct ConfigurationInfoMessage *cim;
461 const struct SolicitTrafficMessage *stm;
462 const struct GNUNET_MessageHeader *em;
466 const struct GNUNET_CORE_MessageHandler *mh;
470 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
472 ("Client was disconnected from core service, trying to reconnect.\n"));
476 msize = ntohs (msg->size);
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Processing message of type %u and size %u from core service\n",
480 ntohs (msg->type), msize);
482 switch (ntohs (msg->type))
484 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
485 if (NULL == h->connects)
490 if (msize != sizeof (struct ConnectNotifyMessage))
495 cnm = (const struct ConnectNotifyMessage *) msg;
499 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
500 if (NULL == h->disconnects)
505 if (msize != sizeof (struct ConnectNotifyMessage))
510 cnm = (const struct ConnectNotifyMessage *) msg;
511 h->disconnects (h->cls,
514 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
516 sizeof (struct NotifyTrafficMessage) +
517 sizeof (struct GNUNET_MessageHeader))
522 ntm = (const struct NotifyTrafficMessage *) msg;
523 em = (const struct GNUNET_MessageHeader *) &ntm[1];
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Received message of type %u from peer `%4s'\n",
527 ntohs (em->type), GNUNET_i2s (&ntm->peer));
529 if ((GNUNET_NO == h->inbound_hdr_only) &&
530 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
535 et = ntohs (em->type);
536 for (hpos = 0; hpos < h->hcnt; hpos++)
538 mh = &h->handlers[hpos];
541 if ((mh->expected_size != ntohs (em->size)) &&
542 (mh->expected_size != 0))
548 h->handlers[hpos].callback (h->cls, &ntm->peer, em))
550 /* error in processing, disconnect ! */
555 if (NULL != h->inbound_notify)
556 h->inbound_notify (h->cls, &ntm->peer, em);
558 case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
560 sizeof (struct NotifyTrafficMessage) +
561 sizeof (struct GNUNET_MessageHeader))
566 ntm = (const struct NotifyTrafficMessage *) msg;
567 em = (const struct GNUNET_MessageHeader *) &ntm[1];
568 if ((GNUNET_NO == h->outbound_hdr_only) &&
569 (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
574 if (NULL == h->outbound_notify)
579 h->outbound_notify (h->cls, &ntm->peer, em);
581 case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
582 if (msize != sizeof (struct ConfigurationInfoMessage))
587 if (NULL == h->submitted)
589 cim = (const struct ConfigurationInfoMessage *) msg;
591 /* process configuration data */
592 if (h->submitted->info != NULL)
593 h->submitted->info (h->submitted->info_cls,
596 ntohl (cim->bpm_out),
597 GNUNET_TIME_relative_ntoh (cim->latency),
598 (int) ntohl (cim->reserved_amount),
600 /* done, clean up! */
601 GNUNET_CORE_notify_transmit_ready_cancel (h->submitted); // HUH?
602 trigger_next_request (h);
604 case GNUNET_MESSAGE_TYPE_CORE_SOLICIT_TRAFFIC:
605 if (msize != sizeof (struct SolicitTrafficMessage))
610 stm = (const struct SolicitTrafficMessage *) msg;
616 ss = ntohl (stm->solicit_size);
617 if ((ss > GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
618 (ss + sizeof (struct SendMessage) > GNUNET_SERVER_MAX_MESSAGE_SIZE))
623 solicit_traffic (h, &stm->peer, ss);
629 GNUNET_CLIENT_receive (h->client,
630 &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
636 * Function called when we are ready to transmit our
637 * "START" message (or when this operation timed out).
640 * @param size number of bytes available in buf
641 * @param buf where the callee should write the message
642 * @return number of bytes written to buf
644 static size_t transmit_start (void *cls, size_t size, void *buf);
648 * Function called on the first message received from
649 * the service (contains our public key, etc.).
650 * Should trigger calling the init callback
651 * and then start our regular message processing.
654 * @param msg message received, NULL on timeout or fatal error
657 init_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg)
659 struct GNUNET_CORE_Handle *h = cls;
660 const struct InitReplyMessage *m;
661 GNUNET_CORE_StartupCallback init;
662 struct GNUNET_PeerIdentity my_identity;
665 (ntohs (msg->size) != sizeof (struct InitReplyMessage)) ||
666 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY))
668 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
670 ("Error connecting to core service (failed to receive `%s' message).\n"),
672 GNUNET_break (msg == NULL);
673 transmit_start (h, 0, NULL);
676 m = (const struct InitReplyMessage *) msg;
677 /* start our message processing loop */
679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 ("Successfully connected to core service, starting processing loop.\n"));
683 h->currently_down = GNUNET_NO;
684 trigger_next_request (h);
685 GNUNET_CLIENT_receive (h->client,
686 &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
687 if (NULL != (init = h->init))
689 /* mark so we don't call init on reconnect */
692 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
693 _("Successfully connected to core service.\n"));
695 GNUNET_CRYPTO_hash (&m->publicKey,
697 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
698 &my_identity.hashPubKey);
699 init (h->cls, h, &my_identity, &m->publicKey);
705 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
707 struct GNUNET_CORE_Handle *h = cls;
708 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
714 * Function called when we are ready to transmit our
715 * "START" message (or when this operation timed out).
718 * @param size number of bytes available in buf
719 * @param buf where the callee should write the message
720 * @return number of bytes written to buf
723 transmit_start (void *cls, size_t size, void *buf)
725 struct GNUNET_CORE_Handle *h = cls;
726 struct InitMessage *init;
731 struct GNUNET_TIME_Relative delay;
736 if ((h->init == NULL) ||
737 (GNUNET_TIME_absolute_get ().value < h->startup_timeout.value))
739 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
740 _("Failed to connect to core service, retrying.\n"));
741 delay = GNUNET_TIME_absolute_get_remaining (h->startup_timeout);
742 if ((h->init == NULL) || (delay.value > 1000))
743 delay = GNUNET_TIME_UNIT_SECONDS;
746 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
748 GNUNET_SCHEDULER_add_delayed (h->sched,
749 delay, &reconnect_task, h);
752 /* timeout on initial connect */
753 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
754 _("Failed to connect to core service, giving up.\n"));
755 h->init (h->cls, NULL, NULL, NULL);
756 GNUNET_CORE_disconnect (h);
759 msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
760 GNUNET_assert (size >= msize);
762 init->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT);
763 init->header.size = htons (msize);
764 opt = GNUNET_CORE_OPTION_NOTHING;
765 if (h->connects != NULL)
766 opt |= GNUNET_CORE_OPTION_SEND_CONNECT;
767 if (h->disconnects != NULL)
768 opt |= GNUNET_CORE_OPTION_SEND_DISCONNECT;
770 opt |= GNUNET_CORE_OPTION_SEND_BFC;
771 if (h->inbound_notify != NULL)
773 if (h->inbound_hdr_only)
774 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
776 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
778 if (h->outbound_notify != NULL)
780 if (h->outbound_hdr_only)
781 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
783 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
785 init->options = htonl (opt);
786 ts = (uint16_t *) & init[1];
787 for (hpos = 0; hpos < h->hcnt; hpos++)
788 ts[hpos] = htons (h->handlers[hpos].type);
789 GNUNET_CLIENT_receive (h->client,
792 GNUNET_TIME_absolute_get_remaining
793 (h->startup_timeout));
794 return sizeof (struct InitMessage) + h->hcnt * sizeof (uint16_t);
799 * Connect to the core service. Note that the connection may
800 * complete (or fail) asynchronously.
802 * @param sched scheduler to use
803 * @param cfg configuration to use
804 * @param timeout after how long should we give up trying to connect to the core service?
805 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
806 * @param init callback to call on timeout or once we have successfully
807 * connected to the core service; note that timeout is only meaningful if init is not NULL
808 * @param connects function to call on peer connect, can be NULL
809 * @param disconnects function to call on peer disconnect / timeout, can be NULL
810 * @param bfc function to call to fill up spare bandwidth, can be NULL
811 * @param inbound_notify function to call for all inbound messages, can be NULL
812 * @param inbound_hdr_only set to GNUNET_YES if inbound_notify will only read the
813 * GNUNET_MessageHeader and hence we do not need to give it the full message;
814 * can be used to improve efficiency, ignored if inbound_notify is NULLL
815 * @param outbound_notify function to call for all outbound messages, can be NULL
816 * @param outbound_hdr_only set to GNUNET_YES if outbound_notify will only read the
817 * GNUNET_MessageHeader and hence we do not need to give it the full message
818 * can be used to improve efficiency, ignored if outbound_notify is NULLL
819 * @param handlers callbacks for messages we care about, NULL-terminated
820 * @return handle to the core service (only useful for disconnect until 'init' is called);
821 * NULL on error (in this case, init is never called)
823 struct GNUNET_CORE_Handle *
824 GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
825 const struct GNUNET_CONFIGURATION_Handle *cfg,
826 struct GNUNET_TIME_Relative timeout,
828 GNUNET_CORE_StartupCallback init,
829 GNUNET_CORE_ClientEventHandler connects,
830 GNUNET_CORE_ClientEventHandler disconnects,
831 GNUNET_CORE_BufferFillCallback bfc,
832 GNUNET_CORE_MessageCallback inbound_notify,
833 int inbound_hdr_only,
834 GNUNET_CORE_MessageCallback outbound_notify,
835 int outbound_hdr_only,
836 const struct GNUNET_CORE_MessageHandler *handlers)
838 struct GNUNET_CORE_Handle *h;
840 h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
845 h->connects = connects;
846 h->disconnects = disconnects;
848 h->inbound_notify = inbound_notify;
849 h->outbound_notify = outbound_notify;
850 h->inbound_hdr_only = inbound_hdr_only;
851 h->outbound_hdr_only = outbound_hdr_only;
852 h->handlers = handlers;
853 h->client = GNUNET_CLIENT_connect (sched, "core", cfg);
854 if (h->client == NULL)
859 h->startup_timeout = GNUNET_TIME_relative_to_absolute (timeout);
861 while (handlers[h->hcnt].callback != NULL)
863 GNUNET_assert (h->hcnt <
864 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
865 sizeof (struct InitMessage)) / sizeof (uint16_t));
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Trying to connect to core service in next %llu ms.\n",
872 GNUNET_CLIENT_notify_transmit_ready (h->client,
873 sizeof (struct InitMessage) +
874 sizeof (uint16_t) * h->hcnt, timeout,
882 * Disconnect from the core service.
884 * @param handle connection to core to disconnect
887 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
889 if (handle->th != NULL)
890 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
891 if (handle->solicit_transmit_req != NULL)
892 GNUNET_CORE_notify_transmit_ready_cancel (handle->solicit_transmit_req);
893 if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
894 GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task);
895 GNUNET_CLIENT_disconnect (handle->client);
896 GNUNET_free_non_null (handle->solicit_buffer);
897 GNUNET_free (handle);
902 * Build the configure message.
905 produce_configure_message (void *cls, size_t size, void *buf)
907 struct GNUNET_CORE_TransmitHandle *th = cls;
908 struct GNUNET_CORE_Handle *ch = th->ch;
912 /* communicate handle timeout/error! */
913 if (th->info != NULL)
914 th->info (th->info_cls, NULL, 0, 0, GNUNET_TIME_UNIT_ZERO, 0, 0.0);
915 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
916 GNUNET_CORE_notify_transmit_ready_cancel (th);
917 if (ch->submitted == th)
918 ch->submitted = NULL;
919 trigger_next_request (ch);
922 GNUNET_assert (size >= sizeof (struct RequestConfigureMessage));
923 memcpy (buf, &th[1], sizeof (struct RequestConfigureMessage));
924 if (th->prev == NULL)
925 ch->pending_head = th->next;
927 th->prev->next = th->next;
928 if (th->next == NULL)
929 ch->pending_tail = th->prev;
931 th->next->prev = th->prev;
932 GNUNET_assert (ch->submitted == NULL);
934 return sizeof (struct RequestConfigureMessage);
939 * Obtain statistics and/or change preferences for the given peer.
941 * @param handle connection to core to use
942 * @param peer identifies the peer
943 * @param timeout after how long should we give up (and call "info" with NULL
944 * for "peer" to signal an error)?
945 * @param bpm_out set to the current bandwidth limit (sending) for this peer,
946 * caller should set "bpm_out" to "-1" to avoid changing
947 * the current value; otherwise "bpm_out" will be lowered to
948 * the specified value; passing a pointer to "0" can be used to force
949 * us to disconnect from the peer; "bpm_out" might not increase
950 * as specified since the upper bound is generally
951 * determined by the other peer!
952 * @param amount reserve N bytes for receiving, negative
953 * amounts can be used to undo a (recent) reservation;
954 * @param preference increase incoming traffic share preference by this amount;
955 * in the absence of "amount" reservations, we use this
956 * preference value to assign proportional bandwidth shares
957 * to all connected peers
958 * @param info function to call with the resulting configuration information
959 * @param info_cls closure for info
962 GNUNET_CORE_peer_configure (struct GNUNET_CORE_Handle *handle,
963 const struct GNUNET_PeerIdentity *peer,
964 struct GNUNET_TIME_Relative timeout,
965 unsigned int bpm_out,
967 unsigned long long preference,
968 GNUNET_CORE_PeerConfigurationInfoCallback info,
971 struct RequestConfigureMessage *rcm;
972 struct GNUNET_CORE_TransmitHandle *th;
974 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle) +
975 sizeof (struct RequestConfigureMessage));
977 th->prev = handle->pending_tail;
978 if (handle->pending_tail == NULL)
979 handle->pending_head = th;
981 handle->pending_tail->next = th;
983 th->get_message = &produce_configure_message;
984 th->get_message_cls = th;
986 th->info_cls = info_cls;
987 th->timeout = GNUNET_TIME_relative_to_absolute (timeout);
988 th->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched,
990 &timeout_request, th);
991 th->msize = sizeof (struct RequestConfigureMessage);
992 rcm = (struct RequestConfigureMessage *) &th[1];
993 rcm->header.size = htons (sizeof (struct RequestConfigureMessage));
994 rcm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONFIGURE);
995 rcm->reserved = htonl (0);
996 rcm->limit_outbound_bpm = htonl (bpm_out);
997 rcm->reserve_inbound = htonl (amount);
998 rcm->preference_change = GNUNET_htonll(preference);
1000 if (handle->pending_head == th)
1001 trigger_next_request (handle);
1006 * Build the message requesting data transmission.
1009 produce_send (void *cls, size_t size, void *buf)
1011 struct GNUNET_CORE_TransmitHandle *th = cls;
1012 struct GNUNET_CORE_Handle *h;
1013 struct SendMessage *sm;
1015 GNUNET_CONNECTION_TransmitReadyNotify notify;
1021 /* timeout or error */
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "P2P transmission request for `%4s' timed out.\n",
1025 GNUNET_i2s(&th->peer));
1027 GNUNET_assert (0 == th->notify (th->notify_cls, 0, NULL));
1028 if (th->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1029 GNUNET_CORE_notify_transmit_ready_cancel (th);
1030 trigger_next_request (h);
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "Preparing for P2P transmission to `%4s'.\n",
1036 GNUNET_i2s(&th->peer));
1038 GNUNET_assert (th->timeout_task != GNUNET_SCHEDULER_NO_TASK);
1039 sm = (struct SendMessage *) buf;
1040 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
1041 sm->priority = htonl (th->priority);
1042 sm->deadline = GNUNET_TIME_absolute_hton (th->timeout);
1043 sm->peer = th->peer;
1044 notify = th->notify;
1045 notify_cls = th->notify_cls;
1046 GNUNET_CORE_notify_transmit_ready_cancel (th);
1047 trigger_next_request (h);
1048 GNUNET_assert (size >= sizeof (struct SendMessage));
1049 dt = notify (notify_cls, size - sizeof (struct SendMessage), &sm[1]);
1052 /* client decided to send nothing! */
1055 GNUNET_assert (dt >= sizeof (struct GNUNET_MessageHeader));
1056 sm->header.size = htons (dt + sizeof (struct SendMessage));
1057 GNUNET_assert (dt + sizeof (struct SendMessage) < size);
1058 return dt + sizeof (struct SendMessage);
1063 * Ask the core to call "notify" once it is ready to transmit the
1064 * given number of bytes to the specified "target". If we are not yet
1065 * connected to the specified peer, a call to this function will cause
1066 * us to try to establish a connection.
1068 * @param handle connection to core service
1069 * @param priority how important is the message?
1070 * @param maxdelay how long can the message wait?
1071 * @param target who should receive the message,
1072 * use NULL for this peer (loopback)
1073 * @param notify_size how many bytes of buffer space does notify want?
1074 * @param notify function to call when buffer space is available
1075 * @param notify_cls closure for notify
1076 * @return non-NULL if the notify callback was queued,
1077 * NULL if we can not even queue the request (insufficient
1078 * memory); if NULL is returned, "notify" will NOT be called.
1080 struct GNUNET_CORE_TransmitHandle *
1081 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
1082 unsigned int priority,
1083 struct GNUNET_TIME_Relative maxdelay,
1084 const struct GNUNET_PeerIdentity *target,
1086 GNUNET_CONNECTION_TransmitReadyNotify notify,
1089 struct GNUNET_CORE_TransmitHandle *th;
1091 GNUNET_assert (notify_size + sizeof (struct SendMessage) <
1092 GNUNET_SERVER_MAX_MESSAGE_SIZE);
1093 th = GNUNET_malloc (sizeof (struct GNUNET_CORE_TransmitHandle));
1095 /* append to list */
1096 th->prev = handle->pending_tail;
1097 if (handle->pending_tail == NULL)
1098 handle->pending_head = th;
1100 handle->pending_tail->next = th;
1101 th->get_message = &produce_send;
1102 th->get_message_cls = th;
1103 th->notify = notify;
1104 th->notify_cls = notify_cls;
1106 th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
1107 th->timeout_task = GNUNET_SCHEDULER_add_delayed (handle->sched,
1109 &timeout_request, th);
1110 th->priority = priority;
1111 th->msize = sizeof (struct SendMessage) + notify_size;
1112 /* was the request queue previously empty? */
1113 if ( (handle->pending_head == th) &&
1114 (handle->th == NULL) )
1115 trigger_next_request (handle);
1121 * Cancel the specified transmission-ready notification.
1123 * @param h handle that was returned by "notify_transmit_ready".
1126 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
1129 struct GNUNET_CORE_Handle *handle = h->ch;
1131 if (handle->submitted == h)
1133 handle->submitted = NULL;
1137 if (h->prev == NULL)
1138 handle->pending_head = h->next;
1140 h->prev->next = h->next;
1141 if (h->next == NULL)
1142 handle->pending_tail = h->prev;
1144 h->next->prev = h->prev;
1146 if (h->timeout_task != GNUNET_SCHEDULER_NO_TASK)
1147 GNUNET_SCHEDULER_cancel (handle->sched, h->timeout_task);
1152 /* end of core_api.c */