00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <linux/module.h>
00010 #include <linux/kernel.h>
00011 #include "netdb.h"
00012 #include "ares.h"
00013 #include "ares_dns.h"
00014 #include "memcopy.h"
00015 #include "lwip/sys.h"
00016 #include "lwip/ip_addr.h"
00017 #include "lwip/inet.h"
00018 #include "lwip/sockets.h"
00019 #include <rtl_printf.h>
00020
00021 #ifndef INADDR_NONE
00022 #define INADDR_NONE 0xffffffff
00023 #endif
00024
00025 #define QUESTION "cybeles.disca.upv.es"
00026 #define DNS_SERVER_ADDR "158.42.4.4"
00027
00028 static void dns_client_callback(void *arg, int status, struct hostent *host)
00029 {
00030 struct in_addr addr;
00031 char *mem, **p;
00032
00033 if (status != ARES_SUCCESS)
00034 {
00035 rtl_printf("%s: %s\n", (char *) arg, ares_strerror(status, &mem));
00036 ares_free_errmem(mem);
00037 return;
00038 }
00039
00040 for (p = host->h_addr_list; *p; p++)
00041 {
00042 char *address = " ";
00043 memcopy(&addr, *p, sizeof(struct in_addr));
00044 address = inet_ntoa(addr);
00045 rtl_printf("%s: %s\n", host->h_name, address);
00046 }
00047 }
00048
00049
00050
00051 static void dns_client(void *arg){
00052 ares_channel channel;
00053 int status, nfds;
00054 fd_set read_fds, write_fds;
00055 struct timeval *tvp, tv;
00056 char *errmem;
00057 struct in_addr addr;
00058 struct in_addr ns1;
00059 struct ares_options options;
00060
00061 inet_aton(DNS_SERVER_ADDR,&ns1);
00062
00063 options.servers = &ns1;
00064 options.nservers = 1;
00065 options.flags = ARES_FLAG_USEVC;
00066
00067 status = ares_init_options(&channel, &options, ARES_OPT_SERVERS | ARES_OPT_FLAGS);
00068
00069 if (status != ARES_SUCCESS)
00070 {
00071 rtl_printf("ares_init: %s\n", ares_strerror(status, &errmem));
00072 ares_free_errmem(errmem);
00073 sys_thread_exit();
00074 }
00075
00076
00077 addr.s_addr = inet_addr(QUESTION);
00078 if (addr.s_addr == INADDR_NONE){
00079 ares_gethostbyname(channel, QUESTION, AF_INET, dns_client_callback, QUESTION);
00080 }else{
00081 ares_gethostbyaddr(channel, &addr, sizeof(addr), AF_INET, dns_client_callback,
00082 QUESTION);
00083 }
00084
00085
00086 while (1)
00087 {
00088 FD_ZERO(&read_fds);
00089 FD_ZERO(&write_fds);
00090 nfds = ares_fds(channel, &read_fds, &write_fds);
00091 if (nfds == 0)
00092 break;
00093 tvp = ares_timeout(channel, NULL, &tv);
00094 select(nfds, &read_fds, &write_fds, NULL, tvp);
00095 ares_process(channel, &read_fds, &write_fds);
00096 }
00097
00098 ares_destroy(channel);
00099 }
00100
00101
00102 int init_module(void){
00103 printk("\n\nDNS client module inserted.\n\n");
00104 sys_thread_new(dns_client, NULL, 0);
00105 return 0;
00106 }
00107
00108
00109 void cleanup_module(void){
00110 printk("\n\nDNS client module removed.\n\n");
00111 }