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_get_path.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_GetPath
43 GNUNET_CADET_PathCB path_cb;
46 * Closure for @c path_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;
71 * Peer we want information about.
73 struct GNUNET_PeerIdentity id;
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_get_path (void *cls,
88 const struct GNUNET_CADET_LocalInfoPath *message)
90 size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPath);
94 esize = ntohs (message->header.size);
100 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity)))
103 return GNUNET_SYSERR;
110 * Process a local peer info reply, pass info to the user.
113 * @param message Message itself.
116 handle_get_path (void *cls,
117 const struct GNUNET_CADET_LocalInfoPath *message)
119 struct GNUNET_CADET_GetPath *gp = cls;
120 struct GNUNET_CADET_PeerPathDetail ppd;
123 ppd.path = (const struct GNUNET_PeerIdentity *) &message[1];
124 ppd.target_offset = ntohl (message->off);
125 ppd.path_length = (ntohs (message->header.size) - sizeof (*message))
126 / sizeof (struct GNUNET_PeerIdentity);
127 gp->path_cb (gp->path_cb_cls,
133 * Process a local peer info reply, pass info to the user.
136 * @param message Message itself.
139 handle_get_path_end (void *cls,
140 const struct GNUNET_MessageHeader *message)
142 struct GNUNET_CADET_GetPath *gp = cls;
145 gp->path_cb (gp->path_cb_cls,
147 GNUNET_CADET_get_path_cancel (gp);
152 * Reconnect to the service and try again.
154 * @param cls a `struct GNUNET_CADET_GetPath` operation
157 reconnect (void *cls);
161 * Function called on connection trouble. Reconnects.
163 * @param cls a `struct GNUNET_CADET_GetPath`
164 * @param error error code from MQ
167 error_handler (void *cls,
168 enum GNUNET_MQ_Error error)
170 struct GNUNET_CADET_GetPath *gp = cls;
172 GNUNET_MQ_destroy (gp->mq);
174 gp->backoff = GNUNET_TIME_randomized_backoff (gp->backoff,
175 GNUNET_TIME_UNIT_MINUTES);
176 gp->reconnect_task = GNUNET_SCHEDULER_add_delayed (gp->backoff,
183 * Reconnect to the service and try again.
185 * @param cls a `struct GNUNET_CADET_GetPath` operation
188 reconnect (void *cls)
190 struct GNUNET_CADET_GetPath *gp = cls;
191 struct GNUNET_MQ_MessageHandler handlers[] = {
192 GNUNET_MQ_hd_var_size (get_path,
193 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH,
194 struct GNUNET_CADET_LocalInfoPath,
196 GNUNET_MQ_hd_fixed_size (get_path_end,
197 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END,
198 struct GNUNET_MessageHeader,
200 GNUNET_MQ_handler_end ()
202 struct GNUNET_CADET_RequestPathInfoMessage *msg;
203 struct GNUNET_MQ_Envelope *env;
205 gp->reconnect_task = NULL;
206 gp->mq = GNUNET_CLIENT_connect (gp->cfg,
213 env = GNUNET_MQ_msg (msg,
214 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH);
216 GNUNET_MQ_send (gp->mq,
222 * Request information about paths known to the running cadet peer.
224 * @param cfg configuration to use
225 * @param id Peer whose paths to examine.
226 * @param callback Function to call with the requested data.
227 * @param callback_cls Closure for @c callback.
228 * @return NULL on error
230 struct GNUNET_CADET_GetPath *
231 GNUNET_CADET_get_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
232 const struct GNUNET_PeerIdentity *id,
233 GNUNET_CADET_PathCB callback,
236 struct GNUNET_CADET_GetPath *gp;
238 if (NULL == callback)
243 gp = GNUNET_new (struct GNUNET_CADET_GetPath);
244 gp->path_cb = callback;
245 gp->path_cb_cls = callback_cls;
259 * Cancel @a gp operation.
261 * @param gp operation to cancel
262 * @return closure from #GNUNET_CADET_get_path().
265 GNUNET_CADET_get_path_cancel (struct GNUNET_CADET_GetPath *gp)
267 void *ret = gp->path_cb_cls;
270 GNUNET_MQ_destroy (gp->mq);
271 if (NULL != gp->reconnect_task)
272 GNUNET_SCHEDULER_cancel (gp->reconnect_task);
278 /* end of cadet_api_get_path.c */