[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH] Abfrage des DNS-Caches via lostio erlauben



DNS-Requests verschoben von tcpip:/dns/%s nach tcpip:/dns/request/%s
Abfrage des Caches via tcpip:/dns/cache
Signed-off-by: Roman Muentener <fmnssun@xxxxxxxxx>
---
 src/modules/lib/posix/net.c     |    2 +-
 src/modules/tcpip/dns.c         |   15 ++++++++++-----
 src/modules/tcpip/include/dns.h |   10 ++++++++++
 src/modules/tcpip/lostio_if.c   |   33 +++++++++++++++++++++++++++++++--
 4 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/src/modules/lib/posix/net.c b/src/modules/lib/posix/net.c
index d1c4c6b..78b9d7a 100644
--- a/src/modules/lib/posix/net.c
+++ b/src/modules/lib/posix/net.c
@@ -203,7 +203,7 @@ struct hostent* gethostbyname(const char* name)
         unsigned long count = 0;
 
         // Datei oeffnen
-        asprintf(&path, "tcpip:/dns/%s", name);
+        asprintf(&path, "tcpip:/dns/request/%s", name);
         f = fopen(path, "r");
         free(path);
 
diff --git a/src/modules/tcpip/dns.c b/src/modules/tcpip/dns.c
index 2cc4a36..b0e741c 100644
--- a/src/modules/tcpip/dns.c
+++ b/src/modules/tcpip/dns.c
@@ -64,14 +64,19 @@
 
 #define DNS_CACHE_SIZE 64
 
-struct dns_cache_entry {
-    char*   name;
-    struct dns_request_result* result;
-};
-
 static list_t* dns_cache = NULL;
 
 /**
+ * Gibt einen Pointer auf den DNS-Cache
+ *
+ * @return Pointer zum DNS-Cache
+ */
+list_t* get_dns_cache(void)
+{
+    return dns_cache;
+}
+
+/**
  * Holt eine IP-Adresse aus dem DNS-Cache
  *
  * @return Zum gegebenen Domainnamen passende IP-Adresse oder 0, wenn die
diff --git a/src/modules/tcpip/include/dns.h b/src/modules/tcpip/include/dns.h
index 9682514..e7f360c 100644
--- a/src/modules/tcpip/include/dns.h
+++ b/src/modules/tcpip/include/dns.h
@@ -37,6 +37,9 @@
 #define _DNS_H_
 
 #include <stdint.h>
+#include <stdlib.h>
+#include <types.h>
+#include "collections.h"
 
 #define DNS_PORT 53
 
@@ -96,11 +99,18 @@ static inline void dns_set_opcode(struct dns_header* header, uint8_t opcode)
 #define QTYPE_A big_endian_word(1)
 #define QCLASS_IN big_endian_word(1)
 
+list_t* get_dns_cache(void);
+
 struct dns_request_result {
     uint32_t* ip_address;
     uint32_t ip_count;
 };
 
+struct dns_cache_entry {
+    char*   name;
+    struct dns_request_result* result;
+};
+
 struct dns_request_result* dns_request(char* domain);
 
 #endif
diff --git a/src/modules/tcpip/lostio_if.c b/src/modules/tcpip/lostio_if.c
index 32fdf44..d93c23b 100644
--- a/src/modules/tcpip/lostio_if.c
+++ b/src/modules/tcpip/lostio_if.c
@@ -49,7 +49,7 @@
 #include "dns.h"
 
 //Hier koennen die Debug-Nachrichten aktiviert werden
-#define DEBUG 0
+#define DEBUG 1
 
 #if DEBUG == 1
     #define DEBUG_MSG(s) printf("[ TCPIP ] debug: %s() '%s'\n",__FUNCTION__,s)
@@ -131,6 +131,8 @@ void init_lostio_interface(void)
     // Dateien/Verzeichnisse anlegen
     vfstree_create_node("/dns", LOSTIO_TYPES_DNS, 0, NULL, 
         LOSTIO_FLAG_BROWSABLE);
+    vfstree_create_node("/dns/request", LOSTIO_TYPES_DNS, 0, NULL, 
+        LOSTIO_FLAG_BROWSABLE);
     vfstree_create_node("/route", LOSTIO_TYPES_ROUTE, 1, NULL, 0);
 }
 
@@ -165,7 +167,34 @@ bool lostio_tcp_not_found(char** path, byte flags, pid_t pid, io_resource_t* ps)
 {
     DEBUG_MSG("Knoten anlegen");
 
-    if (strncmp(*path, "/dns/", 5) == 0) {
+    if(strncmp(*path,"/dns/cache",10) == 0) {
+        struct dns_cache_entry* entry;
+        int i, size = 0, offset = 0;
+        char* data = NULL;
+        for (i = 0; (entry = list_get_element_at(get_dns_cache(), i)); i++) {
+            int j;
+            for(j = 0; j < entry->result->ip_count; j++) {
+                char* line;
+                asprintf(&line,"%s :: %s\n",entry->name,ip_to_string(entry->result->ip_address[j]));
+                int len = strlen(line);
+                size += len;
+                data = realloc(data,size);
+                strncpy(data+offset,line,len);
+                free(line);
+                offset += len;
+            }
+        }
+
+        data = realloc(data,size+1);
+        data[size] = '\0';
+
+
+        return vfstree_create_node(*path, LOSTIO_TYPES_RAMFILE,
+            size+1, data, 0);
+    }
+
+
+    if (strncmp(*path, "/dns/request/", 13) == 0) {
         // DNS Request absetzen
         struct dns_request_result* result = dns_request(strrchr(*path, '/') + 1);
 
-- 
1.6.0.4