~donmcc/ood

fbeee83240f3933f3233a04ae016a7f3b627d5d2 — Don McCaughey 10 months ago feb96d8
Change `sa_address` from a struct into a union.

The struct only held the anonymous union after removing the `formatted`
field, so simplify.
4 files changed, 51 insertions(+), 53 deletions(-)

M src/libood/ood/inet/socket.h
M src/sa/sa.c
M src/sa/sa.h
M src/sa/sa_tests.c
M src/libood/ood/inet/socket.h => src/libood/ood/inet/socket.h +2 -2
@@ 8,7 8,7 @@
#define OOD_INET_SOCKET_FLAGS_LISTENING 0x00000001


struct sa_address;
union sa_address;
struct ood_inet_socket;




@@ 19,7 19,7 @@ typedef void
struct ood_inet_socket {
    int fd;
    uint32_t flags;
    struct sa_address *address;
    union sa_address *address;
    void *data;
    ood_inet_socket_fn *on_error;
    ood_inet_socket_fn *on_read;

M src/sa/sa.c => src/sa/sa.c +13 -13
@@ 6,7 6,7 @@
#include <stdio.h>


struct sa_address *
union sa_address *
sa_alloc_from_ipv4_address_and_port(in_addr_t in_addr, in_port_t in_port)
{
    struct sockaddr_in ipv4_address = {


@@ 18,10 18,10 @@ sa_alloc_from_ipv4_address_and_port(in_addr_t in_addr, in_port_t in_port)
}


struct sa_address *
union sa_address *
sa_alloc_from_sockaddr_in(struct sockaddr_in const *ipv4_address)
{
    struct sa_address *address = calloc(1, sizeof(struct sa_address));
    union sa_address *address = calloc(1, sizeof(union sa_address));
    if (!address) goto fail;

    address->ipv4 = *ipv4_address;


@@ 42,7 42,7 @@ fail:
}


struct sa_address *
union sa_address *
sa_alloc_from_ipv6_address_and_port(struct in6_addr in6_addr, in_port_t in_port)
{
    struct sockaddr_in6 inet6 = {


@@ 54,10 54,10 @@ sa_alloc_from_ipv6_address_and_port(struct in6_addr in6_addr, in_port_t in_port)
}


struct sa_address *
union sa_address *
sa_alloc_from_sockaddr_in6(struct sockaddr_in6 const *ipv6_address)
{
    struct sa_address *address = calloc(1, sizeof(struct sa_address));
    union sa_address *address = calloc(1, sizeof(union sa_address));
    if (!address) goto fail;

    address->ipv6 = *ipv6_address;


@@ 78,7 78,7 @@ fail:
}


struct sa_address *
union sa_address *
sa_alloc_from_path(char const *path)
{
    if (!path) return NULL;


@@ 92,10 92,10 @@ sa_alloc_from_path(char const *path)
}


struct sa_address *
union sa_address *
sa_alloc_from_sockaddr_un(struct sockaddr_un const *local_address)
{
    struct sa_address *address = calloc(1, sizeof(struct sa_address));
    union sa_address *address = calloc(1, sizeof(union sa_address));
    if (!address) return NULL;

    address->local = *local_address;


@@ 105,7 105,7 @@ sa_alloc_from_sockaddr_un(struct sockaddr_un const *local_address)


socklen_t
sa_len(struct sa_address const *address)
sa_len(union sa_address const *address)
{
    switch (address->generic.sa_family) {
        case AF_UNSPEC: return sizeof(struct sockaddr_storage);


@@ 119,7 119,7 @@ sa_len(struct sa_address const *address)


char const *
sa_str(struct sa_address const *address)
sa_str(union sa_address const *address)
{
    switch (address->generic.sa_family) {
        case AF_UNSPEC: return "(unspecified)";


@@ 131,7 131,7 @@ sa_str(struct sa_address const *address)
}

extern int
sa_bind(int socket, struct sa_address const *address);
sa_bind(int socket, union sa_address const *address);

extern int
sa_connect(int socket, struct sa_address const *address);
sa_connect(int socket, union sa_address const *address);

M src/sa/sa.h => src/sa/sa.h +21 -23
@@ 25,56 25,54 @@
#endif


struct sa_address {
    union {
        struct sockaddr generic;
        struct {
            struct sockaddr_in ipv4;
            char ipv4_string[SA_IPV4_STR_SIZE];
        };
        struct {
            struct sockaddr_in6 ipv6;
            char ipv6_string[SA_IPV6_STR_SIZE];
        };
        struct sockaddr_un local;
        struct sockaddr_storage storage;
union sa_address {
    struct sockaddr generic;
    struct {
        struct sockaddr_in ipv4;
        char ipv4_string[SA_IPV4_STR_SIZE];
    };
    struct {
        struct sockaddr_in6 ipv6;
        char ipv6_string[SA_IPV6_STR_SIZE];
    };
    struct sockaddr_un local;
    struct sockaddr_storage storage;
};


struct sa_address *
union sa_address *
sa_alloc_from_ipv4_address_and_port(in_addr_t in_addr, in_port_t in_port);

struct sa_address *
union sa_address *
sa_alloc_from_sockaddr_in(struct sockaddr_in const *ipv4_address);

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

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

struct sa_address *
union sa_address *
sa_alloc_from_path(char const *path);

struct sa_address *
union sa_address *
sa_alloc_from_sockaddr_un(struct sockaddr_un const *local_address);

socklen_t
sa_len(struct sa_address const *address);
sa_len(union sa_address const *address);

char const *
sa_str(struct sa_address const *address);
sa_str(union sa_address const *address);


inline int
sa_bind(int socket, struct sa_address const *address)
sa_bind(int socket, union sa_address const *address)
{
    return bind(socket, &address->generic, sa_len(address));
}

inline int
sa_connect(int socket, struct sa_address const *address)
sa_connect(int socket, union sa_address const *address)
{
    return connect(socket, &address->generic, sa_len(address));
}

M src/sa/sa_tests.c => src/sa/sa_tests.c +15 -15
@@ 11,7 11,7 @@
static void
test_sa_alloc_from_ipv4_address_and_port(void)
{
    struct sa_address *address = sa_alloc_from_ipv4_address_and_port(INADDR_LOOPBACK, 8080);
    union sa_address *address = sa_alloc_from_ipv4_address_and_port(INADDR_LOOPBACK, 8080);

    assert(address);
    assert(AF_INET == address->generic.sa_family);


@@ 38,7 38,7 @@ test_sa_alloc_from_sockaddr_in(void)
            .sin_port=htons(8080),
    };

    struct sa_address *address = sa_alloc_from_sockaddr_in(&ipv4);
    union sa_address *address = sa_alloc_from_sockaddr_in(&ipv4);

    assert(address);
    assert(AF_INET == address->generic.sa_family);


@@ 57,7 57,7 @@ test_sa_alloc_from_sockaddr_in(void)
static void
test_sa_alloc_from_ipv6_address_and_port(void)
{
    struct sa_address *address = sa_alloc_from_ipv6_address_and_port(in6addr_loopback, 8080);
    union sa_address *address = sa_alloc_from_ipv6_address_and_port(in6addr_loopback, 8080);

    assert(address);
    assert(AF_INET6 == address->generic.sa_family);


@@ 82,7 82,7 @@ test_sa_alloc_from_sockaddr_in6(void)
            .sin6_port=htons(8080),
    };

    struct sa_address *address = sa_alloc_from_sockaddr_in6(&ipv6);
    union sa_address *address = sa_alloc_from_sockaddr_in6(&ipv6);

    assert(address);
    assert(AF_INET6 == address->generic.sa_family);


@@ 101,7 101,7 @@ test_sa_alloc_from_sockaddr_in6(void)
static void
test_sa_alloc_from_path(void)
{
    struct sa_address *address = sa_alloc_from_path("/var/dood/http");
    union sa_address *address = sa_alloc_from_path("/var/dood/http");

    assert(address);
    assert(AF_UNIX == address->generic.sa_family);


@@ 119,7 119,7 @@ test_sa_alloc_from_path(void)
static void
test_sa_alloc_from_path_when_null(void)
{
    struct sa_address *address = sa_alloc_from_path(NULL);
    union sa_address *address = sa_alloc_from_path(NULL);

    assert(!address);
}


@@ 131,7 131,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);
    union sa_address *address = sa_alloc_from_path(too_long);

    assert(!address);
}


@@ 145,7 145,7 @@ test_sa_alloc_from_sockaddr_un(void)
            .sun_path = "/var/dood/http",
    };

    struct sa_address *address = sa_alloc_from_sockaddr_un(&local);
    union sa_address *address = sa_alloc_from_sockaddr_un(&local);

    assert(address);
    assert(AF_UNIX == address->generic.sa_family);


@@ 163,7 163,7 @@ test_sa_alloc_from_sockaddr_un(void)
static void
test_sa_len_for_unspecified(void)
{
    struct sa_address address = {
    union sa_address address = {
            .generic = {
                    .sa_family = AF_UNSPEC,
            }


@@ 176,7 176,7 @@ test_sa_len_for_unspecified(void)
static void
test_sa_len_for_ipv4(void)
{
    struct sa_address *address = sa_alloc_from_ipv4_address_and_port(INADDR_LOOPBACK, 8080);
    union sa_address *address = sa_alloc_from_ipv4_address_and_port(INADDR_LOOPBACK, 8080);

    assert(sizeof(struct sockaddr_in) == sa_len(address));



@@ 187,7 187,7 @@ test_sa_len_for_ipv4(void)
static void
test_sa_len_for_ipv6(void)
{
    struct sa_address *address = sa_alloc_from_ipv6_address_and_port(in6addr_loopback, 8080);
    union sa_address *address = sa_alloc_from_ipv6_address_and_port(in6addr_loopback, 8080);

    assert(sizeof(struct sockaddr_in6) == sa_len(address));



@@ 198,7 198,7 @@ test_sa_len_for_ipv6(void)
static void
test_sa_len_for_unix(void)
{
    struct sa_address *address = sa_alloc_from_path("/var/dood/http");
    union sa_address *address = sa_alloc_from_path("/var/dood/http");

    assert(sizeof(struct sockaddr_un) == sa_len(address));



@@ 209,7 209,7 @@ test_sa_len_for_unix(void)
static void
test_sa_len_for_unsupported(void)
{
    struct sa_address address = {
    union sa_address address = {
            .generic = {
                    .sa_family = 0xff,
            }


@@ 222,7 222,7 @@ test_sa_len_for_unsupported(void)
static void
test_sa_str_for_unspecified(void)
{
    struct sa_address address = {
    union sa_address address = {
            .generic = {
                    .sa_family = AF_UNSPEC,
            }


@@ 235,7 235,7 @@ test_sa_str_for_unspecified(void)
static void
test_sa_str_for_unsupported(void)
{
    struct sa_address address = {
    union sa_address address = {
            .generic = {
                    .sa_family = 0xff,
            }