forked from uNetworking/uSockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibusockets_new.h
261 lines (228 loc) · 12 KB
/
libusockets_new.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Authored by Alex Hultman, 2018-2019.
* Intellectual property of third-party.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "libusockets.h"
/* This is an experimental new API that is identical for SSL and non-SSL. */
#ifndef LIBUSOCKETS_NEW_H
#define LIBUSOCKETS_NEW_H
/* Todo: make every struct type end with _t */
struct us_new_socket_t;
struct us_new_socket_context_t;
/* Temporary (must match with ssl options) */
struct us_new_socket_context_options_t {
const char *key_file_name;
const char *cert_file_name;
const char *passphrase;
const char *dh_params_file_name;
};
/* Per-socket-context functions */
inline static struct us_new_socket_t *us_new_socket_context_connect(const int ssl, struct us_new_socket_context_t *c, const char *host, int port, int options, int socket_ext_size) {
#ifdef LIBUS_NO_SSL
return (struct us_new_socket_t *) us_socket_context_connect((struct us_socket_context *) c, host, port, options, socket_ext_size);
#else
return ssl ? (struct us_new_socket_t *) us_ssl_socket_context_connect((struct us_ssl_socket_context *) c, host, port, options, socket_ext_size) : (struct us_new_socket_t *) us_socket_context_connect((struct us_socket_context *) c, host, port, options, socket_ext_size);
#endif
}
inline static struct us_new_socket_context_t *us_new_create_socket_context(const int ssl, struct us_loop *loop, int socket_context_ext_size, struct us_new_socket_context_options_t options) {
#ifdef LIBUS_NO_SSL
return (struct us_new_socket_context_t *) us_create_socket_context(loop, socket_context_ext_size);
#else
/* Convert between the two structs */
struct us_ssl_socket_context_options ssl_options = {
options.key_file_name,
options.cert_file_name,
options.passphrase,
options.dh_params_file_name
};
return ssl ? (struct us_new_socket_context_t *) us_create_ssl_socket_context(loop, socket_context_ext_size, ssl_options) : (struct us_new_socket_context_t *) us_create_socket_context(loop, socket_context_ext_size);
#endif
}
inline static struct us_new_socket_context_t *us_new_create_child_socket_context(const int ssl, struct us_new_socket_context_t *c, int socket_context_ext_size) {
#ifdef LIBUS_NO_SSL
return (struct us_new_socket_context_t *) us_create_child_socket_context((struct us_socket_context *) c, socket_context_ext_size);
#else
return ssl ? (struct us_new_socket_context_t *) us_create_child_ssl_socket_context((struct us_ssl_socket_context *) c, socket_context_ext_size) : (struct us_new_socket_context_t *) us_create_child_socket_context((struct us_socket_context *) c, socket_context_ext_size);
#endif
}
inline static struct us_new_socket_t *us_new_socket_context_adopt_socket(const int ssl, struct us_new_socket_context_t *c, struct us_new_socket_t *s, int socket_ext_size) {
#ifdef LIBUS_NO_SSL
return (struct us_new_socket_t *) us_socket_context_adopt_socket((struct us_socket_context *) c, (struct us_socket *) s, socket_ext_size);
#else
return ssl ? (struct us_new_socket_t *) us_ssl_socket_context_adopt_socket((struct us_ssl_socket_context *) c, (struct us_ssl_socket *) s, socket_ext_size) : (struct us_new_socket_t *) us_socket_context_adopt_socket((struct us_socket_context *) c, (struct us_socket *) s, socket_ext_size);
#endif
}
inline static void us_new_socket_context_free(const int ssl, struct us_new_socket_context_t *c) {
#ifdef LIBUS_NO_SSL
us_socket_context_free((struct us_socket_context *) c);
#else
if (ssl) {
us_ssl_socket_context_free((struct us_ssl_socket_context *) c);
} else {
us_socket_context_free((struct us_socket_context *) c);
}
#endif
}
inline static struct us_listen_socket *us_new_socket_context_listen(const int ssl, struct us_new_socket_context_t *c, const char *host, int port, int options, int socket_ext_size) {
#ifdef LIBUS_NO_SSL
return us_socket_context_listen((struct us_socket_context *) c, host, port, options, socket_ext_size);
#else
return ssl ? us_ssl_socket_context_listen((struct us_ssl_socket_context *) c, host, port, options, socket_ext_size) : us_socket_context_listen((struct us_socket_context *) c, host, port, options, socket_ext_size);
#endif
}
inline static struct us_loop *us_new_socket_context_loop(const int ssl, struct us_new_socket_context_t *c) {
#ifdef LIBUS_NO_SSL
return us_socket_context_loop((struct us_socket_context *) c);
#else
return ssl ? us_ssl_socket_context_loop((struct us_ssl_socket_context *) c) : us_socket_context_loop((struct us_socket_context *) c);
#endif
}
inline static void us_new_socket_context_on_data(const int ssl, struct us_new_socket_context_t *c, struct us_new_socket_t *(*on_data)(struct us_new_socket_t *s, char *data, int length)) {
#ifdef LIBUS_NO_SSL
us_socket_context_on_data((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s, char *data, int length)) on_data);
#else
if (ssl) {
us_ssl_socket_context_on_data((struct us_ssl_socket_context *) c, (struct us_ssl_socket *(*)(struct us_ssl_socket *s, char *data, int length)) on_data);
} else {
us_socket_context_on_data((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s, char *data, int length)) on_data);
}
#endif
}
inline static void us_new_socket_context_on_timeout(const int ssl, struct us_new_socket_context_t *c, struct us_new_socket_t *(*on_timeout)(struct us_new_socket_t *s)) {
#ifdef LIBUS_NO_SSL
us_socket_context_on_timeout((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_timeout);
#else
if (ssl) {
us_ssl_socket_context_on_timeout((struct us_ssl_socket_context *) c, (struct us_ssl_socket *(*)(struct us_ssl_socket *s)) on_timeout);
} else {
us_socket_context_on_timeout((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_timeout);
}
#endif
}
inline static void us_new_socket_context_on_end(const int ssl, struct us_new_socket_context_t *c, struct us_new_socket_t *(*on_end)(struct us_new_socket_t *s)) {
#ifdef LIBUS_NO_SSL
us_socket_context_on_end((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_end);
#else
if (ssl) {
us_ssl_socket_context_on_end((struct us_ssl_socket_context *) c, (struct us_ssl_socket *(*)(struct us_ssl_socket *s)) on_end);
} else {
us_socket_context_on_end((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_end);
}
#endif
}
inline static void us_new_socket_context_on_writable(const int ssl, struct us_new_socket_context_t *c, struct us_new_socket_t *(*on_writable)(struct us_new_socket_t *s)) {
#ifdef LIBUS_NO_SSL
us_socket_context_on_writable((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_writable);
#else
if (ssl) {
us_ssl_socket_context_on_writable((struct us_ssl_socket_context *) c, (struct us_ssl_socket *(*)(struct us_ssl_socket *s)) on_writable);
} else {
us_socket_context_on_writable((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_writable);
}
#endif
}
inline static void us_new_socket_context_on_close(const int ssl, struct us_new_socket_context_t *c, struct us_new_socket_t *(*on_close)(struct us_new_socket_t *s)) {
#ifdef LIBUS_NO_SSL
us_socket_context_on_close((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_close);
#else
if (ssl) {
us_ssl_socket_context_on_close((struct us_ssl_socket_context *) c, (struct us_ssl_socket *(*)(struct us_ssl_socket *s)) on_close);
} else {
us_socket_context_on_close((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s)) on_close);
}
#endif
}
inline static void us_new_socket_context_on_open(const int ssl, struct us_new_socket_context_t *c, struct us_new_socket_t *(*on_open)(struct us_new_socket_t *s, int is_client, char *ip, int ip_length)) {
#ifdef LIBUS_NO_SSL
us_socket_context_on_open((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s, int is_client, char *ip, int ip_length)) on_open);
#else
if (ssl) {
us_ssl_socket_context_on_open((struct us_ssl_socket_context *) c, (struct us_ssl_socket *(*)(struct us_ssl_socket *s, int is_client, char *ip, int ip_length)) on_open);
} else {
us_socket_context_on_open((struct us_socket_context *) c, (struct us_socket *(*)(struct us_socket *s, int is_client, char *ip, int ip_length)) on_open);
}
#endif
}
inline static void *us_new_socket_context_ext(const int ssl, struct us_new_socket_context_t *c) {
#ifdef LIBUS_NO_SSL
return us_socket_context_ext((struct us_socket_context *) c);
#else
return ssl ? us_ssl_socket_context_ext((struct us_ssl_socket_context *) c) : us_socket_context_ext((struct us_socket_context *) c);
#endif
}
/* Per-socket functions */
inline static struct us_new_socket_context_t *us_new_socket_context(const int ssl, struct us_new_socket_t *s) {
#ifdef LIBUS_NO_SSL
return (struct us_new_socket_context_t *) us_socket_get_context((struct us_socket *) s);
#else
return ssl ? (struct us_new_socket_context_t *) us_ssl_socket_get_context((struct us_ssl_socket *) s) : (struct us_new_socket_context_t *) us_socket_get_context((struct us_socket *) s);
#endif
}
inline static void us_new_socket_timeout(const int ssl, struct us_new_socket_t *s, unsigned int seconds) {
#ifdef LIBUS_NO_SSL
us_socket_timeout((struct us_socket *) s, seconds);
#else
if (ssl) {
us_ssl_socket_timeout((struct us_ssl_socket *) s, seconds);
} else {
us_socket_timeout((struct us_socket *) s, seconds);
}
#endif
}
inline static void us_new_socket_shutdown(const int ssl, struct us_new_socket_t *s) {
#ifdef LIBUS_NO_SSL
us_socket_shutdown((struct us_socket *) s);
#else
if (ssl) {
us_ssl_socket_shutdown((struct us_ssl_socket *) s);
} else {
us_socket_shutdown((struct us_socket *) s);
}
#endif
}
inline static int us_new_socket_write(const int ssl, struct us_new_socket_t *s, const char *data, int length, int msg_more) {
#ifdef LIBUS_NO_SSL
return us_socket_write((struct us_socket *) s, data, length, msg_more);
#else
return ssl ? us_ssl_socket_write((struct us_ssl_socket *) s, data, length, msg_more) : us_socket_write((struct us_socket *) s, data, length, msg_more);
#endif
}
inline static void *us_new_socket_ext(const int ssl, struct us_new_socket_t *s) {
#ifdef LIBUS_NO_SSL
return us_socket_ext((struct us_socket *) s);
#else
return ssl ? us_ssl_socket_ext((struct us_ssl_socket *) s) : us_socket_ext((struct us_socket *) s);
#endif
}
inline static struct us_new_socket_t *us_new_socket_close(const int ssl, struct us_new_socket_t *s) {
#ifdef LIBUS_NO_SSL
return (struct us_new_socket_t *) us_socket_close((struct us_socket *) s);
#else
return ssl ? (struct us_new_socket_t *) us_ssl_socket_close((struct us_ssl_socket *) s) : (struct us_new_socket_t *) us_socket_close((struct us_socket *) s);
#endif
}
inline static int us_new_socket_is_closed(const int ssl, struct us_new_socket_t *s) {
/* There is no SSL variant of this function */
return us_socket_is_closed((struct us_socket *) s);
}
inline static int us_new_socket_is_shut_down(const int ssl, struct us_new_socket_t *s) {
#ifdef LIBUS_NO_SSL
return us_socket_is_shut_down((struct us_socket *) s);
#else
return ssl ? us_ssl_socket_is_shut_down((struct us_ssl_socket *) s) : us_socket_is_shut_down((struct us_socket *) s);
#endif
}
inline static void us_new_socket_remote_address(const int ssl, struct us_new_socket_t *s, char *buf, int *length) {
/* There is no SSL variant of this function */
us_socket_remote_address((struct us_socket *) s, buf, length);
}
#endif