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
42 GNUNET_CADET_ChannelCB channel_cb;
45 * Info callback closure for @c channel_cb.
50 * Configuration we use.
52 const struct GNUNET_CONFIGURATION_Handle *cfg;
55 * Message queue to talk to CADET service.
57 struct GNUNET_MQ_Handle *mq;
62 struct GNUNET_SCHEDULER_Task *reconnect_task;
65 * Backoff for reconnect attempts.
67 struct GNUNET_TIME_Relative backoff;
70 * Peer we want information about.
72 struct GNUNET_PeerIdentity peer;
77 * Check that message received from CADET service is well-formed.
80 * @param message the message we got
81 * @return #GNUNET_OK if the message is well-formed,
82 * #GNUNET_SYSERR otherwise
85 check_channel_info (void *cls,
86 const struct GNUNET_CADET_ChannelInfoMessage *message)
95 * Process a local peer info reply, pass info to the user.
98 * @param message Message itself.
101 handle_channel_info (void *cls,
102 const struct GNUNET_CADET_ChannelInfoMessage *message)
104 struct GNUNET_CADET_ChannelMonitor *cm = cls;
105 struct GNUNET_CADET_ChannelInternals ci;
107 ci.root = message->root;
108 ci.dest = message->dest;
109 cm->channel_cb (cm->channel_cb_cls,
111 GNUNET_CADET_get_channel_cancel (cm);
116 * Process a local peer info reply, pass info to the user.
119 * @param message Message itself.
122 handle_channel_info_end (void *cls,
123 const struct GNUNET_MessageHeader *message)
125 struct GNUNET_CADET_ChannelMonitor *cm = cls;
127 cm->channel_cb (cm->channel_cb_cls,
129 GNUNET_CADET_get_channel_cancel (cm);
134 * Reconnect to the service and try again.
136 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation
139 reconnect (void *cls);
143 * Function called on connection trouble. Reconnects.
145 * @param cls a `struct GNUNET_CADET_ChannelMonitor``
146 * @param error error code from MQ
149 error_handler (void *cls,
150 enum GNUNET_MQ_Error error)
152 struct GNUNET_CADET_ChannelMonitor *cm = cls;
154 GNUNET_MQ_destroy (cm->mq);
156 cm->backoff = GNUNET_TIME_randomized_backoff (cm->backoff,
157 GNUNET_TIME_UNIT_MINUTES);
158 cm->reconnect_task = GNUNET_SCHEDULER_add_delayed (cm->backoff,
165 * Reconnect to the service and try again.
167 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation
170 reconnect (void *cls)
172 struct GNUNET_CADET_ChannelMonitor *cm = cls;
173 struct GNUNET_MQ_MessageHandler handlers[] = {
174 GNUNET_MQ_hd_fixed_size (channel_info_end,
175 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL_END,
176 struct GNUNET_MessageHeader,
178 GNUNET_MQ_hd_var_size (channel_info,
179 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL,
180 struct GNUNET_CADET_ChannelInfoMessage,
182 GNUNET_MQ_handler_end ()
184 struct GNUNET_CADET_RequestChannelInfoMessage *msg;
185 struct GNUNET_MQ_Envelope *env;
187 cm->reconnect_task = NULL;
188 cm->mq = GNUNET_CLIENT_connect (cm->cfg,
195 env = GNUNET_MQ_msg (msg,
196 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL);
197 msg->target = cm->peer;
198 GNUNET_MQ_send (cm->mq,
204 * Request information about a specific channel of the running cadet peer.
206 * @param cfg configuration to use
207 * @param peer ID of the other end of the channel.
208 * @param callback Function to call with the requested data.
209 * @param callback_cls Closure for @c callback.
210 * @return NULL on error
212 struct GNUNET_CADET_ChannelMonitor *
213 GNUNET_CADET_get_channel (const struct GNUNET_CONFIGURATION_Handle *cfg,
214 struct GNUNET_PeerIdentity *peer,
215 GNUNET_CADET_ChannelCB callback,
218 struct GNUNET_CADET_ChannelMonitor *cm;
220 if (NULL == callback)
225 cm = GNUNET_new (struct GNUNET_CADET_ChannelMonitor);
226 cm->channel_cb = callback;
227 cm->channel_cb_cls = callback_cls;
241 * Cancel a channel monitor request. The callback will not be called (anymore).
243 * @param h Cadet handle.
244 * @return Closure that was given to #GNUNET_CADET_get_channel().
247 GNUNET_CADET_get_channel_cancel (struct GNUNET_CADET_ChannelMonitor *cm)
249 void *ret = cm->channel_cb_cls;
252 GNUNET_MQ_destroy (cm->mq);
253 if (NULL != cm->reconnect_task)
254 GNUNET_SCHEDULER_cancel (cm->reconnect_task);
260 /* end of cadet_api_get_channel.c */