Usermode/libaxwin4 - Implementation and debug
[tpg/acess2.git] / Usermode / Applications / dnsresolve_src / main.c
1 /*\r
2  * DNS Resolver\r
3  */\r
4 #include <acess/sys.h>\r
5 #include <stdio.h>\r
6 #include <net.h>\r
7 \r
8 // === CONSTANTS ===\r
9 #define NAMESERVER      {10,0,2,3}\r
10 \r
11 uint8_t packet[] = {\r
12                 // --- UDP header\r
13                 53, 0,  // Dest port\r
14                 4, 0,   // IPv4\r
15                 10,0,2,3,       // Dest address\r
16                 // --- DNS Packet\r
17                 0x13,0x37,\r
18                 0x01,   // Recursion Desired\r
19                 0x00,\r
20                 0,1,    // QDCOUNT\r
21                 0,0,\r
22                 0,0,\r
23                 0,0,    // ARCOUNT\r
24                 \r
25                 #if 0\r
26                 // www.mutabah.net, A, IN\r
27                 3,'w','w','w',\r
28                 7,'m','u','t','a','b','a','h',\r
29                 3,'n','e','t',\r
30                 #else\r
31                 // mussel.ucc.asn.au, A, IN\r
32                 6,'m','u','s','s','e','l',\r
33                 3,'u','c','c',\r
34                 3,'a','s','n',\r
35                 2,'a','u',\r
36                 #endif\r
37                 0,\r
38                 0,1,    // A\r
39                 0,1             // IN\r
40         };\r
41 \r
42 // === TYPES ===\r
43 typedef struct sDNSPacket\r
44 {\r
45         uint16_t        ID;\r
46         // 1: Query/Response, 4: Opcode, 1: Authorative Answer, 1: Truncation, 1: Recursion Denied\r
47         uint8_t opcode;\r
48         uint8_t rcode;  // 1: Recusions Desired, 3: Zero, 4: Response Code\r
49         uint16_t        qdcount;        // Question Count\r
50         uint16_t        ancount;        // Answer Count\r
51         uint16_t        nscount;        // Nameserver Resource Count\r
52         uint16_t        arcount;        // Additional Records count\r
53 }       tDNSPacket;\r
54 \r
55 // === PROTOTYPES ===\r
56 \r
57 // ==== CODE ====\r
58 int main(int argc, char *argv[], char *envp[])\r
59 {\r
60          int    fd, tmp;\r
61         uint16_t        port = 53;\r
62         uint8_t         destip[4] = NAMESERVER;\r
63         uint8_t         buf[512];\r
64         tDNSPacket      *hdr = (void *) (buf + 4 + 4);\r
65 \r
66         fd = Net_OpenSocket(4, destip, "udp");\r
67         if( fd == -1 ) {\r
68                 fprintf(stderr, "Unable to create a UDP socket\n");\r
69                 return 0;\r
70         }\r
71         port = 0;       ioctl(fd, ioctl(fd, 3, "getset_localport"), &port);\r
72         port = 53;      ioctl(fd, ioctl(fd, 3, "getset_remoteport"), &port);\r
73         ioctl(fd, ioctl(fd, 3, "set_remoteaddr"), &destip);\r
74         tmp = 32;       ioctl(fd, ioctl(fd, 3, "getset_remotemask"), &tmp);\r
75         \r
76         write(fd, sizeof(packet), packet);\r
77         read(fd, 512, buf);\r
78         \r
79         printf("hdr = {\n");\r
80         printf("  .ID = 0x%04x\n", hdr->ID);\r
81         printf("  .opcode = 0x%04x\n", hdr->opcode);\r
82         printf("  .rcode = 0x%02x\n", hdr->rcode);\r
83         printf("  .qdcount = 0x%04x\n", hdr->qdcount);\r
84         printf("  .ancount = 0x%04x\n", hdr->ancount);\r
85         printf("  .nscount = 0x%04x\n", hdr->nscount);\r
86         printf("  .arcount = 0x%04x\n", hdr->arcount);\r
87         printf("}\n");\r
88         \r
89         \r
90         close(fd);\r
91         return 0;\r
92 }\r

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