Commit | Line | Data |
---|---|---|
5ff67f98 DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
0eb85d71 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b8c620d6 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
5ff67f98 DF |
16 | */ |
17 | ||
85bbbfa4 | 18 | #ifndef lint |
b8c620d6 | 19 | static char sccsid[] = "@(#)af.c 5.8 (Berkeley) %G%"; |
0eb85d71 | 20 | #endif /* not lint */ |
85bbbfa4 | 21 | |
7fe7fe74 | 22 | #include "defs.h" |
ecce71aa | 23 | |
85bbbfa4 SL |
24 | /* |
25 | * Address family support routines | |
26 | */ | |
85bbbfa4 | 27 | int inet_hash(), inet_netmatch(), inet_output(), |
a773b026 | 28 | inet_portmatch(), inet_portcheck(), |
7892134c | 29 | inet_checkhost(), inet_rtflags(), inet_sendroute(), inet_canon(); |
17fe297f | 30 | char *inet_format(); |
4fad5a6e | 31 | |
17fe297f | 32 | #define NIL { 0 } |
85bbbfa4 SL |
33 | #define INET \ |
34 | { inet_hash, inet_netmatch, inet_output, \ | |
a773b026 | 35 | inet_portmatch, inet_portcheck, inet_checkhost, \ |
7892134c | 36 | inet_rtflags, inet_sendroute, inet_canon, \ |
4fad5a6e MK |
37 | inet_format \ |
38 | } | |
85bbbfa4 | 39 | |
4fad5a6e MK |
40 | struct afswitch afswitch[AF_MAX] = { |
41 | NIL, /* 0- unused */ | |
42 | NIL, /* 1- Unix domain, unused */ | |
43 | INET, /* Internet */ | |
44 | }; | |
17fe297f MK |
45 | |
46 | int af_max = sizeof(afswitch) / sizeof(afswitch[0]); | |
85bbbfa4 | 47 | |
5562f0a3 MK |
48 | struct sockaddr_in inet_default = { AF_INET, INADDR_ANY }; |
49 | ||
85bbbfa4 SL |
50 | inet_hash(sin, hp) |
51 | register struct sockaddr_in *sin; | |
52 | struct afhash *hp; | |
53 | { | |
6dcf6dbc | 54 | register u_long n; |
85bbbfa4 | 55 | |
6dcf6dbc MK |
56 | n = inet_netof(sin->sin_addr); |
57 | if (n) | |
58 | while ((n & 0xff) == 0) | |
59 | n >>= 8; | |
60 | hp->afh_nethash = n; | |
7fe7fe74 | 61 | hp->afh_hosthash = ntohl(sin->sin_addr.s_addr); |
6db0b3a4 | 62 | hp->afh_hosthash &= 0x7fffffff; |
85bbbfa4 SL |
63 | } |
64 | ||
65 | inet_netmatch(sin1, sin2) | |
66 | struct sockaddr_in *sin1, *sin2; | |
67 | { | |
68 | ||
68a24a8f | 69 | return (inet_netof(sin1->sin_addr) == inet_netof(sin2->sin_addr)); |
85bbbfa4 SL |
70 | } |
71 | ||
72 | /* | |
73 | * Verify the message is from the right port. | |
74 | */ | |
75 | inet_portmatch(sin) | |
c8c4156e | 76 | register struct sockaddr_in *sin; |
85bbbfa4 | 77 | { |
6db0b3a4 | 78 | |
7fe7fe74 | 79 | return (sin->sin_port == sp->s_port); |
85bbbfa4 SL |
80 | } |
81 | ||
a773b026 SL |
82 | /* |
83 | * Verify the message is from a "trusted" port. | |
84 | */ | |
85 | inet_portcheck(sin) | |
7fe7fe74 | 86 | struct sockaddr_in *sin; |
a773b026 | 87 | { |
a773b026 | 88 | |
7fe7fe74 | 89 | return (ntohs(sin->sin_port) <= IPPORT_RESERVED); |
a773b026 SL |
90 | } |
91 | ||
85bbbfa4 SL |
92 | /* |
93 | * Internet output routine. | |
94 | */ | |
7fe7fe74 SL |
95 | inet_output(s, flags, sin, size) |
96 | int s, flags; | |
85bbbfa4 SL |
97 | struct sockaddr_in *sin; |
98 | int size; | |
99 | { | |
85bbbfa4 SL |
100 | struct sockaddr_in dst; |
101 | ||
102 | dst = *sin; | |
103 | sin = &dst; | |
c8c4156e | 104 | if (sin->sin_port == 0) |
7fe7fe74 SL |
105 | sin->sin_port = sp->s_port; |
106 | if (sendto(s, packet, size, flags, sin, sizeof (*sin)) < 0) | |
107 | perror("sendto"); | |
85bbbfa4 SL |
108 | } |
109 | ||
110 | /* | |
0ad851d4 SL |
111 | * Return 1 if the address is believed |
112 | * for an Internet host -- THIS IS A KLUDGE. | |
85bbbfa4 SL |
113 | */ |
114 | inet_checkhost(sin) | |
115 | struct sockaddr_in *sin; | |
116 | { | |
e2f67b08 | 117 | u_long i = ntohl(sin->sin_addr.s_addr); |
49139b80 | 118 | |
8ead7088 MK |
119 | #ifndef IN_EXPERIMENTAL |
120 | #define IN_EXPERIMENTAL(i) (((long) (i) & 0xe0000000) == 0xe0000000) | |
121 | #endif | |
d5568f13 | 122 | |
8ead7088 | 123 | if (IN_EXPERIMENTAL(i) || sin->sin_port != 0) |
5562f0a3 | 124 | return (0); |
49139b80 | 125 | if (i != 0 && (i & 0xff000000) == 0) |
5562f0a3 | 126 | return (0); |
a7cec344 MK |
127 | for (i = 0; i < sizeof(sin->sin_zero)/sizeof(sin->sin_zero[0]); i++) |
128 | if (sin->sin_zero[i]) | |
129 | return (0); | |
5562f0a3 | 130 | return (1); |
d5568f13 MK |
131 | } |
132 | ||
85bbbfa4 SL |
133 | inet_canon(sin) |
134 | struct sockaddr_in *sin; | |
135 | { | |
7fe7fe74 | 136 | |
85bbbfa4 SL |
137 | sin->sin_port = 0; |
138 | } | |
139 | ||
17fe297f MK |
140 | char * |
141 | inet_format(sin) | |
142 | struct sockaddr_in *sin; | |
85bbbfa4 | 143 | { |
17fe297f | 144 | char *inet_ntoa(); |
6db0b3a4 | 145 | |
17fe297f | 146 | return (inet_ntoa(sin->sin_addr)); |
85bbbfa4 | 147 | } |