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.c237
1 files changed, 194 insertions, 43 deletions
diff --git a/src/transport/gnunet-communicator-tcp.c b/src/transport/gnunet-communicator-tcp.c
index 880145424..df1bdc410 100644
--- a/src/transport/gnunet-communicator-tcp.c
+++ b/src/transport/gnunet-communicator-tcp.c
@@ -308,6 +308,38 @@ struct TCPRekey
308 struct GNUNET_TIME_AbsoluteNBO monotonic_time; 308 struct GNUNET_TIME_AbsoluteNBO monotonic_time;
309}; 309};
310 310
311/**
312 * Signature we use to verify that the ephemeral key was really chosen by
313 * the specified sender.
314 */
315struct TcpRekeySignature
316{
317 /**
318 * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY
319 */
320 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
321
322 /**
323 * Identity of the inititor of the TCP connection (TCP client).
324 */
325 struct GNUNET_PeerIdentity sender;
326
327 /**
328 * Presumed identity of the target of the TCP connection (TCP server)
329 */
330 struct GNUNET_PeerIdentity receiver;
331
332 /**
333 * Ephemeral key used by the @e sender.
334 */
335 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral;
336
337 /**
338 * Monotonic time of @e sender, to possibly help detect replay attacks
339 * (if receiver persists times by sender).
340 */
341 struct GNUNET_TIME_AbsoluteNBO monotonic_time;
342};
311 343
312/** 344/**
313 * TCP finish. Sender asks for the connection to be closed. 345 * TCP finish. Sender asks for the connection to be closed.
@@ -799,6 +831,11 @@ size_t unverified_size;
799static struct GNUNET_PEERSTORE_Handle *peerstore; 831static struct GNUNET_PEERSTORE_Handle *peerstore;
800 832
801/** 833/**
834 * A flag indicating we are already doing a shutdown.
835 */
836int shutdown_running = GNUNET_NO;
837
838/**
802 * We have been notified that our listen socket has something to 839 * We have been notified that our listen socket has something to
803 * read. Do the read and reschedule this function to be called again 840 * read. Do the read and reschedule this function to be called again
804 * once more is available. 841 * once more is available.
@@ -858,11 +895,6 @@ queue_destroy (struct Queue *queue)
858 GNUNET_PEERSTORE_iterate_cancel (queue->handshake_ack_monotime_get); 895 GNUNET_PEERSTORE_iterate_cancel (queue->handshake_ack_monotime_get);
859 queue->handshake_ack_monotime_get = NULL; 896 queue->handshake_ack_monotime_get = NULL;
860 } 897 }
861 if (NULL != (mq = queue->mq))
862 {
863 queue->mq = NULL;
864 GNUNET_MQ_destroy (mq);
865 }
866 if (NULL != queue->qh) 898 if (NULL != queue->qh)
867 { 899 {
868 GNUNET_TRANSPORT_communicator_mq_del (queue->qh); 900 GNUNET_TRANSPORT_communicator_mq_del (queue->qh);
@@ -885,7 +917,11 @@ queue_destroy (struct Queue *queue)
885 GNUNET_SCHEDULER_cancel (queue->write_task); 917 GNUNET_SCHEDULER_cancel (queue->write_task);
886 queue->write_task = NULL; 918 queue->write_task = NULL;
887 } 919 }
888 GNUNET_NETWORK_socket_close (queue->sock); 920 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_close (queue->sock))
921 {
922 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
923 "closing socket failed\n");
924 }
889 gcry_cipher_close (queue->in_cipher); 925 gcry_cipher_close (queue->in_cipher);
890 gcry_cipher_close (queue->out_cipher); 926 gcry_cipher_close (queue->out_cipher);
891 GNUNET_free (queue->address); 927 GNUNET_free (queue->address);
@@ -894,12 +930,18 @@ queue_destroy (struct Queue *queue)
894 else 930 else
895 GNUNET_free (queue); 931 GNUNET_free (queue);
896 932
897 if (NULL == lt->listen_task) 933 if ((NULL != lt->listen_sock) && (NULL == lt->listen_task))
934 {
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936 "add read net listen\n");
898 lt->listen_task = GNUNET_SCHEDULER_add_read_net ( 937 lt->listen_task = GNUNET_SCHEDULER_add_read_net (
899 GNUNET_TIME_UNIT_FOREVER_REL, 938 GNUNET_TIME_UNIT_FOREVER_REL,
900 lt->listen_sock, 939 lt->listen_sock,
901 &listen_cb, 940 &listen_cb,
902 lt); 941 lt);
942 }
943 else
944 GNUNET_free (lt);
903} 945}
904 946
905 947
@@ -1001,6 +1043,13 @@ core_read_finished_cb (void *cls, int success)
1001 "# messages lost in communicator API towards CORE", 1043 "# messages lost in communicator API towards CORE",
1002 1, 1044 1,
1003 GNUNET_NO); 1045 GNUNET_NO);
1046 if (NULL == queue)
1047 return;
1048
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "backpressure %u\n",
1051 queue->backpressure);
1052
1004 queue->backpressure--; 1053 queue->backpressure--;
1005 /* handle deferred queue destruction */ 1054 /* handle deferred queue destruction */
1006 if ((queue->destroyed) && (0 == queue->backpressure)) 1055 if ((queue->destroyed) && (0 == queue->backpressure))
@@ -1008,15 +1057,18 @@ core_read_finished_cb (void *cls, int success)
1008 GNUNET_free (queue); 1057 GNUNET_free (queue);
1009 return; 1058 return;
1010 } 1059 }
1011 reschedule_queue_timeout (queue); 1060 else if (GNUNET_YES != queue->destroyed)
1012 /* possibly unchoke reading, now that CORE made progress */ 1061 {
1013 if (NULL == queue->read_task) 1062 reschedule_queue_timeout (queue);
1014 queue->read_task = 1063 /* possibly unchoke reading, now that CORE made progress */
1015 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining ( 1064 if (NULL == queue->read_task)
1016 queue->timeout), 1065 queue->read_task =
1017 queue->sock, 1066 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (
1018 &queue_read, 1067 queue->timeout),
1019 queue); 1068 queue->sock,
1069 &queue_read,
1070 queue);
1071 }
1020} 1072}
1021 1073
1022 1074
@@ -1219,13 +1271,34 @@ rekey_monotime_cb (void *cls,
1219static void 1271static void
1220do_rekey (struct Queue *queue, const struct TCPRekey *rekey) 1272do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
1221{ 1273{
1222 struct TcpHandshakeSignature thp; 1274 struct TcpRekeySignature thp;
1275
1223 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY); 1276 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY);
1224 thp.purpose.size = htonl (sizeof(thp)); 1277 thp.purpose.size = htonl (sizeof(thp));
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "do_rekey size %u\n",
1280 thp.purpose.size);
1225 thp.sender = queue->target; 1281 thp.sender = queue->target;
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "sender %s\n",
1284 GNUNET_p2s (&thp.sender.public_key));
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1286 "sender %s\n",
1287 GNUNET_p2s (&queue->target.public_key));
1226 thp.receiver = my_identity; 1288 thp.receiver = my_identity;
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 "receiver %s\n",
1291 GNUNET_p2s (&thp.receiver.public_key));
1227 thp.ephemeral = rekey->ephemeral; 1292 thp.ephemeral = rekey->ephemeral;
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1294 "ephemeral %s\n",
1295 GNUNET_e2s (&thp.ephemeral));
1228 thp.monotonic_time = rekey->monotonic_time; 1296 thp.monotonic_time = rekey->monotonic_time;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "time %s\n",
1299 GNUNET_STRINGS_absolute_time_to_string (
1300 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1301 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1229 if (GNUNET_OK != 1302 if (GNUNET_OK !=
1230 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY, 1303 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY,
1231 &thp, 1304 &thp,
@@ -1739,11 +1812,19 @@ extract_address (const char *bindto)
1739 char *cp; 1812 char *cp;
1740 char *rest = NULL; 1813 char *rest = NULL;
1741 1814
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1816 "extract address with bindto %s\n",
1817 bindto);
1818
1742 if (NULL == bindto) 1819 if (NULL == bindto)
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1744 "bindto is NULL\n"); 1821 "bindto is NULL\n");
1745 1822
1746 cp = GNUNET_strdup (bindto); 1823 cp = GNUNET_strdup (bindto);
1824
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "extract address 2\n");
1827
1747 start = cp; 1828 start = cp;
1748 if (('[' == *cp) && (']' == cp[strlen (cp) - 1])) 1829 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1749 { 1830 {
@@ -1763,7 +1844,11 @@ extract_address (const char *bindto)
1763 } 1844 }
1764 } 1845 }
1765 1846
1766 GNUNET_free (cp); 1847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1848 "extract address 3\n");
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "extract address with start %s\n",
1851 start);
1767 1852
1768 return start; 1853 return start;
1769} 1854}
@@ -1865,10 +1950,25 @@ tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1865 // cp = GNUNET_strdup (bindto); 1950 // cp = GNUNET_strdup (bindto);
1866 start = extract_address (bindto); 1951 start = extract_address (bindto);
1867 1952
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1954 "start %s\n",
1955 start);
1956
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "!bindto %s\n",
1959 bindto);
1960
1961
1962
1868 if (1 == inet_pton (AF_INET, start, &v4.sin_addr)) 1963 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
1869 { 1964 {
1870 // colon = strrchr (cp, ':'); 1965 // colon = strrchr (cp, ':');
1871 port = extract_port (bindto); 1966 port = extract_port (bindto);
1967
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1969 "port %u\n",
1970 port);
1971
1872 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port); 1972 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
1873 } 1973 }
1874 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr)) 1974 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
@@ -1878,6 +1978,8 @@ tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1878 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port); 1978 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
1879 } 1979 }
1880 1980
1981 // GNUNET_free (start);
1982
1881 return in; 1983 return in;
1882} 1984}
1883 1985
@@ -1913,7 +2015,7 @@ static void
1913inject_rekey (struct Queue *queue) 2015inject_rekey (struct Queue *queue)
1914{ 2016{
1915 struct TCPRekey rekey; 2017 struct TCPRekey rekey;
1916 struct TcpHandshakeSignature thp; 2018 struct TcpRekeySignature thp;
1917 2019
1918 GNUNET_assert (0 == queue->pwrite_off); 2020 GNUNET_assert (0 == queue->pwrite_off);
1919 memset (&rekey, 0, sizeof(rekey)); 2021 memset (&rekey, 0, sizeof(rekey));
@@ -1925,10 +2027,26 @@ inject_rekey (struct Queue *queue)
1925 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg)); 2027 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
1926 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY); 2028 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY);
1927 thp.purpose.size = htonl (sizeof(thp)); 2029 thp.purpose.size = htonl (sizeof(thp));
2030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2031 "inject_rekey size %u\n",
2032 thp.purpose.size);
1928 thp.sender = my_identity; 2033 thp.sender = my_identity;
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2035 "sender %s\n",
2036 GNUNET_p2s (&thp.sender.public_key));
1929 thp.receiver = queue->target; 2037 thp.receiver = queue->target;
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2039 "receiver %s\n",
2040 GNUNET_p2s (&thp.receiver.public_key));
1930 thp.ephemeral = rekey.ephemeral; 2041 thp.ephemeral = rekey.ephemeral;
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2043 "ephemeral %s\n",
2044 GNUNET_e2s (&thp.ephemeral));
1931 thp.monotonic_time = rekey.monotonic_time; 2045 thp.monotonic_time = rekey.monotonic_time;
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2047 "time %s\n",
2048 GNUNET_STRINGS_absolute_time_to_string (
2049 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1932 GNUNET_CRYPTO_eddsa_sign (my_private_key, 2050 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1933 &thp, 2051 &thp,
1934 &rekey.sender_sig); 2052 &rekey.sender_sig);
@@ -2004,10 +2122,11 @@ queue_write (void *cls)
2004 queue->cwrite_off += queue->pwrite_off; 2122 queue->cwrite_off += queue->pwrite_off;
2005 queue->pwrite_off = 0; 2123 queue->pwrite_off = 0;
2006 } 2124 }
2007 if ((0 == queue->pwrite_off) && 2125 if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
2008 ((0 == queue->rekey_left_bytes) || 2126 ((0 == queue->rekey_left_bytes) ||
2009 (0 == 2127 (0 ==
2010 GNUNET_TIME_absolute_get_remaining (queue->rekey_time).rel_value_us))) 2128 GNUNET_TIME_absolute_get_remaining (
2129 queue->rekey_time).rel_value_us))))
2011 { 2130 {
2012 inject_rekey (queue); 2131 inject_rekey (queue);
2013 } 2132 }
@@ -2729,6 +2848,10 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2729 return GNUNET_SYSERR; 2848 return GNUNET_SYSERR;
2730 } 2849 }
2731 2850
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2852 "in %s\n",
2853 GNUNET_a2s (in, in_len));
2854
2732 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP); 2855 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2733 if (NULL == sock) 2856 if (NULL == sock)
2734 { 2857 {
@@ -2794,6 +2917,11 @@ get_queue_delete_it (void *cls,
2794 2917
2795 (void) cls; 2918 (void) cls;
2796 (void) target; 2919 (void) target;
2920 if (NULL != queue->listen_task)
2921 {
2922 GNUNET_SCHEDULER_cancel (queue->listen_task);
2923 queue->listen_task = NULL;
2924 }
2797 queue_destroy (queue); 2925 queue_destroy (queue);
2798 return GNUNET_OK; 2926 return GNUNET_OK;
2799} 2927}
@@ -2807,6 +2935,12 @@ get_queue_delete_it (void *cls,
2807static void 2935static void
2808do_shutdown (void *cls) 2936do_shutdown (void *cls)
2809{ 2937{
2938
2939 if (GNUNET_YES == shutdown_running)
2940 return;
2941 else
2942 shutdown_running = GNUNET_YES;
2943
2810 while (NULL != proto_head) 2944 while (NULL != proto_head)
2811 free_proto_queue (proto_head); 2945 free_proto_queue (proto_head);
2812 if (NULL != nat) 2946 if (NULL != nat)
@@ -2836,6 +2970,16 @@ do_shutdown (void *cls)
2836 GNUNET_NT_scanner_done (is); 2970 GNUNET_NT_scanner_done (is);
2837 is = NULL; 2971 is = NULL;
2838 } 2972 }
2973 if (NULL != peerstore)
2974 {
2975 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
2976 peerstore = NULL;
2977 }
2978 if (NULL != resolve_request_handle)
2979 {
2980 GNUNET_RESOLVER_request_cancel (resolve_request_handle);
2981 resolve_request_handle = NULL;
2982 }
2839} 2983}
2840 2984
2841 2985
@@ -3059,8 +3203,7 @@ nat_register ()
3059 { 3203 {
3060 3204
3061 saddr_lens[i] = addrs_head->addr_len; 3205 saddr_lens[i] = addrs_head->addr_len;
3062 saddrs[i] = GNUNET_malloc (saddr_lens[i]); 3206 saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3063 saddrs[i] = addrs_head->addr;
3064 3207
3065 i++; 3208 i++;
3066 3209
@@ -3078,8 +3221,8 @@ nat_register ()
3078 3221
3079 i = 0; 3222 i = 0;
3080 3223
3081 /*for (i = addrs_lens - 1; i >= 0; i--) 3224 for (i = addrs_lens - 1; i >= 0; i--)
3082 GNUNET_free (saddrs[i]);*/ 3225 GNUNET_free (saddrs[i]);
3083 GNUNET_free (saddrs); 3226 GNUNET_free (saddrs);
3084 GNUNET_free (saddr_lens); 3227 GNUNET_free (saddr_lens);
3085 3228
@@ -3221,9 +3364,6 @@ run (void *cls,
3221 return; 3364 return;
3222 } 3365 }
3223 3366
3224 // cp = GNUNET_strdup (bindto);
3225 start = extract_address (bindto);
3226
3227 if (1 == sscanf (bindto, "%u%1s", &port, dummy)) 3367 if (1 == sscanf (bindto, "%u%1s", &port, dummy))
3228 { 3368 {
3229 po = tcp_address_to_sockaddr_port_only (bindto, &port); 3369 po = tcp_address_to_sockaddr_port_only (bindto, &port);
@@ -3248,9 +3388,16 @@ run (void *cls,
3248 add_addr (po->addr_ipv6, addr_len_ipv6); 3388 add_addr (po->addr_ipv6, addr_len_ipv6);
3249 } 3389 }
3250 3390
3391 GNUNET_free (po);
3251 nat_register (); 3392 nat_register ();
3393 GNUNET_free (bindto);
3394
3395 return;
3252 } 3396 }
3253 else if (1 == inet_pton (AF_INET, start, &v4.sin_addr)) 3397
3398 start = extract_address (bindto);
3399
3400 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
3254 { 3401 {
3255 port = extract_port (bindto); 3402 port = extract_port (bindto);
3256 3403
@@ -3258,26 +3405,30 @@ run (void *cls,
3258 init_socket (in, in_len); 3405 init_socket (in, in_len);
3259 add_addr (in, in_len); 3406 add_addr (in, in_len);
3260 nat_register (); 3407 nat_register ();
3408 GNUNET_free (bindto);
3409 return;
3261 } 3410 }
3262 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr)) 3411
3412 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
3263 { 3413 {
3264 port = extract_port (bindto); 3414 port = extract_port (bindto);
3265 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, port); 3415 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, port);
3266 init_socket (in, in_len); 3416 init_socket (in, in_len);
3267 add_addr (in, in_len); 3417 add_addr (in, in_len);
3268 nat_register (); 3418 nat_register ();
3419 GNUNET_free (bindto);
3420 return;
3269 } 3421 }
3270 else
3271 {
3272 port = extract_port (bindto);
3273 3422
3274 resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto, ":", 3423
3275 &rest), 3424 port = extract_port (bindto);
3276 AF_UNSPEC, 3425
3277 GNUNET_TIME_UNIT_MINUTES, 3426 resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto, ":",
3278 &init_socket_resolv, 3427 &rest),
3279 &port); 3428 AF_UNSPEC,
3280 } 3429 GNUNET_TIME_UNIT_MINUTES,
3430 &init_socket_resolv,
3431 &port);
3281 GNUNET_free (bindto); 3432 GNUNET_free (bindto);
3282} 3433}
3283 3434