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