]> git.saurik.com Git - apple/network_cmds.git/blame - routed.tproj/inet.c
network_cmds-307.1.1.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 *
a2c93a76
A
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
12 * this file.
b7080c8e
A
13 *
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,
a2c93a76
A
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
b7080c8e
A
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
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.
43 *
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
54 * SUCH DAMAGE.
55 *
56 * @(#)defs.h 8.1 (Berkeley) 6/5/93
57 */
58
59
60/*
61 * Temporarily, copy these routines from the kernel,
62 * as we need to know about subnets.
63 */
64#include "defs.h"
65
66extern struct interface *ifnet;
67
68/*
69 * Formulate an Internet address from network + host.
70 */
71__private_extern__
72struct in_addr
73inet_makeaddr(net, host)
74 u_long net, host;
75{
76 register struct interface *ifp;
77 register u_long mask;
78 u_long addr;
79
80 if (IN_CLASSA(net))
81 mask = IN_CLASSA_HOST;
82 else if (IN_CLASSB(net))
83 mask = IN_CLASSB_HOST;
84 else
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;
89 break;
90 }
91 addr = net | (host & mask);
92 addr = htonl(addr);
93 return (*(struct in_addr *)&addr);
94}
95
96/*
97 * Return the network number from an internet address.
98 */
b8dff150 99__private_extern__ int
b7080c8e
A
100inet_netof(in)
101 struct in_addr in;
102{
103 register u_long i = ntohl(in.s_addr);
104 register u_long net;
105 register struct interface *ifp;
106
107 if (IN_CLASSA(i))
108 net = i & IN_CLASSA_NET;
109 else if (IN_CLASSB(i))
110 net = i & IN_CLASSB_NET;
111 else
112 net = i & IN_CLASSC_NET;
113
114 /*
115 * Check whether network is a subnet;
116 * if so, return subnet number.
117 */
118 for (ifp = ifnet; ifp; ifp = ifp->int_next)
119 if ((ifp->int_netmask & net) == ifp->int_net)
120 return (i & ifp->int_subnetmask);
121 return (net);
122}
123
124/*
125 * Return the host portion of an internet address.
126 */
b8dff150 127int
b7080c8e
A
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);
b8dff150 153 return ((int)host);
b7080c8e
A
154}
155
156/*
157 * Return the netmask pertaining to an internet address.
158 */
b8dff150 159int
b7080c8e
A
160inet_maskof(inaddr)
161 u_long inaddr;
162{
163 register u_long i = ntohl(inaddr);
164 register u_long mask;
165 register struct interface *ifp;
166
167 if (i == 0) {
168 mask = 0;
169 } else if (IN_CLASSA(i)) {
170 mask = IN_CLASSA_NET;
171 } else if (IN_CLASSB(i)) {
172 mask = IN_CLASSB_NET;
173 } else
174 mask = IN_CLASSC_NET;
175
176 /*
177 * Check whether network is a subnet;
178 * if so, use the modified interpretation of `host'.
179 */
180 for (ifp = ifnet; ifp; ifp = ifp->int_next)
181 if ((ifp->int_netmask & i) == ifp->int_net)
182 mask = ifp->int_subnetmask;
b8dff150 183 return ((int)htonl(mask));
b7080c8e
A
184}
185
186/*
187 * Return RTF_HOST if the address is
188 * for an Internet host, RTF_SUBNET for a subnet,
189 * 0 for a network.
190 */
b8dff150 191int
b7080c8e
A
192inet_rtflags(sin)
193 struct sockaddr_in *sin;
194{
195 register u_long i = ntohl(sin->sin_addr.s_addr);
196 register u_long net, host;
197 register struct interface *ifp;
198
199 if (IN_CLASSA(i)) {
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;
205 } else {
206 net = i & IN_CLASSC_NET;
207 host = i & IN_CLASSC_HOST;
208 }
209
210 /*
211 * Check whether this network is subnetted;
212 * if so, check whether this is a subnet or a host.
213 */
214 for (ifp = ifnet; ifp; ifp = ifp->int_next)
215 if (net == ifp->int_net) {
216 if (host &~ ifp->int_subnetmask)
217 return (RTF_HOST);
218 else if (ifp->int_subnetmask != ifp->int_netmask)
219 return (RTF_SUBNET);
220 else
221 return (0); /* network */
222 }
223 if (host == 0)
224 return (0); /* network */
225 else
226 return (RTF_HOST);
227}
228
229/*
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.
233 */
b8dff150 234int
b7080c8e
A
235inet_sendroute(rt, dst)
236 struct rt_entry *rt;
237 struct sockaddr_in *dst;
238{
239 register u_long r =
240 ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
241 register u_long d = ntohl(dst->sin_addr.s_addr);
242
243 if (IN_CLASSA(r)) {
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);
247 return (1);
248 }
249 if (r & IN_CLASSA_HOST)
250 return (0);
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);
256 return (1);
257 }
258 if (r & IN_CLASSB_HOST)
259 return (0);
260 return ((rt->rt_state & RTS_INTERNAL) != 0);
261 } else {
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);
265 return (1);
266 }
267 if (r & IN_CLASSC_HOST)
268 return (0);
269 return ((rt->rt_state & RTS_INTERNAL) != 0);
270 }
271}