added SSL backend for IOMultiplexer
[TransparentIRC.git] / src / IOHandler.h
1 /* IOHandler.h - TransparentIRC 0.1
2  * Copyright (C) 2011-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 "overall.h"
20
21 struct IODescriptor;
22 struct IOEvent;
23 struct IOSSLNode;
24
25 #define IOHANDLER_CALLBACK(NAME) void NAME(UNUSED_ARG(struct IOEvent *io_event))
26 typedef IOHANDLER_CALLBACK(iohandler_callback);
27
28 enum IOType {
29     IOTYPE_UNKNOWN, /* ignore descriptor (uninitialized) */
30     IOTYPE_SERVER, /* server socket */
31     IOTYPE_CLIENT, /* client socket */
32     IOTYPE_STDIN, /* stdin */
33     IOTYPE_TIMER /* timer */
34 };
35
36 enum IOStatus { 
37     IO_CLOSED, /* descriptor is dead (socket waiting for removal or timer) */
38     IO_LISTENING, /* descriptor is waiting for connections (server socket) */
39     IO_CONNECTING, /* descriptor is waiting for connection approval (connecting client socket) */
40     IO_CONNECTED, /* descriptor is connected (connected client socket) */
41     IO_SSLWAIT /* waiting for SSL backend (e.g. handshake) */
42 };
43
44 enum IOEventType {
45     IOEVENT_IGNORE,
46     IOEVENT_READABLE, /* socket is readable - not read anything yet, could also be disconnect notification */
47     IOEVENT_RECV, /* client socket received something (recv_str valid) */
48     IOEVENT_CONNECTED, /* client socket connected successful */
49     IOEVENT_NOTCONNECTED, /* client socket could not connect (errid valid) */
50     IOEVENT_CLOSED, /* client socket lost connection (errid valid) */
51     IOEVENT_ACCEPT, /* server socket accepted new connection (accept_fd valid) */
52     IOEVENT_TIMEOUT, /* timer timed out */
53     IOEVENT_SSLFAILED /* failed to initialize SSL session */
54 };
55
56 struct IOBuffer {
57     char *buffer;
58     size_t bufpos, buflen;
59 };
60
61 struct IODescriptor {
62     int fd;
63     enum IOType type;
64     enum IOStatus state;
65     struct timeval timeout;
66     iohandler_callback *callback;
67     struct IOBuffer readbuf;
68     struct IOBuffer writebuf;
69     void *data;
70     int read_lines : 1;
71     int ssl : 1;
72     int ssl_active : 1;
73     int ssl_hs_read : 1;
74     int ssl_hs_write : 1;
75     struct IOSSLNode *sslnode;
76     
77     struct IODescriptor *next, *prev;
78 };
79
80 struct IOEvent {
81     enum IOEventType type;
82     struct IODescriptor *iofd;
83     union {
84         char *recv_str;
85         int accept_fd;
86         int errid;
87     } data;
88 };
89
90 struct IODescriptor *iohandler_add(int sockfd, enum IOType type, struct timeval *timeout, iohandler_callback *callback);
91 struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback *callback);
92 struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bind, iohandler_callback *callback);
93 struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback);
94 void iohandler_write(struct IODescriptor *iofd, const char *line);
95 void iohandler_send(struct IODescriptor *iofd, const char *data, size_t datalen);
96 void iohandler_printf(struct IODescriptor *iofd, const char *text, ...);
97 void iohandler_close(struct IODescriptor *iofd);
98 void iohandler_update(struct IODescriptor *iofd);
99 void iohandler_set_timeout(struct IODescriptor *iofd, struct timeval *timeout);
100
101 void iohandler_poll();
102
103 #endif