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_2.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 * Information we track for each peer.
42 * Corresponding CORE handle.
44 struct GNUNET_CORE_Handle *h;
47 * Message queue for the peer.
49 struct GNUNET_MQ_Handle *mq;
52 * Message we are currently trying to pass to the CORE service
53 * for this peer (from @e mq).
55 struct GNUNET_MQ_Envelope *env;
58 * Value the client returned when we connected, used
59 * as the closure in various places.
64 * Peer the record is about.
66 struct GNUNET_PeerIdentity peer;
69 * SendMessageRequest ID generator for this peer.
77 * Context for the core service connection.
79 struct GNUNET_CORE_Handle
83 * Configuration we're using.
85 const struct GNUNET_CONFIGURATION_Handle *cfg;
88 * Closure for the various callbacks.
93 * Function to call once we've handshaked with the core service.
95 GNUNET_CORE_StartupCallback init;
98 * Function to call whenever we're notified about a peer connecting.
100 GNUNET_CORE_ConnecTEventHandler connects;
103 * Function to call whenever we're notified about a peer disconnecting.
105 GNUNET_CORE_DisconnecTEventHandler disconnects;
108 * Function handlers for messages of particular type.
110 struct GNUNET_MQ_MessageHandler *handlers;
113 * Our message queue for transmissions to the service.
115 struct GNUNET_MQ_Handle *mq;
118 * Hash map listing all of the peers that we are currently
121 struct GNUNET_CONTAINER_MultiPeerMap *peers;
124 * Identity of this peer.
126 struct GNUNET_PeerIdentity me;
129 * ID of reconnect task (if any).
131 struct GNUNET_SCHEDULER_Task *reconnect_task;
134 * Current delay we use for re-trying to connect to core.
136 struct GNUNET_TIME_Relative retry_backoff;
139 * Number of entries in the handlers array.
144 * Did we ever get INIT?
152 * Our current client connection went down. Clean it up
153 * and try to reconnect!
155 * @param h our handle to the core service
158 reconnect (struct GNUNET_CORE_Handle *h);
162 * Task schedule to try to re-connect to core.
164 * @param cls the `struct GNUNET_CORE_Handle`
165 * @param tc task context
168 reconnect_task (void *cls)
170 struct GNUNET_CORE_Handle *h = cls;
172 h->reconnect_task = NULL;
173 LOG (GNUNET_ERROR_TYPE_DEBUG,
174 "Connecting to CORE service after delay\n");
180 * Notify clients about disconnect and free the entry for connected
183 * @param cls the `struct GNUNET_CORE_Handle *`
184 * @param key the peer identity (not used)
185 * @param value the `struct PeerRecord` to free.
186 * @return #GNUNET_YES (continue)
189 disconnect_and_free_peer_entry (void *cls,
190 const struct GNUNET_PeerIdentity *key,
193 struct GNUNET_CORE_Handle *h = cls;
194 struct PeerRecord *pr = value;
196 GNUNET_assert (pr->h == h);
197 if (NULL != h->disconnects)
198 h->disconnects (h->cls,
201 GNUNET_assert (GNUNET_YES ==
202 GNUNET_CONTAINER_multipeermap_remove (h->peers,
205 GNUNET_MQ_destroy (pr->mq);
206 GNUNET_assert (NULL == pr->mq);
213 * Close down any existing connection to the CORE service and
214 * try re-establishing it later.
216 * @param h our handle
219 reconnect_later (struct GNUNET_CORE_Handle *h)
221 GNUNET_assert (NULL == h->reconnect_task);
224 GNUNET_MQ_destroy (h->mq);
227 GNUNET_assert (NULL == h->reconnect_task);
229 GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
232 GNUNET_CONTAINER_multipeermap_iterate (h->peers,
233 &disconnect_and_free_peer_entry,
235 h->retry_backoff = GNUNET_TIME_STD_BACKOFF (h->retry_backoff);
240 * Error handler for the message queue to the CORE service.
241 * On errors, we reconnect.
243 * @param cls closure, a `struct GNUNET_CORE_Handle *`
244 * @param error error code
247 handle_mq_error (void *cls,
248 enum GNUNET_MQ_Error error)
250 struct GNUNET_CORE_Handle *h = cls;
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 * Inquire with CORE what options should be set for a message
261 * so that it is transmitted with the given @a priority and
262 * the given @a cork value.
264 * @param cork desired corking
265 * @param priority desired message priority
266 * @param[out] flags set to `flags` value for #GNUNET_MQ_set_options()
267 * @return `extra` argument to give to #GNUNET_MQ_set_options()
270 GNUNET_CORE_get_mq_options (int cork,
271 enum GNUNET_CORE_Priority priority,
274 *flags = ((uint64_t) priority) + (((uint64_t) cork) << 32);
280 * Implement sending functionality of a message queue for
281 * us sending messages to a peer.
283 * @param mq the message queue
284 * @param msg the message to send
285 * @param impl_state state of the implementation
288 core_mq_send_impl (struct GNUNET_MQ_Handle *mq,
289 const struct GNUNET_MessageHeader *msg,
292 struct PeerRecord *pr = impl_state;
293 struct GNUNET_CORE_Handle *h = pr->h;
294 struct SendMessageRequest *smr;
295 struct SendMessage *sm;
296 struct GNUNET_MQ_Envelope *env;
300 enum GNUNET_CORE_Priority priority;
302 GNUNET_assert (NULL == pr->env);
303 /* extract options from envelope */
304 env = GNUNET_MQ_get_current_envelope (mq);
305 GNUNET_break (NULL ==
306 GNUNET_MQ_env_get_options (env,
308 cork = (int) (flags >> 32);
309 priority = (uint32_t) flags;
311 /* check message size for sanity */
312 msize = ntohs (msg->size);
313 if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct SendMessage))
316 GNUNET_MQ_impl_send_continue (mq);
320 /* ask core for transmission */
321 LOG (GNUNET_ERROR_TYPE_DEBUG,
322 "Asking core for transmission of %u bytes to `%s'\n",
323 (unsigned int) msize,
324 GNUNET_i2s (&pr->peer));
325 env = GNUNET_MQ_msg (smr,
326 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
327 smr->priority = htonl ((uint32_t) priority);
328 // smr->deadline = GNUNET_TIME_absolute_hton (deadline);
329 smr->peer = pr->peer;
330 smr->reserved = htonl (0);
331 smr->size = htons (msize);
332 smr->smr_id = htons (++pr->smr_id_gen);
333 GNUNET_MQ_send (h->mq,
336 /* prepare message with actual transmission data */
337 pr->env = GNUNET_MQ_msg_nested_mh (sm,
338 GNUNET_MESSAGE_TYPE_CORE_SEND,
340 sm->priority = htonl ((uint32_t) priority);
341 // sm->deadline = GNUNET_TIME_absolute_hton (deadline);
343 sm->cork = htonl ((uint32_t) cork);
344 sm->reserved = htonl (0);
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Calling get_message with buffer of %u bytes (%s)\n",
347 (unsigned int) msize,
348 cork ? "corked" : "uncorked");
353 * Handle destruction of a message queue. Implementations must not
354 * free @a mq, but should take care of @a impl_state.
356 * @param mq the message queue to destroy
357 * @param impl_state state of the implementation
360 core_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
363 struct PeerRecord *pr = impl_state;
365 GNUNET_assert (mq == pr->mq);
371 * Implementation function that cancels the currently sent message.
372 * Should basically undo whatever #mq_send_impl() did.
374 * @param mq message queue
375 * @param impl_state state specific to the implementation
378 core_mq_cancel_impl (struct GNUNET_MQ_Handle *mq,
381 struct PeerRecord *pr = impl_state;
383 GNUNET_assert (NULL != pr->env);
384 GNUNET_MQ_discard (pr->env);
390 * We had an error processing a message we forwarded from a peer to
391 * the CORE service. We should just complain about it but otherwise
392 * continue processing.
395 * @param error error code
398 core_mq_error_handler (void *cls,
399 enum GNUNET_MQ_Error error)
401 /* struct PeerRecord *pr = cls; */
408 * Add the given peer to the list of our connected peers
409 * and create the respective data structures and notify
412 * @param h the core handle
413 * @param peer the peer that is connecting to us
416 connect_peer (struct GNUNET_CORE_Handle *h,
417 const struct GNUNET_PeerIdentity *peer)
419 struct PeerRecord *pr;
423 pr = GNUNET_new (struct PeerRecord);
426 GNUNET_assert (GNUNET_YES ==
427 GNUNET_CONTAINER_multipeermap_put (h->peers,
430 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
431 pr->mq = GNUNET_MQ_queue_for_callbacks (&core_mq_send_impl,
432 &core_mq_destroy_impl,
433 &core_mq_cancel_impl,
436 &core_mq_error_handler,
438 /* get our default options */
439 extra = GNUNET_CORE_get_mq_options (GNUNET_NO,
440 GNUNET_CORE_PRIO_BEST_EFFORT,
442 GNUNET_MQ_set_options (pr->mq,
445 if (NULL != h->connects)
447 pr->client_cls = h->connects (h->cls,
450 GNUNET_MQ_set_handlers_closure (pr->mq,
457 * Handle init reply message received from CORE service. Notify
458 * application that we are now connected to the CORE. Also fake
459 * loopback connection.
461 * @param cls the `struct GNUNET_CORE_Handle`
462 * @param m the init reply
465 handle_init_reply (void *cls,
466 const struct InitReplyMessage *m)
468 struct GNUNET_CORE_Handle *h = cls;
469 GNUNET_CORE_StartupCallback init;
471 GNUNET_break (0 == ntohl (m->reserved));
472 h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
473 if (NULL != (init = h->init))
475 /* mark so we don't call init on reconnect */
477 h->me = m->my_identity;
478 LOG (GNUNET_ERROR_TYPE_DEBUG,
479 "Connected to core service of peer `%s'.\n",
480 GNUNET_i2s (&h->me));
481 h->have_init = GNUNET_YES;
487 LOG (GNUNET_ERROR_TYPE_DEBUG,
488 "Successfully reconnected to core service.\n");
489 if (GNUNET_NO == h->have_init)
491 h->me = m->my_identity;
492 h->have_init = GNUNET_YES;
496 GNUNET_break (0 == memcmp (&h->me,
498 sizeof (struct GNUNET_PeerIdentity)));
501 /* fake 'connect to self' */
508 * Handle connect message received from CORE service.
509 * Notify the application about the new connection.
511 * @param cls the `struct GNUNET_CORE_Handle`
512 * @param cnm the connect message
515 handle_connect_notify (void *cls,
516 const struct ConnectNotifyMessage *cnm)
518 struct GNUNET_CORE_Handle *h = cls;
519 struct PeerRecord *pr;
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "Received notification about connection from `%s'.\n",
523 GNUNET_i2s (&cnm->peer));
524 if (0 == memcmp (&h->me,
526 sizeof (struct GNUNET_PeerIdentity)))
528 /* connect to self!? */
532 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
546 * Handle disconnect message received from CORE service.
547 * Notify the application about the lost connection.
549 * @param cls the `struct GNUNET_CORE_Handle`
550 * @param dnm message about the disconnect event
553 handle_disconnect_notify (void *cls,
554 const struct DisconnectNotifyMessage *dnm)
556 struct GNUNET_CORE_Handle *h = cls;
557 struct PeerRecord *pr;
559 if (0 == memcmp (&h->me,
561 sizeof (struct GNUNET_PeerIdentity)))
563 /* disconnect from self!? */
567 GNUNET_break (0 == ntohl (dnm->reserved));
568 LOG (GNUNET_ERROR_TYPE_DEBUG,
569 "Received notification about disconnect from `%s'.\n",
570 GNUNET_i2s (&dnm->peer));
571 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
579 disconnect_and_free_peer_entry (h,
586 * Check that message received from CORE service is well-formed.
588 * @param cls the `struct GNUNET_CORE_Handle`
589 * @param ntm the message we got
590 * @return #GNUNET_OK if the message is well-formed
593 check_notify_inbound (void *cls,
594 const struct NotifyTrafficMessage *ntm)
597 const struct GNUNET_MessageHeader *em;
599 msize = ntohs (ntm->header.size) - sizeof (struct NotifyTrafficMessage);
600 if (msize < sizeof (struct GNUNET_MessageHeader))
603 return GNUNET_SYSERR;
605 em = (const struct GNUNET_MessageHeader *) &ntm[1];
606 if (msize != ntohs (em->size))
609 return GNUNET_SYSERR;
616 * Handle inbound message received from CORE service. If applicable,
617 * notify the application.
619 * @param cls the `struct GNUNET_CORE_Handle`
620 * @param ntm the message we got from CORE.
623 handle_notify_inbound (void *cls,
624 const struct NotifyTrafficMessage *ntm)
626 struct GNUNET_CORE_Handle *h = cls;
627 const struct GNUNET_MessageHeader *em;
628 struct PeerRecord *pr;
630 LOG (GNUNET_ERROR_TYPE_DEBUG,
631 "Received inbound message from `%s'.\n",
632 GNUNET_i2s (&ntm->peer));
633 em = (const struct GNUNET_MessageHeader *) &ntm[1];
634 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
642 GNUNET_MQ_inject_message (pr->mq,
648 * Handle message received from CORE service notifying us that we are
649 * now allowed to send a message to a peer. If that message is still
650 * pending, put it into the queue to be transmitted.
652 * @param cls the `struct GNUNET_CORE_Handle`
653 * @param smr the message we got
656 handle_send_ready (void *cls,
657 const struct SendMessageReady *smr)
659 struct GNUNET_CORE_Handle *h = cls;
660 struct PeerRecord *pr;
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));
675 /* request must have been cancelled between the original request
676 * and the response from CORE, ignore CORE's readiness */
679 if (ntohs (smr->smr_id) != pr->smr_id_gen)
681 /* READY message is for expired or cancelled message,
682 * ignore! (we should have already sent another request) */
686 /* ok, all good, send message out! */
687 GNUNET_MQ_send (h->mq,
690 GNUNET_MQ_impl_send_continue (pr->mq);
695 * Our current client connection went down. Clean it up and try to
698 * @param h our handle to the core service
701 reconnect (struct GNUNET_CORE_Handle *h)
703 struct GNUNET_MQ_MessageHandler handlers[] = {
704 GNUNET_MQ_hd_fixed_size (init_reply,
705 GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY,
706 struct InitReplyMessage,
708 GNUNET_MQ_hd_fixed_size (connect_notify,
709 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT,
710 struct ConnectNotifyMessage,
712 GNUNET_MQ_hd_fixed_size (disconnect_notify,
713 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT,
714 struct DisconnectNotifyMessage,
716 GNUNET_MQ_hd_var_size (notify_inbound,
717 GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND,
718 struct NotifyTrafficMessage,
720 GNUNET_MQ_hd_fixed_size (send_ready,
721 GNUNET_MESSAGE_TYPE_CORE_SEND_READY,
722 struct SendMessageReady,
724 GNUNET_MQ_handler_end ()
726 struct InitMessage *init;
727 struct GNUNET_MQ_Envelope *env;
730 GNUNET_assert (NULL == h->mq);
731 h->mq = GNUNET_CLIENT_connecT (h->cfg,
741 env = GNUNET_MQ_msg_extra (init,
742 sizeof (uint16_t) * h->hcnt,
743 GNUNET_MESSAGE_TYPE_CORE_INIT);
744 LOG (GNUNET_ERROR_TYPE_INFO,
745 "(Re)connecting to CORE service\n");
746 init->options = htonl (0);
747 ts = (uint16_t *) &init[1];
748 for (unsigned int hpos = 0; hpos < h->hcnt; hpos++)
749 ts[hpos] = htons (h->handlers[hpos].type);
750 GNUNET_MQ_send (h->mq,
756 * Connect to the core service. Note that the connection may complete
757 * (or fail) asynchronously.
759 * @param cfg configuration to use
760 * @param cls closure for the various callbacks that follow (including handlers in the handlers array)
761 * @param init callback to call once we have successfully
762 * connected to the core service
763 * @param connects function to call on peer connect, can be NULL
764 * @param disconnects function to call on peer disconnect / timeout, can be NULL
765 * @param handlers callbacks for messages we care about, NULL-terminated
766 * @return handle to the core service (only useful for disconnect until @a init is called);
767 * NULL on error (in this case, init is never called)
769 struct GNUNET_CORE_Handle *
770 GNUNET_CORE_connecT (const struct GNUNET_CONFIGURATION_Handle *cfg,
772 GNUNET_CORE_StartupCallback init,
773 GNUNET_CORE_ConnecTEventHandler connects,
774 GNUNET_CORE_DisconnecTEventHandler disconnects,
775 const struct GNUNET_MQ_MessageHandler *handlers)
777 struct GNUNET_CORE_Handle *h;
780 h = GNUNET_new (struct GNUNET_CORE_Handle);
784 h->connects = connects;
785 h->disconnects = disconnects;
786 h->peers = GNUNET_CONTAINER_multipeermap_create (128,
789 if (NULL != handlers)
790 while (NULL != handlers[hcnt].cb)
792 h->handlers = GNUNET_new_array (hcnt + 1,
793 struct GNUNET_MQ_MessageHandler);
794 if (NULL != handlers)
795 GNUNET_memcpy (h->handlers,
797 hcnt * sizeof (struct GNUNET_MQ_MessageHandler));
799 GNUNET_assert (hcnt <
800 (GNUNET_SERVER_MAX_MESSAGE_SIZE -
801 sizeof (struct InitMessage)) / sizeof (uint16_t));
802 LOG (GNUNET_ERROR_TYPE_DEBUG,
803 "Connecting to CORE service\n");
807 GNUNET_CORE_disconnect (h);
815 * Disconnect from the core service.
817 * @param handle connection to core to disconnect
820 GNUNET_CORE_disconnecT (struct GNUNET_CORE_Handle *handle)
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Disconnecting from CORE service\n");
824 GNUNET_CONTAINER_multipeermap_iterate (handle->peers,
825 &disconnect_and_free_peer_entry,
827 GNUNET_CONTAINER_multipeermap_destroy (handle->peers);
828 handle->peers = NULL;
829 if (NULL != handle->reconnect_task)
831 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
832 handle->reconnect_task = NULL;
834 if (NULL != handle->mq)
836 GNUNET_MQ_destroy (handle->mq);
839 GNUNET_free (handle->handlers);
840 GNUNET_free (handle);
845 * Obtain the message queue for a connected peer.
847 * @param h the core handle
848 * @param pid the identity of the peer to check if it has been connected to us
849 * @return NULL if peer is not connected
851 struct GNUNET_MQ_Handle *
852 GNUNET_CORE_get_mq (const struct GNUNET_CORE_Handle *h,
853 const struct GNUNET_PeerIdentity *pid)
855 struct PeerRecord *pr;
857 pr = GNUNET_CONTAINER_multipeermap_get (h->peers,
865 /* end of core_api.c */