78113ee02b89efe7e7b8dd5202c169ba3f2a07e5
[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 struct _IOSocket;
68
69 struct _IODNSQuery {
70         void *query;
71         
72         unsigned int flags : 8;
73         unsigned int type : 8;
74         union {
75                 struct IODNSAddress addr;
76                 char *host;
77         } request;
78         
79         struct IODNSResult *result;
80         
81         void *parent;
82         
83         struct _IODNSQuery *next, *prev;
84 };
85
86 struct IODNSEngine {
87         const char *name;
88         int (*init)();
89         void (*stop)();
90         void (*add)(struct _IODNSQuery *query);
91         void (*remove)(struct _IODNSQuery *query);
92         void (*loop)();
93         void (*socket_callback)(struct _IOSocket *iosock, int readable, int writeable);
94 };
95
96 void _init_iodns();
97 void _stop_iodns();
98 struct _IODNSQuery *_create_dnsquery();
99 void _start_dnsquery(struct _IODNSQuery *query);
100 void _stop_dnsquery(struct _IODNSQuery *query);
101
102 /* call only from engines! */
103 enum IODNSEventType;
104 void _free_dnsquery(struct _IODNSQuery *query);
105 void iodns_socket_callback(struct _IOSocket *iosock, int wantread, int wantwrite);
106 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state);
107 void iodns_poll();
108
109 #endif
110
111 struct IODNSEvent;
112 struct sockaddr;
113
114 #define IODNS_CALLBACK(NAME) void NAME(struct IODNSEvent *event)
115 typedef IODNS_CALLBACK(iodns_callback);
116
117 enum IODNSEventType {
118         IODNSEVENT_SUCCESS,
119         IODNSEVENT_FAILED
120 };
121
122 #define IODNS_RECORD_A    0x01
123 #define IODNS_RECORD_AAAA 0x02
124 #define IODNS_RECORD_PTR  0x04
125
126 #define IODNS_FORWARD     0x03
127 #define IODNS_REVERSE     0x04
128
129 struct IODNSQuery {
130         void *query;
131         
132         iodns_callback *callback;
133         void *data;
134 };
135
136 struct IODNSResult {
137         unsigned int type : 8;
138         union {
139                 struct IODNSAddress addr;
140                 char *host;
141         } result;
142         struct IODNSResult *next;
143 };
144
145 struct IODNSEvent {
146         enum IODNSEventType type;
147         struct IODNSQuery *query;
148         struct IODNSResult *result;
149 };
150
151 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback, void *arg);
152 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, size_t addrlen, iodns_callback *callback, void *arg);
153 void iodns_abort(struct IODNSQuery *query);
154
155 void iodns_free_result(struct IODNSResult *result);
156
157 #endif