xref: /netbsd-src/crypto/external/bsd/openssh/dist/addr.c (revision 1c7715dda22cf2bd169e2f84953c050393e8fe9c)
1*1c7715ddSchristos /*	$NetBSD: addr.c,v 1.7 2024/07/08 22:33:43 christos Exp $	*/
2*1c7715ddSchristos /* $OpenBSD: addr.c,v 1.8 2024/04/02 09:29:31 deraadt Exp $ */
3cffc2a7aSchristos 
4cffc2a7aSchristos /*
5cffc2a7aSchristos  * Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
6cffc2a7aSchristos  *
7cffc2a7aSchristos  * Permission to use, copy, modify, and distribute this software for any
8cffc2a7aSchristos  * purpose with or without fee is hereby granted, provided that the above
9cffc2a7aSchristos  * copyright notice and this permission notice appear in all copies.
10cffc2a7aSchristos  *
11cffc2a7aSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12cffc2a7aSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13cffc2a7aSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14cffc2a7aSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15cffc2a7aSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16cffc2a7aSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17cffc2a7aSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18cffc2a7aSchristos  */
19cffc2a7aSchristos 
2017418e98Schristos #include "includes.h"
21*1c7715ddSchristos __RCSID("$NetBSD: addr.c,v 1.7 2024/07/08 22:33:43 christos Exp $");
2217418e98Schristos 
23cffc2a7aSchristos #include <sys/types.h>
24cffc2a7aSchristos #include <sys/socket.h>
25cffc2a7aSchristos #include <netinet/in.h>
26cffc2a7aSchristos #include <arpa/inet.h>
27cffc2a7aSchristos 
28cffc2a7aSchristos #include <netdb.h>
29cffc2a7aSchristos #include <string.h>
30cffc2a7aSchristos #include <stdlib.h>
31cffc2a7aSchristos #include <stdio.h>
32*1c7715ddSchristos #include <limits.h>
33cffc2a7aSchristos 
34cffc2a7aSchristos #include "addr.h"
35cffc2a7aSchristos 
36cffc2a7aSchristos #define _SA(x)	((struct sockaddr *)(x))
37cffc2a7aSchristos 
38cffc2a7aSchristos int
addr_unicast_masklen(int af)39cffc2a7aSchristos addr_unicast_masklen(int af)
40cffc2a7aSchristos {
41cffc2a7aSchristos 	switch (af) {
42cffc2a7aSchristos 	case AF_INET:
43cffc2a7aSchristos 		return 32;
44cffc2a7aSchristos 	case AF_INET6:
45cffc2a7aSchristos 		return 128;
46cffc2a7aSchristos 	default:
47cffc2a7aSchristos 		return -1;
48cffc2a7aSchristos 	}
49cffc2a7aSchristos }
50cffc2a7aSchristos 
51cffc2a7aSchristos static inline int
masklen_valid(int af,u_int masklen)52cffc2a7aSchristos masklen_valid(int af, u_int masklen)
53cffc2a7aSchristos {
54cffc2a7aSchristos 	switch (af) {
55cffc2a7aSchristos 	case AF_INET:
56cffc2a7aSchristos 		return masklen <= 32 ? 0 : -1;
57cffc2a7aSchristos 	case AF_INET6:
58cffc2a7aSchristos 		return masklen <= 128 ? 0 : -1;
59cffc2a7aSchristos 	default:
60cffc2a7aSchristos 		return -1;
61cffc2a7aSchristos 	}
62cffc2a7aSchristos }
63cffc2a7aSchristos 
64cffc2a7aSchristos int
addr_xaddr_to_sa(const struct xaddr * xa,struct sockaddr * sa,socklen_t * len,u_int16_t port)65cffc2a7aSchristos addr_xaddr_to_sa(const struct xaddr *xa, struct sockaddr *sa, socklen_t *len,
66cffc2a7aSchristos     u_int16_t port)
67cffc2a7aSchristos {
68cffc2a7aSchristos 	struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
69cffc2a7aSchristos 	struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
70cffc2a7aSchristos 
71cffc2a7aSchristos 	if (xa == NULL || sa == NULL || len == NULL)
72cffc2a7aSchristos 		return -1;
73cffc2a7aSchristos 
74cffc2a7aSchristos 	switch (xa->af) {
75cffc2a7aSchristos 	case AF_INET:
76cffc2a7aSchristos 		if (*len < sizeof(*in4))
77cffc2a7aSchristos 			return -1;
78cffc2a7aSchristos 		memset(sa, '\0', sizeof(*in4));
79cffc2a7aSchristos 		*len = sizeof(*in4);
80cffc2a7aSchristos #ifdef SOCK_HAS_LEN
81cffc2a7aSchristos 		in4->sin_len = sizeof(*in4);
82cffc2a7aSchristos #endif
83cffc2a7aSchristos 		in4->sin_family = AF_INET;
84cffc2a7aSchristos 		in4->sin_port = htons(port);
85cffc2a7aSchristos 		memcpy(&in4->sin_addr, &xa->v4, sizeof(in4->sin_addr));
86cffc2a7aSchristos 		break;
87cffc2a7aSchristos 	case AF_INET6:
88cffc2a7aSchristos 		if (*len < sizeof(*in6))
89cffc2a7aSchristos 			return -1;
90cffc2a7aSchristos 		memset(sa, '\0', sizeof(*in6));
91cffc2a7aSchristos 		*len = sizeof(*in6);
92cffc2a7aSchristos #ifdef SOCK_HAS_LEN
93cffc2a7aSchristos 		in6->sin6_len = sizeof(*in6);
94cffc2a7aSchristos #endif
95cffc2a7aSchristos 		in6->sin6_family = AF_INET6;
96cffc2a7aSchristos 		in6->sin6_port = htons(port);
97cffc2a7aSchristos 		memcpy(&in6->sin6_addr, &xa->v6, sizeof(in6->sin6_addr));
98cffc2a7aSchristos 		in6->sin6_scope_id = xa->scope_id;
99cffc2a7aSchristos 		break;
100cffc2a7aSchristos 	default:
101cffc2a7aSchristos 		return -1;
102cffc2a7aSchristos 	}
103cffc2a7aSchristos 	return 0;
104cffc2a7aSchristos }
105cffc2a7aSchristos 
106cffc2a7aSchristos /*
107cffc2a7aSchristos  * Convert struct sockaddr to struct xaddr
108cffc2a7aSchristos  * Returns 0 on success, -1 on failure.
109cffc2a7aSchristos  */
110cffc2a7aSchristos int
addr_sa_to_xaddr(struct sockaddr * sa,socklen_t slen,struct xaddr * xa)111cffc2a7aSchristos addr_sa_to_xaddr(struct sockaddr *sa, socklen_t slen, struct xaddr *xa)
112cffc2a7aSchristos {
113cffc2a7aSchristos 	struct sockaddr_in *in4 = (struct sockaddr_in *)sa;
114cffc2a7aSchristos 	struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)sa;
115cffc2a7aSchristos 
116cffc2a7aSchristos 	memset(xa, '\0', sizeof(*xa));
117cffc2a7aSchristos 
118cffc2a7aSchristos 	switch (sa->sa_family) {
119cffc2a7aSchristos 	case AF_INET:
120cffc2a7aSchristos 		if (slen < (socklen_t)sizeof(*in4))
121cffc2a7aSchristos 			return -1;
122cffc2a7aSchristos 		xa->af = AF_INET;
123cffc2a7aSchristos 		memcpy(&xa->v4, &in4->sin_addr, sizeof(xa->v4));
124cffc2a7aSchristos 		break;
125cffc2a7aSchristos 	case AF_INET6:
126cffc2a7aSchristos 		if (slen < (socklen_t)sizeof(*in6))
127cffc2a7aSchristos 			return -1;
128cffc2a7aSchristos 		xa->af = AF_INET6;
129cffc2a7aSchristos 		memcpy(&xa->v6, &in6->sin6_addr, sizeof(xa->v6));
130cffc2a7aSchristos #ifdef HAVE_STRUCT_SOCKADDR_IN6_SIN6_SCOPE_ID
131cffc2a7aSchristos 		xa->scope_id = in6->sin6_scope_id;
132cffc2a7aSchristos #endif
133cffc2a7aSchristos 		break;
134cffc2a7aSchristos 	default:
135cffc2a7aSchristos 		return -1;
136cffc2a7aSchristos 	}
137cffc2a7aSchristos 
138cffc2a7aSchristos 	return 0;
139cffc2a7aSchristos }
140cffc2a7aSchristos 
141cffc2a7aSchristos int
addr_invert(struct xaddr * n)142cffc2a7aSchristos addr_invert(struct xaddr *n)
143cffc2a7aSchristos {
144cffc2a7aSchristos 	int i;
145cffc2a7aSchristos 
146cffc2a7aSchristos 	if (n == NULL)
147cffc2a7aSchristos 		return -1;
148cffc2a7aSchristos 
149cffc2a7aSchristos 	switch (n->af) {
150cffc2a7aSchristos 	case AF_INET:
151cffc2a7aSchristos 		n->v4.s_addr = ~n->v4.s_addr;
152cffc2a7aSchristos 		return 0;
153cffc2a7aSchristos 	case AF_INET6:
154cffc2a7aSchristos 		for (i = 0; i < 4; i++)
155cffc2a7aSchristos 			n->addr32[i] = ~n->addr32[i];
156cffc2a7aSchristos 		return 0;
157cffc2a7aSchristos 	default:
158cffc2a7aSchristos 		return -1;
159cffc2a7aSchristos 	}
160cffc2a7aSchristos }
161cffc2a7aSchristos 
162cffc2a7aSchristos /*
163cffc2a7aSchristos  * Calculate a netmask of length 'l' for address family 'af' and
164cffc2a7aSchristos  * store it in 'n'.
165cffc2a7aSchristos  * Returns 0 on success, -1 on failure.
166cffc2a7aSchristos  */
167cffc2a7aSchristos int
addr_netmask(int af,u_int l,struct xaddr * n)168cffc2a7aSchristos addr_netmask(int af, u_int l, struct xaddr *n)
169cffc2a7aSchristos {
170cffc2a7aSchristos 	int i;
171cffc2a7aSchristos 
172cffc2a7aSchristos 	if (masklen_valid(af, l) != 0 || n == NULL)
173cffc2a7aSchristos 		return -1;
174cffc2a7aSchristos 
175cffc2a7aSchristos 	memset(n, '\0', sizeof(*n));
176cffc2a7aSchristos 	switch (af) {
177cffc2a7aSchristos 	case AF_INET:
178cffc2a7aSchristos 		n->af = AF_INET;
179cffc2a7aSchristos 		if (l == 0)
180cffc2a7aSchristos 			return 0;
181cffc2a7aSchristos 		n->v4.s_addr = htonl((0xffffffff << (32 - l)) & 0xffffffff);
182cffc2a7aSchristos 		return 0;
183cffc2a7aSchristos 	case AF_INET6:
184cffc2a7aSchristos 		n->af = AF_INET6;
185cffc2a7aSchristos 		for (i = 0; i < 4 && l >= 32; i++, l -= 32)
186cffc2a7aSchristos 			n->addr32[i] = 0xffffffffU;
187cffc2a7aSchristos 		if (i < 4 && l != 0)
188cffc2a7aSchristos 			n->addr32[i] = htonl((0xffffffff << (32 - l)) &
189cffc2a7aSchristos 			    0xffffffff);
190cffc2a7aSchristos 		return 0;
191cffc2a7aSchristos 	default:
192cffc2a7aSchristos 		return -1;
193cffc2a7aSchristos 	}
194cffc2a7aSchristos }
195cffc2a7aSchristos 
196cffc2a7aSchristos int
addr_hostmask(int af,u_int l,struct xaddr * n)197cffc2a7aSchristos addr_hostmask(int af, u_int l, struct xaddr *n)
198cffc2a7aSchristos {
199cffc2a7aSchristos 	if (addr_netmask(af, l, n) == -1 || addr_invert(n) == -1)
200cffc2a7aSchristos 		return -1;
201cffc2a7aSchristos 	return 0;
202cffc2a7aSchristos }
203cffc2a7aSchristos 
204cffc2a7aSchristos /*
205cffc2a7aSchristos  * Perform logical AND of addresses 'a' and 'b', storing result in 'dst'.
206cffc2a7aSchristos  * Returns 0 on success, -1 on failure.
207cffc2a7aSchristos  */
208cffc2a7aSchristos int
addr_and(struct xaddr * dst,const struct xaddr * a,const struct xaddr * b)209cffc2a7aSchristos addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
210cffc2a7aSchristos {
211cffc2a7aSchristos 	int i;
212cffc2a7aSchristos 
213cffc2a7aSchristos 	if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
214cffc2a7aSchristos 		return -1;
215cffc2a7aSchristos 
216cffc2a7aSchristos 	memcpy(dst, a, sizeof(*dst));
217cffc2a7aSchristos 	switch (a->af) {
218cffc2a7aSchristos 	case AF_INET:
219cffc2a7aSchristos 		dst->v4.s_addr &= b->v4.s_addr;
220cffc2a7aSchristos 		return 0;
221cffc2a7aSchristos 	case AF_INET6:
222cffc2a7aSchristos 		dst->scope_id = a->scope_id;
223cffc2a7aSchristos 		for (i = 0; i < 4; i++)
224cffc2a7aSchristos 			dst->addr32[i] &= b->addr32[i];
225cffc2a7aSchristos 		return 0;
226cffc2a7aSchristos 	default:
227cffc2a7aSchristos 		return -1;
228cffc2a7aSchristos 	}
229cffc2a7aSchristos }
230cffc2a7aSchristos 
231cffc2a7aSchristos int
addr_or(struct xaddr * dst,const struct xaddr * a,const struct xaddr * b)232b1066cf3Schristos addr_or(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
233b1066cf3Schristos {
234b1066cf3Schristos 	int i;
235b1066cf3Schristos 
236b1066cf3Schristos 	if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
237b1066cf3Schristos 		return (-1);
238b1066cf3Schristos 
239b1066cf3Schristos 	memcpy(dst, a, sizeof(*dst));
240b1066cf3Schristos 	switch (a->af) {
241b1066cf3Schristos 	case AF_INET:
242b1066cf3Schristos 		dst->v4.s_addr |= b->v4.s_addr;
243b1066cf3Schristos 		return (0);
244b1066cf3Schristos 	case AF_INET6:
245b1066cf3Schristos 		for (i = 0; i < 4; i++)
246b1066cf3Schristos 			dst->addr32[i] |= b->addr32[i];
247b1066cf3Schristos 		return (0);
248b1066cf3Schristos 	default:
249b1066cf3Schristos 		return (-1);
250b1066cf3Schristos 	}
251b1066cf3Schristos }
252b1066cf3Schristos 
253b1066cf3Schristos int
addr_cmp(const struct xaddr * a,const struct xaddr * b)254cffc2a7aSchristos addr_cmp(const struct xaddr *a, const struct xaddr *b)
255cffc2a7aSchristos {
256cffc2a7aSchristos 	int i;
257cffc2a7aSchristos 
258cffc2a7aSchristos 	if (a->af != b->af)
259cffc2a7aSchristos 		return (a->af == AF_INET6 ? 1 : -1);
260cffc2a7aSchristos 
261cffc2a7aSchristos 	switch (a->af) {
262cffc2a7aSchristos 	case AF_INET:
263cffc2a7aSchristos 		/*
264cffc2a7aSchristos 		 * Can't just subtract here as 255.255.255.255 - 0.0.0.0 is
265cffc2a7aSchristos 		 * too big to fit into a signed int
266cffc2a7aSchristos 		 */
267cffc2a7aSchristos 		if (a->v4.s_addr == b->v4.s_addr)
268cffc2a7aSchristos 			return 0;
269cffc2a7aSchristos 		return (ntohl(a->v4.s_addr) > ntohl(b->v4.s_addr) ? 1 : -1);
270a03ec00cSchristos 	case AF_INET6:
271cffc2a7aSchristos 		/*
272cffc2a7aSchristos 		 * Do this a byte at a time to avoid the above issue and
273cffc2a7aSchristos 		 * any endian problems
274cffc2a7aSchristos 		 */
275cffc2a7aSchristos 		for (i = 0; i < 16; i++)
276cffc2a7aSchristos 			if (a->addr8[i] - b->addr8[i] != 0)
277cffc2a7aSchristos 				return (a->addr8[i] - b->addr8[i]);
278cffc2a7aSchristos 		if (a->scope_id == b->scope_id)
279cffc2a7aSchristos 			return (0);
280cffc2a7aSchristos 		return (a->scope_id > b->scope_id ? 1 : -1);
281cffc2a7aSchristos 	default:
282cffc2a7aSchristos 		return (-1);
283cffc2a7aSchristos 	}
284cffc2a7aSchristos }
285cffc2a7aSchristos 
286cffc2a7aSchristos int
addr_is_all0s(const struct xaddr * a)287cffc2a7aSchristos addr_is_all0s(const struct xaddr *a)
288cffc2a7aSchristos {
289cffc2a7aSchristos 	int i;
290cffc2a7aSchristos 
291cffc2a7aSchristos 	switch (a->af) {
292cffc2a7aSchristos 	case AF_INET:
293cffc2a7aSchristos 		return (a->v4.s_addr == 0 ? 0 : -1);
294a03ec00cSchristos 	case AF_INET6:
295cffc2a7aSchristos 		for (i = 0; i < 4; i++)
296cffc2a7aSchristos 			if (a->addr32[i] != 0)
297cffc2a7aSchristos 				return -1;
298cffc2a7aSchristos 		return 0;
299cffc2a7aSchristos 	default:
300cffc2a7aSchristos 		return -1;
301cffc2a7aSchristos 	}
302cffc2a7aSchristos }
303cffc2a7aSchristos 
304b1066cf3Schristos /* Increment the specified address. Note, does not do overflow checking */
305b1066cf3Schristos void
addr_increment(struct xaddr * a)306b1066cf3Schristos addr_increment(struct xaddr *a)
307b1066cf3Schristos {
308b1066cf3Schristos 	int i;
309b1066cf3Schristos 	uint32_t n;
310b1066cf3Schristos 
311b1066cf3Schristos 	switch (a->af) {
312b1066cf3Schristos 	case AF_INET:
313b1066cf3Schristos 		a->v4.s_addr = htonl(ntohl(a->v4.s_addr) + 1);
314b1066cf3Schristos 		break;
315b1066cf3Schristos 	case AF_INET6:
316b1066cf3Schristos 		for (i = 0; i < 4; i++) {
317b1066cf3Schristos 			/* Increment with carry */
318b1066cf3Schristos 			n = ntohl(a->addr32[3 - i]) + 1;
319b1066cf3Schristos 			a->addr32[3 - i] = htonl(n);
320b1066cf3Schristos 			if (n != 0)
321b1066cf3Schristos 				break;
322b1066cf3Schristos 		}
323b1066cf3Schristos 		break;
324b1066cf3Schristos 	}
325b1066cf3Schristos }
326b1066cf3Schristos 
327cffc2a7aSchristos /*
328cffc2a7aSchristos  * Test whether host portion of address 'a', as determined by 'masklen'
329cffc2a7aSchristos  * is all zeros.
330a03ec00cSchristos  * Returns 0 if host portion of address is all-zeros,
331cffc2a7aSchristos  * -1 if not all zeros or on failure.
332cffc2a7aSchristos  */
333cffc2a7aSchristos int
addr_host_is_all0s(const struct xaddr * a,u_int masklen)334cffc2a7aSchristos addr_host_is_all0s(const struct xaddr *a, u_int masklen)
335cffc2a7aSchristos {
336cffc2a7aSchristos 	struct xaddr tmp_addr, tmp_mask, tmp_result;
337cffc2a7aSchristos 
338cffc2a7aSchristos 	memcpy(&tmp_addr, a, sizeof(tmp_addr));
339cffc2a7aSchristos 	if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
340cffc2a7aSchristos 		return -1;
341cffc2a7aSchristos 	if (addr_and(&tmp_result, &tmp_addr, &tmp_mask) == -1)
342cffc2a7aSchristos 		return -1;
343cffc2a7aSchristos 	return addr_is_all0s(&tmp_result);
344cffc2a7aSchristos }
345cffc2a7aSchristos 
346b1066cf3Schristos #if 0
347b1066cf3Schristos int
348b1066cf3Schristos addr_host_to_all0s(struct xaddr *a, u_int masklen)
349b1066cf3Schristos {
350b1066cf3Schristos 	struct xaddr tmp_mask;
351b1066cf3Schristos 
352b1066cf3Schristos 	if (addr_netmask(a->af, masklen, &tmp_mask) == -1)
353b1066cf3Schristos 		return (-1);
354b1066cf3Schristos 	if (addr_and(a, a, &tmp_mask) == -1)
355b1066cf3Schristos 		return (-1);
356b1066cf3Schristos 	return (0);
357b1066cf3Schristos }
358b1066cf3Schristos #endif
359b1066cf3Schristos 
360b1066cf3Schristos int
addr_host_to_all1s(struct xaddr * a,u_int masklen)361b1066cf3Schristos addr_host_to_all1s(struct xaddr *a, u_int masklen)
362b1066cf3Schristos {
363b1066cf3Schristos 	struct xaddr tmp_mask;
364b1066cf3Schristos 
365b1066cf3Schristos 	if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
366b1066cf3Schristos 		return (-1);
367b1066cf3Schristos 	if (addr_or(a, a, &tmp_mask) == -1)
368b1066cf3Schristos 		return (-1);
369b1066cf3Schristos 	return (0);
370b1066cf3Schristos }
371b1066cf3Schristos 
372cffc2a7aSchristos /*
373a03ec00cSchristos  * Parse string address 'p' into 'n'.
374cffc2a7aSchristos  * Returns 0 on success, -1 on failure.
375cffc2a7aSchristos  */
376cffc2a7aSchristos int
addr_pton(const char * p,struct xaddr * n)377cffc2a7aSchristos addr_pton(const char *p, struct xaddr *n)
378cffc2a7aSchristos {
379cffc2a7aSchristos 	struct addrinfo hints, *ai;
380cffc2a7aSchristos 
381cffc2a7aSchristos 	memset(&hints, '\0', sizeof(hints));
382cffc2a7aSchristos 	hints.ai_flags = AI_NUMERICHOST;
383cffc2a7aSchristos 
384cffc2a7aSchristos 	if (p == NULL || getaddrinfo(p, NULL, &hints, &ai) != 0)
385cffc2a7aSchristos 		return -1;
386cffc2a7aSchristos 
387a03ec00cSchristos 	if (ai == NULL)
388cffc2a7aSchristos 		return -1;
389cffc2a7aSchristos 
390a03ec00cSchristos 	if (ai->ai_addr == NULL) {
391a03ec00cSchristos 		freeaddrinfo(ai);
392a03ec00cSchristos 		return -1;
393a03ec00cSchristos 	}
394a03ec00cSchristos 
395cffc2a7aSchristos 	if (n != NULL && addr_sa_to_xaddr(ai->ai_addr, ai->ai_addrlen,
396cffc2a7aSchristos 	    n) == -1) {
397cffc2a7aSchristos 		freeaddrinfo(ai);
398cffc2a7aSchristos 		return -1;
399cffc2a7aSchristos 	}
400cffc2a7aSchristos 
401cffc2a7aSchristos 	freeaddrinfo(ai);
402cffc2a7aSchristos 	return 0;
403cffc2a7aSchristos }
404cffc2a7aSchristos 
405cffc2a7aSchristos int
addr_sa_pton(const char * h,const char * s,struct sockaddr * sa,socklen_t slen)406cffc2a7aSchristos addr_sa_pton(const char *h, const char *s, struct sockaddr *sa, socklen_t slen)
407cffc2a7aSchristos {
408cffc2a7aSchristos 	struct addrinfo hints, *ai;
409cffc2a7aSchristos 
410cffc2a7aSchristos 	memset(&hints, '\0', sizeof(hints));
411cffc2a7aSchristos 	hints.ai_flags = AI_NUMERICHOST;
412cffc2a7aSchristos 
413cffc2a7aSchristos 	if (h == NULL || getaddrinfo(h, s, &hints, &ai) != 0)
414cffc2a7aSchristos 		return -1;
415cffc2a7aSchristos 
416a03ec00cSchristos 	if (ai == NULL)
417cffc2a7aSchristos 		return -1;
418cffc2a7aSchristos 
419a03ec00cSchristos 	if (ai->ai_addr == NULL) {
420a03ec00cSchristos 		freeaddrinfo(ai);
421cffc2a7aSchristos 		return -1;
422a03ec00cSchristos 	}
423a03ec00cSchristos 
424a03ec00cSchristos 	if (sa != NULL) {
425a03ec00cSchristos 		if (slen < ai->ai_addrlen) {
426a03ec00cSchristos 			freeaddrinfo(ai);
427a03ec00cSchristos 			return -1;
428a03ec00cSchristos 		}
429cffc2a7aSchristos 		memcpy(sa, &ai->ai_addr, ai->ai_addrlen);
430cffc2a7aSchristos 	}
431cffc2a7aSchristos 
432cffc2a7aSchristos 	freeaddrinfo(ai);
433cffc2a7aSchristos 	return 0;
434cffc2a7aSchristos }
435cffc2a7aSchristos 
436cffc2a7aSchristos int
addr_ntop(const struct xaddr * n,char * p,size_t len)437cffc2a7aSchristos addr_ntop(const struct xaddr *n, char *p, size_t len)
438cffc2a7aSchristos {
439cffc2a7aSchristos 	struct sockaddr_storage ss;
440cffc2a7aSchristos 	socklen_t slen = sizeof(ss);
441cffc2a7aSchristos 
442cffc2a7aSchristos 	if (addr_xaddr_to_sa(n, _SA(&ss), &slen, 0) == -1)
443cffc2a7aSchristos 		return -1;
444a03ec00cSchristos 	if (p == NULL || len == 0)
445cffc2a7aSchristos 		return -1;
446cffc2a7aSchristos 	if (getnameinfo(_SA(&ss), slen, p, len, NULL, 0,
447a629fefcSchristos 	    NI_NUMERICHOST) != 0)
448cffc2a7aSchristos 		return -1;
449cffc2a7aSchristos 
450cffc2a7aSchristos 	return 0;
451cffc2a7aSchristos }
452cffc2a7aSchristos 
453cffc2a7aSchristos /*
454cffc2a7aSchristos  * Parse a CIDR address (x.x.x.x/y or xxxx:yyyy::/z).
455cffc2a7aSchristos  * Return -1 on parse error, -2 on inconsistency or 0 on success.
456cffc2a7aSchristos  */
457cffc2a7aSchristos int
addr_pton_cidr(const char * p,struct xaddr * n,u_int * l)458cffc2a7aSchristos addr_pton_cidr(const char *p, struct xaddr *n, u_int *l)
459cffc2a7aSchristos {
460cffc2a7aSchristos 	struct xaddr tmp;
461*1c7715ddSchristos 	u_int masklen = 999;
462*1c7715ddSchristos 	char addrbuf[64], *mp;
463*1c7715ddSchristos 	const char *errstr;
464cffc2a7aSchristos 
465cffc2a7aSchristos 	/* Don't modify argument */
466cffc2a7aSchristos 	if (p == NULL || strlcpy(addrbuf, p, sizeof(addrbuf)) >= sizeof(addrbuf))
467cffc2a7aSchristos 		return -1;
468cffc2a7aSchristos 
469cffc2a7aSchristos 	if ((mp = strchr(addrbuf, '/')) != NULL) {
470cffc2a7aSchristos 		*mp = '\0';
471cffc2a7aSchristos 		mp++;
472*1c7715ddSchristos 		masklen = (u_int)strtonum(mp, 0, INT_MAX, &errstr);
473*1c7715ddSchristos 		if (errstr)
474cffc2a7aSchristos 			return -1;
475cffc2a7aSchristos 	}
476cffc2a7aSchristos 
477cffc2a7aSchristos 	if (addr_pton(addrbuf, &tmp) == -1)
478cffc2a7aSchristos 		return -1;
479cffc2a7aSchristos 
480cffc2a7aSchristos 	if (mp == NULL)
481cffc2a7aSchristos 		masklen = addr_unicast_masklen(tmp.af);
482cffc2a7aSchristos 	if (masklen_valid(tmp.af, masklen) == -1)
483cffc2a7aSchristos 		return -2;
484cffc2a7aSchristos 	if (addr_host_is_all0s(&tmp, masklen) != 0)
485cffc2a7aSchristos 		return -2;
486cffc2a7aSchristos 
487cffc2a7aSchristos 	if (n != NULL)
488cffc2a7aSchristos 		memcpy(n, &tmp, sizeof(*n));
489cffc2a7aSchristos 	if (l != NULL)
490cffc2a7aSchristos 		*l = masklen;
491cffc2a7aSchristos 
492cffc2a7aSchristos 	return 0;
493cffc2a7aSchristos }
494cffc2a7aSchristos 
495cffc2a7aSchristos int
addr_netmatch(const struct xaddr * host,const struct xaddr * net,u_int masklen)496cffc2a7aSchristos addr_netmatch(const struct xaddr *host, const struct xaddr *net, u_int masklen)
497cffc2a7aSchristos {
498cffc2a7aSchristos 	struct xaddr tmp_mask, tmp_result;
499cffc2a7aSchristos 
500cffc2a7aSchristos 	if (host->af != net->af)
501cffc2a7aSchristos 		return -1;
502cffc2a7aSchristos 
503cffc2a7aSchristos 	if (addr_netmask(host->af, masklen, &tmp_mask) == -1)
504cffc2a7aSchristos 		return -1;
505cffc2a7aSchristos 	if (addr_and(&tmp_result, host, &tmp_mask) == -1)
506cffc2a7aSchristos 		return -1;
507cffc2a7aSchristos 	return addr_cmp(&tmp_result, net);
508cffc2a7aSchristos }
509