[IOMultiplexerV2] IPv4 fallback for IPv6 Sockets
[NextIRCd.git] / src / IOHandler / 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 #include <sys/time.h>
20 #include <stddef.h>
21 #include "IODNSAddress.struct.h"
22
23 struct IOSocketBuffer {
24     char *buffer;
25     size_t bufpos, buflen;
26 };
27
28 #ifndef _IOHandler_internals
29 #include "IOHandler.h"
30 #else
31
32 struct _IOSocket;
33 struct IOSocket;
34 struct IOSocketBuffer;
35 struct IOSSLDescriptor;
36 struct _IODNSQuery;
37 struct IODNSEvent;
38
39 struct IOEngine {
40     const char *name;
41     int (*init)(void);
42     void (*add)(struct _IOSocket *iosock);
43     void (*remove)(struct _IOSocket *iosock);
44     void (*update)(struct _IOSocket *iosock);
45     void (*loop)(struct timeval *timeout);
46     void (*cleanup)(void);
47 };
48
49 /* IO Engines */
50 extern struct IOEngine engine_select; /* select system call (should always be useable) */
51 extern struct IOEngine engine_kevent;
52 extern struct IOEngine engine_epoll;
53 extern struct IOEngine engine_win32;
54
55
56 /* _IOSocket linked list */
57 extern struct _IOSocket *iosocket_first;
58 extern struct _IOSocket *iosocket_last;
59
60 /* _IOSocket socket_flags */
61 #define IOSOCKETFLAG_ACTIVE           0x0001
62 #define IOSOCKETFLAG_LISTENING        0x0002
63 #define IOSOCKETFLAG_PENDING_BINDDNS  0x0004
64 #define IOSOCKETFLAG_PENDING_DESTDNS  0x0008
65 #define IOSOCKETFLAG_DNSDONE_BINDDNS  0x0010
66 #define IOSOCKETFLAG_DNSDONE_DESTDNS  0x0020
67 #define IOSOCKETFLAG_DNSERROR         0x0040
68 #define IOSOCKETFLAG_IPV6SOCKET       0x0080
69 #define IOSOCKETFLAG_PARENT_PUBLIC    0x0100
70 #define IOSOCKETFLAG_PARENT_DNSENGINE 0x0200
71 #define IOSOCKETFLAG_SSLSOCKET        0x0400 /* use ssl after connecting */
72 #define IOSOCKETFLAG_SSL_HANDSHAKE    0x0800 /* SSL Handshake in progress */
73 #define IOSOCKETFLAG_SSL_WANTWRITE    0x1000
74 #define IOSOCKETFLAG_SHUTDOWN         0x2000 /* disconnect pending */
75 #define IOSOCKETFLAG_CONNECTING       0x4000
76 #define IOSOCKETFLAG_INCOMING         0x8000 /* incoming (accepted) connection */
77 #define IOSOCKETFLAG_DEAD            0x10000
78 #define IOSOCKETFLAG_RECONNECT_IPV4  0x20000 /* possible fallback to ipv4 connect if ipv6 fails */
79
80 struct IOSocketDNSLookup {
81         unsigned int bindlookup : 1;
82         char hostname[256];
83         struct _IOSocket *iosocket;
84         struct _IODNSQuery *query;
85         struct IODNSResult *result;
86 };
87
88 struct _IOSocket {
89     int fd;
90         
91         unsigned int socket_flags : 24;
92         
93         struct {
94                 struct IODNSAddress addr;
95                 struct IOSocketDNSLookup *addrlookup;
96         } bind;
97         struct {
98                 struct IODNSAddress addr;
99                 struct IOSocketDNSLookup *addrlookup;
100         } dest;
101         
102         unsigned int port : 16;
103         
104         struct IOSocketBuffer readbuf;
105     struct IOSocketBuffer writebuf;
106         
107         struct IOSSLDescriptor *sslnode;
108         
109         void *parent;
110         
111         struct _IOSocket *next, *prev;
112 };
113
114 void _init_sockets();
115 void iosocket_loop(int usec);
116 void iosocket_lookup_callback(struct IOSocketDNSLookup *lookup, struct IODNSEvent *event);
117 void iosocket_events_callback(struct _IOSocket *iosock, int readable, int writeable);
118
119 #define iosocket_wants_writes(IOSOCK) (IOSOCK->writebuf.bufpos || (IOSOCK->socket_flags & (IOSOCKETFLAG_CONNECTING | IOSOCKETFLAG_SSL_WANTWRITE)))
120
121 #endif
122
123 struct IOSocketEvent;
124
125 #define IOSOCKET_CALLBACK(NAME) void NAME(struct IOSocketEvent *event)
126 typedef IOSOCKET_CALLBACK(iosocket_callback);
127
128 enum IOSocketStatus { 
129     IOSOCKET_CLOSED, /* descriptor is dead (socket waiting for removal or timer) */
130     IOSOCKET_LISTENING, /* descriptor is waiting for connections (server socket) */
131     IOSOCKET_CONNECTING, /* descriptor is waiting for connection approval (connecting client socket) */
132     IOSOCKET_CONNECTED /* descriptor is connected (connected client socket) */
133 };
134
135 enum IOSocketEventType {
136     IOSOCKETEVENT_IGNORE,
137     IOSOCKETEVENT_RECV, /* client socket received something (read_lines == 1  =>  recv_str valid;  read_lines == 0  =>  recv_buf valid) */
138     IOSOCKETEVENT_CONNECTED, /* client socket connected successful */
139     IOSOCKETEVENT_NOTCONNECTED, /* client socket could not connect (errid valid) */
140     IOSOCKETEVENT_CLOSED, /* client socket lost connection (errid valid) */
141     IOSOCKETEVENT_ACCEPT, /* server socket accepted new connection (accept_socket valid) */
142     IOSOCKETEVENT_SSLFAILED, /* failed to initialize SSL session */
143         IOSOCKETEVENT_DNSFAILED /* failed to lookup DNS information (recv_str contains error message) */
144 };
145
146 struct IOSocket {
147         void *iosocket;
148         
149         enum IOSocketStatus status;
150         int listening : 1;
151         int ssl : 1;
152         int parse_delimiter : 1;
153         int parse_empty : 1; /* parse "empty" lines (only if parse_delimiter is set) */
154         unsigned char delimiters[IOSOCKET_PARSE_DELIMITERS_COUNT];
155         
156         void *data;
157         iosocket_callback *callback;
158 };
159
160 struct IOSocketEvent {
161     enum IOSocketEventType type;
162     struct IOSocket *socket;
163     union {
164         char *recv_str;
165                 struct IOSocketBuffer *recv_buf;
166         int errid;
167         struct IOSocket *accept_socket;
168     } data;
169 };
170
171
172 #define IOSOCKET_ADDR_IPV4 0x01
173 #define IOSOCKET_ADDR_IPV6 0x02 /* overrides IOSOCKET_ADDR_IPV4 */
174 #define IOSOCKET_PROTO_UDP 0x04
175
176 struct IOSocket *iosocket_connect(const char *hostname, unsigned int port, int ssl, const char *bindhost, iosocket_callback *callback);
177 struct IOSocket *iosocket_connect_flags(const char *hostname, unsigned int port, int ssl, const char *bindhost, iosocket_callback *callback, int flags);
178 struct IOSocket *iosocket_listen(const char *hostname, unsigned int port, iosocket_callback *callback);
179 struct IOSocket *iosocket_listen_flags(const char *hostname, unsigned int port, iosocket_callback *callback, int flags);
180 struct IOSocket *iosocket_listen_ssl(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iosocket_callback *callback);
181 struct IOSocket *iosocket_listen_ssl_flags(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iosocket_callback *callback, int flags);
182 void iosocket_write(struct IOSocket *iosocket, const char *line);
183 void iosocket_send(struct IOSocket *iosocket, const char *data, size_t datalen);
184 void iosocket_printf(struct IOSocket *iosocket, const char *text, ...);
185 void iosocket_close(struct IOSocket *iosocket);
186
187 #endif