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