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

[tyndur-devel] [PATCH] + dns requests geben nun mehrere ip adresen zurueck



---
 src/modules/c/getip/main.c      |   14 ++++++++-
 src/modules/tcpip/dns.c         |   56 ++++++++++++++++++++++++++------------
 src/modules/tcpip/include/dns.h |    7 ++++-
 src/modules/tcpip/lostio_if.c   |   21 +++++++++++---
 4 files changed, 72 insertions(+), 26 deletions(-)

diff --git a/src/modules/c/getip/main.c b/src/modules/c/getip/main.c
index 865e586..163407e 100644
--- a/src/modules/c/getip/main.c
+++ b/src/modules/c/getip/main.c
@@ -39,7 +39,8 @@
 #include "netdb.h"
 #include "stdio.h"
 #include "arpa/inet.h"
-
+#include "errno.h"
+#include "network.h"
 
 
 int main(int argc, char* argv[])
@@ -52,16 +53,25 @@ int main(int argc, char* argv[])
         return 0;
     }
 
+    
+
     struct in_addr addr;
 
     struct hostent* info;
     info = gethostbyname(argv[1]);
+
+
+    if(info == NULL) {
+        printf("ERROR: %s\n",hstrerror(h_errno));
+        return 2;
+    }
+
     int i = 0;
     while (info->h_addr_list[i] != 0) {
         addr.s_addr = *(unsigned long *)info->h_addr_list[i++];
 
         printf("IP Address: %s\n" , inet_ntoa(addr));
     }
-
+    
     return 0;
 }
diff --git a/src/modules/tcpip/dns.c b/src/modules/tcpip/dns.c
index 0f6e18d..72ff33c 100644
--- a/src/modules/tcpip/dns.c
+++ b/src/modules/tcpip/dns.c
@@ -66,7 +66,7 @@
 
 struct dns_cache_entry {
     char*   name;
-    dword   ip;
+    struct dns_request_result* result;
 };
 
 static list_t* dns_cache = NULL;
@@ -77,7 +77,7 @@ static list_t* dns_cache = NULL;
  * @return Zum gegebenen Domainnamen passende IP-Adresse oder 0, wenn die
  * Domain nicht im Cache ist.
  */
-static dword dns_cache_get(char* domain)
+static struct dns_request_result* dns_cache_get(char* domain)
 {
     struct dns_cache_entry* entry;
     int i;
@@ -87,12 +87,12 @@ static dword dns_cache_get(char* domain)
     for (i = 0; (entry = list_get_element_at(dns_cache, i)); i++) {
         if (strcmp(domain, entry->name) == 0) {
             DEBUG_MSG1(" -- %s: Treffer\n", entry->name);
-            return entry->ip;
+            return entry->result;
         }
         DEBUG_MSG1(" -- %s: Treffer\n", entry->name);
     }
-
-    return 0;
+    DEBUG_MSG1(" -- %s: Keine Treffer\n",domain);
+    return NULL;
 }
 
 /**
@@ -100,17 +100,17 @@ static dword dns_cache_get(char* domain)
  * ein alter Eintrag geloescht, um den Cache auf einer ertraeglichen Groesse zu
  * halten.
  */
-static void dns_cache_add(char* domain, dword ip)
+static void dns_cache_add(char* domain, struct dns_request_result* result)
 {
     struct dns_cache_entry* entry = malloc(sizeof(*entry));
     size_t domain_len = strlen(domain);
 
-    entry->ip = ip;
+    entry->result = result;
     entry->name = malloc(domain_len + 1);
     strncpy(entry->name, domain, domain_len);
     entry->name[domain_len] = '\0';
 
-    DEBUG_MSG2("dns: cache_add: %s => %08x\n", entry->name, entry->ip);
+    DEBUG_MSG1("dns: cache_add: %s\n", entry->name);
 
     list_push(dns_cache, entry);
 
@@ -120,24 +120,31 @@ static void dns_cache_add(char* domain, dword ip)
 }
 
 /**
- * Loest einen Domainnamen in eine IP-Adresse auf.
+ * Loest einen Domainnamen in eine IP-Adressen auf.
  * 
  * @param domain Der aufzuloesende Domainname als nullterminierter String,
  * wobei die Labels durch Punkte getrennt sind
  */
-dword dns_request(char* domain)
+
+struct dns_request_result* dns_request(char* domain)
 {
+    // Resultat
+    struct dns_request_result* dns_result = dns_cache_get(domain);
+    dword result;
+
     // Cache anlegen, falls noch keiner da ist
     if (dns_cache == NULL) {
+        DEBUG_MSG("DNS: Erzeuge Cache\n");
         dns_cache = list_create();
     }
 
-    // Wenn es im Cache ist, daraus nehmen
-    dword result = dns_cache_get(domain);
-    if (result) {
-        return result;
+    
+    if(dns_cache_get(domain) != NULL) {
+      return dns_result;
     }
 
+    dns_result = (struct dns_request_result*)malloc(sizeof(struct dns_request_result));
+
     // Ansonsten brauchen wir eine Verbindung zum DNS-Server
     dword dns_ip = options.nameserver;
 
@@ -145,7 +152,7 @@ dword dns_request(char* domain)
     struct tcp_connection* conn = tcp_open_connection(dns_ip, DNS_PORT);
     if (conn == NULL) {
         DEBUG_MSG("DNS: Konnte Verbindung nicht aufbauen.\n");
-        return 0;
+        return NULL;
     }
 
     // Puffer fuer die Nachricht reservieren:
@@ -219,6 +226,8 @@ dword dns_request(char* domain)
     // Auf Antwort warten
     DEBUG_MSG("DNS: Warte auf Antwort\n");
     struct tcp_in_buffer* in_buffer;
+    // Index fuer dns_result
+    int dns_result_index = 0;
     while(1) {
         if ((in_buffer = list_pop(conn->to_lostio))) {
             void* reply = in_buffer->data;
@@ -233,6 +242,11 @@ dword dns_request(char* domain)
                 big_endian_word(((struct dns_header*) reply)->an_count);
             reply += sizeof(struct dns_header);
 
+            // Speicher fuer die IPs holen
+            dns_result->ip_address = (dword*)malloc(sizeof(dword) * an_count);
+            // Menge speichern
+            dns_result->ip_count = an_count;
+
             // Question Section ignorieren
             for (i = 0; i < qd_count; i++) {                
                 // QNAMEs
@@ -272,13 +286,17 @@ dword dns_request(char* domain)
 
                 if (*((word*) reply) == 0x100) {
                     result = *((dword*) (reply + 10));
-                    dns_cache_add(domain, result);
+                    
                     
                     // TODO Verbindung schliessen
-                    return result;
+                    reply += answer_len;
                 } else {
+                    result = 0;
                     reply += answer_len;
                 }
+
+                dns_result->ip_address[dns_result_index] = result;
+                dns_result_index++;
             }
             
             break;
@@ -288,5 +306,7 @@ dword dns_request(char* domain)
 
     // TODO Verbindung schliessen
 
-    return 0;
+    dns_cache_add(domain,dns_result);
+
+    return dns_result;
 }
diff --git a/src/modules/tcpip/include/dns.h b/src/modules/tcpip/include/dns.h
index cc4281f..e9b6368 100644
--- a/src/modules/tcpip/include/dns.h
+++ b/src/modules/tcpip/include/dns.h
@@ -49,6 +49,11 @@ struct dns_header {
     uint16_t    ar_count;
 } __attribute__((packed));
 
+struct dns_request_result {
+    dword* ip_address;
+    dword ip_count;
+};
+
 
 // Konstanten für die Header-Flags
 
@@ -96,6 +101,6 @@ 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)
 
-dword dns_request(char* domain);
+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 a849f18..f48953f 100644
--- a/src/modules/tcpip/lostio_if.c
+++ b/src/modules/tcpip/lostio_if.c
@@ -164,12 +164,18 @@ void lostio_add_device(struct device *device)
 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) {
-        dword ip = dns_request(strrchr(*path, '/') + 1);
-        if (ip == 0)
+        struct dns_request_result* result = dns_request(strrchr(*path, '/') + 1);
+        if (result == NULL)
             return FALSE;
-        char* data = ip_to_string(ip);
+        char* data = calloc(16 * result->ip_count,sizeof(char));
+        
+        int i=0;
+        for(;i < result->ip_count;i++) {
+            strcat(data,ip_to_string(result->ip_address[i]));
+        }
+
         return vfstree_create_node(*path, LOSTIO_TYPES_RAMFILE, strlen(data), data, 0);
     } else {
         return vfstree_create_node(*path, LOSTIO_TYPES_TCPPORT, 0, NULL, 0);
@@ -195,7 +201,12 @@ bool lostio_tcp_pre_open(char** path, byte flags, pid_t pid, io_resource_t* ps)
     *delim = '\0';
     dword ip = string_to_ip(*path + 1);
     if (!ip) {
-        ip = dns_request(*path + 1);
+        struct dns_request_result* result = dns_request(*path + 1);
+        if(result != NULL) {
+            if(result->ip_count > 0) {
+              ip = result->ip_address[0];
+            }
+        }
     }
 
     if (!ip) {
-- 
1.6.0.4