400 Connection *conn = malloc(sizeof(Connection)); |
400 Connection *conn = malloc(sizeof(Connection)); |
401 conn->address = ca; |
401 conn->address = ca; |
402 conn->fd = clientfd; |
402 conn->fd = clientfd; |
403 conn->listener = ls; |
403 conn->listener = ls; |
404 if(ls->ssl) { |
404 if(ls->ssl) { |
|
405 // SSL connections are always non-blocking |
|
406 // set socket non blocking |
|
407 int flags; |
|
408 if((flags = fcntl(conn->fd, F_GETFL, 0)) == -1) { |
|
409 flags = 0; |
|
410 } |
|
411 if(fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK)) { |
|
412 perror("Error: acceptor_thread: fcntl"); |
|
413 // TODO: error |
|
414 } |
|
415 |
405 SSL *ssl = SSL_new(ls->ssl->sslctx); |
416 SSL *ssl = SSL_new(ls->ssl->sslctx); |
406 SSL_set_fd(ssl, clientfd); |
417 SSL_set_fd(ssl, clientfd); |
407 int ssl_ar = SSL_accept(ssl); |
418 |
408 if(ssl_ar <= 0) { |
419 conn->ssl = ssl; |
409 int error = SSL_get_error(ssl, ssl_ar); |
420 conn->read = connection_ssl_read; |
410 char *errstr; |
421 conn->write = connection_ssl_write; |
411 switch(error) { |
422 conn->close = connection_ssl_close; |
412 default: errstr = "unknown"; break; |
|
413 case SSL_ERROR_ZERO_RETURN: errstr = "SSL_ERROR_ZERO_RETURN"; break; |
|
414 case SSL_ERROR_WANT_READ: errstr = "SSL_ERROR_WANT_READ"; break; |
|
415 case SSL_ERROR_WANT_WRITE: errstr = "SSL_ERROR_WANT_WRITE"; break; |
|
416 case SSL_ERROR_WANT_CONNECT: errstr = "SSL_ERROR_WANT_CONNECT"; break; |
|
417 case SSL_ERROR_WANT_ACCEPT: errstr = "SSL_ERROR_WANT_ACCEPT"; break; |
|
418 case SSL_ERROR_WANT_X509_LOOKUP: errstr = "SSL_ERROR_WANT_X509_LOOKUP"; break; |
|
419 case SSL_ERROR_SYSCALL: errstr = "SSL_ERROR_SYSCALL"; break; |
|
420 case SSL_ERROR_SSL: errstr = "SL_ERROR_SSL"; break; |
|
421 } |
|
422 log_ereport(LOG_VERBOSE, "SSL accept error[%d]: %s", error, errstr); |
|
423 free(conn); |
|
424 conn = NULL; |
|
425 close(clientfd); |
|
426 } else { |
|
427 conn->ssl = ssl; |
|
428 conn->read = connection_ssl_read; |
|
429 conn->write = connection_ssl_write; |
|
430 conn->close = connection_ssl_close; |
|
431 } |
|
432 } else { |
423 } else { |
433 conn->ssl = NULL; |
424 conn->ssl = NULL; |
434 conn->read = connection_read; |
425 conn->read = connection_read; |
435 conn->write = connection_write; |
426 conn->write = connection_write; |
436 conn->close = connection_close; |
427 conn->close = connection_close; |