2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
6 * - Hostname<->Address resolution
9 #include "include/dns.h"
33 struct sDNSCallbackInfo
36 tNet_LookupAddrs_Callback *callback;
37 enum eTypes desired_type;
38 enum eClass desired_class;
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);
48 struct sDNSServer *gaDNSServers;
50 struct sHostEntry *gaHostEntries;
53 int Net_Lookup_AnyAddr(const char *Name, int AddrType, void *Addr)
55 struct sLookupAnyInfo cb_info = {
56 .expected_type = AddrType,
60 return Net_Lookup_Addrs(Name, &cb_info, int_lookupany_callback);
62 int int_lookupany_callback(void *info_v, int AddrType, const void *Addr)
64 struct sLookupAnyInfo *info = info_v;
65 if( AddrType == info->expected_type && info->have_result == false )
67 memcpy(info->dest_ptr, Addr, Net_GetAddressSize(AddrType));
69 info->have_result = true;
75 int Net_Lookup_Addrs(const char *Name, void *cb_info, tNet_LookupAddrs_Callback *callback)
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"
81 // 2. Check the hosts list
82 for( int i = 0; i < giNumHostEntries; i ++ )
84 const struct sHostEntry* he = &gaHostEntries[i];
85 for( const char * const *namep = (const char**)he->Names; *namep; namep ++ )
87 if( strcasecmp(Name, *namep) == 0 )
89 if( callback(cb_info, he->AddrType, he->AddrData) != 0 )
94 // 3. Contact DNS server specified in config
95 for( int i = 0; i < giNumDNSServers; i ++ )
97 const struct sDNSServer *s = &gaDNSServers[i];
98 struct sDNSCallbackInfo info = {
100 .callback = callback,
101 .desired_type = TYPE_A,
102 .desired_class = CLASS_IN,
105 if( ! DNS_Query(s->AddrType, s->AddrData, Name, info.desired_type, info.desired_class, int_DNS_callback, &info) )
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)
123 struct sDNSCallbackInfo *info = info_v;
125 // Check type matches (if pattern was provided)
126 if( info->desired_type != QTYPE_STAR && type != info->desired_type )
128 if( info->desired_class != QCLASS_STAR && class != info->desired_class )
136 info->callback( info->cb_info, 4, rdata );
139 // if( rdlength != 16 )
141 // info->callback( info->cb_info, 6, rdata );
144 // Ignore anything not A/AAAA
147 info->got_value = true;
150 int Net_Lookup_Name(int AddrType, const void *Addr, char *Dest[256])