diff options
Diffstat (limited to 'src/cadet/gnunet-cadet.c')
-rw-r--r-- | src/cadet/gnunet-cadet.c | 712 |
1 files changed, 361 insertions, 351 deletions
diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c index 4e81d52f2..f5cf86211 100644 --- a/src/cadet/gnunet-cadet.c +++ b/src/cadet/gnunet-cadet.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 | /** | 21 | /** |
22 | * @file cadet/gnunet-cadet.c | 22 | * @file cadet/gnunet-cadet.c |
@@ -143,7 +143,7 @@ static unsigned int sent_pkt; | |||
143 | * Wait for input on STDIO and send it out over the #ch. | 143 | * Wait for input on STDIO and send it out over the #ch. |
144 | */ | 144 | */ |
145 | static void | 145 | static void |
146 | listen_stdio (void); | 146 | listen_stdio(void); |
147 | 147 | ||
148 | 148 | ||
149 | /** | 149 | /** |
@@ -154,21 +154,25 @@ listen_stdio (void); | |||
154 | * @return Human readable string. | 154 | * @return Human readable string. |
155 | */ | 155 | */ |
156 | static const char * | 156 | static const char * |
157 | enc_2s (uint16_t status) | 157 | enc_2s(uint16_t status) |
158 | { | 158 | { |
159 | switch (status) | 159 | switch (status) |
160 | { | 160 | { |
161 | case 0: | 161 | case 0: |
162 | return "NULL "; | 162 | return "NULL "; |
163 | case 1: | 163 | |
164 | return "KSENT"; | 164 | case 1: |
165 | case 2: | 165 | return "KSENT"; |
166 | return "KRECV"; | 166 | |
167 | case 3: | 167 | case 2: |
168 | return "READY"; | 168 | return "KRECV"; |
169 | default: | 169 | |
170 | return ""; | 170 | case 3: |
171 | } | 171 | return "READY"; |
172 | |||
173 | default: | ||
174 | return ""; | ||
175 | } | ||
172 | } | 176 | } |
173 | 177 | ||
174 | 178 | ||
@@ -180,23 +184,28 @@ enc_2s (uint16_t status) | |||
180 | * @return Human readable string. | 184 | * @return Human readable string. |
181 | */ | 185 | */ |
182 | static const char * | 186 | static const char * |
183 | conn_2s (uint16_t status) | 187 | conn_2s(uint16_t status) |
184 | { | 188 | { |
185 | switch (status) | 189 | switch (status) |
186 | { | 190 | { |
187 | case 0: | 191 | case 0: |
188 | return "NEW "; | 192 | return "NEW "; |
189 | case 1: | 193 | |
190 | return "SRCH "; | 194 | case 1: |
191 | case 2: | 195 | return "SRCH "; |
192 | return "WAIT "; | 196 | |
193 | case 3: | 197 | case 2: |
194 | return "READY"; | 198 | return "WAIT "; |
195 | case 4: | 199 | |
196 | return "SHUTD"; | 200 | case 3: |
197 | default: | 201 | return "READY"; |
198 | return ""; | 202 | |
199 | } | 203 | case 4: |
204 | return "SHUTD"; | ||
205 | |||
206 | default: | ||
207 | return ""; | ||
208 | } | ||
200 | } | 209 | } |
201 | 210 | ||
202 | 211 | ||
@@ -206,60 +215,60 @@ conn_2s (uint16_t status) | |||
206 | * @param cls Closure (unused). | 215 | * @param cls Closure (unused). |
207 | */ | 216 | */ |
208 | static void | 217 | static void |
209 | shutdown_task (void *cls) | 218 | shutdown_task(void *cls) |
210 | { | 219 | { |
211 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n"); | 220 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n"); |
212 | if (NULL != lp) | 221 | if (NULL != lp) |
213 | { | 222 | { |
214 | GNUNET_CADET_close_port (lp); | 223 | GNUNET_CADET_close_port(lp); |
215 | lp = NULL; | 224 | lp = NULL; |
216 | } | 225 | } |
217 | if (NULL != ch) | 226 | if (NULL != ch) |
218 | { | 227 | { |
219 | GNUNET_CADET_channel_destroy (ch); | 228 | GNUNET_CADET_channel_destroy(ch); |
220 | ch = NULL; | 229 | ch = NULL; |
221 | } | 230 | } |
222 | if (NULL != gpo) | 231 | if (NULL != gpo) |
223 | { | 232 | { |
224 | GNUNET_CADET_get_path_cancel (gpo); | 233 | GNUNET_CADET_get_path_cancel(gpo); |
225 | gpo = NULL; | 234 | gpo = NULL; |
226 | } | 235 | } |
227 | if (NULL != plo) | 236 | if (NULL != plo) |
228 | { | 237 | { |
229 | GNUNET_CADET_list_peers_cancel (plo); | 238 | GNUNET_CADET_list_peers_cancel(plo); |
230 | plo = NULL; | 239 | plo = NULL; |
231 | } | 240 | } |
232 | if (NULL != tio) | 241 | if (NULL != tio) |
233 | { | 242 | { |
234 | GNUNET_CADET_list_tunnels_cancel (tio); | 243 | GNUNET_CADET_list_tunnels_cancel(tio); |
235 | tio = NULL; | 244 | tio = NULL; |
236 | } | 245 | } |
237 | if (NULL != mh) | 246 | if (NULL != mh) |
238 | { | 247 | { |
239 | GNUNET_CADET_disconnect (mh); | 248 | GNUNET_CADET_disconnect(mh); |
240 | mh = NULL; | 249 | mh = NULL; |
241 | } | 250 | } |
242 | if (NULL != rd_task) | 251 | if (NULL != rd_task) |
243 | { | 252 | { |
244 | GNUNET_SCHEDULER_cancel (rd_task); | 253 | GNUNET_SCHEDULER_cancel(rd_task); |
245 | rd_task = NULL; | 254 | rd_task = NULL; |
246 | } | 255 | } |
247 | if (NULL != echo_task) | 256 | if (NULL != echo_task) |
248 | { | 257 | { |
249 | GNUNET_SCHEDULER_cancel (echo_task); | 258 | GNUNET_SCHEDULER_cancel(echo_task); |
250 | echo_task = NULL; | 259 | echo_task = NULL; |
251 | } | 260 | } |
252 | if (NULL != job) | 261 | if (NULL != job) |
253 | { | 262 | { |
254 | GNUNET_SCHEDULER_cancel (job); | 263 | GNUNET_SCHEDULER_cancel(job); |
255 | job = NULL; | 264 | job = NULL; |
256 | } | 265 | } |
257 | } | 266 | } |
258 | 267 | ||
259 | void | 268 | void |
260 | mq_cb (void *cls) | 269 | mq_cb(void *cls) |
261 | { | 270 | { |
262 | listen_stdio (); | 271 | listen_stdio(); |
263 | } | 272 | } |
264 | 273 | ||
265 | 274 | ||
@@ -269,7 +278,7 @@ mq_cb (void *cls) | |||
269 | * @param cls Closure (unused). | 278 | * @param cls Closure (unused). |
270 | */ | 279 | */ |
271 | static void | 280 | static void |
272 | read_stdio (void *cls) | 281 | read_stdio(void *cls) |
273 | { | 282 | { |
274 | struct GNUNET_MQ_Envelope *env; | 283 | struct GNUNET_MQ_Envelope *env; |
275 | struct GNUNET_MessageHeader *msg; | 284 | struct GNUNET_MessageHeader *msg; |
@@ -277,41 +286,41 @@ read_stdio (void *cls) | |||
277 | ssize_t data_size; | 286 | ssize_t data_size; |
278 | 287 | ||
279 | rd_task = NULL; | 288 | rd_task = NULL; |
280 | data_size = read (0, buf, 60000); | 289 | data_size = read(0, buf, 60000); |
281 | if (data_size < 1) | 290 | if (data_size < 1) |
282 | { | 291 | { |
283 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 292 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
284 | "read() returned %s\n", | 293 | "read() returned %s\n", |
285 | strerror (errno)); | 294 | strerror(errno)); |
286 | GNUNET_SCHEDULER_shutdown (); | 295 | GNUNET_SCHEDULER_shutdown(); |
287 | return; | 296 | return; |
288 | } | 297 | } |
289 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 298 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
290 | "Read %u bytes from stdio\n", | 299 | "Read %u bytes from stdio\n", |
291 | (unsigned int) data_size); | 300 | (unsigned int)data_size); |
292 | env = GNUNET_MQ_msg_extra (msg, data_size, GNUNET_MESSAGE_TYPE_CADET_CLI); | 301 | env = GNUNET_MQ_msg_extra(msg, data_size, GNUNET_MESSAGE_TYPE_CADET_CLI); |
293 | GNUNET_memcpy (&msg[1], buf, data_size); | 302 | GNUNET_memcpy(&msg[1], buf, data_size); |
294 | GNUNET_MQ_send (GNUNET_CADET_get_mq (ch), env); | 303 | GNUNET_MQ_send(GNUNET_CADET_get_mq(ch), env); |
295 | 304 | ||
296 | sent_pkt++; | 305 | sent_pkt++; |
297 | 306 | ||
298 | if (GNUNET_NO == echo) | 307 | if (GNUNET_NO == echo) |
299 | { | ||
300 | // Use MQ's notification if too much data of stdin is pooring in too fast. | ||
301 | if (STREAM_BUFFER_SIZE < sent_pkt) | ||
302 | { | 308 | { |
303 | GNUNET_MQ_notify_sent (env, mq_cb, cls); | 309 | // Use MQ's notification if too much data of stdin is pooring in too fast. |
304 | sent_pkt = 0; | 310 | if (STREAM_BUFFER_SIZE < sent_pkt) |
311 | { | ||
312 | GNUNET_MQ_notify_sent(env, mq_cb, cls); | ||
313 | sent_pkt = 0; | ||
314 | } | ||
315 | else | ||
316 | { | ||
317 | listen_stdio(); | ||
318 | } | ||
305 | } | 319 | } |
306 | else | 320 | else |
307 | { | 321 | { |
308 | listen_stdio (); | 322 | echo_time = GNUNET_TIME_absolute_get(); |
309 | } | 323 | } |
310 | } | ||
311 | else | ||
312 | { | ||
313 | echo_time = GNUNET_TIME_absolute_get (); | ||
314 | } | ||
315 | } | 324 | } |
316 | 325 | ||
317 | 326 | ||
@@ -319,20 +328,20 @@ read_stdio (void *cls) | |||
319 | * Wait for input on STDIO and send it out over the #ch. | 328 | * Wait for input on STDIO and send it out over the #ch. |
320 | */ | 329 | */ |
321 | static void | 330 | static void |
322 | listen_stdio () | 331 | listen_stdio() |
323 | { | 332 | { |
324 | struct GNUNET_NETWORK_FDSet *rs; | 333 | struct GNUNET_NETWORK_FDSet *rs; |
325 | 334 | ||
326 | /* FIXME: why use 'rs' here, seems overly complicated... */ | 335 | /* FIXME: why use 'rs' here, seems overly complicated... */ |
327 | rs = GNUNET_NETWORK_fdset_create (); | 336 | rs = GNUNET_NETWORK_fdset_create(); |
328 | GNUNET_NETWORK_fdset_set_native (rs, 0); /* STDIN */ | 337 | GNUNET_NETWORK_fdset_set_native(rs, 0); /* STDIN */ |
329 | rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 338 | rd_task = GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
330 | GNUNET_TIME_UNIT_FOREVER_REL, | 339 | GNUNET_TIME_UNIT_FOREVER_REL, |
331 | rs, | 340 | rs, |
332 | NULL, | 341 | NULL, |
333 | &read_stdio, | 342 | &read_stdio, |
334 | NULL); | 343 | NULL); |
335 | GNUNET_NETWORK_fdset_destroy (rs); | 344 | GNUNET_NETWORK_fdset_destroy(rs); |
336 | } | 345 | } |
337 | 346 | ||
338 | 347 | ||
@@ -346,12 +355,12 @@ listen_stdio () | |||
346 | * @param channel connection to the other end (henceforth invalid) | 355 | * @param channel connection to the other end (henceforth invalid) |
347 | */ | 356 | */ |
348 | static void | 357 | static void |
349 | channel_ended (void *cls, const struct GNUNET_CADET_Channel *channel) | 358 | channel_ended(void *cls, const struct GNUNET_CADET_Channel *channel) |
350 | { | 359 | { |
351 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n"); | 360 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n"); |
352 | GNUNET_assert (channel == ch); | 361 | GNUNET_assert(channel == ch); |
353 | ch = NULL; | 362 | ch = NULL; |
354 | GNUNET_SCHEDULER_shutdown (); | 363 | GNUNET_SCHEDULER_shutdown(); |
355 | } | 364 | } |
356 | 365 | ||
357 | 366 | ||
@@ -370,20 +379,20 @@ channel_ended (void *cls, const struct GNUNET_CADET_Channel *channel) | |||
370 | * @return initial channel context for the channel, we use @a channel | 379 | * @return initial channel context for the channel, we use @a channel |
371 | */ | 380 | */ |
372 | static void * | 381 | static void * |
373 | channel_incoming (void *cls, | 382 | channel_incoming(void *cls, |
374 | struct GNUNET_CADET_Channel *channel, | 383 | struct GNUNET_CADET_Channel *channel, |
375 | const struct GNUNET_PeerIdentity *initiator) | 384 | const struct GNUNET_PeerIdentity *initiator) |
376 | { | 385 | { |
377 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 386 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, |
378 | "Incoming connection from %s\n", | 387 | "Incoming connection from %s\n", |
379 | GNUNET_i2s_full (initiator)); | 388 | GNUNET_i2s_full(initiator)); |
380 | GNUNET_assert (NULL == ch); | 389 | GNUNET_assert(NULL == ch); |
381 | GNUNET_assert (NULL != lp); | 390 | GNUNET_assert(NULL != lp); |
382 | GNUNET_CADET_close_port (lp); | 391 | GNUNET_CADET_close_port(lp); |
383 | lp = NULL; | 392 | lp = NULL; |
384 | ch = channel; | 393 | ch = channel; |
385 | if (GNUNET_NO == echo) | 394 | if (GNUNET_NO == echo) |
386 | listen_stdio (); | 395 | listen_stdio(); |
387 | return channel; | 396 | return channel; |
388 | } | 397 | } |
389 | 398 | ||
@@ -394,7 +403,7 @@ channel_incoming (void *cls, | |||
394 | * @param cls Closure (NULL). | 403 | * @param cls Closure (NULL). |
395 | */ | 404 | */ |
396 | static void | 405 | static void |
397 | send_echo (void *cls) | 406 | send_echo(void *cls) |
398 | { | 407 | { |
399 | struct GNUNET_MQ_Envelope *env; | 408 | struct GNUNET_MQ_Envelope *env; |
400 | struct GNUNET_MessageHeader *msg; | 409 | struct GNUNET_MessageHeader *msg; |
@@ -402,8 +411,8 @@ send_echo (void *cls) | |||
402 | echo_task = NULL; | 411 | echo_task = NULL; |
403 | if (NULL == ch) | 412 | if (NULL == ch) |
404 | return; | 413 | return; |
405 | env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CLI); | 414 | env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_CADET_CLI); |
406 | GNUNET_MQ_send (GNUNET_CADET_get_mq (ch), env); | 415 | GNUNET_MQ_send(GNUNET_CADET_get_mq(ch), env); |
407 | } | 416 | } |
408 | 417 | ||
409 | 418 | ||
@@ -416,7 +425,7 @@ send_echo (void *cls) | |||
416 | * #GNUNET_SYSERR to close it (signal serious error). | 425 | * #GNUNET_SYSERR to close it (signal serious error). |
417 | */ | 426 | */ |
418 | static int | 427 | static int |
419 | check_data (void *cls, const struct GNUNET_MessageHeader *message) | 428 | check_data(void *cls, const struct GNUNET_MessageHeader *message) |
420 | { | 429 | { |
421 | return GNUNET_OK; /* all is well-formed */ | 430 | return GNUNET_OK; /* all is well-formed */ |
422 | } | 431 | } |
@@ -433,59 +442,59 @@ check_data (void *cls, const struct GNUNET_MessageHeader *message) | |||
433 | * @param message The actual message. | 442 | * @param message The actual message. |
434 | */ | 443 | */ |
435 | static void | 444 | static void |
436 | handle_data (void *cls, const struct GNUNET_MessageHeader *message) | 445 | handle_data(void *cls, const struct GNUNET_MessageHeader *message) |
437 | { | 446 | { |
438 | size_t payload_size = ntohs (message->size) - sizeof (*message); | 447 | size_t payload_size = ntohs(message->size) - sizeof(*message); |
439 | uint16_t len; | 448 | uint16_t len; |
440 | ssize_t done; | 449 | ssize_t done; |
441 | uint16_t off; | 450 | uint16_t off; |
442 | const char *buf; | 451 | const char *buf; |
443 | 452 | ||
444 | GNUNET_CADET_receive_done (ch); | 453 | GNUNET_CADET_receive_done(ch); |
445 | if (GNUNET_YES == echo) | 454 | if (GNUNET_YES == echo) |
446 | { | ||
447 | if (NULL != listen_port) | ||
448 | { | ||
449 | struct GNUNET_MQ_Envelope *env; | ||
450 | struct GNUNET_MessageHeader *msg; | ||
451 | |||
452 | env = | ||
453 | GNUNET_MQ_msg_extra (msg, payload_size, GNUNET_MESSAGE_TYPE_CADET_CLI); | ||
454 | GNUNET_memcpy (&msg[1], &message[1], payload_size); | ||
455 | GNUNET_MQ_send (GNUNET_CADET_get_mq (ch), env); | ||
456 | return; | ||
457 | } | ||
458 | else | ||
459 | { | 455 | { |
460 | struct GNUNET_TIME_Relative latency; | 456 | if (NULL != listen_port) |
461 | 457 | { | |
462 | latency = GNUNET_TIME_absolute_get_duration (echo_time); | 458 | struct GNUNET_MQ_Envelope *env; |
463 | echo_time = GNUNET_TIME_UNIT_FOREVER_ABS; | 459 | struct GNUNET_MessageHeader *msg; |
464 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 460 | |
465 | "time: %s\n", | 461 | env = |
466 | GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO)); | 462 | GNUNET_MQ_msg_extra(msg, payload_size, GNUNET_MESSAGE_TYPE_CADET_CLI); |
467 | echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | 463 | GNUNET_memcpy(&msg[1], &message[1], payload_size); |
468 | &send_echo, | 464 | GNUNET_MQ_send(GNUNET_CADET_get_mq(ch), env); |
469 | NULL); | 465 | return; |
466 | } | ||
467 | else | ||
468 | { | ||
469 | struct GNUNET_TIME_Relative latency; | ||
470 | |||
471 | latency = GNUNET_TIME_absolute_get_duration(echo_time); | ||
472 | echo_time = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
473 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, | ||
474 | "time: %s\n", | ||
475 | GNUNET_STRINGS_relative_time_to_string(latency, GNUNET_NO)); | ||
476 | echo_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, | ||
477 | &send_echo, | ||
478 | NULL); | ||
479 | } | ||
470 | } | 480 | } |
471 | } | ||
472 | 481 | ||
473 | len = ntohs (message->size) - sizeof (*message); | 482 | len = ntohs(message->size) - sizeof(*message); |
474 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len); | 483 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len); |
475 | buf = (const char *) &message[1]; | 484 | buf = (const char *)&message[1]; |
476 | off = 0; | 485 | off = 0; |
477 | while (off < len) | 486 | while (off < len) |
478 | { | ||
479 | done = write (1, &buf[off], len - off); | ||
480 | if (done <= 0) | ||
481 | { | 487 | { |
482 | if (-1 == done) | 488 | done = write(1, &buf[off], len - off); |
483 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write"); | 489 | if (done <= 0) |
484 | GNUNET_SCHEDULER_shutdown (); | 490 | { |
485 | return; | 491 | if (-1 == done) |
492 | GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "write"); | ||
493 | GNUNET_SCHEDULER_shutdown(); | ||
494 | return; | ||
495 | } | ||
496 | off += done; | ||
486 | } | 497 | } |
487 | off += done; | ||
488 | } | ||
489 | } | 498 | } |
490 | 499 | ||
491 | 500 | ||
@@ -499,19 +508,19 @@ handle_data (void *cls, const struct GNUNET_MessageHeader *message) | |||
499 | * @param ple information about peer, or NULL on "EOF". | 508 | * @param ple information about peer, or NULL on "EOF". |
500 | */ | 509 | */ |
501 | static void | 510 | static void |
502 | peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple) | 511 | peers_callback(void *cls, const struct GNUNET_CADET_PeerListEntry *ple) |
503 | { | 512 | { |
504 | if (NULL == ple) | 513 | if (NULL == ple) |
505 | { | 514 | { |
506 | plo = NULL; | 515 | plo = NULL; |
507 | GNUNET_SCHEDULER_shutdown (); | 516 | GNUNET_SCHEDULER_shutdown(); |
508 | return; | 517 | return; |
509 | } | 518 | } |
510 | fprintf (stdout, | 519 | fprintf(stdout, |
511 | "%s tunnel: %c, paths: %u\n", | 520 | "%s tunnel: %c, paths: %u\n", |
512 | GNUNET_i2s_full (&ple->peer), | 521 | GNUNET_i2s_full(&ple->peer), |
513 | ple->have_tunnel ? 'Y' : 'N', | 522 | ple->have_tunnel ? 'Y' : 'N', |
514 | ple->n_paths); | 523 | ple->n_paths); |
515 | } | 524 | } |
516 | 525 | ||
517 | 526 | ||
@@ -523,20 +532,20 @@ peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple) | |||
523 | * @param ppd path detail | 532 | * @param ppd path detail |
524 | */ | 533 | */ |
525 | static void | 534 | static void |
526 | path_callback (void *cls, const struct GNUNET_CADET_PeerPathDetail *ppd) | 535 | path_callback(void *cls, const struct GNUNET_CADET_PeerPathDetail *ppd) |
527 | { | 536 | { |
528 | if (NULL == ppd) | 537 | if (NULL == ppd) |
529 | { | 538 | { |
530 | gpo = NULL; | 539 | gpo = NULL; |
531 | GNUNET_SCHEDULER_shutdown (); | 540 | GNUNET_SCHEDULER_shutdown(); |
532 | return; | 541 | return; |
533 | } | 542 | } |
534 | fprintf (stdout, "Path of length %u: ", ppd->path_length); | 543 | fprintf(stdout, "Path of length %u: ", ppd->path_length); |
535 | for (unsigned int i = 0; i < ppd->path_length; i++) | 544 | for (unsigned int i = 0; i < ppd->path_length; i++) |
536 | fprintf (stdout, | 545 | fprintf(stdout, |
537 | (i == ppd->target_offset) ? "*%s* " : "%s ", | 546 | (i == ppd->target_offset) ? "*%s* " : "%s ", |
538 | GNUNET_i2s (&ppd->path[i])); | 547 | GNUNET_i2s(&ppd->path[i])); |
539 | fprintf (stdout, "\n"); | 548 | fprintf(stdout, "\n"); |
540 | } | 549 | } |
541 | 550 | ||
542 | 551 | ||
@@ -547,21 +556,21 @@ path_callback (void *cls, const struct GNUNET_CADET_PeerPathDetail *ppd) | |||
547 | * @param td tunnel details | 556 | * @param td tunnel details |
548 | */ | 557 | */ |
549 | static void | 558 | static void |
550 | tunnels_callback (void *cls, const struct GNUNET_CADET_TunnelDetails *td) | 559 | tunnels_callback(void *cls, const struct GNUNET_CADET_TunnelDetails *td) |
551 | { | 560 | { |
552 | if (NULL == td) | 561 | if (NULL == td) |
553 | { | 562 | { |
554 | tio = NULL; | 563 | tio = NULL; |
555 | GNUNET_SCHEDULER_shutdown (); | 564 | GNUNET_SCHEDULER_shutdown(); |
556 | return; | 565 | return; |
557 | } | 566 | } |
558 | fprintf (stdout, | 567 | fprintf(stdout, |
559 | "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n", | 568 | "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n", |
560 | GNUNET_i2s_full (&td->peer), | 569 | GNUNET_i2s_full(&td->peer), |
561 | enc_2s (td->estate), | 570 | enc_2s(td->estate), |
562 | conn_2s (td->cstate), | 571 | conn_2s(td->cstate), |
563 | td->channels, | 572 | td->channels, |
564 | td->connections); | 573 | td->connections); |
565 | } | 574 | } |
566 | 575 | ||
567 | 576 | ||
@@ -571,10 +580,10 @@ tunnels_callback (void *cls, const struct GNUNET_CADET_TunnelDetails *td) | |||
571 | * @param cls Closure (unused). | 580 | * @param cls Closure (unused). |
572 | */ | 581 | */ |
573 | static void | 582 | static void |
574 | get_peers (void *cls) | 583 | get_peers(void *cls) |
575 | { | 584 | { |
576 | job = NULL; | 585 | job = NULL; |
577 | plo = GNUNET_CADET_list_peers (my_cfg, &peers_callback, NULL); | 586 | plo = GNUNET_CADET_list_peers(my_cfg, &peers_callback, NULL); |
578 | } | 587 | } |
579 | 588 | ||
580 | 589 | ||
@@ -584,20 +593,20 @@ get_peers (void *cls) | |||
584 | * @param cls Closure (unused). | 593 | * @param cls Closure (unused). |
585 | */ | 594 | */ |
586 | static void | 595 | static void |
587 | show_peer (void *cls) | 596 | show_peer(void *cls) |
588 | { | 597 | { |
589 | struct GNUNET_PeerIdentity pid; | 598 | struct GNUNET_PeerIdentity pid; |
590 | 599 | ||
591 | job = NULL; | 600 | job = NULL; |
592 | if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id, | 601 | if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string(peer_id, |
593 | strlen (peer_id), | 602 | strlen(peer_id), |
594 | &pid.public_key)) | 603 | &pid.public_key)) |
595 | { | 604 | { |
596 | fprintf (stderr, _ ("Invalid peer ID `%s'\n"), peer_id); | 605 | fprintf(stderr, _("Invalid peer ID `%s'\n"), peer_id); |
597 | GNUNET_SCHEDULER_shutdown (); | 606 | GNUNET_SCHEDULER_shutdown(); |
598 | return; | 607 | return; |
599 | } | 608 | } |
600 | gpo = GNUNET_CADET_get_path (my_cfg, &pid, &path_callback, NULL); | 609 | gpo = GNUNET_CADET_get_path(my_cfg, &pid, &path_callback, NULL); |
601 | } | 610 | } |
602 | 611 | ||
603 | 612 | ||
@@ -607,10 +616,10 @@ show_peer (void *cls) | |||
607 | * @param cls Closure (unused). | 616 | * @param cls Closure (unused). |
608 | */ | 617 | */ |
609 | static void | 618 | static void |
610 | get_tunnels (void *cls) | 619 | get_tunnels(void *cls) |
611 | { | 620 | { |
612 | job = NULL; | 621 | job = NULL; |
613 | tio = GNUNET_CADET_list_tunnels (my_cfg, &tunnels_callback, NULL); | 622 | tio = GNUNET_CADET_list_tunnels(my_cfg, &tunnels_callback, NULL); |
614 | } | 623 | } |
615 | 624 | ||
616 | 625 | ||
@@ -620,10 +629,10 @@ get_tunnels (void *cls) | |||
620 | * @param cls Closure (unused). | 629 | * @param cls Closure (unused). |
621 | */ | 630 | */ |
622 | static void | 631 | static void |
623 | show_channel (void *cls) | 632 | show_channel(void *cls) |
624 | { | 633 | { |
625 | job = NULL; | 634 | job = NULL; |
626 | GNUNET_break (0); | 635 | GNUNET_break(0); |
627 | } | 636 | } |
628 | 637 | ||
629 | 638 | ||
@@ -633,10 +642,10 @@ show_channel (void *cls) | |||
633 | * @param cls Closure (unused). | 642 | * @param cls Closure (unused). |
634 | */ | 643 | */ |
635 | static void | 644 | static void |
636 | show_connection (void *cls) | 645 | show_connection(void *cls) |
637 | { | 646 | { |
638 | job = NULL; | 647 | job = NULL; |
639 | GNUNET_break (0); | 648 | GNUNET_break(0); |
640 | } | 649 | } |
641 | 650 | ||
642 | 651 | ||
@@ -649,17 +658,17 @@ show_connection (void *cls) | |||
649 | * @param cfg configuration | 658 | * @param cfg configuration |
650 | */ | 659 | */ |
651 | static void | 660 | static void |
652 | run (void *cls, | 661 | run(void *cls, |
653 | char *const *args, | 662 | char *const *args, |
654 | const char *cfgfile, | 663 | const char *cfgfile, |
655 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 664 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
656 | { | 665 | { |
657 | struct GNUNET_MQ_MessageHandler handlers[] = | 666 | struct GNUNET_MQ_MessageHandler handlers[] = |
658 | {GNUNET_MQ_hd_var_size (data, | 667 | { GNUNET_MQ_hd_var_size(data, |
659 | GNUNET_MESSAGE_TYPE_CADET_CLI, | 668 | GNUNET_MESSAGE_TYPE_CADET_CLI, |
660 | struct GNUNET_MessageHeader, | 669 | struct GNUNET_MessageHeader, |
661 | NULL), | 670 | NULL), |
662 | GNUNET_MQ_handler_end ()}; | 671 | GNUNET_MQ_handler_end() }; |
663 | 672 | ||
664 | /* FIXME add option to monitor apps */ | 673 | /* FIXME add option to monitor apps */ |
665 | my_cfg = cfg; | 674 | my_cfg = cfg; |
@@ -670,102 +679,102 @@ run (void *cls, | |||
670 | if ((0 != (request_peers | request_tunnels) || NULL != conn_id || | 679 | if ((0 != (request_peers | request_tunnels) || NULL != conn_id || |
671 | NULL != channel_id) && | 680 | NULL != channel_id) && |
672 | target_id != NULL) | 681 | target_id != NULL) |
673 | { | 682 | { |
674 | fprintf (stderr, | 683 | fprintf(stderr, |
675 | _ ("Extra arguments are not applicable " | 684 | _("Extra arguments are not applicable " |
676 | "in combination with this option.\n")); | 685 | "in combination with this option.\n")); |
677 | return; | 686 | return; |
678 | } | 687 | } |
679 | 688 | ||
680 | if (NULL != peer_id) | 689 | if (NULL != peer_id) |
681 | { | 690 | { |
682 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n"); | 691 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show peer\n"); |
683 | job = GNUNET_SCHEDULER_add_now (&show_peer, NULL); | 692 | job = GNUNET_SCHEDULER_add_now(&show_peer, NULL); |
684 | } | 693 | } |
685 | else if (NULL != channel_id) | 694 | else if (NULL != channel_id) |
686 | { | 695 | { |
687 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n"); | 696 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show channel\n"); |
688 | job = GNUNET_SCHEDULER_add_now (&show_channel, NULL); | 697 | job = GNUNET_SCHEDULER_add_now(&show_channel, NULL); |
689 | } | 698 | } |
690 | else if (NULL != conn_id) | 699 | else if (NULL != conn_id) |
691 | { | 700 | { |
692 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n"); | 701 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show connection\n"); |
693 | job = GNUNET_SCHEDULER_add_now (&show_connection, NULL); | 702 | job = GNUNET_SCHEDULER_add_now(&show_connection, NULL); |
694 | } | 703 | } |
695 | else if (GNUNET_YES == request_peers) | 704 | else if (GNUNET_YES == request_peers) |
696 | { | 705 | { |
697 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n"); | 706 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n"); |
698 | job = GNUNET_SCHEDULER_add_now (&get_peers, NULL); | 707 | job = GNUNET_SCHEDULER_add_now(&get_peers, NULL); |
699 | } | 708 | } |
700 | else if (GNUNET_YES == request_tunnels) | 709 | else if (GNUNET_YES == request_tunnels) |
701 | { | 710 | { |
702 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n"); | 711 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n"); |
703 | job = GNUNET_SCHEDULER_add_now (&get_tunnels, NULL); | 712 | job = GNUNET_SCHEDULER_add_now(&get_tunnels, NULL); |
704 | } | 713 | } |
705 | |||
706 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET service\n"); | ||
707 | mh = GNUNET_CADET_connect (cfg); | ||
708 | GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); | ||
709 | if (NULL == mh) | ||
710 | { | ||
711 | GNUNET_SCHEDULER_shutdown (); | ||
712 | return; | ||
713 | } | ||
714 | if (NULL != listen_port) | ||
715 | { | ||
716 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening CADET listen port\n"); | ||
717 | GNUNET_CRYPTO_hash (listen_port, strlen (listen_port), &porthash); | ||
718 | lp = GNUNET_CADET_open_port (mh, | ||
719 | &porthash, | ||
720 | &channel_incoming, | ||
721 | NULL, | ||
722 | NULL /* window changes */, | ||
723 | &channel_ended, | ||
724 | handlers); | ||
725 | } | ||
726 | if (NULL != target_id) | ||
727 | { | ||
728 | struct GNUNET_PeerIdentity pid; | ||
729 | 714 | ||
730 | if (GNUNET_OK != | 715 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET service\n"); |
731 | GNUNET_CRYPTO_eddsa_public_key_from_string (target_id, | 716 | mh = GNUNET_CADET_connect(cfg); |
732 | strlen (target_id), | 717 | GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL); |
733 | &pid.public_key)) | 718 | if (NULL == mh) |
734 | { | 719 | { |
735 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | 720 | GNUNET_SCHEDULER_shutdown(); |
736 | _ ("Invalid target `%s'\n"), | ||
737 | target_id); | ||
738 | GNUNET_SCHEDULER_shutdown (); | ||
739 | return; | 721 | return; |
740 | } | 722 | } |
741 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 723 | if (NULL != listen_port) |
742 | "Connecting to `%s:%s'\n", | ||
743 | target_id, | ||
744 | target_port); | ||
745 | GNUNET_CRYPTO_hash (target_port, strlen (target_port), &porthash); | ||
746 | ch = GNUNET_CADET_channel_create (mh, | ||
747 | NULL, | ||
748 | &pid, | ||
749 | &porthash, | ||
750 | NULL /* window changes */, | ||
751 | &channel_ended, | ||
752 | handlers); | ||
753 | if (GNUNET_YES == echo) | ||
754 | { | 724 | { |
755 | echo_task = GNUNET_SCHEDULER_add_now (&send_echo, NULL); | 725 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening CADET listen port\n"); |
726 | GNUNET_CRYPTO_hash(listen_port, strlen(listen_port), &porthash); | ||
727 | lp = GNUNET_CADET_open_port(mh, | ||
728 | &porthash, | ||
729 | &channel_incoming, | ||
730 | NULL, | ||
731 | NULL /* window changes */, | ||
732 | &channel_ended, | ||
733 | handlers); | ||
756 | } | 734 | } |
757 | else | 735 | if (NULL != target_id) |
758 | { | 736 | { |
759 | listen_stdio (); | 737 | struct GNUNET_PeerIdentity pid; |
738 | |||
739 | if (GNUNET_OK != | ||
740 | GNUNET_CRYPTO_eddsa_public_key_from_string(target_id, | ||
741 | strlen(target_id), | ||
742 | &pid.public_key)) | ||
743 | { | ||
744 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, | ||
745 | _("Invalid target `%s'\n"), | ||
746 | target_id); | ||
747 | GNUNET_SCHEDULER_shutdown(); | ||
748 | return; | ||
749 | } | ||
750 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
751 | "Connecting to `%s:%s'\n", | ||
752 | target_id, | ||
753 | target_port); | ||
754 | GNUNET_CRYPTO_hash(target_port, strlen(target_port), &porthash); | ||
755 | ch = GNUNET_CADET_channel_create(mh, | ||
756 | NULL, | ||
757 | &pid, | ||
758 | &porthash, | ||
759 | NULL /* window changes */, | ||
760 | &channel_ended, | ||
761 | handlers); | ||
762 | if (GNUNET_YES == echo) | ||
763 | { | ||
764 | echo_task = GNUNET_SCHEDULER_add_now(&send_echo, NULL); | ||
765 | } | ||
766 | else | ||
767 | { | ||
768 | listen_stdio(); | ||
769 | } | ||
760 | } | 770 | } |
761 | } | ||
762 | 771 | ||
763 | if ((NULL == lp) && (NULL == job) && (NULL == ch)) | 772 | if ((NULL == lp) && (NULL == job) && (NULL == ch)) |
764 | { | 773 | { |
765 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, _ ("No action requested\n")); | 774 | GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, _("No action requested\n")); |
766 | GNUNET_SCHEDULER_shutdown (); | 775 | GNUNET_SCHEDULER_shutdown(); |
767 | return; | 776 | return; |
768 | } | 777 | } |
769 | } | 778 | } |
770 | 779 | ||
771 | 780 | ||
@@ -777,60 +786,61 @@ run (void *cls, | |||
777 | * @return 0 ok, 1 on error | 786 | * @return 0 ok, 1 on error |
778 | */ | 787 | */ |
779 | int | 788 | int |
780 | main (int argc, char *const *argv) | 789 | main(int argc, char *const *argv) |
781 | { | 790 | { |
782 | int res; | 791 | int res; |
783 | const char helpstr[] = | 792 | const char helpstr[] = |
784 | "Create tunnels and retrieve info about CADET's status."; | 793 | "Create tunnels and retrieve info about CADET's status."; |
785 | struct GNUNET_GETOPT_CommandLineOption options[] = | 794 | struct GNUNET_GETOPT_CommandLineOption options[] = |
786 | {/* I would use the terminology 'circuit' here... --lynX */ | 795 | { /* I would use the terminology 'circuit' here... --lynX */ |
787 | GNUNET_GETOPT_option_string ( | 796 | GNUNET_GETOPT_option_string( |
788 | 'C', | 797 | 'C', |
789 | "connection", | 798 | "connection", |
790 | "CONNECTION_ID", | 799 | "CONNECTION_ID", |
791 | gettext_noop ("Provide information about a particular connection"), | 800 | gettext_noop("Provide information about a particular connection"), |
792 | &conn_id), | 801 | &conn_id), |
793 | GNUNET_GETOPT_option_flag ('e', | 802 | GNUNET_GETOPT_option_flag('e', |
794 | "echo", | 803 | "echo", |
795 | gettext_noop ("Activate echo mode"), | 804 | gettext_noop("Activate echo mode"), |
796 | &echo), | 805 | &echo), |
797 | GNUNET_GETOPT_option_string ( | 806 | GNUNET_GETOPT_option_string( |
798 | 'o', | 807 | 'o', |
799 | "open-port", | 808 | "open-port", |
800 | "SHARED_SECRET", | 809 | "SHARED_SECRET", |
801 | gettext_noop ( | 810 | gettext_noop( |
802 | "Listen for connections using a shared secret among sender and recipient"), | 811 | "Listen for connections using a shared secret among sender and recipient"), |
803 | &listen_port), | 812 | &listen_port), |
804 | GNUNET_GETOPT_option_string ('p', | 813 | GNUNET_GETOPT_option_string('p', |
805 | "peer", | 814 | "peer", |
806 | "PEER_ID", | 815 | "PEER_ID", |
807 | gettext_noop ( | 816 | gettext_noop( |
808 | "Provide information about a patricular peer"), | 817 | "Provide information about a patricular peer"), |
809 | &peer_id), | 818 | &peer_id), |
810 | GNUNET_GETOPT_option_flag ('P', | 819 | GNUNET_GETOPT_option_flag('P', |
811 | "peers", | 820 | "peers", |
812 | gettext_noop ( | 821 | gettext_noop( |
813 | "Provide information about all peers"), | 822 | "Provide information about all peers"), |
814 | &request_peers), | 823 | &request_peers), |
815 | GNUNET_GETOPT_option_flag ('T', | 824 | GNUNET_GETOPT_option_flag('T', |
816 | "tunnels", | 825 | "tunnels", |
817 | gettext_noop ( | 826 | gettext_noop( |
818 | "Provide information about all tunnels"), | 827 | "Provide information about all tunnels"), |
819 | &request_tunnels), | 828 | &request_tunnels), |
820 | GNUNET_GETOPT_OPTION_END}; | 829 | GNUNET_GETOPT_OPTION_END |
821 | 830 | }; | |
822 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 831 | |
832 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) | ||
823 | return 2; | 833 | return 2; |
824 | 834 | ||
825 | res = GNUNET_PROGRAM_run (argc, | 835 | res = GNUNET_PROGRAM_run(argc, |
826 | argv, | 836 | argv, |
827 | "gnunet-cadet (OPTIONS | PEER_ID SHARED_SECRET)", | 837 | "gnunet-cadet (OPTIONS | PEER_ID SHARED_SECRET)", |
828 | gettext_noop (helpstr), | 838 | gettext_noop(helpstr), |
829 | options, | 839 | options, |
830 | &run, | 840 | &run, |
831 | NULL); | 841 | NULL); |
832 | 842 | ||
833 | GNUNET_free ((void *) argv); | 843 | GNUNET_free((void *)argv); |
834 | 844 | ||
835 | if (GNUNET_OK == res) | 845 | if (GNUNET_OK == res) |
836 | return 0; | 846 | return 0; |