]> git.ipfire.org Git - thirdparty/dhcp.git/blob - common/inet.c
b7c4ba4384803911d0cbcbe46b055c544845abf2
[thirdparty/dhcp.git] / common / inet.c
1 /* inet.c
2
3 Subroutines to manipulate internet addresses in a safely portable
4 way... */
5
6 /*
7 * Copyright (c) 1996 The Internet Software Consortium. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
41 */
42
43 #include "dhcpd.h"
44
45 /* Return just the network number of an internet address... */
46
47 struct iaddr subnet_number (addr, mask)
48 struct iaddr addr;
49 struct iaddr mask;
50 {
51 int i;
52 struct iaddr rv;
53
54 rv.len = 0;
55
56 /* Both addresses must have the same length... */
57 if (addr.len != mask.len)
58 return rv;
59
60 rv.len = addr.len;
61 for (i = 0; i < rv.len; i++)
62 rv.iabuf [i] = addr.iabuf [i] & mask.iabuf [i];
63 return rv;
64 }
65
66 /* Combine a network number and a integer to produce an internet address.
67 This won't work for subnets with more than 32 bits of host address, but
68 maybe this isn't a problem. */
69
70 struct iaddr ip_addr (subnet, mask, host_address)
71 struct iaddr subnet;
72 struct iaddr mask;
73 u_int32_t host_address;
74 {
75 int i, j, k;
76 u_int32_t swaddr;
77 struct iaddr rv;
78 unsigned char habuf [sizeof swaddr];
79
80 swaddr = htonl (host_address);
81 memcpy (habuf, &swaddr, sizeof swaddr);
82
83 /* Combine the subnet address and the host address. If
84 the host address is bigger than can fit in the subnet,
85 return a zero-length iaddr structure. */
86 rv = subnet;
87 j = rv.len - sizeof habuf;
88 for (i = sizeof habuf - 1; i >= 0; i--) {
89 if (mask.iabuf [i + j]) {
90 if (habuf [i] > (mask.iabuf [i + j] ^ 0xFF)) {
91 rv.len = 0;
92 return rv;
93 }
94 for (k = i - 1; k >= 0; k--) {
95 if (habuf [k]) {
96 rv.len = 0;
97 return rv;
98 }
99 }
100 rv.iabuf [i + j] |= habuf [i];
101 break;
102 } else
103 rv.iabuf [i + j] = habuf [i];
104 }
105
106 return rv;
107 }
108
109 /* Given a subnet number and netmask, return the address on that subnet
110 for which the host portion of the address is all ones (the standard
111 broadcast address). */
112
113 struct iaddr broadcast_addr (subnet, mask)
114 struct iaddr subnet;
115 struct iaddr mask;
116 {
117 int i, j, k;
118 struct iaddr rv;
119
120 if (subnet.len != mask.len) {
121 rv.len = 0;
122 return rv;
123 }
124
125 for (i = 0; i < subnet.len; i++) {
126 rv.iabuf [i] = subnet.iabuf [i] | (~mask.iabuf [i] & 255);
127 }
128 rv.len = subnet.len;
129
130 return rv;
131 }
132
133 u_int32_t host_addr (addr, mask)
134 struct iaddr addr;
135 struct iaddr mask;
136 {
137 int i;
138 u_int32_t swaddr;
139 struct iaddr rv;
140
141 rv.len = 0;
142
143 /* Mask out the network bits... */
144 rv.len = addr.len;
145 for (i = 0; i < rv.len; i++)
146 rv.iabuf [i] = addr.iabuf [i] & ~mask.iabuf [i];
147
148 /* Copy out up to 32 bits... */
149 memcpy (&swaddr, &rv.iabuf [rv.len - sizeof swaddr], sizeof swaddr);
150
151 /* Swap it and return it. */
152 return ntohl (swaddr);
153 }
154
155 int addr_eq (addr1, addr2)
156 struct iaddr addr1, addr2;
157 {
158 if (addr1.len != addr2.len)
159 return 0;
160 return memcmp (addr1.iabuf, addr2.iabuf, addr1.len) == 0;
161 }
162
163 char *piaddr (addr)
164 struct iaddr addr;
165 {
166 static char pbuf [4 * 16];
167 char *s = pbuf;
168 int i;
169
170 if (addr.len == 0) {
171 strcpy (s, "<null address>");
172 }
173 for (i = 0; i < addr.len; i++) {
174 sprintf (s, "%s%d", i ? "." : "", addr.iabuf [i]);
175 s += strlen (s);
176 }
177 return pbuf;
178 }