From b75373955d92c8ac1e9554182025843c01cbb96c Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 2 Jan 2012 05:03:07 +0000 Subject: [PATCH] -again moving towards DNS API sanity --- src/dns/dns_api.c | 13 +-- src/dns/gnunet-service-dns.c | 6 +- src/include/gnunet_dns_service.h | 89 +++++++++++++----- src/vpn/gnunet-daemon-vpn.c | 157 ++++++++++++++----------------- src/vpn/gnunet-daemon-vpn.h | 28 +----- 5 files changed, 142 insertions(+), 151 deletions(-) diff --git a/src/dns/dns_api.c b/src/dns/dns_api.c index 68b703eba..7a1577365 100644 --- a/src/dns/dns_api.c +++ b/src/dns/dns_api.c @@ -19,7 +19,7 @@ */ /** - * @file vpn/gnunet-daemon-vpn-dns.c + * @file dns/dns_api.c * @brief * @author Philipp Toelke */ @@ -56,7 +56,7 @@ struct GNUNET_DNS_Handle const struct GNUNET_CONFIGURATION_Handle *cfg; - GNUNET_SCHEDULER_Task process_answer_cb; + GNUNET_DNS_ResponseCallback process_answer_cb; void *process_answer_cb_cls; }; @@ -177,11 +177,8 @@ dns_answer_handler (void *cls, #endif return; } - void *pkt = GNUNET_malloc (ntohs (msg->size)); - - memcpy (pkt, msg, ntohs (msg->size)); - - GNUNET_SCHEDULER_add_now (h->process_answer_cb, pkt); + h->process_answer_cb (h->process_answer_cb_cls, + (const struct answer_packet*) msg); GNUNET_CLIENT_receive (h->dns_connection, &dns_answer_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); } @@ -192,7 +189,7 @@ dns_answer_handler (void *cls, */ struct GNUNET_DNS_Handle * GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_SCHEDULER_Task cb, + GNUNET_DNS_ResponseCallback cb, void *cb_cls) { struct GNUNET_DNS_Handle *h; diff --git a/src/dns/gnunet-service-dns.c b/src/dns/gnunet-service-dns.c index 55c87b9c6..7d24e630f 100644 --- a/src/dns/gnunet-service-dns.c +++ b/src/dns/gnunet-service-dns.c @@ -386,7 +386,7 @@ mesh_send (void *cls, size_t size, void *buf) } -void +static void mesh_connect (void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED) { @@ -1212,7 +1212,7 @@ open_port () return GNUNET_YES; } -void +static void handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen, int r); @@ -1320,7 +1320,7 @@ read_response (void *cls GNUNET_UNUSED, &read_response, NULL); } -void +static void handle_response (struct dns_pkt *dns, struct sockaddr *addr, socklen_t addrlen, int r) { diff --git a/src/include/gnunet_dns_service.h b/src/include/gnunet_dns_service.h index c42f7787f..d697b9c16 100644 --- a/src/include/gnunet_dns_service.h +++ b/src/include/gnunet_dns_service.h @@ -37,38 +37,44 @@ #include "gnunet_util_lib.h" +/** + * Subtypes of DNS answers. + */ enum GNUNET_DNS_ANSWER_Subtype { - /** - * Answers of this type contain a dns-packet that just has to be transmitted - */ + /** + * Answers of this type contain a dns-packet that just has to be transmitted + */ GNUNET_DNS_ANSWER_TYPE_IP, - /** - * Answers of this type contain an incomplete dns-packet. The IP-Address - * is all 0s. The addroffset points to it. - */ + /** + * Answers of this type contain an incomplete dns-packet. The IP-Address + * is all 0s. The addroffset points to it. + */ GNUNET_DNS_ANSWER_TYPE_SERVICE, - /** - * Answers of this type contain an incomplete dns-packet as answer to a - * PTR-Query. The resolved name is not allocated. The addroffset points to it. - */ + /** + * Answers of this type contain an incomplete dns-packet as answer to a + * PTR-Query. The resolved name is not allocated. The addroffset points to it. + */ GNUNET_DNS_ANSWER_TYPE_REV, - - /** - * Answers of this type contains an IP6-Address but traffic to this IP should - * be routed through the GNUNet. - */ + + /** + * Answers of this type contains an IP6-Address but traffic to this IP should + * be routed through the GNUNet. + */ GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA, - - /** - * Answers of this type contains an IP4-Address but traffic to this IP should - * be routed through the GNUNet. - */ + + /** + * Answers of this type contains an IP4-Address but traffic to this IP should + * be routed through the GNUNet. + */ GNUNET_DNS_ANSWER_TYPE_REMOTE_A + }; + +GNUNET_NETWORK_STRUCT_BEGIN struct GNUNET_vpn_service_descriptor { GNUNET_HashCode peer GNUNET_PACKED; @@ -77,7 +83,7 @@ struct GNUNET_vpn_service_descriptor uint32_t service_type GNUNET_PACKED; }; -GNUNET_NETWORK_STRUCT_BEGIN + struct answer_packet { /* General data */ @@ -108,6 +114,8 @@ struct answer_packet unsigned char data[1]; }; +GNUNET_NETWORK_STRUCT_END + struct answer_packet_list { @@ -116,18 +124,45 @@ struct answer_packet_list struct GNUNET_SERVER_Client *client; struct answer_packet pkt; }; -GNUNET_NETWORK_STRUCT_END + +/** + * Type of a function to be called by the DNS API whenever + * a DNS reply is obtained. + * + * @param cls closure + * @param pkt reply that we got + */ +typedef void (*GNUNET_DNS_ResponseCallback)(void *cls, + const struct answer_packet *pkt); + + +/** + * Opaque DNS handle + */ struct GNUNET_DNS_Handle; + /** * Connect to the service-dns + * + * @param cfg configuration to use + * @param cb function to call with DNS replies + * @param cb_cls closure to pass to cb + * @return DNS handle */ struct GNUNET_DNS_Handle * GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - GNUNET_SCHEDULER_Task cb, + GNUNET_DNS_ResponseCallback cb, void *cb_cls); + +/** + * Signal the DNS service that it needs to re-initialize the DNS + * hijacking (the network setup has changed significantly). + * + * @param h DNS handle + */ void GNUNET_DNS_restart_hijack (struct GNUNET_DNS_Handle *h); @@ -170,7 +205,11 @@ GNUNET_DNS_queue_request_v6 (struct GNUNET_DNS_Handle *h, size_t udp_packet_len, const char *udp_packet); - +/** + * Disconnect from the DNS service. + * + * @param h DNS handle + */ void GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *h); diff --git a/src/vpn/gnunet-daemon-vpn.c b/src/vpn/gnunet-daemon-vpn.c index cd587d7b1..7d303ecd5 100644 --- a/src/vpn/gnunet-daemon-vpn.c +++ b/src/vpn/gnunet-daemon-vpn.c @@ -80,6 +80,47 @@ GNUNET_SCHEDULER_TaskIdentifier conn_task; GNUNET_SCHEDULER_TaskIdentifier shs_task; + +/** + * Sets a bit active in a bitArray. + * + * @param bitArray memory area to set the bit in + * @param bitIdx which bit to set + */ +static void +setBit (char *bitArray, unsigned int bitIdx) +{ + size_t arraySlot; + unsigned int targetBit; + + arraySlot = bitIdx / 8; + targetBit = (1L << (bitIdx % 8)); + bitArray[arraySlot] |= targetBit; +} + + +/** + * Checks if a bit is active in the bitArray + * + * @param bitArray memory area to set the bit in + * @param bitIdx which bit to test + * @return GNUNET_YES if the bit is set, GNUNET_NO if not. + */ +int +testBit (char *bitArray, unsigned int bitIdx) +{ + size_t slot; + unsigned int targetBit; + + slot = bitIdx / 8; + targetBit = (1L << (bitIdx % 8)); + if (bitArray[slot] & targetBit) + return GNUNET_YES; + else + return GNUNET_NO; +} + + /** * Function scheduled as very last function, cleans up after us *{{{ @@ -510,12 +551,9 @@ new_ip4addr_remote (unsigned char *buf, unsigned char *addr, char addrlen) * doing nothing for "real" services. */ void -process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +process_answer (void *cls, + const struct answer_packet *pkt) { - if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) - return; - - struct answer_packet *pkt = cls; struct answer_packet_list *list; /* This answer is about a .gnunet-service @@ -525,13 +563,18 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) */ if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_SERVICE) { - pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP; GNUNET_HashCode key; memset (&key, 0, sizeof (GNUNET_HashCode)); - unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset); + list = + GNUNET_malloc (htons (pkt->hdr.size) + + sizeof (struct answer_packet_list) - + sizeof (struct answer_packet)); + memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); + + unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (pkt->addroffset); unsigned char *k = (unsigned char *) &key; new_ip6addr ((struct in6_addr*) c, @@ -576,12 +619,8 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_free (value); - list = - GNUNET_malloc (htons (pkt->hdr.size) + - sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); + list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; - memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); } else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REV) @@ -590,7 +629,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) memset (&key, 0, sizeof key); unsigned char *k = (unsigned char *) &key; - unsigned char *s = pkt->data + 12; + const unsigned char *s = pkt->data + 12; int i = 0; /* Whoever designed the reverse IPv6-lookup is batshit insane */ @@ -614,10 +653,7 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) uint16_t offset = ntohs (pkt->addroffset); if (map_entry == NULL) - { - GNUNET_free (pkt); return; - } GNUNET_CONTAINER_heap_update_cost (heap, map_entry->heap_node, GNUNET_TIME_absolute_get ().abs_value); @@ -653,16 +689,23 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) } else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_AAAA) { - pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP; GNUNET_HashCode key; memset (&key, 0, sizeof (GNUNET_HashCode)); - unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset); + list = + GNUNET_malloc (htons (pkt->hdr.size) + + sizeof (struct answer_packet_list) - + sizeof (struct answer_packet)); + + memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); + list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; + + unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (list->pkt.addroffset); new_ip6addr_remote ((struct in6_addr*) c, - pkt->addr, pkt->addrsize); + list->pkt.addr, list->pkt.addrsize); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], @@ -707,24 +750,25 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) else GNUNET_free (value); + + } + else if (pkt->subtype == GNUNET_DNS_ANSWER_TYPE_REMOTE_A) + { list = GNUNET_malloc (htons (pkt->hdr.size) + 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_A) - { - pkt->subtype = GNUNET_DNS_ANSWER_TYPE_IP; + list->pkt.subtype = GNUNET_DNS_ANSWER_TYPE_IP; GNUNET_HashCode key; memset (&key, 0, sizeof (GNUNET_HashCode)); - unsigned char *c = ((unsigned char *) pkt) + ntohs (pkt->addroffset); + unsigned char *c = ((unsigned char *) &list->pkt) + ntohs (pkt->addroffset); - new_ip4addr_remote (c, pkt->addr, pkt->addrsize); + new_ip4addr_remote (c, list->pkt.addr, pkt->addrsize); GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New mapping to %d.%d.%d.%d\n", c[0], c[1], c[2], c[3]); unsigned char *k = (unsigned char *) &key; @@ -770,22 +814,13 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) else GNUNET_free (value); - list = - GNUNET_malloc (htons (pkt->hdr.size) + - sizeof (struct answer_packet_list) - - sizeof (struct answer_packet)); - - memcpy (&list->pkt, pkt, htons (pkt->hdr.size)); } else { GNUNET_break (0); - GNUNET_free (pkt); return; } - GNUNET_free (pkt); - GNUNET_CONTAINER_DLL_insert_after (answer_proc_head, answer_proc_tail, answer_proc_tail, list); @@ -794,60 +829,6 @@ process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) return; } -/** - * Sets a bit active in a bitArray. - * - * @param bitArray memory area to set the bit in - * @param bitIdx which bit to set - */ -void -setBit (char *bitArray, unsigned int bitIdx) -{ - size_t arraySlot; - unsigned int targetBit; - - arraySlot = bitIdx / 8; - targetBit = (1L << (bitIdx % 8)); - bitArray[arraySlot] |= targetBit; -} - -/** - * Clears a bit from bitArray. - * - * @param bitArray memory area to set the bit in - * @param bitIdx which bit to unset - */ -void -clearBit (char *bitArray, unsigned int bitIdx) -{ - size_t slot; - unsigned int targetBit; - - slot = bitIdx / 8; - targetBit = (1L << (bitIdx % 8)); - bitArray[slot] = bitArray[slot] & (~targetBit); -} - -/** - * Checks if a bit is active in the bitArray - * - * @param bitArray memory area to set the bit in - * @param bitIdx which bit to test - * @return GNUNET_YES if the bit is set, GNUNET_NO if not. - */ -int -testBit (char *bitArray, unsigned int bitIdx) -{ - size_t slot; - unsigned int targetBit; - - slot = bitIdx / 8; - targetBit = (1L << (bitIdx % 8)); - if (bitArray[slot] & targetBit) - return GNUNET_YES; - else - return GNUNET_NO; -} /** * @brief Add the port to the list of additional ports in the map_entry diff --git a/src/vpn/gnunet-daemon-vpn.h b/src/vpn/gnunet-daemon-vpn.h index 91280368a..c2970402c 100644 --- a/src/vpn/gnunet-daemon-vpn.h +++ b/src/vpn/gnunet-daemon-vpn.h @@ -34,15 +34,6 @@ extern struct answer_packet_list *answer_proc_tail; extern struct GNUNET_DNS_Handle *dns_handle; -/** - * This gets scheduled with cls pointing to an answer_packet and does everything - * needed in order to send it to the helper. - * - * At the moment this means "inventing" and IPv6-Address for .gnunet-services and - * doing nothing for "real" services. - */ -void -process_answer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); void send_icmp6_response (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc); @@ -99,24 +90,6 @@ struct map_entry */ }; -/** - * Sets a bit active in a bitArray. - * - * @param bitArray memory area to set the bit in - * @param bitIdx which bit to set - */ -void -setBit (char *bitArray, unsigned int bitIdx); - -/** - * Clears a bit from bitArray. - * - * @param bitArray memory area to set the bit in - * @param bitIdx which bit to unset - */ -void -clearBit (char *bitArray, unsigned int bitIdx); - /** * Checks if a bit is active in the bitArray * @@ -127,6 +100,7 @@ clearBit (char *bitArray, unsigned int bitIdx); int testBit (char *bitArray, unsigned int bitIdx); + struct remote_addr { char addrlen; -- 2.25.1