[IOMultiplexerV2] dev snapshot
[NextIRCd.git] / src / IODNSLookup.h
1 /* IODNSLookup.h - IOMultiplexer v2
2  * Copyright (C) 2014  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 #ifndef _IODNSLookup_h
18 #define _IODNSLookup_h
19 #ifndef _IOHandler_internals
20 #include "IOHandler.h"
21 #else
22
23 extern struct IODNSEngine dnsengine_cares;
24 extern struct IODNSEngine dnsengine_default;
25
26 #ifdef HAVE_PTHREAD_H
27 extern pthread_mutex_t iodns_sync;
28 #endif
29
30 struct IODNSAddress;
31
32 #define IODNSFLAG_RUNNING        0x01
33 #define IODNSFLAG_PROCESSING     0x02
34 #define IODNSFLAG_PARENT_PUBLIC  0x04
35 #define IODNSFLAG_PARENT_SOCKET  0x08
36
37 struct _IODNSQuery {
38         void *query;
39         
40         unsigned int flags : 8;
41         unsigned int type : 8;
42     union {
43                 struct IODNSAddress addr;
44                 char *host;
45         } request;
46         
47         struct IODNSResult *result;
48         
49         void *parent;
50         
51         struct _IODNSQuery *next, *prev;
52 };
53
54 struct IODNSEngine {
55     const char *name;
56     int (*init)();
57         void (*stop)();
58     void (*add)(struct _IODNSQuery *query);
59     void (*remove)(struct _IODNSQuery *query);
60     void (*loop)();
61 };
62
63 void _init_iodns();
64 void _stop_iodns();
65 struct _IODNSQuery *_create_dnsquery();
66 void _start_dnsquery(struct _IODNSQuery *query);
67 void _stop_dnsquery(struct _IODNSQuery *query);
68
69 /* call only from engines! */
70 void _free_dnsquery(struct _IODNSQuery *query)
71 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state);
72
73 void iodns_poll();
74
75 #endif
76
77 #define IODNS_CALLBACK(NAME) void NAME(struct IODNSEvent *event)
78 typedef IODNS_CALLBACK(iodns_callback);
79
80 enum IODNSEventType {
81     IODNSEVENT_SUCCESS,
82     IODNSEVENT_FAILED
83 };
84
85 #define IODNS_RECORD_A    0x01
86 #define IODNS_RECORD_AAAA 0x02
87 #define IODNS_RECORD_PTR  0x04
88
89 #define IODNS_FORWARD     0x03
90 #define IODNS_REVERSE     0x04
91
92 struct IODNSAddress {
93         size_t addresslen;
94         struct sockaddr *address;
95 };
96
97 struct IODNSQuery {
98         void *query;
99         
100     iodns_callback *callback;
101     void *data;
102 };
103
104 struct IODNSResult {
105     unsigned int type : 8;
106     union {
107                 struct IODNSAddress addr;
108                 char *host;
109         } result;
110     struct IODNSResult *next;
111 };
112
113 struct IODNSEvent {
114     enum IODNSEventType type;
115     struct IODNSQuery *query;
116         struct IODNSResult *result;
117 };
118
119 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback);
120 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, socklen_t addrlen, iodns_callback *callback);
121 void iodns_abort(struct IODNSQuery *query);
122
123 void iodns_free_result(struct IODNSResult *result);
124
125 #endif