]>
git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/inet.c
6b031aa99a850c4c20ce10b0234390329d7b8a91
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.
99 __private_extern__
int
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.
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.
163 register u_long i
= ntohl(inaddr
);
164 register u_long mask
;
165 register struct interface
*ifp
;
169 } else if (IN_CLASSA(i
)) {
170 mask
= IN_CLASSA_NET
;
171 } else if (IN_CLASSB(i
)) {
172 mask
= IN_CLASSB_NET
;
174 mask
= IN_CLASSC_NET
;
177 * Check whether network is a subnet;
178 * if so, use the modified interpretation of `host'.
180 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
181 if ((ifp
->int_netmask
& i
) == ifp
->int_net
)
182 mask
= ifp
->int_subnetmask
;
183 return ((int)htonl(mask
));
187 * Return RTF_HOST if the address is
188 * for an Internet host, RTF_SUBNET for a subnet,
193 struct sockaddr_in
*sin
;
195 register u_long i
= ntohl(sin
->sin_addr
.s_addr
);
196 register u_long net
, host
;
197 register struct interface
*ifp
;
200 net
= i
& IN_CLASSA_NET
;
201 host
= i
& IN_CLASSA_HOST
;
202 } else if (IN_CLASSB(i
)) {
203 net
= i
& IN_CLASSB_NET
;
204 host
= i
& IN_CLASSB_HOST
;
206 net
= i
& IN_CLASSC_NET
;
207 host
= i
& IN_CLASSC_HOST
;
211 * Check whether this network is subnetted;
212 * if so, check whether this is a subnet or a host.
214 for (ifp
= ifnet
; ifp
; ifp
= ifp
->int_next
)
215 if (net
== ifp
->int_net
) {
216 if (host
&~ ifp
->int_subnetmask
)
218 else if (ifp
->int_subnetmask
!= ifp
->int_netmask
)
221 return (0); /* network */
224 return (0); /* network */
230 * Return true if a route to subnet/host of route rt should be sent to dst.
231 * Send it only if dst is on the same logical network if not "internal",
232 * otherwise only if the route is the "internal" route for the logical net.
235 inet_sendroute(rt
, dst
)
237 struct sockaddr_in
*dst
;
240 ntohl(((struct sockaddr_in
*)&rt
->rt_dst
)->sin_addr
.s_addr
);
241 register u_long d
= ntohl(dst
->sin_addr
.s_addr
);
244 if ((r
& IN_CLASSA_NET
) == (d
& IN_CLASSA_NET
)) {
245 if ((r
& IN_CLASSA_HOST
) == 0)
246 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
249 if (r
& IN_CLASSA_HOST
)
251 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
252 } else if (IN_CLASSB(r
)) {
253 if ((r
& IN_CLASSB_NET
) == (d
& IN_CLASSB_NET
)) {
254 if ((r
& IN_CLASSB_HOST
) == 0)
255 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
258 if (r
& IN_CLASSB_HOST
)
260 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);
262 if ((r
& IN_CLASSC_NET
) == (d
& IN_CLASSC_NET
)) {
263 if ((r
& IN_CLASSC_HOST
) == 0)
264 return ((rt
->rt_state
& RTS_INTERNAL
) == 0);
267 if (r
& IN_CLASSC_HOST
)
269 return ((rt
->rt_state
& RTS_INTERNAL
) != 0);