]> git.saurik.com Git - apple/network_cmds.git/blame - routed.tproj/inet.c
network_cmds-176.tar.gz
[apple/network_cmds.git] / routed.tproj / inet.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ac2f15b3
A
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
b7080c8e
A
14 *
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,
ac2f15b3
A
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.
b7080c8e
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 *
57 * @(#)defs.h 8.1 (Berkeley) 6/5/93
58 */
59
60
61/*
62 * Temporarily, copy these routines from the kernel,
63 * as we need to know about subnets.
64 */
65#include "defs.h"
66
67extern struct interface *ifnet;
68
69/*
70 * Formulate an Internet address from network + host.
71 */
72__private_extern__
73struct in_addr
74inet_makeaddr(net, host)
75 u_long net, host;
76{
77 register struct interface *ifp;
78 register u_long mask;
79 u_long addr;
80
81 if (IN_CLASSA(net))
82 mask = IN_CLASSA_HOST;
83 else if (IN_CLASSB(net))
84 mask = IN_CLASSB_HOST;
85 else
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;
90 break;
91 }
92 addr = net | (host & mask);
93 addr = htonl(addr);
94 return (*(struct in_addr *)&addr);
95}
96
97/*
98 * Return the network number from an internet address.
99 */
100__private_extern__
101inet_netof(in)
102 struct in_addr in;
103{
104 register u_long i = ntohl(in.s_addr);
105 register u_long net;
106 register struct interface *ifp;
107
108 if (IN_CLASSA(i))
109 net = i & IN_CLASSA_NET;
110 else if (IN_CLASSB(i))
111 net = i & IN_CLASSB_NET;
112 else
113 net = i & IN_CLASSC_NET;
114
115 /*
116 * Check whether network is a subnet;
117 * if so, return subnet number.
118 */
119 for (ifp = ifnet; ifp; ifp = ifp->int_next)
120 if ((ifp->int_netmask & net) == ifp->int_net)
121 return (i & ifp->int_subnetmask);
122 return (net);
123}
124
125/*
126 * Return the host portion of an internet address.
127 */
128inet_lnaof(in)
129 struct in_addr in;
130{
131 register u_long i = ntohl(in.s_addr);
132 register u_long net, host;
133 register struct interface *ifp;
134
135 if (IN_CLASSA(i)) {
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;
141 } else {
142 net = i & IN_CLASSC_NET;
143 host = i & IN_CLASSC_HOST;
144 }
145
146 /*
147 * Check whether network is a subnet;
148 * if so, use the modified interpretation of `host'.
149 */
150 for (ifp = ifnet; ifp; ifp = ifp->int_next)
151 if ((ifp->int_netmask & net) == ifp->int_net)
152 return (host &~ ifp->int_subnetmask);
153 return (host);
154}
155
156/*
157 * Return the netmask pertaining to an internet address.
158 */
159inet_maskof(inaddr)
160 u_long inaddr;
161{
162 register u_long i = ntohl(inaddr);
163 register u_long mask;
164 register struct interface *ifp;
165
166 if (i == 0) {
167 mask = 0;
168 } else if (IN_CLASSA(i)) {
169 mask = IN_CLASSA_NET;
170 } else if (IN_CLASSB(i)) {
171 mask = IN_CLASSB_NET;
172 } else
173 mask = IN_CLASSC_NET;
174
175 /*
176 * Check whether network is a subnet;
177 * if so, use the modified interpretation of `host'.
178 */
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));
183}
184
185/*
186 * Return RTF_HOST if the address is
187 * for an Internet host, RTF_SUBNET for a subnet,
188 * 0 for a network.
189 */
190inet_rtflags(sin)
191 struct sockaddr_in *sin;
192{
193 register u_long i = ntohl(sin->sin_addr.s_addr);
194 register u_long net, host;
195 register struct interface *ifp;
196
197 if (IN_CLASSA(i)) {
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;
203 } else {
204 net = i & IN_CLASSC_NET;
205 host = i & IN_CLASSC_HOST;
206 }
207
208 /*
209 * Check whether this network is subnetted;
210 * if so, check whether this is a subnet or a host.
211 */
212 for (ifp = ifnet; ifp; ifp = ifp->int_next)
213 if (net == ifp->int_net) {
214 if (host &~ ifp->int_subnetmask)
215 return (RTF_HOST);
216 else if (ifp->int_subnetmask != ifp->int_netmask)
217 return (RTF_SUBNET);
218 else
219 return (0); /* network */
220 }
221 if (host == 0)
222 return (0); /* network */
223 else
224 return (RTF_HOST);
225}
226
227/*
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.
231 */
232inet_sendroute(rt, dst)
233 struct rt_entry *rt;
234 struct sockaddr_in *dst;
235{
236 register u_long r =
237 ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
238 register u_long d = ntohl(dst->sin_addr.s_addr);
239
240 if (IN_CLASSA(r)) {
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);
244 return (1);
245 }
246 if (r & IN_CLASSA_HOST)
247 return (0);
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);
253 return (1);
254 }
255 if (r & IN_CLASSB_HOST)
256 return (0);
257 return ((rt->rt_state & RTS_INTERNAL) != 0);
258 } else {
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);
262 return (1);
263 }
264 if (r & IN_CLASSC_HOST)
265 return (0);
266 return ((rt->rt_state & RTS_INTERNAL) != 0);
267 }
268}