2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 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.
22 * @file transport/transport_api.c
23 * @brief library to obtain our HELLO from our transport service
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_transport_service.h"
33 #include "transport.h"
37 * Functions to call with this peer's HELLO.
39 struct GNUNET_TRANSPORT_GetHelloHandle
45 const struct GNUNET_CONFIGURATION_Handle *cfg;
50 struct GNUNET_MQ_Handle *mq;
53 * Callback to call once we got our HELLO.
55 GNUNET_TRANSPORT_HelloUpdateCallback rec;
63 * Task for calling the HelloUpdateCallback when we already have a HELLO
65 struct GNUNET_SCHEDULER_Task *notify_task;
68 * ID of the task trying to reconnect to the service.
70 struct GNUNET_SCHEDULER_Task *reconnect_task;
73 * Delay until we try to reconnect.
75 struct GNUNET_TIME_Relative reconnect_delay;
81 * Function we use for checking incoming HELLO messages.
83 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
84 * @param msg message received
85 * @return #GNUNET_OK if message is well-formed
88 check_hello (void *cls,
89 const struct GNUNET_MessageHeader *msg)
91 struct GNUNET_PeerIdentity me;
94 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
102 (unsigned int) ntohs (msg->size),
109 * Function we use for handling incoming HELLO messages.
111 * @param cls closure, a `struct GNUNET_TRANSPORT_GetHelloHandle *`
112 * @param msg message received
115 handle_hello (void *cls,
116 const struct GNUNET_MessageHeader *msg)
118 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
120 ghh->rec (ghh->rec_cls,
126 * Function that will schedule the job that will try
127 * to connect us again to the client.
129 * @param ghh transport service to reconnect
132 schedule_reconnect (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
136 * Generic error handler, called with the appropriate
137 * error code and the same closure specified at the creation of
139 * Not every message queue implementation supports an error handler.
141 * @param cls closure with the `struct GNUNET_TRANSPORT_Handle *`
142 * @param error error code
145 mq_error_handler (void *cls,
146 enum GNUNET_MQ_Error error)
148 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Error receiving from transport service, disconnecting temporarily.\n");
152 GNUNET_MQ_destroy (ghh->mq);
154 schedule_reconnect (ghh);
159 * Try again to connect to transport service.
161 * @param cls the handle to the transport service
164 reconnect (void *cls)
166 GNUNET_MQ_hd_var_size (hello,
167 GNUNET_MESSAGE_TYPE_HELLO,
168 struct GNUNET_MessageHeader);
169 struct GNUNET_TRANSPORT_GetHelloHandle *ghh = cls;
170 struct GNUNET_MQ_MessageHandler handlers[] = {
171 make_hello_handler (ghh),
172 GNUNET_MQ_handler_end ()
174 struct GNUNET_MQ_Envelope *env;
175 struct StartMessage *s;
177 ghh->reconnect_task = NULL;
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Connecting to transport service.\n");
180 GNUNET_assert (NULL == ghh->mq);
181 ghh->mq = GNUNET_CLIENT_connecT (ghh->cfg,
188 env = GNUNET_MQ_msg (s,
189 GNUNET_MESSAGE_TYPE_TRANSPORT_START);
190 s->options = htonl (0);
191 GNUNET_MQ_send (ghh->mq,
197 * Function that will schedule the job that will try
198 * to connect us again to the client.
200 * @param ghh transport service to reconnect
203 schedule_reconnect (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
205 ghh->reconnect_task =
206 GNUNET_SCHEDULER_add_delayed (ghh->reconnect_delay,
209 ghh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (ghh->reconnect_delay);
214 * Obtain the HELLO message for this peer. The callback given in this function
215 * is never called synchronously.
217 * @param cfg configuration
218 * @param rec function to call with the HELLO, sender will be our peer
219 * identity; message and sender will be NULL on timeout
220 * (handshake with transport service pending/failed).
221 * cost estimate will be 0.
222 * @param rec_cls closure for @a rec
223 * @return handle to cancel the operation
225 struct GNUNET_TRANSPORT_GetHelloHandle *
226 GNUNET_TRANSPORT_get_hello (const struct GNUNET_CONFIGURATION_Handle *cfg,
227 GNUNET_TRANSPORT_HelloUpdateCallback rec,
230 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
232 ghh = GNUNET_new (struct GNUNET_TRANSPORT_GetHelloHandle);
234 ghh->rec_cls = rec_cls;
247 * Stop receiving updates about changes to our HELLO message.
249 * @param ghh handle to cancel
252 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh)
256 GNUNET_MQ_destroy (ghh->mq);
263 /* end of transport_api_get_hello.c */