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