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.c137
1 files changed, 98 insertions, 39 deletions
diff --git a/src/transport/gnunet-communicator-tcp.c b/src/transport/gnunet-communicator-tcp.c
index 4caef909c..59f42496a 100644
--- a/src/transport/gnunet-communicator-tcp.c
+++ b/src/transport/gnunet-communicator-tcp.c
@@ -394,11 +394,6 @@ struct Queue
394 struct GNUNET_PeerIdentity target; 394 struct GNUNET_PeerIdentity target;
395 395
396 /** 396 /**
397 * ID of listen task
398 */
399 struct GNUNET_SCHEDULER_Task *listen_task;
400
401 /**
402 * Listen socket. 397 * Listen socket.
403 */ 398 */
404 struct GNUNET_NETWORK_Handle *listen_sock; 399 struct GNUNET_NETWORK_Handle *listen_sock;
@@ -638,11 +633,6 @@ struct ProtoQueue
638 struct ProtoQueue *prev; 633 struct ProtoQueue *prev;
639 634
640 /** 635 /**
641 * ID of listen task
642 */
643 struct GNUNET_SCHEDULER_Task *listen_task;
644
645 /**
646 * Listen socket. 636 * Listen socket.
647 */ 637 */
648 struct GNUNET_NETWORK_Handle *listen_sock; 638 struct GNUNET_NETWORK_Handle *listen_sock;
@@ -761,6 +751,11 @@ static struct GNUNET_TRANSPORT_CommunicatorHandle *ch;
761static struct GNUNET_CONTAINER_MultiPeerMap *queue_map; 751static struct GNUNET_CONTAINER_MultiPeerMap *queue_map;
762 752
763/** 753/**
754 * ListenTasks (map from socket to `struct ListenTask`)
755 */
756static struct GNUNET_CONTAINER_MultiHashMap *lt_map;
757
758/**
764 * Our public key. 759 * Our public key.
765 */ 760 */
766static struct GNUNET_PeerIdentity my_identity; 761static struct GNUNET_PeerIdentity my_identity;
@@ -816,6 +811,16 @@ struct Addresses *addrs_head;
816struct Addresses *addrs_tail; 811struct Addresses *addrs_tail;
817 812
818/** 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/**
819 * Number of addresses in the DLL for register at NAT service. 824 * Number of addresses in the DLL for register at NAT service.
820 */ 825 */
821int addrs_lens; 826int addrs_lens;
@@ -850,7 +855,6 @@ unsigned int bind_port;
850static void 855static void
851listen_cb (void *cls); 856listen_cb (void *cls);
852 857
853
854/** 858/**
855 * Functions with this signature are called whenever we need 859 * Functions with this signature are called whenever we need
856 * to close a queue due to a disconnect or failure to 860 * to close a queue due to a disconnect or failure to
@@ -861,10 +865,14 @@ listen_cb (void *cls);
861static void 865static void
862queue_destroy (struct Queue *queue) 866queue_destroy (struct Queue *queue)
863{ 867{
864 struct ListenTask *lt; 868 struct ListenTask *lt = NULL;
865 lt = GNUNET_new (struct ListenTask); 869 struct GNUNET_HashCode h_sock;
866 lt->listen_sock = queue->listen_sock; 870
867 lt->listen_task = queue->listen_task; 871 GNUNET_CRYPTO_hash (queue->listen_sock,
872 sizeof(queue->listen_sock),
873 &h_sock);
874
875 lt = GNUNET_CONTAINER_multihashmap_get (lt_map, &h_sock);
868 876
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Disconnecting queue for peer `%s'\n", 878 "Disconnecting queue for peer `%s'\n",
@@ -934,7 +942,7 @@ queue_destroy (struct Queue *queue)
934 else 942 else
935 GNUNET_free (queue); 943 GNUNET_free (queue);
936 944
937 if ((NULL != lt->listen_sock) && (NULL == lt->listen_task)) 945 if ((! shutdown_running) && (NULL == lt->listen_task))
938 { 946 {
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "add read net listen\n"); 948 "add read net listen\n");
@@ -1982,7 +1990,8 @@ tcp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1982 // colon = strrchr (cp, ':'); 1990 // colon = strrchr (cp, ':');
1983 port = extract_port (bindto); 1991 port = extract_port (bindto);
1984 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port); 1992 in = tcp_address_to_sockaddr_numeric_v6 (sock_len, v6, port);
1985 }else{ 1993 }
1994 else{
1986 GNUNET_assert (0); 1995 GNUNET_assert (0);
1987 } 1996 }
1988 1997
@@ -2530,11 +2539,6 @@ decrypt_and_check_tc (struct Queue *queue,
2530static void 2539static void
2531free_proto_queue (struct ProtoQueue *pq) 2540free_proto_queue (struct ProtoQueue *pq)
2532{ 2541{
2533 if (NULL != pq->listen_task)
2534 {
2535 GNUNET_SCHEDULER_cancel (pq->listen_task);
2536 pq->listen_task = NULL;
2537 }
2538 if (NULL != pq->listen_sock) 2542 if (NULL != pq->listen_sock)
2539 { 2543 {
2540 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pq->listen_sock)); 2544 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (pq->listen_sock));
@@ -2653,7 +2657,6 @@ proto_read_kx (void *cls)
2653 queue->address = pq->address; /* steals reference */ 2657 queue->address = pq->address; /* steals reference */
2654 queue->address_len = pq->address_len; 2658 queue->address_len = pq->address_len;
2655 queue->target = tc.sender; 2659 queue->target = tc.sender;
2656 queue->listen_task = pq->listen_task;
2657 queue->listen_sock = pq->listen_sock; 2660 queue->listen_sock = pq->listen_sock;
2658 queue->sock = pq->sock; 2661 queue->sock = pq->sock;
2659 2662
@@ -2696,6 +2699,9 @@ listen_cb (void *cls)
2696 struct ProtoQueue *pq; 2699 struct ProtoQueue *pq;
2697 struct ListenTask *lt; 2700 struct ListenTask *lt;
2698 2701
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2703 "listen_cb\n");
2704
2699 lt = cls; 2705 lt = cls;
2700 2706
2701 lt->listen_task = NULL; 2707 lt->listen_task = NULL;
@@ -2909,6 +2915,36 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2909} 2915}
2910 2916
2911/** 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/**
2912 * Iterator over all message queues to clean up. 2948 * Iterator over all message queues to clean up.
2913 * 2949 *
2914 * @param cls NULL 2950 * @param cls NULL
@@ -2925,16 +2961,10 @@ get_queue_delete_it (void *cls,
2925 2961
2926 (void) cls; 2962 (void) cls;
2927 (void) target; 2963 (void) target;
2928 if (NULL != queue->listen_task)
2929 {
2930 GNUNET_SCHEDULER_cancel (queue->listen_task);
2931 queue->listen_task = NULL;
2932 }
2933 queue_destroy (queue); 2964 queue_destroy (queue);
2934 return GNUNET_OK; 2965 return GNUNET_OK;
2935} 2966}
2936 2967
2937
2938/** 2968/**
2939 * Shutdown the UNIX communicator. 2969 * Shutdown the UNIX communicator.
2940 * 2970 *
@@ -2943,7 +2973,6 @@ get_queue_delete_it (void *cls,
2943static void 2973static void
2944do_shutdown (void *cls) 2974do_shutdown (void *cls)
2945{ 2975{
2946
2947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2948 "Shutdown %s!\n", 2977 "Shutdown %s!\n",
2949 shutdown_running ? "running" : "not running"); 2978 shutdown_running ? "running" : "not running");
@@ -2960,6 +2989,7 @@ do_shutdown (void *cls)
2960 GNUNET_NAT_unregister (nat); 2989 GNUNET_NAT_unregister (nat);
2961 nat = NULL; 2990 nat = NULL;
2962 } 2991 }
2992 GNUNET_CONTAINER_multihashmap_iterate (lt_map, &get_lt_delete_it, NULL);
2963 GNUNET_CONTAINER_multipeermap_iterate (queue_map, &get_queue_delete_it, NULL); 2993 GNUNET_CONTAINER_multipeermap_iterate (queue_map, &get_queue_delete_it, NULL);
2964 GNUNET_CONTAINER_multipeermap_destroy (queue_map); 2994 GNUNET_CONTAINER_multipeermap_destroy (queue_map);
2965 GNUNET_TRANSPORT_communicator_address_remove_all (ch); 2995 GNUNET_TRANSPORT_communicator_address_remove_all (ch);
@@ -2993,6 +3023,8 @@ do_shutdown (void *cls)
2993 GNUNET_RESOLVER_request_cancel (resolve_request_handle); 3023 GNUNET_RESOLVER_request_cancel (resolve_request_handle);
2994 resolve_request_handle = NULL; 3024 resolve_request_handle = NULL;
2995 } 3025 }
3026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3027 "Shutdown done!\n");
2996} 3028}
2997 3029
2998 3030
@@ -3043,10 +3075,10 @@ nat_address_cb (void *cls,
3043 char *my_addr; 3075 char *my_addr;
3044 struct GNUNET_TRANSPORT_AddressIdentifier *ai; 3076 struct GNUNET_TRANSPORT_AddressIdentifier *ai;
3045 3077
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "nat address cb %s %s\n", 3079 "nat address cb %s %s\n",
3048 add_remove ? "add" : "remove", 3080 add_remove ? "add" : "remove",
3049 GNUNET_a2s (addr, addrlen)); 3081 GNUNET_a2s (addr, addrlen));
3050 3082
3051 if (GNUNET_YES == add_remove) 3083 if (GNUNET_YES == add_remove)
3052 { 3084 {
@@ -3098,7 +3130,7 @@ add_addr (struct sockaddr *in, socklen_t in_len)
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3099 "add address %s\n", 3131 "add address %s\n",
3100 GNUNET_a2s (saddrs->addr, saddrs->addr_len)); 3132 GNUNET_a2s (saddrs->addr, saddrs->addr_len));
3101 3133
3102 addrs_lens++; 3134 addrs_lens++;
3103} 3135}
3104 3136
@@ -3117,6 +3149,7 @@ init_socket (struct sockaddr *addr,
3117 socklen_t sto_len; 3149 socklen_t sto_len;
3118 struct GNUNET_NETWORK_Handle *listen_sock; 3150 struct GNUNET_NETWORK_Handle *listen_sock;
3119 struct ListenTask *lt; 3151 struct ListenTask *lt;
3152 struct GNUNET_HashCode h_sock;
3120 3153
3121 if (NULL == addr) 3154 if (NULL == addr)
3122 { 3155 {
@@ -3168,13 +3201,12 @@ init_socket (struct sockaddr *addr,
3168 sto_len = in_len; 3201 sto_len = in_len;
3169 } 3202 }
3170 3203
3171 //addr = (struct sockaddr *) &in_sto; 3204 // addr = (struct sockaddr *) &in_sto;
3172 in_len = sto_len; 3205 in_len = sto_len;
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3174 "Bound to `%s'\n", 3207 "Bound to `%s'\n",
3175 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len)); 3208 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
3176 stats = GNUNET_STATISTICS_create ("C-TCP", cfg); 3209 stats = GNUNET_STATISTICS_create ("C-TCP", cfg);
3177 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
3178 3210
3179 if (NULL == is) 3211 if (NULL == is)
3180 is = GNUNET_NT_scanner_init (); 3212 is = GNUNET_NT_scanner_init ();
@@ -3203,6 +3235,27 @@ init_socket (struct sockaddr *addr,
3203 &listen_cb, 3235 &listen_cb,
3204 lt); 3236 lt);
3205 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
3206 if (NULL == queue_map) 3259 if (NULL == queue_map)
3207 queue_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO); 3260 queue_map = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3208 3261
@@ -3335,12 +3388,14 @@ init_socket_resolv (void *cls,
3335 { 3388 {
3336 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 3389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3337 "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");
3338 if (NULL == addrs_head){ 3391 if (NULL == addrs_head)
3392 {
3339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 3393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3340 "Resolver finished resolving, but we do not listen to an address!.\n"); 3394 "Resolver finished resolving, but we do not listen to an address!.\n");
3341 return; 3395 return;
3342 } 3396 }
3343 nat_register (); 3397 nat_register ();
3398
3344 } 3399 }
3345} 3400}
3346 3401
@@ -3405,6 +3460,8 @@ run (void *cls,
3405 return; 3460 return;
3406 } 3461 }
3407 3462
3463 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
3464
3408 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy)) 3465 if (1 == sscanf (bindto, "%u%1s", &bind_port, dummy))
3409 { 3466 {
3410 po = tcp_address_to_sockaddr_port_only (bindto, &bind_port); 3467 po = tcp_address_to_sockaddr_port_only (bindto, &bind_port);
@@ -3444,6 +3501,7 @@ run (void *cls,
3444 init_socket (in, in_len); 3501 init_socket (in, in_len);
3445 nat_register (); 3502 nat_register ();
3446 GNUNET_free (bindto); 3503 GNUNET_free (bindto);
3504
3447 return; 3505 return;
3448 } 3506 }
3449 3507
@@ -3454,6 +3512,7 @@ run (void *cls,
3454 init_socket (in, in_len); 3512 init_socket (in, in_len);
3455 nat_register (); 3513 nat_register ();
3456 GNUNET_free (bindto); 3514 GNUNET_free (bindto);
3515
3457 return; 3516 return;
3458 } 3517 }
3459 3518