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