-
Notifications
You must be signed in to change notification settings - Fork 0
/
lws.c
359 lines (326 loc) · 8.73 KB
/
lws.c
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// SPDX-License-Identifier: MIT
// Copyright (C) 2021 Marcelo Diop-Gonzalez
#include <glib.h>
#include <libwebsockets.h>
#include <stdbool.h>
#include "net-backend.h"
struct websocket_conn {
struct lws *wsi;
char *host;
char *path;
void *user;
unsigned char **headers_start;
unsigned char *headers_end;
};
const int _net_write_buf_padding = LWS_PRE;
int ws_conn_write(struct websocket_conn *ws, const char *buf, size_t len) {
if (lws_write(ws->wsi, (unsigned char *)buf, len, LWS_WRITE_TEXT) < len) {
// TODO: exchg_log() should be accessible here without including exchg.h
fprintf(stderr, "lws_write() error writing %zu bytes:\n%s\n", len, buf);
return -1;
}
return len;
}
int ws_conn_add_header(struct websocket_conn *ws, const unsigned char *name,
const unsigned char *val, size_t len) {
if (lws_add_http_header_by_name(ws->wsi, name, val, len,
ws->headers_start, ws->headers_end)) {
fprintf(stderr, "lws_add_http_header_by_name() error\n");
return -1;
}
return 0;
}
void ws_conn_close(struct websocket_conn *ws) {
lws_set_timeout(ws->wsi, PENDING_TIMEOUT_USER_OK,
LWS_TO_KILL_ASYNC);
}
static int websocket_callback(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len) {
const struct net_callbacks *c = lws_context_user(lws_get_context(wsi));
const struct websocket_conn_callbacks *ops = &c->ws;
struct websocket_conn *ws = user;
switch (reason) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
lwsl_err("Websocket Connection Error: %s%s: %s\n",
ws->host, ws->path, in ? (char *)in : "(null)");
ops->on_error(ws->user);
free(ws->host);
free(ws->path);
free(ws);
break;
case LWS_CALLBACK_CLIENT_ESTABLISHED:
ops->on_established(ws->user);
break;
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
ws->headers_start = (unsigned char **)in;
ws->headers_end = *ws->headers_start + len;
return ops->add_headers(ws->user, ws);
case LWS_CALLBACK_CLIENT_RECEIVE:
return ops->recv(ws->user, in, len);
case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
// TODO
break;
case LWS_CALLBACK_CLIENT_CLOSED:
ops->on_closed(ws->user);
free(ws->host);
free(ws->path);
free(ws);
return 0;
default:
break;
}
return lws_callback_http_dummy(wsi, reason, user, in, len);
}
static int prepare_http_client_read(struct lws *wsi) {
char buffer[1024 + LWS_PRE];
char *p = buffer + LWS_PRE;
int len = sizeof(buffer) - LWS_PRE;
return lws_http_client_read(wsi, &p, &len);
}
struct http_conn {
struct lws *wsi;
char *host;
char *path;
int status;
bool want_write;
unsigned char **headers_start;
unsigned char *headers_end;
void *user;
};
int http_conn_status(struct http_conn *req) {
return req->status;
}
int http_conn_add_header(struct http_conn *req, const unsigned char *name,
const unsigned char *val, size_t len) {
if (lws_add_http_header_by_name(req->wsi, name, val, len,
req->headers_start, req->headers_end)) {
fprintf(stderr, "lws_add_http_header_by_name() error\n");
return -1;
}
return 0;
}
void http_conn_close(struct http_conn *req) {
lws_set_timeout(req->wsi, PENDING_TIMEOUT_USER_OK,
LWS_TO_KILL_ASYNC);
}
void http_conn_want_write(struct http_conn *req) {
req->want_write = true;
}
static int http_callback(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len) {
const struct net_callbacks *c = lws_context_user(lws_get_context(wsi));
const struct http_callbacks *http = &c->http;
struct http_conn *req = user;
char *body;
size_t body_len;
int ret;
switch (reason) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
lwsl_err("HTTP CONNECTION ERROR: %s%s: %s\n",
req->host, req->path, in ? (char *)in : "(null)");
http->on_error(req->user, (char *)in);
free(req->host);
free(req->path);
free(req);
break;
case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
req->status = lws_http_client_http_response(wsi);
http->on_established(req->user, req->status);
break;
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
if (req->want_write) {
lws_client_http_body_pending(wsi, 1);
lws_callback_on_writable(wsi);
}
req->headers_start = (unsigned char **)in;
req->headers_end = *req->headers_start + len;
return http->add_headers(req->user, req);
case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE:
http->write(req->user, &body, &body_len);
if (lws_write(req->wsi, (unsigned char *)body,
body_len, LWS_WRITE_TEXT) < body_len) {
lwsl_err("%s%s: write error\n", req->host, req->path);
ret = -1;
} else {
ret = 0;
}
lws_client_http_body_pending(wsi, 0);
return ret;
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
return prepare_http_client_read(wsi);
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
return http->recv(req->user, in, len);
// TODO: not always called. to trigger, remove ssl global init
// in context_create_info, or set hostname to garbage
case LWS_CALLBACK_CLOSED_CLIENT_HTTP:
http->on_closed(req->user);
free(req->host);
free(req->path);
free(req);
break;
default:
break;
}
return lws_callback_http_dummy(wsi, reason, user, in, len);
}
struct lws_protocols protocols[] = {
{
"ws",
websocket_callback,
},
{
"http",
http_callback,
},
{},
};
struct exchg_net_context {
GMainLoop *loop;
struct lws_context *ctx;
};
struct http_conn *http_dial(struct exchg_net_context *ctx,
const char *host, const char *path,
const char *method, void *private) {
struct http_conn *req = malloc(sizeof(*req));
if (!req) {
fprintf(stderr, "OOM: %s\n", __func__);
return NULL;
}
memset(req, 0, sizeof(*req));
struct lws_client_connect_info info = {
.context = ctx->ctx,
.port = 443,
.address = host,
.path = path,
.method = method,
.host = host,
.origin = host,
.ssl_connection = LCCSCF_USE_SSL,
.protocol = "http",
.userdata = req,
.pwsi = &req->wsi,
};
req->host = strdup(host);
req->path = strdup(path);
req->user = private;
if (!req->host || !req->path) {
fprintf(stderr, "OOM: %s\n", __func__);
free(req->host);
free(req->path);
free(req);
return NULL;
}
if (!lws_client_connect_via_info(&info)) {
fprintf(stderr, "lws_client_connect_via_info() error connecting to %s%s\n", host, path);
free(req);
return NULL;
}
return req;
}
struct websocket_conn *ws_dial(struct exchg_net_context *ctx, const char *host,
const char *path, void *private) {
struct websocket_conn *ws = malloc(sizeof(*ws));
if (!ws) {
fprintf(stderr, "OOM: %s\n", __func__);
return NULL;
}
memset(ws, 0, sizeof(*ws));
struct lws_client_connect_info info = {
.context = ctx->ctx,
.port = 443,
.address = host,
.path = path,
.host = host,
.origin = host,
.ssl_connection = LCCSCF_USE_SSL,
.protocol = "ws",
.userdata = ws,
.pwsi = &ws->wsi,
};
if (!lws_client_connect_via_info(&info)) {
free(ws);
fprintf(stderr, "websocket connection to %s%s failed\n", host, path);
return NULL;
}
ws->user = private;
ws->host = strdup(host);
ws->path = strdup(path);
if (!ws->host || !ws->path) {
fprintf(stderr, "OOM: %s\n", __func__);
free(ws->host);
free(ws->path);
free(ws);
return NULL;
}
return ws;
}
void net_service(struct exchg_net_context *ctx) {
g_main_context_iteration(NULL, TRUE);
}
void net_run(struct exchg_net_context *ctx) {
g_main_loop_run(ctx->loop);
}
void net_stop(struct exchg_net_context *ctx) {
g_main_loop_quit(ctx->loop);
}
void net_destroy(struct exchg_net_context *ctx) {
lws_context_destroy(ctx->ctx);
g_main_loop_unref(ctx->loop);
free(ctx);
}
struct exchg_net_context *net_new(struct net_callbacks *c) {
struct exchg_net_context *ret = malloc(sizeof(*ret));
if (!ret) {
fprintf(stderr, "OOM: %s\n", __func__);
return NULL;
}
ret->loop = g_main_loop_new(NULL, false);
struct lws_context_creation_info info = {
.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT |
LWS_SERVER_OPTION_GLIB,
.port = CONTEXT_PORT_NO_LISTEN,
.protocols = protocols,
.foreign_loops = (void **)&ret->loop,
.user = c,
};
lws_set_log_level(LLL_WARN | LLL_ERR, NULL);
ret->ctx = lws_create_context(&info);
if (!ret->ctx) {
g_main_loop_unref(ret->loop);
free(ret);
fprintf(stderr, "lws_create_context failed\n");
return NULL;
}
return ret;
}
struct timer {
GSource *source;
void (*f)(void *);
void *p;
};
gboolean timeout_callback(void *p) {
struct timer *t = p;
t->f(t->p);
g_source_unref(t->source);
free(t);
return FALSE;
}
struct timer *timer_new(struct exchg_net_context *ctx, void (*f)(void *),
void *p, int seconds) {
struct timer *t = malloc(sizeof(*t));
if (!t) {
fprintf(stderr, "%s: OOM\n", __func__);
return NULL;
}
t->f = f;
t->p = p;
t->source = g_timeout_source_new_seconds(seconds);
g_source_set_callback(t->source, timeout_callback, t, NULL);
g_source_attach(t->source, NULL);
return t;
}
void timer_cancel(struct timer *t) {
g_source_destroy(t->source);
g_source_unref(t->source);
free(t);
}