aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/cadet_api_get_tunnel.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cadet/cadet_api_get_tunnel.c')
-rw-r--r--src/cadet/cadet_api_get_tunnel.c279
1 files changed, 0 insertions, 279 deletions
diff --git a/src/cadet/cadet_api_get_tunnel.c b/src/cadet/cadet_api_get_tunnel.c
deleted file mode 100644
index 1d2776352..000000000
--- a/src/cadet/cadet_api_get_tunnel.c
+++ /dev/null
@@ -1,279 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2011, 2017 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19*/
20/**
21 * @file cadet/cadet_api_get_tunnel.c
22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_constants.h"
29#include "gnunet_cadet_service.h"
30#include "cadet.h"
31#include "cadet_protocol.h"
32
33
34/**
35 * Operation handle.
36 */
37struct GNUNET_CADET_GetTunnel
38{
39
40 /**
41 * Monitor callback
42 */
43 GNUNET_CADET_TunnelCB callback;
44
45 /**
46 * Closure for @e callback.
47 */
48 void *callback_cls;
49
50 /**
51 * Message queue to talk to CADET service.
52 */
53 struct GNUNET_MQ_Handle *mq;
54
55 /**
56 * Configuration we use.
57 */
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
59
60 /**
61 * Task to reconnect.
62 */
63 struct GNUNET_SCHEDULER_Task *reconnect_task;
64
65 /**
66 * Backoff for reconnect attempts.
67 */
68 struct GNUNET_TIME_Relative backoff;
69
70 /**
71 * Peer we want information about.
72 */
73 struct GNUNET_PeerIdentity id;
74};
75
76
77
78/**
79 * Check that message received from CADET service is well-formed.
80 *
81 * @param cls the `struct GNUNET_CADET_Handle`
82 * @param msg the message we got
83 * @return #GNUNET_OK if the message is well-formed,
84 * #GNUNET_SYSERR otherwise
85 */
86static int
87check_get_tunnel (void *cls,
88 const struct GNUNET_CADET_LocalInfoTunnel *msg)
89{
90 unsigned int ch_n;
91 unsigned int c_n;
92 size_t esize;
93 size_t msize;
94
95 (void) cls;
96 /* Verify message sanity */
97 msize = ntohs (msg->header.size);
98 esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
99 if (esize > msize)
100 {
101 GNUNET_break (0);
102 return GNUNET_SYSERR;
103 }
104 ch_n = ntohl (msg->channels);
105 c_n = ntohl (msg->connections);
106 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
107 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
108 if (msize != esize)
109 {
110 GNUNET_break_op (0);
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
112 "m:%u, e: %u (%u ch, %u conn)\n",
113 (unsigned int) msize,
114 (unsigned int) esize,
115 ch_n,
116 c_n);
117 return GNUNET_SYSERR;
118 }
119 return GNUNET_OK;
120}
121
122
123/**
124 * Process a local tunnel info reply, pass info to the user.
125 *
126 * @param cls a `struct GNUNET_CADET_GetTunnel *`
127 * @param msg Message itself.
128 */
129static void
130handle_get_tunnel (void *cls,
131 const struct GNUNET_CADET_LocalInfoTunnel *msg)
132{
133 struct GNUNET_CADET_GetTunnel *gt = cls;
134 unsigned int ch_n;
135 unsigned int c_n;
136 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
137 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
138
139 ch_n = ntohl (msg->channels);
140 c_n = ntohl (msg->connections);
141
142 /* Call Callback with tunnel info. */
143 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
144 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
145 gt->callback (gt->callback_cls,
146 &msg->destination,
147 ch_n,
148 c_n,
149 chns,
150 conns,
151 ntohs (msg->estate),
152 ntohs (msg->cstate));
153 GNUNET_CADET_get_tunnel_cancel (gt);
154}
155
156
157/**
158 * Reconnect to the service and try again.
159 *
160 * @param cls a `struct GNUNET_CADET_GetTunnel` operation
161 */
162static void
163reconnect (void *cls);
164
165
166/**
167 * Function called on connection trouble. Reconnects.
168 *
169 * @param cls a `struct GNUNET_CADET_GetTunnel`
170 * @param error error code from MQ
171 */
172static void
173error_handler (void *cls,
174 enum GNUNET_MQ_Error error)
175{
176 struct GNUNET_CADET_GetTunnel *gt = cls;
177
178 GNUNET_MQ_destroy (gt->mq);
179 gt->mq = NULL;
180 gt->backoff = GNUNET_TIME_randomized_backoff (gt->backoff,
181 GNUNET_TIME_UNIT_MINUTES);
182 gt->reconnect_task = GNUNET_SCHEDULER_add_delayed (gt->backoff,
183 &reconnect,
184 gt);
185}
186
187
188/**
189 * Reconnect to the service and try again.
190 *
191 * @param cls a `struct GNUNET_CADET_GetTunnel` operation
192 */
193static void
194reconnect (void *cls)
195{
196 struct GNUNET_CADET_GetTunnel *gt = cls;
197 struct GNUNET_MQ_MessageHandler handlers[] = {
198 GNUNET_MQ_hd_var_size (get_tunnel,
199 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
200 struct GNUNET_CADET_LocalInfoTunnel,
201 gt),
202 GNUNET_MQ_handler_end ()
203 };
204 struct GNUNET_MQ_Envelope *env;
205 struct GNUNET_CADET_LocalInfo *msg;
206
207 gt->reconnect_task = NULL;
208 gt->mq = GNUNET_CLIENT_connect (gt->cfg,
209 "cadet",
210 handlers,
211 &error_handler,
212 gt);
213 if (NULL == gt->mq)
214 return;
215 env = GNUNET_MQ_msg (msg,
216 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
217 msg->peer = gt->id;
218 GNUNET_MQ_send (gt->mq,
219 env);
220}
221
222/**
223 * Request information about a tunnel of the running cadet peer.
224 * The callback will be called for the tunnel once.
225 *
226 * @param cfg configuration to use
227 * @param id Peer whose tunnel to examine.
228 * @param callback Function to call with the requested data.
229 * @param callback_cls Closure for @c callback.
230 * @return NULL on error
231 */
232struct GNUNET_CADET_GetTunnel *
233GNUNET_CADET_get_tunnel (const struct GNUNET_CONFIGURATION_Handle *cfg,
234 const struct GNUNET_PeerIdentity *id,
235 GNUNET_CADET_TunnelCB callback,
236 void *callback_cls)
237{
238 struct GNUNET_CADET_GetTunnel *gt;
239
240 if (NULL == callback)
241 {
242 GNUNET_break (0);
243 return NULL;
244 }
245 gt = GNUNET_new (struct GNUNET_CADET_GetTunnel);
246 gt->callback = callback;
247 gt->callback_cls = callback_cls;
248 gt->cfg = cfg;
249 gt->id = *id;
250 reconnect (gt);
251 if (NULL == gt->mq)
252 {
253 GNUNET_free (gt);
254 return NULL;
255 }
256 return gt;
257}
258
259
260/**
261 * Cancel a monitor request. The monitor callback will not be called.
262 *
263 * @param lt operation handle
264 * @return Closure given to #GNUNET_CADET_get_tunnel(), if any.
265 */
266void *
267GNUNET_CADET_get_tunnel_cancel (struct GNUNET_CADET_GetTunnel *gt)
268{
269 void *ret = gt->callback_cls;
270
271 if (NULL != gt->mq)
272 GNUNET_MQ_destroy (gt->mq);
273 if (NULL != gt->reconnect_task)
274 GNUNET_SCHEDULER_cancel (gt->reconnect_task);
275 GNUNET_free (gt);
276 return ret;
277}
278
279/* end of cadet_api_get_tunnel.c */