~donmcc/ood

1a1afc8286f4542d469bb9331be97e89bf8cc3ba — Don McCaughey 1 year, 8 months ago af17d36
Renaming "sa" functions to match parameters.

Change `sa_alloc_from_ipv4_address_and_port()` to
`sa_alloc_from_in_addr_and_port()`.

Change `sa_alloc_from_ipv6_address_and_port()` to
`sa_alloc_from_in6_addr_and_port()`.

Change `sa_alloc_from_local_path()` to `sa_alloc_from_path()`.

In the `sa_address` union, shorten fields `ipv4_string` and
`ipv6_string` to `ipv4_str` and `ipv6_str` respectively.

Change test names to match.
4 files changed, 35 insertions(+), 35 deletions(-)

M src/libood/ood/inet/socket.c
M src/sa/sa.c
M src/sa/sa.h
M src/sa/sa_tests.c
M src/libood/ood/inet/socket.c => src/libood/ood/inet/socket.c +1 -1
@@ 71,7 71,7 @@ ood_inet_socket_alloc(in_addr_t in_addr, in_port_t in_port)
    if (!listener) return NULL;

    listener->fd = -1;
    listener->address = sa_alloc_from_ipv4_address_and_port(in_addr, in_port);
    listener->address = sa_alloc_from_in_addr_and_port(in_addr, in_port);
    if (!listener->address) {
        free(listener);
        return NULL;

M src/sa/sa.c => src/sa/sa.c +9 -9
@@ 7,7 7,7 @@


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


@@ 19,7 19,7 @@ sa_alloc_from_ipv4_address_and_port(in_addr_t in_addr, in_port_t in_port)


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


@@ 31,7 31,7 @@ sa_alloc_from_ipv6_address_and_port(struct in6_addr in6_addr, in_port_t in_port)


union sa_address *
sa_alloc_from_local_path(char const *path)
sa_alloc_from_path(char const *path)
{
    if (!path) return NULL;
    if (strlen(path) >= UNIX_PATH_MAX) return NULL;


@@ 64,10 64,10 @@ sa_alloc_from_sockaddr_in(struct sockaddr_in const *ipv4_address)

    address->ipv4 = *ipv4_address;
    const char *wrote_address = inet_ntop(AF_INET, &ipv4_address->sin_addr,
                                          address->ipv4_string, SA_IPV4_STR_SIZE);
                                          address->ipv4_str, SA_IPV4_STR_SIZE);
    if (!wrote_address) goto fail;

    char *s = address->ipv4_string + strlen(address->ipv4_string);
    char *s = address->ipv4_str + strlen(address->ipv4_str);
    int wrote_port = sprintf(s, ":%i",ntohs(ipv4_address->sin_port));
    if (wrote_port < 0) goto fail;



@@ 89,10 89,10 @@ sa_alloc_from_sockaddr_in6(struct sockaddr_in6 const *ipv6_address)
    address->ipv6 = *ipv6_address;

    char const *wrote_address = inet_ntop(AF_INET6, &ipv6_address->sin6_addr,
                                          address->ipv6_string, SA_IPV6_STR_SIZE);
                                          address->ipv6_str, SA_IPV6_STR_SIZE);
    if (!wrote_address) goto fail;

    char *s = address->ipv6_string + strlen(address->ipv6_string);
    char *s = address->ipv6_str + strlen(address->ipv6_str);
    int wrote_port = sprintf(s, ":%i", ntohs(ipv6_address->sin6_port));
    if (wrote_port < 0) goto fail;



@@ 135,8 135,8 @@ sa_str(union sa_address const *address)
    switch (address->generic.sa_family) {
        case AF_UNSPEC: return "(unspecified)";
        case AF_UNIX: return address->local.sun_path;
        case AF_INET: return address->ipv4_string;
        case AF_INET6: return address->ipv6_string;
        case AF_INET: return address->ipv4_str;
        case AF_INET6: return address->ipv6_str;
        default: return "(unsupported)";
    }
}

M src/sa/sa.h => src/sa/sa.h +5 -5
@@ 36,11 36,11 @@ union sa_address {
    struct sockaddr generic;
    struct {
        struct sockaddr_in ipv4;
        char ipv4_string[SA_IPV4_STR_SIZE];
        char ipv4_str[SA_IPV4_STR_SIZE];
    };
    struct {
        struct sockaddr_in6 ipv6;
        char ipv6_string[SA_IPV6_STR_SIZE];
        char ipv6_str[SA_IPV6_STR_SIZE];
    };
    struct sockaddr_un local;
    struct sockaddr_storage storage;


@@ 50,13 50,13 @@ union sa_address {
// # Allocate socket address from components

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

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

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


// # Allocate socket address from a `sockaddr`-type struct.

M src/sa/sa_tests.c => src/sa/sa_tests.c +20 -20
@@ 9,9 9,9 @@


static void
test_sa_alloc_from_ipv4_address_and_port(void)
test_sa_alloc_from_in_addr_and_port(void)
{
    union sa_address *address = sa_alloc_from_ipv4_address_and_port(INADDR_LOOPBACK, 8080);
    union sa_address *address = sa_alloc_from_in_addr_and_port(INADDR_LOOPBACK, 8080);

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


@@ 28,9 28,9 @@ test_sa_alloc_from_ipv4_address_and_port(void)


static void
test_sa_alloc_from_ipv6_address_and_port(void)
test_sa_alloc_from_in6_addr_and_port(void)
{
    union sa_address *address = sa_alloc_from_ipv6_address_and_port(in6addr_loopback, 8080);
    union sa_address *address = sa_alloc_from_in6_addr_and_port(in6addr_loopback, 8080);

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


@@ 47,9 47,9 @@ test_sa_alloc_from_ipv6_address_and_port(void)


static void
test_sa_alloc_from_local_path(void)
test_sa_alloc_from_path(void)
{
    union sa_address *address = sa_alloc_from_local_path("/var/dood/http");
    union sa_address *address = sa_alloc_from_path("/var/dood/http");

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


@@ 65,21 65,21 @@ test_sa_alloc_from_local_path(void)


static void
test_sa_alloc_from_local_path_when_null(void)
test_sa_alloc_from_path_when_null(void)
{
    union sa_address *address = sa_alloc_from_local_path(NULL);
    union sa_address *address = sa_alloc_from_path(NULL);

    assert(!address);
}


static void
test_sa_alloc_from_local_path_when_too_long(void)
test_sa_alloc_from_path_when_too_long(void)
{
    char const *too_long = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
    assert(strlen(too_long) > UNIX_PATH_MAX);

    union sa_address *address = sa_alloc_from_local_path(too_long);
    union sa_address *address = sa_alloc_from_path(too_long);

    assert(!address);
}


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

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



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

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



@@ 300,9 300,9 @@ test_sa_len_for_ipv6(void)


static void
test_sa_len_for_unix(void)
test_sa_len_for_local(void)
{
    union sa_address *address = sa_alloc_from_local_path("/var/dood/http");
    union sa_address *address = sa_alloc_from_path("/var/dood/http");

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



@@ 352,11 352,11 @@ test_sa_str_for_unsupported(void)
int
main(int argc, char *argv[])
{
    test_sa_alloc_from_ipv4_address_and_port();
    test_sa_alloc_from_ipv6_address_and_port();
    test_sa_alloc_from_local_path();
    test_sa_alloc_from_local_path_when_null();
    test_sa_alloc_from_local_path_when_too_long();
    test_sa_alloc_from_in_addr_and_port();
    test_sa_alloc_from_in6_addr_and_port();
    test_sa_alloc_from_path();
    test_sa_alloc_from_path_when_null();
    test_sa_alloc_from_path_when_too_long();

    test_sa_alloc_from_sockaddr_for_unspecified();
    test_sa_alloc_from_sockaddr_for_ipv4();


@@ 371,7 371,7 @@ main(int argc, char *argv[])
    test_sa_len_for_unspecified();
    test_sa_len_for_ipv4();
    test_sa_len_for_ipv6();
    test_sa_len_for_unix();
    test_sa_len_for_local();
    test_sa_len_for_unsupported();

    test_sa_str_for_unspecified();