diff options
Diffstat (limited to 'src/lib/libc/net')
72 files changed, 12454 insertions, 0 deletions
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc new file mode 100644 index 0000000000..f4153bbfb4 --- /dev/null +++ b/src/lib/libc/net/Makefile.inc | |||
@@ -0,0 +1,98 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.51 2014/04/07 17:57:56 schwarze Exp $ | ||
2 | |||
3 | # net sources | ||
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/net ${LIBCSRCDIR}/net | ||
5 | |||
6 | CFLAGS+=-DRESOLVSORT | ||
7 | |||
8 | SRCS+= base64.c freeaddrinfo.c gai_strerror.c getaddrinfo.c gethostnamadr.c \ | ||
9 | getifaddrs.c getnameinfo.c getnetbyaddr.c getnetbyname.c getnetent.c \ | ||
10 | getnetnamadr.c getpeereid.c getproto.c getprotoent.c getprotoname.c \ | ||
11 | getservbyname.c getservbyport.c getservent.c getrrsetbyname.c \ | ||
12 | herror.c if_indextoname.c if_nameindex.c if_nametoindex.c inet_addr.c \ | ||
13 | inet_lnaof.c inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \ | ||
14 | inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c inet_pton.c \ | ||
15 | linkaddr.c rcmd.c ruserok.c rresvport.c recv.c res_comp.c res_data.c \ | ||
16 | res_debug.c res_debug_syms.c res_init.c res_mkquery.c res_query.c \ | ||
17 | res_random.c res_send.c send.c sethostent.c ethers.c rcmdsh.c | ||
18 | |||
19 | # IPv6 | ||
20 | SRCS+= ip6opt.c rthdr.c vars6.c | ||
21 | |||
22 | # machine-dependent net sources | ||
23 | # m-d Makefile.inc must include sources for: | ||
24 | # htonl() htons() ntohl() ntohs() | ||
25 | |||
26 | .include "${LIBCSRCDIR}/arch/${MACHINE_CPU}/net/Makefile.inc" | ||
27 | |||
28 | MAN+= byteorder.3 ethers.3 gai_strerror.3 getaddrinfo.3 gethostbyname.3 \ | ||
29 | getifaddrs.3 getnameinfo.3 getnetent.3 getpeereid.3 getprotoent.3 \ | ||
30 | getrrsetbyname.3 getservent.3 if_indextoname.3 inet.3 \ | ||
31 | inet_net.3 inet6_option_space.3 inet6_rthdr_space.3 \ | ||
32 | inet6_opt_init.3 inet6_rth_space.3 link_addr.3 \ | ||
33 | rcmd.3 rcmdsh.3 resolver.3 | ||
34 | |||
35 | MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \ | ||
36 | byteorder.3 ntohs.3 byteorder.3 htobe16.3 byteorder.3 htobe32.3 \ | ||
37 | byteorder.3 htobe64.3 byteorder.3 betoh16.3 byteorder.3 betoh32.3 \ | ||
38 | byteorder.3 betoh64.3 byteorder.3 htole16.3 byteorder.3 htole32.3 \ | ||
39 | byteorder.3 htole64.3 byteorder.3 letoh16.3 byteorder.3 letoh32.3 \ | ||
40 | byteorder.3 letoh64.3 byteorder.3 swap16.3 byteorder.3 swap32.3 \ | ||
41 | byteorder.3 swap64.3 | ||
42 | MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \ | ||
43 | ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 | ||
44 | MLINKS+=getaddrinfo.3 freeaddrinfo.3 | ||
45 | MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \ | ||
46 | gethostbyname.3 sethostent.3 gethostbyname.3 gethostent.3 \ | ||
47 | gethostbyname.3 herror.3 gethostbyname.3 gethostbyname2.3 \ | ||
48 | gethostbyname.3 hstrerror.3 | ||
49 | MLINKS+=getifaddrs.3 freeifaddrs.3 | ||
50 | MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \ | ||
51 | getnetent.3 getnetbyname.3 getnetent.3 setnetent.3 | ||
52 | MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \ | ||
53 | getprotoent.3 getprotobynumber.3 getprotoent.3 setprotoent.3 \ | ||
54 | getprotoent.3 getprotoent_r.3 getprotoent.3 getprotobyname_r.3 \ | ||
55 | getprotoent.3 getprotobynumber_r.3 getprotoent.3 setprotoent_r.3 \ | ||
56 | getprotoent.3 endprotoent_r.3 | ||
57 | MLINKS+=getservent.3 endservent.3 getservent.3 getservbyname.3 \ | ||
58 | getservent.3 getservbyport.3 getservent.3 setservent.3 \ | ||
59 | getservent.3 getservent_r.3 getservent.3 getservbyname_r.3 \ | ||
60 | getservent.3 getservbyport_r.3 getservent.3 setservent_r.3 \ | ||
61 | getservent.3 endservent_r.3 | ||
62 | MLINKS+= if_indextoname.3 if_nametoindex.3 if_indextoname.3 if_nameindex.3 \ | ||
63 | if_indextoname.3 if_freenameindex.3 | ||
64 | MLINKS+=inet.3 inet_addr.3 inet.3 inet_aton.3 \ | ||
65 | inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ | ||
66 | inet.3 inet_network.3 inet.3 inet_ntoa.3 \ | ||
67 | inet.3 inet_ntop.3 inet.3 inet_pton.3 | ||
68 | MLINKS+=inet_net.3 inet_net_ntop.3 inet_net.3 inet_net_pton.3 | ||
69 | MLINKS+=link_addr.3 link_ntoa.3 | ||
70 | MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 \ | ||
71 | rcmd.3 rresvport_af.3 rcmd.3 rcmd_af.3 rcmd.3 iruserok_sa.3 | ||
72 | MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ | ||
73 | resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \ | ||
74 | resolver.3 res_search.3 | ||
75 | MLINKS+=getrrsetbyname.3 freerrset.3 | ||
76 | MLINKS+=inet6_option_space.3 inet6_option_init.3 \ | ||
77 | inet6_option_space.3 inet6_option_append.3 \ | ||
78 | inet6_option_space.3 inet6_option_alloc.3 \ | ||
79 | inet6_option_space.3 inet6_option_next.3 \ | ||
80 | inet6_option_space.3 inet6_option_find.3 | ||
81 | MLINKS+=inet6_rthdr_space.3 inet6_rthdr_init.3 \ | ||
82 | inet6_rthdr_space.3 inet6_rthdr_add.3 \ | ||
83 | inet6_rthdr_space.3 inet6_rthdr_lasthop.3 \ | ||
84 | inet6_rthdr_space.3 inet6_rthdr_reverse.3 \ | ||
85 | inet6_rthdr_space.3 inet6_rthdr_segments.3 \ | ||
86 | inet6_rthdr_space.3 inet6_rthdr_getaddr.3 \ | ||
87 | inet6_rthdr_space.3 inet6_rthdr_getflags.3 | ||
88 | MLINKS+=inet6_opt_init.3 inet6_opt_append.3 \ | ||
89 | inet6_opt_init.3 inet6_opt_finish.3 \ | ||
90 | inet6_opt_init.3 inet6_opt_set_val.3 \ | ||
91 | inet6_opt_init.3 inet6_opt_next.3 \ | ||
92 | inet6_opt_init.3 inet6_opt_find.3 \ | ||
93 | inet6_opt_init.3 inet6_opt_get_val.3 | ||
94 | MLINKS+=inet6_rth_space.3 inet6_rth_init.3 \ | ||
95 | inet6_rth_space.3 inet6_rth_add.3 \ | ||
96 | inet6_rth_space.3 inet6_rth_reverse.3 \ | ||
97 | inet6_rth_space.3 inet6_rth_segments.3 \ | ||
98 | inet6_rth_space.3 inet6_rth_getaddr.3 \ | ||
diff --git a/src/lib/libc/net/base64.c b/src/lib/libc/net/base64.c new file mode 100644 index 0000000000..7c3d1d319f --- /dev/null +++ b/src/lib/libc/net/base64.c | |||
@@ -0,0 +1,316 @@ | |||
1 | /* $OpenBSD: base64.c,v 1.7 2013/12/31 02:32:56 tedu Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | /* | ||
21 | * Portions Copyright (c) 1995 by International Business Machines, Inc. | ||
22 | * | ||
23 | * International Business Machines, Inc. (hereinafter called IBM) grants | ||
24 | * permission under its copyrights to use, copy, modify, and distribute this | ||
25 | * Software with or without fee, provided that the above copyright notice and | ||
26 | * all paragraphs of this notice appear in all copies, and that the name of IBM | ||
27 | * not be used in connection with the marketing of any product incorporating | ||
28 | * the Software or modifications thereof, without specific, written prior | ||
29 | * permission. | ||
30 | * | ||
31 | * To the extent it has a right to do so, IBM grants an immunity from suit | ||
32 | * under its patents, if any, for the use, sale or manufacture of products to | ||
33 | * the extent that such products are used for performing Domain Name System | ||
34 | * dynamic updates in TCP/IP networks by means of the Software. No immunity is | ||
35 | * granted for any product per se or for any other function of any product. | ||
36 | * | ||
37 | * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, | ||
38 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
39 | * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, | ||
40 | * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING | ||
41 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN | ||
42 | * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. | ||
43 | */ | ||
44 | |||
45 | #include <sys/types.h> | ||
46 | #include <sys/param.h> | ||
47 | #include <sys/socket.h> | ||
48 | #include <netinet/in.h> | ||
49 | #include <arpa/inet.h> | ||
50 | #include <arpa/nameser.h> | ||
51 | |||
52 | #include <ctype.h> | ||
53 | #include <resolv.h> | ||
54 | #include <stdio.h> | ||
55 | |||
56 | #include <stdlib.h> | ||
57 | #include <string.h> | ||
58 | |||
59 | static const char Base64[] = | ||
60 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
61 | static const char Pad64 = '='; | ||
62 | |||
63 | /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) | ||
64 | The following encoding technique is taken from RFC 1521 by Borenstein | ||
65 | and Freed. It is reproduced here in a slightly edited form for | ||
66 | convenience. | ||
67 | |||
68 | A 65-character subset of US-ASCII is used, enabling 6 bits to be | ||
69 | represented per printable character. (The extra 65th character, "=", | ||
70 | is used to signify a special processing function.) | ||
71 | |||
72 | The encoding process represents 24-bit groups of input bits as output | ||
73 | strings of 4 encoded characters. Proceeding from left to right, a | ||
74 | 24-bit input group is formed by concatenating 3 8-bit input groups. | ||
75 | These 24 bits are then treated as 4 concatenated 6-bit groups, each | ||
76 | of which is translated into a single digit in the base64 alphabet. | ||
77 | |||
78 | Each 6-bit group is used as an index into an array of 64 printable | ||
79 | characters. The character referenced by the index is placed in the | ||
80 | output string. | ||
81 | |||
82 | Table 1: The Base64 Alphabet | ||
83 | |||
84 | Value Encoding Value Encoding Value Encoding Value Encoding | ||
85 | 0 A 17 R 34 i 51 z | ||
86 | 1 B 18 S 35 j 52 0 | ||
87 | 2 C 19 T 36 k 53 1 | ||
88 | 3 D 20 U 37 l 54 2 | ||
89 | 4 E 21 V 38 m 55 3 | ||
90 | 5 F 22 W 39 n 56 4 | ||
91 | 6 G 23 X 40 o 57 5 | ||
92 | 7 H 24 Y 41 p 58 6 | ||
93 | 8 I 25 Z 42 q 59 7 | ||
94 | 9 J 26 a 43 r 60 8 | ||
95 | 10 K 27 b 44 s 61 9 | ||
96 | 11 L 28 c 45 t 62 + | ||
97 | 12 M 29 d 46 u 63 / | ||
98 | 13 N 30 e 47 v | ||
99 | 14 O 31 f 48 w (pad) = | ||
100 | 15 P 32 g 49 x | ||
101 | 16 Q 33 h 50 y | ||
102 | |||
103 | Special processing is performed if fewer than 24 bits are available | ||
104 | at the end of the data being encoded. A full encoding quantum is | ||
105 | always completed at the end of a quantity. When fewer than 24 input | ||
106 | bits are available in an input group, zero bits are added (on the | ||
107 | right) to form an integral number of 6-bit groups. Padding at the | ||
108 | end of the data is performed using the '=' character. | ||
109 | |||
110 | Since all base64 input is an integral number of octets, only the | ||
111 | ------------------------------------------------- | ||
112 | following cases can arise: | ||
113 | |||
114 | (1) the final quantum of encoding input is an integral | ||
115 | multiple of 24 bits; here, the final unit of encoded | ||
116 | output will be an integral multiple of 4 characters | ||
117 | with no "=" padding, | ||
118 | (2) the final quantum of encoding input is exactly 8 bits; | ||
119 | here, the final unit of encoded output will be two | ||
120 | characters followed by two "=" padding characters, or | ||
121 | (3) the final quantum of encoding input is exactly 16 bits; | ||
122 | here, the final unit of encoded output will be three | ||
123 | characters followed by one "=" padding character. | ||
124 | */ | ||
125 | |||
126 | int | ||
127 | b64_ntop(src, srclength, target, targsize) | ||
128 | u_char const *src; | ||
129 | size_t srclength; | ||
130 | char *target; | ||
131 | size_t targsize; | ||
132 | { | ||
133 | size_t datalength = 0; | ||
134 | u_char input[3]; | ||
135 | u_char output[4]; | ||
136 | int i; | ||
137 | |||
138 | while (2 < srclength) { | ||
139 | input[0] = *src++; | ||
140 | input[1] = *src++; | ||
141 | input[2] = *src++; | ||
142 | srclength -= 3; | ||
143 | |||
144 | output[0] = input[0] >> 2; | ||
145 | output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); | ||
146 | output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); | ||
147 | output[3] = input[2] & 0x3f; | ||
148 | |||
149 | if (datalength + 4 > targsize) | ||
150 | return (-1); | ||
151 | target[datalength++] = Base64[output[0]]; | ||
152 | target[datalength++] = Base64[output[1]]; | ||
153 | target[datalength++] = Base64[output[2]]; | ||
154 | target[datalength++] = Base64[output[3]]; | ||
155 | } | ||
156 | |||
157 | /* Now we worry about padding. */ | ||
158 | if (0 != srclength) { | ||
159 | /* Get what's left. */ | ||
160 | input[0] = input[1] = input[2] = '\0'; | ||
161 | for (i = 0; i < srclength; i++) | ||
162 | input[i] = *src++; | ||
163 | |||
164 | output[0] = input[0] >> 2; | ||
165 | output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); | ||
166 | output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); | ||
167 | |||
168 | if (datalength + 4 > targsize) | ||
169 | return (-1); | ||
170 | target[datalength++] = Base64[output[0]]; | ||
171 | target[datalength++] = Base64[output[1]]; | ||
172 | if (srclength == 1) | ||
173 | target[datalength++] = Pad64; | ||
174 | else | ||
175 | target[datalength++] = Base64[output[2]]; | ||
176 | target[datalength++] = Pad64; | ||
177 | } | ||
178 | if (datalength >= targsize) | ||
179 | return (-1); | ||
180 | target[datalength] = '\0'; /* Returned value doesn't count \0. */ | ||
181 | return (datalength); | ||
182 | } | ||
183 | |||
184 | /* skips all whitespace anywhere. | ||
185 | converts characters, four at a time, starting at (or after) | ||
186 | src from base - 64 numbers into three 8 bit bytes in the target area. | ||
187 | it returns the number of data bytes stored at the target, or -1 on error. | ||
188 | */ | ||
189 | |||
190 | int | ||
191 | b64_pton(src, target, targsize) | ||
192 | char const *src; | ||
193 | u_char *target; | ||
194 | size_t targsize; | ||
195 | { | ||
196 | int tarindex, state, ch; | ||
197 | u_char nextbyte; | ||
198 | char *pos; | ||
199 | |||
200 | state = 0; | ||
201 | tarindex = 0; | ||
202 | |||
203 | while ((ch = (unsigned char)*src++) != '\0') { | ||
204 | if (isspace(ch)) /* Skip whitespace anywhere. */ | ||
205 | continue; | ||
206 | |||
207 | if (ch == Pad64) | ||
208 | break; | ||
209 | |||
210 | pos = strchr(Base64, ch); | ||
211 | if (pos == 0) /* A non-base64 character. */ | ||
212 | return (-1); | ||
213 | |||
214 | switch (state) { | ||
215 | case 0: | ||
216 | if (target) { | ||
217 | if (tarindex >= targsize) | ||
218 | return (-1); | ||
219 | target[tarindex] = (pos - Base64) << 2; | ||
220 | } | ||
221 | state = 1; | ||
222 | break; | ||
223 | case 1: | ||
224 | if (target) { | ||
225 | if (tarindex >= targsize) | ||
226 | return (-1); | ||
227 | target[tarindex] |= (pos - Base64) >> 4; | ||
228 | nextbyte = ((pos - Base64) & 0x0f) << 4; | ||
229 | if (tarindex + 1 < targsize) | ||
230 | target[tarindex+1] = nextbyte; | ||
231 | else if (nextbyte) | ||
232 | return (-1); | ||
233 | } | ||
234 | tarindex++; | ||
235 | state = 2; | ||
236 | break; | ||
237 | case 2: | ||
238 | if (target) { | ||
239 | if (tarindex >= targsize) | ||
240 | return (-1); | ||
241 | target[tarindex] |= (pos - Base64) >> 2; | ||
242 | nextbyte = ((pos - Base64) & 0x03) << 6; | ||
243 | if (tarindex + 1 < targsize) | ||
244 | target[tarindex+1] = nextbyte; | ||
245 | else if (nextbyte) | ||
246 | return (-1); | ||
247 | } | ||
248 | tarindex++; | ||
249 | state = 3; | ||
250 | break; | ||
251 | case 3: | ||
252 | if (target) { | ||
253 | if (tarindex >= targsize) | ||
254 | return (-1); | ||
255 | target[tarindex] |= (pos - Base64); | ||
256 | } | ||
257 | tarindex++; | ||
258 | state = 0; | ||
259 | break; | ||
260 | } | ||
261 | } | ||
262 | |||
263 | /* | ||
264 | * We are done decoding Base-64 chars. Let's see if we ended | ||
265 | * on a byte boundary, and/or with erroneous trailing characters. | ||
266 | */ | ||
267 | |||
268 | if (ch == Pad64) { /* We got a pad char. */ | ||
269 | ch = (unsigned char)*src++; /* Skip it, get next. */ | ||
270 | switch (state) { | ||
271 | case 0: /* Invalid = in first position */ | ||
272 | case 1: /* Invalid = in second position */ | ||
273 | return (-1); | ||
274 | |||
275 | case 2: /* Valid, means one byte of info */ | ||
276 | /* Skip any number of spaces. */ | ||
277 | for (; ch != '\0'; ch = (unsigned char)*src++) | ||
278 | if (!isspace(ch)) | ||
279 | break; | ||
280 | /* Make sure there is another trailing = sign. */ | ||
281 | if (ch != Pad64) | ||
282 | return (-1); | ||
283 | ch = (unsigned char)*src++; /* Skip the = */ | ||
284 | /* Fall through to "single trailing =" case. */ | ||
285 | /* FALLTHROUGH */ | ||
286 | |||
287 | case 3: /* Valid, means two bytes of info */ | ||
288 | /* | ||
289 | * We know this char is an =. Is there anything but | ||
290 | * whitespace after it? | ||
291 | */ | ||
292 | for (; ch != '\0'; ch = (unsigned char)*src++) | ||
293 | if (!isspace(ch)) | ||
294 | return (-1); | ||
295 | |||
296 | /* | ||
297 | * Now make sure for cases 2 and 3 that the "extra" | ||
298 | * bits that slopped past the last full byte were | ||
299 | * zeros. If we don't check them, they become a | ||
300 | * subliminal channel. | ||
301 | */ | ||
302 | if (target && tarindex < targsize && | ||
303 | target[tarindex] != 0) | ||
304 | return (-1); | ||
305 | } | ||
306 | } else { | ||
307 | /* | ||
308 | * We ended by seeing the end of the string. Make sure we | ||
309 | * have no partial bytes lying around. | ||
310 | */ | ||
311 | if (state != 0) | ||
312 | return (-1); | ||
313 | } | ||
314 | |||
315 | return (tarindex); | ||
316 | } | ||
diff --git a/src/lib/libc/net/byteorder.3 b/src/lib/libc/net/byteorder.3 new file mode 100644 index 0000000000..53f9fd071d --- /dev/null +++ b/src/lib/libc/net/byteorder.3 | |||
@@ -0,0 +1,206 @@ | |||
1 | .\" $OpenBSD: byteorder.3,v 1.18 2013/06/05 03:39:22 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: June 5 2013 $ | ||
31 | .Dt BYTEORDER 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm htonl , | ||
35 | .Nm htons , | ||
36 | .Nm ntohl , | ||
37 | .Nm ntohs , | ||
38 | .Nm htobe64 , | ||
39 | .Nm htobe32 , | ||
40 | .Nm htobe16 , | ||
41 | .Nm betoh64 , | ||
42 | .Nm betoh32 , | ||
43 | .Nm betoh16 , | ||
44 | .Nm htole64 , | ||
45 | .Nm htole32 , | ||
46 | .Nm htole16 , | ||
47 | .Nm letoh64 , | ||
48 | .Nm letoh32 , | ||
49 | .Nm letoh16 , | ||
50 | .Nm swap64 , | ||
51 | .Nm swap32 , | ||
52 | .Nm swap16 | ||
53 | .Nd convert values between different byte orderings | ||
54 | .Sh SYNOPSIS | ||
55 | .In sys/types.h | ||
56 | .Ft u_int32_t | ||
57 | .Fn htonl "u_int32_t host32" | ||
58 | .Ft u_int16_t | ||
59 | .Fn htons "u_int16_t host16" | ||
60 | .Ft u_int32_t | ||
61 | .Fn ntohl "u_int32_t net32" | ||
62 | .Ft u_int16_t | ||
63 | .Fn ntohs "u_int16_t net16" | ||
64 | .Ft u_int64_t | ||
65 | .Fn htobe64 "u_int64_t host64" | ||
66 | .Ft u_int32_t | ||
67 | .Fn htobe32 "u_int32_t host32" | ||
68 | .Ft u_int16_t | ||
69 | .Fn htobe16 "u_int16_t host16" | ||
70 | .Ft u_int64_t | ||
71 | .Fn betoh64 "u_int64_t big64" | ||
72 | .Ft u_int32_t | ||
73 | .Fn betoh32 "u_int32_t big32" | ||
74 | .Ft u_int16_t | ||
75 | .Fn betoh16 "u_int16_t big16" | ||
76 | .Ft u_int64_t | ||
77 | .Fn htole64 "u_int64_t host64" | ||
78 | .Ft u_int32_t | ||
79 | .Fn htole32 "u_int32_t host32" | ||
80 | .Ft u_int16_t | ||
81 | .Fn htole16 "u_int16_t host16" | ||
82 | .Ft u_int64_t | ||
83 | .Fn letoh64 "u_int64_t little64" | ||
84 | .Ft u_int32_t | ||
85 | .Fn letoh32 "u_int32_t little32" | ||
86 | .Ft u_int16_t | ||
87 | .Fn letoh16 "u_int16_t little16" | ||
88 | .Ft u_int64_t | ||
89 | .Fn swap64 "u_int64_t val64" | ||
90 | .Ft u_int32_t | ||
91 | .Fn swap32 "u_int32_t val32" | ||
92 | .Ft u_int16_t | ||
93 | .Fn swap16 "u_int16_t val16" | ||
94 | .Sh DESCRIPTION | ||
95 | These routines convert 16, 32 and 64-bit quantities between different | ||
96 | byte orderings. | ||
97 | The | ||
98 | .Dq swap | ||
99 | functions reverse the byte ordering of | ||
100 | the given quantity; the others convert either from/to the native | ||
101 | byte order used by the host to/from either little- or big-endian (a.k.a | ||
102 | network) order. | ||
103 | .Pp | ||
104 | Apart from the swap functions, the names can be described by this form: | ||
105 | {src-order}to{dst-order}{size}. | ||
106 | Both {src-order} and {dst-order} can take the following forms: | ||
107 | .Pp | ||
108 | .Bl -tag -width "be " -offset indent -compact | ||
109 | .It h | ||
110 | Host order. | ||
111 | .It n | ||
112 | Network order (big-endian). | ||
113 | .It be | ||
114 | Big-endian (most significant byte first). | ||
115 | .It le | ||
116 | Little-endian (least significant byte first). | ||
117 | .El | ||
118 | .Pp | ||
119 | One of the specified orderings must be | ||
120 | .Sq h . | ||
121 | {size} will take these forms: | ||
122 | .Pp | ||
123 | .Bl -tag -width "32 " -offset indent -compact | ||
124 | .It l | ||
125 | Long (32-bit, used in conjunction with forms involving | ||
126 | .Sq n ) . | ||
127 | .It s | ||
128 | Short (16-bit, used in conjunction with forms involving | ||
129 | .Sq n ) . | ||
130 | .It 16 | ||
131 | 16-bit. | ||
132 | .It 32 | ||
133 | 32-bit. | ||
134 | .It 64 | ||
135 | 64-bit. | ||
136 | .El | ||
137 | .Pp | ||
138 | The swap functions are of the form: swap{size}. | ||
139 | .Pp | ||
140 | Names involving | ||
141 | .Sq n | ||
142 | convert quantities between network | ||
143 | byte order and host byte order. | ||
144 | The last letter | ||
145 | .Pf ( Sq s | ||
146 | or | ||
147 | .Sq l ) | ||
148 | is a mnemonic | ||
149 | for the traditional names for such quantities, | ||
150 | .Li short | ||
151 | and | ||
152 | .Li long , | ||
153 | respectively. | ||
154 | Today, the C concept of | ||
155 | .Li short | ||
156 | and | ||
157 | .Li long | ||
158 | integers need not coincide with this traditional misunderstanding. | ||
159 | On machines which have a byte order which is the same as the network | ||
160 | order, routines are defined as null macros. | ||
161 | .Pp | ||
162 | The functions involving either | ||
163 | .Dq be , | ||
164 | .Dq le , | ||
165 | or | ||
166 | .Dq swap | ||
167 | use the numbers | ||
168 | 16, 32, or 64 for specifying the bitwidth of the quantities they operate on. | ||
169 | Currently all supported architectures are either big- or little-endian | ||
170 | so either the | ||
171 | .Dq be | ||
172 | or | ||
173 | .Dq le | ||
174 | variants are implemented as null macros. | ||
175 | .Pp | ||
176 | The routines mentioned above which have either {src-order} or {dst-order} | ||
177 | set to | ||
178 | .Sq n | ||
179 | are most often used in | ||
180 | conjunction with Internet addresses and ports as returned by | ||
181 | .Xr gethostbyname 3 | ||
182 | and | ||
183 | .Xr getservent 3 . | ||
184 | .Sh SEE ALSO | ||
185 | .Xr gethostbyname 3 , | ||
186 | .Xr getservent 3 | ||
187 | .Sh STANDARDS | ||
188 | The | ||
189 | .Fn htonl , | ||
190 | .Fn htons , | ||
191 | .Fn ntohl , | ||
192 | and | ||
193 | .Fn ntohs | ||
194 | functions conform to | ||
195 | .St -p1003.1 . | ||
196 | The other functions are extensions that should not be used when portability | ||
197 | is required. | ||
198 | .Sh HISTORY | ||
199 | The | ||
200 | .Nm byteorder | ||
201 | functions appeared in | ||
202 | .Bx 4.2 . | ||
203 | .Sh BUGS | ||
204 | On the vax, alpha, amd64, i386, and some mips and arm architectures, | ||
205 | bytes are handled backwards from most everyone else in the world. | ||
206 | This is not expected to be fixed in the near future. | ||
diff --git a/src/lib/libc/net/ethers.3 b/src/lib/libc/net/ethers.3 new file mode 100644 index 0000000000..c254a120a7 --- /dev/null +++ b/src/lib/libc/net/ethers.3 | |||
@@ -0,0 +1,124 @@ | |||
1 | .\" $OpenBSD: ethers.3,v 1.23 2014/03/18 03:58:49 lteo Exp $ | ||
2 | .\" | ||
3 | .\" Written by roland@frob.com. Public domain. | ||
4 | .\" | ||
5 | .Dd $Mdocdate: March 18 2014 $ | ||
6 | .Dt ETHERS 3 | ||
7 | .Os | ||
8 | .Sh NAME | ||
9 | .Nm ether_aton , | ||
10 | .Nm ether_ntoa , | ||
11 | .Nm ether_ntohost , | ||
12 | .Nm ether_hostton , | ||
13 | .Nm ether_line | ||
14 | .Nd get ethers entry | ||
15 | .Sh SYNOPSIS | ||
16 | .In sys/types.h | ||
17 | .In sys/socket.h | ||
18 | .In net/if.h | ||
19 | .In netinet/in.h | ||
20 | .In netinet/if_ether.h | ||
21 | .Ft char * | ||
22 | .Fn ether_ntoa "struct ether_addr *e" | ||
23 | .Ft struct ether_addr * | ||
24 | .Fn ether_aton "const char *s" | ||
25 | .Ft int | ||
26 | .Fn ether_ntohost "char *hostname" "struct ether_addr *e" | ||
27 | .Ft int | ||
28 | .Fn ether_hostton "const char *hostname" "struct ether_addr *e" | ||
29 | .Ft int | ||
30 | .Fn ether_line "const char *l" "struct ether_addr *e" "char *hostname" | ||
31 | .Sh DESCRIPTION | ||
32 | Ethernet addresses are represented by the | ||
33 | following structure: | ||
34 | .Bd -literal -offset indent | ||
35 | struct ether_addr { | ||
36 | u_int8_t ether_addr_octet[ETHER_ADDR_LEN]; | ||
37 | }; | ||
38 | .Ed | ||
39 | .Pp | ||
40 | The | ||
41 | .Fn ether_ntoa | ||
42 | function converts this structure into an | ||
43 | .Tn ASCII | ||
44 | string of the form | ||
45 | .Dq xx:xx:xx:xx:xx:xx , | ||
46 | consisting of 6 hexadecimal numbers separated | ||
47 | by colons. | ||
48 | It returns a pointer to a static buffer that is reused for each call. | ||
49 | The | ||
50 | .Fn ether_aton | ||
51 | converts an | ||
52 | .Tn ASCII | ||
53 | string of the same form and to a structure | ||
54 | containing the 6 octets of the address. | ||
55 | It returns a pointer to a static structure that is reused for each call. | ||
56 | .Fn ether_aton | ||
57 | will return NULL if the string does not represent a valid address. | ||
58 | .Pp | ||
59 | The | ||
60 | .Fn ether_ntohost | ||
61 | and | ||
62 | .Fn ether_hostton | ||
63 | functions interrogate the database mapping host names to Ethernet | ||
64 | addresses, | ||
65 | .Pa /etc/ethers . | ||
66 | The | ||
67 | .Fn ether_ntohost | ||
68 | function looks up the given Ethernet address and writes the associated | ||
69 | host name into the character buffer passed. | ||
70 | This buffer should be | ||
71 | .Dv MAXHOSTNAMELEN | ||
72 | characters in size. | ||
73 | The | ||
74 | .Fn ether_hostton | ||
75 | function looks up the given host name and writes the associated | ||
76 | Ethernet address into the structure passed. | ||
77 | Both functions return | ||
78 | zero if they find the requested host name or address, and \-1 if not. | ||
79 | .Pp | ||
80 | Each call reads | ||
81 | .Pa /etc/ethers | ||
82 | from the beginning; if a | ||
83 | .Ql + | ||
84 | appears alone on a line in the file, then | ||
85 | .Fn ether_hostton | ||
86 | will consult the | ||
87 | .Pa ethers.byname | ||
88 | YP map, and | ||
89 | .Fn ether_ntohost | ||
90 | will consult the | ||
91 | .Pa ethers.byaddr | ||
92 | YP map. | ||
93 | .Pp | ||
94 | The | ||
95 | .Fn ether_line | ||
96 | function parses a line from the | ||
97 | .Pa /etc/ethers | ||
98 | file and fills in the passed | ||
99 | .Li struct ether_addr | ||
100 | and character buffer with the Ethernet address and host name on the line. | ||
101 | It returns zero if the line was successfully parsed and \-1 if not. | ||
102 | The character buffer should be | ||
103 | .Dv MAXHOSTNAMELEN | ||
104 | characters in size. | ||
105 | .Sh FILES | ||
106 | .Bl -tag -width /etc/ethers -compact | ||
107 | .It Pa /etc/ethers | ||
108 | .El | ||
109 | .Sh SEE ALSO | ||
110 | .Xr ethers 5 | ||
111 | .Sh HISTORY | ||
112 | The | ||
113 | .Fn ether_ntoa , | ||
114 | .Fn ether_aton , | ||
115 | .Fn ether_ntohost , | ||
116 | .Fn ether_hostton , | ||
117 | and | ||
118 | .Fn ether_line | ||
119 | functions were adopted from SunOS and appeared in | ||
120 | .Nx 0.9b . | ||
121 | .Sh BUGS | ||
122 | The data space used by these functions is static; if future use | ||
123 | requires the data, it should be copied before any subsequent calls to | ||
124 | these functions overwrite it. | ||
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c new file mode 100644 index 0000000000..af31bb8e60 --- /dev/null +++ b/src/lib/libc/net/ethers.c | |||
@@ -0,0 +1,234 @@ | |||
1 | /* $OpenBSD: ethers.c,v 1.21 2013/11/24 23:51:28 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | /* | ||
20 | * ethers(3) a la Sun. | ||
21 | * Originally Written by Roland McGrath <roland@frob.com> 10/14/93. | ||
22 | * Substantially modified by Todd C. Miller <Todd.Miller@courtesan.com> | ||
23 | */ | ||
24 | |||
25 | #include <sys/types.h> | ||
26 | #include <sys/socket.h> | ||
27 | #include <net/if.h> | ||
28 | #include <netinet/in.h> | ||
29 | #include <netinet/if_ether.h> | ||
30 | #include <sys/param.h> | ||
31 | #include <paths.h> | ||
32 | #include <errno.h> | ||
33 | #include <stdio.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <ctype.h> | ||
37 | #ifdef YP | ||
38 | #include <rpcsvc/ypclnt.h> | ||
39 | #endif | ||
40 | |||
41 | #ifndef _PATH_ETHERS | ||
42 | #define _PATH_ETHERS "/etc/ethers" | ||
43 | #endif | ||
44 | |||
45 | static char * _ether_aton(const char *, struct ether_addr *); | ||
46 | |||
47 | char * | ||
48 | ether_ntoa(struct ether_addr *e) | ||
49 | { | ||
50 | static char a[] = "xx:xx:xx:xx:xx:xx"; | ||
51 | |||
52 | (void)snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
53 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
54 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
55 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
56 | |||
57 | return (a); | ||
58 | } | ||
59 | |||
60 | static char * | ||
61 | _ether_aton(const char *s, struct ether_addr *e) | ||
62 | { | ||
63 | int i; | ||
64 | long l; | ||
65 | char *pp; | ||
66 | |||
67 | while (isspace((unsigned char)*s)) | ||
68 | s++; | ||
69 | |||
70 | /* expect 6 hex octets separated by ':' or space/NUL if last octet */ | ||
71 | for (i = 0; i < 6; i++) { | ||
72 | l = strtol(s, &pp, 16); | ||
73 | if (pp == s || l > 0xFF || l < 0) | ||
74 | return (NULL); | ||
75 | if (!(*pp == ':' || | ||
76 | (i == 5 && (isspace((unsigned char)*pp) || | ||
77 | *pp == '\0')))) | ||
78 | return (NULL); | ||
79 | e->ether_addr_octet[i] = (u_char)l; | ||
80 | s = pp + 1; | ||
81 | } | ||
82 | |||
83 | /* return character after the octets ala strtol(3) */ | ||
84 | return (pp); | ||
85 | } | ||
86 | |||
87 | struct ether_addr * | ||
88 | ether_aton(const char *s) | ||
89 | { | ||
90 | static struct ether_addr n; | ||
91 | |||
92 | return (_ether_aton(s, &n) ? &n : NULL); | ||
93 | } | ||
94 | |||
95 | int | ||
96 | ether_ntohost(char *hostname, struct ether_addr *e) | ||
97 | { | ||
98 | FILE *f; | ||
99 | char buf[BUFSIZ+1], *p; | ||
100 | size_t len; | ||
101 | struct ether_addr try; | ||
102 | #ifdef YP | ||
103 | char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; | ||
104 | int trylen; | ||
105 | #endif | ||
106 | |||
107 | #ifdef YP | ||
108 | snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x", | ||
109 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
110 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
111 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
112 | trylen = strlen(trybuf); | ||
113 | #endif | ||
114 | |||
115 | f = fopen(_PATH_ETHERS, "r"); | ||
116 | if (f == NULL) | ||
117 | return (-1); | ||
118 | while ((p = fgetln(f, &len)) != NULL) { | ||
119 | if (p[len-1] == '\n') | ||
120 | len--; | ||
121 | if (len > sizeof(buf) - 2) | ||
122 | continue; | ||
123 | (void)memcpy(buf, p, len); | ||
124 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
125 | buf[len+1] = '\0'; | ||
126 | #ifdef YP | ||
127 | /* A + in the file means try YP now. */ | ||
128 | if (!strncmp(buf, "+\n", sizeof(buf))) { | ||
129 | char *ypbuf, *ypdom; | ||
130 | int ypbuflen; | ||
131 | |||
132 | if (yp_get_default_domain(&ypdom)) | ||
133 | continue; | ||
134 | if (yp_match(ypdom, "ethers.byaddr", trybuf, | ||
135 | trylen, &ypbuf, &ypbuflen)) | ||
136 | continue; | ||
137 | if (ether_line(ypbuf, &try, hostname) == 0) { | ||
138 | free(ypbuf); | ||
139 | (void)fclose(f); | ||
140 | return (0); | ||
141 | } | ||
142 | free(ypbuf); | ||
143 | continue; | ||
144 | } | ||
145 | #endif | ||
146 | if (ether_line(buf, &try, hostname) == 0 && | ||
147 | memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { | ||
148 | (void)fclose(f); | ||
149 | return (0); | ||
150 | } | ||
151 | } | ||
152 | (void)fclose(f); | ||
153 | errno = ENOENT; | ||
154 | return (-1); | ||
155 | } | ||
156 | |||
157 | int | ||
158 | ether_hostton(const char *hostname, struct ether_addr *e) | ||
159 | { | ||
160 | FILE *f; | ||
161 | char buf[BUFSIZ+1], *p; | ||
162 | char try[MAXHOSTNAMELEN]; | ||
163 | size_t len; | ||
164 | #ifdef YP | ||
165 | int hostlen = strlen(hostname); | ||
166 | #endif | ||
167 | |||
168 | f = fopen(_PATH_ETHERS, "r"); | ||
169 | if (f==NULL) | ||
170 | return (-1); | ||
171 | |||
172 | while ((p = fgetln(f, &len)) != NULL) { | ||
173 | if (p[len-1] == '\n') | ||
174 | len--; | ||
175 | if (len > sizeof(buf) - 2) | ||
176 | continue; | ||
177 | memcpy(buf, p, len); | ||
178 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
179 | buf[len+1] = '\0'; | ||
180 | #ifdef YP | ||
181 | /* A + in the file means try YP now. */ | ||
182 | if (!strncmp(buf, "+\n", sizeof(buf))) { | ||
183 | char *ypbuf, *ypdom; | ||
184 | int ypbuflen; | ||
185 | |||
186 | if (yp_get_default_domain(&ypdom)) | ||
187 | continue; | ||
188 | if (yp_match(ypdom, "ethers.byname", hostname, hostlen, | ||
189 | &ypbuf, &ypbuflen)) | ||
190 | continue; | ||
191 | if (ether_line(ypbuf, e, try) == 0) { | ||
192 | free(ypbuf); | ||
193 | (void)fclose(f); | ||
194 | return (0); | ||
195 | } | ||
196 | free(ypbuf); | ||
197 | continue; | ||
198 | } | ||
199 | #endif | ||
200 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { | ||
201 | (void)fclose(f); | ||
202 | return (0); | ||
203 | } | ||
204 | } | ||
205 | (void)fclose(f); | ||
206 | errno = ENOENT; | ||
207 | return (-1); | ||
208 | } | ||
209 | |||
210 | int | ||
211 | ether_line(const char *line, struct ether_addr *e, char *hostname) | ||
212 | { | ||
213 | char *p; | ||
214 | size_t n; | ||
215 | |||
216 | /* Parse "xx:xx:xx:xx:xx:xx" */ | ||
217 | if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) | ||
218 | goto bad; | ||
219 | |||
220 | /* Now get the hostname */ | ||
221 | while (isspace((unsigned char)*p)) | ||
222 | p++; | ||
223 | if (*p == '\0') | ||
224 | goto bad; | ||
225 | n = strcspn(p, " \t\n"); | ||
226 | if (n >= MAXHOSTNAMELEN) | ||
227 | goto bad; | ||
228 | strlcpy(hostname, p, n + 1); | ||
229 | return (0); | ||
230 | |||
231 | bad: | ||
232 | errno = EINVAL; | ||
233 | return (-1); | ||
234 | } | ||
diff --git a/src/lib/libc/net/freeaddrinfo.c b/src/lib/libc/net/freeaddrinfo.c new file mode 100644 index 0000000000..58702d0b18 --- /dev/null +++ b/src/lib/libc/net/freeaddrinfo.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: freeaddrinfo.c,v 1.6 2005/03/25 13:24:11 otto Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996, 1997, 1998, 1999, Craig Metz, All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. All advertising materials mentioning features or use of this software | ||
15 | * must display the following acknowledgement: | ||
16 | * This product includes software developed by Craig Metz and | ||
17 | * by other contributors. | ||
18 | * 4. Neither the name of the author nor the names of contributors | ||
19 | * may be used to endorse or promote products derived from this software | ||
20 | * without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | #include <stdlib.h> | ||
36 | #include <netdb.h> | ||
37 | |||
38 | void | ||
39 | freeaddrinfo(struct addrinfo *ai) | ||
40 | { | ||
41 | struct addrinfo *p; | ||
42 | |||
43 | do { | ||
44 | p = ai; | ||
45 | ai = ai->ai_next; | ||
46 | if (p->ai_canonname) | ||
47 | free(p->ai_canonname); | ||
48 | free((void *)p); | ||
49 | } while (ai); | ||
50 | } | ||
diff --git a/src/lib/libc/net/gai_strerror.3 b/src/lib/libc/net/gai_strerror.3 new file mode 100644 index 0000000000..e0373eb91e --- /dev/null +++ b/src/lib/libc/net/gai_strerror.3 | |||
@@ -0,0 +1,95 @@ | |||
1 | .\" $OpenBSD: gai_strerror.3,v 1.9 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" $KAME: gai_strerror.3,v 1.1 2005/01/05 03:04:47 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") | ||
5 | .\" Copyright (C) 2000, 2001 Internet Software Consortium. | ||
6 | .\" | ||
7 | .\" Permission to use, copy, modify, and distribute this software for any | ||
8 | .\" purpose with or without fee is hereby granted, provided that the above | ||
9 | .\" copyright notice and this permission notice appear in all copies. | ||
10 | .\" | ||
11 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH | ||
12 | .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
13 | .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
14 | .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
15 | .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE | ||
16 | .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
17 | .\" PERFORMANCE OF THIS SOFTWARE. | ||
18 | .\" | ||
19 | .Dd $Mdocdate: January 21 2014 $ | ||
20 | .Dt GAI_STRERROR 3 | ||
21 | .Os | ||
22 | .Sh NAME | ||
23 | .Nm gai_strerror | ||
24 | .Nd get error message string from EAI_xxx error code | ||
25 | .Sh SYNOPSIS | ||
26 | .In sys/types.h | ||
27 | .In sys/socket.h | ||
28 | .In netdb.h | ||
29 | .Ft "const char *" | ||
30 | .Fn gai_strerror "int ecode" | ||
31 | .Sh DESCRIPTION | ||
32 | The | ||
33 | .Fn gai_strerror | ||
34 | function returns an error message string corresponding to the error code | ||
35 | returned by | ||
36 | .Xr getaddrinfo 3 | ||
37 | or | ||
38 | .Xr getnameinfo 3 . | ||
39 | .Pp | ||
40 | The following error codes and their meaning are defined in | ||
41 | .In netdb.h : | ||
42 | .Pp | ||
43 | .Bl -tag -width "EAI_ADDRFAMILYXX" -offset indent -compact | ||
44 | .It Dv EAI_ADDRFAMILY | ||
45 | address family for | ||
46 | .Fa hostname | ||
47 | not supported | ||
48 | .It Dv EAI_AGAIN | ||
49 | temporary failure in name resolution | ||
50 | .It Dv EAI_BADFLAGS | ||
51 | invalid value for | ||
52 | .Fa ai_flags | ||
53 | .It Dv EAI_BADHINTS | ||
54 | invalid value for | ||
55 | .Fa hints | ||
56 | .It Dv EAI_FAIL | ||
57 | non-recoverable failure in name resolution | ||
58 | .It Dv EAI_FAMILY | ||
59 | .Fa ai_family | ||
60 | not supported. | ||
61 | .It Dv EAI_MEMORY | ||
62 | memory allocation failure | ||
63 | .It Dv EAI_NODATA | ||
64 | no address associated with | ||
65 | .Fa hostname | ||
66 | .It Dv EAI_NONAME | ||
67 | .Fa hostname | ||
68 | or | ||
69 | .Fa servname | ||
70 | not provided, or not known | ||
71 | .It Dv EAI_OVERFLOW | ||
72 | argument buffer overflow | ||
73 | .It Dv EAI_PROTOCOL | ||
74 | resolved protocol is unknown | ||
75 | .It Dv EAI_SERVICE | ||
76 | .Fa servname | ||
77 | not supported for | ||
78 | .Fa ai_socktype | ||
79 | .It Dv EAI_SOCKTYPE | ||
80 | .Fa ai_socktype | ||
81 | not supported | ||
82 | .It Dv EAI_SYSTEM | ||
83 | system error returned in | ||
84 | .Va errno | ||
85 | .El | ||
86 | .Sh RETURN VALUES | ||
87 | .Fn gai_strerror | ||
88 | returns a pointer to the error message string corresponding to | ||
89 | .Fa ecode . | ||
90 | If | ||
91 | .Fa ecode | ||
92 | is out of range, an implementation-specific error message string is returned. | ||
93 | .Sh SEE ALSO | ||
94 | .Xr getaddrinfo 3 , | ||
95 | .Xr getnameinfo 3 | ||
diff --git a/src/lib/libc/net/gai_strerror.c b/src/lib/libc/net/gai_strerror.c new file mode 100644 index 0000000000..f4126413f8 --- /dev/null +++ b/src/lib/libc/net/gai_strerror.c | |||
@@ -0,0 +1,78 @@ | |||
1 | /* $OpenBSD: gai_strerror.c,v 1.7 2009/06/02 16:47:50 jasper Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1997-1999, Craig Metz, All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. All advertising materials mentioning features or use of this software | ||
15 | * must display the following acknowledgement: | ||
16 | * This product includes software developed by Craig Metz and | ||
17 | * by other contributors. | ||
18 | * 4. Neither the name of the author nor the names of contributors | ||
19 | * may be used to endorse or promote products derived from this software | ||
20 | * without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | /* gai_strerror() v1.38 */ | ||
36 | |||
37 | #include <sys/types.h> | ||
38 | #include <netdb.h> | ||
39 | #include <errno.h> | ||
40 | |||
41 | const char * | ||
42 | gai_strerror(int errnum) | ||
43 | { | ||
44 | switch (errnum) { | ||
45 | case 0: | ||
46 | return "no error"; | ||
47 | case EAI_BADFLAGS: | ||
48 | return "invalid value for ai_flags"; | ||
49 | case EAI_NONAME: | ||
50 | return "name or service is not known"; | ||
51 | case EAI_AGAIN: | ||
52 | return "temporary failure in name resolution"; | ||
53 | case EAI_FAIL: | ||
54 | return "non-recoverable failure in name resolution"; | ||
55 | case EAI_NODATA: | ||
56 | return "no address associated with name"; | ||
57 | case EAI_FAMILY: | ||
58 | return "ai_family not supported"; | ||
59 | case EAI_SOCKTYPE: | ||
60 | return "ai_socktype not supported"; | ||
61 | case EAI_SERVICE: | ||
62 | return "service not supported for ai_socktype"; | ||
63 | case EAI_ADDRFAMILY: | ||
64 | return "address family for name not supported"; | ||
65 | case EAI_MEMORY: | ||
66 | return "memory allocation failure"; | ||
67 | case EAI_SYSTEM: | ||
68 | return "system error"; | ||
69 | case EAI_BADHINTS: | ||
70 | return "invalid value for hints"; | ||
71 | case EAI_PROTOCOL: | ||
72 | return "resolved protocol is unknown"; | ||
73 | case EAI_OVERFLOW: | ||
74 | return "argument buffer overflow"; | ||
75 | default: | ||
76 | return "unknown/invalid error"; | ||
77 | } | ||
78 | } | ||
diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3 new file mode 100644 index 0000000000..db4417ab35 --- /dev/null +++ b/src/lib/libc/net/getaddrinfo.3 | |||
@@ -0,0 +1,465 @@ | |||
1 | .\" $OpenBSD: getaddrinfo.3,v 1.54 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" $KAME: getaddrinfo.3,v 1.36 2005/01/05 03:23:05 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") | ||
5 | .\" Copyright (C) 2000, 2001 Internet Software Consortium. | ||
6 | .\" | ||
7 | .\" Permission to use, copy, modify, and distribute this software for any | ||
8 | .\" purpose with or without fee is hereby granted, provided that the above | ||
9 | .\" copyright notice and this permission notice appear in all copies. | ||
10 | .\" | ||
11 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH | ||
12 | .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
13 | .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
14 | .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
15 | .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE | ||
16 | .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
17 | .\" PERFORMANCE OF THIS SOFTWARE. | ||
18 | .\" | ||
19 | .Dd $Mdocdate: January 21 2014 $ | ||
20 | .Dt GETADDRINFO 3 | ||
21 | .Os | ||
22 | .Sh NAME | ||
23 | .Nm getaddrinfo , | ||
24 | .Nm freeaddrinfo | ||
25 | .Nd host and service name to socket address structure | ||
26 | .Sh SYNOPSIS | ||
27 | .In sys/types.h | ||
28 | .In sys/socket.h | ||
29 | .In netdb.h | ||
30 | .Ft int | ||
31 | .Fn getaddrinfo "const char *hostname" "const char *servname" \ | ||
32 | "const struct addrinfo *hints" "struct addrinfo **res" | ||
33 | .Ft void | ||
34 | .Fn freeaddrinfo "struct addrinfo *ai" | ||
35 | .Sh DESCRIPTION | ||
36 | The | ||
37 | .Fn getaddrinfo | ||
38 | function is used to get a list of | ||
39 | .Tn IP | ||
40 | addresses and port numbers for host | ||
41 | .Fa hostname | ||
42 | and service | ||
43 | .Fa servname . | ||
44 | It is a replacement for and provides more flexibility than the | ||
45 | .Xr gethostbyname 3 | ||
46 | and | ||
47 | .Xr getservbyname 3 | ||
48 | functions. | ||
49 | .Pp | ||
50 | The | ||
51 | .Fa hostname | ||
52 | and | ||
53 | .Fa servname | ||
54 | arguments are either pointers to NUL-terminated strings or the null pointer. | ||
55 | An acceptable value for | ||
56 | .Fa hostname | ||
57 | is either a valid host name or a numeric host address string consisting | ||
58 | of a dotted decimal IPv4 address or an IPv6 address. | ||
59 | The | ||
60 | .Fa servname | ||
61 | is either a decimal port number or a service name listed in | ||
62 | .Xr services 5 . | ||
63 | At least one of | ||
64 | .Fa hostname | ||
65 | and | ||
66 | .Fa servname | ||
67 | must be non-null. | ||
68 | .Pp | ||
69 | .Fa hints | ||
70 | is an optional pointer to a | ||
71 | .Li struct addrinfo , | ||
72 | as defined by | ||
73 | .In netdb.h : | ||
74 | .Bd -literal | ||
75 | struct addrinfo { | ||
76 | int ai_flags; /* input flags */ | ||
77 | int ai_family; /* protocol family for socket */ | ||
78 | int ai_socktype; /* socket type */ | ||
79 | int ai_protocol; /* protocol for socket */ | ||
80 | socklen_t ai_addrlen; /* length of socket-address */ | ||
81 | struct sockaddr *ai_addr; /* socket-address for socket */ | ||
82 | char *ai_canonname; /* canonical name for service location */ | ||
83 | struct addrinfo *ai_next; /* pointer to next in list */ | ||
84 | }; | ||
85 | .Ed | ||
86 | .Pp | ||
87 | This structure can be used to provide hints concerning the type of socket | ||
88 | that the caller supports or wishes to use. | ||
89 | The caller can supply the following structure elements in | ||
90 | .Fa hints : | ||
91 | .Bl -tag -width "ai_socktypeXX" | ||
92 | .It Fa ai_family | ||
93 | The protocol family that should be used. | ||
94 | When | ||
95 | .Fa ai_family | ||
96 | is set to | ||
97 | .Dv PF_UNSPEC , | ||
98 | it means the caller will accept any protocol family supported by the | ||
99 | operating system. | ||
100 | .It Fa ai_socktype | ||
101 | Denotes the type of socket that is wanted: | ||
102 | .Dv SOCK_STREAM , | ||
103 | .Dv SOCK_DGRAM , | ||
104 | or | ||
105 | .Dv SOCK_RAW . | ||
106 | When | ||
107 | .Fa ai_socktype | ||
108 | is zero the caller will accept any socket type. | ||
109 | .It Fa ai_protocol | ||
110 | Indicates which transport protocol is desired, | ||
111 | .Dv IPPROTO_UDP | ||
112 | or | ||
113 | .Dv IPPROTO_TCP . | ||
114 | If | ||
115 | .Fa ai_protocol | ||
116 | is zero the caller will accept any protocol. | ||
117 | .It Fa ai_flags | ||
118 | .Fa ai_flags | ||
119 | is formed by | ||
120 | .Tn OR Ns 'ing | ||
121 | the following values: | ||
122 | .Bl -tag -width "AI_CANONNAMEXX" | ||
123 | .It Dv AI_CANONNAME | ||
124 | If the | ||
125 | .Dv AI_CANONNAME | ||
126 | bit is set, a successful call to | ||
127 | .Fn getaddrinfo | ||
128 | will return a NUL-terminated string containing the canonical name | ||
129 | of the specified host name in the | ||
130 | .Fa ai_canonname | ||
131 | element of the first | ||
132 | .Li addrinfo | ||
133 | structure returned. | ||
134 | .It Dv AI_FQDN | ||
135 | If the | ||
136 | .Dv AI_FQDN | ||
137 | bit is set, a successful call to | ||
138 | .Fn getaddrinfo | ||
139 | will return a NUL-terminated string containing the fully qualified domain name | ||
140 | of the specified host name in the | ||
141 | .Fa ai_canonname | ||
142 | element of the first | ||
143 | .Li addrinfo | ||
144 | structure returned. | ||
145 | .Pp | ||
146 | This is different from the | ||
147 | .Dv AI_CANONNAME | ||
148 | bit flag that returns the canonical name registered in DNS, | ||
149 | which may be different from the fully qualified domain name | ||
150 | that the host name resolved to. | ||
151 | Only one of the | ||
152 | .Dv AI_FQDN | ||
153 | and | ||
154 | .Dv AI_CANONNAME | ||
155 | bits can be set. | ||
156 | .It Dv AI_NUMERICHOST | ||
157 | If the | ||
158 | .Dv AI_NUMERICHOST | ||
159 | bit is set, it indicates that | ||
160 | .Fa hostname | ||
161 | should be treated as a numeric string defining an IPv4 or IPv6 address | ||
162 | and no name resolution should be attempted. | ||
163 | .It Dv AI_NUMERICSERV | ||
164 | If the | ||
165 | .Dv AI_NUMERICSERV | ||
166 | bit is set, it indicates that | ||
167 | .Fa servname | ||
168 | should be treated as a numeric port string | ||
169 | and no service name resolution should be attempted. | ||
170 | .It Dv AI_PASSIVE | ||
171 | If the | ||
172 | .Dv AI_PASSIVE | ||
173 | bit is set it indicates that the returned socket address structure | ||
174 | is intended for use in a call to | ||
175 | .Xr bind 2 . | ||
176 | In this case, if the | ||
177 | .Fa hostname | ||
178 | argument is the null pointer, then the IP address portion of the | ||
179 | socket address structure will be set to | ||
180 | .Dv INADDR_ANY | ||
181 | for an IPv4 address or | ||
182 | .Dv IN6ADDR_ANY_INIT | ||
183 | for an IPv6 address. | ||
184 | .Pp | ||
185 | If the | ||
186 | .Dv AI_PASSIVE | ||
187 | bit is not set, the returned socket address structure will be ready | ||
188 | for use in a call to | ||
189 | .Xr connect 2 | ||
190 | for a connection-oriented protocol or | ||
191 | .Xr connect 2 , | ||
192 | .Xr sendto 2 , | ||
193 | or | ||
194 | .Xr sendmsg 2 | ||
195 | if a connectionless protocol was chosen. | ||
196 | The | ||
197 | .Tn IP | ||
198 | address portion of the socket address structure will be set to the | ||
199 | loopback address if | ||
200 | .Fa hostname | ||
201 | is the null pointer and | ||
202 | .Dv AI_PASSIVE | ||
203 | is not set. | ||
204 | .El | ||
205 | .El | ||
206 | .Pp | ||
207 | All other elements of the | ||
208 | .Li addrinfo | ||
209 | structure passed via | ||
210 | .Fa hints | ||
211 | must be zero or the null pointer. | ||
212 | .Pp | ||
213 | If | ||
214 | .Fa hints | ||
215 | is the null pointer, | ||
216 | .Fn getaddrinfo | ||
217 | behaves as if the caller provided a | ||
218 | .Li struct addrinfo | ||
219 | with | ||
220 | .Fa ai_family | ||
221 | set to | ||
222 | .Dv PF_UNSPEC | ||
223 | and all other elements set to zero or | ||
224 | .Dv NULL . | ||
225 | .Pp | ||
226 | After a successful call to | ||
227 | .Fn getaddrinfo , | ||
228 | .Fa *res | ||
229 | is a pointer to a linked list of one or more | ||
230 | .Li addrinfo | ||
231 | structures. | ||
232 | The list can be traversed by following the | ||
233 | .Fa ai_next | ||
234 | pointer in each | ||
235 | .Li addrinfo | ||
236 | structure until a null pointer is encountered. | ||
237 | The three members | ||
238 | .Fa ai_family , | ||
239 | .Fa ai_socktype , | ||
240 | and | ||
241 | .Fa ai_protocol | ||
242 | in each returned | ||
243 | .Li addrinfo | ||
244 | structure are suitable for a call to | ||
245 | .Xr socket 2 . | ||
246 | For each | ||
247 | .Li addrinfo | ||
248 | structure in the list, the | ||
249 | .Fa ai_addr | ||
250 | member points to a filled-in socket address structure of length | ||
251 | .Fa ai_addrlen . | ||
252 | .Pp | ||
253 | This implementation of | ||
254 | .Fn getaddrinfo | ||
255 | allows numeric IPv6 address notation with scope identifier, | ||
256 | as documented in RFC 4007. | ||
257 | By appending the percent character and scope identifier to addresses, | ||
258 | one can fill the | ||
259 | .Li sin6_scope_id | ||
260 | field for addresses. | ||
261 | This would make management of scoped addresses easier | ||
262 | and allows cut-and-paste input of scoped addresses. | ||
263 | .Pp | ||
264 | At this moment the code supports only link-local addresses with the format. | ||
265 | The scope identifier is hardcoded to the name of the hardware interface | ||
266 | associated | ||
267 | with the link | ||
268 | .Po | ||
269 | such as | ||
270 | .Li ne0 | ||
271 | .Pc . | ||
272 | An example is | ||
273 | .Dq Li fe80::1%ne0 , | ||
274 | which means | ||
275 | .Do | ||
276 | .Li fe80::1 | ||
277 | on the link associated with the | ||
278 | .Li ne0 | ||
279 | interface | ||
280 | .Dc . | ||
281 | .Pp | ||
282 | The current implementation assumes a one-to-one relationship between | ||
283 | the interface and link, which is not necessarily true from the specification. | ||
284 | .Pp | ||
285 | All of the information returned by | ||
286 | .Fn getaddrinfo | ||
287 | is dynamically allocated: the | ||
288 | .Li addrinfo | ||
289 | structures themselves as well as the socket address structures and | ||
290 | the canonical host name strings included in the | ||
291 | .Li addrinfo | ||
292 | structures. | ||
293 | .Pp | ||
294 | Memory allocated for the dynamically allocated structures created by | ||
295 | a successful call to | ||
296 | .Fn getaddrinfo | ||
297 | is released by the | ||
298 | .Fn freeaddrinfo | ||
299 | function. | ||
300 | The | ||
301 | .Fa ai | ||
302 | pointer should be an | ||
303 | .Li addrinfo | ||
304 | structure created by a call to | ||
305 | .Fn getaddrinfo . | ||
306 | .Sh RETURN VALUES | ||
307 | .Fn getaddrinfo | ||
308 | returns zero on success or one of the error codes listed in | ||
309 | .Xr gai_strerror 3 | ||
310 | if an error occurs. | ||
311 | If an error occurs, no memory is allocated by | ||
312 | .Fn getaddrinfo , | ||
313 | therefore it is not necessary to release the | ||
314 | .Li addrinfo | ||
315 | structure(s). | ||
316 | .Sh EXAMPLES | ||
317 | The following code tries to connect to | ||
318 | .Dq Li www.kame.net | ||
319 | service | ||
320 | .Dq Li www | ||
321 | via a stream socket. | ||
322 | It loops through all the addresses available, regardless of address family. | ||
323 | If the destination resolves to an IPv4 address, it will use an | ||
324 | .Dv AF_INET | ||
325 | socket. | ||
326 | Similarly, if it resolves to IPv6, an | ||
327 | .Dv AF_INET6 | ||
328 | socket is used. | ||
329 | Observe that there is no hardcoded reference to a particular address family. | ||
330 | The code works even if | ||
331 | .Fn getaddrinfo | ||
332 | returns addresses that are not IPv4/v6. | ||
333 | .Bd -literal -offset indent | ||
334 | struct addrinfo hints, *res, *res0; | ||
335 | int error; | ||
336 | int save_errno; | ||
337 | int s; | ||
338 | const char *cause = NULL; | ||
339 | |||
340 | memset(&hints, 0, sizeof(hints)); | ||
341 | hints.ai_family = PF_UNSPEC; | ||
342 | hints.ai_socktype = SOCK_STREAM; | ||
343 | error = getaddrinfo("www.kame.net", "www", &hints, &res0); | ||
344 | if (error) | ||
345 | errx(1, "%s", gai_strerror(error)); | ||
346 | s = -1; | ||
347 | for (res = res0; res; res = res->ai_next) { | ||
348 | s = socket(res->ai_family, res->ai_socktype, | ||
349 | res->ai_protocol); | ||
350 | if (s == -1) { | ||
351 | cause = "socket"; | ||
352 | continue; | ||
353 | } | ||
354 | |||
355 | if (connect(s, res->ai_addr, res->ai_addrlen) == -1) { | ||
356 | cause = "connect"; | ||
357 | save_errno = errno; | ||
358 | close(s); | ||
359 | errno = save_errno; | ||
360 | s = -1; | ||
361 | continue; | ||
362 | } | ||
363 | |||
364 | break; /* okay we got one */ | ||
365 | } | ||
366 | if (s == -1) | ||
367 | err(1, "%s", cause); | ||
368 | freeaddrinfo(res0); | ||
369 | .Ed | ||
370 | .Pp | ||
371 | The following example tries to open a wildcard listening socket onto service | ||
372 | .Dq Li www , | ||
373 | for all the address families available. | ||
374 | .Bd -literal -offset indent | ||
375 | struct addrinfo hints, *res, *res0; | ||
376 | int error; | ||
377 | int save_errno; | ||
378 | int s[MAXSOCK]; | ||
379 | int nsock; | ||
380 | const char *cause = NULL; | ||
381 | |||
382 | memset(&hints, 0, sizeof(hints)); | ||
383 | hints.ai_family = PF_UNSPEC; | ||
384 | hints.ai_socktype = SOCK_STREAM; | ||
385 | hints.ai_flags = AI_PASSIVE; | ||
386 | error = getaddrinfo(NULL, "www", &hints, &res0); | ||
387 | if (error) | ||
388 | errx(1, "%s", gai_strerror(error)); | ||
389 | nsock = 0; | ||
390 | for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) { | ||
391 | s[nsock] = socket(res->ai_family, res->ai_socktype, | ||
392 | res->ai_protocol); | ||
393 | if (s[nsock] == -1) { | ||
394 | cause = "socket"; | ||
395 | continue; | ||
396 | } | ||
397 | |||
398 | if (bind(s[nsock], res->ai_addr, res->ai_addrlen) == -1) { | ||
399 | cause = "bind"; | ||
400 | save_errno = errno; | ||
401 | close(s[nsock]); | ||
402 | errno = save_errno; | ||
403 | continue; | ||
404 | } | ||
405 | (void) listen(s[nsock], 5); | ||
406 | |||
407 | nsock++; | ||
408 | } | ||
409 | if (nsock == 0) | ||
410 | err(1, "%s", cause); | ||
411 | freeaddrinfo(res0); | ||
412 | .Ed | ||
413 | .Sh SEE ALSO | ||
414 | .Xr bind 2 , | ||
415 | .Xr connect 2 , | ||
416 | .Xr send 2 , | ||
417 | .Xr socket 2 , | ||
418 | .Xr gai_strerror 3 , | ||
419 | .Xr gethostbyname 3 , | ||
420 | .Xr getnameinfo 3 , | ||
421 | .Xr getservbyname 3 , | ||
422 | .Xr resolver 3 , | ||
423 | .Xr hosts 5 , | ||
424 | .Xr resolv.conf 5 , | ||
425 | .Xr services 5 , | ||
426 | .Xr hostname 7 , | ||
427 | .Xr named 8 | ||
428 | .Rs | ||
429 | .%A Craig Metz | ||
430 | .%B Proceedings of the Freenix Track: 2000 USENIX Annual Technical Conference | ||
431 | .%D June 2000 | ||
432 | .%T Protocol Independence Using the Sockets API | ||
433 | .Re | ||
434 | .Sh STANDARDS | ||
435 | The | ||
436 | .Fn getaddrinfo | ||
437 | function is defined by the | ||
438 | .St -p1003.1g-2000 | ||
439 | draft specification and documented in RFC 3493. | ||
440 | .Pp | ||
441 | The | ||
442 | .Dv AI_FQDN | ||
443 | flag bit first appeared in Windows 7. | ||
444 | .Pp | ||
445 | .Rs | ||
446 | .%A R. Gilligan | ||
447 | .%A S. Thomson | ||
448 | .%A J. Bound | ||
449 | .%A J. McCann | ||
450 | .%A W. Stevens | ||
451 | .%D February 2003 | ||
452 | .%R RFC 3493 | ||
453 | .%T Basic Socket Interface Extensions for IPv6 | ||
454 | .Re | ||
455 | .Pp | ||
456 | .Rs | ||
457 | .%A S. Deering | ||
458 | .%A B. Haberman | ||
459 | .%A T. Jinmei | ||
460 | .%A E. Nordmark | ||
461 | .%A B. Zill | ||
462 | .%D March 2005 | ||
463 | .%R RFC 4007 | ||
464 | .%T IPv6 Scoped Address Architecture | ||
465 | .Re | ||
diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3 new file mode 100644 index 0000000000..206750a2c3 --- /dev/null +++ b/src/lib/libc/net/gethostbyname.3 | |||
@@ -0,0 +1,297 @@ | |||
1 | .\" $OpenBSD: gethostbyname.3,v 1.26 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1987, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: June 5 2013 $ | ||
31 | .Dt GETHOSTBYNAME 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm gethostbyname , | ||
35 | .Nm gethostbyname2 , | ||
36 | .Nm gethostbyaddr , | ||
37 | .Nm gethostent , | ||
38 | .Nm sethostent , | ||
39 | .Nm endhostent , | ||
40 | .Nm hstrerror , | ||
41 | .Nm herror | ||
42 | .Nd get network host entry | ||
43 | .Sh SYNOPSIS | ||
44 | .In netdb.h | ||
45 | .Vt extern int h_errno ; | ||
46 | .Ft struct hostent * | ||
47 | .Fn gethostbyname "const char *name" | ||
48 | .Ft struct hostent * | ||
49 | .Fn gethostbyname2 "const char *name" "int af" | ||
50 | .Ft struct hostent * | ||
51 | .Fn gethostbyaddr "const void *addr" "socklen_t len" "int af" | ||
52 | .Ft struct hostent * | ||
53 | .Fn gethostent void | ||
54 | .Ft void | ||
55 | .Fn sethostent "int stayopen" | ||
56 | .Ft void | ||
57 | .Fn endhostent void | ||
58 | .Ft void | ||
59 | .Fn herror "const char *string" | ||
60 | .Ft const char * | ||
61 | .Fn hstrerror "int err" | ||
62 | .Sh DESCRIPTION | ||
63 | The | ||
64 | .Fn gethostbyname , | ||
65 | .Fn gethostbyname2 , | ||
66 | and | ||
67 | .Fn gethostbyaddr | ||
68 | functions each return a pointer to an object with the following structure | ||
69 | describing an Internet host referenced by name or by address, respectively. | ||
70 | This structure contains either information obtained from the name server (i.e., | ||
71 | .Xr resolver 3 | ||
72 | and | ||
73 | .Xr named 8 ) , | ||
74 | broken-out fields from a line in | ||
75 | .Pa /etc/hosts , | ||
76 | or database entries supplied by the | ||
77 | .Xr yp 8 | ||
78 | system. | ||
79 | .Xr resolv.conf 5 | ||
80 | describes how the particular database is chosen. | ||
81 | .Bd -literal -offset indent | ||
82 | struct hostent { | ||
83 | char *h_name; /* official name of host */ | ||
84 | char **h_aliases; /* alias list */ | ||
85 | int h_addrtype; /* host address type */ | ||
86 | int h_length; /* length of address */ | ||
87 | char **h_addr_list; /* list of returned addresses */ | ||
88 | }; | ||
89 | #define h_addr h_addr_list[0] /* address, for backward compat */ | ||
90 | .Ed | ||
91 | .Pp | ||
92 | The members of this structure are: | ||
93 | .Bl -tag -width h_addr_list | ||
94 | .It Fa h_name | ||
95 | Official name of the host. | ||
96 | .It Fa h_aliases | ||
97 | A null-terminated array of alternate names for the host. | ||
98 | .It Fa h_addrtype | ||
99 | The type of address being returned. | ||
100 | .It Fa h_length | ||
101 | The length, in bytes, of the address. | ||
102 | .It Fa h_addr_list | ||
103 | A null-terminated array of network addresses for the host. | ||
104 | Host addresses are returned in network byte order. | ||
105 | .It Fa h_addr | ||
106 | The first address in | ||
107 | .Fa h_addr_list ; | ||
108 | this is for backward compatibility. | ||
109 | .El | ||
110 | .Pp | ||
111 | The function | ||
112 | .Fn gethostbyname | ||
113 | will search for the named host in the current domain and its parents | ||
114 | using the search lookup semantics detailed in | ||
115 | .Xr resolv.conf 5 | ||
116 | and | ||
117 | .Xr hostname 7 . | ||
118 | .Pp | ||
119 | .Fn gethostbyname2 | ||
120 | is an advanced form of | ||
121 | .Fn gethostbyname | ||
122 | which allows lookups in address families other than | ||
123 | .Dv AF_INET . | ||
124 | Currently, the only supported address family besides | ||
125 | .Dv AF_INET | ||
126 | is | ||
127 | .Dv AF_INET6 . | ||
128 | .Pp | ||
129 | The | ||
130 | .Fn gethostbyaddr | ||
131 | function will search for the specified address of length | ||
132 | .Fa len | ||
133 | in the address family | ||
134 | .Fa af . | ||
135 | The only address family currently supported is | ||
136 | .Dv AF_INET . | ||
137 | .Pp | ||
138 | The | ||
139 | .Fn sethostent | ||
140 | function may be used to request the use of a connected | ||
141 | .Tn TCP | ||
142 | socket for queries. | ||
143 | If the | ||
144 | .Fa stayopen | ||
145 | flag is non-zero, | ||
146 | this sets the option to send all queries to the name server using | ||
147 | .Tn TCP | ||
148 | and to retain the connection after each call to | ||
149 | .Fn gethostbyname | ||
150 | or | ||
151 | .Fn gethostbyaddr . | ||
152 | Otherwise, queries are performed using | ||
153 | .Tn UDP | ||
154 | datagrams. | ||
155 | .Pp | ||
156 | The | ||
157 | .Fn endhostent | ||
158 | function closes the | ||
159 | .Tn TCP | ||
160 | connection. | ||
161 | .Pp | ||
162 | The | ||
163 | .Fn herror | ||
164 | function prints an error message describing the failure. | ||
165 | If its argument | ||
166 | .Fa string | ||
167 | is non-null, | ||
168 | it is prepended to the message string and separated from it by a colon | ||
169 | .Pq Ql \&: | ||
170 | and a space. | ||
171 | The error message is printed with a trailing newline. | ||
172 | The contents of the error message is the same as that returned by | ||
173 | .Fn hstrerror | ||
174 | with argument | ||
175 | .Fa h_errno . | ||
176 | .Sh ENVIRONMENT | ||
177 | .Bl -tag -width HOSTALIASES | ||
178 | .It HOSTALIASES | ||
179 | A file containing local host aliases. | ||
180 | See | ||
181 | .Xr hostname 7 | ||
182 | for more information. | ||
183 | .It RES_OPTIONS | ||
184 | A list of options to override the resolver's internal defaults. | ||
185 | See | ||
186 | .Xr resolver 3 | ||
187 | for more information. | ||
188 | .El | ||
189 | .Sh FILES | ||
190 | .Bl -tag -width /etc/resolv.conf -compact | ||
191 | .It Pa /etc/hosts | ||
192 | .It Pa /etc/resolv.conf | ||
193 | .El | ||
194 | .Sh DIAGNOSTICS | ||
195 | Error return status from | ||
196 | .Fn gethostbyname , | ||
197 | .Fn gethostbyname2 , | ||
198 | and | ||
199 | .Fn gethostbyaddr | ||
200 | is indicated by return of a null pointer. | ||
201 | The external integer | ||
202 | .Va h_errno | ||
203 | may then be checked to see whether this is a temporary failure | ||
204 | or an invalid or unknown host. | ||
205 | .Pp | ||
206 | The variable | ||
207 | .Va h_errno | ||
208 | can have the following values: | ||
209 | .Bl -tag -width HOST_NOT_FOUND | ||
210 | .It Dv HOST_NOT_FOUND | ||
211 | No such host is known. | ||
212 | .It Dv TRY_AGAIN | ||
213 | This is usually a temporary error | ||
214 | and means that the local server did not receive | ||
215 | a response from an authoritative server. | ||
216 | A retry at some later time may succeed. | ||
217 | .It Dv NO_RECOVERY | ||
218 | Some unexpected server failure was encountered. | ||
219 | This is a non-recoverable error. | ||
220 | .It Dv NO_DATA | ||
221 | The requested name is valid but does not have an IP address; | ||
222 | this is not a temporary error. | ||
223 | This means that the name is known to the name server but there is no address | ||
224 | associated with this name. | ||
225 | Another type of request to the name server using this domain name | ||
226 | will result in an answer; | ||
227 | for example, a mail-forwarder may be registered for this domain. | ||
228 | .It Dv NETDB_INTERNAL | ||
229 | An internal error occurred. | ||
230 | This may occur when an address family other than | ||
231 | .Dv AF_INET | ||
232 | or | ||
233 | .Dv AF_INET6 | ||
234 | is specified or when a resource is unable to be allocated. | ||
235 | .It Dv NETDB_SUCCESS | ||
236 | The function completed successfully. | ||
237 | .El | ||
238 | .Sh SEE ALSO | ||
239 | .Xr getaddrinfo 3 , | ||
240 | .Xr getnameinfo 3 , | ||
241 | .Xr resolver 3 , | ||
242 | .Xr hosts 5 , | ||
243 | .Xr resolv.conf 5 , | ||
244 | .Xr hostname 7 , | ||
245 | .Xr named 8 | ||
246 | .Sh HISTORY | ||
247 | The | ||
248 | .Fn herror | ||
249 | function appeared in | ||
250 | .Bx 4.3 . | ||
251 | The | ||
252 | .Fn endhostent , | ||
253 | .Fn gethostbyaddr , | ||
254 | .Fn gethostbyname , | ||
255 | .Fn gethostent , | ||
256 | and | ||
257 | .Fn sethostent | ||
258 | functions appeared in | ||
259 | .Bx 4.2 . | ||
260 | .Sh CAVEATS | ||
261 | If the search routines in | ||
262 | .Xr resolv.conf 5 | ||
263 | decide to read the | ||
264 | .Pa /etc/hosts | ||
265 | file, | ||
266 | .Fn gethostent | ||
267 | and other functions will | ||
268 | read the next line of the file, | ||
269 | re-opening the file if necessary. | ||
270 | .Pp | ||
271 | The | ||
272 | .Fn sethostent | ||
273 | function opens and/or rewinds the file | ||
274 | .Pa /etc/hosts . | ||
275 | If the | ||
276 | .Fa stayopen | ||
277 | argument is non-zero, the file will not be closed after each call to | ||
278 | .Fn gethostbyname , | ||
279 | .Fn gethostbyname2 , | ||
280 | or | ||
281 | .Fn gethostbyaddr . | ||
282 | .Pp | ||
283 | The | ||
284 | .Fn endhostent | ||
285 | function closes the file. | ||
286 | .Sh BUGS | ||
287 | These functions use static data storage; | ||
288 | if the data is needed for future use, it should be | ||
289 | copied before any subsequent calls overwrite it. | ||
290 | .Pp | ||
291 | Only the Internet | ||
292 | address formats are currently understood. | ||
293 | .Pp | ||
294 | YP does not support any address families other than | ||
295 | .Dv AF_INET | ||
296 | and uses | ||
297 | the traditional database format. | ||
diff --git a/src/lib/libc/net/getifaddrs.3 b/src/lib/libc/net/getifaddrs.3 new file mode 100644 index 0000000000..0b3538302a --- /dev/null +++ b/src/lib/libc/net/getifaddrs.3 | |||
@@ -0,0 +1,160 @@ | |||
1 | .\" $OpenBSD: getifaddrs.3,v 1.19 2014/04/07 17:57:56 schwarze Exp $ | ||
2 | .\" BSDI getifaddrs.3,v 2.5 2000/02/23 14:51:59 dab Exp | ||
3 | .\" | ||
4 | .\" Copyright (c) 1995, 1999 | ||
5 | .\" Berkeley Software Design, Inc. All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" | ||
13 | .\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
14 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
15 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
16 | .\" ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
17 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
18 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
19 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
20 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
21 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
22 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
23 | .\" SUCH DAMAGE. | ||
24 | .Dd $Mdocdate: April 7 2014 $ | ||
25 | .Dt GETIFADDRS 3 | ||
26 | .Os | ||
27 | .Sh NAME | ||
28 | .Nm getifaddrs , | ||
29 | .Nm freeifaddrs | ||
30 | .Nd get interface addresses | ||
31 | .Sh SYNOPSIS | ||
32 | .In sys/types.h | ||
33 | .In sys/socket.h | ||
34 | .In ifaddrs.h | ||
35 | .Ft int | ||
36 | .Fn getifaddrs "struct ifaddrs **ifap" | ||
37 | .Ft void | ||
38 | .Fn freeifaddrs "struct ifaddrs *ifap" | ||
39 | .Sh DESCRIPTION | ||
40 | The | ||
41 | .Fn getifaddrs | ||
42 | function stores a reference to a linked list of the network interfaces | ||
43 | on the local machine in the memory referenced by | ||
44 | .Fa ifap . | ||
45 | The list consists of | ||
46 | .Nm ifaddrs | ||
47 | structures, as defined in the include file | ||
48 | .In ifaddrs.h . | ||
49 | The | ||
50 | .Nm ifaddrs | ||
51 | structure contains at least the following entries: | ||
52 | .Bd -literal | ||
53 | struct ifaddrs *ifa_next; /* Pointer to next struct */ | ||
54 | char *ifa_name; /* Interface name */ | ||
55 | u_int ifa_flags; /* Interface flags */ | ||
56 | struct sockaddr *ifa_addr; /* Interface address */ | ||
57 | struct sockaddr *ifa_netmask; /* Interface netmask */ | ||
58 | struct sockaddr *ifa_broadaddr; /* Interface broadcast address */ | ||
59 | struct sockaddr *ifa_dstaddr; /* P2P interface destination */ | ||
60 | void *ifa_data; /* Address specific data */ | ||
61 | .Ed | ||
62 | .Bl -tag -width ifa_broadaddr | ||
63 | .It Fa ifa_next | ||
64 | Contains a pointer to the next structure on the list. | ||
65 | This field is set to | ||
66 | .Dv NULL | ||
67 | in the last structure on the list. | ||
68 | .It Fa ifa_name | ||
69 | Contains the interface name. | ||
70 | .It Fa ifa_flags | ||
71 | Contains the interface flags, as set by | ||
72 | .Xr ifconfig 8 . | ||
73 | .It Fa ifa_addr | ||
74 | References either the address of the interface or the link level | ||
75 | address of the interface, if one exists, otherwise it is | ||
76 | .Dv NULL . | ||
77 | (The | ||
78 | .Fa sa_family | ||
79 | field of the | ||
80 | .Fa ifa_addr | ||
81 | field should be consulted to determine the format of the | ||
82 | .Fa ifa_addr | ||
83 | address.) | ||
84 | .It Fa ifa_netmask | ||
85 | References the netmask associated with | ||
86 | .Fa ifa_addr , | ||
87 | if one is set, otherwise it is | ||
88 | .Dv NULL . | ||
89 | .It Fa ifa_broadaddr | ||
90 | This field, which should only be referenced for non-P2P interfaces, | ||
91 | references the broadcast address associated with | ||
92 | .Fa ifa_addr , | ||
93 | if one exists, otherwise it is | ||
94 | .Dv NULL . | ||
95 | .It Fa ifa_dstaddr | ||
96 | This field, which should only be referenced for P2P interfaces, | ||
97 | references the destination address on a P2P interface, | ||
98 | if one exists, otherwise it is | ||
99 | .Dv NULL . | ||
100 | .It Fa ifa_data | ||
101 | References address family specific data. | ||
102 | For | ||
103 | .Dv AF_LINK | ||
104 | addresses it contains a pointer to the | ||
105 | .Li struct if_data | ||
106 | (as defined in include file | ||
107 | .In net/if.h ) | ||
108 | which contains various interface attributes and statistics. | ||
109 | For all other address families, | ||
110 | .Fa ifa_data | ||
111 | is | ||
112 | .Dv NULL . | ||
113 | .El | ||
114 | .Pp | ||
115 | The data returned by | ||
116 | .Fn getifaddrs | ||
117 | is dynamically allocated and should be freed using | ||
118 | .Fn freeifaddrs | ||
119 | when no longer needed. | ||
120 | .Sh RETURN VALUES | ||
121 | Upon successful completion, a value of 0 is returned. | ||
122 | Otherwise, a value of \-1 is returned and | ||
123 | .Va errno | ||
124 | is set to indicate the error. | ||
125 | .Sh ERRORS | ||
126 | The | ||
127 | .Fn getifaddrs | ||
128 | may fail and set | ||
129 | .Va errno | ||
130 | for any of the errors specified for the library routines | ||
131 | .Xr ioctl 2 , | ||
132 | .Xr socket 2 , | ||
133 | .Xr malloc 3 , | ||
134 | or | ||
135 | .Xr sysctl 3 . | ||
136 | .Sh SEE ALSO | ||
137 | .Xr ioctl 2 , | ||
138 | .Xr socket 2 , | ||
139 | .Xr sysctl 3 , | ||
140 | .Xr networking 4 , | ||
141 | .Xr ifconfig 8 | ||
142 | .Sh HISTORY | ||
143 | The | ||
144 | .Fn getifaddrs | ||
145 | function first appeared in BSDI | ||
146 | .Bsx . | ||
147 | The function has been available on | ||
148 | .Ox | ||
149 | since | ||
150 | .Ox 2.7 . | ||
151 | .Sh BUGS | ||
152 | If both | ||
153 | .In net/if.h | ||
154 | and | ||
155 | .In ifaddrs.h | ||
156 | are being included, | ||
157 | .In net/if.h | ||
158 | .Em must | ||
159 | be included before | ||
160 | .In ifaddrs.h . | ||
diff --git a/src/lib/libc/net/getifaddrs.c b/src/lib/libc/net/getifaddrs.c new file mode 100644 index 0000000000..da42a23783 --- /dev/null +++ b/src/lib/libc/net/getifaddrs.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /* $OpenBSD: getifaddrs.c,v 1.11 2013/03/20 14:15:56 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1995, 1999 | ||
5 | * Berkeley Software Design, Inc. All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * | ||
13 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
16 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
23 | * SUCH DAMAGE. | ||
24 | * | ||
25 | * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp | ||
26 | */ | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/ioctl.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if.h> | ||
32 | #include <sys/param.h> | ||
33 | #include <net/route.h> | ||
34 | #include <sys/sysctl.h> | ||
35 | #include <net/if_dl.h> | ||
36 | |||
37 | #include <errno.h> | ||
38 | #include <ifaddrs.h> | ||
39 | #include <stddef.h> | ||
40 | #include <stdlib.h> | ||
41 | #include <string.h> | ||
42 | #include <unistd.h> | ||
43 | |||
44 | #define SALIGN (sizeof(long) - 1) | ||
45 | #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1)) | ||
46 | |||
47 | int | ||
48 | getifaddrs(struct ifaddrs **pif) | ||
49 | { | ||
50 | int icnt = 1; | ||
51 | int dcnt = 0; | ||
52 | int ncnt = 0; | ||
53 | int mib[6]; | ||
54 | size_t needed; | ||
55 | char *buf = NULL, *bufp; | ||
56 | char *next; | ||
57 | struct ifaddrs *cif = 0; | ||
58 | char *p, *p0; | ||
59 | struct rt_msghdr *rtm; | ||
60 | struct if_msghdr *ifm; | ||
61 | struct ifa_msghdr *ifam; | ||
62 | struct sockaddr_dl *dl; | ||
63 | struct sockaddr *sa; | ||
64 | u_short index = 0; | ||
65 | size_t len, alen, dlen; | ||
66 | struct ifaddrs *ifa, *ift; | ||
67 | int i; | ||
68 | char *data; | ||
69 | char *names; | ||
70 | |||
71 | mib[0] = CTL_NET; | ||
72 | mib[1] = PF_ROUTE; | ||
73 | mib[2] = 0; /* protocol */ | ||
74 | mib[3] = 0; /* wildcard address family */ | ||
75 | mib[4] = NET_RT_IFLIST; | ||
76 | mib[5] = 0; /* no flags */ | ||
77 | while (1) { | ||
78 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) { | ||
79 | free(buf); | ||
80 | return (-1); | ||
81 | } | ||
82 | if (needed == 0) | ||
83 | break; | ||
84 | if ((bufp = realloc(buf, needed)) == NULL) { | ||
85 | free(buf); | ||
86 | return (-1); | ||
87 | } | ||
88 | buf = bufp; | ||
89 | if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) { | ||
90 | if (errno == ENOMEM) | ||
91 | continue; | ||
92 | free(buf); | ||
93 | return (-1); | ||
94 | } | ||
95 | break; | ||
96 | } | ||
97 | |||
98 | for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { | ||
99 | rtm = (struct rt_msghdr *)next; | ||
100 | if (rtm->rtm_version != RTM_VERSION) | ||
101 | continue; | ||
102 | switch (rtm->rtm_type) { | ||
103 | case RTM_IFINFO: | ||
104 | ifm = (struct if_msghdr *)rtm; | ||
105 | if (ifm->ifm_addrs & RTA_IFP) { | ||
106 | index = ifm->ifm_index; | ||
107 | ++icnt; | ||
108 | dl = (struct sockaddr_dl *)(next + | ||
109 | rtm->rtm_hdrlen); | ||
110 | dcnt += SA_RLEN((struct sockaddr *)dl) + | ||
111 | ALIGNBYTES; | ||
112 | dcnt += sizeof(ifm->ifm_data); | ||
113 | ncnt += dl->sdl_nlen + 1; | ||
114 | } else | ||
115 | index = 0; | ||
116 | break; | ||
117 | |||
118 | case RTM_NEWADDR: | ||
119 | ifam = (struct ifa_msghdr *)rtm; | ||
120 | if (index && ifam->ifam_index != index) | ||
121 | abort(); /* XXX abort illegal in library */ | ||
122 | |||
123 | #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD) | ||
124 | if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) | ||
125 | break; | ||
126 | p = next + rtm->rtm_hdrlen; | ||
127 | ++icnt; | ||
128 | /* Scan to look for length of address */ | ||
129 | alen = 0; | ||
130 | for (p0 = p, i = 0; i < RTAX_MAX; i++) { | ||
131 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
132 | == 0) | ||
133 | continue; | ||
134 | sa = (struct sockaddr *)p; | ||
135 | len = SA_RLEN(sa); | ||
136 | if (i == RTAX_IFA) { | ||
137 | alen = len; | ||
138 | break; | ||
139 | } | ||
140 | p += len; | ||
141 | } | ||
142 | for (p = p0, i = 0; i < RTAX_MAX; i++) { | ||
143 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
144 | == 0) | ||
145 | continue; | ||
146 | sa = (struct sockaddr *)p; | ||
147 | len = SA_RLEN(sa); | ||
148 | if (i == RTAX_NETMASK && sa->sa_len == 0) | ||
149 | dcnt += alen; | ||
150 | else | ||
151 | dcnt += len; | ||
152 | p += len; | ||
153 | } | ||
154 | break; | ||
155 | } | ||
156 | } | ||
157 | |||
158 | if (icnt + dcnt + ncnt == 1) { | ||
159 | *pif = NULL; | ||
160 | free(buf); | ||
161 | return (0); | ||
162 | } | ||
163 | data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt); | ||
164 | if (data == NULL) { | ||
165 | free(buf); | ||
166 | return(-1); | ||
167 | } | ||
168 | |||
169 | ifa = (struct ifaddrs *)data; | ||
170 | data += sizeof(struct ifaddrs) * icnt; | ||
171 | names = data + dcnt; | ||
172 | |||
173 | memset(ifa, 0, sizeof(struct ifaddrs) * icnt); | ||
174 | ift = ifa; | ||
175 | |||
176 | index = 0; | ||
177 | for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { | ||
178 | rtm = (struct rt_msghdr *)next; | ||
179 | if (rtm->rtm_version != RTM_VERSION) | ||
180 | continue; | ||
181 | switch (rtm->rtm_type) { | ||
182 | case RTM_IFINFO: | ||
183 | ifm = (struct if_msghdr *)rtm; | ||
184 | if (ifm->ifm_addrs & RTA_IFP) { | ||
185 | index = ifm->ifm_index; | ||
186 | dl = (struct sockaddr_dl *)(next + | ||
187 | rtm->rtm_hdrlen); | ||
188 | |||
189 | cif = ift; | ||
190 | ift->ifa_name = names; | ||
191 | ift->ifa_flags = (int)ifm->ifm_flags; | ||
192 | memcpy(names, dl->sdl_data, dl->sdl_nlen); | ||
193 | names[dl->sdl_nlen] = 0; | ||
194 | names += dl->sdl_nlen + 1; | ||
195 | |||
196 | ift->ifa_addr = (struct sockaddr *)data; | ||
197 | memcpy(data, dl, | ||
198 | ((struct sockaddr *)dl)->sa_len); | ||
199 | data += SA_RLEN((struct sockaddr *)dl); | ||
200 | |||
201 | /* ifm_data needs to be aligned */ | ||
202 | ift->ifa_data = data = (void *)ALIGN(data); | ||
203 | dlen = rtm->rtm_hdrlen - | ||
204 | offsetof(struct if_msghdr, ifm_data); | ||
205 | if (dlen > sizeof(ifm->ifm_data)) | ||
206 | dlen = sizeof(ifm->ifm_data); | ||
207 | memcpy(data, &ifm->ifm_data, dlen); | ||
208 | data += sizeof(ifm->ifm_data); | ||
209 | |||
210 | ift = (ift->ifa_next = ift + 1); | ||
211 | } else | ||
212 | index = 0; | ||
213 | break; | ||
214 | |||
215 | case RTM_NEWADDR: | ||
216 | ifam = (struct ifa_msghdr *)rtm; | ||
217 | if (index && ifam->ifam_index != index) | ||
218 | abort(); /* XXX abort illegal in library */ | ||
219 | |||
220 | if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) | ||
221 | break; | ||
222 | ift->ifa_name = cif->ifa_name; | ||
223 | ift->ifa_flags = cif->ifa_flags; | ||
224 | ift->ifa_data = NULL; | ||
225 | p = next + rtm->rtm_hdrlen; | ||
226 | /* Scan to look for length of address */ | ||
227 | alen = 0; | ||
228 | for (p0 = p, i = 0; i < RTAX_MAX; i++) { | ||
229 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
230 | == 0) | ||
231 | continue; | ||
232 | sa = (struct sockaddr *)p; | ||
233 | len = SA_RLEN(sa); | ||
234 | if (i == RTAX_IFA) { | ||
235 | alen = len; | ||
236 | break; | ||
237 | } | ||
238 | p += len; | ||
239 | } | ||
240 | for (p = p0, i = 0; i < RTAX_MAX; i++) { | ||
241 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
242 | == 0) | ||
243 | continue; | ||
244 | sa = (struct sockaddr *)p; | ||
245 | len = SA_RLEN(sa); | ||
246 | switch (i) { | ||
247 | case RTAX_IFA: | ||
248 | ift->ifa_addr = (struct sockaddr *)data; | ||
249 | memcpy(data, p, len); | ||
250 | data += len; | ||
251 | break; | ||
252 | |||
253 | case RTAX_NETMASK: | ||
254 | ift->ifa_netmask = | ||
255 | (struct sockaddr *)data; | ||
256 | if (sa->sa_len == 0) { | ||
257 | memset(data, 0, alen); | ||
258 | data += alen; | ||
259 | break; | ||
260 | } | ||
261 | memcpy(data, p, len); | ||
262 | data += len; | ||
263 | break; | ||
264 | |||
265 | case RTAX_BRD: | ||
266 | ift->ifa_broadaddr = | ||
267 | (struct sockaddr *)data; | ||
268 | memcpy(data, p, len); | ||
269 | data += len; | ||
270 | break; | ||
271 | } | ||
272 | p += len; | ||
273 | } | ||
274 | |||
275 | |||
276 | ift = (ift->ifa_next = ift + 1); | ||
277 | break; | ||
278 | } | ||
279 | } | ||
280 | |||
281 | free(buf); | ||
282 | if (--ift >= ifa) { | ||
283 | ift->ifa_next = NULL; | ||
284 | *pif = ifa; | ||
285 | } else { | ||
286 | *pif = NULL; | ||
287 | free(ifa); | ||
288 | } | ||
289 | return (0); | ||
290 | } | ||
291 | |||
292 | void | ||
293 | freeifaddrs(struct ifaddrs *ifp) | ||
294 | { | ||
295 | free(ifp); | ||
296 | } | ||
diff --git a/src/lib/libc/net/getnameinfo.3 b/src/lib/libc/net/getnameinfo.3 new file mode 100644 index 0000000000..d023b2523d --- /dev/null +++ b/src/lib/libc/net/getnameinfo.3 | |||
@@ -0,0 +1,265 @@ | |||
1 | .\" $OpenBSD: getnameinfo.3,v 1.46 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" $KAME: getnameinfo.3,v 1.37 2005/01/05 03:23:05 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC") | ||
5 | .\" Copyright (C) 2000, 2001 Internet Software Consortium. | ||
6 | .\" | ||
7 | .\" Permission to use, copy, modify, and distribute this software for any | ||
8 | .\" purpose with or without fee is hereby granted, provided that the above | ||
9 | .\" copyright notice and this permission notice appear in all copies. | ||
10 | .\" | ||
11 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH | ||
12 | .\" REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
13 | .\" AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
14 | .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
15 | .\" LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE | ||
16 | .\" OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
17 | .\" PERFORMANCE OF THIS SOFTWARE. | ||
18 | .\" | ||
19 | .Dd $Mdocdate: January 21 2014 $ | ||
20 | .Dt GETNAMEINFO 3 | ||
21 | .Os | ||
22 | .Sh NAME | ||
23 | .Nm getnameinfo | ||
24 | .Nd socket address structure to hostname and service name | ||
25 | .Sh SYNOPSIS | ||
26 | .In sys/types.h | ||
27 | .In sys/socket.h | ||
28 | .In netdb.h | ||
29 | .Ft int | ||
30 | .Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" "char *host" \ | ||
31 | "size_t hostlen" "char *serv" "size_t servlen" "int flags" | ||
32 | .Sh DESCRIPTION | ||
33 | The | ||
34 | .Fn getnameinfo | ||
35 | function is used to convert a | ||
36 | .Li sockaddr | ||
37 | structure to a pair of host name and service strings. | ||
38 | It is a replacement for and provides more flexibility than the | ||
39 | .Xr gethostbyaddr 3 | ||
40 | and | ||
41 | .Xr getservbyport 3 | ||
42 | functions and is the converse of the | ||
43 | .Xr getaddrinfo 3 | ||
44 | function. | ||
45 | .Pp | ||
46 | The | ||
47 | .Li sockaddr | ||
48 | structure | ||
49 | .Fa sa | ||
50 | should point to either a | ||
51 | .Li sockaddr_in | ||
52 | or | ||
53 | .Li sockaddr_in6 | ||
54 | structure (for IPv4 or IPv6 respectively) that is | ||
55 | .Fa salen | ||
56 | bytes long. | ||
57 | .Pp | ||
58 | The host and service names associated with | ||
59 | .Fa sa | ||
60 | are stored in | ||
61 | .Fa host | ||
62 | and | ||
63 | .Fa serv | ||
64 | which have length parameters | ||
65 | .Fa hostlen | ||
66 | and | ||
67 | .Fa servlen . | ||
68 | The maximum value for | ||
69 | .Fa hostlen | ||
70 | is | ||
71 | .Dv NI_MAXHOST | ||
72 | and | ||
73 | the maximum value for | ||
74 | .Fa servlen | ||
75 | is | ||
76 | .Dv NI_MAXSERV , | ||
77 | as defined by | ||
78 | .In netdb.h . | ||
79 | If a length parameter is zero, no string will be stored. | ||
80 | Otherwise, enough space must be provided to store the | ||
81 | host name or service string plus a byte for the NUL terminator. | ||
82 | .Pp | ||
83 | The | ||
84 | .Fa flags | ||
85 | argument is formed by | ||
86 | .Tn OR Ns 'ing | ||
87 | the following values: | ||
88 | .Bl -tag -width "NI_NUMERICHOSTXX" | ||
89 | .It Dv NI_NOFQDN | ||
90 | A fully qualified domain name is not required for local hosts. | ||
91 | The local part of the fully qualified domain name is returned instead. | ||
92 | .It Dv NI_NUMERICHOST | ||
93 | Return the address in numeric form, as if calling | ||
94 | .Xr inet_ntop 3 , | ||
95 | instead of a host name. | ||
96 | .It Dv NI_NAMEREQD | ||
97 | A name is required. | ||
98 | If the host name cannot be found in DNS and this flag is set, | ||
99 | a non-zero error code is returned. | ||
100 | If the host name is not found and the flag is not set, the | ||
101 | address is returned in numeric form. | ||
102 | .It NI_NUMERICSERV | ||
103 | The service name is returned as a digit string representing the port number. | ||
104 | .It NI_DGRAM | ||
105 | Specifies that the service being looked up is a datagram | ||
106 | service, and causes | ||
107 | .Xr getservbyport 3 | ||
108 | to be called with a second argument of | ||
109 | .Dq udp | ||
110 | instead of its default of | ||
111 | .Dq tcp . | ||
112 | This is required for the few ports (512\-514) that have different services | ||
113 | for | ||
114 | .Tn UDP | ||
115 | and | ||
116 | .Tn TCP . | ||
117 | .El | ||
118 | .Pp | ||
119 | This implementation allows numeric IPv6 address notation with scope identifier, | ||
120 | as documented in RFC 4007. | ||
121 | IPv6 link-local address will appear as a string like | ||
122 | .Dq Li fe80::1%ne0 . | ||
123 | Refer to | ||
124 | .Xr getaddrinfo 3 | ||
125 | for more information. | ||
126 | .Sh RETURN VALUES | ||
127 | .Fn getnameinfo | ||
128 | returns zero on success or one of the error codes listed in | ||
129 | .Xr gai_strerror 3 | ||
130 | if an error occurs. | ||
131 | .Sh EXAMPLES | ||
132 | The following code tries to get a numeric host name, and service name, | ||
133 | for a given socket address. | ||
134 | Observe that there is no hardcoded reference to a particular address family. | ||
135 | .Bd -literal -offset indent | ||
136 | struct sockaddr *sa; /* input */ | ||
137 | char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; | ||
138 | |||
139 | if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf, | ||
140 | sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) | ||
141 | errx(1, "could not get numeric hostname"); | ||
142 | printf("host=%s, serv=%s\en", hbuf, sbuf); | ||
143 | .Ed | ||
144 | .Pp | ||
145 | The following version checks if the socket address has a reverse address mapping: | ||
146 | .Bd -literal -offset indent | ||
147 | struct sockaddr *sa; /* input */ | ||
148 | char hbuf[NI_MAXHOST]; | ||
149 | |||
150 | if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0, | ||
151 | NI_NAMEREQD)) | ||
152 | errx(1, "could not resolve hostname"); | ||
153 | printf("host=%s\en", hbuf); | ||
154 | .Ed | ||
155 | .Sh SEE ALSO | ||
156 | .Xr gai_strerror 3 , | ||
157 | .Xr getaddrinfo 3 , | ||
158 | .Xr gethostbyaddr 3 , | ||
159 | .Xr getservbyport 3 , | ||
160 | .Xr inet_ntop 3 , | ||
161 | .Xr resolver 3 , | ||
162 | .Xr hosts 5 , | ||
163 | .Xr resolv.conf 5 , | ||
164 | .Xr services 5 , | ||
165 | .Xr hostname 7 , | ||
166 | .Xr named 8 | ||
167 | .Rs | ||
168 | .%A Craig Metz | ||
169 | .%T Protocol Independence Using the Sockets API | ||
170 | .%B Proceedings of the Freenix Track: 2000 USENIX Annual Technical Conference | ||
171 | .%D June 2000 | ||
172 | .Re | ||
173 | .Sh STANDARDS | ||
174 | The | ||
175 | .Fn getnameinfo | ||
176 | function is defined by the | ||
177 | .St -p1003.1g-2000 | ||
178 | draft specification and documented in RFC 3493. | ||
179 | .Pp | ||
180 | .Rs | ||
181 | .%A R. Gilligan | ||
182 | .%A S. Thomson | ||
183 | .%A J. Bound | ||
184 | .%A J. McCann | ||
185 | .%A W. Stevens | ||
186 | .%D February 2003 | ||
187 | .%R RFC 3493 | ||
188 | .%T Basic Socket Interface Extensions for IPv6 | ||
189 | .Re | ||
190 | .Pp | ||
191 | .Rs | ||
192 | .%A S. Deering | ||
193 | .%A B. Haberman | ||
194 | .%A T. Jinmei | ||
195 | .%A E. Nordmark | ||
196 | .%A B. Zill | ||
197 | .%D March 2005 | ||
198 | .%R RFC 4007 | ||
199 | .%T IPv6 Scoped Address Architecture | ||
200 | .Re | ||
201 | .Sh CAVEATS | ||
202 | .Fn getnameinfo | ||
203 | can return both numeric and FQDN forms of the address specified in | ||
204 | .Fa sa . | ||
205 | There is no return value that indicates whether the string returned in | ||
206 | .Fa host | ||
207 | is a result of binary to numeric-text translation (like | ||
208 | .Xr inet_ntop 3 ) , | ||
209 | or is the result of a DNS reverse lookup. | ||
210 | Because of this, malicious parties could set up a PTR record as follows: | ||
211 | .Bd -literal -offset indent | ||
212 | 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1 | ||
213 | .Ed | ||
214 | .Pp | ||
215 | and trick the caller of | ||
216 | .Fn getnameinfo | ||
217 | into believing that | ||
218 | .Fa sa | ||
219 | is | ||
220 | .Li 10.1.1.1 | ||
221 | when it is actually | ||
222 | .Li 127.0.0.1 . | ||
223 | .Pp | ||
224 | To prevent such attacks, the use of | ||
225 | .Dv NI_NAMEREQD | ||
226 | is recommended when the result of | ||
227 | .Fn getnameinfo | ||
228 | is used | ||
229 | for access control purposes: | ||
230 | .Bd -literal -offset indent | ||
231 | struct sockaddr *sa; | ||
232 | char addr[NI_MAXHOST]; | ||
233 | struct addrinfo hints, *res; | ||
234 | int error; | ||
235 | |||
236 | error = getnameinfo(sa, sa->sa_len, addr, sizeof(addr), | ||
237 | NULL, 0, NI_NAMEREQD); | ||
238 | if (error == 0) { | ||
239 | memset(&hints, 0, sizeof(hints)); | ||
240 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
241 | hints.ai_flags = AI_NUMERICHOST; | ||
242 | if (getaddrinfo(addr, "0", &hints, &res) == 0) { | ||
243 | /* malicious PTR record */ | ||
244 | freeaddrinfo(res); | ||
245 | printf("bogus PTR record\en"); | ||
246 | return -1; | ||
247 | } | ||
248 | /* addr is FQDN as a result of PTR lookup */ | ||
249 | } else { | ||
250 | /* addr is numeric string */ | ||
251 | error = getnameinfo(sa, sa->sa_len, addr, sizeof(addr), | ||
252 | NULL, 0, NI_NUMERICHOST); | ||
253 | } | ||
254 | .Ed | ||
255 | .Sh BUGS | ||
256 | The implementation of | ||
257 | .Fn getnameinfo | ||
258 | is not thread-safe. | ||
259 | .Pp | ||
260 | .Ox | ||
261 | intentionally uses a different | ||
262 | .Dv NI_MAXHOST | ||
263 | value from what | ||
264 | .Tn "RFC 2553" | ||
265 | suggests, to avoid buffer length handling mistakes. | ||
diff --git a/src/lib/libc/net/getnetbyaddr.c b/src/lib/libc/net/getnetbyaddr.c new file mode 100644 index 0000000000..a85106630c --- /dev/null +++ b/src/lib/libc/net/getnetbyaddr.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* $OpenBSD: getnetbyaddr.c,v 1.8 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <netdb.h> | ||
32 | |||
33 | extern int _net_stayopen; | ||
34 | |||
35 | struct netent * | ||
36 | _getnetbyaddr(in_addr_t net, int type) | ||
37 | { | ||
38 | struct netent *p; | ||
39 | |||
40 | setnetent(_net_stayopen); | ||
41 | while ((p = getnetent())) | ||
42 | if (p->n_addrtype == type && p->n_net == net) | ||
43 | break; | ||
44 | if (!_net_stayopen) | ||
45 | endnetent(); | ||
46 | return (p); | ||
47 | } | ||
diff --git a/src/lib/libc/net/getnetbyname.c b/src/lib/libc/net/getnetbyname.c new file mode 100644 index 0000000000..e6540cf12c --- /dev/null +++ b/src/lib/libc/net/getnetbyname.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* $OpenBSD: getnetbyname.c,v 1.8 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <netdb.h> | ||
32 | #include <string.h> | ||
33 | |||
34 | extern int _net_stayopen; | ||
35 | |||
36 | struct netent * | ||
37 | _getnetbyname(const char *name) | ||
38 | { | ||
39 | struct netent *p; | ||
40 | char **cp; | ||
41 | |||
42 | setnetent(_net_stayopen); | ||
43 | while ((p = getnetent())) { | ||
44 | if (strcasecmp(p->n_name, name) == 0) | ||
45 | break; | ||
46 | for (cp = p->n_aliases; *cp != 0; cp++) | ||
47 | if (strcasecmp(*cp, name) == 0) | ||
48 | goto found; | ||
49 | } | ||
50 | found: | ||
51 | if (!_net_stayopen) | ||
52 | endnetent(); | ||
53 | return (p); | ||
54 | } | ||
diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3 new file mode 100644 index 0000000000..e0344e2dd0 --- /dev/null +++ b/src/lib/libc/net/getnetent.3 | |||
@@ -0,0 +1,140 @@ | |||
1 | .\" $OpenBSD: getnetent.3,v 1.16 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: June 5 2013 $ | ||
31 | .Dt GETNETENT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getnetent , | ||
35 | .Nm getnetbyaddr , | ||
36 | .Nm getnetbyname , | ||
37 | .Nm setnetent , | ||
38 | .Nm endnetent | ||
39 | .Nd get network entry | ||
40 | .Sh SYNOPSIS | ||
41 | .In netdb.h | ||
42 | .Ft struct netent * | ||
43 | .Fn getnetent "void" | ||
44 | .Ft struct netent * | ||
45 | .Fn getnetbyname "const char *name" | ||
46 | .Ft struct netent * | ||
47 | .Fn getnetbyaddr "in_addr_t net" "int type" | ||
48 | .Ft void | ||
49 | .Fn setnetent "int stayopen" | ||
50 | .Ft void | ||
51 | .Fn endnetent "void" | ||
52 | .Sh DESCRIPTION | ||
53 | The | ||
54 | .Fn getnetent , | ||
55 | .Fn getnetbyname , | ||
56 | and | ||
57 | .Fn getnetbyaddr | ||
58 | functions each return a pointer to an object with the following structure | ||
59 | containing the broken-out fields of a line in the network database, | ||
60 | .Pa /etc/networks . | ||
61 | .Bd -literal -offset indent | ||
62 | struct netent { | ||
63 | char *n_name; /* official name of net */ | ||
64 | char **n_aliases; /* alias list */ | ||
65 | int n_addrtype; /* net number type */ | ||
66 | in_addr_t n_net; /* net number */ | ||
67 | }; | ||
68 | .Ed | ||
69 | .Pp | ||
70 | The members of this structure are: | ||
71 | .Bl -tag -width n_addrtype | ||
72 | .It Fa n_name | ||
73 | The official name of the network. | ||
74 | .It Fa n_aliases | ||
75 | A null-terminated list of alternate names for the network. | ||
76 | .It Fa n_addrtype | ||
77 | The type of the network number returned; currently only | ||
78 | .Dv AF_INET . | ||
79 | .It Fa n_net | ||
80 | The network number. | ||
81 | Network numbers are returned in machine byte order. | ||
82 | .El | ||
83 | .Pp | ||
84 | The | ||
85 | .Fn getnetent | ||
86 | function reads the next line of the file, opening the file if necessary. | ||
87 | .Pp | ||
88 | The | ||
89 | .Fn setnetent | ||
90 | function opens and rewinds the file. | ||
91 | If the | ||
92 | .Fa stayopen | ||
93 | flag is non-zero, | ||
94 | the net database will not be closed after each call to | ||
95 | .Fn getnetbyname | ||
96 | or | ||
97 | .Fn getnetbyaddr . | ||
98 | .Pp | ||
99 | The | ||
100 | .Fn endnetent | ||
101 | function closes the file. | ||
102 | .Pp | ||
103 | The | ||
104 | .Fn getnetbyname | ||
105 | and | ||
106 | .Fn getnetbyaddr | ||
107 | functions search the domain name server if the system is configured to use one. | ||
108 | If the search fails, or no name server is configured, they sequentially | ||
109 | search from the beginning of the file until a matching net name or | ||
110 | net address and type is found, or until | ||
111 | .Dv EOF | ||
112 | is encountered. | ||
113 | Network numbers are supplied in host order. | ||
114 | .Sh FILES | ||
115 | .Bl -tag -width /etc/networks -compact | ||
116 | .It Pa /etc/networks | ||
117 | .El | ||
118 | .Sh DIAGNOSTICS | ||
119 | Null pointer (0) returned on | ||
120 | .Dv EOF | ||
121 | or error. | ||
122 | .Sh SEE ALSO | ||
123 | .Xr resolver 3 , | ||
124 | .Xr networks 5 | ||
125 | .Sh HISTORY | ||
126 | The | ||
127 | .Fn getnetent , | ||
128 | .Fn getnetbyaddr , | ||
129 | .Fn getnetbyname , | ||
130 | .Fn setnetent , | ||
131 | and | ||
132 | .Fn endnetent | ||
133 | functions appeared in | ||
134 | .Bx 4.2 . | ||
135 | .Sh BUGS | ||
136 | The data space used by these functions is static; if future use | ||
137 | requires the data, it should be copied before any subsequent calls | ||
138 | to these functions overwrite it. | ||
139 | Only Internet network numbers are currently understood. | ||
140 | Expecting network numbers to fit in no more than 32 bits is naive. | ||
diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c new file mode 100644 index 0000000000..57fe459e2b --- /dev/null +++ b/src/lib/libc/net/getnetent.c | |||
@@ -0,0 +1,120 @@ | |||
1 | /* $OpenBSD: getnetent.c,v 1.13 2012/04/10 16:41:10 eric Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/param.h> | ||
32 | #include <sys/socket.h> | ||
33 | #include <netinet/in.h> | ||
34 | #include <arpa/inet.h> | ||
35 | #include <netdb.h> | ||
36 | #include <stdio.h> | ||
37 | #include <string.h> | ||
38 | |||
39 | #define MAXALIASES 35 | ||
40 | |||
41 | static FILE *netf; | ||
42 | static char line[BUFSIZ+1]; | ||
43 | static struct netent net; | ||
44 | static char *net_aliases[MAXALIASES]; | ||
45 | int _net_stayopen; | ||
46 | |||
47 | void | ||
48 | setnetent(int f) | ||
49 | { | ||
50 | if (netf == NULL) | ||
51 | netf = fopen(_PATH_NETWORKS, "r" ); | ||
52 | else | ||
53 | rewind(netf); | ||
54 | _net_stayopen |= f; | ||
55 | } | ||
56 | |||
57 | void | ||
58 | endnetent(void) | ||
59 | { | ||
60 | if (netf) { | ||
61 | fclose(netf); | ||
62 | netf = NULL; | ||
63 | } | ||
64 | _net_stayopen = 0; | ||
65 | } | ||
66 | |||
67 | struct netent * | ||
68 | getnetent(void) | ||
69 | { | ||
70 | char *p, *cp, **q; | ||
71 | size_t len; | ||
72 | |||
73 | if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) | ||
74 | return (NULL); | ||
75 | again: | ||
76 | if ((p = fgetln(netf, &len)) == NULL) | ||
77 | return (NULL); | ||
78 | if (p[len-1] == '\n') | ||
79 | len--; | ||
80 | if (len >= sizeof(line) || len == 0) | ||
81 | goto again; | ||
82 | p = memcpy(line, p, len); | ||
83 | line[len] = '\0'; | ||
84 | if (*p == '#') | ||
85 | goto again; | ||
86 | if ((cp = strchr(p, '#')) != NULL) | ||
87 | *cp = '\0'; | ||
88 | net.n_name = p; | ||
89 | if (strlen(net.n_name) >= MAXHOSTNAMELEN-1) | ||
90 | net.n_name[MAXHOSTNAMELEN-1] = '\0'; | ||
91 | cp = strpbrk(p, " \t"); | ||
92 | if (cp == NULL) | ||
93 | goto again; | ||
94 | *cp++ = '\0'; | ||
95 | while (*cp == ' ' || *cp == '\t') | ||
96 | cp++; | ||
97 | p = strpbrk(cp, " \t"); | ||
98 | if (p != NULL) | ||
99 | *p++ = '\0'; | ||
100 | net.n_net = inet_network(cp); | ||
101 | net.n_addrtype = AF_INET; | ||
102 | q = net.n_aliases = net_aliases; | ||
103 | cp = p; | ||
104 | while (cp && *cp) { | ||
105 | if (*cp == ' ' || *cp == '\t') { | ||
106 | cp++; | ||
107 | continue; | ||
108 | } | ||
109 | if (q < &net_aliases[MAXALIASES - 1]) { | ||
110 | *q++ = cp; | ||
111 | if (strlen(cp) >= MAXHOSTNAMELEN-1) | ||
112 | cp[MAXHOSTNAMELEN-1] = '\0'; | ||
113 | } | ||
114 | cp = strpbrk(cp, " \t"); | ||
115 | if (cp != NULL) | ||
116 | *cp++ = '\0'; | ||
117 | } | ||
118 | *q = NULL; | ||
119 | return (&net); | ||
120 | } | ||
diff --git a/src/lib/libc/net/getpeereid.3 b/src/lib/libc/net/getpeereid.3 new file mode 100644 index 0000000000..9c5742a245 --- /dev/null +++ b/src/lib/libc/net/getpeereid.3 | |||
@@ -0,0 +1,121 @@ | |||
1 | .\" $OpenBSD: getpeereid.3,v 1.3 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .Dd $Mdocdate: June 5 2013 $ | ||
30 | .Dt GETPEEREID 3 | ||
31 | .Os | ||
32 | .Sh NAME | ||
33 | .Nm getpeereid | ||
34 | .Nd get effective user and group identification of locally-connected peer | ||
35 | .Sh SYNOPSIS | ||
36 | .In sys/types.h | ||
37 | .In sys/socket.h | ||
38 | .Ft int | ||
39 | .Fn getpeereid "int s" "uid_t *euid" "gid_t *egid" | ||
40 | .Sh DESCRIPTION | ||
41 | .Fn getpeereid | ||
42 | returns the effective user ID and group ID of the peer connected to | ||
43 | a | ||
44 | .Ux Ns -domain | ||
45 | socket (see | ||
46 | .Xr unix 4 ) . | ||
47 | The argument | ||
48 | .Fa s | ||
49 | must be of type | ||
50 | .Dv SOCK_STREAM | ||
51 | or | ||
52 | .Dv SOCK_SEQPACKET . | ||
53 | .Pp | ||
54 | One common use is for | ||
55 | .Ux Ns -domain | ||
56 | servers to determine the credentials of clients that have connected to it. | ||
57 | .Pp | ||
58 | .Fn getpeereid | ||
59 | takes three parameters: | ||
60 | .Bl -bullet | ||
61 | .It | ||
62 | .Fa s | ||
63 | contains the file descriptor of the socket whose peer credentials | ||
64 | should be looked up. | ||
65 | .It | ||
66 | .Fa euid | ||
67 | points to a | ||
68 | .Li uid_t | ||
69 | variable into which the effective user ID for the connected peer will | ||
70 | be stored. | ||
71 | .It | ||
72 | .Fa egid | ||
73 | points to a | ||
74 | .Li gid_t | ||
75 | variable into which the effective group ID for the connected peer will | ||
76 | be stored. | ||
77 | .El | ||
78 | .Sh RETURN VALUES | ||
79 | If the call succeeds, a 0 is returned and | ||
80 | .Fa euid | ||
81 | and | ||
82 | .Fa egid | ||
83 | are set to the effective user ID and group ID of the connected peer. | ||
84 | Otherwise, | ||
85 | .Va errno | ||
86 | is set and a value of \-1 is returned. | ||
87 | .Sh ERRORS | ||
88 | On failure, | ||
89 | .Va errno | ||
90 | is set to one of the following: | ||
91 | .Bl -tag -width Er | ||
92 | .It Bq Er EBADF | ||
93 | The argument | ||
94 | .Fa s | ||
95 | is not a valid descriptor. | ||
96 | .It Bq Er ENOTSOCK | ||
97 | The argument | ||
98 | .Fa s | ||
99 | is a file, not a socket. | ||
100 | .It Bq Er EOPNOTSUPP | ||
101 | The socket is not in the | ||
102 | .Ux Ns -domain . | ||
103 | .It Bq Er ENOTCONN | ||
104 | The socket is not connected. | ||
105 | .It Bq Er ENOBUFS | ||
106 | Insufficient resources were available in the system | ||
107 | to perform the operation. | ||
108 | .El | ||
109 | .Sh SEE ALSO | ||
110 | .Xr accept 2 , | ||
111 | .Xr bind 2 , | ||
112 | .Xr getpeername 2 , | ||
113 | .Xr getsockname 2 , | ||
114 | .Xr getsockopt 2 , | ||
115 | .Xr socket 2 , | ||
116 | .Xr unix 4 | ||
117 | .Sh HISTORY | ||
118 | The | ||
119 | .Fn getpeereid | ||
120 | function call appeared in | ||
121 | .Ox 3.0 . | ||
diff --git a/src/lib/libc/net/getpeereid.c b/src/lib/libc/net/getpeereid.c new file mode 100644 index 0000000000..208e541f17 --- /dev/null +++ b/src/lib/libc/net/getpeereid.c | |||
@@ -0,0 +1,36 @@ | |||
1 | /* $OpenBSD: getpeereid.c,v 1.1 2010/07/01 19:15:30 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2010 Theo de Raadt <deraadt@openbsd.org> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <sys/types.h> | ||
20 | #include <sys/socket.h> | ||
21 | |||
22 | int | ||
23 | getpeereid(int s, uid_t *euid, gid_t *egid) | ||
24 | { | ||
25 | struct sockpeercred creds; | ||
26 | socklen_t credslen = sizeof(creds); | ||
27 | int error; | ||
28 | |||
29 | error = getsockopt(s, SOL_SOCKET, SO_PEERCRED, | ||
30 | &creds, &credslen); | ||
31 | if (error) | ||
32 | return (error); | ||
33 | *euid = creds.uid; | ||
34 | *egid = creds.gid; | ||
35 | return (0); | ||
36 | } | ||
diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c new file mode 100644 index 0000000000..07fa33288d --- /dev/null +++ b/src/lib/libc/net/getproto.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* $OpenBSD: getproto.c,v 1.7 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <netdb.h> | ||
32 | #include <stdio.h> | ||
33 | |||
34 | int | ||
35 | getprotobynumber_r(int num, struct protoent *pe, struct protoent_data *pd) | ||
36 | { | ||
37 | int error; | ||
38 | |||
39 | setprotoent_r(pd->stayopen, pd); | ||
40 | while ((error = getprotoent_r(pe, pd)) == 0) | ||
41 | if (pe->p_proto == num) | ||
42 | break; | ||
43 | if (!pd->stayopen && pd->fp != NULL) { | ||
44 | (void)fclose(pd->fp); | ||
45 | pd->fp = NULL; | ||
46 | } | ||
47 | return (error); | ||
48 | } | ||
49 | |||
50 | struct protoent * | ||
51 | getprotobynumber(int num) | ||
52 | { | ||
53 | extern struct protoent_data _protoent_data; | ||
54 | static struct protoent proto; | ||
55 | |||
56 | if (getprotobynumber_r(num, &proto, &_protoent_data) != 0) | ||
57 | return (NULL); | ||
58 | return (&proto); | ||
59 | } | ||
diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3 new file mode 100644 index 0000000000..cc2c69836a --- /dev/null +++ b/src/lib/libc/net/getprotoent.3 | |||
@@ -0,0 +1,213 @@ | |||
1 | .\" $OpenBSD: getprotoent.3,v 1.18 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: June 5 2013 $ | ||
31 | .Dt GETPROTOENT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getprotoent , | ||
35 | .Nm getprotoent_r , | ||
36 | .Nm getprotobynumber , | ||
37 | .Nm getprotobynumber_r , | ||
38 | .Nm getprotobyname , | ||
39 | .Nm getprotobyname_r , | ||
40 | .Nm setprotoent , | ||
41 | .Nm setprotoent_r , | ||
42 | .Nm endprotoent , | ||
43 | .Nm endprotoent_r | ||
44 | .Nd get protocol entry | ||
45 | .Sh SYNOPSIS | ||
46 | .In netdb.h | ||
47 | .Ft struct protoent * | ||
48 | .Fn getprotoent "void" | ||
49 | .Ft int | ||
50 | .Fn getprotoent_r "struct protoent *protoent" "struct protoent_data *protoent_data" | ||
51 | .Ft struct protoent * | ||
52 | .Fn getprotobyname "const char *name" | ||
53 | .Ft int | ||
54 | .Fn getprotobyname_r "const char *name" "struct protoent *protoent" "struct protoent_data *protoent_data" | ||
55 | .Ft struct protoent * | ||
56 | .Fn getprotobynumber "int proto" | ||
57 | .Ft int | ||
58 | .Fn getprotobynumber_r "int proto" "struct protoent *protoent" "struct protoent_data *protoent_data" | ||
59 | .Ft void | ||
60 | .Fn setprotoent "int stayopen" | ||
61 | .Ft void | ||
62 | .Fn setprotoent_r "int stayopen" "struct protoent_data *protoent_data" | ||
63 | .Ft void | ||
64 | .Fn endprotoent "void" | ||
65 | .Ft void | ||
66 | .Fn endprotoent_r "struct protoent_data *protoent_data" | ||
67 | .Sh DESCRIPTION | ||
68 | The | ||
69 | .Fn getprotoent , | ||
70 | .Fn getprotobyname , | ||
71 | and | ||
72 | .Fn getprotobynumber | ||
73 | functions each return a pointer to an object with the following structure | ||
74 | containing the broken-out fields of a line in the network protocol database, | ||
75 | .Pa /etc/protocols . | ||
76 | .Bd -literal -offset indent | ||
77 | .Pp | ||
78 | struct protoent { | ||
79 | char *p_name; /* official name of protocol */ | ||
80 | char **p_aliases; /* alias list */ | ||
81 | int p_proto; /* protocol number */ | ||
82 | }; | ||
83 | .Ed | ||
84 | .Pp | ||
85 | The members of this structure are: | ||
86 | .Bl -tag -width p_aliases | ||
87 | .It Fa p_name | ||
88 | The official name of the protocol. | ||
89 | .It Fa p_aliases | ||
90 | A null-terminated list of alternate names for the protocol. | ||
91 | .It Fa p_proto | ||
92 | The protocol number. | ||
93 | .El | ||
94 | .Pp | ||
95 | The | ||
96 | .Fn getprotoent | ||
97 | function reads the next line of the file, opening the file if necessary. | ||
98 | .Pp | ||
99 | The | ||
100 | .Fn setprotoent | ||
101 | function opens and rewinds the file. | ||
102 | If the | ||
103 | .Fa stayopen | ||
104 | flag is non-zero, | ||
105 | the protocol database will not be closed after each call to | ||
106 | .Fn getprotobyname | ||
107 | or | ||
108 | .Fn getprotobynumber . | ||
109 | .Pp | ||
110 | The | ||
111 | .Fn endprotoent | ||
112 | function closes the file. | ||
113 | .Pp | ||
114 | The | ||
115 | .Fn getprotobyname | ||
116 | and | ||
117 | .Fn getprotobynumber | ||
118 | functions sequentially search from the beginning of the file until a | ||
119 | matching protocol name or protocol number is found, or until | ||
120 | .Dv EOF | ||
121 | is encountered. | ||
122 | .Pp | ||
123 | The | ||
124 | .Fn getprotoent_r , | ||
125 | .Fn getprotobyport_r , | ||
126 | .Fn getprotobyname_r , | ||
127 | .Fn setprotoent_r , | ||
128 | and | ||
129 | .Fn endprotoent_r | ||
130 | functions are reentrant versions of the above functions that take a | ||
131 | pointer to a | ||
132 | .Vt protoent_data | ||
133 | structure which is used to store state information. | ||
134 | The structure must be zero-filled before it is used | ||
135 | and should be considered opaque for the sake of portability. | ||
136 | .Pp | ||
137 | The | ||
138 | .Fn getprotoent_r , | ||
139 | .Fn getprotobyport_r , | ||
140 | and | ||
141 | .Fn getprotobyname_r | ||
142 | functions | ||
143 | also take a pointer to a | ||
144 | .Vt protoent | ||
145 | structure which is used to store the results of the database lookup. | ||
146 | .Sh RETURN VALUES | ||
147 | The | ||
148 | .Fn getprotoent , | ||
149 | .Fn getprotobyport , | ||
150 | and | ||
151 | .Fn getprotobyname | ||
152 | functions return a pointer to a | ||
153 | .Vt protoent | ||
154 | structure on success or a null pointer if end-of-file | ||
155 | is reached or an error occurs. | ||
156 | .Pp | ||
157 | The | ||
158 | .Fn getprotoent_r , | ||
159 | .Fn getprotobyport_r , | ||
160 | and | ||
161 | .Fn getprotobyname_r | ||
162 | functions return 0 on success or \-1 if end-of-file | ||
163 | is reached or an error occurs. | ||
164 | .Sh FILES | ||
165 | .Bl -tag -width /etc/protocols -compact | ||
166 | .It Pa /etc/protocols | ||
167 | .El | ||
168 | .Sh SEE ALSO | ||
169 | .Xr protocols 5 | ||
170 | .Sh STANDARDS | ||
171 | The | ||
172 | .Fn getprotoent , | ||
173 | .Fn getprotobynumber , | ||
174 | .Fn getprotobyname , | ||
175 | .Fn setprotoent , | ||
176 | and | ||
177 | .Fn endprotoent | ||
178 | functions conform to | ||
179 | .St -p1003.1-2004 . | ||
180 | .Pp | ||
181 | The | ||
182 | .Fn getprotoent_r , | ||
183 | .Fn getprotobyport_r , | ||
184 | .Fn getprotobyname_r , | ||
185 | .Fn setprotoent_r , | ||
186 | and | ||
187 | .Fn endprotoent_r | ||
188 | functions are not currently standardized. | ||
189 | This implementation follows the API used by HP, IBM, and Digital. | ||
190 | .Sh HISTORY | ||
191 | The | ||
192 | .Fn getprotoent , | ||
193 | .Fn getprotobynumber , | ||
194 | .Fn getprotobyname , | ||
195 | .Fn setprotoent , | ||
196 | and | ||
197 | .Fn endprotoent | ||
198 | functions appeared in | ||
199 | .Bx 4.2 . | ||
200 | .Pp | ||
201 | The | ||
202 | .Fn getprotoent_r , | ||
203 | .Fn getprotobyport_r , | ||
204 | .Fn getprotobyname_r , | ||
205 | .Fn setprotoent_r , | ||
206 | and | ||
207 | .Fn endprotoent_r | ||
208 | functions appeared in | ||
209 | .Ox 3.7 . | ||
210 | .Sh BUGS | ||
211 | The non-reentrant functions use a static data space; if the data is needed | ||
212 | for future use, it should be copied before any subsequent calls overwrite it. | ||
213 | Only the Internet protocols are currently understood. | ||
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c new file mode 100644 index 0000000000..f0705e0765 --- /dev/null +++ b/src/lib/libc/net/getprotoent.c | |||
@@ -0,0 +1,166 @@ | |||
1 | /* $OpenBSD: getprotoent.c,v 1.10 2007/09/02 15:19:17 deraadt Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/types.h> | ||
32 | #include <sys/socket.h> | ||
33 | |||
34 | #include <errno.h> | ||
35 | #include <limits.h> | ||
36 | #include <netdb.h> | ||
37 | #include <stdio.h> | ||
38 | #include <stdlib.h> | ||
39 | #include <string.h> | ||
40 | |||
41 | void | ||
42 | setprotoent_r(int f, struct protoent_data *pd) | ||
43 | { | ||
44 | if (pd->fp == NULL) | ||
45 | pd->fp = fopen(_PATH_PROTOCOLS, "r" ); | ||
46 | else | ||
47 | rewind(pd->fp); | ||
48 | pd->stayopen |= f; | ||
49 | } | ||
50 | |||
51 | void | ||
52 | endprotoent_r(struct protoent_data *pd) | ||
53 | { | ||
54 | if (pd->fp) { | ||
55 | fclose(pd->fp); | ||
56 | pd->fp = NULL; | ||
57 | } | ||
58 | free(pd->aliases); | ||
59 | pd->aliases = NULL; | ||
60 | pd->maxaliases = 0; | ||
61 | free(pd->line); | ||
62 | pd->line = NULL; | ||
63 | pd->stayopen = 0; | ||
64 | } | ||
65 | |||
66 | int | ||
67 | getprotoent_r(struct protoent *pe, struct protoent_data *pd) | ||
68 | { | ||
69 | char *p, *cp, **q, *endp; | ||
70 | size_t len; | ||
71 | long l; | ||
72 | int serrno; | ||
73 | |||
74 | if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "r" )) == NULL) | ||
75 | return (-1); | ||
76 | again: | ||
77 | if ((p = fgetln(pd->fp, &len)) == NULL) | ||
78 | return (-1); | ||
79 | if (len == 0 || *p == '#' || *p == '\n') | ||
80 | goto again; | ||
81 | if (p[len-1] == '\n') | ||
82 | len--; | ||
83 | if ((cp = memchr(p, '#', len)) != NULL) | ||
84 | len = cp - p; | ||
85 | cp = realloc(pd->line, len + 1); | ||
86 | if (cp == NULL) | ||
87 | return (-1); | ||
88 | pd->line = pe->p_name = memcpy(cp, p, len); | ||
89 | cp[len] = '\0'; | ||
90 | cp = strpbrk(cp, " \t"); | ||
91 | if (cp == NULL) | ||
92 | goto again; | ||
93 | *cp++ = '\0'; | ||
94 | while (*cp == ' ' || *cp == '\t') | ||
95 | cp++; | ||
96 | p = strpbrk(cp, " \t"); | ||
97 | if (p != NULL) | ||
98 | *p++ = '\0'; | ||
99 | l = strtol(cp, &endp, 10); | ||
100 | if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX) | ||
101 | goto again; | ||
102 | pe->p_proto = l; | ||
103 | if (pd->aliases == NULL) { | ||
104 | pd->maxaliases = 5; | ||
105 | pd->aliases = calloc(pd->maxaliases, sizeof(char *)); | ||
106 | if (pd->aliases == NULL) { | ||
107 | serrno = errno; | ||
108 | endprotoent_r(pd); | ||
109 | errno = serrno; | ||
110 | return (-1); | ||
111 | } | ||
112 | } | ||
113 | q = pe->p_aliases = pd->aliases; | ||
114 | if (p != NULL) { | ||
115 | cp = p; | ||
116 | while (cp && *cp) { | ||
117 | if (*cp == ' ' || *cp == '\t') { | ||
118 | cp++; | ||
119 | continue; | ||
120 | } | ||
121 | if (q == &pe->p_aliases[pd->maxaliases - 1]) { | ||
122 | p = realloc(pe->p_aliases, | ||
123 | 2 * pd->maxaliases * sizeof(char *)); | ||
124 | if (p == NULL) { | ||
125 | serrno = errno; | ||
126 | endprotoent_r(pd); | ||
127 | errno = serrno; | ||
128 | return (-1); | ||
129 | } | ||
130 | pd->maxaliases *= 2; | ||
131 | q = (char **)p + (q - pe->p_aliases); | ||
132 | pe->p_aliases = pd->aliases = (char **)p; | ||
133 | } | ||
134 | *q++ = cp; | ||
135 | cp = strpbrk(cp, " \t"); | ||
136 | if (cp != NULL) | ||
137 | *cp++ = '\0'; | ||
138 | } | ||
139 | } | ||
140 | *q = NULL; | ||
141 | return (0); | ||
142 | } | ||
143 | |||
144 | struct protoent_data _protoent_data; /* shared with getproto{,name}.c */ | ||
145 | |||
146 | void | ||
147 | setprotoent(int f) | ||
148 | { | ||
149 | setprotoent_r(f, &_protoent_data); | ||
150 | } | ||
151 | |||
152 | void | ||
153 | endprotoent(void) | ||
154 | { | ||
155 | endprotoent_r(&_protoent_data); | ||
156 | } | ||
157 | |||
158 | struct protoent * | ||
159 | getprotoent(void) | ||
160 | { | ||
161 | static struct protoent proto; | ||
162 | |||
163 | if (getprotoent_r(&proto, &_protoent_data) != 0) | ||
164 | return (NULL); | ||
165 | return (&proto); | ||
166 | } | ||
diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c new file mode 100644 index 0000000000..749b6b3f13 --- /dev/null +++ b/src/lib/libc/net/getprotoname.c | |||
@@ -0,0 +1,67 @@ | |||
1 | /* $OpenBSD: getprotoname.c,v 1.7 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <netdb.h> | ||
32 | #include <stdio.h> | ||
33 | #include <string.h> | ||
34 | |||
35 | int | ||
36 | getprotobyname_r(const char *name, struct protoent *pe, | ||
37 | struct protoent_data *pd) | ||
38 | { | ||
39 | char **cp; | ||
40 | int error; | ||
41 | |||
42 | setprotoent_r(pd->stayopen, pd); | ||
43 | while ((error = getprotoent_r(pe, pd)) == 0) { | ||
44 | if (strcmp(pe->p_name, name) == 0) | ||
45 | break; | ||
46 | for (cp = pe->p_aliases; *cp != 0; cp++) | ||
47 | if (strcmp(*cp, name) == 0) | ||
48 | goto found; | ||
49 | } | ||
50 | found: | ||
51 | if (!pd->stayopen && pd->fp != NULL) { | ||
52 | fclose(pd->fp); | ||
53 | pd->fp = NULL; | ||
54 | } | ||
55 | return (error); | ||
56 | } | ||
57 | |||
58 | struct protoent * | ||
59 | getprotobyname(const char *name) | ||
60 | { | ||
61 | extern struct protoent_data _protoent_data; | ||
62 | static struct protoent proto; | ||
63 | |||
64 | if (getprotobyname_r(name, &proto, &_protoent_data) != 0) | ||
65 | return (NULL); | ||
66 | return (&proto); | ||
67 | } | ||
diff --git a/src/lib/libc/net/getrrsetbyname.3 b/src/lib/libc/net/getrrsetbyname.3 new file mode 100644 index 0000000000..677b2a4c2e --- /dev/null +++ b/src/lib/libc/net/getrrsetbyname.3 | |||
@@ -0,0 +1,165 @@ | |||
1 | .\" $OpenBSD: getrrsetbyname.3,v 1.18 2013/07/16 15:21:11 schwarze Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (C) 2000, 2001 Internet Software Consortium. | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM | ||
10 | .\" DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | ||
11 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL | ||
12 | .\" INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
13 | .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING | ||
14 | .\" FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | ||
15 | .\" NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | ||
16 | .\" WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | .\" | ||
18 | .Dd $Mdocdate: July 16 2013 $ | ||
19 | .Dt GETRRSETBYNAME 3 | ||
20 | .Os | ||
21 | .Sh NAME | ||
22 | .Nm freerrset , | ||
23 | .Nm getrrsetbyname | ||
24 | .Nd retrieve DNS records | ||
25 | .Sh SYNOPSIS | ||
26 | .In netdb.h | ||
27 | .Ft int | ||
28 | .Fn getrrsetbyname "const char *hostname" "unsigned int rdclass" \ | ||
29 | "unsigned int rdtype" "unsigned int flags" "struct rrsetinfo **res" | ||
30 | .Ft void | ||
31 | .Fn freerrset "struct rrsetinfo *rrset" | ||
32 | .Sh DESCRIPTION | ||
33 | .Fn getrrsetbyname | ||
34 | gets a set of resource records associated with a | ||
35 | .Fa hostname , | ||
36 | .Fa rdclass , | ||
37 | and | ||
38 | .Fa rdtype . | ||
39 | .Fa hostname | ||
40 | is a pointer to a NUL-terminated string. | ||
41 | The | ||
42 | .Fa flags | ||
43 | field is currently unused and must be zero. | ||
44 | .Pp | ||
45 | After a successful call to | ||
46 | .Fn getrrsetbyname , | ||
47 | .Fa *res | ||
48 | is a pointer to an | ||
49 | .Li rrsetinfo | ||
50 | structure, containing a list of one or more | ||
51 | .Li rdatainfo | ||
52 | structures containing resource records and potentially another list of | ||
53 | .Li rdatainfo | ||
54 | structures containing SIG resource records associated with those records. | ||
55 | The members | ||
56 | .Li rri_rdclass | ||
57 | and | ||
58 | .Li rri_rdtype | ||
59 | are copied from the parameters. | ||
60 | .Li rri_ttl | ||
61 | and | ||
62 | .Li rri_name | ||
63 | are properties of the obtained rrset. | ||
64 | The resource records contained in | ||
65 | .Li rri_rdatas | ||
66 | and | ||
67 | .Li rri_sigs | ||
68 | are in uncompressed DNS wire format. | ||
69 | Properties of the rdataset are represented in the | ||
70 | .Li rri_flags | ||
71 | bitfield. | ||
72 | If the | ||
73 | .Dv RRSET_VALIDATED | ||
74 | bit is set, the data has been DNSSEC | ||
75 | validated and the signatures verified. | ||
76 | .Pp | ||
77 | The following structures are used: | ||
78 | .Bd -literal -offset indent | ||
79 | struct rdatainfo { | ||
80 | unsigned int rdi_length; /* length of data */ | ||
81 | unsigned char *rdi_data; /* record data */ | ||
82 | }; | ||
83 | |||
84 | struct rrsetinfo { | ||
85 | unsigned int rri_flags; /* RRSET_VALIDATED ... */ | ||
86 | unsigned int rri_rdclass; /* class number */ | ||
87 | unsigned int rri_rdtype; /* RR type number */ | ||
88 | unsigned int rri_ttl; /* time to live */ | ||
89 | unsigned int rri_nrdatas; /* size of rdatas array */ | ||
90 | unsigned int rri_nsigs; /* size of sigs array */ | ||
91 | char *rri_name; /* canonical name */ | ||
92 | struct rdatainfo *rri_rdatas; /* individual records */ | ||
93 | struct rdatainfo *rri_sigs; /* individual signatures */ | ||
94 | }; | ||
95 | .Ed | ||
96 | .Pp | ||
97 | All of the information returned by | ||
98 | .Fn getrrsetbyname | ||
99 | is dynamically allocated: the | ||
100 | .Li rrsetinfo | ||
101 | and | ||
102 | .Li rdatainfo | ||
103 | structures, | ||
104 | and the canonical host name strings pointed to by the | ||
105 | .Li rrsetinfo | ||
106 | structure. | ||
107 | Memory allocated for the dynamically allocated structures created by | ||
108 | a successful call to | ||
109 | .Fn getrrsetbyname | ||
110 | is released by | ||
111 | .Fn freerrset . | ||
112 | .Li rrset | ||
113 | is a pointer to a | ||
114 | .Li struct rrsetinfo | ||
115 | created by a call to | ||
116 | .Fn getrrsetbyname . | ||
117 | .Pp | ||
118 | If the EDNS0 option is activated in | ||
119 | .Xr resolv.conf 5 , | ||
120 | .Fn getrrsetbyname | ||
121 | will request DNSSEC authentication using the EDNS0 DNSSEC OK (DO) bit. | ||
122 | .Sh RETURN VALUES | ||
123 | .Fn getrrsetbyname | ||
124 | returns zero on success, and one of the following error | ||
125 | codes if an error occurred: | ||
126 | .Bl -tag -width ERRSET_NOMEMORY | ||
127 | .It Bq Er ERRSET_NONAME | ||
128 | The name does not exist. | ||
129 | .It Bq Er ERRSET_NODATA | ||
130 | The name exists, but does not have data of the desired type. | ||
131 | .It Bq Er ERRSET_NOMEMORY | ||
132 | Memory could not be allocated. | ||
133 | .It Bq Er ERRSET_INVAL | ||
134 | A parameter is invalid. | ||
135 | .It Bq Er ERRSET_FAIL | ||
136 | Other failure. | ||
137 | .El | ||
138 | .Sh SEE ALSO | ||
139 | .Xr resolver 3 , | ||
140 | .Xr resolv.conf 5 , | ||
141 | .Xr named 8 | ||
142 | .Sh HISTORY | ||
143 | .Fn getrrsetbyname | ||
144 | first appeared in | ||
145 | .Ox 3.0 . | ||
146 | The API first appeared in ISC BIND version 9. | ||
147 | .Sh AUTHORS | ||
148 | .An Jakob Schlyter Aq Mt jakob@openbsd.org | ||
149 | .Sh CAVEATS | ||
150 | The | ||
151 | .Dv RRSET_VALIDATED | ||
152 | flag in | ||
153 | .Li rri_flags | ||
154 | is set if the AD (authenticated data) bit in the DNS answer is | ||
155 | set. | ||
156 | This flag | ||
157 | .Em should not | ||
158 | be trusted unless the transport between the nameserver and the resolver | ||
159 | is secure (e.g. IPsec, trusted network, loopback communication). | ||
160 | .Sh BUGS | ||
161 | The data in | ||
162 | .Li *rdi_data | ||
163 | should be returned in uncompressed wire format. | ||
164 | Currently, the data is in compressed format and the caller can't | ||
165 | uncompress since it doesn't have the full message. | ||
diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c new file mode 100644 index 0000000000..beb8943af6 --- /dev/null +++ b/src/lib/libc/net/getservbyname.c | |||
@@ -0,0 +1,70 @@ | |||
1 | /* $OpenBSD: getservbyname.c,v 1.10 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <netdb.h> | ||
32 | #include <stdio.h> | ||
33 | #include <string.h> | ||
34 | |||
35 | int | ||
36 | getservbyname_r(const char *name, const char *proto, struct servent *se, | ||
37 | struct servent_data *sd) | ||
38 | { | ||
39 | char **cp; | ||
40 | int error; | ||
41 | |||
42 | setservent_r(sd->stayopen, sd); | ||
43 | while ((error = getservent_r(se, sd)) == 0) { | ||
44 | if (strcmp(name, se->s_name) == 0) | ||
45 | goto gotname; | ||
46 | for (cp = se->s_aliases; *cp; cp++) | ||
47 | if (strcmp(name, *cp) == 0) | ||
48 | goto gotname; | ||
49 | continue; | ||
50 | gotname: | ||
51 | if (proto == 0 || strcmp(se->s_proto, proto) == 0) | ||
52 | break; | ||
53 | } | ||
54 | if (!sd->stayopen && sd->fp != NULL) { | ||
55 | fclose(sd->fp); | ||
56 | sd->fp = NULL; | ||
57 | } | ||
58 | return (error); | ||
59 | } | ||
60 | |||
61 | struct servent * | ||
62 | getservbyname(const char *name, const char *proto) | ||
63 | { | ||
64 | extern struct servent_data _servent_data; | ||
65 | static struct servent serv; | ||
66 | |||
67 | if (getservbyname_r(name, proto, &serv, &_servent_data) != 0) | ||
68 | return (NULL); | ||
69 | return (&serv); | ||
70 | } | ||
diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c new file mode 100644 index 0000000000..46679ba366 --- /dev/null +++ b/src/lib/libc/net/getservbyport.c | |||
@@ -0,0 +1,64 @@ | |||
1 | /* $OpenBSD: getservbyport.c,v 1.7 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <netdb.h> | ||
32 | #include <stdio.h> | ||
33 | #include <string.h> | ||
34 | |||
35 | int | ||
36 | getservbyport_r(int port, const char *proto, struct servent *se, | ||
37 | struct servent_data *sd) | ||
38 | { | ||
39 | int error; | ||
40 | |||
41 | setservent_r(sd->stayopen, sd); | ||
42 | while ((error = getservent_r(se, sd)) == 0) { | ||
43 | if (se->s_port != port) | ||
44 | continue; | ||
45 | if (proto == 0 || strcmp(se->s_proto, proto) == 0) | ||
46 | break; | ||
47 | } | ||
48 | if (!sd->stayopen && sd->fp != NULL) { | ||
49 | fclose(sd->fp); | ||
50 | sd->fp = NULL; | ||
51 | } | ||
52 | return (error); | ||
53 | } | ||
54 | |||
55 | struct servent * | ||
56 | getservbyport(int port, const char *proto) | ||
57 | { | ||
58 | extern struct servent_data _servent_data; | ||
59 | static struct servent serv; | ||
60 | |||
61 | if (getservbyport_r(port, proto, &serv, &_servent_data) != 0) | ||
62 | return (NULL); | ||
63 | return (&serv); | ||
64 | } | ||
diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3 new file mode 100644 index 0000000000..29dd3eb5f4 --- /dev/null +++ b/src/lib/libc/net/getservent.3 | |||
@@ -0,0 +1,220 @@ | |||
1 | .\" $OpenBSD: getservent.3,v 1.21 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: June 5 2013 $ | ||
31 | .Dt GETSERVENT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getservent , | ||
35 | .Nm getservent_r , | ||
36 | .Nm getservbyport , | ||
37 | .Nm getservbyport_r , | ||
38 | .Nm getservbyname , | ||
39 | .Nm getservbyname_r , | ||
40 | .Nm setservent , | ||
41 | .Nm setservent_r , | ||
42 | .Nm endservent , | ||
43 | .Nm endservent_r | ||
44 | .Nd get service entry | ||
45 | .Sh SYNOPSIS | ||
46 | .In netdb.h | ||
47 | .Ft struct servent * | ||
48 | .Fn getservent "void" | ||
49 | .Ft int | ||
50 | .Fn getservent_r "struct servent *servent" "struct servent_data *servent_data" | ||
51 | .Ft struct servent * | ||
52 | .Fn getservbyname "const char *name" "const char *proto" | ||
53 | .Ft int | ||
54 | .Fn getservbyname_r "const char *name" "const char *proto" "struct servent *servent" "struct servent_data *servent_data" | ||
55 | .Ft struct servent * | ||
56 | .Fn getservbyport "int port" "const char *proto" | ||
57 | .Ft int | ||
58 | .Fn getservbyport_r "int port" "const char *proto" "struct servent *servent" "struct servent_data *servent_data" | ||
59 | .Ft void | ||
60 | .Fn setservent "int stayopen" | ||
61 | .Ft void | ||
62 | .Fn setservent_r "int stayopen" "struct servent_data *servent_data" | ||
63 | .Ft void | ||
64 | .Fn endservent "void" | ||
65 | .Ft void | ||
66 | .Fn endservent_r "struct servent_data *servent_data" | ||
67 | .Sh DESCRIPTION | ||
68 | The | ||
69 | .Fn getservent , | ||
70 | .Fn getservbyname , | ||
71 | and | ||
72 | .Fn getservbyport | ||
73 | functions each return a pointer to an object with the following structure | ||
74 | containing the broken-out fields of a line in the network services database, | ||
75 | .Pa /etc/services . | ||
76 | .Bd -literal -offset indent | ||
77 | struct servent { | ||
78 | char *s_name; /* official name of service */ | ||
79 | char **s_aliases; /* alias list */ | ||
80 | int s_port; /* port service resides at */ | ||
81 | char *s_proto; /* protocol to use */ | ||
82 | }; | ||
83 | .Ed | ||
84 | .Pp | ||
85 | The members of this structure are: | ||
86 | .Bl -tag -width s_aliases | ||
87 | .It Fa s_name | ||
88 | The official name of the service. | ||
89 | .It Fa s_aliases | ||
90 | A null-terminated list of alternate names for the service. | ||
91 | .It Fa s_port | ||
92 | The port number at which the service resides. | ||
93 | Port numbers are returned in network byte order. | ||
94 | .It Fa s_proto | ||
95 | The name of the protocol to use when contacting the service. | ||
96 | .El | ||
97 | .Pp | ||
98 | The | ||
99 | .Fn getservent | ||
100 | function reads the next line of the file, opening the file if necessary. | ||
101 | .Pp | ||
102 | The | ||
103 | .Fn setservent | ||
104 | function opens and rewinds the file. | ||
105 | If the | ||
106 | .Fa stayopen | ||
107 | flag is non-zero, | ||
108 | the services database will not be closed after each call to | ||
109 | .Fn getservbyname | ||
110 | or | ||
111 | .Fn getservbyport . | ||
112 | .Pp | ||
113 | The | ||
114 | .Fn endservent | ||
115 | function closes the file. | ||
116 | .Pp | ||
117 | The | ||
118 | .Fn getservbyname | ||
119 | and | ||
120 | .Fn getservbyport | ||
121 | functions sequentially search from the beginning of the file until a | ||
122 | matching protocol name or port number (specified in network byte order) | ||
123 | is found, or until | ||
124 | .Dv EOF | ||
125 | is encountered. | ||
126 | If a protocol name is also supplied (non-null), | ||
127 | searches must also match the protocol. | ||
128 | .Pp | ||
129 | The | ||
130 | .Fn getservent_r , | ||
131 | .Fn getservbyport_r , | ||
132 | .Fn getservbyname_r , | ||
133 | .Fn setservent_r , | ||
134 | and | ||
135 | .Fn endservent_r | ||
136 | functions are reentrant versions of the above functions that take a | ||
137 | pointer to a | ||
138 | .Fa servent_data | ||
139 | structure which is used to store state information. | ||
140 | The structure must be zero-filled before it is used | ||
141 | and should be considered opaque for the sake of portability. | ||
142 | .Pp | ||
143 | The | ||
144 | .Fn getservent_r , | ||
145 | .Fn getservbyport_r , | ||
146 | and | ||
147 | .Fn getservbyname_r | ||
148 | functions | ||
149 | also take a pointer to a | ||
150 | .Fa servent | ||
151 | structure which is used to store the results of the database lookup. | ||
152 | .Sh RETURN VALUES | ||
153 | The | ||
154 | .Fn getservent , | ||
155 | .Fn getservbyport , | ||
156 | and | ||
157 | .Fn getservbyname | ||
158 | functions return a pointer to a | ||
159 | .Fa servent | ||
160 | structure on success or a null pointer if end-of-file | ||
161 | is reached or an error occurs. | ||
162 | .Pp | ||
163 | The | ||
164 | .Fn getservent_r , | ||
165 | .Fn getservbyport_r , | ||
166 | and | ||
167 | .Fn getservbyname_r | ||
168 | functions return 0 on success or \-1 if end-of-file | ||
169 | is reached or an error occurs. | ||
170 | .Sh FILES | ||
171 | .Bl -tag -width /etc/services -compact | ||
172 | .It Pa /etc/services | ||
173 | .El | ||
174 | .Sh SEE ALSO | ||
175 | .Xr getprotoent 3 , | ||
176 | .Xr services 5 | ||
177 | .Sh STANDARDS | ||
178 | The | ||
179 | .Fn getservent , | ||
180 | .Fn getservbynumber , | ||
181 | .Fn getservbyname , | ||
182 | .Fn setservent , | ||
183 | and | ||
184 | .Fn endservent | ||
185 | functions conform to | ||
186 | .St -p1003.1-2004 . | ||
187 | .Pp | ||
188 | The | ||
189 | .Fn getservent_r , | ||
190 | .Fn getservbyport_r , | ||
191 | .Fn getservbyname_r , | ||
192 | .Fn setservent_r , | ||
193 | and | ||
194 | .Fn endservent_r | ||
195 | functions are not currently standardized. | ||
196 | This implementation follows the API used by HP, IBM, and Digital. | ||
197 | .Sh HISTORY | ||
198 | The | ||
199 | .Fn getservent , | ||
200 | .Fn getservbyport , | ||
201 | .Fn getservbyname , | ||
202 | .Fn setservent , | ||
203 | and | ||
204 | .Fn endservent | ||
205 | functions appeared in | ||
206 | .Bx 4.2 . | ||
207 | .Pp | ||
208 | The | ||
209 | .Fn getservent_r , | ||
210 | .Fn getservbyport_r , | ||
211 | .Fn getservbyname_r , | ||
212 | .Fn setservent_r , | ||
213 | and | ||
214 | .Fn endservent_r | ||
215 | functions appeared in | ||
216 | .Ox 3.7 . | ||
217 | .Sh BUGS | ||
218 | The non-reentrant functions use static data storage; if the data is needed | ||
219 | for future use, it should be copied before any subsequent calls overwrite it. | ||
220 | Expecting port numbers to fit in a 32-bit quantity is probably naive. | ||
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c new file mode 100644 index 0000000000..c81a4cf3e2 --- /dev/null +++ b/src/lib/libc/net/getservent.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /* $OpenBSD: getservent.c,v 1.12 2007/09/02 15:19:17 deraadt Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/types.h> | ||
32 | #include <sys/socket.h> | ||
33 | |||
34 | #include <errno.h> | ||
35 | #include <limits.h> | ||
36 | #include <netdb.h> | ||
37 | #include <stdio.h> | ||
38 | #include <string.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | void | ||
42 | setservent_r(int f, struct servent_data *sd) | ||
43 | { | ||
44 | if (sd->fp == NULL) | ||
45 | sd->fp = fopen(_PATH_SERVICES, "r" ); | ||
46 | else | ||
47 | rewind(sd->fp); | ||
48 | sd->stayopen |= f; | ||
49 | } | ||
50 | |||
51 | void | ||
52 | endservent_r(struct servent_data *sd) | ||
53 | { | ||
54 | if (sd->fp) { | ||
55 | fclose(sd->fp); | ||
56 | sd->fp = NULL; | ||
57 | } | ||
58 | free(sd->aliases); | ||
59 | sd->aliases = NULL; | ||
60 | sd->maxaliases = 0; | ||
61 | free(sd->line); | ||
62 | sd->line = NULL; | ||
63 | sd->stayopen = 0; | ||
64 | } | ||
65 | |||
66 | int | ||
67 | getservent_r(struct servent *se, struct servent_data *sd) | ||
68 | { | ||
69 | char *p, *cp, **q, *endp; | ||
70 | size_t len; | ||
71 | long l; | ||
72 | int serrno; | ||
73 | |||
74 | if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "r" )) == NULL) | ||
75 | return (-1); | ||
76 | again: | ||
77 | if ((p = fgetln(sd->fp, &len)) == NULL) | ||
78 | return (-1); | ||
79 | if (len == 0 || *p == '#' || *p == '\n') | ||
80 | goto again; | ||
81 | if (p[len-1] == '\n') | ||
82 | len--; | ||
83 | if ((cp = memchr(p, '#', len)) != NULL) | ||
84 | len = cp - p; | ||
85 | cp = realloc(sd->line, len + 1); | ||
86 | if (cp == NULL) | ||
87 | return (-1); | ||
88 | sd->line = se->s_name = memcpy(cp, p, len); | ||
89 | cp[len] = '\0'; | ||
90 | p = strpbrk(cp, " \t"); | ||
91 | if (p == NULL) | ||
92 | goto again; | ||
93 | *p++ = '\0'; | ||
94 | while (*p == ' ' || *p == '\t') | ||
95 | p++; | ||
96 | cp = strpbrk(p, ",/"); | ||
97 | if (cp == NULL) | ||
98 | goto again; | ||
99 | *cp++ = '\0'; | ||
100 | l = strtol(p, &endp, 10); | ||
101 | if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX) | ||
102 | goto again; | ||
103 | se->s_port = htons((in_port_t)l); | ||
104 | se->s_proto = cp; | ||
105 | if (sd->aliases == NULL) { | ||
106 | sd->maxaliases = 10; | ||
107 | sd->aliases = calloc(sd->maxaliases, sizeof(char *)); | ||
108 | if (sd->aliases == NULL) { | ||
109 | serrno = errno; | ||
110 | endservent_r(sd); | ||
111 | errno = serrno; | ||
112 | return (-1); | ||
113 | } | ||
114 | } | ||
115 | q = se->s_aliases = sd->aliases; | ||
116 | cp = strpbrk(cp, " \t"); | ||
117 | if (cp != NULL) | ||
118 | *cp++ = '\0'; | ||
119 | while (cp && *cp) { | ||
120 | if (*cp == ' ' || *cp == '\t') { | ||
121 | cp++; | ||
122 | continue; | ||
123 | } | ||
124 | if (q == &se->s_aliases[sd->maxaliases - 1]) { | ||
125 | p = realloc(se->s_aliases, | ||
126 | 2 * sd->maxaliases * sizeof(char *)); | ||
127 | if (p == NULL) { | ||
128 | serrno = errno; | ||
129 | endservent_r(sd); | ||
130 | errno = serrno; | ||
131 | return (-1); | ||
132 | } | ||
133 | sd->maxaliases *= 2; | ||
134 | q = (char **)p + (q - se->s_aliases); | ||
135 | se->s_aliases = sd->aliases = (char **)p; | ||
136 | } | ||
137 | *q++ = cp; | ||
138 | cp = strpbrk(cp, " \t"); | ||
139 | if (cp != NULL) | ||
140 | *cp++ = '\0'; | ||
141 | } | ||
142 | *q = NULL; | ||
143 | return (0); | ||
144 | } | ||
145 | |||
146 | struct servent_data _servent_data; /* shared with getservby{name,port}.c */ | ||
147 | |||
148 | void | ||
149 | setservent(int f) | ||
150 | { | ||
151 | setservent_r(f, &_servent_data); | ||
152 | } | ||
153 | |||
154 | void | ||
155 | endservent(void) | ||
156 | { | ||
157 | endservent_r(&_servent_data); | ||
158 | } | ||
159 | |||
160 | struct servent * | ||
161 | getservent(void) | ||
162 | { | ||
163 | static struct servent serv; | ||
164 | |||
165 | if (getservent_r(&serv, &_servent_data) != 0) | ||
166 | return (NULL); | ||
167 | return (&serv); | ||
168 | } | ||
diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c new file mode 100644 index 0000000000..7787115a9d --- /dev/null +++ b/src/lib/libc/net/herror.c | |||
@@ -0,0 +1,106 @@ | |||
1 | /* $OpenBSD: herror.c,v 1.8 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1987, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1987, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. 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 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #include <sys/types.h> | ||
55 | #include <sys/param.h> | ||
56 | #include <sys/uio.h> | ||
57 | #include <netdb.h> | ||
58 | #include <unistd.h> | ||
59 | #include <string.h> | ||
60 | |||
61 | const char * const h_errlist[] = { | ||
62 | "Resolver Error 0 (no error)", | ||
63 | "Unknown host", /* 1 HOST_NOT_FOUND */ | ||
64 | "Host name lookup failure", /* 2 TRY_AGAIN */ | ||
65 | "Unknown server error", /* 3 NO_RECOVERY */ | ||
66 | "No address associated with name", /* 4 NO_ADDRESS */ | ||
67 | }; | ||
68 | const int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] }; | ||
69 | |||
70 | extern int h_errno; | ||
71 | |||
72 | /* | ||
73 | * herror -- | ||
74 | * print the error indicated by the h_errno value. | ||
75 | */ | ||
76 | void | ||
77 | herror(const char *s) | ||
78 | { | ||
79 | struct iovec iov[4]; | ||
80 | struct iovec *v = iov; | ||
81 | |||
82 | if (s && *s) { | ||
83 | v->iov_base = (char *)s; | ||
84 | v->iov_len = strlen(s); | ||
85 | v++; | ||
86 | v->iov_base = ": "; | ||
87 | v->iov_len = 2; | ||
88 | v++; | ||
89 | } | ||
90 | v->iov_base = (char *)hstrerror(h_errno); | ||
91 | v->iov_len = strlen(v->iov_base); | ||
92 | v++; | ||
93 | v->iov_base = "\n"; | ||
94 | v->iov_len = 1; | ||
95 | writev(STDERR_FILENO, iov, (v - iov) + 1); | ||
96 | } | ||
97 | |||
98 | const char * | ||
99 | hstrerror(int err) | ||
100 | { | ||
101 | if (err < 0) | ||
102 | return ("Resolver internal error"); | ||
103 | else if (err < h_nerr) | ||
104 | return (h_errlist[err]); | ||
105 | return ("Unknown resolver error"); | ||
106 | } | ||
diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c new file mode 100644 index 0000000000..5ab4189597 --- /dev/null +++ b/src/lib/libc/net/htonl.c | |||
@@ -0,0 +1,21 @@ | |||
1 | /* $OpenBSD: htonl.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <sys/types.h> | ||
8 | #include <machine/endian.h> | ||
9 | |||
10 | #undef htonl | ||
11 | |||
12 | u_int32_t | ||
13 | htonl(u_int32_t x) | ||
14 | { | ||
15 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
16 | u_char *s = (u_char *)&x; | ||
17 | return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); | ||
18 | #else | ||
19 | return x; | ||
20 | #endif | ||
21 | } | ||
diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c new file mode 100644 index 0000000000..c8b73fdbb7 --- /dev/null +++ b/src/lib/libc/net/htons.c | |||
@@ -0,0 +1,21 @@ | |||
1 | /* $OpenBSD: htons.c,v 1.8 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <sys/types.h> | ||
8 | #include <machine/endian.h> | ||
9 | |||
10 | #undef htons | ||
11 | |||
12 | u_int16_t | ||
13 | htons(u_int16_t x) | ||
14 | { | ||
15 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
16 | u_char *s = (u_char *) &x; | ||
17 | return (u_int16_t)(s[0] << 8 | s[1]); | ||
18 | #else | ||
19 | return x; | ||
20 | #endif | ||
21 | } | ||
diff --git a/src/lib/libc/net/if_indextoname.3 b/src/lib/libc/net/if_indextoname.3 new file mode 100644 index 0000000000..5e6e7f2fdb --- /dev/null +++ b/src/lib/libc/net/if_indextoname.3 | |||
@@ -0,0 +1,143 @@ | |||
1 | .\" $OpenBSD: if_indextoname.3,v 1.15 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" Copyright (c) 1983, 1991, 1993 | ||
3 | .\" The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors | ||
14 | .\" may be used to endorse or promote products derived from this software | ||
15 | .\" without specific prior written permission. | ||
16 | .\" | ||
17 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | .\" SUCH DAMAGE. | ||
28 | .\" | ||
29 | .\" From: @(#)rcmd.3 8.1 (Berkeley) 6/4/93 | ||
30 | .\" | ||
31 | .Dd $Mdocdate: January 21 2014 $ | ||
32 | .Dt IF_NAMETOINDEX 3 | ||
33 | .Os | ||
34 | .Sh NAME | ||
35 | .Nm if_nametoindex , | ||
36 | .Nm if_indextoname , | ||
37 | .Nm if_nameindex , | ||
38 | .Nm if_freenameindex | ||
39 | .Nd convert interface index to name, and vice versa | ||
40 | .Sh SYNOPSIS | ||
41 | .In sys/types.h | ||
42 | .In sys/socket.h | ||
43 | .In net/if.h | ||
44 | .Ft "unsigned int" | ||
45 | .Fn if_nametoindex "const char *ifname" | ||
46 | .Ft "char *" | ||
47 | .Fn if_indextoname "unsigned int ifindex" "char *ifname" | ||
48 | .Ft "struct if_nameindex *" | ||
49 | .Fn if_nameindex "void" | ||
50 | .Ft "void" | ||
51 | .Fn if_freenameindex "struct if_nameindex *ptr" | ||
52 | .Sh DESCRIPTION | ||
53 | These functions map interface indexes to interface names (such as | ||
54 | .Dq lo0 ) , | ||
55 | and vice versa. | ||
56 | .Pp | ||
57 | The | ||
58 | .Fn if_nametoindex | ||
59 | function converts an interface name specified by the | ||
60 | .Fa ifname | ||
61 | argument to an interface index (positive integer value). | ||
62 | If the specified interface does not exist, 0 will be returned. | ||
63 | .Pp | ||
64 | .Fn if_indextoname | ||
65 | converts an interface index specified by the | ||
66 | .Fa ifindex | ||
67 | argument to an interface name. | ||
68 | The | ||
69 | .Fa ifname | ||
70 | argument must point to a buffer of at least | ||
71 | .Dv IF_NAMESIZE | ||
72 | bytes into which the interface name corresponding to the specified index is | ||
73 | returned. | ||
74 | .Pf ( Dv IF_NAMESIZE | ||
75 | is also defined in | ||
76 | .In net/if.h | ||
77 | and its value includes a terminating NUL byte at the end of the | ||
78 | interface name.) | ||
79 | This pointer is also the return value of the function. | ||
80 | If there is no interface corresponding to the specified index, | ||
81 | .Dv NULL | ||
82 | is returned. | ||
83 | .Pp | ||
84 | .Fn if_nameindex | ||
85 | returns an array of | ||
86 | .Vt if_nameindex | ||
87 | structures. | ||
88 | .Vt if_nameindex | ||
89 | is also defined in | ||
90 | .In net/if.h , | ||
91 | and is as follows: | ||
92 | .Bd -literal -offset indent | ||
93 | struct if_nameindex { | ||
94 | unsigned int if_index; /* 1, 2, ... */ | ||
95 | char *if_name; /* NUL-terminated name */ | ||
96 | }; | ||
97 | .Ed | ||
98 | .Pp | ||
99 | The end of the array of structures is indicated by a structure with | ||
100 | an | ||
101 | .Fa if_index | ||
102 | of 0 and an | ||
103 | .Fa if_name | ||
104 | of | ||
105 | .Dv NULL . | ||
106 | The function returns a null pointer on error. | ||
107 | The memory used for this array of structures along with the interface | ||
108 | names pointed to by the | ||
109 | .Fa if_name | ||
110 | members is obtained dynamically. | ||
111 | This memory is freed by the | ||
112 | .Fn if_freenameindex | ||
113 | function. | ||
114 | .Pp | ||
115 | .Fn if_freenameindex | ||
116 | takes a pointer that was returned by | ||
117 | .Fn if_nameindex | ||
118 | as argument | ||
119 | .Pq Fa ptr , | ||
120 | and it reclaims the region allocated. | ||
121 | .Sh DIAGNOSTICS | ||
122 | .Fn if_nametoindex | ||
123 | returns 0 on error, positive integer on success. | ||
124 | .Fn if_indextoname | ||
125 | and | ||
126 | .Fn if_nameindex | ||
127 | return | ||
128 | .Dv NULL | ||
129 | on errors. | ||
130 | .Sh SEE ALSO | ||
131 | .Xr getifaddrs 3 , | ||
132 | .Xr networking 4 | ||
133 | .Sh STANDARDS | ||
134 | .Rs | ||
135 | .%A R. Gilligan | ||
136 | .%A S. Thomson | ||
137 | .%A J. Bound | ||
138 | .%A J. McCann | ||
139 | .%A W. Stevens | ||
140 | .%D February 2003 | ||
141 | .%R RFC 3493 | ||
142 | .%T Basic Socket Interface Extensions for IPv6 | ||
143 | .Re | ||
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c new file mode 100644 index 0000000000..f99e52e387 --- /dev/null +++ b/src/lib/libc/net/if_indextoname.c | |||
@@ -0,0 +1,86 @@ | |||
1 | /* $OpenBSD: if_indextoname.c,v 1.9 2002/03/07 22:40:23 millert Exp $ */ | ||
2 | /* $KAME: if_indextoname.c,v 1.6 2000/11/07 22:33:25 jinmei Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1997, 2000 | ||
6 | * Berkeley Software Design, Inc. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
24 | * SUCH DAMAGE. | ||
25 | * | ||
26 | * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp | ||
27 | */ | ||
28 | |||
29 | #include <sys/types.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if_dl.h> | ||
32 | #include <net/if.h> | ||
33 | #include <ifaddrs.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <errno.h> | ||
37 | |||
38 | /* | ||
39 | * From RFC 2533: | ||
40 | * | ||
41 | * The second function maps an interface index into its corresponding | ||
42 | * name. | ||
43 | * | ||
44 | * #include <net/if.h> | ||
45 | * | ||
46 | * char *if_indextoname(unsigned int ifindex, char *ifname); | ||
47 | * | ||
48 | * The ifname argument must point to a buffer of at least IF_NAMESIZE | ||
49 | * bytes into which the interface name corresponding to the specified | ||
50 | * index is returned. (IF_NAMESIZE is also defined in <net/if.h> and | ||
51 | * its value includes a terminating null byte at the end of the | ||
52 | * interface name.) This pointer is also the return value of the | ||
53 | * function. If there is no interface corresponding to the specified | ||
54 | * index, NULL is returned, and errno is set to ENXIO, if there was a | ||
55 | * system error (such as running out of memory), if_indextoname returns | ||
56 | * NULL and errno would be set to the proper value (e.g., ENOMEM). | ||
57 | */ | ||
58 | |||
59 | char * | ||
60 | if_indextoname(unsigned int ifindex, char *ifname) | ||
61 | { | ||
62 | struct ifaddrs *ifaddrs, *ifa; | ||
63 | int error = 0; | ||
64 | |||
65 | if (getifaddrs(&ifaddrs) < 0) | ||
66 | return(NULL); /* getifaddrs properly set errno */ | ||
67 | |||
68 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
69 | if (ifa->ifa_addr && | ||
70 | ifa->ifa_addr->sa_family == AF_LINK && | ||
71 | ifindex == ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index) | ||
72 | break; | ||
73 | } | ||
74 | |||
75 | if (ifa == NULL) { | ||
76 | error = ENXIO; | ||
77 | ifname = NULL; | ||
78 | } | ||
79 | else | ||
80 | strlcpy(ifname, ifa->ifa_name, IFNAMSIZ); | ||
81 | |||
82 | freeifaddrs(ifaddrs); | ||
83 | |||
84 | errno = error; | ||
85 | return(ifname); | ||
86 | } | ||
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c new file mode 100644 index 0000000000..c5a771869b --- /dev/null +++ b/src/lib/libc/net/if_nameindex.c | |||
@@ -0,0 +1,146 @@ | |||
1 | /* $OpenBSD: if_nameindex.c,v 1.10 2010/09/24 13:29:29 claudio Exp $ */ | ||
2 | /* $KAME: if_nameindex.c,v 1.7 2000/11/24 08:17:20 itojun Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1997, 2000 | ||
6 | * Berkeley Software Design, Inc. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
24 | * SUCH DAMAGE. | ||
25 | * | ||
26 | * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp | ||
27 | */ | ||
28 | |||
29 | #include <sys/types.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if_dl.h> | ||
32 | #include <net/if.h> | ||
33 | #include <ifaddrs.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | /* | ||
38 | * From RFC 2553: | ||
39 | * | ||
40 | * 4.3 Return All Interface Names and Indexes | ||
41 | * | ||
42 | * The if_nameindex structure holds the information about a single | ||
43 | * interface and is defined as a result of including the <net/if.h> | ||
44 | * header. | ||
45 | * | ||
46 | * struct if_nameindex { | ||
47 | * unsigned int if_index; | ||
48 | * char *if_name; | ||
49 | * }; | ||
50 | * | ||
51 | * The final function returns an array of if_nameindex structures, one | ||
52 | * structure per interface. | ||
53 | * | ||
54 | * struct if_nameindex *if_nameindex(void); | ||
55 | * | ||
56 | * The end of the array of structures is indicated by a structure with | ||
57 | * an if_index of 0 and an if_name of NULL. The function returns a NULL | ||
58 | * pointer upon an error, and would set errno to the appropriate value. | ||
59 | * | ||
60 | * The memory used for this array of structures along with the interface | ||
61 | * names pointed to by the if_name members is obtained dynamically. | ||
62 | * This memory is freed by the next function. | ||
63 | * | ||
64 | * 4.4. Free Memory | ||
65 | * | ||
66 | * The following function frees the dynamic memory that was allocated by | ||
67 | * if_nameindex(). | ||
68 | * | ||
69 | * #include <net/if.h> | ||
70 | * | ||
71 | * void if_freenameindex(struct if_nameindex *ptr); | ||
72 | * | ||
73 | * The argument to this function must be a pointer that was returned by | ||
74 | * if_nameindex(). | ||
75 | */ | ||
76 | |||
77 | struct if_nameindex * | ||
78 | if_nameindex(void) | ||
79 | { | ||
80 | struct ifaddrs *ifaddrs, *ifa; | ||
81 | unsigned int ni; | ||
82 | size_t nbytes; | ||
83 | struct if_nameindex *ifni, *ifni2; | ||
84 | char *cp; | ||
85 | |||
86 | if (getifaddrs(&ifaddrs) < 0) | ||
87 | return(NULL); | ||
88 | |||
89 | /* | ||
90 | * First, find out how many interfaces there are, and how | ||
91 | * much space we need for the string names. | ||
92 | */ | ||
93 | ni = 0; | ||
94 | nbytes = 0; | ||
95 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
96 | if (ifa->ifa_addr && | ||
97 | ifa->ifa_addr->sa_family == AF_LINK) { | ||
98 | nbytes += strlen(ifa->ifa_name) + 1; | ||
99 | ni++; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | /* | ||
104 | * Next, allocate a chunk of memory, use the first part | ||
105 | * for the array of structures, and the last part for | ||
106 | * the strings. | ||
107 | */ | ||
108 | cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes); | ||
109 | ifni = (struct if_nameindex *)cp; | ||
110 | if (ifni == NULL) | ||
111 | goto out; | ||
112 | cp += (ni + 1) * sizeof(struct if_nameindex); | ||
113 | |||
114 | /* | ||
115 | * Now just loop through the list of interfaces again, | ||
116 | * filling in the if_nameindex array and making copies | ||
117 | * of all the strings. | ||
118 | */ | ||
119 | ifni2 = ifni; | ||
120 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
121 | if (ifa->ifa_addr && | ||
122 | ifa->ifa_addr->sa_family == AF_LINK) { | ||
123 | ifni2->if_index = | ||
124 | ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index; | ||
125 | ifni2->if_name = cp; | ||
126 | nbytes = strlen(ifa->ifa_name) + 1; | ||
127 | memcpy(cp, ifa->ifa_name, nbytes); | ||
128 | ifni2++; | ||
129 | cp += nbytes; | ||
130 | } | ||
131 | } | ||
132 | /* | ||
133 | * Finally, don't forget to terminate the array. | ||
134 | */ | ||
135 | ifni2->if_index = 0; | ||
136 | ifni2->if_name = NULL; | ||
137 | out: | ||
138 | freeifaddrs(ifaddrs); | ||
139 | return(ifni); | ||
140 | } | ||
141 | |||
142 | void | ||
143 | if_freenameindex(struct if_nameindex *ptr) | ||
144 | { | ||
145 | free(ptr); | ||
146 | } | ||
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c new file mode 100644 index 0000000000..8bd792b949 --- /dev/null +++ b/src/lib/libc/net/if_nametoindex.c | |||
@@ -0,0 +1,81 @@ | |||
1 | /* $OpenBSD: if_nametoindex.c,v 1.8 2002/03/07 22:40:23 millert Exp $ */ | ||
2 | /* $KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1997, 2000 | ||
6 | * Berkeley Software Design, Inc. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
24 | * SUCH DAMAGE. | ||
25 | * | ||
26 | * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp | ||
27 | */ | ||
28 | |||
29 | #include <sys/types.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if.h> | ||
32 | #include <net/if_dl.h> | ||
33 | #include <ifaddrs.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <errno.h> | ||
37 | |||
38 | /* | ||
39 | * From RFC 2553: | ||
40 | * | ||
41 | * 4.1 Name-to-Index | ||
42 | * | ||
43 | * | ||
44 | * The first function maps an interface name into its corresponding | ||
45 | * index. | ||
46 | * | ||
47 | * #include <net/if.h> | ||
48 | * | ||
49 | * unsigned int if_nametoindex(const char *ifname); | ||
50 | * | ||
51 | * If the specified interface name does not exist, the return value is | ||
52 | * 0, and errno is set to ENXIO. If there was a system error (such as | ||
53 | * running out of memory), the return value is 0 and errno is set to the | ||
54 | * proper value (e.g., ENOMEM). | ||
55 | */ | ||
56 | |||
57 | unsigned int | ||
58 | if_nametoindex(const char *ifname) | ||
59 | { | ||
60 | struct ifaddrs *ifaddrs, *ifa; | ||
61 | unsigned int ni; | ||
62 | |||
63 | if (getifaddrs(&ifaddrs) < 0) | ||
64 | return(0); | ||
65 | |||
66 | ni = 0; | ||
67 | |||
68 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
69 | if (ifa->ifa_addr && | ||
70 | ifa->ifa_addr->sa_family == AF_LINK && | ||
71 | strcmp(ifa->ifa_name, ifname) == 0) { | ||
72 | ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index; | ||
73 | break; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | freeifaddrs(ifaddrs); | ||
78 | if (!ni) | ||
79 | errno = ENXIO; | ||
80 | return(ni); | ||
81 | } | ||
diff --git a/src/lib/libc/net/inet.3 b/src/lib/libc/net/inet.3 new file mode 100644 index 0000000000..e56ca0a59a --- /dev/null +++ b/src/lib/libc/net/inet.3 | |||
@@ -0,0 +1,355 @@ | |||
1 | .\" $OpenBSD: inet.3,v 1.26 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" $NetBSD: inet.3,v 1.7 1997/06/18 02:25:24 lukem Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1983, 1990, 1991, 1993 | ||
5 | .\" The Regents of the University of California. All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" @(#)inet.3 8.1 (Berkeley) 6/4/93 | ||
32 | .\" | ||
33 | .Dd $Mdocdate: June 5 2013 $ | ||
34 | .Dt INET 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm inet_aton , | ||
38 | .Nm inet_addr , | ||
39 | .Nm inet_network , | ||
40 | .Nm inet_pton , | ||
41 | .Nm inet_ntop , | ||
42 | .Nm inet_ntoa , | ||
43 | .Nm inet_makeaddr , | ||
44 | .Nm inet_netof , | ||
45 | .Nm inet_lnaof | ||
46 | .Nd Internet address manipulation routines | ||
47 | .Sh SYNOPSIS | ||
48 | .In sys/types.h | ||
49 | .In sys/socket.h | ||
50 | .In netinet/in.h | ||
51 | .In arpa/inet.h | ||
52 | .Ft int | ||
53 | .Fn inet_aton "const char *cp" "struct in_addr *addr" | ||
54 | .Ft in_addr_t | ||
55 | .Fn inet_addr "const char *cp" | ||
56 | .Ft in_addr_t | ||
57 | .Fn inet_network "const char *cp" | ||
58 | .Ft int | ||
59 | .Fn inet_pton "int af" "const char *src" "void *dst" | ||
60 | .Ft const char * | ||
61 | .Fn inet_ntop "int af" "const void *src" "char *dst" "socklen_t size" | ||
62 | .Ft char * | ||
63 | .Fn inet_ntoa "struct in_addr in" | ||
64 | .Ft struct in_addr | ||
65 | .Fn inet_makeaddr "in_addr_t net" "in_addr_t lna" | ||
66 | .Ft in_addr_t | ||
67 | .Fn inet_netof "struct in_addr in" | ||
68 | .Ft in_addr_t | ||
69 | .Fn inet_lnaof "struct in_addr in" | ||
70 | .Sh DESCRIPTION | ||
71 | The routines | ||
72 | .Fn inet_aton , | ||
73 | .Fn inet_addr , | ||
74 | and | ||
75 | .Fn inet_network | ||
76 | interpret character strings representing | ||
77 | numbers expressed in the Internet standard | ||
78 | .Dq dot | ||
79 | notation. | ||
80 | .Pp | ||
81 | The | ||
82 | .Fn inet_aton | ||
83 | routine interprets the specified character string as an Internet address, | ||
84 | placing the address into the structure provided. | ||
85 | It returns 1 if the string was successfully interpreted, | ||
86 | or 0 if the string was invalid. | ||
87 | .Pp | ||
88 | The | ||
89 | .Fn inet_addr | ||
90 | and | ||
91 | .Fn inet_network | ||
92 | functions return numbers suitable for use | ||
93 | as Internet addresses and Internet network | ||
94 | numbers, respectively. | ||
95 | Both functions return the constant | ||
96 | .Dv INADDR_NONE | ||
97 | if the specified character string is malformed. | ||
98 | .Pp | ||
99 | The | ||
100 | .Fn inet_pton | ||
101 | function converts a presentation format address (that is, printable form | ||
102 | as held in a character string) to network format (usually a | ||
103 | .Li struct in_addr | ||
104 | or some other internal binary representation, in network byte order). | ||
105 | It returns 1 if the address was valid for the specified address family; | ||
106 | 0 if the address wasn't parseable in the specified address family; or \-1 | ||
107 | if some system error occurred (in which case | ||
108 | .Va errno | ||
109 | will have been set). | ||
110 | This function is presently valid for | ||
111 | .Dv AF_INET | ||
112 | and | ||
113 | .Dv AF_INET6 . | ||
114 | .Pp | ||
115 | The function | ||
116 | .Fn inet_ntop | ||
117 | converts an address from network format (usually a | ||
118 | .Li struct in_addr | ||
119 | or some other binary form, in network byte order) to presentation format | ||
120 | (suitable for external display purposes). | ||
121 | It returns | ||
122 | .Dv NULL | ||
123 | if a system | ||
124 | error occurs (in which case, | ||
125 | .Va errno | ||
126 | will have been set), or it returns a pointer to the destination string. | ||
127 | .Pp | ||
128 | The routine | ||
129 | .Fn inet_ntoa | ||
130 | takes an Internet address and returns an | ||
131 | ASCII string representing the address in dot notation. | ||
132 | .Pp | ||
133 | The routine | ||
134 | .Fn inet_makeaddr | ||
135 | takes an Internet network number and a local | ||
136 | network address and constructs an Internet address | ||
137 | from it. | ||
138 | .Pp | ||
139 | The routines | ||
140 | .Fn inet_netof | ||
141 | and | ||
142 | .Fn inet_lnaof | ||
143 | break apart Internet host addresses, returning | ||
144 | the network number and local network address part, | ||
145 | respectively. | ||
146 | .Pp | ||
147 | All Internet addresses are returned in network | ||
148 | order (bytes ordered from left to right). | ||
149 | All network numbers and local address parts are | ||
150 | returned as machine format integer values. | ||
151 | .Sh INTERNET ADDRESSES (IP VERSION 4) | ||
152 | Values specified using dot notation take one of the following forms: | ||
153 | .Bd -literal -offset indent | ||
154 | a.b.c.d | ||
155 | a.b.c | ||
156 | a.b | ||
157 | a | ||
158 | .Ed | ||
159 | .Pp | ||
160 | When four parts are specified, each is interpreted | ||
161 | as a byte of data and assigned, from left to right, | ||
162 | to the four bytes of an Internet address. | ||
163 | Note that when an Internet address is viewed as a 32-bit | ||
164 | integer quantity on a system that uses little-endian | ||
165 | byte order | ||
166 | (such as the Intel 386, 486 and Pentium processors) | ||
167 | the bytes referred to above appear as | ||
168 | .Dq Li d.c.b.a . | ||
169 | That is, little-endian bytes are ordered from right to left. | ||
170 | .Pp | ||
171 | When a three part address is specified, the last | ||
172 | part is interpreted as a 16-bit quantity and placed | ||
173 | in the rightmost two bytes of the network address. | ||
174 | This makes the three part address format convenient | ||
175 | for specifying Class B network addresses as | ||
176 | .Dq Li 128.net.host . | ||
177 | .Pp | ||
178 | When a two part address is supplied, the last part | ||
179 | is interpreted as a 24-bit quantity and placed in | ||
180 | the rightmost three bytes of the network address. | ||
181 | This makes the two part address format convenient | ||
182 | for specifying Class A network addresses as | ||
183 | .Dq Li net.host . | ||
184 | .Pp | ||
185 | When only one part is given, the value is stored | ||
186 | directly in the network address without any byte | ||
187 | rearrangement. | ||
188 | .Pp | ||
189 | All numbers supplied as | ||
190 | .Dq parts | ||
191 | in a dot notation | ||
192 | may be decimal, octal, or hexadecimal, as specified | ||
193 | in the C language (i.e., a leading 0x or 0X implies | ||
194 | hexadecimal; a leading 0 implies octal; | ||
195 | otherwise, the number is interpreted as decimal). | ||
196 | .Sh INTERNET ADDRESSES (IP VERSION 6) | ||
197 | In order to support scoped IPv6 addresses, | ||
198 | .Xr getaddrinfo 3 | ||
199 | and | ||
200 | .Xr getnameinfo 3 | ||
201 | are recommended rather than the functions presented here. | ||
202 | .Pp | ||
203 | The presentation format of an IPv6 address is given in RFC 4291: | ||
204 | .Pp | ||
205 | There are three conventional forms for representing IPv6 addresses as | ||
206 | text strings: | ||
207 | .Bl -enum | ||
208 | .It | ||
209 | The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the | ||
210 | hexadecimal values of the eight 16-bit pieces of the address. | ||
211 | Examples: | ||
212 | .Bd -literal -offset indent | ||
213 | FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 | ||
214 | 1080:0:0:0:8:800:200C:417A | ||
215 | .Ed | ||
216 | .Pp | ||
217 | Note that it is not necessary to write the leading zeros in an | ||
218 | individual field, but there must be at least one numeral in | ||
219 | every field (except for the case described in 2.). | ||
220 | .It | ||
221 | Due to the method of allocating certain styles of IPv6 | ||
222 | addresses, it will be common for addresses to contain long | ||
223 | strings of zero bits. | ||
224 | In order to make writing addresses | ||
225 | containing zero bits easier, a special syntax is available to | ||
226 | compress the zeros. | ||
227 | The use of | ||
228 | .Dq \&:\&: | ||
229 | indicates multiple groups | ||
230 | of 16 bits of zeros. | ||
231 | The | ||
232 | .Dq \&:\&: | ||
233 | can only appear once in an | ||
234 | address. | ||
235 | The | ||
236 | .Dq \&:\&: | ||
237 | can also be used to compress the leading and/or trailing zeros in an address. | ||
238 | .Pp | ||
239 | For example the following addresses: | ||
240 | .Bd -literal -offset indent | ||
241 | 1080:0:0:0:8:800:200C:417A a unicast address | ||
242 | FF01:0:0:0:0:0:0:43 a multicast address | ||
243 | 0:0:0:0:0:0:0:1 the loopback address | ||
244 | 0:0:0:0:0:0:0:0 the unspecified addresses | ||
245 | .Ed | ||
246 | .Pp | ||
247 | may be represented as: | ||
248 | .Bd -literal -offset indent | ||
249 | 1080::8:800:200C:417A a unicast address | ||
250 | FF01::43 a multicast address | ||
251 | ::1 the loopback address | ||
252 | :: the unspecified addresses | ||
253 | .Ed | ||
254 | .It | ||
255 | An alternative form that is sometimes more convenient when | ||
256 | dealing with a mixed environment of IPv4 and IPv6 nodes is | ||
257 | x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values | ||
258 | of the six high-order 16-bit pieces of the address, and the 'd's | ||
259 | are the decimal values of the four low-order 8-bit pieces of the | ||
260 | address (standard IPv4 representation). | ||
261 | Examples: | ||
262 | .Bd -literal -offset indent | ||
263 | 0:0:0:0:0:0:13.1.68.3 | ||
264 | 0:0:0:0:0:FFFF:129.144.52.38 | ||
265 | .Ed | ||
266 | .Pp | ||
267 | or in compressed form: | ||
268 | .Bd -literal -offset indent | ||
269 | ::13.1.68.3 | ||
270 | ::FFFF:129.144.52.38 | ||
271 | .Ed | ||
272 | .El | ||
273 | .Sh SEE ALSO | ||
274 | .Xr byteorder 3 , | ||
275 | .Xr gethostbyname 3 , | ||
276 | .Xr getnetent 3 , | ||
277 | .Xr inet_net 3 , | ||
278 | .Xr hosts 5 , | ||
279 | .Xr networks 5 | ||
280 | .Sh STANDARDS | ||
281 | The | ||
282 | .Nm inet_ntop | ||
283 | and | ||
284 | .Nm inet_pton | ||
285 | functions conform to the IETF IPv6 BSD API and address formatting | ||
286 | specifications. | ||
287 | Note that | ||
288 | .Nm inet_pton | ||
289 | does not accept 1-, 2-, or 3-part dotted addresses; all four parts | ||
290 | must be specified. | ||
291 | This is a narrower input set than that accepted by | ||
292 | .Nm inet_aton . | ||
293 | .Pp | ||
294 | .Rs | ||
295 | .%A R. Gilligan | ||
296 | .%A S. Thomson | ||
297 | .%A J. Bound | ||
298 | .%A J. McCann | ||
299 | .%A W. Stevens | ||
300 | .%D February 2003 | ||
301 | .%R RFC 3493 | ||
302 | .%T Basic Socket Interface Extensions for IPv6 | ||
303 | .Re | ||
304 | .Pp | ||
305 | .Rs | ||
306 | .%A R. Hinden | ||
307 | .%A S. Deering | ||
308 | .%D February 2006 | ||
309 | .%R RFC 4291 | ||
310 | .%T IP Version 6 Addressing Architecture | ||
311 | .Re | ||
312 | .Sh HISTORY | ||
313 | The | ||
314 | .Nm inet_addr , | ||
315 | .Nm inet_network , | ||
316 | .Nm inet_makeaddr , | ||
317 | .Nm inet_lnaof , | ||
318 | and | ||
319 | .Nm inet_netof | ||
320 | functions appeared in | ||
321 | .Bx 4.2 . | ||
322 | The | ||
323 | .Nm inet_aton | ||
324 | and | ||
325 | .Nm inet_ntoa | ||
326 | functions appeared in | ||
327 | .Bx 4.3 . | ||
328 | The | ||
329 | .Nm inet_pton | ||
330 | and | ||
331 | .Nm inet_ntop | ||
332 | functions appeared in BIND 4.9.4. | ||
333 | .Sh BUGS | ||
334 | The value | ||
335 | .Dv INADDR_NONE | ||
336 | (0xffffffff) is a valid broadcast address, but | ||
337 | .Fn inet_addr | ||
338 | cannot return that value without indicating failure. | ||
339 | Also, | ||
340 | .Fn inet_addr | ||
341 | should have been designed to return a | ||
342 | .Li struct in_addr . | ||
343 | The newer | ||
344 | .Fn inet_aton | ||
345 | function does not share these problems, and almost all existing code | ||
346 | should be modified to use | ||
347 | .Fn inet_aton | ||
348 | instead. | ||
349 | .Pp | ||
350 | The problem of host byte ordering versus network byte ordering is | ||
351 | confusing. | ||
352 | .Pp | ||
353 | The string returned by | ||
354 | .Fn inet_ntoa | ||
355 | resides in a static memory area. | ||
diff --git a/src/lib/libc/net/inet6_opt_init.3 b/src/lib/libc/net/inet6_opt_init.3 new file mode 100644 index 0000000000..dd7ab53f6f --- /dev/null +++ b/src/lib/libc/net/inet6_opt_init.3 | |||
@@ -0,0 +1,328 @@ | |||
1 | .\" $OpenBSD: inet6_opt_init.3,v 1.6 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" $KAME: inet6_opt_init.3,v 1.7 2004/12/27 05:08:23 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (C) 2004 WIDE Project. | ||
5 | .\" All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. Neither the name of the project nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd $Mdocdate: January 21 2014 $ | ||
32 | .Dt INET6_OPT_INIT 3 | ||
33 | .Os | ||
34 | .\" | ||
35 | .Sh NAME | ||
36 | .Nm inet6_opt_init , | ||
37 | .Nm inet6_opt_append , | ||
38 | .Nm inet6_opt_finish , | ||
39 | .Nm inet6_opt_set_val , | ||
40 | .Nm inet6_opt_next , | ||
41 | .Nm inet6_opt_find , | ||
42 | .Nm inet6_opt_get_val | ||
43 | .Nd IPv6 Hop-by-Hop and Destination Options manipulation | ||
44 | .\" | ||
45 | .Sh SYNOPSIS | ||
46 | .In netinet/in.h | ||
47 | .Ft "int" | ||
48 | .Fn inet6_opt_init "void *extbuf" "socklen_t extlen" | ||
49 | .Ft "int" | ||
50 | .Fn inet6_opt_append "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t len" "u_int8_t align" "void **databufp" | ||
51 | .Ft "int" | ||
52 | .Fn inet6_opt_finish "void *extbuf" "socklen_t extlen" "int offset" | ||
53 | .Ft "int" | ||
54 | .Fn inet6_opt_set_val "void *databuf" "int offset" "void *val" "socklen_t vallen" | ||
55 | .Ft "int" | ||
56 | .Fn inet6_opt_next "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t *typep" "socklen_t *lenp" "void **databufp" | ||
57 | .Ft "int" | ||
58 | .Fn inet6_opt_find "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t *lenp" "void **databufp" | ||
59 | .Ft "int" | ||
60 | .Fn inet6_opt_get_val "void *databuf" "socklen_t offset" "void *val" "socklen_t vallen" | ||
61 | .\" | ||
62 | .Sh DESCRIPTION | ||
63 | Building and parsing the Hop-by-Hop and Destination options is | ||
64 | complicated. | ||
65 | The advanced sockets API defines a set of functions to | ||
66 | help applications create and manipulate Hop-by-Hop and Destination | ||
67 | options. | ||
68 | These functions use the | ||
69 | formatting rules specified in Appendix B in RFC 2460, i.e. that the | ||
70 | largest field is placed last in the option. | ||
71 | The function prototypes | ||
72 | for these functions are all contained in the header file | ||
73 | .In netinet/in.h . | ||
74 | .\" | ||
75 | .Ss inet6_opt_init | ||
76 | The | ||
77 | .Fn inet6_opt_init | ||
78 | function | ||
79 | returns the number of bytes needed for an empty | ||
80 | extension header, one without any options. | ||
81 | If the | ||
82 | .Va extbuf | ||
83 | argument points to a valid section of memory | ||
84 | then the | ||
85 | .Fn inet6_opt_init | ||
86 | function also initializes the extension header's length field. | ||
87 | When attempting to initialize an extension buffer passed in the | ||
88 | .Va extbuf | ||
89 | argument, | ||
90 | .Fa extlen | ||
91 | must be a positive multiple of 8 or else the function fails and | ||
92 | returns \-1 to the caller. | ||
93 | .\" | ||
94 | .Ss inet6_opt_append | ||
95 | The | ||
96 | .Fn inet6_opt_append | ||
97 | function can perform different jobs. | ||
98 | When a valid | ||
99 | .Fa extbuf | ||
100 | argument is supplied it appends an option to the extension buffer and | ||
101 | returns the updated total length as well as a pointer to the newly | ||
102 | created option in | ||
103 | .Fa databufp . | ||
104 | If the value | ||
105 | of | ||
106 | .Fa extbuf | ||
107 | is | ||
108 | .Dv NULL | ||
109 | then the | ||
110 | .Fn inet6_opt_append | ||
111 | function only reports what the total length would | ||
112 | be if the option were actually appended. | ||
113 | The | ||
114 | .Fa len | ||
115 | and | ||
116 | .Fa align | ||
117 | arguments specify the length of the option and the required data | ||
118 | alignment which must be used when appending the option. | ||
119 | The | ||
120 | .Fa offset | ||
121 | argument should be the length returned by the | ||
122 | .Fn inet6_opt_init | ||
123 | function or a previous call to | ||
124 | .Fn inet6_opt_append . | ||
125 | .Pp | ||
126 | The | ||
127 | .Fa type | ||
128 | argument is the 8-bit option type. | ||
129 | .Pp | ||
130 | After | ||
131 | .Fn inet6_opt_append | ||
132 | has been called, the application can use the buffer pointed to by | ||
133 | .Fa databufp | ||
134 | directly, or use | ||
135 | .Fn inet6_opt_set_val | ||
136 | to specify the data to be contained in the option. | ||
137 | .Pp | ||
138 | Option types of | ||
139 | .Li 0 | ||
140 | and | ||
141 | .Li 1 | ||
142 | are reserved for the | ||
143 | .Li Pad1 | ||
144 | and | ||
145 | .Li PadN | ||
146 | options. | ||
147 | All other values from 2 through 255 may be used by applications. | ||
148 | .Pp | ||
149 | The length of the option data is contained in an 8-bit value and so | ||
150 | may contain any value from 0 through 255. | ||
151 | .Pp | ||
152 | The | ||
153 | .Fa align | ||
154 | parameter must have a value of 1, 2, 4, or 8 and cannot exceed the | ||
155 | value of | ||
156 | .Fa len . | ||
157 | The alignment values represent no alignment, 16-bit, 32-bit and 64-bit | ||
158 | alignments respectively. | ||
159 | .\" | ||
160 | .Ss inet6_opt_finish | ||
161 | The | ||
162 | .Fn inet6_opt_finish | ||
163 | calculates the final padding necessary to make the extension header a | ||
164 | multiple of 8 bytes, as required by the IPv6 extension header | ||
165 | specification, and returns the extension header's updated total | ||
166 | length. | ||
167 | The | ||
168 | .Fa offset | ||
169 | argument should be the length returned by | ||
170 | .Fn inet6_opt_init | ||
171 | or | ||
172 | .Fn inet6_opt_append . | ||
173 | When | ||
174 | .Fa extbuf | ||
175 | is not | ||
176 | .Dv NULL | ||
177 | the function also sets up the appropriate padding bytes by inserting a | ||
178 | Pad1 or PadN option of the proper length. | ||
179 | .Pp | ||
180 | If the extension header is too small to contain the proper padding | ||
181 | then an error of \-1 is returned to the caller. | ||
182 | .\" | ||
183 | .Ss inet6_opt_set_val | ||
184 | The | ||
185 | .Fn inet6_opt_set_val | ||
186 | function inserts data items of various sizes into the data portion of | ||
187 | the option. | ||
188 | The | ||
189 | .Fa databuf | ||
190 | argument is a pointer to memory that was returned by the | ||
191 | .Fn inet6_opt_append | ||
192 | call and the | ||
193 | .Fa offset | ||
194 | argument specifies where the option should be placed in the | ||
195 | data buffer. | ||
196 | The | ||
197 | .Fa val | ||
198 | argument points to an area of memory containing the data to be | ||
199 | inserted into the extension header, and the | ||
200 | .Fa vallen | ||
201 | argument indicates how much data to copy. | ||
202 | .Pp | ||
203 | The caller should ensure that each field is aligned on its natural | ||
204 | boundaries as described in Appendix B of RFC 2460. | ||
205 | .Pp | ||
206 | The function returns the offset for the next field which is calculated as | ||
207 | .Fa offset | ||
208 | + | ||
209 | .Fa vallen | ||
210 | and is used when composing options with multiple fields. | ||
211 | .\" | ||
212 | .Ss inet6_opt_next | ||
213 | The | ||
214 | .Fn inet6_opt_next | ||
215 | function parses received extension headers. | ||
216 | The | ||
217 | .Fa extbuf | ||
218 | and | ||
219 | .Fa extlen | ||
220 | arguments specify the location and length of the extension header | ||
221 | being parsed. | ||
222 | The | ||
223 | .Fa offset | ||
224 | argument should either be zero, for the first option, or the length value | ||
225 | returned by a previous call to | ||
226 | .Fn inet6_opt_next | ||
227 | or | ||
228 | .Fn inet6_opt_find . | ||
229 | The return value specifies the position where to continue scanning the | ||
230 | extension buffer. | ||
231 | The option is returned in the arguments | ||
232 | .Fa typep , lenp , | ||
233 | and | ||
234 | .Fa databufp . | ||
235 | .Fa typep , lenp , | ||
236 | and | ||
237 | .Fa databufp | ||
238 | point to the 8-bit option type, the 8-bit option length and the option | ||
239 | data respectively. | ||
240 | This function does not return any PAD1 or PADN options. | ||
241 | When an error occurs or there are no more options the return | ||
242 | value is \-1. | ||
243 | .\" | ||
244 | .Ss inet6_opt_find | ||
245 | The | ||
246 | .Fn inet6_opt_find | ||
247 | function searches the extension buffer for a particular option type, | ||
248 | passed in through the | ||
249 | .Fa type | ||
250 | argument. | ||
251 | If the option is found then the | ||
252 | .Fa lenp | ||
253 | and | ||
254 | .Fa databufp | ||
255 | arguments are updated to point to the option's length and data | ||
256 | respectively. | ||
257 | .Fa extbuf | ||
258 | and | ||
259 | .Fa extlen | ||
260 | must point to a valid extension buffer and give its length. | ||
261 | The | ||
262 | .Fa offset | ||
263 | argument can be used to search from a location anywhere in the | ||
264 | extension header. | ||
265 | .Ss inet6_opt_get_val | ||
266 | The | ||
267 | .Fn inet6_opt_get_val | ||
268 | function extracts data items of various sizes in the data portion of | ||
269 | the option. | ||
270 | The | ||
271 | .Fa databuf | ||
272 | is a pointer returned by the | ||
273 | .Fn inet6_opt_next | ||
274 | or | ||
275 | .Fn inet6_opt_find | ||
276 | functions. | ||
277 | The | ||
278 | .Fa val | ||
279 | argument points to where the data will be extracted. | ||
280 | The | ||
281 | .Fa offset | ||
282 | argument specifies from where in the data portion of the option the | ||
283 | value should be extracted; the first byte of option data is specified | ||
284 | by an offset of zero. | ||
285 | .Pp | ||
286 | It is expected that each field is aligned on its natural boundaries as | ||
287 | described in Appendix B of RFC 2460. | ||
288 | .Pp | ||
289 | The function returns the offset for the next field | ||
290 | by calculating | ||
291 | .Fa offset | ||
292 | + | ||
293 | .Fa vallen | ||
294 | which can be used when extracting option content with multiple fields. | ||
295 | Robust receivers must verify alignment before calling this function. | ||
296 | .\" | ||
297 | .Sh EXAMPLES | ||
298 | RFC 3542 gives comprehensive examples in Section 23. | ||
299 | KAME also provides examples in the | ||
300 | .Pa advapitest | ||
301 | directory of its kit. | ||
302 | .\" | ||
303 | .Sh DIAGNOSTICS | ||
304 | All the functions return | ||
305 | \-1 | ||
306 | on an error. | ||
307 | .\" | ||
308 | .Sh STANDARDS | ||
309 | .Rs | ||
310 | .%A S. Deering | ||
311 | .%A R. Hinden | ||
312 | .%D December 1998 | ||
313 | .%R RFC 2460 | ||
314 | .%T Internet Protocol, Version 6 (IPv6) Specification | ||
315 | .Re | ||
316 | .Pp | ||
317 | .Rs | ||
318 | .%A W. Stevens | ||
319 | .%A M. Thomas | ||
320 | .%A E. Nordmark | ||
321 | .%A T. Jinmei | ||
322 | .%D May 2003 | ||
323 | .%R RFC 3542 | ||
324 | .%T Advanced Sockets Application Program Interface (API) for IPv6 | ||
325 | .Re | ||
326 | .Sh HISTORY | ||
327 | The implementation first appeared in KAME advanced networking kit. | ||
328 | .\" | ||
diff --git a/src/lib/libc/net/inet6_option_space.3 b/src/lib/libc/net/inet6_option_space.3 new file mode 100644 index 0000000000..4b156636f7 --- /dev/null +++ b/src/lib/libc/net/inet6_option_space.3 | |||
@@ -0,0 +1,442 @@ | |||
1 | .\" $OpenBSD: inet6_option_space.3,v 1.24 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" $KAME: inet6_option_space.3,v 1.11 2005/01/05 03:00:44 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (C) 2004 WIDE Project. | ||
5 | .\" All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. Neither the name of the project nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd $Mdocdate: January 21 2014 $ | ||
32 | .Dt INET6_OPTION_SPACE 3 | ||
33 | .Os | ||
34 | .\" | ||
35 | .Sh NAME | ||
36 | .Nm inet6_option_space , | ||
37 | .Nm inet6_option_init , | ||
38 | .Nm inet6_option_append , | ||
39 | .Nm inet6_option_alloc , | ||
40 | .Nm inet6_option_next , | ||
41 | .Nm inet6_option_find | ||
42 | .Nd IPv6 Hop-by-Hop and Destination Option Manipulation | ||
43 | .\" | ||
44 | .Sh SYNOPSIS | ||
45 | .In sys/types.h | ||
46 | .In netinet/in.h | ||
47 | .Ft "int" | ||
48 | .Fn inet6_option_space "int nbytes" | ||
49 | .Ft "int" | ||
50 | .Fn inet6_option_init "void *bp" "struct cmsghdr **cmsgp" "int type" | ||
51 | .Ft "int" | ||
52 | .Fn inet6_option_append "struct cmsghdr *cmsg" "const u_int8_t *typep" "int multx" "int plusy" | ||
53 | .Ft "u_int8_t *" | ||
54 | .Fn inet6_option_alloc "struct cmsghdr *cmsg" "int datalen" "int multx" "int plusy" | ||
55 | .Ft "int" | ||
56 | .Fn inet6_option_next "const struct cmsghdr *cmsg" "u_int8_t **tptrp" | ||
57 | .Ft "int" | ||
58 | .Fn inet6_option_find "const struct cmsghdr *cmsg" "u_int8_t **tptrp" "int type" | ||
59 | .\" | ||
60 | .Sh DESCRIPTION | ||
61 | .\" | ||
62 | Note: | ||
63 | RFC 2292 has been superseded by RFC 3542. | ||
64 | The use of functions described in this page is deprecated. | ||
65 | See | ||
66 | .Xr inet6_opt_init 3 . | ||
67 | .Pp | ||
68 | Manipulating and parsing IPv6's Hop-by-Hop and Destination options is | ||
69 | complicated by the need to properly align and pad data as well as the | ||
70 | need to manipulate ancillary information that is not part of the data | ||
71 | stream. | ||
72 | RFC 2292 defines a set of functions, which are implemented as | ||
73 | part of the Kame libraries, to help developers create, change, | ||
74 | and parse Hop-by-Hop and Destination options. | ||
75 | All of the prototypes | ||
76 | for the option functions are defined in the | ||
77 | .In netinet/in.h | ||
78 | header file. | ||
79 | .\" | ||
80 | .Ss inet6_option_space | ||
81 | In order to determine the amount of space necessary to hold any option | ||
82 | the | ||
83 | .Fn inet6_option_space | ||
84 | function is called. | ||
85 | It returns the number of bytes required to hold | ||
86 | an option when it is stored as ancillary data, including the | ||
87 | .Li cmsghdr | ||
88 | structure at the beginning, and any necessary padding at the end. | ||
89 | The | ||
90 | .Fa nbytes | ||
91 | argument indicates the size of the structure defining the option, | ||
92 | and must include any pad bytes at the beginning (the value | ||
93 | .Li y | ||
94 | in the alignment term | ||
95 | .Dq Li "xn + y" ) , | ||
96 | the type byte, the length byte, and the option data. | ||
97 | .Pp | ||
98 | Note: If multiple options are stored in a single ancillary data | ||
99 | object, which is the recommended technique, the | ||
100 | .Fn inet6_option_space | ||
101 | function overestimates the amount of space required by the size of | ||
102 | .Li N-1 | ||
103 | .Li cmsghdr | ||
104 | structures, where | ||
105 | .Li N | ||
106 | is the number of options to be stored in the object. | ||
107 | Usually this has | ||
108 | no impact because it is assumed that most Hop-by-Hop and Destination | ||
109 | option headers carry only one option as indicated in appendix B of RFC 2460. | ||
110 | .\" | ||
111 | .Ss inet6_option_init | ||
112 | The | ||
113 | .Fn inet6_option_init | ||
114 | function is called to initialize any ancillary data object that will contain | ||
115 | a Hop-by-Hop or Destination option. | ||
116 | It returns | ||
117 | .Li 0 | ||
118 | on success and | ||
119 | .Li \-1 | ||
120 | when an error occurs. | ||
121 | .Pp | ||
122 | The | ||
123 | .Fa bp | ||
124 | argument points to a previously allocated area of memory which must be | ||
125 | large enough to contain all the arguments that the application intends | ||
126 | to add later via the | ||
127 | .Fn inet6_option_append | ||
128 | and | ||
129 | .Fn inet6_option_alloc | ||
130 | routines. | ||
131 | .Pp | ||
132 | The | ||
133 | .Fa cmsgp | ||
134 | argument is a pointer to a pointer to a | ||
135 | .Li cmsghdr | ||
136 | structure. | ||
137 | The | ||
138 | .Fa *cmsgp | ||
139 | argument | ||
140 | points to a | ||
141 | .Li cmsghdr | ||
142 | structure which is constructed by this function and stored in the | ||
143 | area of memory pointed to by | ||
144 | .Fa bp . | ||
145 | .Pp | ||
146 | The | ||
147 | .Fa type | ||
148 | is either | ||
149 | .Dv IPV6_HOPOPTS | ||
150 | or | ||
151 | .Dv IPV6_DSTOPTS | ||
152 | and is stored in the | ||
153 | .Li cmsg_type | ||
154 | member of the | ||
155 | .Li cmsghdr | ||
156 | structure mentioned above. | ||
157 | .\" | ||
158 | .Ss inet6_option_append | ||
159 | This function appends a Hop-by-Hop option or a Destination option into | ||
160 | an ancillary data object previously initialized by a call to | ||
161 | .Fn inet6_option_init . | ||
162 | The | ||
163 | .Fn inet6_option_append | ||
164 | function returns | ||
165 | .Li 0 | ||
166 | if it succeeds or | ||
167 | .Li \-1 | ||
168 | when an error occurs. | ||
169 | .Pp | ||
170 | The | ||
171 | .Fa cmsg | ||
172 | argument is a pointer to the | ||
173 | .Li cmsghdr | ||
174 | structure that was initialized by a call to | ||
175 | .Fn inet6_option_init . | ||
176 | .Pp | ||
177 | The | ||
178 | .Fa typep | ||
179 | argument is a pointer to the 8-bit option type. | ||
180 | All options are | ||
181 | encoded as type-length-value tuples and it is assumed that | ||
182 | the | ||
183 | .Fa typep | ||
184 | field is immediately followed by the 8-bit option data length field, | ||
185 | which is then followed by the option data. | ||
186 | .Pp | ||
187 | The option types of | ||
188 | .Li 0 | ||
189 | and | ||
190 | .Li 1 | ||
191 | are reserved for the | ||
192 | .Li Pad1 | ||
193 | and | ||
194 | .Li PadN | ||
195 | options respectively. | ||
196 | All other values from | ||
197 | .Li 2 | ||
198 | through | ||
199 | .Li 255 | ||
200 | are available for applications to use. | ||
201 | .Pp | ||
202 | The option data length, since it is stored in 8 bites, must have a | ||
203 | value between | ||
204 | .Li 0 | ||
205 | and | ||
206 | .Li 255 , | ||
207 | inclusive. | ||
208 | .Pp | ||
209 | The | ||
210 | .Fa multx | ||
211 | argument | ||
212 | is the value | ||
213 | .Li x | ||
214 | in the alignment term | ||
215 | .Dq Li xn + y | ||
216 | and indicates the byte alignment necessary for the data. | ||
217 | Alignments may be specified as | ||
218 | .Li 1 , | ||
219 | .Li 2 , | ||
220 | .Li 4 , | ||
221 | or | ||
222 | .Li 8 | ||
223 | bytes, which is no alignment, 16-bit, 32-bit and 64-bit alignments | ||
224 | respectively. | ||
225 | .Pp | ||
226 | The | ||
227 | .Fa plusy | ||
228 | argument | ||
229 | is the value | ||
230 | .Li y | ||
231 | in the alignment term | ||
232 | .Dq Li xn + y | ||
233 | and must have a value between | ||
234 | .Li 0 | ||
235 | and | ||
236 | .Li 7 , | ||
237 | inclusive, indicating the amount of padding that is necessary for an | ||
238 | option. | ||
239 | .\" | ||
240 | .Ss inet6_option_alloc | ||
241 | The | ||
242 | .Fn inet6_option_alloc | ||
243 | function appends a Hop-by-Hop option or a Destination option into an | ||
244 | ancillary data object that has previously been initialized by a call to | ||
245 | .Fn inet6_option_init . | ||
246 | A successful call to the | ||
247 | .Fn inet6_option_alloc | ||
248 | function returns a pointer to the 8-bit option type field, | ||
249 | which is at the beginning of the allocated region. | ||
250 | .Fn inet6_option_alloc | ||
251 | returns | ||
252 | .Dv NULL | ||
253 | when an error has occurred. | ||
254 | .Pp | ||
255 | The difference between the | ||
256 | .Fn inet6_option_alloc | ||
257 | and | ||
258 | .Fn inet6_option_append | ||
259 | functions is that the latter copies the contents of a previously built | ||
260 | option into the ancillary data object while the former returns a | ||
261 | pointer to the place in the data object where the option's TLV must | ||
262 | then be built by the application. | ||
263 | .Pp | ||
264 | The | ||
265 | .Fa cmsg | ||
266 | argument is a pointer to a | ||
267 | .Li cmsghdr | ||
268 | structure that was initialized by | ||
269 | .Fn inet6_option_init . | ||
270 | .Pp | ||
271 | The | ||
272 | .Fa datalen | ||
273 | argument is the value of the option data length byte for this option. | ||
274 | This value is required as an argument to allow the function to | ||
275 | determine if padding must be appended at the end of the option. | ||
276 | (The | ||
277 | .Fn inet6_option_append | ||
278 | function does not need a data length argument | ||
279 | since the option data length must already be stored by the caller.) | ||
280 | .Pp | ||
281 | The | ||
282 | .Fa multx | ||
283 | and | ||
284 | .Fa plusy | ||
285 | arguments | ||
286 | are identical to the arguments of the same name described in the | ||
287 | .Fn inet6_option_init | ||
288 | function above. | ||
289 | .\" | ||
290 | .Ss inet6_option_next | ||
291 | The | ||
292 | .Fn inet6_option_next | ||
293 | function is used to process Hop-by-Hop and Destination options that | ||
294 | are present in an ancillary data object. | ||
295 | When an option remains to | ||
296 | be processed, the return value of the | ||
297 | .Fn inet6_option_next | ||
298 | function is | ||
299 | .Li 0 | ||
300 | and the | ||
301 | .Fa *tptrp | ||
302 | argument points to the 8-bit option type field, which is followed by | ||
303 | the 8-bit option data length, and then the option data. | ||
304 | When no more | ||
305 | options remain to be processed, the return value is | ||
306 | .Li \-1 | ||
307 | and | ||
308 | .Fa *tptrp | ||
309 | is | ||
310 | .Dv NULL . | ||
311 | When an error occurs, the return value is | ||
312 | .Li \-1 , | ||
313 | but the | ||
314 | .Fa *tptrp | ||
315 | argument is not | ||
316 | .Dv NULL . | ||
317 | This set of return values allows a program to easily loop through all | ||
318 | the options in an ancillary data object, checking for the error and | ||
319 | end of stream conditions along the way. | ||
320 | .Pp | ||
321 | When a valid option is returned, the | ||
322 | .Fa cmsg | ||
323 | argument points to a | ||
324 | .Li cmsghdr | ||
325 | where the | ||
326 | .Li cmsg_level | ||
327 | element equals | ||
328 | .Dv IPPROTO_IPV6 | ||
329 | and the | ||
330 | .Li cmsg_type | ||
331 | element is either | ||
332 | .Dv IPV6_HOPOPTS | ||
333 | or | ||
334 | .Dv IPV6_DSTOPTS . | ||
335 | .Pp | ||
336 | The | ||
337 | .Fa tptrp | ||
338 | argument is a pointer to a pointer to an 8-bit byte and | ||
339 | .Fa *tptrp | ||
340 | is used by the function to remember its place in the ancillary data | ||
341 | object each time the function is called. | ||
342 | When the | ||
343 | .Fn inet6_option_next | ||
344 | function is called for the first time on a given ancillary data object, | ||
345 | .Fa *tptrp | ||
346 | must be set to | ||
347 | .Dv NULL . | ||
348 | .Pp | ||
349 | Each time the function returns success, | ||
350 | the | ||
351 | .Fa *tptrp | ||
352 | argument points to the 8-bit option type field for the next option to | ||
353 | be processed. | ||
354 | .\" | ||
355 | .Ss inet6_option_find | ||
356 | The | ||
357 | .Fn inet6_option_find | ||
358 | function allows an application to search for a particular option type | ||
359 | in an ancillary data object. | ||
360 | The | ||
361 | .Fa cmsg | ||
362 | argument is a pointer to a | ||
363 | .Li cmsghdr | ||
364 | structure in which the | ||
365 | .Li cmsg_level | ||
366 | element equals | ||
367 | .Dv IPPROTO_IPV6 | ||
368 | and the | ||
369 | .Li cmsg_type | ||
370 | element is either | ||
371 | .Dv IPV6_HOPOPTS | ||
372 | or | ||
373 | .Dv IPV6_DSTOPTS . | ||
374 | .Pp | ||
375 | The | ||
376 | .Fa tptrp | ||
377 | argument is handled exactly as in the | ||
378 | .Fn inet6_option_next | ||
379 | function described above. | ||
380 | .Pp | ||
381 | The | ||
382 | .Fn inet6_option_find | ||
383 | function starts searching for an option of the specified type | ||
384 | beginning after the value of | ||
385 | .Fa *tptrp . | ||
386 | .\" | ||
387 | .Sh EXAMPLES | ||
388 | RFC 2292 gives comprehensive examples in chapter 6. | ||
389 | .\" | ||
390 | .Sh DIAGNOSTICS | ||
391 | The | ||
392 | .Fn inet6_option_init | ||
393 | and | ||
394 | .Fn inet6_option_append | ||
395 | functions return | ||
396 | .Li 0 | ||
397 | on success or | ||
398 | .Li \-1 | ||
399 | on an error. | ||
400 | .Pp | ||
401 | The | ||
402 | .Fn inet6_option_alloc | ||
403 | function returns | ||
404 | .Dv NULL | ||
405 | on an error. | ||
406 | .Pp | ||
407 | When | ||
408 | .Fn inet6_option_next | ||
409 | or | ||
410 | .Fn inet6_option_find | ||
411 | detect an error they return | ||
412 | .Li \-1 , | ||
413 | setting | ||
414 | .Fa *tptrp | ||
415 | to a non | ||
416 | .Dv NULL | ||
417 | value. | ||
418 | .\" | ||
419 | .Sh SEE ALSO | ||
420 | .Xr inet6 4 , | ||
421 | .Xr ip6 4 | ||
422 | .\" | ||
423 | .Sh STANDARDS | ||
424 | .Rs | ||
425 | .%A W. Stevens | ||
426 | .%A M. Thomas | ||
427 | .%D February 1998 | ||
428 | .%R RFC 2292 | ||
429 | .%T Advanced Sockets API for IPv6 | ||
430 | .Re | ||
431 | .Pp | ||
432 | .Rs | ||
433 | .%A S. Deering | ||
434 | .%A R. Hinden | ||
435 | .%D December 1998 | ||
436 | .%R RFC 2460 | ||
437 | .%T Internet Protocol, Version 6 (IPv6) Specification | ||
438 | .Re | ||
439 | .\" | ||
440 | .Sh HISTORY | ||
441 | This implementation first appeared in the KAME advanced networking kit. | ||
442 | .\" | ||
diff --git a/src/lib/libc/net/inet6_rth_space.3 b/src/lib/libc/net/inet6_rth_space.3 new file mode 100644 index 0000000000..fcd023481f --- /dev/null +++ b/src/lib/libc/net/inet6_rth_space.3 | |||
@@ -0,0 +1,220 @@ | |||
1 | .\" $OpenBSD: inet6_rth_space.3,v 1.6 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" $KAME: inet6_rth_space.3,v 1.7 2005/01/05 03:00:44 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (C) 2004 WIDE Project. | ||
5 | .\" All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. Neither the name of the project nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd $Mdocdate: January 21 2014 $ | ||
32 | .Dt INET6_RTH_SPACE 3 | ||
33 | .Os | ||
34 | .\" | ||
35 | .Sh NAME | ||
36 | .Nm inet6_rth_space , | ||
37 | .Nm inet6_rth_init , | ||
38 | .Nm inet6_rth_add , | ||
39 | .Nm inet6_rth_reverse , | ||
40 | .Nm inet6_rth_segments , | ||
41 | .Nm inet6_rth_getaddr | ||
42 | .Nd IPv6 Routing Header Options manipulation | ||
43 | .\" | ||
44 | .Sh SYNOPSIS | ||
45 | .In netinet/in.h | ||
46 | .Ft socklen_t | ||
47 | .Fn inet6_rth_space "int" "int" | ||
48 | .Ft "void *" | ||
49 | .Fn inet6_rth_init "void *" "socklen_t" "int" "int" | ||
50 | .Ft int | ||
51 | .Fn inet6_rth_add "void *" "const struct in6_addr *" | ||
52 | .Ft int | ||
53 | .Fn inet6_rth_reverse "const void *" "void *" | ||
54 | .Ft int | ||
55 | .Fn inet6_rth_segments "const void *" | ||
56 | .Ft "struct in6_addr *" | ||
57 | .Fn inet6_rth_getaddr "const void *" "int" | ||
58 | .\" | ||
59 | .Sh DESCRIPTION | ||
60 | The IPv6 Advanced API, RFC 3542, defines the functions that an | ||
61 | application calls to build and examine IPv6 Routing headers. | ||
62 | Routing headers are used to perform source routing in IPv6 networks. | ||
63 | The RFC uses the word | ||
64 | .Dq segments | ||
65 | to describe addresses and that is the term used here as well. | ||
66 | All of the functions are defined in the header file | ||
67 | .In netinet/in.h . | ||
68 | The functions described in this manual page all operate | ||
69 | on routing header structures which are defined in | ||
70 | .In netinet/ip6.h | ||
71 | but which should not need to be modified outside the use of this API. | ||
72 | The size and shape of the route header structures may change, so using | ||
73 | the APIs is a more portable, long term, solution. | ||
74 | .Pp | ||
75 | The functions in the API are split into two groups, those that build a | ||
76 | routing header and those that parse a received routing header. | ||
77 | The builder functions are described first, followed by the parser functions. | ||
78 | .Ss inet6_rth_space | ||
79 | The | ||
80 | .Fn inet6_rth_space | ||
81 | function returns the number of bytes required to hold a Routing Header | ||
82 | of the type, specified in the | ||
83 | .Fa type | ||
84 | argument and containing the number of addresses specified in the | ||
85 | .Fa segments | ||
86 | argument. | ||
87 | When the type is | ||
88 | .Dv IPV6_RTHDR_TYPE_0 | ||
89 | the number of segments must be from 0 through 127. | ||
90 | The return value from this function is the number of bytes required to | ||
91 | store the routing header. | ||
92 | If the value 0 is returned then either the | ||
93 | route header type was not recognized or another error occurred. | ||
94 | .Ss inet6_rth_init | ||
95 | The | ||
96 | .Fn inet6_rth_init | ||
97 | function initializes the pre-allocated buffer pointed to by | ||
98 | .Fa bp | ||
99 | to contain a routing header of the specified type. | ||
100 | The | ||
101 | .Fa bp_len | ||
102 | argument is used to verify that the buffer is large enough. | ||
103 | The caller must allocate the buffer pointed to by bp. | ||
104 | The necessary buffer size should be determined by calling | ||
105 | .Fn inet6_rth_space | ||
106 | described in the previous sections. | ||
107 | .Pp | ||
108 | The | ||
109 | .Fn inet6_rth_init | ||
110 | function returns a pointer to | ||
111 | .Fa bp | ||
112 | on success and | ||
113 | .Dv NULL | ||
114 | when there is an error. | ||
115 | .Ss inet6_rth_add | ||
116 | The | ||
117 | .Fn inet6_rth_add | ||
118 | function adds the IPv6 address pointed to by | ||
119 | .Fa addr | ||
120 | to the end of the routing header being constructed. | ||
121 | .Pp | ||
122 | A successful addition results in the function returning 0, otherwise | ||
123 | \-1 is returned. | ||
124 | .Ss inet6_rth_reverse | ||
125 | The | ||
126 | .Fn inet6_rth_reverse | ||
127 | function takes a routing header, pointed to by the | ||
128 | argument | ||
129 | .Fa in , | ||
130 | and writes a new routing header into the argument pointed to by | ||
131 | .Fa out . | ||
132 | The routing header at that sends datagrams along the reverse of that | ||
133 | route. | ||
134 | Both arguments are allowed to point to the same buffer meaning | ||
135 | that the reversal can occur in place. | ||
136 | .Pp | ||
137 | The return value of the function is 0 on success, or \-1 when | ||
138 | there is an error. | ||
139 | .\" | ||
140 | .Pp | ||
141 | The next set of functions operate on a routing header that the | ||
142 | application wants to parse. | ||
143 | In the usual case such a routing header | ||
144 | is received from the network, although these functions can also be | ||
145 | used with routing headers that the application itself created. | ||
146 | .Ss inet6_rth_segments | ||
147 | The | ||
148 | .Fn inet6_rth_segments | ||
149 | function returns the number of segments contained in the | ||
150 | routing header pointed to by | ||
151 | .Fa bp . | ||
152 | The return value is the number of segments contained in the routing | ||
153 | header, or \-1 if an error occurred. | ||
154 | It is not an error for 0 to be | ||
155 | returned as a routing header may contain 0 segments. | ||
156 | .\" | ||
157 | .Ss inet6_rth_getaddr | ||
158 | The | ||
159 | .Fn inet6_rth_getaddr | ||
160 | function is used to retrieve a single address from a routing header. | ||
161 | The | ||
162 | .Fa index | ||
163 | is the location in the routing header from which the application wants | ||
164 | to retrieve an address. | ||
165 | The | ||
166 | .Fa index | ||
167 | parameter must have a value between 0 and one less than the number of | ||
168 | segments present in the routing header. | ||
169 | The | ||
170 | .Fn inet6_rth_segments | ||
171 | function, described in the last section, should be used to determine | ||
172 | the total number of segments in the routing header. | ||
173 | The | ||
174 | .Fn inet6_rth_getaddr | ||
175 | function returns a pointer to an IPv6 address on success or | ||
176 | .Dv NULL | ||
177 | when an error has occurred. | ||
178 | .\" | ||
179 | .Sh EXAMPLES | ||
180 | RFC 3542 gives extensive examples in Section 21, Appendix B. | ||
181 | KAME also provides examples in the advapitest directory of its kit. | ||
182 | .\" | ||
183 | .Sh DIAGNOSTICS | ||
184 | The | ||
185 | .Fn inet6_rth_space | ||
186 | and | ||
187 | .Fn inet6_rth_getaddr | ||
188 | functions return 0 on errors. | ||
189 | .Pp | ||
190 | The | ||
191 | .Fn inet6_rthdr_init | ||
192 | function returns | ||
193 | .Dv NULL | ||
194 | on error. | ||
195 | The | ||
196 | .Fn inet6_rth_add | ||
197 | and | ||
198 | .Fn inet6_rth_reverse | ||
199 | functions return 0 on success, or \-1 upon an error. | ||
200 | .\" | ||
201 | .Sh STANDARDS | ||
202 | .Rs | ||
203 | .%A S. Deering | ||
204 | .%A R. Hinden | ||
205 | .%D December 1998 | ||
206 | .%R RFC 2460 | ||
207 | .%T Internet Protocol, Version 6 (IPv6) Specification | ||
208 | .Re | ||
209 | .Pp | ||
210 | .Rs | ||
211 | .%A W. Stevens | ||
212 | .%A M. Thomas | ||
213 | .%A E. Nordmark | ||
214 | .%A T. Jinmei | ||
215 | .%D May 2003 | ||
216 | .%R RFC 3542 | ||
217 | .%T Advanced Sockets Application Programming Interface (API) for IPv6 | ||
218 | .Re | ||
219 | .Sh HISTORY | ||
220 | The implementation first appeared in KAME advanced networking kit. | ||
diff --git a/src/lib/libc/net/inet6_rthdr_space.3 b/src/lib/libc/net/inet6_rthdr_space.3 new file mode 100644 index 0000000000..c72e84ff1a --- /dev/null +++ b/src/lib/libc/net/inet6_rthdr_space.3 | |||
@@ -0,0 +1,310 @@ | |||
1 | .\" $OpenBSD: inet6_rthdr_space.3,v 1.23 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" $KAME: inet6_rthdr_space.3,v 1.11 2005/01/05 03:00:44 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (C) 2004 WIDE Project. | ||
5 | .\" All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. Neither the name of the project nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd $Mdocdate: January 21 2014 $ | ||
32 | .Dt INET6_RTHDR_SPACE 3 | ||
33 | .Os | ||
34 | .\" | ||
35 | .Sh NAME | ||
36 | .Nm inet6_rthdr_space , | ||
37 | .Nm inet6_rthdr_init , | ||
38 | .Nm inet6_rthdr_add , | ||
39 | .Nm inet6_rthdr_lasthop , | ||
40 | .Nm inet6_rthdr_reverse , | ||
41 | .Nm inet6_rthdr_segments , | ||
42 | .Nm inet6_rthdr_getaddr , | ||
43 | .Nm inet6_rthdr_getflags | ||
44 | .Nd IPv6 Routing Header Options Manipulation | ||
45 | .\" | ||
46 | .Sh SYNOPSIS | ||
47 | .In sys/types.h | ||
48 | .In netinet/in.h | ||
49 | .Ft size_t | ||
50 | .Fn inet6_rthdr_space "int type" "int segments" | ||
51 | .Ft "struct cmsghdr *" | ||
52 | .Fn inet6_rthdr_init "void *bp" "int type" | ||
53 | .Ft int | ||
54 | .Fn inet6_rthdr_add "struct cmsghdr *cmsg" "const struct in6_addr *addr" "unsigned int flags" | ||
55 | .Ft int | ||
56 | .Fn inet6_rthdr_lasthop "struct cmsghdr *cmsg" "unsigned int flags" | ||
57 | .Ft int | ||
58 | .Fn inet6_rthdr_reverse "const struct cmsghdr *in" "struct cmsghdr *out" | ||
59 | .Ft int | ||
60 | .Fn inet6_rthdr_segments "const struct cmsghdr *cmsg" | ||
61 | .Ft "struct in6_addr *" | ||
62 | .Fn inet6_rthdr_getaddr "struct cmsghdr *cmsg" "int index" | ||
63 | .Ft int | ||
64 | .Fn inet6_rthdr_getflags "const struct cmsghdr *cmsg" "int index" | ||
65 | .\" | ||
66 | .Sh DESCRIPTION | ||
67 | Note: | ||
68 | RFC 2292 has been superseded by RFC 3542. | ||
69 | The use of functions described in this page is deprecated. | ||
70 | See | ||
71 | .Xr inet6_rth_space 3 . | ||
72 | .Pp | ||
73 | The RFC 2292 IPv6 Advanced API defined eight functions for | ||
74 | applications to use for building and parsing routing headers. | ||
75 | The | ||
76 | eight functions are split into two groups, the first of which builds | ||
77 | the header and the second of which can parse it. | ||
78 | The function prototypes for these functions are all in the | ||
79 | .In netinet/in.h | ||
80 | header. | ||
81 | Although direct manipulation of a routing header is possible, | ||
82 | this set of APIs make it unnecessary and such direct manipulation | ||
83 | should be avoided so that changes to the underlying structures do not | ||
84 | break applications. | ||
85 | .Pp | ||
86 | Please note that RFC 2292 uses the term | ||
87 | .Dq segments | ||
88 | instead of the term | ||
89 | .Dq addresses | ||
90 | but they are considered equivalent for this manual page. | ||
91 | .\" | ||
92 | .Ss inet6_rthdr_space | ||
93 | The | ||
94 | .Fn inet6_rthdr_space | ||
95 | function returns the number of bytes required to hold a routing header | ||
96 | of the specified | ||
97 | .Fa type | ||
98 | and containing the specified number of | ||
99 | .Fa segments . | ||
100 | Only one | ||
101 | .Fa type | ||
102 | is supported, | ||
103 | .Dv IPV6_RTHDR_TYPE_0 , | ||
104 | and it can hold from 1 to 23 segments. | ||
105 | The return value includes the | ||
106 | size of the | ||
107 | .Vt cmsghdr | ||
108 | structure that precedes the routing header and | ||
109 | any required padding. | ||
110 | .Pp | ||
111 | A return value of 0 indicates an error. | ||
112 | Either the type was specified | ||
113 | incorrectly, or the number of segments was less than one or greater | ||
114 | than 23. | ||
115 | .Pp | ||
116 | Note: The | ||
117 | .Fn inet6_rthdr_space | ||
118 | function only returns the size required by the routing header and does | ||
119 | not allocate memory for the caller. | ||
120 | .\" | ||
121 | .Ss inet6_rthdr_init | ||
122 | The | ||
123 | .Fn inet6_rthdr_init | ||
124 | function initializes a buffer, pointed to by | ||
125 | .Fa bp | ||
126 | with an appropriate | ||
127 | .Li cmsghdr | ||
128 | structure followed by a routing header of the specified | ||
129 | .Fa type . | ||
130 | .Pp | ||
131 | The caller must use the | ||
132 | .Fn inet6_rthdr_space | ||
133 | function to determine the size of the buffer, and then allocate that | ||
134 | buffer before calling | ||
135 | .Fn inet6_rthdr_init . | ||
136 | .Pp | ||
137 | The return value is a pointer to a | ||
138 | .Li cmsghdr | ||
139 | structure, which is used as the first argument to the | ||
140 | .Fn inet6_rthdr_add | ||
141 | and | ||
142 | .Fn inet6_rthdr_lasthop | ||
143 | functions in order to construct the routing header. | ||
144 | When an error occurs the return value is | ||
145 | .Dv NULL . | ||
146 | .\" | ||
147 | .Ss inet6_rthdr_add | ||
148 | The | ||
149 | .Fn inet6_rthdr_add | ||
150 | function adds the IPv6 address pointed to by | ||
151 | .Fa addr | ||
152 | to the end of the | ||
153 | routing header being constructed and sets the type of this address to the | ||
154 | value of | ||
155 | .Fa flags . | ||
156 | The | ||
157 | .Fa flags | ||
158 | must be either | ||
159 | .Dv IPV6_RTHDR_LOOSE | ||
160 | or | ||
161 | .Dv IPV6_RTHDR_STRICT | ||
162 | indicating whether loose or strict source routing is required. | ||
163 | .Pp | ||
164 | When the function succeeds it returns 0, otherwise \-1 is returned. | ||
165 | .\" | ||
166 | .Ss inet6_rthdr_lasthop | ||
167 | The | ||
168 | .Fn inet6_rthdr_lasthop | ||
169 | function specifies the strict or loose flag for the final hop of a | ||
170 | routing header. | ||
171 | The | ||
172 | .Fa flags | ||
173 | argument must be either | ||
174 | .Dv IPV6_RTHDR_LOOSE | ||
175 | or | ||
176 | .Dv IPV6_RTHDR_STRICT . | ||
177 | .Pp | ||
178 | The return value of the function is 0 upon success, and \-1 when an | ||
179 | error has occurred. | ||
180 | .Pp | ||
181 | Please note that a routing header specifying | ||
182 | .Li N | ||
183 | intermediate nodes requires | ||
184 | .Li N+1 | ||
185 | strict or loose flags meaning that | ||
186 | .Fn inet6_rthdr_add | ||
187 | must be called | ||
188 | .Li N | ||
189 | times and then | ||
190 | .Fn inet6_rthdr_lasthop | ||
191 | must be called once. | ||
192 | .\" | ||
193 | .Ss inet6_rthdr_reverse | ||
194 | This function was never implemented. | ||
195 | .Pp | ||
196 | The following three functions provide an API for parsing a received | ||
197 | routing header: | ||
198 | .\" | ||
199 | .Ss inet6_rthdr_segments | ||
200 | The | ||
201 | .Fn inet6_rthdr_segments | ||
202 | function returns the number of segments contained in the routing | ||
203 | header pointed to by the | ||
204 | .Fa cmsg | ||
205 | argument. | ||
206 | On success the return value is from 1 to 23. | ||
207 | When an error occurs, \-1 is returned. | ||
208 | .\" | ||
209 | .Ss inet6_rthdr_getaddr | ||
210 | The | ||
211 | .Fn inet6_rthdr_getaddr | ||
212 | function returns a pointer to the IPv6 address specified by the | ||
213 | .Fa index | ||
214 | argument from the routing header pointed to by | ||
215 | .Fa cmsg . | ||
216 | The index must be between 1 and the number returned by | ||
217 | .Fn inet6_rthdr_segments , | ||
218 | described above. | ||
219 | An application must call | ||
220 | .Fn inet6_rthdr_segments | ||
221 | to obtain the number of segments in the routing header. | ||
222 | .Pp | ||
223 | If an error occurs, | ||
224 | .Dv NULL | ||
225 | is returned. | ||
226 | .\" | ||
227 | .Ss inet6_rthdr_getflags | ||
228 | The | ||
229 | .Fn inet6_rthdr_getflags | ||
230 | function returns the flags value of the segment specified by | ||
231 | .Fa index | ||
232 | of the routing header pointed to by | ||
233 | .Fa cmsg . | ||
234 | The | ||
235 | .Fa index | ||
236 | argument must be between 0 and the value returned by | ||
237 | .Fn inet6_rthdr_segments . | ||
238 | The return value will be either | ||
239 | .Dv IPV6_RTHDR_LOOSE | ||
240 | or | ||
241 | .Dv IPV6_RTHDR_STRICT | ||
242 | indicating whether loose or strict source routing was requested for | ||
243 | that segment. | ||
244 | .Pp | ||
245 | When an error occurs, \-1 is returned. | ||
246 | .Pp | ||
247 | Note: Flags begin at index 0 while segments begin at index 1, to | ||
248 | maintain consistency with the terminology and figures in RFC 2460. | ||
249 | .\" | ||
250 | .Sh EXAMPLES | ||
251 | RFC 2292 gives comprehensive examples in chapter 8. | ||
252 | .\" | ||
253 | .Sh DIAGNOSTICS | ||
254 | The | ||
255 | .Fn inet6_rthdr_space | ||
256 | function returns 0 when an error occurs. | ||
257 | .Pp | ||
258 | The | ||
259 | .Fn inet6_rthdr_add | ||
260 | and | ||
261 | .Fn inet6_rthdr_lasthop | ||
262 | functions return 0 on success, and \-1 on error. | ||
263 | .Pp | ||
264 | The | ||
265 | .Fn inet6_rthdr_init | ||
266 | and | ||
267 | .Fn inet6_rthdr_getaddr | ||
268 | functions | ||
269 | return | ||
270 | .Dv NULL | ||
271 | on error. | ||
272 | .Pp | ||
273 | The | ||
274 | .Fn inet6_rthdr_segments | ||
275 | and | ||
276 | .Fn inet6_rthdr_getflags | ||
277 | functions return \-1 on error. | ||
278 | .\" | ||
279 | .Sh SEE ALSO | ||
280 | .Xr inet6 4 , | ||
281 | .Xr ip6 4 | ||
282 | .Sh STANDARDS | ||
283 | .Rs | ||
284 | .%A W. Stevens | ||
285 | .%A M. Thomas | ||
286 | .%D February 1998 | ||
287 | .%R RFC 2292 | ||
288 | .%T Advanced Sockets API for IPv6 | ||
289 | .Re | ||
290 | .Pp | ||
291 | .Rs | ||
292 | .%A S. Deering | ||
293 | .%A R. Hinden | ||
294 | .%D December 1998 | ||
295 | .%R RFC 2460 | ||
296 | .%T Internet Protocol, Version 6 (IPv6) Specification | ||
297 | .Re | ||
298 | .\" | ||
299 | .Sh HISTORY | ||
300 | This implementation first appeared in the KAME advanced networking kit. | ||
301 | .\" | ||
302 | .Sh BUGS | ||
303 | The | ||
304 | .Fn inet6_rthdr_reverse | ||
305 | function was never implemented. | ||
306 | .\".Pp | ||
307 | .\"This API is deprecated in favor of | ||
308 | .\".Xr inet6_rth_space 3 | ||
309 | .\".Sh SEE ALSO | ||
310 | .\".Xr inet6_rth_space 3 | ||
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c new file mode 100644 index 0000000000..18762ab522 --- /dev/null +++ b/src/lib/libc/net/inet_addr.c | |||
@@ -0,0 +1,175 @@ | |||
1 | /* $OpenBSD: inet_addr.c,v 1.10 2013/11/24 23:51:28 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1983, 1990, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1983, 1990, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. 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 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #include <sys/types.h> | ||
55 | #include <sys/param.h> | ||
56 | #include <netinet/in.h> | ||
57 | #include <arpa/inet.h> | ||
58 | #include <ctype.h> | ||
59 | |||
60 | /* | ||
61 | * Ascii internet address interpretation routine. | ||
62 | * The value returned is in network order. | ||
63 | */ | ||
64 | in_addr_t | ||
65 | inet_addr(const char *cp) | ||
66 | { | ||
67 | struct in_addr val; | ||
68 | |||
69 | if (inet_aton(cp, &val)) | ||
70 | return (val.s_addr); | ||
71 | return (INADDR_NONE); | ||
72 | } | ||
73 | |||
74 | /* | ||
75 | * Check whether "cp" is a valid ascii representation | ||
76 | * of an Internet address and convert to a binary address. | ||
77 | * Returns 1 if the address is valid, 0 if not. | ||
78 | * This replaces inet_addr, the return value from which | ||
79 | * cannot distinguish between failure and a local broadcast address. | ||
80 | */ | ||
81 | int | ||
82 | inet_aton(const char *cp, struct in_addr *addr) | ||
83 | { | ||
84 | in_addr_t val; | ||
85 | int base, n; | ||
86 | char c; | ||
87 | u_int parts[4]; | ||
88 | u_int *pp = parts; | ||
89 | |||
90 | c = *cp; | ||
91 | for (;;) { | ||
92 | /* | ||
93 | * Collect number up to ``.''. | ||
94 | * Values are specified as for C: | ||
95 | * 0x=hex, 0=octal, isdigit=decimal. | ||
96 | */ | ||
97 | if (!isdigit((unsigned char)c)) | ||
98 | return (0); | ||
99 | val = 0; base = 10; | ||
100 | if (c == '0') { | ||
101 | c = *++cp; | ||
102 | if (c == 'x' || c == 'X') | ||
103 | base = 16, c = *++cp; | ||
104 | else | ||
105 | base = 8; | ||
106 | } | ||
107 | for (;;) { | ||
108 | if (isascii((unsigned char)c) && | ||
109 | isdigit((unsigned char)c)) { | ||
110 | val = (val * base) + (c - '0'); | ||
111 | c = *++cp; | ||
112 | } else if (base == 16 && | ||
113 | isascii((unsigned char)c) && | ||
114 | isxdigit((unsigned char)c)) { | ||
115 | val = (val << 4) | | ||
116 | (c + 10 - (islower((unsigned char)c) ? 'a' : 'A')); | ||
117 | c = *++cp; | ||
118 | } else | ||
119 | break; | ||
120 | } | ||
121 | if (c == '.') { | ||
122 | /* | ||
123 | * Internet format: | ||
124 | * a.b.c.d | ||
125 | * a.b.c (with c treated as 16 bits) | ||
126 | * a.b (with b treated as 24 bits) | ||
127 | */ | ||
128 | if (pp >= parts + 3) | ||
129 | return (0); | ||
130 | *pp++ = val; | ||
131 | c = *++cp; | ||
132 | } else | ||
133 | break; | ||
134 | } | ||
135 | /* | ||
136 | * Check for trailing characters. | ||
137 | */ | ||
138 | if (c != '\0' && | ||
139 | (!isascii((unsigned char)c) || !isspace((unsigned char)c))) | ||
140 | return (0); | ||
141 | /* | ||
142 | * Concoct the address according to | ||
143 | * the number of parts specified. | ||
144 | */ | ||
145 | n = pp - parts + 1; | ||
146 | switch (n) { | ||
147 | |||
148 | case 0: | ||
149 | return (0); /* initial nondigit */ | ||
150 | |||
151 | case 1: /* a -- 32 bits */ | ||
152 | break; | ||
153 | |||
154 | case 2: /* a.b -- 8.24 bits */ | ||
155 | if ((val > 0xffffff) || (parts[0] > 0xff)) | ||
156 | return (0); | ||
157 | val |= parts[0] << 24; | ||
158 | break; | ||
159 | |||
160 | case 3: /* a.b.c -- 8.8.16 bits */ | ||
161 | if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) | ||
162 | return (0); | ||
163 | val |= (parts[0] << 24) | (parts[1] << 16); | ||
164 | break; | ||
165 | |||
166 | case 4: /* a.b.c.d -- 8.8.8.8 bits */ | ||
167 | if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) | ||
168 | return (0); | ||
169 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); | ||
170 | break; | ||
171 | } | ||
172 | if (addr) | ||
173 | addr->s_addr = htonl(val); | ||
174 | return (1); | ||
175 | } | ||
diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c new file mode 100644 index 0000000000..b1a58cd2c1 --- /dev/null +++ b/src/lib/libc/net/inet_lnaof.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: inet_lnaof.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/param.h> | ||
32 | #include <netinet/in.h> | ||
33 | #include <arpa/inet.h> | ||
34 | |||
35 | /* | ||
36 | * Return the local network address portion of an | ||
37 | * internet address; handles class a/b/c network | ||
38 | * number formats. | ||
39 | */ | ||
40 | in_addr_t | ||
41 | inet_lnaof(struct in_addr in) | ||
42 | { | ||
43 | in_addr_t i = ntohl(in.s_addr); | ||
44 | |||
45 | if (IN_CLASSA(i)) | ||
46 | return ((i)&IN_CLASSA_HOST); | ||
47 | else if (IN_CLASSB(i)) | ||
48 | return ((i)&IN_CLASSB_HOST); | ||
49 | else | ||
50 | return ((i)&IN_CLASSC_HOST); | ||
51 | } | ||
diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c new file mode 100644 index 0000000000..87d9325231 --- /dev/null +++ b/src/lib/libc/net/inet_makeaddr.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* $OpenBSD: inet_makeaddr.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/param.h> | ||
32 | #include <netinet/in.h> | ||
33 | #include <arpa/inet.h> | ||
34 | |||
35 | /* | ||
36 | * Formulate an Internet address from network + host. Used in | ||
37 | * building addresses stored in the ifnet structure. | ||
38 | */ | ||
39 | struct in_addr | ||
40 | inet_makeaddr(in_addr_t net, in_addr_t host) | ||
41 | { | ||
42 | in_addr_t addr; | ||
43 | |||
44 | if (net < 128) | ||
45 | addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); | ||
46 | else if (net < 65536) | ||
47 | addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST); | ||
48 | else if (net < 16777216L) | ||
49 | addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST); | ||
50 | else | ||
51 | addr = net | host; | ||
52 | addr = htonl(addr); | ||
53 | return (*(struct in_addr *)&addr); | ||
54 | } | ||
diff --git a/src/lib/libc/net/inet_net.3 b/src/lib/libc/net/inet_net.3 new file mode 100644 index 0000000000..a0231b4537 --- /dev/null +++ b/src/lib/libc/net/inet_net.3 | |||
@@ -0,0 +1,197 @@ | |||
1 | .\" $OpenBSD: inet_net.3,v 1.16 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" $NetBSD: inet_net.3,v 1.1 1997/06/18 02:25:27 lukem Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1997 The NetBSD Foundation, Inc. | ||
5 | .\" All rights reserved. | ||
6 | .\" | ||
7 | .\" This code is derived from software contributed to The NetBSD Foundation | ||
8 | .\" by Luke Mewburn. | ||
9 | .\" | ||
10 | .\" Redistribution and use in source and binary forms, with or without | ||
11 | .\" modification, are permitted provided that the following conditions | ||
12 | .\" are met: | ||
13 | .\" 1. Redistributions of source code must retain the above copyright | ||
14 | .\" notice, this list of conditions and the following disclaimer. | ||
15 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
16 | .\" notice, this list of conditions and the following disclaimer in the | ||
17 | .\" documentation and/or other materials provided with the distribution. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
20 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
21 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
22 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE | ||
23 | .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | .\" POSSIBILITY OF SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd $Mdocdate: June 5 2013 $ | ||
32 | .Dt INET_NET 3 | ||
33 | .Os | ||
34 | .Sh NAME | ||
35 | .Nm inet_net_ntop , | ||
36 | .Nm inet_net_pton | ||
37 | .Nd Internet network number manipulation routines | ||
38 | .Sh SYNOPSIS | ||
39 | .In sys/types.h | ||
40 | .In sys/socket.h | ||
41 | .In netinet/in.h | ||
42 | .In arpa/inet.h | ||
43 | .Ft char * | ||
44 | .Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size" | ||
45 | .Ft int | ||
46 | .Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn inet_net_ntop | ||
50 | function converts an Internet network number from network format (usually a | ||
51 | .Li struct in_addr | ||
52 | or some other binary form, in network byte order) to CIDR presentation format | ||
53 | (suitable for external display purposes). | ||
54 | .Fa bits | ||
55 | is the number of bits in | ||
56 | .Fa src | ||
57 | that are the network number. | ||
58 | It returns | ||
59 | .Dv NULL | ||
60 | if a system error occurs (in which case, | ||
61 | .Va errno | ||
62 | will have been set), or it returns a pointer to the destination string. | ||
63 | .Pp | ||
64 | The | ||
65 | .Fn inet_net_pton | ||
66 | function converts a presentation format Internet network number (that is, | ||
67 | printable form as held in a character string) to network format (usually a | ||
68 | .Li struct in_addr | ||
69 | or some other internal binary representation, in network byte order). | ||
70 | It returns the number of bits (either computed based on the class, or | ||
71 | specified with /CIDR), or \-1 if a failure occurred | ||
72 | (in which case | ||
73 | .Va errno | ||
74 | will have been set. | ||
75 | It will be set to | ||
76 | .Er ENOENT | ||
77 | if the Internet network number was not valid). | ||
78 | .Pp | ||
79 | Caution: | ||
80 | The | ||
81 | .Fa dst | ||
82 | field should be zeroed before calling | ||
83 | .Fn inet_net_pton | ||
84 | as the function will only fill the number of bytes necessary to | ||
85 | encode the network number in network byte order. | ||
86 | .Pp | ||
87 | The only values for | ||
88 | .Fa af | ||
89 | currently supported are | ||
90 | .Dv AF_INET | ||
91 | and | ||
92 | .Dv AF_INET6 . | ||
93 | .Fa size | ||
94 | is the size of the result buffer | ||
95 | .Fa dst . | ||
96 | .Sh NETWORK NUMBERS (IP VERSION 4) | ||
97 | The external representation of Internet network numbers may be specified in | ||
98 | one of the following forms: | ||
99 | .Bd -literal -offset indent | ||
100 | a | ||
101 | a.b | ||
102 | a.b.c | ||
103 | a.b.c.d | ||
104 | .Ed | ||
105 | .Pp | ||
106 | Any of the above four forms may have | ||
107 | .Dq Li /bits | ||
108 | appended where | ||
109 | .Dq Li bits | ||
110 | is in the range | ||
111 | .Li 0-32 | ||
112 | and is used to explicitly specify the number of bits in the network address. | ||
113 | When | ||
114 | .Dq Li /bits | ||
115 | is not specified the number of bits in the network address is calculated | ||
116 | as the larger of the number of bits in the class to which the address | ||
117 | belongs and the number of bits provided rounded up modulo 8. | ||
118 | Examples: | ||
119 | .Pp | ||
120 | .Bl -tag -width 10.1.2.3/24 -offset indent -compact | ||
121 | .It Li 10 | ||
122 | an 8-bit network number (class A), value | ||
123 | .Li 10.0.0.0 . | ||
124 | .It Li 192 | ||
125 | a 24-bit network number (class C), value | ||
126 | .Li 192.0.0.0 . | ||
127 | .It Li 10.10 | ||
128 | a 16-bit network number, value | ||
129 | .Li 10.10.0.0 . | ||
130 | .It Li 10.1.2 | ||
131 | a 24-bit network number, value | ||
132 | .Li 10.1.2.0 . | ||
133 | .It Li 10.1.2.3 | ||
134 | a 32-bit network number, value | ||
135 | .Li 10.1.2.3 . | ||
136 | .It Li 10.1.2.3/24 | ||
137 | a 24-bit network number (explicit), value | ||
138 | .Li 10.1.2.3 . | ||
139 | .El | ||
140 | .Pp | ||
141 | Note that when the number of bits is specified using | ||
142 | .Dq Li /bits | ||
143 | notation, the value of the address still includes all bits supplied | ||
144 | in the external representation, even those bits which are the host | ||
145 | part of an Internet address. | ||
146 | Also, unlike | ||
147 | .Xr inet_pton 3 | ||
148 | where the external representation is assumed to be a host address, the | ||
149 | external representation for | ||
150 | .Fn inet_net_pton | ||
151 | is assumed to be a network address. | ||
152 | Thus | ||
153 | .Dq Li 10.1 | ||
154 | is assumed to be | ||
155 | .Dq Li 10.1.0.0 | ||
156 | not | ||
157 | .Dq Li 10.0.0.1 | ||
158 | .Pp | ||
159 | All numbers supplied as | ||
160 | .Dq parts | ||
161 | in a | ||
162 | .Ql \&. | ||
163 | notation | ||
164 | may be decimal, octal, or hexadecimal, as specified | ||
165 | in the C language (i.e., a leading 0x or 0X implies | ||
166 | hexadecimal; otherwise, a leading 0 implies octal; | ||
167 | otherwise, the number is interpreted as decimal). | ||
168 | .Sh NETWORK NUMBERS (IP VERSION 6) | ||
169 | See | ||
170 | .Xr inet_pton 3 | ||
171 | for valid external representations of IP version 6 addresses. | ||
172 | A valid external representation may have | ||
173 | .Dq Li /bits | ||
174 | appended where | ||
175 | .Dq Li bits | ||
176 | is in the range | ||
177 | .Li 0-128 | ||
178 | and is used to explicitly specify the number of bits in the network address. | ||
179 | When | ||
180 | .Dq Li /bits | ||
181 | is not specified 128 is used. | ||
182 | Note that when the number of bits is specified using | ||
183 | .Dq Li /bits | ||
184 | notation, the value of the address still includes all bits supplied | ||
185 | in the external representation, even those bits which are the host | ||
186 | part of an Internet address. | ||
187 | .Sh SEE ALSO | ||
188 | .Xr byteorder 3 , | ||
189 | .Xr inet 3 , | ||
190 | .Xr inet_pton 3 , | ||
191 | .Xr networks 5 | ||
192 | .Sh HISTORY | ||
193 | The | ||
194 | .Nm inet_net_ntop | ||
195 | and | ||
196 | .Nm inet_net_pton | ||
197 | functions first appeared in BIND 4.9.4. | ||
diff --git a/src/lib/libc/net/inet_net_ntop.c b/src/lib/libc/net/inet_net_ntop.c new file mode 100644 index 0000000000..35ae7909d3 --- /dev/null +++ b/src/lib/libc/net/inet_net_ntop.c | |||
@@ -0,0 +1,162 @@ | |||
1 | /* $OpenBSD: inet_net_ntop.c,v 1.7 2012/06/22 19:13:37 gilles Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org> | ||
5 | * Copyright (c) 1996 by Internet Software Consortium. | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
12 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
13 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
14 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
15 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
16 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
17 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
18 | * SOFTWARE. | ||
19 | */ | ||
20 | |||
21 | #include <sys/types.h> | ||
22 | #include <sys/socket.h> | ||
23 | #include <netinet/in.h> | ||
24 | #include <arpa/inet.h> | ||
25 | |||
26 | #include <errno.h> | ||
27 | #include <stdio.h> | ||
28 | #include <string.h> | ||
29 | #include <stdlib.h> | ||
30 | |||
31 | static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t); | ||
32 | static char *inet_net_ntop_ipv6(const u_char *, int, char *, size_t); | ||
33 | |||
34 | /* | ||
35 | * char * | ||
36 | * inet_net_ntop(af, src, bits, dst, size) | ||
37 | * convert network number from network to presentation format. | ||
38 | * generates CIDR style result always. | ||
39 | * return: | ||
40 | * pointer to dst, or NULL if an error occurred (check errno). | ||
41 | * author: | ||
42 | * Paul Vixie (ISC), July 1996 | ||
43 | */ | ||
44 | char * | ||
45 | inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size) | ||
46 | { | ||
47 | switch (af) { | ||
48 | case AF_INET: | ||
49 | return (inet_net_ntop_ipv4(src, bits, dst, size)); | ||
50 | case AF_INET6: | ||
51 | return (inet_net_ntop_ipv6(src, bits, dst, size)); | ||
52 | default: | ||
53 | errno = EAFNOSUPPORT; | ||
54 | return (NULL); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | /* | ||
59 | * static char * | ||
60 | * inet_net_ntop_ipv4(src, bits, dst, size) | ||
61 | * convert IPv4 network number from network to presentation format. | ||
62 | * generates CIDR style result always. | ||
63 | * return: | ||
64 | * pointer to dst, or NULL if an error occurred (check errno). | ||
65 | * note: | ||
66 | * network byte order assumed. this means 192.5.5.240/28 has | ||
67 | * 0x11110000 in its fourth octet. | ||
68 | * author: | ||
69 | * Paul Vixie (ISC), July 1996 | ||
70 | */ | ||
71 | static char * | ||
72 | inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) | ||
73 | { | ||
74 | char *odst = dst; | ||
75 | u_int m; | ||
76 | int b; | ||
77 | char *ep; | ||
78 | int advance; | ||
79 | |||
80 | ep = dst + size; | ||
81 | if (ep <= dst) | ||
82 | goto emsgsize; | ||
83 | |||
84 | if (bits < 0 || bits > 32) { | ||
85 | errno = EINVAL; | ||
86 | return (NULL); | ||
87 | } | ||
88 | if (bits == 0) { | ||
89 | if (ep - dst < sizeof "0") | ||
90 | goto emsgsize; | ||
91 | *dst++ = '0'; | ||
92 | *dst = '\0'; | ||
93 | } | ||
94 | |||
95 | /* Format whole octets. */ | ||
96 | for (b = bits / 8; b > 0; b--) { | ||
97 | if (ep - dst < sizeof "255.") | ||
98 | goto emsgsize; | ||
99 | advance = snprintf(dst, ep - dst, "%u", *src++); | ||
100 | if (advance <= 0 || advance >= ep - dst) | ||
101 | goto emsgsize; | ||
102 | dst += advance; | ||
103 | if (b > 1) { | ||
104 | if (dst + 1 >= ep) | ||
105 | goto emsgsize; | ||
106 | *dst++ = '.'; | ||
107 | *dst = '\0'; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | /* Format partial octet. */ | ||
112 | b = bits % 8; | ||
113 | if (b > 0) { | ||
114 | if (ep - dst < sizeof ".255") | ||
115 | goto emsgsize; | ||
116 | if (dst != odst) | ||
117 | if (dst + 1 >= ep) | ||
118 | goto emsgsize; | ||
119 | *dst++ = '.'; | ||
120 | m = ((1 << b) - 1) << (8 - b); | ||
121 | advance = snprintf(dst, ep - dst, "%u", *src & m); | ||
122 | if (advance <= 0 || advance >= ep - dst) | ||
123 | goto emsgsize; | ||
124 | dst += advance; | ||
125 | } | ||
126 | |||
127 | /* Format CIDR /width. */ | ||
128 | if (ep - dst < sizeof "/32") | ||
129 | goto emsgsize; | ||
130 | advance = snprintf(dst, ep - dst, "/%u", bits); | ||
131 | if (advance <= 0 || advance >= ep - dst) | ||
132 | goto emsgsize; | ||
133 | dst += advance; | ||
134 | return (odst); | ||
135 | |||
136 | emsgsize: | ||
137 | errno = EMSGSIZE; | ||
138 | return (NULL); | ||
139 | } | ||
140 | |||
141 | static char * | ||
142 | inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) | ||
143 | { | ||
144 | int ret; | ||
145 | char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")]; | ||
146 | |||
147 | if (bits < 0 || bits > 128) { | ||
148 | errno = EINVAL; | ||
149 | return (NULL); | ||
150 | } | ||
151 | |||
152 | if (inet_ntop(AF_INET6, src, buf, size) == NULL) | ||
153 | return (NULL); | ||
154 | |||
155 | ret = snprintf(dst, size, "%s/%d", buf, bits); | ||
156 | if (ret == -1 || ret >= size) { | ||
157 | errno = EMSGSIZE; | ||
158 | return (NULL); | ||
159 | } | ||
160 | |||
161 | return (dst); | ||
162 | } | ||
diff --git a/src/lib/libc/net/inet_net_pton.c b/src/lib/libc/net/inet_net_pton.c new file mode 100644 index 0000000000..1683a79043 --- /dev/null +++ b/src/lib/libc/net/inet_net_pton.c | |||
@@ -0,0 +1,231 @@ | |||
1 | /* $OpenBSD: inet_net_pton.c,v 1.8 2013/11/25 18:23:51 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org> | ||
5 | * Copyright (c) 1996 by Internet Software Consortium. | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
12 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
13 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
14 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
15 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
16 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
17 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
18 | * SOFTWARE. | ||
19 | */ | ||
20 | |||
21 | #include <sys/types.h> | ||
22 | #include <sys/socket.h> | ||
23 | #include <netinet/in.h> | ||
24 | #include <arpa/inet.h> | ||
25 | |||
26 | #include <assert.h> | ||
27 | #include <ctype.h> | ||
28 | #include <errno.h> | ||
29 | #include <stdio.h> | ||
30 | #include <string.h> | ||
31 | #include <stdlib.h> | ||
32 | |||
33 | static int inet_net_pton_ipv4(const char *, u_char *, size_t); | ||
34 | static int inet_net_pton_ipv6(const char *, u_char *, size_t); | ||
35 | |||
36 | /* | ||
37 | * static int | ||
38 | * inet_net_pton(af, src, dst, size) | ||
39 | * convert network number from presentation to network format. | ||
40 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | ||
41 | * "size" is in bytes and describes "dst". | ||
42 | * return: | ||
43 | * number of bits, either imputed classfully or specified with /CIDR, | ||
44 | * or -1 if some failure occurred (check errno). ENOENT means it was | ||
45 | * not a valid network specification. | ||
46 | * author: | ||
47 | * Paul Vixie (ISC), June 1996 | ||
48 | */ | ||
49 | int | ||
50 | inet_net_pton(int af, const char *src, void *dst, size_t size) | ||
51 | { | ||
52 | switch (af) { | ||
53 | case AF_INET: | ||
54 | return (inet_net_pton_ipv4(src, dst, size)); | ||
55 | case AF_INET6: | ||
56 | return (inet_net_pton_ipv6(src, dst, size)); | ||
57 | default: | ||
58 | errno = EAFNOSUPPORT; | ||
59 | return (-1); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | /* | ||
64 | * static int | ||
65 | * inet_net_pton_ipv4(src, dst, size) | ||
66 | * convert IPv4 network number from presentation to network format. | ||
67 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | ||
68 | * "size" is in bytes and describes "dst". | ||
69 | * return: | ||
70 | * number of bits, either imputed classfully or specified with /CIDR, | ||
71 | * or -1 if some failure occurred (check errno). ENOENT means it was | ||
72 | * not an IPv4 network specification. | ||
73 | * note: | ||
74 | * network byte order assumed. this means 192.5.5.240/28 has | ||
75 | * 0x11110000 in its fourth octet. | ||
76 | * author: | ||
77 | * Paul Vixie (ISC), June 1996 | ||
78 | */ | ||
79 | static int | ||
80 | inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) | ||
81 | { | ||
82 | static const char | ||
83 | xdigits[] = "0123456789abcdef", | ||
84 | digits[] = "0123456789"; | ||
85 | int n, ch, tmp, dirty, bits; | ||
86 | const u_char *odst = dst; | ||
87 | |||
88 | ch = (unsigned char)*src++; | ||
89 | if (ch == '0' && (src[0] == 'x' || src[0] == 'X') | ||
90 | && isascii((unsigned char)src[1]) && isxdigit((unsigned char)src[1])) { | ||
91 | /* Hexadecimal: Eat nybble string. */ | ||
92 | if (size <= 0) | ||
93 | goto emsgsize; | ||
94 | *dst = 0, dirty = 0; | ||
95 | src++; /* skip x or X. */ | ||
96 | while ((ch = (unsigned char)*src++) != '\0' && | ||
97 | isascii(ch) && isxdigit(ch)) { | ||
98 | if (isupper(ch)) | ||
99 | ch = tolower(ch); | ||
100 | n = strchr(xdigits, ch) - xdigits; | ||
101 | assert(n >= 0 && n <= 15); | ||
102 | *dst |= n; | ||
103 | if (!dirty++) | ||
104 | *dst <<= 4; | ||
105 | else if (size-- > 0) | ||
106 | *++dst = 0, dirty = 0; | ||
107 | else | ||
108 | goto emsgsize; | ||
109 | } | ||
110 | if (dirty) | ||
111 | size--; | ||
112 | } else if (isascii(ch) && isdigit(ch)) { | ||
113 | /* Decimal: eat dotted digit string. */ | ||
114 | for (;;) { | ||
115 | tmp = 0; | ||
116 | do { | ||
117 | n = strchr(digits, ch) - digits; | ||
118 | assert(n >= 0 && n <= 9); | ||
119 | tmp *= 10; | ||
120 | tmp += n; | ||
121 | if (tmp > 255) | ||
122 | goto enoent; | ||
123 | } while ((ch = (unsigned char)*src++) != '\0' && | ||
124 | isascii(ch) && isdigit(ch)); | ||
125 | if (size-- <= 0) | ||
126 | goto emsgsize; | ||
127 | *dst++ = (u_char) tmp; | ||
128 | if (ch == '\0' || ch == '/') | ||
129 | break; | ||
130 | if (ch != '.') | ||
131 | goto enoent; | ||
132 | ch = (unsigned char)*src++; | ||
133 | if (!isascii(ch) || !isdigit(ch)) | ||
134 | goto enoent; | ||
135 | } | ||
136 | } else | ||
137 | goto enoent; | ||
138 | |||
139 | bits = -1; | ||
140 | if (ch == '/' && isascii((unsigned char)src[0]) && | ||
141 | isdigit((unsigned char)src[0]) && dst > odst) { | ||
142 | /* CIDR width specifier. Nothing can follow it. */ | ||
143 | ch = (unsigned char)*src++; /* Skip over the /. */ | ||
144 | bits = 0; | ||
145 | do { | ||
146 | n = strchr(digits, ch) - digits; | ||
147 | assert(n >= 0 && n <= 9); | ||
148 | bits *= 10; | ||
149 | bits += n; | ||
150 | if (bits > 32) | ||
151 | goto emsgsize; | ||
152 | } while ((ch = (unsigned char)*src++) != '\0' && | ||
153 | isascii(ch) && isdigit(ch)); | ||
154 | if (ch != '\0') | ||
155 | goto enoent; | ||
156 | } | ||
157 | |||
158 | /* Firey death and destruction unless we prefetched EOS. */ | ||
159 | if (ch != '\0') | ||
160 | goto enoent; | ||
161 | |||
162 | /* If nothing was written to the destination, we found no address. */ | ||
163 | if (dst == odst) | ||
164 | goto enoent; | ||
165 | /* If no CIDR spec was given, infer width from net class. */ | ||
166 | if (bits == -1) { | ||
167 | if (*odst >= 240) /* Class E */ | ||
168 | bits = 32; | ||
169 | else if (*odst >= 224) /* Class D */ | ||
170 | bits = 4; | ||
171 | else if (*odst >= 192) /* Class C */ | ||
172 | bits = 24; | ||
173 | else if (*odst >= 128) /* Class B */ | ||
174 | bits = 16; | ||
175 | else /* Class A */ | ||
176 | bits = 8; | ||
177 | /* If imputed mask is narrower than specified octets, widen. */ | ||
178 | if (bits < ((dst - odst) * 8)) | ||
179 | bits = (dst - odst) * 8; | ||
180 | } | ||
181 | /* Extend network to cover the actual mask. */ | ||
182 | while (bits > ((dst - odst) * 8)) { | ||
183 | if (size-- <= 0) | ||
184 | goto emsgsize; | ||
185 | *dst++ = '\0'; | ||
186 | } | ||
187 | return (bits); | ||
188 | |||
189 | enoent: | ||
190 | errno = ENOENT; | ||
191 | return (-1); | ||
192 | |||
193 | emsgsize: | ||
194 | errno = EMSGSIZE; | ||
195 | return (-1); | ||
196 | } | ||
197 | |||
198 | |||
199 | static int | ||
200 | inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) | ||
201 | { | ||
202 | int ret; | ||
203 | int bits; | ||
204 | char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")]; | ||
205 | char *sep; | ||
206 | const char *errstr; | ||
207 | |||
208 | if (strlcpy(buf, src, sizeof buf) >= sizeof buf) { | ||
209 | errno = EMSGSIZE; | ||
210 | return (-1); | ||
211 | } | ||
212 | |||
213 | sep = strchr(buf, '/'); | ||
214 | if (sep != NULL) | ||
215 | *sep++ = '\0'; | ||
216 | |||
217 | ret = inet_pton(AF_INET6, buf, dst); | ||
218 | if (ret != 1) | ||
219 | return (-1); | ||
220 | |||
221 | if (sep == NULL) | ||
222 | return 128; | ||
223 | |||
224 | bits = strtonum(sep, 0, 128, &errstr); | ||
225 | if (errstr) { | ||
226 | errno = EINVAL; | ||
227 | return (-1); | ||
228 | } | ||
229 | |||
230 | return bits; | ||
231 | } | ||
diff --git a/src/lib/libc/net/inet_neta.c b/src/lib/libc/net/inet_neta.c new file mode 100644 index 0000000000..e3e7d0eb71 --- /dev/null +++ b/src/lib/libc/net/inet_neta.c | |||
@@ -0,0 +1,80 @@ | |||
1 | /* $OpenBSD: inet_neta.c,v 1.7 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #include <sys/types.h> | ||
21 | #include <sys/socket.h> | ||
22 | #include <netinet/in.h> | ||
23 | #include <arpa/inet.h> | ||
24 | |||
25 | #include <errno.h> | ||
26 | #include <stdio.h> | ||
27 | #include <string.h> | ||
28 | |||
29 | /* | ||
30 | * char * | ||
31 | * inet_neta(src, dst, size) | ||
32 | * format an in_addr_t network number into presentation format. | ||
33 | * return: | ||
34 | * pointer to dst, or NULL if an error occurred (check errno). | ||
35 | * note: | ||
36 | * format of ``src'' is as for inet_network(). | ||
37 | * author: | ||
38 | * Paul Vixie (ISC), July 1996 | ||
39 | */ | ||
40 | char * | ||
41 | inet_neta(in_addr_t src, char *dst, size_t size) | ||
42 | { | ||
43 | char *odst = dst; | ||
44 | char *ep; | ||
45 | int advance; | ||
46 | |||
47 | if (src == 0x00000000) { | ||
48 | if (size < sizeof "0.0.0.0") | ||
49 | goto emsgsize; | ||
50 | strlcpy(dst, "0.0.0.0", size); | ||
51 | return dst; | ||
52 | } | ||
53 | ep = dst + size; | ||
54 | if (ep <= dst) | ||
55 | goto emsgsize; | ||
56 | while (src & 0xffffffff) { | ||
57 | u_char b = (src & 0xff000000) >> 24; | ||
58 | |||
59 | src <<= 8; | ||
60 | if (b || src) { | ||
61 | if (ep - dst < sizeof "255.") | ||
62 | goto emsgsize; | ||
63 | advance = snprintf(dst, ep - dst, "%u", b); | ||
64 | if (advance <= 0 || advance >= ep - dst) | ||
65 | goto emsgsize; | ||
66 | dst += advance; | ||
67 | if (src != 0L) { | ||
68 | if (dst + 1 >= ep) | ||
69 | goto emsgsize; | ||
70 | *dst++ = '.'; | ||
71 | *dst = '\0'; | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | return (odst); | ||
76 | |||
77 | emsgsize: | ||
78 | errno = EMSGSIZE; | ||
79 | return (NULL); | ||
80 | } | ||
diff --git a/src/lib/libc/net/inet_netof.c b/src/lib/libc/net/inet_netof.c new file mode 100644 index 0000000000..2f468c3aca --- /dev/null +++ b/src/lib/libc/net/inet_netof.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: inet_netof.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/param.h> | ||
32 | #include <netinet/in.h> | ||
33 | #include <arpa/inet.h> | ||
34 | |||
35 | /* | ||
36 | * Return the network number from an internet | ||
37 | * address; handles class a/b/c network #'s. | ||
38 | */ | ||
39 | in_addr_t | ||
40 | inet_netof(struct in_addr in) | ||
41 | { | ||
42 | in_addr_t i = ntohl(in.s_addr); | ||
43 | |||
44 | if (IN_CLASSA(i)) | ||
45 | return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); | ||
46 | else if (IN_CLASSB(i)) | ||
47 | return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); | ||
48 | else | ||
49 | return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); | ||
50 | } | ||
diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c new file mode 100644 index 0000000000..ecf554e4f9 --- /dev/null +++ b/src/lib/libc/net/inet_network.c | |||
@@ -0,0 +1,84 @@ | |||
1 | /* $OpenBSD: inet_network.c,v 1.11 2013/11/25 17:29:19 deraadt Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/types.h> | ||
32 | #include <netinet/in.h> | ||
33 | #include <arpa/inet.h> | ||
34 | #include <ctype.h> | ||
35 | |||
36 | /* | ||
37 | * Internet network address interpretation routine. | ||
38 | * The library routines call this routine to interpret | ||
39 | * network numbers. | ||
40 | */ | ||
41 | in_addr_t | ||
42 | inet_network(const char *cp) | ||
43 | { | ||
44 | in_addr_t val, base, n; | ||
45 | u_char c; | ||
46 | in_addr_t parts[4], *pp = parts; | ||
47 | int i; | ||
48 | |||
49 | again: | ||
50 | val = 0; base = 10; | ||
51 | if (*cp == '0') | ||
52 | base = 8, cp++; | ||
53 | if (*cp == 'x' || *cp == 'X') | ||
54 | base = 16, cp++; | ||
55 | while ((c = *cp)) { | ||
56 | if (isdigit(c)) { | ||
57 | val = (val * base) + (c - '0'); | ||
58 | cp++; | ||
59 | continue; | ||
60 | } | ||
61 | if (base == 16 && isxdigit(c)) { | ||
62 | val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); | ||
63 | cp++; | ||
64 | continue; | ||
65 | } | ||
66 | break; | ||
67 | } | ||
68 | if (*cp == '.') { | ||
69 | if (pp >= parts + 3) | ||
70 | return (INADDR_NONE); | ||
71 | *pp++ = val, cp++; | ||
72 | goto again; | ||
73 | } | ||
74 | if (*cp && !isspace(*cp)) | ||
75 | return (INADDR_NONE); | ||
76 | *pp++ = val; | ||
77 | n = pp - parts; | ||
78 | for (val = 0, i = 0; i < 4; i++) { | ||
79 | val <<= 8; | ||
80 | if (i < n) | ||
81 | val |= parts[i] & 0xff; | ||
82 | } | ||
83 | return (val); | ||
84 | } | ||
diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c new file mode 100644 index 0000000000..ff5d93ded2 --- /dev/null +++ b/src/lib/libc/net/inet_ntoa.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: inet_ntoa.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | /* | ||
32 | * Convert network-format internet address | ||
33 | * to base 256 d.d.d.d representation. | ||
34 | */ | ||
35 | #include <sys/types.h> | ||
36 | #include <netinet/in.h> | ||
37 | #include <arpa/inet.h> | ||
38 | #include <stdio.h> | ||
39 | |||
40 | char * | ||
41 | inet_ntoa(struct in_addr in) | ||
42 | { | ||
43 | static char b[18]; | ||
44 | char *p; | ||
45 | |||
46 | p = (char *)∈ | ||
47 | #define UC(b) (((int)b)&0xff) | ||
48 | (void)snprintf(b, sizeof(b), | ||
49 | "%u.%u.%u.%u", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); | ||
50 | return (b); | ||
51 | } | ||
diff --git a/src/lib/libc/net/inet_ntop.c b/src/lib/libc/net/inet_ntop.c new file mode 100644 index 0000000000..359acd8cda --- /dev/null +++ b/src/lib/libc/net/inet_ntop.c | |||
@@ -0,0 +1,205 @@ | |||
1 | /* $OpenBSD: inet_ntop.c,v 1.9 2014/02/05 14:20:43 millert Exp $ */ | ||
2 | |||
3 | /* Copyright (c) 1996 by Internet Software Consortium. | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
10 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
11 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
12 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
13 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
14 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
15 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
16 | * SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <sys/param.h> | ||
20 | #include <sys/types.h> | ||
21 | #include <sys/socket.h> | ||
22 | #include <netinet/in.h> | ||
23 | #include <arpa/inet.h> | ||
24 | #include <arpa/nameser.h> | ||
25 | #include <string.h> | ||
26 | #include <errno.h> | ||
27 | #include <stdio.h> | ||
28 | |||
29 | /* | ||
30 | * WARNING: Don't even consider trying to compile this on a system where | ||
31 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | ||
32 | */ | ||
33 | |||
34 | static const char *inet_ntop4(const u_char *src, char *dst, size_t size); | ||
35 | static const char *inet_ntop6(const u_char *src, char *dst, size_t size); | ||
36 | |||
37 | /* const char * | ||
38 | * inet_ntop(af, src, dst, size) | ||
39 | * convert a network format address to presentation format. | ||
40 | * return: | ||
41 | * pointer to presentation format address (`dst'), or NULL (see errno). | ||
42 | * author: | ||
43 | * Paul Vixie, 1996. | ||
44 | */ | ||
45 | const char * | ||
46 | inet_ntop(int af, const void *src, char *dst, socklen_t size) | ||
47 | { | ||
48 | switch (af) { | ||
49 | case AF_INET: | ||
50 | return (inet_ntop4(src, dst, (size_t)size)); | ||
51 | case AF_INET6: | ||
52 | return (inet_ntop6(src, dst, (size_t)size)); | ||
53 | default: | ||
54 | errno = EAFNOSUPPORT; | ||
55 | return (NULL); | ||
56 | } | ||
57 | /* NOTREACHED */ | ||
58 | } | ||
59 | |||
60 | /* const char * | ||
61 | * inet_ntop4(src, dst, size) | ||
62 | * format an IPv4 address, more or less like inet_ntoa() | ||
63 | * return: | ||
64 | * `dst' (as a const) | ||
65 | * notes: | ||
66 | * (1) uses no statics | ||
67 | * (2) takes a u_char* not an in_addr as input | ||
68 | * author: | ||
69 | * Paul Vixie, 1996. | ||
70 | */ | ||
71 | static const char * | ||
72 | inet_ntop4(const u_char *src, char *dst, size_t size) | ||
73 | { | ||
74 | static const char fmt[] = "%u.%u.%u.%u"; | ||
75 | char tmp[sizeof "255.255.255.255"]; | ||
76 | int l; | ||
77 | |||
78 | l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]); | ||
79 | if (l <= 0 || l >= size) { | ||
80 | errno = ENOSPC; | ||
81 | return (NULL); | ||
82 | } | ||
83 | strlcpy(dst, tmp, size); | ||
84 | return (dst); | ||
85 | } | ||
86 | |||
87 | /* const char * | ||
88 | * inet_ntop6(src, dst, size) | ||
89 | * convert IPv6 binary address into presentation (printable) format | ||
90 | * author: | ||
91 | * Paul Vixie, 1996. | ||
92 | */ | ||
93 | static const char * | ||
94 | inet_ntop6(const u_char *src, char *dst, size_t size) | ||
95 | { | ||
96 | /* | ||
97 | * Note that int32_t and int16_t need only be "at least" large enough | ||
98 | * to contain a value of the specified size. On some systems, like | ||
99 | * Crays, there is no such thing as an integer variable with 16 bits. | ||
100 | * Keep this in mind if you think this function should have been coded | ||
101 | * to use pointer overlays. All the world's not a VAX. | ||
102 | */ | ||
103 | char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; | ||
104 | char *tp, *ep; | ||
105 | struct { int base, len; } best, cur; | ||
106 | u_int words[IN6ADDRSZ / INT16SZ]; | ||
107 | int i; | ||
108 | int advance; | ||
109 | |||
110 | /* | ||
111 | * Preprocess: | ||
112 | * Copy the input (bytewise) array into a wordwise array. | ||
113 | * Find the longest run of 0x00's in src[] for :: shorthanding. | ||
114 | */ | ||
115 | memset(words, '\0', sizeof words); | ||
116 | for (i = 0; i < IN6ADDRSZ; i++) | ||
117 | words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); | ||
118 | best.base = -1; | ||
119 | cur.base = -1; | ||
120 | for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { | ||
121 | if (words[i] == 0) { | ||
122 | if (cur.base == -1) | ||
123 | cur.base = i, cur.len = 1; | ||
124 | else | ||
125 | cur.len++; | ||
126 | } else { | ||
127 | if (cur.base != -1) { | ||
128 | if (best.base == -1 || cur.len > best.len) | ||
129 | best = cur; | ||
130 | cur.base = -1; | ||
131 | } | ||
132 | } | ||
133 | } | ||
134 | if (cur.base != -1) { | ||
135 | if (best.base == -1 || cur.len > best.len) | ||
136 | best = cur; | ||
137 | } | ||
138 | if (best.base != -1 && best.len < 2) | ||
139 | best.base = -1; | ||
140 | |||
141 | /* | ||
142 | * Format the result. | ||
143 | */ | ||
144 | tp = tmp; | ||
145 | ep = tmp + sizeof(tmp); | ||
146 | for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) { | ||
147 | /* Are we inside the best run of 0x00's? */ | ||
148 | if (best.base != -1 && i >= best.base && | ||
149 | i < (best.base + best.len)) { | ||
150 | if (i == best.base) { | ||
151 | if (tp + 1 >= ep) { | ||
152 | errno = ENOSPC; | ||
153 | return (NULL); | ||
154 | } | ||
155 | *tp++ = ':'; | ||
156 | } | ||
157 | continue; | ||
158 | } | ||
159 | /* Are we following an initial run of 0x00s or any real hex? */ | ||
160 | if (i != 0) { | ||
161 | if (tp + 1 >= ep) { | ||
162 | errno = ENOSPC; | ||
163 | return (NULL); | ||
164 | } | ||
165 | *tp++ = ':'; | ||
166 | } | ||
167 | /* Is this address an encapsulated IPv4? */ | ||
168 | if (i == 6 && best.base == 0 && | ||
169 | (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { | ||
170 | if (!inet_ntop4(src+12, tp, (size_t)(ep - tp))) | ||
171 | return (NULL); | ||
172 | tp += strlen(tp); | ||
173 | break; | ||
174 | } | ||
175 | advance = snprintf(tp, ep - tp, "%x", words[i]); | ||
176 | if (advance <= 0 || advance >= ep - tp) { | ||
177 | errno = ENOSPC; | ||
178 | return (NULL); | ||
179 | } | ||
180 | tp += advance; | ||
181 | } | ||
182 | /* Was it a trailing run of 0x00's? */ | ||
183 | if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) { | ||
184 | if (tp + 1 >= ep) { | ||
185 | errno = ENOSPC; | ||
186 | return (NULL); | ||
187 | } | ||
188 | *tp++ = ':'; | ||
189 | } | ||
190 | if (tp + 1 >= ep) { | ||
191 | errno = ENOSPC; | ||
192 | return (NULL); | ||
193 | } | ||
194 | *tp++ = '\0'; | ||
195 | |||
196 | /* | ||
197 | * Check for overflow, copy, and we're done. | ||
198 | */ | ||
199 | if ((size_t)(tp - tmp) > size) { | ||
200 | errno = ENOSPC; | ||
201 | return (NULL); | ||
202 | } | ||
203 | strlcpy(dst, tmp, size); | ||
204 | return (dst); | ||
205 | } | ||
diff --git a/src/lib/libc/net/inet_pton.c b/src/lib/libc/net/inet_pton.c new file mode 100644 index 0000000000..7e521c3286 --- /dev/null +++ b/src/lib/libc/net/inet_pton.c | |||
@@ -0,0 +1,213 @@ | |||
1 | /* $OpenBSD: inet_pton.c,v 1.8 2010/05/06 15:47:14 claudio Exp $ */ | ||
2 | |||
3 | /* Copyright (c) 1996 by Internet Software Consortium. | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
10 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
11 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
12 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
13 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
14 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
15 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
16 | * SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <sys/param.h> | ||
20 | #include <sys/types.h> | ||
21 | #include <sys/socket.h> | ||
22 | #include <netinet/in.h> | ||
23 | #include <arpa/inet.h> | ||
24 | #include <arpa/nameser.h> | ||
25 | #include <string.h> | ||
26 | #include <errno.h> | ||
27 | |||
28 | /* | ||
29 | * WARNING: Don't even consider trying to compile this on a system where | ||
30 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | ||
31 | */ | ||
32 | |||
33 | static int inet_pton4(const char *src, u_char *dst); | ||
34 | static int inet_pton6(const char *src, u_char *dst); | ||
35 | |||
36 | /* int | ||
37 | * inet_pton(af, src, dst) | ||
38 | * convert from presentation format (which usually means ASCII printable) | ||
39 | * to network format (which is usually some kind of binary format). | ||
40 | * return: | ||
41 | * 1 if the address was valid for the specified address family | ||
42 | * 0 if the address wasn't valid (`dst' is untouched in this case) | ||
43 | * -1 if some other error occurred (`dst' is untouched in this case, too) | ||
44 | * author: | ||
45 | * Paul Vixie, 1996. | ||
46 | */ | ||
47 | int | ||
48 | inet_pton(int af, const char *src, void *dst) | ||
49 | { | ||
50 | switch (af) { | ||
51 | case AF_INET: | ||
52 | return (inet_pton4(src, dst)); | ||
53 | case AF_INET6: | ||
54 | return (inet_pton6(src, dst)); | ||
55 | default: | ||
56 | errno = EAFNOSUPPORT; | ||
57 | return (-1); | ||
58 | } | ||
59 | /* NOTREACHED */ | ||
60 | } | ||
61 | |||
62 | /* int | ||
63 | * inet_pton4(src, dst) | ||
64 | * like inet_aton() but without all the hexadecimal and shorthand. | ||
65 | * return: | ||
66 | * 1 if `src' is a valid dotted quad, else 0. | ||
67 | * notice: | ||
68 | * does not touch `dst' unless it's returning 1. | ||
69 | * author: | ||
70 | * Paul Vixie, 1996. | ||
71 | */ | ||
72 | static int | ||
73 | inet_pton4(const char *src, u_char *dst) | ||
74 | { | ||
75 | static const char digits[] = "0123456789"; | ||
76 | int saw_digit, octets, ch; | ||
77 | u_char tmp[INADDRSZ], *tp; | ||
78 | |||
79 | saw_digit = 0; | ||
80 | octets = 0; | ||
81 | *(tp = tmp) = 0; | ||
82 | while ((ch = *src++) != '\0') { | ||
83 | const char *pch; | ||
84 | |||
85 | if ((pch = strchr(digits, ch)) != NULL) { | ||
86 | u_int new = *tp * 10 + (pch - digits); | ||
87 | |||
88 | if (new > 255) | ||
89 | return (0); | ||
90 | if (! saw_digit) { | ||
91 | if (++octets > 4) | ||
92 | return (0); | ||
93 | saw_digit = 1; | ||
94 | } | ||
95 | *tp = new; | ||
96 | } else if (ch == '.' && saw_digit) { | ||
97 | if (octets == 4) | ||
98 | return (0); | ||
99 | *++tp = 0; | ||
100 | saw_digit = 0; | ||
101 | } else | ||
102 | return (0); | ||
103 | } | ||
104 | if (octets < 4) | ||
105 | return (0); | ||
106 | |||
107 | memcpy(dst, tmp, INADDRSZ); | ||
108 | return (1); | ||
109 | } | ||
110 | |||
111 | /* int | ||
112 | * inet_pton6(src, dst) | ||
113 | * convert presentation level address to network order binary form. | ||
114 | * return: | ||
115 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. | ||
116 | * notice: | ||
117 | * does not touch `dst' unless it's returning 1. | ||
118 | * credit: | ||
119 | * inspired by Mark Andrews. | ||
120 | * author: | ||
121 | * Paul Vixie, 1996. | ||
122 | */ | ||
123 | static int | ||
124 | inet_pton6(const char *src, u_char *dst) | ||
125 | { | ||
126 | static const char xdigits_l[] = "0123456789abcdef", | ||
127 | xdigits_u[] = "0123456789ABCDEF"; | ||
128 | u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp; | ||
129 | const char *xdigits, *curtok; | ||
130 | int ch, saw_xdigit, count_xdigit; | ||
131 | u_int val; | ||
132 | |||
133 | memset((tp = tmp), '\0', IN6ADDRSZ); | ||
134 | endp = tp + IN6ADDRSZ; | ||
135 | colonp = NULL; | ||
136 | /* Leading :: requires some special handling. */ | ||
137 | if (*src == ':') | ||
138 | if (*++src != ':') | ||
139 | return (0); | ||
140 | curtok = src; | ||
141 | saw_xdigit = count_xdigit = 0; | ||
142 | val = 0; | ||
143 | while ((ch = *src++) != '\0') { | ||
144 | const char *pch; | ||
145 | |||
146 | if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) | ||
147 | pch = strchr((xdigits = xdigits_u), ch); | ||
148 | if (pch != NULL) { | ||
149 | if (count_xdigit >= 4) | ||
150 | return (0); | ||
151 | val <<= 4; | ||
152 | val |= (pch - xdigits); | ||
153 | if (val > 0xffff) | ||
154 | return (0); | ||
155 | saw_xdigit = 1; | ||
156 | count_xdigit++; | ||
157 | continue; | ||
158 | } | ||
159 | if (ch == ':') { | ||
160 | curtok = src; | ||
161 | if (!saw_xdigit) { | ||
162 | if (colonp) | ||
163 | return (0); | ||
164 | colonp = tp; | ||
165 | continue; | ||
166 | } else if (*src == '\0') { | ||
167 | return (0); | ||
168 | } | ||
169 | if (tp + INT16SZ > endp) | ||
170 | return (0); | ||
171 | *tp++ = (u_char) (val >> 8) & 0xff; | ||
172 | *tp++ = (u_char) val & 0xff; | ||
173 | saw_xdigit = 0; | ||
174 | count_xdigit = 0; | ||
175 | val = 0; | ||
176 | continue; | ||
177 | } | ||
178 | if (ch == '.' && ((tp + INADDRSZ) <= endp) && | ||
179 | inet_pton4(curtok, tp) > 0) { | ||
180 | tp += INADDRSZ; | ||
181 | saw_xdigit = 0; | ||
182 | count_xdigit = 0; | ||
183 | break; /* '\0' was seen by inet_pton4(). */ | ||
184 | } | ||
185 | return (0); | ||
186 | } | ||
187 | if (saw_xdigit) { | ||
188 | if (tp + INT16SZ > endp) | ||
189 | return (0); | ||
190 | *tp++ = (u_char) (val >> 8) & 0xff; | ||
191 | *tp++ = (u_char) val & 0xff; | ||
192 | } | ||
193 | if (colonp != NULL) { | ||
194 | /* | ||
195 | * Since some memmove()'s erroneously fail to handle | ||
196 | * overlapping regions, we'll do the shift by hand. | ||
197 | */ | ||
198 | const int n = tp - colonp; | ||
199 | int i; | ||
200 | |||
201 | if (tp == endp) | ||
202 | return (0); | ||
203 | for (i = 1; i <= n; i++) { | ||
204 | endp[- i] = colonp[n - i]; | ||
205 | colonp[n - i] = 0; | ||
206 | } | ||
207 | tp = endp; | ||
208 | } | ||
209 | if (tp != endp) | ||
210 | return (0); | ||
211 | memcpy(dst, tmp, IN6ADDRSZ); | ||
212 | return (1); | ||
213 | } | ||
diff --git a/src/lib/libc/net/ip6opt.c b/src/lib/libc/net/ip6opt.c new file mode 100644 index 0000000000..7b4fdc4c4e --- /dev/null +++ b/src/lib/libc/net/ip6opt.c | |||
@@ -0,0 +1,593 @@ | |||
1 | /* $OpenBSD: ip6opt.c,v 1.6 2014/02/17 21:05:39 stsp Exp $ */ | ||
2 | /* $KAME: ip6opt.c,v 1.18 2005/06/15 07:11:35 keiichi Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. Neither the name of the project nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include <sys/param.h> | ||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | |||
37 | #include <netinet/in.h> | ||
38 | #include <netinet/ip6.h> | ||
39 | |||
40 | #include <string.h> | ||
41 | #include <stdio.h> | ||
42 | |||
43 | static int ip6optlen(u_int8_t *opt, u_int8_t *lim); | ||
44 | static void inet6_insert_padopt(u_char *p, int len); | ||
45 | |||
46 | /* | ||
47 | * This function returns the number of bytes required to hold an option | ||
48 | * when it is stored as ancillary data, including the cmsghdr structure | ||
49 | * at the beginning, and any padding at the end (to make its size a | ||
50 | * multiple of 8 bytes). The argument is the size of the structure | ||
51 | * defining the option, which must include any pad bytes at the | ||
52 | * beginning (the value y in the alignment term "xn + y"), the type | ||
53 | * byte, the length byte, and the option data. | ||
54 | */ | ||
55 | int | ||
56 | inet6_option_space(int nbytes) | ||
57 | { | ||
58 | nbytes += 2; /* we need space for nxt-hdr and length fields */ | ||
59 | return (CMSG_SPACE((nbytes + 7) & ~7)); | ||
60 | } | ||
61 | |||
62 | /* | ||
63 | * This function is called once per ancillary data object that will | ||
64 | * contain either Hop-by-Hop or Destination options. It returns 0 on | ||
65 | * success or -1 on an error. | ||
66 | */ | ||
67 | int | ||
68 | inet6_option_init(void *bp, struct cmsghdr **cmsgp, int type) | ||
69 | { | ||
70 | struct cmsghdr *ch = (struct cmsghdr *)bp; | ||
71 | |||
72 | /* argument validation */ | ||
73 | if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS) | ||
74 | return (-1); | ||
75 | |||
76 | ch->cmsg_level = IPPROTO_IPV6; | ||
77 | ch->cmsg_type = type; | ||
78 | ch->cmsg_len = CMSG_LEN(0); | ||
79 | |||
80 | *cmsgp = ch; | ||
81 | return (0); | ||
82 | } | ||
83 | |||
84 | /* | ||
85 | * This function appends a Hop-by-Hop option or a Destination option | ||
86 | * into an ancillary data object that has been initialized by | ||
87 | * inet6_option_init(). This function returns 0 if it succeeds or -1 on | ||
88 | * an error. | ||
89 | * multx is the value x in the alignment term "xn + y" described | ||
90 | * earlier. It must have a value of 1, 2, 4, or 8. | ||
91 | * plusy is the value y in the alignment term "xn + y" described | ||
92 | * earlier. It must have a value between 0 and 7, inclusive. | ||
93 | */ | ||
94 | int | ||
95 | inet6_option_append(struct cmsghdr *cmsg, const u_int8_t *typep, int multx, | ||
96 | int plusy) | ||
97 | { | ||
98 | int padlen, optlen, off; | ||
99 | u_char *bp = (u_char *)cmsg + cmsg->cmsg_len; | ||
100 | struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
101 | |||
102 | /* argument validation */ | ||
103 | if (multx != 1 && multx != 2 && multx != 4 && multx != 8) | ||
104 | return (-1); | ||
105 | if (plusy < 0 || plusy > 7) | ||
106 | return (-1); | ||
107 | #if 0 | ||
108 | if (typep[0] > 255) | ||
109 | return (-1); | ||
110 | #endif | ||
111 | |||
112 | /* | ||
113 | * If this is the first option, allocate space for the | ||
114 | * first 2 bytes(for next header and length fields) of | ||
115 | * the option header. | ||
116 | */ | ||
117 | if (bp == (u_char *)eh) { | ||
118 | bp += 2; | ||
119 | cmsg->cmsg_len += 2; | ||
120 | } | ||
121 | |||
122 | /* calculate pad length before the option. */ | ||
123 | off = bp - (u_char *)eh; | ||
124 | padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - | ||
125 | (off % multx); | ||
126 | padlen += plusy; | ||
127 | padlen %= multx; /* keep the pad as short as possible */ | ||
128 | /* insert padding */ | ||
129 | inet6_insert_padopt(bp, padlen); | ||
130 | cmsg->cmsg_len += padlen; | ||
131 | bp += padlen; | ||
132 | |||
133 | /* copy the option */ | ||
134 | if (typep[0] == IP6OPT_PAD1) | ||
135 | optlen = 1; | ||
136 | else | ||
137 | optlen = typep[1] + 2; | ||
138 | memcpy(bp, typep, optlen); | ||
139 | bp += optlen; | ||
140 | cmsg->cmsg_len += optlen; | ||
141 | |||
142 | /* calculate pad length after the option and insert the padding */ | ||
143 | off = bp - (u_char *)eh; | ||
144 | padlen = ((off + 7) & ~7) - off; | ||
145 | inet6_insert_padopt(bp, padlen); | ||
146 | bp += padlen; | ||
147 | cmsg->cmsg_len += padlen; | ||
148 | |||
149 | /* update the length field of the ip6 option header */ | ||
150 | eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; | ||
151 | |||
152 | return (0); | ||
153 | } | ||
154 | |||
155 | /* | ||
156 | * This function appends a Hop-by-Hop option or a Destination option | ||
157 | * into an ancillary data object that has been initialized by | ||
158 | * inet6_option_init(). This function returns a pointer to the 8-bit | ||
159 | * option type field that starts the option on success, or NULL on an | ||
160 | * error. | ||
161 | * The difference between this function and inet6_option_append() is | ||
162 | * that the latter copies the contents of a previously built option into | ||
163 | * the ancillary data object while the current function returns a | ||
164 | * pointer to the space in the data object where the option's TLV must | ||
165 | * then be built by the caller. | ||
166 | * | ||
167 | */ | ||
168 | u_int8_t * | ||
169 | inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy) | ||
170 | { | ||
171 | int padlen, off; | ||
172 | u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len; | ||
173 | u_int8_t *retval; | ||
174 | struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
175 | |||
176 | /* argument validation */ | ||
177 | if (multx != 1 && multx != 2 && multx != 4 && multx != 8) | ||
178 | return (NULL); | ||
179 | if (plusy < 0 || plusy > 7) | ||
180 | return (NULL); | ||
181 | |||
182 | /* | ||
183 | * If this is the first option, allocate space for the | ||
184 | * first 2 bytes(for next header and length fields) of | ||
185 | * the option header. | ||
186 | */ | ||
187 | if (bp == (u_char *)eh) { | ||
188 | bp += 2; | ||
189 | cmsg->cmsg_len += 2; | ||
190 | } | ||
191 | |||
192 | /* calculate pad length before the option. */ | ||
193 | off = bp - (u_char *)eh; | ||
194 | padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - | ||
195 | (off % multx); | ||
196 | padlen += plusy; | ||
197 | padlen %= multx; /* keep the pad as short as possible */ | ||
198 | /* insert padding */ | ||
199 | inet6_insert_padopt(bp, padlen); | ||
200 | cmsg->cmsg_len += padlen; | ||
201 | bp += padlen; | ||
202 | |||
203 | /* keep space to store specified length of data */ | ||
204 | retval = bp; | ||
205 | bp += datalen; | ||
206 | cmsg->cmsg_len += datalen; | ||
207 | |||
208 | /* calculate pad length after the option and insert the padding */ | ||
209 | off = bp - (u_char *)eh; | ||
210 | padlen = ((off + 7) & ~7) - off; | ||
211 | inet6_insert_padopt(bp, padlen); | ||
212 | bp += padlen; | ||
213 | cmsg->cmsg_len += padlen; | ||
214 | |||
215 | /* update the length field of the ip6 option header */ | ||
216 | eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; | ||
217 | |||
218 | return (retval); | ||
219 | } | ||
220 | |||
221 | /* | ||
222 | * This function processes the next Hop-by-Hop option or Destination | ||
223 | * option in an ancillary data object. If another option remains to be | ||
224 | * processed, the return value of the function is 0 and *tptrp points to | ||
225 | * the 8-bit option type field (which is followed by the 8-bit option | ||
226 | * data length, followed by the option data). If no more options remain | ||
227 | * to be processed, the return value is -1 and *tptrp is NULL. If an | ||
228 | * error occurs, the return value is -1 and *tptrp is not NULL. | ||
229 | * (RFC 2292, 6.3.5) | ||
230 | */ | ||
231 | int | ||
232 | inet6_option_next(const struct cmsghdr *cmsg, u_int8_t **tptrp) | ||
233 | { | ||
234 | struct ip6_ext *ip6e; | ||
235 | int hdrlen, optlen; | ||
236 | u_int8_t *lim; | ||
237 | |||
238 | if (cmsg->cmsg_level != IPPROTO_IPV6 || | ||
239 | (cmsg->cmsg_type != IPV6_HOPOPTS && | ||
240 | cmsg->cmsg_type != IPV6_DSTOPTS)) | ||
241 | return (-1); | ||
242 | |||
243 | /* message length validation */ | ||
244 | if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) | ||
245 | return (-1); | ||
246 | ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
247 | hdrlen = (ip6e->ip6e_len + 1) << 3; | ||
248 | if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) | ||
249 | return (-1); | ||
250 | |||
251 | /* | ||
252 | * If the caller does not specify the starting point, | ||
253 | * simply return the 1st option. | ||
254 | * Otherwise, search the option list for the next option. | ||
255 | */ | ||
256 | lim = (u_int8_t *)ip6e + hdrlen; | ||
257 | if (*tptrp == NULL) | ||
258 | *tptrp = (u_int8_t *)(ip6e + 1); | ||
259 | else { | ||
260 | if ((optlen = ip6optlen(*tptrp, lim)) == 0) | ||
261 | return (-1); | ||
262 | |||
263 | *tptrp = *tptrp + optlen; | ||
264 | } | ||
265 | if (*tptrp >= lim) { /* there is no option */ | ||
266 | *tptrp = NULL; | ||
267 | return (-1); | ||
268 | } | ||
269 | /* | ||
270 | * Finally, checks if the next option is safely stored in the | ||
271 | * cmsg data. | ||
272 | */ | ||
273 | if (ip6optlen(*tptrp, lim) == 0) | ||
274 | return (-1); | ||
275 | else | ||
276 | return (0); | ||
277 | } | ||
278 | |||
279 | /* | ||
280 | * This function is similar to the inet6_option_next() function, | ||
281 | * except this function lets the caller specify the option type to be | ||
282 | * searched for, instead of always returning the next option in the | ||
283 | * ancillary data object. | ||
284 | * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think | ||
285 | * it's a typo. The variable should be type of u_int8_t **. | ||
286 | */ | ||
287 | int | ||
288 | inet6_option_find(const struct cmsghdr *cmsg, u_int8_t **tptrp, int type) | ||
289 | { | ||
290 | struct ip6_ext *ip6e; | ||
291 | int hdrlen, optlen; | ||
292 | u_int8_t *optp, *lim; | ||
293 | |||
294 | if (cmsg->cmsg_level != IPPROTO_IPV6 || | ||
295 | (cmsg->cmsg_type != IPV6_HOPOPTS && | ||
296 | cmsg->cmsg_type != IPV6_DSTOPTS)) | ||
297 | return (-1); | ||
298 | |||
299 | /* message length validation */ | ||
300 | if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) | ||
301 | return (-1); | ||
302 | ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
303 | hdrlen = (ip6e->ip6e_len + 1) << 3; | ||
304 | if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) | ||
305 | return (-1); | ||
306 | |||
307 | /* | ||
308 | * If the caller does not specify the starting point, | ||
309 | * search from the beginning of the option list. | ||
310 | * Otherwise, search from *the next option* of the specified point. | ||
311 | */ | ||
312 | lim = (u_int8_t *)ip6e + hdrlen; | ||
313 | if (*tptrp == NULL) | ||
314 | *tptrp = (u_int8_t *)(ip6e + 1); | ||
315 | else { | ||
316 | if ((optlen = ip6optlen(*tptrp, lim)) == 0) | ||
317 | return (-1); | ||
318 | |||
319 | *tptrp = *tptrp + optlen; | ||
320 | } | ||
321 | for (optp = *tptrp; optp < lim; optp += optlen) { | ||
322 | if (*optp == type) { | ||
323 | *tptrp = optp; | ||
324 | return (0); | ||
325 | } | ||
326 | if ((optlen = ip6optlen(optp, lim)) == 0) | ||
327 | return (-1); | ||
328 | } | ||
329 | |||
330 | /* search failed */ | ||
331 | *tptrp = NULL; | ||
332 | return (-1); | ||
333 | } | ||
334 | |||
335 | /* | ||
336 | * Calculate the length of a given IPv6 option. Also checks | ||
337 | * if the option is safely stored in user's buffer according to the | ||
338 | * calculated length and the limitation of the buffer. | ||
339 | */ | ||
340 | static int | ||
341 | ip6optlen(u_int8_t *opt, u_int8_t *lim) | ||
342 | { | ||
343 | int optlen; | ||
344 | |||
345 | if (*opt == IP6OPT_PAD1) | ||
346 | optlen = 1; | ||
347 | else { | ||
348 | /* is there enough space to store type and len? */ | ||
349 | if (opt + 2 > lim) | ||
350 | return (0); | ||
351 | optlen = *(opt + 1) + 2; | ||
352 | } | ||
353 | if (opt + optlen <= lim) | ||
354 | return (optlen); | ||
355 | |||
356 | return (0); | ||
357 | } | ||
358 | |||
359 | static void | ||
360 | inet6_insert_padopt(u_char *p, int len) | ||
361 | { | ||
362 | switch(len) { | ||
363 | case 0: | ||
364 | return; | ||
365 | case 1: | ||
366 | p[0] = IP6OPT_PAD1; | ||
367 | return; | ||
368 | default: | ||
369 | p[0] = IP6OPT_PADN; | ||
370 | p[1] = len - 2; | ||
371 | memset(&p[2], 0, len - 2); | ||
372 | return; | ||
373 | } | ||
374 | } | ||
375 | |||
376 | /* | ||
377 | * The following functions are defined in RFC3542, which is a successor | ||
378 | * of RFC2292. | ||
379 | */ | ||
380 | |||
381 | int | ||
382 | inet6_opt_init(void *extbuf, socklen_t extlen) | ||
383 | { | ||
384 | struct ip6_ext *ext = (struct ip6_ext *)extbuf; | ||
385 | |||
386 | if (ext) { | ||
387 | if (extlen <= 0 || (extlen % 8)) | ||
388 | return (-1); | ||
389 | ext->ip6e_len = (extlen >> 3) - 1; | ||
390 | } | ||
391 | |||
392 | return (2); /* sizeof the next and the length fields */ | ||
393 | } | ||
394 | |||
395 | int | ||
396 | inet6_opt_append(void *extbuf, socklen_t extlen, int offset, u_int8_t type, | ||
397 | socklen_t len, u_int8_t align, void **databufp) | ||
398 | { | ||
399 | int currentlen = offset, padlen = 0; | ||
400 | |||
401 | /* | ||
402 | * The option type must have a value from 2 to 255, inclusive. | ||
403 | * (0 and 1 are reserved for the Pad1 and PadN options, respectively.) | ||
404 | */ | ||
405 | #if 0 /* always false */ | ||
406 | if (type < 2 || type > 255) | ||
407 | #else | ||
408 | if (type < 2) | ||
409 | #endif | ||
410 | return (-1); | ||
411 | |||
412 | /* | ||
413 | * The option data length must have a value between 0 and 255, | ||
414 | * inclusive, and is the length of the option data that follows. | ||
415 | */ | ||
416 | if (len < 0 || len > 255) | ||
417 | return (-1); | ||
418 | |||
419 | /* | ||
420 | * The align parameter must have a value of 1, 2, 4, or 8. | ||
421 | * The align value can not exceed the value of len. | ||
422 | */ | ||
423 | if (align != 1 && align != 2 && align != 4 && align != 8) | ||
424 | return (-1); | ||
425 | if (align > len) | ||
426 | return (-1); | ||
427 | |||
428 | /* Calculate the padding length. */ | ||
429 | currentlen += 2 + len; /* 2 means "type + len" */ | ||
430 | if (currentlen % align) | ||
431 | padlen = align - (currentlen % align); | ||
432 | |||
433 | /* The option must fit in the extension header buffer. */ | ||
434 | currentlen += padlen; | ||
435 | if (extlen && /* XXX: right? */ | ||
436 | currentlen > extlen) | ||
437 | return (-1); | ||
438 | |||
439 | if (extbuf) { | ||
440 | u_int8_t *optp = (u_int8_t *)extbuf + offset; | ||
441 | |||
442 | if (padlen == 1) { | ||
443 | /* insert a Pad1 option */ | ||
444 | *optp = IP6OPT_PAD1; | ||
445 | optp++; | ||
446 | } else if (padlen > 0) { | ||
447 | /* insert a PadN option for alignment */ | ||
448 | *optp++ = IP6OPT_PADN; | ||
449 | *optp++ = padlen - 2; | ||
450 | memset(optp, 0, padlen - 2); | ||
451 | optp += (padlen - 2); | ||
452 | } | ||
453 | |||
454 | *optp++ = type; | ||
455 | *optp++ = len; | ||
456 | |||
457 | *databufp = optp; | ||
458 | } | ||
459 | |||
460 | return (currentlen); | ||
461 | } | ||
462 | |||
463 | int | ||
464 | inet6_opt_finish(void *extbuf, socklen_t extlen, int offset) | ||
465 | { | ||
466 | int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0;; | ||
467 | |||
468 | if (extbuf) { | ||
469 | u_int8_t *padp; | ||
470 | int padlen = updatelen - offset; | ||
471 | |||
472 | if (updatelen > extlen) | ||
473 | return (-1); | ||
474 | |||
475 | padp = (u_int8_t *)extbuf + offset; | ||
476 | if (padlen == 1) | ||
477 | *padp = IP6OPT_PAD1; | ||
478 | else if (padlen > 0) { | ||
479 | *padp++ = IP6OPT_PADN; | ||
480 | *padp++ = (padlen - 2); | ||
481 | memset(padp, 0, padlen - 2); | ||
482 | } | ||
483 | } | ||
484 | |||
485 | return (updatelen); | ||
486 | } | ||
487 | |||
488 | int | ||
489 | inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen) | ||
490 | { | ||
491 | |||
492 | memcpy((u_int8_t *)databuf + offset, val, vallen); | ||
493 | return (offset + vallen); | ||
494 | } | ||
495 | |||
496 | int | ||
497 | inet6_opt_next(void *extbuf, socklen_t extlen, int offset, u_int8_t *typep, | ||
498 | socklen_t *lenp, void **databufp) | ||
499 | { | ||
500 | u_int8_t *optp, *lim; | ||
501 | int optlen; | ||
502 | |||
503 | /* Validate extlen. XXX: is the variable really necessary?? */ | ||
504 | if (extlen == 0 || (extlen % 8)) | ||
505 | return (-1); | ||
506 | lim = (u_int8_t *)extbuf + extlen; | ||
507 | |||
508 | /* | ||
509 | * If this is the first time this function called for this options | ||
510 | * header, simply return the 1st option. | ||
511 | * Otherwise, search the option list for the next option. | ||
512 | */ | ||
513 | if (offset == 0) | ||
514 | optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1); | ||
515 | else | ||
516 | optp = (u_int8_t *)extbuf + offset; | ||
517 | |||
518 | /* Find the next option skipping any padding options. */ | ||
519 | while (optp < lim) { | ||
520 | switch(*optp) { | ||
521 | case IP6OPT_PAD1: | ||
522 | optp++; | ||
523 | break; | ||
524 | case IP6OPT_PADN: | ||
525 | if ((optlen = ip6optlen(optp, lim)) == 0) | ||
526 | goto optend; | ||
527 | optp += optlen; | ||
528 | break; | ||
529 | default: /* found */ | ||
530 | if ((optlen = ip6optlen(optp, lim)) == 0) | ||
531 | goto optend; | ||
532 | *typep = *optp; | ||
533 | *lenp = optlen - 2; | ||
534 | *databufp = optp + 2; | ||
535 | return (optp + optlen - (u_int8_t *)extbuf); | ||
536 | } | ||
537 | } | ||
538 | |||
539 | optend: | ||
540 | *databufp = NULL; /* for safety */ | ||
541 | return (-1); | ||
542 | } | ||
543 | |||
544 | int | ||
545 | inet6_opt_find(void *extbuf, socklen_t extlen, int offset, u_int8_t type, | ||
546 | socklen_t *lenp, void **databufp) | ||
547 | { | ||
548 | u_int8_t *optp, *lim; | ||
549 | int optlen; | ||
550 | |||
551 | /* Validate extlen. XXX: is the variable really necessary?? */ | ||
552 | if (extlen == 0 || (extlen % 8)) | ||
553 | return (-1); | ||
554 | lim = (u_int8_t *)extbuf + extlen; | ||
555 | |||
556 | /* | ||
557 | * If this is the first time this function called for this options | ||
558 | * header, simply return the 1st option. | ||
559 | * Otherwise, search the option list for the next option. | ||
560 | */ | ||
561 | if (offset == 0) | ||
562 | optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1); | ||
563 | else | ||
564 | optp = (u_int8_t *)extbuf + offset; | ||
565 | |||
566 | /* Find the specified option */ | ||
567 | while (optp < lim) { | ||
568 | if ((optlen = ip6optlen(optp, lim)) == 0) | ||
569 | goto optend; | ||
570 | |||
571 | if (*optp == type) { /* found */ | ||
572 | *lenp = optlen - 2; | ||
573 | *databufp = optp + 2; | ||
574 | return (optp + optlen - (u_int8_t *)extbuf); | ||
575 | } | ||
576 | |||
577 | optp += optlen; | ||
578 | } | ||
579 | |||
580 | optend: | ||
581 | *databufp = NULL; /* for safety */ | ||
582 | return (-1); | ||
583 | } | ||
584 | |||
585 | int | ||
586 | inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen) | ||
587 | { | ||
588 | |||
589 | /* we can't assume alignment here */ | ||
590 | memcpy(val, (u_int8_t *)databuf + offset, vallen); | ||
591 | |||
592 | return (offset + vallen); | ||
593 | } | ||
diff --git a/src/lib/libc/net/link_addr.3 b/src/lib/libc/net/link_addr.3 new file mode 100644 index 0000000000..f234fe84cc --- /dev/null +++ b/src/lib/libc/net/link_addr.3 | |||
@@ -0,0 +1,127 @@ | |||
1 | .\" $OpenBSD: link_addr.3,v 1.13 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" This code is derived from software contributed to Berkeley by | ||
7 | .\" Donn Seeley at BSDI. | ||
8 | .\" | ||
9 | .\" Redistribution and use in source and binary forms, with or without | ||
10 | .\" modification, are permitted provided that the following conditions | ||
11 | .\" are met: | ||
12 | .\" 1. Redistributions of source code must retain the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer. | ||
14 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
15 | .\" notice, this list of conditions and the following disclaimer in the | ||
16 | .\" documentation and/or other materials provided with the distribution. | ||
17 | .\" 3. 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 | .Dd $Mdocdate: June 5 2013 $ | ||
34 | .Dt LINK_ADDR 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm link_addr , | ||
38 | .Nm link_ntoa | ||
39 | .Nd elementary address specification routines for link level access | ||
40 | .Sh SYNOPSIS | ||
41 | .In sys/types.h | ||
42 | .In sys/socket.h | ||
43 | .In net/if_dl.h | ||
44 | .Ft void | ||
45 | .Fn link_addr "const char *addr" "struct sockaddr_dl *sdl" | ||
46 | .Ft char * | ||
47 | .Fn link_ntoa "const struct sockaddr_dl *sdl" | ||
48 | .Sh DESCRIPTION | ||
49 | The | ||
50 | .Fn link_addr | ||
51 | function interprets character strings representing | ||
52 | link-level addresses, returning binary information suitable | ||
53 | for use in system calls. | ||
54 | .Fn link_ntoa | ||
55 | takes | ||
56 | a link-level | ||
57 | address and returns an | ||
58 | .Tn ASCII | ||
59 | string representing some of the information present, | ||
60 | including the link level address itself, and the interface name | ||
61 | or number, if present. | ||
62 | This facility is experimental and is | ||
63 | still subject to change. | ||
64 | .Pp | ||
65 | For | ||
66 | .Fn link_addr , | ||
67 | the string | ||
68 | .Fa addr | ||
69 | may contain | ||
70 | an optional network interface identifier of the form | ||
71 | .Dq name unit-number , | ||
72 | suitable for the first argument to | ||
73 | .Xr ifconfig 8 , | ||
74 | followed in all cases by a colon and | ||
75 | an interface address in the form of | ||
76 | groups of hexadecimal digits | ||
77 | separated by periods. | ||
78 | Each group represents a byte of address; | ||
79 | address bytes are filled left to right from | ||
80 | low order bytes through high order bytes. | ||
81 | .Pp | ||
82 | .\" A regular expression may make this format clearer: | ||
83 | .\" .Bd -literal -offset indent | ||
84 | .\" ([a-z]+[0-9]+:)?[0-9a-f]+(\e.[0-9a-f]+)* | ||
85 | .\" .Ed | ||
86 | .\" .Pp | ||
87 | Thus | ||
88 | .Li le0:8.0.9.13.d.30 | ||
89 | represents an Ethernet address | ||
90 | to be transmitted on the first Lance Ethernet interface. | ||
91 | .Sh RETURN VALUES | ||
92 | .Fn link_ntoa | ||
93 | always returns a NUL-terminated string. | ||
94 | .Fn link_addr | ||
95 | has no return value. | ||
96 | (See | ||
97 | .Sx BUGS . ) | ||
98 | .Sh SEE ALSO | ||
99 | .Xr ifconfig 8 | ||
100 | .Sh HISTORY | ||
101 | The | ||
102 | .Fn link_addr | ||
103 | and | ||
104 | .Fn link_ntoa | ||
105 | functions appeared in | ||
106 | .Bx 4.3 Reno . | ||
107 | .Sh BUGS | ||
108 | The returned values for | ||
109 | .Fn link_ntoa | ||
110 | reside in a static memory area. | ||
111 | .Pp | ||
112 | The function | ||
113 | .Fn link_addr | ||
114 | should diagnose improperly formed input, and there should be an unambiguous | ||
115 | way to recognize this. | ||
116 | .Pp | ||
117 | If the | ||
118 | .Fa sdl_len | ||
119 | field of the link socket address | ||
120 | .Fa sdl | ||
121 | is 0, | ||
122 | .Fn link_ntoa | ||
123 | will not insert a colon before the interface address bytes. | ||
124 | If this translated address is given to | ||
125 | .Fn link_addr | ||
126 | without inserting an initial colon, | ||
127 | the latter will not interpret it correctly. | ||
diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c new file mode 100644 index 0000000000..ac96f3acdf --- /dev/null +++ b/src/lib/libc/net/linkaddr.c | |||
@@ -0,0 +1,148 @@ | |||
1 | /* $OpenBSD: linkaddr.c,v 1.5 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/types.h> | ||
32 | #include <sys/socket.h> | ||
33 | #include <net/if_dl.h> | ||
34 | #include <string.h> | ||
35 | |||
36 | /* States*/ | ||
37 | #define NAMING 0 | ||
38 | #define GOTONE 1 | ||
39 | #define GOTTWO 2 | ||
40 | #define RESET 3 | ||
41 | /* Inputs */ | ||
42 | #define DIGIT (4*0) | ||
43 | #define END (4*1) | ||
44 | #define DELIM (4*2) | ||
45 | #define LETTER (4*3) | ||
46 | |||
47 | void | ||
48 | link_addr(const char *addr, struct sockaddr_dl *sdl) | ||
49 | { | ||
50 | char *cp = sdl->sdl_data; | ||
51 | char *cplim = sdl->sdl_len + (char *)sdl; | ||
52 | int byte = 0, state = NAMING, new; | ||
53 | |||
54 | bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); | ||
55 | sdl->sdl_family = AF_LINK; | ||
56 | do { | ||
57 | state &= ~LETTER; | ||
58 | if ((*addr >= '0') && (*addr <= '9')) { | ||
59 | new = *addr - '0'; | ||
60 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | ||
61 | new = *addr - 'a' + 10; | ||
62 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | ||
63 | new = *addr - 'A' + 10; | ||
64 | } else if (*addr == 0) { | ||
65 | state |= END; | ||
66 | } else if (state == NAMING && | ||
67 | (((*addr >= 'A') && (*addr <= 'Z')) || | ||
68 | ((*addr >= 'a') && (*addr <= 'z')))) | ||
69 | state |= LETTER; | ||
70 | else | ||
71 | state |= DELIM; | ||
72 | addr++; | ||
73 | switch (state /* | INPUT */) { | ||
74 | case NAMING | DIGIT: | ||
75 | case NAMING | LETTER: | ||
76 | *cp++ = addr[-1]; | ||
77 | continue; | ||
78 | case NAMING | DELIM: | ||
79 | state = RESET; | ||
80 | sdl->sdl_nlen = cp - sdl->sdl_data; | ||
81 | continue; | ||
82 | case GOTTWO | DIGIT: | ||
83 | *cp++ = byte; | ||
84 | /* FALLTHROUGH */ | ||
85 | case RESET | DIGIT: | ||
86 | state = GOTONE; | ||
87 | byte = new; | ||
88 | continue; | ||
89 | case GOTONE | DIGIT: | ||
90 | state = GOTTWO; | ||
91 | byte = new + (byte << 4); | ||
92 | continue; | ||
93 | default: /* | DELIM */ | ||
94 | state = RESET; | ||
95 | *cp++ = byte; | ||
96 | byte = 0; | ||
97 | continue; | ||
98 | case GOTONE | END: | ||
99 | case GOTTWO | END: | ||
100 | *cp++ = byte; | ||
101 | /* FALLTHROUGH */ | ||
102 | case RESET | END: | ||
103 | break; | ||
104 | } | ||
105 | break; | ||
106 | } while (cp < cplim); | ||
107 | sdl->sdl_alen = cp - LLADDR(sdl); | ||
108 | new = cp - (char *)sdl; | ||
109 | if (new > sizeof(*sdl)) | ||
110 | sdl->sdl_len = new; | ||
111 | return; | ||
112 | } | ||
113 | |||
114 | static char hexlist[] = "0123456789abcdef"; | ||
115 | |||
116 | char * | ||
117 | link_ntoa(const struct sockaddr_dl *sdl) | ||
118 | { | ||
119 | static char obuf[64]; | ||
120 | char *out = obuf; | ||
121 | int i; | ||
122 | u_char *in = (u_char *)LLADDR(sdl); | ||
123 | u_char *inlim = in + sdl->sdl_alen; | ||
124 | int firsttime = 1; | ||
125 | |||
126 | if (sdl->sdl_nlen) { | ||
127 | bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen); | ||
128 | out += sdl->sdl_nlen; | ||
129 | if (sdl->sdl_alen) | ||
130 | *out++ = ':'; | ||
131 | } | ||
132 | while (in < inlim) { | ||
133 | if (firsttime) | ||
134 | firsttime = 0; | ||
135 | else | ||
136 | *out++ = '.'; | ||
137 | i = *in++; | ||
138 | if (i > 0xf) { | ||
139 | out[1] = hexlist[i & 0xf]; | ||
140 | i >>= 4; | ||
141 | out[0] = hexlist[i]; | ||
142 | out += 2; | ||
143 | } else | ||
144 | *out++ = hexlist[i]; | ||
145 | } | ||
146 | *out = 0; | ||
147 | return (obuf); | ||
148 | } | ||
diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c new file mode 100644 index 0000000000..36414b7a13 --- /dev/null +++ b/src/lib/libc/net/ntohl.c | |||
@@ -0,0 +1,21 @@ | |||
1 | /* $OpenBSD: ntohl.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <sys/types.h> | ||
8 | #include <machine/endian.h> | ||
9 | |||
10 | #undef ntohl | ||
11 | |||
12 | u_int32_t | ||
13 | ntohl(u_int32_t x) | ||
14 | { | ||
15 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
16 | u_char *s = (u_char *)&x; | ||
17 | return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); | ||
18 | #else | ||
19 | return x; | ||
20 | #endif | ||
21 | } | ||
diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c new file mode 100644 index 0000000000..8f345e84ad --- /dev/null +++ b/src/lib/libc/net/ntohs.c | |||
@@ -0,0 +1,21 @@ | |||
1 | /* $OpenBSD: ntohs.c,v 1.8 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <sys/types.h> | ||
8 | #include <machine/endian.h> | ||
9 | |||
10 | #undef ntohs | ||
11 | |||
12 | u_int16_t | ||
13 | ntohs(u_int16_t x) | ||
14 | { | ||
15 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
16 | u_char *s = (u_char *) &x; | ||
17 | return (u_int16_t)(s[0] << 8 | s[1]); | ||
18 | #else | ||
19 | return x; | ||
20 | #endif | ||
21 | } | ||
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3 new file mode 100644 index 0000000000..447aa23ff1 --- /dev/null +++ b/src/lib/libc/net/rcmd.3 | |||
@@ -0,0 +1,266 @@ | |||
1 | .\" $OpenBSD: rcmd.3,v 1.29 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: January 21 2014 $ | ||
31 | .Dt RCMD 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm rcmd , | ||
35 | .Nm rcmd_af , | ||
36 | .Nm rresvport , | ||
37 | .Nm rresvport_af , | ||
38 | .Nm iruserok , | ||
39 | .Nm ruserok , | ||
40 | .Nm iruserok_sa | ||
41 | .Nd routines for returning a stream to a remote command | ||
42 | .Sh SYNOPSIS | ||
43 | .In unistd.h | ||
44 | .Ft int | ||
45 | .Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" | ||
46 | .Ft int | ||
47 | .Fn rcmd_af "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" "int af" | ||
48 | .Ft int | ||
49 | .Fn rresvport "int *port" | ||
50 | .Ft int | ||
51 | .Fn rresvport_af "int *port" "int af" | ||
52 | .Ft int | ||
53 | .Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser" | ||
54 | .Ft int | ||
55 | .Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" | ||
56 | .Ft int | ||
57 | .Fn iruserok_sa "const void *sa" "int salen" "int superuser" "const char *ruser" "const char *luser" | ||
58 | .Sh DESCRIPTION | ||
59 | The | ||
60 | .Fn rcmd | ||
61 | function is used by the superuser to execute a command on a remote | ||
62 | machine using an authentication scheme based on reserved | ||
63 | port numbers. | ||
64 | If the calling process is not setuid, the | ||
65 | .Ev RSH | ||
66 | environment variable is set, and | ||
67 | .Fa inport | ||
68 | is | ||
69 | .Dq shell/tcp , | ||
70 | .Xr rcmdsh 3 | ||
71 | is called instead with the value of | ||
72 | .Ev RSH . | ||
73 | Alternately, if the user is not the superuser, | ||
74 | .Fn rcmd | ||
75 | will invoke | ||
76 | .Xr rcmdsh 3 | ||
77 | to run the command via | ||
78 | .Xr rsh 1 . | ||
79 | While | ||
80 | .Fn rcmd | ||
81 | can handle IPv4 cases only, | ||
82 | the | ||
83 | .Fn rcmd_af | ||
84 | function can handle other cases as well. | ||
85 | .Pp | ||
86 | The | ||
87 | .Fn rresvport | ||
88 | and | ||
89 | .Fn rresvport_af | ||
90 | functions return a descriptor to a socket | ||
91 | with an address in the privileged port space. | ||
92 | The | ||
93 | .Fn iruserok | ||
94 | and | ||
95 | .Fn ruserok | ||
96 | functions are used by servers | ||
97 | to authenticate clients requesting service with | ||
98 | .Fn rcmd . | ||
99 | All four functions are present in the same file and are used | ||
100 | by the | ||
101 | .Xr rshd 8 | ||
102 | server (among others). | ||
103 | .Fn iruserok_sa | ||
104 | is an address family independent variant of | ||
105 | .Fn iruserok . | ||
106 | .Pp | ||
107 | The | ||
108 | .Fn rcmd | ||
109 | function looks up the host | ||
110 | .Fa *ahost | ||
111 | using | ||
112 | .Xr gethostbyname 3 , | ||
113 | returning \-1 if the host does not exist. | ||
114 | Otherwise | ||
115 | .Fa *ahost | ||
116 | is set to the standard name of the host | ||
117 | and a connection is established to a server | ||
118 | residing at the well-known Internet port | ||
119 | .Fa inport . | ||
120 | If the user is not the superuser, the only valid port is | ||
121 | .Dq shell/tcp | ||
122 | (usually port 514). | ||
123 | .Pp | ||
124 | If the connection succeeds, | ||
125 | a socket in the Internet domain of type | ||
126 | .Dv SOCK_STREAM | ||
127 | is returned to the caller, and given to the remote | ||
128 | command as stdin and stdout. | ||
129 | If | ||
130 | .Fa fd2p | ||
131 | is non-zero, then an auxiliary channel to a control | ||
132 | process will be set up, and a descriptor for it will be placed | ||
133 | in | ||
134 | .Fa *fd2p . | ||
135 | The control process will return diagnostic | ||
136 | output from the command (unit 2) on this channel, and will also | ||
137 | accept bytes on this channel as being | ||
138 | .Tn UNIX | ||
139 | signal numbers, to be | ||
140 | forwarded to the process group of the command. | ||
141 | If | ||
142 | .Fa fd2p | ||
143 | is | ||
144 | .Va NULL , | ||
145 | then the standard error (unit 2 of the remote command) will be made | ||
146 | the same as the standard output and no provision is made for sending | ||
147 | arbitrary signals to the remote process, although you may be able to | ||
148 | get its attention by using out-of-band data. | ||
149 | Note that if the user is not the superuser, | ||
150 | .Fa fd2p | ||
151 | must be | ||
152 | .Va NULL . | ||
153 | .Pp | ||
154 | .Fn rcmd_af | ||
155 | takes address family in the last argument. | ||
156 | If the last argument is | ||
157 | .Dv PF_UNSPEC , | ||
158 | interpretation of | ||
159 | .Fa *ahost | ||
160 | will obey the underlying address resolution like DNS. | ||
161 | .Pp | ||
162 | The protocol is described in detail in | ||
163 | .Xr rshd 8 . | ||
164 | .Pp | ||
165 | The | ||
166 | .Fn rresvport | ||
167 | and | ||
168 | .Fn rresvport_af | ||
169 | functions are used to obtain a socket with a privileged | ||
170 | address bound to it. | ||
171 | This socket is suitable for use by | ||
172 | .Fn rcmd | ||
173 | and several other functions. | ||
174 | Privileged Internet ports are those in the range 0 to | ||
175 | .Va IPPORT_RESERVED - 1 , | ||
176 | which happens to be 1023. | ||
177 | Only the superuser is allowed to bind an address of this sort to a socket. | ||
178 | .Fn rresvport | ||
179 | and | ||
180 | .Fn rresvport_af | ||
181 | need to be seeded with a port number; if that port | ||
182 | is not available these functions will find another. | ||
183 | .Pp | ||
184 | The | ||
185 | .Fn iruserok | ||
186 | and | ||
187 | .Fn ruserok | ||
188 | functions take a remote host's IP address or name, respectively, | ||
189 | two user names and a flag indicating whether the local user's | ||
190 | name is that of the superuser. | ||
191 | Then, if the user is | ||
192 | .Em not | ||
193 | the superuser, it checks the | ||
194 | .Pa /etc/hosts.equiv | ||
195 | file. | ||
196 | If that lookup is not done, or is unsuccessful, the | ||
197 | .Pa .rhosts | ||
198 | in the local user's home directory is checked to see if the request for | ||
199 | service is allowed. | ||
200 | .Pp | ||
201 | If this file does not exist, is not a regular file, is owned by anyone | ||
202 | other than the user or the superuser, or is writeable by anyone other | ||
203 | than the owner, the check automatically fails. | ||
204 | Zero is returned if the machine name is listed in the | ||
205 | .Pa hosts.equiv | ||
206 | file, or the host and remote user name are found in the | ||
207 | .Pa .rhosts | ||
208 | file; otherwise | ||
209 | .Fn iruserok | ||
210 | and | ||
211 | .Fn ruserok | ||
212 | return \-1. | ||
213 | If the local domain (as obtained from | ||
214 | .Xr gethostname 3 ) | ||
215 | is the same as the remote domain, only the machine name need be specified. | ||
216 | .Pp | ||
217 | If the IP address of the remote host is known, | ||
218 | .Fn iruserok | ||
219 | should be used in preference to | ||
220 | .Fn ruserok , | ||
221 | as it does not require trusting the DNS server for the remote host's domain. | ||
222 | .Pp | ||
223 | While | ||
224 | .Fn iruserok | ||
225 | can handle IPv4 addresses only, | ||
226 | .Fn iruserok_sa | ||
227 | and | ||
228 | .Fn ruserok | ||
229 | can handle other address families as well, like IPv6. | ||
230 | The first argument of | ||
231 | .Fn iruserok_sa | ||
232 | is typed as | ||
233 | .Li "void *" | ||
234 | to avoid dependency between | ||
235 | .In unistd.h | ||
236 | and | ||
237 | .In sys/socket.h . | ||
238 | .Sh DIAGNOSTICS | ||
239 | The | ||
240 | .Fn rcmd | ||
241 | function returns a valid socket descriptor on success. | ||
242 | It returns \-1 on error and prints a diagnostic message on the standard error. | ||
243 | .Pp | ||
244 | The | ||
245 | .Fn rresvport | ||
246 | and | ||
247 | .Fn rresvport_af | ||
248 | functions return a valid, bound socket descriptor on success. | ||
249 | It returns \-1 on error with the global value | ||
250 | .Va errno | ||
251 | set according to the reason for failure. | ||
252 | The error code | ||
253 | .Er EAGAIN | ||
254 | is overloaded to mean | ||
255 | .Dq all network ports in use . | ||
256 | .Sh SEE ALSO | ||
257 | .Xr rsh 1 , | ||
258 | .Xr intro 2 , | ||
259 | .Xr bindresvport 3 , | ||
260 | .Xr bindresvport_sa 3 , | ||
261 | .Xr rcmdsh 3 , | ||
262 | .Xr rshd 8 | ||
263 | .Sh HISTORY | ||
264 | These | ||
265 | functions appeared in | ||
266 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c new file mode 100644 index 0000000000..d566e0ca4c --- /dev/null +++ b/src/lib/libc/net/rcmd.c | |||
@@ -0,0 +1,303 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. | ||
3 | * Copyright (c) 1983, 1993, 1994 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/param.h> | ||
32 | #include <sys/socket.h> | ||
33 | #include <sys/stat.h> | ||
34 | |||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/inet.h> | ||
37 | |||
38 | #include <signal.h> | ||
39 | #include <fcntl.h> | ||
40 | #include <netdb.h> | ||
41 | #include <unistd.h> | ||
42 | #include <pwd.h> | ||
43 | #include <errno.h> | ||
44 | #include <stdio.h> | ||
45 | #include <ctype.h> | ||
46 | #include <string.h> | ||
47 | #include <syslog.h> | ||
48 | #include <stdlib.h> | ||
49 | |||
50 | int | ||
51 | rcmd(char **ahost, int rport, const char *locuser, const char *remuser, | ||
52 | const char *cmd, int *fd2p) | ||
53 | { | ||
54 | return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); | ||
55 | } | ||
56 | |||
57 | int | ||
58 | rcmd_af(char **ahost, int porta, const char *locuser, const char *remuser, | ||
59 | const char *cmd, int *fd2p, int af) | ||
60 | { | ||
61 | static char hbuf[MAXHOSTNAMELEN]; | ||
62 | char pbuf[NI_MAXSERV]; | ||
63 | struct addrinfo hints, *res, *r; | ||
64 | int error; | ||
65 | struct sockaddr_storage from; | ||
66 | fd_set *readsp = NULL; | ||
67 | sigset_t oldmask, mask; | ||
68 | pid_t pid; | ||
69 | int s, lport, timo; | ||
70 | char c, *p; | ||
71 | int refused; | ||
72 | in_port_t rport = porta; | ||
73 | |||
74 | /* call rcmdsh() with specified remote shell if appropriate. */ | ||
75 | if (!issetugid() && (p = getenv("RSH")) && *p) { | ||
76 | struct servent *sp = getservbyname("shell", "tcp"); | ||
77 | |||
78 | if (sp && sp->s_port == rport) | ||
79 | return (rcmdsh(ahost, rport, locuser, remuser, | ||
80 | cmd, p)); | ||
81 | } | ||
82 | |||
83 | /* use rsh(1) if non-root and remote port is shell. */ | ||
84 | if (geteuid()) { | ||
85 | struct servent *sp = getservbyname("shell", "tcp"); | ||
86 | |||
87 | if (sp && sp->s_port == rport) | ||
88 | return (rcmdsh(ahost, rport, locuser, remuser, | ||
89 | cmd, NULL)); | ||
90 | } | ||
91 | |||
92 | pid = getpid(); | ||
93 | snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); | ||
94 | memset(&hints, 0, sizeof(hints)); | ||
95 | hints.ai_family = af; | ||
96 | hints.ai_socktype = SOCK_STREAM; | ||
97 | hints.ai_flags = AI_CANONNAME; | ||
98 | error = getaddrinfo(*ahost, pbuf, &hints, &res); | ||
99 | if (error) { | ||
100 | #if 0 | ||
101 | warnx("%s: %s", *ahost, gai_strerror(error)); | ||
102 | #endif | ||
103 | return (-1); | ||
104 | } | ||
105 | if (res->ai_canonname) { | ||
106 | strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); | ||
107 | *ahost = hbuf; | ||
108 | } else | ||
109 | ; /*XXX*/ | ||
110 | |||
111 | r = res; | ||
112 | refused = 0; | ||
113 | sigemptyset(&mask); | ||
114 | sigaddset(&mask, SIGURG); | ||
115 | sigprocmask(SIG_BLOCK, &mask, &oldmask); | ||
116 | for (timo = 1, lport = IPPORT_RESERVED - 1;;) { | ||
117 | s = rresvport_af(&lport, r->ai_family); | ||
118 | if (s < 0) { | ||
119 | if (errno == EAGAIN) | ||
120 | (void)fprintf(stderr, | ||
121 | "rcmd: socket: All ports in use\n"); | ||
122 | else | ||
123 | (void)fprintf(stderr, "rcmd: socket: %s\n", | ||
124 | strerror(errno)); | ||
125 | if (r->ai_next) { | ||
126 | r = r->ai_next; | ||
127 | continue; | ||
128 | } else { | ||
129 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
130 | freeaddrinfo(res); | ||
131 | return (-1); | ||
132 | } | ||
133 | } | ||
134 | fcntl(s, F_SETOWN, pid); | ||
135 | if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) | ||
136 | break; | ||
137 | (void)close(s); | ||
138 | if (errno == EADDRINUSE) { | ||
139 | lport--; | ||
140 | continue; | ||
141 | } | ||
142 | if (errno == ECONNREFUSED) | ||
143 | refused++; | ||
144 | if (r->ai_next) { | ||
145 | int oerrno = errno; | ||
146 | char hbuf[NI_MAXHOST]; | ||
147 | const int niflags = NI_NUMERICHOST; | ||
148 | |||
149 | hbuf[0] = '\0'; | ||
150 | if (getnameinfo(r->ai_addr, r->ai_addrlen, | ||
151 | hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) | ||
152 | strlcpy(hbuf, "(invalid)", sizeof hbuf); | ||
153 | (void)fprintf(stderr, "connect to address %s: ", hbuf); | ||
154 | errno = oerrno; | ||
155 | perror(0); | ||
156 | r = r->ai_next; | ||
157 | hbuf[0] = '\0'; | ||
158 | if (getnameinfo(r->ai_addr, r->ai_addrlen, | ||
159 | hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) | ||
160 | strlcpy(hbuf, "(invalid)", sizeof hbuf); | ||
161 | (void)fprintf(stderr, "Trying %s...\n", hbuf); | ||
162 | continue; | ||
163 | } | ||
164 | if (refused && timo <= 16) { | ||
165 | (void)sleep(timo); | ||
166 | timo *= 2; | ||
167 | r = res; | ||
168 | refused = 0; | ||
169 | continue; | ||
170 | } | ||
171 | (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, | ||
172 | strerror(errno)); | ||
173 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
174 | freeaddrinfo(res); | ||
175 | return (-1); | ||
176 | } | ||
177 | /* given "af" can be PF_UNSPEC, we need the real af for "s" */ | ||
178 | af = r->ai_family; | ||
179 | freeaddrinfo(res); | ||
180 | #if 0 | ||
181 | /* | ||
182 | * try to rresvport() to the same port. This will make rresvport() | ||
183 | * fail it's first bind, resulting in it choosing a random port. | ||
184 | */ | ||
185 | lport--; | ||
186 | #endif | ||
187 | if (fd2p == 0) { | ||
188 | write(s, "", 1); | ||
189 | lport = 0; | ||
190 | } else { | ||
191 | char num[8]; | ||
192 | int s2 = rresvport_af(&lport, af), s3; | ||
193 | socklen_t len = sizeof(from); | ||
194 | int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask); | ||
195 | |||
196 | if (s2 < 0) | ||
197 | goto bad; | ||
198 | readsp = (fd_set *)malloc(fdssize); | ||
199 | if (readsp == NULL) { | ||
200 | close(s2); | ||
201 | goto bad; | ||
202 | } | ||
203 | listen(s2, 1); | ||
204 | (void)snprintf(num, sizeof(num), "%d", lport); | ||
205 | if (write(s, num, strlen(num)+1) != strlen(num)+1) { | ||
206 | (void)fprintf(stderr, | ||
207 | "rcmd: write (setting up stderr): %s\n", | ||
208 | strerror(errno)); | ||
209 | (void)close(s2); | ||
210 | goto bad; | ||
211 | } | ||
212 | again: | ||
213 | bzero(readsp, fdssize); | ||
214 | FD_SET(s, readsp); | ||
215 | FD_SET(s2, readsp); | ||
216 | errno = 0; | ||
217 | if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 || | ||
218 | !FD_ISSET(s2, readsp)) { | ||
219 | if (errno != 0) | ||
220 | (void)fprintf(stderr, | ||
221 | "rcmd: select (setting up stderr): %s\n", | ||
222 | strerror(errno)); | ||
223 | else | ||
224 | (void)fprintf(stderr, | ||
225 | "select: protocol failure in circuit setup\n"); | ||
226 | (void)close(s2); | ||
227 | goto bad; | ||
228 | } | ||
229 | s3 = accept(s2, (struct sockaddr *)&from, &len); | ||
230 | if (s3 < 0) { | ||
231 | (void)fprintf(stderr, | ||
232 | "rcmd: accept: %s\n", strerror(errno)); | ||
233 | lport = 0; | ||
234 | close(s2); | ||
235 | goto bad; | ||
236 | } | ||
237 | |||
238 | /* | ||
239 | * XXX careful for ftp bounce attacks. If discovered, shut them | ||
240 | * down and check for the real auxiliary channel to connect. | ||
241 | */ | ||
242 | switch (from.ss_family) { | ||
243 | case AF_INET: | ||
244 | case AF_INET6: | ||
245 | if (getnameinfo((struct sockaddr *)&from, len, | ||
246 | NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 && | ||
247 | atoi(num) != 20) { | ||
248 | break; | ||
249 | } | ||
250 | close(s3); | ||
251 | goto again; | ||
252 | default: | ||
253 | break; | ||
254 | } | ||
255 | (void)close(s2); | ||
256 | |||
257 | *fd2p = s3; | ||
258 | switch (from.ss_family) { | ||
259 | case AF_INET: | ||
260 | case AF_INET6: | ||
261 | if (getnameinfo((struct sockaddr *)&from, len, | ||
262 | NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || | ||
263 | (atoi(num) >= IPPORT_RESERVED || | ||
264 | atoi(num) < IPPORT_RESERVED / 2)) { | ||
265 | (void)fprintf(stderr, | ||
266 | "socket: protocol failure in circuit setup.\n"); | ||
267 | goto bad2; | ||
268 | } | ||
269 | break; | ||
270 | default: | ||
271 | break; | ||
272 | } | ||
273 | } | ||
274 | (void)write(s, locuser, strlen(locuser)+1); | ||
275 | (void)write(s, remuser, strlen(remuser)+1); | ||
276 | (void)write(s, cmd, strlen(cmd)+1); | ||
277 | if (read(s, &c, 1) != 1) { | ||
278 | (void)fprintf(stderr, | ||
279 | "rcmd: %s: %s\n", *ahost, strerror(errno)); | ||
280 | goto bad2; | ||
281 | } | ||
282 | if (c != 0) { | ||
283 | while (read(s, &c, 1) == 1) { | ||
284 | (void)write(STDERR_FILENO, &c, 1); | ||
285 | if (c == '\n') | ||
286 | break; | ||
287 | } | ||
288 | goto bad2; | ||
289 | } | ||
290 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
291 | free(readsp); | ||
292 | return (s); | ||
293 | bad2: | ||
294 | if (lport) | ||
295 | (void)close(*fd2p); | ||
296 | bad: | ||
297 | if (readsp) | ||
298 | free(readsp); | ||
299 | (void)close(s); | ||
300 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
301 | return (-1); | ||
302 | } | ||
303 | |||
diff --git a/src/lib/libc/net/rcmdsh.3 b/src/lib/libc/net/rcmdsh.3 new file mode 100644 index 0000000000..c84a2e4d93 --- /dev/null +++ b/src/lib/libc/net/rcmdsh.3 | |||
@@ -0,0 +1,96 @@ | |||
1 | .\" $OpenBSD: rcmdsh.3,v 1.14 2013/06/05 03:39:23 tedu Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: June 5 2013 $ | ||
31 | .Dt RCMDSH 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm rcmdsh | ||
35 | .Nd return a stream to a remote command without superuser | ||
36 | .Sh SYNOPSIS | ||
37 | .In unistd.h | ||
38 | .Ft int | ||
39 | .Fn rcmdsh "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "char *rshprog" | ||
40 | .Sh DESCRIPTION | ||
41 | The | ||
42 | .Fn rcmdsh | ||
43 | function is used by normal users to execute a command on a remote machine | ||
44 | using an authentication scheme based on reserved port numbers using | ||
45 | .Xr rsh 1 | ||
46 | or the value of | ||
47 | .Fa rshprog | ||
48 | (if non-null). | ||
49 | .Fa rshprog | ||
50 | may be a fully-qualified path, a non-qualified command, or a command containing | ||
51 | space-separated command line arguments. | ||
52 | .Pp | ||
53 | The | ||
54 | .Fn rcmdsh | ||
55 | function looks up the host | ||
56 | .Fa *ahost | ||
57 | using | ||
58 | .Xr gethostbyname 3 , | ||
59 | returning \-1 if the host does not exist. | ||
60 | Otherwise | ||
61 | .Fa *ahost | ||
62 | is set to the standard name of the host and a connection is established to | ||
63 | a server residing at the well-known Internet port | ||
64 | .Li shell/tcp | ||
65 | (or whatever port is used by | ||
66 | .Fa rshprog ) . | ||
67 | The parameter | ||
68 | .Fa inport | ||
69 | is ignored; it is only included to provide an interface similar to | ||
70 | .Xr rcmd 3 . | ||
71 | .Pp | ||
72 | If the connection succeeds, a socket in the | ||
73 | .Ux Ns -domain | ||
74 | of type | ||
75 | .Dv SOCK_STREAM | ||
76 | is returned to the caller, and given to the remote | ||
77 | command as stdin and stdout, and stderr. | ||
78 | .Sh DIAGNOSTICS | ||
79 | The | ||
80 | .Fn rcmdsh | ||
81 | function returns a valid socket descriptor on success. | ||
82 | It returns \-1 on error and prints a diagnostic message on the standard error. | ||
83 | .Sh SEE ALSO | ||
84 | .Xr rsh 1 , | ||
85 | .Xr socketpair 2 , | ||
86 | .Xr rcmd 3 , | ||
87 | .Xr rshd 8 | ||
88 | .Sh HISTORY | ||
89 | The | ||
90 | .Fn rcmdsh | ||
91 | function first appeared in | ||
92 | .Ox 2.0 . | ||
93 | .Sh BUGS | ||
94 | If | ||
95 | .Xr rsh 1 | ||
96 | encounters an error, a file descriptor is still returned instead of \-1. | ||
diff --git a/src/lib/libc/net/rcmdsh.c b/src/lib/libc/net/rcmdsh.c new file mode 100644 index 0000000000..a472162711 --- /dev/null +++ b/src/lib/libc/net/rcmdsh.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /* $OpenBSD: rcmdsh.c,v 1.12 2007/09/02 15:19:17 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2001, MagniComp | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in | ||
14 | * the documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the MagniComp nor the names of its contributors may | ||
16 | * be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR | ||
23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
28 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | /* | ||
32 | * This is an rcmd() replacement originally by | ||
33 | * Chris Siebenmann <cks@utcc.utoronto.ca>. | ||
34 | */ | ||
35 | |||
36 | #include <sys/types.h> | ||
37 | #include <sys/socket.h> | ||
38 | #include <sys/wait.h> | ||
39 | #include <signal.h> | ||
40 | #include <errno.h> | ||
41 | #include <netdb.h> | ||
42 | #include <stdio.h> | ||
43 | #include <stdlib.h> | ||
44 | #include <string.h> | ||
45 | #include <pwd.h> | ||
46 | #include <paths.h> | ||
47 | #include <unistd.h> | ||
48 | |||
49 | /* | ||
50 | * This is a replacement rcmd() function that uses the rsh(1) | ||
51 | * program in place of a direct rcmd(3) function call so as to | ||
52 | * avoid having to be root. Note that rport is ignored. | ||
53 | */ | ||
54 | /* ARGSUSED */ | ||
55 | int | ||
56 | rcmdsh(char **ahost, int rport, const char *locuser, const char *remuser, | ||
57 | const char *cmd, char *rshprog) | ||
58 | { | ||
59 | struct hostent *hp; | ||
60 | int sp[2]; | ||
61 | pid_t cpid; | ||
62 | char *p; | ||
63 | struct passwd *pw; | ||
64 | |||
65 | /* What rsh/shell to use. */ | ||
66 | if (rshprog == NULL) | ||
67 | rshprog = _PATH_RSH; | ||
68 | |||
69 | /* locuser must exist on this host. */ | ||
70 | if ((pw = getpwnam(locuser)) == NULL) { | ||
71 | (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser); | ||
72 | return(-1); | ||
73 | } | ||
74 | |||
75 | /* Validate remote hostname. */ | ||
76 | if (strcmp(*ahost, "localhost") != 0) { | ||
77 | if (((hp = gethostbyname2(*ahost, AF_INET)) == NULL) && | ||
78 | ((hp = gethostbyname2(*ahost, AF_INET6)) == NULL)) { | ||
79 | herror(*ahost); | ||
80 | return(-1); | ||
81 | } | ||
82 | *ahost = hp->h_name; | ||
83 | } | ||
84 | |||
85 | /* Get a socketpair we'll use for stdin and stdout. */ | ||
86 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) { | ||
87 | perror("rcmdsh: socketpair"); | ||
88 | return(-1); | ||
89 | } | ||
90 | |||
91 | cpid = fork(); | ||
92 | if (cpid < 0) { | ||
93 | perror("rcmdsh: fork failed"); | ||
94 | return(-1); | ||
95 | } else if (cpid == 0) { | ||
96 | /* | ||
97 | * Child. We use sp[1] to be stdin/stdout, and close sp[0]. | ||
98 | */ | ||
99 | (void) close(sp[0]); | ||
100 | if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { | ||
101 | perror("rcmdsh: dup2 failed"); | ||
102 | _exit(255); | ||
103 | } | ||
104 | /* Fork again to lose parent. */ | ||
105 | cpid = fork(); | ||
106 | if (cpid < 0) { | ||
107 | perror("rcmdsh: fork to lose parent failed"); | ||
108 | _exit(255); | ||
109 | } | ||
110 | if (cpid > 0) | ||
111 | _exit(0); | ||
112 | |||
113 | /* In grandchild here. Become local user for rshprog. */ | ||
114 | if (setuid(pw->pw_uid)) { | ||
115 | (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n", | ||
116 | pw->pw_uid, strerror(errno)); | ||
117 | _exit(255); | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * If remote host is "localhost" and local and remote user | ||
122 | * are the same, avoid running remote shell for efficiency. | ||
123 | */ | ||
124 | if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) { | ||
125 | char *argv[4]; | ||
126 | if (pw->pw_shell[0] == '\0') | ||
127 | rshprog = _PATH_BSHELL; | ||
128 | else | ||
129 | rshprog = pw->pw_shell; | ||
130 | p = strrchr(rshprog, '/'); | ||
131 | argv[0] = p ? p + 1 : rshprog; | ||
132 | argv[1] = "-c"; | ||
133 | argv[2] = (char *)cmd; | ||
134 | argv[3] = NULL; | ||
135 | execvp(rshprog, argv); | ||
136 | } else if ((p = strchr(rshprog, ' ')) == NULL) { | ||
137 | /* simple case */ | ||
138 | char *argv[6]; | ||
139 | p = strrchr(rshprog, '/'); | ||
140 | argv[0] = p ? p + 1 : rshprog; | ||
141 | argv[1] = "-l"; | ||
142 | argv[2] = (char *)remuser; | ||
143 | argv[3] = *ahost; | ||
144 | argv[4] = (char *)cmd; | ||
145 | argv[5] = NULL; | ||
146 | execvp(rshprog, argv); | ||
147 | } else { | ||
148 | /* must pull args out of rshprog and dyn alloc argv */ | ||
149 | char **argv, **ap; | ||
150 | int n; | ||
151 | for (n = 7; (p = strchr(++p, ' ')) != NULL; n++) | ||
152 | continue; | ||
153 | rshprog = strdup(rshprog); | ||
154 | ap = argv = calloc(sizeof(char *), n); | ||
155 | if (rshprog == NULL || argv == NULL) { | ||
156 | perror("rcmdsh"); | ||
157 | _exit(255); | ||
158 | } | ||
159 | while ((p = strsep(&rshprog, " ")) != NULL) { | ||
160 | if (*p == '\0') | ||
161 | continue; | ||
162 | *ap++ = p; | ||
163 | } | ||
164 | if (ap != argv) /* all spaces?!? */ | ||
165 | rshprog = argv[0]; | ||
166 | if ((p = strrchr(argv[0], '/')) != NULL) | ||
167 | argv[0] = p + 1; | ||
168 | *ap++ = "-l"; | ||
169 | *ap++ = (char *)remuser; | ||
170 | *ap++ = *ahost; | ||
171 | *ap++ = (char *)cmd; | ||
172 | *ap++ = NULL; | ||
173 | execvp(rshprog, argv); | ||
174 | } | ||
175 | (void) fprintf(stderr, "rcmdsh: execvp %s failed: %s\n", | ||
176 | rshprog, strerror(errno)); | ||
177 | _exit(255); | ||
178 | } else { | ||
179 | /* Parent. close sp[1], return sp[0]. */ | ||
180 | (void) close(sp[1]); | ||
181 | /* Reap child. */ | ||
182 | (void) wait(NULL); | ||
183 | return(sp[0]); | ||
184 | } | ||
185 | /* NOTREACHED */ | ||
186 | } | ||
diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c new file mode 100644 index 0000000000..6241cc6b12 --- /dev/null +++ b/src/lib/libc/net/recv.c | |||
@@ -0,0 +1,40 @@ | |||
1 | /* $OpenBSD: recv.c,v 1.5 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/types.h> | ||
32 | #include <sys/socket.h> | ||
33 | |||
34 | #include <stddef.h> | ||
35 | |||
36 | ssize_t | ||
37 | recv(int s, void *buf, size_t len, int flags) | ||
38 | { | ||
39 | return (recvfrom(s, buf, len, flags, NULL, 0)); | ||
40 | } | ||
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c new file mode 100644 index 0000000000..69a6ce0abb --- /dev/null +++ b/src/lib/libc/net/res_comp.c | |||
@@ -0,0 +1,475 @@ | |||
1 | /* $OpenBSD: res_comp.c,v 1.14 2008/04/16 22:35:23 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. 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 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #include <sys/types.h> | ||
55 | #include <sys/param.h> | ||
56 | #include <netinet/in.h> | ||
57 | #include <arpa/nameser.h> | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include <resolv.h> | ||
61 | #include <ctype.h> | ||
62 | |||
63 | #include <unistd.h> | ||
64 | #include <string.h> | ||
65 | |||
66 | static int dn_find(u_char *, u_char *, u_char **, u_char **); | ||
67 | |||
68 | /* | ||
69 | * Expand compressed domain name 'comp_dn' to full domain name. | ||
70 | * 'msg' is a pointer to the begining of the message, | ||
71 | * 'eomorig' points to the first location after the message, | ||
72 | * 'exp_dn' is a pointer to a buffer of size 'length' for the result. | ||
73 | * Return size of compressed name or -1 if there was an error. | ||
74 | */ | ||
75 | int | ||
76 | dn_expand(const u_char *msg, const u_char *eomorig, const u_char *comp_dn, | ||
77 | char *exp_dn, int length) | ||
78 | { | ||
79 | const u_char *cp; | ||
80 | char *dn; | ||
81 | int n, c; | ||
82 | char *eom; | ||
83 | int len = -1, checked = 0; | ||
84 | |||
85 | dn = exp_dn; | ||
86 | cp = comp_dn; | ||
87 | if (length > MAXHOSTNAMELEN-1) | ||
88 | length = MAXHOSTNAMELEN-1; | ||
89 | eom = exp_dn + length; | ||
90 | /* | ||
91 | * fetch next label in domain name | ||
92 | */ | ||
93 | while ((n = *cp++)) { | ||
94 | /* | ||
95 | * Check for indirection | ||
96 | */ | ||
97 | switch (n & INDIR_MASK) { | ||
98 | case 0: | ||
99 | if (dn != exp_dn) { | ||
100 | if (dn >= eom) | ||
101 | return (-1); | ||
102 | *dn++ = '.'; | ||
103 | } | ||
104 | if (dn+n >= eom) | ||
105 | return (-1); | ||
106 | checked += n + 1; | ||
107 | while (--n >= 0) { | ||
108 | if (((c = *cp++) == '.') || (c == '\\')) { | ||
109 | if (dn + n + 2 >= eom) | ||
110 | return (-1); | ||
111 | *dn++ = '\\'; | ||
112 | } | ||
113 | *dn++ = c; | ||
114 | if (cp >= eomorig) /* out of range */ | ||
115 | return (-1); | ||
116 | } | ||
117 | break; | ||
118 | |||
119 | case INDIR_MASK: | ||
120 | if (len < 0) | ||
121 | len = cp - comp_dn + 1; | ||
122 | cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); | ||
123 | if (cp < msg || cp >= eomorig) /* out of range */ | ||
124 | return (-1); | ||
125 | checked += 2; | ||
126 | /* | ||
127 | * Check for loops in the compressed name; | ||
128 | * if we've looked at the whole message, | ||
129 | * there must be a loop. | ||
130 | */ | ||
131 | if (checked >= eomorig - msg) | ||
132 | return (-1); | ||
133 | break; | ||
134 | |||
135 | default: | ||
136 | return (-1); /* flag error */ | ||
137 | } | ||
138 | } | ||
139 | *dn = '\0'; | ||
140 | if (len < 0) | ||
141 | len = cp - comp_dn; | ||
142 | return (len); | ||
143 | } | ||
144 | |||
145 | /* | ||
146 | * Compress domain name 'exp_dn' into 'comp_dn'. | ||
147 | * Return the size of the compressed name or -1. | ||
148 | * 'length' is the size of the array pointed to by 'comp_dn'. | ||
149 | * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] | ||
150 | * is a pointer to the beginning of the message. The list ends with NULL. | ||
151 | * 'lastdnptr' is a pointer to the end of the arrary pointed to | ||
152 | * by 'dnptrs'. Side effect is to update the list of pointers for | ||
153 | * labels inserted into the message as we compress the name. | ||
154 | * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' | ||
155 | * is NULL, we don't update the list. | ||
156 | */ | ||
157 | int | ||
158 | dn_comp(const char *exp_dn, u_char *comp_dn, int length, u_char **dnptrs, | ||
159 | u_char **lastdnptr) | ||
160 | { | ||
161 | u_char *cp, *dn; | ||
162 | int c, l; | ||
163 | u_char **cpp, **lpp, *sp, *eob; | ||
164 | u_char *msg; | ||
165 | |||
166 | dn = (u_char *)exp_dn; | ||
167 | cp = comp_dn; | ||
168 | eob = cp + length; | ||
169 | lpp = cpp = NULL; | ||
170 | if (dnptrs != NULL) { | ||
171 | if ((msg = *dnptrs++) != NULL) { | ||
172 | for (cpp = dnptrs; *cpp != NULL; cpp++) | ||
173 | ; | ||
174 | lpp = cpp; /* end of list to search */ | ||
175 | } | ||
176 | } else | ||
177 | msg = NULL; | ||
178 | for (c = *dn++; c != '\0'; ) { | ||
179 | /* look to see if we can use pointers */ | ||
180 | if (msg != NULL) { | ||
181 | if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { | ||
182 | if (cp+1 >= eob) | ||
183 | return (-1); | ||
184 | *cp++ = (l >> 8) | INDIR_MASK; | ||
185 | *cp++ = l % 256; | ||
186 | return (cp - comp_dn); | ||
187 | } | ||
188 | /* not found, save it */ | ||
189 | if (lastdnptr != NULL && cpp < lastdnptr-1) { | ||
190 | *cpp++ = cp; | ||
191 | *cpp = NULL; | ||
192 | } | ||
193 | } | ||
194 | sp = cp++; /* save ptr to length byte */ | ||
195 | do { | ||
196 | if (c == '.') { | ||
197 | c = *dn++; | ||
198 | break; | ||
199 | } | ||
200 | if (c == '\\') { | ||
201 | if ((c = *dn++) == '\0') | ||
202 | break; | ||
203 | } | ||
204 | if (cp >= eob) { | ||
205 | if (msg != NULL) | ||
206 | *lpp = NULL; | ||
207 | return (-1); | ||
208 | } | ||
209 | *cp++ = c; | ||
210 | } while ((c = *dn++) != '\0'); | ||
211 | /* catch trailing '.'s but not '..' */ | ||
212 | if ((l = cp - sp - 1) == 0 && c == '\0') { | ||
213 | cp--; | ||
214 | break; | ||
215 | } | ||
216 | if (l <= 0 || l > MAXLABEL) { | ||
217 | if (msg != NULL) | ||
218 | *lpp = NULL; | ||
219 | return (-1); | ||
220 | } | ||
221 | *sp = l; | ||
222 | } | ||
223 | if (cp >= eob) { | ||
224 | if (msg != NULL) | ||
225 | *lpp = NULL; | ||
226 | return (-1); | ||
227 | } | ||
228 | *cp++ = '\0'; | ||
229 | return (cp - comp_dn); | ||
230 | } | ||
231 | |||
232 | /* | ||
233 | * Skip over a compressed domain name. Return the size or -1. | ||
234 | */ | ||
235 | int | ||
236 | __dn_skipname(const u_char *comp_dn, const u_char *eom) | ||
237 | { | ||
238 | const u_char *cp; | ||
239 | int n; | ||
240 | |||
241 | cp = comp_dn; | ||
242 | while (cp < eom && (n = *cp++)) { | ||
243 | /* | ||
244 | * check for indirection | ||
245 | */ | ||
246 | switch (n & INDIR_MASK) { | ||
247 | case 0: /* normal case, n == len */ | ||
248 | cp += n; | ||
249 | continue; | ||
250 | case INDIR_MASK: /* indirection */ | ||
251 | cp++; | ||
252 | break; | ||
253 | default: /* illegal type */ | ||
254 | return (-1); | ||
255 | } | ||
256 | break; | ||
257 | } | ||
258 | if (cp > eom) | ||
259 | return (-1); | ||
260 | return (cp - comp_dn); | ||
261 | } | ||
262 | |||
263 | static int | ||
264 | mklower(int ch) | ||
265 | { | ||
266 | if (isascii(ch) && isupper(ch)) | ||
267 | return (tolower(ch)); | ||
268 | return (ch); | ||
269 | } | ||
270 | |||
271 | /* | ||
272 | * Search for expanded name from a list of previously compressed names. | ||
273 | * Return the offset from msg if found or -1. | ||
274 | * dnptrs is the pointer to the first name on the list, | ||
275 | * not the pointer to the start of the message. | ||
276 | */ | ||
277 | static int | ||
278 | dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr) | ||
279 | { | ||
280 | u_char *dn, *cp, **cpp; | ||
281 | int n; | ||
282 | u_char *sp; | ||
283 | |||
284 | for (cpp = dnptrs; cpp < lastdnptr; cpp++) { | ||
285 | dn = exp_dn; | ||
286 | sp = cp = *cpp; | ||
287 | while ((n = *cp++)) { | ||
288 | /* | ||
289 | * check for indirection | ||
290 | */ | ||
291 | switch (n & INDIR_MASK) { | ||
292 | case 0: /* normal case, n == len */ | ||
293 | while (--n >= 0) { | ||
294 | if (*dn == '.') | ||
295 | goto next; | ||
296 | if (*dn == '\\') | ||
297 | dn++; | ||
298 | if (mklower(*dn++) != mklower(*cp++)) | ||
299 | goto next; | ||
300 | } | ||
301 | if ((n = *dn++) == '\0' && *cp == '\0') | ||
302 | return (sp - msg); | ||
303 | if (n == '.') | ||
304 | continue; | ||
305 | goto next; | ||
306 | |||
307 | case INDIR_MASK: /* indirection */ | ||
308 | cp = msg + (((n & 0x3f) << 8) | *cp); | ||
309 | break; | ||
310 | |||
311 | default: /* illegal type */ | ||
312 | return (-1); | ||
313 | } | ||
314 | } | ||
315 | if (*dn == '\0') | ||
316 | return (sp - msg); | ||
317 | next: ; | ||
318 | } | ||
319 | return (-1); | ||
320 | } | ||
321 | |||
322 | /* | ||
323 | * Verify that a domain name uses an acceptable character set. | ||
324 | */ | ||
325 | |||
326 | /* | ||
327 | * Note the conspicuous absence of ctype macros in these definitions. On | ||
328 | * non-ASCII hosts, we can't depend on string literals or ctype macros to | ||
329 | * tell us anything about network-format data. The rest of the BIND system | ||
330 | * is not careful about this, but for some reason, we're doing it right here. | ||
331 | */ | ||
332 | #define PERIOD 0x2e | ||
333 | #define hyphenchar(c) ((c) == 0x2d) | ||
334 | #define bslashchar(c) ((c) == 0x5c) | ||
335 | #define underscorechar(c) ((c) == 0x5f) | ||
336 | #define periodchar(c) ((c) == PERIOD) | ||
337 | #define asterchar(c) ((c) == 0x2a) | ||
338 | #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ | ||
339 | || ((c) >= 0x61 && (c) <= 0x7a)) | ||
340 | #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) | ||
341 | |||
342 | #define borderchar(c) (alphachar(c) || digitchar(c)) | ||
343 | #define middlechar(c) (borderchar(c) || hyphenchar(c) || underscorechar(c)) | ||
344 | #define domainchar(c) ((c) > 0x20 && (c) < 0x7f) | ||
345 | |||
346 | int | ||
347 | res_hnok(const char *dn) | ||
348 | { | ||
349 | int pch = PERIOD, ch = *dn++; | ||
350 | |||
351 | while (ch != '\0') { | ||
352 | int nch = *dn++; | ||
353 | |||
354 | if (periodchar(ch)) { | ||
355 | ; | ||
356 | } else if (periodchar(pch)) { | ||
357 | if (!borderchar(ch)) | ||
358 | return (0); | ||
359 | } else if (periodchar(nch) || nch == '\0') { | ||
360 | if (!borderchar(ch)) | ||
361 | return (0); | ||
362 | } else { | ||
363 | if (!middlechar(ch)) | ||
364 | return (0); | ||
365 | } | ||
366 | pch = ch, ch = nch; | ||
367 | } | ||
368 | return (1); | ||
369 | } | ||
370 | |||
371 | /* | ||
372 | * hostname-like (A, MX, WKS) owners can have "*" as their first label | ||
373 | * but must otherwise be as a host name. | ||
374 | */ | ||
375 | int | ||
376 | res_ownok(const char *dn) | ||
377 | { | ||
378 | if (asterchar(dn[0])) { | ||
379 | if (periodchar(dn[1])) | ||
380 | return (res_hnok(dn+2)); | ||
381 | if (dn[1] == '\0') | ||
382 | return (1); | ||
383 | } | ||
384 | return (res_hnok(dn)); | ||
385 | } | ||
386 | |||
387 | /* | ||
388 | * SOA RNAMEs and RP RNAMEs can have any printable character in their first | ||
389 | * label, but the rest of the name has to look like a host name. | ||
390 | */ | ||
391 | int | ||
392 | res_mailok(const char *dn) | ||
393 | { | ||
394 | int ch, escaped = 0; | ||
395 | |||
396 | /* "." is a valid missing representation */ | ||
397 | if (*dn == '\0') | ||
398 | return(1); | ||
399 | |||
400 | /* otherwise <label>.<hostname> */ | ||
401 | while ((ch = *dn++) != '\0') { | ||
402 | if (!domainchar(ch)) | ||
403 | return (0); | ||
404 | if (!escaped && periodchar(ch)) | ||
405 | break; | ||
406 | if (escaped) | ||
407 | escaped = 0; | ||
408 | else if (bslashchar(ch)) | ||
409 | escaped = 1; | ||
410 | } | ||
411 | if (periodchar(ch)) | ||
412 | return (res_hnok(dn)); | ||
413 | return(0); | ||
414 | } | ||
415 | |||
416 | /* | ||
417 | * This function is quite liberal, since RFC 1034's character sets are only | ||
418 | * recommendations. | ||
419 | */ | ||
420 | int | ||
421 | res_dnok(const char *dn) | ||
422 | { | ||
423 | int ch; | ||
424 | |||
425 | while ((ch = *dn++) != '\0') | ||
426 | if (!domainchar(ch)) | ||
427 | return (0); | ||
428 | return (1); | ||
429 | } | ||
430 | |||
431 | /* | ||
432 | * Routines to insert/extract short/long's. | ||
433 | */ | ||
434 | |||
435 | u_int16_t | ||
436 | _getshort(const u_char *msgp) | ||
437 | { | ||
438 | u_int16_t u; | ||
439 | |||
440 | GETSHORT(u, msgp); | ||
441 | return (u); | ||
442 | } | ||
443 | |||
444 | #ifdef NeXT | ||
445 | /* | ||
446 | * nExt machines have some funky library conventions, which we must maintain. | ||
447 | */ | ||
448 | u_int16_t | ||
449 | res_getshort(msgp) | ||
450 | const u_char *msgp; | ||
451 | { | ||
452 | return (_getshort(msgp)); | ||
453 | } | ||
454 | #endif | ||
455 | |||
456 | u_int32_t | ||
457 | _getlong(const u_char *msgp) | ||
458 | { | ||
459 | u_int32_t u; | ||
460 | |||
461 | GETLONG(u, msgp); | ||
462 | return (u); | ||
463 | } | ||
464 | |||
465 | void | ||
466 | __putshort(u_int16_t s, u_char *msgp) | ||
467 | { | ||
468 | PUTSHORT(s, msgp); | ||
469 | } | ||
470 | |||
471 | void | ||
472 | __putlong(u_int32_t l, u_char *msgp) | ||
473 | { | ||
474 | PUTLONG(l, msgp); | ||
475 | } | ||
diff --git a/src/lib/libc/net/res_data.c b/src/lib/libc/net/res_data.c new file mode 100644 index 0000000000..a5f6b03a7f --- /dev/null +++ b/src/lib/libc/net/res_data.c | |||
@@ -0,0 +1,105 @@ | |||
1 | /* $OpenBSD: res_data.c,v 1.3 2005/08/06 20:30:03 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1995 | ||
5 | * - | ||
6 | * Copyright (c) 1995 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. 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 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #include <sys/types.h> | ||
55 | #include <sys/param.h> | ||
56 | #include <sys/socket.h> | ||
57 | #include <sys/time.h> | ||
58 | #include <netinet/in.h> | ||
59 | #include <arpa/inet.h> | ||
60 | #include <arpa/nameser.h> | ||
61 | |||
62 | #include <stdio.h> | ||
63 | #include <ctype.h> | ||
64 | #include <resolv.h> | ||
65 | #include <unistd.h> | ||
66 | #include <stdlib.h> | ||
67 | #include <string.h> | ||
68 | |||
69 | const char *_res_opcodes[] = { | ||
70 | "QUERY", | ||
71 | "IQUERY", | ||
72 | "CQUERYM", | ||
73 | "CQUERYU", /* experimental */ | ||
74 | "NOTIFY", /* experimental */ | ||
75 | "5", | ||
76 | "6", | ||
77 | "7", | ||
78 | "8", | ||
79 | "UPDATEA", | ||
80 | "UPDATED", | ||
81 | "UPDATEDA", | ||
82 | "UPDATEM", | ||
83 | "UPDATEMA", | ||
84 | "ZONEINIT", | ||
85 | "ZONEREF", | ||
86 | }; | ||
87 | |||
88 | const char *_res_resultcodes[] = { | ||
89 | "NOERROR", | ||
90 | "FORMERR", | ||
91 | "SERVFAIL", | ||
92 | "NXDOMAIN", | ||
93 | "NOTIMP", | ||
94 | "REFUSED", | ||
95 | "6", | ||
96 | "7", | ||
97 | "8", | ||
98 | "9", | ||
99 | "10", | ||
100 | "11", | ||
101 | "12", | ||
102 | "13", | ||
103 | "14", | ||
104 | "NOCHANGE", | ||
105 | }; | ||
diff --git a/src/lib/libc/net/res_debug_syms.c b/src/lib/libc/net/res_debug_syms.c new file mode 100644 index 0000000000..7e7d22c914 --- /dev/null +++ b/src/lib/libc/net/res_debug_syms.c | |||
@@ -0,0 +1,189 @@ | |||
1 | /* $OpenBSD: res_debug_syms.c,v 1.1 2005/08/06 20:30:04 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1990, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1990, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. 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 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * Portions Copyright (c) 1995 by International Business Machines, Inc. | ||
52 | * | ||
53 | * International Business Machines, Inc. (hereinafter called IBM) grants | ||
54 | * permission under its copyrights to use, copy, modify, and distribute this | ||
55 | * Software with or without fee, provided that the above copyright notice and | ||
56 | * all paragraphs of this notice appear in all copies, and that the name of IBM | ||
57 | * not be used in connection with the marketing of any product incorporating | ||
58 | * the Software or modifications thereof, without specific, written prior | ||
59 | * permission. | ||
60 | * | ||
61 | * To the extent it has a right to do so, IBM grants an immunity from suit | ||
62 | * under its patents, if any, for the use, sale or manufacture of products to | ||
63 | * the extent that such products are used for performing Domain Name System | ||
64 | * dynamic updates in TCP/IP networks by means of the Software. No immunity is | ||
65 | * granted for any product per se or for any other function of any product. | ||
66 | * | ||
67 | * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, | ||
68 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
69 | * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, | ||
70 | * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING | ||
71 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN | ||
72 | * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. | ||
73 | * --Copyright-- | ||
74 | */ | ||
75 | |||
76 | |||
77 | #include <sys/types.h> | ||
78 | #include <netinet/in.h> | ||
79 | #include <arpa/nameser.h> | ||
80 | |||
81 | #include <resolv.h> | ||
82 | #include <stdio.h> | ||
83 | |||
84 | /* | ||
85 | * Names of RR classes and qclasses. Classes and qclasses are the same, except | ||
86 | * that C_ANY is a qclass but not a class. (You can ask for records of class | ||
87 | * C_ANY, but you can't have any records of that class in the database.) | ||
88 | */ | ||
89 | const struct res_sym __p_class_syms[] = { | ||
90 | {C_IN, "IN"}, | ||
91 | {C_CHAOS, "CHAOS"}, | ||
92 | {C_HS, "HS"}, | ||
93 | {C_HS, "HESIOD"}, | ||
94 | {C_ANY, "ANY"}, | ||
95 | {C_IN, (char *)0} | ||
96 | }; | ||
97 | |||
98 | /* | ||
99 | * Names of RR types and qtypes. Types and qtypes are the same, except | ||
100 | * that T_ANY is a qtype but not a type. (You can ask for records of type | ||
101 | * T_ANY, but you can't have any records of that type in the database.) | ||
102 | */ | ||
103 | const struct res_sym __p_type_syms[] = { | ||
104 | {T_A, "A", "address"}, | ||
105 | {T_NS, "NS", "name server"}, | ||
106 | {T_MD, "MD", "mail destination (deprecated)"}, | ||
107 | {T_MF, "MF", "mail forwarder (deprecated)"}, | ||
108 | {T_CNAME, "CNAME", "canonical name"}, | ||
109 | {T_SOA, "SOA", "start of authority"}, | ||
110 | {T_MB, "MB", "mailbox"}, | ||
111 | {T_MG, "MG", "mail group member"}, | ||
112 | {T_MR, "MR", "mail rename"}, | ||
113 | {T_NULL, "NULL", "null"}, | ||
114 | {T_WKS, "WKS", "well-known service (deprecated)"}, | ||
115 | {T_PTR, "PTR", "domain name pointer"}, | ||
116 | {T_HINFO, "HINFO", "host information"}, | ||
117 | {T_MINFO, "MINFO", "mailbox information"}, | ||
118 | {T_MX, "MX", "mail exchanger"}, | ||
119 | {T_TXT, "TXT", "text"}, | ||
120 | {T_RP, "RP", "responsible person"}, | ||
121 | {T_AFSDB, "AFSDB", "DCE or AFS server"}, | ||
122 | {T_X25, "X25", "X25 address"}, | ||
123 | {T_ISDN, "ISDN", "ISDN address"}, | ||
124 | {T_RT, "RT", "router"}, | ||
125 | {T_NSAP, "NSAP", "nsap address"}, | ||
126 | {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"}, | ||
127 | {T_SIG, "SIG", "signature"}, | ||
128 | {T_KEY, "KEY", "key"}, | ||
129 | {T_PX, "PX", "mapping information"}, | ||
130 | {T_GPOS, "GPOS", "geographical position (withdrawn)"}, | ||
131 | {T_AAAA, "AAAA", "IPv6 address"}, | ||
132 | {T_LOC, "LOC", "location"}, | ||
133 | {T_NXT, "NXT", "next valid name (unimplemented)"}, | ||
134 | {T_EID, "EID", "endpoint identifier (unimplemented)"}, | ||
135 | {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"}, | ||
136 | {T_SRV, "SRV", "server selection"}, | ||
137 | {T_ATMA, "ATMA", "ATM address (unimplemented)"}, | ||
138 | {T_IXFR, "IXFR", "incremental zone transfer"}, | ||
139 | {T_AXFR, "AXFR", "zone transfer"}, | ||
140 | {T_MAILB, "MAILB", "mailbox-related data (deprecated)"}, | ||
141 | {T_MAILA, "MAILA", "mail agent (deprecated)"}, | ||
142 | {T_UINFO, "UINFO", "user information (nonstandard)"}, | ||
143 | {T_UID, "UID", "user ID (nonstandard)"}, | ||
144 | {T_GID, "GID", "group ID (nonstandard)"}, | ||
145 | {T_NAPTR, "NAPTR", "URN Naming Authority"}, | ||
146 | #ifdef ALLOW_T_UNSPEC | ||
147 | {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"}, | ||
148 | #endif /* ALLOW_T_UNSPEC */ | ||
149 | {T_ANY, "ANY", "\"any\""}, | ||
150 | {0, NULL, NULL} | ||
151 | }; | ||
152 | |||
153 | const char * | ||
154 | __sym_ntos(const struct res_sym *syms, int number, int *success) | ||
155 | { | ||
156 | static char unname[20]; | ||
157 | |||
158 | for (; syms->name != 0; syms++) { | ||
159 | if (number == syms->number) { | ||
160 | if (success) | ||
161 | *success = 1; | ||
162 | return (syms->name); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | snprintf(unname, sizeof unname, "%d", number); | ||
167 | if (success) | ||
168 | *success = 0; | ||
169 | return (unname); | ||
170 | } | ||
171 | |||
172 | /* | ||
173 | * Return a string for the type | ||
174 | */ | ||
175 | const char * | ||
176 | __p_type(int type) | ||
177 | { | ||
178 | return (__sym_ntos (__p_type_syms, type, (int *)0)); | ||
179 | } | ||
180 | |||
181 | /* | ||
182 | * Return a mnemonic for class | ||
183 | */ | ||
184 | const char * | ||
185 | __p_class(int class) | ||
186 | { | ||
187 | return (__sym_ntos (__p_class_syms, class, (int *)0)); | ||
188 | } | ||
189 | |||
diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c new file mode 100644 index 0000000000..e22771995a --- /dev/null +++ b/src/lib/libc/net/res_random.c | |||
@@ -0,0 +1,275 @@ | |||
1 | /* $OpenBSD: res_random.c,v 1.20 2013/11/12 07:00:24 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | ||
5 | * Copyright 2008 Damien Miller <djm@openbsd.org> | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using | ||
9 | * such a mathematical system to generate more random (yet non-repeating) | ||
10 | * ids to solve the resolver/named problem. But Niels designed the | ||
11 | * actual system based on the constraints. | ||
12 | * | ||
13 | * Later modified by Damien Miller to wrap the LCG output in a 15-bit | ||
14 | * permutation generator based on a Luby-Rackoff block cipher. This | ||
15 | * ensures the output is non-repeating and preserves the MSB twiddle | ||
16 | * trick, but makes it more resistant to LCG prediction. | ||
17 | * | ||
18 | * Redistribution and use in source and binary forms, with or without | ||
19 | * modification, are permitted provided that the following conditions | ||
20 | * are met: | ||
21 | * 1. Redistributions of source code must retain the above copyright | ||
22 | * notice, this list of conditions and the following disclaimer. | ||
23 | * 2. Redistributions in binary form must reproduce the above copyright | ||
24 | * notice, this list of conditions and the following disclaimer in the | ||
25 | * documentation and/or other materials provided with the distribution. | ||
26 | * | ||
27 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
28 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
29 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
30 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
32 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
33 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
34 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
35 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
36 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
37 | */ | ||
38 | |||
39 | /* | ||
40 | * seed = random 15bit | ||
41 | * n = prime, g0 = generator to n, | ||
42 | * j = random so that gcd(j,n-1) == 1 | ||
43 | * g = g0^j mod n will be a generator again. | ||
44 | * | ||
45 | * X[0] = random seed. | ||
46 | * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator | ||
47 | * with a = 7^(even random) mod m, | ||
48 | * b = random with gcd(b,m) == 1 | ||
49 | * m = 31104 and a maximal period of m-1. | ||
50 | * | ||
51 | * The transaction id is determined by: | ||
52 | * id[n] = seed xor (g^X[n] mod n) | ||
53 | * | ||
54 | * Effectivly the id is restricted to the lower 15 bits, thus | ||
55 | * yielding two different cycles by toggling the msb on and off. | ||
56 | * This avoids reuse issues caused by reseeding. | ||
57 | * | ||
58 | * The output of this generator is then randomly permuted though a | ||
59 | * custom 15 bit Luby-Rackoff block cipher. | ||
60 | */ | ||
61 | |||
62 | #include <sys/types.h> | ||
63 | #include <netinet/in.h> | ||
64 | #include <sys/time.h> | ||
65 | #include <resolv.h> | ||
66 | |||
67 | #include <unistd.h> | ||
68 | #include <stdlib.h> | ||
69 | #include <string.h> | ||
70 | |||
71 | #include "thread_private.h" | ||
72 | |||
73 | #define RU_OUT 180 /* Time after wich will be reseeded */ | ||
74 | #define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */ | ||
75 | #define RU_GEN 2 /* Starting generator */ | ||
76 | #define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */ | ||
77 | #define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */ | ||
78 | #define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */ | ||
79 | #define RU_ROUNDS 11 /* Number of rounds for permute (odd) */ | ||
80 | |||
81 | struct prf_ctx { | ||
82 | /* PRF lookup table for odd rounds (7 bits input to 8 bits output) */ | ||
83 | u_char prf7[(RU_ROUNDS / 2) * (1 << 7)]; | ||
84 | |||
85 | /* PRF lookup table for even rounds (8 bits input to 7 bits output) */ | ||
86 | u_char prf8[((RU_ROUNDS + 1) / 2) * (1 << 8)]; | ||
87 | }; | ||
88 | |||
89 | #define PFAC_N 3 | ||
90 | const static u_int16_t pfacts[PFAC_N] = { | ||
91 | 2, | ||
92 | 3, | ||
93 | 2729 | ||
94 | }; | ||
95 | |||
96 | static u_int16_t ru_x; | ||
97 | static u_int16_t ru_seed, ru_seed2; | ||
98 | static u_int16_t ru_a, ru_b; | ||
99 | static u_int16_t ru_g; | ||
100 | static u_int16_t ru_counter = 0; | ||
101 | static u_int16_t ru_msb = 0; | ||
102 | static struct prf_ctx *ru_prf = NULL; | ||
103 | static time_t ru_reseed; | ||
104 | |||
105 | static u_int16_t pmod(u_int16_t, u_int16_t, u_int16_t); | ||
106 | static void res_initid(void); | ||
107 | |||
108 | /* | ||
109 | * Do a fast modular exponation, returned value will be in the range | ||
110 | * of 0 - (mod-1) | ||
111 | */ | ||
112 | static u_int16_t | ||
113 | pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod) | ||
114 | { | ||
115 | u_int16_t s, t, u; | ||
116 | |||
117 | s = 1; | ||
118 | t = gen; | ||
119 | u = exp; | ||
120 | |||
121 | while (u) { | ||
122 | if (u & 1) | ||
123 | s = (s * t) % mod; | ||
124 | u >>= 1; | ||
125 | t = (t * t) % mod; | ||
126 | } | ||
127 | return (s); | ||
128 | } | ||
129 | |||
130 | /* | ||
131 | * 15-bit permutation based on Luby-Rackoff block cipher | ||
132 | */ | ||
133 | static u_int | ||
134 | permute15(u_int in) | ||
135 | { | ||
136 | int i; | ||
137 | u_int left, right, tmp; | ||
138 | |||
139 | if (ru_prf == NULL) | ||
140 | return in; | ||
141 | |||
142 | left = (in >> 8) & 0x7f; | ||
143 | right = in & 0xff; | ||
144 | |||
145 | /* | ||
146 | * Each round swaps the width of left and right. Even rounds have | ||
147 | * a 7-bit left, odd rounds have an 8-bit left. Since this uses an | ||
148 | * odd number of rounds, left is always 8 bits wide at the end. | ||
149 | */ | ||
150 | for (i = 0; i < RU_ROUNDS; i++) { | ||
151 | if ((i & 1) == 0) | ||
152 | tmp = ru_prf->prf8[(i << (8 - 1)) | right] & 0x7f; | ||
153 | else | ||
154 | tmp = ru_prf->prf7[((i - 1) << (7 - 1)) | right]; | ||
155 | tmp ^= left; | ||
156 | left = right; | ||
157 | right = tmp; | ||
158 | } | ||
159 | |||
160 | return (right << 8) | left; | ||
161 | } | ||
162 | |||
163 | /* | ||
164 | * Initializes the seed and chooses a suitable generator. Also toggles | ||
165 | * the msb flag. The msb flag is used to generate two distinct | ||
166 | * cycles of random numbers and thus avoiding reuse of ids. | ||
167 | * | ||
168 | * This function is called from res_randomid() when needed, an | ||
169 | * application does not have to worry about it. | ||
170 | */ | ||
171 | static void | ||
172 | res_initid(void) | ||
173 | { | ||
174 | u_int16_t j, i; | ||
175 | u_int32_t tmp; | ||
176 | int noprime = 1; | ||
177 | struct timespec ts; | ||
178 | |||
179 | ru_x = arc4random_uniform(RU_M); | ||
180 | |||
181 | /* 15 bits of random seed */ | ||
182 | tmp = arc4random(); | ||
183 | ru_seed = (tmp >> 16) & 0x7FFF; | ||
184 | ru_seed2 = tmp & 0x7FFF; | ||
185 | |||
186 | /* Determine the LCG we use */ | ||
187 | tmp = arc4random(); | ||
188 | ru_b = (tmp & 0xfffe) | 1; | ||
189 | ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); | ||
190 | while (ru_b % 3 == 0) | ||
191 | ru_b += 2; | ||
192 | |||
193 | j = arc4random_uniform(RU_N); | ||
194 | |||
195 | /* | ||
196 | * Do a fast gcd(j,RU_N-1), so we can find a j with | ||
197 | * gcd(j, RU_N-1) == 1, giving a new generator for | ||
198 | * RU_GEN^j mod RU_N | ||
199 | */ | ||
200 | |||
201 | while (noprime) { | ||
202 | for (i = 0; i < PFAC_N; i++) | ||
203 | if (j % pfacts[i] == 0) | ||
204 | break; | ||
205 | |||
206 | if (i >= PFAC_N) | ||
207 | noprime = 0; | ||
208 | else | ||
209 | j = (j + 1) % RU_N; | ||
210 | } | ||
211 | |||
212 | ru_g = pmod(RU_GEN, j, RU_N); | ||
213 | ru_counter = 0; | ||
214 | |||
215 | /* Initialise PRF for Luby-Rackoff permutation */ | ||
216 | if (ru_prf == NULL) | ||
217 | ru_prf = malloc(sizeof(*ru_prf)); | ||
218 | if (ru_prf != NULL) | ||
219 | arc4random_buf(ru_prf, sizeof(*ru_prf)); | ||
220 | |||
221 | clock_gettime(CLOCK_MONOTONIC, &ts); | ||
222 | ru_reseed = ts.tv_sec + RU_OUT; | ||
223 | ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; | ||
224 | } | ||
225 | |||
226 | u_int | ||
227 | res_randomid(void) | ||
228 | { | ||
229 | struct timespec ts; | ||
230 | u_int r; | ||
231 | _THREAD_PRIVATE_MUTEX(random); | ||
232 | |||
233 | clock_gettime(CLOCK_MONOTONIC, &ts); | ||
234 | |||
235 | _THREAD_PRIVATE_MUTEX_LOCK(random); | ||
236 | |||
237 | if (ru_counter >= RU_MAX || ts.tv_sec > ru_reseed) | ||
238 | res_initid(); | ||
239 | |||
240 | /* Linear Congruential Generator */ | ||
241 | ru_x = (ru_a * ru_x + ru_b) % RU_M; | ||
242 | ru_counter++; | ||
243 | |||
244 | r = permute15(ru_seed ^ pmod(ru_g, ru_seed2 + ru_x, RU_N)) | ru_msb; | ||
245 | |||
246 | _THREAD_PRIVATE_MUTEX_UNLOCK(random); | ||
247 | |||
248 | return (r); | ||
249 | } | ||
250 | |||
251 | #if 0 | ||
252 | int | ||
253 | main(int argc, char **argv) | ||
254 | { | ||
255 | int i, n; | ||
256 | u_int16_t wert; | ||
257 | |||
258 | res_initid(); | ||
259 | |||
260 | printf("Generator: %u\n", ru_g); | ||
261 | printf("Seed: %u\n", ru_seed); | ||
262 | printf("Reseed at %ld\n", ru_reseed); | ||
263 | printf("Ru_X: %u\n", ru_x); | ||
264 | printf("Ru_A: %u\n", ru_a); | ||
265 | printf("Ru_B: %u\n", ru_b); | ||
266 | |||
267 | n = argc > 1 ? atoi(argv[1]) : 60001; | ||
268 | for (i=0;i<n;i++) { | ||
269 | wert = res_randomid(); | ||
270 | printf("%u\n", wert); | ||
271 | } | ||
272 | return 0; | ||
273 | } | ||
274 | #endif | ||
275 | |||
diff --git a/src/lib/libc/net/resolver.3 b/src/lib/libc/net/resolver.3 new file mode 100644 index 0000000000..93b365a619 --- /dev/null +++ b/src/lib/libc/net/resolver.3 | |||
@@ -0,0 +1,398 @@ | |||
1 | .\" $OpenBSD: resolver.3,v 1.29 2014/01/21 03:15:45 schwarze Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1985, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd $Mdocdate: January 21 2014 $ | ||
31 | .Dt RESOLVER 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm res_query , | ||
35 | .Nm res_search , | ||
36 | .Nm res_mkquery , | ||
37 | .Nm res_send , | ||
38 | .Nm res_init , | ||
39 | .Nm dn_comp , | ||
40 | .Nm dn_expand | ||
41 | .Nd resolver routines | ||
42 | .Sh SYNOPSIS | ||
43 | .In sys/types.h | ||
44 | .In netinet/in.h | ||
45 | .In arpa/nameser.h | ||
46 | .In resolv.h | ||
47 | .Ft int | ||
48 | .Fo res_query | ||
49 | .Fa "const char *dname" | ||
50 | .Fa "int class" | ||
51 | .Fa "int type" | ||
52 | .Fa "unsigned char *answer" | ||
53 | .Fa "int anslen" | ||
54 | .Fc | ||
55 | .Ft int | ||
56 | .Fo res_search | ||
57 | .Fa "const char *dname" | ||
58 | .Fa "int class" | ||
59 | .Fa "int type" | ||
60 | .Fa "unsigned char *answer" | ||
61 | .Fa "int anslen" | ||
62 | .Fc | ||
63 | .Ft int | ||
64 | .Fo res_mkquery | ||
65 | .Fa "int op" | ||
66 | .Fa "const char *dname" | ||
67 | .Fa "int class" | ||
68 | .Fa "int type" | ||
69 | .Fa "const unsigned char *data" | ||
70 | .Fa "int datalen" | ||
71 | .Fa "const unsigned char *newrr" | ||
72 | .Fa "unsigned char *buf" | ||
73 | .Fa "int buflen" | ||
74 | .Fc | ||
75 | .Ft int | ||
76 | .Fo res_send | ||
77 | .Fa "const unsigned char *msg" | ||
78 | .Fa "int msglen" | ||
79 | .Fa "unsigned char *answer" | ||
80 | .Fa "int anslen" | ||
81 | .Fc | ||
82 | .Ft int | ||
83 | .Fn res_init "void" | ||
84 | .Ft int | ||
85 | .Fo dn_comp | ||
86 | .Fa "const char *exp_dn" | ||
87 | .Fa "unsigned char *comp_dn" | ||
88 | .Fa "int length" | ||
89 | .Fa "unsigned char **dnptrs" | ||
90 | .Fa "unsigned char **lastdnptr" | ||
91 | .Fc | ||
92 | .Ft int | ||
93 | .Fo dn_expand | ||
94 | .Fa "const unsigned char *msg" | ||
95 | .Fa "const unsigned char *eomorig" | ||
96 | .Fa "const unsigned char *comp_dn" | ||
97 | .Fa "char *exp_dn" | ||
98 | .Fa "int length" | ||
99 | .Fc | ||
100 | .Sh DESCRIPTION | ||
101 | These routines are used for making, sending, and interpreting | ||
102 | query and reply messages with Internet domain name servers. | ||
103 | .Pp | ||
104 | Global configuration and state information that is used by the | ||
105 | resolver routines is kept in the structure | ||
106 | .Li _res . | ||
107 | Most of the values have reasonable defaults and can be ignored. | ||
108 | Options stored in | ||
109 | .Li _res.options | ||
110 | are defined in | ||
111 | .In resolv.h | ||
112 | and are as follows. | ||
113 | Options are stored as a simple bit mask containing the bitwise OR | ||
114 | of the options enabled. | ||
115 | .Bl -tag -width RES_USE_DNSSEC | ||
116 | .It Dv RES_INIT | ||
117 | True if the initial name server address and default domain name are | ||
118 | initialized (i.e.\& | ||
119 | .Fn res_init | ||
120 | has been called). | ||
121 | .It Dv RES_DEBUG | ||
122 | Print debugging messages, | ||
123 | if libc is compiled with | ||
124 | .Dv DEBUG . | ||
125 | By default on | ||
126 | .Ox | ||
127 | this option does nothing. | ||
128 | .It Dv RES_AAONLY | ||
129 | Accept authoritative answers only. | ||
130 | With this option, | ||
131 | .Fn res_send | ||
132 | should continue until it finds an authoritative answer or finds an error. | ||
133 | Currently this is not implemented. | ||
134 | .It Dv RES_USEVC | ||
135 | Use TCP connections for queries instead of UDP datagrams. | ||
136 | .It Dv RES_PRIMARY | ||
137 | Query the primary name server only. | ||
138 | Currently this is not implemented. | ||
139 | .It Dv RES_IGNTC | ||
140 | Ignore truncation errors, i.e. don't retry with TCP. | ||
141 | .It Dv RES_RECURSE | ||
142 | Set the recursion-desired bit in queries. | ||
143 | .Pf ( Fn res_send | ||
144 | does not do iterative queries and expects the name server | ||
145 | to handle recursion.) | ||
146 | This option is enabled by default. | ||
147 | .It Dv RES_DEFNAMES | ||
148 | If set, | ||
149 | .Fn res_search | ||
150 | will append the default domain name to single-component names | ||
151 | (those that do not contain a dot). | ||
152 | This option is enabled by default. | ||
153 | .It Dv RES_STAYOPEN | ||
154 | Used with | ||
155 | .Dv RES_USEVC | ||
156 | to keep the TCP connection open between queries. | ||
157 | This is useful only in programs that regularly do many queries. | ||
158 | UDP should be the normal mode used. | ||
159 | .It Dv RES_DNSRCH | ||
160 | If this option is set, | ||
161 | .Fn res_search | ||
162 | will search for host names in the current domain and in parent domains; see | ||
163 | .Xr hostname 7 . | ||
164 | This is used by the standard host lookup routine | ||
165 | .Xr gethostbyname 3 . | ||
166 | This option is enabled by default. | ||
167 | .It Dv RES_INSECURE_1 | ||
168 | Do not require the IP source address on the reply packet | ||
169 | to be equal to the server's address. | ||
170 | .It Dv RES_INSECURE_2 | ||
171 | Do not check if the query section of the reply packet | ||
172 | is equal to that of the query packet. | ||
173 | .It Dv RES_NOALIASES | ||
174 | Turn off the | ||
175 | .Ev HOSTALIASES | ||
176 | feature. | ||
177 | See | ||
178 | .Xr hostname 7 | ||
179 | for more information. | ||
180 | .It Dv RES_USE_INET6 | ||
181 | Enables support for IPv6-only applications. | ||
182 | This causes IPv4 addresses to be returned as an IPv4 mapped address. | ||
183 | For example, 10.1.1.1 will be returned as ::ffff:10.1.1.1. | ||
184 | The option is not meaningful on | ||
185 | .Ox . | ||
186 | .It Dv RES_USE_EDNS0 | ||
187 | Attach an OPT pseudo-RR for the EDNS0 extension, | ||
188 | as specified in RFC 2671. | ||
189 | This informs DNS servers of a client's receive buffer size, | ||
190 | allowing them to take advantage of a non-default receive buffer size, | ||
191 | and thus to send larger replies. | ||
192 | DNS query packets with the EDNS0 extension are not compatible with | ||
193 | non-EDNS0 DNS servers. | ||
194 | .It Dv RES_USE_DNSSEC | ||
195 | Request that the resolver uses | ||
196 | Domain Name System Security Extensions (DNSSEC), | ||
197 | as defined in RFCs 4033, 4034, and 4035. | ||
198 | .El | ||
199 | .Pp | ||
200 | The | ||
201 | .Fn res_init | ||
202 | routine reads the configuration file (if any; see | ||
203 | .Xr resolv.conf 5 ) | ||
204 | to get the default domain name, search list, and the Internet address | ||
205 | of the local name server(s). | ||
206 | If no server is configured, the host running | ||
207 | the resolver is tried. | ||
208 | The current domain name is defined by the hostname | ||
209 | if not specified in the configuration file; | ||
210 | it can be overridden by the environment variable | ||
211 | .Ev LOCALDOMAIN . | ||
212 | This environment variable may contain several blank-separated | ||
213 | tokens if you wish to override the | ||
214 | .Fa search list | ||
215 | on a per-process basis. | ||
216 | This is similar to the | ||
217 | .Fa search | ||
218 | command in the configuration file. | ||
219 | Another environment variable | ||
220 | .Ev RES_OPTIONS | ||
221 | can be set to override certain internal resolver options which | ||
222 | are otherwise set by changing fields in the | ||
223 | .Fa _res | ||
224 | structure or are inherited from the configuration file's | ||
225 | .Fa options | ||
226 | command. | ||
227 | The syntax of the | ||
228 | .Ev RES_OPTIONS | ||
229 | environment variable is explained in | ||
230 | .Xr resolv.conf 5 . | ||
231 | Initialization normally occurs on the first call | ||
232 | to one of the following routines. | ||
233 | .Pp | ||
234 | The | ||
235 | .Fn res_query | ||
236 | function provides an interface to the server query mechanism. | ||
237 | It constructs a query, sends it to the local server, | ||
238 | awaits a response, and makes preliminary checks on the reply. | ||
239 | The query requests information of the specified | ||
240 | .Fa type | ||
241 | and | ||
242 | .Fa class | ||
243 | for the specified fully qualified domain name | ||
244 | .Fa dname . | ||
245 | The reply message is left in the | ||
246 | .Fa answer | ||
247 | buffer with length | ||
248 | .Fa anslen | ||
249 | supplied by the caller. | ||
250 | Values for the | ||
251 | .Fa class | ||
252 | and | ||
253 | .Fa type | ||
254 | fields | ||
255 | are defined in | ||
256 | .In arpa/nameser.h . | ||
257 | .Pp | ||
258 | The | ||
259 | .Fn res_search | ||
260 | routine makes a query and awaits a response like | ||
261 | .Fn res_query , | ||
262 | but in addition, it implements the default and search rules controlled by the | ||
263 | .Dv RES_DEFNAMES | ||
264 | and | ||
265 | .Dv RES_DNSRCH | ||
266 | options. | ||
267 | It returns the first successful reply. | ||
268 | .Pp | ||
269 | The remaining routines are lower-level routines used by | ||
270 | .Fn res_query . | ||
271 | The | ||
272 | .Fn res_mkquery | ||
273 | function constructs a standard query message and places it in | ||
274 | .Fa buf . | ||
275 | It returns the size of the query, or \-1 if the query is larger than | ||
276 | .Fa buflen . | ||
277 | The query type | ||
278 | .Fa op | ||
279 | is usually | ||
280 | .Dv QUERY , | ||
281 | but can be any of the query types defined in | ||
282 | .In arpa/nameser.h . | ||
283 | The domain name for the query is given by | ||
284 | .Fa dname . | ||
285 | .Fa newrr | ||
286 | is currently unused but is intended for making update messages. | ||
287 | .Pp | ||
288 | The | ||
289 | .Fn res_send | ||
290 | routine sends a pre-formatted query and returns an answer. | ||
291 | It will call | ||
292 | .Fn res_init | ||
293 | if | ||
294 | .Dv RES_INIT | ||
295 | is not set, send the query to the local name server, and | ||
296 | handle timeouts and retries. | ||
297 | The length of the reply message is returned, or \-1 if there were errors. | ||
298 | .Pp | ||
299 | The | ||
300 | .Fn dn_comp | ||
301 | function compresses the domain name | ||
302 | .Fa exp_dn | ||
303 | and stores it in | ||
304 | .Fa comp_dn . | ||
305 | The size of the compressed name is returned or \-1 if there were errors. | ||
306 | The size of the array pointed to by | ||
307 | .Fa comp_dn | ||
308 | is given by | ||
309 | .Fa length . | ||
310 | The compression uses an array of pointers | ||
311 | .Fa dnptrs | ||
312 | to previously compressed names in the current message. | ||
313 | The first pointer points | ||
314 | to the beginning of the message and the list ends with | ||
315 | .Dv NULL . | ||
316 | The limit to the array is specified by | ||
317 | .Fa lastdnptr . | ||
318 | A side effect of | ||
319 | .Fn dn_comp | ||
320 | is to update the list of pointers for labels inserted into the message | ||
321 | as the name is compressed. | ||
322 | If | ||
323 | .Fa dnptrs | ||
324 | is | ||
325 | .Dv NULL , | ||
326 | names are not compressed. | ||
327 | If | ||
328 | .Fa lastdnptr | ||
329 | is | ||
330 | .Dv NULL , | ||
331 | the list of labels is not updated. | ||
332 | .Pp | ||
333 | The | ||
334 | .Fn dn_expand | ||
335 | entry expands the compressed domain name | ||
336 | .Fa comp_dn | ||
337 | to a full domain name. | ||
338 | The compressed name is contained in a query or reply message; | ||
339 | .Fa msg | ||
340 | is a pointer to the beginning of the message. | ||
341 | The uncompressed name is placed in the buffer indicated by | ||
342 | .Fa exp_dn | ||
343 | which is of size | ||
344 | .Fa length . | ||
345 | The size of compressed name is returned or \-1 if there was an error. | ||
346 | .Sh FILES | ||
347 | .Bl -tag -width "/etc/resolv.confXX" | ||
348 | .It Pa /etc/resolv.conf | ||
349 | The configuration file. | ||
350 | .El | ||
351 | .Sh SEE ALSO | ||
352 | .Xr gethostbyname 3 , | ||
353 | .Xr resolv.conf 5 , | ||
354 | .Xr hostname 7 , | ||
355 | .Xr named 8 | ||
356 | .Rs | ||
357 | .%T Name Server Operations Guide for BIND | ||
358 | .Re | ||
359 | .Sh STANDARDS | ||
360 | .Rs | ||
361 | .%A M. Stahl | ||
362 | .%D November 1987 | ||
363 | .%R RFC 1032 | ||
364 | .%T Domain Administrators Guide | ||
365 | .Re | ||
366 | .Pp | ||
367 | .Rs | ||
368 | .%A M. Lottor | ||
369 | .%D November 1987 | ||
370 | .%R RFC 1033 | ||
371 | .%T Domain Administrators Operations Guide | ||
372 | .Re | ||
373 | .Pp | ||
374 | .Rs | ||
375 | .%A P. Mockapetris | ||
376 | .%D November 1987 | ||
377 | .%R RFC 1034 | ||
378 | .%T Domain Names \(en Concepts and Facilities | ||
379 | .Re | ||
380 | .Pp | ||
381 | .Rs | ||
382 | .%A P. Mockapetris | ||
383 | .%D November 1987 | ||
384 | .%R RFC 1035 | ||
385 | .%T Domain Names \(en Implementation and Specification | ||
386 | .Re | ||
387 | .Pp | ||
388 | .Rs | ||
389 | .%A J. Klensin | ||
390 | .%D October 2008 | ||
391 | .%R RFC 5321 | ||
392 | .%T Simple Mail Transfer Protocol | ||
393 | .Re | ||
394 | .Sh HISTORY | ||
395 | The | ||
396 | .Nm | ||
397 | function appeared in | ||
398 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/net/rresvport.c b/src/lib/libc/net/rresvport.c new file mode 100644 index 0000000000..ccc411500b --- /dev/null +++ b/src/lib/libc/net/rresvport.c | |||
@@ -0,0 +1,107 @@ | |||
1 | /* $OpenBSD: rresvport.c,v 1.9 2005/11/10 10:00:17 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. | ||
4 | * Copyright (c) 1983, 1993, 1994 | ||
5 | * The Regents of the University of California. All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the University nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <sys/param.h> | ||
33 | #include <sys/socket.h> | ||
34 | #include <sys/stat.h> | ||
35 | |||
36 | #include <netinet/in.h> | ||
37 | #include <arpa/inet.h> | ||
38 | |||
39 | #include <signal.h> | ||
40 | #include <fcntl.h> | ||
41 | #include <netdb.h> | ||
42 | #include <unistd.h> | ||
43 | #include <pwd.h> | ||
44 | #include <errno.h> | ||
45 | #include <stdio.h> | ||
46 | #include <ctype.h> | ||
47 | #include <string.h> | ||
48 | #include <syslog.h> | ||
49 | #include <stdlib.h> | ||
50 | #include <netgroup.h> | ||
51 | |||
52 | int | ||
53 | rresvport(int *alport) | ||
54 | { | ||
55 | return rresvport_af(alport, AF_INET); | ||
56 | } | ||
57 | |||
58 | |||
59 | int | ||
60 | rresvport_af(int *alport, int af) | ||
61 | { | ||
62 | struct sockaddr_storage ss; | ||
63 | struct sockaddr *sa; | ||
64 | u_int16_t *portp; | ||
65 | int s; | ||
66 | |||
67 | bzero(&ss, sizeof ss); | ||
68 | sa = (struct sockaddr *)&ss; | ||
69 | |||
70 | switch (af) { | ||
71 | case AF_INET: | ||
72 | sa->sa_len = sizeof(struct sockaddr_in); | ||
73 | portp = &((struct sockaddr_in *)sa)->sin_port; | ||
74 | break; | ||
75 | case AF_INET6: | ||
76 | sa->sa_len = sizeof(struct sockaddr_in6); | ||
77 | portp = &((struct sockaddr_in6 *)sa)->sin6_port; | ||
78 | break; | ||
79 | default: | ||
80 | errno = EPFNOSUPPORT; | ||
81 | return (-1); | ||
82 | } | ||
83 | sa->sa_family = af; | ||
84 | |||
85 | s = socket(af, SOCK_STREAM, 0); | ||
86 | if (s < 0) | ||
87 | return (-1); | ||
88 | |||
89 | *portp = htons(*alport); | ||
90 | if (*alport < IPPORT_RESERVED - 1) { | ||
91 | if (bind(s, sa, sa->sa_len) >= 0) | ||
92 | return (s); | ||
93 | if (errno != EADDRINUSE) { | ||
94 | (void)close(s); | ||
95 | return (-1); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | *portp = 0; | ||
100 | sa->sa_family = af; | ||
101 | if (bindresvport_sa(s, sa) == -1) { | ||
102 | (void)close(s); | ||
103 | return (-1); | ||
104 | } | ||
105 | *alport = ntohs(*portp); | ||
106 | return (s); | ||
107 | } | ||
diff --git a/src/lib/libc/net/rthdr.c b/src/lib/libc/net/rthdr.c new file mode 100644 index 0000000000..d10334e027 --- /dev/null +++ b/src/lib/libc/net/rthdr.c | |||
@@ -0,0 +1,378 @@ | |||
1 | /* $OpenBSD: rthdr.c,v 1.8 2006/12/09 01:12:28 itojun Exp $ */ | ||
2 | /* $KAME: rthdr.c,v 1.22 2006/02/09 08:18:58 keiichi Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. Neither the name of the project nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include <sys/param.h> | ||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | |||
37 | #include <netinet/in.h> | ||
38 | #include <netinet/ip6.h> | ||
39 | |||
40 | #include <string.h> | ||
41 | #include <stdio.h> | ||
42 | |||
43 | /* | ||
44 | * RFC2292 API | ||
45 | */ | ||
46 | |||
47 | size_t | ||
48 | inet6_rthdr_space(int type, int seg) | ||
49 | { | ||
50 | switch (type) { | ||
51 | case IPV6_RTHDR_TYPE_0: | ||
52 | if (seg < 1 || seg > 23) | ||
53 | return (0); | ||
54 | return (CMSG_SPACE(sizeof(struct in6_addr) * seg + | ||
55 | sizeof(struct ip6_rthdr0))); | ||
56 | default: | ||
57 | return (0); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | struct cmsghdr * | ||
62 | inet6_rthdr_init(void *bp, int type) | ||
63 | { | ||
64 | struct cmsghdr *ch = (struct cmsghdr *)bp; | ||
65 | struct ip6_rthdr *rthdr; | ||
66 | |||
67 | rthdr = (struct ip6_rthdr *)CMSG_DATA(ch); | ||
68 | |||
69 | ch->cmsg_level = IPPROTO_IPV6; | ||
70 | ch->cmsg_type = IPV6_RTHDR; | ||
71 | |||
72 | switch (type) { | ||
73 | case IPV6_RTHDR_TYPE_0: | ||
74 | ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0)); | ||
75 | bzero(rthdr, sizeof(struct ip6_rthdr0)); | ||
76 | rthdr->ip6r_type = IPV6_RTHDR_TYPE_0; | ||
77 | return (ch); | ||
78 | default: | ||
79 | return (NULL); | ||
80 | } | ||
81 | } | ||
82 | |||
83 | int | ||
84 | inet6_rthdr_add(struct cmsghdr *cmsg, const struct in6_addr *addr, u_int flags) | ||
85 | { | ||
86 | struct ip6_rthdr *rthdr; | ||
87 | |||
88 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
89 | |||
90 | switch (rthdr->ip6r_type) { | ||
91 | case IPV6_RTHDR_TYPE_0: | ||
92 | { | ||
93 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
94 | if (flags != IPV6_RTHDR_LOOSE) | ||
95 | return (-1); | ||
96 | if (rt0->ip6r0_segleft == 23) | ||
97 | return (-1); | ||
98 | rt0->ip6r0_segleft++; | ||
99 | bcopy(addr, (caddr_t)rt0 + ((rt0->ip6r0_len + 1) << 3), | ||
100 | sizeof(struct in6_addr)); | ||
101 | rt0->ip6r0_len += sizeof(struct in6_addr) >> 3; | ||
102 | cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3); | ||
103 | break; | ||
104 | } | ||
105 | default: | ||
106 | return (-1); | ||
107 | } | ||
108 | |||
109 | return (0); | ||
110 | } | ||
111 | |||
112 | int | ||
113 | inet6_rthdr_lasthop(struct cmsghdr *cmsg, unsigned int flags) | ||
114 | { | ||
115 | struct ip6_rthdr *rthdr; | ||
116 | |||
117 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
118 | |||
119 | switch (rthdr->ip6r_type) { | ||
120 | case IPV6_RTHDR_TYPE_0: | ||
121 | { | ||
122 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
123 | if (flags != IPV6_RTHDR_LOOSE) | ||
124 | return (-1); | ||
125 | if (rt0->ip6r0_segleft > 23) | ||
126 | return (-1); | ||
127 | break; | ||
128 | } | ||
129 | default: | ||
130 | return (-1); | ||
131 | } | ||
132 | |||
133 | return (0); | ||
134 | } | ||
135 | |||
136 | #if 0 | ||
137 | int | ||
138 | inet6_rthdr_reverse(in, out) | ||
139 | const struct cmsghdr *in; | ||
140 | struct cmsghdr *out; | ||
141 | { | ||
142 | |||
143 | return (-1); | ||
144 | } | ||
145 | #endif | ||
146 | |||
147 | int | ||
148 | inet6_rthdr_segments(const struct cmsghdr *cmsg) | ||
149 | { | ||
150 | struct ip6_rthdr *rthdr; | ||
151 | |||
152 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
153 | |||
154 | switch (rthdr->ip6r_type) { | ||
155 | case IPV6_RTHDR_TYPE_0: | ||
156 | { | ||
157 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
158 | |||
159 | if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) | ||
160 | return (-1); | ||
161 | |||
162 | return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); | ||
163 | } | ||
164 | |||
165 | default: | ||
166 | return (-1); | ||
167 | } | ||
168 | } | ||
169 | |||
170 | struct in6_addr * | ||
171 | inet6_rthdr_getaddr(struct cmsghdr *cmsg, int index) | ||
172 | { | ||
173 | struct ip6_rthdr *rthdr; | ||
174 | |||
175 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
176 | |||
177 | switch (rthdr->ip6r_type) { | ||
178 | case IPV6_RTHDR_TYPE_0: | ||
179 | { | ||
180 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
181 | int naddr; | ||
182 | |||
183 | if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) | ||
184 | return NULL; | ||
185 | naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); | ||
186 | if (index <= 0 || naddr < index) | ||
187 | return NULL; | ||
188 | return ((struct in6_addr *)(rt0 + 1)) + index; | ||
189 | } | ||
190 | |||
191 | default: | ||
192 | return NULL; | ||
193 | } | ||
194 | } | ||
195 | |||
196 | int | ||
197 | inet6_rthdr_getflags(const struct cmsghdr *cmsg, int index) | ||
198 | { | ||
199 | struct ip6_rthdr *rthdr; | ||
200 | |||
201 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
202 | |||
203 | switch (rthdr->ip6r_type) { | ||
204 | case IPV6_RTHDR_TYPE_0: | ||
205 | { | ||
206 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
207 | int naddr; | ||
208 | |||
209 | if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) | ||
210 | return (-1); | ||
211 | naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); | ||
212 | if (index < 0 || naddr < index) | ||
213 | return (-1); | ||
214 | return IPV6_RTHDR_LOOSE; | ||
215 | } | ||
216 | |||
217 | default: | ||
218 | return (-1); | ||
219 | } | ||
220 | } | ||
221 | |||
222 | /* | ||
223 | * RFC3542 (2292bis) API | ||
224 | */ | ||
225 | |||
226 | socklen_t | ||
227 | inet6_rth_space(int type, int segments) | ||
228 | { | ||
229 | switch (type) { | ||
230 | case IPV6_RTHDR_TYPE_0: | ||
231 | return (((segments * 2) + 1) << 3); | ||
232 | default: | ||
233 | return (0); /* type not suppported */ | ||
234 | } | ||
235 | } | ||
236 | |||
237 | void * | ||
238 | inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments) | ||
239 | { | ||
240 | struct ip6_rthdr *rth = (struct ip6_rthdr *)bp; | ||
241 | struct ip6_rthdr0 *rth0; | ||
242 | |||
243 | switch (type) { | ||
244 | case IPV6_RTHDR_TYPE_0: | ||
245 | /* length validation */ | ||
246 | if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments)) | ||
247 | return (NULL); | ||
248 | |||
249 | memset(bp, 0, bp_len); | ||
250 | rth0 = (struct ip6_rthdr0 *)rth; | ||
251 | rth0->ip6r0_len = segments * 2; | ||
252 | rth0->ip6r0_type = IPV6_RTHDR_TYPE_0; | ||
253 | rth0->ip6r0_segleft = 0; | ||
254 | rth0->ip6r0_reserved = 0; | ||
255 | break; | ||
256 | default: | ||
257 | return (NULL); /* type not supported */ | ||
258 | } | ||
259 | |||
260 | return (bp); | ||
261 | } | ||
262 | |||
263 | int | ||
264 | inet6_rth_add(void *bp, const struct in6_addr *addr) | ||
265 | { | ||
266 | struct ip6_rthdr *rth = (struct ip6_rthdr *)bp; | ||
267 | struct ip6_rthdr0 *rth0; | ||
268 | struct in6_addr *nextaddr; | ||
269 | |||
270 | switch (rth->ip6r_type) { | ||
271 | case IPV6_RTHDR_TYPE_0: | ||
272 | rth0 = (struct ip6_rthdr0 *)rth; | ||
273 | nextaddr = (struct in6_addr *)(rth0 + 1) + rth0->ip6r0_segleft; | ||
274 | *nextaddr = *addr; | ||
275 | rth0->ip6r0_segleft++; | ||
276 | break; | ||
277 | default: | ||
278 | return (-1); /* type not supported */ | ||
279 | } | ||
280 | |||
281 | return (0); | ||
282 | } | ||
283 | |||
284 | int | ||
285 | inet6_rth_reverse(const void *in, void *out) | ||
286 | { | ||
287 | struct ip6_rthdr *rth_in = (struct ip6_rthdr *)in; | ||
288 | struct ip6_rthdr0 *rth0_in, *rth0_out; | ||
289 | int i, segments; | ||
290 | |||
291 | switch (rth_in->ip6r_type) { | ||
292 | case IPV6_RTHDR_TYPE_0: | ||
293 | rth0_in = (struct ip6_rthdr0 *)in; | ||
294 | rth0_out = (struct ip6_rthdr0 *)out; | ||
295 | |||
296 | /* parameter validation XXX too paranoid? */ | ||
297 | if (rth0_in->ip6r0_len % 2) | ||
298 | return (-1); | ||
299 | segments = rth0_in->ip6r0_len / 2; | ||
300 | |||
301 | /* we can't use memcpy here, since in and out may overlap */ | ||
302 | memmove((void *)rth0_out, (void *)rth0_in, | ||
303 | ((rth0_in->ip6r0_len) + 1) << 3); | ||
304 | rth0_out->ip6r0_segleft = segments; | ||
305 | |||
306 | /* reverse the addresses */ | ||
307 | for (i = 0; i < segments / 2; i++) { | ||
308 | struct in6_addr addr_tmp, *addr1, *addr2; | ||
309 | |||
310 | addr1 = (struct in6_addr *)(rth0_out + 1) + i; | ||
311 | addr2 = (struct in6_addr *)(rth0_out + 1) + | ||
312 | (segments - i - 1); | ||
313 | addr_tmp = *addr1; | ||
314 | *addr1 = *addr2; | ||
315 | *addr2 = addr_tmp; | ||
316 | } | ||
317 | |||
318 | break; | ||
319 | default: | ||
320 | return (-1); /* type not supported */ | ||
321 | } | ||
322 | |||
323 | return (0); | ||
324 | } | ||
325 | |||
326 | int | ||
327 | inet6_rth_segments(const void *bp) | ||
328 | { | ||
329 | struct ip6_rthdr *rh = (struct ip6_rthdr *)bp; | ||
330 | struct ip6_rthdr0 *rh0; | ||
331 | int addrs; | ||
332 | |||
333 | switch (rh->ip6r_type) { | ||
334 | case IPV6_RTHDR_TYPE_0: | ||
335 | rh0 = (struct ip6_rthdr0 *)bp; | ||
336 | |||
337 | /* | ||
338 | * Validation for a type-0 routing header. | ||
339 | * Is this too strict? | ||
340 | */ | ||
341 | if ((rh0->ip6r0_len % 2) != 0 || | ||
342 | (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft) | ||
343 | return (-1); | ||
344 | |||
345 | return (addrs); | ||
346 | default: | ||
347 | return (-1); /* unknown type */ | ||
348 | } | ||
349 | } | ||
350 | |||
351 | struct in6_addr * | ||
352 | inet6_rth_getaddr(const void *bp, int idx) | ||
353 | { | ||
354 | struct ip6_rthdr *rh = (struct ip6_rthdr *)bp; | ||
355 | struct ip6_rthdr0 *rh0; | ||
356 | int addrs; | ||
357 | |||
358 | switch (rh->ip6r_type) { | ||
359 | case IPV6_RTHDR_TYPE_0: | ||
360 | rh0 = (struct ip6_rthdr0 *)bp; | ||
361 | |||
362 | /* | ||
363 | * Validation for a type-0 routing header. | ||
364 | * Is this too strict? | ||
365 | */ | ||
366 | if ((rh0->ip6r0_len % 2) != 0 || | ||
367 | (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft) | ||
368 | return (NULL); | ||
369 | |||
370 | if (idx < 0 || addrs <= idx) | ||
371 | return (NULL); | ||
372 | |||
373 | return (((struct in6_addr *)(rh0 + 1)) + idx); | ||
374 | default: | ||
375 | return (NULL); /* unknown type */ | ||
376 | break; | ||
377 | } | ||
378 | } | ||
diff --git a/src/lib/libc/net/ruserok.c b/src/lib/libc/net/ruserok.c new file mode 100644 index 0000000000..21646c156b --- /dev/null +++ b/src/lib/libc/net/ruserok.c | |||
@@ -0,0 +1,438 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. | ||
3 | * Copyright (c) 1983, 1993, 1994 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/socket.h> | ||
32 | #include <sys/stat.h> | ||
33 | |||
34 | #include <netinet/in.h> | ||
35 | #include <arpa/inet.h> | ||
36 | |||
37 | #include <ctype.h> | ||
38 | #include <errno.h> | ||
39 | #include <fcntl.h> | ||
40 | #include <limits.h> | ||
41 | #include <netdb.h> | ||
42 | #include <netgroup.h> | ||
43 | #include <pwd.h> | ||
44 | #include <signal.h> | ||
45 | #include <stdio.h> | ||
46 | #include <stdlib.h> | ||
47 | #include <string.h> | ||
48 | #include <syslog.h> | ||
49 | #include <unistd.h> | ||
50 | |||
51 | int __ivaliduser(FILE *, in_addr_t, const char *, const char *); | ||
52 | int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t, | ||
53 | const char *, const char *); | ||
54 | static int __icheckhost(struct sockaddr *, socklen_t, const char *); | ||
55 | static char *__gethostloop(struct sockaddr *, socklen_t); | ||
56 | |||
57 | int __check_rhosts_file = 1; | ||
58 | char *__rcmd_errstr; | ||
59 | |||
60 | int | ||
61 | ruserok(const char *rhost, int superuser, const char *ruser, const char *luser) | ||
62 | { | ||
63 | struct addrinfo hints, *res, *r; | ||
64 | int error; | ||
65 | |||
66 | memset(&hints, 0, sizeof(hints)); | ||
67 | hints.ai_family = PF_UNSPEC; | ||
68 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
69 | error = getaddrinfo(rhost, "0", &hints, &res); | ||
70 | if (error) | ||
71 | return (-1); | ||
72 | |||
73 | for (r = res; r; r = r->ai_next) { | ||
74 | if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, | ||
75 | luser) == 0) { | ||
76 | freeaddrinfo(res); | ||
77 | return (0); | ||
78 | } | ||
79 | } | ||
80 | freeaddrinfo(res); | ||
81 | return (-1); | ||
82 | } | ||
83 | |||
84 | /* | ||
85 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
86 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
87 | * has ip addresses, we don't have to trust a nameserver. When it | ||
88 | * contains hostnames, we spin through the list of addresses the nameserver | ||
89 | * gives us and look for a match. | ||
90 | * | ||
91 | * Returns 0 if ok, -1 if not ok. | ||
92 | */ | ||
93 | int | ||
94 | iruserok(u_int32_t raddr, int superuser, const char *ruser, const char *luser) | ||
95 | { | ||
96 | struct sockaddr_in sin; | ||
97 | |||
98 | memset(&sin, 0, sizeof(sin)); | ||
99 | sin.sin_family = AF_INET; | ||
100 | sin.sin_len = sizeof(struct sockaddr_in); | ||
101 | memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); | ||
102 | return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, | ||
103 | luser); | ||
104 | } | ||
105 | |||
106 | int | ||
107 | iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser, | ||
108 | const char *luser) | ||
109 | { | ||
110 | struct sockaddr *sa; | ||
111 | char *cp; | ||
112 | struct stat sbuf; | ||
113 | struct passwd *pwd; | ||
114 | FILE *hostf; | ||
115 | uid_t uid; | ||
116 | int first; | ||
117 | char pbuf[PATH_MAX]; | ||
118 | |||
119 | sa = (struct sockaddr *)raddr; | ||
120 | first = 1; | ||
121 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
122 | again: | ||
123 | if (hostf) { | ||
124 | if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { | ||
125 | (void)fclose(hostf); | ||
126 | return (0); | ||
127 | } | ||
128 | (void)fclose(hostf); | ||
129 | } | ||
130 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
131 | int len; | ||
132 | |||
133 | first = 0; | ||
134 | if ((pwd = getpwnam(luser)) == NULL) | ||
135 | return (-1); | ||
136 | len = snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir); | ||
137 | if (len < 0 || len >= sizeof pbuf) | ||
138 | return (-1); | ||
139 | |||
140 | /* | ||
141 | * Change effective uid while opening .rhosts. If root and | ||
142 | * reading an NFS mounted file system, can't read files that | ||
143 | * are protected read/write owner only. | ||
144 | */ | ||
145 | uid = geteuid(); | ||
146 | (void)seteuid(pwd->pw_uid); | ||
147 | hostf = fopen(pbuf, "r"); | ||
148 | (void)seteuid(uid); | ||
149 | |||
150 | if (hostf == NULL) | ||
151 | return (-1); | ||
152 | /* | ||
153 | * If not a regular file, or is owned by someone other than | ||
154 | * user or root or if writeable by anyone but the owner, quit. | ||
155 | */ | ||
156 | cp = NULL; | ||
157 | if (lstat(pbuf, &sbuf) < 0) | ||
158 | cp = ".rhosts lstat failed"; | ||
159 | else if (!S_ISREG(sbuf.st_mode)) | ||
160 | cp = ".rhosts not regular file"; | ||
161 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
162 | cp = ".rhosts fstat failed"; | ||
163 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
164 | cp = "bad .rhosts owner"; | ||
165 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
166 | cp = ".rhosts writable by other than owner"; | ||
167 | /* If there were any problems, quit. */ | ||
168 | if (cp) { | ||
169 | __rcmd_errstr = cp; | ||
170 | (void)fclose(hostf); | ||
171 | return (-1); | ||
172 | } | ||
173 | goto again; | ||
174 | } | ||
175 | return (-1); | ||
176 | } | ||
177 | |||
178 | /* | ||
179 | * XXX | ||
180 | * Don't make static, used by lpd(8). | ||
181 | * | ||
182 | * Returns 0 if ok, -1 if not ok. | ||
183 | */ | ||
184 | int | ||
185 | __ivaliduser(FILE *hostf, in_addr_t raddrl, const char *luser, | ||
186 | const char *ruser) | ||
187 | { | ||
188 | struct sockaddr_in sin; | ||
189 | |||
190 | memset(&sin, 0, sizeof(sin)); | ||
191 | sin.sin_family = AF_INET; | ||
192 | sin.sin_len = sizeof(struct sockaddr_in); | ||
193 | memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr)); | ||
194 | return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, | ||
195 | luser, ruser); | ||
196 | } | ||
197 | |||
198 | int | ||
199 | __ivaliduser_sa(FILE *hostf, struct sockaddr *raddr, socklen_t salen, | ||
200 | const char *luser, const char *ruser) | ||
201 | { | ||
202 | char *user, *p; | ||
203 | char *buf; | ||
204 | const char *auser, *ahost; | ||
205 | int hostok, userok; | ||
206 | char *rhost = (char *)-1; | ||
207 | char domain[MAXHOSTNAMELEN]; | ||
208 | size_t buflen; | ||
209 | |||
210 | getdomainname(domain, sizeof(domain)); | ||
211 | |||
212 | while ((buf = fgetln(hostf, &buflen))) { | ||
213 | p = buf; | ||
214 | if (*p == '#') | ||
215 | continue; | ||
216 | while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') { | ||
217 | if (!isprint((unsigned char)*p)) | ||
218 | goto bail; | ||
219 | *p = isupper((unsigned char)*p) ? | ||
220 | tolower((unsigned char)*p) : *p; | ||
221 | p++; | ||
222 | } | ||
223 | if (p >= buf + buflen) | ||
224 | continue; | ||
225 | if (*p == ' ' || *p == '\t') { | ||
226 | *p++ = '\0'; | ||
227 | while (p < buf + buflen && (*p == ' ' || *p == '\t')) | ||
228 | p++; | ||
229 | if (p >= buf + buflen) | ||
230 | continue; | ||
231 | user = p; | ||
232 | while (p < buf + buflen && *p != '\n' && *p != ' ' && | ||
233 | *p != '\t') { | ||
234 | if (!isprint((unsigned char)*p)) | ||
235 | goto bail; | ||
236 | p++; | ||
237 | } | ||
238 | } else | ||
239 | user = p; | ||
240 | *p = '\0'; | ||
241 | |||
242 | if (p == buf) | ||
243 | continue; | ||
244 | |||
245 | auser = *user ? user : luser; | ||
246 | ahost = buf; | ||
247 | |||
248 | if (strlen(ahost) >= MAXHOSTNAMELEN) | ||
249 | continue; | ||
250 | |||
251 | /* | ||
252 | * innetgr() must lookup a hostname (we do not attempt | ||
253 | * to change the semantics so that netgroups may have | ||
254 | * #.#.#.# addresses in the list.) | ||
255 | */ | ||
256 | if (ahost[0] == '+') | ||
257 | switch (ahost[1]) { | ||
258 | case '\0': | ||
259 | hostok = 1; | ||
260 | break; | ||
261 | case '@': | ||
262 | if (rhost == (char *)-1) | ||
263 | rhost = __gethostloop(raddr, salen); | ||
264 | hostok = 0; | ||
265 | if (rhost) | ||
266 | hostok = innetgr(&ahost[2], rhost, | ||
267 | NULL, domain); | ||
268 | break; | ||
269 | default: | ||
270 | hostok = __icheckhost(raddr, salen, &ahost[1]); | ||
271 | break; | ||
272 | } | ||
273 | else if (ahost[0] == '-') | ||
274 | switch (ahost[1]) { | ||
275 | case '\0': | ||
276 | hostok = -1; | ||
277 | break; | ||
278 | case '@': | ||
279 | if (rhost == (char *)-1) | ||
280 | rhost = __gethostloop(raddr, salen); | ||
281 | hostok = 0; | ||
282 | if (rhost) | ||
283 | hostok = -innetgr(&ahost[2], rhost, | ||
284 | NULL, domain); | ||
285 | break; | ||
286 | default: | ||
287 | hostok = -__icheckhost(raddr, salen, &ahost[1]); | ||
288 | break; | ||
289 | } | ||
290 | else | ||
291 | hostok = __icheckhost(raddr, salen, ahost); | ||
292 | |||
293 | |||
294 | if (auser[0] == '+') | ||
295 | switch (auser[1]) { | ||
296 | case '\0': | ||
297 | userok = 1; | ||
298 | break; | ||
299 | case '@': | ||
300 | userok = innetgr(&auser[2], NULL, ruser, | ||
301 | domain); | ||
302 | break; | ||
303 | default: | ||
304 | userok = strcmp(ruser, &auser[1]) ? 0 : 1; | ||
305 | break; | ||
306 | } | ||
307 | else if (auser[0] == '-') | ||
308 | switch (auser[1]) { | ||
309 | case '\0': | ||
310 | userok = -1; | ||
311 | break; | ||
312 | case '@': | ||
313 | userok = -innetgr(&auser[2], NULL, ruser, | ||
314 | domain); | ||
315 | break; | ||
316 | default: | ||
317 | userok = strcmp(ruser, &auser[1]) ? 0 : -1; | ||
318 | break; | ||
319 | } | ||
320 | else | ||
321 | userok = strcmp(ruser, auser) ? 0 : 1; | ||
322 | |||
323 | /* Check if one component did not match */ | ||
324 | if (hostok == 0 || userok == 0) | ||
325 | continue; | ||
326 | |||
327 | /* Check if we got a forbidden pair */ | ||
328 | if (userok <= -1 || hostok <= -1) | ||
329 | return (-1); | ||
330 | |||
331 | /* Check if we got a valid pair */ | ||
332 | if (hostok >= 1 && userok >= 1) | ||
333 | return (0); | ||
334 | } | ||
335 | bail: | ||
336 | return (-1); | ||
337 | } | ||
338 | |||
339 | /* | ||
340 | * Returns "true" if match, 0 if no match. If we do not find any | ||
341 | * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. | ||
342 | */ | ||
343 | static int | ||
344 | __icheckhost(struct sockaddr *raddr, socklen_t salen, const char *lhost) | ||
345 | { | ||
346 | struct addrinfo hints, *res, *r; | ||
347 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
348 | int error; | ||
349 | const int niflags = NI_NUMERICHOST; | ||
350 | |||
351 | h1[0] = '\0'; | ||
352 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
353 | niflags) != 0) | ||
354 | return (0); | ||
355 | |||
356 | /* Resolve laddr into sockaddr */ | ||
357 | memset(&hints, 0, sizeof(hints)); | ||
358 | hints.ai_family = raddr->sa_family; | ||
359 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
360 | res = NULL; | ||
361 | error = getaddrinfo(lhost, "0", &hints, &res); | ||
362 | if (error) | ||
363 | return (0); | ||
364 | |||
365 | /* | ||
366 | * Try string comparisons between raddr and laddr. | ||
367 | */ | ||
368 | for (r = res; r; r = r->ai_next) { | ||
369 | h2[0] = '\0'; | ||
370 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
371 | NULL, 0, niflags) != 0) | ||
372 | continue; | ||
373 | if (strcmp(h1, h2) == 0) { | ||
374 | freeaddrinfo(res); | ||
375 | return (1); | ||
376 | } | ||
377 | } | ||
378 | |||
379 | /* No match. */ | ||
380 | freeaddrinfo(res); | ||
381 | return (0); | ||
382 | } | ||
383 | |||
384 | /* | ||
385 | * Return the hostname associated with the supplied address. | ||
386 | * Do a reverse lookup as well for security. If a loop cannot | ||
387 | * be found, pack the result of inet_ntoa() into the string. | ||
388 | */ | ||
389 | static char * | ||
390 | __gethostloop(struct sockaddr *raddr, socklen_t salen) | ||
391 | { | ||
392 | static char remotehost[NI_MAXHOST]; | ||
393 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
394 | struct addrinfo hints, *res, *r; | ||
395 | int error; | ||
396 | const int niflags = NI_NUMERICHOST; | ||
397 | |||
398 | h1[0] = remotehost[0] = '\0'; | ||
399 | if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), | ||
400 | NULL, 0, NI_NAMEREQD) != 0) | ||
401 | return (NULL); | ||
402 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
403 | niflags) != 0) | ||
404 | return (NULL); | ||
405 | |||
406 | /* | ||
407 | * Look up the name and check that the supplied | ||
408 | * address is in the list | ||
409 | */ | ||
410 | memset(&hints, 0, sizeof(hints)); | ||
411 | hints.ai_family = raddr->sa_family; | ||
412 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
413 | hints.ai_flags = AI_CANONNAME; | ||
414 | res = NULL; | ||
415 | error = getaddrinfo(remotehost, "0", &hints, &res); | ||
416 | if (error) | ||
417 | return (NULL); | ||
418 | |||
419 | for (r = res; r; r = r->ai_next) { | ||
420 | h2[0] = '\0'; | ||
421 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
422 | NULL, 0, niflags) != 0) | ||
423 | continue; | ||
424 | if (strcmp(h1, h2) == 0) { | ||
425 | freeaddrinfo(res); | ||
426 | return (remotehost); | ||
427 | } | ||
428 | } | ||
429 | |||
430 | /* | ||
431 | * either the DNS adminstrator has made a configuration | ||
432 | * mistake, or someone has attempted to spoof us | ||
433 | */ | ||
434 | syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", | ||
435 | h1, res->ai_canonname ? res->ai_canonname : remotehost); | ||
436 | freeaddrinfo(res); | ||
437 | return (NULL); | ||
438 | } | ||
diff --git a/src/lib/libc/net/send.c b/src/lib/libc/net/send.c new file mode 100644 index 0000000000..1bfc80b87a --- /dev/null +++ b/src/lib/libc/net/send.c | |||
@@ -0,0 +1,40 @@ | |||
1 | /* $OpenBSD: send.c,v 1.5 2005/08/06 20:30:04 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988, 1993 | ||
4 | * The Regents of the University of California. All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <sys/types.h> | ||
32 | #include <sys/socket.h> | ||
33 | |||
34 | #include <stddef.h> | ||
35 | |||
36 | ssize_t | ||
37 | send(int s, const void *msg, size_t len, int flags) | ||
38 | { | ||
39 | return (sendto(s, msg, len, flags, NULL, 0)); | ||
40 | } | ||
diff --git a/src/lib/libc/net/vars6.c b/src/lib/libc/net/vars6.c new file mode 100644 index 0000000000..a22d49d047 --- /dev/null +++ b/src/lib/libc/net/vars6.c | |||
@@ -0,0 +1,42 @@ | |||
1 | /* $OpenBSD: vars6.c,v 1.2 2003/05/10 10:56:47 jmc Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the project nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <sys/types.h> | ||
33 | #include <netinet/in.h> | ||
34 | |||
35 | /* | ||
36 | * Definitions of some constant IPv6 addresses. | ||
37 | */ | ||
38 | const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; | ||
39 | const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; | ||
40 | const struct in6_addr in6addr_nodelocal_allnodes = IN6ADDR_NODELOCAL_ALLNODES_INIT; | ||
41 | const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; | ||
42 | |||