[IOMultiplexer] Added asynchronous DNS Lookups
[IOMultiplexer.git] / src / IOHandler.h
1 /* IOHandler.h - 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 #ifndef _IOHandler_h
18 #define _IOHandler_h
19 #include <stddef.h>
20 #include <sys/time.h> /* struct timeval */
21
22 #define IO_READ_BUFLEN 1024
23 #define IO_MAX_TIMEOUT 10
24 #define IO_LINE_LEN    1024
25
26 struct timeval;
27 struct IODescriptor;
28 struct IOEvent;
29 struct IOSSLNode;
30 struct IODNSQuery;
31 struct IODNSEvent;
32
33 enum IOLogType {
34     IOLOG_DEBUG,
35     IOLOG_WARNING,
36     IOLOG_ERROR,
37     IOLOG_FATAL
38 };
39
40 #define IOHANDLER_CALLBACK(NAME) void NAME(struct IOEvent *event)
41 typedef IOHANDLER_CALLBACK(iohandler_callback);
42
43 #define IOHANDLER_LOG_BACKEND(NAME) void NAME(enum IOLogType type, const char *line)
44 typedef IOHANDLER_LOG_BACKEND(iohandler_log_callback);
45
46 extern iohandler_log_callback *iolog_backend;
47
48 enum IOType {
49     IOTYPE_UNKNOWN, /* ignore descriptor (uninitialized) */
50     IOTYPE_SERVER, /* server socket */
51     IOTYPE_CLIENT, /* client socket */
52     IOTYPE_STDIN, /* stdin */
53     IOTYPE_TIMER /* timer */
54 };
55
56 enum IOStatus { 
57     IO_PENDING, /* descriptor is initializing */
58     IO_CLOSED, /* descriptor is dead (socket waiting for removal or timer) */
59     IO_LISTENING, /* descriptor is waiting for connections (server socket) */
60     IO_CONNECTING, /* descriptor is waiting for connection approval (connecting client socket) */
61     IO_CONNECTED, /* descriptor is connected (connected client socket) */
62     IO_SSLWAIT /* waiting for SSL backend (e.g. handshake) */
63 };
64
65 enum IOEventType {
66     IOEVENT_IGNORE,
67     IOEVENT_READABLE, /* socket is readable - not read anything yet, could also be disconnect notification */
68     IOEVENT_RECV, /* client socket received something (recv_str valid) */
69     IOEVENT_CONNECTED, /* client socket connected successful */
70     IOEVENT_NOTCONNECTED, /* client socket could not connect (errid valid) */
71     IOEVENT_CLOSED, /* client socket lost connection (errid valid) */
72     IOEVENT_ACCEPT, /* server socket accepted new connection (accept_iofd valid) */
73     IOEVENT_SSLACCEPT, /* SSL server socket accepted new connection (accept_iofd valid) */
74     IOEVENT_TIMEOUT, /* timer timed out */
75     IOEVENT_SSLFAILED, /* failed to initialize SSL session */
76     IOEVENT_DNS_FAILED,
77     IOEVENT_SOCKET_ERROR, /* socket() call failed (errid valid) */
78     IOEVENT_BIND_ERROR, /* bind() call failed (errid valid) */
79     
80 };
81
82 struct IOBuffer {
83     char *buffer;
84     size_t bufpos, buflen;
85 };
86
87 struct IOLowlevelDescriptor;
88
89 struct IODescriptor {
90     union {
91         struct IOLowlevelDescriptor *iold;
92         struct IODescriptorStartup *iostartup;
93     } fd;
94     unsigned int flags; /* internal flags! see IOEngine.h  */
95     enum IOType type;
96     enum IOStatus state;
97     int constant_timeout;
98     iohandler_callback *callback;
99     struct IOBuffer readbuf;
100     struct IOBuffer writebuf;
101     void *data;
102     int read_lines : 1;
103     int ssl : 1;
104     int ssl_server_hs : 1;
105     int ssl_active : 1;
106     int ssl_hs_read : 1;
107     int ssl_hs_write : 1;
108     int iofd_free_lock : 1;
109     int iofd_want_free : 1;
110 };
111
112 struct IOEvent {
113     enum IOEventType type;
114     struct IODescriptor *iofd;
115     union {
116         char *recv_str;
117         int errid;
118         struct IODescriptor *accept_iofd;
119     } data;
120 };
121
122 #define IOHANDLER_LISTEN_IPV4 0x01
123 #define IOHANDLER_LISTEN_IPV6 0x02 /* overrides IOHANDLER_LISTEN_IPV4 */
124
125 #define IOHANDLER_CONNECT_IPV4 0x01
126 #define IOHANDLER_CONNECT_IPV6 0x02 /* overrides IOHANDLER_CONNECT_IPV4 */
127
128 #define IOHANDLER_SETTING_HIGH_PRECISION_TIMER 0x01
129
130 void iohandler_set(int setting, int value);
131
132 struct IODescriptor *iohandler_add(int sockfd, enum IOType type, struct timeval *timeout, iohandler_callback *callback);
133 struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback *callback);
134 struct IODescriptor *iohandler_constant_timer(int msec, iohandler_callback *callback);
135 struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bind, iohandler_callback *callback);
136 struct IODescriptor *iohandler_connect_flags(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback, int flags);
137 struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback);
138 struct IODescriptor *iohandler_listen_flags(const char *hostname, unsigned int port, iohandler_callback *callback, int flags);
139 struct IODescriptor *iohandler_listen_ssl(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iohandler_callback *callback);
140 struct IODescriptor *iohandler_listen_ssl_flags(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iohandler_callback *callback, int flags);
141 void iohandler_write(struct IODescriptor *iofd, const char *line);
142 void iohandler_send(struct IODescriptor *iofd, const char *data, size_t datalen);
143 void iohandler_printf(struct IODescriptor *iofd, const char *text, ...);
144 void iohandler_close(struct IODescriptor *iofd);
145 void iohandler_update(struct IODescriptor *iofd);
146 void iohandler_set_timeout(struct IODescriptor *iofd, struct timeval *timeout);
147
148 void iohandler_poll();
149 void iohandler_poll_timeout(struct timeval timeout);
150
151 #endif