1 /* IOSockets.h - IOMultiplexer v2
2 * Copyright (C) 2014 Philipp Kreil (pk910)
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.
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.
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/>.
19 #ifndef _IOHandler_internals
20 #include "IOHandler.h"
23 #define IOSOCKET_LINE_LEN 1024
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 */
39 struct IOSocketBuffer;
40 struct IOSSLDescriptor;
43 struct IOSocketDNSLookup {
44 unsigned int bindlookup : 1;
46 struct _IOSocket *iosocket;
47 struct _IODNSQuery *query;
48 struct IODNSResult *result;
54 unsigned int socket_flags : 16;
57 struct IODNSAddress addr;
58 struct IOSocketDNSLookup *addrlookup;
61 struct IODNSAddress addr;
62 struct IOSocketDNSLookup *addrlookup;
65 unsigned int port : 16;
67 struct IOSocketBuffer readbuf;
68 struct IOSocketBuffer writebuf;
70 struct IOSSLDescriptor *sslnode;
74 struct _IOSocket *next, *prev;
78 void iosocket_lookup_callback(struct IOSocketDNSLookup *lookup, struct IODNSEvent *event);
84 #define IOSOCKET_CALLBACK(NAME) void NAME(struct IOSocketEvent *event)
85 typedef IOSOCKET_CALLBACK(iosocket_callback);
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) */
94 enum IOSocketEventType {
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 */
108 enum IOSocketStatus status;
114 iosocket_callback *callback;
117 struct IOSocketBuffer {
119 size_t bufpos, buflen;
122 struct IOSocketEvent {
123 enum IOSocketEventType type;
124 struct IOSocket *socket;
127 struct IOSocketBuffer *recv_buf;
129 struct IOSocket *accept_socket;
134 #define IOSOCKET_ADDR_IPV4 0x01
135 #define IOSOCKET_ADDR_IPV6 0x02 /* overrides IOSOCKET_ADDR_IPV4 */
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);