From 07735d51d4c46f739fbdeb924326ebf9ccd73eaa Mon Sep 17 00:00:00 2001
From: Roman Khlopkov <roman.khlopkov@demlabs.net>
Date: Thu, 16 Jul 2020 15:53:12 +0300
Subject: [PATCH] [+] Hostinfo to DNS reply

---
 modules/net/dap_chain_net.c          | 10 ++++---
 modules/net/dap_dns_server.c         | 40 ++++++++++++++++------------
 modules/net/include/dap_dns_server.h |  2 +-
 3 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index 81b6a3e93e..4088ec9878 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -512,11 +512,13 @@ static int s_net_states_proc(dap_chain_net_t * l_net)
             log_it(L_DEBUG, "%s.state: NET_STATE_LINKS_CONNECTING",l_net->pub.name);
             log_it(L_DEBUG, "Establishing connection with " NODE_ADDR_FP_STR,
                    NODE_ADDR_FP_ARGS_S( l_pvt_net->links_addrs[l_links_count]) );
-            dap_chain_node_info_t *l_link_node_info = dap_chain_node_info_read(l_net, &l_pvt_net->links_addrs[l_links_count]);
-            if ( l_link_node_info ) {
-                dap_chain_node_client_t *l_node_client = dap_chain_node_client_connect(l_link_node_info);
+            dap_chain_node_info_t l_link_node_info;
+            int res = dap_dns_client_get_addr(inet_addr("192.168.10.10"), l_net->pub.name, &l_link_node_info);
+                   // = dap_chain_node_info_read(l_net, &l_pvt_net->links_addrs[l_links_count]);
+            if (res) {
+                dap_chain_node_client_t *l_node_client = dap_chain_node_client_connect(&l_link_node_info);
                 if(!l_node_client) {
-                    DAP_DELETE(l_link_node_info);
+                    //DAP_DELETE(l_link_node_info);
                     ret = -1;
                     break;
                 }
diff --git a/modules/net/dap_dns_server.c b/modules/net/dap_dns_server.c
index 18b51fc799..cab76a43f3 100644
--- a/modules/net/dap_dns_server.c
+++ b/modules/net/dap_dns_server.c
@@ -102,13 +102,16 @@ void dap_dns_buf_put_uint64(dap_dns_buf_t *buf, uint64_t val) {
 
 dap_chain_node_info_t *dap_dns_resolve_hostname(char *str) {
     log_it(L_DEBUG, "DNS parser retrieve hostname %s", str);
-    uint16_t l_nets_count;
-    dap_chain_net_t **l_nets = dap_chain_net_list(&l_nets_count);
-    if (!l_nets_count) {
-        log_it(L_WARNING, "No chain network present");
-        return 0;
+    dap_chain_net_t *l_net = dap_chain_net_by_name(str);
+    if (l_net == NULL) {
+        uint16_t l_nets_count;
+        dap_chain_net_t **l_nets = dap_chain_net_list(&l_nets_count);
+        if (!l_nets_count) {
+            log_it(L_WARNING, "No chain network present");
+            return 0;
+        }
+        l_net = l_nets[rand() % l_nets_count];
     }
-    dap_chain_net_t *l_net = l_nets[rand() % l_nets_count];
     // get nodes list from global_db
     dap_global_db_obj_t *l_objs = NULL;
     size_t l_nodes_count = 0;
@@ -368,7 +371,7 @@ void dap_dns_server_stop() {
     DAP_DELETE(s_dns_server);
 }
 
-int s_dns_get_ip(uint32_t a_addr, char *a_name, uint32_t *a_result)
+int dap_dns_client_get_addr(uint32_t a_addr, char *a_name, dap_chain_node_info_t *a_result)
 {
     const size_t l_buf_size = 1024;
     uint8_t l_buf[l_buf_size];
@@ -463,17 +466,20 @@ int s_dns_get_ip(uint32_t a_addr, char *a_name, uint32_t *a_result)
     }
     l_cur = l_buf + l_addr_point;
     if (a_result) {
-        *a_result = ntohl(*(uint32_t *)l_cur);
+        a_result->hdr.ext_addr_v4.s_addr = ntohl(*(uint32_t *)l_cur);
+    }
+    l_cur = l_buf + 5 * sizeof(uint16_t);
+    int l_additions_count = ntohs(*(uint16_t *)l_cur);
+    if (l_additions_count == 1) {
+        l_cur = l_buf + l_addr_point + DNS_ANSWER_SIZE;
+        if (a_result) {
+            a_result->hdr.ext_port = ntohs(*(uint16_t *)l_cur);
+        }
+        l_cur += sizeof(uint16_t);
+        if (a_result) {
+           a_result->hdr.address.uint64 = be64toh(*(uint64_t *)l_cur);
+        }
     }
     closesocket(l_sock);
     return 0;
 }
-
-uint32_t dap_dns_client_get_addr(uint32_t a_dns_addr)
-{
-    uint32_t l_res;
-    if (s_dns_get_ip(a_dns_addr, "kelvin.sync", &l_res)) {
-        return 0;
-    }
-    return l_res;
-}
diff --git a/modules/net/include/dap_dns_server.h b/modules/net/include/dap_dns_server.h
index bfb8aa2211..e36022f8bf 100644
--- a/modules/net/include/dap_dns_server.h
+++ b/modules/net/include/dap_dns_server.h
@@ -124,4 +124,4 @@ void dap_dns_server_start();
 void dap_dns_server_stop();
 int dap_dns_zone_register(char *zone, dap_dns_zone_callback_t callback);
 int dap_dns_zone_unregister(char *zone);
-uint32_t dap_dns_client_get_addr(uint32_t a_dns_addr);
+int dap_dns_client_get_addr(uint32_t a_addr, char *a_name, dap_chain_node_info_t *a_result);
-- 
GitLab