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_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 * Send message of @a type to CADET service of @a h
76 * @param h handle to CADET service
77 * @param type message type of trivial information request to send
80 send_info_request (struct GNUNET_CADET_Handle *h,
83 struct GNUNET_MessageHeader *msg;
84 struct GNUNET_MQ_Envelope *env;
86 env = GNUNET_MQ_msg (msg,
88 GNUNET_MQ_send (h->mq,
94 * Check that message received from CADET service is well-formed.
96 * @param cls the `struct GNUNET_CADET_Handle`
97 * @param message the message we got
98 * @return #GNUNET_OK if the message is well-formed,
99 * #GNUNET_SYSERR otherwise
102 check_get_tunnels (void *cls,
103 const struct GNUNET_MessageHeader *message)
108 esize = ntohs (message->size);
109 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == esize)
111 if (sizeof (struct GNUNET_MessageHeader) == esize)
113 return GNUNET_SYSERR;
118 * Process a local reply about info on all tunnels, pass info to the user.
120 * @param cls Closure (Cadet handle).
121 * @param message Message itself.
124 handle_get_tunnels (void *cls,
125 const struct GNUNET_MessageHeader *msg)
127 struct GNUNET_CADET_Handle *h = cls;
128 const struct GNUNET_CADET_LocalInfoTunnel *info =
129 (const struct GNUNET_CADET_LocalInfoTunnel *) msg;
131 if (NULL == h->info_cb.tunnels_cb)
133 if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) == ntohs (msg->size))
134 h->info_cb.tunnels_cb (h->info_cls,
136 ntohl (info->channels),
137 ntohl (info->connections),
138 ntohs (info->estate),
139 ntohs (info->cstate));
141 h->info_cb.tunnels_cb (h->info_cls,
151 reconnect (void *cls)
153 struct GNUNET_CADET_ListTunnels *lt = cls;
154 struct GNUNET_MQ_MessageHandler *handlers[] = {
155 GNUNET_MQ_hd_var_size (get_tunnels,
156 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
157 struct GNUNET_MessageHeader,
159 GNUNET_MQ_handler_end ()
161 struct GNUNET_MessageHeader *msg;
162 struct GNUNET_MQ_Envelope *env;
164 cm->mq = GNUNET_CLIENT_connect (cm->cfg,
170 env = GNUNET_MQ_msg (msg,
172 GNUNET_MQ_send (cm->mq,
178 * Request information about tunnels of the running cadet peer.
179 * The callback will be called for every tunnel of the service.
180 * Only one info request (of any kind) can be active at once.
182 * WARNING: unstable API, likely to change in the future!
184 * @param h Handle to the cadet peer.
185 * @param callback Function to call with the requested data.
186 * @param callback_cls Closure for @c callback.
187 * @return #GNUNET_OK / #GNUNET_SYSERR
189 struct GNUNET_CADET_ListTunnels *
190 GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg,
191 GNUNET_CADET_TunnelsCB callback,
195 if (NULL != h->info_cb.tunnels_cb)
198 return GNUNET_SYSERR;
200 send_info_request (h,
201 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
202 h->info_cb.tunnels_cb = callback;
203 h->info_cls = callback_cls;
209 * Cancel a monitor request. The monitor callback will not be called.
211 * @param h Cadet handle.
212 * @return Closure given to GNUNET_CADET_list_tunnels().
215 GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt)
217 void *cls = h->info_cls;
219 h->info_cb.tunnels_cb = NULL;