-
Notifications
You must be signed in to change notification settings - Fork 2
/
ws-header.h
73 lines (62 loc) · 1.96 KB
/
ws-header.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
#ifndef WSS_PROXY_WS_HEADER_H
#define WSS_PROXY_WS_HEADER_H
#include <stdint.h>
struct ws_header_info {
uint8_t fin: 1;
uint8_t rsv: 3;
uint8_t op: 4;
uint8_t mask: 1;
uint8_t header_size: 4;
uint16_t payload_size;
uint32_t mask_key;
};
enum ws_op {
OP_CONTINUATION = 0,
OP_TEXT = 1,
OP_BINARY = 2,
OP_CLOSE = 8,
OP_PING = 9,
OP_PONG = 10,
};
// https://www.iana.org/assignments/websocket/websocket.xhtml
enum ws_close_reason {
CLOSE_NORMAL_CLOSURE = 1000,
CLOSE_GOING_AWAY = 1001,
CLOSE_PROTOCOL_ERROR = 1002,
CLOSE_UNSUPPORTED_DATA = 1003,
CLOSE_RESERVED = 1004,
CLOSE_NO_STATUS_RCVD = 1005,
CLOSE_ABNORMAL_CLOSURE = 1006,
CLOSE_INVALID_FRAME_PAYLOAD_DATA = 1007,
CLOSE_POLICY_VIOLATION = 1008,
CLOSE_MESSAGE_TOO_BIG = 1009,
CLOSE_MANDATORY_EXT = 1010,
CLOSE_INTERNAL_ERROR = 1011,
CLOSE_SERVICE_RESTART = 1012, // Alexey Melnikov, 2012/05/24
CLOSE_TRY_AGAIN_LATER = 1013, // Alexey Melnikov, 2012/05/24
CLOSE_BAD_GATEWAY = 1014, // Alexey Melnikov, 2016/09/08
CLOSE_TLS_HANDSHAKE = 1015,
CLOSE_UNAUTHORIZED = 3000, // Leo Tietz
CLOSE_FORBIDDEN = 3003, // Ada Young
CLOSE_TIMEOUT = 3008, // Morgan Jones
};
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#define WS_HEADER_SIZE 2
#define EXTEND_WS_HEADER_SIZE 4
#define MASK_SIZE 4
#define MAX_WS_HEADER_SIZE (EXTEND_WS_HEADER_SIZE + MASK_SIZE)
#define MAX_CONTROL_FRAME_SIZE 125
/**
* @return 0 when success, >0 for bytes required, -1 for unsupported 64 bits length
*/
int parse_ws_header(const uint8_t *buffer, uint16_t size, struct ws_header_info *info);
/**
* @param payload there should be ws header before payload, use MAX_WS_HEADER_SIZE if unsure
* @return start of the websocket frame
*/
uint8_t *build_ws_header(struct ws_header_info *info, uint8_t *payload, uint16_t payload_size);
void mask(void *buffer, uint16_t size, uint32_t mask_key);
#define unmask mask
#endif // WSS_PROXY_WS_HEADER_H