]> git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/inet.c
network_cmds-176.3.1.tar.gz
[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 * "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.
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,
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."
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
66 extern struct interface *ifnet;
67
68 /*
69 * Formulate an Internet address from network + host.
70 */
71 __private_extern__
72 struct in_addr
73 inet_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 */
99 __private_extern__
100 inet_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 */
127 inet_lnaof(in)
128 struct in_addr in;
129 {
130 register u_long i = ntohl(in.s_addr);
131 register u_long net, host;
132 register struct interface *ifp;
133
134 if (IN_CLASSA(i)) {
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;
140 } else {
141 net = i & IN_CLASSC_NET;
142 host = i & IN_CLASSC_HOST;
143 }
144
145 /*
146 * Check whether network is a subnet;
147 * if so, use the modified interpretation of `host'.
148 */
149 for (ifp = ifnet; ifp; ifp = ifp->int_next)
150 if ((ifp->int_netmask & net) == ifp->int_net)
151 return (host &~ ifp->int_subnetmask);
152 return (host);
153 }
154
155 /*
156 * Return the netmask pertaining to an internet address.
157 */
158 inet_maskof(inaddr)
159 u_long inaddr;
160 {
161 register u_long i = ntohl(inaddr);
162 register u_long mask;
163 register struct interface *ifp;
164
165 if (i == 0) {
166 mask = 0;
167 } else if (IN_CLASSA(i)) {
168 mask = IN_CLASSA_NET;
169 } else if (IN_CLASSB(i)) {
170 mask = IN_CLASSB_NET;
171 } else
172 mask = IN_CLASSC_NET;
173
174 /*
175 * Check whether network is a subnet;
176 * if so, use the modified interpretation of `host'.
177 */
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));
182 }
183
184 /*
185 * Return RTF_HOST if the address is
186 * for an Internet host, RTF_SUBNET for a subnet,
187 * 0 for a network.
188 */
189 inet_rtflags(sin)
190 struct sockaddr_in *sin;
191 {
192 register u_long i = ntohl(sin->sin_addr.s_addr);
193 register u_long net, host;
194 register struct interface *ifp;
195
196 if (IN_CLASSA(i)) {
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;
202 } else {
203 net = i & IN_CLASSC_NET;
204 host = i & IN_CLASSC_HOST;
205 }
206
207 /*
208 * Check whether this network is subnetted;
209 * if so, check whether this is a subnet or a host.
210 */
211 for (ifp = ifnet; ifp; ifp = ifp->int_next)
212 if (net == ifp->int_net) {
213 if (host &~ ifp->int_subnetmask)
214 return (RTF_HOST);
215 else if (ifp->int_subnetmask != ifp->int_netmask)
216 return (RTF_SUBNET);
217 else
218 return (0); /* network */
219 }
220 if (host == 0)
221 return (0); /* network */
222 else
223 return (RTF_HOST);
224 }
225
226 /*
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.
230 */
231 inet_sendroute(rt, dst)
232 struct rt_entry *rt;
233 struct sockaddr_in *dst;
234 {
235 register u_long r =
236 ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
237 register u_long d = ntohl(dst->sin_addr.s_addr);
238
239 if (IN_CLASSA(r)) {
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);
243 return (1);
244 }
245 if (r & IN_CLASSA_HOST)
246 return (0);
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);
252 return (1);
253 }
254 if (r & IN_CLASSB_HOST)
255 return (0);
256 return ((rt->rt_state & RTS_INTERNAL) != 0);
257 } else {
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);
261 return (1);
262 }
263 if (r & IN_CLASSC_HOST)
264 return (0);
265 return ((rt->rt_state & RTS_INTERNAL) != 0);
266 }
267 }