2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
6 * - Hostname<->Address resolution
9 #include "include/dns.h"
11 #include <stdlib.h> // malloc (for loading config)
13 #include <acess/sys.h> // _SysDebug
35 struct sDNSCallbackInfo
38 tNet_LookupAddrs_Callback *callback;
39 enum eTypes desired_type;
40 enum eClass desired_class;
45 int int_lookupany_callback(void *info_v, int AddrType, const void *Addr);
46 void int_DNS_callback(void *info, const char *name, enum eTypes type, enum eClass class, unsigned int ttl, size_t rdlength, const void *rdata);
50 struct sDNSServer *gaDNSServers;
52 struct sHostEntry *gaHostEntries;
55 int Net_Lookup_AnyAddr(const char *Name, int AddrType, void *Addr)
57 struct sLookupAnyInfo cb_info = {
58 .expected_type = AddrType,
62 return Net_Lookup_Addrs(Name, &cb_info, int_lookupany_callback);
64 int int_lookupany_callback(void *info_v, int AddrType, const void *Addr)
66 struct sLookupAnyInfo *info = info_v;
67 if( AddrType == info->expected_type && info->have_result == false )
69 memcpy(info->dest_ptr, Addr, Net_GetAddressSize(AddrType));
71 info->have_result = true;
77 int Net_Lookup_Addrs(const char *Name, void *cb_info, tNet_LookupAddrs_Callback *callback)
79 _SysDebug("Net_Lookup_Addrs(Name='%s')", Name);
80 // 1. Load (if not loaded) the DNS config from "/Acess/Conf/dns"
81 // - "* <ip> <ip>" for DNS server(s)
82 // - "127.0.0.1 localhost localhost.localdomain"
86 gaDNSServers = malloc( 1 * sizeof(gaDNSServers[0]) );
87 gaDNSServers[0].AddrType = Net_ParseAddress("192.168.1.1", gaDNSServers[0].AddrData);
90 // 2. Check the hosts list
91 for( int i = 0; i < giNumHostEntries; i ++ )
93 const struct sHostEntry* he = &gaHostEntries[i];
94 for( const char * const *namep = (const char**)he->Names; *namep; namep ++ )
96 if( strcasecmp(Name, *namep) == 0 )
98 if( callback(cb_info, he->AddrType, he->AddrData) != 0 )
103 // 3. Contact DNS server specified in config
104 for( int i = 0; i < giNumDNSServers; i ++ )
106 const struct sDNSServer *s = &gaDNSServers[i];
107 struct sDNSCallbackInfo info = {
109 .callback = callback,
110 .desired_type = TYPE_A,
111 .desired_class = CLASS_IN,
114 if( ! DNS_Query(s->AddrType, s->AddrData, Name, info.desired_type, info.desired_class, int_DNS_callback, &info) )
130 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)
132 struct sDNSCallbackInfo *info = info_v;
133 _SysDebug("int_DNS_callback(name='%s', type=%i, class=%i)", name, type, class);
135 // Check type matches (if pattern was provided)
136 if( info->desired_type != QTYPE_STAR && type != info->desired_type )
138 if( info->desired_class != QCLASS_STAR && class != info->desired_class )
146 info->callback( info->cb_info, 4, rdata );
149 // if( rdlength != 16 )
151 // info->callback( info->cb_info, 6, rdata );
154 // Ignore anything not A/AAAA
157 info->got_value = true;
160 int Net_Lookup_Name(int AddrType, const void *Addr, char *Dest[256])