4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / sbin / routed / inet.c
CommitLineData
5ff67f98 1/*
b2d6cd51
KB
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
0eb85d71 4 *
d60d530a 5 * %sccs.include.redist.c%
5ff67f98
DF
6 */
7
76a2200d 8#ifndef lint
b2d6cd51 9static char sccsid[] = "@(#)inet.c 8.1 (Berkeley) %G%";
0eb85d71 10#endif /* not lint */
5ff67f98 11
76a2200d
MK
12/*
13 * Temporarily, copy these routines from the kernel,
14 * as we need to know about subnets.
15 */
16#include "defs.h"
17
d002aa8c
MK
18extern struct interface *ifnet;
19
76a2200d 20/*
d002aa8c 21 * Formulate an Internet address from network + host.
76a2200d
MK
22 */
23struct in_addr
d002aa8c
MK
24inet_makeaddr(net, host)
25 u_long net, host;
76a2200d 26{
d002aa8c
MK
27 register struct interface *ifp;
28 register u_long mask;
76a2200d
MK
29 u_long addr;
30
d002aa8c
MK
31 if (IN_CLASSA(net))
32 mask = IN_CLASSA_HOST;
33 else if (IN_CLASSB(net))
34 mask = IN_CLASSB_HOST;
76a2200d 35 else
d002aa8c
MK
36 mask = IN_CLASSC_HOST;
37 for (ifp = ifnet; ifp; ifp = ifp->int_next)
38 if ((ifp->int_netmask & net) == ifp->int_net) {
39 mask = ~ifp->int_subnetmask;
40 break;
41 }
42 addr = net | (host & mask);
76a2200d
MK
43 addr = htonl(addr);
44 return (*(struct in_addr *)&addr);
45}
46
47/*
48 * Return the network number from an internet address.
49 */
50inet_netof(in)
51 struct in_addr in;
52{
53 register u_long i = ntohl(in.s_addr);
d002aa8c 54 register u_long net;
e1fe8748 55 register struct interface *ifp;
76a2200d 56
d002aa8c
MK
57 if (IN_CLASSA(i))
58 net = i & IN_CLASSA_NET;
59 else if (IN_CLASSB(i))
60 net = i & IN_CLASSB_NET;
61 else
62 net = i & IN_CLASSC_NET;
e1fe8748
MK
63
64 /*
d002aa8c 65 * Check whether network is a subnet;
e1fe8748
MK
66 * if so, return subnet number.
67 */
d002aa8c
MK
68 for (ifp = ifnet; ifp; ifp = ifp->int_next)
69 if ((ifp->int_netmask & net) == ifp->int_net)
70 return (i & ifp->int_subnetmask);
e1fe8748 71 return (net);
76a2200d
MK
72}
73
74/*
75 * Return the host portion of an internet address.
76 */
77inet_lnaof(in)
78 struct in_addr in;
79{
80 register u_long i = ntohl(in.s_addr);
d002aa8c 81 register u_long net, host;
e1fe8748 82 register struct interface *ifp;
76a2200d
MK
83
84 if (IN_CLASSA(i)) {
d002aa8c
MK
85 net = i & IN_CLASSA_NET;
86 host = i & IN_CLASSA_HOST;
76a2200d 87 } else if (IN_CLASSB(i)) {
d002aa8c
MK
88 net = i & IN_CLASSB_NET;
89 host = i & IN_CLASSB_HOST;
76a2200d 90 } else {
d002aa8c
MK
91 net = i & IN_CLASSC_NET;
92 host = i & IN_CLASSC_HOST;
76a2200d 93 }
76a2200d 94
e1fe8748 95 /*
d002aa8c 96 * Check whether network is a subnet;
e1fe8748
MK
97 * if so, use the modified interpretation of `host'.
98 */
d002aa8c
MK
99 for (ifp = ifnet; ifp; ifp = ifp->int_next)
100 if ((ifp->int_netmask & net) == ifp->int_net)
101 return (host &~ ifp->int_subnetmask);
e1fe8748 102 return (host);
76a2200d 103}
4fad5a6e 104
ad76aa05
KS
105/*
106 * Return the netmask pertaining to an internet address.
107 */
108inet_maskof(in)
109 struct in_addr in;
110{
111 register u_long i = ntohl(in.s_addr);
112 register u_long mask;
113 register struct interface *ifp;
114
f9a25ff6
KS
115 if (i == 0) {
116 mask = 0;
117 } else if (IN_CLASSA(i)) {
ad76aa05
KS
118 mask = IN_CLASSA_NET;
119 } else if (IN_CLASSB(i)) {
120 mask = i & IN_CLASSB_NET;
121 } else
122 mask = i & IN_CLASSC_NET;
123
124 /*
125 * Check whether network is a subnet;
126 * if so, use the modified interpretation of `host'.
127 */
128 for (ifp = ifnet; ifp; ifp = ifp->int_next)
129 if ((ifp->int_netmask & i) == ifp->int_net)
130 mask = ifp->int_subnetmask;
131 return (htonl(mask));
132}
133
4fad5a6e
MK
134/*
135 * Return RTF_HOST if the address is
136 * for an Internet host, RTF_SUBNET for a subnet,
137 * 0 for a network.
138 */
139inet_rtflags(sin)
140 struct sockaddr_in *sin;
141{
142 register u_long i = ntohl(sin->sin_addr.s_addr);
143 register u_long net, host;
144 register struct interface *ifp;
145
146 if (IN_CLASSA(i)) {
147 net = i & IN_CLASSA_NET;
148 host = i & IN_CLASSA_HOST;
149 } else if (IN_CLASSB(i)) {
150 net = i & IN_CLASSB_NET;
151 host = i & IN_CLASSB_HOST;
152 } else {
153 net = i & IN_CLASSC_NET;
154 host = i & IN_CLASSC_HOST;
155 }
156
4fad5a6e
MK
157 /*
158 * Check whether this network is subnetted;
159 * if so, check whether this is a subnet or a host.
160 */
161 for (ifp = ifnet; ifp; ifp = ifp->int_next)
162 if (net == ifp->int_net) {
97cb3618
MK
163 if (host &~ ifp->int_subnetmask)
164 return (RTF_HOST);
165 else if (ifp->int_subnetmask != ifp->int_netmask)
4fad5a6e
MK
166 return (RTF_SUBNET);
167 else
97cb3618 168 return (0); /* network */
4fad5a6e 169 }
97cb3618
MK
170 if (host == 0)
171 return (0); /* network */
172 else
173 return (RTF_HOST);
4fad5a6e
MK
174}
175
176/*
7892134c
MK
177 * Return true if a route to subnet/host of route rt should be sent to dst.
178 * Send it only if dst is on the same logical network if not "internal",
179 * otherwise only if the route is the "internal" route for the logical net.
4fad5a6e 180 */
7892134c 181inet_sendroute(rt, dst)
09069ad0
MK
182 struct rt_entry *rt;
183 struct sockaddr_in *dst;
4fad5a6e 184{
09069ad0
MK
185 register u_long r =
186 ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
4fad5a6e
MK
187 register u_long d = ntohl(dst->sin_addr.s_addr);
188
09069ad0
MK
189 if (IN_CLASSA(r)) {
190 if ((r & IN_CLASSA_NET) == (d & IN_CLASSA_NET)) {
191 if ((r & IN_CLASSA_HOST) == 0)
192 return ((rt->rt_state & RTS_INTERNAL) == 0);
4fad5a6e 193 return (1);
09069ad0 194 }
97cb3618
MK
195 if (r & IN_CLASSA_HOST)
196 return (0);
197 return ((rt->rt_state & RTS_INTERNAL) != 0);
09069ad0
MK
198 } else if (IN_CLASSB(r)) {
199 if ((r & IN_CLASSB_NET) == (d & IN_CLASSB_NET)) {
200 if ((r & IN_CLASSB_HOST) == 0)
201 return ((rt->rt_state & RTS_INTERNAL) == 0);
4fad5a6e 202 return (1);
09069ad0 203 }
97cb3618
MK
204 if (r & IN_CLASSB_HOST)
205 return (0);
206 return ((rt->rt_state & RTS_INTERNAL) != 0);
4fad5a6e 207 } else {
09069ad0
MK
208 if ((r & IN_CLASSC_NET) == (d & IN_CLASSC_NET)) {
209 if ((r & IN_CLASSC_HOST) == 0)
210 return ((rt->rt_state & RTS_INTERNAL) == 0);
4fad5a6e 211 return (1);
09069ad0 212 }
97cb3618
MK
213 if (r & IN_CLASSC_HOST)
214 return (0);
215 return ((rt->rt_state & RTS_INTERNAL) != 0);
4fad5a6e
MK
216 }
217}