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