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.c859
1 files changed, 341 insertions, 518 deletions
diff --git a/src/transport/gnunet-communicator-tcp.c b/src/transport/gnunet-communicator-tcp.c
index e9223401f..8ab99700c 100644
--- a/src/transport/gnunet-communicator-tcp.c
+++ b/src/transport/gnunet-communicator-tcp.c
@@ -46,7 +46,8 @@
46 * How long do we believe our addresses to remain up (before 46 * How long do we believe our addresses to remain up (before
47 * the other peer should revalidate). 47 * the other peer should revalidate).
48 */ 48 */
49#define ADDRESS_VALIDITY_PERIOD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4) 49#define ADDRESS_VALIDITY_PERIOD \
50 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
50 51
51/** 52/**
52 * 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
@@ -84,7 +85,9 @@
84 * Size of the initial key exchange message sent first in both 85 * Size of the initial key exchange message sent first in both
85 * directions. 86 * directions.
86 */ 87 */
87#define INITIAL_KX_SIZE (sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)+sizeof (struct TCPConfirmation)) 88#define INITIAL_KX_SIZE \
89 (sizeof (struct GNUNET_CRYPTO_EcdhePublicKey) + \
90 sizeof (struct TCPConfirmation))
88 91
89 92
90/** 93/**
@@ -154,7 +157,6 @@ struct TCPConfirmation
154 * (if receiver persists times by sender). 157 * (if receiver persists times by sender).
155 */ 158 */
156 struct GNUNET_TIME_AbsoluteNBO monotonic_time; 159 struct GNUNET_TIME_AbsoluteNBO monotonic_time;
157
158}; 160};
159 161
160 162
@@ -175,7 +177,7 @@ struct TCPBox
175 /** 177 /**
176 * HMAC for the following encrypted message. Yes, we MUST use 178 * HMAC for the following encrypted message. Yes, we MUST use
177 * mac-then-encrypt here, as we want to hide the message sizes on 179 * mac-then-encrypt here, as we want to hide the message sizes on
178 * the wire (zero plaintext design!). Using CTR mode padding oracle 180 * the wire (zero plaintext design!). Using CTR mode, padding oracle
179 * attacks do not apply. Besides, due to the use of ephemeral keys 181 * attacks do not apply. Besides, due to the use of ephemeral keys
180 * (hopefully with effective replay protection from monotonic time!) 182 * (hopefully with effective replay protection from monotonic time!)
181 * the attacker is limited in using the oracle. 183 * the attacker is limited in using the oracle.
@@ -184,7 +186,6 @@ struct TCPBox
184 186
185 /* followed by as may bytes of payload as indicated in @e header, 187 /* followed by as may bytes of payload as indicated in @e header,
186 excluding the TCPBox itself! */ 188 excluding the TCPBox itself! */
187
188}; 189};
189 190
190 191
@@ -225,7 +226,6 @@ struct TCPRekey
225 * (if receiver persists times by sender). 226 * (if receiver persists times by sender).
226 */ 227 */
227 struct GNUNET_TIME_AbsoluteNBO monotonic_time; 228 struct GNUNET_TIME_AbsoluteNBO monotonic_time;
228
229}; 229};
230 230
231 231
@@ -251,7 +251,6 @@ struct TCPFinish
251 * the attacker is limited in using the oracle. 251 * the attacker is limited in using the oracle.
252 */ 252 */
253 struct GNUNET_ShortHashCode hmac; 253 struct GNUNET_ShortHashCode hmac;
254
255}; 254};
256 255
257 256
@@ -296,7 +295,8 @@ struct Queue
296 struct GNUNET_HashCode out_hmac; 295 struct GNUNET_HashCode out_hmac;
297 296
298 /** 297 /**
299 * Our ephemeral key. Stored here temporarily during rekeying / key generation. 298 * Our ephemeral key. Stored here temporarily during rekeying / key
299 * generation.
300 */ 300 */
301 struct GNUNET_CRYPTO_EcdhePrivateKey ephemeral; 301 struct GNUNET_CRYPTO_EcdhePrivateKey ephemeral;
302 302
@@ -600,10 +600,9 @@ queue_destroy (struct Queue *queue)
600 GNUNET_TRANSPORT_communicator_mq_del (queue->qh); 600 GNUNET_TRANSPORT_communicator_mq_del (queue->qh);
601 queue->qh = NULL; 601 queue->qh = NULL;
602 } 602 }
603 GNUNET_assert (GNUNET_YES == 603 GNUNET_assert (
604 GNUNET_CONTAINER_multipeermap_remove (queue_map, 604 GNUNET_YES ==
605 &queue->target, 605 GNUNET_CONTAINER_multipeermap_remove (queue_map, &queue->target, queue));
606 queue));
607 GNUNET_STATISTICS_set (stats, 606 GNUNET_STATISTICS_set (stats,
608 "# queues active", 607 "# queues active",
609 GNUNET_CONTAINER_multipeermap_size (queue_map), 608 GNUNET_CONTAINER_multipeermap_size (queue_map),
@@ -656,9 +655,7 @@ calculate_hmac (struct GNUNET_HashCode *hmac_secret,
656 buf_size, 655 buf_size,
657 &mac); 656 &mac);
658 /* truncate to `struct GNUNET_ShortHashCode` */ 657 /* truncate to `struct GNUNET_ShortHashCode` */
659 memcpy (smac, 658 memcpy (smac, &mac, sizeof (struct GNUNET_ShortHashCode));
660 &mac,
661 sizeof (struct GNUNET_ShortHashCode));
662 /* ratchet hmac key */ 659 /* ratchet hmac key */
663 GNUNET_CRYPTO_hash (hmac_secret, 660 GNUNET_CRYPTO_hash (hmac_secret,
664 sizeof (struct GNUNET_HashCode), 661 sizeof (struct GNUNET_HashCode),
@@ -677,21 +674,14 @@ queue_finish (struct Queue *queue)
677{ 674{
678 struct TCPFinish fin; 675 struct TCPFinish fin;
679 676
680 memset (&fin, 677 memset (&fin, 0, sizeof (fin));
681 0,
682 sizeof (fin));
683 fin.header.size = htons (sizeof (fin)); 678 fin.header.size = htons (sizeof (fin));
684 fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH); 679 fin.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH);
685 calculate_hmac (&queue->out_hmac, 680 calculate_hmac (&queue->out_hmac, &fin, sizeof (fin), &fin.hmac);
686 &fin,
687 sizeof (fin),
688 &fin.hmac);
689 /* if there is any message left in pwrite_buf, we 681 /* if there is any message left in pwrite_buf, we
690 overwrite it (possibly dropping the last message 682 overwrite it (possibly dropping the last message
691 from CORE hard here) */ 683 from CORE hard here) */
692 memcpy (queue->pwrite_buf, 684 memcpy (queue->pwrite_buf, &fin, sizeof (fin));
693 &fin,
694 sizeof (fin));
695 queue->pwrite_off = sizeof (fin); 685 queue->pwrite_off = sizeof (fin);
696 /* This flag will ensure that #queue_write() no longer 686 /* This flag will ensure that #queue_write() no longer
697 notifies CORE about the possibility of sending 687 notifies CORE about the possibility of sending
@@ -711,8 +701,8 @@ queue_finish (struct Queue *queue)
711static void 701static void
712reschedule_queue_timeout (struct Queue *queue) 702reschedule_queue_timeout (struct Queue *queue)
713{ 703{
714 queue->timeout 704 queue->timeout =
715 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 705 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
716} 706}
717 707
718 708
@@ -733,8 +723,7 @@ queue_read (void *cls);
733 * @param success #GNUNET_OK on success 723 * @param success #GNUNET_OK on success
734 */ 724 */
735static void 725static void
736core_read_finished_cb (void *cls, 726core_read_finished_cb (void *cls, int success)
737 int success)
738{ 727{
739 struct Queue *queue = cls; 728 struct Queue *queue = cls;
740 729
@@ -745,8 +734,7 @@ core_read_finished_cb (void *cls,
745 GNUNET_NO); 734 GNUNET_NO);
746 queue->backpressure--; 735 queue->backpressure--;
747 /* handle deferred queue destruction */ 736 /* handle deferred queue destruction */
748 if ( (queue->destroyed) && 737 if ((queue->destroyed) && (0 == queue->backpressure))
749 (0 == queue->backpressure) )
750 { 738 {
751 GNUNET_free (queue); 739 GNUNET_free (queue);
752 return; 740 return;
@@ -754,11 +742,12 @@ core_read_finished_cb (void *cls,
754 reschedule_queue_timeout (queue); 742 reschedule_queue_timeout (queue);
755 /* possibly unchoke reading, now that CORE made progress */ 743 /* possibly unchoke reading, now that CORE made progress */
756 if (NULL == queue->read_task) 744 if (NULL == queue->read_task)
757 queue->read_task 745 queue->read_task =
758 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (queue->timeout), 746 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (
759 queue->sock, 747 queue->timeout),
760 &queue_read, 748 queue->sock,
761 queue); 749 &queue_read,
750 queue);
762} 751}
763 752
764 753
@@ -818,39 +807,35 @@ setup_cipher (const struct GNUNET_HashCode *dh,
818 gcry_cipher_hd_t *cipher, 807 gcry_cipher_hd_t *cipher,
819 struct GNUNET_HashCode *hmac_key) 808 struct GNUNET_HashCode *hmac_key)
820{ 809{
821 char key[256/8]; 810 char key[256 / 8];
822 char ctr[128/8]; 811 char ctr[128 / 8];
823 812
824 gcry_cipher_open (cipher, 813 gcry_cipher_open (cipher,
825 GCRY_CIPHER_AES256 /* low level: go for speed */, 814 GCRY_CIPHER_AES256 /* low level: go for speed */,
826 GCRY_CIPHER_MODE_CTR, 815 GCRY_CIPHER_MODE_CTR,
827 0 /* flags */); 816 0 /* flags */);
828 GNUNET_assert (GNUNET_YES == 817 GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_kdf (key,
829 GNUNET_CRYPTO_kdf (key, 818 sizeof (key),
830 sizeof (key), 819 "TCP-key",
831 "TCP-key", 820 strlen ("TCP-key"),
832 strlen ("TCP-key"), 821 dh,
833 dh, 822 sizeof (*dh),
834 sizeof (*dh), 823 pid,
835 pid, 824 sizeof (*pid),
836 sizeof (*pid), 825 NULL,
837 NULL, 0)); 826 0));
838 gcry_cipher_setkey (*cipher, 827 gcry_cipher_setkey (*cipher, key, sizeof (key));
839 key, 828 GNUNET_assert (GNUNET_YES == GNUNET_CRYPTO_kdf (ctr,
840 sizeof (key)); 829 sizeof (ctr),
841 GNUNET_assert (GNUNET_YES == 830 "TCP-ctr",
842 GNUNET_CRYPTO_kdf (ctr, 831 strlen ("TCP-ctr"),
843 sizeof (ctr), 832 dh,
844 "TCP-ctr", 833 sizeof (*dh),
845 strlen ("TCP-ctr"), 834 pid,
846 dh, 835 sizeof (*pid),
847 sizeof (*dh), 836 NULL,
848 pid, 837 0));
849 sizeof (*pid), 838 gcry_cipher_setctr (*cipher, ctr, sizeof (ctr));
850 NULL, 0));
851 gcry_cipher_setctr (*cipher,
852 ctr,
853 sizeof (ctr));
854 GNUNET_assert (GNUNET_YES == 839 GNUNET_assert (GNUNET_YES ==
855 GNUNET_CRYPTO_kdf (hmac_key, 840 GNUNET_CRYPTO_kdf (hmac_key,
856 sizeof (struct GNUNET_HashCode), 841 sizeof (struct GNUNET_HashCode),
@@ -860,7 +845,8 @@ setup_cipher (const struct GNUNET_HashCode *dh,
860 sizeof (*dh), 845 sizeof (*dh),
861 pid, 846 pid,
862 sizeof (*pid), 847 sizeof (*pid),
863 NULL, 0)); 848 NULL,
849 0));
864} 850}
865 851
866 852
@@ -876,13 +862,8 @@ setup_in_cipher (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
876{ 862{
877 struct GNUNET_HashCode dh; 863 struct GNUNET_HashCode dh;
878 864
879 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, 865 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &dh);
880 ephemeral, 866 setup_cipher (&dh, &my_identity, &queue->in_cipher, &queue->in_hmac);
881 &dh);
882 setup_cipher (&dh,
883 &my_identity,
884 &queue->in_cipher,
885 &queue->in_hmac);
886} 867}
887 868
888 869
@@ -895,8 +876,7 @@ setup_in_cipher (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
895 * @param rekey the rekey message 876 * @param rekey the rekey message
896 */ 877 */
897static void 878static void
898do_rekey (struct Queue *queue, 879do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
899 const struct TCPRekey *rekey)
900{ 880{
901 struct TcpHandshakeSignature thp; 881 struct TcpHandshakeSignature thp;
902 882
@@ -918,8 +898,7 @@ do_rekey (struct Queue *queue,
918 } 898 }
919 gcry_cipher_close (queue->in_cipher); 899 gcry_cipher_close (queue->in_cipher);
920 queue->rekeyed = GNUNET_YES; 900 queue->rekeyed = GNUNET_YES;
921 setup_in_cipher (&rekey->ephemeral, 901 setup_in_cipher (&rekey->ephemeral, queue);
922 queue);
923} 902}
924 903
925 904
@@ -933,14 +912,11 @@ do_rekey (struct Queue *queue,
933static size_t 912static size_t
934try_handle_plaintext (struct Queue *queue) 913try_handle_plaintext (struct Queue *queue)
935{ 914{
936 const struct GNUNET_MessageHeader *hdr 915 const struct GNUNET_MessageHeader *hdr =
937 = (const struct GNUNET_MessageHeader *) queue->pread_buf; 916 (const struct GNUNET_MessageHeader *) queue->pread_buf;
938 const struct TCPBox *box 917 const struct TCPBox *box = (const struct TCPBox *) queue->pread_buf;
939 = (const struct TCPBox *) queue->pread_buf; 918 const struct TCPRekey *rekey = (const struct TCPRekey *) queue->pread_buf;
940 const struct TCPRekey *rekey 919 const struct TCPFinish *fin = (const struct TCPFinish *) queue->pread_buf;
941 = (const struct TCPRekey *) queue->pread_buf;
942 const struct TCPFinish *fin
943 = (const struct TCPFinish *) queue->pread_buf;
944 struct TCPRekey rekeyz; 920 struct TCPRekey rekeyz;
945 struct TCPFinish finz; 921 struct TCPFinish finz;
946 struct GNUNET_ShortHashCode tmac; 922 struct GNUNET_ShortHashCode tmac;
@@ -956,21 +932,14 @@ try_handle_plaintext (struct Queue *queue)
956 /* Special case: header size excludes box itself! */ 932 /* Special case: header size excludes box itself! */
957 if (ntohs (hdr->size) + sizeof (struct TCPBox) > queue->pread_off) 933 if (ntohs (hdr->size) + sizeof (struct TCPBox) > queue->pread_off)
958 return 0; 934 return 0;
959 calculate_hmac (&queue->in_hmac, 935 calculate_hmac (&queue->in_hmac, &box[1], ntohs (hdr->size), &tmac);
960 &box[1], 936 if (0 != memcmp (&tmac, &box->hmac, sizeof (tmac)))
961 ntohs (hdr->size),
962 &tmac);
963 if (0 != memcmp (&tmac,
964 &box->hmac,
965 sizeof (tmac)))
966 { 937 {
967 GNUNET_break_op (0); 938 GNUNET_break_op (0);
968 queue_finish (queue); 939 queue_finish (queue);
969 return 0; 940 return 0;
970 } 941 }
971 pass_plaintext_to_core (queue, 942 pass_plaintext_to_core (queue, (const void *) &box[1], ntohs (hdr->size));
972 (const void *) &box[1],
973 ntohs (hdr->size));
974 size = ntohs (hdr->size) + sizeof (*box); 943 size = ntohs (hdr->size) + sizeof (*box);
975 break; 944 break;
976 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY: 945 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY:
@@ -983,23 +952,15 @@ try_handle_plaintext (struct Queue *queue)
983 return 0; 952 return 0;
984 } 953 }
985 rekeyz = *rekey; 954 rekeyz = *rekey;
986 memset (&rekeyz.hmac, 955 memset (&rekeyz.hmac, 0, sizeof (rekeyz.hmac));
987 0, 956 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof (rekeyz), &tmac);
988 sizeof (rekeyz.hmac)); 957 if (0 != memcmp (&tmac, &box->hmac, sizeof (tmac)))
989 calculate_hmac (&queue->in_hmac,
990 &rekeyz,
991 sizeof (rekeyz),
992 &tmac);
993 if (0 != memcmp (&tmac,
994 &box->hmac,
995 sizeof (tmac)))
996 { 958 {
997 GNUNET_break_op (0); 959 GNUNET_break_op (0);
998 queue_finish (queue); 960 queue_finish (queue);
999 return 0; 961 return 0;
1000 } 962 }
1001 do_rekey (queue, 963 do_rekey (queue, rekey);
1002 rekey);
1003 size = ntohs (hdr->size); 964 size = ntohs (hdr->size);
1004 break; 965 break;
1005 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH: 966 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_FINISH:
@@ -1012,16 +973,9 @@ try_handle_plaintext (struct Queue *queue)
1012 return 0; 973 return 0;
1013 } 974 }
1014 finz = *fin; 975 finz = *fin;
1015 memset (&finz.hmac, 976 memset (&finz.hmac, 0, sizeof (finz.hmac));
1016 0, 977 calculate_hmac (&queue->in_hmac, &rekeyz, sizeof (rekeyz), &tmac);
1017 sizeof (finz.hmac)); 978 if (0 != memcmp (&tmac, &fin->hmac, sizeof (tmac)))
1018 calculate_hmac (&queue->in_hmac,
1019 &rekeyz,
1020 sizeof (rekeyz),
1021 &tmac);
1022 if (0 != memcmp (&tmac,
1023 &fin->hmac,
1024 sizeof (tmac)))
1025 { 979 {
1026 GNUNET_break_op (0); 980 GNUNET_break_op (0);
1027 queue_finish (queue); 981 queue_finish (queue);
@@ -1058,27 +1012,22 @@ queue_read (void *cls)
1058 BUF_SIZE - queue->cread_off); 1012 BUF_SIZE - queue->cread_off);
1059 if (-1 == rcvd) 1013 if (-1 == rcvd)
1060 { 1014 {
1061 if ( (EAGAIN != errno) && 1015 if ((EAGAIN != errno) && (EINTR != errno))
1062 (EINTR != errno) )
1063 { 1016 {
1064 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, 1017 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
1065 "recv");
1066 queue_finish (queue); 1018 queue_finish (queue);
1067 return; 1019 return;
1068 } 1020 }
1069 /* try again */ 1021 /* try again */
1070 queue->read_task 1022 queue->read_task =
1071 = GNUNET_SCHEDULER_add_read_net (left, 1023 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1072 queue->sock,
1073 &queue_read,
1074 queue);
1075 return; 1024 return;
1076 } 1025 }
1077 if (0 != rcvd) 1026 if (0 != rcvd)
1078 reschedule_queue_timeout (queue); 1027 reschedule_queue_timeout (queue);
1079 queue->cread_off += rcvd; 1028 queue->cread_off += rcvd;
1080 while ( (queue->pread_off < sizeof (queue->pread_buf)) && 1029 while ((queue->pread_off < sizeof (queue->pread_buf)) &&
1081 (queue->cread_off > 0) ) 1030 (queue->cread_off > 0))
1082 { 1031 {
1083 size_t max = GNUNET_MIN (sizeof (queue->pread_buf) - queue->pread_off, 1032 size_t max = GNUNET_MIN (sizeof (queue->pread_buf) - queue->pread_off,
1084 queue->cread_off); 1033 queue->cread_off);
@@ -1093,14 +1042,14 @@ queue_read (void *cls)
1093 max)); 1042 max));
1094 queue->pread_off += max; 1043 queue->pread_off += max;
1095 total = 0; 1044 total = 0;
1096 while ( (GNUNET_NO == queue->rekeyed) && 1045 while ((GNUNET_NO == queue->rekeyed) &&
1097 (0 != (done = try_handle_plaintext (queue))) ) 1046 (0 != (done = try_handle_plaintext (queue))))
1098 { 1047 {
1099 /* 'done' bytes of plaintext were used, shift buffer */ 1048 /* 'done' bytes of plaintext were used, shift buffer */
1100 GNUNET_assert (done <= queue->pread_off); 1049 GNUNET_assert (done <= queue->pread_off);
1101 /* NOTE: this memmove() could possibly sometimes be 1050 /* NOTE: this memmove() could possibly sometimes be
1102 avoided if we pass 'total' into try_handle_plaintext() 1051 avoided if we pass 'total' into try_handle_plaintext()
1103 and use it at an offset into the buffer there! */ 1052 and use it at an offset into the buffer there! */
1104 memmove (queue->pread_buf, 1053 memmove (queue->pread_buf,
1105 &queue->pread_buf[done], 1054 &queue->pread_buf[done],
1106 queue->pread_off - done); 1055 queue->pread_off - done);
@@ -1117,9 +1066,7 @@ queue_read (void *cls)
1117 max = total; 1066 max = total;
1118 queue->rekeyed = GNUNET_NO; 1067 queue->rekeyed = GNUNET_NO;
1119 } 1068 }
1120 memmove (queue->cread_buf, 1069 memmove (queue->cread_buf, &queue->cread_buf[max], queue->cread_off - max);
1121 &queue->cread_buf[max],
1122 queue->cread_off - max);
1123 queue->cread_off -= max; 1070 queue->cread_off -= max;
1124 } 1071 }
1125 1072
@@ -1131,19 +1078,17 @@ queue_read (void *cls)
1131 if (max_queue_length < queue->backpressure) 1078 if (max_queue_length < queue->backpressure)
1132 { 1079 {
1133 /* continue reading */ 1080 /* continue reading */
1134 queue->read_task 1081 queue->read_task =
1135 = GNUNET_SCHEDULER_add_read_net (left, 1082 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read, queue);
1136 queue->sock,
1137 &queue_read,
1138 queue);
1139 } 1083 }
1140 return; 1084 return;
1141 } 1085 }
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Queue %p was idle for %s, disconnecting\n", 1087 "Queue %p was idle for %s, disconnecting\n",
1144 queue, 1088 queue,
1145 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 1089 GNUNET_STRINGS_relative_time_to_string (
1146 GNUNET_YES)); 1090 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1091 GNUNET_YES));
1147 queue_finish (queue); 1092 queue_finish (queue);
1148} 1093}
1149 1094
@@ -1156,8 +1101,7 @@ queue_read (void *cls)
1156 * @return converted bindto specification 1101 * @return converted bindto specification
1157 */ 1102 */
1158static struct sockaddr * 1103static struct sockaddr *
1159tcp_address_to_sockaddr (const char *bindto, 1104tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1160 socklen_t *sock_len)
1161{ 1105{
1162 struct sockaddr *in; 1106 struct sockaddr *in;
1163 unsigned int port; 1107 unsigned int port;
@@ -1165,10 +1109,7 @@ tcp_address_to_sockaddr (const char *bindto,
1165 char *colon; 1109 char *colon;
1166 char *cp; 1110 char *cp;
1167 1111
1168 if (1 == SSCANF (bindto, 1112 if (1 == SSCANF (bindto, "%u%1s", &port, dummy))
1169 "%u%1s",
1170 &port,
1171 dummy))
1172 { 1113 {
1173 /* interpreting value as just a PORT number */ 1114 /* interpreting value as just a PORT number */
1174 if (port > UINT16_MAX) 1115 if (port > UINT16_MAX)
@@ -1178,12 +1119,11 @@ tcp_address_to_sockaddr (const char *bindto,
1178 bindto); 1119 bindto);
1179 return NULL; 1120 return NULL;
1180 } 1121 }
1181 if ( (GNUNET_NO == 1122 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1182 GNUNET_NETWORK_test_pf (PF_INET6)) || 1123 (GNUNET_YES ==
1183 (GNUNET_YES == 1124 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1184 GNUNET_CONFIGURATION_get_value_yesno (cfg, 1125 COMMUNICATOR_CONFIG_SECTION,
1185 COMMUNICATOR_CONFIG_SECTION, 1126 "DISABLE_V6")))
1186 "DISABLE_V6")) )
1187 { 1127 {
1188 struct sockaddr_in *i4; 1128 struct sockaddr_in *i4;
1189 1129
@@ -1212,10 +1152,7 @@ tcp_address_to_sockaddr (const char *bindto,
1212 /* interpet value after colon as port */ 1152 /* interpet value after colon as port */
1213 *colon = '\0'; 1153 *colon = '\0';
1214 colon++; 1154 colon++;
1215 if (1 == SSCANF (colon, 1155 if (1 == SSCANF (colon, "%u%1s", &port, dummy))
1216 "%u%1s",
1217 &port,
1218 dummy))
1219 { 1156 {
1220 /* interpreting value as just a PORT number */ 1157 /* interpreting value as just a PORT number */
1221 if (port > UINT16_MAX) 1158 if (port > UINT16_MAX)
@@ -1229,9 +1166,10 @@ tcp_address_to_sockaddr (const char *bindto,
1229 } 1166 }
1230 else 1167 else
1231 { 1168 {
1232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1169 GNUNET_log (
1233 "BINDTO specification `%s' invalid: last ':' not followed by number\n", 1170 GNUNET_ERROR_TYPE_ERROR,
1234 bindto); 1171 "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1172 bindto);
1235 GNUNET_free (cp); 1173 GNUNET_free (cp);
1236 return NULL; 1174 return NULL;
1237 } 1175 }
@@ -1245,13 +1183,10 @@ tcp_address_to_sockaddr (const char *bindto,
1245 /* try IPv4 */ 1183 /* try IPv4 */
1246 struct sockaddr_in v4; 1184 struct sockaddr_in v4;
1247 1185
1248 if (1 == inet_pton (AF_INET, 1186 if (1 == inet_pton (AF_INET, cp, &v4))
1249 cp,
1250 &v4))
1251 { 1187 {
1252 v4.sin_port = htons ((uint16_t) port); 1188 v4.sin_port = htons ((uint16_t) port);
1253 in = GNUNET_memdup (&v4, 1189 in = GNUNET_memdup (&v4, sizeof (v4));
1254 sizeof (v4));
1255 *sock_len = sizeof (v4); 1190 *sock_len = sizeof (v4);
1256 GNUNET_free (cp); 1191 GNUNET_free (cp);
1257 return in; 1192 return in;
@@ -1263,19 +1198,15 @@ tcp_address_to_sockaddr (const char *bindto,
1263 const char *start; 1198 const char *start;
1264 1199
1265 start = cp; 1200 start = cp;
1266 if ( ('[' == *cp) && 1201 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1267 (']' == cp[strlen (cp)-1]) )
1268 { 1202 {
1269 start++; /* skip over '[' */ 1203 start++; /* skip over '[' */
1270 cp[strlen (cp) -1] = '\0'; /* eat ']' */ 1204 cp[strlen (cp) - 1] = '\0'; /* eat ']' */
1271 } 1205 }
1272 if (1 == inet_pton (AF_INET6, 1206 if (1 == inet_pton (AF_INET6, start, &v6))
1273 start,
1274 &v6))
1275 { 1207 {
1276 v6.sin6_port = htons ((uint16_t) port); 1208 v6.sin6_port = htons ((uint16_t) port);
1277 in = GNUNET_memdup (&v6, 1209 in = GNUNET_memdup (&v6, sizeof (v6));
1278 sizeof (v6));
1279 *sock_len = sizeof (v6); 1210 *sock_len = sizeof (v6);
1280 GNUNET_free (cp); 1211 GNUNET_free (cp);
1281 return in; 1212 return in;
@@ -1298,20 +1229,13 @@ setup_out_cipher (struct Queue *queue)
1298{ 1229{
1299 struct GNUNET_HashCode dh; 1230 struct GNUNET_HashCode dh;
1300 1231
1301 GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, 1232 GNUNET_CRYPTO_ecdh_eddsa (&queue->ephemeral, &queue->target.public_key, &dh);
1302 &queue->target.public_key,
1303 &dh);
1304 /* we don't need the private key anymore, drop it! */ 1233 /* we don't need the private key anymore, drop it! */
1305 memset (&queue->ephemeral, 1234 memset (&queue->ephemeral, 0, sizeof (queue->ephemeral));
1306 0, 1235 setup_cipher (&dh, &queue->target, &queue->out_cipher, &queue->out_hmac);
1307 sizeof (queue->ephemeral));
1308 setup_cipher (&dh,
1309 &queue->target,
1310 &queue->out_cipher,
1311 &queue->out_hmac);
1312 queue->rekey_time = GNUNET_TIME_relative_to_absolute (REKEY_TIME_INTERVAL); 1236 queue->rekey_time = GNUNET_TIME_relative_to_absolute (REKEY_TIME_INTERVAL);
1313 queue->rekey_left_bytes = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, 1237 queue->rekey_left_bytes =
1314 REKEY_MAX_BYTES); 1238 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, REKEY_MAX_BYTES);
1315} 1239}
1316 1240
1317 1241
@@ -1328,33 +1252,25 @@ inject_rekey (struct Queue *queue)
1328 struct TcpHandshakeSignature thp; 1252 struct TcpHandshakeSignature thp;
1329 1253
1330 GNUNET_assert (0 == queue->pwrite_off); 1254 GNUNET_assert (0 == queue->pwrite_off);
1331 memset (&rekey, 1255 memset (&rekey, 0, sizeof (rekey));
1332 0,
1333 sizeof (rekey));
1334 GNUNET_assert (GNUNET_OK == 1256 GNUNET_assert (GNUNET_OK ==
1335 GNUNET_CRYPTO_ecdhe_key_create2 (&queue->ephemeral)); 1257 GNUNET_CRYPTO_ecdhe_key_create2 (&queue->ephemeral));
1336 rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY); 1258 rekey.header.type = ntohs (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_REKEY);
1337 rekey.header.size = ntohs (sizeof (rekey)); 1259 rekey.header.size = ntohs (sizeof (rekey));
1338 GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, 1260 GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &rekey.ephemeral);
1339 &rekey.ephemeral); 1261 rekey.monotonic_time =
1340 rekey.monotonic_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg)); 1262 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
1341 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY); 1263 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY);
1342 thp.purpose.size = htonl (sizeof (thp)); 1264 thp.purpose.size = htonl (sizeof (thp));
1343 thp.sender = my_identity; 1265 thp.sender = my_identity;
1344 thp.receiver = queue->target; 1266 thp.receiver = queue->target;
1345 thp.ephemeral = rekey.ephemeral; 1267 thp.ephemeral = rekey.ephemeral;
1346 thp.monotonic_time = rekey.monotonic_time; 1268 thp.monotonic_time = rekey.monotonic_time;
1347 GNUNET_assert (GNUNET_OK == 1269 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key,
1348 GNUNET_CRYPTO_eddsa_sign (my_private_key, 1270 &thp.purpose,
1349 &thp.purpose, 1271 &rekey.sender_sig));
1350 &rekey.sender_sig)); 1272 calculate_hmac (&queue->out_hmac, &rekey, sizeof (rekey), &rekey.hmac);
1351 calculate_hmac (&queue->out_hmac, 1273 memcpy (queue->pwrite_buf, &rekey, sizeof (rekey));
1352 &rekey,
1353 sizeof (rekey),
1354 &rekey.hmac);
1355 memcpy (queue->pwrite_buf,
1356 &rekey,
1357 sizeof (rekey));
1358 queue->rekey_state = GNUNET_YES; 1274 queue->rekey_state = GNUNET_YES;
1359} 1275}
1360 1276
@@ -1388,12 +1304,9 @@ queue_write (void *cls)
1388 sent = GNUNET_NETWORK_socket_send (queue->sock, 1304 sent = GNUNET_NETWORK_socket_send (queue->sock,
1389 queue->cwrite_buf, 1305 queue->cwrite_buf,
1390 queue->cwrite_off); 1306 queue->cwrite_off);
1391 if ( (-1 == sent) && 1307 if ((-1 == sent) && (EAGAIN != errno) && (EINTR != errno))
1392 (EAGAIN != errno) &&
1393 (EINTR != errno) )
1394 { 1308 {
1395 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 1309 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
1396 "send");
1397 queue_destroy (queue); 1310 queue_destroy (queue);
1398 return; 1311 return;
1399 } 1312 }
@@ -1405,7 +1318,7 @@ queue_write (void *cls)
1405 &queue->cwrite_buf[usent], 1318 &queue->cwrite_buf[usent],
1406 queue->cwrite_off - usent); 1319 queue->cwrite_off - usent);
1407 reschedule_queue_timeout (queue); 1320 reschedule_queue_timeout (queue);
1408 } 1321 }
1409 /* can we encrypt more? (always encrypt full messages, needed 1322 /* can we encrypt more? (always encrypt full messages, needed
1410 such that #mq_cancel() can work!) */ 1323 such that #mq_cancel() can work!) */
1411 if (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE) 1324 if (queue->cwrite_off + queue->pwrite_off <= BUF_SIZE)
@@ -1423,34 +1336,32 @@ queue_write (void *cls)
1423 queue->cwrite_off += queue->pwrite_off; 1336 queue->cwrite_off += queue->pwrite_off;
1424 queue->pwrite_off = 0; 1337 queue->pwrite_off = 0;
1425 } 1338 }
1426 if ( (GNUNET_YES == queue->rekey_state) && 1339 if ((GNUNET_YES == queue->rekey_state) && (0 == queue->pwrite_off))
1427 (0 == queue->pwrite_off) )
1428 switch_key (queue); 1340 switch_key (queue);
1429 if ( (0 == queue->pwrite_off) && 1341 if ((0 == queue->pwrite_off) &&
1430 ( (0 == queue->rekey_left_bytes) || 1342 ((0 == queue->rekey_left_bytes) ||
1431 (0 == GNUNET_TIME_absolute_get_remaining (queue->rekey_time).rel_value_us) ) ) 1343 (0 ==
1344 GNUNET_TIME_absolute_get_remaining (queue->rekey_time).rel_value_us)))
1432 inject_rekey (queue); 1345 inject_rekey (queue);
1433 if ( (0 == queue->pwrite_off) && 1346 if ((0 == queue->pwrite_off) && (! queue->finishing) &&
1434 (! queue->finishing) && 1347 (queue->mq_awaits_continue))
1435 (queue->mq_awaits_continue) )
1436 { 1348 {
1437 queue->mq_awaits_continue = GNUNET_NO; 1349 queue->mq_awaits_continue = GNUNET_NO;
1438 GNUNET_MQ_impl_send_continue (queue->mq); 1350 GNUNET_MQ_impl_send_continue (queue->mq);
1439 } 1351 }
1440 /* did we just finish writing 'finish'? */ 1352 /* did we just finish writing 'finish'? */
1441 if ( (0 == queue->cwrite_off) && 1353 if ((0 == queue->cwrite_off) && (GNUNET_YES == queue->finishing))
1442 (GNUNET_YES == queue->finishing) )
1443 { 1354 {
1444 queue_destroy (queue); 1355 queue_destroy (queue);
1445 return; 1356 return;
1446 } 1357 }
1447 /* do we care to write more? */ 1358 /* do we care to write more? */
1448 if (0 < queue->cwrite_off) 1359 if (0 < queue->cwrite_off)
1449 queue->write_task 1360 queue->write_task =
1450 = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL, 1361 GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
1451 queue->sock, 1362 queue->sock,
1452 &queue_write, 1363 &queue_write,
1453 queue); 1364 queue);
1454} 1365}
1455 1366
1456 1367
@@ -1477,17 +1388,10 @@ mq_send (struct GNUNET_MQ_Handle *mq,
1477 GNUNET_assert (0 == queue->pread_off); 1388 GNUNET_assert (0 == queue->pread_off);
1478 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX); 1389 box.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_TCP_BOX);
1479 box.header.size = htons (msize); 1390 box.header.size = htons (msize);
1480 calculate_hmac (&queue->out_hmac, 1391 calculate_hmac (&queue->out_hmac, msg, msize, &box.hmac);
1481 msg, 1392 memcpy (&queue->pread_buf[queue->pread_off], &box, sizeof (box));
1482 msize,
1483 &box.hmac);
1484 memcpy (&queue->pread_buf[queue->pread_off],
1485 &box,
1486 sizeof (box));
1487 queue->pread_off += sizeof (box); 1393 queue->pread_off += sizeof (box);
1488 memcpy (&queue->pread_buf[queue->pread_off], 1394 memcpy (&queue->pread_buf[queue->pread_off], msg, msize);
1489 msg,
1490 msize);
1491 queue->pread_off += msize; 1395 queue->pread_off += msize;
1492 GNUNET_assert (NULL != queue->sock); 1396 GNUNET_assert (NULL != queue->sock);
1493 if (NULL == queue->write_task) 1397 if (NULL == queue->write_task)
@@ -1508,8 +1412,7 @@ mq_send (struct GNUNET_MQ_Handle *mq,
1508 * @param impl_state our `struct Queue` 1412 * @param impl_state our `struct Queue`
1509 */ 1413 */
1510static void 1414static void
1511mq_destroy (struct GNUNET_MQ_Handle *mq, 1415mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
1512 void *impl_state)
1513{ 1416{
1514 struct Queue *queue = impl_state; 1417 struct Queue *queue = impl_state;
1515 1418
@@ -1528,8 +1431,7 @@ mq_destroy (struct GNUNET_MQ_Handle *mq,
1528 * @param impl_state our `struct Queue` 1431 * @param impl_state our `struct Queue`
1529 */ 1432 */
1530static void 1433static void
1531mq_cancel (struct GNUNET_MQ_Handle *mq, 1434mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
1532 void *impl_state)
1533{ 1435{
1534 struct Queue *queue = impl_state; 1436 struct Queue *queue = impl_state;
1535 1437
@@ -1548,8 +1450,7 @@ mq_cancel (struct GNUNET_MQ_Handle *mq,
1548 * @param error error code 1450 * @param error error code
1549 */ 1451 */
1550static void 1452static void
1551mq_error (void *cls, 1453mq_error (void *cls, enum GNUNET_MQ_Error error)
1552 enum GNUNET_MQ_Error error)
1553{ 1454{
1554 struct Queue *queue = cls; 1455 struct Queue *queue = cls;
1555 1456
@@ -1569,30 +1470,28 @@ mq_error (void *cls,
1569 * @param queue queue to boot 1470 * @param queue queue to boot
1570 */ 1471 */
1571static void 1472static void
1572boot_queue (struct Queue *queue, 1473boot_queue (struct Queue *queue, enum GNUNET_TRANSPORT_ConnectionStatus cs)
1573 enum GNUNET_TRANSPORT_ConnectionStatus cs)
1574{ 1474{
1575 queue->nt = GNUNET_NT_scanner_get_type (is, 1475 queue->nt =
1576 queue->address, 1476 GNUNET_NT_scanner_get_type (is, queue->address, queue->address_len);
1577 queue->address_len); 1477 (void) GNUNET_CONTAINER_multipeermap_put (
1578 (void) GNUNET_CONTAINER_multipeermap_put (queue_map, 1478 queue_map,
1579 &queue->target, 1479 &queue->target,
1580 queue, 1480 queue,
1581 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 1481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1582 GNUNET_STATISTICS_set (stats, 1482 GNUNET_STATISTICS_set (stats,
1583 "# queues active", 1483 "# queues active",
1584 GNUNET_CONTAINER_multipeermap_size (queue_map), 1484 GNUNET_CONTAINER_multipeermap_size (queue_map),
1585 GNUNET_NO); 1485 GNUNET_NO);
1586 queue->timeout 1486 queue->timeout =
1587 = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 1487 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1588 queue->mq 1488 queue->mq = GNUNET_MQ_queue_for_callbacks (&mq_send,
1589 = GNUNET_MQ_queue_for_callbacks (&mq_send, 1489 &mq_destroy,
1590 &mq_destroy, 1490 &mq_cancel,
1591 &mq_cancel, 1491 queue,
1592 queue, 1492 NULL,
1593 NULL, 1493 &mq_error,
1594 &mq_error, 1494 queue);
1595 queue);
1596 { 1495 {
1597 char *foreign_addr; 1496 char *foreign_addr;
1598 1497
@@ -1602,27 +1501,24 @@ boot_queue (struct Queue *queue,
1602 GNUNET_asprintf (&foreign_addr, 1501 GNUNET_asprintf (&foreign_addr,
1603 "%s-%s", 1502 "%s-%s",
1604 COMMUNICATOR_ADDRESS_PREFIX, 1503 COMMUNICATOR_ADDRESS_PREFIX,
1605 GNUNET_a2s(queue->address, 1504 GNUNET_a2s (queue->address, queue->address_len));
1606 queue->address_len));
1607 break; 1505 break;
1608 case AF_INET6: 1506 case AF_INET6:
1609 GNUNET_asprintf (&foreign_addr, 1507 GNUNET_asprintf (&foreign_addr,
1610 "%s-%s", 1508 "%s-%s",
1611 COMMUNICATOR_ADDRESS_PREFIX, 1509 COMMUNICATOR_ADDRESS_PREFIX,
1612 GNUNET_a2s(queue->address, 1510 GNUNET_a2s (queue->address, queue->address_len));
1613 queue->address_len));
1614 break; 1511 break;
1615 default: 1512 default:
1616 GNUNET_assert (0); 1513 GNUNET_assert (0);
1617 } 1514 }
1618 queue->qh 1515 queue->qh = GNUNET_TRANSPORT_communicator_mq_add (ch,
1619 = GNUNET_TRANSPORT_communicator_mq_add (ch, 1516 &queue->target,
1620 &queue->target, 1517 foreign_addr,
1621 foreign_addr, 1518 0 /* no MTU */,
1622 0 /* no MTU */, 1519 queue->nt,
1623 queue->nt, 1520 cs,
1624 cs, 1521 queue->mq);
1625 queue->mq);
1626 GNUNET_free (foreign_addr); 1522 GNUNET_free (foreign_addr);
1627 } 1523 }
1628} 1524}
@@ -1640,28 +1536,26 @@ boot_queue (struct Queue *queue,
1640 */ 1536 */
1641static void 1537static void
1642transmit_kx (struct Queue *queue, 1538transmit_kx (struct Queue *queue,
1643 const struct GNUNET_CRYPTO_EcdhePublicKey *epub) 1539 const struct GNUNET_CRYPTO_EcdhePublicKey *epub)
1644{ 1540{
1645 struct TcpHandshakeSignature ths; 1541 struct TcpHandshakeSignature ths;
1646 struct TCPConfirmation tc; 1542 struct TCPConfirmation tc;
1647 1543
1648 memcpy (queue->cwrite_buf, 1544 memcpy (queue->cwrite_buf, epub, sizeof (*epub));
1649 epub,
1650 sizeof (*epub));
1651 queue->cwrite_off = sizeof (epub); 1545 queue->cwrite_off = sizeof (epub);
1652 /* compute 'tc' and append in encrypted format to cwrite_buf */ 1546 /* compute 'tc' and append in encrypted format to cwrite_buf */
1653 tc.sender = my_identity; 1547 tc.sender = my_identity;
1654 tc.monotonic_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg)); 1548 tc.monotonic_time =
1549 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
1655 ths.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE); 1550 ths.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE);
1656 ths.purpose.size = htonl (sizeof (ths)); 1551 ths.purpose.size = htonl (sizeof (ths));
1657 ths.sender = my_identity; 1552 ths.sender = my_identity;
1658 ths.receiver = queue->target; 1553 ths.receiver = queue->target;
1659 ths.ephemeral = *epub; 1554 ths.ephemeral = *epub;
1660 ths.monotonic_time = tc.monotonic_time; 1555 ths.monotonic_time = tc.monotonic_time;
1661 GNUNET_assert (GNUNET_OK == 1556 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key,
1662 GNUNET_CRYPTO_eddsa_sign (my_private_key, 1557 &ths.purpose,
1663 &ths.purpose, 1558 &tc.sender_sig));
1664 &tc.sender_sig));
1665 GNUNET_assert (0 == 1559 GNUNET_assert (0 ==
1666 gcry_cipher_encrypt (queue->out_cipher, 1560 gcry_cipher_encrypt (queue->out_cipher,
1667 &queue->cwrite_buf[queue->cwrite_off], 1561 &queue->cwrite_buf[queue->cwrite_off],
@@ -1685,12 +1579,10 @@ start_initial_kx_out (struct Queue *queue)
1685 struct GNUNET_CRYPTO_EcdhePublicKey epub; 1579 struct GNUNET_CRYPTO_EcdhePublicKey epub;
1686 1580
1687 GNUNET_assert (GNUNET_OK == 1581 GNUNET_assert (GNUNET_OK ==
1688 GNUNET_CRYPTO_ecdhe_key_create2 (&queue->ephemeral)); 1582 GNUNET_CRYPTO_ecdhe_key_create2 (&queue->ephemeral));
1689 GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, 1583 GNUNET_CRYPTO_ecdhe_key_get_public (&queue->ephemeral, &epub);
1690 &epub);
1691 setup_out_cipher (queue); 1584 setup_out_cipher (queue);
1692 transmit_kx (queue, 1585 transmit_kx (queue, &epub);
1693 &epub);
1694} 1586}
1695 1587
1696 1588
@@ -1712,19 +1604,18 @@ decrypt_and_check_tc (struct Queue *queue,
1712{ 1604{
1713 struct TcpHandshakeSignature ths; 1605 struct TcpHandshakeSignature ths;
1714 1606
1715 GNUNET_assert (0 == 1607 GNUNET_assert (
1716 gcry_cipher_decrypt (queue->in_cipher, 1608 0 ==
1717 tc, 1609 gcry_cipher_decrypt (queue->in_cipher,
1718 sizeof (*tc), 1610 tc,
1719 &ibuf[sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)], 1611 sizeof (*tc),
1720 sizeof (tc))); 1612 &ibuf[sizeof (struct GNUNET_CRYPTO_EcdhePublicKey)],
1613 sizeof (tc)));
1721 ths.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE); 1614 ths.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE);
1722 ths.purpose.size = htonl (sizeof (ths)); 1615 ths.purpose.size = htonl (sizeof (ths));
1723 ths.sender = tc->sender; 1616 ths.sender = tc->sender;
1724 ths.receiver = my_identity; 1617 ths.receiver = my_identity;
1725 memcpy (&ths.ephemeral, 1618 memcpy (&ths.ephemeral, ibuf, sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
1726 ibuf,
1727 sizeof (struct GNUNET_CRYPTO_EcdhePublicKey));
1728 ths.monotonic_time = tc->monotonic_time; 1619 ths.monotonic_time = tc->monotonic_time;
1729 return GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE, 1620 return GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_TCP_HANDSHAKE,
1730 &ths.purpose, 1621 &ths.purpose,
@@ -1743,9 +1634,7 @@ free_proto_queue (struct ProtoQueue *pq)
1743{ 1634{
1744 GNUNET_NETWORK_socket_close (pq->sock); 1635 GNUNET_NETWORK_socket_close (pq->sock);
1745 GNUNET_free (pq->address); 1636 GNUNET_free (pq->address);
1746 GNUNET_CONTAINER_DLL_remove (proto_head, 1637 GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
1747 proto_tail,
1748 pq);
1749 GNUNET_free (pq); 1638 GNUNET_free (pq);
1750} 1639}
1751 1640
@@ -1773,48 +1662,38 @@ proto_read_kx (void *cls)
1773 return; 1662 return;
1774 } 1663 }
1775 rcvd = GNUNET_NETWORK_socket_recv (pq->sock, 1664 rcvd = GNUNET_NETWORK_socket_recv (pq->sock,
1776 &pq->ibuf[pq->ibuf_off], 1665 &pq->ibuf[pq->ibuf_off],
1777 sizeof (pq->ibuf) - pq->ibuf_off); 1666 sizeof (pq->ibuf) - pq->ibuf_off);
1778 if (-1 == rcvd) 1667 if (-1 == rcvd)
1779 { 1668 {
1780 if ( (EAGAIN != errno) && 1669 if ((EAGAIN != errno) && (EINTR != errno))
1781 (EINTR != errno) )
1782 { 1670 {
1783 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, 1671 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
1784 "recv");
1785 free_proto_queue (pq); 1672 free_proto_queue (pq);
1786 return; 1673 return;
1787 } 1674 }
1788 /* try again */ 1675 /* try again */
1789 pq->read_task = GNUNET_SCHEDULER_add_read_net (left, 1676 pq->read_task =
1790 pq->sock, 1677 GNUNET_SCHEDULER_add_read_net (left, pq->sock, &proto_read_kx, pq);
1791 &proto_read_kx,
1792 pq);
1793 return; 1678 return;
1794 } 1679 }
1795 pq->ibuf_off += rcvd; 1680 pq->ibuf_off += rcvd;
1796 if (pq->ibuf_off > sizeof (pq->ibuf)) 1681 if (pq->ibuf_off > sizeof (pq->ibuf))
1797 { 1682 {
1798 /* read more */ 1683 /* read more */
1799 pq->read_task = GNUNET_SCHEDULER_add_read_net (left, 1684 pq->read_task =
1800 pq->sock, 1685 GNUNET_SCHEDULER_add_read_net (left, pq->sock, &proto_read_kx, pq);
1801 &proto_read_kx,
1802 pq);
1803 return; 1686 return;
1804 } 1687 }
1805 /* we got all the data, let's find out who we are talking to! */ 1688 /* we got all the data, let's find out who we are talking to! */
1806 queue = GNUNET_new (struct Queue); 1689 queue = GNUNET_new (struct Queue);
1807 setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf, 1690 setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) pq->ibuf,
1808 queue); 1691 queue);
1809 if (GNUNET_OK != 1692 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, pq->ibuf))
1810 decrypt_and_check_tc (queue,
1811 &tc,
1812 pq->ibuf))
1813 { 1693 {
1814 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1694 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1815 "Invalid TCP KX received from %s\n", 1695 "Invalid TCP KX received from %s\n",
1816 GNUNET_a2s (queue->address, 1696 GNUNET_a2s (queue->address, queue->address_len));
1817 queue->address_len));
1818 gcry_cipher_close (queue->in_cipher); 1697 gcry_cipher_close (queue->in_cipher);
1819 GNUNET_free (queue); 1698 GNUNET_free (queue);
1820 free_proto_queue (pq); 1699 free_proto_queue (pq);
@@ -1824,16 +1703,13 @@ proto_read_kx (void *cls)
1824 queue->address_len = pq->address_len; 1703 queue->address_len = pq->address_len;
1825 queue->target = tc.sender; 1704 queue->target = tc.sender;
1826 start_initial_kx_out (queue); 1705 start_initial_kx_out (queue);
1827 boot_queue (queue, 1706 boot_queue (queue, GNUNET_TRANSPORT_CS_INBOUND);
1828 GNUNET_TRANSPORT_CS_INBOUND); 1707 queue->read_task =
1829 queue->read_task 1708 GNUNET_SCHEDULER_add_read_net (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1830 = GNUNET_SCHEDULER_add_read_net (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 1709 queue->sock,
1831 queue->sock, 1710 &queue_read,
1832 &queue_read, 1711 queue);
1833 queue); 1712 GNUNET_CONTAINER_DLL_remove (proto_head, proto_tail, pq);
1834 GNUNET_CONTAINER_DLL_remove (proto_head,
1835 proto_tail,
1836 pq);
1837 GNUNET_free (pq); 1713 GNUNET_free (pq);
1838} 1714}
1839 1715
@@ -1856,43 +1732,33 @@ listen_cb (void *cls)
1856 listen_task = NULL; 1732 listen_task = NULL;
1857 GNUNET_assert (NULL != listen_sock); 1733 GNUNET_assert (NULL != listen_sock);
1858 addrlen = sizeof (in); 1734 addrlen = sizeof (in);
1859 memset (&in, 1735 memset (&in, 0, sizeof (in));
1860 0,
1861 sizeof (in));
1862 sock = GNUNET_NETWORK_socket_accept (listen_sock, 1736 sock = GNUNET_NETWORK_socket_accept (listen_sock,
1863 (struct sockaddr *) &in, 1737 (struct sockaddr *) &in,
1864 &addrlen); 1738 &addrlen);
1865 if ( (NULL == sock) && 1739 if ((NULL == sock) && ((EMFILE == errno) || (ENFILE == errno)))
1866 ( (EMFILE == errno) ||
1867 (ENFILE == errno) ) )
1868 return; /* system limit reached, wait until connection goes down */ 1740 return; /* system limit reached, wait until connection goes down */
1869 listen_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 1741 listen_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
1870 listen_sock, 1742 listen_sock,
1871 &listen_cb, 1743 &listen_cb,
1872 NULL); 1744 NULL);
1873 if ( (NULL == sock) && 1745 if ((NULL == sock) && ((EAGAIN == errno) || (ENOBUFS == errno)))
1874 ( (EAGAIN == errno) ||
1875 (ENOBUFS == errno) ) )
1876 return; 1746 return;
1877 if (NULL == sock) 1747 if (NULL == sock)
1878 { 1748 {
1879 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 1749 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "accept");
1880 "accept");
1881 return; 1750 return;
1882 } 1751 }
1883 pq = GNUNET_new (struct ProtoQueue); 1752 pq = GNUNET_new (struct ProtoQueue);
1884 pq->address_len = addrlen; 1753 pq->address_len = addrlen;
1885 pq->address = GNUNET_memdup (&in, 1754 pq->address = GNUNET_memdup (&in, addrlen);
1886 addrlen);
1887 pq->timeout = GNUNET_TIME_relative_to_absolute (PROTO_QUEUE_TIMEOUT); 1755 pq->timeout = GNUNET_TIME_relative_to_absolute (PROTO_QUEUE_TIMEOUT);
1888 pq->sock = sock; 1756 pq->sock = sock;
1889 pq->read_task = GNUNET_SCHEDULER_add_read_net (PROTO_QUEUE_TIMEOUT, 1757 pq->read_task = GNUNET_SCHEDULER_add_read_net (PROTO_QUEUE_TIMEOUT,
1890 pq->sock, 1758 pq->sock,
1891 &proto_read_kx, 1759 &proto_read_kx,
1892 pq); 1760 pq);
1893 GNUNET_CONTAINER_DLL_insert (proto_head, 1761 GNUNET_CONTAINER_DLL_insert (proto_head, proto_tail, pq);
1894 proto_tail,
1895 pq);
1896} 1762}
1897 1763
1898 1764
@@ -1919,58 +1785,46 @@ queue_read_kx (void *cls)
1919 return; 1785 return;
1920 } 1786 }
1921 rcvd = GNUNET_NETWORK_socket_recv (queue->sock, 1787 rcvd = GNUNET_NETWORK_socket_recv (queue->sock,
1922 &queue->cread_buf[queue->cread_off], 1788 &queue->cread_buf[queue->cread_off],
1923 BUF_SIZE - queue->cread_off); 1789 BUF_SIZE - queue->cread_off);
1924 if (-1 == rcvd) 1790 if (-1 == rcvd)
1925 { 1791 {
1926 if ( (EAGAIN != errno) && 1792 if ((EAGAIN != errno) && (EINTR != errno))
1927 (EINTR != errno) )
1928 { 1793 {
1929 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, 1794 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
1930 "recv");
1931 queue_destroy (queue); 1795 queue_destroy (queue);
1932 return; 1796 return;
1933 } 1797 }
1934 queue->read_task = GNUNET_SCHEDULER_add_read_net (left, 1798 queue->read_task =
1935 queue->sock, 1799 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
1936 &queue_read_kx,
1937 queue);
1938 return; 1800 return;
1939 } 1801 }
1940 queue->cread_off += rcvd; 1802 queue->cread_off += rcvd;
1941 if (queue->cread_off < 1803 if (queue->cread_off < INITIAL_KX_SIZE)
1942 INITIAL_KX_SIZE)
1943 { 1804 {
1944 /* read more */ 1805 /* read more */
1945 queue->read_task = GNUNET_SCHEDULER_add_read_net (left, 1806 queue->read_task =
1946 queue->sock, 1807 GNUNET_SCHEDULER_add_read_net (left, queue->sock, &queue_read_kx, queue);
1947 &queue_read_kx,
1948 queue);
1949 return; 1808 return;
1950 } 1809 }
1951 /* we got all the data, let's find out who we are talking to! */ 1810 /* we got all the data, let's find out who we are talking to! */
1952 setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *) queue->cread_buf, 1811 setup_in_cipher ((const struct GNUNET_CRYPTO_EcdhePublicKey *)
1953 queue); 1812 queue->cread_buf,
1954 if (GNUNET_OK != 1813 queue);
1955 decrypt_and_check_tc (queue, 1814 if (GNUNET_OK != decrypt_and_check_tc (queue, &tc, queue->cread_buf))
1956 &tc,
1957 queue->cread_buf))
1958 { 1815 {
1959 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1816 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1960 "Invalid TCP KX received from %s\n", 1817 "Invalid TCP KX received from %s\n",
1961 GNUNET_a2s (queue->address, 1818 GNUNET_a2s (queue->address, queue->address_len));
1962 queue->address_len));
1963 queue_destroy (queue); 1819 queue_destroy (queue);
1964 return; 1820 return;
1965 } 1821 }
1966 if (0 != memcmp (&tc.sender, 1822 if (0 !=
1967 &queue->target, 1823 memcmp (&tc.sender, &queue->target, sizeof (struct GNUNET_PeerIdentity)))
1968 sizeof (struct GNUNET_PeerIdentity)))
1969 { 1824 {
1970 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1971 "Invalid sender in TCP KX received from %s\n", 1826 "Invalid sender in TCP KX received from %s\n",
1972 GNUNET_a2s (queue->address, 1827 GNUNET_a2s (queue->address, queue->address_len));
1973 queue->address_len));
1974 queue_destroy (queue); 1828 queue_destroy (queue);
1975 return; 1829 return;
1976 } 1830 }
@@ -1979,11 +1833,10 @@ queue_read_kx (void *cls)
1979 reschedule_queue_timeout (queue); 1833 reschedule_queue_timeout (queue);
1980 /* prepare to continue with regular read task immediately */ 1834 /* prepare to continue with regular read task immediately */
1981 memmove (queue->cread_buf, 1835 memmove (queue->cread_buf,
1982 &queue->cread_buf[INITIAL_KX_SIZE], 1836 &queue->cread_buf[INITIAL_KX_SIZE],
1983 queue->cread_off - (INITIAL_KX_SIZE)); 1837 queue->cread_off - (INITIAL_KX_SIZE));
1984 queue->cread_off -= INITIAL_KX_SIZE; 1838 queue->cread_off -= INITIAL_KX_SIZE;
1985 queue->read_task = GNUNET_SCHEDULER_add_now (&queue_read, 1839 queue->read_task = GNUNET_SCHEDULER_add_now (&queue_read, queue);
1986 queue);
1987} 1840}
1988 1841
1989 1842
@@ -2002,12 +1855,11 @@ queue_read_kx (void *cls)
2002 * @param peer identity of the other peer 1855 * @param peer identity of the other peer
2003 * @param address where to send the message, human-readable 1856 * @param address where to send the message, human-readable
2004 * communicator-specific format, 0-terminated, UTF-8 1857 * communicator-specific format, 0-terminated, UTF-8
2005 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is invalid 1858 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is
1859 * invalid
2006 */ 1860 */
2007static int 1861static int
2008mq_init (void *cls, 1862mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2009 const struct GNUNET_PeerIdentity *peer,
2010 const char *address)
2011{ 1863{
2012 struct Queue *queue; 1864 struct Queue *queue;
2013 const char *path; 1865 const char *path;
@@ -2016,37 +1868,31 @@ mq_init (void *cls,
2016 struct GNUNET_NETWORK_Handle *sock; 1868 struct GNUNET_NETWORK_Handle *sock;
2017 1869
2018 if (0 != strncmp (address, 1870 if (0 != strncmp (address,
2019 COMMUNICATOR_ADDRESS_PREFIX "-", 1871 COMMUNICATOR_ADDRESS_PREFIX "-",
2020 strlen (COMMUNICATOR_ADDRESS_PREFIX "-"))) 1872 strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
2021 { 1873 {
2022 GNUNET_break_op (0); 1874 GNUNET_break_op (0);
2023 return GNUNET_SYSERR; 1875 return GNUNET_SYSERR;
2024 } 1876 }
2025 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")]; 1877 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
2026 in = tcp_address_to_sockaddr (path, 1878 in = tcp_address_to_sockaddr (path, &in_len);
2027 &in_len);
2028 1879
2029 sock = GNUNET_NETWORK_socket_create (in->sa_family, 1880 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2030 SOCK_STREAM,
2031 IPPROTO_TCP);
2032 if (NULL == sock) 1881 if (NULL == sock)
2033 { 1882 {
2034 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2035 "socket(%d) failed: %s", 1884 "socket(%d) failed: %s",
2036 in->sa_family, 1885 in->sa_family,
2037 STRERROR (errno)); 1886 STRERROR (errno));
2038 GNUNET_free (in); 1887 GNUNET_free (in);
2039 return GNUNET_SYSERR; 1888 return GNUNET_SYSERR;
2040 } 1889 }
2041 if (GNUNET_OK != 1890 if (GNUNET_OK != GNUNET_NETWORK_socket_connect (sock, in, in_len))
2042 GNUNET_NETWORK_socket_connect (sock,
2043 in,
2044 in_len))
2045 { 1891 {
2046 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1892 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2047 "connect to `%s' failed: %s", 1893 "connect to `%s' failed: %s",
2048 address, 1894 address,
2049 STRERROR (errno)); 1895 STRERROR (errno));
2050 GNUNET_NETWORK_socket_close (sock); 1896 GNUNET_NETWORK_socket_close (sock);
2051 GNUNET_free (in); 1897 GNUNET_free (in);
2052 return GNUNET_SYSERR; 1898 return GNUNET_SYSERR;
@@ -2057,19 +1903,18 @@ mq_init (void *cls,
2057 queue->address = in; 1903 queue->address = in;
2058 queue->address_len = in_len; 1904 queue->address_len = in_len;
2059 queue->sock = sock; 1905 queue->sock = sock;
2060 boot_queue (queue, 1906 boot_queue (queue, GNUNET_TRANSPORT_CS_OUTBOUND);
2061 GNUNET_TRANSPORT_CS_OUTBOUND); 1907 queue->read_task =
2062 queue->read_task 1908 GNUNET_SCHEDULER_add_read_net (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2063 = GNUNET_SCHEDULER_add_read_net (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 1909 queue->sock,
2064 queue->sock, 1910 &queue_read_kx,
2065 &queue_read_kx, 1911 queue);
2066 queue);
2067 if (NULL == queue) 1912 if (NULL == queue)
2068 { 1913 {
2069 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1914 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2070 "Failed to setup queue to %s at `%s'\n", 1915 "Failed to setup queue to %s at `%s'\n",
2071 GNUNET_i2s (peer), 1916 GNUNET_i2s (peer),
2072 path); 1917 path);
2073 GNUNET_NETWORK_socket_close (sock); 1918 GNUNET_NETWORK_socket_close (sock);
2074 return GNUNET_NO; 1919 return GNUNET_NO;
2075 } 1920 }
@@ -2088,8 +1933,8 @@ mq_init (void *cls,
2088 */ 1933 */
2089static int 1934static int
2090get_queue_delete_it (void *cls, 1935get_queue_delete_it (void *cls,
2091 const struct GNUNET_PeerIdentity *target, 1936 const struct GNUNET_PeerIdentity *target,
2092 void *value) 1937 void *value)
2093{ 1938{
2094 struct Queue *queue = value; 1939 struct Queue *queue = value;
2095 1940
@@ -2110,8 +1955,8 @@ do_shutdown (void *cls)
2110{ 1955{
2111 if (NULL != nat) 1956 if (NULL != nat)
2112 { 1957 {
2113 GNUNET_NAT_unregister (nat); 1958 GNUNET_NAT_unregister (nat);
2114 nat = NULL; 1959 nat = NULL;
2115 } 1960 }
2116 if (NULL != listen_task) 1961 if (NULL != listen_task)
2117 { 1962 {
@@ -2120,13 +1965,10 @@ do_shutdown (void *cls)
2120 } 1965 }
2121 if (NULL != listen_sock) 1966 if (NULL != listen_sock)
2122 { 1967 {
2123 GNUNET_break (GNUNET_OK == 1968 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (listen_sock));
2124 GNUNET_NETWORK_socket_close (listen_sock));
2125 listen_sock = NULL; 1969 listen_sock = NULL;
2126 } 1970 }
2127 GNUNET_CONTAINER_multipeermap_iterate (queue_map, 1971 GNUNET_CONTAINER_multipeermap_iterate (queue_map, &get_queue_delete_it, NULL);
2128 &get_queue_delete_it,
2129 NULL);
2130 GNUNET_CONTAINER_multipeermap_destroy (queue_map); 1972 GNUNET_CONTAINER_multipeermap_destroy (queue_map);
2131 if (NULL != ch) 1973 if (NULL != ch)
2132 { 1974 {
@@ -2135,8 +1977,7 @@ do_shutdown (void *cls)
2135 } 1977 }
2136 if (NULL != stats) 1978 if (NULL != stats)
2137 { 1979 {
2138 GNUNET_STATISTICS_destroy (stats, 1980 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
2139 GNUNET_NO);
2140 stats = NULL; 1981 stats = NULL;
2141 } 1982 }
2142 if (NULL != my_private_key) 1983 if (NULL != my_private_key)
@@ -2146,8 +1987,8 @@ do_shutdown (void *cls)
2146 } 1987 }
2147 if (NULL != is) 1988 if (NULL != is)
2148 { 1989 {
2149 GNUNET_NT_scanner_done (is); 1990 GNUNET_NT_scanner_done (is);
2150 is = NULL; 1991 is = NULL;
2151 } 1992 }
2152} 1993}
2153 1994
@@ -2190,11 +2031,11 @@ enc_notify_cb (void *cls,
2190 */ 2031 */
2191static void 2032static void
2192nat_address_cb (void *cls, 2033nat_address_cb (void *cls,
2193 void **app_ctx, 2034 void **app_ctx,
2194 int add_remove, 2035 int add_remove,
2195 enum GNUNET_NAT_AddressClass ac, 2036 enum GNUNET_NAT_AddressClass ac,
2196 const struct sockaddr *addr, 2037 const struct sockaddr *addr,
2197 socklen_t addrlen) 2038 socklen_t addrlen)
2198{ 2039{
2199 char *my_addr; 2040 char *my_addr;
2200 struct GNUNET_TRANSPORT_AddressIdentifier *ai; 2041 struct GNUNET_TRANSPORT_AddressIdentifier *ai;
@@ -2204,17 +2045,15 @@ nat_address_cb (void *cls,
2204 enum GNUNET_NetworkType nt; 2045 enum GNUNET_NetworkType nt;
2205 2046
2206 GNUNET_asprintf (&my_addr, 2047 GNUNET_asprintf (&my_addr,
2207 "%s-%s", 2048 "%s-%s",
2208 COMMUNICATOR_ADDRESS_PREFIX, 2049 COMMUNICATOR_ADDRESS_PREFIX,
2209 GNUNET_a2s (addr, 2050 GNUNET_a2s (addr, addrlen));
2210 addrlen)); 2051 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
2211 nt = GNUNET_NT_scanner_get_type (is, 2052 ai =
2212 addr, 2053 GNUNET_TRANSPORT_communicator_address_add (ch,
2213 addrlen); 2054 my_addr,
2214 ai = GNUNET_TRANSPORT_communicator_address_add (ch, 2055 nt,
2215 my_addr, 2056 GNUNET_TIME_UNIT_FOREVER_REL);
2216 nt,
2217 GNUNET_TIME_UNIT_FOREVER_REL);
2218 GNUNET_free (my_addr); 2057 GNUNET_free (my_addr);
2219 *app_ctx = ai; 2058 *app_ctx = ai;
2220 } 2059 }
@@ -2251,9 +2090,9 @@ run (void *cls,
2251 cfg = c; 2090 cfg = c;
2252 if (GNUNET_OK != 2091 if (GNUNET_OK !=
2253 GNUNET_CONFIGURATION_get_value_filename (cfg, 2092 GNUNET_CONFIGURATION_get_value_filename (cfg,
2254 COMMUNICATOR_CONFIG_SECTION, 2093 COMMUNICATOR_CONFIG_SECTION,
2255 "BINDTO", 2094 "BINDTO",
2256 &bindto)) 2095 &bindto))
2257 { 2096 {
2258 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2097 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2259 COMMUNICATOR_CONFIG_SECTION, 2098 COMMUNICATOR_CONFIG_SECTION,
@@ -2262,40 +2101,32 @@ run (void *cls,
2262 } 2101 }
2263 if (GNUNET_OK != 2102 if (GNUNET_OK !=
2264 GNUNET_CONFIGURATION_get_value_number (cfg, 2103 GNUNET_CONFIGURATION_get_value_number (cfg,
2265 COMMUNICATOR_CONFIG_SECTION, 2104 COMMUNICATOR_CONFIG_SECTION,
2266 "MAX_QUEUE_LENGTH", 2105 "MAX_QUEUE_LENGTH",
2267 &max_queue_length)) 2106 &max_queue_length))
2268 max_queue_length = DEFAULT_MAX_QUEUE_LENGTH; 2107 max_queue_length = DEFAULT_MAX_QUEUE_LENGTH;
2269 2108
2270 in = tcp_address_to_sockaddr (bindto, 2109 in = tcp_address_to_sockaddr (bindto, &in_len);
2271 &in_len);
2272 if (NULL == in) 2110 if (NULL == in)
2273 { 2111 {
2274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2275 "Failed to setup TCP socket address with path `%s'\n", 2113 "Failed to setup TCP socket address with path `%s'\n",
2276 bindto); 2114 bindto);
2277 GNUNET_free (bindto); 2115 GNUNET_free (bindto);
2278 return; 2116 return;
2279 } 2117 }
2280 listen_sock = GNUNET_NETWORK_socket_create (in->sa_family, 2118 listen_sock =
2281 SOCK_STREAM, 2119 GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2282 IPPROTO_TCP);
2283 if (NULL == listen_sock) 2120 if (NULL == listen_sock)
2284 { 2121 {
2285 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 2122 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
2286 "socket");
2287 GNUNET_free (in); 2123 GNUNET_free (in);
2288 GNUNET_free (bindto); 2124 GNUNET_free (bindto);
2289 return; 2125 return;
2290 } 2126 }
2291 if (GNUNET_OK != 2127 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (listen_sock, in, in_len))
2292 GNUNET_NETWORK_socket_bind (listen_sock,
2293 in,
2294 in_len))
2295 { 2128 {
2296 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 2129 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "bind", bindto);
2297 "bind",
2298 bindto);
2299 GNUNET_NETWORK_socket_close (listen_sock); 2130 GNUNET_NETWORK_socket_close (listen_sock);
2300 listen_sock = NULL; 2131 listen_sock = NULL;
2301 GNUNET_free (in); 2132 GNUNET_free (in);
@@ -2306,12 +2137,10 @@ run (void *cls,
2306 thus, get the real IN-address from the socket */ 2137 thus, get the real IN-address from the socket */
2307 sto_len = sizeof (in_sto); 2138 sto_len = sizeof (in_sto);
2308 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock), 2139 if (0 != getsockname (GNUNET_NETWORK_get_fd (listen_sock),
2309 (struct sockaddr *) &in_sto, 2140 (struct sockaddr *) &in_sto,
2310 &sto_len)) 2141 &sto_len))
2311 { 2142 {
2312 memcpy (&in_sto, 2143 memcpy (&in_sto, in, in_len);
2313 in,
2314 in_len);
2315 sto_len = in_len; 2144 sto_len = in_len;
2316 } 2145 }
2317 GNUNET_free (in); 2146 GNUNET_free (in);
@@ -2319,37 +2148,34 @@ run (void *cls,
2319 in = (struct sockaddr *) &in_sto; 2148 in = (struct sockaddr *) &in_sto;
2320 in_len = sto_len; 2149 in_len = sto_len;
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "Bound to `%s'\n", 2151 "Bound to `%s'\n",
2323 GNUNET_a2s ((const struct sockaddr *) &in_sto, 2152 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
2324 sto_len)); 2153 stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
2325 stats = GNUNET_STATISTICS_create ("C-TCP", 2154 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
2326 cfg);
2327 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
2328 NULL);
2329 is = GNUNET_NT_scanner_init (); 2155 is = GNUNET_NT_scanner_init ();
2330 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); 2156 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
2331 if (NULL == my_private_key) 2157 if (NULL == my_private_key)
2332 { 2158 {
2333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2159 GNUNET_log (
2334 _("Transport service is lacking key configuration settings. Exiting.\n")); 2160 GNUNET_ERROR_TYPE_ERROR,
2161 _ (
2162 "Transport service is lacking key configuration settings. Exiting.\n"));
2335 GNUNET_SCHEDULER_shutdown (); 2163 GNUNET_SCHEDULER_shutdown ();
2336 return; 2164 return;
2337 } 2165 }
2338 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, 2166 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
2339 &my_identity.public_key);
2340 /* start listening */ 2167 /* start listening */
2341 listen_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 2168 listen_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
2342 listen_sock, 2169 listen_sock,
2343 &listen_cb, 2170 &listen_cb,
2344 NULL); 2171 NULL);
2345 queue_map = GNUNET_CONTAINER_multipeermap_create (10, 2172 queue_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2346 GNUNET_NO);
2347 ch = GNUNET_TRANSPORT_communicator_connect (cfg, 2173 ch = GNUNET_TRANSPORT_communicator_connect (cfg,
2348 COMMUNICATOR_CONFIG_SECTION, 2174 COMMUNICATOR_CONFIG_SECTION,
2349 COMMUNICATOR_ADDRESS_PREFIX, 2175 COMMUNICATOR_ADDRESS_PREFIX,
2350 GNUNET_TRANSPORT_CC_RELIABLE, 2176 GNUNET_TRANSPORT_CC_RELIABLE,
2351 &mq_init, 2177 &mq_init,
2352 NULL, 2178 NULL,
2353 &enc_notify_cb, 2179 &enc_notify_cb,
2354 NULL); 2180 NULL);
2355 if (NULL == ch) 2181 if (NULL == ch)
@@ -2359,14 +2185,14 @@ run (void *cls,
2359 return; 2185 return;
2360 } 2186 }
2361 nat = GNUNET_NAT_register (cfg, 2187 nat = GNUNET_NAT_register (cfg,
2362 COMMUNICATOR_CONFIG_SECTION, 2188 COMMUNICATOR_CONFIG_SECTION,
2363 IPPROTO_TCP, 2189 IPPROTO_TCP,
2364 1 /* one address */, 2190 1 /* one address */,
2365 (const struct sockaddr **) &in, 2191 (const struct sockaddr **) &in,
2366 &in_len, 2192 &in_len,
2367 &nat_address_cb, 2193 &nat_address_cb,
2368 NULL /* FIXME: support reversal: #5529 */, 2194 NULL /* FIXME: support reversal: #5529 */,
2369 NULL /* closure */); 2195 NULL /* closure */);
2370} 2196}
2371 2197
2372 2198
@@ -2378,28 +2204,25 @@ run (void *cls,
2378 * @return 0 ok, 1 on error 2204 * @return 0 ok, 1 on error
2379 */ 2205 */
2380int 2206int
2381main (int argc, 2207main (int argc, char *const *argv)
2382 char *const *argv)
2383{ 2208{
2384 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 2209 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2385 GNUNET_GETOPT_OPTION_END 2210 GNUNET_GETOPT_OPTION_END};
2386 };
2387 int ret; 2211 int ret;
2388 2212
2389 if (GNUNET_OK != 2213 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2390 GNUNET_STRINGS_get_utf8_args (argc, argv,
2391 &argc, &argv))
2392 return 2; 2214 return 2;
2393 2215
2394 ret = 2216 ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
2395 (GNUNET_OK == 2217 argv,
2396 GNUNET_PROGRAM_run (argc, argv, 2218 "gnunet-communicator-tcp",
2397 "gnunet-communicator-tcp", 2219 _ ("GNUnet TCP communicator"),
2398 _("GNUnet TCP communicator"), 2220 options,
2399 options, 2221 &run,
2400 &run, 2222 NULL))
2401 NULL)) ? 0 : 1; 2223 ? 0
2402 GNUNET_free ((void*) argv); 2224 : 1;
2225 GNUNET_free ((void *) argv);
2403 return ret; 2226 return ret;
2404} 2227}
2405 2228