summaryrefslogtreecommitdiff
path: root/src/transport/transport_api2_application.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/transport_api2_application.c')
-rw-r--r--src/transport/transport_api2_application.c189
1 files changed, 93 insertions, 96 deletions
diff --git a/src/transport/transport_api2_application.c b/src/transport/transport_api2_application.c
index 7d790d738..8ddf48a86 100644
--- a/src/transport/transport_api2_application.c
+++ b/src/transport/transport_api2_application.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file transport/transport_api2_application.c 21 * @file transport/transport_api2_application.c
22 * @brief enable clients to ask TRANSPORT about establishing connections to peers 22 * @brief enable clients to ask TRANSPORT about establishing connections to peers
@@ -30,14 +30,13 @@
30 30
31 31
32#define LOG(kind, ...) \ 32#define LOG(kind, ...) \
33 GNUNET_log_from (kind, "transport-application-api", __VA_ARGS__) 33 GNUNET_log_from(kind, "transport-application-api", __VA_ARGS__)
34 34
35 35
36/** 36/**
37 * Handle for TRANSPORT address suggestion requests. 37 * Handle for TRANSPORT address suggestion requests.
38 */ 38 */
39struct GNUNET_TRANSPORT_ApplicationSuggestHandle 39struct GNUNET_TRANSPORT_ApplicationSuggestHandle {
40{
41 /** 40 /**
42 * ID of the peer for which address suggestion was requested. 41 * ID of the peer for which address suggestion was requested.
43 */ 42 */
@@ -63,9 +62,7 @@ struct GNUNET_TRANSPORT_ApplicationSuggestHandle
63/** 62/**
64 * Handle to the TRANSPORT subsystem for application management. 63 * Handle to the TRANSPORT subsystem for application management.
65 */ 64 */
66struct GNUNET_TRANSPORT_ApplicationHandle 65struct GNUNET_TRANSPORT_ApplicationHandle {
67{
68
69 /** 66 /**
70 * Our configuration. 67 * Our configuration.
71 */ 68 */
@@ -101,7 +98,7 @@ struct GNUNET_TRANSPORT_ApplicationHandle
101 * @param ch handle to use to re-connect. 98 * @param ch handle to use to re-connect.
102 */ 99 */
103static void 100static void
104reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch); 101reconnect(struct GNUNET_TRANSPORT_ApplicationHandle *ch);
105 102
106 103
107/** 104/**
@@ -110,12 +107,12 @@ reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch);
110 * @param cls handle to use to re-connect. 107 * @param cls handle to use to re-connect.
111 */ 108 */
112static void 109static void
113reconnect_task (void *cls) 110reconnect_task(void *cls)
114{ 111{
115 struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls; 112 struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;
116 113
117 ch->task = NULL; 114 ch->task = NULL;
118 reconnect (ch); 115 reconnect(ch);
119} 116}
120 117
121 118
@@ -125,15 +122,15 @@ reconnect_task (void *cls)
125 * @param ch our handle 122 * @param ch our handle
126 */ 123 */
127static void 124static void
128force_reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch) 125force_reconnect(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
129{ 126{
130 if (NULL != ch->mq) 127 if (NULL != ch->mq)
131 { 128 {
132 GNUNET_MQ_destroy (ch->mq); 129 GNUNET_MQ_destroy(ch->mq);
133 ch->mq = NULL; 130 ch->mq = NULL;
134 } 131 }
135 ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff); 132 ch->backoff = GNUNET_TIME_STD_BACKOFF(ch->backoff);
136 ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff, &reconnect_task, ch); 133 ch->task = GNUNET_SCHEDULER_add_delayed(ch->backoff, &reconnect_task, ch);
137} 134}
138 135
139 136
@@ -145,14 +142,14 @@ force_reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch)
145 * @param error details about the error 142 * @param error details about the error
146 */ 143 */
147static void 144static void
148error_handler (void *cls, enum GNUNET_MQ_Error error) 145error_handler(void *cls, enum GNUNET_MQ_Error error)
149{ 146{
150 struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls; 147 struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;
151 148
152 LOG (GNUNET_ERROR_TYPE_DEBUG, 149 LOG(GNUNET_ERROR_TYPE_DEBUG,
153 "TRANSPORT connection died (code %d), reconnecting\n", 150 "TRANSPORT connection died (code %d), reconnecting\n",
154 (int) error); 151 (int)error);
155 force_reconnect (ch); 152 force_reconnect(ch);
156} 153}
157 154
158 155
@@ -166,9 +163,9 @@ error_handler (void *cls, enum GNUNET_MQ_Error error)
166 * failure (message queue no longer exists) 163 * failure (message queue no longer exists)
167 */ 164 */
168static int 165static int
169transmit_suggestion (void *cls, 166transmit_suggestion(void *cls,
170 const struct GNUNET_PeerIdentity *peer, 167 const struct GNUNET_PeerIdentity *peer,
171 void *value) 168 void *value)
172{ 169{
173 struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls; 170 struct GNUNET_TRANSPORT_ApplicationHandle *ch = cls;
174 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh = value; 171 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh = value;
@@ -177,11 +174,11 @@ transmit_suggestion (void *cls,
177 174
178 if (NULL == ch->mq) 175 if (NULL == ch->mq)
179 return GNUNET_SYSERR; 176 return GNUNET_SYSERR;
180 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST); 177 ev = GNUNET_MQ_msg(m, GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST);
181 m->pk = htonl ((uint32_t) sh->pk); 178 m->pk = htonl((uint32_t)sh->pk);
182 m->bw = sh->bw; 179 m->bw = sh->bw;
183 m->peer = *peer; 180 m->peer = *peer;
184 GNUNET_MQ_send (ch->mq, ev); 181 GNUNET_MQ_send(ch->mq, ev);
185 return GNUNET_OK; 182 return GNUNET_OK;
186} 183}
187 184
@@ -192,21 +189,21 @@ transmit_suggestion (void *cls,
192 * @param ch handle to use to re-connect. 189 * @param ch handle to use to re-connect.
193 */ 190 */
194static void 191static void
195reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch) 192reconnect(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
196{ 193{
197 static const struct GNUNET_MQ_MessageHandler handlers[] = {{NULL, 0, 0}}; 194 static const struct GNUNET_MQ_MessageHandler handlers[] = { { NULL, 0, 0 } };
198 195
199 GNUNET_assert (NULL == ch->mq); 196 GNUNET_assert(NULL == ch->mq);
200 ch->mq = 197 ch->mq =
201 GNUNET_CLIENT_connect (ch->cfg, "transport", handlers, &error_handler, ch); 198 GNUNET_CLIENT_connect(ch->cfg, "transport", handlers, &error_handler, ch);
202 if (NULL == ch->mq) 199 if (NULL == ch->mq)
203 { 200 {
204 force_reconnect (ch); 201 force_reconnect(ch);
205 return; 202 return;
206 } 203 }
207 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests, 204 GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests,
208 &transmit_suggestion, 205 &transmit_suggestion,
209 ch); 206 ch);
210} 207}
211 208
212 209
@@ -217,15 +214,15 @@ reconnect (struct GNUNET_TRANSPORT_ApplicationHandle *ch)
217 * @return transport application handle, NULL on error 214 * @return transport application handle, NULL on error
218 */ 215 */
219struct GNUNET_TRANSPORT_ApplicationHandle * 216struct GNUNET_TRANSPORT_ApplicationHandle *
220GNUNET_TRANSPORT_application_init ( 217GNUNET_TRANSPORT_application_init(
221 const struct GNUNET_CONFIGURATION_Handle *cfg) 218 const struct GNUNET_CONFIGURATION_Handle *cfg)
222{ 219{
223 struct GNUNET_TRANSPORT_ApplicationHandle *ch; 220 struct GNUNET_TRANSPORT_ApplicationHandle *ch;
224 221
225 ch = GNUNET_new (struct GNUNET_TRANSPORT_ApplicationHandle); 222 ch = GNUNET_new(struct GNUNET_TRANSPORT_ApplicationHandle);
226 ch->cfg = cfg; 223 ch->cfg = cfg;
227 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); 224 ch->sug_requests = GNUNET_CONTAINER_multipeermap_create(32, GNUNET_YES);
228 reconnect (ch); 225 reconnect(ch);
229 return ch; 226 return ch;
230} 227}
231 228
@@ -240,11 +237,11 @@ GNUNET_TRANSPORT_application_init (
240 * @return #GNUNET_OK (continue to iterate) 237 * @return #GNUNET_OK (continue to iterate)
241 */ 238 */
242static int 239static int
243free_sug_handle (void *cls, const struct GNUNET_PeerIdentity *key, void *value) 240free_sug_handle(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
244{ 241{
245 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *cur = value; 242 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *cur = value;
246 243
247 GNUNET_free (cur); 244 GNUNET_free(cur);
248 return GNUNET_OK; 245 return GNUNET_OK;
249} 246}
250 247
@@ -255,24 +252,24 @@ free_sug_handle (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
255 * @param ch handle to release 252 * @param ch handle to release
256 */ 253 */
257void 254void
258GNUNET_TRANSPORT_application_done ( 255GNUNET_TRANSPORT_application_done(
259 struct GNUNET_TRANSPORT_ApplicationHandle *ch) 256 struct GNUNET_TRANSPORT_ApplicationHandle *ch)
260{ 257{
261 if (NULL != ch->mq) 258 if (NULL != ch->mq)
262 { 259 {
263 GNUNET_MQ_destroy (ch->mq); 260 GNUNET_MQ_destroy(ch->mq);
264 ch->mq = NULL; 261 ch->mq = NULL;
265 } 262 }
266 if (NULL != ch->task) 263 if (NULL != ch->task)
267 { 264 {
268 GNUNET_SCHEDULER_cancel (ch->task); 265 GNUNET_SCHEDULER_cancel(ch->task);
269 ch->task = NULL; 266 ch->task = NULL;
270 } 267 }
271 GNUNET_CONTAINER_multipeermap_iterate (ch->sug_requests, 268 GNUNET_CONTAINER_multipeermap_iterate(ch->sug_requests,
272 &free_sug_handle, 269 &free_sug_handle,
273 NULL); 270 NULL);
274 GNUNET_CONTAINER_multipeermap_destroy (ch->sug_requests); 271 GNUNET_CONTAINER_multipeermap_destroy(ch->sug_requests);
275 GNUNET_free (ch); 272 GNUNET_free(ch);
276} 273}
277 274
278 275
@@ -287,7 +284,7 @@ GNUNET_TRANSPORT_application_done (
287 * @return suggest handle, NULL if a request is already pending 284 * @return suggest handle, NULL if a request is already pending
288 */ 285 */
289struct GNUNET_TRANSPORT_ApplicationSuggestHandle * 286struct GNUNET_TRANSPORT_ApplicationSuggestHandle *
290GNUNET_TRANSPORT_application_suggest ( 287GNUNET_TRANSPORT_application_suggest(
291 struct GNUNET_TRANSPORT_ApplicationHandle *ch, 288 struct GNUNET_TRANSPORT_ApplicationHandle *ch,
292 const struct GNUNET_PeerIdentity *peer, 289 const struct GNUNET_PeerIdentity *peer,
293 enum GNUNET_MQ_PriorityPreferences pk, 290 enum GNUNET_MQ_PriorityPreferences pk,
@@ -295,22 +292,22 @@ GNUNET_TRANSPORT_application_suggest (
295{ 292{
296 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *s; 293 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *s;
297 294
298 s = GNUNET_new (struct GNUNET_TRANSPORT_ApplicationSuggestHandle); 295 s = GNUNET_new(struct GNUNET_TRANSPORT_ApplicationSuggestHandle);
299 s->ch = ch; 296 s->ch = ch;
300 s->id = *peer; 297 s->id = *peer;
301 s->pk = pk; 298 s->pk = pk;
302 s->bw = bw; 299 s->bw = bw;
303 (void) GNUNET_CONTAINER_multipeermap_put ( 300 (void)GNUNET_CONTAINER_multipeermap_put(
304 ch->sug_requests, 301 ch->sug_requests,
305 &s->id, 302 &s->id,
306 s, 303 s,
307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 304 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
308 LOG (GNUNET_ERROR_TYPE_DEBUG, 305 LOG(GNUNET_ERROR_TYPE_DEBUG,
309 "Requesting TRANSPORT to suggest address for `%s'\n", 306 "Requesting TRANSPORT to suggest address for `%s'\n",
310 GNUNET_i2s (peer)); 307 GNUNET_i2s(peer));
311 if (NULL == ch->mq) 308 if (NULL == ch->mq)
312 return s; 309 return s;
313 GNUNET_assert (GNUNET_OK == transmit_suggestion (ch, &s->id, s)); 310 GNUNET_assert(GNUNET_OK == transmit_suggestion(ch, &s->id, s));
314 return s; 311 return s;
315} 312}
316 313
@@ -321,30 +318,30 @@ GNUNET_TRANSPORT_application_suggest (
321 * @param sh handle to stop 318 * @param sh handle to stop
322 */ 319 */
323void 320void
324GNUNET_TRANSPORT_application_suggest_cancel ( 321GNUNET_TRANSPORT_application_suggest_cancel(
325 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh) 322 struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh)
326{ 323{
327 struct GNUNET_TRANSPORT_ApplicationHandle *ch = sh->ch; 324 struct GNUNET_TRANSPORT_ApplicationHandle *ch = sh->ch;
328 struct GNUNET_MQ_Envelope *ev; 325 struct GNUNET_MQ_Envelope *ev;
329 struct ExpressPreferenceMessage *m; 326 struct ExpressPreferenceMessage *m;
330 327
331 LOG (GNUNET_ERROR_TYPE_DEBUG, 328 LOG(GNUNET_ERROR_TYPE_DEBUG,
332 "Telling TRANSPORT we no longer care for an address for `%s'\n", 329 "Telling TRANSPORT we no longer care for an address for `%s'\n",
333 GNUNET_i2s (&sh->id)); 330 GNUNET_i2s(&sh->id));
334 GNUNET_assert ( 331 GNUNET_assert(
335 GNUNET_OK == 332 GNUNET_OK ==
336 GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests, &sh->id, sh)); 333 GNUNET_CONTAINER_multipeermap_remove(ch->sug_requests, &sh->id, sh));
337 if (NULL == ch->mq) 334 if (NULL == ch->mq)
338 { 335 {
339 GNUNET_free (sh); 336 GNUNET_free(sh);
340 return; 337 return;
341 } 338 }
342 ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST_CANCEL); 339 ev = GNUNET_MQ_msg(m, GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST_CANCEL);
343 m->pk = htonl ((uint32_t) sh->pk); 340 m->pk = htonl((uint32_t)sh->pk);
344 m->bw = sh->bw; 341 m->bw = sh->bw;
345 m->peer = sh->id; 342 m->peer = sh->id;
346 GNUNET_MQ_send (ch->mq, ev); 343 GNUNET_MQ_send(ch->mq, ev);
347 GNUNET_free (sh); 344 GNUNET_free(sh);
348} 345}
349 346
350 347
@@ -364,7 +361,7 @@ GNUNET_TRANSPORT_application_suggest_cancel (
364 * @param addr address to validate 361 * @param addr address to validate
365 */ 362 */
366void 363void
367GNUNET_TRANSPORT_application_validate ( 364GNUNET_TRANSPORT_application_validate(
368 struct GNUNET_TRANSPORT_ApplicationHandle *ch, 365 struct GNUNET_TRANSPORT_ApplicationHandle *ch,
369 const struct GNUNET_PeerIdentity *peer, 366 const struct GNUNET_PeerIdentity *peer,
370 enum GNUNET_NetworkType nt, 367 enum GNUNET_NetworkType nt,
@@ -375,23 +372,23 @@ GNUNET_TRANSPORT_application_validate (
375 size_t alen; 372 size_t alen;
376 373
377 if (NULL == ch->mq) 374 if (NULL == ch->mq)
378 { 375 {
379 GNUNET_log ( 376 GNUNET_log(
380 GNUNET_ERROR_TYPE_WARNING, 377 GNUNET_ERROR_TYPE_WARNING,
381 "Address validation for %s:%s skipped as transport is not connected\n", 378 "Address validation for %s:%s skipped as transport is not connected\n",
382 GNUNET_i2s (peer), 379 GNUNET_i2s(peer),
383 addr); 380 addr);
384 return; 381 return;
385 } 382 }
386 alen = strlen (addr) + 1; 383 alen = strlen(addr) + 1;
387 ev = 384 ev =
388 GNUNET_MQ_msg_extra (m, 385 GNUNET_MQ_msg_extra(m,
389 alen, 386 alen,
390 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION); 387 GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION);
391 m->peer = *peer; 388 m->peer = *peer;
392 m->nt = htonl ((uint32_t) nt); 389 m->nt = htonl((uint32_t)nt);
393 memcpy (&m[1], addr, alen); 390 memcpy(&m[1], addr, alen);
394 GNUNET_MQ_send (ch->mq, ev); 391 GNUNET_MQ_send(ch->mq, ev);
395} 392}
396 393
397 394