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