Usermode/libnet - Implementation of DNS lookup, untested
[tpg/acess2.git] / Usermode / Libraries / libnet.so_src / hostnames.c
1 /*
2  * Acess2 Networking Toolkit
3  * By John Hodge (thePowersGang)
4  * 
5  * dns.c
6  * - Hostname<->Address resolution
7  */
8 #include <net.h>
9 #include "include/dns.h"
10 #include <string.h>
11 #include <stdbool.h>
12
13 // === TYPES ===
14 struct sDNSServer
15 {
16          int    AddrType;
17         char    AddrData[16];
18 };
19
20 struct sHostEntry
21 {
22          int    AddrType;
23         char    AddrData[16];
24         char    *Names[];
25 };
26
27 struct sDNSCallbackInfo
28 {
29          int    expected_size;
30         void    *dest_ptr;
31         enum eTypes     desired_type;
32         enum eClass     desired_class;
33         bool    have_result;
34 };
35
36 // === PROTOTYPES ===
37 void    int_DNS_callback(void *info, const char *name, enum eTypes type, enum eClass class, unsigned int ttl, size_t rdlength, const void *rdata);
38
39 // === GLOBALS ===
40  int    giNumDNSServers;
41 struct sDNSServer       *gaDNSServers;
42  int    giNumHostEntries;
43 struct sHostEntry       *gaHostEntries;
44
45 // === CODE ===
46 int Net_Lookup_AnyAddr(const char *Name, int AddrType, void *Addr)
47 {
48         // 1. Load (if not loaded) the DNS config from "/Acess/Conf/dns"
49         // - "* <ip> <ip>" for DNS server(s)
50         // - "127.0.0.1 localhost localhost.localdomain"
51         
52         // 2. Check the hosts list
53         for( int i = 0; i < giNumHostEntries; i ++ )
54         {
55                 const struct sHostEntry* he = &gaHostEntries[i];
56                 if( he->AddrType == AddrType )
57                 {
58                         for( const char * const *namep = (const char**)he->Names; *namep; namep ++ )
59                         {
60                                 if( strcasecmp(Name, *namep) == 0 )
61                                 {
62                                         memcpy(Addr, he->AddrData, Net_GetAddressSize(AddrType));
63                                         return 0;
64                                 }
65                         }
66                 }
67         }
68         // 3. Contact DNS server specified in config
69         for( int i = 0; i < giNumDNSServers; i ++ )
70         {
71                 // TODO
72                 const struct sDNSServer *s = &gaDNSServers[i];
73                 struct sDNSCallbackInfo info = {
74                         .expected_size = Net_GetAddressSize(AddrType),
75                         .dest_ptr = Addr,
76                         .desired_type = TYPE_A,
77                         .desired_class = CLASS_IN,
78                         .have_result = false
79                         };
80                 if( ! DNS_Query(s->AddrType, s->AddrData, Name, info.desired_type, info.desired_class, int_DNS_callback, &info) )
81                 {
82                         if( info.have_result )
83                         {
84                                 return 0;
85                         }
86                         else
87                         {
88                                 // NXDomain, I guess
89                                 return 1;
90                         }
91                 }
92         }
93         return 1;
94 }
95
96 void int_DNS_callback(void *info_v, const char *name, enum eTypes type, enum eClass class, unsigned int ttl, size_t rdlength, const void *rdata)
97 {
98         struct sDNSCallbackInfo *info = info_v;
99         if( type == info->desired_type && class == info->desired_class && info->have_result == false )
100         {
101                 // We're just working with A and AAAA, so copying from rdata is safe
102                 if( rdlength != info->expected_size ) {
103                         // ... oh, that's not good
104                         return ;
105                 }
106                 
107                 memcpy(info->dest_ptr, rdata, rdlength);
108                 
109                 info->have_result = true;
110         }
111 }
112
113 int Net_Lookup_Name(int AddrType, const void *Addr, char *Dest[256])
114 {
115         return 1;
116 }
117

UCC git Repository :: git.ucc.asn.au