[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
36 struct _IODNSQuery {
37         void *query;
38         
39         unsigned int flags : 8;
40         unsigned int type : 8;
41     union {
42                 struct IODNSAddress addr;
43                 char *host;
44         } request;
45         
46         struct IODNSResult *result;
47         
48         void *parent;
49         
50         struct _IODNSQuery *next, *prev;
51 };
52
53 struct IODNSEngine {
54     const char *name;
55     int (*init)();
56         void (*stop)();
57     void (*add)(struct _IODNSQuery *query);
58     void (*remove)(struct _IODNSQuery *query);
59     void (*loop)();
60 };
61
62 void _init_iodns();
63 void _stop_iodns();
64 struct _IODNSQuery *_create_dnsquery();
65 void _start_dnsquery(struct _IODNSQuery *query);
66 void _stop_dnsquery(struct _IODNSQuery *query);
67
68 /* call only from engines! */
69 void _free_dnsquery(struct _IODNSQuery *query)
70 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state);
71
72 void iodns_poll();
73
74 #endif
75
76 #define IODNS_CALLBACK(NAME) void NAME(struct IODNSEvent *event)
77 typedef IODNS_CALLBACK(iodns_callback);
78
79 enum IODNSEventType {
80     IODNSEVENT_SUCCESS,
81     IODNSEVENT_FAILED
82 };
83
84 #define IODNS_RECORD_A    0x01
85 #define IODNS_RECORD_AAAA 0x02
86 #define IODNS_RECORD_PTR  0x04
87
88 #define IODNS_FORWARD     0x03
89 #define IODNS_REVERSE     0x04
90
91 struct IODNSAddress {
92         size_t addresslen;
93         struct sockaddr *address;
94 };
95
96 struct IODNSQuery {
97         void *query;
98         
99     iodns_callback *callback;
100     void *data;
101 };
102
103 struct IODNSResult {
104     unsigned int type : 8;
105     union {
106                 struct IODNSAddress addr;
107                 char *host;
108         } result;
109     struct IODNSResult *next;
110 };
111
112 struct IODNSEvent {
113     enum IODNSEventType type;
114     struct IODNSQuery *query;
115         struct IODNSResult *result;
116 };
117
118 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback);
119 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, socklen_t addrlen, iodns_callback *callback);
120 void iodns_abort(struct IODNSQuery *query);
121
122 void iodns_free_result(struct IODNSResult *result);
123
124 #endif