/* $OpenBSD: inet.c,v 1.7 2004/05/04 21:48:16 deraadt Exp $ */12/*3* Subroutines to manipulate internet addresses in a safely portable4* way...5*/67/*-8* SPDX-License-Identifier: BSD-3-Clause9*10* Copyright (c) 1996 The Internet Software Consortium. All rights reserved.11*12* Redistribution and use in source and binary forms, with or without13* modification, are permitted provided that the following conditions14* are met:15*16* 1. Redistributions of source code must retain the above copyright17* notice, this list of conditions and the following disclaimer.18* 2. Redistributions in binary form must reproduce the above copyright19* notice, this list of conditions and the following disclaimer in the20* documentation and/or other materials provided with the distribution.21* 3. Neither the name of The Internet Software Consortium nor the names22* of its contributors may be used to endorse or promote products derived23* from this software without specific prior written permission.24*25* THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND26* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,27* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF28* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE29* DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR30* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,31* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT32* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF33* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND34* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,35* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT36* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF37* SUCH DAMAGE.38*39* This software has been written for the Internet Software Consortium40* by Ted Lemon <[email protected]> in cooperation with Vixie41* Enterprises. To learn more about the Internet Software Consortium,42* see ``http://www.vix.com/isc''. To learn more about Vixie43* Enterprises, see ``http://www.vix.com''.44*/4546#include <sys/cdefs.h>47#include "dhcpd.h"4849/*50* Return just the network number of an internet address...51*/52struct iaddr53subnet_number(struct iaddr addr, struct iaddr mask)54{55struct iaddr rv;56unsigned i;5758rv.len = 0;5960/* Both addresses must have the same length... */61if (addr.len != mask.len)62return (rv);6364rv.len = addr.len;65for (i = 0; i < rv.len; i++)66rv.iabuf[i] = addr.iabuf[i] & mask.iabuf[i];67return (rv);68}6970/*71* Given a subnet number and netmask, return the address on that subnet72* for which the host portion of the address is all ones (the standard73* broadcast address).74*/75struct iaddr76broadcast_addr(struct iaddr subnet, struct iaddr mask)77{78struct iaddr rv;79unsigned i;8081if (subnet.len != mask.len) {82rv.len = 0;83return (rv);84}8586for (i = 0; i < subnet.len; i++)87rv.iabuf[i] = subnet.iabuf[i] | (~mask.iabuf[i] & 255);88rv.len = subnet.len;8990return (rv);91}9293int94addr_eq(struct iaddr addr1, struct iaddr addr2)95{96if (addr1.len != addr2.len)97return (0);98return (memcmp(addr1.iabuf, addr2.iabuf, addr1.len) == 0);99}100101char *102piaddr(struct iaddr addr)103{104static char pbuf[32];105struct in_addr a;106char *s;107108memcpy(&a, &(addr.iabuf), sizeof(struct in_addr));109110if (addr.len == 0)111strlcpy(pbuf, "<null address>", sizeof(pbuf));112else {113s = inet_ntoa(a);114if (s != NULL)115strlcpy(pbuf, s, sizeof(pbuf));116else117strlcpy(pbuf, "<invalid address>", sizeof(pbuf));118}119return (pbuf);120}121122123