feersum_tls.c.inc

filter: Statementstmt 59.4% 515 / 866 Branchbran 46.4% 305 / 656 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 53.8% 820 / 1522 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« picohttpparser-git/picohttpparser.c ↑ Summary feersum_conn.xs »
1 /* TLS 1.3 via picotls: handshake, record layer and the read/write callbacks
2 * for TLS connections; plain connections never enter this file. */
3  
4 #ifdef FEERSUM_HAS_TLS
5  
6 static ptls_iovec_t tls_alpn_protos[] = {
7 #ifdef FEERSUM_HAS_H2
8 { (uint8_t *)ALPN_H2 + 1, 2 },
9 #endif
10 { (uint8_t *)ALPN_HTTP11 + 1, 8 },
11 };
12  
13 static ptls_iovec_t tls_alpn_h1_only[] = {
14 { (uint8_t *)ALPN_HTTP11 + 1, 8 },
15 };
16  
17 static int
18 153 negotiate_alpn(ptls_t *tls, const ptls_iovec_t *protos, size_t num_protos,
19 ptls_on_client_hello_parameters_t *params)
20 {
21 size_t i, j;
22 235 ▶ for (i = 0; i < num_protos; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_protos; i++) { 153 82 235
23 153 ▶ for (j = 0; j < params->negotiated_protocols.count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < params->negotiated_protocols.count; j++) { 71 82 153
24 71 ▶ if (params->negotiated_protocols.list[j].len == protos[i].len &&
Branch: 1 branch
BranchTrueFalseTotal
if (params->negotiated_protocols.list[j].len == protos[i].len && 71 0 71
25 71 memcmp(params->negotiated_protocols.list[j].base, protos[i].base,
26 71 ▶ protos[i].len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
protos[i].len) == 0) { 71 0 71
27 71 ptls_set_negotiated_protocol(tls,
28 71 (const char *)protos[i].base, protos[i].len);
29 71 return 0;
30 }
31 }
32 }
33 /* RFC 7301 3.2: ALPN offered with no overlap must abort, not fall back */
34 82 ▶ if (params->negotiated_protocols.count > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (params->negotiated_protocols.count > 0) 0 82 82
35 0 return PTLS_ALERT_NO_APPLICATION_PROTOCOL;
36  
37 82 return 0;
38 }
39  
40 static void
41 153 sni_lookup(ptls_t *tls, ptls_on_client_hello_parameters_t *params)
42 {
43 153 struct feer_conn *c = *(struct feer_conn **)ptls_get_data_ptr(tls);
44 153 ▶ struct feer_listen *lsnr = c ? c->listener : NULL;
Branch: 1 branch
BranchTrueFalseTotal
struct feer_listen *lsnr = c ? c->listener : NULL; 153 0 153
45 153 ▶ if (!lsnr || lsnr->n_sni_entries == 0 || !params->server_name.base)
Branch: 3 branches
BranchTrueFalseTotal
if (!lsnr || lsnr->n_sni_entries == 0 || !params->server_name.base) 153 0 153
if (!lsnr || lsnr->n_sni_entries == 0 || !params->server_name.base) 1 152 153
if (!lsnr || lsnr->n_sni_entries == 0 || !params->server_name.base) 0 1 1
46 152 return;
47  
48 /* hostname is pre-lowered at registration; ASCII-only per RFC 6066 */
49 1 size_t name_len = params->server_name.len;
50 /* a trailing dot names the same host */
51 1 ▶ if (name_len > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if (name_len > 0 && 1 0 1
52 1 ▶ ((const char *)params->server_name.base)[name_len - 1] == '.')
Branch: 1 branch
BranchTrueFalseTotal
((const char *)params->server_name.base)[name_len - 1] == '.') 0 1 1
53 0 name_len--;
54 1 ▶ for (int i = 0; i < lsnr->n_sni_entries; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < lsnr->n_sni_entries; i++) { 1 0 1
55 1 struct feer_sni_entry *e = &lsnr->sni_entries[i];
56 2 if (strlen(e->hostname) == name_len &&
57 1 str_case_eq_fixed(e->hostname,
58 1 (const char *)params->server_name.base, name_len)) {
59 /* Pin the chosen context (a set_tls rotation may free it) and
60 * release the old one last: ptls_set_context touches the context
61 * it replaces. */
62 1 struct feer_tls_ctx_ref *old = c->tls_ctx_ref;
63 1 e->ctx_ref->refcount++;
64 1 c->tls_ctx_ref = e->ctx_ref;
65 1 ptls_set_context(tls, e->ctx_ref->ctx);
66 1 feer_tls_ctx_ref_dec(old);
67 1 return;
68 }
69 }
70 }
71  
72 static int
73 0 on_client_hello_cb(ptls_on_client_hello_t *self, ptls_t *tls,
74 ptls_on_client_hello_parameters_t *params)
75 {
76 PERL_UNUSED_VAR(self);
77 0 sni_lookup(tls, params);
78 0 return negotiate_alpn(tls, tls_alpn_protos,
79 sizeof(tls_alpn_protos) / sizeof(tls_alpn_protos[0]), params);
80 }
81  
82 static ptls_on_client_hello_t on_client_hello = { on_client_hello_cb };
83  
84 static int
85 153 on_client_hello_no_h2_cb(ptls_on_client_hello_t *self, ptls_t *tls,
86 ptls_on_client_hello_parameters_t *params)
87 {
88 PERL_UNUSED_VAR(self);
89 153 sni_lookup(tls, params);
90 153 return negotiate_alpn(tls, tls_alpn_h1_only,
91 sizeof(tls_alpn_h1_only) / sizeof(tls_alpn_h1_only[0]), params);
92 }
93  
94 static ptls_on_client_hello_t on_client_hello_no_h2 = { on_client_hello_no_h2_cb };
95  
96 static ptls_key_exchange_algorithm_t *feer_tls_key_exchanges[] = {
97 #if PTLS_OPENSSL_HAVE_X25519
98 &ptls_openssl_x25519,
99 #endif
100 &ptls_openssl_secp256r1,
101 NULL
102 };
103  
104 static ptls_cipher_suite_t *feer_tls_cipher_suites[] = {
105 &ptls_openssl_aes256gcmsha384,
106 &ptls_openssl_aes128gcmsha256,
107 #if PTLS_OPENSSL_HAVE_CHACHA20_POLY1305
108 &ptls_openssl_chacha20poly1305sha256,
109 #endif
110 NULL
111 };
112  
113 static ptls_context_t *
114 259 feer_tls_create_context(pTHX_ const char *cert_file, const char *key_file, int h2)
115 {
116 ptls_context_t *ctx;
117 FILE *fp;
118 int ret;
119  
120 259 Newxz(ctx, 1, ptls_context_t);
121  
122 259 ctx->random_bytes = ptls_openssl_random_bytes;
123 259 ctx->get_time = &ptls_get_time;
124 259 ctx->key_exchanges = feer_tls_key_exchanges;
125 259 ctx->cipher_suites = feer_tls_cipher_suites;
126 259 ctx->max_buffer_size = FEER_TLS_MAX_HANDSHAKE_BUF;
127  
128 259 ret = ptls_load_certificates(ctx, cert_file);
129 259 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 3 256 259
130 3 warn("Feersum TLS: failed to load certificate from '%s' (error %d)\n",
131 cert_file, ret);
132 3 feer_tls_free_context(ctx);
133 3 return NULL;
134 }
135  
136 256 fp = fopen(key_file, "r");
137 256 ▶ if (!fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { 2 254 256
138 2 warn("Feersum TLS: failed to open key file '%s': %s\n",
139 key_file, strerror(errno));
140 2 goto cert_cleanup;
141 }
142  
143 /* "" rather than NULL: a NULL passphrase makes OpenSSL prompt on the tty */
144 254 EVP_PKEY *pkey = PEM_read_PrivateKey(fp, NULL, NULL, (void *)"");
145 254 fclose(fp);
146 254 ▶ if (!pkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pkey) { 0 254 254
147 0 warn("Feersum TLS: failed to read private key from '%s' "
148 "(encrypted keys are not supported)\n", key_file);
149 0 goto cert_cleanup;
150 }
151  
152 /* picotls's PEM reader loads a truncated certificate as garbage DER without
153 * complaint, and nothing else checks that the key matches the leaf */
154 254 ▶ if (ctx->certificates.count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->certificates.count > 0) { 254 0 254
155 254 const unsigned char *der = ctx->certificates.list[0].base;
156 254 X509 *leaf = d2i_X509(NULL, &der, (long)ctx->certificates.list[0].len);
157 254 ▶ if (!leaf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!leaf) { 1 253 254
158 1 warn("Feersum TLS: certificate in '%s' is not valid DER "
159 "(truncated or corrupt PEM?)\n", cert_file);
160 1 EVP_PKEY_free(pkey);
161 2 goto cert_cleanup;
162 }
163 253 ▶ if (X509_check_private_key(leaf, pkey) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (X509_check_private_key(leaf, pkey) != 1) { 1 252 253
164 1 X509_free(leaf);
165 1 EVP_PKEY_free(pkey);
166 1 warn("Feersum TLS: private key '%s' does not match certificate "
167 "'%s'\n", key_file, cert_file);
168 1 goto cert_cleanup;
169 }
170 252 X509_free(leaf);
171 }
172  
173 ptls_openssl_sign_certificate_t *sign_cert;
174 252 Newx(sign_cert, 1, ptls_openssl_sign_certificate_t);
175 252 ▶ if (ptls_openssl_init_sign_certificate(sign_cert, pkey) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ptls_openssl_init_sign_certificate(sign_cert, pkey) != 0) { 0 252 252
176 0 Safefree(sign_cert);
177 0 EVP_PKEY_free(pkey);
178 0 warn("Feersum TLS: incompatible private key type in '%s'\n", key_file);
179 0 goto cert_cleanup;
180 }
181 252 EVP_PKEY_free(pkey);
182 252 ctx->sign_certificate = &sign_cert->super;
183  
184 252 ▶ if (h2)
Branch: 1 branch
BranchTrueFalseTotal
if (h2) 0 252 252
185 0 ctx->on_client_hello = &on_client_hello;
186 else
187 252 ctx->on_client_hello = &on_client_hello_no_h2;
188  
189 trace("TLS context created: cert=%s key=%s h2=%d\n", cert_file, key_file, h2);
190 252 return ctx;
191  
192 4 cert_cleanup:
193 4 feer_tls_free_context(ctx);
194 4 return NULL;
195 }
196  
197 static void
198 257 feer_tls_free_context(ptls_context_t *ctx)
199 {
200 257 ▶ if (!ctx) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) return; 0 257 257
201 257 ▶ if (ctx->certificates.list) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->certificates.list) { 257 0 257
202 size_t i;
203 511 ▶ for (i = 0; i < ctx->certificates.count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->certificates.count; i++) 254 257 511
204 254 free(ctx->certificates.list[i].base);
205 257 free(ctx->certificates.list);
206 }
207 257 ▶ if (ctx->sign_certificate) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->sign_certificate) { 250 7 257
208 250 ptls_openssl_sign_certificate_t *sign_cert =
209 (ptls_openssl_sign_certificate_t *)ctx->sign_certificate;
210 250 ptls_openssl_dispose_sign_certificate(sign_cert);
211 250 Safefree(sign_cert);
212 }
213 257 Safefree(ctx);
214 }
215  
216 /* Live connections hold a ref, so set_tls rotation or listener slot reuse
217 * cannot free a context a ptls_t still points at. */
218 static struct feer_tls_ctx_ref *
219 252 feer_tls_ctx_ref_new(ptls_context_t *ctx)
220 {
221 struct feer_tls_ctx_ref *ref;
222 252 Newx(ref, 1, struct feer_tls_ctx_ref);
223 252 ref->ctx = ctx;
224 252 ref->refcount = 1;
225 252 return ref;
226 }
227  
228 static void
229 417 feer_tls_ctx_ref_dec(struct feer_tls_ctx_ref *ref)
230 {
231 417 ▶ if (--ref->refcount <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--ref->refcount <= 0) { 250 167 417
232 250 feer_tls_free_context(ref->ctx);
233 250 Safefree(ref);
234 }
235 417 }
236  
237 static void
238 725 feer_tls_cleanup_listener(struct feer_listen *lsnr)
239 {
240 725 ▶ if (lsnr->tls_ctx_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (lsnr->tls_ctx_ref) { 150 575 725
241 150 feer_tls_ctx_ref_dec(lsnr->tls_ctx_ref);
242 150 lsnr->tls_ctx_ref = NULL;
243 }
244 762 ▶ for (int i = 0; i < lsnr->n_sni_entries; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < lsnr->n_sni_entries; i++) { 37 725 762
245 37 Safefree(lsnr->sni_entries[i].hostname);
246 37 feer_tls_ctx_ref_dec(lsnr->sni_entries[i].ctx_ref);
247 }
248 725 lsnr->n_sni_entries = 0;
249 725 }
250  
251 static void
252 170 feer_tls_init_conn(struct feer_conn *c, struct feer_tls_ctx_ref *ref)
253 {
254 170 c->tls = ptls_new(ref->ctx, 1 /* is_server */);
255 170 ▶ if (unlikely(!c->tls)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->tls)) { 0 170 170
256 0 trouble("ptls_new failed for fd=%d\n", c->fd);
257 0 return;
258 }
259 /* read back by sni_lookup */
260 170 *ptls_get_data_ptr(c->tls) = c;
261 170 ref->refcount++;
262 170 c->tls_ctx_ref = ref;
263 170 ptls_buffer_init(&c->tls_wbuf, "", 0);
264 170 c->tls_tunnel_sv0 = -1;
265 170 c->tls_tunnel_sv1 = -1;
266 }
267  
268 static void
269 1149 feer_tls_rbuf_release(struct feer_conn *c)
270 {
271 1149 ▶ if (c->tls_rbuf_base)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_rbuf_base) 0 1149 1149
272 0 Safefree(c->tls_rbuf_base);
273 1149 c->tls_rbuf = c->tls_rbuf_base = NULL;
274 1149 c->tls_rbuf_len = 0;
275 1149 }
276  
277 /* before the borrowed read buffer goes out of scope */
278 static void
279 837 feer_tls_rbuf_own(struct feer_conn *c)
280 {
281 837 ▶ if (c->tls_rbuf_base) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_rbuf_base) return; 0 837 837
282 837 ▶ if (c->tls_rbuf_len == 0) { c->tls_rbuf = NULL; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_rbuf_len == 0) { c->tls_rbuf = NULL; return; } 837 0 837
283 uint8_t *p;
284 0 Newx(p, c->tls_rbuf_len, uint8_t);
285 0 memcpy(p, c->tls_rbuf, c->tls_rbuf_len);
286 0 c->tls_rbuf = c->tls_rbuf_base = p;
287 }
288  
289 static inline void
290 335 feer_tls_decbuf_done(ptls_buffer_t *b)
291 {
292 335 ▶ if (b->is_allocated)
Branch: 1 branch
BranchTrueFalseTotal
if (b->is_allocated) 0 335 335
293 0 ptls_buffer_dispose(b);
294 335 }
295  
296 static void
297 1149 feer_tls_free_conn(struct feer_conn *c)
298 {
299 1149 ▶ if (c->tls) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls) { 164 985 1149
300 164 ptls_free(c->tls);
301 164 c->tls = NULL;
302 }
303 1149 ▶ if (c->tls_ctx_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_ctx_ref) { 164 985 1149
304 164 feer_tls_ctx_ref_dec(c->tls_ctx_ref);
305 164 c->tls_ctx_ref = NULL;
306 }
307 1149 ptls_buffer_dispose(&c->tls_wbuf);
308 1149 feer_tls_rbuf_release(c);
309 1149 ▶ if (c->tls_tunnel) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel) { 9 1140 1149
310 9 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_read_w);
311 9 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_write_w);
312 9 c->tls_tunnel = 0;
313 9 ▶ if (c->tls_tunnel_sv0 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_sv0 >= 0) { 0 9 9
314 0 close(c->tls_tunnel_sv0);
315 0 c->tls_tunnel_sv0 = -1;
316 }
317 9 ▶ if (c->tls_tunnel_sv1 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_sv1 >= 0) { 0 9 9
318 0 close(c->tls_tunnel_sv1);
319 0 c->tls_tunnel_sv1 = -1;
320 }
321 9 ▶ if (c->tls_tunnel_wbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_wbuf) { 0 9 9
322 dTHX;
323 0 SvREFCNT_dec(c->tls_tunnel_wbuf);
324 0 c->tls_tunnel_wbuf = NULL;
325 0 c->tls_tunnel_wbuf_pos = 0;
326 }
327 }
328 1149 }
329  
330 /* Returns bytes written (0 if nothing pending), -1 on EAGAIN, -2 on error */
331 static int
332 676 feer_tls_flush_wbuf(struct feer_conn *c)
333 {
334 676 ▶ if (c->tls_wbuf.off == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off == 0) 0 676 676
335 0 return 0;
336  
337 676 size_t pending = c->tls_wbuf.off - c->tls_wbuf_sent;
338 676 ssize_t written = write(c->fd, c->tls_wbuf.base + c->tls_wbuf_sent, pending);
339 676 ▶ if (written < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (written < 0) { 2 674 676
340 2 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { 2 0 2
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { 2 0 2
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { 0 2 2
341 0 return -1;
342 }
343 trace("TLS flush write error fd=%d: %s\n", c->fd, strerror(errno));
344 2 return -2;
345 }
346  
347 #ifdef FEERSUM_HAS_H2
348 if (written > 0 && c->h2_session)
349 h2_refresh_stream_write_timers(c);
350 /* only a live timer: safe_close_conn flushes here after stopping watchers */
351 if (written > 0 && c->h2_session && ev_is_active(&c->read_ev_timer)) {
352 c->h2_stall_strikes = 0;
353 c->prev_read_outq = 0;
354 restart_read_timer(c);
355 }
356 #endif
357  
358 674 ▶ if ((size_t)written < pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)written < pending) { 9 665 674
359 9 c->tls_wbuf_sent += written;
360 /* compact only once the sent prefix outweighs the unsent tail */
361 9 size_t live = c->tls_wbuf.off - c->tls_wbuf_sent;
362 9 ▶ if (c->tls_wbuf_sent >= live) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf_sent >= live) { 5 4 9
363 5 memmove(c->tls_wbuf.base, c->tls_wbuf.base + c->tls_wbuf_sent, live);
364 5 c->tls_wbuf.off = live;
365 5 c->tls_wbuf_sent = 0;
366 }
367 9 return (int)written;
368 }
369  
370 665 c->tls_wbuf.off = 0;
371 665 c->tls_wbuf_sent = 0;
372 665 return (int)written;
373 }
374  
375 static void
376 1089 feer_tls_note_idle(struct feer_conn *c)
377 {
378 1089 ▶ if (!c->tls || c->tls_wbuf.capacity <= FEER_TLS_WBUF_KEEP) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!c->tls || c->tls_wbuf.capacity <= FEER_TLS_WBUF_KEEP) return; 18 1071 1089
if (!c->tls || c->tls_wbuf.capacity <= FEER_TLS_WBUF_KEEP) return; 18 0 18
379 0 c->tls_wbuf_quiet = 0;
380 /* not once unlisten() has stopped check_cb: it would hold the loop open */
381 0 ▶ if (!ev_is_active(&c->server->tls_wbuf_trim_w)
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_is_active(&c->server->tls_wbuf_trim_w) 0 0 0
382 0 ▶ && ev_is_active(&c->server->ec))
Branch: 1 branch
BranchTrueFalseTotal
&& ev_is_active(&c->server->ec)) 0 0 0
383 0 ev_timer_start(feersum_ev_loop, &c->server->tls_wbuf_trim_w);
384 }
385  
386 /* marked on one tick, trimmed on the next: only a conn idle a whole interval */
387 static void
388 0 tls_wbuf_trim_cb(EV_P_ ev_timer *w, int revents)
389 {
390 0 struct feer_server *server = (struct feer_server *)w->data;
391 0 struct rinq *head = server->idle_keepalive_rinq, *n = head;
392 0 int oversized = 0;
393 PERL_UNUSED_VAR(revents);
394 0 ▶ if (n) do {
Branch: 1 branch
BranchTrueFalseTotal
if (n) do { 0 0 0
395 0 struct feer_conn *c = (struct feer_conn *)n->ref;
396 0 ▶ if (c->tls && c->tls_wbuf.capacity > FEER_TLS_WBUF_KEEP) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->tls && c->tls_wbuf.capacity > FEER_TLS_WBUF_KEEP) { 0 0 0
if (c->tls && c->tls_wbuf.capacity > FEER_TLS_WBUF_KEEP) { 0 0 0
397 0 ▶ if (c->tls_wbuf_quiet && c->tls_wbuf.off == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->tls_wbuf_quiet && c->tls_wbuf.off == 0) { 0 0 0
if (c->tls_wbuf_quiet && c->tls_wbuf.off == 0) { 0 0 0
398 0 ptls_buffer_dispose(&c->tls_wbuf);
399 0 ptls_buffer_init(&c->tls_wbuf, "", 0);
400 } else {
401 0 c->tls_wbuf_quiet = 1;
402 0 oversized = 1;
403 }
404 }
405 0 n = n->next;
406 0 ▶ } while (n != head);
Branch: 1 branch
BranchTrueFalseTotal
} while (n != head); 0 0 0
407 0 ▶ if (!oversized)
Branch: 1 branch
BranchTrueFalseTotal
if (!oversized) 0 0 0
408 0 ev_timer_stop(EV_A, w);
409 0 }
410  
411  
412 /* TLS tunnel for io()/psgix.io: a socketpair, sv0 Feersum's end, sv1 the app's */
413  
414 static void
415 27 tls_tunnel_maybe_send_input_eof(struct feer_conn *c)
416 {
417 27 ▶ if (!c->tls_tunnel || !c->tls_input_eof || c->tls_tunnel_sv0 < 0
Branch: 3 branches
BranchTrueFalseTotal
if (!c->tls_tunnel || !c->tls_input_eof || c->tls_tunnel_sv0 < 0 27 0 27
if (!c->tls_tunnel || !c->tls_input_eof || c->tls_tunnel_sv0 < 0 14 13 27
if (!c->tls_tunnel || !c->tls_input_eof || c->tls_tunnel_sv0 < 0 14 0 14
418 14 ▶ || c->tls_tunnel_input_eof_sent || c->tls_rbuf_len > 0)
Branch: 2 branches
BranchTrueFalseTotal
|| c->tls_tunnel_input_eof_sent || c->tls_rbuf_len > 0) 7 7 14
|| c->tls_tunnel_input_eof_sent || c->tls_rbuf_len > 0) 0 7 7
419 20 return;
420 7 ▶ if (c->tls_tunnel_wbuf
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_wbuf 0 7 7
421 0 ▶ && SvCUR(c->tls_tunnel_wbuf) > c->tls_tunnel_wbuf_pos)
Branch: 1 branch
BranchTrueFalseTotal
&& SvCUR(c->tls_tunnel_wbuf) > c->tls_tunnel_wbuf_pos) 0 0 0
422 0 return;
423 7 shutdown(c->tls_tunnel_sv0, SHUT_WR);
424 7 c->tls_tunnel_input_eof_sent = 1;
425 }
426  
427 static void
428 8 tls_tunnel_note_input_eof(struct feer_conn *c)
429 {
430 8 c->tls_input_eof = 1;
431 /* every TLS input EOF funnels here, not only a tunnel's; repeats are no-ops */
432 8 feersum_note_input_eof(c);
433 8 ▶ if (c->tls_tunnel) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel) { 7 1 8
434 7 stop_read_watcher(c);
435 7 tls_tunnel_maybe_send_input_eof(c);
436 }
437 8 }
438  
439 static int
440 13 tls_tunnel_write_or_buffer(struct feer_conn *c, const char *data, size_t len)
441 {
442 dTHX;
443 13 ▶ if (c->tls_tunnel_sv0 < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_sv0 < 0) return -1; 0 13 13
444  
445 /* A queued remainder goes first, or this write overtakes it and the app
446 * sees its stream out of order. */
447 13 ssize_t nw = 0;
448 13 ▶ if (!(c->tls_tunnel_wbuf
Branch: 1 branch
BranchTrueFalseTotal
if (!(c->tls_tunnel_wbuf 0 13 13
449 0 ▶ && SvCUR(c->tls_tunnel_wbuf) > c->tls_tunnel_wbuf_pos)) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvCUR(c->tls_tunnel_wbuf) > c->tls_tunnel_wbuf_pos)) { 0 0 0
450 13 nw = write(c->tls_tunnel_sv0, data, len);
451 13 ▶ if (nw == (ssize_t)len)
Branch: 1 branch
BranchTrueFalseTotal
if (nw == (ssize_t)len) 13 0 13
452 13 return 0;
453  
454 0 ▶ if (nw < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nw < 0) { 0 0 0
455 0 ▶ if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) 0 0 0
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) 0 0 0
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) 0 0 0
456 0 return -1;
457 0 nw = 0;
458 }
459 }
460  
461 0 size_t remaining = len - nw;
462 0 ▶ if (!c->tls_tunnel_wbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tls_tunnel_wbuf) { 0 0 0
463 0 c->tls_tunnel_wbuf = newSV_buf(remaining + 256);
464 }
465  
466 0 ▶ if (c->tls_tunnel_wbuf_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_wbuf_pos > 0) { 0 0 0
467 0 STRLEN remain = SvCUR(c->tls_tunnel_wbuf) - c->tls_tunnel_wbuf_pos;
468 0 ▶ if (remain > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (remain > 0) 0 0 0
469 0 memmove(SvPVX(c->tls_tunnel_wbuf),
470 0 SvPVX(c->tls_tunnel_wbuf) + c->tls_tunnel_wbuf_pos,
471 remain);
472 0 SvCUR_set(c->tls_tunnel_wbuf, remain);
473 0 c->tls_tunnel_wbuf_pos = 0;
474 }
475  
476 0 ▶ if (SvCUR(c->tls_tunnel_wbuf) + remaining > FEER_TUNNEL_MAX_WBUF) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(c->tls_tunnel_wbuf) + remaining > FEER_TUNNEL_MAX_WBUF) { 0 0 0
477 0 trouble("TLS tunnel wbuf overflow fd=%d\n", c->fd);
478 0 return -1;
479 }
480  
481 0 sv_catpvn(c->tls_tunnel_wbuf, data + nw, remaining);
482 0 ▶ if (!ev_is_active(&c->tls_tunnel_write_w))
Branch: 1 branch
BranchTrueFalseTotal
if (!ev_is_active(&c->tls_tunnel_write_w)) 0 0 0
483 0 ev_io_start(feersum_ev_loop, &c->tls_tunnel_write_w);
484 0 return 0;
485 }
486  
487 static int feer_tls_drain_one_record(struct feer_conn *c, ptls_buffer_t *decbuf, uint8_t *scratch);
488  
489 static void
490 20 drain_tls_tunnel_rbuf(struct feer_conn *c, uint8_t *scratch)
491 {
492 20 ▶ while (c->tls_tunnel && c->tls_rbuf_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->tls_tunnel && c->tls_rbuf_len > 0) { 20 0 20
while (c->tls_tunnel && c->tls_rbuf_len > 0) { 0 20 20
493 0 ▶ if (ev_is_active(&c->tls_tunnel_write_w))
Branch: 1 branch
BranchTrueFalseTotal
if (ev_is_active(&c->tls_tunnel_write_w)) 0 0 0
494 0 break; /* sv[0] full - let write watcher drain first */
495 ptls_buffer_t db;
496 0 int drain_rv = feer_tls_drain_one_record(c, &db, scratch);
497 0 ▶ if (unlikely(drain_rv == -2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(drain_rv == -2)) { 0 0 0
498 0 feer_shutdown_conn(c, "TLS receive error (tunnel drain)");
499 0 return;
500 }
501 0 ▶ if (drain_rv < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (drain_rv < 0) break; 0 0 0
502 0 ▶ if (drain_rv == 1) continue; /* non-data (e.g. KeyUpdate) */
Branch: 1 branch
BranchTrueFalseTotal
if (drain_rv == 1) continue; /* non-data (e.g. KeyUpdate) */ 0 0 0
503 0 ▶ if (tls_tunnel_write_or_buffer(c, (const char *)db.base, db.off) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tls_tunnel_write_or_buffer(c, (const char *)db.base, db.off) < 0) { 0 0 0
504 0 ptls_buffer_dispose(&db);
505 0 feer_shutdown_conn(c, "TLS tunnel drain error");
506 0 return;
507 }
508 0 ptls_buffer_dispose(&db);
509 }
510 20 tls_tunnel_maybe_send_input_eof(c);
511 }
512  
513 /* Stop the watchers (a readable-at-EOF sv0 spins the loop) and close sv0 so
514 * the app sees EOF; sv1 belongs to feer_tls_free_conn. Idempotent. */
515 static void
516 9 feer_tls_tunnel_shutdown(struct feer_conn *c)
517 {
518 9 ▶ if (!c->tls_tunnel) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tls_tunnel) return; 0 9 9
519 9 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_read_w);
520 9 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_write_w);
521 9 ▶ if (c->tls_tunnel_sv0 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_sv0 >= 0) { 9 0 9
522 9 close(c->tls_tunnel_sv0);
523 9 c->tls_tunnel_sv0 = -1;
524 }
525 /* cannot free c: callers hold their own reference through teardown */
526 9 ▶ if (c->tls_tunnel_ref_held) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_ref_held) { 9 0 9
527 9 c->tls_tunnel_ref_held = 0;
528 9 ▶ if (PL_phase != PERL_PHASE_DESTRUCT)
Branch: 1 branch
BranchTrueFalseTotal
if (PL_phase != PERL_PHASE_DESTRUCT) 9 0 9
529 9 SvREFCNT_dec(c->self);
530 }
531 }
532  
533 static void
534 30 tls_tunnel_sv0_read_cb(EV_P_ struct ev_io *w, int revents)
535 {
536 30 struct feer_conn *c = (struct feer_conn *)w->data;
537 PERL_UNUSED_VAR(revents);
538 30 SvREFCNT_inc_void_NN(c->self);
539  
540 char buf[FEER_TUNNEL_BUFSZ];
541 30 ssize_t nread = read(c->tls_tunnel_sv0, buf, sizeof(buf));
542  
543 30 ▶ if (nread < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nread < 0) { 0 30 30
544 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
545 0 goto cleanup;
546 /* Linux AF_UNIX reports ECONNRESET, not EOF, when sv1 closes with
547 * unread inbound bytes; take the EOF path so queued ciphertext is
548 * still flushed */
549 0 ▶ if (errno != ECONNRESET && errno != EPIPE) {
Branch: 2 branches
BranchTrueFalseTotal
if (errno != ECONNRESET && errno != EPIPE) { 0 0 0
if (errno != ECONNRESET && errno != EPIPE) { 0 0 0
550 0 feer_shutdown_conn(c, "TLS tunnel read error");
551 0 goto cleanup;
552 }
553 0 nread = 0;
554 }
555  
556 30 ▶ if (nread == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nread == 0) { 9 21 30
557 /* App closed sv1: flush queued ciphertext before shutting down */
558 9 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_read_w);
559 9 ▶ if (c->tls_wbuf.off > 0 && feer_tls_flush_wbuf(c) != -2
Branch: 2 branches
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0 && feer_tls_flush_wbuf(c) != -2 0 9 9
if (c->tls_wbuf.off > 0 && feer_tls_flush_wbuf(c) != -2 0 0 0
560 0 ▶ && c->tls_wbuf.off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->tls_wbuf.off > 0) { 0 0 0
561 0 c->tls_tunnel_eof = 1;
562 0 start_write_watcher(c);
563 /* write_timeout is off for a takeover; re-arm now the app is gone */
564 0 ensure_write_timer(c);
565 0 goto cleanup;
566 }
567 9 feer_shutdown_conn(c, "TLS tunnel EOF");
568 9 goto cleanup;
569 }
570 21 ▶ if (feer_tls_send(c, buf, nread) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (feer_tls_send(c, buf, nread) != 0) { 0 21 21
571 0 feer_shutdown_conn(c, "TLS tunnel encrypt error");
572 0 goto cleanup;
573 }
574  
575 21 int flush_ret = feer_tls_flush_wbuf(c);
576 21 ▶ if (flush_ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret == -2) { 0 21 21
577 0 feer_shutdown_conn(c, "TLS tunnel flush error");
578 0 goto cleanup;
579 }
580 21 ▶ if (c->tls_wbuf.off > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0) 0 21 21
581 0 start_write_watcher(c);
582  
583 /* Backpressure: tls_wbuf has no bound of its own, so stop pulling from the
584 * app past the cap; try_tls_conn_write re-arms this watcher once the wire
585 * catches up. */
586 21 ▶ if (unlikely(c->tls_wbuf.off > FEER_TUNNEL_MAX_WBUF))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->tls_wbuf.off > FEER_TUNNEL_MAX_WBUF)) 21 0 21
587 0 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_read_w);
588  
589 21 cleanup:
590 30 SvREFCNT_dec(c->self);
591 30 }
592  
593 static void
594 0 tls_tunnel_sv0_write_cb(EV_P_ struct ev_io *w, int revents)
595 {
596 PERL_UNUSED_VAR(revents);
597 0 struct feer_conn *c = (struct feer_conn *)w->data;
598 0 SvREFCNT_inc_void_NN(c->self);
599  
600 0 ▶ if (!c->tls_tunnel_wbuf ||
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tls_tunnel_wbuf || 0 0 0
601 0 ▶ SvCUR(c->tls_tunnel_wbuf) <= c->tls_tunnel_wbuf_pos) {
Branch: 1 branch
BranchTrueFalseTotal
SvCUR(c->tls_tunnel_wbuf) <= c->tls_tunnel_wbuf_pos) { 0 0 0
602 0 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_write_w);
603 0 ▶ if (c->tls_tunnel_wbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_wbuf) { 0 0 0
604 0 SvCUR_set(c->tls_tunnel_wbuf, 0);
605 0 c->tls_tunnel_wbuf_pos = 0;
606 }
607 0 tls_tunnel_maybe_send_input_eof(c);
608 0 goto cleanup;
609 }
610  
611 0 STRLEN avail = SvCUR(c->tls_tunnel_wbuf) - c->tls_tunnel_wbuf_pos;
612 0 const char *ptr = SvPVX(c->tls_tunnel_wbuf) + c->tls_tunnel_wbuf_pos;
613 0 ssize_t nw = write(c->tls_tunnel_sv0, ptr, avail);
614  
615 0 ▶ if (nw < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nw < 0) { 0 0 0
616 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
617 0 goto cleanup;
618 0 feer_shutdown_conn(c, "TLS tunnel write error");
619 0 goto cleanup;
620 }
621  
622 0 c->tls_tunnel_wbuf_pos += nw;
623 0 ▶ if (c->tls_tunnel_wbuf_pos >= SvCUR(c->tls_tunnel_wbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel_wbuf_pos >= SvCUR(c->tls_tunnel_wbuf)) { 0 0 0
624 0 ev_io_stop(feersum_ev_loop, &c->tls_tunnel_write_w);
625 0 SvCUR_set(c->tls_tunnel_wbuf, 0);
626 0 c->tls_tunnel_wbuf_pos = 0;
627  
628 0 ▶ if (c->tls_rbuf_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_rbuf_len > 0) { 0 0 0
629 uint8_t scratch[TLS_DEC_SCRATCH];
630 0 drain_tls_tunnel_rbuf(c, scratch);
631 } else
632 0 tls_tunnel_maybe_send_input_eof(c);
633 }
634  
635 0 cleanup:
636 0 SvREFCNT_dec(c->self);
637 0 }
638  
639 static void
640 9 feer_tls_setup_tunnel(struct feer_conn *c)
641 {
642 9 ▶ if (c->tls_tunnel) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel) return; 0 9 9
643  
644 int sv[2];
645 9 ▶ if (feer_socketpair_nb(sv) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (feer_socketpair_nb(sv) < 0) { 0 9 9
646 0 trouble("socketpair/fcntl failed for TLS tunnel fd=%d: %s\n",
647 c->fd, strerror(errno));
648 0 return;
649 }
650  
651 9 c->tls_tunnel_sv0 = sv[0];
652 9 c->tls_tunnel_sv1 = sv[1];
653  
654 9 ev_io_init(&c->tls_tunnel_read_w, tls_tunnel_sv0_read_cb, sv[0], EV_READ);
655 9 c->tls_tunnel_read_w.data = (void *)c;
656 9 ev_io_start(feersum_ev_loop, &c->tls_tunnel_read_w);
657  
658 9 ev_io_init(&c->tls_tunnel_write_w, tls_tunnel_sv0_write_cb, sv[0], EV_WRITE);
659 9 c->tls_tunnel_write_w.data = (void *)c;
660  
661 9 c->tls_tunnel = 1;
662 9 c->tls_tunnel_ref_held = 1;
663 9 SvREFCNT_inc_void_NN(c->self);
664  
665 9 ▶ if (c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf) > 0) { 9 0 9
if (c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf) > 0) { 9 0 9
if (c->rbuf && SvOK(c->rbuf) && SvCUR(c->rbuf) > 0) { 0 9 9
666 dTHX;
667 0 ▶ if (tls_tunnel_write_or_buffer(c, SvPVX(c->rbuf), SvCUR(c->rbuf)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tls_tunnel_write_or_buffer(c, SvPVX(c->rbuf), SvCUR(c->rbuf)) < 0) { 0 0 0
668 0 trouble("TLS tunnel rbuf flush failed fd=%d\n", c->fd);
669 }
670 0 SvCUR_set(c->rbuf, 0);
671 }
672  
673 9 ▶ if (c->tls_input_eof)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_input_eof) 0 9 9
674 0 tls_tunnel_note_input_eof(c);
675 else
676 9 start_read_watcher(c);
677  
678 trace("TLS tunnel socketpair established fd=%d sv0=%d sv1=%d\n",
679 c->fd, sv[0], sv[1]);
680 }
681  
682 /* Precondition: c->tls_rbuf is NULL, or the old buffer leaks */
683 INLINE_UNLESS_DEBUG static void
684 0 feer_tls_save_rbuf(struct feer_conn *c, const uint8_t *src, size_t len)
685 {
686 0 Newx(c->tls_rbuf, len, uint8_t);
687 0 memcpy(c->tls_rbuf, src, len);
688 0 c->tls_rbuf_base = c->tls_rbuf;
689 0 c->tls_rbuf_len = len;
690 0 }
691  
692 /* Record the alert owed to the peer; safe_close_conn sends it in place of
693 * close_notify */
694 INLINE_UNLESS_DEBUG static void
695 0 feer_tls_note_alert(struct feer_conn *c, int ret)
696 {
697 0 ▶ if (PTLS_ERROR_GET_CLASS(ret) == PTLS_ERROR_CLASS_SELF_ALERT)
Branch: 1 branch
BranchTrueFalseTotal
if (PTLS_ERROR_GET_CLASS(ret) == PTLS_ERROR_CLASS_SELF_ALERT) 0 0 0
698 0 c->tls_fatal_alert = (uint8_t)PTLS_ERROR_TO_ALERT(ret);
699 0 }
700  
701 /* Returns 0: app data in decbuf (caller disposes); 1: non-data record
702 * (KeyUpdate, NewSessionTicket), retry while tls_rbuf remains; -1: nothing
703 * left or peer close_notify; -2: undecryptable, caller must tear down */
704 static int
705 168 feer_tls_drain_one_record(struct feer_conn *c, ptls_buffer_t *decbuf, uint8_t *scratch)
706 {
707 168 ▶ if (!c->tls_rbuf || c->tls_rbuf_len == 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!c->tls_rbuf || c->tls_rbuf_len == 0) return -1; 0 168 168
if (!c->tls_rbuf || c->tls_rbuf_len == 0) return -1; 0 0 0
708 0 ptls_buffer_init(decbuf, scratch, TLS_DEC_SCRATCH);
709  
710 0 size_t consumed = c->tls_rbuf_len;
711 0 int ret = ptls_receive(c->tls, decbuf, c->tls_rbuf, &consumed);
712 0 ▶ if (ret == 0 && consumed < c->tls_rbuf_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (ret == 0 && consumed < c->tls_rbuf_len) { 0 0 0
if (ret == 0 && consumed < c->tls_rbuf_len) { 0 0 0
713 0 c->tls_rbuf += consumed;
714 0 c->tls_rbuf_len -= consumed;
715 } else
716 0 feer_tls_rbuf_release(c);
717  
718 0 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 0 0
719 0 feer_tls_decbuf_done(decbuf);
720 /* close_notify arrives as an error but must not tear down here: a
721 * request coalesced ahead of it is decrypted and still owed a response */
722 0 ▶ if (ret == PTLS_ERROR_CLASS_PEER_ALERT + PTLS_ALERT_CLOSE_NOTIFY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == PTLS_ERROR_CLASS_PEER_ALERT + PTLS_ALERT_CLOSE_NOTIFY) { 0 0 0
723 0 tls_tunnel_note_input_eof(c);
724 0 feersum_note_input_eof(c);
725 0 return -1;
726 }
727 0 feer_tls_note_alert(c, ret);
728 0 return -2;
729 }
730 0 ▶ if (decbuf->off == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (decbuf->off == 0) { 0 0 0
731 0 feer_tls_decbuf_done(decbuf);
732 0 return 1;
733 }
734 0 return 0;
735 }
736  
737 /* Appends decbuf and every further complete record to rbuf; disposes decbuf.
738 * Returns bytes appended; *failed is set when a record would not decrypt */
739 static size_t
740 168 feer_tls_rbuf_append_drain(struct feer_conn *c, ptls_buffer_t *decbuf, int *failed,
741 uint8_t *scratch)
742 {
743 dTHX;
744 168 size_t added = 0;
745 168 ▶ if (decbuf->off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (decbuf->off > 0) { 168 0 168
746 168 ▶ if (!c->rbuf)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->rbuf) 153 15 168
747 153 c->rbuf = newSV_buf(decbuf->off + READ_BUFSZ);
748 168 sv_catpvn(c->rbuf, (const char *)decbuf->base, decbuf->off);
749 168 added = decbuf->off;
750 }
751 168 feer_tls_decbuf_done(decbuf);
752 ptls_buffer_t db;
753 int drain_rv;
754 168 ▶ while ((drain_rv = feer_tls_drain_one_record(c, &db, scratch)) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while ((drain_rv = feer_tls_drain_one_record(c, &db, scratch)) >= 0) { 0 168 168
755 0 ▶ if (drain_rv == 1) continue; /* non-data TLS record */
Branch: 1 branch
BranchTrueFalseTotal
if (drain_rv == 1) continue; /* non-data TLS record */ 0 0 0
756 0 ▶ if (!c->rbuf)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->rbuf) 0 0 0
757 0 c->rbuf = newSV_buf(db.off + READ_BUFSZ);
758 0 sv_catpvn(c->rbuf, (const char *)db.base, db.off);
759 0 added += db.off;
760 0 feer_tls_decbuf_done(&db);
761 }
762 168 ▶ if (unlikely(drain_rv == -2))
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(drain_rv == -2)) 0 168 168
763 0 *failed = 1;
764 168 return added;
765 }
766  
767 #ifdef FEERSUM_HAS_H2
768 static void
769 drain_h2_tls_records(struct feer_conn *c, uint8_t *scratch)
770 {
771 dTHX;
772 while (c->h2_session && c->fd >= 0
773 && !c->closing_linger && !c->h2_close_pending) {
774 ptls_buffer_t db;
775 int drain_rv = feer_tls_drain_one_record(c, &db, scratch);
776 if (unlikely(drain_rv == -2)) {
777 stop_all_watchers(c);
778 safe_close_conn(c, "TLS receive error (H2 drain)");
779 break;
780 }
781 if (drain_rv < 0) break;
782 if (drain_rv == 1) continue; /* non-data TLS record */
783 feer_h2_session_recv(c, db.base, db.off);
784 feer_tls_decbuf_done(&db);
785 if (c->fd < 0) break;
786 h2_session_send_and_poll(aTHX_ c);
787 restart_read_timer(c);
788 }
789 }
790 #endif
791  
792 /* call after parsing all decrypted bytes: retires only an incomplete request */
793 static void
794 664 tls_http_input_eof(struct feer_conn *c)
795 {
796 664 ▶ if (!c->tls_input_eof || c->fd < 0 || c->closing_linger || c->tls_tunnel)
Branch: 4 branches
BranchTrueFalseTotal
if (!c->tls_input_eof || c->fd < 0 || c->closing_linger || c->tls_tunnel) 7 657 664
if (!c->tls_input_eof || c->fd < 0 || c->closing_linger || c->tls_tunnel) 7 0 7
if (!c->tls_input_eof || c->fd < 0 || c->closing_linger || c->tls_tunnel) 7 0 7
if (!c->tls_input_eof || c->fd < 0 || c->closing_linger || c->tls_tunnel) 7 0 7
797 664 return;
798 0 stop_read_watcher(c);
799 0 stop_header_timer(c);
800 #ifdef FEERSUM_HAS_H2
801 if (c->h2_session) {
802 feer_h2_input_eof(c);
803 return;
804 }
805 #endif
806 0 ▶ if (c->receiving != RECEIVE_SHUTDOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving != RECEIVE_SHUTDOWN) { 0 0 0
807 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
808 0 feer_shutdown_conn(c, "TLS EOF before complete request");
809 0 return;
810 }
811  
812 /* only the last buffered request is answered with Connection: close */
813 0 ▶ STRLEN buffered = c->rbuf ? SvCUR(c->rbuf) : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN buffered = c->rbuf ? SvCUR(c->rbuf) : 0; 0 0 0
814 0 ssize_t consumed = c->received_cl - (ssize_t)buffered;
815 0 ssize_t body_left = c->expected_cl - consumed;
816 0 ▶ if (body_left < 0) body_left = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (body_left < 0) body_left = 0; 0 0 0
817 0 ▶ if (buffered <= (STRLEN)body_left)
Branch: 1 branch
BranchTrueFalseTotal
if (buffered <= (STRLEN)body_left) 0 0 0
818 0 c->is_keepalive = 0;
819 }
820  
821 static void
822 664 try_tls_conn_read(EV_P_ ev_io *w, int revents)
823 {
824 664 struct feer_conn *c = (struct feer_conn *)w->data;
825 PERL_UNUSED_VAR(revents);
826 PERL_UNUSED_VAR(loop);
827  
828 dTHX;
829 664 SvREFCNT_inc_void_NN(c->self);
830 664 feer_conn_set_busy(c);
831 trace("tls_conn_read fd=%d hs_done=%d\n", c->fd, c->tls_handshake_done);
832  
833 664 ssize_t got_n = 0;
834 664 uint8_t *merged_late = NULL;
835  
836 664 ▶ if (unlikely(c->pipelined)) goto tls_pipelined;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->pipelined)) goto tls_pipelined; 5 659 664
837 659 ▶ if (unlikely(c->tls_input_eof)) goto tls_read_cleanup;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->tls_input_eof)) goto tls_read_cleanup; 0 659 659
838  
839 659 ▶ if (unlikely(!c->tls)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->tls)) { 0 659 659
840 0 trouble("tls_conn_read: no TLS context fd=%d\n", c->fd);
841 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
842 0 stop_all_watchers(c);
843 0 safe_close_conn(c, "no TLS context");
844 0 goto tls_read_cleanup;
845 }
846  
847 uint8_t rawbuf[TLS_RAW_BUFSZ];
848 /* one decrypt buffer per callback: each record is consumed before the next */
849 uint8_t scratch[TLS_DEC_SCRATCH];
850 659 ssize_t nread = read(c->fd, rawbuf, sizeof(rawbuf));
851  
852 659 ▶ if (nread == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nread == 0) { 7 652 659
853 trace("TLS EOF fd=%d\n", c->fd);
854 /* Peer half-close on a tunnel: relay EOF to the app via SHUT_WR and
855 * keep the write side alive; closing here drops what the app still
856 * owes the peer */
857 7 ▶ if (c->tls_tunnel && c->tls_tunnel_sv0 >= 0 && !c->tls_tunnel_eof) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->tls_tunnel && c->tls_tunnel_sv0 >= 0 && !c->tls_tunnel_eof) { 7 0 7
if (c->tls_tunnel && c->tls_tunnel_sv0 >= 0 && !c->tls_tunnel_eof) { 7 0 7
if (c->tls_tunnel && c->tls_tunnel_sv0 >= 0 && !c->tls_tunnel_eof) { 7 0 7
858 7 tls_tunnel_note_input_eof(c);
859 7 drain_tls_tunnel_rbuf(c, scratch);
860 7 goto tls_read_cleanup;
861 }
862 0 tls_tunnel_note_input_eof(c);
863 0 goto tls_read_cleanup;
864 }
865  
866 652 ▶ if (nread < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nread < 0) { 0 652 652
867 0 ▶ if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
Branch: 3 branches
BranchTrueFalseTotal
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) 0 0 0
868 0 goto tls_read_cleanup;
869 trace("TLS read error fd=%d: %s\n", c->fd, strerror(errno));
870 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
871 0 stop_all_watchers(c);
872 0 safe_close_conn(c, "TLS read error");
873 0 goto tls_read_cleanup;
874 }
875  
876 /* A PROXY header precedes the ClientHello in plaintext */
877 652 ▶ if (unlikely(c->receiving == RECEIVE_PROXY_HEADER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->receiving == RECEIVE_PROXY_HEADER)) { 152 500 652
878 152 ▶ if (!c->rbuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->rbuf) { 23 129 152
879 23 c->rbuf = newSV_buf(nread + 256);
880 }
881 152 sv_catpvn(c->rbuf, (const char *)rawbuf, nread);
882  
883 152 int ret = try_parse_proxy_header(c);
884 152 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 4 148 152
885 4 feer_shutdown_conn(c, "Invalid PROXY header on TLS listener");
886 4 change_receiving_state(c, RECEIVE_SHUTDOWN);
887 4 goto tls_read_cleanup;
888 }
889 148 ▶ if (ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) { 129 19 148
890 129 restart_read_timer(c); /* need more data, but this was progress */
891 129 goto tls_read_cleanup;
892 }
893  
894 19 STRLEN remaining = SvCUR(c->rbuf) - ret;
895  
896 19 ▶ feer_clear_remote_cache(c);
Branch: 2 branches
BranchTrueFalseTotal
feer_clear_remote_cache(c); 0 19 19
feer_clear_remote_cache(c); 0 19 19
897  
898 19 change_receiving_state(c, RECEIVE_HEADERS);
899  
900 19 ▶ if (remaining > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining > 0) { 0 19 19
901 /* the leftover ClientHello may exceed rawbuf, so park it in tls_rbuf */
902 0 feer_tls_save_rbuf(c, (const uint8_t *)SvPVX(c->rbuf) + ret, remaining);
903 0 nread = 0;
904 }
905 19 SvREFCNT_dec(c->rbuf);
906 19 c->rbuf = NULL;
907 19 ▶ if (remaining == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (remaining == 0) 19 0 19
908 19 goto tls_read_cleanup;
909 }
910  
911 /* Prepend the unconsumed tail of the previous read (a record split across reads) */
912 500 uint8_t *inbuf = rawbuf;
913 500 size_t inlen = (size_t)nread;
914 500 uint8_t *merged = NULL;
915  
916 500 ▶ if (c->tls_rbuf_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_rbuf_len > 0) { 0 500 500
917 0 ▶ if (nread > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nread > 0) { 0 0 0
918 0 inlen = c->tls_rbuf_len + (size_t)nread;
919 0 Newx(merged, inlen, uint8_t);
920 0 memcpy(merged, c->tls_rbuf, c->tls_rbuf_len);
921 0 memcpy(merged + c->tls_rbuf_len, rawbuf, (size_t)nread);
922 0 inbuf = merged;
923 0 Safefree(c->tls_rbuf_base);
924 } else {
925 /* PROXY leftover only - transfer ownership to merged, no copy */
926 0 merged = c->tls_rbuf_base;
927 0 inbuf = c->tls_rbuf;
928 0 inlen = c->tls_rbuf_len;
929 }
930 0 c->tls_rbuf = c->tls_rbuf_base = NULL;
931 0 c->tls_rbuf_len = 0;
932 }
933  
934 500 ▶ if (!c->tls_handshake_done) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tls_handshake_done) { 166 334 500
935 166 size_t consumed = inlen;
936  
937 166 int ret = ptls_handshake(c->tls, &c->tls_wbuf, inbuf, &consumed, NULL);
938  
939 166 ▶ if (c->tls_wbuf.off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0) { 157 9 166
940 157 int flush_ret = feer_tls_flush_wbuf(c);
941 157 ▶ if (flush_ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret == -2) { 0 157 157
942 0 trouble("TLS flush error during handshake fd=%d\n", c->fd);
943 0 ▶ if (merged) Safefree(merged);
Branch: 1 branch
BranchTrueFalseTotal
if (merged) Safefree(merged); 0 0 0
944 0 stop_all_watchers(c);
945 0 safe_close_conn(c, "TLS handshake flush error");
946 166 goto tls_read_cleanup;
947 }
948 157 ▶ if (flush_ret == -1 || c->tls_wbuf.off > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (flush_ret == -1 || c->tls_wbuf.off > 0) { 157 0 157
if (flush_ret == -1 || c->tls_wbuf.off > 0) { 0 157 157
949 0 start_write_watcher(c);
950 }
951 }
952  
953 166 ▶ if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) { 153 13 166
954 153 c->tls_handshake_done = 1;
955 trace("TLS handshake complete fd=%d\n", c->fd);
956  
957 153 const char *proto = ptls_get_negotiated_protocol(c->tls);
958 153 ▶ if (proto && strcmp(proto, "h2") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (proto && strcmp(proto, "h2") == 0) { 71 82 153
959 trace("TLS ALPN: h2 negotiated fd=%d\n", c->fd);
960 #ifdef FEERSUM_HAS_H2
961 feer_h2_init_session(c);
962 #endif
963 } else {
964 trace("TLS ALPN: http/1.1 (or none) fd=%d\n", c->fd);
965 }
966  
967 153 ▶ if (consumed < inlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (consumed < inlen) { 0 153 153
968 0 size_t remaining = inlen - consumed;
969 0 uint8_t *extra = inbuf + consumed;
970  
971 #ifdef FEERSUM_HAS_H2
972 if (c->h2_session) {
973 ptls_buffer_t decbuf;
974 ptls_buffer_init(&decbuf, scratch, TLS_DEC_SCRATCH);
975 size_t dec_consumed = remaining;
976 int dec_ret = ptls_receive(c->tls, &decbuf, extra, &dec_consumed);
977 if (dec_ret != 0) {
978 feer_tls_decbuf_done(&decbuf);
979 if (merged) Safefree(merged);
980 feer_tls_note_alert(c, dec_ret);
981 stop_all_watchers(c);
982 safe_close_conn(c, "TLS receive error (post-handshake H2)");
983 goto tls_read_cleanup;
984 }
985 if (dec_consumed < remaining)
986 feer_tls_save_rbuf(c, extra + dec_consumed, remaining - dec_consumed);
987 if (decbuf.off > 0) {
988 feer_h2_session_recv(c, decbuf.base, decbuf.off);
989 }
990 feer_tls_decbuf_done(&decbuf);
991 if (c->fd < 0) {
992 if (merged) Safefree(merged);
993 goto tls_read_cleanup;
994 }
995 h2_session_send_and_poll(aTHX_ c);
996 restart_read_timer(c);
997  
998 drain_h2_tls_records(c, scratch);
999  
1000 if (merged) Safefree(merged);
1001 goto tls_read_cleanup;
1002 }
1003 #endif
1004 ptls_buffer_t decbuf;
1005 0 ptls_buffer_init(&decbuf, scratch, TLS_DEC_SCRATCH);
1006 0 size_t dec_consumed = remaining;
1007 0 int dec_ret = ptls_receive(c->tls, &decbuf, extra, &dec_consumed);
1008 0 ▶ if (dec_ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dec_ret != 0) { 0 0 0
1009 0 feer_tls_decbuf_done(&decbuf);
1010 0 ▶ if (merged) Safefree(merged);
Branch: 1 branch
BranchTrueFalseTotal
if (merged) Safefree(merged); 0 0 0
1011 0 feer_tls_note_alert(c, dec_ret);
1012 0 stop_all_watchers(c);
1013 0 safe_close_conn(c, "TLS receive error (post-handshake H1)");
1014 0 goto tls_read_cleanup;
1015 }
1016 0 ▶ if (dec_consumed < remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (dec_consumed < remaining) 0 0 0
1017 0 feer_tls_save_rbuf(c, extra + dec_consumed, remaining - dec_consumed);
1018 0 int drain_failed = 0;
1019 0 size_t added = feer_tls_rbuf_append_drain(c, &decbuf, &drain_failed, scratch);
1020 0 ▶ if (unlikely(drain_failed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(drain_failed)) { 0 0 0
1021 0 ▶ if (merged) Safefree(merged);
Branch: 1 branch
BranchTrueFalseTotal
if (merged) Safefree(merged); 0 0 0
1022 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
1023 0 stop_all_watchers(c);
1024 0 safe_close_conn(c, "TLS receive error (H1 drain)");
1025 0 goto tls_read_cleanup;
1026 }
1027 0 ▶ if (added > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (added > 0) { 0 0 0
1028 0 ▶ if (merged) Safefree(merged);
Branch: 1 branch
BranchTrueFalseTotal
if (merged) Safefree(merged); 0 0 0
1029 0 got_n = (ssize_t)added;
1030 0 goto tls_parse;
1031 }
1032 }
1033 13 ▶ } else if (ret == PTLS_ERROR_IN_PROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ret == PTLS_ERROR_IN_PROGRESS) { 9 4 13
1034 trace("TLS handshake in progress fd=%d\n", c->fd);
1035 9 restart_read_timer(c);
1036 9 ▶ if (consumed < inlen)
Branch: 1 branch
BranchTrueFalseTotal
if (consumed < inlen) 0 9 9
1037 0 feer_tls_save_rbuf(c, inbuf + consumed, inlen - consumed);
1038 } else {
1039 trace("TLS handshake error fd=%d ret=%d\n", c->fd, ret);
1040 4 stop_all_watchers(c);
1041 4 safe_close_conn(c, "TLS handshake error");
1042 }
1043 166 ▶ if (merged) Safefree(merged);
Branch: 1 branch
BranchTrueFalseTotal
if (merged) Safefree(merged); 0 166 166
1044 166 goto tls_read_cleanup;
1045 }
1046  
1047 {
1048 ptls_buffer_t decbuf;
1049 334 ptls_buffer_init(&decbuf, scratch, TLS_DEC_SCRATCH);
1050 334 size_t consumed = inlen;
1051 334 int ret = ptls_receive(c->tls, &decbuf, inbuf, &consumed);
1052  
1053 /* borrowed: drained in place, copied out once by feer_tls_rbuf_own */
1054 334 ▶ if (ret == 0 && consumed < inlen) {
Branch: 2 branches
BranchTrueFalseTotal
if (ret == 0 && consumed < inlen) { 334 0 334
if (ret == 0 && consumed < inlen) { 0 334 334
1055 0 c->tls_rbuf = inbuf + consumed;
1056 0 c->tls_rbuf_base = NULL;
1057 0 c->tls_rbuf_len = inlen - consumed;
1058 }
1059 334 merged_late = merged;
1060 334 merged = NULL;
1061  
1062 334 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 334 334
1063 trace("TLS receive error fd=%d ret=%d\n", c->fd, ret);
1064 0 feer_tls_decbuf_done(&decbuf);
1065 0 ▶ if (ret == PTLS_ERROR_CLASS_PEER_ALERT + PTLS_ALERT_CLOSE_NOTIFY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == PTLS_ERROR_CLASS_PEER_ALERT + PTLS_ALERT_CLOSE_NOTIFY) { 0 0 0
1066 0 tls_tunnel_note_input_eof(c);
1067 166 goto tls_read_cleanup;
1068 }
1069 0 feer_tls_note_alert(c, ret);
1070 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
1071 0 stop_all_watchers(c);
1072 0 safe_close_conn(c, "TLS receive error");
1073 0 goto tls_read_cleanup;
1074 }
1075  
1076 334 ▶ if (decbuf.off == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (decbuf.off == 0) { 153 181 334
1077 153 feer_tls_decbuf_done(&decbuf);
1078 153 goto tls_read_cleanup; /* non-data record (KeyUpdate etc.) */
1079 }
1080  
1081 #ifdef FEERSUM_HAS_H2
1082 if (c->h2_session) {
1083 feer_h2_session_recv(c, decbuf.base, decbuf.off);
1084 feer_tls_decbuf_done(&decbuf);
1085 if (c->fd < 0) goto tls_read_cleanup;
1086 h2_session_send_and_poll(aTHX_ c);
1087 restart_read_timer(c);
1088  
1089 drain_h2_tls_records(c, scratch);
1090  
1091 goto tls_read_cleanup;
1092 }
1093 #endif
1094  
1095 181 ▶ if (c->tls_tunnel) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_tunnel) { 13 168 181
1096 13 ▶ if (tls_tunnel_write_or_buffer(c, (const char *)decbuf.base, decbuf.off) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tls_tunnel_write_or_buffer(c, (const char *)decbuf.base, decbuf.off) < 0) { 0 13 13
1097 0 feer_tls_decbuf_done(&decbuf);
1098 0 feer_shutdown_conn(c, "TLS tunnel write error");
1099 0 goto tls_read_cleanup;
1100 }
1101 13 feer_tls_decbuf_done(&decbuf);
1102  
1103 /* Records coalesced in tls_rbuf would otherwise wait for the next read event */
1104 13 drain_tls_tunnel_rbuf(c, scratch);
1105  
1106 13 goto tls_read_cleanup;
1107 }
1108  
1109 168 int drain_failed = 0;
1110 168 got_n = (ssize_t)feer_tls_rbuf_append_drain(c, &decbuf, &drain_failed, scratch);
1111 168 ▶ if (unlikely(drain_failed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(drain_failed)) { 0 168 168
1112 0 change_receiving_state(c, RECEIVE_SHUTDOWN);
1113 0 stop_all_watchers(c);
1114 0 safe_close_conn(c, "TLS receive error (H1 drain)");
1115 0 goto tls_read_cleanup;
1116 }
1117 }
1118 168 goto tls_parse;
1119  
1120 5 tls_pipelined:
1121 5 got_n = c->pipelined;
1122 5 c->pipelined = 0;
1123  
1124 173 tls_parse:
1125 173 feer_tls_rbuf_own(c);
1126 173 ▶ if (merged_late) { Safefree(merged_late); merged_late = NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (merged_late) { Safefree(merged_late); merged_late = NULL; } 0 173 173
1127 173 restart_read_timer(c);
1128 173 begin_request_headers(c);
1129  
1130 173 ▶ if (likely(c->receiving <= RECEIVE_HEADERS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (likely(c->receiving <= RECEIVE_HEADERS)) { 171 2 173
1131 171 int parse_ret = try_parse_http(c, (size_t)got_n);
1132 171 ▶ if (parse_ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_ret == -1) { 0 171 171
1133 /* picohttpparser returns -1 for both; only the header cap leaves
1134 * num_headers at MAX_HEADERS */
1135 0 ▶ if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS)) 0 0 0
if (unlikely(c->req && c->req->num_headers >= MAX_HEADERS)) 0 0 0
1136 0 respond_with_server_error(c, "Too many header fields\n", 431);
1137 else
1138 0 respond_with_server_error(c, "Malformed request\n", 400);
1139 0 finish_receiving(c);
1140 0 goto tls_read_cleanup;
1141 }
1142 171 ▶ if (parse_ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_ret == -2) { 4 167 171
1143 4 ▶ if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) { 0 4 4
1144 trace("tls header buffer too large %d: %"Sz_uf" > %"Sz_uf"\n",
1145 c->fd, (Sz)SvCUR(c->rbuf), (Sz)c->cached_max_read_buf);
1146 0 respond_with_server_error(c,
1147 "Request header fields too large\n", 431);
1148 0 finish_receiving(c);
1149 }
1150 4 goto tls_read_cleanup;
1151 }
1152 /* parse_ret is the header block size; SvCUR may already include body bytes */
1153 167 ▶ if (unlikely((STRLEN)parse_ret > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely((STRLEN)parse_ret > c->cached_max_read_buf)) { 0 167 167
1154 trace("tls header block too large %d: %"Sz_uf" > %"Sz_uf"\n",
1155 c->fd, (Sz)parse_ret, (Sz)c->cached_max_read_buf);
1156 0 respond_with_server_error(c,
1157 "Request header fields too large\n", 431);
1158 0 finish_receiving(c);
1159 0 goto tls_read_cleanup;
1160 }
1161 167 ▶ if (!process_request_headers(c, parse_ret))
Branch: 1 branch
BranchTrueFalseTotal
if (!process_request_headers(c, parse_ret)) 165 2 167
1162 165 finish_receiving(c);
1163 }
1164 2 ▶ else if (likely(c->receiving == RECEIVE_BODY)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (likely(c->receiving == RECEIVE_BODY)) { 2 0 2
1165 2 c->received_cl += got_n;
1166 2 ▶ if (c->received_cl >= c->expected_cl) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->received_cl >= c->expected_cl) { 2 0 2
1167 2 sched_request_callback(c);
1168 2 finish_receiving(c);
1169 }
1170 }
1171 0 ▶ else if (c->receiving == RECEIVE_CHUNKED) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->receiving == RECEIVE_CHUNKED) { 0 0 0
1172 0 int ret = try_parse_chunked(c);
1173 0 ▶ if (ret == -2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -2) { 0 0 0
1174 /* -2 is the size cap, not a protocol error */
1175 0 respond_with_server_error(c, "Request body too large\n", 413);
1176 0 finish_receiving(c);
1177 }
1178 0 ▶ else if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ret == -1) { 0 0 0
1179 0 respond_with_server_error(c, "Malformed chunked encoding\n", 400);
1180 0 finish_receiving(c);
1181 }
1182 0 ▶ else if (ret == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ret == 0) { 0 0 0
1183 0 sched_request_callback(c);
1184 0 finish_receiving(c);
1185 }
1186 0 ▶ else if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (unlikely(SvCUR(c->rbuf) > c->cached_max_read_buf)) { 0 0 0
1187 /* ret == 1: still need more, but rbuf is already past the cap */
1188 trace("tls chunked buffer too large %d: %"Sz_uf" > %"Sz_uf"\n",
1189 c->fd, (Sz)SvCUR(c->rbuf), (Sz)c->cached_max_read_buf);
1190 0 respond_with_server_error(c, "Request too large\n", 413);
1191 0 finish_receiving(c);
1192 }
1193 /* ret == 1: need more data, watcher stays active */
1194 }
1195 0 ▶ else if (c->receiving == RECEIVE_STREAMING) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c->receiving == RECEIVE_STREAMING) { 0 0 0
1196 0 c->received_cl += got_n;
1197 0 ▶ if (c->poll_read_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_read_cb) { 0 0 0
1198 0 call_poll_callback(c, 0);
1199 }
1200 0 ▶ if (c->receiving >= RECEIVE_SHUTDOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->receiving >= RECEIVE_SHUTDOWN) { 0 0 0
1201 0 finish_receiving(c);
1202 0 goto tls_read_cleanup;
1203 }
1204 0 ▶ if (c->expected_cl > 0 && c->received_cl >= c->expected_cl)
Branch: 2 branches
BranchTrueFalseTotal
if (c->expected_cl > 0 && c->received_cl >= c->expected_cl) 0 0 0
if (c->expected_cl > 0 && c->received_cl >= c->expected_cl) 0 0 0
1205 0 finish_receiving(c);
1206 }
1207  
1208 0 tls_read_cleanup:
1209 664 feer_tls_rbuf_own(c);
1210 664 ▶ if (merged_late) Safefree(merged_late);
Branch: 1 branch
BranchTrueFalseTotal
if (merged_late) Safefree(merged_late); 0 664 664
1211 664 tls_http_input_eof(c);
1212 #ifdef FEERSUM_HAS_H2
1213 /* a partial or non-data record may never reach session_send */
1214 if (c->h2_session)
1215 h2_update_conn_idle(c);
1216 #endif
1217 664 SvREFCNT_dec(c->self);
1218 664 }
1219  
1220 /* With the read watcher stopped a close_notify can hide in pending ciphertext.
1221 * Buffers app data without parsing; returns 1 when the peer is gone. */
1222 static int
1223 43 feer_tls_decline_drain(struct feer_conn *c)
1224 {
1225 uint8_t rawbuf[TLS_RAW_BUFSZ];
1226 uint8_t scratch[TLS_DEC_SCRATCH];
1227 ssize_t n;
1228 do {
1229 43 n = recv(c->fd, rawbuf, sizeof(rawbuf), MSG_DONTWAIT);
1230 43 ▶ } while (n < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (n < 0 && errno == EINTR); 38 5 43
} while (n < 0 && errno == EINTR); 0 38 38
1231 43 ▶ if (n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) { 42 1 43
1232 /* FIN is left to the peer_is_gone peek */
1233 42 ▶ return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK;
Branch: 3 branches
BranchTrueFalseTotal
return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK; 38 4 42
return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK; 0 38 38
return n < 0 && errno != EAGAIN && errno != EWOULDBLOCK; 0 0 0
1234 }
1235  
1236 1 uint8_t *inbuf = rawbuf;
1237 1 size_t inlen = (size_t)n;
1238 1 uint8_t *merged = NULL;
1239 1 ▶ if (c->tls_rbuf_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_rbuf_len > 0) { 0 1 1
1240 0 inlen = c->tls_rbuf_len + (size_t)n;
1241 0 Newx(merged, inlen, uint8_t);
1242 0 memcpy(merged, c->tls_rbuf, c->tls_rbuf_len);
1243 0 memcpy(merged + c->tls_rbuf_len, rawbuf, (size_t)n);
1244 0 inbuf = merged;
1245 0 Safefree(c->tls_rbuf_base);
1246 0 c->tls_rbuf = c->tls_rbuf_base = NULL;
1247 0 c->tls_rbuf_len = 0;
1248 }
1249  
1250 trace("TLS decline drain fd=%d bytes=%"Sz_uf"\n", c->fd, (Sz)inlen);
1251 ptls_buffer_t decbuf;
1252 1 ptls_buffer_init(&decbuf, scratch, TLS_DEC_SCRATCH);
1253 1 size_t consumed = inlen;
1254 1 int ret = ptls_receive(c->tls, &decbuf, inbuf, &consumed);
1255 1 ▶ if (ret == 0 && consumed < inlen)
Branch: 2 branches
BranchTrueFalseTotal
if (ret == 0 && consumed < inlen) 0 1 1
if (ret == 0 && consumed < inlen) 0 0 0
1256 0 feer_tls_save_rbuf(c, inbuf + consumed, inlen - consumed);
1257 1 ▶ if (merged) Safefree(merged);
Branch: 1 branch
BranchTrueFalseTotal
if (merged) Safefree(merged); 0 1 1
1258  
1259 1 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 1 0 1
1260 1 feer_tls_decbuf_done(&decbuf);
1261 1 ▶ if (ret == PTLS_ERROR_CLASS_PEER_ALERT + PTLS_ALERT_CLOSE_NOTIFY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == PTLS_ERROR_CLASS_PEER_ALERT + PTLS_ALERT_CLOSE_NOTIFY) { 1 0 1
1262 1 tls_tunnel_note_input_eof(c);
1263 1 feersum_note_input_eof(c);
1264 1 return 0;
1265 }
1266 0 ▶ if (ret == PTLS_ERROR_IN_PROGRESS)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == PTLS_ERROR_IN_PROGRESS) 0 0 0
1267 0 return 0;
1268 0 feer_tls_note_alert(c, ret);
1269 0 return 1;
1270 }
1271 0 int drain_failed = 0;
1272 0 feer_tls_rbuf_append_drain(c, &decbuf, &drain_failed, scratch);
1273 0 ▶ if (drain_failed)
Branch: 1 branch
BranchTrueFalseTotal
if (drain_failed) 0 0 0
1274 0 return 1;
1275 /* The read watcher is stopped while parked, so the read path's rbuf cap
1276 * never runs here; enforce it or a declining streamer buffers unbounded. */
1277 0 ▶ if (unlikely(c->rbuf && SvCUR(c->rbuf) > c->cached_max_read_buf))
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->rbuf && SvCUR(c->rbuf) > c->cached_max_read_buf)) 0 0 0
if (unlikely(c->rbuf && SvCUR(c->rbuf) > c->cached_max_read_buf)) 0 0 0
1278 0 return 1;
1279 0 return 0;
1280 }
1281  
1282 /* 0: m done, 1: stopped at the window (progress kept in m), -1: error. gbuf
1283 * is emptied first: a body's DESTROY may write here and must queue after it */
1284 static int
1285 210 tls_encrypt_iomatrix(struct feer_conn *c, struct iomatrix *m)
1286 {
1287 uint8_t gbuf[TLS_GATHER_BUFSZ];
1288 210 size_t glen = 0;
1289 210 int ret = 0, full = 0;
1290 unsigned int i;
1291 433 ▶ for (i = m->offset; i < m->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = m->offset; i < m->count; i++) { 231 202 433
1292 231 size_t len = m->iov[i].iov_len;
1293 231 ▶ if (len == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) continue; 0 231 231
1294 231 ▶ if (c->tls_wbuf.off + glen >= FEER_TLS_ENCRYPT_AHEAD) { full = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off + glen >= FEER_TLS_ENCRYPT_AHEAD) { full = 1; break; } 0 231 231
1295  
1296 231 ▶ if (glen > 0 && (len >= sizeof(gbuf) || glen + len > sizeof(gbuf))) {
Branch: 3 branches
BranchTrueFalseTotal
if (glen > 0 && (len >= sizeof(gbuf) || glen + len > sizeof(gbuf))) { 20 211 231
if (glen > 0 && (len >= sizeof(gbuf) || glen + len > sizeof(gbuf))) { 18 2 20
if (glen > 0 && (len >= sizeof(gbuf) || glen + len > sizeof(gbuf))) { 0 18 18
1297 2 ret = ptls_send(c->tls, &c->tls_wbuf, gbuf, glen);
1298 2 glen = 0;
1299 2 ▶ if (ret != 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) break; 0 2 2
1300 }
1301 231 ▶ if (len >= sizeof(gbuf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= sizeof(gbuf)) { 48 183 231
1302 /* at least one full record, so a nearly full window adds no runt */
1303 96 size_t room = c->tls_wbuf.off < FEER_TLS_ENCRYPT_AHEAD
1304 48 ▶ ? FEER_TLS_ENCRYPT_AHEAD - c->tls_wbuf.off : 0;
Branch: 1 branch
BranchTrueFalseTotal
? FEER_TLS_ENCRYPT_AHEAD - c->tls_wbuf.off : 0; 48 0 48
1305 48 size_t piece = len;
1306 48 ▶ if (piece > room) piece = room < 16384 ? 16384 : room;
Branch: 1 branch
BranchTrueFalseTotal
if (piece > room) piece = room < 16384 ? 16384 : room; 8 40 48
1307 48 ▶ if (piece > len) piece = len;
Branch: 1 branch
BranchTrueFalseTotal
if (piece > len) piece = len; 0 48 48
1308 48 ret = ptls_send(c->tls, &c->tls_wbuf, m->iov[i].iov_base, piece);
1309 48 ▶ if (ret != 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) break; 0 48 48
1310 48 c->wbuf_len -= piece;
1311 48 ▶ if (piece < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (piece < len) { 8 40 48
1312 8 m->iov[i].iov_base = (char *)m->iov[i].iov_base + piece;
1313 8 m->iov[i].iov_len -= piece;
1314 8 full = 1;
1315 8 break;
1316 }
1317 40 continue;
1318 }
1319 183 c->wbuf_len -= len;
1320 183 memcpy(gbuf + glen, m->iov[i].iov_base, len);
1321 183 glen += len;
1322 }
1323 210 ▶ if (ret == 0 && glen > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ret == 0 && glen > 0) 210 0 210
if (ret == 0 && glen > 0) 163 47 210
1324 163 ret = ptls_send(c->tls, &c->tls_wbuf, gbuf, glen);
1325 210 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 210 210
1326 0 trouble("ptls_send error fd=%d ret=%d\n", c->fd, ret);
1327 0 return -1;
1328 }
1329 210 ▶ if (full) {
Branch: 1 branch
BranchTrueFalseTotal
if (full) { 8 202 210
1330 8 m->offset = i;
1331 8 return 1;
1332 }
1333 202 return 0;
1334 }
1335  
1336 static void
1337 520 try_tls_conn_write(EV_P_ ev_io *w, int revents)
1338 {
1339 520 struct feer_conn *c = (struct feer_conn *)w->data;
1340 PERL_UNUSED_VAR(revents);
1341 PERL_UNUSED_VAR(loop);
1342  
1343 dTHX;
1344 520 SvREFCNT_inc_void_NN(c->self);
1345 trace("tls_conn_write fd=%d\n", c->fd);
1346  
1347 520 ▶ if (unlikely(!c->tls)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(!c->tls)) { 0 520 520
1348 0 trouble("tls_conn_write: no TLS context fd=%d\n", c->fd);
1349 0 stop_write_watcher(c);
1350 0 goto tls_write_cleanup;
1351 }
1352  
1353 520 ▶ if (c->tls_wbuf.off > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0) { 9 511 520
1354 9 int flush_ret = feer_tls_flush_wbuf(c);
1355 9 ▶ if (flush_ret == -1) goto tls_write_again;
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret == -1) goto tls_write_again; 0 9 9
1356 9 ▶ if (flush_ret == -2) goto tls_write_error;
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret == -2) goto tls_write_error; 0 9 9
1357 /* Once the body is fully encrypted this is the only progress point;
1358 * without it write_timeout reaps a slow but steady reader */
1359 9 ▶ if (flush_ret > 0) restart_write_timer(c);
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret > 0) restart_write_timer(c); 9 0 9
1360 9 ▶ if (c->tls_wbuf.off > 0) goto tls_write_again;
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0) goto tls_write_again; 2 7 9
1361 }
1362  
1363 #ifdef FEERSUM_HAS_H2
1364 /* The last H2 stream closed during shutdown/EOF and its response has only now
1365 * reached the wire; linger so it is delivered before the close */
1366 if (unlikely(c->h2_close_pending) && c->tls_wbuf.off == 0) {
1367 c->h2_close_pending = 0;
1368 c->want_linger_close = 1;
1369 feer_shutdown_conn(c, "H2 final output drained");
1370 goto tls_write_cleanup;
1371 }
1372 #endif
1373  
1374 /* The app closed its tunnel end and the last ciphertext has now gone out. */
1375 518 ▶ if (unlikely(c->tls_tunnel_eof)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(c->tls_tunnel_eof)) { 0 518 518
1376 0 c->tls_tunnel_eof = 0;
1377 0 feer_shutdown_conn(c, "TLS tunnel EOF drained");
1378 0 goto tls_write_cleanup;
1379 }
1380  
1381 /* wire caught up: resume the tunnel reader paused at the backpressure cap */
1382 518 ▶ if (unlikely(c->tls_tunnel) && c->tls_tunnel_sv0 >= 0
Branch: 2 branches
BranchTrueFalseTotal
if (unlikely(c->tls_tunnel) && c->tls_tunnel_sv0 >= 0 0 518 518
if (unlikely(c->tls_tunnel) && c->tls_tunnel_sv0 >= 0 0 0 0
1383 0 ▶ && !ev_is_active(&c->tls_tunnel_read_w))
Branch: 1 branch
BranchTrueFalseTotal
&& !ev_is_active(&c->tls_tunnel_read_w)) 0 0 0
1384 0 ev_io_start(feersum_ev_loop, &c->tls_tunnel_read_w);
1385  
1386 #ifdef FEERSUM_HAS_H2
1387 if (c->h2_session) {
1388 h2_session_send_and_poll(aTHX_ c);
1389 if (c->tls_wbuf.off > 0) {
1390 int flush_ret = feer_tls_flush_wbuf(c);
1391 if (flush_ret == -1) goto tls_write_again;
1392 if (flush_ret == -2) goto tls_write_error;
1393 if (flush_ret > 0) restart_write_timer(c);
1394 }
1395 if (c->tls_wbuf.off == 0) {
1396 /* a pump stopped on its iteration budget: keep the watcher so the
1397 * next loop iteration re-enters it, nothing else would */
1398 if (unlikely(c->h2_pump_pending)) {
1399 c->h2_pump_pending = 0;
1400 }
1401 /* stop the deadline too, or an idle conn is reaped write_timeout
1402 * after its last byte */
1403 else {
1404 stop_write_watcher(c);
1405 stop_write_timer(c);
1406 }
1407 } else
1408 goto tls_write_again;
1409 h2_update_conn_idle(c);
1410 goto tls_write_cleanup;
1411 }
1412 #endif
1413  
1414 518 ▶ if (c->wbuf_rinq && c->cached_wbuf_low_water > 0
Branch: 2 branches
BranchTrueFalseTotal
if (c->wbuf_rinq && c->cached_wbuf_low_water > 0 206 312 518
if (c->wbuf_rinq && c->cached_wbuf_low_water > 0 1 205 206
1415 1 ▶ && c->wbuf_len <= c->cached_wbuf_low_water
Branch: 1 branch
BranchTrueFalseTotal
&& c->wbuf_len <= c->cached_wbuf_low_water 1 0 1
1416 1 ▶ && c->responding == RESPOND_STREAMING && c->poll_write_cb) {
Branch: 2 branches
BranchTrueFalseTotal
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 1 0 1
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 1 0 1
1417 1 ▶ if (c->poll_write_cb_is_io_handle)
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_write_cb_is_io_handle) 0 1 1
1418 0 pump_io_handle(c);
1419 else
1420 1 call_poll_callback(c, 1);
1421 }
1422  
1423 /* wbuf_rinq before sendfile: headers first */
1424 519 ▶ while (c->wbuf_rinq) {
Branch: 1 branch
BranchTrueFalseTotal
while (c->wbuf_rinq) { 207 312 519
1425 struct iomatrix *m;
1426 207 int window_full = 0;
1427 409 ▶ while ((m = c->wbuf_rinq ? (struct iomatrix *)c->wbuf_rinq->ref : NULL)) {
Branch: 2 branches
BranchTrueFalseTotal
while ((m = c->wbuf_rinq ? (struct iomatrix *)c->wbuf_rinq->ref : NULL)) { 210 199 409
while ((m = c->wbuf_rinq ? (struct iomatrix *)c->wbuf_rinq->ref : NULL)) { 210 199 409
1428 210 int ret = tls_encrypt_iomatrix(c, m);
1429 210 ▶ if (ret > 0) { window_full = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (ret > 0) { window_full = 1; break; } 8 202 210
1430 202 (void)rinq_shift(&c->wbuf_rinq);
1431 unsigned int i;
1432 425 ▶ for (i = 0; i < m->count; i++) SvREFCNT_dec(m->sv[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < m->count; i++) SvREFCNT_dec(m->sv[i]); 223 202 425
1433 202 ▶ IOMATRIX_FREE(m);
Branch: 1 branch
BranchTrueFalseTotal
IOMATRIX_FREE(m); 202 0 202
1434 202 ▶ if (ret < 0) goto tls_write_error;
Branch: 1 branch
BranchTrueFalseTotal
if (ret < 0) goto tls_write_error; 0 202 202
1435  
1436 /* Refill only once the wire has taken everything: an IO::Handle
1437 * body pumps to EOF, so refilling on an unflushed buffer piles the
1438 * whole encrypted file into tls_wbuf */
1439 202 ▶ if (c->cached_wbuf_low_water > 0
Branch: 1 branch
BranchTrueFalseTotal
if (c->cached_wbuf_low_water > 0 4 198 202
1440 4 ▶ && c->wbuf_len <= c->cached_wbuf_low_water
Branch: 1 branch
BranchTrueFalseTotal
&& c->wbuf_len <= c->cached_wbuf_low_water 4 0 4
1441 4 ▶ && c->responding == RESPOND_STREAMING && c->poll_write_cb) {
Branch: 2 branches
BranchTrueFalseTotal
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 3 1 4
&& c->responding == RESPOND_STREAMING && c->poll_write_cb) { 3 0 3
1442 3 int lw_flush = feer_tls_flush_wbuf(c);
1443 3 ▶ if (unlikely(lw_flush == -2)) goto tls_write_error;
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(lw_flush == -2)) goto tls_write_error; 0 3 3
1444 3 ▶ if (c->tls_wbuf.off == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off == 0) { 3 0 3
1445 3 ▶ if (c->poll_write_cb_is_io_handle)
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_write_cb_is_io_handle) 0 3 3
1446 0 pump_io_handle(c);
1447 else
1448 3 call_poll_callback(c, 1);
1449 }
1450 }
1451 }
1452  
1453 207 int flush_ret = feer_tls_flush_wbuf(c);
1454 207 ▶ if (flush_ret == -1) goto tls_write_again;
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret == -1) goto tls_write_again; 0 207 207
1455 207 ▶ if (flush_ret == -2) goto tls_write_error;
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret == -2) goto tls_write_error; 1 206 207
1456 206 ▶ if (flush_ret > 0) restart_write_timer(c);
Branch: 1 branch
BranchTrueFalseTotal
if (flush_ret > 0) restart_write_timer(c); 206 0 206
1457 /* tls_write_finished needs tls_wbuf empty; falling through with a
1458 * remainder leaves nothing armed to drain it */
1459 206 ▶ if (c->tls_wbuf.off > 0) goto tls_write_again;
Branch: 1 branch
BranchTrueFalseTotal
if (c->tls_wbuf.off > 0) goto tls_write_again; 7 199 206
1460 199 ▶ if (!window_full) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!window_full) break; 198 1 199
1461 }
1462  
1463 510 ▶ if (c->sendfile_fd >= 0 && c->sendfile_remain > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->sendfile_fd >= 0 && c->sendfile_remain > 0) { 276 234 510
if (c->sendfile_fd >= 0 && c->sendfile_remain > 0) { 0 276 276
1464 uint8_t filebuf[TLS_RAW_BUFSZ];
1465 276 size_t to_read = c->sendfile_remain;
1466 276 ▶ if (to_read > sizeof(filebuf)) to_read = sizeof(filebuf);
Branch: 1 branch
BranchTrueFalseTotal
if (to_read > sizeof(filebuf)) to_read = sizeof(filebuf); 274 2 276
1467  
1468 276 ssize_t file_nread = pread(c->sendfile_fd, filebuf, to_read, c->sendfile_off);
1469 276 ▶ if (file_nread < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_nread < 0) { 0 276 276
1470 0 ▶ if (errno == EINTR)
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) 0 0 0
1471 274 goto tls_write_again;
1472 0 trouble("TLS pread(sendfile_fd) fd=%d: %s\n", c->fd, strerror(errno));
1473 0 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 0 0
CLOSE_SENDFILE_FD(c); 0 0 0
1474 /* body truncated: the connection cannot be reused */
1475 0 c->is_keepalive = 0;
1476 0 change_responding_state(c, RESPOND_SHUTDOWN);
1477 2 goto tls_write_finished;
1478 }
1479 276 ▶ if (file_nread == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (file_nread == 0) { 0 276 276
1480 0 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 0 0
CLOSE_SENDFILE_FD(c); 0 0 0
1481 /* file shrank mid-transfer: body is short of the committed
1482 * Content-Length, so the connection cannot be reused */
1483 0 c->is_keepalive = 0;
1484 0 change_responding_state(c, RESPOND_SHUTDOWN);
1485 0 goto tls_write_finished;
1486 }
1487  
1488 276 int ret = ptls_send(c->tls, &c->tls_wbuf, filebuf, file_nread);
1489 276 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 276 276
1490 0 trouble("ptls_send(sendfile) error fd=%d ret=%d\n", c->fd, ret);
1491 0 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 0 0 0
CLOSE_SENDFILE_FD(c); 0 0 0
1492 0 goto tls_write_error;
1493 }
1494  
1495 276 c->sendfile_off += file_nread;
1496 276 c->sendfile_remain -= file_nread;
1497  
1498 276 ▶ if (c->sendfile_remain == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->sendfile_remain == 0) { 2 274 276
1499 2 ▶ CLOSE_SENDFILE_FD(c);
Branch: 2 branches
BranchTrueFalseTotal
CLOSE_SENDFILE_FD(c); 2 0 2
CLOSE_SENDFILE_FD(c); 0 2 2
1500 }
1501  
1502 {
1503 276 int sf_flush_ret = feer_tls_flush_wbuf(c);
1504 276 ▶ if (sf_flush_ret == -1) goto tls_write_again;
Branch: 1 branch
BranchTrueFalseTotal
if (sf_flush_ret == -1) goto tls_write_again; 0 276 276
1505 276 ▶ if (sf_flush_ret == -2) goto tls_write_error;
Branch: 1 branch
BranchTrueFalseTotal
if (sf_flush_ret == -2) goto tls_write_error; 0 276 276
1506 }
1507 /* without this write_timeout reaps a long sendfile mid-file */
1508 276 ▶ if (file_nread > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (file_nread > 0) 276 0 276
1509 276 restart_write_timer(c);
1510 276 ▶ if (c->sendfile_remain > 0 || c->tls_wbuf.off > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (c->sendfile_remain > 0 || c->tls_wbuf.off > 0) 2 274 276
if (c->sendfile_remain > 0 || c->tls_wbuf.off > 0) 0 2 2
1511 274 goto tls_write_again;
1512 2 goto tls_write_finished;
1513 }
1514  
1515 234 tls_write_finished:
1516 236 ▶ if ((!c->wbuf_rinq || (c->cached_wbuf_low_water > 0
Branch: 2 branches
BranchTrueFalseTotal
if ((!c->wbuf_rinq || (c->cached_wbuf_low_water > 0 0 236 236
if ((!c->wbuf_rinq || (c->cached_wbuf_low_water > 0 0 0 0
1517 0 ▶ && c->wbuf_len <= c->cached_wbuf_low_water))
Branch: 1 branch
BranchTrueFalseTotal
&& c->wbuf_len <= c->cached_wbuf_low_water)) 0 0 0
1518 236 ▶ && c->sendfile_fd < 0 && c->tls_wbuf.off == 0) {
Branch: 2 branches
BranchTrueFalseTotal
&& c->sendfile_fd < 0 && c->tls_wbuf.off == 0) { 236 0 236
&& c->sendfile_fd < 0 && c->tls_wbuf.off == 0) { 236 0 236
1519 236 ▶ if (c->responding == RESPOND_SHUTDOWN || c->responding == RESPOND_NORMAL) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->responding == RESPOND_SHUTDOWN || c->responding == RESPOND_NORMAL) { 79 157 236
if (c->responding == RESPOND_SHUTDOWN || c->responding == RESPOND_NORMAL) { 0 79 79
1520 157 handle_keepalive_or_close(c, try_tls_conn_read);
1521 153 ▶ } else if (c->responding == RESPOND_STREAMING && c->poll_write_cb) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (c->responding == RESPOND_STREAMING && c->poll_write_cb) { 79 0 79
} else if (c->responding == RESPOND_STREAMING && c->poll_write_cb) { 78 1 79
1522 78 unsigned int writes_before = c->poll_writes_seen;
1523 78 ▶ if (c->poll_write_cb_is_io_handle)
Branch: 1 branch
BranchTrueFalseTotal
if (c->poll_write_cb_is_io_handle) 0 78 78
1524 0 pump_io_handle(c);
1525 else
1526 78 call_poll_callback(c, 1 /* is_write */);
1527 /* pure decline: park and pace, as try_conn_write does */
1528 78 ▶ if (!c->wbuf_rinq && c->tls_wbuf.off == 0
Branch: 2 branches
BranchTrueFalseTotal
if (!c->wbuf_rinq && c->tls_wbuf.off == 0 43 35 78
if (!c->wbuf_rinq && c->tls_wbuf.off == 0 43 0 43
1529 43 ▶ && c->responding == RESPOND_STREAMING && c->poll_write_cb
Branch: 2 branches
BranchTrueFalseTotal
&& c->responding == RESPOND_STREAMING && c->poll_write_cb 43 0 43
&& c->responding == RESPOND_STREAMING && c->poll_write_cb 43 0 43
1530 82 ▶ && c->poll_writes_seen == writes_before) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->poll_writes_seen == writes_before) { 43 0 43
1531 /* drain first so a pending close_notify latches before the
1532 * peek and the EOF bound; tunnel/takeover input is the app's */
1533 43 ▶ bool derr = !c->tls_tunnel && !c->io_taken
Branch: 1 branch
BranchTrueFalseTotal
bool derr = !c->tls_tunnel && !c->io_taken 43 0 43
1534 86 ▶ && feer_tls_decline_drain(c);
Branch: 2 branches
BranchTrueFalseTotal
&& feer_tls_decline_drain(c); 43 0 43
&& feer_tls_decline_drain(c); 0 43 43
1535 43 bool gone = peer_is_gone(c);
1536 43 feersum_maybe_fire_on_eof(c);
1537 43 ▶ if (unlikely(gone)) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlikely(gone)) { 0 43 43
1538 trace("TLS poll_cb declined and the peer is gone %d\n", c->fd);
1539 0 feer_shutdown_conn(c, "poll_cb decline, peer gone");
1540 }
1541 43 ▶ else if (unlikely(derr)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (unlikely(derr)) { 0 43 43
1542 trace("TLS input error at poll_cb decline %d\n", c->fd);
1543 0 feer_shutdown_conn(c, "TLS input error at decline");
1544 }
1545 43 ▶ else if (unlikely(feer_eof_park_expired(c))) {
Branch: 1 branch
BranchTrueFalseTotal
else if (unlikely(feer_eof_park_expired(c))) { 1 42 43
1546 trace("TLS poll_cb declined and the EOF park expired %d\n", c->fd);
1547 1 feer_shutdown_conn(c, "poll_cb decline, EOF park expired");
1548 }
1549 42 ▶ else if (c->responding == RESPOND_STREAMING && c->poll_write_cb
Branch: 2 branches
BranchTrueFalseTotal
else if (c->responding == RESPOND_STREAMING && c->poll_write_cb 38 4 42
else if (c->responding == RESPOND_STREAMING && c->poll_write_cb 38 0 38
1550 38 ▶ && c->poll_writes_seen == writes_before) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->poll_writes_seen == writes_before) { 38 0 38
1551 38 feersum_poll_retry_park(c);
1552 }
1553 else {
1554 /* on_eof (also fired by the drain) may have finished,
1555 * paused or fed the response: re-enter, do not park */
1556 4 goto tls_write_again;
1557 }
1558 }
1559 else {
1560 /* a paced-retry entry has no watcher armed, and pump_io_handle
1561 * buffers without arming one */
1562 35 start_write_watcher(c);
1563 }
1564 1 ▶ } else if (c->responding == RESPOND_STREAMING) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->responding == RESPOND_STREAMING) { 1 0 1
1565 1 stop_write_watcher(c);
1566 1 stop_write_timer(c);
1567 0 ▶ } else if (c->responding == RESPOND_NOT_STARTED) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->responding == RESPOND_NOT_STARTED) { 0 0 0
1568 /* tunnel or handshake flush drained; stop the deadline too, or an
1569 * established tunnel is reaped write_timeout after its last byte */
1570 0 stop_write_watcher(c);
1571 0 stop_write_timer(c);
1572 }
1573 }
1574 232 goto tls_write_cleanup;
1575  
1576 287 tls_write_again:
1577 /* conn_write_ready() also enters here directly, so the watcher is not
1578 * necessarily armed */
1579 287 start_write_watcher(c);
1580 287 ensure_write_timer(c);
1581 287 goto tls_write_cleanup;
1582  
1583 1 tls_write_error:
1584 1 feer_shutdown_conn(c, "TLS write error");
1585  
1586 520 tls_write_cleanup:
1587 520 SvREFCNT_dec(c->self);
1588 520 }
1589  
1590 static int
1591 23 feer_tls_send(struct feer_conn *c, const void *data, size_t len)
1592 {
1593 23 ▶ if (!c->tls || len == 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!c->tls || len == 0) return 0; 23 0 23
if (!c->tls || len == 0) return 0; 0 23 23
1594  
1595 23 int ret = ptls_send(c->tls, &c->tls_wbuf, data, len);
1596 23 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 23 23
1597 0 trouble("feer_tls_send error fd=%d ret=%d\n", c->fd, ret);
1598 0 return -1;
1599 }
1600 23 return 0;
1601 }
1602  
1603 #endif /* FEERSUM_HAS_TLS */
« picohttpparser-git/picohttpparser.c ↑ Summary feersum_conn.xs »