aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/cadet_api.c
diff options
context:
space:
mode:
authorSchanzenbach, Martin <mschanzenbach@posteo.de>2017-01-25 13:51:39 +0100
committerSchanzenbach, Martin <mschanzenbach@posteo.de>2017-01-25 13:51:39 +0100
commit59d393a1124cfd1aaffdf994bf6f8a9baaac8361 (patch)
tree1c0d9204813ca26d6db0f35c9287f81c2a2efe74 /src/cadet/cadet_api.c
parent3cb90c74c5f591fd2541d154a8e7b05a1c2f4539 (diff)
parentf27982470b7d56cd83f5abfc1befa69bc20d952b (diff)
downloadgnunet-59d393a1124cfd1aaffdf994bf6f8a9baaac8361.tar.gz
gnunet-59d393a1124cfd1aaffdf994bf6f8a9baaac8361.zip
Merge remote-tracking branch 'origin/master' into credentials
Diffstat (limited to 'src/cadet/cadet_api.c')
-rw-r--r--src/cadet/cadet_api.c407
1 files changed, 204 insertions, 203 deletions
diff --git a/src/cadet/cadet_api.c b/src/cadet/cadet_api.c
index 8f1274d63..72b7b692d 100644
--- a/src/cadet/cadet_api.c
+++ b/src/cadet/cadet_api.c
@@ -169,7 +169,7 @@ struct GNUNET_CADET_Handle
169 /** 169 /**
170 * child of the next channel to create (to avoid reusing IDs often) 170 * child of the next channel to create (to avoid reusing IDs often)
171 */ 171 */
172 struct GNUNET_CADET_ClientChannelNumber next_chid; 172 struct GNUNET_CADET_ClientChannelNumber next_ccn;
173 173
174 /** 174 /**
175 * Configuration given by the client, in case of reconnection 175 * Configuration given by the client, in case of reconnection
@@ -238,7 +238,7 @@ struct GNUNET_CADET_Channel
238 /** 238 /**
239 * Local ID of the channel 239 * Local ID of the channel
240 */ 240 */
241 struct GNUNET_CADET_ClientChannelNumber chid; 241 struct GNUNET_CADET_ClientChannelNumber ccn;
242 242
243 /** 243 /**
244 * Channel's port, if any. 244 * Channel's port, if any.
@@ -256,11 +256,6 @@ struct GNUNET_CADET_Channel
256 void *ctx; 256 void *ctx;
257 257
258 /** 258 /**
259 * Size of packet queued in this channel
260 */
261 unsigned int packet_size;
262
263 /**
264 * Channel options: reliability, etc. 259 * Channel options: reliability, etc.
265 */ 260 */
266 enum GNUNET_CADET_ChannelOption options; 261 enum GNUNET_CADET_ChannelOption options;
@@ -268,7 +263,7 @@ struct GNUNET_CADET_Channel
268 /** 263 /**
269 * Are we allowed to send to the service? 264 * Are we allowed to send to the service?
270 */ 265 */
271 int allow_send; 266 unsigned int allow_send;
272 267
273}; 268};
274 269
@@ -361,17 +356,17 @@ find_port (const struct GNUNET_CADET_Handle *h,
361 * Get the channel handler for the channel specified by id from the given handle 356 * Get the channel handler for the channel specified by id from the given handle
362 * 357 *
363 * @param h Cadet handle 358 * @param h Cadet handle
364 * @param chid ID of the wanted channel 359 * @param ccn ID of the wanted channel
365 * @return handle to the required channel or NULL if not found 360 * @return handle to the required channel or NULL if not found
366 */ 361 */
367static struct GNUNET_CADET_Channel * 362static struct GNUNET_CADET_Channel *
368retrieve_channel (struct GNUNET_CADET_Handle *h, 363retrieve_channel (struct GNUNET_CADET_Handle *h,
369 struct GNUNET_CADET_ClientChannelNumber chid) 364 struct GNUNET_CADET_ClientChannelNumber ccn)
370{ 365{
371 struct GNUNET_CADET_Channel *ch; 366 struct GNUNET_CADET_Channel *ch;
372 367
373 for (ch = h->channels_head; NULL != ch; ch = ch->next) 368 for (ch = h->channels_head; NULL != ch; ch = ch->next)
374 if (ch->chid.channel_of_client == chid.channel_of_client) 369 if (ch->ccn.channel_of_client == ccn.channel_of_client)
375 return ch; 370 return ch;
376 return NULL; 371 return NULL;
377} 372}
@@ -381,13 +376,13 @@ retrieve_channel (struct GNUNET_CADET_Handle *h,
381 * Create a new channel and insert it in the channel list of the cadet handle 376 * Create a new channel and insert it in the channel list of the cadet handle
382 * 377 *
383 * @param h Cadet handle 378 * @param h Cadet handle
384 * @param chid Desired chid of the channel, 0 to assign one automatically. 379 * @param ccn Desired ccn of the channel, 0 to assign one automatically.
385 * 380 *
386 * @return Handle to the created channel. 381 * @return Handle to the created channel.
387 */ 382 */
388static struct GNUNET_CADET_Channel * 383static struct GNUNET_CADET_Channel *
389create_channel (struct GNUNET_CADET_Handle *h, 384create_channel (struct GNUNET_CADET_Handle *h,
390 struct GNUNET_CADET_ClientChannelNumber chid) 385 struct GNUNET_CADET_ClientChannelNumber ccn)
391{ 386{
392 struct GNUNET_CADET_Channel *ch; 387 struct GNUNET_CADET_Channel *ch;
393 388
@@ -396,24 +391,23 @@ create_channel (struct GNUNET_CADET_Handle *h,
396 h->channels_tail, 391 h->channels_tail,
397 ch); 392 ch);
398 ch->cadet = h; 393 ch->cadet = h;
399 if (0 == chid.channel_of_client) 394 if (0 == ccn.channel_of_client)
400 { 395 {
401 ch->chid = h->next_chid; 396 ch->ccn = h->next_ccn;
402 while (NULL != retrieve_channel (h, 397 while (NULL != retrieve_channel (h,
403 h->next_chid)) 398 h->next_ccn))
404 { 399 {
405 h->next_chid.channel_of_client 400 h->next_ccn.channel_of_client
406 = htonl (1 + ntohl (h->next_chid.channel_of_client)); 401 = htonl (1 + ntohl (h->next_ccn.channel_of_client));
407 if (0 == ntohl (h->next_chid.channel_of_client)) 402 if (0 == ntohl (h->next_ccn.channel_of_client))
408 h->next_chid.channel_of_client 403 h->next_ccn.channel_of_client
409 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI); 404 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
410 } 405 }
411 } 406 }
412 else 407 else
413 { 408 {
414 ch->chid = chid; 409 ch->ccn = ccn;
415 } 410 }
416 ch->allow_send = GNUNET_NO;
417 return ch; 411 return ch;
418} 412}
419 413
@@ -432,29 +426,35 @@ create_channel (struct GNUNET_CADET_Handle *h,
432 */ 426 */
433// FIXME: simplify: call_cleaner is always #GNUNET_YES!!! 427// FIXME: simplify: call_cleaner is always #GNUNET_YES!!!
434static void 428static void
435destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner) 429destroy_channel (struct GNUNET_CADET_Channel *ch,
430 int call_cleaner)
436{ 431{
437 struct GNUNET_CADET_Handle *h; 432 struct GNUNET_CADET_Handle *h;
438 struct GNUNET_CADET_TransmitHandle *th; 433 struct GNUNET_CADET_TransmitHandle *th;
439 struct GNUNET_CADET_TransmitHandle *next; 434 struct GNUNET_CADET_TransmitHandle *next;
440 435
441 LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
442
443 if (NULL == ch) 436 if (NULL == ch)
444 { 437 {
445 GNUNET_break (0); 438 GNUNET_break (0);
446 return; 439 return;
447 } 440 }
448 h = ch->cadet; 441 h = ch->cadet;
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 " destroy_channel %X of %p\n",
444 ch->ccn,
445 h);
449 446
450 GNUNET_CONTAINER_DLL_remove (h->channels_head, 447 GNUNET_CONTAINER_DLL_remove (h->channels_head,
451 h->channels_tail, 448 h->channels_tail,
452 ch); 449 ch);
453 450
454 /* signal channel destruction */ 451 /* signal channel destruction */
455 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) ) 452 if ( (NULL != h->cleaner) &&
453 (0 != ch->peer) &&
454 (GNUNET_YES == call_cleaner) )
456 { 455 {
457 LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n"); 456 LOG (GNUNET_ERROR_TYPE_DEBUG,
457 " calling cleaner\n");
458 h->cleaner (h->cls, ch, ch->ctx); 458 h->cleaner (h->cls, ch, ch->ctx);
459 } 459 }
460 460
@@ -475,7 +475,6 @@ destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
475 if (0 != ch->peer) 475 if (0 != ch->peer)
476 GNUNET_PEER_change_rc (ch->peer, -1); 476 GNUNET_PEER_change_rc (ch->peer, -1);
477 GNUNET_free (ch); 477 GNUNET_free (ch);
478
479} 478}
480 479
481 480
@@ -490,7 +489,9 @@ static void
490add_to_queue (struct GNUNET_CADET_Handle *h, 489add_to_queue (struct GNUNET_CADET_Handle *h,
491 struct GNUNET_CADET_TransmitHandle *th) 490 struct GNUNET_CADET_TransmitHandle *th)
492{ 491{
493 GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th); 492 GNUNET_CONTAINER_DLL_insert_tail (h->th_head,
493 h->th_tail,
494 th);
494} 495}
495 496
496 497
@@ -514,29 +515,6 @@ remove_from_queue (struct GNUNET_CADET_TransmitHandle *th)
514} 515}
515 516
516 517
517/**
518 * Send an ack on the channel to confirm the processing of a message.
519 *
520 * @param ch Channel on which to send the ACK.
521 */
522static void
523send_ack (struct GNUNET_CADET_Channel *ch)
524{
525 struct GNUNET_CADET_LocalAck *msg;
526 struct GNUNET_MQ_Envelope *env;
527
528 env = GNUNET_MQ_msg (msg,
529 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
530
531 LOG (GNUNET_ERROR_TYPE_DEBUG,
532 "Sending ACK on channel %X\n",
533 ch->chid.channel_of_client);
534 msg->channel_id = ch->chid;
535 GNUNET_MQ_send (ch->cadet->mq,
536 env);
537}
538
539
540 518
541/******************************************************************************/ 519/******************************************************************************/
542/*********************** RECEIVE HANDLERS ****************************/ 520/*********************** RECEIVE HANDLERS ****************************/
@@ -558,22 +536,27 @@ request_data (void *cls)
558 struct GNUNET_MQ_Envelope *env; 536 struct GNUNET_MQ_Envelope *env;
559 size_t osize; 537 size_t osize;
560 538
561 LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting Data: %u bytes\n", th->size); 539 LOG (GNUNET_ERROR_TYPE_DEBUG,
562 540 "Requesting Data: %u bytes (allow send is %u)\n",
563 GNUNET_assert (GNUNET_YES == th->channel->allow_send); 541 th->size,
564 th->channel->allow_send = GNUNET_NO; 542 th->channel->allow_send);
543
544 GNUNET_assert (0 < th->channel->allow_send);
545 th->channel->allow_send--;
546 /* NOTE: we may be allowed to send another packet immediately,
547 albeit the current logic waits for the ACK. */
565 th->request_data_task = NULL; 548 th->request_data_task = NULL;
566 th->channel->packet_size = 0;
567 remove_from_queue (th); 549 remove_from_queue (th);
568 550
569 env = GNUNET_MQ_msg_extra (msg, 551 env = GNUNET_MQ_msg_extra (msg,
570 th->size, 552 th->size,
571 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA); 553 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
572 msg->id = th->channel->chid; 554 msg->ccn = th->channel->ccn;
573 osize = th->notify (th->notify_cls, 555 osize = th->notify (th->notify_cls,
574 th->size, 556 th->size,
575 &msg[1]); 557 &msg[1]);
576 GNUNET_assert (osize == th->size); 558 GNUNET_assert (osize == th->size);
559
577 GNUNET_MQ_send (th->channel->cadet->mq, 560 GNUNET_MQ_send (th->channel->cadet->mq,
578 env); 561 env);
579 GNUNET_free (th); 562 GNUNET_free (th);
@@ -588,55 +571,59 @@ request_data (void *cls)
588 */ 571 */
589static void 572static void
590handle_channel_created (void *cls, 573handle_channel_created (void *cls,
591 const struct GNUNET_CADET_ChannelOpenMessageMessage *msg) 574 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
592{ 575{
593 struct GNUNET_CADET_Handle *h = cls; 576 struct GNUNET_CADET_Handle *h = cls;
594 struct GNUNET_CADET_Channel *ch; 577 struct GNUNET_CADET_Channel *ch;
595 struct GNUNET_CADET_Port *port; 578 struct GNUNET_CADET_Port *port;
596 const struct GNUNET_HashCode *port_number; 579 const struct GNUNET_HashCode *port_number;
597 struct GNUNET_CADET_ClientChannelNumber chid; 580 struct GNUNET_CADET_ClientChannelNumber ccn;
598 581
599 chid = msg->channel_id; 582 ccn = msg->ccn;
600 port_number = &msg->port; 583 port_number = &msg->port;
601 LOG (GNUNET_ERROR_TYPE_DEBUG, 584 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
602 "Creating incoming channel %X [%s]\n",
603 ntohl (chid.channel_of_client),
604 GNUNET_h2s (port_number));
605 if (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
606 { 585 {
607 GNUNET_break (0); 586 GNUNET_break (0);
608 return; 587 return;
609 } 588 }
610 port = find_port (h, port_number); 589 port = find_port (h, port_number);
611 if (NULL != port) 590 if (NULL == port)
612 {
613 void *ctx;
614
615 ch = create_channel (h, chid);
616 ch->allow_send = GNUNET_NO;
617 ch->peer = GNUNET_PEER_intern (&msg->peer);
618 ch->cadet = h;
619 ch->chid = chid;
620 ch->port = port;
621 ch->options = ntohl (msg->opt);
622
623 LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
624 ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options);
625 if (NULL != ctx)
626 ch->ctx = ctx;
627 LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
628 }
629 else
630 { 591 {
631 struct GNUNET_CADET_ChannelDestroyMessage *d_msg; 592 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
632 struct GNUNET_MQ_Envelope *env; 593 struct GNUNET_MQ_Envelope *env;
633 594
634 LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n"); 595 GNUNET_break (0);
635 env = GNUNET_MQ_msg (d_msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); 596 LOG (GNUNET_ERROR_TYPE_DEBUG,
636 d_msg->channel_id = msg->channel_id; 597 "No handler for incoming channel %X [%s]\n",
637 GNUNET_MQ_send (h->mq, env); 598 ntohl (ccn.channel_of_client),
599 GNUNET_h2s (port_number));
600 /* FIXME: should disconnect instead, this is a serious error! */
601 env = GNUNET_MQ_msg (d_msg,
602 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
603 d_msg->ccn = msg->ccn;
604 GNUNET_MQ_send (h->mq,
605 env);
606 return;
638 } 607 }
639 return; 608
609 ch = create_channel (h,
610 ccn);
611 ch->peer = GNUNET_PEER_intern (&msg->peer);
612 ch->cadet = h;
613 ch->ccn = ccn;
614 ch->port = port;
615 ch->options = ntohl (msg->opt);
616
617 LOG (GNUNET_ERROR_TYPE_DEBUG,
618 "Creating incoming channel %X [%s] %p\n",
619 ntohl (ccn.channel_of_client),
620 GNUNET_h2s (port_number),
621 ch);
622 ch->ctx = port->handler (port->cls,
623 ch,
624 &msg->peer,
625 port->hash,
626 ch->options);
640} 627}
641 628
642 629
@@ -648,24 +635,24 @@ handle_channel_created (void *cls,
648 */ 635 */
649static void 636static void
650handle_channel_destroy (void *cls, 637handle_channel_destroy (void *cls,
651 const struct GNUNET_CADET_ChannelDestroyMessage *msg) 638 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
652{ 639{
653 struct GNUNET_CADET_Handle *h = cls; 640 struct GNUNET_CADET_Handle *h = cls;
654 struct GNUNET_CADET_Channel *ch; 641 struct GNUNET_CADET_Channel *ch;
655 struct GNUNET_CADET_ClientChannelNumber chid; 642 struct GNUNET_CADET_ClientChannelNumber ccn;
656 643
657 chid = msg->channel_id; 644 ccn = msg->ccn;
658 LOG (GNUNET_ERROR_TYPE_DEBUG, 645 LOG (GNUNET_ERROR_TYPE_DEBUG,
659 "Channel %X Destroy from service\n", 646 "Channel %X Destroy from service\n",
660 ntohl (chid.channel_of_client)); 647 ntohl (ccn.channel_of_client));
661 ch = retrieve_channel (h, 648 ch = retrieve_channel (h,
662 chid); 649 ccn);
663 650
664 if (NULL == ch) 651 if (NULL == ch)
665 { 652 {
666 LOG (GNUNET_ERROR_TYPE_DEBUG, 653 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "channel %X unknown\n", 654 "channel %X unknown\n",
668 ntohl (chid.channel_of_client)); 655 ntohl (ccn.channel_of_client));
669 return; 656 return;
670 } 657 }
671 destroy_channel (ch, 658 destroy_channel (ch,
@@ -697,7 +684,7 @@ check_local_data (void *cls,
697 } 684 }
698 685
699 ch = retrieve_channel (h, 686 ch = retrieve_channel (h,
700 message->id); 687 message->ccn);
701 if (NULL == ch) 688 if (NULL == ch)
702 { 689 {
703 GNUNET_break_op (0); 690 GNUNET_break_op (0);
@@ -722,45 +709,44 @@ handle_local_data (void *cls,
722 const struct GNUNET_MessageHeader *payload; 709 const struct GNUNET_MessageHeader *payload;
723 const struct GNUNET_CADET_MessageHandler *handler; 710 const struct GNUNET_CADET_MessageHandler *handler;
724 struct GNUNET_CADET_Channel *ch; 711 struct GNUNET_CADET_Channel *ch;
725 unsigned int i;
726 uint16_t type; 712 uint16_t type;
727 713
728 LOG (GNUNET_ERROR_TYPE_DEBUG, 714 ch = retrieve_channel (h,
729 "Got a data message!\n"); 715 message->ccn);
730 ch = retrieve_channel (h, message->id);
731 GNUNET_assert (NULL != ch); 716 GNUNET_assert (NULL != ch);
732 717
733 payload = (struct GNUNET_MessageHeader *) &message[1]; 718 payload = (struct GNUNET_MessageHeader *) &message[1];
734 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n", 719 type = ntohs (payload->type);
735 GC_f2s (ntohl (ch->chid.channel_of_client) >= 720 LOG (GNUNET_ERROR_TYPE_DEBUG,
721 "Got a %s data on channel %s [%X] of type %s (%u)\n",
722 GC_f2s (ntohl (ch->ccn.channel_of_client) >=
736 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI), 723 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
737 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), 724 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
738 ntohl (message->id.channel_of_client)); 725 ntohl (message->ccn.channel_of_client),
739 726 GC_m2s (type),
740 type = ntohs (payload->type); 727 type);
741 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type)); 728 for (unsigned i=0;i<h->n_handlers;i++)
742 for (i = 0; i < h->n_handlers; i++)
743 { 729 {
744 handler = &h->message_handlers[i]; 730 handler = &h->message_handlers[i];
745 LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
746 handler->type);
747 if (handler->type == type) 731 if (handler->type == type)
748 { 732 {
749 if (GNUNET_OK != 733 if (GNUNET_OK !=
750 handler->callback (h->cls, ch, &ch->ctx, payload)) 734 handler->callback (h->cls,
751 { 735 ch,
752 LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n"); 736 &ch->ctx,
753 GNUNET_CADET_channel_destroy (ch); 737 payload))
754 break;
755 }
756 else
757 { 738 {
758 LOG (GNUNET_ERROR_TYPE_DEBUG, 739 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "callback completed successfully\n"); 740 "callback caused disconnection\n");
760 break; 741 GNUNET_CADET_channel_destroy (ch);
742 return;
761 } 743 }
744 return;
762 } 745 }
763 } 746 }
747 /* Other peer sent message we do not comprehend. */
748 GNUNET_break_op (0);
749 GNUNET_CADET_receive_done (ch);
764} 750}
765 751
766 752
@@ -777,41 +763,33 @@ handle_local_ack (void *cls,
777{ 763{
778 struct GNUNET_CADET_Handle *h = cls; 764 struct GNUNET_CADET_Handle *h = cls;
779 struct GNUNET_CADET_Channel *ch; 765 struct GNUNET_CADET_Channel *ch;
780 struct GNUNET_CADET_ClientChannelNumber chid; 766 struct GNUNET_CADET_ClientChannelNumber ccn;
767 struct GNUNET_CADET_TransmitHandle *th;
781 768
782 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n"); 769 ccn = message->ccn;
783 chid = message->channel_id; 770 ch = retrieve_channel (h, ccn);
784 ch = retrieve_channel (h, chid);
785 if (NULL == ch) 771 if (NULL == ch)
786 { 772 {
787 LOG (GNUNET_ERROR_TYPE_DEBUG, 773 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 "ACK on unknown channel %X\n", 774 "ACK on unknown channel %X\n",
789 ntohl (chid.channel_of_client)); 775 ntohl (ccn.channel_of_client));
790 return; 776 return;
791 } 777 }
778 ch->allow_send++;
792 LOG (GNUNET_ERROR_TYPE_DEBUG, 779 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 " on channel %X!\n", 780 "Got an ACK on channel %X, allow send now %u!\n",
794 ntohl (ch->chid.channel_of_client)); 781 ntohl (ch->ccn.channel_of_client),
795 ch->allow_send = GNUNET_YES; 782 ch->allow_send);
796 if (0 < ch->packet_size) 783 for (th = h->th_head; NULL != th; th = th->next)
797 { 784 {
798 struct GNUNET_CADET_TransmitHandle *th; 785 if ( (th->channel == ch) &&
799 struct GNUNET_CADET_TransmitHandle *next; 786 (NULL == th->request_data_task) )
800 LOG (GNUNET_ERROR_TYPE_DEBUG,
801 " pending data, sending %u bytes!\n",
802 ch->packet_size);
803 for (th = h->th_head; NULL != th; th = next)
804 { 787 {
805 next = th->next; 788 th->request_data_task
806 if (th->channel == ch) 789 = GNUNET_SCHEDULER_add_now (&request_data,
807 { 790 th);
808 GNUNET_assert (NULL == th->request_data_task); 791 break;
809 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th);
810 break;
811 }
812 } 792 }
813 /* Complain if we got thru all th without sending anything, ch was wrong */
814 GNUNET_break (NULL != th);
815 } 793 }
816} 794}
817 795
@@ -1273,20 +1251,19 @@ handle_get_tunnel (void *cls,
1273 * original state. 1251 * original state.
1274 * 1252 *
1275 * @param h handle to the cadet 1253 * @param h handle to the cadet
1276 * 1254 * @return #GNUNET_YES in case of success, #GNUNET_NO otherwise (service down...)
1277 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
1278 */ 1255 */
1279static int 1256static int
1280do_reconnect (struct GNUNET_CADET_Handle *h) 1257do_reconnect (struct GNUNET_CADET_Handle *h)
1281{ 1258{
1282 struct GNUNET_MQ_MessageHandler handlers[] = { 1259 struct GNUNET_MQ_MessageHandler handlers[] = {
1283 GNUNET_MQ_hd_fixed_size (channel_created, 1260 GNUNET_MQ_hd_fixed_size (channel_created,
1284 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN, 1261 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1285 struct GNUNET_CADET_ChannelOpenMessageMessage, 1262 struct GNUNET_CADET_LocalChannelCreateMessage,
1286 h), 1263 h),
1287 GNUNET_MQ_hd_fixed_size (channel_destroy, 1264 GNUNET_MQ_hd_fixed_size (channel_destroy,
1288 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY, 1265 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1289 struct GNUNET_CADET_ChannelDestroyMessage, 1266 struct GNUNET_CADET_LocalChannelDestroyMessage,
1290 h), 1267 h),
1291 GNUNET_MQ_hd_var_size (local_data, 1268 GNUNET_MQ_hd_var_size (local_data,
1292 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 1269 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
@@ -1370,7 +1347,7 @@ reconnect (struct GNUNET_CADET_Handle *h)
1370 1347
1371 LOG (GNUNET_ERROR_TYPE_DEBUG, 1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "Requested RECONNECT, destroying all channels\n"); 1349 "Requested RECONNECT, destroying all channels\n");
1373 for (ch = h->channels_head; NULL != ch; ch = h->channels_head) 1350 while (NULL != (ch = h->channels_head))
1374 destroy_channel (ch, GNUNET_YES); 1351 destroy_channel (ch, GNUNET_YES);
1375 if (NULL == h->reconnect_task) 1352 if (NULL == h->reconnect_task)
1376 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time, 1353 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
@@ -1390,9 +1367,10 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1390{ 1367{
1391 struct GNUNET_CADET_Handle *h; 1368 struct GNUNET_CADET_Handle *h;
1392 1369
1393 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
1394 h = GNUNET_new (struct GNUNET_CADET_Handle); 1370 h = GNUNET_new (struct GNUNET_CADET_Handle);
1395 LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h); 1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "GNUNET_CADET_connect() %p\n",
1373 h);
1396 h->cfg = cfg; 1374 h->cfg = cfg;
1397 h->cleaner = cleaner; 1375 h->cleaner = cleaner;
1398 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES); 1376 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES);
@@ -1405,7 +1383,7 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1405 } 1383 }
1406 h->cls = cls; 1384 h->cls = cls;
1407 h->message_handlers = handlers; 1385 h->message_handlers = handlers;
1408 h->next_chid.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI); 1386 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1409 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS; 1387 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1410 h->reconnect_task = NULL; 1388 h->reconnect_task = NULL;
1411 1389
@@ -1413,11 +1391,18 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1413 for (h->n_handlers = 0; 1391 for (h->n_handlers = 0;
1414 handlers && handlers[h->n_handlers].type; 1392 handlers && handlers[h->n_handlers].type;
1415 h->n_handlers++) ; 1393 h->n_handlers++) ;
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
1417 return h; 1394 return h;
1418} 1395}
1419 1396
1420 1397
1398/**
1399 * Disconnect from the cadet service. All channels will be destroyed. All channel
1400 * disconnect callbacks will be called on any still connected peers, notifying
1401 * about their disconnection. The registered inbound channel cleaner will be
1402 * called should any inbound channels still exist.
1403 *
1404 * @param handle connection to cadet to disconnect
1405 */
1421void 1406void
1422GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle) 1407GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1423{ 1408{
@@ -1431,12 +1416,12 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1431 while (NULL != ch) 1416 while (NULL != ch)
1432 { 1417 {
1433 aux = ch->next; 1418 aux = ch->next;
1434 if (ntohl (ch->chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) 1419 if (ntohl (ch->ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1435 { 1420 {
1436 GNUNET_break (0); 1421 GNUNET_break (0);
1437 LOG (GNUNET_ERROR_TYPE_DEBUG, 1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "channel %X not destroyed\n", 1423 "channel %X not destroyed\n",
1439 ntohl (ch->chid.channel_of_client)); 1424 ntohl (ch->ccn.channel_of_client));
1440 } 1425 }
1441 destroy_channel (ch, 1426 destroy_channel (ch,
1442 GNUNET_YES); 1427 GNUNET_YES);
@@ -1497,7 +1482,6 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1497 * @param port Hash representing the port number. 1482 * @param port Hash representing the port number.
1498 * @param new_channel Function called when an channel is received. 1483 * @param new_channel Function called when an channel is received.
1499 * @param new_channel_cls Closure for @a new_channel. 1484 * @param new_channel_cls Closure for @a new_channel.
1500 *
1501 * @return Port handle. 1485 * @return Port handle.
1502 */ 1486 */
1503struct GNUNET_CADET_Port * 1487struct GNUNET_CADET_Port *
@@ -1565,7 +1549,6 @@ GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
1565 * @param peer peer identity the channel should go to 1549 * @param peer peer identity the channel should go to
1566 * @param port Port hash (port number). 1550 * @param port Port hash (port number).
1567 * @param options CadetOption flag field, with all desired option bits set to 1. 1551 * @param options CadetOption flag field, with all desired option bits set to 1.
1568 *
1569 * @return handle to the channel 1552 * @return handle to the channel
1570 */ 1553 */
1571struct GNUNET_CADET_Channel * 1554struct GNUNET_CADET_Channel *
@@ -1575,28 +1558,28 @@ GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1575 const struct GNUNET_HashCode *port, 1558 const struct GNUNET_HashCode *port,
1576 enum GNUNET_CADET_ChannelOption options) 1559 enum GNUNET_CADET_ChannelOption options)
1577{ 1560{
1578 struct GNUNET_CADET_ChannelOpenMessageMessage *msg; 1561 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1579 struct GNUNET_MQ_Envelope *env; 1562 struct GNUNET_MQ_Envelope *env;
1580 struct GNUNET_CADET_Channel *ch; 1563 struct GNUNET_CADET_Channel *ch;
1581 struct GNUNET_CADET_ClientChannelNumber chid; 1564 struct GNUNET_CADET_ClientChannelNumber ccn;
1582 1565
1583 LOG (GNUNET_ERROR_TYPE_DEBUG, 1566 ccn.channel_of_client = htonl (0);
1584 "Creating new channel to %s:%u\n", 1567 ch = create_channel (h, ccn);
1585 GNUNET_i2s (peer), port);
1586 chid.channel_of_client = htonl (0);
1587 ch = create_channel (h, chid);
1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n",
1590 ntohl (ch->chid.channel_of_client));
1591 ch->ctx = channel_ctx; 1568 ch->ctx = channel_ctx;
1592 ch->peer = GNUNET_PEER_intern (peer); 1569 ch->peer = GNUNET_PEER_intern (peer);
1593 1570
1594 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN); 1571 LOG (GNUNET_ERROR_TYPE_DEBUG,
1595 msg->channel_id = ch->chid; 1572 "Creating new channel to %s:%u at %p number %X\n",
1573 GNUNET_i2s (peer),
1574 port,
1575 ch,
1576 ntohl (ch->ccn.channel_of_client));
1577 env = GNUNET_MQ_msg (msg,
1578 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1579 msg->ccn = ch->ccn;
1596 msg->port = *port; 1580 msg->port = *port;
1597 msg->peer = *peer; 1581 msg->peer = *peer;
1598 msg->opt = htonl (options); 1582 msg->opt = htonl (options);
1599 ch->allow_send = GNUNET_NO;
1600 GNUNET_MQ_send (h->mq, 1583 GNUNET_MQ_send (h->mq,
1601 env); 1584 env);
1602 return ch; 1585 return ch;
@@ -1607,12 +1590,13 @@ void
1607GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel) 1590GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1608{ 1591{
1609 struct GNUNET_CADET_Handle *h; 1592 struct GNUNET_CADET_Handle *h;
1610 struct GNUNET_CADET_ChannelDestroyMessage *msg; 1593 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
1611 struct GNUNET_MQ_Envelope *env; 1594 struct GNUNET_MQ_Envelope *env;
1612 struct GNUNET_CADET_TransmitHandle *th; 1595 struct GNUNET_CADET_TransmitHandle *th;
1613 struct GNUNET_CADET_TransmitHandle *next; 1596 struct GNUNET_CADET_TransmitHandle *next;
1614 1597
1615 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n"); 1598 LOG (GNUNET_ERROR_TYPE_DEBUG,
1599 "Destroying channel\n");
1616 h = channel->cadet; 1600 h = channel->cadet;
1617 for (th = h->th_head; th != NULL; th = next) 1601 for (th = h->th_head; th != NULL; th = next)
1618 { 1602 {
@@ -1629,18 +1613,24 @@ GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1629 } 1613 }
1630 else 1614 else
1631 { 1615 {
1632 LOG (GNUNET_ERROR_TYPE_WARNING, "no meta-traffic should be queued\n"); 1616 LOG (GNUNET_ERROR_TYPE_WARNING,
1617 "no meta-traffic should be queued\n");
1633 } 1618 }
1634 GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th); 1619 GNUNET_CONTAINER_DLL_remove (h->th_head,
1620 h->th_tail,
1621 th);
1635 GNUNET_CADET_notify_transmit_ready_cancel (th); 1622 GNUNET_CADET_notify_transmit_ready_cancel (th);
1636 } 1623 }
1637 } 1624 }
1638 1625
1639 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); 1626 env = GNUNET_MQ_msg (msg,
1640 msg->channel_id = channel->chid; 1627 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1641 GNUNET_MQ_send (h->mq, env); 1628 msg->ccn = channel->ccn;
1629 GNUNET_MQ_send (h->mq,
1630 env);
1642 1631
1643 destroy_channel (channel, GNUNET_YES); 1632 destroy_channel (channel,
1633 GNUNET_YES);
1644} 1634}
1645 1635
1646 1636
@@ -1694,19 +1684,17 @@ GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1694 struct GNUNET_CADET_TransmitHandle *th; 1684 struct GNUNET_CADET_TransmitHandle *th;
1695 1685
1696 GNUNET_assert (NULL != channel); 1686 GNUNET_assert (NULL != channel);
1697 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1698 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1701 if (ntohl (channel->chid.channel_of_client) >=
1702 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1703 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1704 else
1705 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
1706 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
1707 GNUNET_assert (NULL != notify); 1687 GNUNET_assert (NULL != notify);
1708 GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed 1688 GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
1709 1689 LOG (GNUNET_ERROR_TYPE_DEBUG,
1690 "CADET NOTIFY TRANSMIT READY on channel %X allow_send is %u to %s with %u bytes\n",
1691 channel->ccn,
1692 channel->allow_send,
1693 (ntohl (channel->ccn.channel_of_client) >=
1694 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1695 ? "origin"
1696 : "destination",
1697 (unsigned int) notify_size);
1710 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us) 1698 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != maxdelay.rel_value_us)
1711 { 1699 {
1712 LOG (GNUNET_ERROR_TYPE_WARNING, 1700 LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -1716,16 +1704,15 @@ GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1716 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle); 1704 th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
1717 th->channel = channel; 1705 th->channel = channel;
1718 th->size = notify_size; 1706 th->size = notify_size;
1719 channel->packet_size = th->size;
1720 LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
1721 th->notify = notify; 1707 th->notify = notify;
1722 th->notify_cls = notify_cls; 1708 th->notify_cls = notify_cls;
1723 if (GNUNET_YES == channel->allow_send) 1709 if (0 != channel->allow_send)
1724 th->request_data_task = GNUNET_SCHEDULER_add_now (&request_data, th); 1710 th->request_data_task
1711 = GNUNET_SCHEDULER_add_now (&request_data,
1712 th);
1725 else 1713 else
1726 add_to_queue (channel->cadet, th); 1714 add_to_queue (channel->cadet,
1727 1715 th);
1728 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
1729 return th; 1716 return th;
1730} 1717}
1731 1718
@@ -1736,18 +1723,32 @@ GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *t
1736 if (NULL != th->request_data_task) 1723 if (NULL != th->request_data_task)
1737 { 1724 {
1738 GNUNET_SCHEDULER_cancel (th->request_data_task); 1725 GNUNET_SCHEDULER_cancel (th->request_data_task);
1726 th->request_data_task = NULL;
1739 } 1727 }
1740 th->request_data_task = NULL;
1741
1742 remove_from_queue (th); 1728 remove_from_queue (th);
1743 GNUNET_free (th); 1729 GNUNET_free (th);
1744} 1730}
1745 1731
1746 1732
1733/**
1734 * Send an ack on the channel to confirm the processing of a message.
1735 *
1736 * @param ch Channel on which to send the ACK.
1737 */
1747void 1738void
1748GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel) 1739GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
1749{ 1740{
1750 send_ack (channel); 1741 struct GNUNET_CADET_LocalAck *msg;
1742 struct GNUNET_MQ_Envelope *env;
1743
1744 env = GNUNET_MQ_msg (msg,
1745 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1746 LOG (GNUNET_ERROR_TYPE_DEBUG,
1747 "Sending ACK on channel %X\n",
1748 channel->ccn.channel_of_client);
1749 msg->ccn = channel->ccn;
1750 GNUNET_MQ_send (channel->cadet->mq,
1751 env);
1751} 1752}
1752 1753
1753 1754
@@ -1994,7 +1995,7 @@ GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1994 1995
1995 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL); 1996 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
1996 msg->peer = *initiator; 1997 msg->peer = *initiator;
1997 msg->channel_id.channel_of_client = htonl (channel_number); 1998 msg->ccn.channel_of_client = htonl (channel_number);
1998 GNUNET_MQ_send (h->mq, env); 1999 GNUNET_MQ_send (h->mq, env);
1999 2000
2000 h->info_cb.channel_cb = callback; 2001 h->info_cb.channel_cb = callback;