88093de37d1a3eb3a398564a22c1be784608c392
[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 sLookupAnyInfo
28 {
29          int    expected_type;
30         void    *dest_ptr;
31         bool    have_result;
32 };
33 struct sDNSCallbackInfo
34 {
35         void    *cb_info;
36         tNet_LookupAddrs_Callback       *callback;
37         enum eTypes     desired_type;
38         enum eClass     desired_class;
39         bool    got_value;
40 };
41
42 // === PROTOTYPES ===
43  int    int_lookupany_callback(void *info_v, int AddrType, const void *Addr);
44 void    int_DNS_callback(void *info, const char *name, enum eTypes type, enum eClass class, unsigned int ttl, size_t rdlength, const void *rdata);
45
46 // === GLOBALS ===
47  int    giNumDNSServers;
48 struct sDNSServer       *gaDNSServers;
49  int    giNumHostEntries;
50 struct sHostEntry       *gaHostEntries;
51
52 // === CODE ===
53 int Net_Lookup_AnyAddr(const char *Name, int AddrType, void *Addr)
54 {
55         struct sLookupAnyInfo   cb_info = {
56                 .expected_type = AddrType,
57                 .dest_ptr = Addr,
58                 .have_result = false,
59                 };
60         return Net_Lookup_Addrs(Name, &cb_info, int_lookupany_callback);
61 }
62 int int_lookupany_callback(void *info_v, int AddrType, const void *Addr)
63 {
64         struct sLookupAnyInfo   *info = info_v;
65         if( AddrType == info->expected_type && info->have_result == false )
66         {
67                 memcpy(info->dest_ptr, Addr, Net_GetAddressSize(AddrType));
68                 
69                 info->have_result = true;
70                 return 1;
71         }
72         return 0;
73 }
74
75 int Net_Lookup_Addrs(const char *Name, void *cb_info, tNet_LookupAddrs_Callback *callback)
76 {
77         // 1. Load (if not loaded) the DNS config from "/Acess/Conf/dns"
78         // - "* <ip> <ip>" for DNS server(s)
79         // - "127.0.0.1 localhost localhost.localdomain"
80         
81         // 2. Check the hosts list
82         for( int i = 0; i < giNumHostEntries; i ++ )
83         {
84                 const struct sHostEntry* he = &gaHostEntries[i];
85                 for( const char * const *namep = (const char**)he->Names; *namep; namep ++ )
86                 {
87                         if( strcasecmp(Name, *namep) == 0 )
88                         {
89                                 if( callback(cb_info, he->AddrType, he->AddrData) != 0 )
90                                         return 0;
91                         }
92                 }
93         }
94         // 3. Contact DNS server specified in config
95         for( int i = 0; i < giNumDNSServers; i ++ )
96         {
97                 const struct sDNSServer *s = &gaDNSServers[i];
98                 struct sDNSCallbackInfo info = {
99                         .cb_info = cb_info,
100                         .callback = callback,
101                         .desired_type = TYPE_A,
102                         .desired_class = CLASS_IN,
103                         .got_value = false,
104                         };
105                 if( ! DNS_Query(s->AddrType, s->AddrData, Name, info.desired_type, info.desired_class, int_DNS_callback, &info) )
106                 {
107                         if( info.got_value )
108                         {
109                                 return 0;
110                         }
111                         else
112                         {
113                                 // NXDomain, I guess
114                                 return 1;
115                         }
116                 }
117         }
118         return 1;
119 }
120
121 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)
122 {
123         struct sDNSCallbackInfo *info = info_v;
124         
125         // Check type matches (if pattern was provided)
126         if( info->desired_type != QTYPE_STAR && type != info->desired_type )
127                 return ;
128         if( info->desired_class != QCLASS_STAR && class != info->desired_class )
129                 return ;
130         
131         switch( type )
132         {
133         case TYPE_A:
134                 if( rdlength != 4 )
135                         return ;
136                 info->callback( info->cb_info, 4, rdata );
137                 break;
138         //case TYPE_AAAA:
139         //      if( rdlength != 16 )
140         //              return ;
141         //      info->callback( info->cb_info, 6, rdata );
142         //      break;
143         default:
144                 // Ignore anything not A/AAAA
145                 break;
146         }
147         info->got_value = true;
148 }
149
150 int Net_Lookup_Name(int AddrType, const void *Addr, char *Dest[256])
151 {
152         return 1;
153 }
154

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