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

[tyndur-devel] [PATCH 3/4] libc: POSIX: gethostbyname()



+ libc: POSIX: gethostbyname()
---
 src/modules/include/netinet/in.h |    1 +
 src/modules/include/network.h    |    2 +-
 src/modules/lib/network.c        |    2 +-
 src/modules/lib/posix/net.c      |   40 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/src/modules/include/netinet/in.h b/src/modules/include/netinet/in.h
index 14963a4..5bf18d2 100644
--- a/src/modules/include/netinet/in.h
+++ b/src/modules/include/netinet/in.h
@@ -31,6 +31,7 @@
 
 #include <sys/socket.h>
 #include <stdint.h>
+#include <sys/socket.h>
 
 /// Adresse fuer das INET-Protokol (IPv4)
 struct sockaddr_in {
diff --git a/src/modules/include/network.h b/src/modules/include/network.h
index 9be97db..ef0bb99 100644
--- a/src/modules/include/network.h
+++ b/src/modules/include/network.h
@@ -48,7 +48,7 @@ struct net_device {
     dword ip;
 };
 
-dword string_to_ip(char* ip);
+dword string_to_ip(const char* ip);
 char* ip_to_string(dword ip);
 char* mac_to_string(uint64_t mac);
 void register_netcard(dword device_numer, uint64_t mac, dword ip);
diff --git a/src/modules/lib/network.c b/src/modules/lib/network.c
index 927ada8..d0343d3 100644
--- a/src/modules/lib/network.c
+++ b/src/modules/lib/network.c
@@ -40,7 +40,7 @@
  * Dezimalzahlen (a.b.c.d). Andere Darstellungen (z.B. eine Hexzahl) sind
  * derzeit nicht unterst�tzt.
  */
-dword string_to_ip(char* ip)
+dword string_to_ip(const char* ip)
 {
     byte ip_bytes[4] = { 0, 0, 0, 0 };
 
diff --git a/src/modules/lib/posix/net.c b/src/modules/lib/posix/net.c
index 2a41daf..c6fbdb8 100644
--- a/src/modules/lib/posix/net.c
+++ b/src/modules/lib/posix/net.c
@@ -28,6 +28,9 @@
 
 #include <netinet/in.h>
 #include <network.h>
+#include <netdb.h>
+
+#include <stdio.h>
 
 #include <stdlib.h>
 #include <string.h>
@@ -51,3 +54,40 @@ unsigned short int ntohs(unsigned short int netshort)
 {
     return big_endian_dword(netshort);
 }
+
+struct hostent* gethostbyname(const char* name)
+{
+    static struct hostent hostent;
+    static char* aliases[] = { NULL };
+    static uint32_t ip;
+    static uint32_t* h_addr_list[] = { &ip, NULL };
+
+    // Erstmal schauen, ob es eine IP-Adresse ist
+    ip = string_to_ip(name);
+
+    // Wenn nicht, muessen wir eine DNS-Anfrage machen
+    if (ip == 0) {
+        char* path;
+        FILE* f;
+        char ip_str[16];
+
+        asprintf(&path, "tcpip:/dns/%s", name);
+        f = fopen(path, "r");
+        if (!f) {
+            return NULL;
+        }
+        fread(ip_str, 1, 16, f);
+        fclose(f);
+
+        ip = string_to_ip(ip_str);
+    }
+
+    // hostent aktualisieren
+    hostent.h_name = (char*) name;
+    hostent.h_aliases = aliases;
+    hostent.h_addrtype = AF_INET;
+    hostent.h_length = 4;
+    hostent.h_addr_list = (char**) h_addr_list;
+
+    return &hostent;
+}
-- 
1.6.0.2