xref: /freebsd/sbin/dhclient/inet.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
147c08596SBrooks Davis /*	$OpenBSD: inet.c,v 1.7 2004/05/04 21:48:16 deraadt Exp $	*/
247c08596SBrooks Davis 
347c08596SBrooks Davis /*
447c08596SBrooks Davis  * Subroutines to manipulate internet addresses in a safely portable
547c08596SBrooks Davis  * way...
647c08596SBrooks Davis  */
747c08596SBrooks Davis 
88a16b7a1SPedro F. Giffuni /*-
98a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
108a16b7a1SPedro F. Giffuni  *
1147c08596SBrooks Davis  * Copyright (c) 1996 The Internet Software Consortium.  All rights reserved.
1247c08596SBrooks Davis  *
1347c08596SBrooks Davis  * Redistribution and use in source and binary forms, with or without
1447c08596SBrooks Davis  * modification, are permitted provided that the following conditions
1547c08596SBrooks Davis  * are met:
1647c08596SBrooks Davis  *
1747c08596SBrooks Davis  * 1. Redistributions of source code must retain the above copyright
1847c08596SBrooks Davis  *    notice, this list of conditions and the following disclaimer.
1947c08596SBrooks Davis  * 2. Redistributions in binary form must reproduce the above copyright
2047c08596SBrooks Davis  *    notice, this list of conditions and the following disclaimer in the
2147c08596SBrooks Davis  *    documentation and/or other materials provided with the distribution.
2247c08596SBrooks Davis  * 3. Neither the name of The Internet Software Consortium nor the names
2347c08596SBrooks Davis  *    of its contributors may be used to endorse or promote products derived
2447c08596SBrooks Davis  *    from this software without specific prior written permission.
2547c08596SBrooks Davis  *
2647c08596SBrooks Davis  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
2747c08596SBrooks Davis  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2847c08596SBrooks Davis  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2947c08596SBrooks Davis  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
3047c08596SBrooks Davis  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
3147c08596SBrooks Davis  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3247c08596SBrooks Davis  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3347c08596SBrooks Davis  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3447c08596SBrooks Davis  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3547c08596SBrooks Davis  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3647c08596SBrooks Davis  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3747c08596SBrooks Davis  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3847c08596SBrooks Davis  * SUCH DAMAGE.
3947c08596SBrooks Davis  *
4047c08596SBrooks Davis  * This software has been written for the Internet Software Consortium
4147c08596SBrooks Davis  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
4247c08596SBrooks Davis  * Enterprises.  To learn more about the Internet Software Consortium,
4347c08596SBrooks Davis  * see ``http://www.vix.com/isc''.  To learn more about Vixie
4447c08596SBrooks Davis  * Enterprises, see ``http://www.vix.com''.
4547c08596SBrooks Davis  */
4647c08596SBrooks Davis 
478794fdbbSBrooks Davis #include <sys/cdefs.h>
4847c08596SBrooks Davis #include "dhcpd.h"
4947c08596SBrooks Davis 
5047c08596SBrooks Davis /*
5147c08596SBrooks Davis  * Return just the network number of an internet address...
5247c08596SBrooks Davis  */
5347c08596SBrooks Davis struct iaddr
subnet_number(struct iaddr addr,struct iaddr mask)5447c08596SBrooks Davis subnet_number(struct iaddr addr, struct iaddr mask)
5547c08596SBrooks Davis {
5647c08596SBrooks Davis 	struct iaddr rv;
57*afe6f835SAlan Somers 	unsigned i;
5847c08596SBrooks Davis 
5947c08596SBrooks Davis 	rv.len = 0;
6047c08596SBrooks Davis 
6147c08596SBrooks Davis 	/* Both addresses must have the same length... */
6247c08596SBrooks Davis 	if (addr.len != mask.len)
6347c08596SBrooks Davis 		return (rv);
6447c08596SBrooks Davis 
6547c08596SBrooks Davis 	rv.len = addr.len;
6647c08596SBrooks Davis 	for (i = 0; i < rv.len; i++)
6747c08596SBrooks Davis 		rv.iabuf[i] = addr.iabuf[i] & mask.iabuf[i];
6847c08596SBrooks Davis 	return (rv);
6947c08596SBrooks Davis }
7047c08596SBrooks Davis 
7147c08596SBrooks Davis /*
7247c08596SBrooks Davis  * Given a subnet number and netmask, return the address on that subnet
7347c08596SBrooks Davis  * for which the host portion of the address is all ones (the standard
7447c08596SBrooks Davis  * broadcast address).
7547c08596SBrooks Davis  */
7647c08596SBrooks Davis struct iaddr
broadcast_addr(struct iaddr subnet,struct iaddr mask)7747c08596SBrooks Davis broadcast_addr(struct iaddr subnet, struct iaddr mask)
7847c08596SBrooks Davis {
7947c08596SBrooks Davis 	struct iaddr rv;
80*afe6f835SAlan Somers 	unsigned i;
8147c08596SBrooks Davis 
8247c08596SBrooks Davis 	if (subnet.len != mask.len) {
8347c08596SBrooks Davis 		rv.len = 0;
8447c08596SBrooks Davis 		return (rv);
8547c08596SBrooks Davis 	}
8647c08596SBrooks Davis 
8747c08596SBrooks Davis 	for (i = 0; i < subnet.len; i++)
8847c08596SBrooks Davis 		rv.iabuf[i] = subnet.iabuf[i] | (~mask.iabuf[i] & 255);
8947c08596SBrooks Davis 	rv.len = subnet.len;
9047c08596SBrooks Davis 
9147c08596SBrooks Davis 	return (rv);
9247c08596SBrooks Davis }
9347c08596SBrooks Davis 
9447c08596SBrooks Davis int
addr_eq(struct iaddr addr1,struct iaddr addr2)9547c08596SBrooks Davis addr_eq(struct iaddr addr1, struct iaddr addr2)
9647c08596SBrooks Davis {
9747c08596SBrooks Davis 	if (addr1.len != addr2.len)
9847c08596SBrooks Davis 		return (0);
9947c08596SBrooks Davis 	return (memcmp(addr1.iabuf, addr2.iabuf, addr1.len) == 0);
10047c08596SBrooks Davis }
10147c08596SBrooks Davis 
10247c08596SBrooks Davis char *
piaddr(struct iaddr addr)10347c08596SBrooks Davis piaddr(struct iaddr addr)
10447c08596SBrooks Davis {
10547c08596SBrooks Davis 	static char pbuf[32];
10647c08596SBrooks Davis 	struct in_addr a;
10747c08596SBrooks Davis 	char *s;
10847c08596SBrooks Davis 
10947c08596SBrooks Davis 	memcpy(&a, &(addr.iabuf), sizeof(struct in_addr));
11047c08596SBrooks Davis 
11147c08596SBrooks Davis 	if (addr.len == 0)
11247c08596SBrooks Davis 		strlcpy(pbuf, "<null address>", sizeof(pbuf));
11347c08596SBrooks Davis 	else {
11447c08596SBrooks Davis 		s = inet_ntoa(a);
11547c08596SBrooks Davis 		if (s != NULL)
11647c08596SBrooks Davis 			strlcpy(pbuf, s, sizeof(pbuf));
11747c08596SBrooks Davis 		else
11847c08596SBrooks Davis 			strlcpy(pbuf, "<invalid address>", sizeof(pbuf));
11947c08596SBrooks Davis 	}
12047c08596SBrooks Davis 	return (pbuf);
12147c08596SBrooks Davis }
122