-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket.h
80 lines (70 loc) · 1.55 KB
/
websocket.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
/* SPDX-License-Identifier: MIT */
/*
* websocket.h - WebSocket frame header protocol
*
* WebSockets are defined here: http://tools.ietf.org/html/rfc6455
*
* Copyright (C) 2014, 2019, 2022 Andrew Clayton
*/
#ifndef _WEBSOCKET_H_
#define _WEBSOCKET_H_
#include <stdint.h>
#include "short_types.h"
#define SHORT_HDR_LEN 2
#define MAX_HDR_LEN 14
#define MKEY_LEN 4
#define PAYLEN_LEN 125
#define PAYLEN_LEN16 126
#define PAYLEN_LEN64 127
#define WS_KEY "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
/*
* Structure idea from
* http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/
*
* It takes advantage of the fact that the websocket frame header will always
* be at least 16bits. These 16bits are mapped into the two bytes within the
* structure.
*
* The base frame protocol is described here:
* http://tools.ietf.org/html/rfc6455#section-5.2
*/
struct websocket_header {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
u16 fin:1;
u16 rsv1:1;
u16 rsv2:1;
u16 rsv3:1;
u16 opcode:4;
u16 masked:1;
u16 pay_len:7;
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
u16 opcode:4;
u16 rsv3:1;
u16 rsv2:1;
u16 rsv1:1;
u16 fin:1;
u16 pay_len:7;
u16 masked:1;
#else
#error __BYTE_ORDER__ not supported
#endif
};
static const char *websocket_opcodes[] = {
"Continuation Frame",
"Text Frame",
"Binary Frame",
"RESERVED",
"RESERVED",
"RESERVED",
"RESERVED",
"RESERVED",
"Connection Close",
"Ping",
"Pong",
"RESERVED",
"RESERVED",
"RESERVED",
"RESERVED",
"RESERVED" };
#endif /* _WEBSOCKET_H_ */