2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
6 * - Hostname<->Address resolution
9 #include "include/dns.h"
27 struct sDNSCallbackInfo
31 enum eTypes desired_type;
32 enum eClass desired_class;
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);
41 struct sDNSServer *gaDNSServers;
43 struct sHostEntry *gaHostEntries;
46 int Net_Lookup_AnyAddr(const char *Name, int AddrType, void *Addr)
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"
52 // 2. Check the hosts list
53 for( int i = 0; i < giNumHostEntries; i ++ )
55 const struct sHostEntry* he = &gaHostEntries[i];
56 if( he->AddrType == AddrType )
58 for( const char * const *namep = (const char**)he->Names; *namep; namep ++ )
60 if( strcasecmp(Name, *namep) == 0 )
62 memcpy(Addr, he->AddrData, Net_GetAddressSize(AddrType));
68 // 3. Contact DNS server specified in config
69 for( int i = 0; i < giNumDNSServers; i ++ )
72 const struct sDNSServer *s = &gaDNSServers[i];
73 struct sDNSCallbackInfo info = {
74 .expected_size = Net_GetAddressSize(AddrType),
76 .desired_type = TYPE_A,
77 .desired_class = CLASS_IN,
80 if( ! DNS_Query(s->AddrType, s->AddrData, Name, info.desired_type, info.desired_class, int_DNS_callback, &info) )
82 if( info.have_result )
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)
98 struct sDNSCallbackInfo *info = info_v;
99 if( type == info->desired_type && class == info->desired_class && info->have_result == false )
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
107 memcpy(info->dest_ptr, rdata, rdlength);
109 info->have_result = true;
113 int Net_Lookup_Name(int AddrType, const void *Addr, char *Dest[256])