2 This file is part of GNUnet.
3 Copyright (C) 2009-2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file core/core_api.c
22 * @brief core service; this is the main API for encrypted P2P
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_core_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "core-api",__VA_ARGS__)
36 * Handle for a transmission request.
38 struct GNUNET_CORE_TransmitHandle
42 * Corresponding peer record.
44 struct PeerRecord *peer;
47 * Function that will be called to get the actual request
48 * (once we are ready to transmit this request to the core).
49 * The function will be called with a NULL buffer to signal
52 GNUNET_CONNECTION_TransmitReadyNotify get_message;
55 * Closure for @e get_message.
57 void *get_message_cls;
60 * Deadline for the transmission (the request does not get cancelled
61 * at this time, this is merely how soon the application wants this out).
63 struct GNUNET_TIME_Absolute deadline;
66 * When did this request get queued?
68 struct GNUNET_TIME_Absolute request_time;
71 * How important is this message?
73 enum GNUNET_CORE_Priority priority;
81 * Size of this request.
86 * Send message request ID for this request.
94 * Information we track for each peer.
100 * Corresponding CORE handle.
102 struct GNUNET_CORE_Handle *ch;
105 * Pending request, if any. 'th->peer' is set to NULL if the
106 * request is not active.
108 struct GNUNET_CORE_TransmitHandle th;
111 * Peer the record is about.
113 struct GNUNET_PeerIdentity peer;
116 * SendMessageRequest ID generator for this peer.
124 * Context for the core service connection.
126 struct GNUNET_CORE_Handle
130 * Configuration we're using.
132 const struct GNUNET_CONFIGURATION_Handle *cfg;
135 * Closure for the various callbacks.
140 * Function to call once we've handshaked with the core service.
142 GNUNET_CORE_StartupCallback init;
145 * Function to call whenever we're notified about a peer connecting.
147 GNUNET_CORE_ConnectEventHandler connects;
150 * Function to call whenever we're notified about a peer disconnecting.
152 GNUNET_CORE_DisconnectEventHandler disconnects;
155 * Function to call whenever we receive an inbound message.
157 GNUNET_CORE_MessageCallback inbound_notify;
160 * Function to call whenever we receive an outbound message.
162 GNUNET_CORE_MessageCallback outbound_notify;
165 * Function handlers for messages of particular type.
167 struct GNUNET_CORE_MessageHandler *handlers;
170 * Our message queue for transmissions to the service.
172 struct GNUNET_MQ_Handle *mq;
175 * Hash map listing all of the peers that we are currently
178 struct GNUNET_CONTAINER_MultiPeerMap *peers;
181 * Identity of this peer.
183 struct GNUNET_PeerIdentity me;
186 * ID of reconnect task (if any).
188 struct GNUNET_SCHEDULER_Task *reconnect_task;
191 * Current delay we use for re-trying to connect to core.
193 struct GNUNET_TIME_Relative retry_backoff;
196 * Number of entries in the handlers array.
201 * For inbound notifications without a specific handler, do
202 * we expect to only receive headers?
204 int inbound_hdr_only;
207 * For outbound notifications without a specific handler, do
208 * we expect to only receive headers?
210 int outbound_hdr_only;
213 * Are we currently disconnected and hence unable to forward
219 * Did we ever get INIT?
227 * Our current client connection went down. Clean it up
228 * and try to reconnect!
230 * @param h our handle to the core service
233 reconnect (struct GNUNET_CORE_Handle *h);
237 * Task schedule to try to re-connect to core.
239 * @param cls the `struct GNUNET_CORE_Handle`
240 * @param tc task context
243 reconnect_task (void *cls)
245 struct GNUNET_CORE_Handle *h = cls;
247 h->reconnect_task = NULL;
248 LOG (GNUNET_ERROR_TYPE_DEBUG,
249 "Connecting to CORE service after delay\n");
255 * Notify clients about disconnect and free the entry for connected
258 * @param cls the `struct GNUNET_CORE_Handle *`
259 * @param key the peer identity (not used)
260 * @param value the `struct PeerRecord` to free.
261 * @return #GNUNET_YES (continue)
264 disconnect_and_free_peer_entry (void *cls,
265 const struct GNUNET_PeerIdentity *key,
268 struct GNUNET_CORE_Handle *h = cls;
269 struct GNUNET_CORE_TransmitHandle *th;
270 struct PeerRecord *pr = value;
272 if (NULL != h->disconnects)
273 h->disconnects (h->cls,
275 /* all requests should have been cancelled, clean up anyway, just in case */
277 if (NULL != th->peer)
282 /* done with 'voluntary' cleanups, now on to normal freeing */
283 GNUNET_assert (GNUNET_YES ==
284 GNUNET_CONTAINER_multipeermap_remove (h->peers,
287 GNUNET_assert (pr->ch == h);
294 * Close down any existing connection to the CORE service and
295 * try re-establishing it later.
297 * @param h our handle
300 reconnect_later (struct GNUNET_CORE_Handle *h)
302 GNUNET_assert (NULL == h->reconnect_task);
305 GNUNET_MQ_destroy (h->mq);
308 h->currently_down = GNUNET_YES;
309 GNUNET_assert (h->reconnect_task == NULL);
311 GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
314 GNUNET_CONTAINER_multipeermap_iterate (h->peers,
315 &disconnect_and_free_peer_entry,
317 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
322 * Generic error handler, called with the appropriate error code and
323 * the same closure specified at the creation of the message queue.
324 * Not every message queue implementation supports an error handler.
326 * @param cls closure, a `struct GNUNET_CORE_Handle *`
327 * @param error error code
330 handle_mq_error (void *cls,
331 enum GNUNET_MQ_Error error)
333 struct GNUNET_CORE_Handle *h = cls;
340 * Handle init reply message received from CORE service. Notify
341 * application that we are now connected to the CORE. Also fake
342 * loopback connection.
344 * @param cls the `struct GNUNET_CORE_Handle`
345 * @param m the init reply
348 handle_init_reply (void *cls,
349 const struct InitReplyMessage *m)
351 struct GNUNET_CORE_Handle *h = cls;
352 GNUNET_CORE_StartupCallback init;
353 struct PeerRecord *pr;
355 GNUNET_break (0 == ntohl (m->reserved));
356 GNUNET_break (GNUNET_YES == h->currently_down);
357 h->currently_down = GNUNET_NO;
358 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
359 if (NULL != (init = h->init))
361 /* mark so we don't call init on reconnect */
363 h->me = m->my_identity;
364 LOG (GNUNET_ERROR_TYPE_DEBUG,
365 "Connected to core service of peer `%s'.\n",
366 GNUNET_i2s (&h->me));
367 h->have_init = GNUNET_YES;
373 LOG (GNUNET_ERROR_TYPE_DEBUG,
374 "Successfully reconnected to core service.\n");
375 if (GNUNET_NO == h->have_init)
377 h->me = m->my_identity;
378 h->have_init = GNUNET_YES;
382 GNUNET_break (0 == memcmp (&h->me,
384 sizeof (struct GNUNET_PeerIdentity)));
387 /* fake 'connect to self' */
388 pr = GNUNET_new (struct PeerRecord);
391 GNUNET_assert (GNUNET_YES ==
392 GNUNET_CONTAINER_multipeermap_put (h->peers,
395 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
396 if (NULL != h->connects)
403 * Handle connect message received from CORE service.
404 * Notify the application about the new connection.
406 * @param cls the `struct GNUNET_CORE_Handle`
407 * @param cnm the connect message
410 handle_connect_notify (void *cls,
411 const struct ConnectNotifyMessage * cnm)
413 struct GNUNET_CORE_Handle *h = cls;
414 struct PeerRecord *pr;
416 GNUNET_break (GNUNET_NO == h->currently_down);
417 LOG (GNUNET_ERROR_TYPE_DEBUG,
418 "Received notification about connection from `%s'.\n",
419 GNUNET_i2s (&cnm->peer));
420 if (0 == memcmp (&h->me,
422 sizeof (struct GNUNET_PeerIdentity)))
424 /* connect to self!? */
428 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
436 pr = GNUNET_new (struct PeerRecord);
437 pr->peer = cnm->peer;
439 GNUNET_assert (GNUNET_YES ==
440 GNUNET_CONTAINER_multipeermap_put (h->peers,
443 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
444 if (NULL != h->connects)
451 * Handle disconnect message received from CORE service.
452 * Notify the application about the lost connection.
454 * @param cls the `struct GNUNET_CORE_Handle`
455 * @param dnm message about the disconnect event
458 handle_disconnect_notify (void *cls,
459 const struct DisconnectNotifyMessage * dnm)
461 struct GNUNET_CORE_Handle *h = cls;
462 struct PeerRecord *pr;
464 GNUNET_break (GNUNET_NO == h->currently_down);
465 if (0 == memcmp (&h->me,
467 sizeof (struct GNUNET_PeerIdentity)))
469 /* connection to self!? */
473 GNUNET_break (0 == ntohl (dnm->reserved));
474 LOG (GNUNET_ERROR_TYPE_DEBUG,
475 "Received notification about disconnect from `%s'.\n",
476 GNUNET_i2s (&dnm->peer));
477 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
485 disconnect_and_free_peer_entry (h,
492 * Check that message received from CORE service is well-formed.
494 * @param cls the `struct GNUNET_CORE_Handle`
495 * @param ntm the message we got
496 * @return #GNUNET_OK if the message is well-formed
499 check_notify_inbound (void *cls,
500 const struct NotifyTrafficMessage *ntm)
502 struct GNUNET_CORE_Handle *h = cls;
504 const struct GNUNET_MessageHeader *em;
506 GNUNET_break (GNUNET_NO == h->currently_down);
507 msize = ntohs (ntm->header.size) - sizeof (struct NotifyTrafficMessage);
508 if (msize < sizeof (struct GNUNET_MessageHeader))
511 return GNUNET_SYSERR;
513 em = (const struct GNUNET_MessageHeader *) &ntm[1];
514 if ( (GNUNET_NO == h->inbound_hdr_only) &&
515 (msize != ntohs (em->size)) )
518 return GNUNET_SYSERR;
525 * Handle inbound message received from CORE service. If applicable,
526 * notify the application.
528 * @param cls the `struct GNUNET_CORE_Handle`
529 * @param ntm the message we got from CORE.
532 handle_notify_inbound (void *cls,
533 const struct NotifyTrafficMessage *ntm)
535 struct GNUNET_CORE_Handle *h = cls;
536 const struct GNUNET_MessageHeader *em;
537 struct PeerRecord *pr;
540 GNUNET_break (GNUNET_NO == h->currently_down);
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Received inbound message from `%s'.\n",
543 GNUNET_i2s (&ntm->peer));
544 em = (const struct GNUNET_MessageHeader *) &ntm[1];
545 et = ntohs (em->type);
546 for (unsigned int hpos = 0; NULL != h->handlers[hpos].callback; hpos++)
548 const struct GNUNET_CORE_MessageHandler *mh;
550 mh = &h->handlers[hpos];
553 if ( (mh->expected_size != ntohs (em->size)) &&
554 (0 != mh->expected_size) )
556 LOG (GNUNET_ERROR_TYPE_ERROR,
557 "Unexpected message size %u for message of type %u from peer `%s'\n",
560 GNUNET_i2s (&ntm->peer));
564 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
573 h->handlers[hpos].callback (h->cls,
577 /* error in processing, do not process other messages! */
581 if (NULL != h->inbound_notify)
582 h->inbound_notify (h->cls,
589 * Check that message received from CORE service is well-formed.
591 * @param cls the `struct GNUNET_CORE_Handle`
592 * @param ntm the message we got
593 * @return #GNUNET_OK if the message is well-formed
596 check_notify_outbound (void *cls,
597 const struct NotifyTrafficMessage *ntm)
599 struct GNUNET_CORE_Handle *h = cls;
601 const struct GNUNET_MessageHeader *em;
603 GNUNET_break (GNUNET_NO == h->currently_down);
604 LOG (GNUNET_ERROR_TYPE_DEBUG,
605 "Received outbound message from `%s'.\n",
606 GNUNET_i2s (&ntm->peer));
607 msize = ntohs (ntm->header.size) - sizeof (struct NotifyTrafficMessage);
608 if (msize < sizeof (struct GNUNET_MessageHeader))
611 return GNUNET_SYSERR;
613 em = (const struct GNUNET_MessageHeader *) &ntm[1];
614 if ( (GNUNET_NO == h->outbound_hdr_only) &&
615 (msize != ntohs (em->size)) )
618 return GNUNET_SYSERR;
625 * Handle outbound message received from CORE service. If applicable,
626 * notify the application.
628 * @param cls the `struct GNUNET_CORE_Handle`
629 * @param ntm the message we got
632 handle_notify_outbound (void *cls,
633 const struct NotifyTrafficMessage *ntm)
635 struct GNUNET_CORE_Handle *h = cls;
636 const struct GNUNET_MessageHeader *em;
638 GNUNET_break (GNUNET_NO == h->currently_down);
639 em = (const struct GNUNET_MessageHeader *) &ntm[1];
640 LOG (GNUNET_ERROR_TYPE_DEBUG,
641 "Received notification about transmission to `%s'.\n",
642 GNUNET_i2s (&ntm->peer));
643 if (NULL == h->outbound_notify)
648 h->outbound_notify (h->cls,
655 * Handle message received from CORE service notifying us that we are
656 * now allowed to send a message to a peer. If that message is still
657 * pending, put it into the queue to be transmitted.
659 * @param cls the `struct GNUNET_CORE_Handle`
660 * @param ntm the message we got
663 handle_send_ready (void *cls,
664 const struct SendMessageReady *smr)
666 struct GNUNET_CORE_Handle *h = cls;
667 struct PeerRecord *pr;
668 struct GNUNET_CORE_TransmitHandle *th;
669 struct SendMessage *sm;
670 struct GNUNET_MQ_Envelope *env;
671 struct GNUNET_TIME_Relative delay;
672 struct GNUNET_TIME_Relative overdue;
675 GNUNET_break (GNUNET_NO == h->currently_down);
676 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
684 LOG (GNUNET_ERROR_TYPE_DEBUG,
685 "Received notification about transmission readiness to `%s'.\n",
686 GNUNET_i2s (&smr->peer));
687 if (NULL == pr->th.peer)
689 /* request must have been cancelled between the original request
690 * and the response from CORE, ignore CORE's readiness */
694 if (ntohs (smr->smr_id) != th->smr_id)
696 /* READY message is for expired or cancelled message,
697 * ignore! (we should have already sent another request) */
700 /* ok, all good, send message out! */
702 env = GNUNET_MQ_msg_extra (sm,
704 GNUNET_MESSAGE_TYPE_CORE_SEND);
705 sm->priority = htonl ((uint32_t) th->priority);
706 sm->deadline = GNUNET_TIME_absolute_hton (th->deadline);
708 sm->cork = htonl ((uint32_t) th->cork);
709 sm->reserved = htonl (0);
710 ret = th->get_message (th->get_message_cls,
713 sm->header.size = htons (ret + sizeof (struct SendMessage));
715 // GNUNET_assert (ret == th->msize); /* NOTE: API change! */
716 delay = GNUNET_TIME_absolute_get_duration (th->request_time);
717 overdue = GNUNET_TIME_absolute_get_duration (th->deadline);
718 if (overdue.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
719 LOG (GNUNET_ERROR_TYPE_WARNING,
720 "Transmitting overdue %u bytes to `%s' at priority %u with %s delay %s\n",
722 GNUNET_i2s (&pr->peer),
723 (unsigned int) th->priority,
724 GNUNET_STRINGS_relative_time_to_string (delay,
726 (th->cork) ? " (corked)" : "");
728 LOG (GNUNET_ERROR_TYPE_DEBUG,
729 "Transmitting %u bytes to `%s' at priority %u with %s delay %s\n",
731 GNUNET_i2s (&pr->peer),
732 (unsigned int) th->priority,
733 GNUNET_STRINGS_relative_time_to_string (delay,
735 (th->cork) ? " (corked)" : "");
736 GNUNET_MQ_send (h->mq,
742 * Our current client connection went down. Clean it up and try to
745 * @param h our handle to the core service
748 reconnect (struct GNUNET_CORE_Handle *h)
750 GNUNET_MQ_hd_fixed_size (init_reply,
751 GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY,
752 struct InitReplyMessage);
753 GNUNET_MQ_hd_fixed_size (connect_notify,
754 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT,
755 struct ConnectNotifyMessage);
756 GNUNET_MQ_hd_fixed_size (disconnect_notify,
757 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT,
758 struct DisconnectNotifyMessage);
759 GNUNET_MQ_hd_var_size (notify_inbound,
760 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND,
761 struct NotifyTrafficMessage);
762 GNUNET_MQ_hd_var_size (notify_outbound,
763 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND,
764 struct NotifyTrafficMessage);
765 GNUNET_MQ_hd_fixed_size (send_ready,
766 GNUNET_MESSAGE_TYPE_CORE_SEND_READY,
767 struct SendMessageReady);
768 struct GNUNET_MQ_MessageHandler handlers[] = {
769 make_init_reply_handler (h),
770 make_connect_notify_handler (h),
771 make_disconnect_notify_handler (h),
772 make_notify_inbound_handler (h),
773 make_notify_outbound_handler (h),
774 make_send_ready_handler (h),
775 GNUNET_MQ_handler_end ()
777 struct InitMessage *init;
778 struct GNUNET_MQ_Envelope *env;
782 GNUNET_assert (NULL == h->mq);
783 GNUNET_assert (GNUNET_YES == h->currently_down);
784 h->mq = GNUNET_CLIENT_connecT (h->cfg,
794 env = GNUNET_MQ_msg_extra (init,
795 sizeof (uint16_t) * h->hcnt,
796 GNUNET_MESSAGE_TYPE_CORE_INIT);
798 if (NULL != h->inbound_notify)
800 if (h->inbound_hdr_only)
801 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
803 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
805 if (NULL != h->outbound_notify)
807 if (h->outbound_hdr_only)
808 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
810 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
812 LOG (GNUNET_ERROR_TYPE_INFO,
813 "(Re)connecting to CORE service, monitoring messages of type %u\n",
815 init->options = htonl (opt);
816 ts = (uint16_t *) &init[1];
817 for (unsigned int hpos = 0; hpos < h->hcnt; hpos++)
818 ts[hpos] = htons (h->handlers[hpos].type);
819 GNUNET_MQ_send (h->mq,
825 * Connect to the core service. Note that the connection may complete
826 * (or fail) asynchronously.
828 * @param cfg configuration to use
829 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
830 * @param init callback to call once we have successfully
831 * connected to the core service
832 * @param connects function to call on peer connect, can be NULL
833 * @param disconnects function to call on peer disconnect / timeout, can be NULL
834 * @param inbound_notify function to call for all inbound messages, can be NULL
835 * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
836 * GNUNET_MessageHeader and hence we do not need to give it the full message;
837 * can be used to improve efficiency, ignored if @a inbound_notify is NULLL
838 * @param outbound_notify function to call for all outbound messages, can be NULL
839 * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
840 * GNUNET_MessageHeader and hence we do not need to give it the full message
841 * can be used to improve efficiency, ignored if @a outbound_notify is NULLL
842 * @param handlers callbacks for messages we care about, NULL-terminated
843 * @return handle to the core service (only useful for disconnect until 'init' is called);
844 * NULL on error (in this case, init is never called)
846 struct GNUNET_CORE_Handle *
847 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
849 GNUNET_CORE_StartupCallback init,
850 GNUNET_CORE_ConnectEventHandler connects,
851 GNUNET_CORE_DisconnectEventHandler disconnects,
852 GNUNET_CORE_MessageCallback inbound_notify,
853 int inbound_hdr_only,
854 GNUNET_CORE_MessageCallback outbound_notify,
855 int outbound_hdr_only,
856 const struct GNUNET_CORE_MessageHandler *handlers)
858 struct GNUNET_CORE_Handle *h;
861 h = GNUNET_new (struct GNUNET_CORE_Handle);
865 h->connects = connects;
866 h->disconnects = disconnects;
867 h->inbound_notify = inbound_notify;
868 h->outbound_notify = outbound_notify;
869 h->inbound_hdr_only = inbound_hdr_only;
870 h->outbound_hdr_only = outbound_hdr_only;
871 h->currently_down = GNUNET_YES;
872 h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
874 if (NULL != handlers)
875 while (NULL != handlers[hcnt].callback)
877 h->handlers = GNUNET_new_array (hcnt + 1,
878 struct GNUNET_CORE_MessageHandler);
879 if (NULL != handlers)
882 hcnt * sizeof (struct GNUNET_CORE_MessageHandler));
884 GNUNET_assert (hcnt <
885 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
886 sizeof (struct InitMessage)) / sizeof (uint16_t));
887 LOG (GNUNET_ERROR_TYPE_DEBUG,
888 "Connecting to CORE service\n");
895 * Disconnect from the core service. This function can only
896 * be called *after* all pending #GNUNET_CORE_notify_transmit_ready()
897 * requests have been explicitly canceled.
899 * @param handle connection to core to disconnect
902 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 "Disconnecting from CORE service\n");
906 GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
907 &disconnect_and_free_peer_entry,
909 GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
910 handle->peers = NULL;
911 if (NULL != handle->reconnect_task)
913 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
914 handle->reconnect_task = NULL;
916 if (NULL != handle->mq)
918 GNUNET_MQ_destroy (handle->mq);
921 GNUNET_free (handle->handlers);
922 GNUNET_free (handle);
927 * Ask the core to call @a notify once it is ready to transmit the
928 * given number of bytes to the specified @a target. Must only be
929 * called after a connection to the respective peer has been
930 * established (and the client has been informed about this). You may
931 * have one request of this type pending for each connected peer at
932 * any time. If a peer disconnects, the application MUST call
933 * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
934 * transmission request, if one such request is pending.
936 * @param handle connection to core service
937 * @param cork is corking allowed for this transmission?
938 * @param priority how important is the message?
939 * @param maxdelay how long can the message wait? Only effective if @a cork is #GNUNET_YES
940 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
941 * @param notify_size how many bytes of buffer space does @a notify want?
942 * @param notify function to call when buffer space is available;
943 * will be called with NULL on timeout; clients MUST cancel
944 * all pending transmission requests DURING the disconnect
946 * @param notify_cls closure for @a notify
947 * @return non-NULL if the notify callback was queued,
948 * NULL if we can not even queue the request (request already pending);
949 * if NULL is returned, @a notify will NOT be called.
951 struct GNUNET_CORE_TransmitHandle *
952 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
954 enum GNUNET_CORE_Priority priority,
955 struct GNUNET_TIME_Relative maxdelay,
956 const struct GNUNET_PeerIdentity *target,
958 GNUNET_CONNECTION_TransmitReadyNotify notify,
961 struct PeerRecord *pr;
962 struct GNUNET_CORE_TransmitHandle *th;
963 struct SendMessageRequest *smr;
964 struct GNUNET_MQ_Envelope *env;
966 GNUNET_assert (NULL != notify);
967 if ( (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) ||
968 (notify_size + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
973 LOG (GNUNET_ERROR_TYPE_DEBUG,
974 "Asking core for transmission of %u bytes to `%s'\n",
975 (unsigned int) notify_size,
976 GNUNET_i2s (target));
977 pr = GNUNET_CONTAINER_multipeermap_get (handle->peers,
981 /* attempt to send to peer that is not connected */
985 if (NULL != pr->th.peer)
987 /* attempting to queue a second request for the same destination */
994 sizeof (struct GNUNET_CORE_TransmitHandle));
996 th->get_message = notify;
997 th->get_message_cls = notify_cls;
998 th->request_time = GNUNET_TIME_absolute_get ();
999 if (GNUNET_YES == cork)
1000 th->deadline = GNUNET_TIME_relative_to_absolute (maxdelay);
1002 th->deadline = th->request_time;
1003 th->priority = priority;
1004 th->msize = notify_size;
1006 env = GNUNET_MQ_msg (smr,
1007 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
1008 smr->priority = htonl ((uint32_t) th->priority);
1009 smr->deadline = GNUNET_TIME_absolute_hton (th->deadline);
1010 smr->peer = pr->peer;
1011 smr->reserved = htonl (0);
1012 smr->size = htons (th->msize);
1013 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
1014 GNUNET_MQ_send (handle->mq,
1016 LOG (GNUNET_ERROR_TYPE_DEBUG,
1017 "Transmission request added to queue\n");
1023 * Cancel the specified transmission-ready notification.
1025 * @param th handle that was returned by #GNUNET_CORE_notify_transmit_ready().
1028 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1030 struct PeerRecord *pr = th->peer;
1032 LOG (GNUNET_ERROR_TYPE_DEBUG,
1033 "Aborting transmission request to core for %u bytes to `%s'\n",
1034 (unsigned int) th->msize,
1035 GNUNET_i2s (&pr->peer));
1041 * Check if the given peer is currently connected. This function is for special
1042 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1043 * expected to track which peers are connected based on the connect/disconnect
1044 * callbacks from #GNUNET_CORE_connect(). This function is NOT part of the
1045 * 'versioned', 'official' API. The difference between this function and the
1046 * function GNUNET_CORE_is_peer_connected() is that this one returns
1047 * synchronously after looking in the CORE API cache. The function
1048 * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1049 * its response is given asynchronously.
1051 * @param h the core handle
1052 * @param pid the identity of the peer to check if it has been connected to us
1053 * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
1056 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1057 const struct GNUNET_PeerIdentity *pid)
1059 return GNUNET_CONTAINER_multipeermap_contains (h->peers,
1064 /* end of core_api.c */