]>
git.ipfire.org Git - thirdparty/dhcp.git/blob - common/inet.c
b7c4ba4384803911d0cbcbe46b055c544845abf2
3 Subroutines to manipulate internet addresses in a safely portable
7 * Copyright (c) 1996 The Internet Software Consortium. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
38 * Enterprises. To learn more about the Internet Software Consortium,
39 * see ``http://www.vix.com/isc''. To learn more about Vixie
40 * Enterprises, see ``http://www.vix.com''.
45 /* Return just the network number of an internet address... */
47 struct iaddr
subnet_number (addr
, mask
)
56 /* Both addresses must have the same length... */
57 if (addr
.len
!= mask
.len
)
61 for (i
= 0; i
< rv
.len
; i
++)
62 rv
.iabuf
[i
] = addr
.iabuf
[i
] & mask
.iabuf
[i
];
66 /* Combine a network number and a integer to produce an internet address.
67 This won't work for subnets with more than 32 bits of host address, but
68 maybe this isn't a problem. */
70 struct iaddr
ip_addr (subnet
, mask
, host_address
)
73 u_int32_t host_address
;
78 unsigned char habuf
[sizeof swaddr
];
80 swaddr
= htonl (host_address
);
81 memcpy (habuf
, &swaddr
, sizeof swaddr
);
83 /* Combine the subnet address and the host address. If
84 the host address is bigger than can fit in the subnet,
85 return a zero-length iaddr structure. */
87 j
= rv
.len
- sizeof habuf
;
88 for (i
= sizeof habuf
- 1; i
>= 0; i
--) {
89 if (mask
.iabuf
[i
+ j
]) {
90 if (habuf
[i
] > (mask
.iabuf
[i
+ j
] ^ 0xFF)) {
94 for (k
= i
- 1; k
>= 0; k
--) {
100 rv
.iabuf
[i
+ j
] |= habuf
[i
];
103 rv
.iabuf
[i
+ j
] = habuf
[i
];
109 /* Given a subnet number and netmask, return the address on that subnet
110 for which the host portion of the address is all ones (the standard
111 broadcast address). */
113 struct iaddr
broadcast_addr (subnet
, mask
)
120 if (subnet
.len
!= mask
.len
) {
125 for (i
= 0; i
< subnet
.len
; i
++) {
126 rv
.iabuf
[i
] = subnet
.iabuf
[i
] | (~mask
.iabuf
[i
] & 255);
133 u_int32_t
host_addr (addr
, mask
)
143 /* Mask out the network bits... */
145 for (i
= 0; i
< rv
.len
; i
++)
146 rv
.iabuf
[i
] = addr
.iabuf
[i
] & ~mask
.iabuf
[i
];
148 /* Copy out up to 32 bits... */
149 memcpy (&swaddr
, &rv
.iabuf
[rv
.len
- sizeof swaddr
], sizeof swaddr
);
151 /* Swap it and return it. */
152 return ntohl (swaddr
);
155 int addr_eq (addr1
, addr2
)
156 struct iaddr addr1
, addr2
;
158 if (addr1
.len
!= addr2
.len
)
160 return memcmp (addr1
.iabuf
, addr2
.iabuf
, addr1
.len
) == 0;
166 static char pbuf
[4 * 16];
171 strcpy (s
, "<null address>");
173 for (i
= 0; i
< addr
.len
; i
++) {
174 sprintf (s
, "%s%d", i
? "." : "", addr
.iabuf
[i
]);