]>
git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/inet.c
8990b7ba013254b49b5d0f9228a89af5812c24da
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1983, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgment:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)defs.h 8.1 (Berkeley) 6/5/93
60 * Temporarily, copy these routines from the kernel,
61 * as we need to know about subnets.
65 extern struct interface
*ifnet
;
68 * Formulate an Internet address from network + host.
72 inet_makeaddr(net
, host
)
75 register struct interface
*ifp
;
80 mask
= IN_CLASSA_HOST
;
81 else if (IN_CLASSB(net
))
82 mask
= IN_CLASSB_HOST
;
84 mask
= IN_CLASSC_HOST
;
85 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
86 if ((ifp
->int_netmask
& net
) == ifp
->int_net
) {
87 mask
= ~ifp
->int_subnetmask
;
90 addr
= net
| (host
& mask
);
92 return (*(struct in_addr
*)&addr
);
96 * Return the network number from an internet address.
102 register u_long i
= ntohl(in
.s_addr
);
104 register struct interface
*ifp
;
107 net
= i
& IN_CLASSA_NET
;
108 else if (IN_CLASSB(i
))
109 net
= i
& IN_CLASSB_NET
;
111 net
= i
& IN_CLASSC_NET
;
114 * Check whether network is a subnet;
115 * if so, return subnet number.
117 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
118 if ((ifp
->int_netmask
& net
) == ifp
->int_net
)
119 return (i
& ifp
->int_subnetmask
);
124 * Return the host portion of an internet address.
129 register u_long i
= ntohl(in
.s_addr
);
130 register u_long net
, host
;
131 register struct interface
*ifp
;
134 net
= i
& IN_CLASSA_NET
;
135 host
= i
& IN_CLASSA_HOST
;
136 } else if (IN_CLASSB(i
)) {
137 net
= i
& IN_CLASSB_NET
;
138 host
= i
& IN_CLASSB_HOST
;
140 net
= i
& IN_CLASSC_NET
;
141 host
= i
& IN_CLASSC_HOST
;
145 * Check whether network is a subnet;
146 * if so, use the modified interpretation of `host'.
148 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
149 if ((ifp
->int_netmask
& net
) == ifp
->int_net
)
150 return (host
&~ ifp
->int_subnetmask
);
155 * Return the netmask pertaining to an internet address.
160 register u_long i
= ntohl(inaddr
);
161 register u_long mask
;
162 register struct interface
*ifp
;
166 } else if (IN_CLASSA(i
)) {
167 mask
= IN_CLASSA_NET
;
168 } else if (IN_CLASSB(i
)) {
169 mask
= IN_CLASSB_NET
;
171 mask
= IN_CLASSC_NET
;
174 * Check whether network is a subnet;
175 * if so, use the modified interpretation of `host'.
177 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
178 if ((ifp
->int_netmask
& i
) == ifp
->int_net
)
179 mask
= ifp
->int_subnetmask
;
180 return (htonl(mask
));
184 * Return RTF_HOST if the address is
185 * for an Internet host, RTF_SUBNET for a subnet,
189 struct sockaddr_in
*sin
;
191 register u_long i
= ntohl(sin
->sin_addr
.s_addr
);
192 register u_long net
, host
;
193 register struct interface
*ifp
;
196 net
= i
& IN_CLASSA_NET
;
197 host
= i
& IN_CLASSA_HOST
;
198 } else if (IN_CLASSB(i
)) {
199 net
= i
& IN_CLASSB_NET
;
200 host
= i
& IN_CLASSB_HOST
;
202 net
= i
& IN_CLASSC_NET
;
203 host
= i
& IN_CLASSC_HOST
;
207 * Check whether this network is subnetted;
208 * if so, check whether this is a subnet or a host.
210 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
211 if (net
== ifp
->int_net
) {
212 if (host
&~ ifp
->int_subnetmask
)
214 else if (ifp
->int_subnetmask
!= ifp
->int_netmask
)
217 return (0); /* network */
220 return (0); /* network */
226 * Return true if a route to subnet/host of route rt should be sent to dst.
227 * Send it only if dst is on the same logical network if not "internal",
228 * otherwise only if the route is the "internal" route for the logical net.
230 inet_sendroute(rt
, dst
)
232 struct sockaddr_in
*dst
;
235 ntohl(((struct sockaddr_in
*)&rt
->rt_dst
)->sin_addr
.s_addr
);
236 register u_long d
= ntohl(dst
->sin_addr
.s_addr
);
239 if ((r
& IN_CLASSA_NET
) == (d
& IN_CLASSA_NET
)) {
240 if ((r
& IN_CLASSA_HOST
) == 0)
241 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
244 if (r
& IN_CLASSA_HOST
)
246 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
247 } else if (IN_CLASSB(r
)) {
248 if ((r
& IN_CLASSB_NET
) == (d
& IN_CLASSB_NET
)) {
249 if ((r
& IN_CLASSB_HOST
) == 0)
250 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
253 if (r
& IN_CLASSB_HOST
)
255 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
257 if ((r
& IN_CLASSC_NET
) == (d
& IN_CLASSC_NET
)) {
258 if ((r
& IN_CLASSC_HOST
) == 0)
259 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
262 if (r
& IN_CLASSC_HOST
)
264 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);