2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file cadet/cadet_api_get_tunnel.c
22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_cadet_service.h"
31 #include "cadet_protocol.h"
37 struct GNUNET_CADET_GetTunnel
43 GNUNET_CADET_TunnelCB callback;
46 * Closure for @e callback.
51 * Message queue to talk to CADET service.
53 struct GNUNET_MQ_Handle *mq;
56 * Configuration we use.
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
63 struct GNUNET_SCHEDULER_Task *reconnect_task;
66 * Backoff for reconnect attempts.
68 struct GNUNET_TIME_Relative backoff;
71 * Peer we want information about.
73 struct GNUNET_PeerIdentity id;
79 * Check that message received from CADET service is well-formed.
81 * @param cls the `struct GNUNET_CADET_Handle`
82 * @param msg the message we got
83 * @return #GNUNET_OK if the message is well-formed,
84 * #GNUNET_SYSERR otherwise
87 check_get_tunnel (void *cls,
88 const struct GNUNET_CADET_LocalInfoTunnel *msg)
96 /* Verify message sanity */
97 msize = ntohs (msg->header.size);
98 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
102 return GNUNET_SYSERR;
104 ch_n = ntohl (msg->channels);
105 c_n = ntohl (msg->connections);
106 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
107 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "m:%u, e: %u (%u ch, %u conn)\n",
113 (unsigned int) msize,
114 (unsigned int) esize,
117 return GNUNET_SYSERR;
124 * Process a local tunnel info reply, pass info to the user.
126 * @param cls a `struct GNUNET_CADET_GetTunnel *`
127 * @param msg Message itself.
130 handle_get_tunnel (void *cls,
131 const struct GNUNET_CADET_LocalInfoTunnel *msg)
133 struct GNUNET_CADET_GetTunnel *gt = cls;
136 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
137 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
139 ch_n = ntohl (msg->channels);
140 c_n = ntohl (msg->connections);
142 /* Call Callback with tunnel info. */
143 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
144 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
145 gt->callback (gt->callback_cls,
152 ntohs (msg->cstate));
157 * Reconnect to the service and try again.
159 * @param cls a `struct GNUNET_CADET_GetTunnel` operation
162 reconnect (void *cls);
166 * Function called on connection trouble. Reconnects.
168 * @param cls a `struct GNUNET_CADET_GetTunnel`
169 * @param error error code from MQ
172 error_handler (void *cls,
173 enum GNUNET_MQ_Error error)
175 struct GNUNET_CADET_GetTunnel *gt = cls;
177 GNUNET_MQ_destroy (gt->mq);
179 gt->backoff = GNUNET_TIME_randomized_backoff (gt->backoff,
180 GNUNET_TIME_UNIT_MINUTES);
181 gt->reconnect_task = GNUNET_SCHEDULER_add_delayed (gt->backoff,
188 * Reconnect to the service and try again.
190 * @param cls a `struct GNUNET_CADET_GetTunnel` operation
193 reconnect (void *cls)
195 struct GNUNET_CADET_GetTunnel *gt = cls;
196 struct GNUNET_MQ_MessageHandler handlers[] = {
197 GNUNET_MQ_hd_var_size (get_tunnel,
198 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
199 struct GNUNET_CADET_LocalInfoTunnel,
201 GNUNET_MQ_handler_end ()
203 struct GNUNET_MQ_Envelope *env;
204 struct GNUNET_CADET_LocalInfo *msg;
206 gt->reconnect_task = NULL;
207 gt->mq = GNUNET_CLIENT_connect (gt->cfg,
214 env = GNUNET_MQ_msg (msg,
215 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
217 GNUNET_MQ_send (gt->mq,
222 * Request information about a tunnel of the running cadet peer.
223 * The callback will be called for the tunnel once.
225 * @param cfg configuration to use
226 * @param id Peer whose tunnel to examine.
227 * @param callback Function to call with the requested data.
228 * @param callback_cls Closure for @c callback.
229 * @return NULL on error
231 struct GNUNET_CADET_GetTunnel *
232 GNUNET_CADET_get_tunnel (const struct GNUNET_CONFIGURATION_Handle *cfg,
233 const struct GNUNET_PeerIdentity *id,
234 GNUNET_CADET_TunnelCB callback,
237 struct GNUNET_CADET_GetTunnel *gt;
239 if (NULL == callback)
244 gt = GNUNET_new (struct GNUNET_CADET_GetTunnel);
245 gt->callback = callback;
246 gt->callback_cls = callback_cls;
260 * Cancel a monitor request. The monitor callback will not be called.
262 * @param lt operation handle
263 * @return Closure given to #GNUNET_CADET_get_tunnel(), if any.
266 GNUNET_CADET_get_tunnel_cancel (struct GNUNET_CADET_GetTunnel *gt)
268 void *ret = gt->callback_cls;
271 GNUNET_MQ_destroy (gt->mq);
272 if (NULL != gt->reconnect_task)
273 GNUNET_SCHEDULER_cancel (gt->reconnect_task);
278 /* end of cadet_api_get_tunnel.c */