xref: /original-bsd/sbin/routed/inet.c (revision a910c8b7)
1 #ifndef lint
2 static char *sccsid[] = "@(#)inet.c	4.2 (Berkeley) 05/01/84";
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 
10 /*
11  * Formulate an Internet address from network + host.  Used in
12  * building addresses stored in the ifnet structure.
13  */
14 struct in_addr
15 if_makeaddr(net, host)
16 	int net, host;
17 {
18 	u_long addr;
19 
20 	if (net < IN_CLASSA_MAX)
21 		addr = (net << IN_CLASSA_NSHIFT) | host;
22 	else if (net < IN_CLASSB_MAX)
23 		addr = (net << IN_CLASSB_NSHIFT) | host;
24 	else
25 		addr = (net << IN_CLASSC_NSHIFT) | host;
26 	addr = htonl(addr);
27 	return (*(struct in_addr *)&addr);
28 }
29 
30 /*
31  * Return the network number from an internet address.
32  */
33 inet_netof(in)
34 	struct in_addr in;
35 {
36 	register u_long i = ntohl(in.s_addr);
37 	register u_long net, subnet;
38 	register struct interface *ifp;
39 	extern struct interface *ifnet;
40 
41 	if (IN_CLASSA(i)) {
42 		net = (i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT;
43 		if (IN_SUBNETA(i)) {
44 			subnet = (i & IN_CLASSA_SUBNET) >> IN_CLASSA_SUBNSHIFT;
45 			/* Fall through and check whether a subnet */
46 		} else
47 			return (net);
48 	} else if (IN_CLASSB(i)) {
49 		net = (i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT;
50 		if (IN_SUBNETB(i)) {
51 			subnet = (i & IN_CLASSB_SUBNET) >> IN_CLASSB_SUBNSHIFT;
52 			/* Fall through and check whether a subnet */
53 		} else
54 			return (net);
55 	} else {
56 		return ((i & IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
57 	}
58 
59 	/*
60 	 * Check whether network is a subnet of a `local' network;
61 	 * if so, return subnet number.
62 	 */
63 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
64 		if (ifp->int_flags & IFF_LOCAL) {
65 			if (ifp->int_net == net)
66 				return (subnet);
67 			if ((ifp->int_net >> SUBNETSHIFT) == net)
68 				return (subnet);
69 		}
70 	}
71 	return (net);
72 }
73 
74 /*
75  * Return the host portion of an internet address.
76  */
77 inet_lnaof(in)
78 	struct in_addr in;
79 {
80 	register u_long i = ntohl(in.s_addr);
81 	register u_long net, host, subhost;
82 	register struct interface *ifp;
83 
84 	if (IN_CLASSA(i)) {
85 		if (IN_SUBNETA(i)) {
86 			net = (i & IN_CLASSA_NET) >> IN_CLASSA_NSHIFT;
87 			host = i & IN_CLASSA_HOST;
88 			subhost = i & IN_CLASSA_SUBHOST;
89 			/* Fall through and check whether a subnet */
90 		} else
91 			return (i & IN_CLASSA_HOST);
92 	} else if (IN_CLASSB(i)) {
93 		if (IN_SUBNETB(i)) {
94 			net = (i & IN_CLASSB_NET) >> IN_CLASSB_NSHIFT;
95 			host = i & IN_CLASSB_HOST;
96 			subhost = i & IN_CLASSB_SUBHOST;
97 			/* Fall through and check whether a subnet */
98 		} else
99 			return (i & IN_CLASSB_HOST);
100 	} else {
101 		return (i & IN_CLASSC_HOST);
102 	}
103 
104 	/*
105 	 * Check whether network is a subnet of a `local' network;
106 	 * if so, use the modified interpretation of `host'.
107 	 */
108 	for (ifp = ifnet; ifp; ifp = ifp->int_next) {
109 		if (ifp->int_flags & IFF_LOCAL) {
110 			if (ifp->int_net == net)
111 				return (subhost);
112 			if ((ifp->int_net >> SUBNETSHIFT) == net)
113 				return (subhost);
114 		}
115 	}
116 	return (host);
117 }
118