sockaddr.c 8.97 KB
Newer Older
Michael Graff's avatar
Michael Graff committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM 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.
 */

#include <config.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
Mark Andrews's avatar
Mark Andrews committed
23
#include <stdio.h>
Michael Graff's avatar
Michael Graff committed
24
25
26
27
28

#include <isc/types.h>
#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/sockaddr.h>
Mark Andrews's avatar
Mark Andrews committed
29
#include <isc/mem.h>
Michael Graff's avatar
Michael Graff committed
30
31

isc_boolean_t
32
isc_sockaddr_equal(const isc_sockaddr_t *a, const isc_sockaddr_t *b)
Michael Graff's avatar
Michael Graff committed
33
{
Bob Halley's avatar
Bob Halley committed
34
	REQUIRE(a != NULL && b != NULL);
Michael Graff's avatar
Michael Graff committed
35

Bob Halley's avatar
Bob Halley committed
36
	if (a->length != b->length)
Michael Graff's avatar
Michael Graff committed
37
38
		return (ISC_FALSE);

39
40
41
42
43
44
	/*
	 * We don't just memcmp because the sin_zero field isn't always
	 * zero.
	 */

	if (a->type.sa.sa_family != b->type.sa.sa_family)
Michael Graff's avatar
Michael Graff committed
45
		return (ISC_FALSE);
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
	switch (a->type.sa.sa_family) {
	case AF_INET:
		if (memcmp(&a->type.sin.sin_addr, &b->type.sin.sin_addr,
			   sizeof a->type.sin.sin_addr) != 0)
			return (ISC_FALSE);
		if (a->type.sin.sin_port != b->type.sin.sin_port)
			return (ISC_FALSE);
		break;
	case AF_INET6:
		if (memcmp(&a->type.sin6.sin6_addr, &b->type.sin6.sin6_addr,
			   sizeof a->type.sin6.sin6_addr) != 0)
			return (ISC_FALSE);
		if (a->type.sin6.sin6_port != b->type.sin6.sin6_port)
			return (ISC_FALSE);
		break;
	default:
		if (memcmp(&a->type, &b->type, a->length) != 0)
			return (ISC_FALSE);
	}
Bob Halley's avatar
Bob Halley committed
65
66
	return (ISC_TRUE);
}
Michael Graff's avatar
Michael Graff committed
67

68
69

/*
70
 * Compare just the addresses (ignore ports)
71
72
 */
isc_boolean_t
73
isc_sockaddr_eqaddr(const isc_sockaddr_t *a, const isc_sockaddr_t *b)
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
{
	REQUIRE(a != NULL && b != NULL);

	if (a->length != b->length)
		return (ISC_FALSE);

	if (a->type.sa.sa_family != b->type.sa.sa_family)
		return (ISC_FALSE);
	switch (a->type.sa.sa_family) {
	case AF_INET:
		if (memcmp(&a->type.sin.sin_addr, &b->type.sin.sin_addr,
			   sizeof a->type.sin.sin_addr) != 0)
			return (ISC_FALSE);
		break;
	case AF_INET6:
		if (memcmp(&a->type.sin6.sin6_addr, &b->type.sin6.sin6_addr,
			   sizeof a->type.sin6.sin6_addr) != 0)
			return (ISC_FALSE);
		break;
	default:
		if (memcmp(&a->type, &b->type, a->length) != 0)
			return (ISC_FALSE);
	}
	return (ISC_TRUE);
}

100
101
102
103
104
105
106
107
108
109
/*
 * Compare just a prefix of the addresses (ignore ports and
 * low address bits)
 */

isc_boolean_t
isc_sockaddr_eqaddrprefix(const isc_sockaddr_t *a, const isc_sockaddr_t *b,
			  unsigned int prefixlen)
{
	unsigned char *pa, *pb;
110
111
112
	unsigned int ipabytes; /* Length of whole IP address in bytes */
	unsigned int nbytes;   /* Number of significant whole bytes */
	unsigned int nbits;    /* Number of significant leftover bits */
113
114
115
116
117
118
119
120
121
122
123
124
125
	
	REQUIRE(a != NULL && b != NULL);

	if (a->length != b->length)
		return (ISC_FALSE);

	if (a->type.sa.sa_family != b->type.sa.sa_family)
		return (ISC_FALSE);

	switch (a->type.sa.sa_family) {
	case AF_INET:
		pa = (unsigned char *) &a->type.sin.sin_addr;
		pb = (unsigned char *) &b->type.sin.sin_addr;
126
		ipabytes = 4;
127
128
129
130
		break;
	case AF_INET6:
		pa = ((unsigned char *) &a->type.sin6.sin6_addr);
		pb = ((unsigned char *) &b->type.sin6.sin6_addr);
131
		ipabytes = 16;
132
133
134
		break;
	default:
		pa = pb = NULL; /* Avoid silly compiler warning. */
135
		ipabytes = 0; /* Ditto. */
136
137
138
139
		return (ISC_FALSE); /* XXX got a better idea? */
	}

	/* Don't crash if we get a pattern like 10.0.0.1/9999999. */
140
141
	if (prefixlen > ipabytes * 8)
		prefixlen = ipabytes * 8;
142
143
144
145
146
147
148
149
150
151

	nbytes = prefixlen / 8;
	nbits = prefixlen % 8;

	if (nbytes > 0) {
		if (memcmp(pa, pb, nbytes) != 0)
			return (ISC_FALSE);
	}
	if (nbits > 0) {
		unsigned int bytea, byteb, mask;
152
		INSIST(nbytes < ipabytes);
153
154
155
156
157
158
159
160
161
162
		INSIST(nbits < 8);
		bytea = pa[nbytes];
		byteb = pb[nbytes];
		mask = (0xFF << (8-nbits)) & 0xFF;
		if ((bytea & mask) != (byteb & mask))
			return (ISC_FALSE);
	}
	return (ISC_TRUE);
}

163
164
isc_result_t
isc_sockaddr_totext(const isc_sockaddr_t *sockaddr, isc_buffer_t *target) {
Mark Andrews's avatar
Mark Andrews committed
165
	char abuf[sizeof "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255"];
166
	unsigned int alen;
Mark Andrews's avatar
Mark Andrews committed
167
	char pbuf[sizeof "65000"];
168
169
	unsigned int plen;
	isc_region_t avail;
170
171
172
	const struct sockaddr *sa;
	const struct sockaddr_in *sin;
	const struct sockaddr_in6 *sin6;
Mark Andrews's avatar
Mark Andrews committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

	REQUIRE(sockaddr != NULL);

	sa = &sockaddr->type.sa;
	switch (sa->sa_family) {
	case AF_INET:
		sin = &sockaddr->type.sin;
		inet_ntop(sa->sa_family, &sin->sin_addr, abuf, sizeof abuf);
		sprintf(pbuf, "%u", ntohs(sin->sin_port));
		break;
	case AF_INET6:
		sin6 = &sockaddr->type.sin6;
		inet_ntop(sa->sa_family, &sin6->sin6_addr, abuf, sizeof abuf);
		sprintf(pbuf, "%u", ntohs(sin6->sin6_port));
		break;
	default:
Bob Halley's avatar
Bob Halley committed
189
		return (ISC_R_NOTIMPLEMENTED);
Mark Andrews's avatar
Mark Andrews committed
190
	}
191
192
193
194
195

	alen = strlen(abuf);
	plen = strlen(pbuf);

	isc_buffer_available(target, &avail);
196
	if (alen + 1 + plen + 1 > avail.length)
197
198
		return (ISC_R_NOSPACE);
	    
Andreas Gustafsson's avatar
Andreas Gustafsson committed
199
	isc_buffer_putmem(target, (unsigned char *) abuf, alen);
Bob Halley's avatar
Bob Halley committed
200
	isc_buffer_putmem(target, (unsigned char *)"#", 1);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
201
	isc_buffer_putmem(target, (unsigned char *) pbuf, plen);
202

203
204
205
206
207
	/* Null terminate after used region. */
	isc_buffer_available(target, &avail);
	INSIST(avail.length >= 1);
	avail.base[0] = '\0';

208
	return (ISC_R_SUCCESS);
Mark Andrews's avatar
Mark Andrews committed
209
210
}

Bob Halley's avatar
Bob Halley committed
211
unsigned int
212
isc_sockaddr_hash(const isc_sockaddr_t *sockaddr, isc_boolean_t address_only) {
Bob Halley's avatar
Bob Halley committed
213
214
215
216
217
218
219
220
	unsigned int length;
	const unsigned char *s;
	unsigned int h = 0;
	unsigned int g;
	
	/*
	 * Provide a hash value for 'sockaddr'.
	 */
Michael Graff's avatar
Michael Graff committed
221

Bob Halley's avatar
Bob Halley committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
	REQUIRE(sockaddr != NULL);

	if (address_only) {
		switch (sockaddr->type.sa.sa_family) {
		case AF_INET:
			return (ntohl(sockaddr->type.sin.sin_addr.s_addr));
		case AF_INET6:
			s = (unsigned char *)&sockaddr->type.sin6.sin6_addr;
			length = sizeof sockaddr->type.sin6.sin6_addr;
			break;
		default:
			UNEXPECTED_ERROR(__FILE__, __LINE__,
					 "unknown address family: %d\n",
					 (int)sockaddr->type.sa.sa_family);
			s = (unsigned char *)&sockaddr->type;
			length = sockaddr->length;
		}
	} else {
		s = (unsigned char *)&sockaddr->type;
		length = sockaddr->length;
Michael Graff's avatar
Michael Graff committed
242
	}
Bob Halley's avatar
Bob Halley committed
243
244
245
246
247
248
249
250
251

	while (length > 0) {
		h = ( h << 4 ) + *s;
		if ((g = ( h & 0xf0000000 )) != 0) {
			h = h ^ (g >> 24);
			h = h ^ g;
		}
		s++;
		length--;
Michael Graff's avatar
Michael Graff committed
252
	}
Bob Halley's avatar
Bob Halley committed
253
	return (h);
Michael Graff's avatar
Michael Graff committed
254
}
Bob Halley's avatar
Bob Halley committed
255
256

void
257
isc_sockaddr_fromin(isc_sockaddr_t *sockaddr, const struct in_addr *ina,
258
		    in_port_t port)
Bob Halley's avatar
Bob Halley committed
259
{
260
	memset(sockaddr, 0, sizeof *sockaddr);
Bob Halley's avatar
Bob Halley committed
261
	sockaddr->type.sin.sin_family = AF_INET;
262
#ifdef ISC_PLATFORM_HAVESALEN
Bob Halley's avatar
Bob Halley committed
263
264
265
266
267
268
269
270
271
	sockaddr->type.sin.sin_len = sizeof sockaddr->type.sin;
#endif
	sockaddr->type.sin.sin_addr = *ina;
	sockaddr->type.sin.sin_port = htons(port);
	sockaddr->length = sizeof sockaddr->type.sin;
	ISC_LINK_INIT(sockaddr, link);
}

void
272
isc_sockaddr_fromin6(isc_sockaddr_t *sockaddr, const struct in6_addr *ina6,
273
		     in_port_t port)
Bob Halley's avatar
Bob Halley committed
274
{
275
	memset(sockaddr, 0, sizeof *sockaddr);
Bob Halley's avatar
Bob Halley committed
276
	sockaddr->type.sin6.sin6_family = AF_INET6;
277
#ifdef ISC_PLATFORM_HAVESALEN
Bob Halley's avatar
Bob Halley committed
278
279
280
281
282
283
284
	sockaddr->type.sin6.sin6_len = sizeof sockaddr->type.sin6;
#endif
	sockaddr->type.sin6.sin6_addr = *ina6;
	sockaddr->type.sin6.sin6_port = htons(port);
	sockaddr->length = sizeof sockaddr->type.sin6;
	ISC_LINK_INIT(sockaddr, link);
}
285
286

void
287
isc_sockaddr_v6fromin(isc_sockaddr_t *sockaddr, const struct in_addr *ina,
288
		      in_port_t port)
289
290
291
{
	memset(sockaddr, 0, sizeof *sockaddr);
	sockaddr->type.sin6.sin6_family = AF_INET6;
292
#ifdef ISC_PLATFORM_HAVESALEN
293
294
	sockaddr->type.sin6.sin6_len = sizeof sockaddr->type.sin6;
#endif
295
296
	sockaddr->type.sin6.sin6_addr.s6_addr[10] = 0xff;
	sockaddr->type.sin6.sin6_addr.s6_addr[11] = 0xff;
297
298
299
300
301
302
303
	memcpy(&sockaddr->type.sin6.sin6_addr.s6_addr[12], ina, 4);
	sockaddr->type.sin6.sin6_port = htons(port);
	sockaddr->length = sizeof sockaddr->type.sin6;
	ISC_LINK_INIT(sockaddr, link);
}

int
304
isc_sockaddr_pf(const isc_sockaddr_t *sockaddr) {
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325

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

#if (AF_INET == PF_INET && AF_INET6 == PF_INET6)
	/*
	 * Assume that PF_xxx == AF_xxx for all AF and PF.
	 */
	return (sockaddr->type.sa.sa_family);
#else
	switch (sockaddr->type.sa.sa_family) {
	case AF_INET:
		return (PF_INET);
	case AF_INET6:
		return (PF_INET);
	default:
		FATAL_ERROR(__FILE__, __LINE__, "unknown address family");
	}
#endif
}
Bob Halley's avatar
Bob Halley committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339

void
isc_sockaddr_setport(isc_sockaddr_t *sockaddr, in_port_t port) {
	switch (sockaddr->type.sa.sa_family) {
	case AF_INET:
		sockaddr->type.sin.sin_port = htons(port);
		break;
	case AF_INET6:
		sockaddr->type.sin6.sin6_port = htons(port);
		break;
	default:
		FATAL_ERROR(__FILE__, __LINE__, "unknown address family");
	}
}
David Lawrence's avatar
David Lawrence committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357

in_port_t
isc_sockaddr_getport(isc_sockaddr_t *sockaddr) {
	in_port_t port = 0;

	switch (sockaddr->type.sa.sa_family) {
	case AF_INET:
		port = ntohs(sockaddr->type.sin.sin_port);
		break;
	case AF_INET6:
		port = ntohs(sockaddr->type.sin6.sin6_port);
		break;
	default:
		FATAL_ERROR(__FILE__, __LINE__, "unknown address family");
	}

	return (port);
}