Commit f464392e authored by Kevin Wolf's avatar Kevin Wolf
Browse files

Revert "tcpip: DNS-Namen in mehrere IP-Adressen aufloesen"

Das neue gethostbyname() fuehrt zu Page Faults.
This reverts commit b9a07aef

.
Signed-off-by: Kevin Wolf's avatarKevin Wolf <kevin@tyndur.org>
No related merge requests found
Showing with 33 additions and 130 deletions
+33 -130
......@@ -184,14 +184,7 @@ struct hostent* gethostbyname(const char* name)
static struct hostent hostent;
static char* aliases[] = { NULL };
static uint32_t ip;
static uint32_t** h_addr_list = NULL;
int i;
// Also, falls h_addr_list nicht NULL ist... muessen wir mal bisschen
// rumfreen
for (i = 0; h_addr_list && h_addr_list[i]; i++) {
free(h_addr_list[i]);
}
static uint32_t* h_addr_list[] = { &ip, NULL };
// Erstmal schauen, ob es eine IP-Adresse ist
ip = string_to_ip(name);
......@@ -200,49 +193,17 @@ struct hostent* gethostbyname(const char* name)
if (ip == 0) {
char* path;
FILE* f;
unsigned long count = 0;
char ip_str[16];
// Datei oeffnen
asprintf(&path, "tcpip:/dns/%s", name);
f = fopen(path, "r");
free(path);
if (!f) {
return NULL;
}
// Dateigroesse feststellen
fseek(f, 0, SEEK_END);
count = ftell(f);
rewind(f);
// Daten einlesen
char ip_str[count + 1];
fread(ip_str, 1, count, f);
fread(ip_str, 1, 16, f);
fclose(f);
ip_str[count] = '\0';
// An einem \n ist die IP-Adresse zu Ende
char* p;
p = strtok(ip_str, "\n");
i = 0;
while (p != NULL)
{
// IP in die Liste speichern
uint32_t* ip_q = malloc(sizeof(uint32_t));
*ip_q = string_to_ip(p);
h_addr_list = realloc(h_addr_list, sizeof(uint32_t*) * (i + 1));
h_addr_list[i] = ip_q;
i++;
// Nach \n aufteilen, naechste IP nehmen
p = strtok(NULL, "\n");
}
// Wir muessen mit NULL terminieren
h_addr_list[i] = NULL;
ip = string_to_ip(ip_str);
}
// hostent aktualisieren
......
......@@ -66,7 +66,7 @@
struct dns_cache_entry {
char* name;
struct dns_request_result* result;
dword ip;
};
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 struct dns_request_result* dns_cache_get(char* domain)
static dword dns_cache_get(char* domain)
{
struct dns_cache_entry* entry;
int i;
......@@ -87,22 +87,12 @@ static struct dns_request_result* 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->result;
return entry->ip;
}
DEBUG_MSG1(" -- %s: Treffer\n", entry->name);
}
DEBUG_MSG1(" -- %s: Keine Treffer\n",domain);
return NULL;
}
/**
* Gibt die Ressourcen eines dns_request_result wieder frei
* @param result Das freizugebene Resultat
*/
static void dns_free_result(struct dns_request_result* result)
{
free(result->ip_address);
free(result);
return 0;
}
/**
......@@ -110,59 +100,52 @@ static void dns_free_result(struct dns_request_result* result)
* ein alter Eintrag geloescht, um den Cache auf einer ertraeglichen Groesse zu
* halten.
*/
static void dns_cache_add(char* domain, struct dns_request_result* result)
static void dns_cache_add(char* domain, dword ip)
{
struct dns_cache_entry* entry = malloc(sizeof(*entry));
size_t domain_len = strlen(domain);
entry->result = result;
entry->ip = ip;
entry->name = malloc(domain_len + 1);
strncpy(entry->name, domain, domain_len);
entry->name[domain_len] = '\0';
DEBUG_MSG1("dns: cache_add: %s\n", entry->name);
DEBUG_MSG2("dns: cache_add: %s => %08x\n", entry->name, entry->ip);
list_push(dns_cache, entry);
if (list_size(dns_cache) > DNS_CACHE_SIZE) {
dns_free_result(list_get_element_at(dns_cache,DNS_CACHE_SIZE));
list_remove(dns_cache, DNS_CACHE_SIZE);
}
}
/**
* Loest einen Domainnamen in alle zugehoerigen IP-Adressen auf.
*
* Loest einen Domainnamen in eine IP-Adresse auf.
*
* @param domain Der aufzuloesende Domainname als nullterminierter String,
* wobei die Labels durch Punkte getrennt sind
*/
struct dns_request_result* dns_request(char* domain)
dword dns_request(char* domain)
{
struct dns_request_result* dns_result;
uint32_t 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
dns_result = dns_cache_get(domain);
if (dns_result != NULL) {
return dns_result;
dword result = dns_cache_get(domain);
if (result) {
return result;
}
dns_result = malloc(sizeof(struct dns_request_result));
// Ansonsten brauchen wir eine Verbindung zum DNS-Server
uint32_t dns_ip = options.nameserver;
dword dns_ip = options.nameserver;
DEBUG_MSG("DNS: Oeffne Verbindung\n");
struct tcp_connection* conn = tcp_open_connection(dns_ip, DNS_PORT);
if (conn == NULL) {
DEBUG_MSG("DNS: Konnte Verbindung nicht aufbauen.\n");
return NULL;
return 0;
}
// Puffer fuer die Nachricht reservieren:
......@@ -237,9 +220,6 @@ struct dns_request_result* dns_request(char* domain)
// Auf Antwort warten
DEBUG_MSG("DNS: Warte auf Antwort\n");
struct tcp_in_buffer* in_buffer;
// Zaehler fuer den Index von dns_result
int dns_result_index = 0;
while(1) {
if ((in_buffer = list_pop(conn->to_lostio))) {
void* reply = in_buffer->data;
......@@ -254,9 +234,6 @@ struct dns_request_result* 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 = malloc(sizeof(uint32_t) * an_count);
// Question Section ignorieren
for (i = 0; i < qd_count; i++) {
// QNAMEs
......@@ -295,20 +272,17 @@ struct dns_request_result* dns_request(char* domain)
10 + big_endian_word(*((word*) (reply + 8)));
if (*((word*) reply) == 0x100) {
result = *((uint32_t*) (reply + 10));
dns_result->ip_address[dns_result_index] = result;
dns_result_index++;
result = *((dword*) (reply + 10));
dns_cache_add(domain, result);
tcp_close_connection(conn);
reply += answer_len;
return result;
} else {
result = 0;
reply += answer_len;
}
}
dns_result->ip_count = dns_result_index;
break;
}
wait_for_rpc();
......@@ -316,7 +290,5 @@ struct dns_request_result* dns_request(char* domain)
tcp_close_connection(conn);
dns_cache_add(domain,dns_result);
return dns_result;
return 0;
}
......@@ -96,11 +96,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)
struct dns_request_result {
uint32_t* ip_address;
uint32_t ip_count;
};
struct dns_request_result* dns_request(char* domain);
dword dns_request(char* domain);
#endif
......@@ -164,34 +164,13 @@ 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) {
// DNS Request absetzen
struct dns_request_result* result = dns_request(strrchr(*path, '/') + 1);
// Keine IP-Adresse erhalten?
if ((result == NULL) || (result->ip_count == 0)) {
dword ip = dns_request(strrchr(*path, '/') + 1);
if (ip == 0)
return FALSE;
}
char* data = calloc(16 * result->ip_count,sizeof(char));
int i;
for(i = 0; i < result->ip_count; i++) {
char* ip = ip_to_string(result->ip_address[i]);
DEBUG_MSG2("IP = %s , len = %d\n",ip,strlen(ip));
strcat(data, ip);
if((i + 1) < result->ip_count) {
strcat(data, "\n");
}
free(ip);
}
// TODO: Data wird nicht gefreet. (vorerst!)
return vfstree_create_node(*path, LOSTIO_TYPES_RAMFILE,
16 * result->ip_count, data, 0);
char* data = ip_to_string(ip);
return vfstree_create_node(*path, LOSTIO_TYPES_RAMFILE, strlen(data), data, 0);
} else {
return vfstree_create_node(*path, LOSTIO_TYPES_TCPPORT, 0, NULL, 0);
}
......@@ -216,11 +195,7 @@ 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) {
// DNS Request absetzen
struct dns_request_result* result = dns_request(*path + 1);
if (result && (result->ip_count > 0)) {
ip = result->ip_address[0];
}
ip = dns_request(*path + 1);
}
if (!ip) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment