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