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
43 GNUNET_CADET_TunnelsCB tunnels_cb;
46 * Info callback closure for @c tunnels_cb.
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;
74 * Process a local reply about info on all tunnels, pass info to the user.
76 * @param cls a `struct GNUNET_CADET_ListTunnels *`
77 * @param info Message itself.
80 handle_get_tunnels (void *cls,
81 const struct GNUNET_CADET_LocalInfoTunnel *info)
83 struct GNUNET_CADET_ListTunnels *lt = cls;
84 struct GNUNET_CADET_TunnelDetails td;
86 td.peer = info->destination;
87 td.channels = ntohl (info->channels);
88 td.connections = ntohl (info->connections);
89 td.estate = ntohs (info->estate);
90 td.cstate = ntohs (info->cstate);
91 lt->tunnels_cb (lt->tunnels_cb_cls,
97 * Process a local reply about info on all tunnels, pass info to the user.
99 * @param cls a `struct GNUNET_CADET_ListTunnels *`
100 * @param message Message itself.
103 handle_get_tunnels_end (void *cls,
104 const struct GNUNET_MessageHeader *msg)
106 struct GNUNET_CADET_ListTunnels *lt = cls;
109 lt->tunnels_cb (lt->tunnels_cb_cls,
111 GNUNET_CADET_list_tunnels_cancel (lt);
116 * Reconnect to the service and try again.
118 * @param cls a `struct GNUNET_CADET_ListTunnels` operation
121 reconnect (void *cls);
125 * Function called on connection trouble. Reconnects.
127 * @param cls a `struct GNUNET_CADET_ListTunnels`
128 * @param error error code from MQ
131 error_handler (void *cls,
132 enum GNUNET_MQ_Error error)
134 struct GNUNET_CADET_ListTunnels *lt = cls;
136 GNUNET_MQ_destroy (lt->mq);
138 lt->backoff = GNUNET_TIME_randomized_backoff (lt->backoff,
139 GNUNET_TIME_UNIT_MINUTES);
140 lt->reconnect_task = GNUNET_SCHEDULER_add_delayed (lt->backoff,
147 * Reconnect to the service and try again.
149 * @param cls a `struct GNUNET_CADET_ListTunnels` operation
152 reconnect (void *cls)
154 struct GNUNET_CADET_ListTunnels *lt = cls;
155 struct GNUNET_MQ_MessageHandler handlers[] = {
156 GNUNET_MQ_hd_fixed_size (get_tunnels,
157 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
158 struct GNUNET_CADET_LocalInfoTunnel,
160 GNUNET_MQ_hd_fixed_size (get_tunnels_end,
161 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS_END,
162 struct GNUNET_MessageHeader,
164 GNUNET_MQ_handler_end ()
166 struct GNUNET_MessageHeader *msg;
167 struct GNUNET_MQ_Envelope *env;
169 lt->reconnect_task = NULL;
170 lt->mq = GNUNET_CLIENT_connect (lt->cfg,
177 env = GNUNET_MQ_msg (msg,
178 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS);
179 GNUNET_MQ_send (lt->mq,
185 * Request information about tunnels of the running cadet peer.
186 * The callback will be called for every tunnel of the service.
187 * Only one info request (of any kind) can be active at once.
189 * @param cfg configuration to use
190 * @param callback Function to call with the requested data.
191 * @param callback_cls Closure for @c callback.
192 * @return NULL on error
194 struct GNUNET_CADET_ListTunnels *
195 GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg,
196 GNUNET_CADET_TunnelsCB callback,
199 struct GNUNET_CADET_ListTunnels *lt;
201 if (NULL == callback)
206 lt = GNUNET_new (struct GNUNET_CADET_ListTunnels);
207 lt->tunnels_cb = callback;
208 lt->tunnels_cb_cls = callback_cls;
221 * Cancel a monitor request. The monitor callback will not be called.
223 * @param lt operation handle
224 * @return Closure given to GNUNET_CADET_list_tunnels().
227 GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt)
229 void *ret = lt->tunnels_cb_cls;
232 GNUNET_MQ_destroy (lt->mq);
233 if (NULL != lt->reconnect_task)
234 GNUNET_SCHEDULER_cancel (lt->reconnect_task);
240 /* end of cadet_api_list_tunnels.c */