Skip to content

Commit

Permalink
Added Endpoint::drop_connection
Browse files Browse the repository at this point in the history
- Sometimes when ngtcp2 does not want a connection close packet written, we delete the connection from the endpoint's internal map
- In these cases, we still want to notify the calling endpoint that the connection was closed
  • Loading branch information
dr7ana authored and tewinget committed Sep 12, 2023
1 parent 82d0a5a commit 3d9c072
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
4 changes: 3 additions & 1 deletion include/quic/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ namespace oxen::quic

void close_conns(std::optional<Direction> d = std::nullopt);

void close_connection(Connection& conn, io_error code = io_error{0}, std::string_view msg = "NO_ERROR"sv);
void drop_connection(Connection& conn, io_error ec = io_error{0});

void close_connection(Connection& conn, io_error ec = io_error{0}, std::string_view msg = "NO_ERROR"sv);

const Address& local() { return _local; }

Expand Down
5 changes: 4 additions & 1 deletion include/quic/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace oxen::quic
STREAM_EXCEPTION = (1ULL << 62) - 32,
STREAM_CONNECTION_EXPIRED = (1ULL << 62) - 31,

CONN_WRITE_CLOSE_FAIL = (1ULL << 62)
CONN_WRITE_CLOSE_FAIL = (1ULL << 62),
CONN_SEND_CLOSE_FAIL = (1ULL << 62) + 1
};

inline const char* quic_strerror(uint64_t e)
Expand All @@ -39,6 +40,8 @@ namespace oxen::quic
return "No error - draining connection";
case error::CONN_WRITE_CLOSE_FAIL:
return "Error - Failed to write connection close";
case error::CONN_SEND_CLOSE_FAIL:
return "Error = Failed to send connection close";
default:
return "Unknown error";
}
Expand Down
6 changes: 3 additions & 3 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ namespace oxen::quic

_endpoint.call([this]() {
log::debug(log_cat, "Note: connection (CID: {}) is in closing period; endpoint deleting connection", scid());
_endpoint.delete_connection(scid());
_endpoint.drop_connection(*this, io_error{NGTCP2_ERR_CLOSING});
});
return;
}
Expand Down Expand Up @@ -326,7 +326,7 @@ namespace oxen::quic
ngtcp2_strerror(rv));
_endpoint.call([this]() {
log::debug(log_cat, "Endpoint deleting CID: {}", scid());
_endpoint.delete_connection(scid());
_endpoint.drop_connection(*this, io_error{NGTCP2_ERR_DROP_CONN});
});
break;
case NGTCP2_ERR_CRYPTO:
Expand All @@ -340,7 +340,7 @@ namespace oxen::quic
ngtcp2_strerror(rv));
_endpoint.call([this]() {
log::debug(log_cat, "Endpoint deleting CID: {}", scid());
_endpoint.delete_connection(scid());
_endpoint.drop_connection(*this, io_error{NGTCP2_ERR_CRYPTO});
});
break;
default:
Expand Down
12 changes: 11 additions & 1 deletion src/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ namespace oxen::quic
return;
}

void Endpoint::drop_connection(Connection& conn, io_error ec)
{
log::debug(log_cat, "Dropping connection (CID: {})", *conn.scid().data);

if (connection_close_cb)
connection_close_cb(conn, ec.code());

delete_connection(conn.scid());
}

void Endpoint::close_connection(Connection& conn, io_error ec, std::string_view msg)
{
log::debug(log_cat, "Closing connection (CID: {})", *conn.scid().data);
Expand All @@ -183,7 +193,7 @@ namespace oxen::quic
"Connection (CID: {}) passed idle expiry timer; closing now without close "
"packet",
*conn.scid().data);
delete_connection(conn.scid());
drop_connection(conn, io_error{NGTCP2_ERR_IDLE_CLOSE});
return;
}

Expand Down
3 changes: 0 additions & 3 deletions tests/001-handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ namespace oxen::quic::test
auto client_established = bool_waiter{[](connection_interface&) {}};
auto server_established = bool_waiter{[](connection_interface&) {}};

// connection_open_callback server_established{server_waiter};
// connection_open_callback client_established{client_waiter};

Network test_net{};

auto server_tls = GNUTLSCreds::make("./serverkey.pem"s, "./servercert.pem"s, "./clientcert.pem"s);
Expand Down

0 comments on commit 3d9c072

Please sign in to comment.