]>
git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/inet.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgment:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)defs.h 8.1 (Berkeley) 6/5/93
62 * Temporarily, copy these routines from the kernel,
63 * as we need to know about subnets.
67 extern struct interface
*ifnet
;
70 * Formulate an Internet address from network + host.
74 inet_makeaddr(net
, host
)
77 register struct interface
*ifp
;
82 mask
= IN_CLASSA_HOST
;
83 else if (IN_CLASSB(net
))
84 mask
= IN_CLASSB_HOST
;
86 mask
= IN_CLASSC_HOST
;
87 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
88 if ((ifp
->int_netmask
& net
) == ifp
->int_net
) {
89 mask
= ~ifp
->int_subnetmask
;
92 addr
= net
| (host
& mask
);
94 return (*(struct in_addr
*)&addr
);
98 * Return the network number from an internet address.
104 register u_long i
= ntohl(in
.s_addr
);
106 register struct interface
*ifp
;
109 net
= i
& IN_CLASSA_NET
;
110 else if (IN_CLASSB(i
))
111 net
= i
& IN_CLASSB_NET
;
113 net
= i
& IN_CLASSC_NET
;
116 * Check whether network is a subnet;
117 * if so, return subnet number.
119 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
120 if ((ifp
->int_netmask
& net
) == ifp
->int_net
)
121 return (i
& ifp
->int_subnetmask
);
126 * Return the host portion of an internet address.
131 register u_long i
= ntohl(in
.s_addr
);
132 register u_long net
, host
;
133 register struct interface
*ifp
;
136 net
= i
& IN_CLASSA_NET
;
137 host
= i
& IN_CLASSA_HOST
;
138 } else if (IN_CLASSB(i
)) {
139 net
= i
& IN_CLASSB_NET
;
140 host
= i
& IN_CLASSB_HOST
;
142 net
= i
& IN_CLASSC_NET
;
143 host
= i
& IN_CLASSC_HOST
;
147 * Check whether network is a subnet;
148 * if so, use the modified interpretation of `host'.
150 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
151 if ((ifp
->int_netmask
& net
) == ifp
->int_net
)
152 return (host
&~ ifp
->int_subnetmask
);
157 * Return the netmask pertaining to an internet address.
162 register u_long i
= ntohl(inaddr
);
163 register u_long mask
;
164 register struct interface
*ifp
;
168 } else if (IN_CLASSA(i
)) {
169 mask
= IN_CLASSA_NET
;
170 } else if (IN_CLASSB(i
)) {
171 mask
= IN_CLASSB_NET
;
173 mask
= IN_CLASSC_NET
;
176 * Check whether network is a subnet;
177 * if so, use the modified interpretation of `host'.
179 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
180 if ((ifp
->int_netmask
& i
) == ifp
->int_net
)
181 mask
= ifp
->int_subnetmask
;
182 return (htonl(mask
));
186 * Return RTF_HOST if the address is
187 * for an Internet host, RTF_SUBNET for a subnet,
191 struct sockaddr_in
*sin
;
193 register u_long i
= ntohl(sin
->sin_addr
.s_addr
);
194 register u_long net
, host
;
195 register struct interface
*ifp
;
198 net
= i
& IN_CLASSA_NET
;
199 host
= i
& IN_CLASSA_HOST
;
200 } else if (IN_CLASSB(i
)) {
201 net
= i
& IN_CLASSB_NET
;
202 host
= i
& IN_CLASSB_HOST
;
204 net
= i
& IN_CLASSC_NET
;
205 host
= i
& IN_CLASSC_HOST
;
209 * Check whether this network is subnetted;
210 * if so, check whether this is a subnet or a host.
212 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
213 if (net
== ifp
->int_net
) {
214 if (host
&~ ifp
->int_subnetmask
)
216 else if (ifp
->int_subnetmask
!= ifp
->int_netmask
)
219 return (0); /* network */
222 return (0); /* network */
228 * Return true if a route to subnet/host of route rt should be sent to dst.
229 * Send it only if dst is on the same logical network if not "internal",
230 * otherwise only if the route is the "internal" route for the logical net.
232 inet_sendroute(rt
, dst
)
234 struct sockaddr_in
*dst
;
237 ntohl(((struct sockaddr_in
*)&rt
->rt_dst
)->sin_addr
.s_addr
);
238 register u_long d
= ntohl(dst
->sin_addr
.s_addr
);
241 if ((r
& IN_CLASSA_NET
) == (d
& IN_CLASSA_NET
)) {
242 if ((r
& IN_CLASSA_HOST
) == 0)
243 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
246 if (r
& IN_CLASSA_HOST
)
248 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
249 } else if (IN_CLASSB(r
)) {
250 if ((r
& IN_CLASSB_NET
) == (d
& IN_CLASSB_NET
)) {
251 if ((r
& IN_CLASSB_HOST
) == 0)
252 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
255 if (r
& IN_CLASSB_HOST
)
257 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
259 if ((r
& IN_CLASSC_NET
) == (d
& IN_CLASSC_NET
)) {
260 if ((r
& IN_CLASSC_HOST
) == 0)
261 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
264 if (r
& IN_CLASSC_HOST
)
266 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);