Skip to content

Commit

Permalink
Rename ngtcp2_conn_write_connection_close2 to ngtcp2_conn_write_conne…
Browse files Browse the repository at this point in the history
…ction_close

Rename ngtcp2_conn_write_connection_close2 to
ngtcp2_conn_write_connection_close.  The existing
ngtcp2_conn_write_connection_close and
ngtcp2_conn_write_application_close are removed.
  • Loading branch information
tatsuhiro-t committed Mar 25, 2022
1 parent e1f593c commit 90da093
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 206 deletions.
4 changes: 2 additions & 2 deletions examples/client.cc
Expand Up @@ -1552,11 +1552,11 @@ int Client::handle_error() {

ngtcp2_pkt_info pi;

auto nwrite = ngtcp2_conn_write_connection_close2(
auto nwrite = ngtcp2_conn_write_connection_close(
conn_, &ps.path, &pi, buf.data(), buf.size(), &last_error_,
util::timestamp(loop_));
if (nwrite < 0) {
std::cerr << "ngtcp2_conn_write_connection_close2: "
std::cerr << "ngtcp2_conn_write_connection_close: "
<< ngtcp2_strerror(nwrite) << std::endl;
return -1;
}
Expand Down
25 changes: 15 additions & 10 deletions examples/gtlssimpleclient.c
Expand Up @@ -145,7 +145,7 @@ struct client {
size_t nwrite;
} stream;

uint64_t last_error;
ngtcp2_connection_close_error last_error;

ev_io rev;
ev_timer timer;
Expand Down Expand Up @@ -221,7 +221,8 @@ static int alert_read_func(gnutls_session_t session,
(void)level;
(void)alert_level;

c->last_error = NGTCP2_CRYPTO_ERROR | alert_desc;
ngtcp2_connection_close_error_set_transport_error_tls_alert(
&c->last_error, (uint8_t)alert_desc, NULL, 0);

return 0;
}
Expand Down Expand Up @@ -575,11 +576,13 @@ static int client_read(struct client *c) {
case NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM:
case NGTCP2_ERR_TRANSPORT_PARAM:
case NGTCP2_ERR_PROTO:
c->last_error = ngtcp2_err_infer_quic_transport_error_code(rv);
ngtcp2_connection_close_error_set_transport_error_liberr(&c->last_error,
rv, NULL, 0);
break;
default:
if (!c->last_error) {
c->last_error = ngtcp2_err_infer_quic_transport_error_code(rv);
if (!c->last_error.error_code) {
ngtcp2_connection_close_error_set_transport_error_liberr(
&c->last_error, rv, NULL, 0);
}
break;
}
Expand Down Expand Up @@ -671,7 +674,8 @@ static int client_write_streams(struct client *c) {
default:
fprintf(stderr, "ngtcp2_conn_writev_stream: %s\n",
ngtcp2_strerror((int)nwrite));
c->last_error = ngtcp2_err_infer_quic_transport_error_code((int)nwrite);
ngtcp2_connection_close_error_set_transport_error_liberr(
&c->last_error, (int)nwrite, NULL, 0);
return -1;
}
}
Expand Down Expand Up @@ -729,15 +733,14 @@ static void client_close(struct client *c) {
ngtcp2_path_storage ps;
uint8_t buf[1280];

if (ngtcp2_conn_is_in_closing_period(c->conn) || !c->last_error) {
if (ngtcp2_conn_is_in_closing_period(c->conn) || !c->last_error.error_code) {
goto fin;
}

ngtcp2_path_storage_zero(&ps);

nwrite = ngtcp2_conn_write_connection_close(c->conn, &ps.path, &pi, buf,
sizeof(buf), c->last_error, NULL,
0, timestamp());
nwrite = ngtcp2_conn_write_connection_close(
c->conn, &ps.path, &pi, buf, sizeof(buf), &c->last_error, timestamp());
if (nwrite < 0) {
fprintf(stderr, "ngtcp2_conn_write_connection_close: %s\n",
ngtcp2_strerror((int)nwrite));
Expand Down Expand Up @@ -796,6 +799,8 @@ static int client_init(struct client *c) {

memset(c, 0, sizeof(*c));

ngtcp2_connection_close_error_default(&c->last_error);

c->fd = create_sock((struct sockaddr *)&remote_addr, &remote_addrlen,
REMOTE_HOST, REMOTE_PORT);
if (c->fd == -1) {
Expand Down
2 changes: 1 addition & 1 deletion examples/h09client.cc
Expand Up @@ -1450,7 +1450,7 @@ int Client::handle_error() {

ngtcp2_pkt_info pi;

auto nwrite = ngtcp2_conn_write_connection_close2(
auto nwrite = ngtcp2_conn_write_connection_close(
conn_, &ps.path, &pi, buf.data(), buf.size(), &last_error_,
util::timestamp(loop_));
if (nwrite < 0) {
Expand Down
2 changes: 1 addition & 1 deletion examples/h09server.cc
Expand Up @@ -1286,7 +1286,7 @@ int Handler::start_closing_period() {
ngtcp2_path_storage_zero(&ps);

ngtcp2_pkt_info pi;
auto n = ngtcp2_conn_write_connection_close2(
auto n = ngtcp2_conn_write_connection_close(
conn_, &ps.path, &pi, conn_closebuf_->wpos(), conn_closebuf_->left(),
&last_error_, util::timestamp(loop_));
if (n < 0) {
Expand Down
2 changes: 1 addition & 1 deletion examples/server.cc
Expand Up @@ -2008,7 +2008,7 @@ int Handler::start_closing_period() {
ngtcp2_path_storage_zero(&ps);

ngtcp2_pkt_info pi;
auto n = ngtcp2_conn_write_connection_close2(
auto n = ngtcp2_conn_write_connection_close(
conn_, &ps.path, &pi, conn_closebuf_->wpos(), conn_closebuf_->left(),
&last_error_, util::timestamp(loop_));
if (n < 0) {
Expand Down
25 changes: 15 additions & 10 deletions examples/simpleclient.c
Expand Up @@ -146,7 +146,7 @@ struct client {
size_t nwrite;
} stream;

uint64_t last_error;
ngtcp2_connection_close_error last_error;

ev_io rev;
ev_timer timer;
Expand Down Expand Up @@ -203,7 +203,8 @@ static int send_alert(SSL *ssl, OSSL_ENCRYPTION_LEVEL ossl_level,
struct client *c = SSL_get_app_data(ssl);
(void)ossl_level;

c->last_error = NGTCP2_CRYPTO_ERROR | alert;
ngtcp2_connection_close_error_set_transport_error_tls_alert(&c->last_error,
alert, NULL, 0);

return 1;
}
Expand Down Expand Up @@ -472,11 +473,13 @@ static int client_read(struct client *c) {
case NGTCP2_ERR_MALFORMED_TRANSPORT_PARAM:
case NGTCP2_ERR_TRANSPORT_PARAM:
case NGTCP2_ERR_PROTO:
c->last_error = ngtcp2_err_infer_quic_transport_error_code(rv);
ngtcp2_connection_close_error_set_transport_error_liberr(&c->last_error,
rv, NULL, 0);
break;
default:
if (!c->last_error) {
c->last_error = ngtcp2_err_infer_quic_transport_error_code(rv);
if (!c->last_error.error_code) {
ngtcp2_connection_close_error_set_transport_error_liberr(
&c->last_error, rv, NULL, 0);
}
break;
}
Expand Down Expand Up @@ -568,7 +571,8 @@ static int client_write_streams(struct client *c) {
default:
fprintf(stderr, "ngtcp2_conn_writev_stream: %s\n",
ngtcp2_strerror((int)nwrite));
c->last_error = ngtcp2_err_infer_quic_transport_error_code((int)nwrite);
ngtcp2_connection_close_error_set_transport_error_liberr(
&c->last_error, (int)nwrite, NULL, 0);
return -1;
}
}
Expand Down Expand Up @@ -626,15 +630,14 @@ static void client_close(struct client *c) {
ngtcp2_path_storage ps;
uint8_t buf[1280];

if (ngtcp2_conn_is_in_closing_period(c->conn) || !c->last_error) {
if (ngtcp2_conn_is_in_closing_period(c->conn) || !c->last_error.error_code) {
goto fin;
}

ngtcp2_path_storage_zero(&ps);

nwrite = ngtcp2_conn_write_connection_close(c->conn, &ps.path, &pi, buf,
sizeof(buf), c->last_error, NULL,
0, timestamp());
nwrite = ngtcp2_conn_write_connection_close(
c->conn, &ps.path, &pi, buf, sizeof(buf), &c->last_error, timestamp());
if (nwrite < 0) {
fprintf(stderr, "ngtcp2_conn_write_connection_close: %s\n",
ngtcp2_strerror((int)nwrite));
Expand Down Expand Up @@ -693,6 +696,8 @@ static int client_init(struct client *c) {

memset(c, 0, sizeof(*c));

ngtcp2_connection_close_error_default(&c->last_error);

c->fd = create_sock((struct sockaddr *)&remote_addr, &remote_addrlen,
REMOTE_HOST, REMOTE_PORT);
if (c->fd == -1) {
Expand Down
143 changes: 7 additions & 136 deletions lib/includes/ngtcp2/ngtcp2.h
Expand Up @@ -4236,8 +4236,7 @@ NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_stream_versioned(
*
* When application sees :macro:`NGTCP2_ERR_WRITE_MORE`, it must not
* call other ngtcp2 API functions (application can still call
* `ngtcp2_conn_write_connection_close` or
* `ngtcp2_conn_write_application_close` to handle error from this
* `ngtcp2_conn_write_connection_close` to handle error from this
* function). Just keep calling `ngtcp2_conn_writev_stream`,
* `ngtcp2_conn_write_pkt`, or `ngtcp2_conn_writev_datagram` until it
* returns a positive number (which indicates a complete packet is
Expand Down Expand Up @@ -4367,8 +4366,7 @@ NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_stream_versioned(
*
* When application sees :macro:`NGTCP2_ERR_WRITE_MORE`, it must not
* call other ngtcp2 API functions (application can still call
* `ngtcp2_conn_write_connection_close` or
* `ngtcp2_conn_write_application_close` to handle error from this
* `ngtcp2_conn_write_connection_close` to handle error from this
* function). Just keep calling `ngtcp2_conn_writev_datagram`,
* `ngtcp2_conn_writev_stream` or `ngtcp2_conn_write_pkt` until it
* returns a positive number (which indicates a complete packet is
Expand Down Expand Up @@ -4406,100 +4404,6 @@ NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_writev_datagram_versioned(
uint32_t flags, uint64_t dgram_id, const ngtcp2_vec *datav, size_t datavcnt,
ngtcp2_tstamp ts);

/**
* @function
*
* `ngtcp2_conn_write_connection_close` writes a packet which contains
* a CONNECTION_CLOSE frame (type 0x1c) in the buffer pointed by
* |dest| whose capacity is |datalen|.
*
* If |path| is not ``NULL``, this function stores the network path
* with which the packet should be sent. Each addr field must point
* to the buffer which should be at least ``sizeof(struct
* sockaddr_storage)`` bytes long. The assignment might not be done
* if nothing is written to |dest|.
*
* If |pi| is not ``NULL``, this function stores packet metadata in it
* if it succeeds. The metadata includes ECN markings.
*
* This function must not be called from inside the callback
* functions.
*
* At the moment, successful call to this function makes connection
* close. We may change this behaviour in the future to allow
* graceful shutdown.
*
* This function returns the number of bytes written in |dest| if it
* succeeds, or one of the following negative error codes:
*
* :macro:`NGTCP2_ERR_NOMEM`
* Out of memory
* :macro:`NGTCP2_ERR_NOBUF`
* Buffer is too small
* :macro:`NGTCP2_ERR_INVALID_STATE`
* The current state does not allow sending CONNECTION_CLOSE.
* :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
* Packet number is exhausted, and cannot send any more packet.
* :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
* User callback failed
*
* TODO: Deprecated, remove this function in favor of
* ngtcp2_conn_write_connection_close2_versioned.
*/
NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_connection_close_versioned(
ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, uint64_t error_code,
const uint8_t *reason, size_t reasonlen, ngtcp2_tstamp ts);

/**
* @function
*
* `ngtcp2_conn_write_application_close` writes a packet which
* contains a CONNECTION_CLOSE frame (type 0x1d) in the buffer pointed
* by |dest| whose capacity is |datalen|.
*
* If |path| is not ``NULL``, this function stores the network path
* with which the packet should be sent. Each addr field must point
* to the buffer which should be at least ``sizeof(struct
* sockaddr_storage)`` bytes long. The assignment might not be done
* if nothing is written to |dest|.
*
* If |pi| is not ``NULL``, this function stores packet metadata in it
* if it succeeds. The metadata includes ECN markings.
*
* If handshake has not been confirmed yet, CONNECTION_CLOSE (type
* 0x1c) with error code :macro:`NGTCP2_APPLICATION_ERROR` is written
* instead.
*
* This function must not be called from inside the callback
* functions.
*
* At the moment, successful call to this function makes connection
* close. We may change this behaviour in the future to allow
* graceful shutdown.
*
* This function returns the number of bytes written in |dest| if it
* succeeds, or one of the following negative error codes:
*
* :macro:`NGTCP2_ERR_NOMEM`
* Out of memory
* :macro:`NGTCP2_ERR_NOBUF`
* Buffer is too small
* :macro:`NGTCP2_ERR_INVALID_STATE`
* The current state does not allow sending CONNECTION_CLOSE.
* :macro:`NGTCP2_ERR_PKT_NUM_EXHAUSTED`
* Packet number is exhausted, and cannot send any more packet.
* :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
* User callback failed
*
* TODO: Deprecated, remove this function in favor of
* ngtcp2_conn_write_connection_close2_versioned.
*/
NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_application_close_versioned(
ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen, uint64_t app_error_code,
const uint8_t *reason, size_t reasonlen, ngtcp2_tstamp ts);

/**
* @function
*
Expand Down Expand Up @@ -5132,9 +5036,9 @@ NGTCP2_EXTERN void ngtcp2_connection_close_error_set_application_error(
/**
* @function
*
* `ngtcp2_conn_write_connection_close2` writes a packet which
* contains CONNECTION_CLOSE frame(s) (type 0x1c or 0x1d) in the
* buffer pointed by |dest| whose capacity is |datalen|.
* `ngtcp2_conn_write_connection_close` writes a packet which contains
* CONNECTION_CLOSE frame(s) (type 0x1c or 0x1d) in the buffer pointed
* by |dest| whose capacity is |datalen|.
*
* If |path| is not ``NULL``, this function stores the network path
* with which the packet should be sent. Each addr field must point
Expand Down Expand Up @@ -5173,11 +5077,8 @@ NGTCP2_EXTERN void ngtcp2_connection_close_error_set_application_error(
* Packet number is exhausted, and cannot send any more packet.
* :macro:`NGTCP2_ERR_CALLBACK_FAILURE`
* User callback failed
*
* TODO: Rename this function to
* ngtcp2_conn_write_connection_close_versioned.
*/
NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_connection_close2_versioned(
NGTCP2_EXTERN ngtcp2_ssize ngtcp2_conn_write_connection_close_versioned(
ngtcp2_conn *conn, ngtcp2_path *path, int pkt_info_version,
ngtcp2_pkt_info *pi, uint8_t *dest, size_t destlen,
const ngtcp2_connection_close_error *ccerr, ngtcp2_tstamp ts);
Expand Down Expand Up @@ -5526,40 +5427,10 @@ NGTCP2_EXTERN int ngtcp2_is_supported_version(uint32_t version);
* `ngtcp2_conn_write_connection_close` is a wrapper around
* `ngtcp2_conn_write_connection_close_versioned` to set the correct
* struct version.
*
* TODO: Deprecated, remove this function in favor of
* ngtcp2_conn_write_connection_close2.
*/
#define ngtcp2_conn_write_connection_close(CONN, PATH, PI, DEST, DESTLEN, \
ERROR_CODE, REASON, REASONLEN, TS) \
CCERR, TS) \
ngtcp2_conn_write_connection_close_versioned( \
(CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
(ERROR_CODE), (REASON), (REASONLEN), (TS))

/*
* `ngtcp2_conn_write_application_close` is a wrapper around
* `ngtcp2_conn_write_application_close_versioned` to set the correct
* struct version.
*
* TODO: Deprecated, remove this function in favor of
* ngtcp2_conn_write_connection_close2.
*/
#define ngtcp2_conn_write_application_close( \
CONN, PATH, PI, DEST, DESTLEN, APP_ERROR_CODE, REASON, REASONLEN, TS) \
ngtcp2_conn_write_application_close_versioned( \
(CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
(APP_ERROR_CODE), (REASON), (REASONLEN), (TS))

/*
* `ngtcp2_conn_write_connection_close2` is a wrapper around
* `ngtcp2_conn_write_connection_close2_versioned` to set the correct
* struct version.
*
* TODO: Rename this function to ngtcp2_conn_write_connection_close.
*/
#define ngtcp2_conn_write_connection_close2(CONN, PATH, PI, DEST, DESTLEN, \
CCERR, TS) \
ngtcp2_conn_write_connection_close2_versioned( \
(CONN), (PATH), NGTCP2_PKT_INFO_VERSION, (PI), (DEST), (DESTLEN), \
(CCERR), (TS))

Expand Down

0 comments on commit 90da093

Please sign in to comment.