Commit | Line | Data |
---|---|---|
13bd785c WJ |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | static char sccsid[] = "@(#)af.c 5.11 (Berkeley) 2/28/91"; | |
36 | #endif /* not lint */ | |
37 | ||
38 | #include "defs.h" | |
39 | ||
40 | /* | |
41 | * Address family support routines | |
42 | */ | |
43 | int inet_hash(), inet_netmatch(), inet_output(), | |
44 | inet_portmatch(), inet_portcheck(), | |
45 | inet_checkhost(), inet_rtflags(), inet_sendroute(), inet_canon(); | |
46 | char *inet_format(); | |
47 | ||
48 | #define NIL { 0 } | |
49 | #define INET \ | |
50 | { inet_hash, inet_netmatch, inet_output, \ | |
51 | inet_portmatch, inet_portcheck, inet_checkhost, \ | |
52 | inet_rtflags, inet_sendroute, inet_canon, \ | |
53 | inet_format \ | |
54 | } | |
55 | ||
56 | struct afswitch afswitch[AF_MAX] = { | |
57 | NIL, /* 0- unused */ | |
58 | NIL, /* 1- Unix domain, unused */ | |
59 | INET, /* Internet */ | |
60 | }; | |
61 | ||
62 | int af_max = sizeof(afswitch) / sizeof(afswitch[0]); | |
63 | ||
64 | struct sockaddr_in inet_default = { | |
65 | #ifdef RTM_ADD | |
66 | sizeof (inet_default), | |
67 | #endif | |
68 | AF_INET, INADDR_ANY }; | |
69 | ||
70 | inet_hash(sin, hp) | |
71 | register struct sockaddr_in *sin; | |
72 | struct afhash *hp; | |
73 | { | |
74 | register u_long n; | |
75 | ||
76 | n = inet_netof(sin->sin_addr); | |
77 | if (n) | |
78 | while ((n & 0xff) == 0) | |
79 | n >>= 8; | |
80 | hp->afh_nethash = n; | |
81 | hp->afh_hosthash = ntohl(sin->sin_addr.s_addr); | |
82 | hp->afh_hosthash &= 0x7fffffff; | |
83 | } | |
84 | ||
85 | inet_netmatch(sin1, sin2) | |
86 | struct sockaddr_in *sin1, *sin2; | |
87 | { | |
88 | ||
89 | return (inet_netof(sin1->sin_addr) == inet_netof(sin2->sin_addr)); | |
90 | } | |
91 | ||
92 | /* | |
93 | * Verify the message is from the right port. | |
94 | */ | |
95 | inet_portmatch(sin) | |
96 | register struct sockaddr_in *sin; | |
97 | { | |
98 | ||
99 | return (sin->sin_port == sp->s_port); | |
100 | } | |
101 | ||
102 | /* | |
103 | * Verify the message is from a "trusted" port. | |
104 | */ | |
105 | inet_portcheck(sin) | |
106 | struct sockaddr_in *sin; | |
107 | { | |
108 | ||
109 | return (ntohs(sin->sin_port) <= IPPORT_RESERVED); | |
110 | } | |
111 | ||
112 | /* | |
113 | * Internet output routine. | |
114 | */ | |
115 | inet_output(s, flags, sin, size) | |
116 | int s, flags; | |
117 | struct sockaddr_in *sin; | |
118 | int size; | |
119 | { | |
120 | struct sockaddr_in dst; | |
121 | ||
122 | dst = *sin; | |
123 | sin = &dst; | |
124 | if (sin->sin_port == 0) | |
125 | sin->sin_port = sp->s_port; | |
126 | if (sin->sin_len == 0) | |
127 | sin->sin_len = sizeof (*sin); | |
128 | if (sendto(s, packet, size, flags, | |
129 | (struct sockaddr *)sin, sizeof (*sin)) < 0) | |
130 | perror("sendto"); | |
131 | } | |
132 | ||
133 | /* | |
134 | * Return 1 if the address is believed | |
135 | * for an Internet host -- THIS IS A KLUDGE. | |
136 | */ | |
137 | inet_checkhost(sin) | |
138 | struct sockaddr_in *sin; | |
139 | { | |
140 | u_long i = ntohl(sin->sin_addr.s_addr); | |
141 | ||
142 | #ifndef IN_EXPERIMENTAL | |
143 | #define IN_EXPERIMENTAL(i) (((long) (i) & 0xe0000000) == 0xe0000000) | |
144 | #endif | |
145 | ||
146 | if (IN_EXPERIMENTAL(i) || sin->sin_port != 0) | |
147 | return (0); | |
148 | if (i != 0 && (i & 0xff000000) == 0) | |
149 | return (0); | |
150 | for (i = 0; i < sizeof(sin->sin_zero)/sizeof(sin->sin_zero[0]); i++) | |
151 | if (sin->sin_zero[i]) | |
152 | return (0); | |
153 | return (1); | |
154 | } | |
155 | ||
156 | inet_canon(sin) | |
157 | struct sockaddr_in *sin; | |
158 | { | |
159 | ||
160 | sin->sin_port = 0; | |
161 | sin->sin_len = sizeof(*sin); | |
162 | } | |
163 | ||
164 | char * | |
165 | inet_format(sin) | |
166 | struct sockaddr_in *sin; | |
167 | { | |
168 | char *inet_ntoa(); | |
169 | ||
170 | return (inet_ntoa(sin->sin_addr)); | |
171 | } |