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
222 * Our current client connection went down. Clean it up
223 * and try to reconnect!
225 * @param h our handle to the core service
228 reconnect (struct GNUNET_CORE_Handle *h);
232 * Task schedule to try to re-connect to core.
234 * @param cls the `struct GNUNET_CORE_Handle`
235 * @param tc task context
238 reconnect_task (void *cls)
240 struct GNUNET_CORE_Handle *h = cls;
242 h->reconnect_task = NULL;
243 LOG (GNUNET_ERROR_TYPE_DEBUG,
244 "Connecting to CORE service after delay\n");
250 * Notify clients about disconnect and free the entry for connected
253 * @param cls the `struct GNUNET_CORE_Handle *`
254 * @param key the peer identity (not used)
255 * @param value the `struct PeerRecord` to free.
256 * @return #GNUNET_YES (continue)
259 disconnect_and_free_peer_entry (void *cls,
260 const struct GNUNET_PeerIdentity *key,
263 struct GNUNET_CORE_Handle *h = cls;
264 struct GNUNET_CORE_TransmitHandle *th;
265 struct PeerRecord *pr = value;
267 if (NULL != h->disconnects)
268 h->disconnects (h->cls,
270 /* all requests should have been cancelled, clean up anyway, just in case */
272 if (NULL != th->peer)
277 /* done with 'voluntary' cleanups, now on to normal freeing */
278 GNUNET_assert (GNUNET_YES ==
279 GNUNET_CONTAINER_multipeermap_remove (h->peers,
282 GNUNET_assert (pr->ch == h);
289 * Close down any existing connection to the CORE service and
290 * try re-establishing it later.
292 * @param h our handle
295 reconnect_later (struct GNUNET_CORE_Handle *h)
297 GNUNET_assert (NULL == h->reconnect_task);
300 GNUNET_MQ_destroy (h->mq);
303 h->currently_down = GNUNET_YES;
304 GNUNET_assert (h->reconnect_task == NULL);
306 GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
309 GNUNET_CONTAINER_multipeermap_iterate (h->peers,
310 &disconnect_and_free_peer_entry,
312 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
317 * Generic error handler, called with the appropriate error code and
318 * the same closure specified at the creation of the message queue.
319 * Not every message queue implementation supports an error handler.
321 * @param cls closure, a `struct GNUNET_CORE_Handle *`
322 * @param error error code
325 handle_mq_error (void *cls,
326 enum GNUNET_MQ_Error error)
328 struct GNUNET_CORE_Handle *h = cls;
335 * Handle init reply message received from CORE service. Notify
336 * application that we are now connected to the CORE. Also fake
337 * loopback connection.
339 * @param cls the `struct GNUNET_CORE_Handle`
340 * @param m the init reply
343 handle_init_reply (void *cls,
344 const struct InitReplyMessage *m)
346 struct GNUNET_CORE_Handle *h = cls;
347 GNUNET_CORE_StartupCallback init;
348 struct PeerRecord *pr;
350 GNUNET_break (0 == ntohl (m->reserved));
351 GNUNET_break (GNUNET_YES == h->currently_down);
352 h->currently_down = GNUNET_NO;
353 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
354 if (NULL != (init = h->init))
356 /* mark so we don't call init on reconnect */
358 h->me = m->my_identity;
359 LOG (GNUNET_ERROR_TYPE_DEBUG,
360 "Connected to core service of peer `%s'.\n",
361 GNUNET_i2s (&h->me));
367 LOG (GNUNET_ERROR_TYPE_DEBUG,
368 "Successfully reconnected to core service.\n");
369 GNUNET_break (0 == memcmp (&h->me,
371 sizeof (struct GNUNET_PeerIdentity)));
373 /* fake 'connect to self' */
374 pr = GNUNET_new (struct PeerRecord);
377 GNUNET_assert (GNUNET_YES ==
378 GNUNET_CONTAINER_multipeermap_put (h->peers,
381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
382 if (NULL != h->connects)
389 * Handle connect message received from CORE service.
390 * Notify the application about the new connection.
392 * @param cls the `struct GNUNET_CORE_Handle`
393 * @param cnm the connect message
396 handle_connect_notify (void *cls,
397 const struct ConnectNotifyMessage * cnm)
399 struct GNUNET_CORE_Handle *h = cls;
400 struct PeerRecord *pr;
402 GNUNET_break (GNUNET_NO == h->currently_down);
403 LOG (GNUNET_ERROR_TYPE_DEBUG,
404 "Received notification about connection from `%s'.\n",
405 GNUNET_i2s (&cnm->peer));
406 if (0 == memcmp (&h->me,
408 sizeof (struct GNUNET_PeerIdentity)))
410 /* connect to self!? */
414 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
422 pr = GNUNET_new (struct PeerRecord);
423 pr->peer = cnm->peer;
425 GNUNET_assert (GNUNET_YES ==
426 GNUNET_CONTAINER_multipeermap_put (h->peers,
429 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
430 if (NULL != h->connects)
437 * Handle disconnect message received from CORE service.
438 * Notify the application about the lost connection.
440 * @param cls the `struct GNUNET_CORE_Handle`
441 * @param dnm message about the disconnect event
444 handle_disconnect_notify (void *cls,
445 const struct DisconnectNotifyMessage * dnm)
447 struct GNUNET_CORE_Handle *h = cls;
448 struct PeerRecord *pr;
450 GNUNET_break (GNUNET_NO == h->currently_down);
451 if (0 == memcmp (&h->me,
453 sizeof (struct GNUNET_PeerIdentity)))
455 /* connection to self!? */
459 GNUNET_break (0 == ntohl (dnm->reserved));
460 LOG (GNUNET_ERROR_TYPE_DEBUG,
461 "Received notification about disconnect from `%s'.\n",
462 GNUNET_i2s (&dnm->peer));
463 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
471 disconnect_and_free_peer_entry (h,
478 * Check that message received from CORE service is well-formed.
480 * @param cls the `struct GNUNET_CORE_Handle`
481 * @param ntm the message we got
482 * @return #GNUNET_OK if the message is well-formed
485 check_notify_inbound (void *cls,
486 const struct NotifyTrafficMessage *ntm)
488 struct GNUNET_CORE_Handle *h = cls;
490 const struct GNUNET_MessageHeader *em;
492 GNUNET_break (GNUNET_NO == h->currently_down);
493 msize = ntohs (ntm->header.size) - sizeof (struct NotifyTrafficMessage);
494 if (msize < sizeof (struct GNUNET_MessageHeader))
497 return GNUNET_SYSERR;
499 em = (const struct GNUNET_MessageHeader *) &ntm[1];
500 if ( (GNUNET_NO == h->inbound_hdr_only) &&
501 (msize != ntohs (em->size)) )
504 return GNUNET_SYSERR;
511 * Handle inbound message received from CORE service. If applicable,
512 * notify the application.
514 * @param cls the `struct GNUNET_CORE_Handle`
515 * @param ntm the message we got from CORE.
518 handle_notify_inbound (void *cls,
519 const struct NotifyTrafficMessage *ntm)
521 struct GNUNET_CORE_Handle *h = cls;
522 const struct GNUNET_MessageHeader *em;
523 struct PeerRecord *pr;
526 GNUNET_break (GNUNET_NO == h->currently_down);
527 LOG (GNUNET_ERROR_TYPE_DEBUG,
528 "Received inbound message from `%s'.\n",
529 GNUNET_i2s (&ntm->peer));
530 em = (const struct GNUNET_MessageHeader *) &ntm[1];
531 et = ntohs (em->type);
532 for (unsigned int hpos = 0; NULL != h->handlers[hpos].callback; hpos++)
534 const struct GNUNET_CORE_MessageHandler *mh;
536 mh = &h->handlers[hpos];
539 if ( (mh->expected_size != ntohs (em->size)) &&
540 (0 != mh->expected_size) )
542 LOG (GNUNET_ERROR_TYPE_ERROR,
543 "Unexpected message size %u for message of type %u from peer `%s'\n",
546 GNUNET_i2s (&ntm->peer));
550 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
559 h->handlers[hpos].callback (h->cls,
563 /* error in processing, do not process other messages! */
567 if (NULL != h->inbound_notify)
568 h->inbound_notify (h->cls,
575 * Check that message received from CORE service is well-formed.
577 * @param cls the `struct GNUNET_CORE_Handle`
578 * @param ntm the message we got
579 * @return #GNUNET_OK if the message is well-formed
582 check_notify_outbound (void *cls,
583 const struct NotifyTrafficMessage *ntm)
585 struct GNUNET_CORE_Handle *h = cls;
587 const struct GNUNET_MessageHeader *em;
589 GNUNET_break (GNUNET_NO == h->currently_down);
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Received outbound message from `%s'.\n",
592 GNUNET_i2s (&ntm->peer));
593 msize = ntohs (ntm->header.size) - sizeof (struct NotifyTrafficMessage);
594 if (msize < sizeof (struct GNUNET_MessageHeader))
597 return GNUNET_SYSERR;
599 em = (const struct GNUNET_MessageHeader *) &ntm[1];
600 if ( (GNUNET_NO == h->outbound_hdr_only) &&
601 (msize != ntohs (em->size)) )
604 return GNUNET_SYSERR;
611 * Handle outbound message received from CORE service. If applicable,
612 * notify the application.
614 * @param cls the `struct GNUNET_CORE_Handle`
615 * @param ntm the message we got
618 handle_notify_outbound (void *cls,
619 const struct NotifyTrafficMessage *ntm)
621 struct GNUNET_CORE_Handle *h = cls;
622 const struct GNUNET_MessageHeader *em;
624 GNUNET_break (GNUNET_NO == h->currently_down);
625 em = (const struct GNUNET_MessageHeader *) &ntm[1];
626 LOG (GNUNET_ERROR_TYPE_DEBUG,
627 "Received notification about transmission to `%s'.\n",
628 GNUNET_i2s (&ntm->peer));
629 if (NULL == h->outbound_notify)
634 h->outbound_notify (h->cls,
641 * Handle message received from CORE service notifying us that we are
642 * now allowed to send a message to a peer. If that message is still
643 * pending, put it into the queue to be transmitted.
645 * @param cls the `struct GNUNET_CORE_Handle`
646 * @param ntm the message we got
649 handle_send_ready (void *cls,
650 const struct SendMessageReady *smr)
652 struct GNUNET_CORE_Handle *h = cls;
653 struct PeerRecord *pr;
654 struct GNUNET_CORE_TransmitHandle *th;
655 struct SendMessage *sm;
656 struct GNUNET_MQ_Envelope *env;
657 struct GNUNET_TIME_Relative delay;
658 struct GNUNET_TIME_Relative overdue;
661 GNUNET_break (GNUNET_NO == h->currently_down);
662 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
670 LOG (GNUNET_ERROR_TYPE_DEBUG,
671 "Received notification about transmission readiness to `%s'.\n",
672 GNUNET_i2s (&smr->peer));
673 if (NULL == pr->th.peer)
675 /* request must have been cancelled between the original request
676 * and the response from CORE, ignore CORE's readiness */
680 if (ntohs (smr->smr_id) != th->smr_id)
682 /* READY message is for expired or cancelled message,
683 * ignore! (we should have already sent another request) */
686 /* ok, all good, send message out! */
688 env = GNUNET_MQ_msg_extra (sm,
690 GNUNET_MESSAGE_TYPE_CORE_SEND);
691 sm->priority = htonl ((uint32_t) th->priority);
692 sm->deadline = GNUNET_TIME_absolute_hton (th->deadline);
694 sm->cork = htonl ((uint32_t) th->cork);
695 sm->reserved = htonl (0);
696 ret = th->get_message (th->get_message_cls,
699 sm->header.size = htons (ret);
701 // GNUNET_assert (ret == th->msize); /* NOTE: API change! */
702 delay = GNUNET_TIME_absolute_get_duration (th->request_time);
703 overdue = GNUNET_TIME_absolute_get_duration (th->deadline);
704 if (overdue.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
705 LOG (GNUNET_ERROR_TYPE_WARNING,
706 "Transmitting overdue %u bytes to `%s' at priority %u with %s delay %s\n",
708 GNUNET_i2s (&pr->peer),
709 (unsigned int) th->priority,
710 GNUNET_STRINGS_relative_time_to_string (delay,
712 (th->cork) ? " (corked)" : "");
714 LOG (GNUNET_ERROR_TYPE_DEBUG,
715 "Transmitting %u bytes to `%s' at priority %u with %s delay %s\n",
717 GNUNET_i2s (&pr->peer),
718 (unsigned int) th->priority,
719 GNUNET_STRINGS_relative_time_to_string (delay,
721 (th->cork) ? " (corked)" : "");
722 GNUNET_MQ_send (h->mq,
728 * Our current client connection went down. Clean it up and try to
731 * @param h our handle to the core service
734 reconnect (struct GNUNET_CORE_Handle *h)
736 GNUNET_MQ_hd_fixed_size (init_reply,
737 GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY,
738 struct InitReplyMessage);
739 GNUNET_MQ_hd_fixed_size (connect_notify,
740 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT,
741 struct ConnectNotifyMessage);
742 GNUNET_MQ_hd_fixed_size (disconnect_notify,
743 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT,
744 struct DisconnectNotifyMessage);
745 GNUNET_MQ_hd_var_size (notify_inbound,
746 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND,
747 struct NotifyTrafficMessage);
748 GNUNET_MQ_hd_var_size (notify_outbound,
749 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND,
750 struct NotifyTrafficMessage);
751 GNUNET_MQ_hd_fixed_size (send_ready,
752 GNUNET_MESSAGE_TYPE_CORE_SEND_READY,
753 struct SendMessageReady);
754 struct GNUNET_MQ_MessageHandler handlers[] = {
755 make_init_reply_handler (h),
756 make_connect_notify_handler (h),
757 make_disconnect_notify_handler (h),
758 make_notify_inbound_handler (h),
759 make_notify_outbound_handler (h),
760 make_send_ready_handler (h),
761 GNUNET_MQ_handler_end ()
763 struct InitMessage *init;
764 struct GNUNET_MQ_Envelope *env;
768 GNUNET_assert (NULL == h->mq);
769 GNUNET_assert (GNUNET_YES == h->currently_down);
770 h->mq = GNUNET_CLIENT_connecT (h->cfg,
780 env = GNUNET_MQ_msg_extra (init,
781 sizeof (uint16_t) * h->hcnt,
782 GNUNET_MESSAGE_TYPE_CORE_INIT);
784 if (NULL != h->inbound_notify)
786 if (h->inbound_hdr_only)
787 opt |= GNUNET_CORE_OPTION_SEND_HDR_INBOUND;
789 opt |= GNUNET_CORE_OPTION_SEND_FULL_INBOUND;
791 if (NULL != h->outbound_notify)
793 if (h->outbound_hdr_only)
794 opt |= GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND;
796 opt |= GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND;
798 LOG (GNUNET_ERROR_TYPE_INFO,
799 "(Re)connecting to CORE service, monitoring messages of type %u\n",
801 init->options = htonl (opt);
802 ts = (uint16_t *) &init[1];
803 for (unsigned int hpos = 0; hpos < h->hcnt; hpos++)
804 ts[hpos] = htons (h->handlers[hpos].type);
805 GNUNET_MQ_send (h->mq,
811 * Connect to the core service. Note that the connection may complete
812 * (or fail) asynchronously.
814 * @param cfg configuration to use
815 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
816 * @param init callback to call once we have successfully
817 * connected to the core service
818 * @param connects function to call on peer connect, can be NULL
819 * @param disconnects function to call on peer disconnect / timeout, can be NULL
820 * @param inbound_notify function to call for all inbound messages, can be NULL
821 * @param inbound_hdr_only set to #GNUNET_YES if inbound_notify will only read the
822 * GNUNET_MessageHeader and hence we do not need to give it the full message;
823 * can be used to improve efficiency, ignored if @a inbound_notify is NULLL
824 * @param outbound_notify function to call for all outbound messages, can be NULL
825 * @param outbound_hdr_only set to #GNUNET_YES if outbound_notify will only read the
826 * GNUNET_MessageHeader and hence we do not need to give it the full message
827 * can be used to improve efficiency, ignored if @a outbound_notify is NULLL
828 * @param handlers callbacks for messages we care about, NULL-terminated
829 * @return handle to the core service (only useful for disconnect until 'init' is called);
830 * NULL on error (in this case, init is never called)
832 struct GNUNET_CORE_Handle *
833 GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
835 GNUNET_CORE_StartupCallback init,
836 GNUNET_CORE_ConnectEventHandler connects,
837 GNUNET_CORE_DisconnectEventHandler disconnects,
838 GNUNET_CORE_MessageCallback inbound_notify,
839 int inbound_hdr_only,
840 GNUNET_CORE_MessageCallback outbound_notify,
841 int outbound_hdr_only,
842 const struct GNUNET_CORE_MessageHandler *handlers)
844 struct GNUNET_CORE_Handle *h;
847 h = GNUNET_new (struct GNUNET_CORE_Handle);
851 h->connects = connects;
852 h->disconnects = disconnects;
853 h->inbound_notify = inbound_notify;
854 h->outbound_notify = outbound_notify;
855 h->inbound_hdr_only = inbound_hdr_only;
856 h->outbound_hdr_only = outbound_hdr_only;
857 h->currently_down = GNUNET_YES;
858 h->peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
860 if (NULL != handlers)
861 while (NULL != handlers[hcnt].callback)
863 h->handlers = GNUNET_new_array (hcnt + 1,
864 struct GNUNET_CORE_MessageHandler);
865 if (NULL != handlers)
868 hcnt * sizeof (struct GNUNET_CORE_MessageHandler));
870 GNUNET_assert (hcnt <
871 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
872 sizeof (struct InitMessage)) / sizeof (uint16_t));
873 LOG (GNUNET_ERROR_TYPE_DEBUG,
874 "Connecting to CORE service\n");
881 * Disconnect from the core service. This function can only
882 * be called *after* all pending #GNUNET_CORE_notify_transmit_ready()
883 * requests have been explicitly canceled.
885 * @param handle connection to core to disconnect
888 GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
890 LOG (GNUNET_ERROR_TYPE_DEBUG,
891 "Disconnecting from CORE service\n");
892 GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
893 &disconnect_and_free_peer_entry,
895 GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
896 handle->peers = NULL;
897 if (NULL != handle->reconnect_task)
899 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
900 handle->reconnect_task = NULL;
902 if (NULL != handle->mq)
904 GNUNET_MQ_destroy (handle->mq);
907 GNUNET_free (handle->handlers);
908 GNUNET_free (handle);
913 * Ask the core to call @a notify once it is ready to transmit the
914 * given number of bytes to the specified @a target. Must only be
915 * called after a connection to the respective peer has been
916 * established (and the client has been informed about this). You may
917 * have one request of this type pending for each connected peer at
918 * any time. If a peer disconnects, the application MUST call
919 * #GNUNET_CORE_notify_transmit_ready_cancel on the respective
920 * transmission request, if one such request is pending.
922 * @param handle connection to core service
923 * @param cork is corking allowed for this transmission?
924 * @param priority how important is the message?
925 * @param maxdelay how long can the message wait? Only effective if @a cork is #GNUNET_YES
926 * @param target who should receive the message, never NULL (can be this peer's identity for loopback)
927 * @param notify_size how many bytes of buffer space does @a notify want?
928 * @param notify function to call when buffer space is available;
929 * will be called with NULL on timeout; clients MUST cancel
930 * all pending transmission requests DURING the disconnect
932 * @param notify_cls closure for @a notify
933 * @return non-NULL if the notify callback was queued,
934 * NULL if we can not even queue the request (request already pending);
935 * if NULL is returned, @a notify will NOT be called.
937 struct GNUNET_CORE_TransmitHandle *
938 GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
940 enum GNUNET_CORE_Priority priority,
941 struct GNUNET_TIME_Relative maxdelay,
942 const struct GNUNET_PeerIdentity *target,
944 GNUNET_CONNECTION_TransmitReadyNotify notify,
947 struct PeerRecord *pr;
948 struct GNUNET_CORE_TransmitHandle *th;
949 struct SendMessageRequest *smr;
950 struct GNUNET_MQ_Envelope *env;
952 GNUNET_assert (NULL != notify);
953 if ( (notify_size > GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE) ||
954 (notify_size + sizeof (struct SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
959 LOG (GNUNET_ERROR_TYPE_DEBUG,
960 "Asking core for transmission of %u bytes to `%s'\n",
961 (unsigned int) notify_size,
962 GNUNET_i2s (target));
963 pr = GNUNET_CONTAINER_multipeermap_get (handle->peers,
967 /* attempt to send to peer that is not connected */
971 if (NULL != pr->th.peer)
973 /* attempting to queue a second request for the same destination */
980 sizeof (struct GNUNET_CORE_TransmitHandle));
982 th->get_message = notify;
983 th->get_message_cls = notify_cls;
984 th->request_time = GNUNET_TIME_absolute_get ();
985 if (GNUNET_YES == cork)
986 th->deadline = GNUNET_TIME_relative_to_absolute (maxdelay);
988 th->deadline = th->request_time;
989 th->priority = priority;
990 th->msize = notify_size;
992 env = GNUNET_MQ_msg (smr,
993 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
994 smr->priority = htonl ((uint32_t) th->priority);
995 smr->deadline = GNUNET_TIME_absolute_hton (th->deadline);
996 smr->peer = pr->peer;
997 smr->reserved = htonl (0);
998 smr->size = htons (th->msize);
999 smr->smr_id = htons (th->smr_id = pr->smr_id_gen++);
1000 GNUNET_MQ_send (handle->mq,
1002 LOG (GNUNET_ERROR_TYPE_DEBUG,
1003 "Transmission request added to queue\n");
1009 * Cancel the specified transmission-ready notification.
1011 * @param th handle that was returned by #GNUNET_CORE_notify_transmit_ready().
1014 GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle *th)
1016 struct PeerRecord *pr = th->peer;
1018 LOG (GNUNET_ERROR_TYPE_DEBUG,
1019 "Aborting transmission request to core for %u bytes to `%s'\n",
1020 (unsigned int) th->msize,
1021 GNUNET_i2s (&pr->peer));
1027 * Check if the given peer is currently connected. This function is for special
1028 * cirumstances (GNUNET_TESTBED uses it), normal users of the CORE API are
1029 * expected to track which peers are connected based on the connect/disconnect
1030 * callbacks from #GNUNET_CORE_connect(). This function is NOT part of the
1031 * 'versioned', 'official' API. The difference between this function and the
1032 * function GNUNET_CORE_is_peer_connected() is that this one returns
1033 * synchronously after looking in the CORE API cache. The function
1034 * GNUNET_CORE_is_peer_connected() sends a message to the CORE service and hence
1035 * its response is given asynchronously.
1037 * @param h the core handle
1038 * @param pid the identity of the peer to check if it has been connected to us
1039 * @return #GNUNET_YES if the peer is connected to us; #GNUNET_NO if not
1042 GNUNET_CORE_is_peer_connected_sync (const struct GNUNET_CORE_Handle *h,
1043 const struct GNUNET_PeerIdentity *pid)
1045 return GNUNET_CONTAINER_multipeermap_contains (h->peers,
1050 /* end of core_api.c */