comparison src/ip.c @ 0:9f8d79c98fd2 0.4.0

initial public release
author Arkadiusz Juszczyk <ark@wobblyhead.net>
date 2026-10-26 11:29 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9f8d79c98fd2
1 /*
2 * Copyright (c) 2026 Arkadiusz Juszczyk <ark@wobblyhead.net>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 #include <sys/types.h>
8 #include <sys/socket.h>
9
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
12
13 #include <errno.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "botblockd.h"
19
20 /*
21 * parse_ipv4 - parse the first whitespace-delimited token as IPv4
22 *
23 * The token ends at end of string or at the first space, tab or
24 * newline. Output is in network byte order, as inet_pton(3) gives it.
25 *
26 * Parameters:
27 * s - start of string (not modified)
28 * out - output for parsed IP (network byte order)
29 *
30 * Returns: 0 on success, -1 if token is empty, too long, or invalid
31 */
32 int
33 parse_ipv4(const char *s, uint32_t *out)
34 {
35 struct in_addr ia;
36 char buf[INET_ADDRSTRLEN];
37 size_t n;
38
39 n = strcspn(s, " \t\n");
40 if (n == 0 || n >= sizeof(buf))
41 return (-1);
42 memcpy(buf, s, n);
43 buf[n] = '\0';
44 if (inet_pton(AF_INET, buf, &ia) != 1)
45 return (-1);
46 *out = ia.s_addr;
47 return (0);
48 }
49
50 /*
51 * parse_cidr - parse "A.B.C.D" or "A.B.C.D/N" into network and mask
52 *
53 * A bare address is treated as a single host (/32). A prefix length N
54 * must be 1..32: /0 would whitelist every address and is rejected as
55 * too wide to be intentional. Host bits below the prefix are accepted
56 * and masked off, so a non-canonical entry such as 10.0.0.5/24 is
57 * stored as the 10.0.0.0/24 network. Outputs are network byte order.
58 *
59 * Parameters:
60 * s - entry text (a whole whitelist line, no surrounding space)
61 * net - output: network address with host bits cleared
62 * mask - output: netmask
63 *
64 * Returns: 0 on success, -1 on a malformed address or prefix
65 */
66 int
67 parse_cidr(const char *s, uint32_t *net, uint32_t *mask)
68 {
69 struct in_addr ia;
70 char buf[INET_ADDRSTRLEN + 3]; /* address, "/" and 2 digits */
71 char *endp, *slash;
72 long prefix;
73
74 if (strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
75 return (-1);
76 prefix = 32;
77 slash = strchr(buf, '/');
78 if (slash != NULL) {
79 *slash = '\0';
80 errno = 0;
81 prefix = strtol(slash + 1, &endp, 10);
82 if (endp == slash + 1 || *endp != '\0' || errno != 0 ||
83 prefix < 1 || prefix > 32)
84 return (-1);
85 }
86 if (inet_pton(AF_INET, buf, &ia) != 1)
87 return (-1);
88
89 /* prefix is 1..32, so the shift is 0..31 and defined */
90 *mask = htonl(0xffffffffu << (32 - prefix));
91 *net = ia.s_addr & *mask;
92 return (0);
93 }
94
95 /*
96 * ip_str - format an IP (network byte order) as dotted-quad text
97 *
98 * The text is kept in a static buffer that the next call overwrites.
99 *
100 * Parameters:
101 * ip - IPv4 address (network byte order)
102 *
103 * Returns: the text, or "?" if the address cannot be formatted
104 */
105 const char *
106 ip_str(uint32_t ip)
107 {
108 static char buf[INET_ADDRSTRLEN];
109 struct in_addr ia;
110
111 ia.s_addr = ip;
112 if (inet_ntop(AF_INET, &ia, buf, sizeof(buf)) == NULL)
113 return ("?");
114 return (buf);
115 }