[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[tyndur-devel] [PATCH v2 3/4] libc: POSIX: gethostbyname()
+ libc: POSIX: gethostbyname()
---
src/modules/include/netdb.h | 44 +++++++++++++++++++++++++++++++++++++++++
src/modules/include/network.h | 2 +-
src/modules/lib/network.c | 2 +-
src/modules/lib/posix/net.c | 40 +++++++++++++++++++++++++++++++++++++
4 files changed, 86 insertions(+), 2 deletions(-)
create mode 100644 src/modules/include/netdb.h
diff --git a/src/modules/include/netdb.h b/src/modules/include/netdb.h
new file mode 100644
index 0000000..d2a3666
--- /dev/null
+++ b/src/modules/include/netdb.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Kevin Wolf.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _NETDB_H_
+#define _NETDB_H_
+
+struct hostent {
+ char* h_name;
+ char** h_aliases;
+ int h_addrtype;
+ int h_length;
+ char** h_addr_list;
+#define h_addr h_addr_list[0]
+};
+
+struct hostent* gethostbyname(const char* name);
+
+#endif
+
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 738eae8..f544b45 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>
unsigned long int htonl(unsigned long int hostlong)
{
@@ -48,3 +51,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