1/*	$OpenBSD: inet.c,v 1.7 2004/05/04 21:48:16 deraadt Exp $	*/
2
3/*
4 * Subroutines to manipulate internet addresses in a safely portable
5 * way...
6 */
7
8/*
9 * Copyright (c) 1996 The Internet Software Consortium.  All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of The Internet Software Consortium nor the names
21 *    of its contributors may be used to endorse or promote products derived
22 *    from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
25 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * This software has been written for the Internet Software Consortium
39 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
40 * Enterprises.  To learn more about the Internet Software Consortium,
41 * see ``http://www.vix.com/isc''.  To learn more about Vixie
42 * Enterprises, see ``http://www.vix.com''.
43 */
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD$");
47
48#include "dhcpd.h"
49
50/*
51 * Return just the network number of an internet address...
52 */
53struct iaddr
54subnet_number(struct iaddr addr, struct iaddr mask)
55{
56	struct iaddr rv;
57	int i;
58
59	rv.len = 0;
60
61	/* Both addresses must have the same length... */
62	if (addr.len != mask.len)
63		return (rv);
64
65	rv.len = addr.len;
66	for (i = 0; i < rv.len; i++)
67		rv.iabuf[i] = addr.iabuf[i] & mask.iabuf[i];
68	return (rv);
69}
70
71/*
72 * Given a subnet number and netmask, return the address on that subnet
73 * for which the host portion of the address is all ones (the standard
74 * broadcast address).
75 */
76struct iaddr
77broadcast_addr(struct iaddr subnet, struct iaddr mask)
78{
79	struct iaddr rv;
80	int i;
81
82	if (subnet.len != mask.len) {
83		rv.len = 0;
84		return (rv);
85	}
86
87	for (i = 0; i < subnet.len; i++)
88		rv.iabuf[i] = subnet.iabuf[i] | (~mask.iabuf[i] & 255);
89	rv.len = subnet.len;
90
91	return (rv);
92}
93
94int
95addr_eq(struct iaddr addr1, struct iaddr addr2)
96{
97	if (addr1.len != addr2.len)
98		return (0);
99	return (memcmp(addr1.iabuf, addr2.iabuf, addr1.len) == 0);
100}
101
102char *
103piaddr(struct iaddr addr)
104{
105	static char pbuf[32];
106	struct in_addr a;
107	char *s;
108
109	memcpy(&a, &(addr.iabuf), sizeof(struct in_addr));
110
111	if (addr.len == 0)
112		strlcpy(pbuf, "<null address>", sizeof(pbuf));
113	else {
114		s = inet_ntoa(a);
115		if (s != NULL)
116			strlcpy(pbuf, s, sizeof(pbuf));
117		else
118			strlcpy(pbuf, "<invalid address>", sizeof(pbuf));
119	}
120	return (pbuf);
121}
122