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.c455
1 files changed, 351 insertions, 104 deletions
diff --git a/src/transport/gnunet-communicator-tcp.c b/src/transport/gnunet-communicator-tcp.c
index 880145424..59f42496a 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.
@@ -362,11 +394,6 @@ struct Queue
362 struct GNUNET_PeerIdentity target; 394 struct GNUNET_PeerIdentity target;
363 395
364 /** 396 /**
365 * ID of listen task
366 */
367 struct GNUNET_SCHEDULER_Task *listen_task;
368
369 /**
370 * Listen socket. 397 * Listen socket.
371 */ 398 */
372 struct GNUNET_NETWORK_Handle *listen_sock; 399 struct GNUNET_NETWORK_Handle *listen_sock;
@@ -606,11 +633,6 @@ struct ProtoQueue
606 struct ProtoQueue *prev; 633 struct ProtoQueue *prev;
607 634
608 /** 635 /**
609 * ID of listen task
610 */
611 struct GNUNET_SCHEDULER_Task *listen_task;
612
613 /**
614 * Listen socket. 636 * Listen socket.
615 */ 637 */
616 struct GNUNET_NETWORK_Handle *listen_sock; 638 struct GNUNET_NETWORK_Handle *listen_sock;
@@ -729,6 +751,11 @@ static struct GNUNET_TRANSPORT_CommunicatorHandle *ch;
729static struct GNUNET_CONTAINER_MultiPeerMap *queue_map; 751static struct GNUNET_CONTAINER_MultiPeerMap *queue_map;
730 752
731/** 753/**
754 * ListenTasks (map from socket to `struct ListenTask`)
755 */
756static struct GNUNET_CONTAINER_MultiHashMap *lt_map;
757
758/**
732 * Our public key. 759 * Our public key.
733 */ 760 */
734static struct GNUNET_PeerIdentity my_identity; 761static struct GNUNET_PeerIdentity my_identity;
@@ -784,6 +811,16 @@ struct Addresses *addrs_head;
784struct Addresses *addrs_tail; 811struct Addresses *addrs_tail;
785 812
786/** 813/**
814 * Head of DLL with ListenTasks.
815 */
816struct ListenTask *lts_head;
817
818/**
819 * Head of DLL with ListenTask.
820 */
821struct ListenTask *lts_tail;
822
823/**
787 * Number of addresses in the DLL for register at NAT service. 824 * Number of addresses in the DLL for register at NAT service.
788 */ 825 */
789int addrs_lens; 826int addrs_lens;
@@ -799,6 +836,16 @@ size_t unverified_size;
799static struct GNUNET_PEERSTORE_Handle *peerstore; 836static struct GNUNET_PEERSTORE_Handle *peerstore;
800 837
801/** 838/**
839 * A flag indicating we are already doing a shutdown.
840 */
841int shutdown_running = GNUNET_NO;
842
843/**
844 * The port the communicator should be assigned to.
845 */
846unsigned int bind_port;
847
848/**
802 * We have been notified that our listen socket has something to 849 * We have been notified that our listen socket has something to
803 * read. Do the read and reschedule this function to be called again 850 * read. Do the read and reschedule this function to be called again
804 * once more is available. 851 * once more is available.
@@ -808,7 +855,6 @@ static struct GNUNET_PEERSTORE_Handle *peerstore;
808static void 855static void
809listen_cb (void *cls); 856listen_cb (void *cls);
810 857
811
812/** 858/**
813 * Functions with this signature are called whenever we need 859 * Functions with this signature are called whenever we need
814 * to close a queue due to a disconnect or failure to 860 * to close a queue due to a disconnect or failure to
@@ -819,11 +865,14 @@ listen_cb (void *cls);
819static void 865static void
820queue_destroy (struct Queue *queue) 866queue_destroy (struct Queue *queue)
821{ 867{
822 struct GNUNET_MQ_Handle *mq; 868 struct ListenTask *lt = NULL;
823 struct ListenTask *lt; 869 struct GNUNET_HashCode h_sock;
824 lt = GNUNET_new (struct ListenTask); 870
825 lt->listen_sock = queue->listen_sock; 871 GNUNET_CRYPTO_hash (queue->listen_sock,
826 lt->listen_task = queue->listen_task; 872 sizeof(queue->listen_sock),
873 &h_sock);
874
875 lt = GNUNET_CONTAINER_multihashmap_get (lt_map, &h_sock);
827 876
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Disconnecting queue for peer `%s'\n", 878 "Disconnecting queue for peer `%s'\n",
@@ -858,11 +907,6 @@ queue_destroy (struct Queue *queue)
858 GNUNET_PEERSTORE_iterate_cancel (queue->handshake_ack_monotime_get); 907 GNUNET_PEERSTORE_iterate_cancel (queue->handshake_ack_monotime_get);
859 queue->handshake_ack_monotime_get = NULL; 908 queue->handshake_ack_monotime_get = NULL;
860 } 909 }
861 if (NULL != (mq = queue->mq))
862 {
863 queue->mq = NULL;
864 GNUNET_MQ_destroy (mq);
865 }
866 if (NULL != queue->qh) 910 if (NULL != queue->qh)
867 { 911 {
868 GNUNET_TRANSPORT_communicator_mq_del (queue->qh); 912 GNUNET_TRANSPORT_communicator_mq_del (queue->qh);
@@ -885,7 +929,11 @@ queue_destroy (struct Queue *queue)
885 GNUNET_SCHEDULER_cancel (queue->write_task); 929 GNUNET_SCHEDULER_cancel (queue->write_task);
886 queue->write_task = NULL; 930 queue->write_task = NULL;
887 } 931 }
888 GNUNET_NETWORK_socket_close (queue->sock); 932 if (GNUNET_SYSERR == GNUNET_NETWORK_socket_close (queue->sock))
933 {
934 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
935 "closing socket failed\n");
936 }
889 gcry_cipher_close (queue->in_cipher); 937 gcry_cipher_close (queue->in_cipher);
890 gcry_cipher_close (queue->out_cipher); 938 gcry_cipher_close (queue->out_cipher);
891 GNUNET_free (queue->address); 939 GNUNET_free (queue->address);
@@ -894,12 +942,18 @@ queue_destroy (struct Queue *queue)
894 else 942 else
895 GNUNET_free (queue); 943 GNUNET_free (queue);
896 944
897 if (NULL == lt->listen_task) 945 if ((! shutdown_running) && (NULL == lt->listen_task))
946 {
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "add read net listen\n");
898 lt->listen_task = GNUNET_SCHEDULER_add_read_net ( 949 lt->listen_task = GNUNET_SCHEDULER_add_read_net (
899 GNUNET_TIME_UNIT_FOREVER_REL, 950 GNUNET_TIME_UNIT_FOREVER_REL,
900 lt->listen_sock, 951 lt->listen_sock,
901 &listen_cb, 952 &listen_cb,
902 lt); 953 lt);
954 }
955 else
956 GNUNET_free (lt);
903} 957}
904 958
905 959
@@ -1001,6 +1055,13 @@ core_read_finished_cb (void *cls, int success)
1001 "# messages lost in communicator API towards CORE", 1055 "# messages lost in communicator API towards CORE",
1002 1, 1056 1,
1003 GNUNET_NO); 1057 GNUNET_NO);
1058 if (NULL == queue)
1059 return;
1060
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "backpressure %u\n",
1063 queue->backpressure);
1064
1004 queue->backpressure--; 1065 queue->backpressure--;
1005 /* handle deferred queue destruction */ 1066 /* handle deferred queue destruction */
1006 if ((queue->destroyed) && (0 == queue->backpressure)) 1067 if ((queue->destroyed) && (0 == queue->backpressure))
@@ -1008,15 +1069,18 @@ core_read_finished_cb (void *cls, int success)
1008 GNUNET_free (queue); 1069 GNUNET_free (queue);
1009 return; 1070 return;
1010 } 1071 }
1011 reschedule_queue_timeout (queue); 1072 else if (GNUNET_YES != queue->destroyed)
1012 /* possibly unchoke reading, now that CORE made progress */ 1073 {
1013 if (NULL == queue->read_task) 1074 reschedule_queue_timeout (queue);
1014 queue->read_task = 1075 /* possibly unchoke reading, now that CORE made progress */
1015 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining ( 1076 if (NULL == queue->read_task)
1016 queue->timeout), 1077 queue->read_task =
1017 queue->sock, 1078 GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_absolute_get_remaining (
1018 &queue_read, 1079 queue->timeout),
1019 queue); 1080 queue->sock,
1081 &queue_read,
1082 queue);
1083 }
1020} 1084}
1021 1085
1022 1086
@@ -1219,13 +1283,34 @@ rekey_monotime_cb (void *cls,
1219static void 1283static void
1220do_rekey (struct Queue *queue, const struct TCPRekey *rekey) 1284do_rekey (struct Queue *queue, const struct TCPRekey *rekey)
1221{ 1285{
1222 struct TcpHandshakeSignature thp; 1286 struct TcpRekeySignature thp;
1287
1223 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY); 1288 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY);
1224 thp.purpose.size = htonl (sizeof(thp)); 1289 thp.purpose.size = htonl (sizeof(thp));
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 "do_rekey size %u\n",
1292 thp.purpose.size);
1225 thp.sender = queue->target; 1293 thp.sender = queue->target;
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1295 "sender %s\n",
1296 GNUNET_p2s (&thp.sender.public_key));
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "sender %s\n",
1299 GNUNET_p2s (&queue->target.public_key));
1226 thp.receiver = my_identity; 1300 thp.receiver = my_identity;
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "receiver %s\n",
1303 GNUNET_p2s (&thp.receiver.public_key));
1227 thp.ephemeral = rekey->ephemeral; 1304 thp.ephemeral = rekey->ephemeral;
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "ephemeral %s\n",
1307 GNUNET_e2s (&thp.ephemeral));
1228 thp.monotonic_time = rekey->monotonic_time; 1308 thp.monotonic_time = rekey->monotonic_time;
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1310 "time %s\n",
1311 GNUNET_STRINGS_absolute_time_to_string (
1312 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1313 GNUNET_assert (ntohl ((&thp)->purpose.size) == sizeof (*(&thp)));
1229 if (GNUNET_OK != 1314 if (GNUNET_OK !=
1230 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY, 1315 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY,
1231 &thp, 1316 &thp,
@@ -1359,7 +1444,7 @@ try_handle_plaintext (struct Queue *queue)
1359 if ((-1 != unverified_size) && (unverified_size > INITIAL_CORE_KX_SIZE)) 1444 if ((-1 != unverified_size) && (unverified_size > INITIAL_CORE_KX_SIZE))
1360 { 1445 {
1361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1362 "Already received data of size %u bigger than KX size %u!\n", 1447 "Already received data of size %lu bigger than KX size %lu!\n",
1363 unverified_size, 1448 unverified_size,
1364 INITIAL_CORE_KX_SIZE); 1449 INITIAL_CORE_KX_SIZE);
1365 GNUNET_break_op (0); 1450 GNUNET_break_op (0);
@@ -1636,6 +1721,8 @@ tcp_address_to_sockaddr_numeric_v6 (socklen_t *sock_len, struct sockaddr_in6 v6,
1636#if HAVE_SOCKADDR_IN_SIN_LEN 1721#if HAVE_SOCKADDR_IN_SIN_LEN
1637 v6.sin6_len = sizeof(sizeof(struct sockaddr_in6)); 1722 v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
1638#endif 1723#endif
1724 v6.sin6_flowinfo = 0;
1725 v6.sin6_scope_id = 0;
1639 in = GNUNET_memdup (&v6, sizeof(v6)); 1726 in = GNUNET_memdup (&v6, sizeof(v6));
1640 *sock_len = sizeof(struct sockaddr_in6); 1727 *sock_len = sizeof(struct sockaddr_in6);
1641 1728
@@ -1739,11 +1826,19 @@ extract_address (const char *bindto)
1739 char *cp; 1826 char *cp;
1740 char *rest = NULL; 1827 char *rest = NULL;
1741 1828
1829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1830 "extract address with bindto %s\n",
1831 bindto);
1832
1742 if (NULL == bindto) 1833 if (NULL == bindto)
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1834 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1744 "bindto is NULL\n"); 1835 "bindto is NULL\n");
1745 1836
1746 cp = GNUNET_strdup (bindto); 1837 cp = GNUNET_strdup (bindto);
1838
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840 "extract address 2\n");
1841
1747 start = cp; 1842 start = cp;
1748 if (('[' == *cp) && (']' == cp[strlen (cp) - 1])) 1843 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
1749 { 1844 {
@@ -1763,7 +1858,11 @@ extract_address (const char *bindto)
1763 } 1858 }
1764 } 1859 }
1765 1860
1766 GNUNET_free (cp); 1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "extract address 3\n");
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864 "extract address with start %s\n",
1865 start);
1767 1866
1768 return start; 1867 return start;
1769} 1868}
@@ -1865,10 +1964,25 @@ tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1865 // cp = GNUNET_strdup (bindto); 1964 // cp = GNUNET_strdup (bindto);
1866 start = extract_address (bindto); 1965 start = extract_address (bindto);
1867 1966
1967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1968 "start %s\n",
1969 start);
1970
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "!bindto %s\n",
1973 bindto);
1974
1975
1976
1868 if (1 == inet_pton (AF_INET, start, &v4.sin_addr)) 1977 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
1869 { 1978 {
1870 // colon = strrchr (cp, ':'); 1979 // colon = strrchr (cp, ':');
1871 port = extract_port (bindto); 1980 port = extract_port (bindto);
1981
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1983 "port %u\n",
1984 port);
1985
1872 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port); 1986 in = tcp_address_to_sockaddr_numeric_v4 (sock_len, v4, port);
1873 } 1987 }
1874 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr)) 1988 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
@@ -1877,6 +1991,11 @@ tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1877 port = extract_port (bindto); 1991 port = extract_port (bindto);
1878 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port); 1992 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
1879 } 1993 }
1994 else{
1995 GNUNET_assert (0);
1996 }
1997
1998 // GNUNET_free (start);
1880 1999
1881 return in; 2000 return in;
1882} 2001}
@@ -1913,7 +2032,7 @@ static void
1913inject_rekey (struct Queue *queue) 2032inject_rekey (struct Queue *queue)
1914{ 2033{
1915 struct TCPRekey rekey; 2034 struct TCPRekey rekey;
1916 struct TcpHandshakeSignature thp; 2035 struct TcpRekeySignature thp;
1917 2036
1918 GNUNET_assert (0 == queue->pwrite_off); 2037 GNUNET_assert (0 == queue->pwrite_off);
1919 memset (&rekey, 0, sizeof(rekey)); 2038 memset (&rekey, 0, sizeof(rekey));
@@ -1925,10 +2044,26 @@ inject_rekey (struct Queue *queue)
1925 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg)); 2044 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
1926 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY); 2045 thp.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_TCP_REKEY);
1927 thp.purpose.size = htonl (sizeof(thp)); 2046 thp.purpose.size = htonl (sizeof(thp));
2047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2048 "inject_rekey size %u\n",
2049 thp.purpose.size);
1928 thp.sender = my_identity; 2050 thp.sender = my_identity;
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 "sender %s\n",
2053 GNUNET_p2s (&thp.sender.public_key));
1929 thp.receiver = queue->target; 2054 thp.receiver = queue->target;
2055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2056 "receiver %s\n",
2057 GNUNET_p2s (&thp.receiver.public_key));
1930 thp.ephemeral = rekey.ephemeral; 2058 thp.ephemeral = rekey.ephemeral;
2059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2060 "ephemeral %s\n",
2061 GNUNET_e2s (&thp.ephemeral));
1931 thp.monotonic_time = rekey.monotonic_time; 2062 thp.monotonic_time = rekey.monotonic_time;
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "time %s\n",
2065 GNUNET_STRINGS_absolute_time_to_string (
2066 GNUNET_TIME_absolute_ntoh (thp.monotonic_time)));
1932 GNUNET_CRYPTO_eddsa_sign (my_private_key, 2067 GNUNET_CRYPTO_eddsa_sign (my_private_key,
1933 &thp, 2068 &thp,
1934 &rekey.sender_sig); 2069 &rekey.sender_sig);
@@ -2004,10 +2139,11 @@ queue_write (void *cls)
2004 queue->cwrite_off += queue->pwrite_off; 2139 queue->cwrite_off += queue->pwrite_off;
2005 queue->pwrite_off = 0; 2140 queue->pwrite_off = 0;
2006 } 2141 }
2007 if ((0 == queue->pwrite_off) && 2142 if ((-1 != unverified_size)&& ((0 == queue->pwrite_off) &&
2008 ((0 == queue->rekey_left_bytes) || 2143 ((0 == queue->rekey_left_bytes) ||
2009 (0 == 2144 (0 ==
2010 GNUNET_TIME_absolute_get_remaining (queue->rekey_time).rel_value_us))) 2145 GNUNET_TIME_absolute_get_remaining (
2146 queue->rekey_time).rel_value_us))))
2011 { 2147 {
2012 inject_rekey (queue); 2148 inject_rekey (queue);
2013 } 2149 }
@@ -2403,11 +2539,6 @@ decrypt_and_check_tc (struct Queue *queue,
2403static void 2539static void
2404free_proto_queue (struct ProtoQueue *pq) 2540free_proto_queue (struct ProtoQueue *pq)
2405{ 2541{
2406 if (NULL != pq->listen_task)
2407 {
2408 GNUNET_SCHEDULER_cancel (pq->listen_task);
2409 pq->listen_task = NULL;
2410 }
2411 if (NULL != pq->listen_sock) 2542 if (NULL != pq->listen_sock)
2412 { 2543 {
2413 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pq->listen_sock)); 2544 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pq->listen_sock));
@@ -2526,7 +2657,6 @@ proto_read_kx (void *cls)
2526 queue->address = pq->address; /* steals reference */ 2657 queue->address = pq->address; /* steals reference */
2527 queue->address_len = pq->address_len; 2658 queue->address_len = pq->address_len;
2528 queue->target = tc.sender; 2659 queue->target = tc.sender;
2529 queue->listen_task = pq->listen_task;
2530 queue->listen_sock = pq->listen_sock; 2660 queue->listen_sock = pq->listen_sock;
2531 queue->sock = pq->sock; 2661 queue->sock = pq->sock;
2532 2662
@@ -2569,6 +2699,9 @@ listen_cb (void *cls)
2569 struct ProtoQueue *pq; 2699 struct ProtoQueue *pq;
2570 struct ListenTask *lt; 2700 struct ListenTask *lt;
2571 2701
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2703 "listen_cb\n");
2704
2572 lt = cls; 2705 lt = cls;
2573 2706
2574 lt->listen_task = NULL; 2707 lt->listen_task = NULL;
@@ -2707,7 +2840,7 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2707 struct Queue *queue; 2840 struct Queue *queue;
2708 const char *path; 2841 const char *path;
2709 struct sockaddr *in; 2842 struct sockaddr *in;
2710 socklen_t in_len; 2843 socklen_t in_len = 0;
2711 struct GNUNET_NETWORK_Handle *sock; 2844 struct GNUNET_NETWORK_Handle *sock;
2712 2845
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -2729,6 +2862,10 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2729 return GNUNET_SYSERR; 2862 return GNUNET_SYSERR;
2730 } 2863 }
2731 2864
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2866 "in %s\n",
2867 GNUNET_a2s (in, in_len));
2868
2732 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP); 2869 sock = GNUNET_NETWORK_socket_create (in->sa_family, SOCK_STREAM, IPPROTO_TCP);
2733 if (NULL == sock) 2870 if (NULL == sock)
2734 { 2871 {
@@ -2778,6 +2915,36 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2778} 2915}
2779 2916
2780/** 2917/**
2918 * Iterator over all ListenTasks to clean up.
2919 *
2920 * @param cls NULL
2921 * @param key unused
2922 * @param value the ListenTask to cancel.
2923 * @return #GNUNET_OK to continue to iterate
2924 */
2925static int
2926get_lt_delete_it (void *cls,
2927 const struct GNUNET_HashCode *key,
2928 void *value)
2929{
2930 struct ListenTask *lt = value;
2931
2932 (void) cls;
2933 (void) key;
2934 if (NULL != lt->listen_task)
2935 {
2936 GNUNET_SCHEDULER_cancel (lt->listen_task);
2937 lt->listen_task = NULL;
2938 }
2939 if (NULL != lt->listen_sock)
2940 {
2941 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (lt->listen_sock));
2942 lt->listen_sock = NULL;
2943 }
2944 return GNUNET_OK;
2945}
2946
2947/**
2781 * Iterator over all message queues to clean up. 2948 * Iterator over all message queues to clean up.
2782 * 2949 *
2783 * @param cls NULL 2950 * @param cls NULL
@@ -2798,7 +2965,6 @@ get_queue_delete_it (void *cls,
2798 return GNUNET_OK; 2965 return GNUNET_OK;
2799} 2966}
2800 2967
2801
2802/** 2968/**
2803 * Shutdown the UNIX communicator. 2969 * Shutdown the UNIX communicator.
2804 * 2970 *
@@ -2807,6 +2973,15 @@ get_queue_delete_it (void *cls,
2807static void 2973static void
2808do_shutdown (void *cls) 2974do_shutdown (void *cls)
2809{ 2975{
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2977 "Shutdown %s!\n",
2978 shutdown_running ? "running" : "not running");
2979
2980 if (GNUNET_YES == shutdown_running)
2981 return;
2982 else
2983 shutdown_running = GNUNET_YES;
2984
2810 while (NULL != proto_head) 2985 while (NULL != proto_head)
2811 free_proto_queue (proto_head); 2986 free_proto_queue (proto_head);
2812 if (NULL != nat) 2987 if (NULL != nat)
@@ -2814,8 +2989,10 @@ do_shutdown (void *cls)
2814 GNUNET_NAT_unregister (nat); 2989 GNUNET_NAT_unregister (nat);
2815 nat = NULL; 2990 nat = NULL;
2816 } 2991 }
2992 GNUNET_CONTAINER_multihashmap_iterate (lt_map, &get_lt_delete_it, NULL);
2817 GNUNET_CONTAINER_multipeermap_iterate (queue_map, &get_queue_delete_it, NULL); 2993 GNUNET_CONTAINER_multipeermap_iterate (queue_map, &get_queue_delete_it, NULL);
2818 GNUNET_CONTAINER_multipeermap_destroy (queue_map); 2994 GNUNET_CONTAINER_multipeermap_destroy (queue_map);
2995 GNUNET_TRANSPORT_communicator_address_remove_all (ch);
2819 if (NULL != ch) 2996 if (NULL != ch)
2820 { 2997 {
2821 GNUNET_TRANSPORT_communicator_disconnect (ch); 2998 GNUNET_TRANSPORT_communicator_disconnect (ch);
@@ -2836,6 +3013,18 @@ do_shutdown (void *cls)
2836 GNUNET_NT_scanner_done (is); 3013 GNUNET_NT_scanner_done (is);
2837 is = NULL; 3014 is = NULL;
2838 } 3015 }
3016 if (NULL != peerstore)
3017 {
3018 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
3019 peerstore = NULL;
3020 }
3021 if (NULL != resolve_request_handle)
3022 {
3023 GNUNET_RESOLVER_request_cancel (resolve_request_handle);
3024 resolve_request_handle = NULL;
3025 }
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3027 "Shutdown done!\n");
2839} 3028}
2840 3029
2841 3030
@@ -2886,6 +3075,11 @@ nat_address_cb (void *cls,
2886 char *my_addr; 3075 char *my_addr;
2887 struct GNUNET_TRANSPORT_AddressIdentifier *ai; 3076 struct GNUNET_TRANSPORT_AddressIdentifier *ai;
2888 3077
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3079 "nat address cb %s %s\n",
3080 add_remove ? "add" : "remove",
3081 GNUNET_a2s (addr, addrlen));
3082
2889 if (GNUNET_YES == add_remove) 3083 if (GNUNET_YES == add_remove)
2890 { 3084 {
2891 enum GNUNET_NetworkType nt; 3085 enum GNUNET_NetworkType nt;
@@ -2912,6 +3106,35 @@ nat_address_cb (void *cls,
2912} 3106}
2913 3107
2914/** 3108/**
3109 * This method adds addresses to the DLL, that are later register at the NAT service.
3110 */
3111static void
3112add_addr (struct sockaddr *in, socklen_t in_len)
3113{
3114
3115 struct Addresses *saddrs;
3116
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3118 "add address %s\n",
3119 GNUNET_a2s (in, in_len));
3120
3121 saddrs = GNUNET_new (struct Addresses);
3122 saddrs->addr = in;
3123 saddrs->addr_len = in_len;
3124 GNUNET_CONTAINER_DLL_insert (addrs_head, addrs_tail, saddrs);
3125
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3127 "after add address %s\n",
3128 GNUNET_a2s (in, in_len));
3129
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3131 "add address %s\n",
3132 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3133
3134 addrs_lens++;
3135}
3136
3137/**
2915 * This method launch network interactions for each address we like to bind to. 3138 * This method launch network interactions for each address we like to bind to.
2916 * 3139 *
2917 * @param addr The address we will listen to. 3140 * @param addr The address we will listen to.
@@ -2919,13 +3142,14 @@ nat_address_cb (void *cls,
2919 * @return GNUNET_SYSERR in case of error. GNUNET_OK in case we are successfully listen to the address. 3142 * @return GNUNET_SYSERR in case of error. GNUNET_OK in case we are successfully listen to the address.
2920 */ 3143 */
2921static int 3144static int
2922init_socket (const struct sockaddr *addr, 3145init_socket (struct sockaddr *addr,
2923 socklen_t in_len) 3146 socklen_t in_len)
2924{ 3147{
2925 struct sockaddr_storage in_sto; 3148 struct sockaddr_storage in_sto;
2926 socklen_t sto_len; 3149 socklen_t sto_len;
2927 struct GNUNET_NETWORK_Handle *listen_sock; 3150 struct GNUNET_NETWORK_Handle *listen_sock;
2928 struct ListenTask *lt; 3151 struct ListenTask *lt;
3152 struct GNUNET_HashCode h_sock;
2929 3153
2930 if (NULL == addr) 3154 if (NULL == addr)
2931 { 3155 {
@@ -2977,13 +3201,12 @@ init_socket (const struct sockaddr *addr,
2977 sto_len = in_len; 3201 sto_len = in_len;
2978 } 3202 }
2979 3203
2980 addr = (struct sockaddr *) &in_sto; 3204 // addr = (struct sockaddr *) &in_sto;
2981 in_len = sto_len; 3205 in_len = sto_len;
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2983 "Bound to `%s'\n", 3207 "Bound to `%s'\n",
2984 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len)); 3208 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
2985 stats = GNUNET_STATISTICS_create ("C-TCP", cfg); 3209 stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
2986 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
2987 3210
2988 if (NULL == is) 3211 if (NULL == is)
2989 is = GNUNET_NT_scanner_init (); 3212 is = GNUNET_NT_scanner_init ();
@@ -3012,6 +3235,27 @@ init_socket (const struct sockaddr *addr,
3012 &listen_cb, 3235 &listen_cb,
3013 lt); 3236 lt);
3014 3237
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3239 "creating hash\n");
3240 GNUNET_CRYPTO_hash (lt->listen_sock,
3241 sizeof(lt->listen_sock),
3242 &h_sock);
3243
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3245 "creating map\n");
3246 if (NULL == lt_map)
3247 lt_map = GNUNET_CONTAINER_multihashmap_create (2, GNUNET_NO);
3248
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 "creating map entry\n");
3251 GNUNET_CONTAINER_multihashmap_put (lt_map,
3252 &h_sock,
3253 lt,
3254 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
3255
3256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3257 "map entry created\n");
3258
3015 if (NULL == queue_map) 3259 if (NULL == queue_map)
3016 queue_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO); 3260 queue_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3017 3261
@@ -3034,6 +3278,7 @@ init_socket (const struct sockaddr *addr,
3034 return GNUNET_SYSERR; 3278 return GNUNET_SYSERR;
3035 } 3279 }
3036 3280
3281 add_addr (addr, in_len);
3037 return GNUNET_OK; 3282 return GNUNET_OK;
3038 3283
3039} 3284}
@@ -3050,6 +3295,10 @@ nat_register ()
3050 int i; 3295 int i;
3051 struct Addresses *pos; 3296 struct Addresses *pos;
3052 3297
3298
3299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3300 "starting nat register!\n");
3301
3053 i = 0; 3302 i = 0;
3054 saddrs = GNUNET_malloc ((addrs_lens + 1) * sizeof(struct sockaddr *)); 3303 saddrs = GNUNET_malloc ((addrs_lens + 1) * sizeof(struct sockaddr *));
3055 3304
@@ -3058,9 +3307,12 @@ nat_register ()
3058 for (pos = addrs_head; NULL != pos; pos = pos->next) 3307 for (pos = addrs_head; NULL != pos; pos = pos->next)
3059 { 3308 {
3060 3309
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3311 "registering address %s\n",
3312 GNUNET_a2s (addrs_head->addr, addrs_head->addr_len));
3313
3061 saddr_lens[i] = addrs_head->addr_len; 3314 saddr_lens[i] = addrs_head->addr_len;
3062 saddrs[i] = GNUNET_malloc (saddr_lens[i]); 3315 saddrs[i] = GNUNET_memdup (addrs_head->addr, saddr_lens[i]);
3063 saddrs[i] = addrs_head->addr;
3064 3316
3065 i++; 3317 i++;
3066 3318
@@ -3078,8 +3330,8 @@ nat_register ()
3078 3330
3079 i = 0; 3331 i = 0;
3080 3332
3081 /*for (i = addrs_lens - 1; i >= 0; i--) 3333 for (i = addrs_lens - 1; i >= 0; i--)
3082 GNUNET_free (saddrs[i]);*/ 3334 GNUNET_free (saddrs[i]);
3083 GNUNET_free (saddrs); 3335 GNUNET_free (saddrs);
3084 GNUNET_free (saddr_lens); 3336 GNUNET_free (saddr_lens);
3085 3337
@@ -3093,22 +3345,6 @@ nat_register ()
3093} 3345}
3094 3346
3095/** 3347/**
3096 * This method adds addresses to the DLL, that are later register at the NAT service.
3097 */
3098static void
3099add_addr (struct sockaddr *in, socklen_t in_len)
3100{
3101
3102 struct Addresses *saddrs;
3103
3104 saddrs = GNUNET_new (struct Addresses);
3105 saddrs->addr = in;
3106 saddrs->addr_len = in_len;
3107 GNUNET_CONTAINER_DLL_insert (addrs_head, addrs_tail, saddrs);
3108 addrs_lens++;
3109}
3110
3111/**
3112 * This method is the callback called by the resolver API, and wraps method init_socket. 3348 * This method is the callback called by the resolver API, and wraps method init_socket.
3113 * 3349 *
3114 * @param cls The port we will bind to. 3350 * @param cls The port we will bind to.
@@ -3123,22 +3359,19 @@ init_socket_resolv (void *cls,
3123 struct sockaddr_in *v4; 3359 struct sockaddr_in *v4;
3124 struct sockaddr_in6 *v6; 3360 struct sockaddr_in6 *v6;
3125 struct sockaddr *in; 3361 struct sockaddr *in;
3126 unsigned int *port;
3127 3362
3128 port = cls; 3363 (void) cls;
3129 if (NULL != addr) 3364 if (NULL != addr)
3130 { 3365 {
3131 if (AF_INET == addr->sa_family) 3366 if (AF_INET == addr->sa_family)
3132 { 3367 {
3133 v4 = (struct sockaddr_in *) addr; 3368 v4 = (struct sockaddr_in *) addr;
3134 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, *port);// _global); 3369 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, *v4, bind_port);// _global);
3135 add_addr (in, in_len);
3136 } 3370 }
3137 else if (AF_INET6 == addr->sa_family) 3371 else if (AF_INET6 == addr->sa_family)
3138 { 3372 {
3139 v6 = (struct sockaddr_in6 *) addr; 3373 v6 = (struct sockaddr_in6 *) addr;
3140 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, *port);// _global); 3374 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, *v6, bind_port);// _global);
3141 add_addr (in, in_len);
3142 } 3375 }
3143 else 3376 else
3144 { 3377 {
@@ -3149,14 +3382,20 @@ init_socket_resolv (void *cls,
3149 AF_INET6); 3382 AF_INET6);
3150 return; 3383 return;
3151 } 3384 }
3152 init_socket (in, 3385 init_socket (in, in_len);
3153 in_len);
3154 } 3386 }
3155 else 3387 else
3156 { 3388 {
3157 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 3389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3158 "Address is NULL. This might be an error or the resolver finished resolving.\n"); 3390 "Address is NULL. This might be an error or the resolver finished resolving.\n");
3391 if (NULL == addrs_head)
3392 {
3393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3394 "Resolver finished resolving, but we do not listen to an address!.\n");
3395 return;
3396 }
3159 nat_register (); 3397 nat_register ();
3398
3160 } 3399 }
3161} 3400}
3162 3401
@@ -3221,12 +3460,11 @@ run (void *cls,
3221 return; 3460 return;
3222 } 3461 }
3223 3462
3224 // cp = GNUNET_strdup (bindto); 3463 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
3225 start = extract_address (bindto);
3226 3464
3227 if (1 == sscanf (bindto, "%u%1s", &port, dummy)) 3465 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
3228 { 3466 {
3229 po = tcp_address_to_sockaddr_port_only (bindto, &port); 3467 po = tcp_address_to_sockaddr_port_only (bindto, &bind_port);
3230 3468
3231 addr_len_ipv4 = po->addr_len_ipv4; 3469 addr_len_ipv4 = po->addr_len_ipv4;
3232 3470
@@ -3238,46 +3476,55 @@ run (void *cls,
3238 if (NULL != po->addr_ipv4) 3476 if (NULL != po->addr_ipv4)
3239 { 3477 {
3240 init_socket (po->addr_ipv4, addr_len_ipv4); 3478 init_socket (po->addr_ipv4, addr_len_ipv4);
3241 add_addr (po->addr_ipv4, addr_len_ipv4);
3242 } 3479 }
3243 3480
3244 if (NULL != po->addr_ipv6) 3481 if (NULL != po->addr_ipv6)
3245 { 3482 {
3246 addr_len_ipv6 = po->addr_len_ipv6; 3483 addr_len_ipv6 = po->addr_len_ipv6;
3247 init_socket (po->addr_ipv6, addr_len_ipv6); 3484 init_socket (po->addr_ipv6, addr_len_ipv6);
3248 add_addr (po->addr_ipv6, addr_len_ipv6);
3249 } 3485 }
3250 3486
3487 GNUNET_free (po);
3251 nat_register (); 3488 nat_register ();
3489 GNUNET_free (bindto);
3490
3491 return;
3252 } 3492 }
3253 else if (1 == inet_pton (AF_INET, start, &v4.sin_addr)) 3493
3494 start = extract_address (bindto);
3495
3496 if (1 == inet_pton (AF_INET, start, &v4.sin_addr))
3254 { 3497 {
3255 port = extract_port (bindto); 3498 bind_port = extract_port (bindto);
3256 3499
3257 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, port); 3500 in = tcp_address_to_sockaddr_numeric_v4 (&in_len, v4, bind_port);
3258 init_socket (in, in_len); 3501 init_socket (in, in_len);
3259 add_addr (in, in_len);
3260 nat_register (); 3502 nat_register ();
3503 GNUNET_free (bindto);
3504
3505 return;
3261 } 3506 }
3262 else if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr)) 3507
3508 if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
3263 { 3509 {
3264 port = extract_port (bindto); 3510 bind_port = extract_port (bindto);
3265 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, port); 3511 in = tcp_address_to_sockaddr_numeric_v6 (&in_len, v6, bind_port);
3266 init_socket (in, in_len); 3512 init_socket (in, in_len);
3267 add_addr (in, in_len);
3268 nat_register (); 3513 nat_register ();
3269 } 3514 GNUNET_free (bindto);
3270 else
3271 {
3272 port = extract_port (bindto);
3273 3515
3274 resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto, ":", 3516 return;
3275 &rest),
3276 AF_UNSPEC,
3277 GNUNET_TIME_UNIT_MINUTES,
3278 &init_socket_resolv,
3279 &port);
3280 } 3517 }
3518
3519
3520 bind_port = extract_port (bindto);
3521
3522 resolve_request_handle = GNUNET_RESOLVER_ip_get (strtok_r (bindto, ":",
3523 &rest),
3524 AF_UNSPEC,
3525 GNUNET_TIME_UNIT_MINUTES,
3526 &init_socket_resolv,
3527 &port);
3281 GNUNET_free (bindto); 3528 GNUNET_free (bindto);
3282} 3529}
3283 3530