summaryrefslogtreecommitdiff
path: root/usr.bin/dig/lib/isc/sockaddr.c
blob: 2b491bbedc0f887e2a8b015d15ddf2416fefea0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/* $Id: sockaddr.c,v 1.17 2024/07/08 13:46:33 florian Exp $ */

/*! \file */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>

#include <isc/buffer.h>

#include <isc/region.h>
#include <isc/sockaddr.h>
#include <string.h>
#include <isc/util.h>

int
isc_sockaddr_equal(const struct sockaddr_storage *a, const struct sockaddr_storage *b) {
	return (isc_sockaddr_compare(a, b, ISC_SOCKADDR_CMPADDR|
					   ISC_SOCKADDR_CMPPORT|
					   ISC_SOCKADDR_CMPSCOPE));
}

int
isc_sockaddr_eqaddr(const struct sockaddr_storage *a, const struct sockaddr_storage *b) {
	return (isc_sockaddr_compare(a, b, ISC_SOCKADDR_CMPADDR|
					   ISC_SOCKADDR_CMPSCOPE));
}

int
isc_sockaddr_compare(const struct sockaddr_storage *a, const struct sockaddr_storage *b,
		     unsigned int flags)
{
	struct sockaddr_in	*sin_a, *sin_b;
	struct sockaddr_in6	*sin6_a, *sin6_b;

	REQUIRE(a != NULL && b != NULL);

	if (a->ss_len != b->ss_len)
		return (0);

	/*
	 * We don't just memcmp because the sin_zero field isn't always
	 * zero.
	 */

	if (a->ss_family != b->ss_family)
		return (0);
	switch (a->ss_family) {
	case AF_INET:
		sin_a = (struct sockaddr_in *) a;
		sin_b = (struct sockaddr_in *) b;
		if ((flags & ISC_SOCKADDR_CMPADDR) != 0 &&
		    memcmp(&sin_a->sin_addr, &sin_b->sin_addr,
			   sizeof(sin_a->sin_addr)) != 0)
			return (0);
		if ((flags & ISC_SOCKADDR_CMPPORT) != 0 &&
		    sin_a->sin_port != sin_b->sin_port)
			return (0);
		break;
	case AF_INET6:
		sin6_a = (struct sockaddr_in6 *) a;
		sin6_b = (struct sockaddr_in6 *) b;

		if ((flags & ISC_SOCKADDR_CMPADDR) != 0 &&
		    memcmp(&sin6_a->sin6_addr, &sin6_b->sin6_addr,
			   sizeof(sin6_a->sin6_addr)) != 0)
			return (0);
		/*
		 * If ISC_SOCKADDR_CMPSCOPEZERO is set then don't return
		 * 0 if one of the scopes in zero.
		 */
		if ((flags & ISC_SOCKADDR_CMPSCOPE) != 0 &&
		    sin6_a->sin6_scope_id != sin6_b->sin6_scope_id &&
		    ((flags & ISC_SOCKADDR_CMPSCOPEZERO) == 0 ||
		      (sin6_a->sin6_scope_id != 0 &&
		       sin6_b->sin6_scope_id != 0)))
			return (0);
		if ((flags & ISC_SOCKADDR_CMPPORT) != 0 &&
		    sin6_a->sin6_port != sin6_b->sin6_port)
			return (0);
		break;
	default:
		if (memcmp(a, b, a->ss_len) != 0)
			return (0);
	}
	return (1);
}

isc_result_t
isc_sockaddr_totext(const struct sockaddr_storage *sockaddr, isc_buffer_t *target) {
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;
	char pbuf[sizeof("65000")];
	unsigned int plen;
	isc_region_t avail;
	char tmp[NI_MAXHOST];

	REQUIRE(sockaddr != NULL);

	/*
	 * Do the port first, giving us the opportunity to check for
	 * unsupported address families.
	 */
	switch (sockaddr->ss_family) {
	case AF_INET:
		sin = (struct sockaddr_in *)sockaddr;
		snprintf(pbuf, sizeof(pbuf), "%u", ntohs(sin->sin_port));
		break;
	case AF_INET6:
		sin6 = (struct sockaddr_in6 *)sockaddr;
		snprintf(pbuf, sizeof(pbuf), "%u", ntohs(sin6->sin6_port));
		break;
	default:
		return (ISC_R_FAILURE);
	}

	plen = strlen(pbuf);
	INSIST(plen < sizeof(pbuf));

	if (getnameinfo((struct sockaddr *)sockaddr, sockaddr->ss_len,
	    tmp, sizeof(tmp), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV) != 0)
		return (ISC_R_FAILURE);
	if (strlen(tmp) > isc_buffer_availablelength(target))
		return (ISC_R_NOSPACE);
	isc_buffer_putmem(target, tmp, strlen(tmp));

	if (1 + plen + 1 > isc_buffer_availablelength(target))
		return (ISC_R_NOSPACE);

	isc_buffer_putmem(target, (const unsigned char *)"#", 1);
	isc_buffer_putmem(target, (const unsigned char *)pbuf, plen);

	/*
	 * Null terminate after used region.
	 */
	isc_buffer_availableregion(target, &avail);
	INSIST(avail.length >= 1);
	avail.base[0] = '\0';

	return (ISC_R_SUCCESS);
}

void
isc_sockaddr_format(const struct sockaddr_storage *sa, char *array, unsigned int size) {
	isc_result_t result;
	isc_buffer_t buf;

	if (size == 0U)
		return;

	isc_buffer_init(&buf, array, size);
	result = isc_sockaddr_totext(sa, &buf);
	if (result != ISC_R_SUCCESS) {
		snprintf(array, size, "<unknown address, family %u>",
			 sa->ss_family);
		array[size - 1] = '\0';
	}
}

void
isc_sockaddr_any(struct sockaddr_storage *sockaddr)
{
	struct sockaddr_in *sin = (struct sockaddr_in *) sockaddr;
	memset(sockaddr, 0, sizeof(*sockaddr));
	sin->sin_family = AF_INET;
	sin->sin_len = sizeof(*sin);
	sin->sin_addr.s_addr = INADDR_ANY;
	sin->sin_port = 0;
}

void
isc_sockaddr_any6(struct sockaddr_storage *sockaddr)
{
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sockaddr;
	memset(sockaddr, 0, sizeof(*sockaddr));
	sin6->sin6_family = AF_INET6;
	sin6->sin6_len = sizeof(*sin6);
	sin6->sin6_addr = in6addr_any;
	sin6->sin6_port = 0;
}

void
isc_sockaddr_anyofpf(struct sockaddr_storage *sockaddr, int pf) {
     switch (pf) {
     case AF_INET:
	     isc_sockaddr_any(sockaddr);
	     break;
     case AF_INET6:
	     isc_sockaddr_any6(sockaddr);
	     break;
     default:
	     INSIST(0);
     }
}

int
isc_sockaddr_pf(const struct sockaddr_storage *sockaddr) {

	/*
	 * Get the protocol family of 'sockaddr'.
	 */

	return (sockaddr->ss_family);
}

in_port_t
isc_sockaddr_getport(const struct sockaddr_storage *sockaddr) {
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;

	switch (sockaddr->ss_family) {
	case AF_INET:
		sin = (struct sockaddr_in *)sockaddr;
		return (ntohs(sin->sin_port));
		break;
	case AF_INET6:
		sin6 = (struct sockaddr_in6 *)sockaddr;
		return (ntohs(sin6->sin6_port));
		break;
	default:
		FATAL_ERROR(__FILE__, __LINE__,
			    "unknown address family: %d",
			    (int)sockaddr->ss_family);
	}
}

int
isc_sockaddr_ismulticast(const struct sockaddr_storage *sockaddr) {
	struct sockaddr_in *sin;
	struct sockaddr_in6 *sin6;

	switch (sockaddr->ss_family) {
	case AF_INET:
		sin = (struct sockaddr_in *)sockaddr;
		return (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)));
	case AF_INET6:
		sin6 = (struct sockaddr_in6 *)sockaddr;
		return (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr));
	default:
		return (0);
	}
}

int
isc_sockaddr_issitelocal(const struct sockaddr_storage *sockaddr) {
	struct sockaddr_in6 *sin6;
	if (sockaddr->ss_family == AF_INET6) {
		sin6 = (struct sockaddr_in6 *)sockaddr;
		return (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr));
	}
	return (0);
}

int
isc_sockaddr_islinklocal(const struct sockaddr_storage *sockaddr) {
	struct sockaddr_in6 *sin6;
	if (sockaddr->ss_family == AF_INET6) {
		sin6 = (struct sockaddr_in6 *)sockaddr;
		return (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr));
	}
	return (0);
}