Skip to content

Commit

Permalink
Updated mqtt connection to properly copy tls_options. (#62)
Browse files Browse the repository at this point in the history
* Updated mqtt connection to properly copy tls_options.

* More bug fixes, noticed when tls_options were fixed.
  • Loading branch information
JonathanHenson authored Apr 2, 2019
1 parent 3c3ca7a commit 6e97359
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions source/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ static void s_mqtt_client_init(
aws_event_loop_group_get_next_loop(connection->client->bootstrap->event_loop_group);
aws_event_loop_schedule_task_future(
el, &connection->reconnect_task->task, connection->reconnect_timeouts.next_attempt);
} else {
connection->state = AWS_MQTT_CLIENT_STATE_DISCONNECTED;
MQTT_CLIENT_CALL_CALLBACK_ARGS(connection, on_connection_complete, error_code, 0, false);
}
return;
}
Expand Down Expand Up @@ -192,7 +189,6 @@ static void s_attempt_reconect(struct aws_task *task, void *userdata, enum aws_t
el, &connection->reconnect_task->task, connection->reconnect_timeouts.next_attempt);
}
} else {

aws_mem_release(reconnect->allocator, reconnect);
}
}
Expand Down Expand Up @@ -323,6 +319,18 @@ struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(struct aws_mqt
goto handle_error;
}

if (aws_hash_table_init(
&connection->outstanding_requests.table,
connection->allocator,
sizeof(struct aws_mqtt_outstanding_request *),
s_hash_uint16_t,
s_uint16_t_eq,
NULL,
&s_outstanding_request_destroy)) {

goto handle_error;
}

/* Initialize the handler */
connection->handler.alloc = connection->allocator;
connection->handler.vtable = aws_mqtt_get_client_channel_vtable();
Expand Down Expand Up @@ -385,6 +393,7 @@ void aws_mqtt_client_connection_destroy(struct aws_mqtt_client_connection *conne
if (connection->slot) {
aws_channel_slot_remove(connection->slot);
}
aws_tls_connection_options_clean_up(&connection->tls_options);

/* Frees all allocated memory */
aws_mem_release(connection->allocator, connection);
Expand Down Expand Up @@ -512,9 +521,18 @@ int aws_mqtt_client_connection_connect(

/* Cheat and set the tls_options host_name to our copy if they're the same */
if (connection_options->tls_options) {
connection->tls_options = *connection_options->tls_options;
struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(connection->host_name);
aws_tls_connection_options_set_server_name(&connection->tls_options, connection->allocator, &host_name_cur);
if (aws_tls_connection_options_copy(&connection->tls_options, connection_options->tls_options)) {
return AWS_OP_ERR;
}

if (!connection_options->tls_options->server_name) {
struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(connection->host_name);
if (aws_tls_connection_options_set_server_name(
&connection->tls_options, connection->allocator, &host_name_cur)) {
goto error;
}
}

} else {
AWS_ZERO_STRUCT(connection->tls_options);
}
Expand All @@ -528,7 +546,7 @@ int aws_mqtt_client_connection_connect(
assert(!connection->reconnect_task);
connection->reconnect_task = aws_mem_acquire(connection->allocator, sizeof(struct aws_mqtt_reconnect_task));
if (!connection->reconnect_task) {
return AWS_OP_ERR;
goto error;
}
aws_atomic_init_ptr(&connection->reconnect_task->connection_ptr, connection);
connection->reconnect_task->allocator = connection->allocator;
Expand All @@ -538,18 +556,26 @@ int aws_mqtt_client_connection_connect(
struct aws_byte_buf client_id_buf =
aws_byte_buf_from_array(connection_options->client_id.ptr, connection_options->client_id.len);
if (aws_byte_buf_init_copy(&connection->client_id, connection->allocator, &client_id_buf)) {
aws_mem_release(connection->allocator, connection->reconnect_task);
return AWS_OP_ERR;
goto client_id_alloc_failed;
}

if (aws_mqtt_client_connection_reconnect(
connection, connection_options->on_connection_complete, connection_options->user_data)) {
aws_mem_release(connection->allocator, connection->reconnect_task);
aws_byte_buf_clean_up(&connection->client_id);
return AWS_OP_ERR;
goto reconnect_failed;
}

return AWS_OP_SUCCESS;

reconnect_failed:
aws_mem_release(connection->allocator, connection->reconnect_task);

client_id_alloc_failed:
aws_mem_release(connection->allocator, connection->reconnect_task);

error:
aws_tls_connection_options_clean_up(&connection->tls_options);
AWS_ZERO_STRUCT(connection->tls_options);
return AWS_OP_ERR;
}

/*******************************************************************************
Expand All @@ -571,19 +597,6 @@ int aws_mqtt_client_connection_reconnect(
aws_mqtt_topic_tree_init(&connection->subscriptions, connection->allocator);
}

/* Init the outstanding requests hash table, the lifetime is limited to that of the socket connection */
if (aws_hash_table_init(
&connection->outstanding_requests.table,
connection->allocator,
sizeof(struct aws_mqtt_outstanding_request *),
s_hash_uint16_t,
s_uint16_t_eq,
NULL,
&s_outstanding_request_destroy)) {

return AWS_OP_ERR;
}

int result = 0;
if (connection->tls_options.ctx) {
result = aws_client_bootstrap_new_tls_socket_channel(
Expand Down

0 comments on commit 6e97359

Please sign in to comment.