include/punk/punk_ws.h

filter: Statementstmt 100% 96 / 96 Branchbran 98.1% 104 / 106 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 99.0% 200 / 202 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_cache.h ↑ 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 4364051 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 2114202 static void pw_utf8_reset(pw_utf8 *u) { u->need = 0; u->cp = 0; u->min = 0; }
75  
76 2102138 static int pw_utf8_ok(pw_utf8 *u) { return u->need == 0; }
77  
78 /* 1 = still valid, 0 = malformed */
79 2102152 static int pw_utf8_step(pw_utf8 *u, const char *p, size_t n) {
80 size_t i;
81 130202340 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 128100197 2102143 130202340
82 128100197 unsigned char c = (unsigned char)p[i];
83 128100197 ▶ if (u->need == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (u->need == 0) { 128100180 17 128100197
84 128100180 ▶ if (c < 0x80) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) continue; 128100165 15 128100180
85 15 ▶ 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; } 4 11 15
86 11 ▶ 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; } 4 7 11
87 7 ▶ 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 5 7
88 5 else return 0; /* continuation or 0xF8+ */
89 }
90 else {
91 17 ▶ if ((c & 0xC0) != 0x80) return 0; /* expected a continuation */
Branch: 1 branch
BranchTrueFalseTotal
if ((c & 0xC0) != 0x80) return 0; /* expected a continuation */ 1 16 17
92 16 u->cp = (u->cp << 6) | (c & 0x3F);
93 16 ▶ if (--u->need == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--u->need == 0) { 8 8 16
94 8 ▶ 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 7 8
95 7 ▶ 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 4 7
if (u->cp >= 0xD800 && u->cp <= 0xDFFF) return 0; /* surrogate */ 1 2 3
96 6 ▶ if (u->cp > 0x10FFFF) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (u->cp > 0x10FFFF) return 0; 1 5 6
97 }
98 }
99 }
100 2102143 return 1;
101 }
102  
103 /* One-shot validation (close reasons, single-frame text). */
104 13 static int pw_utf8_valid(const char *p, size_t n) {
105 pw_utf8 u;
106 13 pw_utf8_reset(&u);
107 13 ▶ 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); 7 6 13
return pw_utf8_step(&u, p, n) && pw_utf8_ok(&u); 6 1 7
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 2247926 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 2247926 size_t need = 2, i;
118 uint64_t plen;
119  
120 2247926 ▶ if (len < 2) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) return PW_NEED_MORE; 135 2247791 2247926
121 2247791 b0 = (unsigned char)buf[0];
122 2247791 b1 = (unsigned char)buf[1];
123  
124 2247791 f->fin = (b0 & 0x80) ? 1 : 0;
125 2247791 f->rsv = (b0 >> 4) & 0x07;
126 2247791 f->opcode = b0 & 0x0F;
127 2247791 f->masked = (b1 & 0x80) ? 1 : 0;
128 2247791 plen = b1 & 0x7F;
129  
130 2247791 ▶ if (f->rsv) return PW_E_PROTO; /* no extensions negotiated */
Branch: 1 branch
BranchTrueFalseTotal
if (f->rsv) return PW_E_PROTO; /* no extensions negotiated */ 2 2247789 2247791
131 2247789 ▶ if (server && !f->masked) return PW_E_PROTO; /* fix 2 */
Branch: 2 branches
BranchTrueFalseTotal
if (server && !f->masked) return PW_E_PROTO; /* fix 2 */ 2247788 1 2247789
if (server && !f->masked) return PW_E_PROTO; /* fix 2 */ 3 2247785 2247788
132  
133 /* fix 3: control frames must be FIN and short, and must not fragment */
134 2247786 ▶ if (pw_is_control(f->opcode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pw_is_control(f->opcode)) { 2148 2245638 2247786
135 2148 ▶ 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; 2147 1 2148
if (!f->fin || plen > PW_MAX_CONTROL) return PW_E_PROTO; 1 2146 2147
136 2146 ▶ 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 2133 13 2146
if (f->opcode != PW_OP_CLOSE && f->opcode != PW_OP_PING 2 2131 2133
137 2 ▶ && f->opcode != PW_OP_PONG)
Branch: 1 branch
BranchTrueFalseTotal
&& f->opcode != PW_OP_PONG) 1 1 2
138 1 return PW_E_PROTO; /* reserved control opcode */
139 }
140 2245638 ▶ 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 2245633 5 2245638
else if (f->opcode != PW_OP_CONT && f->opcode != PW_OP_TEXT 30131 2215502 2245633
141 30131 ▶ && f->opcode != PW_OP_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
&& f->opcode != PW_OP_BINARY) { 1 30130 30131
142 1 return PW_E_PROTO; /* reserved data opcode */
143 }
144  
145 2247782 ▶ if (plen == 126) {
Branch: 1 branch
BranchTrueFalseTotal
if (plen == 126) { 124750 2123032 2247782
146 124750 ▶ if (len < 4) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 4) return PW_NEED_MORE; 2 124748 124750
147 124748 plen = ((uint64_t)(unsigned char)buf[2] << 8)
148 124748 | (uint64_t)(unsigned char)buf[3];
149 /* the minimal encoding and no other (RFC 6455 5.2) */
150 124748 ▶ if (plen < 126) return PW_E_PROTO;
Branch: 1 branch
BranchTrueFalseTotal
if (plen < 126) return PW_E_PROTO; 2 124746 124748
151 124746 need = 4;
152 }
153 2123032 ▶ else if (plen == 127) {
Branch: 1 branch
BranchTrueFalseTotal
else if (plen == 127) { 20171 2102861 2123032
154 /*
155 # uncoverable branch true
156 # uncoverable condition left count:1 class:ignore_covered_err */
157 20171 ▶ if (len < 10) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 10) return PW_NEED_MORE; 1 20170 20171
158 20170 plen = 0;
159 181530 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 161360 20170 181530
160 161360 plen = (plen << 8) | (uint64_t)(unsigned char)buf[2 + i];
161 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
162 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
163 20167 need = 10;
164 }
165  
166 /* fix 4: refuse from the header, before buffering anything */
167 2247774 ▶ 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; 2142756 105018 2247774
if (max_msg && plen > (uint64_t)max_msg) return PW_E_TOO_BIG; 3 2142753 2142756
168  
169 2247771 ▶ if (f->masked) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->masked) { 2247770 1 2247771
170 2247770 ▶ if (len < need + 4) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < need + 4) return PW_NEED_MORE; 534 2247236 2247770
171 11236180 ▶ 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]; 8988944 2247236 11236180
172 2247236 need += 4;
173 }
174 2247237 ▶ if (len < need + plen) return PW_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (len < need + plen) return PW_NEED_MORE; 25958 2221279 2247237
175  
176 2221279 f->payload_len = plen;
177 2221279 f->header_size = need;
178 2221279 f->payload = buf + need;
179 2221279 return (int)(need + plen);
180 }
181  
182 /* Unmask in place. The caller owns the bytes (they live in its read
183 * buffer), so this is the one mutation the decoder makes. */
184 2221278 static void pw_unmask(pw_frame *f, char *payload) {
185 uint64_t i;
186 /*
187 # uncoverable branch true
188 # uncoverable condition left count:1 class:ignore_covered_err */
189 2221278 ▶ if (!f->masked) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!f->masked) return; 0 2221278 2221278
190 478625032 ▶ for (i = 0; i < f->payload_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < f->payload_len; i++) 476403754 2221278 478625032
191 476403754 payload[i] = (char)((unsigned char)payload[i] ^ f->mask_key[i & 3]);
192 }
193  
194 /* ---- encode --------------------------------------------------------------
195 * Server frames are never masked. Returns the header size (2..10). */
196 102225 static size_t pw_encode_header(char *hdr, unsigned char opcode, int fin,
197 size_t len) {
198 102225 size_t n = 0;
199 102225 ▶ hdr[n++] = (char)((fin ? 0x80 : 0x00) | (opcode & 0x0F));
Branch: 1 branch
BranchTrueFalseTotal
hdr[n++] = (char)((fin ? 0x80 : 0x00) | (opcode & 0x0F)); 102224 1 102225
200 102225 ▶ if (len < 126) {
Branch: 1 branch
BranchTrueFalseTotal
if (len < 126) { 2179 100046 102225
201 2179 hdr[n++] = (char)len;
202 }
203 100046 ▶ else if (len <= 0xFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len <= 0xFFFF) { 100004 42 100046
204 100004 hdr[n++] = 126;
205 100004 hdr[n++] = (char)((len >> 8) & 0xFF);
206 100004 hdr[n++] = (char)(len & 0xFF);
207 }
208 else {
209 int i;
210 42 hdr[n++] = 127;
211 378 ▶ for (i = 7; i >= 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 7; i >= 0; i--) 336 42 378
212 336 hdr[n++] = (char)(((uint64_t)len >> (i * 8)) & 0xFF);
213 }
214 102225 return n;
215 }
216  
217 /* The close payload: a 2-byte big-endian code, then the (clamped) reason.
218 * Returns the payload length. `out` needs 2 + PW_MAX_REASON bytes. */
219 33 static size_t pw_close_payload(char *out, uint16_t code,
220 const char *reason, size_t rlen) {
221 33 ▶ if (rlen > PW_MAX_REASON) rlen = PW_MAX_REASON;
Branch: 1 branch
BranchTrueFalseTotal
if (rlen > PW_MAX_REASON) rlen = PW_MAX_REASON; 1 32 33
222 33 out[0] = (char)((code >> 8) & 0xFF);
223 33 out[1] = (char)(code & 0xFF);
224 33 ▶ if (rlen && reason) memcpy(out + 2, reason, rlen); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (rlen && reason) memcpy(out + 2, reason, rlen); /* # uncoverable condition right count:2 class:ignore_covered_err */ 23 10 33
if (rlen && reason) memcpy(out + 2, reason, rlen); /* # uncoverable condition right count:2 class:ignore_covered_err */ 23 0 23
225 33 return 2 + rlen;
226 }
227  
228 /* A close code the peer is allowed to send (RFC 6455 7.4). */
229 37 static int pw_close_code_ok(uint16_t code) {
230 37 ▶ if (code >= 3000 && code <= 4999) return 1; /* registered/private */
Branch: 2 branches
BranchTrueFalseTotal
if (code >= 3000 && code <= 4999) return 1; /* registered/private */ 6 31 37
if (code >= 3000 && code <= 4999) return 1; /* registered/private */ 5 1 6
231 32 ▶ switch (code) {
Branch: 1 branch
BranchTrueFalseTotal
switch (code) { 24 8 32
232 24 case PW_CLOSE_NORMAL: case PW_CLOSE_GOING_AWAY:
233 case PW_CLOSE_PROTOCOL_ERROR: case PW_CLOSE_UNSUPPORTED_DATA:
234 case PW_CLOSE_INVALID_PAYLOAD: case PW_CLOSE_POLICY_VIOLATION:
235 case PW_CLOSE_MESSAGE_TOO_BIG: case PW_CLOSE_MANDATORY_EXT:
236 case PW_CLOSE_INTERNAL_ERROR:
237 24 return 1;
238 8 default:
239 8 return 0; /* includes 1005/1006, which are never sent */
240 }
241 }
242  
243 #endif /* PUNK_WS_H */
« include/punk/punk_cache.h ↑ Summary bin/punk »