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