xref: /original-bsd/sys/netinet/in.c (revision 3c32e3a3)
1 /*	in.c	4.12	83/03/14	*/
2 
3 #include "../h/param.h"
4 #include "../h/mbuf.h"
5 #include "../h/protosw.h"
6 #include "../h/socket.h"
7 #include "../h/socketvar.h"
8 #include "../netinet/in.h"
9 #include "../netinet/in_systm.h"
10 #include "../net/if.h"
11 #include "../net/route.h"
12 #include "../net/af.h"
13 
14 #ifdef INET
15 inet_hash(sin, hp)
16 	register struct sockaddr_in *sin;
17 	struct afhash *hp;
18 {
19 
20 	hp->afh_nethash = in_netof(sin->sin_addr);
21 	hp->afh_hosthash = ntohl(sin->sin_addr.s_addr);
22 }
23 
24 inet_netmatch(sin1, sin2)
25 	struct sockaddr_in *sin1, *sin2;
26 {
27 
28 	return (in_netof(sin1->sin_addr) == in_netof(sin2->sin_addr));
29 }
30 
31 /*
32  * Formulate an Internet address from network + host.  Used in
33  * building addresses stored in the ifnet structure.
34  */
35 struct in_addr
36 if_makeaddr(net, host)
37 	int net, host;
38 {
39 	u_long addr;
40 
41 	if (net < 128)
42 		addr = (net << IN_CLASSA_NSHIFT) | host;
43 	else if (net < 65536)
44 		addr = (net << IN_CLASSB_NSHIFT) | host;
45 	else
46 		addr = (net << IN_CLASSC_NSHIFT) | host;
47 	addr = htonl(addr);
48 	return (*(struct in_addr *)&addr);
49 }
50 
51 /*
52  * Return the network number from an internet address.
53  */
54 in_netof(in)
55 	struct in_addr in;
56 {
57 	register u_long i = ntohl(in.s_addr);
58 
59 	if (IN_CLASSA(i))
60 		return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
61 	else if (IN_CLASSB(i))
62 		return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
63 	else
64 		return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
65 }
66 
67 /*
68  * Return the host portion of an internet address.
69  */
70 in_lnaof(in)
71 	struct in_addr in;
72 {
73 	register u_long i = ntohl(in.s_addr);
74 
75 	if (IN_CLASSA(i))
76 		return ((i)&IN_CLASSA_HOST);
77 	else if (IN_CLASSB(i))
78 		return ((i)&IN_CLASSB_HOST);
79 	else
80 		return ((i)&IN_CLASSC_HOST);
81 }
82 
83 /*
84  * Initialize an interface's routing
85  * table entry according to the network.
86  * INTERNET SPECIFIC.
87  */
88 if_rtinit(ifp, flags)
89 	register struct ifnet *ifp;
90 	int flags;
91 {
92 	struct sockaddr_in sin;
93 
94 	if (ifp->if_flags & IFF_ROUTE)
95 		return;
96 	bzero((caddr_t)&sin, sizeof (sin));
97 	sin.sin_family = AF_INET;
98 	sin.sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
99 	rtinit((struct sockaddr *)&sin, &ifp->if_addr, flags);
100 }
101 #endif
102