Commit | Line | Data |
---|---|---|
af8f6a21 | 1 | /* in.c 4.11 82/11/13 */ |
8473bbab SL |
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" | |
fcfe450e BJ |
8 | #include "../netinet/in.h" |
9 | #include "../netinet/in_systm.h" | |
6187f8f4 SL |
10 | #include "../net/if.h" |
11 | #include "../net/route.h" | |
8473bbab SL |
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 | { | |
a8d3bf7f | 19 | |
8473bbab | 20 | hp->afh_nethash = in_netof(sin->sin_addr); |
2c48b3f8 | 21 | hp->afh_hosthash = ntohl(sin->sin_addr.s_addr); |
8473bbab SL |
22 | } |
23 | ||
24 | inet_netmatch(sin1, sin2) | |
25 | struct sockaddr_in *sin1, *sin2; | |
26 | { | |
a8d3bf7f | 27 | |
8473bbab SL |
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 | ||
8473bbab | 41 | if (net < 128) |
6e7edb25 | 42 | addr = (net << IN_CLASSA_NSHIFT) | host; |
8473bbab | 43 | else if (net < 65536) |
6e7edb25 | 44 | addr = (net << IN_CLASSB_NSHIFT) | host; |
8473bbab | 45 | else |
6e7edb25 | 46 | addr = (net << IN_CLASSC_NSHIFT) | host; |
8473bbab | 47 | addr = htonl(addr); |
8473bbab SL |
48 | return (*(struct in_addr *)&addr); |
49 | } | |
50 | ||
51 | /* | |
a8d3bf7f | 52 | * Return the network number from an internet address. |
8473bbab SL |
53 | */ |
54 | in_netof(in) | |
55 | struct in_addr in; | |
56 | { | |
2c48b3f8 | 57 | register u_long i = ntohl(in.s_addr); |
8473bbab | 58 | |
a8d3bf7f | 59 | if (IN_CLASSA(i)) |
6e7edb25 | 60 | return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); |
a8d3bf7f | 61 | else if (IN_CLASSB(i)) |
6e7edb25 | 62 | return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); |
a8d3bf7f | 63 | else |
6e7edb25 | 64 | return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); |
8473bbab SL |
65 | } |
66 | ||
67 | /* | |
a8d3bf7f | 68 | * Return the host portion of an internet address. |
8473bbab SL |
69 | */ |
70 | in_lnaof(in) | |
71 | struct in_addr in; | |
72 | { | |
2c48b3f8 | 73 | register u_long i = ntohl(in.s_addr); |
8473bbab | 74 | |
a8d3bf7f | 75 | if (IN_CLASSA(i)) |
6e7edb25 | 76 | return ((i)&IN_CLASSA_HOST); |
a8d3bf7f | 77 | else if (IN_CLASSB(i)) |
6e7edb25 | 78 | return ((i)&IN_CLASSB_HOST); |
a8d3bf7f | 79 | else |
6e7edb25 | 80 | return ((i)&IN_CLASSC_HOST); |
8473bbab SL |
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 | ||
8473bbab SL |
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, 0); | |
a1edc12b | 99 | rtinit((struct sockaddr *)&sin, &ifp->if_addr, flags); |
8473bbab SL |
100 | } |
101 | #endif |