aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/gnunet-cadet.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-02-17 20:22:54 +0100
committerChristian Grothoff <christian@grothoff.org>2017-02-17 20:22:54 +0100
commitcbafa8e54f0399bf4213c313a72907ba92250375 (patch)
tree63eb66a2c878048527e99ba3eb2b24c0797774eb /src/cadet/gnunet-cadet.c
parent89d3a0ad2a64c027af5a1a9ac4f3ec23a8ae8868 (diff)
downloadgnunet-cbafa8e54f0399bf4213c313a72907ba92250375.tar.gz
gnunet-cbafa8e54f0399bf4213c313a72907ba92250375.zip
misc fixes in new CADET API, porting gnunet-cadet to new API
Diffstat (limited to 'src/cadet/gnunet-cadet.c')
-rw-r--r--src/cadet/gnunet-cadet.c559
1 files changed, 249 insertions, 310 deletions
diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c
index f9f156f58..d4286a1b0 100644
--- a/src/cadet/gnunet-cadet.c
+++ b/src/cadet/gnunet-cadet.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V. 3 Copyright (C) 2012, 2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -22,6 +22,7 @@
22 * @file cadet/gnunet-cadet.c 22 * @file cadet/gnunet-cadet.c
23 * @brief Print information about cadet tunnels and peers. 23 * @brief Print information about cadet tunnels and peers.
24 * @author Bartlomiej Polot 24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
25 */ 26 */
26#include "platform.h" 27#include "platform.h"
27#include "gnunet_util_lib.h" 28#include "gnunet_util_lib.h"
@@ -30,11 +31,6 @@
30 31
31 32
32/** 33/**
33 * Option -m.
34 */
35static int monitor_mode;
36
37/**
38 * Option -P. 34 * Option -P.
39 */ 35 */
40static int request_peers; 36static int request_peers;
@@ -100,11 +96,6 @@ static char *target_id;
100static char *target_port = "default"; 96static char *target_port = "default";
101 97
102/** 98/**
103 * Data pending in netcat mode.
104 */
105static size_t data_size;
106
107/**
108 * Cadet handle. 99 * Cadet handle.
109 */ 100 */
110static struct GNUNET_CADET_Handle *mh; 101static struct GNUNET_CADET_Handle *mh;
@@ -115,11 +106,6 @@ static struct GNUNET_CADET_Handle *mh;
115static struct GNUNET_CADET_Channel *ch; 106static struct GNUNET_CADET_Channel *ch;
116 107
117/** 108/**
118 * Transmit handle.
119 */
120static struct GNUNET_CADET_TransmitHandle *th;
121
122/**
123 * HashCode of the given port string 109 * HashCode of the given port string
124 */ 110 */
125static struct GNUNET_HashCode porthash; 111static struct GNUNET_HashCode porthash;
@@ -130,11 +116,6 @@ static struct GNUNET_HashCode porthash;
130struct GNUNET_CADET_Port *lp; 116struct GNUNET_CADET_Port *lp;
131 117
132/** 118/**
133 * Shutdown task handle.
134 */
135static struct GNUNET_SCHEDULER_Task *sd;
136
137/**
138 * Task for reading from stdin. 119 * Task for reading from stdin.
139 */ 120 */
140static struct GNUNET_SCHEDULER_Task *rd_task; 121static struct GNUNET_SCHEDULER_Task *rd_task;
@@ -145,6 +126,9 @@ static struct GNUNET_SCHEDULER_Task *rd_task;
145static struct GNUNET_SCHEDULER_Task *job; 126static struct GNUNET_SCHEDULER_Task *job;
146 127
147 128
129/**
130 * Wait for input on STDIO and send it out over the #ch.
131 */
148static void 132static void
149listen_stdio (void); 133listen_stdio (void);
150 134
@@ -214,22 +198,11 @@ shutdown_task (void *cls)
214{ 198{
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Shutdown\n"); 200 "Shutdown\n");
217 if (NULL != th)
218 {
219 GNUNET_CADET_notify_transmit_ready_cancel (th);
220 th = NULL;
221 }
222 if (NULL != ch) 201 if (NULL != ch)
223 { 202 {
224 GNUNET_CADET_channel_destroy (ch); 203 GNUNET_CADET_channel_destroy (ch);
225 ch = NULL; 204 ch = NULL;
226 } 205 }
227 else if (NULL != target_id) {
228 // FIXME: would be nicer to have proper NACK support from cadet_api
229 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
230 "Connection refused to %s\n",
231 target_id);
232 }
233 if (NULL != mh) 206 if (NULL != mh)
234 { 207 {
235 GNUNET_CADET_disconnect (mh); 208 GNUNET_CADET_disconnect (mh);
@@ -254,42 +227,38 @@ shutdown_task (void *cls)
254 227
255 228
256/** 229/**
257 * Function called to notify a client about the connection 230 * Task run in stdio mode, after some data is available at stdin.
258 * begin ready to queue more data. "buf" will be
259 * NULL and "size" zero if the connection was closed for
260 * writing in the meantime.
261 *
262 * FIXME
263 * 231 *
264 * @param cls closure 232 * @param cls Closure (unused).
265 * @param size number of bytes available in buf
266 * @param buf where the callee should write the message
267 * @return number of bytes written to buf
268 */ 233 */
269static size_t 234static void
270data_ready (void *cls, size_t size, void *buf) 235read_stdio (void *cls)
271{ 236{
237 struct GNUNET_MQ_Envelope *env;
272 struct GNUNET_MessageHeader *msg; 238 struct GNUNET_MessageHeader *msg;
273 size_t total_size; 239 char buf[60000];
240 ssize_t data_size;
274 241
275 th = NULL; 242 rd_task = NULL;
276 243 data_size = read (0,
277 if (NULL == buf || 0 == size) 244 buf,
245 60000);
246 if (data_size < 1)
278 { 247 {
279 GNUNET_SCHEDULER_shutdown(); 248 GNUNET_SCHEDULER_shutdown();
280 return 0; 249 return;
281 } 250 }
282
283 total_size = data_size + sizeof (struct GNUNET_MessageHeader);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "sending %u bytes\n", 252 "Read %u bytes from stdio\n",
286 (unsigned int) data_size); 253 (unsigned int) data_size);
287 GNUNET_assert (size >= total_size); 254 env = GNUNET_MQ_msg_extra (msg,
288 255 data_size,
289 msg = buf; 256 GNUNET_MESSAGE_TYPE_CADET_CLI);
290 msg->size = htons (total_size); 257 GNUNET_memcpy (&msg[1],
291 msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_CLI); 258 buf,
292 GNUNET_memcpy (&msg[1], cls, data_size); 259 data_size);
260 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
261 env);
293 if (GNUNET_NO == echo) 262 if (GNUNET_NO == echo)
294 { 263 {
295 listen_stdio (); 264 listen_stdio ();
@@ -298,54 +267,27 @@ data_ready (void *cls, size_t size, void *buf)
298 { 267 {
299 echo_time = GNUNET_TIME_absolute_get (); 268 echo_time = GNUNET_TIME_absolute_get ();
300 } 269 }
301
302 return total_size;
303}
304
305
306/**
307 * Task run in stdio mode, after some data is available at stdin.
308 *
309 * @param cls Closure (unused).
310 */
311static void
312read_stdio (void *cls)
313{
314 static char buf[60000];
315
316 rd_task = NULL;
317 data_size = read (0, buf, 60000);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "stdio read %u bytes\n",
320 (unsigned int) data_size);
321 if (data_size < 1)
322 {
323 GNUNET_SCHEDULER_shutdown();
324 return;
325 }
326 GNUNET_assert (NULL == th);
327 th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
328 GNUNET_TIME_UNIT_FOREVER_REL,
329 sizeof (struct GNUNET_MessageHeader)
330 + data_size,
331 &data_ready, buf);
332} 270}
333 271
334 272
335/** 273/**
336 * Start listening to stdin 274 * Wait for input on STDIO and send it out over the #ch.
337 */ 275 */
338static void 276static void
339listen_stdio (void) 277listen_stdio ()
340{ 278{
341 struct GNUNET_NETWORK_FDSet *rs; 279 struct GNUNET_NETWORK_FDSet *rs;
342 280
281 /* FIXME: why use 'rs' here, seems overly complicated... */
343 rs = GNUNET_NETWORK_fdset_create (); 282 rs = GNUNET_NETWORK_fdset_create ();
344 GNUNET_NETWORK_fdset_set_native (rs, 0); 283 GNUNET_NETWORK_fdset_set_native (rs,
284 0); /* STDIN */
345 rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 285 rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
346 GNUNET_TIME_UNIT_FOREVER_REL, 286 GNUNET_TIME_UNIT_FOREVER_REL,
347 rs, NULL, 287 rs,
348 &read_stdio, NULL); 288 NULL,
289 &read_stdio,
290 NULL);
349 GNUNET_NETWORK_fdset_destroy (rs); 291 GNUNET_NETWORK_fdset_destroy (rs);
350} 292}
351 293
@@ -356,32 +298,17 @@ listen_stdio (void)
356 * 298 *
357 * It must NOT call #GNUNET_CADET_channel_destroy on the channel. 299 * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
358 * 300 *
359 * @param cls closure (set from #GNUNET_CADET_connect) 301 * @param cls closure
360 * @param channel connection to the other end (henceforth invalid) 302 * @param channel connection to the other end (henceforth invalid)
361 * @param channel_ctx place where local state associated
362 * with the channel is stored
363 */ 303 */
364static void 304static void
365channel_ended (void *cls, 305channel_ended (void *cls,
366 const struct GNUNET_CADET_Channel *channel, 306 const struct GNUNET_CADET_Channel *channel)
367 void *channel_ctx)
368{ 307{
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n"); 308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 if (channel != ch) 309 "Channel ended!\n");
371 { 310 GNUNET_assert (channel == ch);
372 GNUNET_break (0); 311 ch = NULL;
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ended: %p, expected: %p\n", channel, ch);
374 }
375 else
376 {
377 ch = NULL;
378 }
379 if (NULL != th)
380 {
381 GNUNET_CADET_notify_transmit_ready_cancel (th);
382 th = NULL;
383 }
384
385 GNUNET_SCHEDULER_shutdown (); 312 GNUNET_SCHEDULER_shutdown ();
386} 313}
387 314
@@ -398,65 +325,27 @@ channel_ended (void *cls,
398 * @param cls closure 325 * @param cls closure
399 * @param channel new handle to the channel 326 * @param channel new handle to the channel
400 * @param initiator peer that started the channel 327 * @param initiator peer that started the channel
401 * @param port Port this channel is for. 328 * @return initial channel context for the channel, we use @a channel
402 * @param options CadetOption flag field, with all active option bits set to 1.
403 *
404 * @return initial channel context for the channel
405 * (can be NULL -- that's not an error)
406 */ 329 */
407static void * 330static void *
408channel_incoming (void *cls, 331channel_incoming (void *cls,
409 struct GNUNET_CADET_Channel *channel, 332 struct GNUNET_CADET_Channel *channel,
410 const struct GNUNET_PeerIdentity *initiator, 333 const struct GNUNET_PeerIdentity *initiator)
411 const struct GNUNET_HashCode *port,
412 enum GNUNET_CADET_ChannelOption options)
413{ 334{
414 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Connected from %s\n", 336 "Incomming connection from %s\n",
416 GNUNET_i2s_full (initiator)); 337 GNUNET_i2s_full (initiator));
417 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 338 GNUNET_assert (NULL == ch);
418 "Incoming channel %p on port %s\n", 339 GNUNET_assert (NULL != lp);
419 channel, GNUNET_h2s (port)); 340 GNUNET_CADET_close_port (lp);
420 if (NULL != ch) 341 lp = NULL;
421 {
422 GNUNET_break (0);
423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
424 "A channel already exists (%p)\n", ch);
425 /*
426 * From now on multiple channels will be sending data to us
427 * making the service of this command unpredictable in its
428 * current implementation. So for now let's just bail out.
429 */
430 GNUNET_SCHEDULER_shutdown();
431 return NULL;
432 }
433 if (NULL == listen_port)
434 {
435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Not listening to channels\n");
436 return NULL;
437 }
438 // Closing the listen port currently breaks open connections.
439 // Is this an intentional departure from POSIX socket behavior?
440 //
441 if (NULL != lp) {
442 /* Now that we have our circuit up and running, let's not
443 * get confused by further incoming connect requests.
444 * You need to start another 'gnunet-cadet -o' process
445 * to receive those.
446 */
447 GNUNET_CADET_close_port (lp);
448 lp = NULL;
449 }
450 ch = channel; 342 ch = channel;
451 if (GNUNET_NO == echo) 343 if (GNUNET_NO == echo)
452 {
453 listen_stdio (); 344 listen_stdio ();
454 return NULL; 345 return channel;
455 }
456 data_size = 0;
457 return NULL;
458} 346}
459 347
348
460/** 349/**
461 * @brief Send an echo request to the remote peer. 350 * @brief Send an echo request to the remote peer.
462 * 351 *
@@ -465,14 +354,16 @@ channel_incoming (void *cls,
465static void 354static void
466send_echo (void *cls) 355send_echo (void *cls)
467{ 356{
357 struct GNUNET_MQ_Envelope *env;
358 struct GNUNET_MessageHeader *msg;
359
468 echo_task = NULL; 360 echo_task = NULL;
469 if (NULL == ch) 361 if (NULL == ch)
470 return; 362 return;
471 GNUNET_assert (NULL == th); 363 env = GNUNET_MQ_msg (msg,
472 th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO, 364 GNUNET_MESSAGE_TYPE_CADET_CLI);
473 GNUNET_TIME_UNIT_FOREVER_REL, 365 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
474 sizeof (struct GNUNET_MessageHeader), 366 env);
475 &data_ready, NULL);
476} 367}
477 368
478 369
@@ -485,44 +376,27 @@ static void
485request_dump (void *cls) 376request_dump (void *cls)
486{ 377{
487 GNUNET_CADET_request_dump (mh); 378 GNUNET_CADET_request_dump (mh);
488 GNUNET_SCHEDULER_cancel (sd); 379 GNUNET_SCHEDULER_shutdown ();
489 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
490 &shutdown_task, NULL);
491} 380}
492 381
493 382
494/** 383/**
495 * Call CADET's monitor API, get info of one connection. 384 * Function called whenever a message is received.
496 * 385 *
497 * @param cls Closure (unused). 386 * Each time the function must call #GNUNET_CADET_receive_done on the channel
387 * in order to receive the next message. This doesn't need to be immediate:
388 * can be delayed if some processing is done on the message.
389 *
390 * @param cls should match #ch
391 * @param message The actual message.
392 * @return #GNUNET_OK to keep the channel open,
393 * #GNUNET_SYSERR to close it (signal serious error).
498 */ 394 */
499static void 395static int
500create_channel (void *cls) 396check_data (void *cls,
397 const struct GNUNET_MessageHeader *message)
501{ 398{
502 struct GNUNET_PeerIdentity pid; 399 return GNUNET_OK; /* all is well-formed */
503 enum GNUNET_CADET_ChannelOption opt;
504
505 GNUNET_assert (NULL == ch);
506
507 if (GNUNET_OK !=
508 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
509 strlen (target_id),
510 &pid.public_key))
511 {
512 FPRINTF (stderr,
513 _("Invalid target `%s'\n"),
514 target_id);
515 GNUNET_SCHEDULER_shutdown ();
516 return;
517 }
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
519 opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
520 GNUNET_CRYPTO_hash (target_port, strlen(target_port), &porthash);
521 ch = GNUNET_CADET_channel_create (mh, NULL, &pid, &porthash, opt);
522 if (GNUNET_NO == echo)
523 listen_stdio ();
524 else
525 echo_task = GNUNET_SCHEDULER_add_now (&send_echo, NULL);
526} 400}
527 401
528 402
@@ -533,42 +407,36 @@ create_channel (void *cls)
533 * in order to receive the next message. This doesn't need to be immediate: 407 * in order to receive the next message. This doesn't need to be immediate:
534 * can be delayed if some processing is done on the message. 408 * can be delayed if some processing is done on the message.
535 * 409 *
536 * @param cls Closure (set from #GNUNET_CADET_connect). 410 * @param cls NULL
537 * @param channel Connection to the other end.
538 * @param channel_ctx Place to store local state associated with the channel.
539 * @param message The actual message. 411 * @param message The actual message.
540 * @return #GNUNET_OK to keep the channel open,
541 * #GNUNET_SYSERR to close it (signal serious error).
542 */ 412 */
543static int 413static void
544data_callback (void *cls, 414handle_data (void *cls,
545 struct GNUNET_CADET_Channel *channel, 415 const struct GNUNET_MessageHeader *message)
546 void **channel_ctx,
547 const struct GNUNET_MessageHeader *message)
548{ 416{
417 size_t payload_size = ntohs (message->size) - sizeof (*message);
549 uint16_t len; 418 uint16_t len;
550 ssize_t done; 419 ssize_t done;
551 uint16_t off; 420 uint16_t off;
552 const char *buf; 421 const char *buf;
553 GNUNET_break (ch == channel);
554 GNUNET_CADET_receive_done (channel);
555 422
423 GNUNET_CADET_receive_done (ch);
556 if (GNUNET_YES == echo) 424 if (GNUNET_YES == echo)
557 { 425 {
558 if (NULL != listen_port) 426 if (NULL != listen_port)
559 { 427 {
560 /* Just listening to echo incoming messages*/ 428 struct GNUNET_MQ_Envelope *env;
561 if (NULL != th) 429 struct GNUNET_MessageHeader *msg;
562 { 430
563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 431 env = GNUNET_MQ_msg_extra (msg,
564 "Last echo reply not yet sent, dropping current reply.\n"); 432 payload_size,
565 return GNUNET_OK; 433 GNUNET_MESSAGE_TYPE_CADET_CLI);
566 } 434 GNUNET_memcpy (&msg[1],
567 th = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO, 435 &message[1],
568 GNUNET_TIME_UNIT_FOREVER_REL, 436 payload_size);
569 sizeof (struct GNUNET_MessageHeader), 437 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
570 &data_ready, NULL); 438 env);
571 return GNUNET_OK; 439 return;
572 } 440 }
573 else 441 else
574 { 442 {
@@ -576,30 +444,37 @@ data_callback (void *cls,
576 444
577 latency = GNUNET_TIME_absolute_get_duration (echo_time); 445 latency = GNUNET_TIME_absolute_get_duration (echo_time);
578 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS; 446 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
579 FPRINTF (stdout, "time: %s\n", 447 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
580 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO)); 448 "time: %s\n",
449 GNUNET_STRINGS_relative_time_to_string (latency,
450 GNUNET_NO));
581 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 451 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
582 &send_echo, NULL); 452 &send_echo,
453 NULL);
583 } 454 }
584 } 455 }
585 456
586 len = ntohs (message->size) - sizeof (*message); 457 len = ntohs (message->size) - sizeof (*message);
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len); 458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Got %u bytes\n",
460 len);
588 buf = (const char *) &message[1]; 461 buf = (const char *) &message[1];
589 off = 0; 462 off = 0;
590 while (off < len) 463 while (off < len)
591 { 464 {
592 done = write (1, &buf[off], len - off); 465 done = write (1,
466 &buf[off],
467 len - off);
593 if (done <= 0) 468 if (done <= 0)
594 { 469 {
595 if (-1 == done) 470 if (-1 == done)
596 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 471 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
597 "write"); 472 "write");
598 return GNUNET_SYSERR; 473 GNUNET_SCHEDULER_shutdown ();
474 return;
599 } 475 }
600 off += done; 476 off += done;
601 } 477 }
602 return GNUNET_OK;
603} 478}
604 479
605 480
@@ -625,16 +500,17 @@ peers_callback (void *cls,
625{ 500{
626 if (NULL == peer) 501 if (NULL == peer)
627 { 502 {
628 if (GNUNET_YES != monitor_mode) 503 GNUNET_SCHEDULER_shutdown();
629 {
630 GNUNET_SCHEDULER_shutdown();
631 }
632 return; 504 return;
633 } 505 }
634 FPRINTF (stdout, "%s tunnel: %c, paths: %u\n", 506 FPRINTF (stdout,
635 GNUNET_i2s_full (peer), tunnel ? 'Y' : 'N', n_paths); 507 "%s tunnel: %c, paths: %u\n",
508 GNUNET_i2s_full (peer),
509 tunnel ? 'Y' : 'N',
510 n_paths);
636} 511}
637 512
513
638/** 514/**
639 * Method called to retrieve information about a specific peer 515 * Method called to retrieve information about a specific peer
640 * known to the service. 516 * known to the service.
@@ -654,19 +530,26 @@ peer_callback (void *cls,
654 int tunnel, 530 int tunnel,
655 int neighbor, 531 int neighbor,
656 unsigned int n_paths, 532 unsigned int n_paths,
657 struct GNUNET_PeerIdentity *paths) 533 const struct GNUNET_PeerIdentity *paths)
658{ 534{
659 unsigned int i; 535 unsigned int i;
660 struct GNUNET_PeerIdentity *p; 536 const struct GNUNET_PeerIdentity *p;
661 537
662 FPRINTF (stdout, "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n", 538 FPRINTF (stdout,
539 "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n",
663 GNUNET_i2s_full (peer), 540 GNUNET_i2s_full (peer),
664 tunnel ? "Y" : "N", neighbor ? "Y" : "N", n_paths); 541 tunnel ? "Y" : "N",
542 neighbor ? "Y" : "N",
543 n_paths);
665 p = paths; 544 p = paths;
666 for (i = 0; i < n_paths && NULL != p;) 545 for (i = 0; i < n_paths && NULL != p;)
667 { 546 {
668 FPRINTF (stdout, "%s ", GNUNET_i2s (p)); 547 FPRINTF (stdout,
669 if (0 == memcmp (p, peer, sizeof (*p))) 548 "%s ",
549 GNUNET_i2s (p));
550 if (0 == memcmp (p,
551 peer,
552 sizeof (*p)))
670 { 553 {
671 FPRINTF (stdout, "\n"); 554 FPRINTF (stdout, "\n");
672 i++; 555 i++;
@@ -698,16 +581,16 @@ tunnels_callback (void *cls,
698{ 581{
699 if (NULL == peer) 582 if (NULL == peer)
700 { 583 {
701 if (GNUNET_YES != monitor_mode) 584 GNUNET_SCHEDULER_shutdown();
702 {
703 GNUNET_SCHEDULER_shutdown();
704 }
705 return; 585 return;
706 } 586 }
707 FPRINTF (stdout, "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n", 587 FPRINTF (stdout,
588 "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n",
708 GNUNET_i2s_full (peer), 589 GNUNET_i2s_full (peer),
709 enc_2s (estate), conn_2s (cstate), 590 enc_2s (estate),
710 channels, connections); 591 conn_2s (cstate),
592 channels,
593 connections);
711} 594}
712 595
713 596
@@ -748,11 +631,7 @@ tunnel_callback (void *cls,
748 FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate)); 631 FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate));
749 FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate)); 632 FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate));
750 } 633 }
751 if (GNUNET_YES != monitor_mode) 634 GNUNET_SCHEDULER_shutdown ();
752 {
753 GNUNET_SCHEDULER_shutdown ();
754 }
755 return;
756} 635}
757 636
758 637
@@ -876,18 +755,21 @@ run (void *cls,
876 const char *cfgfile, 755 const char *cfgfile,
877 const struct GNUNET_CONFIGURATION_Handle *cfg) 756 const struct GNUNET_CONFIGURATION_Handle *cfg)
878{ 757{
879 static const struct GNUNET_CADET_MessageHandler handlers[] = { 758 struct GNUNET_MQ_MessageHandler handlers[] = {
880 {&data_callback, GNUNET_MESSAGE_TYPE_CADET_CLI, 0}, 759 GNUNET_MQ_hd_var_size (data,
881 {NULL, 0, 0} /* FIXME add option to monitor msg types */ 760 GNUNET_MESSAGE_TYPE_CADET_CLI,
761 struct GNUNET_MessageHeader,
762 NULL),
763 GNUNET_MQ_handler_end ()
882 }; 764 };
883 765
884 /* FIXME add option to monitor apps */ 766 /* FIXME add option to monitor apps */
885 767
886 target_id = args[0]; 768 target_id = args[0];
887 if (target_id && args[1]) target_port = args[1]; 769 if (target_id && args[1])
770 target_port = args[1];
888 771
889 if ( (0 != (request_peers | request_tunnels) 772 if ( (0 != (request_peers | request_tunnels)
890 || 0 != monitor_mode
891 || NULL != tunnel_id 773 || NULL != tunnel_id
892 || NULL != conn_id 774 || NULL != conn_id
893 || NULL != channel_id) 775 || NULL != channel_id)
@@ -903,66 +785,128 @@ run (void *cls,
903 { 785 {
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "requesting debug dump\n"); 787 "requesting debug dump\n");
906 GNUNET_SCHEDULER_add_now (&request_dump, NULL); 788 job = GNUNET_SCHEDULER_add_now (&request_dump,
907 } 789 NULL);
908 else if (NULL != target_id)
909 {
910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911 "Creating channel to %s\n",
912 target_id);
913 GNUNET_SCHEDULER_add_now (&create_channel, NULL);
914 } 790 }
915 else if (NULL != peer_id) 791 else if (NULL != peer_id)
916 { 792 {
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n"); 793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 job = GNUNET_SCHEDULER_add_now (&show_peer, NULL); 794 "Show peer\n");
795 job = GNUNET_SCHEDULER_add_now (&show_peer,
796 NULL);
919 } 797 }
920 else if (NULL != tunnel_id) 798 else if (NULL != tunnel_id)
921 { 799 {
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n"); 800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 job = GNUNET_SCHEDULER_add_now (&show_tunnel, NULL); 801 "Show tunnel\n");
802 job = GNUNET_SCHEDULER_add_now (&show_tunnel,
803 NULL);
924 } 804 }
925 else if (NULL != channel_id) 805 else if (NULL != channel_id)
926 { 806 {
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n"); 807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 job = GNUNET_SCHEDULER_add_now (&show_channel, NULL); 808 "Show channel\n");
809 job = GNUNET_SCHEDULER_add_now (&show_channel,
810 NULL);
929 } 811 }
930 else if (NULL != conn_id) 812 else if (NULL != conn_id)
931 { 813 {
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n"); 814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 job = GNUNET_SCHEDULER_add_now (&show_connection, NULL); 815 "Show connection\n");
816 job = GNUNET_SCHEDULER_add_now (&show_connection,
817 NULL);
934 } 818 }
935 else if (GNUNET_YES == request_peers) 819 else if (GNUNET_YES == request_peers)
936 { 820 {
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n"); 821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
938 job = GNUNET_SCHEDULER_add_now (&get_peers, NULL); 822 "Show all peers\n");
823 job = GNUNET_SCHEDULER_add_now (&get_peers,
824 NULL);
939 } 825 }
940 else if (GNUNET_YES == request_tunnels) 826 else if (GNUNET_YES == request_tunnels)
941 { 827 {
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n"); 828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 job = GNUNET_SCHEDULER_add_now (&get_tunnels, NULL); 829 "Show all tunnels\n");
830 job = GNUNET_SCHEDULER_add_now (&get_tunnels,
831 NULL);
944 } 832 }
945 else if (NULL == listen_port) 833
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "Connecting to CADET service\n");
836 mh = GNUNET_CADET_connecT (cfg);
837 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
838 NULL);
839 if (NULL == mh)
946 { 840 {
947 FPRINTF (stderr, "No action requested\n"); 841 GNUNET_SCHEDULER_shutdown ();
948 return; 842 return;
949 } 843 }
950
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET service\n");
952 mh = GNUNET_CADET_connect (cfg,
953 NULL, /* cls */
954 &channel_ended, /* cleaner */
955 handlers);
956 if (NULL == mh)
957 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
958 else
959 sd = GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
960
961 if (NULL != listen_port) 844 if (NULL != listen_port)
962 { 845 {
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening CADET listen port\n"); 846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 GNUNET_CRYPTO_hash (listen_port, strlen(listen_port), &porthash); 847 "Opening CADET listen port\n");
965 lp = GNUNET_CADET_open_port (mh, &porthash, &channel_incoming, NULL); 848 GNUNET_CRYPTO_hash (listen_port,
849 strlen (listen_port),
850 &porthash);
851 lp = GNUNET_CADET_open_porT (mh,
852 &porthash,
853 &channel_incoming,
854 NULL,
855 NULL /* window changes */,
856 &channel_ended,
857 handlers);
858 }
859 if (NULL != target_id)
860 {
861 struct GNUNET_PeerIdentity pid;
862 enum GNUNET_CADET_ChannelOption opt;
863
864 if (GNUNET_OK !=
865 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
866 strlen (target_id),
867 &pid.public_key))
868 {
869 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
870 _("Invalid target `%s'\n"),
871 target_id);
872 GNUNET_SCHEDULER_shutdown ();
873 return;
874 }
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Connecting to `%s:%s'\n",
877 target_id,
878 target_port);
879 opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
880 GNUNET_CRYPTO_hash (target_port,
881 strlen(target_port),
882 &porthash);
883 ch = GNUNET_CADET_channel_creatE (mh,
884 NULL,
885 &pid,
886 &porthash,
887 opt,
888 NULL /* window changes */,
889 &channel_ended,
890 handlers);
891 if (GNUNET_YES == echo)
892 {
893 echo_task = GNUNET_SCHEDULER_add_now (&send_echo,
894 NULL);
895 }
896 else
897 {
898 listen_stdio ();
899 }
900 }
901
902 if ( (NULL == lp) &&
903 (NULL == job) &&
904 (NULL == ch) )
905 {
906 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
907 _("No action requested\n"));
908 GNUNET_SCHEDULER_shutdown ();
909 return;
966 } 910 }
967} 911}
968 912
@@ -975,14 +919,12 @@ run (void *cls,
975 * @return 0 ok, 1 on error 919 * @return 0 ok, 1 on error
976 */ 920 */
977int 921int
978main (int argc, char *const *argv) 922main (int argc,
923 char *const *argv)
979{ 924{
980 int res; 925 int res;
981 const char helpstr[] = "Create channels and retreive info about cadets status."; 926 const char helpstr[] = "Create channels and retreive info about cadets status.";
982 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 927 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
983// {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
984// gettext_noop ("provide information about a particular channel"),
985// GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
986 {'C', "connection", "CONNECTION_ID", 928 {'C', "connection", "CONNECTION_ID",
987 gettext_noop ("provide information about a particular connection"), 929 gettext_noop ("provide information about a particular connection"),
988 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id}, 930 GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
@@ -992,10 +934,7 @@ main (int argc, char *const *argv)
992 {'d', "dump", NULL, 934 {'d', "dump", NULL,
993 gettext_noop ("dump debug information to STDERR"), 935 gettext_noop ("dump debug information to STDERR"),
994 GNUNET_NO, &GNUNET_GETOPT_set_one, &dump}, 936 GNUNET_NO, &GNUNET_GETOPT_set_one, &dump},
995// {'m', "monitor", NULL, 937 {'o', "open-port", "PORT",
996// gettext_noop ("provide information about all events (continuously)"),
997// GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_mode},
998 {'o', "open-port", NULL,
999 gettext_noop ("port to listen to"), 938 gettext_noop ("port to listen to"),
1000 GNUNET_YES, &GNUNET_GETOPT_set_string, &listen_port}, 939 GNUNET_YES, &GNUNET_GETOPT_set_string, &listen_port},
1001 {'p', "peer", "PEER_ID", 940 {'p', "peer", "PEER_ID",
@@ -1014,12 +953,13 @@ main (int argc, char *const *argv)
1014 GNUNET_GETOPT_OPTION_END 953 GNUNET_GETOPT_OPTION_END
1015 }; 954 };
1016 955
1017 monitor_mode = GNUNET_NO; 956 if (GNUNET_OK !=
1018 957 GNUNET_STRINGS_get_utf8_args (argc, argv,
1019 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 958 &argc, &argv))
1020 return 2; 959 return 2;
1021 960
1022 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-cadet (OPTIONS | TARGET PORT)", 961 res = GNUNET_PROGRAM_run (argc, argv,
962 "gnunet-cadet (OPTIONS | TARGET PORT)",
1023 gettext_noop (helpstr), 963 gettext_noop (helpstr),
1024 options, &run, NULL); 964 options, &run, NULL);
1025 965
@@ -1027,8 +967,7 @@ main (int argc, char *const *argv)
1027 967
1028 if (GNUNET_OK == res) 968 if (GNUNET_OK == res)
1029 return 0; 969 return 0;
1030 else 970 return 1;
1031 return 1;
1032} 971}
1033 972
1034/* end of gnunet-cadet.c */ 973/* end of gnunet-cadet.c */