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 $ */
5 * Subroutines to manipulate internet addresses in a safely portable
10 * Copyright (c) 1996 The Internet Software Consortium. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
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''.
49 * Return just the network number of an internet address...
52 subnet_number(struct iaddr addr
, struct iaddr mask
)
59 /* Both addresses must have the same length... */
60 if (addr
.len
!= mask
.len
)
64 for (i
= 0; i
< rv
.len
; i
++)
65 rv
.iabuf
[i
] = addr
.iabuf
[i
] & mask
.iabuf
[i
];
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
75 broadcast_addr(struct iaddr subnet
, struct iaddr mask
)
80 if (subnet
.len
!= mask
.len
) {
85 for (i
= 0; i
< subnet
.len
; i
++)
86 rv
.iabuf
[i
] = subnet
.iabuf
[i
] | (~mask
.iabuf
[i
] & 255);
93 addr_eq(struct iaddr addr1
, struct iaddr addr2
)
95 if (addr1
.len
!= addr2
.len
)
97 return (memcmp(addr1
.iabuf
, addr2
.iabuf
, addr1
.len
) == 0);
101 piaddr(struct iaddr addr
)
103 static char pbuf
[32];
107 memcpy(&a
, &(addr
.iabuf
), sizeof(struct in_addr
));
110 strlcpy(pbuf
, "<null address>", sizeof(pbuf
));
114 strlcpy(pbuf
, s
, sizeof(pbuf
));
116 strlcpy(pbuf
, "<invalid address>", sizeof(pbuf
));