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_hello_get.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_hello_service.h"
33 #include "transport.h"
37 * Functions to call with this peer's HELLO.
39 struct GNUNET_TRANSPORT_HelloGetHandle
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;
78 * Type of HELLOs client cares about.
80 enum GNUNET_TRANSPORT_AddressClass ac;
85 * Function we use for checking incoming HELLO messages.
87 * @param cls closure, a `struct GNUNET_TRANSPORT_Handle *`
88 * @param msg message received
89 * @return #GNUNET_OK if message is well-formed
92 check_hello (void *cls,
93 const struct GNUNET_MessageHeader *msg)
95 struct GNUNET_PeerIdentity me;
98 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
102 return GNUNET_SYSERR;
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
106 (unsigned int) ntohs (msg->size),
113 * Function we use for handling incoming HELLO messages.
115 * @param cls closure, a `struct GNUNET_TRANSPORT_HelloGetHandle *`
116 * @param msg message received
119 handle_hello (void *cls,
120 const struct GNUNET_MessageHeader *msg)
122 struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
124 ghh->rec (ghh->rec_cls,
130 * Function that will schedule the job that will try
131 * to connect us again to the client.
133 * @param ghh transport service to reconnect
136 schedule_reconnect (struct GNUNET_TRANSPORT_HelloGetHandle *ghh);
140 * Generic error handler, called with the appropriate
141 * error code and the same closure specified at the creation of
143 * Not every message queue implementation supports an error handler.
145 * @param cls closure with the `struct GNUNET_TRANSPORT_Handle *`
146 * @param error error code
149 mq_error_handler (void *cls,
150 enum GNUNET_MQ_Error error)
152 struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Error receiving from transport service, disconnecting temporarily.\n");
156 GNUNET_MQ_destroy (ghh->mq);
158 schedule_reconnect (ghh);
163 * Try again to connect to transport service.
165 * @param cls the handle to the transport service
168 reconnect (void *cls)
170 struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
171 struct GNUNET_MQ_MessageHandler handlers[] = {
172 GNUNET_MQ_hd_var_size (hello,
173 GNUNET_MESSAGE_TYPE_HELLO,
174 struct GNUNET_MessageHeader,
176 GNUNET_MQ_handler_end ()
178 struct GNUNET_MQ_Envelope *env;
179 struct StartMessage *s;
181 ghh->reconnect_task = NULL;
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Connecting to transport service.\n");
184 GNUNET_assert (NULL == ghh->mq);
185 ghh->mq = GNUNET_CLIENT_connect (ghh->cfg,
192 env = GNUNET_MQ_msg (s,
193 GNUNET_MESSAGE_TYPE_TRANSPORT_START);
194 s->options = htonl (0);
195 GNUNET_MQ_send (ghh->mq,
201 * Function that will schedule the job that will try
202 * to connect us again to the client.
204 * @param ghh transport service to reconnect
207 schedule_reconnect (struct GNUNET_TRANSPORT_HelloGetHandle *ghh)
209 ghh->reconnect_task =
210 GNUNET_SCHEDULER_add_delayed (ghh->reconnect_delay,
213 ghh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (ghh->reconnect_delay);
218 * Obtain the HELLO message for this peer. The callback given in this function
219 * is never called synchronously.
221 * @param cfg configuration
222 * @param ac which network type should the addresses from the HELLO belong to?
223 * @param rec function to call with the HELLO, sender will be our peer
224 * identity; message and sender will be NULL on timeout
225 * (handshake with transport service pending/failed).
226 * cost estimate will be 0.
227 * @param rec_cls closure for @a rec
228 * @return handle to cancel the operation
230 struct GNUNET_TRANSPORT_HelloGetHandle *
231 GNUNET_TRANSPORT_hello_get (const struct GNUNET_CONFIGURATION_Handle *cfg,
232 enum GNUNET_TRANSPORT_AddressClass ac,
233 GNUNET_TRANSPORT_HelloUpdateCallback rec,
236 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
238 ghh = GNUNET_new (struct GNUNET_TRANSPORT_HelloGetHandle);
240 ghh->rec_cls = rec_cls;
254 * Stop receiving updates about changes to our HELLO message.
256 * @param ghh handle to cancel
259 GNUNET_TRANSPORT_hello_get_cancel (struct GNUNET_TRANSPORT_HelloGetHandle *ghh)
261 if (NULL != ghh->reconnect_task)
263 GNUNET_SCHEDULER_cancel (ghh->reconnect_task);
264 ghh->reconnect_task = NULL;
268 GNUNET_MQ_destroy (ghh->mq);
275 /* end of transport_api_hello_get.c */