aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-communicator-tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/gnunet-communicator-tcp.c')
-rw-r--r--src/transport/gnunet-communicator-tcp.c1798
1 files changed, 903 insertions, 895 deletions
diff --git a/src/transport/gnunet-communicator-tcp.c b/src/transport/gnunet-communicator-tcp.c
index a8d692940..f3ccbd262 100644
--- a/src/transport/gnunet-communicator-tcp.c
+++ b/src/transport/gnunet-communicator-tcp.c
@@ -47,7 +47,7 @@
47 * the other peer should revalidate). 47 * the other peer should revalidate).
48 */ 48 */
49#define ADDRESS_VALIDITY_PERIOD \ 49#define ADDRESS_VALIDITY_PERIOD \
50 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 4) 50 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
51 51
52/** 52/**
53 * How many messages do we keep at most in the queue to the 53 * How many messages do we keep at most in the queue to the
@@ -86,8 +86,8 @@
86 * directions. 86 * directions.
87 */ 87 */
88#define INITIAL_KX_SIZE \ 88#define INITIAL_KX_SIZE \
89 (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) + \ 89 (sizeof(struct GNUNET_CRYPTO_EcdhePublicKey) \
90 sizeof(struct TCPConfirmation)) 90 + sizeof(struct TCPConfirmation))
91 91
92 92
93/** 93/**
@@ -107,7 +107,8 @@ GNUNET_NETWORK_STRUCT_BEGIN
107 * Signature we use to verify that the ephemeral key was really chosen by 107 * Signature we use to verify that the ephemeral key was really chosen by
108 * the specified sender. 108 * the specified sender.
109 */ 109 */
110struct TcpHandshakeSignature { 110struct TcpHandshakeSignature
111{
111 /** 112 /**
112 * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE 113 * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE
113 */ 114 */
@@ -139,7 +140,8 @@ struct TcpHandshakeSignature {
139/** 140/**
140 * Encrypted continuation of TCP initial handshake. 141 * Encrypted continuation of TCP initial handshake.
141 */ 142 */
142struct TCPConfirmation { 143struct TCPConfirmation
144{
143 /** 145 /**
144 * Sender's identity 146 * Sender's identity
145 */ 147 */
@@ -161,7 +163,8 @@ struct TCPConfirmation {
161/** 163/**
162 * TCP message box. Always sent encrypted! 164 * TCP message box. Always sent encrypted!
163 */ 165 */
164struct TCPBox { 166struct TCPBox
167{
165 /** 168 /**
166 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX. Warning: the 169 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX. Warning: the
167 * header size EXCLUDES the size of the `struct TCPBox`. We usually 170 * header size EXCLUDES the size of the `struct TCPBox`. We usually
@@ -189,7 +192,8 @@ struct TCPBox {
189 * TCP rekey message box. Always sent encrypted! Data after 192 * TCP rekey message box. Always sent encrypted! Data after
190 * this message will use the new key. 193 * this message will use the new key.
191 */ 194 */
192struct TCPRekey { 195struct TCPRekey
196{
193 /** 197 /**
194 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY. 198 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY.
195 */ 199 */
@@ -228,7 +232,8 @@ struct TCPRekey {
228 * Needed/useful in case we drop RST/FIN packets on the GNUnet 232 * Needed/useful in case we drop RST/FIN packets on the GNUnet
229 * port due to the possibility of malicious RST/FIN injection. 233 * port due to the possibility of malicious RST/FIN injection.
230 */ 234 */
231struct TCPFinish { 235struct TCPFinish
236{
232 /** 237 /**
233 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH. 238 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH.
234 */ 239 */
@@ -252,7 +257,8 @@ GNUNET_NETWORK_STRUCT_END
252/** 257/**
253 * Handle for a queue. 258 * Handle for a queue.
254 */ 259 */
255struct Queue { 260struct Queue
261{
256 /** 262 /**
257 * To whom are we talking to. 263 * To whom are we talking to.
258 */ 264 */
@@ -430,7 +436,8 @@ struct Queue {
430 * Handle for an incoming connection where we do not yet have enough 436 * Handle for an incoming connection where we do not yet have enough
431 * information to setup a full queue. 437 * information to setup a full queue.
432 */ 438 */
433struct ProtoQueue { 439struct ProtoQueue
440{
434 /** 441 /**
435 * Kept in a DLL. 442 * Kept in a DLL.
436 */ 443 */
@@ -553,7 +560,7 @@ static struct ProtoQueue *proto_tail;
553 * @param cls NULL 560 * @param cls NULL
554 */ 561 */
555static void 562static void
556listen_cb(void *cls); 563listen_cb (void *cls);
557 564
558 565
559/** 566/**
@@ -564,53 +571,53 @@ listen_cb(void *cls);
564 * @param queue queue to close down 571 * @param queue queue to close down
565 */ 572 */
566static void 573static void
567queue_destroy(struct Queue *queue) 574queue_destroy (struct Queue *queue)
568{ 575{
569 struct GNUNET_MQ_Handle *mq; 576 struct GNUNET_MQ_Handle *mq;
570 577
571 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "Disconnecting queue for peer `%s'\n", 579 "Disconnecting queue for peer `%s'\n",
573 GNUNET_i2s(&queue->target)); 580 GNUNET_i2s (&queue->target));
574 if (NULL != (mq = queue->mq)) 581 if (NULL != (mq = queue->mq))
575 { 582 {
576 queue->mq = NULL; 583 queue->mq = NULL;
577 GNUNET_MQ_destroy(mq); 584 GNUNET_MQ_destroy (mq);
578 } 585 }
579 if (NULL != queue->qh) 586 if (NULL != queue->qh)
580 { 587 {
581 GNUNET_TRANSPORT_communicator_mq_del(queue->qh); 588 GNUNET_TRANSPORT_communicator_mq_del (queue->qh);
582 queue->qh = NULL; 589 queue->qh = NULL;
583 } 590 }
584 GNUNET_assert( 591 GNUNET_assert (
585 GNUNET_YES == 592 GNUNET_YES ==
586 GNUNET_CONTAINER_multipeermap_remove(queue_map, &queue->target, queue)); 593 GNUNET_CONTAINER_multipeermap_remove (queue_map, &queue->target, queue));
587 GNUNET_STATISTICS_set(stats, 594 GNUNET_STATISTICS_set (stats,
588 "# queues active", 595 "# queues active",
589 GNUNET_CONTAINER_multipeermap_size(queue_map), 596 GNUNET_CONTAINER_multipeermap_size (queue_map),
590 GNUNET_NO); 597 GNUNET_NO);
591 if (NULL != queue->read_task) 598 if (NULL != queue->read_task)
592 { 599 {
593 GNUNET_SCHEDULER_cancel(queue->read_task); 600 GNUNET_SCHEDULER_cancel (queue->read_task);
594 queue->read_task = NULL; 601 queue->read_task = NULL;
595 } 602 }
596 if (NULL != queue->write_task) 603 if (NULL != queue->write_task)
597 { 604 {
598 GNUNET_SCHEDULER_cancel(queue->write_task); 605 GNUNET_SCHEDULER_cancel (queue->write_task);
599 queue->write_task = NULL; 606 queue->write_task = NULL;
600 } 607 }
601 GNUNET_NETWORK_socket_close(queue->sock); 608 GNUNET_NETWORK_socket_close (queue->sock);
602 gcry_cipher_close(queue->in_cipher); 609 gcry_cipher_close (queue->in_cipher);
603 gcry_cipher_close(queue->out_cipher); 610 gcry_cipher_close (queue->out_cipher);
604 GNUNET_free(queue->address); 611 GNUNET_free (queue->address);
605 if (0 != queue->backpressure) 612 if (0 != queue->backpressure)
606 queue->destroyed = GNUNET_YES; 613 queue->destroyed = GNUNET_YES;
607 else 614 else
608 GNUNET_free(queue); 615 GNUNET_free (queue);
609 if (NULL == listen_task) 616 if (NULL == listen_task)
610 listen_task = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 617 listen_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
611 listen_sock, 618 listen_sock,
612 &listen_cb, 619 &listen_cb,
613 NULL); 620 NULL);
614} 621}
615 622
616 623
@@ -623,24 +630,24 @@ queue_destroy(struct Queue *queue)
623 * @param smac[out] where to write the HMAC 630 * @param smac[out] where to write the HMAC
624 */ 631 */
625static void 632static void
626calculate_hmac(struct GNUNET_HashCode *hmac_secret, 633calculate_hmac (struct GNUNET_HashCode *hmac_secret,
627 const void *buf, 634 const void *buf,
628 size_t buf_size, 635 size_t buf_size,
629 struct GNUNET_ShortHashCode *smac) 636 struct GNUNET_ShortHashCode *smac)
630{ 637{
631 struct GNUNET_HashCode mac; 638 struct GNUNET_HashCode mac;
632 639
633 GNUNET_CRYPTO_hmac_raw(hmac_secret, 640 GNUNET_CRYPTO_hmac_raw (hmac_secret,
634 sizeof(struct GNUNET_HashCode), 641 sizeof(struct GNUNET_HashCode),
635 buf, 642 buf,
636 buf_size, 643 buf_size,
637 &mac); 644 &mac);
638 /* truncate to `struct GNUNET_ShortHashCode` */ 645 /* truncate to `struct GNUNET_ShortHashCode` */
639 memcpy(smac, &mac, sizeof(struct GNUNET_ShortHashCode)); 646 memcpy (smac, &mac, sizeof(struct GNUNET_ShortHashCode));
640 /* ratchet hmac key */ 647 /* ratchet hmac key */
641 GNUNET_CRYPTO_hash(hmac_secret, 648 GNUNET_CRYPTO_hash (hmac_secret,
642 sizeof(struct GNUNET_HashCode), 649 sizeof(struct GNUNET_HashCode),
643 hmac_secret); 650 hmac_secret);
644} 651}
645 652
646 653
@@ -651,18 +658,18 @@ calculate_hmac(struct GNUNET_HashCode *hmac_secret,
651 * @param queue queue to shut down nicely 658 * @param queue queue to shut down nicely
652 */ 659 */
653static void 660static void
654queue_finish(struct Queue *queue) 661queue_finish (struct Queue *queue)
655{ 662{
656 struct TCPFinish fin; 663 struct TCPFinish fin;
657 664
658 memset(&fin, 0, sizeof(fin)); 665 memset (&fin, 0, sizeof(fin));
659 fin.header.size = htons(sizeof(fin)); 666 fin.header.size = htons (sizeof(fin));
660 fin.header.type = htons(GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH); 667 fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH);
661 calculate_hmac(&queue->out_hmac, &fin, sizeof(fin), &fin.hmac); 668 calculate_hmac (&queue->out_hmac, &fin, sizeof(fin), &fin.hmac);
662 /* if there is any message left in pwrite_buf, we 669 /* if there is any message left in pwrite_buf, we
663 overwrite it (possibly dropping the last message 670 overwrite it (possibly dropping the last message
664 from CORE hard here) */ 671 from CORE hard here) */
665 memcpy(queue->pwrite_buf, &fin, sizeof(fin)); 672 memcpy (queue->pwrite_buf, &fin, sizeof(fin));
666 queue->pwrite_off = sizeof(fin); 673 queue->pwrite_off = sizeof(fin);
667 /* This flag will ensure that #queue_write() no longer 674 /* This flag will ensure that #queue_write() no longer
668 notifies CORE about the possibility of sending 675 notifies CORE about the possibility of sending
@@ -680,10 +687,10 @@ queue_finish(struct Queue *queue)
680 * @param queue queue for which the timeout should be rescheduled 687 * @param queue queue for which the timeout should be rescheduled
681 */ 688 */
682static void 689static void
683reschedule_queue_timeout(struct Queue *queue) 690reschedule_queue_timeout (struct Queue *queue)
684{ 691{
685 queue->timeout = 692 queue->timeout =
686 GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 693 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
687} 694}
688 695
689 696
@@ -693,7 +700,7 @@ reschedule_queue_timeout(struct Queue *queue)
693 * @param cls the `struct Queue *` to disconnect 700 * @param cls the `struct Queue *` to disconnect
694 */ 701 */
695static void 702static void
696queue_read(void *cls); 703queue_read (void *cls);
697 704
698 705
699/** 706/**
@@ -704,31 +711,31 @@ queue_read(void *cls);
704 * @param success #GNUNET_OK on success 711 * @param success #GNUNET_OK on success
705 */ 712 */
706static void 713static void
707core_read_finished_cb(void *cls, int success) 714core_read_finished_cb (void *cls, int success)
708{ 715{
709 struct Queue *queue = cls; 716 struct Queue *queue = cls;
710 717
711 if (GNUNET_OK != success) 718 if (GNUNET_OK != success)
712 GNUNET_STATISTICS_update(stats, 719 GNUNET_STATISTICS_update (stats,
713 "# messages lost in communicator API towards CORE", 720 "# messages lost in communicator API towards CORE",
714 1, 721 1,
715 GNUNET_NO); 722 GNUNET_NO);
716 queue->backpressure--; 723 queue->backpressure--;
717 /* handle deferred queue destruction */ 724 /* handle deferred queue destruction */
718 if ((queue->destroyed) && (0 == queue->backpressure)) 725 if ((queue->destroyed) && (0 == queue->backpressure))
719 { 726 {
720 GNUNET_free(queue); 727 GNUNET_free (queue);
721 return; 728 return;
722 } 729 }
723 reschedule_queue_timeout(queue); 730 reschedule_queue_timeout (queue);
724 /* possibly unchoke reading, now that CORE made progress */ 731 /* possibly unchoke reading, now that CORE made progress */
725 if (NULL == queue->read_task) 732 if (NULL == queue->read_task)
726 queue->read_task = 733 queue->read_task =
727 GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_absolute_get_remaining( 734 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (
728 queue->timeout), 735 queue->timeout),
729 queue->sock, 736 queue->sock,
730 &queue_read, 737 &queue_read,
731 queue); 738 queue);
732} 739}
733 740
734 741
@@ -742,34 +749,34 @@ core_read_finished_cb(void *cls, int success)
742 * @param plaintext_len number of bytes of plaintext received 749 * @param plaintext_len number of bytes of plaintext received
743 */ 750 */
744static void 751static void
745pass_plaintext_to_core(struct Queue *queue, 752pass_plaintext_to_core (struct Queue *queue,
746 const void *plaintext, 753 const void *plaintext,
747 size_t plaintext_len) 754 size_t plaintext_len)
748{ 755{
749 const struct GNUNET_MessageHeader *hdr = plaintext; 756 const struct GNUNET_MessageHeader *hdr = plaintext;
750 int ret; 757 int ret;
751 758
752 if (ntohs(hdr->size) != plaintext_len) 759 if (ntohs (hdr->size) != plaintext_len)
753 { 760 {
754 /* NOTE: If we ever allow multiple CORE messages in one 761 /* NOTE: If we ever allow multiple CORE messages in one
755 BOX, this will have to change! */ 762 BOX, this will have to change! */
756 GNUNET_break(0); 763 GNUNET_break (0);
757 return; 764 return;
758 } 765 }
759 ret = GNUNET_TRANSPORT_communicator_receive(ch, 766 ret = GNUNET_TRANSPORT_communicator_receive (ch,
760 &queue->target, 767 &queue->target,
761 hdr, 768 hdr,
762 ADDRESS_VALIDITY_PERIOD, 769 ADDRESS_VALIDITY_PERIOD,
763 &core_read_finished_cb, 770 &core_read_finished_cb,
764 queue); 771 queue);
765 if (GNUNET_OK == ret) 772 if (GNUNET_OK == ret)
766 queue->backpressure++; 773 queue->backpressure++;
767 GNUNET_break(GNUNET_NO != ret); /* backpressure not working!? */ 774 GNUNET_break (GNUNET_NO != ret); /* backpressure not working!? */
768 if (GNUNET_SYSERR == ret) 775 if (GNUNET_SYSERR == ret)
769 GNUNET_STATISTICS_update(stats, 776 GNUNET_STATISTICS_update (stats,
770 "# bytes lost due to CORE not running", 777 "# bytes lost due to CORE not running",
771 plaintext_len, 778 plaintext_len,
772 GNUNET_NO); 779 GNUNET_NO);
773} 780}
774 781
775 782
@@ -783,51 +790,51 @@ pass_plaintext_to_core(struct Queue *queue,
783 * @param hmac_key[out] HMAC key to initialize 790 * @param hmac_key[out] HMAC key to initialize
784 */ 791 */
785static void 792static void
786setup_cipher(const struct GNUNET_HashCode *dh, 793setup_cipher (const struct GNUNET_HashCode *dh,
787 const struct GNUNET_PeerIdentity *pid, 794 const struct GNUNET_PeerIdentity *pid,
788 gcry_cipher_hd_t *cipher, 795 gcry_cipher_hd_t *cipher,
789 struct GNUNET_HashCode *hmac_key) 796 struct GNUNET_HashCode *hmac_key)
790{ 797{
791 char key[256 / 8]; 798 char key[256 / 8];
792 char ctr[128 / 8]; 799 char ctr[128 / 8];
793 800
794 gcry_cipher_open(cipher, 801 gcry_cipher_open (cipher,
795 GCRY_CIPHER_AES256 /* low level: go for speed */, 802 GCRY_CIPHER_AES256 /* low level: go for speed */,
796 GCRY_CIPHER_MODE_CTR, 803 GCRY_CIPHER_MODE_CTR,
797 0 /* flags */); 804 0 /* flags */);
798 GNUNET_assert(GNUNET_YES == GNUNET_CRYPTO_kdf(key, 805 GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_kdf (key,
799 sizeof(key), 806 sizeof(key),
800 "TCP-key", 807 "TCP-key",
801 strlen("TCP-key"), 808 strlen ("TCP-key"),
802 dh, 809 dh,
803 sizeof(*dh), 810 sizeof(*dh),
804 pid, 811 pid,
805 sizeof(*pid), 812 sizeof(*pid),
806 NULL, 813 NULL,
807 0)); 814 0));
808 gcry_cipher_setkey(*cipher, key, sizeof(key)); 815 gcry_cipher_setkey (*cipher, key, sizeof(key));
809 GNUNET_assert(GNUNET_YES == GNUNET_CRYPTO_kdf(ctr, 816 GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_kdf (ctr,
810 sizeof(ctr), 817 sizeof(ctr),
811 "TCP-ctr", 818 "TCP-ctr",
812 strlen("TCP-ctr"), 819 strlen ("TCP-ctr"),
813 dh, 820 dh,
814 sizeof(*dh), 821 sizeof(*dh),
815 pid, 822 pid,
816 sizeof(*pid), 823 sizeof(*pid),
817 NULL, 824 NULL,
818 0)); 825 0));
819 gcry_cipher_setctr(*cipher, ctr, sizeof(ctr)); 826 gcry_cipher_setctr (*cipher, ctr, sizeof(ctr));
820 GNUNET_assert(GNUNET_YES == 827 GNUNET_assert (GNUNET_YES ==
821 GNUNET_CRYPTO_kdf(hmac_key, 828 GNUNET_CRYPTO_kdf (hmac_key,
822 sizeof(struct GNUNET_HashCode), 829 sizeof(struct GNUNET_HashCode),
823 "TCP-hmac", 830 "TCP-hmac",
824 strlen("TCP-hmac"), 831 strlen ("TCP-hmac"),
825 dh, 832 dh,
826 sizeof(*dh), 833 sizeof(*dh),
827 pid, 834 pid,
828 sizeof(*pid), 835 sizeof(*pid),
829 NULL, 836 NULL,
830 0)); 837 0));
831} 838}
832 839
833 840
@@ -838,13 +845,13 @@ setup_cipher(const struct GNUNET_HashCode *dh,
838 * @param queue[in,out] queue to initialize decryption cipher for 845 * @param queue[in,out] queue to initialize decryption cipher for
839 */ 846 */
840static void 847static void
841setup_in_cipher(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, 848setup_in_cipher (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
842 struct Queue *queue) 849 struct Queue *queue)
843{ 850{
844 struct GNUNET_HashCode dh; 851 struct GNUNET_HashCode dh;
845 852
846 GNUNET_CRYPTO_eddsa_ecdh(my_private_key, ephemeral, &dh); 853 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &dh);
847 setup_cipher(&dh, &my_identity, &queue->in_cipher, &queue->in_hmac); 854 setup_cipher (&dh, &my_identity, &queue->in_cipher, &queue->in_hmac);
848} 855}
849 856
850 857
@@ -857,30 +864,30 @@ setup_in_cipher(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
857 * @param rekey the rekey message 864 * @param rekey the rekey message
858 */ 865 */
859static void 866static void
860do_rekey(struct Queue *queue, const struct TCPRekey *rekey) 867do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
861{ 868{
862 struct TcpHandshakeSignature thp; 869 struct TcpHandshakeSignature thp;
863 870
864 thp.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY); 871 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY);
865 thp.purpose.size = htonl(sizeof(thp)); 872 thp.purpose.size = htonl (sizeof(thp));
866 thp.sender = queue->target; 873 thp.sender = queue->target;
867 thp.receiver = my_identity; 874 thp.receiver = my_identity;
868 thp.ephemeral = rekey->ephemeral; 875 thp.ephemeral = rekey->ephemeral;
869 thp.monotonic_time = rekey->monotonic_time; 876 thp.monotonic_time = rekey->monotonic_time;
870 /* FIXME: check monotonic time is monotonic... */ 877 /* FIXME: check monotonic time is monotonic... */
871 if (GNUNET_OK != 878 if (GNUNET_OK !=
872 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY, 879 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY,
873 &thp.purpose, 880 &thp.purpose,
874 &rekey->sender_sig, 881 &rekey->sender_sig,
875 &queue->target.public_key)) 882 &queue->target.public_key))
876 { 883 {
877 GNUNET_break(0); 884 GNUNET_break (0);
878 queue_finish(queue); 885 queue_finish (queue);
879 return; 886 return;
880 } 887 }
881 gcry_cipher_close(queue->in_cipher); 888 gcry_cipher_close (queue->in_cipher);
882 queue->rekeyed = GNUNET_YES; 889 queue->rekeyed = GNUNET_YES;
883 setup_in_cipher(&rekey->ephemeral, queue); 890 setup_in_cipher (&rekey->ephemeral, queue);
884} 891}
885 892
886 893
@@ -892,13 +899,13 @@ do_rekey(struct Queue *queue, const struct TCPRekey *rekey)
892 * @return number of bytes of plaintext handled, 0 for none 899 * @return number of bytes of plaintext handled, 0 for none
893 */ 900 */
894static size_t 901static size_t
895try_handle_plaintext(struct Queue *queue) 902try_handle_plaintext (struct Queue *queue)
896{ 903{
897 const struct GNUNET_MessageHeader *hdr = 904 const struct GNUNET_MessageHeader *hdr =
898 (const struct GNUNET_MessageHeader *)queue->pread_buf; 905 (const struct GNUNET_MessageHeader *) queue->pread_buf;
899 const struct TCPBox *box = (const struct TCPBox *)queue->pread_buf; 906 const struct TCPBox *box = (const struct TCPBox *) queue->pread_buf;
900 const struct TCPRekey *rekey = (const struct TCPRekey *)queue->pread_buf; 907 const struct TCPRekey *rekey = (const struct TCPRekey *) queue->pread_buf;
901 const struct TCPFinish *fin = (const struct TCPFinish *)queue->pread_buf; 908 const struct TCPFinish *fin = (const struct TCPFinish *) queue->pread_buf;
902 struct TCPRekey rekeyz; 909 struct TCPRekey rekeyz;
903 struct TCPFinish finz; 910 struct TCPFinish finz;
904 struct GNUNET_ShortHashCode tmac; 911 struct GNUNET_ShortHashCode tmac;
@@ -907,74 +914,74 @@ try_handle_plaintext(struct Queue *queue)
907 914
908 if (sizeof(*hdr) > queue->pread_off) 915 if (sizeof(*hdr) > queue->pread_off)
909 return 0; /* not even a header */ 916 return 0; /* not even a header */
910 type = ntohs(hdr->type); 917 type = ntohs (hdr->type);
911 switch (type) 918 switch (type)
919 {
920 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX:
921 /* Special case: header size excludes box itself! */
922 if (ntohs (hdr->size) + sizeof(struct TCPBox) > queue->pread_off)
923 return 0;
924 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
925 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
912 { 926 {
913 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX: 927 GNUNET_break_op (0);
914 /* Special case: header size excludes box itself! */ 928 queue_finish (queue);
915 if (ntohs(hdr->size) + sizeof(struct TCPBox) > queue->pread_off) 929 return 0;
916 return 0; 930 }
917 calculate_hmac(&queue->in_hmac, &box[1], ntohs(hdr->size), &tmac); 931 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
918 if (0 != memcmp(&tmac, &box->hmac, sizeof(tmac))) 932 size = ntohs (hdr->size) + sizeof(*box);
919 { 933 break;
920 GNUNET_break_op(0);
921 queue_finish(queue);
922 return 0;
923 }
924 pass_plaintext_to_core(queue, (const void *)&box[1], ntohs(hdr->size));
925 size = ntohs(hdr->size) + sizeof(*box);
926 break;
927
928 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY:
929 if (sizeof(*rekey) > queue->pread_off)
930 return 0;
931 if (ntohs(hdr->size) != sizeof(*rekey))
932 {
933 GNUNET_break_op(0);
934 queue_finish(queue);
935 return 0;
936 }
937 rekeyz = *rekey;
938 memset(&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
939 calculate_hmac(&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
940 if (0 != memcmp(&tmac, &box->hmac, sizeof(tmac)))
941 {
942 GNUNET_break_op(0);
943 queue_finish(queue);
944 return 0;
945 }
946 do_rekey(queue, rekey);
947 size = ntohs(hdr->size);
948 break;
949 934
950 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH: 935 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY:
951 if (sizeof(*fin) > queue->pread_off) 936 if (sizeof(*rekey) > queue->pread_off)
952 return 0; 937 return 0;
953 if (ntohs(hdr->size) != sizeof(*fin)) 938 if (ntohs (hdr->size) != sizeof(*rekey))
954 { 939 {
955 GNUNET_break_op(0); 940 GNUNET_break_op (0);
956 queue_finish(queue); 941 queue_finish (queue);
957 return 0; 942 return 0;
958 } 943 }
959 finz = *fin; 944 rekeyz = *rekey;
960 memset(&finz.hmac, 0, sizeof(finz.hmac)); 945 memset (&rekeyz.hmac, 0, sizeof(rekeyz.hmac));
961 calculate_hmac(&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac); 946 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
962 if (0 != memcmp(&tmac, &fin->hmac, sizeof(tmac))) 947 if (0 != memcmp (&tmac, &box->hmac, sizeof(tmac)))
963 { 948 {
964 GNUNET_break_op(0); 949 GNUNET_break_op (0);
965 queue_finish(queue); 950 queue_finish (queue);
966 return 0; 951 return 0;
967 } 952 }
968 /* handle FINISH by destroying queue */ 953 do_rekey (queue, rekey);
969 queue_destroy(queue); 954 size = ntohs (hdr->size);
970 break; 955 break;
971 956
972 default: 957 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH:
973 GNUNET_break_op(0); 958 if (sizeof(*fin) > queue->pread_off)
974 queue_finish(queue); 959 return 0;
960 if (ntohs (hdr->size) != sizeof(*fin))
961 {
962 GNUNET_break_op (0);
963 queue_finish (queue);
964 return 0;
965 }
966 finz = *fin;
967 memset (&finz.hmac, 0, sizeof(finz.hmac));
968 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof(rekeyz), &tmac);
969 if (0 != memcmp (&tmac, &fin->hmac, sizeof(tmac)))
970 {
971 GNUNET_break_op (0);
972 queue_finish (queue);
975 return 0; 973 return 0;
976 } 974 }
977 GNUNET_assert(0 != size); 975 /* handle FINISH by destroying queue */
976 queue_destroy (queue);
977 break;
978
979 default:
980 GNUNET_break_op (0);
981 queue_finish (queue);
982 return 0;
983 }
984 GNUNET_assert (0 != size);
978 return size; 985 return size;
979} 986}
980 987
@@ -985,96 +992,96 @@ try_handle_plaintext(struct Queue *queue)
985 * @param cls the `struct Queue *` to disconnect 992 * @param cls the `struct Queue *` to disconnect
986 */ 993 */
987static void 994static void
988queue_read(void *cls) 995queue_read (void *cls)
989{ 996{
990 struct Queue *queue = cls; 997 struct Queue *queue = cls;
991 struct GNUNET_TIME_Relative left; 998 struct GNUNET_TIME_Relative left;
992 ssize_t rcvd; 999 ssize_t rcvd;
993 1000
994 queue->read_task = NULL; 1001 queue->read_task = NULL;
995 rcvd = GNUNET_NETWORK_socket_recv(queue->sock, 1002 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
996 &queue->cread_buf[queue->cread_off], 1003 &queue->cread_buf[queue->cread_off],
997 BUF_SIZE - queue->cread_off); 1004 BUF_SIZE - queue->cread_off);
998 if (-1 == rcvd) 1005 if (-1 == rcvd)
1006 {
1007 if ((EAGAIN != errno) && (EINTR != errno))
999 { 1008 {
1000 if ((EAGAIN != errno) && (EINTR != errno)) 1009 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
1001 { 1010 queue_finish (queue);
1002 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "recv");
1003 queue_finish(queue);
1004 return;
1005 }
1006 /* try again */
1007 queue->read_task =
1008 GNUNET_SCHEDULER_add_read_net(left, queue->sock, &queue_read, queue);
1009 return; 1011 return;
1010 } 1012 }
1013 /* try again */
1014 queue->read_task =
1015 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1016 return;
1017 }
1011 if (0 != rcvd) 1018 if (0 != rcvd)
1012 reschedule_queue_timeout(queue); 1019 reschedule_queue_timeout (queue);
1013 queue->cread_off += rcvd; 1020 queue->cread_off += rcvd;
1014 while ((queue->pread_off < sizeof(queue->pread_buf)) && 1021 while ((queue->pread_off < sizeof(queue->pread_buf)) &&
1015 (queue->cread_off > 0)) 1022 (queue->cread_off > 0))
1016 { 1023 {
1017 size_t max = GNUNET_MIN(sizeof(queue->pread_buf) - queue->pread_off, 1024 size_t max = GNUNET_MIN (sizeof(queue->pread_buf) - queue->pread_off,
1018 queue->cread_off); 1025 queue->cread_off);
1019 size_t done; 1026 size_t done;
1020 size_t total; 1027 size_t total;
1021 1028
1022 GNUNET_assert(0 == 1029 GNUNET_assert (0 ==
1023 gcry_cipher_decrypt(queue->in_cipher, 1030 gcry_cipher_decrypt (queue->in_cipher,
1024 &queue->pread_buf[queue->pread_off], 1031 &queue->pread_buf[queue->pread_off],
1025 max, 1032 max,
1026 queue->cread_buf, 1033 queue->cread_buf,
1027 max)); 1034 max));
1028 queue->pread_off += max; 1035 queue->pread_off += max;
1029 total = 0; 1036 total = 0;
1030 while ((GNUNET_NO == queue->rekeyed) && 1037 while ((GNUNET_NO == queue->rekeyed) &&
1031 (0 != (done = try_handle_plaintext(queue)))) 1038 (0 != (done = try_handle_plaintext (queue))))
1032 { 1039 {
1033 /* 'done' bytes of plaintext were used, shift buffer */ 1040 /* 'done' bytes of plaintext were used, shift buffer */
1034 GNUNET_assert(done <= queue->pread_off); 1041 GNUNET_assert (done <= queue->pread_off);
1035 /* NOTE: this memmove() could possibly sometimes be 1042 /* NOTE: this memmove() could possibly sometimes be
1036 avoided if we pass 'total' into try_handle_plaintext() 1043 avoided if we pass 'total' into try_handle_plaintext()
1037 and use it at an offset into the buffer there! */ 1044 and use it at an offset into the buffer there! */
1038 memmove(queue->pread_buf, 1045 memmove (queue->pread_buf,
1039 &queue->pread_buf[done], 1046 &queue->pread_buf[done],
1040 queue->pread_off - done); 1047 queue->pread_off - done);
1041 queue->pread_off -= done; 1048 queue->pread_off -= done;
1042 total += done; 1049 total += done;
1043 } 1050 }
1044 /* when we encounter a rekey message, the decryption above uses the 1051 /* when we encounter a rekey message, the decryption above uses the
1045 wrong key for everything after the rekey; in that case, we have 1052 wrong key for everything after the rekey; in that case, we have
1046 to re-do the decryption at 'total' instead of at 'max'. If there 1053 to re-do the decryption at 'total' instead of at 'max'. If there
1047 is no rekey and the last message is incomplete (max > total), 1054 is no rekey and the last message is incomplete (max > total),
1048 it is safe to keep the decryption so we shift by 'max' */ 1055 it is safe to keep the decryption so we shift by 'max' */
1049 if (GNUNET_YES == queue->rekeyed) 1056 if (GNUNET_YES == queue->rekeyed)
1050 { 1057 {
1051 max = total; 1058 max = total;
1052 queue->rekeyed = GNUNET_NO; 1059 queue->rekeyed = GNUNET_NO;
1053 }
1054 memmove(queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
1055 queue->cread_off -= max;
1056 } 1060 }
1061 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
1062 queue->cread_off -= max;
1063 }
1057 1064
1058 if (BUF_SIZE == queue->cread_off) 1065 if (BUF_SIZE == queue->cread_off)
1059 return; /* buffer full, suspend reading */ 1066 return; /* buffer full, suspend reading */
1060 left = GNUNET_TIME_absolute_get_remaining(queue->timeout); 1067 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
1061 if (0 != left.rel_value_us) 1068 if (0 != left.rel_value_us)
1069 {
1070 if (max_queue_length < queue->backpressure)
1062 { 1071 {
1063 if (max_queue_length < queue->backpressure) 1072 /* continue reading */
1064 { 1073 queue->read_task =
1065 /* continue reading */ 1074 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1066 queue->read_task =
1067 GNUNET_SCHEDULER_add_read_net(left, queue->sock, &queue_read, queue);
1068 }
1069 return;
1070 } 1075 }
1071 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 1076 return;
1072 "Queue %p was idle for %s, disconnecting\n", 1077 }
1073 queue, 1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 GNUNET_STRINGS_relative_time_to_string( 1079 "Queue %p was idle for %s, disconnecting\n",
1075 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 1080 queue,
1076 GNUNET_YES)); 1081 GNUNET_STRINGS_relative_time_to_string (
1077 queue_finish(queue); 1082 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1083 GNUNET_YES));
1084 queue_finish (queue);
1078} 1085}
1079 1086
1080 1087
@@ -1086,7 +1093,7 @@ queue_read(void *cls)
1086 * @return converted bindto specification 1093 * @return converted bindto specification
1087 */ 1094 */
1088static struct sockaddr * 1095static struct sockaddr *
1089tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len) 1096tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1090{ 1097{
1091 struct sockaddr *in; 1098 struct sockaddr *in;
1092 unsigned int port; 1099 unsigned int port;
@@ -1094,88 +1101,88 @@ tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
1094 char *colon; 1101 char *colon;
1095 char *cp; 1102 char *cp;
1096 1103
1097 if (1 == sscanf(bindto, "%u%1s", &port, dummy)) 1104 if (1 == sscanf (bindto, "%u%1s", &port, dummy))
1105 {
1106 /* interpreting value as just a PORT number */
1107 if (port > UINT16_MAX)
1098 { 1108 {
1099 /* interpreting value as just a PORT number */ 1109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1100 if (port > UINT16_MAX) 1110 "BINDTO specification `%s' invalid: value too large for port\n",
1101 { 1111 bindto);
1102 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1112 return NULL;
1103 "BINDTO specification `%s' invalid: value too large for port\n", 1113 }
1104 bindto); 1114 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1105 return NULL; 1115 (GNUNET_YES ==
1106 } 1116 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1107 if ((GNUNET_NO == GNUNET_NETWORK_test_pf(PF_INET6)) || 1117 COMMUNICATOR_CONFIG_SECTION,
1108 (GNUNET_YES == 1118 "DISABLE_V6")))
1109 GNUNET_CONFIGURATION_get_value_yesno(cfg, 1119 {
1110 COMMUNICATOR_CONFIG_SECTION, 1120 struct sockaddr_in *i4;
1111 "DISABLE_V6"))) 1121
1112 { 1122 i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
1113 struct sockaddr_in *i4; 1123 i4->sin_family = AF_INET;
1114 1124 i4->sin_port = htons ((uint16_t) port);
1115 i4 = GNUNET_malloc(sizeof(struct sockaddr_in)); 1125 *sock_len = sizeof(struct sockaddr_in);
1116 i4->sin_family = AF_INET; 1126 in = (struct sockaddr *) i4;
1117 i4->sin_port = htons((uint16_t)port); 1127 }
1118 *sock_len = sizeof(struct sockaddr_in); 1128 else
1119 in = (struct sockaddr *)i4; 1129 {
1120 } 1130 struct sockaddr_in6 *i6;
1121 else 1131
1122 { 1132 i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
1123 struct sockaddr_in6 *i6; 1133 i6->sin6_family = AF_INET6;
1124 1134 i6->sin6_port = htons ((uint16_t) port);
1125 i6 = GNUNET_malloc(sizeof(struct sockaddr_in6)); 1135 *sock_len = sizeof(struct sockaddr_in6);
1126 i6->sin6_family = AF_INET6; 1136 in = (struct sockaddr *) i6;
1127 i6->sin6_port = htons((uint16_t)port);
1128 *sock_len = sizeof(struct sockaddr_in6);
1129 in = (struct sockaddr *)i6;
1130 }
1131 return in;
1132 } 1137 }
1133 cp = GNUNET_strdup(bindto); 1138 return in;
1134 colon = strrchr(cp, ':'); 1139 }
1140 cp = GNUNET_strdup (bindto);
1141 colon = strrchr (cp, ':');
1135 if (NULL != colon) 1142 if (NULL != colon)
1143 {
1144 /* interpet value after colon as port */
1145 *colon = '\0';
1146 colon++;
1147 if (1 == sscanf (colon, "%u%1s", &port, dummy))
1136 { 1148 {
1137 /* interpet value after colon as port */ 1149 /* interpreting value as just a PORT number */
1138 *colon = '\0'; 1150 if (port > UINT16_MAX)
1139 colon++; 1151 {
1140 if (1 == sscanf(colon, "%u%1s", &port, dummy)) 1152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1141 { 1153 "BINDTO specification `%s' invalid: value too large for port\n",
1142 /* interpreting value as just a PORT number */ 1154 bindto);
1143 if (port > UINT16_MAX) 1155 GNUNET_free (cp);
1144 { 1156 return NULL;
1145 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1157 }
1146 "BINDTO specification `%s' invalid: value too large for port\n",
1147 bindto);
1148 GNUNET_free(cp);
1149 return NULL;
1150 }
1151 }
1152 else
1153 {
1154 GNUNET_log(
1155 GNUNET_ERROR_TYPE_ERROR,
1156 "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1157 bindto);
1158 GNUNET_free(cp);
1159 return NULL;
1160 }
1161 } 1158 }
1162 else 1159 else
1163 { 1160 {
1164 /* interpret missing port as 0, aka pick any free one */ 1161 GNUNET_log (
1165 port = 0; 1162 GNUNET_ERROR_TYPE_ERROR,
1163 "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1164 bindto);
1165 GNUNET_free (cp);
1166 return NULL;
1166 } 1167 }
1168 }
1169 else
1170 {
1171 /* interpret missing port as 0, aka pick any free one */
1172 port = 0;
1173 }
1167 { 1174 {
1168 /* try IPv4 */ 1175 /* try IPv4 */
1169 struct sockaddr_in v4; 1176 struct sockaddr_in v4;
1170 1177
1171 if (1 == inet_pton(AF_INET, cp, &v4)) 1178 if (1 == inet_pton (AF_INET, cp, &v4))
1172 { 1179 {
1173 v4.sin_port = htons((uint16_t)port); 1180 v4.sin_port = htons ((uint16_t) port);
1174 in = GNUNET_memdup(&v4, sizeof(v4)); 1181 in = GNUNET_memdup (&v4, sizeof(v4));
1175 *sock_len = sizeof(v4); 1182 *sock_len = sizeof(v4);
1176 GNUNET_free(cp); 1183 GNUNET_free (cp);
1177 return in; 1184 return in;
1178 } 1185 }
1179 } 1186 }
1180 { 1187 {
1181 /* try IPv6 */ 1188 /* try IPv6 */
@@ -1183,22 +1190,22 @@ tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
1183 const char *start; 1190 const char *start;
1184 1191
1185 start = cp; 1192 start = cp;
1186 if (('[' == *cp) && (']' == cp[strlen(cp) - 1])) 1193 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1187 { 1194 {
1188 start++; /* skip over '[' */ 1195 start++; /* skip over '[' */
1189 cp[strlen(cp) - 1] = '\0'; /* eat ']' */ 1196 cp[strlen (cp) - 1] = '\0'; /* eat ']' */
1190 } 1197 }
1191 if (1 == inet_pton(AF_INET6, start, &v6)) 1198 if (1 == inet_pton (AF_INET6, start, &v6))
1192 { 1199 {
1193 v6.sin6_port = htons((uint16_t)port); 1200 v6.sin6_port = htons ((uint16_t) port);
1194 in = GNUNET_memdup(&v6, sizeof(v6)); 1201 in = GNUNET_memdup (&v6, sizeof(v6));
1195 *sock_len = sizeof(v6); 1202 *sock_len = sizeof(v6);
1196 GNUNET_free(cp); 1203 GNUNET_free (cp);
1197 return in; 1204 return in;
1198 } 1205 }
1199 } 1206 }
1200 /* #5528 FIXME (feature!): maybe also try getnameinfo()? */ 1207 /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
1201 GNUNET_free(cp); 1208 GNUNET_free (cp);
1202 return NULL; 1209 return NULL;
1203} 1210}
1204 1211
@@ -1210,17 +1217,17 @@ tcp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
1210 * @param queue queue to setup outgoing (encryption) cipher for 1217 * @param queue queue to setup outgoing (encryption) cipher for
1211 */ 1218 */
1212static void 1219static void
1213setup_out_cipher(struct Queue *queue) 1220setup_out_cipher (struct Queue *queue)
1214{ 1221{
1215 struct GNUNET_HashCode dh; 1222 struct GNUNET_HashCode dh;
1216 1223
1217 GNUNET_CRYPTO_ecdh_eddsa(&queue->ephemeral, &queue->target.public_key, &dh); 1224 GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, &queue->target.public_key, &dh);
1218 /* we don't need the private key anymore, drop it! */ 1225 /* we don't need the private key anymore, drop it! */
1219 memset(&queue->ephemeral, 0, sizeof(queue->ephemeral)); 1226 memset (&queue->ephemeral, 0, sizeof(queue->ephemeral));
1220 setup_cipher(&dh, &queue->target, &queue->out_cipher, &queue->out_hmac); 1227 setup_cipher (&dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1221 queue->rekey_time = GNUNET_TIME_relative_to_absolute(REKEY_TIME_INTERVAL); 1228 queue->rekey_time = GNUNET_TIME_relative_to_absolute (REKEY_TIME_INTERVAL);
1222 queue->rekey_left_bytes = 1229 queue->rekey_left_bytes =
1223 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, REKEY_MAX_BYTES); 1230 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, REKEY_MAX_BYTES);
1224} 1231}
1225 1232
1226 1233
@@ -1231,31 +1238,31 @@ setup_out_cipher(struct Queue *queue)
1231 * @param queue queue to perform rekeying on 1238 * @param queue queue to perform rekeying on
1232 */ 1239 */
1233static void 1240static void
1234inject_rekey(struct Queue *queue) 1241inject_rekey (struct Queue *queue)
1235{ 1242{
1236 struct TCPRekey rekey; 1243 struct TCPRekey rekey;
1237 struct TcpHandshakeSignature thp; 1244 struct TcpHandshakeSignature thp;
1238 1245
1239 GNUNET_assert(0 == queue->pwrite_off); 1246 GNUNET_assert (0 == queue->pwrite_off);
1240 memset(&rekey, 0, sizeof(rekey)); 1247 memset (&rekey, 0, sizeof(rekey));
1241 GNUNET_assert(GNUNET_OK == 1248 GNUNET_assert (GNUNET_OK ==
1242 GNUNET_CRYPTO_ecdhe_key_create2(&queue->ephemeral)); 1249 GNUNET_CRYPTO_ecdhe_key_create2 (&queue->ephemeral));
1243 rekey.header.type = ntohs(GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY); 1250 rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY);
1244 rekey.header.size = ntohs(sizeof(rekey)); 1251 rekey.header.size = ntohs (sizeof(rekey));
1245 GNUNET_CRYPTO_ecdhe_key_get_public(&queue->ephemeral, &rekey.ephemeral); 1252 GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &rekey.ephemeral);
1246 rekey.monotonic_time = 1253 rekey.monotonic_time =
1247 GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_monotonic(cfg)); 1254 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
1248 thp.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY); 1255 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY);
1249 thp.purpose.size = htonl(sizeof(thp)); 1256 thp.purpose.size = htonl (sizeof(thp));
1250 thp.sender = my_identity; 1257 thp.sender = my_identity;
1251 thp.receiver = queue->target; 1258 thp.receiver = queue->target;
1252 thp.ephemeral = rekey.ephemeral; 1259 thp.ephemeral = rekey.ephemeral;
1253 thp.monotonic_time = rekey.monotonic_time; 1260 thp.monotonic_time = rekey.monotonic_time;
1254 GNUNET_assert(GNUNET_OK == GNUNET_CRYPTO_eddsa_sign(my_private_key, 1261 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key,
1255 &thp.purpose, 1262 &thp.purpose,
1256 &rekey.sender_sig)); 1263 &rekey.sender_sig));
1257 calculate_hmac(&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac); 1264 calculate_hmac (&queue->out_hmac, &rekey, sizeof(rekey), &rekey.hmac);
1258 memcpy(queue->pwrite_buf, &rekey, sizeof(rekey)); 1265 memcpy (queue->pwrite_buf, &rekey, sizeof(rekey));
1259 queue->pwrite_off = sizeof(rekey); 1266 queue->pwrite_off = sizeof(rekey);
1260} 1267}
1261 1268
@@ -1267,79 +1274,79 @@ inject_rekey(struct Queue *queue)
1267 * @param cls a `struct Queue` 1274 * @param cls a `struct Queue`
1268 */ 1275 */
1269static void 1276static void
1270queue_write(void *cls) 1277queue_write (void *cls)
1271{ 1278{
1272 struct Queue *queue = cls; 1279 struct Queue *queue = cls;
1273 ssize_t sent; 1280 ssize_t sent;
1274 1281
1275 queue->write_task = NULL; 1282 queue->write_task = NULL;
1276 if (0 != queue->cwrite_off) 1283 if (0 != queue->cwrite_off)
1284 {
1285 sent = GNUNET_NETWORK_socket_send (queue->sock,
1286 queue->cwrite_buf,
1287 queue->cwrite_off);
1288 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1289 {
1290 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
1291 queue_destroy (queue);
1292 return;
1293 }
1294 if (sent > 0)
1277 { 1295 {
1278 sent = GNUNET_NETWORK_socket_send(queue->sock, 1296 size_t usent = (size_t) sent;
1279 queue->cwrite_buf, 1297
1280 queue->cwrite_off); 1298 memmove (queue->cwrite_buf,
1281 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno)) 1299 &queue->cwrite_buf[usent],
1282 { 1300 queue->cwrite_off - usent);
1283 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "send"); 1301 reschedule_queue_timeout (queue);
1284 queue_destroy(queue);
1285 return;
1286 }
1287 if (sent > 0)
1288 {
1289 size_t usent = (size_t)sent;
1290
1291 memmove(queue->cwrite_buf,
1292 &queue->cwrite_buf[usent],
1293 queue->cwrite_off - usent);
1294 reschedule_queue_timeout(queue);
1295 }
1296 } 1302 }
1303 }
1297 /* can we encrypt more? (always encrypt full messages, needed 1304 /* can we encrypt more? (always encrypt full messages, needed
1298 such that #mq_cancel() can work!) */ 1305 such that #mq_cancel() can work!) */
1299 if ((0 < queue->rekey_left_bytes) && 1306 if ((0 < queue->rekey_left_bytes) &&
1300 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE)) 1307 (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE))
1301 { 1308 {
1302 GNUNET_assert(0 == 1309 GNUNET_assert (0 ==
1303 gcry_cipher_encrypt(queue->out_cipher, 1310 gcry_cipher_encrypt (queue->out_cipher,
1304 &queue->cwrite_buf[queue->cwrite_off], 1311 &queue->cwrite_buf[queue->cwrite_off],
1305 queue->pwrite_off, 1312 queue->pwrite_off,
1306 queue->pwrite_buf, 1313 queue->pwrite_buf,
1307 queue->pwrite_off)); 1314 queue->pwrite_off));
1308 if (queue->rekey_left_bytes > queue->pwrite_off) 1315 if (queue->rekey_left_bytes > queue->pwrite_off)
1309 queue->rekey_left_bytes -= queue->pwrite_off; 1316 queue->rekey_left_bytes -= queue->pwrite_off;
1310 else 1317 else
1311 queue->rekey_left_bytes = 0; 1318 queue->rekey_left_bytes = 0;
1312 queue->cwrite_off += queue->pwrite_off; 1319 queue->cwrite_off += queue->pwrite_off;
1313 queue->pwrite_off = 0; 1320 queue->pwrite_off = 0;
1314 } 1321 }
1315 if ((0 == queue->pwrite_off) && 1322 if ((0 == queue->pwrite_off) &&
1316 ((0 == queue->rekey_left_bytes) || 1323 ((0 == queue->rekey_left_bytes) ||
1317 (0 == 1324 (0 ==
1318 GNUNET_TIME_absolute_get_remaining(queue->rekey_time).rel_value_us))) 1325 GNUNET_TIME_absolute_get_remaining (queue->rekey_time).rel_value_us)))
1319 { 1326 {
1320 gcry_cipher_close(queue->out_cipher); 1327 gcry_cipher_close (queue->out_cipher);
1321 setup_out_cipher(queue); 1328 setup_out_cipher (queue);
1322 inject_rekey(queue); 1329 inject_rekey (queue);
1323 } 1330 }
1324 if ((0 == queue->pwrite_off) && (!queue->finishing) && 1331 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1325 (queue->mq_awaits_continue)) 1332 (queue->mq_awaits_continue))
1326 { 1333 {
1327 queue->mq_awaits_continue = GNUNET_NO; 1334 queue->mq_awaits_continue = GNUNET_NO;
1328 GNUNET_MQ_impl_send_continue(queue->mq); 1335 GNUNET_MQ_impl_send_continue (queue->mq);
1329 } 1336 }
1330 /* did we just finish writing 'finish'? */ 1337 /* did we just finish writing 'finish'? */
1331 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing)) 1338 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1332 { 1339 {
1333 queue_destroy(queue); 1340 queue_destroy (queue);
1334 return; 1341 return;
1335 } 1342 }
1336 /* do we care to write more? */ 1343 /* do we care to write more? */
1337 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off)) 1344 if ((0 < queue->cwrite_off) || (0 < queue->pwrite_off))
1338 queue->write_task = 1345 queue->write_task =
1339 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_FOREVER_REL, 1346 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1340 queue->sock, 1347 queue->sock,
1341 &queue_write, 1348 &queue_write,
1342 queue); 1349 queue);
1343} 1350}
1344 1351
1345 1352
@@ -1352,32 +1359,32 @@ queue_write(void *cls)
1352 * @param impl_state our `struct Queue` 1359 * @param impl_state our `struct Queue`
1353 */ 1360 */
1354static void 1361static void
1355mq_send(struct GNUNET_MQ_Handle *mq, 1362mq_send (struct GNUNET_MQ_Handle *mq,
1356 const struct GNUNET_MessageHeader *msg, 1363 const struct GNUNET_MessageHeader *msg,
1357 void *impl_state) 1364 void *impl_state)
1358{ 1365{
1359 struct Queue *queue = impl_state; 1366 struct Queue *queue = impl_state;
1360 uint16_t msize = ntohs(msg->size); 1367 uint16_t msize = ntohs (msg->size);
1361 struct TCPBox box; 1368 struct TCPBox box;
1362 1369
1363 GNUNET_assert(mq == queue->mq); 1370 GNUNET_assert (mq == queue->mq);
1364 if (GNUNET_YES == queue->finishing) 1371 if (GNUNET_YES == queue->finishing)
1365 return; /* this queue is dying, drop msg */ 1372 return; /* this queue is dying, drop msg */
1366 GNUNET_assert(0 == queue->pread_off); 1373 GNUNET_assert (0 == queue->pread_off);
1367 box.header.type = htons(GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX); 1374 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
1368 box.header.size = htons(msize); 1375 box.header.size = htons (msize);
1369 calculate_hmac(&queue->out_hmac, msg, msize, &box.hmac); 1376 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
1370 memcpy(&queue->pread_buf[queue->pread_off], &box, sizeof(box)); 1377 memcpy (&queue->pread_buf[queue->pread_off], &box, sizeof(box));
1371 queue->pread_off += sizeof(box); 1378 queue->pread_off += sizeof(box);
1372 memcpy(&queue->pread_buf[queue->pread_off], msg, msize); 1379 memcpy (&queue->pread_buf[queue->pread_off], msg, msize);
1373 queue->pread_off += msize; 1380 queue->pread_off += msize;
1374 GNUNET_assert(NULL != queue->sock); 1381 GNUNET_assert (NULL != queue->sock);
1375 if (NULL == queue->write_task) 1382 if (NULL == queue->write_task)
1376 queue->write_task = 1383 queue->write_task =
1377 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_FOREVER_REL, 1384 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1378 queue->sock, 1385 queue->sock,
1379 &queue_write, 1386 &queue_write,
1380 queue); 1387 queue);
1381} 1388}
1382 1389
1383 1390
@@ -1390,15 +1397,15 @@ mq_send(struct GNUNET_MQ_Handle *mq,
1390 * @param impl_state our `struct Queue` 1397 * @param impl_state our `struct Queue`
1391 */ 1398 */
1392static void 1399static void
1393mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state) 1400mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
1394{ 1401{
1395 struct Queue *queue = impl_state; 1402 struct Queue *queue = impl_state;
1396 1403
1397 if (mq == queue->mq) 1404 if (mq == queue->mq)
1398 { 1405 {
1399 queue->mq = NULL; 1406 queue->mq = NULL;
1400 queue_finish(queue); 1407 queue_finish (queue);
1401 } 1408 }
1402} 1409}
1403 1410
1404 1411
@@ -1409,11 +1416,11 @@ mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
1409 * @param impl_state our `struct Queue` 1416 * @param impl_state our `struct Queue`
1410 */ 1417 */
1411static void 1418static void
1412mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state) 1419mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
1413{ 1420{
1414 struct Queue *queue = impl_state; 1421 struct Queue *queue = impl_state;
1415 1422
1416 GNUNET_assert(0 != queue->pwrite_off); 1423 GNUNET_assert (0 != queue->pwrite_off);
1417 queue->pwrite_off = 0; 1424 queue->pwrite_off = 0;
1418} 1425}
1419 1426
@@ -1428,15 +1435,15 @@ mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
1428 * @param error error code 1435 * @param error error code
1429 */ 1436 */
1430static void 1437static void
1431mq_error(void *cls, enum GNUNET_MQ_Error error) 1438mq_error (void *cls, enum GNUNET_MQ_Error error)
1432{ 1439{
1433 struct Queue *queue = cls; 1440 struct Queue *queue = cls;
1434 1441
1435 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 1442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1436 "MQ error in queue to %s: %d\n", 1443 "MQ error in queue to %s: %d\n",
1437 GNUNET_i2s(&queue->target), 1444 GNUNET_i2s (&queue->target),
1438 (int)error); 1445 (int) error);
1439 queue_finish(queue); 1446 queue_finish (queue);
1440} 1447}
1441 1448
1442 1449
@@ -1448,58 +1455,58 @@ mq_error(void *cls, enum GNUNET_MQ_Error error)
1448 * @param queue queue to boot 1455 * @param queue queue to boot
1449 */ 1456 */
1450static void 1457static void
1451boot_queue(struct Queue *queue, enum GNUNET_TRANSPORT_ConnectionStatus cs) 1458boot_queue (struct Queue *queue, enum GNUNET_TRANSPORT_ConnectionStatus cs)
1452{ 1459{
1453 queue->nt = 1460 queue->nt =
1454 GNUNET_NT_scanner_get_type(is, queue->address, queue->address_len); 1461 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
1455 (void)GNUNET_CONTAINER_multipeermap_put( 1462 (void) GNUNET_CONTAINER_multipeermap_put (
1456 queue_map, 1463 queue_map,
1457 &queue->target, 1464 &queue->target,
1458 queue, 1465 queue,
1459 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 1466 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1460 GNUNET_STATISTICS_set(stats, 1467 GNUNET_STATISTICS_set (stats,
1461 "# queues active", 1468 "# queues active",
1462 GNUNET_CONTAINER_multipeermap_size(queue_map), 1469 GNUNET_CONTAINER_multipeermap_size (queue_map),
1463 GNUNET_NO); 1470 GNUNET_NO);
1464 queue->timeout = 1471 queue->timeout =
1465 GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 1472 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1466 queue->mq = GNUNET_MQ_queue_for_callbacks(&mq_send, 1473 queue->mq = GNUNET_MQ_queue_for_callbacks (&mq_send,
1467 &mq_destroy, 1474 &mq_destroy,
1468 &mq_cancel, 1475 &mq_cancel,
1469 queue, 1476 queue,
1470 NULL, 1477 NULL,
1471 &mq_error, 1478 &mq_error,
1472 queue); 1479 queue);
1473 { 1480 {
1474 char *foreign_addr; 1481 char *foreign_addr;
1475 1482
1476 switch (queue->address->sa_family) 1483 switch (queue->address->sa_family)
1477 { 1484 {
1478 case AF_INET: 1485 case AF_INET:
1479 GNUNET_asprintf(&foreign_addr, 1486 GNUNET_asprintf (&foreign_addr,
1480 "%s-%s", 1487 "%s-%s",
1481 COMMUNICATOR_ADDRESS_PREFIX, 1488 COMMUNICATOR_ADDRESS_PREFIX,
1482 GNUNET_a2s(queue->address, queue->address_len)); 1489 GNUNET_a2s (queue->address, queue->address_len));
1483 break; 1490 break;
1484 1491
1485 case AF_INET6: 1492 case AF_INET6:
1486 GNUNET_asprintf(&foreign_addr, 1493 GNUNET_asprintf (&foreign_addr,
1487 "%s-%s", 1494 "%s-%s",
1488 COMMUNICATOR_ADDRESS_PREFIX, 1495 COMMUNICATOR_ADDRESS_PREFIX,
1489 GNUNET_a2s(queue->address, queue->address_len)); 1496 GNUNET_a2s (queue->address, queue->address_len));
1490 break; 1497 break;
1491 1498
1492 default: 1499 default:
1493 GNUNET_assert(0); 1500 GNUNET_assert (0);
1494 } 1501 }
1495 queue->qh = GNUNET_TRANSPORT_communicator_mq_add(ch, 1502 queue->qh = GNUNET_TRANSPORT_communicator_mq_add (ch,
1496 &queue->target, 1503 &queue->target,
1497 foreign_addr, 1504 foreign_addr,
1498 0 /* no MTU */, 1505 0 /* no MTU */,
1499 queue->nt, 1506 queue->nt,
1500 cs, 1507 cs,
1501 queue->mq); 1508 queue->mq);
1502 GNUNET_free(foreign_addr); 1509 GNUNET_free (foreign_addr);
1503 } 1510 }
1504} 1511}
1505 1512
@@ -1515,33 +1522,33 @@ boot_queue(struct Queue *queue, enum GNUNET_TRANSPORT_ConnectionStatus cs)
1515 * @param epub our public key for the KX 1522 * @param epub our public key for the KX
1516 */ 1523 */
1517static void 1524static void
1518transmit_kx(struct Queue *queue, 1525transmit_kx (struct Queue *queue,
1519 const struct GNUNET_CRYPTO_EcdhePublicKey *epub) 1526 const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
1520{ 1527{
1521 struct TcpHandshakeSignature ths; 1528 struct TcpHandshakeSignature ths;
1522 struct TCPConfirmation tc; 1529 struct TCPConfirmation tc;
1523 1530
1524 memcpy(queue->cwrite_buf, epub, sizeof(*epub)); 1531 memcpy (queue->cwrite_buf, epub, sizeof(*epub));
1525 queue->cwrite_off = sizeof(epub); 1532 queue->cwrite_off = sizeof(epub);
1526 /* compute 'tc' and append in encrypted format to cwrite_buf */ 1533 /* compute 'tc' and append in encrypted format to cwrite_buf */
1527 tc.sender = my_identity; 1534 tc.sender = my_identity;
1528 tc.monotonic_time = 1535 tc.monotonic_time =
1529 GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_monotonic(cfg)); 1536 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
1530 ths.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE); 1537 ths.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE);
1531 ths.purpose.size = htonl(sizeof(ths)); 1538 ths.purpose.size = htonl (sizeof(ths));
1532 ths.sender = my_identity; 1539 ths.sender = my_identity;
1533 ths.receiver = queue->target; 1540 ths.receiver = queue->target;
1534 ths.ephemeral = *epub; 1541 ths.ephemeral = *epub;
1535 ths.monotonic_time = tc.monotonic_time; 1542 ths.monotonic_time = tc.monotonic_time;
1536 GNUNET_assert(GNUNET_OK == GNUNET_CRYPTO_eddsa_sign(my_private_key, 1543 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key,
1537 &ths.purpose, 1544 &ths.purpose,
1538 &tc.sender_sig)); 1545 &tc.sender_sig));
1539 GNUNET_assert(0 == 1546 GNUNET_assert (0 ==
1540 gcry_cipher_encrypt(queue->out_cipher, 1547 gcry_cipher_encrypt (queue->out_cipher,
1541 &queue->cwrite_buf[queue->cwrite_off], 1548 &queue->cwrite_buf[queue->cwrite_off],
1542 sizeof(tc), 1549 sizeof(tc),
1543 &tc, 1550 &tc,
1544 sizeof(tc))); 1551 sizeof(tc)));
1545 queue->cwrite_off += sizeof(tc); 1552 queue->cwrite_off += sizeof(tc);
1546} 1553}
1547 1554
@@ -1554,15 +1561,15 @@ transmit_kx(struct Queue *queue,
1554 * @param queue the queue to setup 1561 * @param queue the queue to setup
1555 */ 1562 */
1556static void 1563static void
1557start_initial_kx_out(struct Queue *queue) 1564start_initial_kx_out (struct Queue *queue)
1558{ 1565{
1559 struct GNUNET_CRYPTO_EcdhePublicKey epub; 1566 struct GNUNET_CRYPTO_EcdhePublicKey epub;
1560 1567
1561 GNUNET_assert(GNUNET_OK == 1568 GNUNET_assert (GNUNET_OK ==
1562 GNUNET_CRYPTO_ecdhe_key_create2(&queue->ephemeral)); 1569 GNUNET_CRYPTO_ecdhe_key_create2 (&queue->ephemeral));
1563 GNUNET_CRYPTO_ecdhe_key_get_public(&queue->ephemeral, &epub); 1570 GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &epub);
1564 setup_out_cipher(queue); 1571 setup_out_cipher (queue);
1565 transmit_kx(queue, &epub); 1572 transmit_kx (queue, &epub);
1566} 1573}
1567 1574
1568 1575
@@ -1578,31 +1585,32 @@ start_initial_kx_out(struct Queue *queue)
1578 * @return #GNUNET_OK if the signature was OK, #GNUNET_SYSERR if not 1585 * @return #GNUNET_OK if the signature was OK, #GNUNET_SYSERR if not
1579 */ 1586 */
1580static int 1587static int
1581decrypt_and_check_tc(struct Queue *queue, 1588decrypt_and_check_tc (struct Queue *queue,
1582 struct TCPConfirmation *tc, 1589 struct TCPConfirmation *tc,
1583 char *ibuf) 1590 char *ibuf)
1584{ 1591{
1585 struct TcpHandshakeSignature ths; 1592 struct TcpHandshakeSignature ths;
1586 1593
1587 GNUNET_assert( 1594 GNUNET_assert (
1588 0 == 1595 0 ==
1589 gcry_cipher_decrypt(queue->in_cipher, 1596 gcry_cipher_decrypt (queue->in_cipher,
1590 tc, 1597 tc,
1591 sizeof(*tc), 1598 sizeof(*tc),
1592 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)], 1599 &ibuf[sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)],
1593 sizeof(tc))); 1600 sizeof(tc)));
1594 ths.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE); 1601 ths.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE);
1595 ths.purpose.size = htonl(sizeof(ths)); 1602 ths.purpose.size = htonl (sizeof(ths));
1596 ths.sender = tc->sender; 1603 ths.sender = tc->sender;
1597 ths.receiver = my_identity; 1604 ths.receiver = my_identity;
1598 memcpy(&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey)); 1605 memcpy (&ths.ephemeral, ibuf, sizeof(struct GNUNET_CRYPTO_EcdhePublicKey));
1599 ths.monotonic_time = tc->monotonic_time; 1606 ths.monotonic_time = tc->monotonic_time;
1600 /* FIXME: check monotonic time against previous mono times 1607 /* FIXME: check monotonic time against previous mono times
1601 from this sender! */ 1608 from this sender! */
1602 return GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE, 1609 return GNUNET_CRYPTO_eddsa_verify (
1603 &ths.purpose, 1610 GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE,
1604 &tc->sender_sig, 1611 &ths.purpose,
1605 &tc->sender.public_key); 1612 &tc->sender_sig,
1613 &tc->sender.public_key);
1606} 1614}
1607 1615
1608 1616
@@ -1612,12 +1620,12 @@ decrypt_and_check_tc(struct Queue *queue,
1612 * @param pq proto queue to free 1620 * @param pq proto queue to free
1613 */ 1621 */
1614static void 1622static void
1615free_proto_queue(struct ProtoQueue *pq) 1623free_proto_queue (struct ProtoQueue *pq)
1616{ 1624{
1617 GNUNET_NETWORK_socket_close(pq->sock); 1625 GNUNET_NETWORK_socket_close (pq->sock);
1618 GNUNET_free(pq->address); 1626 GNUNET_free (pq->address);
1619 GNUNET_CONTAINER_DLL_remove(proto_head, proto_tail, pq); 1627 GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
1620 GNUNET_free(pq); 1628 GNUNET_free (pq);
1621} 1629}
1622 1630
1623 1631
@@ -1628,7 +1636,7 @@ free_proto_queue(struct ProtoQueue *pq)
1628 * @param cls a `struct ProtoQueue` 1636 * @param cls a `struct ProtoQueue`
1629 */ 1637 */
1630static void 1638static void
1631proto_read_kx(void *cls) 1639proto_read_kx (void *cls)
1632{ 1640{
1633 struct ProtoQueue *pq = cls; 1641 struct ProtoQueue *pq = cls;
1634 ssize_t rcvd; 1642 ssize_t rcvd;
@@ -1637,67 +1645,67 @@ proto_read_kx(void *cls)
1637 struct TCPConfirmation tc; 1645 struct TCPConfirmation tc;
1638 1646
1639 pq->read_task = NULL; 1647 pq->read_task = NULL;
1640 left = GNUNET_TIME_absolute_get_remaining(pq->timeout); 1648 left = GNUNET_TIME_absolute_get_remaining (pq->timeout);
1641 if (0 == left.rel_value_us) 1649 if (0 == left.rel_value_us)
1642 { 1650 {
1643 free_proto_queue(pq); 1651 free_proto_queue (pq);
1644 return; 1652 return;
1645 } 1653 }
1646 rcvd = GNUNET_NETWORK_socket_recv(pq->sock, 1654 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
1647 &pq->ibuf[pq->ibuf_off], 1655 &pq->ibuf[pq->ibuf_off],
1648 sizeof(pq->ibuf) - pq->ibuf_off); 1656 sizeof(pq->ibuf) - pq->ibuf_off);
1649 if (-1 == rcvd) 1657 if (-1 == rcvd)
1658 {
1659 if ((EAGAIN != errno) && (EINTR != errno))
1650 { 1660 {
1651 if ((EAGAIN != errno) && (EINTR != errno)) 1661 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
1652 { 1662 free_proto_queue (pq);
1653 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "recv");
1654 free_proto_queue(pq);
1655 return;
1656 }
1657 /* try again */
1658 pq->read_task =
1659 GNUNET_SCHEDULER_add_read_net(left, pq->sock, &proto_read_kx, pq);
1660 return; 1663 return;
1661 } 1664 }
1665 /* try again */
1666 pq->read_task =
1667 GNUNET_SCHEDULER_add_read_net (left, pq->sock, &proto_read_kx, pq);
1668 return;
1669 }
1662 pq->ibuf_off += rcvd; 1670 pq->ibuf_off += rcvd;
1663 if (pq->ibuf_off > sizeof(pq->ibuf)) 1671 if (pq->ibuf_off > sizeof(pq->ibuf))
1664 { 1672 {
1665 /* read more */ 1673 /* read more */
1666 pq->read_task = 1674 pq->read_task =
1667 GNUNET_SCHEDULER_add_read_net(left, pq->sock, &proto_read_kx, pq); 1675 GNUNET_SCHEDULER_add_read_net (left, pq->sock, &proto_read_kx, pq);
1668 return; 1676 return;
1669 } 1677 }
1670 /* we got all the data, let's find out who we are talking to! */ 1678 /* we got all the data, let's find out who we are talking to! */
1671 queue = GNUNET_new(struct Queue); 1679 queue = GNUNET_new (struct Queue);
1672 setup_in_cipher((const struct GNUNET_CRYPTO_EcdhePublicKey *)pq->ibuf, 1680 setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf,
1673 queue); 1681 queue);
1674 if (GNUNET_OK != decrypt_and_check_tc(queue, &tc, pq->ibuf)) 1682 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
1675 { 1683 {
1676 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 1684 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1677 "Invalid TCP KX received from %s\n", 1685 "Invalid TCP KX received from %s\n",
1678 GNUNET_a2s(queue->address, queue->address_len)); 1686 GNUNET_a2s (queue->address, queue->address_len));
1679 gcry_cipher_close(queue->in_cipher); 1687 gcry_cipher_close (queue->in_cipher);
1680 GNUNET_free(queue); 1688 GNUNET_free (queue);
1681 free_proto_queue(pq); 1689 free_proto_queue (pq);
1682 return; 1690 return;
1683 } 1691 }
1684 queue->address = pq->address; /* steals reference */ 1692 queue->address = pq->address; /* steals reference */
1685 queue->address_len = pq->address_len; 1693 queue->address_len = pq->address_len;
1686 queue->target = tc.sender; 1694 queue->target = tc.sender;
1687 start_initial_kx_out(queue); 1695 start_initial_kx_out (queue);
1688 boot_queue(queue, GNUNET_TRANSPORT_CS_INBOUND); 1696 boot_queue (queue, GNUNET_TRANSPORT_CS_INBOUND);
1689 queue->read_task = 1697 queue->read_task =
1690 GNUNET_SCHEDULER_add_read_net(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 1698 GNUNET_SCHEDULER_add_read_net (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1691 queue->sock,
1692 &queue_read,
1693 queue);
1694 queue->write_task =
1695 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_FOREVER_REL,
1696 queue->sock, 1699 queue->sock,
1697 &queue_write, 1700 &queue_read,
1698 queue); 1701 queue);
1699 GNUNET_CONTAINER_DLL_remove(proto_head, proto_tail, pq); 1702 queue->write_task =
1700 GNUNET_free(pq); 1703 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1704 queue->sock,
1705 &queue_write,
1706 queue);
1707 GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
1708 GNUNET_free (pq);
1701} 1709}
1702 1710
1703 1711
@@ -1709,7 +1717,7 @@ proto_read_kx(void *cls)
1709 * @param cls NULL 1717 * @param cls NULL
1710 */ 1718 */
1711static void 1719static void
1712listen_cb(void *cls) 1720listen_cb (void *cls)
1713{ 1721{
1714 struct sockaddr_storage in; 1722 struct sockaddr_storage in;
1715 socklen_t addrlen; 1723 socklen_t addrlen;
@@ -1717,35 +1725,35 @@ listen_cb(void *cls)
1717 struct ProtoQueue *pq; 1725 struct ProtoQueue *pq;
1718 1726
1719 listen_task = NULL; 1727 listen_task = NULL;
1720 GNUNET_assert(NULL != listen_sock); 1728 GNUNET_assert (NULL != listen_sock);
1721 addrlen = sizeof(in); 1729 addrlen = sizeof(in);
1722 memset(&in, 0, sizeof(in)); 1730 memset (&in, 0, sizeof(in));
1723 sock = GNUNET_NETWORK_socket_accept(listen_sock, 1731 sock = GNUNET_NETWORK_socket_accept (listen_sock,
1724 (struct sockaddr *)&in, 1732 (struct sockaddr *) &in,
1725 &addrlen); 1733 &addrlen);
1726 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno))) 1734 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
1727 return; /* system limit reached, wait until connection goes down */ 1735 return; /* system limit reached, wait until connection goes down */
1728 listen_task = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 1736 listen_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1729 listen_sock, 1737 listen_sock,
1730 &listen_cb, 1738 &listen_cb,
1731 NULL); 1739 NULL);
1732 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno))) 1740 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
1733 return; 1741 return;
1734 if (NULL == sock) 1742 if (NULL == sock)
1735 { 1743 {
1736 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "accept"); 1744 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
1737 return; 1745 return;
1738 } 1746 }
1739 pq = GNUNET_new(struct ProtoQueue); 1747 pq = GNUNET_new (struct ProtoQueue);
1740 pq->address_len = addrlen; 1748 pq->address_len = addrlen;
1741 pq->address = GNUNET_memdup(&in, addrlen); 1749 pq->address = GNUNET_memdup (&in, addrlen);
1742 pq->timeout = GNUNET_TIME_relative_to_absolute(PROTO_QUEUE_TIMEOUT); 1750 pq->timeout = GNUNET_TIME_relative_to_absolute (PROTO_QUEUE_TIMEOUT);
1743 pq->sock = sock; 1751 pq->sock = sock;
1744 pq->read_task = GNUNET_SCHEDULER_add_read_net(PROTO_QUEUE_TIMEOUT, 1752 pq->read_task = GNUNET_SCHEDULER_add_read_net (PROTO_QUEUE_TIMEOUT,
1745 pq->sock, 1753 pq->sock,
1746 &proto_read_kx, 1754 &proto_read_kx,
1747 pq); 1755 pq);
1748 GNUNET_CONTAINER_DLL_insert(proto_head, proto_tail, pq); 1756 GNUNET_CONTAINER_DLL_insert (proto_head, proto_tail, pq);
1749} 1757}
1750 1758
1751 1759
@@ -1757,7 +1765,7 @@ listen_cb(void *cls)
1757 * @param cls a `struct Queue` 1765 * @param cls a `struct Queue`
1758 */ 1766 */
1759static void 1767static void
1760queue_read_kx(void *cls) 1768queue_read_kx (void *cls)
1761{ 1769{
1762 struct Queue *queue = cls; 1770 struct Queue *queue = cls;
1763 ssize_t rcvd; 1771 ssize_t rcvd;
@@ -1765,65 +1773,65 @@ queue_read_kx(void *cls)
1765 struct TCPConfirmation tc; 1773 struct TCPConfirmation tc;
1766 1774
1767 queue->read_task = NULL; 1775 queue->read_task = NULL;
1768 left = GNUNET_TIME_absolute_get_remaining(queue->timeout); 1776 left = GNUNET_TIME_absolute_get_remaining (queue->timeout);
1769 if (0 == left.rel_value_us) 1777 if (0 == left.rel_value_us)
1770 { 1778 {
1771 queue_destroy(queue); 1779 queue_destroy (queue);
1772 return; 1780 return;
1773 } 1781 }
1774 rcvd = GNUNET_NETWORK_socket_recv(queue->sock, 1782 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
1775 &queue->cread_buf[queue->cread_off], 1783 &queue->cread_buf[queue->cread_off],
1776 BUF_SIZE - queue->cread_off); 1784 BUF_SIZE - queue->cread_off);
1777 if (-1 == rcvd) 1785 if (-1 == rcvd)
1786 {
1787 if ((EAGAIN != errno) && (EINTR != errno))
1778 { 1788 {
1779 if ((EAGAIN != errno) && (EINTR != errno)) 1789 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
1780 { 1790 queue_destroy (queue);
1781 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "recv");
1782 queue_destroy(queue);
1783 return;
1784 }
1785 queue->read_task =
1786 GNUNET_SCHEDULER_add_read_net(left, queue->sock, &queue_read_kx, queue);
1787 return; 1791 return;
1788 } 1792 }
1793 queue->read_task =
1794 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
1795 return;
1796 }
1789 queue->cread_off += rcvd; 1797 queue->cread_off += rcvd;
1790 if (queue->cread_off < INITIAL_KX_SIZE) 1798 if (queue->cread_off < INITIAL_KX_SIZE)
1791 { 1799 {
1792 /* read more */ 1800 /* read more */
1793 queue->read_task = 1801 queue->read_task =
1794 GNUNET_SCHEDULER_add_read_net(left, queue->sock, &queue_read_kx, queue); 1802 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
1795 return; 1803 return;
1796 } 1804 }
1797 /* we got all the data, let's find out who we are talking to! */ 1805 /* we got all the data, let's find out who we are talking to! */
1798 setup_in_cipher((const struct GNUNET_CRYPTO_EcdhePublicKey *) 1806 setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *)
1799 queue->cread_buf, 1807 queue->cread_buf,
1800 queue); 1808 queue);
1801 if (GNUNET_OK != decrypt_and_check_tc(queue, &tc, queue->cread_buf)) 1809 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
1802 { 1810 {
1803 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 1811 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1804 "Invalid TCP KX received from %s\n", 1812 "Invalid TCP KX received from %s\n",
1805 GNUNET_a2s(queue->address, queue->address_len)); 1813 GNUNET_a2s (queue->address, queue->address_len));
1806 queue_destroy(queue); 1814 queue_destroy (queue);
1807 return; 1815 return;
1808 } 1816 }
1809 if (0 != 1817 if (0 !=
1810 memcmp(&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity))) 1818 memcmp (&tc.sender, &queue->target, sizeof(struct GNUNET_PeerIdentity)))
1811 { 1819 {
1812 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1820 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1813 "Invalid sender in TCP KX received from %s\n", 1821 "Invalid sender in TCP KX received from %s\n",
1814 GNUNET_a2s(queue->address, queue->address_len)); 1822 GNUNET_a2s (queue->address, queue->address_len));
1815 queue_destroy(queue); 1823 queue_destroy (queue);
1816 return; 1824 return;
1817 } 1825 }
1818 1826
1819 /* update queue timeout */ 1827 /* update queue timeout */
1820 reschedule_queue_timeout(queue); 1828 reschedule_queue_timeout (queue);
1821 /* prepare to continue with regular read task immediately */ 1829 /* prepare to continue with regular read task immediately */
1822 memmove(queue->cread_buf, 1830 memmove (queue->cread_buf,
1823 &queue->cread_buf[INITIAL_KX_SIZE], 1831 &queue->cread_buf[INITIAL_KX_SIZE],
1824 queue->cread_off - (INITIAL_KX_SIZE)); 1832 queue->cread_off - (INITIAL_KX_SIZE));
1825 queue->cread_off -= INITIAL_KX_SIZE; 1833 queue->cread_off -= INITIAL_KX_SIZE;
1826 queue->read_task = GNUNET_SCHEDULER_add_now(&queue_read, queue); 1834 queue->read_task = GNUNET_SCHEDULER_add_now (&queue_read, queue);
1827} 1835}
1828 1836
1829 1837
@@ -1846,7 +1854,7 @@ queue_read_kx(void *cls)
1846 * invalid 1854 * invalid
1847 */ 1855 */
1848static int 1856static int
1849mq_init(void *cls, const struct GNUNET_PeerIdentity *peer, const char *address) 1857mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
1850{ 1858{
1851 struct Queue *queue; 1859 struct Queue *queue;
1852 const char *path; 1860 const char *path;
@@ -1854,54 +1862,54 @@ mq_init(void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
1854 socklen_t in_len; 1862 socklen_t in_len;
1855 struct GNUNET_NETWORK_Handle *sock; 1863 struct GNUNET_NETWORK_Handle *sock;
1856 1864
1857 if (0 != strncmp(address, 1865 if (0 != strncmp (address,
1858 COMMUNICATOR_ADDRESS_PREFIX "-", 1866 COMMUNICATOR_ADDRESS_PREFIX "-",
1859 strlen(COMMUNICATOR_ADDRESS_PREFIX "-"))) 1867 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
1860 { 1868 {
1861 GNUNET_break_op(0); 1869 GNUNET_break_op (0);
1862 return GNUNET_SYSERR; 1870 return GNUNET_SYSERR;
1863 } 1871 }
1864 path = &address[strlen(COMMUNICATOR_ADDRESS_PREFIX "-")]; 1872 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
1865 in = tcp_address_to_sockaddr(path, &in_len); 1873 in = tcp_address_to_sockaddr (path, &in_len);
1866 1874
1867 sock = GNUNET_NETWORK_socket_create(in->sa_family, SOCK_STREAM, IPPROTO_TCP); 1875 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
1868 if (NULL == sock) 1876 if (NULL == sock)
1869 { 1877 {
1870 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1878 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1871 "socket(%d) failed: %s", 1879 "socket(%d) failed: %s",
1872 in->sa_family, 1880 in->sa_family,
1873 strerror(errno)); 1881 strerror (errno));
1874 GNUNET_free(in); 1882 GNUNET_free (in);
1875 return GNUNET_SYSERR; 1883 return GNUNET_SYSERR;
1876 } 1884 }
1877 if (GNUNET_OK != GNUNET_NETWORK_socket_connect(sock, in, in_len)) 1885 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len))
1878 { 1886 {
1879 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 1887 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1880 "connect to `%s' failed: %s", 1888 "connect to `%s' failed: %s",
1881 address, 1889 address,
1882 strerror(errno)); 1890 strerror (errno));
1883 GNUNET_NETWORK_socket_close(sock); 1891 GNUNET_NETWORK_socket_close (sock);
1884 GNUNET_free(in); 1892 GNUNET_free (in);
1885 return GNUNET_SYSERR; 1893 return GNUNET_SYSERR;
1886 } 1894 }
1887 1895
1888 queue = GNUNET_new(struct Queue); 1896 queue = GNUNET_new (struct Queue);
1889 queue->target = *peer; 1897 queue->target = *peer;
1890 queue->address = in; 1898 queue->address = in;
1891 queue->address_len = in_len; 1899 queue->address_len = in_len;
1892 queue->sock = sock; 1900 queue->sock = sock;
1893 boot_queue(queue, GNUNET_TRANSPORT_CS_OUTBOUND); 1901 boot_queue (queue, GNUNET_TRANSPORT_CS_OUTBOUND);
1894 queue->read_task = 1902 queue->read_task =
1895 GNUNET_SCHEDULER_add_read_net(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 1903 GNUNET_SCHEDULER_add_read_net (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1896 queue->sock,
1897 &queue_read_kx,
1898 queue);
1899 start_initial_kx_out(queue);
1900 queue->write_task =
1901 GNUNET_SCHEDULER_add_write_net(GNUNET_TIME_UNIT_FOREVER_REL,
1902 queue->sock, 1904 queue->sock,
1903 &queue_write, 1905 &queue_read_kx,
1904 queue); 1906 queue);
1907 start_initial_kx_out (queue);
1908 queue->write_task =
1909 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1910 queue->sock,
1911 &queue_write,
1912 queue);
1905 return GNUNET_OK; 1913 return GNUNET_OK;
1906} 1914}
1907 1915
@@ -1915,15 +1923,15 @@ mq_init(void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
1915 * @return #GNUNET_OK to continue to iterate 1923 * @return #GNUNET_OK to continue to iterate
1916 */ 1924 */
1917static int 1925static int
1918get_queue_delete_it(void *cls, 1926get_queue_delete_it (void *cls,
1919 const struct GNUNET_PeerIdentity *target, 1927 const struct GNUNET_PeerIdentity *target,
1920 void *value) 1928 void *value)
1921{ 1929{
1922 struct Queue *queue = value; 1930 struct Queue *queue = value;
1923 1931
1924 (void)cls; 1932 (void) cls;
1925 (void)target; 1933 (void) target;
1926 queue_destroy(queue); 1934 queue_destroy (queue);
1927 return GNUNET_OK; 1935 return GNUNET_OK;
1928} 1936}
1929 1937
@@ -1934,47 +1942,47 @@ get_queue_delete_it(void *cls,
1934 * @param cls NULL (always) 1942 * @param cls NULL (always)
1935 */ 1943 */
1936static void 1944static void
1937do_shutdown(void *cls) 1945do_shutdown (void *cls)
1938{ 1946{
1939 while (NULL != proto_head) 1947 while (NULL != proto_head)
1940 free_proto_queue(proto_head); 1948 free_proto_queue (proto_head);
1941 if (NULL != nat) 1949 if (NULL != nat)
1942 { 1950 {
1943 GNUNET_NAT_unregister(nat); 1951 GNUNET_NAT_unregister (nat);
1944 nat = NULL; 1952 nat = NULL;
1945 } 1953 }
1946 if (NULL != listen_task) 1954 if (NULL != listen_task)
1947 { 1955 {
1948 GNUNET_SCHEDULER_cancel(listen_task); 1956 GNUNET_SCHEDULER_cancel (listen_task);
1949 listen_task = NULL; 1957 listen_task = NULL;
1950 } 1958 }
1951 if (NULL != listen_sock) 1959 if (NULL != listen_sock)
1952 { 1960 {
1953 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(listen_sock)); 1961 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (listen_sock));
1954 listen_sock = NULL; 1962 listen_sock = NULL;
1955 } 1963 }
1956 GNUNET_CONTAINER_multipeermap_iterate(queue_map, &get_queue_delete_it, NULL); 1964 GNUNET_CONTAINER_multipeermap_iterate (queue_map, &get_queue_delete_it, NULL);
1957 GNUNET_CONTAINER_multipeermap_destroy(queue_map); 1965 GNUNET_CONTAINER_multipeermap_destroy (queue_map);
1958 if (NULL != ch) 1966 if (NULL != ch)
1959 { 1967 {
1960 GNUNET_TRANSPORT_communicator_disconnect(ch); 1968 GNUNET_TRANSPORT_communicator_disconnect (ch);
1961 ch = NULL; 1969 ch = NULL;
1962 } 1970 }
1963 if (NULL != stats) 1971 if (NULL != stats)
1964 { 1972 {
1965 GNUNET_STATISTICS_destroy(stats, GNUNET_NO); 1973 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1966 stats = NULL; 1974 stats = NULL;
1967 } 1975 }
1968 if (NULL != my_private_key) 1976 if (NULL != my_private_key)
1969 { 1977 {
1970 GNUNET_free(my_private_key); 1978 GNUNET_free (my_private_key);
1971 my_private_key = NULL; 1979 my_private_key = NULL;
1972 } 1980 }
1973 if (NULL != is) 1981 if (NULL != is)
1974 { 1982 {
1975 GNUNET_NT_scanner_done(is); 1983 GNUNET_NT_scanner_done (is);
1976 is = NULL; 1984 is = NULL;
1977 } 1985 }
1978} 1986}
1979 1987
1980 1988
@@ -1990,14 +1998,14 @@ do_shutdown(void *cls)
1990 * @param msg payload 1998 * @param msg payload
1991 */ 1999 */
1992static void 2000static void
1993enc_notify_cb(void *cls, 2001enc_notify_cb (void *cls,
1994 const struct GNUNET_PeerIdentity *sender, 2002 const struct GNUNET_PeerIdentity *sender,
1995 const struct GNUNET_MessageHeader *msg) 2003 const struct GNUNET_MessageHeader *msg)
1996{ 2004{
1997 (void)cls; 2005 (void) cls;
1998 (void)sender; 2006 (void) sender;
1999 (void)msg; 2007 (void) msg;
2000 GNUNET_break_op(0); 2008 GNUNET_break_op (0);
2001} 2009}
2002 2010
2003 2011
@@ -2015,39 +2023,39 @@ enc_notify_cb(void *cls,
2015 * @param addrlen actual length of the @a addr 2023 * @param addrlen actual length of the @a addr
2016 */ 2024 */
2017static void 2025static void
2018nat_address_cb(void *cls, 2026nat_address_cb (void *cls,
2019 void **app_ctx, 2027 void **app_ctx,
2020 int add_remove, 2028 int add_remove,
2021 enum GNUNET_NAT_AddressClass ac, 2029 enum GNUNET_NAT_AddressClass ac,
2022 const struct sockaddr *addr, 2030 const struct sockaddr *addr,
2023 socklen_t addrlen) 2031 socklen_t addrlen)
2024{ 2032{
2025 char *my_addr; 2033 char *my_addr;
2026 struct GNUNET_TRANSPORT_AddressIdentifier *ai; 2034 struct GNUNET_TRANSPORT_AddressIdentifier *ai;
2027 2035
2028 if (GNUNET_YES == add_remove) 2036 if (GNUNET_YES == add_remove)
2029 { 2037 {
2030 enum GNUNET_NetworkType nt; 2038 enum GNUNET_NetworkType nt;
2031 2039
2032 GNUNET_asprintf(&my_addr, 2040 GNUNET_asprintf (&my_addr,
2033 "%s-%s", 2041 "%s-%s",
2034 COMMUNICATOR_ADDRESS_PREFIX, 2042 COMMUNICATOR_ADDRESS_PREFIX,
2035 GNUNET_a2s(addr, addrlen)); 2043 GNUNET_a2s (addr, addrlen));
2036 nt = GNUNET_NT_scanner_get_type(is, addr, addrlen); 2044 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
2037 ai = 2045 ai =
2038 GNUNET_TRANSPORT_communicator_address_add(ch, 2046 GNUNET_TRANSPORT_communicator_address_add (ch,
2039 my_addr, 2047 my_addr,
2040 nt, 2048 nt,
2041 GNUNET_TIME_UNIT_FOREVER_REL); 2049 GNUNET_TIME_UNIT_FOREVER_REL);
2042 GNUNET_free(my_addr); 2050 GNUNET_free (my_addr);
2043 *app_ctx = ai; 2051 *app_ctx = ai;
2044 } 2052 }
2045 else 2053 else
2046 { 2054 {
2047 ai = *app_ctx; 2055 ai = *app_ctx;
2048 GNUNET_TRANSPORT_communicator_address_remove(ai); 2056 GNUNET_TRANSPORT_communicator_address_remove (ai);
2049 *app_ctx = NULL; 2057 *app_ctx = NULL;
2050 } 2058 }
2051} 2059}
2052 2060
2053 2061
@@ -2060,10 +2068,10 @@ nat_address_cb(void *cls,
2060 * @param c configuration 2068 * @param c configuration
2061 */ 2069 */
2062static void 2070static void
2063run(void *cls, 2071run (void *cls,
2064 char *const *args, 2072 char *const *args,
2065 const char *cfgfile, 2073 const char *cfgfile,
2066 const struct GNUNET_CONFIGURATION_Handle *c) 2074 const struct GNUNET_CONFIGURATION_Handle *c)
2067{ 2075{
2068 char *bindto; 2076 char *bindto;
2069 struct sockaddr *in; 2077 struct sockaddr *in;
@@ -2071,113 +2079,113 @@ run(void *cls,
2071 struct sockaddr_storage in_sto; 2079 struct sockaddr_storage in_sto;
2072 socklen_t sto_len; 2080 socklen_t sto_len;
2073 2081
2074 (void)cls; 2082 (void) cls;
2075 cfg = c; 2083 cfg = c;
2076 if (GNUNET_OK != 2084 if (GNUNET_OK !=
2077 GNUNET_CONFIGURATION_get_value_filename(cfg, 2085 GNUNET_CONFIGURATION_get_value_filename (cfg,
2078 COMMUNICATOR_CONFIG_SECTION, 2086 COMMUNICATOR_CONFIG_SECTION,
2079 "BINDTO", 2087 "BINDTO",
2080 &bindto)) 2088 &bindto))
2081 { 2089 {
2082 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, 2090 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2083 COMMUNICATOR_CONFIG_SECTION, 2091 COMMUNICATOR_CONFIG_SECTION,
2084 "BINDTO"); 2092 "BINDTO");
2085 return; 2093 return;
2086 } 2094 }
2087 if (GNUNET_OK != 2095 if (GNUNET_OK !=
2088 GNUNET_CONFIGURATION_get_value_number(cfg, 2096 GNUNET_CONFIGURATION_get_value_number (cfg,
2089 COMMUNICATOR_CONFIG_SECTION, 2097 COMMUNICATOR_CONFIG_SECTION,
2090 "MAX_QUEUE_LENGTH", 2098 "MAX_QUEUE_LENGTH",
2091 &max_queue_length)) 2099 &max_queue_length))
2092 max_queue_length = DEFAULT_MAX_QUEUE_LENGTH; 2100 max_queue_length = DEFAULT_MAX_QUEUE_LENGTH;
2093 2101
2094 in = tcp_address_to_sockaddr(bindto, &in_len); 2102 in = tcp_address_to_sockaddr (bindto, &in_len);
2095 if (NULL == in) 2103 if (NULL == in)
2096 { 2104 {
2097 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 2105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2098 "Failed to setup TCP socket address with path `%s'\n", 2106 "Failed to setup TCP socket address with path `%s'\n",
2099 bindto); 2107 bindto);
2100 GNUNET_free(bindto); 2108 GNUNET_free (bindto);
2101 return; 2109 return;
2102 } 2110 }
2103 listen_sock = 2111 listen_sock =
2104 GNUNET_NETWORK_socket_create(in->sa_family, SOCK_STREAM, IPPROTO_TCP); 2112 GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2105 if (NULL == listen_sock) 2113 if (NULL == listen_sock)
2106 { 2114 {
2107 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "socket"); 2115 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2108 GNUNET_free(in); 2116 GNUNET_free (in);
2109 GNUNET_free(bindto); 2117 GNUNET_free (bindto);
2110 return; 2118 return;
2111 } 2119 }
2112 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(listen_sock, in, in_len)) 2120 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, in, in_len))
2113 { 2121 {
2114 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "bind", bindto); 2122 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "bind", bindto);
2115 GNUNET_NETWORK_socket_close(listen_sock); 2123 GNUNET_NETWORK_socket_close (listen_sock);
2116 listen_sock = NULL; 2124 listen_sock = NULL;
2117 GNUNET_free(in); 2125 GNUNET_free (in);
2118 GNUNET_free(bindto); 2126 GNUNET_free (bindto);
2119 return; 2127 return;
2120 } 2128 }
2121 /* We might have bound to port 0, allowing the OS to figure it out; 2129 /* We might have bound to port 0, allowing the OS to figure it out;
2122 thus, get the real IN-address from the socket */ 2130 thus, get the real IN-address from the socket */
2123 sto_len = sizeof(in_sto); 2131 sto_len = sizeof(in_sto);
2124 if (0 != getsockname(GNUNET_NETWORK_get_fd(listen_sock), 2132 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
2125 (struct sockaddr *)&in_sto, 2133 (struct sockaddr *) &in_sto,
2126 &sto_len)) 2134 &sto_len))
2127 { 2135 {
2128 memcpy(&in_sto, in, in_len); 2136 memcpy (&in_sto, in, in_len);
2129 sto_len = in_len; 2137 sto_len = in_len;
2130 } 2138 }
2131 GNUNET_free(in); 2139 GNUNET_free (in);
2132 GNUNET_free(bindto); 2140 GNUNET_free (bindto);
2133 in = (struct sockaddr *)&in_sto; 2141 in = (struct sockaddr *) &in_sto;
2134 in_len = sto_len; 2142 in_len = sto_len;
2135 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 2143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2136 "Bound to `%s'\n", 2144 "Bound to `%s'\n",
2137 GNUNET_a2s((const struct sockaddr *)&in_sto, sto_len)); 2145 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
2138 stats = GNUNET_STATISTICS_create("C-TCP", cfg); 2146 stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
2139 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); 2147 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
2140 is = GNUNET_NT_scanner_init(); 2148 is = GNUNET_NT_scanner_init ();
2141 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg); 2149 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
2142 if (NULL == my_private_key) 2150 if (NULL == my_private_key)
2143 { 2151 {
2144 GNUNET_log( 2152 GNUNET_log (
2145 GNUNET_ERROR_TYPE_ERROR, 2153 GNUNET_ERROR_TYPE_ERROR,
2146 _( 2154 _ (
2147 "Transport service is lacking key configuration settings. Exiting.\n")); 2155 "Transport service is lacking key configuration settings. Exiting.\n"));
2148 GNUNET_SCHEDULER_shutdown(); 2156 GNUNET_SCHEDULER_shutdown ();
2149 return; 2157 return;
2150 } 2158 }
2151 GNUNET_CRYPTO_eddsa_key_get_public(my_private_key, &my_identity.public_key); 2159 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
2152 /* start listening */ 2160 /* start listening */
2153 listen_task = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL, 2161 listen_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2154 listen_sock, 2162 listen_sock,
2155 &listen_cb, 2163 &listen_cb,
2164 NULL);
2165 queue_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2166 ch = GNUNET_TRANSPORT_communicator_connect (cfg,
2167 COMMUNICATOR_CONFIG_SECTION,
2168 COMMUNICATOR_ADDRESS_PREFIX,
2169 GNUNET_TRANSPORT_CC_RELIABLE,
2170 &mq_init,
2171 NULL,
2172 &enc_notify_cb,
2156 NULL); 2173 NULL);
2157 queue_map = GNUNET_CONTAINER_multipeermap_create(10, GNUNET_NO);
2158 ch = GNUNET_TRANSPORT_communicator_connect(cfg,
2159 COMMUNICATOR_CONFIG_SECTION,
2160 COMMUNICATOR_ADDRESS_PREFIX,
2161 GNUNET_TRANSPORT_CC_RELIABLE,
2162 &mq_init,
2163 NULL,
2164 &enc_notify_cb,
2165 NULL);
2166 if (NULL == ch) 2174 if (NULL == ch)
2167 { 2175 {
2168 GNUNET_break(0); 2176 GNUNET_break (0);
2169 GNUNET_SCHEDULER_shutdown(); 2177 GNUNET_SCHEDULER_shutdown ();
2170 return; 2178 return;
2171 } 2179 }
2172 nat = GNUNET_NAT_register(cfg, 2180 nat = GNUNET_NAT_register (cfg,
2173 COMMUNICATOR_CONFIG_SECTION, 2181 COMMUNICATOR_CONFIG_SECTION,
2174 IPPROTO_TCP, 2182 IPPROTO_TCP,
2175 1 /* one address */, 2183 1 /* one address */,
2176 (const struct sockaddr **)&in, 2184 (const struct sockaddr **) &in,
2177 &in_len, 2185 &in_len,
2178 &nat_address_cb, 2186 &nat_address_cb,
2179 NULL /* FIXME: support reversal: #5529 */, 2187 NULL /* FIXME: support reversal: #5529 */,
2180 NULL /* closure */); 2188 NULL /* closure */);
2181} 2189}
2182 2190
2183 2191
@@ -2189,26 +2197,26 @@ run(void *cls,
2189 * @return 0 ok, 1 on error 2197 * @return 0 ok, 1 on error
2190 */ 2198 */
2191int 2199int
2192main(int argc, char *const *argv) 2200main (int argc, char *const *argv)
2193{ 2201{
2194 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 2202 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2195 GNUNET_GETOPT_OPTION_END 2203 GNUNET_GETOPT_OPTION_END
2196 }; 2204 };
2197 int ret; 2205 int ret;
2198 2206
2199 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 2207 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2200 return 2; 2208 return 2;
2201 2209
2202 ret = (GNUNET_OK == GNUNET_PROGRAM_run(argc, 2210 ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
2203 argv, 2211 argv,
2204 "gnunet-communicator-tcp", 2212 "gnunet-communicator-tcp",
2205 _("GNUnet TCP communicator"), 2213 _ ("GNUnet TCP communicator"),
2206 options, 2214 options,
2207 &run, 2215 &run,
2208 NULL)) 2216 NULL))
2209 ? 0 2217 ? 0
2210 : 1; 2218 : 1;
2211 GNUNET_free((void *)argv); 2219 GNUNET_free ((void *) argv);
2212 return ret; 2220 return ret;
2213} 2221}
2214 2222