Skip to content

Commit

Permalink
Code & reason in close, recursion bugs in context create/free
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jun 26, 2020
1 parent 70f1de0 commit 577c822
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 100 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WITH_OPENSSL=1 enables OpenSSL 1.1+ support
# WITH_OPENSSL=1 enables OpenSSL 1.1+ support or BoringSSL
ifeq ($(WITH_OPENSSL),1)
override CFLAGS += -DLIBUS_USE_OPENSSL
# With problems on macOS, make sure to pass needed LDFLAGS required to find these
Expand Down Expand Up @@ -28,7 +28,7 @@ endif

# WITH_ASAN builds with sanitizers
ifeq ($(WITH_ASAN),1)
override CFLAGS += -fsanitize=address
override CFLAGS += -fsanitize=address -g
override LDFLAGS += -lasan
endif

Expand Down
6 changes: 3 additions & 3 deletions examples/echo_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct us_socket_t *on_echo_socket_writable(struct us_socket_t *s) {
}

/* Socket closed handler */
struct us_socket_t *on_echo_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_echo_socket_close(struct us_socket_t *s, int code, void *reason) {
struct echo_socket *es = (struct echo_socket *) us_socket_ext(SSL, s);

printf("Client disconnected\n");
Expand All @@ -70,7 +70,7 @@ struct us_socket_t *on_echo_socket_close(struct us_socket_t *s) {
/* Socket half-closed handler */
struct us_socket_t *on_echo_socket_end(struct us_socket_t *s) {
us_socket_shutdown(SSL, s);
return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

/* Socket data handler */
Expand Down Expand Up @@ -116,7 +116,7 @@ struct us_socket_t *on_echo_socket_open(struct us_socket_t *s, int is_client, ch
/* Socket timeout handler */
struct us_socket_t *on_echo_socket_timeout(struct us_socket_t *s) {
printf("Client was idle for too long\n");
return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

int main() {
Expand Down
10 changes: 5 additions & 5 deletions examples/hammer_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct us_socket_t *perform_random_operation(struct us_socket_t *s) {
switch (rand() % 5) {
case 0: {
// close
return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}
case 1: {
// adoption cannot happen if closed!
Expand Down Expand Up @@ -141,7 +141,7 @@ struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
return perform_random_operation(s);
}

struct us_socket_t *on_web_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_web_socket_close(struct us_socket_t *s, int code, void *reason) {
assume_state(s, 0);

closed_connections++;
Expand All @@ -155,7 +155,7 @@ struct us_socket_t *on_web_socket_close(struct us_socket_t *s) {
return s;
}

struct us_socket_t *on_http_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_http_socket_close(struct us_socket_t *s, int code, void *reason) {
assume_state(s, 1);

closed_connections++;
Expand All @@ -173,15 +173,15 @@ struct us_socket_t *on_web_socket_end(struct us_socket_t *s) {
assume_state(s, 0);

// we need to close on shutdown
s = us_socket_close(SSL, s);
s = us_socket_close(SSL, s, 0, NULL);
return perform_random_operation(s);
}

struct us_socket_t *on_http_socket_end(struct us_socket_t *s) {
assume_state(s, 1);

// we need to close on shutdown
s = us_socket_close(SSL, s);
s = us_socket_close(SSL, s, 0, NULL);
return perform_random_operation(s);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/http_load_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
return s;
}

struct us_socket_t *on_http_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_http_socket_close(struct us_socket_t *s, int code, void *reason) {
return s;
}

struct us_socket_t *on_http_socket_end(struct us_socket_t *s) {
return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

struct us_socket_t *on_http_socket_data(struct us_socket_t *s, char *data, int length) {
Expand Down
6 changes: 3 additions & 3 deletions examples/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct us_socket_t *on_http_socket_writable(struct us_socket_t *s) {
return s;
}

struct us_socket_t *on_http_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_http_socket_close(struct us_socket_t *s, int code, void *reason) {
printf("Client disconnected\n");

return s;
Expand All @@ -51,7 +51,7 @@ struct us_socket_t *on_http_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_http_socket_end(struct us_socket_t *s) {
/* HTTP does not support half-closed sockets */
us_socket_shutdown(SSL, s);
return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

struct us_socket_t *on_http_socket_data(struct us_socket_t *s, char *data, int length) {
Expand Down Expand Up @@ -84,7 +84,7 @@ struct us_socket_t *on_http_socket_open(struct us_socket_t *s, int is_client, ch

struct us_socket_t *on_http_socket_timeout(struct us_socket_t *s) {
/* Close idle HTTP sockets */
return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

int main() {
Expand Down
22 changes: 11 additions & 11 deletions examples/peer_verify_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ struct us_socket_t *on_client_socket_writable(struct us_socket_t *s) {
return s;
}

struct us_socket_t *on_server_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_server_socket_close(struct us_socket_t *s, int code, void *reason) {
printf("on_server_socket_close\n");

us_listen_socket_close(SSL, listen_socket);

return s;
}

struct us_socket_t *on_client_socket_close(struct us_socket_t *s) {
struct us_socket_t *on_client_socket_close(struct us_socket_t *s, int code, void *reason) {

printf("on_client_socket_close\n");

Expand All @@ -119,12 +119,12 @@ struct us_socket_t *on_client_socket_close(struct us_socket_t *s) {

struct us_socket_t *on_server_socket_end(struct us_socket_t *s) {

return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

struct us_socket_t *on_client_socket_end(struct us_socket_t *s) {

return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

struct us_socket_t *on_server_socket_data(struct us_socket_t *s, char *data, int length) {
Expand Down Expand Up @@ -154,7 +154,7 @@ struct us_socket_t *on_client_socket_data(struct us_socket_t *s, char *data, int

client_received_data = true;

return us_socket_close(SSL, s);
return us_socket_close(SSL, s, 0, NULL);
}

struct us_socket_t *on_server_socket_open(struct us_socket_t *s, int is_client, char *ip, int ip_length) {
Expand Down Expand Up @@ -267,8 +267,8 @@ int expect_peer_verify(const char *test_name, bool expect_data_exchanged,
}

int main() {
expect_peer_verify("trusted client ca", true,

expect_peer_verify("trusted client ca", true,
(struct us_socket_context_options_t){
.key_file_name = ".certs/valid_server_key.pem",
.cert_file_name = ".certs/valid_server_crt.pem",
Expand All @@ -281,7 +281,7 @@ int main() {
});


expect_peer_verify("untrusted client ca", false,
expect_peer_verify("untrusted client ca", false,
(struct us_socket_context_options_t){
.key_file_name = ".certs/valid_server_key.pem",
.cert_file_name = ".certs/valid_server_crt.pem",
Expand All @@ -293,7 +293,7 @@ int main() {
.ca_file_name = ".certs/valid_ca_crt.pem"
});

expect_peer_verify("trusted selfsigned client", true,
expect_peer_verify("trusted selfsigned client", true,
(struct us_socket_context_options_t){
.key_file_name = ".certs/valid_server_key.pem",
.cert_file_name = ".certs/valid_server_crt.pem",
Expand All @@ -305,7 +305,7 @@ int main() {
.ca_file_name = ".certs/valid_ca_crt.pem"
});

expect_peer_verify("untrusted selfsigned client", false,
expect_peer_verify("untrusted selfsigned client", false,
(struct us_socket_context_options_t){
.key_file_name = ".certs/valid_server_key.pem",
.cert_file_name = ".certs/valid_server_crt.pem",
Expand All @@ -317,7 +317,7 @@ int main() {
.ca_file_name = ".certs/valid_ca_crt.pem"
});

expect_peer_verify("peer verify disabled", true,
expect_peer_verify("peer verify disabled", true,
(struct us_socket_context_options_t){
.key_file_name = ".certs/valid_server_key.pem",
.cert_file_name = ".certs/valid_server_crt.pem"
Expand Down
32 changes: 17 additions & 15 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,20 @@ void *us_socket_context_get_native_handle(int ssl, struct us_socket_context_t *c
}

struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *loop, int context_ext_size, struct us_socket_context_options_t options) {
#ifndef LIBUS_NO_SSL
if (ssl) {
/* This function will call us, again, with SSL = false and a bigger ext_size */
return (struct us_socket_context_t *) us_internal_create_ssl_socket_context(loop, context_ext_size, options);
}
#endif

/* For ease of use we copy all passed strings here */
options.ca_file_name = deep_str_copy(options.ca_file_name);
options.cert_file_name = deep_str_copy(options.cert_file_name);
options.dh_params_file_name = deep_str_copy(options.dh_params_file_name);
options.key_file_name = deep_str_copy(options.key_file_name);
options.passphrase = deep_str_copy(options.passphrase);

#ifndef LIBUS_NO_SSL
if (ssl) {
return (struct us_socket_context_t *) us_internal_create_ssl_socket_context(loop, context_ext_size, options);
}
#endif

struct us_socket_context_t *context = malloc(sizeof(struct us_socket_context_t) + context_ext_size);
context->loop = loop;
context->head = 0;
Expand All @@ -131,20 +132,21 @@ struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *
}

void us_socket_context_free(int ssl, struct us_socket_context_t *context) {
/* We also simply free every copied string here */
free((void *) context->options.ca_file_name);
free((void *) context->options.cert_file_name);
free((void *) context->options.dh_params_file_name);
free((void *) context->options.key_file_name);
free((void *) context->options.passphrase);

#ifndef LIBUS_NO_SSL
if (ssl) {
/* This function will call us again with SSL=false */
us_internal_ssl_socket_context_free((struct us_internal_ssl_socket_context_t *) context);
return;
}
#endif

/* We also simply free every copied string here */
free((void *) context->options.ca_file_name);
free((void *) context->options.cert_file_name);
free((void *) context->options.dh_params_file_name);
free((void *) context->options.key_file_name);
free((void *) context->options.passphrase);

us_internal_loop_unlink(context->loop, context);
free(context);
}
Expand Down Expand Up @@ -250,10 +252,10 @@ void us_socket_context_on_open(int ssl, struct us_socket_context_t *context, str
context->on_open = on_open;
}

void us_socket_context_on_close(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_close)(struct us_socket_t *s)) {
void us_socket_context_on_close(int ssl, struct us_socket_context_t *context, struct us_socket_t *(*on_close)(struct us_socket_t *s, int code, void *reason)) {
#ifndef LIBUS_NO_SSL
if (ssl) {
us_internal_ssl_socket_context_on_close((struct us_internal_ssl_socket_context_t *) context, (struct us_internal_ssl_socket_t * (*)(struct us_internal_ssl_socket_t *)) on_close);
us_internal_ssl_socket_context_on_close((struct us_internal_ssl_socket_context_t *) context, (struct us_internal_ssl_socket_t * (*)(struct us_internal_ssl_socket_t *, int code, void *reason)) on_close);
return;
}
#endif
Expand Down
Loading

0 comments on commit 577c822

Please sign in to comment.