[IOMultiplexerV2] dev snapshot
[NextIRCd.git] / src / IOSockets.h
1 /* IOSockets.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 _IOSockets_h
18 #define _IOSockets_h
19 #ifndef _IOHandler_internals
20 #include "IOHandler.h"
21 #else
22
23 #define IOSOCKET_LINE_LEN 1024
24
25 #define IOSOCKETFLAG_ACTIVE           0x0001
26 #define IOSOCKETFLAG_LISTENING        0x0002
27 #define IOSOCKETFLAG_PENDING_BINDDNS  0x0004
28 #define IOSOCKETFLAG_PENDING_DESTDNS  0x0008
29 #define IOSOCKETFLAG_DNSDONE_BINDDNS  0x0010
30 #define IOSOCKETFLAG_DNSDONE_DESTDNS  0x0020
31 #define IOSOCKETFLAG_DNSERROR         0x0040
32 #define IOSOCKETFLAG_IPV6SOCKET       0x0080
33 #define IOSOCKETFLAG_PARENT_PUBLIC    0x0100
34 #define IOSOCKETFLAG_PARENT_DNSENGINE 0x0200
35 #define IOSOCKETFLAG_SSLSOCKET        0x0400 /* use ssl after connecting */
36 #define IOSOCKETFLAG_SHUTDOWN         0x0800 /* disconnect pending */
37
38 struct IODNSAddress;
39 struct IOSocketBuffer;
40 struct IOSSLDescriptor;
41 struct _IODNSQuery;
42
43 struct IOSocketDNSLookup {
44         unsigned int bindlookup : 1;
45         char hostname[256];
46         struct _IOSocket *iosocket;
47         struct _IODNSQuery *query;
48         struct IODNSResult *result;
49 };
50
51 struct _IOSocket {
52     int fd;
53         
54         unsigned int socket_flags : 16;
55         
56         union {
57                 struct IODNSAddress addr;
58                 struct IOSocketDNSLookup *addrlookup;
59         } bind;
60         union {
61                 struct IODNSAddress addr;
62                 struct IOSocketDNSLookup *addrlookup;
63         } dest;
64         
65         unsigned int port : 16;
66         
67         struct IOSocketBuffer readbuf;
68     struct IOSocketBuffer writebuf;
69         
70         struct IOSSLDescriptor *sslnode;
71         
72         void *parent;
73         
74         struct _IOSocket *next, *prev;
75 };
76
77 void _init_sockets();
78 void iosocket_lookup_callback(struct IOSocketDNSLookup *lookup, struct IODNSEvent *event);
79
80 #endif
81
82 struct IOSocketEvent;
83
84 #define IOSOCKET_CALLBACK(NAME) void NAME(struct IOSocketEvent *event)
85 typedef IOSOCKET_CALLBACK(iosocket_callback);
86
87 enum IOSocketStatus { 
88     IOSOCKET_CLOSED, /* descriptor is dead (socket waiting for removal or timer) */
89     IOSOCKET_LISTENING, /* descriptor is waiting for connections (server socket) */
90     IOSOCKET_CONNECTING, /* descriptor is waiting for connection approval (connecting client socket) */
91     IOSOCKET_CONNECTED /* descriptor is connected (connected client socket) */
92 };
93
94 enum IOSocketEventType {
95     IOSOCKETEVENT_IGNORE,
96     IOSOCKETEVENT_RECV, /* client socket received something (read_lines == 1  =>  recv_str valid;  read_lines == 0  =>  recv_buf valid) */
97     IOSOCKETEVENT_CONNECTED, /* client socket connected successful */
98     IOSOCKETEVENT_NOTCONNECTED, /* client socket could not connect (errid valid) */
99     IOSOCKETEVENT_CLOSED, /* client socket lost connection (errid valid) */
100     IOSOCKETEVENT_ACCEPT, /* server socket accepted new connection (accept_socket valid) */
101     IOSOCKETEVENT_SSLFAILED, /* failed to initialize SSL session */
102         IOSOCKETEVENT_DNSFAILED /* failed to lookup DNS information */
103 };
104
105 struct IOSocket {
106         void *iosocket;
107         
108         enum IOSocketStatus status;
109         int listening : 1;
110         int ssl : 1;
111         int read_lines : 1;
112         
113         void *data;
114         iosocket_callback *callback;
115 };
116
117 struct IOSocketBuffer {
118     char *buffer;
119     size_t bufpos, buflen;
120 };
121
122 struct IOSocketEvent {
123     enum IOSocketEventType type;
124     struct IOSocket *socket;
125     union {
126         char *recv_str;
127                 struct IOSocketBuffer *recv_buf;
128         int errid;
129         struct IOSocket *accept_socket;
130     } data;
131 };
132
133
134 #define IOSOCKET_ADDR_IPV4 0x01
135 #define IOSOCKET_ADDR_IPV6 0x02 /* overrides IOSOCKET_ADDR_IPV4 */
136
137 struct IOSocket *iosocket_connect(const char *hostname, unsigned int port, int ssl, const char *bindhost, iosocket_callback *callback);
138 struct IOSocket *iosocket_connect_flags(const char *hostname, unsigned int port, int ssl, const char *bindhost, iosocket_callback *callback, int flags);
139 struct IOSocket *iosocket_listen(const char *hostname, unsigned int port, iosocket_callback *callback);
140 struct IOSocket *iosocket_listen_flags(const char *hostname, unsigned int port, iosocket_callback *callback, int flags);
141 struct IOSocket *iosocket_listen_ssl(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iosocket_callback *callback);
142 struct IOSocket *iosocket_listen_ssl_flags(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iosocket_callback *callback, int flags);
143 void iosocket_write(struct IOSocket *iosocket, const char *line);
144 void iosocket_send(struct IOSocket *iosocket, const char *data, size_t datalen);
145 void iosocket_printf(struct IOSocket *iosocket, const char *text, ...);
146 void iohandler_close(struct IOSocket *iosocket);
147
148 #endif