[IOMultiplexerV2] alpha
[NextIRCd.git] / src / IOHandler / 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 #include <stdlib.h>
20 #include "IODNSAddress.struct.h"
21
22 #ifndef _IOHandler_internals
23 #include "IOHandler.h"
24 #else
25
26 struct IODNSEngine;
27 extern struct IODNSEngine dnsengine_cares;
28 extern struct IODNSEngine dnsengine_default;
29
30 struct _IODNSQuery;
31 extern struct _IODNSQuery *iodnsquery_first;
32 extern struct _IODNSQuery *iodnsquery_last;
33
34 /* Multithreading */
35 #ifdef IODNS_USE_THREADS
36 #ifndef HAVE_PTHREAD_H
37 #undef IODNS_USE_THREADS
38 #endif
39 #endif
40 #ifdef IODNS_USE_THREADS
41 #include <pthread.h>
42 #ifdef PTHREAD_MUTEX_RECURSIVE_NP
43 #define PTHREAD_MUTEX_RECURSIVE_VAL PTHREAD_MUTEX_RECURSIVE_NP
44 #else
45 #define PTHREAD_MUTEX_RECURSIVE_VAL PTHREAD_MUTEX_RECURSIVE
46 #endif
47 #define IOTHREAD_MUTEX_INIT(var) { \
48     pthread_mutexattr_t mutex_attr; \
49     pthread_mutexattr_init(&mutex_attr);\
50     pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE_VAL);\
51     pthread_mutex_init(&var, &mutex_attr); \
52 }
53 #define IOSYNCHRONIZE(var) pthread_mutex_lock(&var)
54 #define IODESYNCHRONIZE(var) pthread_mutex_unlock(&var)
55 #else
56 #define IOTHREAD_MUTEX_INIT(var)
57 #define IOSYNCHRONIZE(var)
58 #define IODESYNCHRONIZE(var)
59 #endif
60
61 #define IODNSFLAG_RUNNING        0x01
62 #define IODNSFLAG_PROCESSING     0x02
63 #define IODNSFLAG_PARENT_PUBLIC  0x04
64 #define IODNSFLAG_PARENT_SOCKET  0x08
65
66 struct IODNSResult;
67
68 struct _IODNSQuery {
69         void *query;
70         
71         unsigned int flags : 8;
72         unsigned int type : 8;
73     union {
74                 struct IODNSAddress addr;
75                 char *host;
76         } request;
77         
78         struct IODNSResult *result;
79         
80         void *parent;
81         
82         struct _IODNSQuery *next, *prev;
83 };
84
85 struct IODNSEngine {
86     const char *name;
87     int (*init)();
88         void (*stop)();
89     void (*add)(struct _IODNSQuery *query);
90     void (*remove)(struct _IODNSQuery *query);
91     void (*loop)();
92 };
93
94 void _init_iodns();
95 void _stop_iodns();
96 struct _IODNSQuery *_create_dnsquery();
97 void _start_dnsquery(struct _IODNSQuery *query);
98 void _stop_dnsquery(struct _IODNSQuery *query);
99
100 /* call only from engines! */
101 enum IODNSEventType;
102 void _free_dnsquery(struct _IODNSQuery *query);
103 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state);
104
105 void iodns_poll();
106
107 #endif
108
109 struct IODNSEvent;
110 struct sockaddr;
111
112 #define IODNS_CALLBACK(NAME) void NAME(struct IODNSEvent *event)
113 typedef IODNS_CALLBACK(iodns_callback);
114
115 enum IODNSEventType {
116     IODNSEVENT_SUCCESS,
117     IODNSEVENT_FAILED
118 };
119
120 #define IODNS_RECORD_A    0x01
121 #define IODNS_RECORD_AAAA 0x02
122 #define IODNS_RECORD_PTR  0x04
123
124 #define IODNS_FORWARD     0x03
125 #define IODNS_REVERSE     0x04
126
127 struct IODNSQuery {
128         void *query;
129         
130     iodns_callback *callback;
131     void *data;
132 };
133
134 struct IODNSResult {
135     unsigned int type : 8;
136     union {
137                 struct IODNSAddress addr;
138                 char *host;
139         } result;
140     struct IODNSResult *next;
141 };
142
143 struct IODNSEvent {
144     enum IODNSEventType type;
145     struct IODNSQuery *query;
146         struct IODNSResult *result;
147 };
148
149 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback);
150 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, size_t addrlen, iodns_callback *callback);
151 void iodns_abort(struct IODNSQuery *query);
152
153 void iodns_free_result(struct IODNSResult *result);
154
155 #endif