1147072Sbrooks/* $OpenBSD: inet.c,v 1.7 2004/05/04 21:48:16 deraadt Exp $ */ 2147072Sbrooks 3147072Sbrooks/* 4147072Sbrooks * Subroutines to manipulate internet addresses in a safely portable 5147072Sbrooks * way... 6147072Sbrooks */ 7147072Sbrooks 8147072Sbrooks/* 9147072Sbrooks * Copyright (c) 1996 The Internet Software Consortium. All rights reserved. 10147072Sbrooks * 11147072Sbrooks * Redistribution and use in source and binary forms, with or without 12147072Sbrooks * modification, are permitted provided that the following conditions 13147072Sbrooks * are met: 14147072Sbrooks * 15147072Sbrooks * 1. Redistributions of source code must retain the above copyright 16147072Sbrooks * notice, this list of conditions and the following disclaimer. 17147072Sbrooks * 2. Redistributions in binary form must reproduce the above copyright 18147072Sbrooks * notice, this list of conditions and the following disclaimer in the 19147072Sbrooks * documentation and/or other materials provided with the distribution. 20147072Sbrooks * 3. Neither the name of The Internet Software Consortium nor the names 21147072Sbrooks * of its contributors may be used to endorse or promote products derived 22147072Sbrooks * from this software without specific prior written permission. 23147072Sbrooks * 24147072Sbrooks * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND 25147072Sbrooks * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 26147072Sbrooks * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 27147072Sbrooks * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28147072Sbrooks * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR 29147072Sbrooks * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30147072Sbrooks * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31147072Sbrooks * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 32147072Sbrooks * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 33147072Sbrooks * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 34147072Sbrooks * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 35147072Sbrooks * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36147072Sbrooks * SUCH DAMAGE. 37147072Sbrooks * 38147072Sbrooks * This software has been written for the Internet Software Consortium 39147072Sbrooks * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie 40147072Sbrooks * Enterprises. To learn more about the Internet Software Consortium, 41147072Sbrooks * see ``http://www.vix.com/isc''. To learn more about Vixie 42147072Sbrooks * Enterprises, see ``http://www.vix.com''. 43147072Sbrooks */ 44147072Sbrooks 45149399Sbrooks#include <sys/cdefs.h> 46149399Sbrooks__FBSDID("$FreeBSD$"); 47149399Sbrooks 48147072Sbrooks#include "dhcpd.h" 49147072Sbrooks 50147072Sbrooks/* 51147072Sbrooks * Return just the network number of an internet address... 52147072Sbrooks */ 53147072Sbrooksstruct iaddr 54147072Sbrookssubnet_number(struct iaddr addr, struct iaddr mask) 55147072Sbrooks{ 56147072Sbrooks struct iaddr rv; 57147072Sbrooks int i; 58147072Sbrooks 59147072Sbrooks rv.len = 0; 60147072Sbrooks 61147072Sbrooks /* Both addresses must have the same length... */ 62147072Sbrooks if (addr.len != mask.len) 63147072Sbrooks return (rv); 64147072Sbrooks 65147072Sbrooks rv.len = addr.len; 66147072Sbrooks for (i = 0; i < rv.len; i++) 67147072Sbrooks rv.iabuf[i] = addr.iabuf[i] & mask.iabuf[i]; 68147072Sbrooks return (rv); 69147072Sbrooks} 70147072Sbrooks 71147072Sbrooks/* 72147072Sbrooks * Given a subnet number and netmask, return the address on that subnet 73147072Sbrooks * for which the host portion of the address is all ones (the standard 74147072Sbrooks * broadcast address). 75147072Sbrooks */ 76147072Sbrooksstruct iaddr 77147072Sbrooksbroadcast_addr(struct iaddr subnet, struct iaddr mask) 78147072Sbrooks{ 79147072Sbrooks struct iaddr rv; 80147072Sbrooks int i; 81147072Sbrooks 82147072Sbrooks if (subnet.len != mask.len) { 83147072Sbrooks rv.len = 0; 84147072Sbrooks return (rv); 85147072Sbrooks } 86147072Sbrooks 87147072Sbrooks for (i = 0; i < subnet.len; i++) 88147072Sbrooks rv.iabuf[i] = subnet.iabuf[i] | (~mask.iabuf[i] & 255); 89147072Sbrooks rv.len = subnet.len; 90147072Sbrooks 91147072Sbrooks return (rv); 92147072Sbrooks} 93147072Sbrooks 94147072Sbrooksint 95147072Sbrooksaddr_eq(struct iaddr addr1, struct iaddr addr2) 96147072Sbrooks{ 97147072Sbrooks if (addr1.len != addr2.len) 98147072Sbrooks return (0); 99147072Sbrooks return (memcmp(addr1.iabuf, addr2.iabuf, addr1.len) == 0); 100147072Sbrooks} 101147072Sbrooks 102147072Sbrookschar * 103147072Sbrookspiaddr(struct iaddr addr) 104147072Sbrooks{ 105147072Sbrooks static char pbuf[32]; 106147072Sbrooks struct in_addr a; 107147072Sbrooks char *s; 108147072Sbrooks 109147072Sbrooks memcpy(&a, &(addr.iabuf), sizeof(struct in_addr)); 110147072Sbrooks 111147072Sbrooks if (addr.len == 0) 112147072Sbrooks strlcpy(pbuf, "<null address>", sizeof(pbuf)); 113147072Sbrooks else { 114147072Sbrooks s = inet_ntoa(a); 115147072Sbrooks if (s != NULL) 116147072Sbrooks strlcpy(pbuf, s, sizeof(pbuf)); 117147072Sbrooks else 118147072Sbrooks strlcpy(pbuf, "<invalid address>", sizeof(pbuf)); 119147072Sbrooks } 120147072Sbrooks return (pbuf); 121147072Sbrooks} 122