new subnets
[unix-history] / usr / src / sbin / routed / inet.c
CommitLineData
76a2200d 1#ifndef lint
d002aa8c 2static char *sccsid[] = "@(#)inet.c 4.3 (Berkeley) %G%";
76a2200d
MK
3#endif
4/*
5 * Temporarily, copy these routines from the kernel,
6 * as we need to know about subnets.
7 */
8#include "defs.h"
9
d002aa8c
MK
10extern struct interface *ifnet;
11
76a2200d 12/*
d002aa8c 13 * Formulate an Internet address from network + host.
76a2200d
MK
14 */
15struct in_addr
d002aa8c
MK
16inet_makeaddr(net, host)
17 u_long net, host;
76a2200d 18{
d002aa8c
MK
19 register struct interface *ifp;
20 register u_long mask;
76a2200d
MK
21 u_long addr;
22
d002aa8c
MK
23 if (IN_CLASSA(net))
24 mask = IN_CLASSA_HOST;
25 else if (IN_CLASSB(net))
26 mask = IN_CLASSB_HOST;
76a2200d 27 else
d002aa8c
MK
28 mask = IN_CLASSC_HOST;
29 for (ifp = ifnet; ifp; ifp = ifp->int_next)
30 if ((ifp->int_netmask & net) == ifp->int_net) {
31 mask = ~ifp->int_subnetmask;
32 break;
33 }
34 addr = net | (host & mask);
76a2200d
MK
35 addr = htonl(addr);
36 return (*(struct in_addr *)&addr);
37}
38
39/*
40 * Return the network number from an internet address.
41 */
42inet_netof(in)
43 struct in_addr in;
44{
45 register u_long i = ntohl(in.s_addr);
d002aa8c 46 register u_long net;
e1fe8748 47 register struct interface *ifp;
76a2200d 48
d002aa8c
MK
49 if (IN_CLASSA(i))
50 net = i & IN_CLASSA_NET;
51 else if (IN_CLASSB(i))
52 net = i & IN_CLASSB_NET;
53 else
54 net = i & IN_CLASSC_NET;
e1fe8748
MK
55
56 /*
d002aa8c 57 * Check whether network is a subnet;
e1fe8748
MK
58 * if so, return subnet number.
59 */
d002aa8c
MK
60 for (ifp = ifnet; ifp; ifp = ifp->int_next)
61 if ((ifp->int_netmask & net) == ifp->int_net)
62 return (i & ifp->int_subnetmask);
e1fe8748 63 return (net);
76a2200d
MK
64}
65
66/*
67 * Return the host portion of an internet address.
68 */
69inet_lnaof(in)
70 struct in_addr in;
71{
72 register u_long i = ntohl(in.s_addr);
d002aa8c 73 register u_long net, host;
e1fe8748 74 register struct interface *ifp;
76a2200d
MK
75
76 if (IN_CLASSA(i)) {
d002aa8c
MK
77 net = i & IN_CLASSA_NET;
78 host = i & IN_CLASSA_HOST;
76a2200d 79 } else if (IN_CLASSB(i)) {
d002aa8c
MK
80 net = i & IN_CLASSB_NET;
81 host = i & IN_CLASSB_HOST;
76a2200d 82 } else {
d002aa8c
MK
83 net = i & IN_CLASSC_NET;
84 host = i & IN_CLASSC_HOST;
76a2200d 85 }
76a2200d 86
e1fe8748 87 /*
d002aa8c 88 * Check whether network is a subnet;
e1fe8748
MK
89 * if so, use the modified interpretation of `host'.
90 */
d002aa8c
MK
91 for (ifp = ifnet; ifp; ifp = ifp->int_next)
92 if ((ifp->int_netmask & net) == ifp->int_net)
93 return (host &~ ifp->int_subnetmask);
e1fe8748 94 return (host);
76a2200d 95}