Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Better error handling for connection_ssl_new
Browse files Browse the repository at this point in the history
- Instead of assertions, do proper error checking/handling.
- evhtp_connection_ssl_new now returns NULL on *ANY* error.
  • Loading branch information
NathanFrench committed Nov 2, 2017
1 parent 9650124 commit 369fe77
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5306,6 +5306,10 @@ evhtp_connection_new_dns(struct event_base * evbase, struct evdns_base * dns_bas
} /* evhtp_connection_new_dns */

#ifndef EVHTP_DISABLE_SSL

#define ssl_sk_new_ bufferevent_openssl_socket_new
#define ssl_sk_connect_ bufferevent_socket_connect

evhtp_connection_t *
evhtp_connection_ssl_new(struct event_base * evbase,
const char * addr,
Expand All @@ -5314,42 +5318,59 @@ evhtp_connection_ssl_new(struct event_base * evbase,
{
evhtp_connection_t * conn;
struct sockaddr_in sin;
int rc;
int8_t err;

if (evbase == NULL)
{
return NULL;
}

if (!(conn = htp__connection_new_(NULL, -1, evhtp_type_client)))
{
if (!(conn = htp__connection_new_(NULL, -1, evhtp_type_client))) {
return NULL;
}

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(addr);
sin.sin_port = htons(port);
conn->evbase = evbase;
err = -1;

conn->ssl = SSL_new(ctx);
evhtp_assert(conn->ssl != NULL);
do {
if ((conn->ssl = SSL_new(ctx)) == NULL) {
break;
}

conn->evbase = evbase;
conn->bev = bufferevent_openssl_socket_new(
evbase, -1,
conn->ssl,
BUFFEREVENT_SSL_CONNECTING,
BEV_OPT_CLOSE_ON_FREE);
if ((conn->bev = ssl_sk_new_(evbase, -1, conn->ssl,
BUFFEREVENT_SSL_CONNECTING,
BEV_OPT_CLOSE_ON_FREE)) == NULL) {
break;
}

evhtp_assert(conn->bev != NULL);
if (bufferevent_enable(conn->bev, EV_READ) == -1) {
break;
}

bufferevent_enable(conn->bev, EV_READ);
bufferevent_setcb(conn->bev, NULL, NULL,
htp__connection_eventcb_, conn);

rc = bufferevent_socket_connect(conn->bev,
(struct sockaddr *)&sin, sizeof(sin));
bufferevent_setcb(conn->bev, NULL, NULL,
htp__connection_eventcb_, conn);

evhtp_assert(rc == 0);

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(addr);
sin.sin_port = htons(port);

if (ssl_sk_connect_(conn->bev,
(struct sockaddr *)&sin,
sizeof(sin)) == -1) {
break;
}

err = 0;
} while (0);


if (err == -1) {
evhtp_safe_free(conn, evhtp_connection_free);

return NULL;
}

return conn;
} /* evhtp_connection_ssl_new */
Expand Down

0 comments on commit 369fe77

Please sign in to comment.