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_get_channel.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_ChannelMonitor
43 GNUNET_CADET_ChannelCB channel_cb;
46 * Info callback closure for @c channel_cb.
51 * Configuration we use.
53 const struct GNUNET_CONFIGURATION_Handle *cfg;
56 * Message queue to talk to CADET service.
58 struct GNUNET_MQ_Handle *mq;
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 peer;
79 * Check that message received from CADET service is well-formed.
82 * @param message the message we got
83 * @return #GNUNET_OK if the message is well-formed,
84 * #GNUNET_SYSERR otherwise
87 check_channel_info (void *cls,
88 const struct GNUNET_CADET_ChannelInfoMessage *message)
97 * Process a local peer info reply, pass info to the user.
100 * @param message Message itself.
103 handle_channel_info (void *cls,
104 const struct GNUNET_CADET_ChannelInfoMessage *message)
106 struct GNUNET_CADET_ChannelMonitor *cm = cls;
107 struct GNUNET_CADET_ChannelInternals ci;
109 ci.root = message->root;
110 ci.dest = message->dest;
111 cm->channel_cb (cm->channel_cb_cls,
113 GNUNET_CADET_get_channel_cancel (cm);
118 * Process a local peer info reply, pass info to the user.
121 * @param message Message itself.
124 handle_channel_info_end (void *cls,
125 const struct GNUNET_MessageHeader *message)
127 struct GNUNET_CADET_ChannelMonitor *cm = cls;
129 cm->channel_cb (cm->channel_cb_cls,
131 GNUNET_CADET_get_channel_cancel (cm);
136 * Reconnect to the service and try again.
138 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation
141 reconnect (void *cls);
145 * Function called on connection trouble. Reconnects.
147 * @param cls a `struct GNUNET_CADET_ChannelMonitor``
148 * @param error error code from MQ
151 error_handler (void *cls,
152 enum GNUNET_MQ_Error error)
154 struct GNUNET_CADET_ChannelMonitor *cm = cls;
156 GNUNET_MQ_destroy (cm->mq);
158 cm->backoff = GNUNET_TIME_randomized_backoff (cm->backoff,
159 GNUNET_TIME_UNIT_MINUTES);
160 cm->reconnect_task = GNUNET_SCHEDULER_add_delayed (cm->backoff,
167 * Reconnect to the service and try again.
169 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation
172 reconnect (void *cls)
174 struct GNUNET_CADET_ChannelMonitor *cm = cls;
175 struct GNUNET_MQ_MessageHandler handlers[] = {
176 GNUNET_MQ_hd_fixed_size (channel_info_end,
177 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL_END,
178 struct GNUNET_MessageHeader,
180 GNUNET_MQ_hd_var_size (channel_info,
181 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL,
182 struct GNUNET_CADET_ChannelInfoMessage,
184 GNUNET_MQ_handler_end ()
186 struct GNUNET_CADET_RequestChannelInfoMessage *msg;
187 struct GNUNET_MQ_Envelope *env;
189 cm->reconnect_task = NULL;
190 cm->mq = GNUNET_CLIENT_connect (cm->cfg,
197 env = GNUNET_MQ_msg (msg,
198 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL);
199 msg->target = cm->peer;
200 GNUNET_MQ_send (cm->mq,
206 * Request information about a specific channel of the running cadet peer.
208 * @param cfg configuration to use
209 * @param peer ID of the other end of the channel.
210 * @param callback Function to call with the requested data.
211 * @param callback_cls Closure for @c callback.
212 * @return NULL on error
214 struct GNUNET_CADET_ChannelMonitor *
215 GNUNET_CADET_get_channel (const struct GNUNET_CONFIGURATION_Handle *cfg,
216 struct GNUNET_PeerIdentity *peer,
217 GNUNET_CADET_ChannelCB callback,
220 struct GNUNET_CADET_ChannelMonitor *cm;
222 if (NULL == callback)
227 cm = GNUNET_new (struct GNUNET_CADET_ChannelMonitor);
228 cm->channel_cb = callback;
229 cm->channel_cb_cls = callback_cls;
243 * Cancel a channel monitor request. The callback will not be called (anymore).
245 * @param h Cadet handle.
246 * @return Closure that was given to #GNUNET_CADET_get_channel().
249 GNUNET_CADET_get_channel_cancel (struct GNUNET_CADET_ChannelMonitor *cm)
251 void *ret = cm->channel_cb_cls;
254 GNUNET_MQ_destroy (cm->mq);
255 if (NULL != cm->reconnect_task)
256 GNUNET_SCHEDULER_cancel (cm->reconnect_task);
261 /* end of cadet_api_get_channel.c */