~donmcc/ood

19f6d38c2d2c14cf656a2b3207642a9ca5192a60 — Don McCaughey 10 months ago 601d06c
Add `sockaddr_storage` to the union in `sa_address`.

Make the union memory in the `sa_address` struct easily available as any
of the `sockaddr` struct types.  Test that the "family" field is
available correctly as any of the types.

Remove an unneeded `const` from the
`sa_alloc_from_ipv6_address_and_port()` parameter list.

Rename `test_sa_str_for_generic()` to
`test_sa_str_for_unspecified_family()`.
3 files changed, 13 insertions(+), 5 deletions(-)

M src/sa/sa.c
M src/sa/sa.h
M src/sa/sa_tests.c
M src/sa/sa.c => src/sa/sa.c +1 -1
@@ 43,7 43,7 @@ fail:


struct sa_address *
sa_alloc_from_ipv6_address_and_port(struct in6_addr const in6_addr, in_port_t in_port)
sa_alloc_from_ipv6_address_and_port(struct in6_addr in6_addr, in_port_t in_port)
{
    struct sockaddr_in6 inet6 = {
            .sin6_family=AF_INET6,

M src/sa/sa.h => src/sa/sa.h +3 -2
@@ 8,7 8,7 @@


/* Size of fixed string buffers for IPv4 and IPv6 string representations,
 * including port.  Note that `SA_PORT_STR_LEN` and `SA_COLON_STR_LEN` do not
 * including port.  Note that `SA_PORT_STR_LEN` and `SA_COLON_STR_LEN` _do not_
 * include space for a nul terminator, but `INET_ADDRSTRLEN` and
 * `INET6_ADDRSTRLEN`, defined by
 * [POSIX](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/netinet_in.h.html)


@@ 37,6 37,7 @@ struct sa_address {
            char ipv6_string[SA_IPV6_STR_SIZE];
        };
        struct sockaddr_un local;
        struct sockaddr_storage storage;
    };
};



@@ 48,7 49,7 @@ struct sa_address *
sa_alloc_from_sockaddr_in(struct sockaddr_in const *ipv4_address);

struct sa_address *
sa_alloc_from_ipv6_address_and_port(struct in6_addr const in6_addr, in_port_t in_port);
sa_alloc_from_ipv6_address_and_port(struct in6_addr in6_addr, in_port_t in_port);

struct sa_address *
sa_alloc_from_sockaddr_in6(struct sockaddr_in6 const *ipv6_address);

M src/sa/sa_tests.c => src/sa/sa_tests.c +9 -2
@@ 18,6 18,7 @@ test_sa_alloc_from_address_and_port(void)
    assert(AF_INET == address->ipv4.sin_family);
    assert(AF_INET == address->ipv6.sin6_family);
    assert(AF_INET == address->local.sun_family);
    assert(AF_INET == address->storage.ss_family);
    assert(htonl(INADDR_LOOPBACK) == address->ipv4.sin_addr.s_addr);
    assert(htons(8080) == address->ipv4.sin_port);
    assert(STR_EQ("127.0.0.1:8080", sa_str(address)));


@@ 44,6 45,7 @@ test_sa_alloc_from_sockaddr_in(void)
    assert(AF_INET == address->ipv4.sin_family);
    assert(AF_INET == address->ipv6.sin6_family);
    assert(AF_INET == address->local.sun_family);
    assert(AF_INET == address->storage.ss_family);
    assert(htonl(INADDR_LOOPBACK) == address->ipv4.sin_addr.s_addr);
    assert(htons(8080) == address->ipv4.sin_port);
    assert(STR_EQ("127.0.0.1:8080", sa_str(address)));


@@ 62,6 64,7 @@ test_sa_alloc_from_ipv6_address_and_port(void)
    assert(AF_INET6 == address->ipv4.sin_family);
    assert(AF_INET6 == address->ipv6.sin6_family);
    assert(AF_INET6 == address->local.sun_family);
    assert(AF_INET6 == address->storage.ss_family);
    assert(IN6_ADDR_EQ(&in6addr_loopback, &address->ipv6.sin6_addr));
    assert(htons(8080) == address->ipv6.sin6_port);
    assert(STR_EQ("::1:8080", sa_str(address)));


@@ 86,6 89,7 @@ test_sa_alloc_from_sockaddr_in6(void)
    assert(AF_INET6 == address->ipv4.sin_family);
    assert(AF_INET6 == address->ipv6.sin6_family);
    assert(AF_INET6 == address->local.sun_family);
    assert(AF_INET6 == address->storage.ss_family);
    assert(IN6_ADDR_EQ(&in6addr_loopback, &address->ipv6.sin6_addr));
    assert(htons(8080) == address->ipv6.sin6_port);
    assert(STR_EQ("::1:8080", sa_str(address)));


@@ 104,6 108,7 @@ test_sa_alloc_from_path(void)
    assert(AF_UNIX == address->ipv4.sin_family);
    assert(AF_UNIX == address->ipv6.sin6_family);
    assert(AF_UNIX == address->local.sun_family);
    assert(AF_UNIX == address->storage.ss_family);
    assert(STR_EQ("/var/dood/http", address->local.sun_path));
    assert(STR_EQ("/var/dood/http", sa_str(address)));



@@ 125,6 130,7 @@ test_sa_alloc_from_path_when_too_long(void)
{
    char const *too_long = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
    assert(strlen(too_long) > UNIX_PATH_MAX);

    struct sa_address *address = sa_alloc_from_path(too_long);

    assert(!address);


@@ 146,6 152,7 @@ test_sa_alloc_from_sockaddr_un(void)
    assert(AF_UNIX == address->ipv4.sin_family);
    assert(AF_UNIX == address->ipv6.sin6_family);
    assert(AF_UNIX == address->local.sun_family);
    assert(AF_UNIX == address->storage.ss_family);
    assert(STR_EQ("/var/dood/http", address->local.sun_path));
    assert(STR_EQ("/var/dood/http", sa_str(address)));



@@ 154,7 161,7 @@ test_sa_alloc_from_sockaddr_un(void)


static void
test_sa_str_for_generic(void)
test_sa_str_for_unspecified_family(void)
{
    struct sa_address generic_address = {
            .generic = {


@@ 193,6 200,6 @@ main(int argc, char *argv[])
    test_sa_alloc_from_path_when_too_long();
    test_sa_alloc_from_sockaddr_un();

    test_sa_str_for_generic();
    test_sa_str_for_unspecified_family();
    test_sa_str_for_unsupported_family();
}