include/punk/punk_ws.h

filter: Statementstmt 100% 96 / 96 Branchbran 96.2% 102 / 106 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 98.0% 198 / 202 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Punk/Plugin.pm ↑ Summary bin/punk »
1 /* punk_ws.h - the RFC 6455 frame codec.
2 *
3 * Transcribed from the C that Hypersonic's
4 * Hypersonic::Protocol::WebSocket::Frame generates (same author, relicensed
5 * Artistic 2.0 with the rest of Punk), with the strictness that a
6 * server-side codec facing the public internet needs added from the start:
7 *
8 * 1. text payloads and close reasons are UTF-8 validated, incrementally
9 * so a multibyte character split across fragments still validates
10 * (violations close 1007);
11 * 2. client frames must be masked (1002);
12 * 3. control frames must be FIN and at most 125 bytes (1002);
13 * 4. fragment reassembly is bounded by max_message_size, rejected from
14 * the frame header before a single byte is buffered (1009);
15 * 5. writes are queued and drained, never assumed complete - a short
16 * write arms a write watcher rather than corrupting the stream.
17 *
18 * This header is the frame layer only: it owns no fds and no event loop.
19 * punk_wsconn.h drives it.
20 */
21  
22 #ifndef PUNK_WS_H
23 #define PUNK_WS_H
24  
25 /* ---- opcodes and close codes --------------------------------------------- */
26  
27 #define PW_OP_CONT 0x0
28 #define PW_OP_TEXT 0x1
29 #define PW_OP_BINARY 0x2
30 #define PW_OP_CLOSE 0x8
31 #define PW_OP_PING 0x9
32 #define PW_OP_PONG 0xA
33  
34 #define PW_CLOSE_NORMAL 1000
35 #define PW_CLOSE_GOING_AWAY 1001
36 #define PW_CLOSE_PROTOCOL_ERROR 1002
37 #define PW_CLOSE_UNSUPPORTED_DATA 1003
38 #define PW_CLOSE_NO_STATUS 1005
39 #define PW_CLOSE_ABNORMAL 1006
40 #define PW_CLOSE_INVALID_PAYLOAD 1007
41 #define PW_CLOSE_POLICY_VIOLATION 1008
42 #define PW_CLOSE_MESSAGE_TOO_BIG 1009
43 #define PW_CLOSE_MANDATORY_EXT 1010
44 #define PW_CLOSE_INTERNAL_ERROR 1011
45  
46 #define PW_MAX_CONTROL 125 /* RFC 6455 5.5: control payload limit */
47 #define PW_MAX_REASON 123 /* a close reason after the 2-byte code */
48  
49 /* pw_decode_frame results: > 0 is the number of bytes consumed, 0 means
50 * "need more data", and these negatives are protocol failures carrying the
51 * close code they must be answered with. */
52 #define PW_NEED_MORE 0
53 #define PW_E_PROTO -1 /* -> 1002 */
54 #define PW_E_TOO_BIG -2 /* -> 1009 */
55 #define PW_E_UTF8 -3 /* -> 1007 */
56  
57 typedef struct {
58 unsigned char fin, rsv, opcode, masked;
59 unsigned char mask_key[4];
60 uint64_t payload_len;
61 const char *payload; /* borrowed, into the caller's buffer */
62 size_t header_size;
63 } pw_frame;
64  
65 4364053 static int pw_is_control(unsigned char op) { return (op & 0x08) != 0; }
66  
67 /* ---- incremental UTF-8 validation ----------------------------------------
68 * state 0 means "at a character boundary" - the only state a message may
69 * end in. Anything else is a partially seen character, carried across
70 * fragments. Rejects overlongs, surrogates and > U+10FFFF. */
71  
72 typedef struct { int need; uint32_t cp; int min; } pw_utf8;
73  
74 2114152 static void pw_utf8_reset(pw_utf8 *u) { u->need = 0; u->cp = 0; u->min = 0; }
75  
76 2102124 static int pw_utf8_ok(pw_utf8 *u) { return u->need == 0; }
77  
78 /* 1 = still valid, 0 = malformed */
79 2102131 static int pw_utf8_step(pw_utf8 *u, const char *p, size_t n) {
80 size_t i;
81 130202250 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 128100125 2102125 130202250
82 128100125 unsigned char c = (unsigned char)p[i];
83 128100125 ▶ if (u->need == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (u->need == 0) { 128100113 12 128100125
84 128100113 ▶ if (c < 0x80) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) continue; 128100103 10 128100113
85 10 ▶ else if ((c & 0xE0) == 0xC0) { u->need = 1; u->cp = c & 0x1F; u->min = 0x80; }
Branch: 1 branch
BranchTrueFalseTotal
else if ((c & 0xE0) == 0xC0) { u->need = 1; u->cp = c & 0x1F; u->min = 0x80; } 3 7 10
86 7 ▶ else if ((c & 0xF0) == 0xE0) { u->need = 2; u->cp = c & 0x0F; u->min = 0x800; }
Branch: 1 branch
BranchTrueFalseTotal
else if ((c & 0xF0) == 0xE0) { u->need = 2; u->cp = c & 0x0F; u->min = 0x800; } 2 5 7
87 5 ▶ else if ((c & 0xF8) == 0xF0) { u->need = 3; u->cp = c & 0x07; u->min = 0x10000; }
Branch: 1 branch
BranchTrueFalseTotal
else if ((c & 0xF8) == 0xF0) { u->need = 3; u->cp = c & 0x07; u->min = 0x10000; } 2 3 5
88 3 else return 0; /* continuation or 0xF8+ */
89 }
90 else {
91 12 ▶ if ((c & 0xC0) != 0x80) return 0; /* expected a continuation */
Branch: 1 branch
BranchTrueFalseTotal
if ((c & 0xC0) != 0x80) return 0; /* expected a continuation */ 0 12 12
92 12 u->cp = (u->cp << 6) | (c & 0x3F);
93 12 ▶ if (--u->need == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--u->need == 0) { 6 6 12
94 6 ▶ if (u->cp < (uint32_t)u->min) return 0; /* overlong */
Branch: 1 branch
BranchTrueFalseTotal
if (u->cp < (uint32_t)u->min) return 0; /* overlong */ 1 5 6
95 5 ▶ if (u->cp >= 0xD800 && u->cp <= 0xDFFF) return 0; /* surrogate */
Branch: 2 branches
BranchTrueFalseTotal
if (u->cp >= 0xD800 && u->cp <= 0xDFFF) return 0; /* surrogate */ 3 2 5
if (u->cp >= 0xD800 && u->cp <= 0xDFFF) return 0; /* surrogate */ 1 2 3
96 4 ▶ if (u->cp > 0x10FFFF) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (u->cp > 0x10FFFF) return 0; 1 3 4
97 }
98 }
99 }
100 2102125 return 1;
101 }
102  
103 /* One-shot validation (close reasons, single-frame text). */
104 11 static int pw_utf8_valid(const char *p, size_t n) {
105 pw_utf8 u;
106 11 pw_utf8_reset(&u);
107 11 ▶ return pw_utf8_step(&u, p, n) && pw_utf8_ok(&u);
Branch: 2 branches
BranchTrueFalseTotal
return pw_utf8_step(&u, p, n) && pw_utf8_ok(&u); 6 5 11
return pw_utf8_step(&u, p, n) && pw_utf8_ok(&u); 5 1 6
108 }
109  
110 /* ---- decode --------------------------------------------------------------
111 * `server` means "these frames come from a client", which is what makes
112 * masking mandatory. max_msg bounds a single frame's payload, checked from
113 * the header so an oversized frame is refused before it is buffered. */
114 2247962 static int pw_decode_frame(const char *buf, size_t len, pw_frame *f,
115 size_t max_msg, int server) {
116 unsigned char b0, b1;
117 2247962 size_t need = 2, i;
118 uint64_t plen;
119  
120 2247962 ▶ if (len < 2) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) return PW_NEED_MORE; 135 2247827 2247962
121 2247827 b0 = (unsigned char)buf[0];
122 2247827 b1 = (unsigned char)buf[1];
123  
124 2247827 f->fin = (b0 & 0x80) ? 1 : 0;
125 2247827 f->rsv = (b0 >> 4) & 0x07;
126 2247827 f->opcode = b0 & 0x0F;
127 2247827 f->masked = (b1 & 0x80) ? 1 : 0;
128 2247827 plen = b1 & 0x7F;
129  
130 2247827 ▶ if (f->rsv) return PW_E_PROTO; /* no extensions negotiated */
Branch: 1 branch
BranchTrueFalseTotal
if (f->rsv) return PW_E_PROTO; /* no extensions negotiated */ 1 2247826 2247827
131 2247826 ▶ if (server && !f->masked) return PW_E_PROTO; /* fix 2 */
Branch: 2 branches
BranchTrueFalseTotal
if (server && !f->masked) return PW_E_PROTO; /* fix 2 */ 2247825 1 2247826
if (server && !f->masked) return PW_E_PROTO; /* fix 2 */ 2 2247823 2247825
132  
133 /* fix 3: control frames must be FIN and short, and must not fragment */
134 2247824 ▶ if (pw_is_control(f->opcode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pw_is_control(f->opcode)) { 2135 2245689 2247824
135 2135 ▶ if (!f->fin || plen > PW_MAX_CONTROL) return PW_E_PROTO;
Branch: 2 branches
BranchTrueFalseTotal
if (!f->fin || plen > PW_MAX_CONTROL) return PW_E_PROTO; 2134 1 2135
if (!f->fin || plen > PW_MAX_CONTROL) return PW_E_PROTO; 1 2133 2134
136 2133 ▶ if (f->opcode != PW_OP_CLOSE && f->opcode != PW_OP_PING
Branch: 2 branches
BranchTrueFalseTotal
if (f->opcode != PW_OP_CLOSE && f->opcode != PW_OP_PING 2131 2 2133
if (f->opcode != PW_OP_CLOSE && f->opcode != PW_OP_PING 1 2130 2131
137 1 ▶ && f->opcode != PW_OP_PONG)
Branch: 1 branch
BranchTrueFalseTotal
&& f->opcode != PW_OP_PONG) 1 0 1
138 1 return PW_E_PROTO; /* reserved control opcode */
139 }
140 2245689 ▶ else if (f->opcode != PW_OP_CONT && f->opcode != PW_OP_TEXT
Branch: 2 branches
BranchTrueFalseTotal
else if (f->opcode != PW_OP_CONT && f->opcode != PW_OP_TEXT 2245688 1 2245689
else if (f->opcode != PW_OP_CONT && f->opcode != PW_OP_TEXT 30205 2215483 2245688
141 30205 ▶ && f->opcode != PW_OP_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
&& f->opcode != PW_OP_BINARY) { 1 30204 30205
142 1 return PW_E_PROTO; /* reserved data opcode */
143 }
144  
145 2247820 ▶ if (plen == 126) {
Branch: 1 branch
BranchTrueFalseTotal
if (plen == 126) { 124825 2122995 2247820
146 124825 ▶ if (len < 4) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 4) return PW_NEED_MORE; 2 124823 124825
147 124823 plen = ((uint64_t)(unsigned char)buf[2] << 8)
148 124823 | (uint64_t)(unsigned char)buf[3];
149 /* the minimal encoding and no other (RFC 6455 5.2) */
150 124823 ▶ if (plen < 126) return PW_E_PROTO;
Branch: 1 branch
BranchTrueFalseTotal
if (plen < 126) return PW_E_PROTO; 2 124821 124823
151 124821 need = 4;
152 }
153 2122995 ▶ else if (plen == 127) {
Branch: 1 branch
BranchTrueFalseTotal
else if (plen == 127) { 20171 2102824 2122995
154 20171 ▶ if (len < 10) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 10) return PW_NEED_MORE; 1 20170 20171
155 20170 plen = 0;
156 181530 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 161360 20170 181530
157 161360 plen = (plen << 8) | (uint64_t)(unsigned char)buf[2 + i];
158 20170 ▶ if (plen & ((uint64_t)1 << 63)) return PW_E_PROTO; /* MSB set */
Branch: 1 branch
BranchTrueFalseTotal
if (plen & ((uint64_t)1 << 63)) return PW_E_PROTO; /* MSB set */ 1 20169 20170
159 20169 ▶ if (plen <= 0xFFFF) return PW_E_PROTO; /* not minimal */
Branch: 1 branch
BranchTrueFalseTotal
if (plen <= 0xFFFF) return PW_E_PROTO; /* not minimal */ 2 20167 20169
160 20167 need = 10;
161 }
162  
163 /* fix 4: refuse from the header, before buffering anything */
164 2247812 ▶ if (max_msg && plen > (uint64_t)max_msg) return PW_E_TOO_BIG;
Branch: 2 branches
BranchTrueFalseTotal
if (max_msg && plen > (uint64_t)max_msg) return PW_E_TOO_BIG; 2142794 105018 2247812
if (max_msg && plen > (uint64_t)max_msg) return PW_E_TOO_BIG; 2 2142792 2142794
165  
166 2247810 ▶ if (f->masked) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->masked) { 2247809 1 2247810
167 2247809 ▶ if (len < need + 4) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < need + 4) return PW_NEED_MORE; 533 2247276 2247809
168 11236380 ▶ for (i = 0; i < 4; i++) f->mask_key[i] = (unsigned char)buf[need + i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) f->mask_key[i] = (unsigned char)buf[need + i]; 8989104 2247276 11236380
169 2247276 need += 4;
170 }
171 2247277 ▶ if (len < need + plen) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < need + plen) return PW_NEED_MORE; 26034 2221243 2247277
172  
173 2221243 f->payload_len = plen;
174 2221243 f->header_size = need;
175 2221243 f->payload = buf + need;
176 2221243 return (int)(need + plen);
177 }
178  
179 /* Unmask in place. The caller owns the bytes (they live in its read
180 * buffer), so this is the one mutation the decoder makes. */
181 2221242 static void pw_unmask(pw_frame *f, char *payload) {
182 uint64_t i;
183 2221242 ▶ if (!f->masked) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!f->masked) return; 0 2221242 2221242
184 478624872 ▶ for (i = 0; i < f->payload_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < f->payload_len; i++) 476403630 2221242 478624872
185 476403630 payload[i] = (char)((unsigned char)payload[i] ^ f->mask_key[i & 3]);
186 }
187  
188 /* ---- encode --------------------------------------------------------------
189 * Server frames are never masked. Returns the header size (2..10). */
190 102146 static size_t pw_encode_header(char *hdr, unsigned char opcode, int fin,
191 size_t len) {
192 102146 size_t n = 0;
193 102146 ▶ hdr[n++] = (char)((fin ? 0x80 : 0x00) | (opcode & 0x0F));
Branch: 1 branch
BranchTrueFalseTotal
hdr[n++] = (char)((fin ? 0x80 : 0x00) | (opcode & 0x0F)); 102145 1 102146
194 102146 ▶ if (len < 126) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < 126) { 2140 100006 102146
195 2140 hdr[n++] = (char)len;
196 }
197 100006 ▶ else if (len <= 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len <= 0xFFFF) { 100004 2 100006
198 100004 hdr[n++] = 126;
199 100004 hdr[n++] = (char)((len >> 8) & 0xFF);
200 100004 hdr[n++] = (char)(len & 0xFF);
201 }
202 else {
203 int i;
204 2 hdr[n++] = 127;
205 18 ▶ for (i = 7; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 7; i >= 0; i--) 16 2 18
206 16 hdr[n++] = (char)(((uint64_t)len >> (i * 8)) & 0xFF);
207 }
208 102146 return n;
209 }
210  
211 /* The close payload: a 2-byte big-endian code, then the (clamped) reason.
212 * Returns the payload length. `out` needs 2 + PW_MAX_REASON bytes. */
213 9 static size_t pw_close_payload(char *out, uint16_t code,
214 const char *reason, size_t rlen) {
215 9 ▶ if (rlen > PW_MAX_REASON) rlen = PW_MAX_REASON;
Branch: 1 branch
BranchTrueFalseTotal
if (rlen > PW_MAX_REASON) rlen = PW_MAX_REASON; 1 8 9
216 9 out[0] = (char)((code >> 8) & 0xFF);
217 9 out[1] = (char)(code & 0xFF);
218 9 ▶ if (rlen && reason) memcpy(out + 2, reason, rlen);
Branch: 2 branches
BranchTrueFalseTotal
if (rlen && reason) memcpy(out + 2, reason, rlen); 6 3 9
if (rlen && reason) memcpy(out + 2, reason, rlen); 6 0 6
219 9 return 2 + rlen;
220 }
221  
222 /* A close code the peer is allowed to send (RFC 6455 7.4). */
223 22 static int pw_close_code_ok(uint16_t code) {
224 22 ▶ if (code >= 3000 && code <= 4999) return 1; /* registered/private */
Branch: 2 branches
BranchTrueFalseTotal
if (code >= 3000 && code <= 4999) return 1; /* registered/private */ 5 17 22
if (code >= 3000 && code <= 4999) return 1; /* registered/private */ 4 1 5
225 18 ▶ switch (code) {
Branch: 1 branch
BranchTrueFalseTotal
switch (code) { 11 7 18
226 11 case PW_CLOSE_NORMAL: case PW_CLOSE_GOING_AWAY:
227 case PW_CLOSE_PROTOCOL_ERROR: case PW_CLOSE_UNSUPPORTED_DATA:
228 case PW_CLOSE_INVALID_PAYLOAD: case PW_CLOSE_POLICY_VIOLATION:
229 case PW_CLOSE_MESSAGE_TOO_BIG: case PW_CLOSE_MANDATORY_EXT:
230 case PW_CLOSE_INTERNAL_ERROR:
231 11 return 1;
232 7 default:
233 7 return 0; /* includes 1005/1006, which are never sent */
234 }
235 }
236  
237 #endif /* PUNK_WS_H */
« blib/lib/Punk/Plugin.pm ↑ Summary bin/punk »