2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017, 2019 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_list_tunnels.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_ListTunnels
42 GNUNET_CADET_TunnelsCB tunnels_cb;
45 * Info callback closure for @c tunnels_cb.
50 * Message queue to talk to CADET service.
52 struct GNUNET_MQ_Handle *mq;
55 * Configuration we use.
57 const struct GNUNET_CONFIGURATION_Handle *cfg;
62 struct GNUNET_SCHEDULER_Task *reconnect_task;
65 * Backoff for reconnect attempts.
67 struct GNUNET_TIME_Relative backoff;
72 * Process a local reply about info on all tunnels, pass info to the user.
74 * @param cls a `struct GNUNET_CADET_ListTunnels *`
75 * @param info Message itself.
78 handle_get_tunnels (void *cls,
79 const struct GNUNET_CADET_LocalInfoTunnel *info)
81 struct GNUNET_CADET_ListTunnels *lt = cls;
82 struct GNUNET_CADET_TunnelDetails td;
84 td.peer = info->destination;
85 td.channels = ntohl (info->channels);
86 td.connections = ntohl (info->connections);
87 td.estate = ntohs (info->estate);
88 td.cstate = ntohs (info->cstate);
89 lt->tunnels_cb (lt->tunnels_cb_cls,
95 * Process a local reply about info on all tunnels, pass info to the user.
97 * @param cls a `struct GNUNET_CADET_ListTunnels *`
98 * @param message Message itself.
101 handle_get_tunnels_end (void *cls,
102 const struct GNUNET_MessageHeader *msg)
104 struct GNUNET_CADET_ListTunnels *lt = cls;
108 lt->tunnels_cb (lt->tunnels_cb_cls,
110 GNUNET_CADET_list_tunnels_cancel (lt);
115 * Reconnect to the service and try again.
117 * @param cls a `struct GNUNET_CADET_ListTunnels` operation
120 reconnect (void *cls);
124 * Function called on connection trouble. Reconnects.
126 * @param cls a `struct GNUNET_CADET_ListTunnels`
127 * @param error error code from MQ
130 error_handler (void *cls,
131 enum GNUNET_MQ_Error error)
133 struct GNUNET_CADET_ListTunnels *lt = cls;
135 GNUNET_MQ_destroy (lt->mq);
137 lt->backoff = GNUNET_TIME_randomized_backoff (lt->backoff,
138 GNUNET_TIME_UNIT_MINUTES);
139 lt->reconnect_task = GNUNET_SCHEDULER_add_delayed (lt->backoff,
146 * Reconnect to the service and try again.
148 * @param cls a `struct GNUNET_CADET_ListTunnels` operation
151 reconnect (void *cls)
153 struct GNUNET_CADET_ListTunnels *lt = cls;
154 struct GNUNET_MQ_MessageHandler handlers[] = {
155 GNUNET_MQ_hd_fixed_size (get_tunnels,
156 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
157 struct GNUNET_CADET_LocalInfoTunnel,
159 GNUNET_MQ_hd_fixed_size (get_tunnels_end,
160 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS_END,
161 struct GNUNET_MessageHeader,
163 GNUNET_MQ_handler_end ()
165 struct GNUNET_MessageHeader *msg;
166 struct GNUNET_MQ_Envelope *env;
168 lt->reconnect_task = NULL;
169 lt->mq = GNUNET_CLIENT_connect (lt->cfg,
176 env = GNUNET_MQ_msg (msg,
177 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS);
178 GNUNET_MQ_send (lt->mq,
184 * Request information about tunnels of the running cadet peer.
185 * The callback will be called for every tunnel of the service.
186 * Only one info request (of any kind) can be active at once.
188 * @param cfg configuration to use
189 * @param callback Function to call with the requested data.
190 * @param callback_cls Closure for @c callback.
191 * @return NULL on error
193 struct GNUNET_CADET_ListTunnels *
194 GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg,
195 GNUNET_CADET_TunnelsCB callback,
198 struct GNUNET_CADET_ListTunnels *lt;
200 if (NULL == callback)
205 lt = GNUNET_new (struct GNUNET_CADET_ListTunnels);
206 lt->tunnels_cb = callback;
207 lt->tunnels_cb_cls = callback_cls;
220 * Cancel a monitor request. The monitor callback will not be called.
222 * @param lt operation handle
223 * @return Closure given to GNUNET_CADET_list_tunnels().
226 GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt)
228 void *ret = lt->tunnels_cb_cls;
231 GNUNET_MQ_destroy (lt->mq);
232 if (NULL != lt->reconnect_task)
233 GNUNET_SCHEDULER_cancel (lt->reconnect_task);
239 /* end of cadet_api_list_tunnels.c */