BSD 4_3_Net_1 release
[unix-history] / routed / inet.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
0eb85d71
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b8c620d6
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
5ff67f98
DF
16 */
17
76a2200d 18#ifndef lint
e3419641 19static char sccsid[] = "@(#)inet.c 5.7 (Berkeley) 6/18/88";
0eb85d71 20#endif /* not lint */
5ff67f98 21
76a2200d
MK
22/*
23 * Temporarily, copy these routines from the kernel,
24 * as we need to know about subnets.
25 */
26#include "defs.h"
27
d002aa8c
MK
28extern struct interface *ifnet;
29
76a2200d 30/*
d002aa8c 31 * Formulate an Internet address from network + host.
76a2200d
MK
32 */
33struct in_addr
d002aa8c
MK
34inet_makeaddr(net, host)
35 u_long net, host;
76a2200d 36{
d002aa8c
MK
37 register struct interface *ifp;
38 register u_long mask;
76a2200d
MK
39 u_long addr;
40
d002aa8c
MK
41 if (IN_CLASSA(net))
42 mask = IN_CLASSA_HOST;
43 else if (IN_CLASSB(net))
44 mask = IN_CLASSB_HOST;
76a2200d 45 else
d002aa8c
MK
46 mask = IN_CLASSC_HOST;
47 for (ifp = ifnet; ifp; ifp = ifp->int_next)
48 if ((ifp->int_netmask & net) == ifp->int_net) {
49 mask = ~ifp->int_subnetmask;
50 break;
51 }
52 addr = net | (host & mask);
76a2200d
MK
53 addr = htonl(addr);
54 return (*(struct in_addr *)&addr);
55}
56
57/*
58 * Return the network number from an internet address.
59 */
60inet_netof(in)
61 struct in_addr in;
62{
63 register u_long i = ntohl(in.s_addr);
d002aa8c 64 register u_long net;
e1fe8748 65 register struct interface *ifp;
76a2200d 66
d002aa8c
MK
67 if (IN_CLASSA(i))
68 net = i & IN_CLASSA_NET;
69 else if (IN_CLASSB(i))
70 net = i & IN_CLASSB_NET;
71 else
72 net = i & IN_CLASSC_NET;
e1fe8748
MK
73
74 /*
d002aa8c 75 * Check whether network is a subnet;
e1fe8748
MK
76 * if so, return subnet number.
77 */
d002aa8c
MK
78 for (ifp = ifnet; ifp; ifp = ifp->int_next)
79 if ((ifp->int_netmask & net) == ifp->int_net)
80 return (i & ifp->int_subnetmask);
e1fe8748 81 return (net);
76a2200d
MK
82}
83
84/*
85 * Return the host portion of an internet address.
86 */
87inet_lnaof(in)
88 struct in_addr in;
89{
90 register u_long i = ntohl(in.s_addr);
d002aa8c 91 register u_long net, host;
e1fe8748 92 register struct interface *ifp;
76a2200d
MK
93
94 if (IN_CLASSA(i)) {
d002aa8c
MK
95 net = i & IN_CLASSA_NET;
96 host = i & IN_CLASSA_HOST;
76a2200d 97 } else if (IN_CLASSB(i)) {
d002aa8c
MK
98 net = i & IN_CLASSB_NET;
99 host = i & IN_CLASSB_HOST;
76a2200d 100 } else {
d002aa8c
MK
101 net = i & IN_CLASSC_NET;
102 host = i & IN_CLASSC_HOST;
76a2200d 103 }
76a2200d 104
e1fe8748 105 /*
d002aa8c 106 * Check whether network is a subnet;
e1fe8748
MK
107 * if so, use the modified interpretation of `host'.
108 */
d002aa8c
MK
109 for (ifp = ifnet; ifp; ifp = ifp->int_next)
110 if ((ifp->int_netmask & net) == ifp->int_net)
111 return (host &~ ifp->int_subnetmask);
e1fe8748 112 return (host);
76a2200d 113}
4fad5a6e
MK
114
115/*
116 * Return RTF_HOST if the address is
117 * for an Internet host, RTF_SUBNET for a subnet,
118 * 0 for a network.
119 */
120inet_rtflags(sin)
121 struct sockaddr_in *sin;
122{
123 register u_long i = ntohl(sin->sin_addr.s_addr);
124 register u_long net, host;
125 register struct interface *ifp;
126
127 if (IN_CLASSA(i)) {
128 net = i & IN_CLASSA_NET;
129 host = i & IN_CLASSA_HOST;
130 } else if (IN_CLASSB(i)) {
131 net = i & IN_CLASSB_NET;
132 host = i & IN_CLASSB_HOST;
133 } else {
134 net = i & IN_CLASSC_NET;
135 host = i & IN_CLASSC_HOST;
136 }
137
4fad5a6e
MK
138 /*
139 * Check whether this network is subnetted;
140 * if so, check whether this is a subnet or a host.
141 */
142 for (ifp = ifnet; ifp; ifp = ifp->int_next)
143 if (net == ifp->int_net) {
97cb3618
MK
144 if (host &~ ifp->int_subnetmask)
145 return (RTF_HOST);
146 else if (ifp->int_subnetmask != ifp->int_netmask)
4fad5a6e
MK
147 return (RTF_SUBNET);
148 else
97cb3618 149 return (0); /* network */
4fad5a6e 150 }
97cb3618
MK
151 if (host == 0)
152 return (0); /* network */
153 else
154 return (RTF_HOST);
4fad5a6e
MK
155}
156
157/*
7892134c
MK
158 * Return true if a route to subnet/host of route rt should be sent to dst.
159 * Send it only if dst is on the same logical network if not "internal",
160 * otherwise only if the route is the "internal" route for the logical net.
4fad5a6e 161 */
7892134c 162inet_sendroute(rt, dst)
09069ad0
MK
163 struct rt_entry *rt;
164 struct sockaddr_in *dst;
4fad5a6e 165{
09069ad0
MK
166 register u_long r =
167 ntohl(((struct sockaddr_in *)&rt->rt_dst)->sin_addr.s_addr);
4fad5a6e
MK
168 register u_long d = ntohl(dst->sin_addr.s_addr);
169
09069ad0
MK
170 if (IN_CLASSA(r)) {
171 if ((r & IN_CLASSA_NET) == (d & IN_CLASSA_NET)) {
172 if ((r & IN_CLASSA_HOST) == 0)
173 return ((rt->rt_state & RTS_INTERNAL) == 0);
4fad5a6e 174 return (1);
09069ad0 175 }
97cb3618
MK
176 if (r & IN_CLASSA_HOST)
177 return (0);
178 return ((rt->rt_state & RTS_INTERNAL) != 0);
09069ad0
MK
179 } else if (IN_CLASSB(r)) {
180 if ((r & IN_CLASSB_NET) == (d & IN_CLASSB_NET)) {
181 if ((r & IN_CLASSB_HOST) == 0)
182 return ((rt->rt_state & RTS_INTERNAL) == 0);
4fad5a6e 183 return (1);
09069ad0 184 }
97cb3618
MK
185 if (r & IN_CLASSB_HOST)
186 return (0);
187 return ((rt->rt_state & RTS_INTERNAL) != 0);
4fad5a6e 188 } else {
09069ad0
MK
189 if ((r & IN_CLASSC_NET) == (d & IN_CLASSC_NET)) {
190 if ((r & IN_CLASSC_HOST) == 0)
191 return ((rt->rt_state & RTS_INTERNAL) == 0);
4fad5a6e 192 return (1);
09069ad0 193 }
97cb3618
MK
194 if (r & IN_CLASSC_HOST)
195 return (0);
196 return ((rt->rt_state & RTS_INTERNAL) != 0);
4fad5a6e
MK
197 }
198}