2 * Acess2 Networking Toolkit
3 * By John Hodge (thePowersGang)
6 * - Hostname<->Address resolution
8 #include <stddef.h> // size_t / NULL
9 #include <stdint.h> // uint*_t
10 #include <string.h> // memcpy, strchr
12 #include <acess/sys.h> // for _SysSelect
13 #include <acess/fd_set.h> // FD_SET
15 #include "include/dns.h"
16 #include "include/dns_int.h"
19 //int DNS_Query(int ServerAType, const void *ServerAddr, const char *name, enum eTypes type, enum eClass class, handle_record_t* handle_record, void *info);
22 int DNS_Query(int ServerAType, const void *ServerAddr, const char *name, enum eTypes type, enum eClass class, handle_record_t* handle_record, void *info)
25 size_t packlen = DNS_int_EncodeQuery(packet, sizeof(packet), name, type, class);
27 _SysDebug("DNS_Query - Serialising packet failed");
31 // Send and wait for reply
33 // > TODO: Lock DNS queries
35 int sock = Net_OpenSocket_UDP(ServerAType, ServerAddr, 53, 0);
38 _SysDebug("DNS_Query - UDP open failed");
39 // TODO: Correctly report this failure with a useful error code
42 int rv = Net_UDP_SendTo(sock, 53, ServerAType, ServerAddr, packlen, packet);
44 _SysDebug("DNS_Query - Write failed");
45 // TODO: Error reporting
55 int64_t timeout = 2000; // Give it two seconds, should be long enough
56 rv = _SysSelect(nfd, &fds, NULL, NULL, &timeout, 0);
58 // Timeout with no reply, give up
59 _SysDebug("DNS_Query - Timeout");
64 // Oops, select failed
65 _SysDebug("DNS_Query - Select failure");
70 int return_len = Net_UDP_RecvFrom(sock, NULL, NULL, NULL, sizeof(packet), packet);
71 if( return_len <= 0 ) {
72 // TODO: Error reporting
73 _SysDebug("DNS_Query - Read failure");
79 // > TODO: Lock DNS queries
81 // For each response in the answer (and additional) sections, call the passed callback
82 return DNS_int_ParseResponse(packet, return_len, info, handle_record);