Usermode/utests - Fix DNS utest, update libc utests to new format (no more EXP files)
[tpg/acess2.git] / Usermode / Libraries / libnet.so_src / dns.c
1 /*
2  * Acess2 Networking Toolkit
3  * By John Hodge (thePowersGang)
4  * 
5  * dns.c
6  * - Hostname<->Address resolution
7  */
8 #include <stddef.h>     // size_t / NULL
9 #include <stdint.h>     // uint*_t
10 #include <string.h>     // memcpy, strchr
11 #include <assert.h>
12 #include <acess/sys.h>  // for _SysSelect
13 #include <acess/fd_set.h>       // FD_SET
14 #include <net.h>
15 #include "include/dns.h"
16 #include "include/dns_int.h"
17
18 // === PROTOTYPES ===
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);
20
21 // === CODE ===
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)
23 {
24         char    packet[512];
25         size_t packlen = DNS_int_EncodeQuery(packet, sizeof(packet), name, type, class);
26         
27         // Send and wait for reply
28         // - Lock
29         //  > TODO: Lock DNS queries
30         // - Send
31         int sock = Net_OpenSocket_UDP(ServerAType, ServerAddr, 53, 0);
32         if( sock < 0 ) {
33                 // Connection failed
34                 _SysDebug("DNS_Query - UDP open failed");
35                 // TODO: Correctly report this failure with a useful error code
36                 return 1;
37         }
38         int rv = Net_UDP_SendTo(sock, 53, ServerAType, ServerAddr, pos, packet);
39         if( rv != pos ) {
40                 _SysDebug("DNS_Query - Write failed");
41                 // TODO: Error reporting
42                 _SysClose(sock);
43                 return 1;
44         }
45         // - Wait
46         {
47                  int    nfd = sock + 1;
48                 fd_set  fds;
49                 FD_ZERO(&fds);
50                 FD_SET(sock, &fds);
51                 int64_t timeout = 2000; // Give it two seconds, should be long enough
52                 rv = _SysSelect(nfd, &fds, NULL, NULL, &timeout, 0);
53                 if( rv == 0 ) {
54                         // Timeout with no reply, give up
55                         _SysDebug("DNS_Query - Timeout");
56                         _SysClose(sock);
57                         return 1;
58                 }
59                 if( rv < 0 ) {
60                         // Oops, select failed
61                         _SysDebug("DNS_Query - Select failure");
62                         _SysClose(sock);
63                         return 1;
64                 }
65         }
66         int return_len = Net_UDP_RecvFrom(sock, NULL, NULL, NULL, sizeof(packet), packet);
67         if( return_len <= 0 ) {
68                 // TODO: Error reporting
69                 _SysDebug("DNS_Query - Read failure");
70                 _SysClose(sock);
71                 return 1;
72         }
73         _SysClose(sock);
74         // - Release
75         //  > TODO: Lock DNS queries
76         
77         // For each response in the answer (and additional) sections, call the passed callback
78         return DNS_int_ParseResponse(packet, return_len, info, handle_record);
79 }
80

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