Lines Matching refs:bev_ssl
367 put_error(struct bufferevent_openssl *bev_ssl, unsigned long err) in put_error() argument
369 if (bev_ssl->n_errors == NUM_ERRORS) in put_error()
376 bev_ssl->errors[bev_ssl->n_errors++] = (ev_uint32_t) err; in put_error()
383 start_reading(struct bufferevent_openssl *bev_ssl) in start_reading() argument
385 if (bev_ssl->underlying) { in start_reading()
386 bufferevent_unsuspend_read_(bev_ssl->underlying, in start_reading()
390 struct bufferevent *bev = &bev_ssl->bev.bev; in start_reading()
393 if (r == 0 && bev_ssl->read_blocked_on_write) in start_reading()
404 start_writing(struct bufferevent_openssl *bev_ssl) in start_writing() argument
407 if (bev_ssl->underlying) { in start_writing()
408 if (bev_ssl->write_blocked_on_read) { in start_writing()
409 bufferevent_unsuspend_read_(bev_ssl->underlying, in start_writing()
413 struct bufferevent *bev = &bev_ssl->bev.bev; in start_writing()
415 if (!r && bev_ssl->write_blocked_on_read) in start_writing()
423 stop_reading(struct bufferevent_openssl *bev_ssl) in stop_reading() argument
425 if (bev_ssl->write_blocked_on_read) in stop_reading()
427 if (bev_ssl->underlying) { in stop_reading()
428 bufferevent_suspend_read_(bev_ssl->underlying, in stop_reading()
431 struct bufferevent *bev = &bev_ssl->bev.bev; in stop_reading()
437 stop_writing(struct bufferevent_openssl *bev_ssl) in stop_writing() argument
439 if (bev_ssl->read_blocked_on_write) in stop_writing()
441 if (bev_ssl->underlying) { in stop_writing()
442 bufferevent_unsuspend_read_(bev_ssl->underlying, in stop_writing()
445 struct bufferevent *bev = &bev_ssl->bev.bev; in stop_writing()
451 set_rbow(struct bufferevent_openssl *bev_ssl) in set_rbow() argument
453 if (!bev_ssl->underlying) in set_rbow()
454 stop_reading(bev_ssl); in set_rbow()
455 bev_ssl->read_blocked_on_write = 1; in set_rbow()
456 return start_writing(bev_ssl); in set_rbow()
460 set_wbor(struct bufferevent_openssl *bev_ssl) in set_wbor() argument
462 if (!bev_ssl->underlying) in set_wbor()
463 stop_writing(bev_ssl); in set_wbor()
464 bev_ssl->write_blocked_on_read = 1; in set_wbor()
465 return start_reading(bev_ssl); in set_wbor()
469 clear_rbow(struct bufferevent_openssl *bev_ssl) in clear_rbow() argument
471 struct bufferevent *bev = &bev_ssl->bev.bev; in clear_rbow()
473 bev_ssl->read_blocked_on_write = 0; in clear_rbow()
475 stop_writing(bev_ssl); in clear_rbow()
477 r = start_reading(bev_ssl); in clear_rbow()
483 clear_wbor(struct bufferevent_openssl *bev_ssl) in clear_wbor() argument
485 struct bufferevent *bev = &bev_ssl->bev.bev; in clear_wbor()
487 bev_ssl->write_blocked_on_read = 0; in clear_wbor()
489 stop_reading(bev_ssl); in clear_wbor()
491 r = start_writing(bev_ssl); in clear_wbor()
496 conn_closed(struct bufferevent_openssl *bev_ssl, int when, int errcode, int ret) in conn_closed() argument
505 if (SSL_get_shutdown(bev_ssl->ssl) & SSL_RECEIVED_SHUTDOWN) in conn_closed()
514 put_error(bev_ssl, errcode); in conn_closed()
518 put_error(bev_ssl, errcode); in conn_closed()
522 put_error(bev_ssl, errcode); in conn_closed()
536 put_error(bev_ssl, err); in conn_closed()
539 if (dirty_shutdown && bev_ssl->allow_dirty_shutdown) in conn_closed()
542 stop_reading(bev_ssl); in conn_closed()
543 stop_writing(bev_ssl); in conn_closed()
547 bufferevent_run_eventcb_(&bev_ssl->bev.bev, event, 0); in conn_closed()
551 init_bio_counts(struct bufferevent_openssl *bev_ssl) in init_bio_counts() argument
555 wbio = SSL_get_wbio(bev_ssl->ssl); in init_bio_counts()
556 bev_ssl->counts.n_written = wbio ? BIO_number_written(wbio) : 0; in init_bio_counts()
557 rbio = SSL_get_rbio(bev_ssl->ssl); in init_bio_counts()
558 bev_ssl->counts.n_read = rbio ? BIO_number_read(rbio) : 0; in init_bio_counts()
562 decrement_buckets(struct bufferevent_openssl *bev_ssl) in decrement_buckets() argument
564 unsigned long num_w = BIO_number_written(SSL_get_wbio(bev_ssl->ssl)); in decrement_buckets()
565 unsigned long num_r = BIO_number_read(SSL_get_rbio(bev_ssl->ssl)); in decrement_buckets()
567 unsigned long w = num_w - bev_ssl->counts.n_written; in decrement_buckets()
568 unsigned long r = num_r - bev_ssl->counts.n_read; in decrement_buckets()
570 bufferevent_decrement_write_buckets_(&bev_ssl->bev, w); in decrement_buckets()
572 bufferevent_decrement_read_buckets_(&bev_ssl->bev, r); in decrement_buckets()
573 bev_ssl->counts.n_written = num_w; in decrement_buckets()
574 bev_ssl->counts.n_read = num_r; in decrement_buckets()
584 do_read(struct bufferevent_openssl *bev_ssl, int n_to_read) { in do_read() argument
586 struct bufferevent *bev = &bev_ssl->bev.bev; in do_read()
592 if (bev_ssl->bev.read_suspended) in do_read()
595 atmost = bufferevent_get_read_max_(&bev_ssl->bev); in do_read()
604 if (bev_ssl->bev.read_suspended) in do_read()
607 r = SSL_read(bev_ssl->ssl, space[i].iov_base, space[i].iov_len); in do_read()
610 if (bev_ssl->read_blocked_on_write) in do_read()
611 if (clear_rbow(bev_ssl) < 0) in do_read()
615 decrement_buckets(bev_ssl); in do_read()
617 int err = SSL_get_error(bev_ssl->ssl, r); in do_read()
622 if (bev_ssl->read_blocked_on_write) in do_read()
623 if (clear_rbow(bev_ssl) < 0) in do_read()
629 if (!bev_ssl->read_blocked_on_write) in do_read()
630 if (set_rbow(bev_ssl) < 0) in do_read()
634 conn_closed(bev_ssl, BEV_EVENT_READING, err, r); in do_read()
644 if (bev_ssl->underlying) in do_read()
654 do_write(struct bufferevent_openssl *bev_ssl, int atmost) in do_write() argument
657 struct bufferevent *bev = &bev_ssl->bev.bev; in do_write()
662 if (bev_ssl->last_write > 0) in do_write()
663 atmost = bev_ssl->last_write; in do_write()
665 atmost = bufferevent_get_write_max_(&bev_ssl->bev); in do_write()
674 if (bev_ssl->bev.write_suspended) in do_write()
684 r = SSL_write(bev_ssl->ssl, space[i].iov_base, in do_write()
688 if (bev_ssl->write_blocked_on_read) in do_write()
689 if (clear_wbor(bev_ssl) < 0) in do_write()
692 bev_ssl->last_write = -1; in do_write()
693 decrement_buckets(bev_ssl); in do_write()
695 int err = SSL_get_error(bev_ssl->ssl, r); in do_write()
700 if (bev_ssl->write_blocked_on_read) in do_write()
701 if (clear_wbor(bev_ssl) < 0) in do_write()
703 bev_ssl->last_write = space[i].iov_len; in do_write()
708 if (!bev_ssl->write_blocked_on_read) in do_write()
709 if (set_wbor(bev_ssl) < 0) in do_write()
711 bev_ssl->last_write = space[i].iov_len; in do_write()
714 conn_closed(bev_ssl, BEV_EVENT_WRITING, err, r); in do_write()
715 bev_ssl->last_write = -1; in do_write()
724 if (bev_ssl->underlying) in do_write()
785 consider_reading(struct bufferevent_openssl *bev_ssl) in consider_reading() argument
791 while (bev_ssl->write_blocked_on_read) { in consider_reading()
792 r = do_write(bev_ssl, WRITE_FRAME); in consider_reading()
796 if (bev_ssl->write_blocked_on_read) in consider_reading()
799 n_to_read = bytes_to_read(bev_ssl); in consider_reading()
802 r = do_read(bev_ssl, n_to_read); in consider_reading()
808 if (bev_ssl->bev.read_suspended) in consider_reading()
819 n_to_read = SSL_pending(bev_ssl->ssl); in consider_reading()
835 if (!n_to_read && bev_ssl->underlying) in consider_reading()
836 n_to_read = bytes_to_read(bev_ssl); in consider_reading()
840 struct bufferevent *bev = &bev_ssl->bev.bev; in consider_reading()
845 if (!bev_ssl->underlying) { in consider_reading()
847 if (bev_ssl->bev.read_suspended || in consider_reading()
848 !(bev_ssl->bev.bev.enabled & EV_READ)) { in consider_reading()
849 event_del(&bev_ssl->bev.bev.ev_read); in consider_reading()
855 consider_writing(struct bufferevent_openssl *bev_ssl) in consider_writing() argument
858 struct evbuffer *output = bev_ssl->bev.bev.output; in consider_writing()
862 while (bev_ssl->read_blocked_on_write) { in consider_writing()
863 r = do_read(bev_ssl, 1024); /* XXXX 1024 is a hack */ in consider_writing()
865 struct bufferevent *bev = &bev_ssl->bev.bev; in consider_writing()
872 if (bev_ssl->read_blocked_on_write) in consider_writing()
874 if (bev_ssl->underlying) { in consider_writing()
875 target = bev_ssl->underlying->output; in consider_writing()
876 wm = &bev_ssl->underlying->wm_write; in consider_writing()
878 while ((bev_ssl->bev.bev.enabled & EV_WRITE) && in consider_writing()
879 (! bev_ssl->bev.write_suspended) && in consider_writing()
887 r = do_write(bev_ssl, n_to_write); in consider_writing()
892 if (!bev_ssl->underlying) { in consider_writing()
894 event_del(&bev_ssl->bev.bev.ev_write); in consider_writing()
895 } else if (bev_ssl->bev.write_suspended || in consider_writing()
896 !(bev_ssl->bev.bev.enabled & EV_WRITE)) { in consider_writing()
898 event_del(&bev_ssl->bev.bev.ev_write); in consider_writing()
906 struct bufferevent_openssl *bev_ssl = ctx; in be_openssl_readcb() local
907 consider_reading(bev_ssl); in be_openssl_readcb()
913 struct bufferevent_openssl *bev_ssl = ctx; in be_openssl_writecb() local
914 consider_writing(bev_ssl); in be_openssl_writecb()
920 struct bufferevent_openssl *bev_ssl = ctx; in be_openssl_eventcb() local
924 if (bev_ssl->allow_dirty_shutdown) in be_openssl_eventcb()
939 bufferevent_run_eventcb_(&bev_ssl->bev.bev, event, 0); in be_openssl_eventcb()
945 struct bufferevent_openssl *bev_ssl = ptr; in be_openssl_readeventcb() local
946 bufferevent_incref_and_lock_(&bev_ssl->bev.bev); in be_openssl_readeventcb()
948 bufferevent_run_eventcb_(&bev_ssl->bev.bev, in be_openssl_readeventcb()
951 consider_reading(bev_ssl); in be_openssl_readeventcb()
953 bufferevent_decref_and_unlock_(&bev_ssl->bev.bev); in be_openssl_readeventcb()
959 struct bufferevent_openssl *bev_ssl = ptr; in be_openssl_writeeventcb() local
960 bufferevent_incref_and_lock_(&bev_ssl->bev.bev); in be_openssl_writeeventcb()
962 bufferevent_run_eventcb_(&bev_ssl->bev.bev, in be_openssl_writeeventcb()
965 consider_writing(bev_ssl); in be_openssl_writeeventcb()
967 bufferevent_decref_and_unlock_(&bev_ssl->bev.bev); in be_openssl_writeeventcb()
971 be_openssl_auto_fd(struct bufferevent_openssl *bev_ssl, evutil_socket_t fd) in be_openssl_auto_fd() argument
973 if (!bev_ssl->underlying) { in be_openssl_auto_fd()
974 struct bufferevent *bev = &bev_ssl->bev.bev; in be_openssl_auto_fd()
983 set_open_callbacks(struct bufferevent_openssl *bev_ssl, evutil_socket_t fd) in set_open_callbacks() argument
985 if (bev_ssl->underlying) { in set_open_callbacks()
986 bufferevent_setcb(bev_ssl->underlying, in set_open_callbacks()
988 bev_ssl); in set_open_callbacks()
991 struct bufferevent *bev = &bev_ssl->bev.bev; in set_open_callbacks()
1004 be_openssl_readeventcb, bev_ssl); in set_open_callbacks()
1007 be_openssl_writeeventcb, bev_ssl); in set_open_callbacks()
1019 do_handshake(struct bufferevent_openssl *bev_ssl) in do_handshake() argument
1023 switch (bev_ssl->state) { in do_handshake()
1031 r = SSL_do_handshake(bev_ssl->ssl); in do_handshake()
1034 decrement_buckets(bev_ssl); in do_handshake()
1037 evutil_socket_t fd = event_get_fd(&bev_ssl->bev.bev.ev_read); in do_handshake()
1039 bev_ssl->state = BUFFEREVENT_SSL_OPEN; in do_handshake()
1040 set_open_callbacks(bev_ssl, fd); /* XXXX handle failure */ in do_handshake()
1042 bufferevent_enable(&bev_ssl->bev.bev, bev_ssl->bev.bev.enabled); in do_handshake()
1043 bufferevent_run_eventcb_(&bev_ssl->bev.bev, in do_handshake()
1047 int err = SSL_get_error(bev_ssl->ssl, r); in do_handshake()
1051 stop_reading(bev_ssl); in do_handshake()
1052 return start_writing(bev_ssl); in do_handshake()
1054 stop_writing(bev_ssl); in do_handshake()
1055 return start_reading(bev_ssl); in do_handshake()
1057 conn_closed(bev_ssl, BEV_EVENT_READING, err, r); in do_handshake()
1066 struct bufferevent_openssl *bev_ssl = ctx; in be_openssl_handshakecb() local
1067 do_handshake(bev_ssl);/* XXX handle failure */ in be_openssl_handshakecb()
1073 struct bufferevent_openssl *bev_ssl = ptr; in be_openssl_handshakeeventcb() local
1075 bufferevent_incref_and_lock_(&bev_ssl->bev.bev); in be_openssl_handshakeeventcb()
1077 bufferevent_run_eventcb_(&bev_ssl->bev.bev, BEV_EVENT_TIMEOUT, 0); in be_openssl_handshakeeventcb()
1079 do_handshake(bev_ssl);/* XXX handle failure */ in be_openssl_handshakeeventcb()
1080 bufferevent_decref_and_unlock_(&bev_ssl->bev.bev); in be_openssl_handshakeeventcb()
1084 set_handshake_callbacks(struct bufferevent_openssl *bev_ssl, evutil_socket_t fd) in set_handshake_callbacks() argument
1086 if (bev_ssl->underlying) { in set_handshake_callbacks()
1087 bufferevent_setcb(bev_ssl->underlying, in set_handshake_callbacks()
1090 bev_ssl); in set_handshake_callbacks()
1095 if (bufferevent_setfd(bev_ssl->underlying, fd)) in set_handshake_callbacks()
1098 return do_handshake(bev_ssl); in set_handshake_callbacks()
1100 struct bufferevent *bev = &bev_ssl->bev.bev; in set_handshake_callbacks()
1109 be_openssl_handshakeeventcb, bev_ssl); in set_handshake_callbacks()
1112 be_openssl_handshakeeventcb, bev_ssl); in set_handshake_callbacks()
1122 struct bufferevent_openssl *bev_ssl = upcast(bev); in bufferevent_ssl_renegotiate() local
1123 if (!bev_ssl) in bufferevent_ssl_renegotiate()
1125 if (SSL_renegotiate(bev_ssl->ssl) < 0) in bufferevent_ssl_renegotiate()
1127 bev_ssl->state = BUFFEREVENT_SSL_CONNECTING; in bufferevent_ssl_renegotiate()
1128 if (set_handshake_callbacks(bev_ssl, be_openssl_auto_fd(bev_ssl, -1)) < 0) in bufferevent_ssl_renegotiate()
1130 if (!bev_ssl->underlying) in bufferevent_ssl_renegotiate()
1131 return do_handshake(bev_ssl); in bufferevent_ssl_renegotiate()
1139 struct bufferevent_openssl *bev_ssl = arg; in be_openssl_outbuf_cb() local
1143 if (cbinfo->n_added && bev_ssl->state == BUFFEREVENT_SSL_OPEN) { in be_openssl_outbuf_cb()
1145 r = bufferevent_add_event_(&bev_ssl->bev.bev.ev_write, in be_openssl_outbuf_cb()
1146 &bev_ssl->bev.bev.timeout_write); in be_openssl_outbuf_cb()
1148 if (bev_ssl->underlying) in be_openssl_outbuf_cb()
1149 consider_writing(bev_ssl); in be_openssl_outbuf_cb()
1159 struct bufferevent_openssl *bev_ssl = upcast(bev); in be_openssl_enable() local
1163 r1 = start_reading(bev_ssl); in be_openssl_enable()
1165 r2 = start_writing(bev_ssl); in be_openssl_enable()
1167 if (bev_ssl->underlying) { in be_openssl_enable()
1174 consider_reading(bev_ssl); in be_openssl_enable()
1176 consider_writing(bev_ssl); in be_openssl_enable()
1184 struct bufferevent_openssl *bev_ssl = upcast(bev); in be_openssl_disable() local
1187 stop_reading(bev_ssl); in be_openssl_disable()
1189 stop_writing(bev_ssl); in be_openssl_disable()
1191 if (bev_ssl->underlying) { in be_openssl_disable()
1203 struct bufferevent_openssl *bev_ssl = upcast(bev); in be_openssl_unlink() local
1205 if (bev_ssl->bev.options & BEV_OPT_CLOSE_ON_FREE) { in be_openssl_unlink()
1206 if (bev_ssl->underlying) { in be_openssl_unlink()
1207 if (BEV_UPCAST(bev_ssl->underlying)->refcnt < 2) { in be_openssl_unlink()
1211 bufferevent_free(bev_ssl->underlying); in be_openssl_unlink()
1218 if (bev_ssl->underlying) { in be_openssl_unlink()
1219 if (bev_ssl->underlying->errorcb == be_openssl_eventcb) in be_openssl_unlink()
1220 bufferevent_setcb(bev_ssl->underlying, in be_openssl_unlink()
1222 bufferevent_unsuspend_read_(bev_ssl->underlying, in be_openssl_unlink()
1231 struct bufferevent_openssl *bev_ssl = upcast(bev); in be_openssl_destruct() local
1233 if (bev_ssl->bev.options & BEV_OPT_CLOSE_ON_FREE) { in be_openssl_destruct()
1234 if (! bev_ssl->underlying) { in be_openssl_destruct()
1236 BIO *bio = SSL_get_wbio(bev_ssl->ssl); in be_openssl_destruct()
1242 SSL_free(bev_ssl->ssl); in be_openssl_destruct()
1249 struct bufferevent_openssl *bev_ssl = upcast(bev); in be_openssl_adj_timeouts() local
1251 if (bev_ssl->underlying) { in be_openssl_adj_timeouts()
1267 be_openssl_set_fd(struct bufferevent_openssl *bev_ssl, in be_openssl_set_fd() argument
1270 bev_ssl->state = state; in be_openssl_set_fd()
1274 if (!SSL_clear(bev_ssl->ssl)) in be_openssl_set_fd()
1276 SSL_set_accept_state(bev_ssl->ssl); in be_openssl_set_fd()
1277 if (set_handshake_callbacks(bev_ssl, fd) < 0) in be_openssl_set_fd()
1281 if (!SSL_clear(bev_ssl->ssl)) in be_openssl_set_fd()
1283 SSL_set_connect_state(bev_ssl->ssl); in be_openssl_set_fd()
1284 if (set_handshake_callbacks(bev_ssl, fd) < 0) in be_openssl_set_fd()
1288 if (set_open_callbacks(bev_ssl, fd) < 0) in be_openssl_set_fd()
1302 struct bufferevent_openssl *bev_ssl = upcast(bev); in be_openssl_ctrl() local
1305 if (!bev_ssl->underlying) { in be_openssl_ctrl()
1308 SSL_set_bio(bev_ssl->ssl, bio, bio); in be_openssl_ctrl()
1311 if (!(bio = BIO_new_bufferevent(bev_ssl->underlying))) in be_openssl_ctrl()
1313 SSL_set_bio(bev_ssl->ssl, bio, bio); in be_openssl_ctrl()
1316 return be_openssl_set_fd(bev_ssl, bev_ssl->old_state, data->fd); in be_openssl_ctrl()
1318 if (bev_ssl->underlying) { in be_openssl_ctrl()
1319 data->fd = event_get_fd(&bev_ssl->underlying->ev_read); in be_openssl_ctrl()
1325 data->ptr = bev_ssl->underlying; in be_openssl_ctrl()
1336 struct bufferevent_openssl *bev_ssl = upcast(bufev); in bufferevent_openssl_get_ssl() local
1337 if (!bev_ssl) in bufferevent_openssl_get_ssl()
1339 return bev_ssl->ssl; in bufferevent_openssl_get_ssl()
1350 struct bufferevent_openssl *bev_ssl = NULL; in bufferevent_openssl_new_impl() local
1358 if (!(bev_ssl = mm_calloc(1, sizeof(struct bufferevent_openssl)))) in bufferevent_openssl_new_impl()
1361 bev_p = &bev_ssl->bev; in bufferevent_openssl_new_impl()
1371 bev_ssl->underlying = underlying; in bufferevent_openssl_new_impl()
1372 bev_ssl->ssl = ssl; in bufferevent_openssl_new_impl()
1374 bev_ssl->outbuf_cb = evbuffer_add_cb(bev_p->bev.output, in bufferevent_openssl_new_impl()
1375 be_openssl_outbuf_cb, bev_ssl); in bufferevent_openssl_new_impl()
1378 bufferevent_enable_locking_(&bev_ssl->bev.bev, NULL); in bufferevent_openssl_new_impl()
1381 bufferevent_init_generic_timeout_cbs_(&bev_ssl->bev.bev); in bufferevent_openssl_new_impl()
1385 bev_ssl->old_state = state; in bufferevent_openssl_new_impl()
1386 bev_ssl->last_write = -1; in bufferevent_openssl_new_impl()
1388 init_bio_counts(bev_ssl); in bufferevent_openssl_new_impl()
1390 fd = be_openssl_auto_fd(bev_ssl, fd); in bufferevent_openssl_new_impl()
1391 if (be_openssl_set_fd(bev_ssl, state, fd)) in bufferevent_openssl_new_impl()
1402 return &bev_ssl->bev.bev; in bufferevent_openssl_new_impl()
1406 if (bev_ssl) { in bufferevent_openssl_new_impl()
1407 bev_ssl->ssl = NULL; in bufferevent_openssl_new_impl()
1408 bufferevent_free(&bev_ssl->bev.bev); in bufferevent_openssl_new_impl()
1491 struct bufferevent_openssl *bev_ssl; in bufferevent_openssl_get_allow_dirty_shutdown() local
1493 bev_ssl = upcast(bev); in bufferevent_openssl_get_allow_dirty_shutdown()
1494 if (bev_ssl) in bufferevent_openssl_get_allow_dirty_shutdown()
1495 allow_dirty_shutdown = bev_ssl->allow_dirty_shutdown; in bufferevent_openssl_get_allow_dirty_shutdown()
1504 struct bufferevent_openssl *bev_ssl; in bufferevent_openssl_set_allow_dirty_shutdown() local
1506 bev_ssl = upcast(bev); in bufferevent_openssl_set_allow_dirty_shutdown()
1507 if (bev_ssl) in bufferevent_openssl_set_allow_dirty_shutdown()
1508 bev_ssl->allow_dirty_shutdown = !!allow_dirty_shutdown; in bufferevent_openssl_set_allow_dirty_shutdown()
1516 struct bufferevent_openssl *bev_ssl; in bufferevent_get_openssl_error() local
1518 bev_ssl = upcast(bev); in bufferevent_get_openssl_error()
1519 if (bev_ssl && bev_ssl->n_errors) { in bufferevent_get_openssl_error()
1520 err = bev_ssl->errors[--bev_ssl->n_errors]; in bufferevent_get_openssl_error()