1*535af610SEd Maste /* $OpenBSD: addr.c,v 1.7 2023/03/27 03:31:05 djm Exp $ */
219261079SEd Maste
319261079SEd Maste /*
419261079SEd Maste * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
519261079SEd Maste *
619261079SEd Maste * Permission to use, copy, modify, and distribute this software for any
719261079SEd Maste * purpose with or without fee is hereby granted, provided that the above
819261079SEd Maste * copyright notice and this permission notice appear in all copies.
919261079SEd Maste *
1019261079SEd Maste * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1119261079SEd Maste * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1219261079SEd Maste * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1319261079SEd Maste * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1419261079SEd Maste * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1519261079SEd Maste * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1619261079SEd Maste * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1719261079SEd Maste */
1819261079SEd Maste
1919261079SEd Maste #include "includes.h"
2019261079SEd Maste
2119261079SEd Maste #include <sys/types.h>
2219261079SEd Maste #include <sys/socket.h>
2319261079SEd Maste #include <netinet/in.h>
2419261079SEd Maste #include <arpa/inet.h>
2519261079SEd Maste
2619261079SEd Maste #include <netdb.h>
2719261079SEd Maste #include <string.h>
2819261079SEd Maste #include <stdlib.h>
2919261079SEd Maste #include <stdio.h>
3019261079SEd Maste
3119261079SEd Maste #include "addr.h"
3219261079SEd Maste
3319261079SEd Maste #define _SA(x) ((struct sockaddr *)(x))
3419261079SEd Maste
3519261079SEd Maste int
addr_unicast_masklen(int af)3619261079SEd Maste addr_unicast_masklen(int af)
3719261079SEd Maste {
3819261079SEd Maste switch (af) {
3919261079SEd Maste case AF_INET:
4019261079SEd Maste return 32;
4119261079SEd Maste case AF_INET6:
4219261079SEd Maste return 128;
4319261079SEd Maste default:
4419261079SEd Maste return -1;
4519261079SEd Maste }
4619261079SEd Maste }
4719261079SEd Maste
4819261079SEd Maste static inline int
masklen_valid(int af,u_int masklen)4919261079SEd Maste masklen_valid(int af, u_int masklen)
5019261079SEd Maste {
5119261079SEd Maste switch (af) {
5219261079SEd Maste case AF_INET:
5319261079SEd Maste return masklen <= 32 ? 0 : -1;
5419261079SEd Maste case AF_INET6:
5519261079SEd Maste return masklen <= 128 ? 0 : -1;
5619261079SEd Maste default:
5719261079SEd Maste return -1;
5819261079SEd Maste }
5919261079SEd Maste }
6019261079SEd Maste
6119261079SEd Maste int
addr_xaddr_to_sa(const struct xaddr * xa,struct sockaddr * sa,socklen_t * len,u_int16_t port)6219261079SEd Maste addr_xaddr_to_sa(const struct xaddr *xa, struct sockaddr *sa, socklen_t *len,
6319261079SEd Maste u_int16_t port)
6419261079SEd Maste {
6519261079SEd Maste struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
6619261079SEd Maste struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
6719261079SEd Maste
6819261079SEd Maste if (xa == NULL || sa == NULL || len == NULL)
6919261079SEd Maste return -1;
7019261079SEd Maste
7119261079SEd Maste switch (xa->af) {
7219261079SEd Maste case AF_INET:
7319261079SEd Maste if (*len < sizeof(*in4))
7419261079SEd Maste return -1;
7519261079SEd Maste memset(sa, '\0', sizeof(*in4));
7619261079SEd Maste *len = sizeof(*in4);
7719261079SEd Maste #ifdef SOCK_HAS_LEN
7819261079SEd Maste in4->sin_len = sizeof(*in4);
7919261079SEd Maste #endif
8019261079SEd Maste in4->sin_family = AF_INET;
8119261079SEd Maste in4->sin_port = htons(port);
8219261079SEd Maste memcpy(&in4->sin_addr, &xa->v4, sizeof(in4->sin_addr));
8319261079SEd Maste break;
8419261079SEd Maste case AF_INET6:
8519261079SEd Maste if (*len < sizeof(*in6))
8619261079SEd Maste return -1;
8719261079SEd Maste memset(sa, '\0', sizeof(*in6));
8819261079SEd Maste *len = sizeof(*in6);
8919261079SEd Maste #ifdef SOCK_HAS_LEN
9019261079SEd Maste in6->sin6_len = sizeof(*in6);
9119261079SEd Maste #endif
9219261079SEd Maste in6->sin6_family = AF_INET6;
9319261079SEd Maste in6->sin6_port = htons(port);
9419261079SEd Maste memcpy(&in6->sin6_addr, &xa->v6, sizeof(in6->sin6_addr));
9519261079SEd Maste #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
9619261079SEd Maste in6->sin6_scope_id = xa->scope_id;
9719261079SEd Maste #endif
9819261079SEd Maste break;
9919261079SEd Maste default:
10019261079SEd Maste return -1;
10119261079SEd Maste }
10219261079SEd Maste return 0;
10319261079SEd Maste }
10419261079SEd Maste
10519261079SEd Maste /*
10619261079SEd Maste * Convert struct sockaddr to struct xaddr
10719261079SEd Maste * Returns 0 on success, -1 on failure.
10819261079SEd Maste */
10919261079SEd Maste int
addr_sa_to_xaddr(struct sockaddr * sa,socklen_t slen,struct xaddr * xa)11019261079SEd Maste addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa)
11119261079SEd Maste {
11219261079SEd Maste struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
11319261079SEd Maste struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
11419261079SEd Maste
11519261079SEd Maste memset(xa, '\0', sizeof(*xa));
11619261079SEd Maste
11719261079SEd Maste switch (sa->sa_family) {
11819261079SEd Maste case AF_INET:
11919261079SEd Maste if (slen < (socklen_t)sizeof(*in4))
12019261079SEd Maste return -1;
12119261079SEd Maste xa->af = AF_INET;
12219261079SEd Maste memcpy(&xa->v4, &in4->sin_addr, sizeof(xa->v4));
12319261079SEd Maste break;
12419261079SEd Maste case AF_INET6:
12519261079SEd Maste if (slen < (socklen_t)sizeof(*in6))
12619261079SEd Maste return -1;
12719261079SEd Maste xa->af = AF_INET6;
12819261079SEd Maste memcpy(&xa->v6, &in6->sin6_addr, sizeof(xa->v6));
12919261079SEd Maste #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
13019261079SEd Maste xa->scope_id = in6->sin6_scope_id;
13119261079SEd Maste #endif
13219261079SEd Maste break;
13319261079SEd Maste default:
13419261079SEd Maste return -1;
13519261079SEd Maste }
13619261079SEd Maste
13719261079SEd Maste return 0;
13819261079SEd Maste }
13919261079SEd Maste
14019261079SEd Maste int
addr_invert(struct xaddr * n)14119261079SEd Maste addr_invert(struct xaddr *n)
14219261079SEd Maste {
14319261079SEd Maste int i;
14419261079SEd Maste
14519261079SEd Maste if (n == NULL)
14619261079SEd Maste return -1;
14719261079SEd Maste
14819261079SEd Maste switch (n->af) {
14919261079SEd Maste case AF_INET:
15019261079SEd Maste n->v4.s_addr = ~n->v4.s_addr;
15119261079SEd Maste return 0;
15219261079SEd Maste case AF_INET6:
15319261079SEd Maste for (i = 0; i < 4; i++)
15419261079SEd Maste n->addr32[i] = ~n->addr32[i];
15519261079SEd Maste return 0;
15619261079SEd Maste default:
15719261079SEd Maste return -1;
15819261079SEd Maste }
15919261079SEd Maste }
16019261079SEd Maste
16119261079SEd Maste /*
16219261079SEd Maste * Calculate a netmask of length 'l' for address family 'af' and
16319261079SEd Maste * store it in 'n'.
16419261079SEd Maste * Returns 0 on success, -1 on failure.
16519261079SEd Maste */
16619261079SEd Maste int
addr_netmask(int af,u_int l,struct xaddr * n)16719261079SEd Maste addr_netmask(int af, u_int l, struct xaddr *n)
16819261079SEd Maste {
16919261079SEd Maste int i;
17019261079SEd Maste
17119261079SEd Maste if (masklen_valid(af, l) != 0 || n == NULL)
17219261079SEd Maste return -1;
17319261079SEd Maste
17419261079SEd Maste memset(n, '\0', sizeof(*n));
17519261079SEd Maste switch (af) {
17619261079SEd Maste case AF_INET:
17719261079SEd Maste n->af = AF_INET;
17819261079SEd Maste if (l == 0)
17919261079SEd Maste return 0;
18019261079SEd Maste n->v4.s_addr = htonl((0xffffffff << (32 - l)) & 0xffffffff);
18119261079SEd Maste return 0;
18219261079SEd Maste case AF_INET6:
18319261079SEd Maste n->af = AF_INET6;
18419261079SEd Maste for (i = 0; i < 4 && l >= 32; i++, l -= 32)
18519261079SEd Maste n->addr32[i] = 0xffffffffU;
18619261079SEd Maste if (i < 4 && l != 0)
18719261079SEd Maste n->addr32[i] = htonl((0xffffffff << (32 - l)) &
18819261079SEd Maste 0xffffffff);
18919261079SEd Maste return 0;
19019261079SEd Maste default:
19119261079SEd Maste return -1;
19219261079SEd Maste }
19319261079SEd Maste }
19419261079SEd Maste
19519261079SEd Maste int
addr_hostmask(int af,u_int l,struct xaddr * n)19619261079SEd Maste addr_hostmask(int af, u_int l, struct xaddr *n)
19719261079SEd Maste {
19819261079SEd Maste if (addr_netmask(af, l, n) == -1 || addr_invert(n) == -1)
19919261079SEd Maste return -1;
20019261079SEd Maste return 0;
20119261079SEd Maste }
20219261079SEd Maste
20319261079SEd Maste /*
20419261079SEd Maste * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
20519261079SEd Maste * Returns 0 on success, -1 on failure.
20619261079SEd Maste */
20719261079SEd Maste int
addr_and(struct xaddr * dst,const struct xaddr * a,const struct xaddr * b)20819261079SEd Maste addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
20919261079SEd Maste {
21019261079SEd Maste int i;
21119261079SEd Maste
21219261079SEd Maste if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
21319261079SEd Maste return -1;
21419261079SEd Maste
21519261079SEd Maste memcpy(dst, a, sizeof(*dst));
21619261079SEd Maste switch (a->af) {
21719261079SEd Maste case AF_INET:
21819261079SEd Maste dst->v4.s_addr &= b->v4.s_addr;
21919261079SEd Maste return 0;
22019261079SEd Maste case AF_INET6:
22119261079SEd Maste dst->scope_id = a->scope_id;
22219261079SEd Maste for (i = 0; i < 4; i++)
22319261079SEd Maste dst->addr32[i] &= b->addr32[i];
22419261079SEd Maste return 0;
22519261079SEd Maste default:
22619261079SEd Maste return -1;
22719261079SEd Maste }
22819261079SEd Maste }
22919261079SEd Maste
23019261079SEd Maste int
addr_or(struct xaddr * dst,const struct xaddr * a,const struct xaddr * b)231f374ba41SEd Maste addr_or(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
232f374ba41SEd Maste {
233f374ba41SEd Maste int i;
234f374ba41SEd Maste
235f374ba41SEd Maste if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
236f374ba41SEd Maste return (-1);
237f374ba41SEd Maste
238f374ba41SEd Maste memcpy(dst, a, sizeof(*dst));
239f374ba41SEd Maste switch (a->af) {
240f374ba41SEd Maste case AF_INET:
241f374ba41SEd Maste dst->v4.s_addr |= b->v4.s_addr;
242f374ba41SEd Maste return (0);
243f374ba41SEd Maste case AF_INET6:
244f374ba41SEd Maste for (i = 0; i < 4; i++)
245f374ba41SEd Maste dst->addr32[i] |= b->addr32[i];
246f374ba41SEd Maste return (0);
247f374ba41SEd Maste default:
248f374ba41SEd Maste return (-1);
249f374ba41SEd Maste }
250f374ba41SEd Maste }
251f374ba41SEd Maste
252f374ba41SEd Maste int
addr_cmp(const struct xaddr * a,const struct xaddr * b)25319261079SEd Maste addr_cmp(const struct xaddr *a, const struct xaddr *b)
25419261079SEd Maste {
25519261079SEd Maste int i;
25619261079SEd Maste
25719261079SEd Maste if (a->af != b->af)
25819261079SEd Maste return (a->af == AF_INET6 ? 1 : -1);
25919261079SEd Maste
26019261079SEd Maste switch (a->af) {
26119261079SEd Maste case AF_INET:
26219261079SEd Maste /*
26319261079SEd Maste * Can't just subtract here as 255.255.255.255 - 0.0.0.0 is
26419261079SEd Maste * too big to fit into a signed int
26519261079SEd Maste */
26619261079SEd Maste if (a->v4.s_addr == b->v4.s_addr)
26719261079SEd Maste return 0;
26819261079SEd Maste return (ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr) ? 1 : -1);
2691323ec57SEd Maste case AF_INET6:
27019261079SEd Maste /*
27119261079SEd Maste * Do this a byte at a time to avoid the above issue and
27219261079SEd Maste * any endian problems
27319261079SEd Maste */
27419261079SEd Maste for (i = 0; i < 16; i++)
27519261079SEd Maste if (a->addr8[i] - b->addr8[i] != 0)
27619261079SEd Maste return (a->addr8[i] - b->addr8[i]);
27719261079SEd Maste if (a->scope_id == b->scope_id)
27819261079SEd Maste return (0);
27919261079SEd Maste return (a->scope_id > b->scope_id ? 1 : -1);
28019261079SEd Maste default:
28119261079SEd Maste return (-1);
28219261079SEd Maste }
28319261079SEd Maste }
28419261079SEd Maste
28519261079SEd Maste int
addr_is_all0s(const struct xaddr * a)28619261079SEd Maste addr_is_all0s(const struct xaddr *a)
28719261079SEd Maste {
28819261079SEd Maste int i;
28919261079SEd Maste
29019261079SEd Maste switch (a->af) {
29119261079SEd Maste case AF_INET:
29219261079SEd Maste return (a->v4.s_addr == 0 ? 0 : -1);
2931323ec57SEd Maste case AF_INET6:
29419261079SEd Maste for (i = 0; i < 4; i++)
29519261079SEd Maste if (a->addr32[i] != 0)
29619261079SEd Maste return -1;
29719261079SEd Maste return 0;
29819261079SEd Maste default:
29919261079SEd Maste return -1;
30019261079SEd Maste }
30119261079SEd Maste }
30219261079SEd Maste
303f374ba41SEd Maste /* Increment the specified address. Note, does not do overflow checking */
304f374ba41SEd Maste void
addr_increment(struct xaddr * a)305f374ba41SEd Maste addr_increment(struct xaddr *a)
306f374ba41SEd Maste {
307f374ba41SEd Maste int i;
308f374ba41SEd Maste uint32_t n;
309f374ba41SEd Maste
310f374ba41SEd Maste switch (a->af) {
311f374ba41SEd Maste case AF_INET:
312f374ba41SEd Maste a->v4.s_addr = htonl(ntohl(a->v4.s_addr) + 1);
313f374ba41SEd Maste break;
314f374ba41SEd Maste case AF_INET6:
315f374ba41SEd Maste for (i = 0; i < 4; i++) {
316f374ba41SEd Maste /* Increment with carry */
317f374ba41SEd Maste n = ntohl(a->addr32[3 - i]) + 1;
318f374ba41SEd Maste a->addr32[3 - i] = htonl(n);
319f374ba41SEd Maste if (n != 0)
320f374ba41SEd Maste break;
321f374ba41SEd Maste }
322f374ba41SEd Maste break;
323f374ba41SEd Maste }
324f374ba41SEd Maste }
325f374ba41SEd Maste
32619261079SEd Maste /*
32719261079SEd Maste * Test whether host portion of address 'a', as determined by 'masklen'
32819261079SEd Maste * is all zeros.
3291323ec57SEd Maste * Returns 0 if host portion of address is all-zeros,
33019261079SEd Maste * -1 if not all zeros or on failure.
33119261079SEd Maste */
33219261079SEd Maste int
addr_host_is_all0s(const struct xaddr * a,u_int masklen)33319261079SEd Maste addr_host_is_all0s(const struct xaddr *a, u_int masklen)
33419261079SEd Maste {
33519261079SEd Maste struct xaddr tmp_addr, tmp_mask, tmp_result;
33619261079SEd Maste
33719261079SEd Maste memcpy(&tmp_addr, a, sizeof(tmp_addr));
33819261079SEd Maste if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
33919261079SEd Maste return -1;
34019261079SEd Maste if (addr_and(&tmp_result, &tmp_addr, &tmp_mask) == -1)
34119261079SEd Maste return -1;
34219261079SEd Maste return addr_is_all0s(&tmp_result);
34319261079SEd Maste }
34419261079SEd Maste
345f374ba41SEd Maste #if 0
346f374ba41SEd Maste int
347f374ba41SEd Maste addr_host_to_all0s(struct xaddr *a, u_int masklen)
348f374ba41SEd Maste {
349f374ba41SEd Maste struct xaddr tmp_mask;
350f374ba41SEd Maste
351f374ba41SEd Maste if (addr_netmask(a->af, masklen, &tmp_mask) == -1)
352f374ba41SEd Maste return (-1);
353f374ba41SEd Maste if (addr_and(a, a, &tmp_mask) == -1)
354f374ba41SEd Maste return (-1);
355f374ba41SEd Maste return (0);
356f374ba41SEd Maste }
357f374ba41SEd Maste #endif
358f374ba41SEd Maste
359f374ba41SEd Maste int
addr_host_to_all1s(struct xaddr * a,u_int masklen)360f374ba41SEd Maste addr_host_to_all1s(struct xaddr *a, u_int masklen)
361f374ba41SEd Maste {
362f374ba41SEd Maste struct xaddr tmp_mask;
363f374ba41SEd Maste
364f374ba41SEd Maste if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
365f374ba41SEd Maste return (-1);
366f374ba41SEd Maste if (addr_or(a, a, &tmp_mask) == -1)
367f374ba41SEd Maste return (-1);
368f374ba41SEd Maste return (0);
369f374ba41SEd Maste }
370f374ba41SEd Maste
37119261079SEd Maste /*
3721323ec57SEd Maste * Parse string address 'p' into 'n'.
37319261079SEd Maste * Returns 0 on success, -1 on failure.
37419261079SEd Maste */
37519261079SEd Maste int
addr_pton(const char * p,struct xaddr * n)37619261079SEd Maste addr_pton(const char *p, struct xaddr *n)
37719261079SEd Maste {
37819261079SEd Maste struct addrinfo hints, *ai;
37919261079SEd Maste
38019261079SEd Maste memset(&hints, '\0', sizeof(hints));
38119261079SEd Maste hints.ai_flags = AI_NUMERICHOST;
38219261079SEd Maste
38319261079SEd Maste if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
38419261079SEd Maste return -1;
38519261079SEd Maste
3861323ec57SEd Maste if (ai == NULL)
38719261079SEd Maste return -1;
38819261079SEd Maste
3891323ec57SEd Maste if (ai->ai_addr == NULL) {
3901323ec57SEd Maste freeaddrinfo(ai);
3911323ec57SEd Maste return -1;
3921323ec57SEd Maste }
3931323ec57SEd Maste
39419261079SEd Maste if (n != NULL && addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen,
39519261079SEd Maste n) == -1) {
39619261079SEd Maste freeaddrinfo(ai);
39719261079SEd Maste return -1;
39819261079SEd Maste }
39919261079SEd Maste
40019261079SEd Maste freeaddrinfo(ai);
40119261079SEd Maste return 0;
40219261079SEd Maste }
40319261079SEd Maste
40419261079SEd Maste int
addr_sa_pton(const char * h,const char * s,struct sockaddr * sa,socklen_t slen)40519261079SEd Maste addr_sa_pton(const char *h, const char *s, struct sockaddr *sa, socklen_t slen)
40619261079SEd Maste {
40719261079SEd Maste struct addrinfo hints, *ai;
40819261079SEd Maste
40919261079SEd Maste memset(&hints, '\0', sizeof(hints));
41019261079SEd Maste hints.ai_flags = AI_NUMERICHOST;
41119261079SEd Maste
41219261079SEd Maste if (h == NULL || getaddrinfo(h, s, &hints, &ai) != 0)
41319261079SEd Maste return -1;
41419261079SEd Maste
4151323ec57SEd Maste if (ai == NULL)
41619261079SEd Maste return -1;
41719261079SEd Maste
4181323ec57SEd Maste if (ai->ai_addr == NULL) {
4191323ec57SEd Maste freeaddrinfo(ai);
42019261079SEd Maste return -1;
4211323ec57SEd Maste }
4221323ec57SEd Maste
4231323ec57SEd Maste if (sa != NULL) {
4241323ec57SEd Maste if (slen < ai->ai_addrlen) {
4251323ec57SEd Maste freeaddrinfo(ai);
4261323ec57SEd Maste return -1;
4271323ec57SEd Maste }
42819261079SEd Maste memcpy(sa, &ai->ai_addr, ai->ai_addrlen);
42919261079SEd Maste }
43019261079SEd Maste
43119261079SEd Maste freeaddrinfo(ai);
43219261079SEd Maste return 0;
43319261079SEd Maste }
43419261079SEd Maste
43519261079SEd Maste int
addr_ntop(const struct xaddr * n,char * p,size_t len)43619261079SEd Maste addr_ntop(const struct xaddr *n, char *p, size_t len)
43719261079SEd Maste {
43819261079SEd Maste struct sockaddr_storage ss;
43919261079SEd Maste socklen_t slen = sizeof(ss);
44019261079SEd Maste
44119261079SEd Maste if (addr_xaddr_to_sa(n, _SA(&ss), &slen, 0) == -1)
44219261079SEd Maste return -1;
4431323ec57SEd Maste if (p == NULL || len == 0)
44419261079SEd Maste return -1;
44519261079SEd Maste if (getnameinfo(_SA(&ss), slen, p, len, NULL, 0,
446*535af610SEd Maste NI_NUMERICHOST) != 0)
44719261079SEd Maste return -1;
44819261079SEd Maste
44919261079SEd Maste return 0;
45019261079SEd Maste }
45119261079SEd Maste
45219261079SEd Maste /*
45319261079SEd Maste * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
45419261079SEd Maste * Return -1 on parse error, -2 on inconsistency or 0 on success.
45519261079SEd Maste */
45619261079SEd Maste int
addr_pton_cidr(const char * p,struct xaddr * n,u_int * l)45719261079SEd Maste addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
45819261079SEd Maste {
45919261079SEd Maste struct xaddr tmp;
46019261079SEd Maste long unsigned int masklen = 999;
46119261079SEd Maste char addrbuf[64], *mp, *cp;
46219261079SEd Maste
46319261079SEd Maste /* Don't modify argument */
46419261079SEd Maste if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) >= sizeof(addrbuf))
46519261079SEd Maste return -1;
46619261079SEd Maste
46719261079SEd Maste if ((mp = strchr(addrbuf, '/')) != NULL) {
46819261079SEd Maste *mp = '\0';
46919261079SEd Maste mp++;
47019261079SEd Maste masklen = strtoul(mp, &cp, 10);
47138a52bd3SEd Maste if (*mp < '0' || *mp > '9' || *cp != '\0' || masklen > 128)
47219261079SEd Maste return -1;
47319261079SEd Maste }
47419261079SEd Maste
47519261079SEd Maste if (addr_pton(addrbuf, &tmp) == -1)
47619261079SEd Maste return -1;
47719261079SEd Maste
47819261079SEd Maste if (mp == NULL)
47919261079SEd Maste masklen = addr_unicast_masklen(tmp.af);
48019261079SEd Maste if (masklen_valid(tmp.af, masklen) == -1)
48119261079SEd Maste return -2;
48219261079SEd Maste if (addr_host_is_all0s(&tmp, masklen) != 0)
48319261079SEd Maste return -2;
48419261079SEd Maste
48519261079SEd Maste if (n != NULL)
48619261079SEd Maste memcpy(n, &tmp, sizeof(*n));
48719261079SEd Maste if (l != NULL)
48819261079SEd Maste *l = masklen;
48919261079SEd Maste
49019261079SEd Maste return 0;
49119261079SEd Maste }
49219261079SEd Maste
49319261079SEd Maste int
addr_netmatch(const struct xaddr * host,const struct xaddr * net,u_int masklen)49419261079SEd Maste addr_netmatch(const struct xaddr *host, const struct xaddr *net, u_int masklen)
49519261079SEd Maste {
49619261079SEd Maste struct xaddr tmp_mask, tmp_result;
49719261079SEd Maste
49819261079SEd Maste if (host->af != net->af)
49919261079SEd Maste return -1;
50019261079SEd Maste
50119261079SEd Maste if (addr_netmask(host->af, masklen, &tmp_mask) == -1)
50219261079SEd Maste return -1;
50319261079SEd Maste if (addr_and(&tmp_result, host, &tmp_mask) == -1)
50419261079SEd Maste return -1;
50519261079SEd Maste return addr_cmp(&tmp_result, net);
50619261079SEd Maste }
507