-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsocket_peer.c
250 lines (209 loc) · 8.63 KB
/
websocket_peer.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
/*
* SPDX-License-Identifier: MIT
*
* The MIT License (MIT)
*
* Copyright (c) <2020> <Stephan Gatzka>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include "cio/compiler.h"
#include "cio/endian.h"
#include "cio/error_code.h"
#include "cio/eventloop.h"
#include "cio/http_client.h"
#include "cio/http_location.h"
#include "cio/http_server.h"
#include "cio/socket_address.h"
#include "cio/util.h"
#include "sclog/sclog.h"
#include "protocol_version.h"
#include "sj_log.h"
#include "websocket_peer.h"
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif
// TODO(gatzka): Make constants configuratble via cmake.
enum { HTTPSERVER_LISTEN_PORT = 8080 };
enum { READ_BUFFER_SIZE = 2000 };
static const uint64_t HEADER_READ_TIMEOUT = UINT64_C(5) * UINT64_C(1000) * UINT64_C(1000) * UINT64_C(1000);
static const uint64_t BODY_READ_TIMEOUT = UINT64_C(5) * UINT64_C(1000) * UINT64_C(1000) * UINT64_C(1000);
static const uint64_t RESPONSE_TIMEOUT = UINT64_C(1) * UINT64_C(1000) * UINT64_C(1000) * UINT64_C(1000);
static const uint64_t CLOSE_TIMEOUT_NS = UINT64_C(1) * UINT64_C(1000) * UINT64_C(1000) * UINT64_C(1000);
static void serve_error(struct cio_http_server *s, const char *reason)
{
(void)s;
//TODO(gatzka): close all peers/websocket_peers?
sclog_message(&sj_log, SCLOG_ERROR, "http server error %s!", reason);
}
static struct cio_socket *alloc_http_client(void)
{
struct cio_http_client *client = malloc(sizeof(*client) + READ_BUFFER_SIZE);
if (cio_unlikely(client == NULL)) {
return NULL;
}
client->buffer_size = READ_BUFFER_SIZE;
return &client->socket;
}
static void free_http_client(struct cio_socket *socket)
{
struct cio_http_client *client = cio_container_of(socket, struct cio_http_client, socket);
free(client);
}
static void on_connect(struct cio_websocket *ws)
{
struct cio_websocket_location_handler *handler = cio_container_of(ws, struct cio_websocket_location_handler, websocket);
struct websocket_peer *ws_peer = cio_container_of(handler, struct websocket_peer, ws_handler);
start_peer(&ws_peer->peer);
}
static void free_websocket_handler(struct cio_websocket_location_handler *wslh)
{
struct websocket_peer *ws_peer = cio_container_of(wslh, struct websocket_peer, ws_handler);
free(ws_peer);
}
static void shutdown_websocket_peer(struct peer *peer)
{
(void)peer;
// Do nothing here. The websocket layer itself closes the websocket connection.
}
static void sent_complete(struct cio_websocket *ws, void *handler_context, enum cio_error err)
{
(void)ws;
struct peer *peer = (struct peer *)handler_context;
if (err != CIO_SUCCESS) {
struct websocket_peer *ws_peer = cio_container_of(peer, struct websocket_peer, peer);
sclog_message(&sj_log, SCLOG_ERROR, "Sending message over websocket failed!");
close_peer(&ws_peer->peer);
}
peer->sent_handler(peer);
}
static void send_message_websocket_peer(struct peer *peer, peer_message_sent_t handler)
{
peer->sent_handler = handler;
struct websocket_peer *ws_peer = cio_container_of(peer, struct websocket_peer, peer);
ws_peer->write_message_length = (uint32_t)peer->wbh.data.head.total_length;
cio_write_buffer_const_element_init(&ws_peer->wb, &ws_peer->write_message_length, sizeof(ws_peer->write_message_length));
cio_write_buffer_queue_head(&peer->wbh, &ws_peer->wb);
enum cio_error err = cio_websocket_write_message_first_chunk(&ws_peer->ws_handler.websocket, peer->wbh.data.head.total_length, &peer->wbh, true, true, sent_complete, peer);
if (err != CIO_SUCCESS) {
sclog_message(&sj_log, SCLOG_ERROR, "Start sending message over websocket failed!");
close_peer(&ws_peer->peer);
}
}
static void message_read(struct cio_websocket *ws, void *handler_context, enum cio_error err, size_t frame_length, uint8_t *data, size_t chunk_length, bool last_chunk, bool last_frame, bool is_binary)
{
(void)ws;
(void)chunk_length;
struct websocket_peer *ws_peer = (struct websocket_peer *)handler_context;
if (cio_unlikely(err != CIO_SUCCESS)) {
sclog_message(&sj_log, SCLOG_ERROR, "Receiving message over websocket failed!");
close_peer(&ws_peer->peer);
return;
}
if (!last_chunk || !is_binary || !last_frame) {
sclog_message(&sj_log, SCLOG_ERROR, "Receiving websocket message in wrong format!");
close_peer(&ws_peer->peer);
return;
}
uint32_t message_length;
memcpy(&message_length, data, sizeof(message_length));
message_length = cio_le32toh(message_length);
if (cio_unlikely(frame_length != message_length + sizeof(message_length))) {
sclog_message(&sj_log, SCLOG_ERROR, "Websocket frame length does not correspond with jet message length!");
close_peer(&ws_peer->peer);
return;
}
uint8_t *message = data + sizeof(message_length);
ws_peer->peer.recvd_hander(&ws_peer->peer, message, message_length);
}
static void receive_message_websocket_peer(struct peer *peer, peer_message_received_t handler)
{
peer->recvd_hander = handler;
struct websocket_peer *ws_peer =
cio_container_of(peer, struct websocket_peer, peer);
enum cio_error err = cio_websocket_read_message(&ws_peer->ws_handler.websocket, message_read, ws_peer);
if (cio_unlikely(err != CIO_SUCCESS)) {
sclog_message(&sj_log, SCLOG_ERROR, "Start receiving message over websocket failed!");
close_peer(&ws_peer->peer);
}
}
static struct cio_http_location_handler *alloc_websocket_handler(const void *config)
{
(void)config;
struct websocket_peer *ws_peer = malloc(sizeof(*ws_peer));
if (cio_unlikely(ws_peer == NULL)) {
return NULL;
}
static const char *subprotocols[] = {"jet"};
enum cio_error err = cio_websocket_location_handler_init(&ws_peer->ws_handler, subprotocols, ARRAY_SIZE(subprotocols), on_connect, free_websocket_handler);
if (cio_unlikely(err != CIO_SUCCESS)) {
free(ws_peer);
return NULL;
}
ws_peer->peer.shutdown_peer = shutdown_websocket_peer;
ws_peer->peer.send_message = send_message_websocket_peer;
ws_peer->peer.receive_message = receive_message_websocket_peer;
return &ws_peer->ws_handler.http_location;
}
enum cio_error prepare_websocket_peer_connection(struct cio_http_server *server, const struct cio_inet_address *address, struct cio_http_location *target_jet, struct cio_eventloop *loop)
{
struct cio_http_server_configuration config = {
.on_error = serve_error,
.read_header_timeout_ns = HEADER_READ_TIMEOUT,
.read_body_timeout_ns = BODY_READ_TIMEOUT,
.response_timeout_ns = RESPONSE_TIMEOUT,
.close_timeout_ns = CLOSE_TIMEOUT_NS,
.use_tcp_fastopen = false,
.alloc_client = alloc_http_client,
.free_client = free_http_client};
enum cio_error err = cio_init_inet_socket_address(&config.endpoint, address, HTTPSERVER_LISTEN_PORT);
if (cio_unlikely(err != CIO_SUCCESS)) {
sclog_message(&sj_log, SCLOG_ERROR, "Could not init server socket address for websocket!");
return err;
}
err = cio_http_server_init(server, loop, &config);
if (cio_unlikely(err != CIO_SUCCESS)) {
sclog_message(&sj_log, SCLOG_ERROR, "Could not init http server!");
return err;
}
err = cio_http_location_init(target_jet, "/api/scramjet/1.0/", NULL, alloc_websocket_handler);
if (cio_unlikely(err != CIO_SUCCESS)) {
sclog_message(&sj_log, SCLOG_ERROR, "Could not init jet service location!");
goto shutdown_server;
}
err = cio_http_server_register_location(server, target_jet);
if (cio_unlikely(err != CIO_SUCCESS)) {
sclog_message(&sj_log, SCLOG_ERROR, "Could not register jet service location!");
goto shutdown_server;
}
err = cio_http_server_serve(server);
if (cio_unlikely(err != CIO_SUCCESS)) {
sclog_message(&sj_log, SCLOG_ERROR, "Could not start serving http!");
goto shutdown_server;
}
return CIO_SUCCESS;
shutdown_server:
cio_http_server_shutdown(server, NULL);
return err;
}