xref: /csrg-svn/sys/netinet/in.c (revision 8397)
1 /*	in.c	4.4	82/10/09	*/
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 	hp->afh_nethash = in_netof(sin->sin_addr);
20 	hp->afh_hosthash = ntohl(sin->sin_addr.s_addr);
21 	if (hp->afh_hosthash < 0)
22 		hp->afh_hosthash = -hp->afh_hosthash;
23 }
24 
25 inet_netmatch(sin1, sin2)
26 	struct sockaddr_in *sin1, *sin2;
27 {
28 	return (sin1->sin_addr.s_net == sin2->sin_addr.s_net);
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 << 24) | host;
43 	else if (net < 65536)
44 		addr = (net << 16) | host;
45 	else
46 		addr = (net << 8) | host;
47 #ifdef vax
48 	addr = htonl(addr);
49 #endif
50 	return (*(struct in_addr *)&addr);
51 }
52 
53 /*
54  * Return the network number from an internet
55  * address; handles class a/b/c network #'s.
56  */
57 in_netof(in)
58 	struct in_addr in;
59 {
60 
61 	return (IN_NETOF(in));
62 }
63 
64 /*
65  * Return the local network address portion of an
66  * internet address; handles class a/b/c network
67  * number formats.
68  */
69 in_lnaof(in)
70 	struct in_addr in;
71 {
72 
73 	return (IN_LNAOF(in));
74 }
75 
76 /*
77  * Initialize an interface's routing
78  * table entry according to the network.
79  * INTERNET SPECIFIC.
80  */
81 if_rtinit(ifp, flags)
82 	register struct ifnet *ifp;
83 	int flags;
84 {
85 	struct sockaddr_in sin;
86 
87 	if (ifp->if_flags & IFF_ROUTE)
88 		return;
89 	bzero((caddr_t)&sin, sizeof (sin));
90 	sin.sin_family = AF_INET;
91 	sin.sin_addr = if_makeaddr(ifp->if_net, 0);
92 	rtinit(&sin, &ifp->if_addr, flags);
93 }
94 #endif
95