From 205e3bf8b151fcce80e8682fa5dc722356b11689 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Philipp=20T=C3=B6lke?= Date: Wed, 2 Nov 2011 10:13:59 +0000 Subject: [PATCH] handle client connections fix #1855 --- src/vpn/gnunet-daemon-vpn-helper.c | 4 +-- src/vpn/gnunet-daemon-vpn.c | 12 +++---- src/vpn/gnunet-service-dns-p.h | 4 +-- src/vpn/gnunet-service-dns.c | 54 +++++++++++++++++++++++------- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/vpn/gnunet-daemon-vpn-helper.c b/src/vpn/gnunet-daemon-vpn-helper.c index 81a52e222..124b53bf1 100644 --- a/src/vpn/gnunet-daemon-vpn-helper.c +++ b/src/vpn/gnunet-daemon-vpn-helper.c @@ -344,7 +344,7 @@ message_token (void *cls __attribute__ ((unused)), void *client size_t len = sizeof (struct query_packet) + ntohs (pkt6_udp->udp_hdr.len) - 9; struct query_packet_list *query = - GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *)); + GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS); query->pkt.hdr.size = htons (len); memcpy(query->pkt.orig_to, &pkt6->ip6_hdr.dadr, 16); @@ -538,7 +538,7 @@ message_token (void *cls __attribute__ ((unused)), void *client size_t len = sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9; struct query_packet_list *query = - GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *)); + GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS); query->pkt.hdr.size = htons (len); memcpy(query->pkt.orig_to, &pkt->ip_hdr.dadr, 4); diff --git a/src/vpn/gnunet-daemon-vpn.c b/src/vpn/gnunet-daemon-vpn.c index d87edfbc4..1191dffe1 100644 --- a/src/vpn/gnunet-daemon-vpn.c +++ b/src/vpn/gnunet-daemon-vpn.c @@ -574,7 +574,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) list = GNUNET_malloc (htons (pkt->hdr.size) + - 2 * sizeof (struct answer_packet_list *)); + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); @@ -622,8 +622,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) char *name = (char *) (map_entry + 1); list = - GNUNET_malloc (2 * sizeof (struct answer_packet_list *) + offset + 2 + - ntohs (namelen)); + GNUNET_malloc (sizeof(struct answer_packet_list) - sizeof(struct answer_packet) + offset + 2 + + ntohs (namelen)); struct answer_packet *rpkt = &list->pkt; @@ -641,7 +641,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { list = GNUNET_malloc (htons (pkt->hdr.size) + - 2 * sizeof (struct answer_packet_list *)); + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); } else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA) @@ -701,7 +701,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) list = GNUNET_malloc (htons (pkt->hdr.size) + - 2 * sizeof (struct answer_packet_list *)); + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); } @@ -763,7 +763,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) list = GNUNET_malloc (htons (pkt->hdr.size) + - 2 * sizeof (struct answer_packet_list *)); + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); } diff --git a/src/vpn/gnunet-service-dns-p.h b/src/vpn/gnunet-service-dns-p.h index 020700385..13b410e2f 100644 --- a/src/vpn/gnunet-service-dns-p.h +++ b/src/vpn/gnunet-service-dns-p.h @@ -102,10 +102,10 @@ struct answer_packet unsigned char data[1]; }; -struct answer_packet_list -{ +struct answer_packet_list { struct answer_packet_list *next GNUNET_PACKED; struct answer_packet_list *prev GNUNET_PACKED; + struct GNUNET_SERVER_Client *client; struct answer_packet pkt; }; diff --git a/src/vpn/gnunet-service-dns.c b/src/vpn/gnunet-service-dns.c index 806c4745a..6f9d0f1ad 100644 --- a/src/vpn/gnunet-service-dns.c +++ b/src/vpn/gnunet-service-dns.c @@ -125,6 +125,27 @@ struct tunnel_state struct GNUNET_MESH_TransmitHandle *th; }; +static void +client_disconnect(void* cls, struct GNUNET_SERVER_Client *client) +{ + if (NULL == head) return; + + struct answer_packet_list *element = head; + while (element != NULL) + { + if (element->client == client) + { + GNUNET_SERVER_client_drop(client); + GNUNET_CONTAINER_DLL_remove(head, tail, element); + struct answer_packet_list* t = element; + element = element->next; + GNUNET_free(t); + } + else + element = element->next; + } +} + /** * Hijack all outgoing DNS-Traffic but for traffic leaving "our" port. */ @@ -238,14 +259,14 @@ send_answer (void *cls, size_t size, void *buf) GNUNET_CONTAINER_DLL_remove (head, tail, query); - GNUNET_free (query); - /* When more data is to be sent, reschedule */ if (head != NULL) - server_notify = GNUNET_SERVER_notify_transmit_ready (cls, ntohs (head->pkt.hdr.size), + server_notify = GNUNET_SERVER_notify_transmit_ready (head->client, ntohs (head->pkt.hdr.size), GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, cls); + &send_answer, NULL); + GNUNET_SERVER_client_drop(query->client); + GNUNET_free (query); return len; } @@ -496,7 +517,7 @@ receive_mesh_answer (void *cls + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */ struct answer_packet_list *answer = - GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *)); + GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); answer->pkt.hdr.size = htons (len); @@ -599,12 +620,13 @@ receive_mesh_answer (void *cls (unsigned long) (&answer->pkt))); GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); + answer->client = query_states[dns->s.id].client; if (server_notify == NULL) server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client, len, GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, - query_states[dns->s.id].client); + NULL); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent answer of length %d on to client, addroffset = %d\n", len, @@ -639,7 +661,7 @@ send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 2 /* We do not know the lenght of the answer yet */ ; struct answer_packet_list *answer = - GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *)); + GNUNET_malloc (len + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); answer->pkt.hdr.size = htons (len); @@ -692,11 +714,12 @@ send_rev_query (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) (unsigned long) (&answer->pkt))); GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); + answer->client = query_states[id].client; if (server_notify == NULL) server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len, GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, query_states[id].client); + &send_answer, NULL); } /** @@ -739,11 +762,13 @@ receive_dht (void *cls, struct GNUNET_TIME_Absolute exp + sizeof (struct dns_record_line) - 1 + 16; /* To hold the IPv6-Address */ struct answer_packet_list *answer = - GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *)); + GNUNET_malloc (len + + sizeof(struct answer_packet_list) - sizeof(struct answer_packet)); answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); answer->pkt.hdr.size = htons (len); answer->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_SERVICE; + answer->client = query_states[id].client; GNUNET_CRYPTO_hash (&rec->peer, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), @@ -808,9 +833,9 @@ receive_dht (void *cls, struct GNUNET_TIME_Absolute exp GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); if (server_notify == NULL) - server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[id].client, len, + server_notify = GNUNET_SERVER_notify_transmit_ready (answer->client, len, GNUNET_TIME_UNIT_FOREVER_REL, - &send_answer, query_states[id].client); + &send_answer, NULL); } /** @@ -841,6 +866,7 @@ receive_query (void *cls query_states[dns->s.id].valid = GNUNET_YES; query_states[dns->s.id].client = client; + GNUNET_SERVER_client_keep(client); memcpy(query_states[dns->s.id].local_ip, pkt->orig_from, pkt->addrlen); query_states[dns->s.id].addrlen = pkt->addrlen; query_states[dns->s.id].local_port = pkt->src_port; @@ -1329,7 +1355,7 @@ handle_response(struct dns_pkt* dns, struct sockaddr *addr, socklen_t addrlen, i size_t len = sizeof (struct answer_packet) + r - 1; /* 1 for the unsigned char data[1]; */ struct answer_packet_list *answer = - GNUNET_malloc (len + 2 * sizeof (struct answer_packet_list *)); + GNUNET_malloc (len + sizeof(struct answer_packet_list) - (sizeof(struct answer_packet))); answer->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_RESPONSE_DNS); answer->pkt.hdr.size = htons (len); @@ -1355,6 +1381,7 @@ handle_response(struct dns_pkt* dns, struct sockaddr *addr, socklen_t addrlen, i GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "sending answer with addrlen = %d\n", addrlen); answer->pkt.dst_port = query_states[dns->s.id].local_port; memcpy (answer->pkt.data, dns, r); + answer->client = query_states[dns->s.id].client; GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, answer); @@ -1362,7 +1389,7 @@ handle_response(struct dns_pkt* dns, struct sockaddr *addr, socklen_t addrlen, i server_notify = GNUNET_SERVER_notify_transmit_ready (query_states[dns->s.id].client, len, GNUNET_TIME_UNIT_FOREVER_REL, &send_answer, - query_states[dns->s.id].client); + NULL); } } GNUNET_free(addr); @@ -1662,6 +1689,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, dht = GNUNET_DHT_connect (cfg, 1024); GNUNET_SCHEDULER_add_now (publish_names, NULL); GNUNET_SERVER_add_handlers (server, handlers); + GNUNET_SERVER_disconnect_notify(server, &client_disconnect, NULL); GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task, cls); } -- 2.25.1