Skip to content

Commit

Permalink
Ensure correct protocol is negotiated
Browse files Browse the repository at this point in the history
  • Loading branch information
64 committed Jan 18, 2018
1 parent d5702f3 commit 64963b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ void client_close_immediate(struct client *client) {
client_free(client);
}

static bool is_protocol_correct(struct client *client) {
log_debug("Negotiated protocol: %s", s2n_get_application_protocol(client->tls));
return strcmp("h2", s2n_get_application_protocol(client->tls)) == 0;
}

static void initiate_graceful_close(struct client *client) {
client->is_closing = true;
if (client->state == HH_NEGOTIATING_TLS || client->state == HH_ALREADY_CLOSED)
Expand Down Expand Up @@ -281,6 +286,14 @@ static int do_negotiate(struct client *client) {
return blind_client(client, s2n_connection_get_delay(client->tls));
}
}
if (client->blocked == S2N_NOT_BLOCKED) {
if (!is_protocol_correct(client)) {
// HTTP/2 was not negotiated, close the connection (TODO: with an alert or message?)
return -1;
}
// Might we need to check that there is no more data available?
change_state(client, HH_WAITING_MAGIC);
}
return 0;
}

Expand Down Expand Up @@ -1004,8 +1017,6 @@ int client_on_write_ready(struct client *client) {
break;
if (do_negotiate(client) < 0)
goto error;
if (client->blocked == S2N_NOT_BLOCKED)
change_state(client, HH_WAITING_MAGIC);
break;
case HH_WAITING_SETTINGS: // Keep sending SETTINGS frame
case HH_IDLE:
Expand Down Expand Up @@ -1041,14 +1052,11 @@ int client_on_data_received(struct client *client) {
switch (client->state) {
case HH_NEGOTIATING_TLS:
if (client->blocked == S2N_BLOCKED_ON_WRITE) {
fprintf(stderr, "Unexpected data on client socket\n");
log_warn("Unexpected data on client socket\n");
goto error;
}
if (do_negotiate(client) < 0)
goto error;
if (client->blocked == S2N_NOT_BLOCKED)
// Might we need to check that there is no more data available?
change_state(client, HH_WAITING_MAGIC);
break;
case HH_WAITING_MAGIC:
case HH_WAITING_SETTINGS:
Expand Down
2 changes: 1 addition & 1 deletion src/hh.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ static void *worker_event_loop(void *state) {
}
if (client->is_closing) {
if (client_close_graceful(client) < 0) {
client_close_immediate(client);
client_close_immediate(client);
}
}
}
Expand Down

0 comments on commit 64963b6

Please sign in to comment.