xref: /openbsd/usr.sbin/dhcpd/inet.c (revision 898184e3)
1 /*	$OpenBSD: inet.c,v 1.5 2004/09/16 21:27:44 claudio 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 "dhcpd.h"
46 
47 /*
48  * Return just the network number of an internet address...
49  */
50 struct iaddr
51 subnet_number(struct iaddr addr, struct iaddr mask)
52 {
53 	struct iaddr rv;
54 	int i;
55 
56 	rv.len = 0;
57 
58 	/* Both addresses must have the same length... */
59 	if (addr.len != mask.len)
60 		return (rv);
61 
62 	rv.len = addr.len;
63 	for (i = 0; i < rv.len; i++)
64 		rv.iabuf[i] = addr.iabuf[i] & mask.iabuf[i];
65 	return (rv);
66 }
67 
68 /*
69  * Combine a network number and a integer to produce an internet address.
70  * This won't work for subnets with more than 32 bits of host address, but
71  * maybe this isn't a problem.
72  */
73 struct iaddr
74 ip_addr(struct iaddr subnet, struct iaddr mask, u_int32_t host_address)
75 {
76 	int i, j, k;
77 	u_int32_t swaddr;
78 	unsigned char habuf[sizeof(swaddr)];
79 	struct iaddr rv;
80 
81 	swaddr = htonl(host_address);
82 	memcpy(habuf, &swaddr, sizeof(swaddr));
83 
84 	/*
85 	 * Combine the subnet address and the host address.   If the
86 	 * host address is bigger than can fit in the subnet, return a
87 	 * zero-length iaddr structure.
88 	 */
89 	rv = subnet;
90 	j = rv.len - sizeof(habuf);
91 	for (i = sizeof(habuf) - 1; i >= 0; i--) {
92 		if (mask.iabuf[i + j]) {
93 			if (habuf[i] > (mask.iabuf[i + j] ^ 0xFF)) {
94 				rv.len = 0;
95 				return (rv);
96 			}
97 			for (k = i - 1; k >= 0; k--)
98 				if (habuf[k]) {
99 					rv.len = 0;
100 					return (rv);
101 				}
102 			rv.iabuf[i + j] |= habuf[i];
103 			break;
104 		} else
105 			rv.iabuf[i + j] = habuf[i];
106 	}
107 
108 	return (rv);
109 }
110 
111 u_int32_t
112 host_addr(struct iaddr addr, struct iaddr mask)
113 {
114 	int i;
115 	u_int32_t swaddr;
116 	struct iaddr rv;
117 
118 	rv.len = 0;
119 
120 	/* Mask out the network bits... */
121 	rv.len = addr.len;
122 	for (i = 0; i < rv.len; i++)
123 		rv.iabuf[i] = addr.iabuf[i] & ~mask.iabuf[i];
124 
125 	/* Copy out up to 32 bits... */
126 	memcpy(&swaddr, &rv.iabuf[rv.len - sizeof(swaddr)], sizeof(swaddr));
127 
128 	/* Swap it and return it. */
129 	return (ntohl(swaddr));
130 }
131 
132 int
133 addr_eq(struct iaddr addr1, struct iaddr addr2)
134 {
135 	if (addr1.len != addr2.len)
136 		return (0);
137 	return (memcmp(addr1.iabuf, addr2.iabuf, addr1.len) == 0);
138 }
139 
140 char *
141 piaddr(struct iaddr addr)
142 {
143 	static char pbuf[32];
144 	const char *s;
145 
146 	if (addr.len == 0)
147 		strlcpy(pbuf, "<null address>", sizeof(pbuf));
148 	else {
149 		s = inet_ntop(AF_INET, &addr.iabuf, pbuf, sizeof pbuf);
150 		if (s == NULL)
151 			strlcpy(pbuf, "<invalid address>", sizeof(pbuf));
152 	}
153 	return (pbuf);
154 }
155