-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp2.c
489 lines (399 loc) · 10.8 KB
/
http2.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/**
* HTTP/2 implementation
*/
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <event2/event.h>
#include "defines.h"
#include "util.h"
#include "http2.h"
static void http2_connection_read(evutil_socket_t, short, void *);
static void http2_connection_write(evutil_socket_t, short, void *);
static void http2_frame_free(struct http2_frame *);
static int http2_frame_recv(struct http2_frame *);
static int http2_frame_send(struct http2_frame *);
static int http2_frame_settings_handler(struct http2_frame *);
static int http2_frame_settings_send(struct http2_connection *, struct http2_setting *, int, int);
/* Frame handlers */
struct http2_frame_handler http2_frame_handlers[] = {
{ HTTP2_FRAME_SETTINGS, http2_frame_settings_handler },
{ -1, NULL }
};
struct http2_connection *
http2_connection_new(int sockfd, struct event_base *evbase)
{
struct http2_connection *conn;
/* Allocates structure */
conn = calloc(1, sizeof(*conn));
if (conn == NULL) {
prterrno("calloc");
return NULL;
}
/* Sets initial values */
conn->cn_sockfd = sockfd;
/* Creates events for socket's reading and writing readiness */
conn->cn_rdevent = event_new(evbase, sockfd, EV_READ,
http2_connection_read, conn);
conn->cn_wrevent = event_new(evbase, sockfd, EV_WRITE,
http2_connection_write, conn);
if (conn->cn_rdevent == NULL || conn->cn_wrevent == NULL) {
prterr("event_new: failure.");
free(conn);
return NULL;
}
/* Arms reading event */
if (event_add(conn->cn_rdevent, NULL) < 0) {
prterr("event_add: failure.");
event_free(conn->cn_rdevent);
free(conn);
return NULL;
}
return conn;
}
void
http2_connection_free(struct http2_connection *conn)
{
struct http2_frame *fr;
if (conn == NULL)
return;
close(conn->cn_sockfd);
event_free(conn->cn_rdevent);
event_free(conn->cn_wrevent);
http2_frame_free(conn->cn_rxframe);
fr = conn->cn_txframe;
while (fr != NULL) {
struct http2_frame *next;
next = fr->fr_next;
http2_frame_free(fr);
fr = next;
}
free(conn);
}
static void
http2_connection_read(evutil_socket_t sockfd, short events, void *arg)
{
struct http2_connection *conn;
struct http2_frame *fr;
ssize_t bytes;
size_t len;
conn = arg;
/* New frame received */
if (conn->cn_rxframe == NULL) {
char buf[HTTP2_FRAME_HEADER_SIZE];
/* Receives header */
bytes = recv(sockfd, buf, sizeof(buf), 0);
if (bytes < 0) {
prterrno("recv");
goto error;
}
else if (bytes == 0) {
prterr("recv: connection was closed.");
goto error;
}
else if (bytes < sizeof(buf)) {
/* TODO handle this */
prterr("recv: imcomplete reception of header.");
goto error;
}
/* Creates a new frame */
conn->cn_rxframe = http2_frame_new(conn);
if (conn->cn_rxframe == NULL) {
prterr("http2_frame_new: failure.");
goto error;
}
/* Fills frame header structure */
conn->cn_rxframe->fr_length =
buf[0] << 16 | buf[1] << 8 | buf[2];
conn->cn_rxframe->fr_type = buf[3];
conn->cn_rxframe->fr_flags = buf[4];
conn->cn_rxframe->fr_streamid =
(buf[5] & 0x7F) << 24 | buf[6] << 16 | buf[7] << 8 | buf[8];
/* Allocates buffer */
conn->cn_rxframe->fr_buf = malloc(conn->cn_rxframe->fr_length);
if (conn->cn_rxframe->fr_buf == NULL) {
perror("malloc");
goto error;
}
conn->cn_rxframe->fr_buflen = 0;
}
fr = conn->cn_rxframe;
/* Receives remaining bytes, if there is any to receive */
len = fr->fr_length - fr->fr_buflen;
if (len != 0) {
bytes = recv(sockfd, &fr->fr_buf[fr->fr_buflen], len, 0);
if (bytes < 0) {
prterrno("recv");
goto error;
}
else if (bytes == 0) {
prterr("recv: connection was closed.");
goto error;
}
fr->fr_buflen += bytes;
}
/* Checks buffer overflow */
if (fr->fr_buflen > fr->fr_length) {
prterr("http2_connection_read: frame buffer overflow!");
goto error;
}
/* Handles fully received frame */
if (fr->fr_buflen == fr->fr_length) {
if (http2_frame_recv(fr) < 0) {
prterr("http2_frame_recv: failure.");
goto error;
}
conn->cn_rxframe = NULL;
}
/* Rearms reading event */
if (event_add(conn->cn_rdevent, NULL) < 0) {
prterr("event_add: failure.");
goto error;
}
return;
error:
http2_connection_free(conn);
return;
}
static void
http2_connection_write(evutil_socket_t sockfd, short events, void *arg)
{
struct http2_connection *conn;
struct http2_frame *fr;
ssize_t bytes;
size_t len;
conn = arg;
fr = conn->cn_txframe;
/* If not sent, creates header and sends it */
if (fr->fr_buflen == -1) {
char buf[HTTP2_FRAME_HEADER_SIZE];
/* length */
buf[0] = (fr->fr_length & 0xFF0000U) >> 16;
buf[1] = (fr->fr_length & 0x00FF00U) >> 8;
buf[2] = (fr->fr_length & 0x0000FFU);
/* type */
buf[3] = fr->fr_type;
/* flags */
buf[4] = fr->fr_flags;
/* reserved bit + stream id */
buf[5] = (fr->fr_streamid & 0x7F000000U) >> 24;
buf[6] = (fr->fr_streamid & 0x00FF0000U) >> 16;
buf[7] = (fr->fr_streamid & 0x0000FF00U) >> 8;
buf[8] = (fr->fr_streamid & 0x000000FFU);
bytes = send(sockfd, buf, sizeof(buf), 0);
if (bytes < 0) {
prterrno("send");
goto error;
}
else if (bytes < sizeof(buf)) {
/* TODO handle this */
prterr("send: incomplete transmission of header.");
goto error;
}
fr->fr_buflen = 0;
prtinfo("(%d) Header for frame of type 0x%02x was sent.",
sockfd, fr->fr_type);
}
/* Sends remaining bytes */
len = fr->fr_length - fr->fr_buflen;
bytes = send(sockfd, &fr->fr_buf[fr->fr_buflen], len, 0);
if (bytes < 0) {
prterrno("send");
goto error;
}
fr->fr_buflen += bytes;
/* Checks buffer overflow */
if (fr->fr_buflen > fr->fr_length) {
prterr("http2_connection_write: frame buffer overflow!");
goto error;
}
/* Frees fully sent frame and sends next on list */
if (fr->fr_buflen == fr->fr_length) {
struct http2_frame *next;
next = fr->fr_next;
prtinfo("(%d) Frame of type 0x%02x was fully sent. (size=%d)",
sockfd, fr->fr_type, fr->fr_length);
http2_frame_free(fr);
/* Returns without rearming writing event if no other frames
* are to be sent */
if (next == NULL) {
conn->cn_txframe = NULL;
return;
}
conn->cn_txframe = next;
}
/* Rearms writing event */
if (event_add(conn->cn_wrevent, NULL) < 0) {
prterr("event_add: failure.");
goto error;
}
return;
error:
http2_connection_free(conn);
return;
}
struct http2_frame *
http2_frame_new(struct http2_connection *conn)
{
struct http2_frame *fr;
fr = calloc(1, sizeof(*fr));
if (fr == NULL) {
prterrno("calloc");
return NULL;
}
fr->fr_conn = conn;
fr->fr_buflen = -1;
return fr;
}
/**
* http2_frame_free() does not and should not free next frames on list.
*/
static void
http2_frame_free(struct http2_frame *fr)
{
if (fr == NULL)
return;
free(fr->fr_buf);
free(fr);
}
static int
http2_frame_recv(struct http2_frame *fr)
{
struct http2_frame_handler *fh;
if (fr == NULL)
return -1;
prtinfo("(%d) RX frame: len=%d type=%02x flags=%02x stream=%d\n",
fr->fr_conn->cn_sockfd, fr->fr_length, fr->fr_type,
fr->fr_flags, fr->fr_streamid);
/* Looks for and calls handler for this frame type */
for (fh = http2_frame_handlers; fh->fh_type != -1; fh++)
if (fh->fh_type == fr->fr_type)
return fh->fh_handler(fr);
/* Not supported frames must be ignored and discarded */
prtinfo("(%d) Unsupported frame type - ignored.", fr->fr_conn->cn_sockfd);
http2_frame_free(fr);
return 0;
}
static int
http2_frame_send(struct http2_frame *fr)
{
if (fr == NULL)
return -1;
/* Enqueues frame */
if (fr->fr_conn->cn_txframe == NULL)
fr->fr_conn->cn_txframe = fr;
else
fr->fr_conn->cn_txlastframe->fr_next = fr;
fr->fr_conn->cn_txlastframe = fr;
prtinfo("(%d) Frame of type 0x%02x enqueued for sending. (size=%d)",
fr->fr_conn->cn_sockfd, fr->fr_type, fr->fr_length);
/* Arms writing event */
if (event_add(fr->fr_conn->cn_wrevent, NULL) < 0) {
prterr("event_add: failure.");
http2_connection_free(fr->fr_conn);
return -1;
}
return 0;
}
static int
http2_frame_settings_handler(struct http2_frame *fr)
{
int pos;
/* Checks frame size */
if ((!(fr->fr_flags & HTTP2_FRAME_SETTINGS_ACK) &&
fr->fr_length % HTTP2_FRAME_SETTINGS_PARAM_SIZE != 0) ||
(fr->fr_flags & HTTP2_FRAME_SETTINGS_ACK &&
fr->fr_length != 0)) {
/* TODO connection error: FRAME_SIZE_ERROR */
prtinfo("(%d) Connection error: "
"SETTINGS frame with wrong frame size "
"(size=%d,ack=%d)",
fr->fr_conn->cn_sockfd, fr->fr_length,
fr->fr_flags & HTTP2_FRAME_SETTINGS_ACK);
return -1;
}
/* Checks stream ID */
if (fr->fr_streamid != 0) {
/* TODO connection error: PROTOCOL_ERROR */
prtinfo("(%d) Connection error: "
"SETTINGS frame with wrong stream ID "
"(id=%d)",
fr->fr_conn->cn_sockfd,
fr->fr_streamid);
return -1;
}
/* On ACK reception, the new requested frames can be set definitely */
if (fr->fr_flags & HTTP2_FRAME_SETTINGS_ACK) {
/* TODO set new settings definitely */
prtinfo("(%d) Previously sent SETTINGS frame acknowledged.",
fr->fr_conn->cn_sockfd);
return 0;
}
prtinfo("(%d) SETTINGS frame received with %d setting(s).",
fr->fr_conn->cn_sockfd,
fr->fr_length / HTTP2_FRAME_SETTINGS_PARAM_SIZE);
/* Saves remote's settings */
for (pos = 0; pos < fr->fr_length;
pos += HTTP2_FRAME_SETTINGS_PARAM_SIZE) {
struct http2_setting set;
char *ptr;
ptr = &fr->fr_buf[pos];
set.set_id = ptr[0] << 8 | ptr[1];
set.set_value = ptr[2] << 24 | ptr[3] << 16 | ptr[4] << 8 | ptr[5];
/* TODO set settings!
http2_setting_set(set);
*/
prtinfo("(%d) New setting: "
"[0x%04x] = 0x%08x.",
fr->fr_conn->cn_sockfd,
set.set_id, set.set_value);
}
/* Sends ACK to remote peer */
if (http2_frame_settings_send(fr->fr_conn, NULL, 0, 1) < 0) {
prterr("http2_frame_settings_send: failure.");
return -1;
}
prtinfo("(%d) SETTINGS ACK frame sent back to remote.",
fr->fr_conn->cn_sockfd);
http2_frame_free(fr);
return 0;
}
static int
http2_frame_settings_send(struct http2_connection *conn,
struct http2_setting *set, int nsets, int ack)
{
struct http2_frame *fr;
fr = http2_frame_new(conn);
if (fr == NULL) {
prterr("http2_frame_new: failure.");
return -1;
}
fr->fr_type = HTTP2_FRAME_SETTINGS;
fr->fr_streamid = 0;
if (ack) {
/* Creates an empty frame with only the ACK flag set */
fr->fr_length = 0;
fr->fr_flags = HTTP2_FRAME_SETTINGS_ACK;
}
else
fr->fr_length = nsets * HTTP2_FRAME_SETTINGS_PARAM_SIZE;
prtinfo("(%d) SETTINGS frame being sent (nsets=%d,ack=%d).",
conn->cn_sockfd, nsets, ack);
if (http2_frame_send(fr) < 0) {
prterr("http2_frame_send: failure.");
return -1;
}
return 0;
}
int
http2_settings_send(struct http2_connection *conn, struct http2_setting *set, int nsets)
{
return http2_frame_settings_send(conn, set, nsets, 0);
}