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