/*
 * Copyright (c) 2026 Arkadiusz Juszczyk <ark@wobblyhead.net>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "botblockd.h"

/*
 * parse_ipv4 - parse the first whitespace-delimited token as IPv4
 *
 * The token ends at end of string or at the first space, tab or
 * newline.  Output is in network byte order, as inet_pton(3) gives it.
 *
 * Parameters:
 *   s   - start of string (not modified)
 *   out - output for parsed IP (network byte order)
 *
 * Returns: 0 on success, -1 if token is empty, too long, or invalid
 */
int
parse_ipv4(const char *s, uint32_t *out)
{
    struct in_addr ia;
    char buf[INET_ADDRSTRLEN];
    size_t n;

    n = strcspn(s, " \t\n");
    if (n == 0 || n >= sizeof(buf))
        return (-1);
    memcpy(buf, s, n);
    buf[n] = '\0';
    if (inet_pton(AF_INET, buf, &ia) != 1)
        return (-1);
    *out = ia.s_addr;
    return (0);
}

/*
 * parse_cidr - parse "A.B.C.D" or "A.B.C.D/N" into network and mask
 *
 * A bare address is treated as a single host (/32).  A prefix length N
 * must be 1..32: /0 would whitelist every address and is rejected as
 * too wide to be intentional.  Host bits below the prefix are accepted
 * and masked off, so a non-canonical entry such as 10.0.0.5/24 is
 * stored as the 10.0.0.0/24 network.  Outputs are network byte order.
 *
 * Parameters:
 *   s    - entry text (a whole whitelist line, no surrounding space)
 *   net  - output: network address with host bits cleared
 *   mask - output: netmask
 *
 * Returns: 0 on success, -1 on a malformed address or prefix
 */
int
parse_cidr(const char *s, uint32_t *net, uint32_t *mask)
{
    struct in_addr ia;
    char buf[INET_ADDRSTRLEN + 3];      /* address, "/" and 2 digits */
    char *endp, *slash;
    long prefix;

    if (strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
        return (-1);
    prefix = 32;
    slash = strchr(buf, '/');
    if (slash != NULL) {
        *slash = '\0';
        errno = 0;
        prefix = strtol(slash + 1, &endp, 10);
        if (endp == slash + 1 || *endp != '\0' || errno != 0 ||
            prefix < 1 || prefix > 32)
            return (-1);
    }
    if (inet_pton(AF_INET, buf, &ia) != 1)
        return (-1);

    /* prefix is 1..32, so the shift is 0..31 and defined */
    *mask = htonl(0xffffffffu << (32 - prefix));
    *net = ia.s_addr & *mask;
    return (0);
}

/*
 * ip_str - format an IP (network byte order) as dotted-quad text
 *
 * The text is kept in a static buffer that the next call overwrites.
 *
 * Parameters:
 *   ip - IPv4 address (network byte order)
 *
 * Returns: the text, or "?" if the address cannot be formatted
 */
const char *
ip_str(uint32_t ip)
{
    static char buf[INET_ADDRSTRLEN];
    struct in_addr ia;

    ia.s_addr = ip;
    if (inet_ntop(AF_INET, &ia, buf, sizeof(buf)) == NULL)
        return ("?");
    return (buf);
}
