]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: quic_tp: use in_addr/in6_addr for preferred_address
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 30 Nov 2023 14:36:10 +0000 (15:36 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 30 Nov 2023 14:59:45 +0000 (15:59 +0100)
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.

include/haproxy/quic_tp-t.h
include/haproxy/quic_tp.h
src/quic_tp.c

index 8ca69c73193daec6ab7e9b9c7c3254889ae4de85..cc0669b2d157812f1e3b3e9b5a42d2e07b42a3db 100644 (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];
 };
index af822641a371b8b1ca4ba0ea37a6c2b7e85fad4f..d3bdd18c9d8eff33942491a66bf2a2630cb100f8 100644 (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);
 }
 
index eea8a31ddc649d31c380acd46d5b3c3109b70923..caf48ceb94621bfa1a5be1c23a349f591a2f017c 100644 (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;