MINOR: quic_tp: use in_addr/in6_addr for preferred_address

preferred_address is a transport parameter specify by the server. It
specified both an IPv4 and IPv6 address. These addresses were defined as
plain array in <struct tp_preferred_address>.

Convert these adressees to use the common types in_addr/in6_addr. With
this change, dumping of preferred_address is extended. It now displays
the addresses using inet_ntop() and CID value.
This commit is contained in:
Amaury Denoyelle 2023-11-30 15:36:10 +01:00
parent a9ad68aa74
commit 0ce213d246
3 changed files with 33 additions and 31 deletions

View File

@ -6,6 +6,7 @@
#endif
#include <inttypes.h>
#include <sys/socket.h>
#define QUIC_STATELESS_RESET_TOKEN_LEN 16
@ -20,8 +21,8 @@ struct tp_cid {
struct tp_preferred_address {
uint16_t ipv4_port;
uint16_t ipv6_port;
uint8_t ipv4_addr[4];
uint8_t ipv6_addr[16];
struct in_addr ipv4_addr;
struct in6_addr ipv6_addr;
struct tp_cid cid;
uint8_t stateless_reset_token[QUIC_STATELESS_RESET_TOKEN_LEN];
};

View File

@ -84,8 +84,7 @@ static inline void quic_transport_params_dump(struct buffer *b,
chunk_appendf(b, " ms_bidi=%llu", (ull)p->initial_max_streams_bidi);
chunk_appendf(b, " ms_uni=%llu\n", (ull)p->initial_max_streams_uni);
if (p->disable_active_migration || p->with_stateless_reset_token ||
p->with_preferred_address) {
if (p->disable_active_migration || p->with_stateless_reset_token) {
int prev = 0;
chunk_appendf(b, " (");
@ -101,15 +100,23 @@ static inline void quic_transport_params_dump(struct buffer *b,
prev = 1;
chunk_appendf(b, "stless_rst_tok");
}
if (p->with_preferred_address) {
if (prev)
chunk_appendf(b, ",");
prev = 1;
chunk_appendf(b, "pref_addr");
}
chunk_appendf(b, ")");
}
if (p->with_preferred_address) {
char bufaddr[INET6_ADDRSTRLEN];
chunk_appendf(b, " pref_addr=");
inet_ntop(AF_INET, &p->preferred_address.ipv4_addr,
bufaddr, sizeof(bufaddr));
chunk_appendf(b, "%s:%hu ", bufaddr, p->preferred_address.ipv4_port);
inet_ntop(AF_INET6, &p->preferred_address.ipv6_addr,
bufaddr, sizeof(bufaddr));
chunk_appendf(b, "[%s]:%hu ", bufaddr, p->preferred_address.ipv6_port);
quic_tp_cid_dump(b, &p->preferred_address.cid);
chunk_appendf(b, "\n");
}
quic_tp_version_info_dump(b, &p->version_information, local);
}

View File

@ -80,10 +80,7 @@ void quic_transport_params_init(struct quic_transport_params *p, int server)
}
/* Encode <addr> preferred address transport parameter in <buf> without its
* "type+len" prefix. Note that the IP addresses must be encoded in network byte
* order.
* So ->ipv4_addr and ->ipv6_addr, which are buffers, must contained values
* already encoded in network byte order.
* "type+len" prefix.
* It is the responsibility of the caller to check there is enough room in <buf> to encode
* this address.
* Never fails.
@ -95,14 +92,14 @@ static void quic_transport_param_enc_pref_addr_val(unsigned char **buf,
write_n16(*buf, addr->ipv4_port);
*buf += sizeof addr->ipv4_port;
memcpy(*buf, addr->ipv4_addr, sizeof addr->ipv4_addr);
*buf += sizeof addr->ipv4_addr;
memcpy(*buf, (uint8_t *)&addr->ipv4_addr.s_addr, sizeof(addr->ipv4_addr.s_addr));
*buf += sizeof(addr->ipv4_addr.s_addr);
write_n16(*buf, addr->ipv6_port);
*buf += sizeof addr->ipv6_port;
memcpy(*buf, addr->ipv6_addr, sizeof addr->ipv6_addr);
*buf += sizeof addr->ipv6_addr;
memcpy(*buf, addr->ipv6_addr.s6_addr, sizeof(addr->ipv6_addr.s6_addr));
*buf += sizeof(addr->ipv6_addr.s6_addr);
*(*buf)++ = addr->cid.len;
if (addr->cid.len) {
@ -123,21 +120,21 @@ static int quic_transport_param_dec_pref_addr(struct tp_preferred_address *addr,
{
ssize_t addr_len;
addr_len = sizeof addr->ipv4_port + sizeof addr->ipv4_addr;
addr_len += sizeof addr->ipv6_port + sizeof addr->ipv6_addr;
addr_len += sizeof addr->cid.len;
addr_len = sizeof(addr->ipv4_port) + sizeof(addr->ipv4_addr.s_addr);
addr_len += sizeof(addr->ipv6_port) + sizeof(addr->ipv6_addr.s6_addr);
addr_len += sizeof(addr->cid.len);
if (end - *buf < addr_len)
return 0;
memcpy(addr->ipv4_addr, *buf, sizeof addr->ipv4_addr);
*buf += sizeof addr->ipv4_addr;
memcpy((uint8_t *)&addr->ipv4_addr.s_addr, *buf, sizeof(addr->ipv4_addr.s_addr));
*buf += sizeof(addr->ipv4_addr.s_addr);
addr->ipv4_port = read_n16(*buf);
*buf += sizeof addr->ipv4_port;
memcpy(addr->ipv6_addr, *buf, sizeof addr->ipv6_addr);
*buf += sizeof addr->ipv6_addr;
memcpy(addr->ipv6_addr.s6_addr, *buf, sizeof(addr->ipv6_addr.s6_addr));
*buf += sizeof(addr->ipv6_addr.s6_addr);
addr->ipv6_port = read_n16(*buf);
*buf += sizeof addr->ipv6_port;
@ -394,9 +391,6 @@ static inline size_t sizeof_quic_cid(const struct tp_cid *cid)
}
/* Encode <addr> preferred address into <buf>.
* Note that the IP addresses must be encoded in network byte order.
* So ->ipv4_addr and ->ipv6_addr, which are buffers, must contained
* values already encoded in network byte order.
* Returns 1 if succeeded, 0 if not.
*/
static int quic_transport_param_enc_pref_addr(unsigned char **buf,
@ -405,10 +399,10 @@ static int quic_transport_param_enc_pref_addr(unsigned char **buf,
{
uint64_t addr_len = 0;
addr_len += sizeof addr->ipv4_port + sizeof addr->ipv4_addr;
addr_len += sizeof addr->ipv6_port + sizeof addr->ipv6_addr;
addr_len += sizeof(addr->ipv4_port) + sizeof(addr->ipv4_addr.s_addr);
addr_len += sizeof(addr->ipv6_port) + sizeof(addr->ipv6_addr.s6_addr);
addr_len += sizeof_quic_cid(&addr->cid);
addr_len += sizeof addr->stateless_reset_token;
addr_len += sizeof(addr->stateless_reset_token);
if (!quic_transport_param_encode_type_len(buf, end, QUIC_TP_PREFERRED_ADDRESS, addr_len))
return 0;