]>
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 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgment:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)defs.h 8.1 (Berkeley) 6/5/93
61 * Temporarily, copy these routines from the kernel,
62 * as we need to know about subnets.
66 extern struct interface
*ifnet
;
69 * Formulate an Internet address from network + host.
73 inet_makeaddr(net
, host
)
76 register struct interface
*ifp
;
81 mask
= IN_CLASSA_HOST
;
82 else if (IN_CLASSB(net
))
83 mask
= IN_CLASSB_HOST
;
85 mask
= IN_CLASSC_HOST
;
86 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
87 if ((ifp
->int_netmask
& net
) == ifp
->int_net
) {
88 mask
= ~ifp
->int_subnetmask
;
91 addr
= net
| (host
& mask
);
93 return (*(struct in_addr
*)&addr
);
97 * Return the network number from an internet address.
103 register u_long i
= ntohl(in
.s_addr
);
105 register struct interface
*ifp
;
108 net
= i
& IN_CLASSA_NET
;
109 else if (IN_CLASSB(i
))
110 net
= i
& IN_CLASSB_NET
;
112 net
= i
& IN_CLASSC_NET
;
115 * Check whether network is a subnet;
116 * if so, return subnet number.
118 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
119 if ((ifp
->int_netmask
& net
) == ifp
->int_net
)
120 return (i
& ifp
->int_subnetmask
);
125 * Return the host portion of an internet address.
130 register u_long i
= ntohl(in
.s_addr
);
131 register u_long net
, host
;
132 register struct interface
*ifp
;
135 net
= i
& IN_CLASSA_NET
;
136 host
= i
& IN_CLASSA_HOST
;
137 } else if (IN_CLASSB(i
)) {
138 net
= i
& IN_CLASSB_NET
;
139 host
= i
& IN_CLASSB_HOST
;
141 net
= i
& IN_CLASSC_NET
;
142 host
= i
& IN_CLASSC_HOST
;
146 * Check whether network is a subnet;
147 * if so, use the modified interpretation of `host'.
149 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
150 if ((ifp
->int_netmask
& net
) == ifp
->int_net
)
151 return (host
&~ ifp
->int_subnetmask
);
156 * Return the netmask pertaining to an internet address.
161 register u_long i
= ntohl(inaddr
);
162 register u_long mask
;
163 register struct interface
*ifp
;
167 } else if (IN_CLASSA(i
)) {
168 mask
= IN_CLASSA_NET
;
169 } else if (IN_CLASSB(i
)) {
170 mask
= IN_CLASSB_NET
;
172 mask
= IN_CLASSC_NET
;
175 * Check whether network is a subnet;
176 * if so, use the modified interpretation of `host'.
178 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
179 if ((ifp
->int_netmask
& i
) == ifp
->int_net
)
180 mask
= ifp
->int_subnetmask
;
181 return (htonl(mask
));
185 * Return RTF_HOST if the address is
186 * for an Internet host, RTF_SUBNET for a subnet,
190 struct sockaddr_in
*sin
;
192 register u_long i
= ntohl(sin
->sin_addr
.s_addr
);
193 register u_long net
, host
;
194 register struct interface
*ifp
;
197 net
= i
& IN_CLASSA_NET
;
198 host
= i
& IN_CLASSA_HOST
;
199 } else if (IN_CLASSB(i
)) {
200 net
= i
& IN_CLASSB_NET
;
201 host
= i
& IN_CLASSB_HOST
;
203 net
= i
& IN_CLASSC_NET
;
204 host
= i
& IN_CLASSC_HOST
;
208 * Check whether this network is subnetted;
209 * if so, check whether this is a subnet or a host.
211 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
212 if (net
== ifp
->int_net
) {
213 if (host
&~ ifp
->int_subnetmask
)
215 else if (ifp
->int_subnetmask
!= ifp
->int_netmask
)
218 return (0); /* network */
221 return (0); /* network */
227 * Return true if a route to subnet/host of route rt should be sent to dst.
228 * Send it only if dst is on the same logical network if not "internal",
229 * otherwise only if the route is the "internal" route for the logical net.
231 inet_sendroute(rt
, dst
)
233 struct sockaddr_in
*dst
;
236 ntohl(((struct sockaddr_in
*)&rt
->rt_dst
)->sin_addr
.s_addr
);
237 register u_long d
= ntohl(dst
->sin_addr
.s_addr
);
240 if ((r
& IN_CLASSA_NET
) == (d
& IN_CLASSA_NET
)) {
241 if ((r
& IN_CLASSA_HOST
) == 0)
242 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
245 if (r
& IN_CLASSA_HOST
)
247 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
248 } else if (IN_CLASSB(r
)) {
249 if ((r
& IN_CLASSB_NET
) == (d
& IN_CLASSB_NET
)) {
250 if ((r
& IN_CLASSB_HOST
) == 0)
251 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
254 if (r
& IN_CLASSB_HOST
)
256 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
258 if ((r
& IN_CLASSC_NET
) == (d
& IN_CLASSC_NET
)) {
259 if ((r
& IN_CLASSC_HOST
) == 0)
260 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
263 if (r
& IN_CLASSC_HOST
)
265 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);