[IOMultiplexerV2] added reverse lookups to IODNSEngine_default and added DNS example...
[NextIRCd.git] / src / IOHandler_test / resolv / iotest.c
1 /* main.c - IOMultiplexer
2  * Copyright (C) 2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "../../IOHandler/IOHandler.h"
21 #include "../../IOHandler/IODNSLookup.h"
22 #include "../../IOHandler/IOLog.h"
23
24 #ifndef WIN32
25 #include "arpa/inet.h"
26 #endif
27 #include "../../IOHandler/compat/inet.h"
28
29 #define TEST_DURATION 100
30
31 static IODNS_CALLBACK(io_callback);
32 static IOLOG_CALLBACK(io_log);
33
34 int main(int argc, char *argv[]) {
35         iohandler_init();
36         iolog_register_callback(io_log);
37         
38         iodns_getaddrinfo("google.de", (IODNS_RECORD_A | IODNS_RECORD_AAAA), io_callback, "01");
39         iodns_getaddrinfo("pk910.de", IODNS_RECORD_AAAA, io_callback, "02");
40         iodns_getaddrinfo("nonexisting.no-tld", (IODNS_RECORD_A | IODNS_RECORD_AAAA), io_callback, "03");
41         iodns_getaddrinfo("google.com", (IODNS_RECORD_A | IODNS_RECORD_AAAA), io_callback, "04");
42         iodns_getaddrinfo("test.pk910.de", IODNS_RECORD_A, io_callback, "05");
43         
44         struct sockaddr_in addr;
45         addr.sin_family = AF_INET;
46         inet_pton(AF_INET, "8.8.8.8", &addr.sin_addr);
47         iodns_getnameinfo((struct sockaddr *)&addr, sizeof(addr), io_callback, "06");
48         
49         iohandler_run();
50         return 1;
51 }
52
53 static IODNS_CALLBACK(io_callback) {
54         struct IODNSQuery *iodns = event->query;
55         char *id = iodns->data;
56         if(event->type == IODNSEVENT_SUCCESS) {
57                 printf("Query %s succeeded:\n", id);
58                 struct IODNSResult *result;
59                 char str[1024];
60                 for(result = event->result; result; result = result->next) {
61                         switch(result->type) {
62                         case IODNS_RECORD_A:
63                                 inet_ntop(AF_INET, &((struct sockaddr_in *)result->result.addr.address)->sin_addr, str, INET_ADDRSTRLEN);
64                                 printf("  A: %s\n", str);
65                                 
66                                 if(!strcmp(id, "05"))
67                                         iodns_getnameinfo(result->result.addr.address, result->result.addr.addresslen, io_callback, "07");
68                                 break;
69                         case IODNS_RECORD_AAAA:
70                                 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)result->result.addr.address)->sin6_addr, str, INET6_ADDRSTRLEN);
71                                 printf("  AAAA: %s\n", str);
72                                 
73                                 if(!strcmp(id, "05"))
74                                         iodns_getnameinfo(result->result.addr.address, result->result.addr.addresslen, io_callback, "07");
75                                 break;
76                         case IODNS_RECORD_PTR:
77                                 printf("  PTR: %s\n", result->result.host);
78                                 break;
79                         }
80                 }
81                 iodns_free_result(event->result);
82         } else
83                 printf("Query %s failed.\n", id);
84 }
85
86 static IOLOG_CALLBACK(io_log) {
87         //printf("%s", line);
88 }