Guest User

Untitled

a guest
Dec 10th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. int ensure_connection_intialized(tls_uv_connection_state_t* state) {
  2. if (state->flags & CONNECTION_STATUS_INIT_DONE)
  3. return 1;
  4.  
  5. if (SSL_is_init_finished(state->ssl)) {
  6. state->flags |= CONNECTION_STATUS_INIT_DONE;
  7. if (validate_connection_certificate(state) == 0) {
  8. state->flags |= CONNECTION_STATUS_WRITE_AND_ABORT;
  9. return 0;
  10. }
  11. return connection_write(state, "OK\r\n", 4);
  12. }
  13.  
  14. return 1;
  15. }
  16.  
  17. void handle_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
  18. tls_uv_connection_state_t* state = client->data;
  19. if (nread <= 0) {
  20. push_libuv_error(nread, "Unable to read");
  21. state->server->options.handler->connection_error(state);
  22. abort_connection_on_error(state);
  23. return;
  24. }
  25.  
  26.  
  27. int rc = BIO_write(state->read, buf->base, nread);
  28. assert(rc == nread);
  29. while (1)
  30. {
  31. int rc = SSL_read(state->ssl, buf->base, buf->len);
  32. if (rc <= 0) {
  33. rc = SSL_get_error(state->ssl, rc);
  34. if (rc != SSL_ERROR_WANT_READ) {
  35. push_ssl_errors();
  36. state->server->options.handler->connection_error(state);
  37. abort_connection_on_error(state);
  38. break;
  39. }
  40.  
  41. maybe_flush_ssl(state);
  42. ensure_connection_intialized(state);
  43. // need to read more, we'll let libuv handle this
  44. break;
  45. }
  46.  
  47. // should be rare: can only happen if we go for 0rtt or something like that
  48. // and we do the handshake and have real data in one network roundtrip
  49. if (ensure_connection_intialized(state) == 0)
  50. break;
  51.  
  52. if (state->flags & CONNECTION_STATUS_WRITE_AND_ABORT) {
  53. // we won't accept anything from this kind of connection
  54. // just read it out of the network and let's give the write
  55. // a chance to kill it
  56. continue;
  57. }
  58. if (read_message(state, buf->base, rc) == 0) {
  59. // handler asked to close the socket
  60. if (maybe_flush_ssl(state)) {
  61. state->flags |= CONNECTION_STATUS_WRITE_AND_ABORT;
  62. break;
  63. }
  64. abort_connection_on_error(state);
  65. break;
  66. }
  67. }
  68.  
  69. free(buf->base);
  70. }
Add Comment
Please, Sign In to add comment