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