summaryrefslogtreecommitdiff
path: root/src/lib/libc/net
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
committercvs2svn <admin@example.com>2025-04-14 17:32:06 +0000
commiteb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch)
treeedb6da6af7e865d488dc1a29309f1e1ec226e603 /src/lib/libc/net
parent247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff)
downloadopenbsd-tb_20250414.tar.gz
openbsd-tb_20250414.tar.bz2
openbsd-tb_20250414.zip
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/lib/libc/net')
-rw-r--r--src/lib/libc/net/Makefile.inc31
-rw-r--r--src/lib/libc/net/base64.c307
-rw-r--r--src/lib/libc/net/ether_aton.3114
-rw-r--r--src/lib/libc/net/ethers.c183
-rw-r--r--src/lib/libc/net/freeaddrinfo.c50
-rw-r--r--src/lib/libc/net/gai_strerror.395
-rw-r--r--src/lib/libc/net/gai_strerror.c79
-rw-r--r--src/lib/libc/net/getaddrinfo.3477
-rw-r--r--src/lib/libc/net/gethostbyname.3271
-rw-r--r--src/lib/libc/net/getifaddrs.3157
-rw-r--r--src/lib/libc/net/getifaddrs.c298
-rw-r--r--src/lib/libc/net/getnameinfo.3264
-rw-r--r--src/lib/libc/net/getnetent.3147
-rw-r--r--src/lib/libc/net/getnetent.c49
-rw-r--r--src/lib/libc/net/getpeereid.3121
-rw-r--r--src/lib/libc/net/getpeereid.c36
-rw-r--r--src/lib/libc/net/getproto.c60
-rw-r--r--src/lib/libc/net/getprotoent.3213
-rw-r--r--src/lib/libc/net/getprotoent.c169
-rw-r--r--src/lib/libc/net/getprotoname.c69
-rw-r--r--src/lib/libc/net/getrrsetbyname.3164
-rw-r--r--src/lib/libc/net/getservbyname.c72
-rw-r--r--src/lib/libc/net/getservbyport.c65
-rw-r--r--src/lib/libc/net/getservent.3220
-rw-r--r--src/lib/libc/net/getservent.c171
-rw-r--r--src/lib/libc/net/herror.c106
-rw-r--r--src/lib/libc/net/htobe64.3164
-rw-r--r--src/lib/libc/net/htonl.3102
-rw-r--r--src/lib/libc/net/htonl.c15
-rw-r--r--src/lib/libc/net/htons.c15
-rw-r--r--src/lib/libc/net/if_indextoname.3143
-rw-r--r--src/lib/libc/net/if_indextoname.c78
-rw-r--r--src/lib/libc/net/if_nameindex.c145
-rw-r--r--src/lib/libc/net/if_nametoindex.c77
-rw-r--r--src/lib/libc/net/inet6_opt_init.3328
-rw-r--r--src/lib/libc/net/inet6_rth_space.3220
-rw-r--r--src/lib/libc/net/inet_addr.3195
-rw-r--r--src/lib/libc/net/inet_addr.c175
-rw-r--r--src/lib/libc/net/inet_lnaof.389
-rw-r--r--src/lib/libc/net/inet_lnaof.c50
-rw-r--r--src/lib/libc/net/inet_makeaddr.c53
-rw-r--r--src/lib/libc/net/inet_net_ntop.3195
-rw-r--r--src/lib/libc/net/inet_net_ntop.c160
-rw-r--r--src/lib/libc/net/inet_net_pton.c246
-rw-r--r--src/lib/libc/net/inet_neta.c80
-rw-r--r--src/lib/libc/net/inet_netof.c49
-rw-r--r--src/lib/libc/net/inet_network.c85
-rw-r--r--src/lib/libc/net/inet_ntoa.c51
-rw-r--r--src/lib/libc/net/inet_ntop.3213
-rw-r--r--src/lib/libc/net/inet_ntop.c205
-rw-r--r--src/lib/libc/net/inet_pton.c213
-rw-r--r--src/lib/libc/net/ip6opt.c285
-rw-r--r--src/lib/libc/net/link_ntoa.378
-rw-r--r--src/lib/libc/net/linkaddr.c85
-rw-r--r--src/lib/libc/net/ntohl.c15
-rw-r--r--src/lib/libc/net/ntohs.c15
-rw-r--r--src/lib/libc/net/rcmd.3232
-rw-r--r--src/lib/libc/net/rcmd.c296
-rw-r--r--src/lib/libc/net/rcmdsh.395
-rw-r--r--src/lib/libc/net/rcmdsh.c194
-rw-r--r--src/lib/libc/net/recv.c41
-rw-r--r--src/lib/libc/net/res_comp.c466
-rw-r--r--src/lib/libc/net/res_data.c104
-rw-r--r--src/lib/libc/net/res_debug_syms.c192
-rw-r--r--src/lib/libc/net/res_init.3441
-rw-r--r--src/lib/libc/net/res_random.c281
-rw-r--r--src/lib/libc/net/rresvport.c107
-rw-r--r--src/lib/libc/net/rthdr.c198
-rw-r--r--src/lib/libc/net/ruserok.c392
-rw-r--r--src/lib/libc/net/send.c41
-rw-r--r--src/lib/libc/net/sockatmark.3122
-rw-r--r--src/lib/libc/net/sockatmark.c40
-rw-r--r--src/lib/libc/net/vars6.c42
73 files changed, 0 insertions, 11096 deletions
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc
deleted file mode 100644
index db2c75dad6..0000000000
--- a/src/lib/libc/net/Makefile.inc
+++ /dev/null
@@ -1,31 +0,0 @@
1# $OpenBSD: Makefile.inc,v 1.61 2024/04/15 14:30:48 naddy Exp $
2
3# net sources
4.PATH: ${LIBCSRCDIR}/net
5
6CFLAGS+=-DRESOLVSORT
7
8SRCS+= base64.c ethers.c freeaddrinfo.c \
9 gai_strerror.c getaddrinfo.c gethostnamadr.c \
10 getifaddrs.c getnameinfo.c getnetent.c \
11 getnetnamadr.c getpeereid.c getproto.c getprotoent.c getprotoname.c \
12 getservbyname.c getservbyport.c getservent.c getrrsetbyname.c \
13 herror.c htonl.c htons.c \
14 if_indextoname.c if_nameindex.c if_nametoindex.c inet_addr.c \
15 inet_lnaof.c inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \
16 inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c inet_pton.c \
17 linkaddr.c ntohl.c ntohs.c rcmd.c rcmdsh.c ruserok.c \
18 rresvport.c recv.c res_comp.c res_data.c \
19 res_debug.c res_debug_syms.c res_init.c res_mkquery.c res_query.c \
20 res_random.c res_send.c \
21 send.c sethostent.c sockatmark.c
22
23# IPv6
24SRCS+= ip6opt.c rthdr.c vars6.c
25
26MAN+= htobe64.3 ether_aton.3 gai_strerror.3 getaddrinfo.3 gethostbyname.3 \
27 getifaddrs.3 getnameinfo.3 getnetent.3 getpeereid.3 getprotoent.3 \
28 getrrsetbyname.3 getservent.3 htonl.3 if_indextoname.3 \
29 inet_addr.3 inet_lnaof.3 inet_net_ntop.3 inet_ntop.3 \
30 inet6_opt_init.3 inet6_rth_space.3 link_ntoa.3 \
31 rcmd.3 rcmdsh.3 res_init.3 sockatmark.3
diff --git a/src/lib/libc/net/base64.c b/src/lib/libc/net/base64.c
deleted file mode 100644
index f36c11a21c..0000000000
--- a/src/lib/libc/net/base64.c
+++ /dev/null
@@ -1,307 +0,0 @@
1/* $OpenBSD: base64.c,v 1.15 2021/10/25 14:41:09 jca 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/socket.h>
47#include <netinet/in.h>
48#include <arpa/inet.h>
49
50#include <ctype.h>
51#include <resolv.h>
52
53#include <stdlib.h>
54#include <string.h>
55
56static const char Base64[] =
57 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
58static const char Pad64 = '=';
59
60/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
61 The following encoding technique is taken from RFC 1521 by Borenstein
62 and Freed. It is reproduced here in a slightly edited form for
63 convenience.
64
65 A 65-character subset of US-ASCII is used, enabling 6 bits to be
66 represented per printable character. (The extra 65th character, "=",
67 is used to signify a special processing function.)
68
69 The encoding process represents 24-bit groups of input bits as output
70 strings of 4 encoded characters. Proceeding from left to right, a
71 24-bit input group is formed by concatenating 3 8-bit input groups.
72 These 24 bits are then treated as 4 concatenated 6-bit groups, each
73 of which is translated into a single digit in the base64 alphabet.
74
75 Each 6-bit group is used as an index into an array of 64 printable
76 characters. The character referenced by the index is placed in the
77 output string.
78
79 Table 1: The Base64 Alphabet
80
81 Value Encoding Value Encoding Value Encoding Value Encoding
82 0 A 17 R 34 i 51 z
83 1 B 18 S 35 j 52 0
84 2 C 19 T 36 k 53 1
85 3 D 20 U 37 l 54 2
86 4 E 21 V 38 m 55 3
87 5 F 22 W 39 n 56 4
88 6 G 23 X 40 o 57 5
89 7 H 24 Y 41 p 58 6
90 8 I 25 Z 42 q 59 7
91 9 J 26 a 43 r 60 8
92 10 K 27 b 44 s 61 9
93 11 L 28 c 45 t 62 +
94 12 M 29 d 46 u 63 /
95 13 N 30 e 47 v
96 14 O 31 f 48 w (pad) =
97 15 P 32 g 49 x
98 16 Q 33 h 50 y
99
100 Special processing is performed if fewer than 24 bits are available
101 at the end of the data being encoded. A full encoding quantum is
102 always completed at the end of a quantity. When fewer than 24 input
103 bits are available in an input group, zero bits are added (on the
104 right) to form an integral number of 6-bit groups. Padding at the
105 end of the data is performed using the '=' character.
106
107 Since all base64 input is an integral number of octets, only the
108 -------------------------------------------------
109 following cases can arise:
110
111 (1) the final quantum of encoding input is an integral
112 multiple of 24 bits; here, the final unit of encoded
113 output will be an integral multiple of 4 characters
114 with no "=" padding,
115 (2) the final quantum of encoding input is exactly 8 bits;
116 here, the final unit of encoded output will be two
117 characters followed by two "=" padding characters, or
118 (3) the final quantum of encoding input is exactly 16 bits;
119 here, the final unit of encoded output will be three
120 characters followed by one "=" padding character.
121 */
122
123int
124b64_ntop(unsigned char const *src, size_t srclength, char *target,
125 size_t targsize)
126{
127 size_t datalength = 0;
128 unsigned char input[3];
129 unsigned char output[4];
130 int i;
131
132 while (2 < srclength) {
133 input[0] = *src++;
134 input[1] = *src++;
135 input[2] = *src++;
136 srclength -= 3;
137
138 output[0] = input[0] >> 2;
139 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
140 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
141 output[3] = input[2] & 0x3f;
142
143 if (datalength + 4 > targsize)
144 return (-1);
145 target[datalength++] = Base64[output[0]];
146 target[datalength++] = Base64[output[1]];
147 target[datalength++] = Base64[output[2]];
148 target[datalength++] = Base64[output[3]];
149 }
150
151 /* Now we worry about padding. */
152 if (0 != srclength) {
153 /* Get what's left. */
154 input[0] = input[1] = input[2] = '\0';
155 for (i = 0; i < srclength; i++)
156 input[i] = *src++;
157
158 output[0] = input[0] >> 2;
159 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
160 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
161
162 if (datalength + 4 > targsize)
163 return (-1);
164 target[datalength++] = Base64[output[0]];
165 target[datalength++] = Base64[output[1]];
166 if (srclength == 1)
167 target[datalength++] = Pad64;
168 else
169 target[datalength++] = Base64[output[2]];
170 target[datalength++] = Pad64;
171 }
172 if (datalength >= targsize)
173 return (-1);
174 target[datalength] = '\0'; /* Returned value doesn't count \0. */
175 return (datalength);
176}
177
178/* skips all whitespace anywhere.
179 converts characters, four at a time, starting at (or after)
180 src from base - 64 numbers into three 8 bit bytes in the target area.
181 it returns the number of data bytes stored at the target, or -1 on error.
182 */
183
184int
185b64_pton(char const *src, unsigned char *target, size_t targsize)
186{
187 int tarindex, state, ch;
188 unsigned char nextbyte;
189 char *pos;
190
191 state = 0;
192 tarindex = 0;
193
194 while ((ch = (unsigned char)*src++) != '\0') {
195 if (isspace(ch)) /* Skip whitespace anywhere. */
196 continue;
197
198 if (ch == Pad64)
199 break;
200
201 pos = strchr(Base64, ch);
202 if (pos == 0) /* A non-base64 character. */
203 return (-1);
204
205 switch (state) {
206 case 0:
207 if (target) {
208 if (tarindex >= targsize)
209 return (-1);
210 target[tarindex] = (pos - Base64) << 2;
211 }
212 state = 1;
213 break;
214 case 1:
215 if (target) {
216 if (tarindex >= targsize)
217 return (-1);
218 target[tarindex] |= (pos - Base64) >> 4;
219 nextbyte = ((pos - Base64) & 0x0f) << 4;
220 if (tarindex + 1 < targsize)
221 target[tarindex+1] = nextbyte;
222 else if (nextbyte)
223 return (-1);
224 }
225 tarindex++;
226 state = 2;
227 break;
228 case 2:
229 if (target) {
230 if (tarindex >= targsize)
231 return (-1);
232 target[tarindex] |= (pos - Base64) >> 2;
233 nextbyte = ((pos - Base64) & 0x03) << 6;
234 if (tarindex + 1 < targsize)
235 target[tarindex+1] = nextbyte;
236 else if (nextbyte)
237 return (-1);
238 }
239 tarindex++;
240 state = 3;
241 break;
242 case 3:
243 if (target) {
244 if (tarindex >= targsize)
245 return (-1);
246 target[tarindex] |= (pos - Base64);
247 }
248 tarindex++;
249 state = 0;
250 break;
251 }
252 }
253
254 /*
255 * We are done decoding Base-64 chars. Let's see if we ended
256 * on a byte boundary, and/or with erroneous trailing characters.
257 */
258
259 if (ch == Pad64) { /* We got a pad char. */
260 ch = (unsigned char)*src++; /* Skip it, get next. */
261 switch (state) {
262 case 0: /* Invalid = in first position */
263 case 1: /* Invalid = in second position */
264 return (-1);
265
266 case 2: /* Valid, means one byte of info */
267 /* Skip any number of spaces. */
268 for (; ch != '\0'; ch = (unsigned char)*src++)
269 if (!isspace(ch))
270 break;
271 /* Make sure there is another trailing = sign. */
272 if (ch != Pad64)
273 return (-1);
274 ch = (unsigned char)*src++; /* Skip the = */
275 /* Fall through to "single trailing =" case. */
276 /* FALLTHROUGH */
277
278 case 3: /* Valid, means two bytes of info */
279 /*
280 * We know this char is an =. Is there anything but
281 * whitespace after it?
282 */
283 for (; ch != '\0'; ch = (unsigned char)*src++)
284 if (!isspace(ch))
285 return (-1);
286
287 /*
288 * Now make sure for cases 2 and 3 that the "extra"
289 * bits that slopped past the last full byte were
290 * zeros. If we don't check them, they become a
291 * subliminal channel.
292 */
293 if (target && tarindex < targsize &&
294 target[tarindex] != 0)
295 return (-1);
296 }
297 } else {
298 /*
299 * We ended by seeing the end of the string. Make sure we
300 * have no partial bytes lying around.
301 */
302 if (state != 0)
303 return (-1);
304 }
305
306 return (tarindex);
307}
diff --git a/src/lib/libc/net/ether_aton.3 b/src/lib/libc/net/ether_aton.3
deleted file mode 100644
index 98562dc44c..0000000000
--- a/src/lib/libc/net/ether_aton.3
+++ /dev/null
@@ -1,114 +0,0 @@
1.\" $OpenBSD: ether_aton.3,v 1.3 2022/09/11 06:38:10 jmc Exp $
2.\"
3.\" Written by roland@frob.com. Public domain.
4.\"
5.Dd $Mdocdate: September 11 2022 $
6.Dt ETHER_ATON 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
32Ethernet addresses are represented by the
33following structure:
34.Bd -literal -offset indent
35struct ether_addr {
36 u_int8_t ether_addr_octet[ETHER_ADDR_LEN];
37};
38.Ed
39.Pp
40The
41.Fn ether_ntoa
42function converts this structure into an
43.Tn ASCII
44string of the form
45.Dq xx:xx:xx:xx:xx:xx ,
46consisting of 6 hexadecimal numbers separated
47by colons.
48It returns a pointer to a static buffer that is reused for each call.
49The
50.Fn ether_aton
51function converts an
52.Tn ASCII
53string of the same form and to a structure
54containing the 6 octets of the address.
55It returns a pointer to a static structure that is reused for each call.
56.Fn ether_aton
57will return NULL if the string does not represent a valid address.
58.Pp
59The
60.Fn ether_ntohost
61and
62.Fn ether_hostton
63functions interrogate the database mapping host names to Ethernet
64addresses,
65.Pa /etc/ethers .
66The
67.Fn ether_ntohost
68function looks up the given Ethernet address and writes the associated
69host name into the character buffer passed.
70This buffer should be
71.Dv MAXHOSTNAMELEN
72characters in size.
73The
74.Fn ether_hostton
75function looks up the given host name and writes the associated
76Ethernet address into the structure passed.
77Both functions return
78zero if they find the requested host name or address, and \-1 if not.
79.Pp
80Each call reads
81.Pa /etc/ethers
82from the beginning.
83.Pp
84The
85.Fn ether_line
86function parses a line from the
87.Pa /etc/ethers
88file and fills in the passed
89.Vt struct ether_addr
90and character buffer with the Ethernet address and host name on the line.
91It returns zero if the line was successfully parsed and \-1 if not.
92The character buffer should be
93.Dv MAXHOSTNAMELEN
94characters in size.
95.Sh FILES
96.Bl -tag -width /etc/ethers -compact
97.It Pa /etc/ethers
98.El
99.Sh SEE ALSO
100.Xr ethers 5
101.Sh HISTORY
102The
103.Fn ether_ntoa ,
104.Fn ether_aton ,
105.Fn ether_ntohost ,
106.Fn ether_hostton ,
107and
108.Fn ether_line
109functions were adopted from SunOS and appeared in
110.Nx 0.9b .
111.Sh BUGS
112The data space used by these functions is static; if future use
113requires the data, it should be copied before any subsequent calls to
114these functions overwrite it.
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c
deleted file mode 100644
index d62be1ca71..0000000000
--- a/src/lib/libc/net/ethers.c
+++ /dev/null
@@ -1,183 +0,0 @@
1/* $OpenBSD: ethers.c,v 1.27 2019/01/25 00:19:25 millert Exp $ */
2
3/*
4 * Copyright (c) 1998 Todd C. Miller <millert@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/*
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 <millert@openbsd.org>
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 <paths.h>
31#include <errno.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <ctype.h>
36#include <limits.h>
37
38#ifndef _PATH_ETHERS
39#define _PATH_ETHERS "/etc/ethers"
40#endif
41
42static char * _ether_aton(const char *, struct ether_addr *);
43
44char *
45ether_ntoa(struct ether_addr *e)
46{
47 static char a[] = "xx:xx:xx:xx:xx:xx";
48
49 (void)snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
50 e->ether_addr_octet[0], e->ether_addr_octet[1],
51 e->ether_addr_octet[2], e->ether_addr_octet[3],
52 e->ether_addr_octet[4], e->ether_addr_octet[5]);
53
54 return (a);
55}
56
57static char *
58_ether_aton(const char *s, struct ether_addr *e)
59{
60 int i;
61 long l;
62 char *pp;
63
64 while (isspace((unsigned char)*s))
65 s++;
66
67 /* expect 6 hex octets separated by ':' or space/NUL if last octet */
68 for (i = 0; i < 6; i++) {
69 l = strtol(s, &pp, 16);
70 if (pp == s || l > 0xFF || l < 0)
71 return (NULL);
72 if (!(*pp == ':' ||
73 (i == 5 && (isspace((unsigned char)*pp) ||
74 *pp == '\0'))))
75 return (NULL);
76 e->ether_addr_octet[i] = (u_char)l;
77 s = pp + 1;
78 }
79
80 /* return character after the octets ala strtol(3) */
81 return (pp);
82}
83
84struct ether_addr *
85ether_aton(const char *s)
86{
87 static struct ether_addr n;
88
89 return (_ether_aton(s, &n) ? &n : NULL);
90}
91
92int
93ether_ntohost(char *hostname, struct ether_addr *e)
94{
95 FILE *f;
96 char buf[BUFSIZ+1], *p;
97 size_t len;
98 struct ether_addr try;
99
100 f = fopen(_PATH_ETHERS, "re");
101 if (f == NULL)
102 return (-1);
103 while ((p = fgetln(f, &len)) != NULL) {
104 if (p[len-1] == '\n')
105 len--;
106 if (len > sizeof(buf) - 2)
107 continue;
108 (void)memcpy(buf, p, len);
109 buf[len] = '\n'; /* code assumes newlines later on */
110 buf[len+1] = '\0';
111 /* A + in the file meant try YP, ignore it. */
112 if (!strncmp(buf, "+\n", sizeof(buf)))
113 continue;
114 if (ether_line(buf, &try, hostname) == 0 &&
115 memcmp(&try, e, sizeof(try)) == 0) {
116 (void)fclose(f);
117 return (0);
118 }
119 }
120 (void)fclose(f);
121 errno = ENOENT;
122 return (-1);
123}
124
125int
126ether_hostton(const char *hostname, struct ether_addr *e)
127{
128 FILE *f;
129 char buf[BUFSIZ+1], *p;
130 char try[HOST_NAME_MAX+1];
131 size_t len;
132
133 f = fopen(_PATH_ETHERS, "re");
134 if (f==NULL)
135 return (-1);
136
137 while ((p = fgetln(f, &len)) != NULL) {
138 if (p[len-1] == '\n')
139 len--;
140 if (len > sizeof(buf) - 2)
141 continue;
142 memcpy(buf, p, len);
143 buf[len] = '\n'; /* code assumes newlines later on */
144 buf[len+1] = '\0';
145 /* A + in the file meant try YP, ignore it. */
146 if (!strncmp(buf, "+\n", sizeof(buf)))
147 continue;
148 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
149 (void)fclose(f);
150 return (0);
151 }
152 }
153 (void)fclose(f);
154 errno = ENOENT;
155 return (-1);
156}
157
158int
159ether_line(const char *line, struct ether_addr *e, char *hostname)
160{
161 char *p;
162 size_t n;
163
164 /* Parse "xx:xx:xx:xx:xx:xx" */
165 if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t'))
166 goto bad;
167
168 /* Now get the hostname */
169 while (isspace((unsigned char)*p))
170 p++;
171 if (*p == '\0')
172 goto bad;
173 n = strcspn(p, " \t\n");
174 if (n >= HOST_NAME_MAX+1)
175 goto bad;
176 strlcpy(hostname, p, n + 1);
177 return (0);
178
179bad:
180 errno = EINVAL;
181 return (-1);
182}
183DEF_WEAK(ether_line);
diff --git a/src/lib/libc/net/freeaddrinfo.c b/src/lib/libc/net/freeaddrinfo.c
deleted file mode 100644
index 154f70cd75..0000000000
--- a/src/lib/libc/net/freeaddrinfo.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: freeaddrinfo.c,v 1.9 2016/09/21 04:38:56 guenther 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
38void
39freeaddrinfo(struct addrinfo *ai)
40{
41 struct addrinfo *p;
42
43 do {
44 p = ai;
45 ai = ai->ai_next;
46 free(p->ai_canonname);
47 free(p);
48 } while (ai);
49}
50DEF_WEAK(freeaddrinfo);
diff --git a/src/lib/libc/net/gai_strerror.3 b/src/lib/libc/net/gai_strerror.3
deleted file mode 100644
index d271f492c5..0000000000
--- a/src/lib/libc/net/gai_strerror.3
+++ /dev/null
@@ -1,95 +0,0 @@
1.\" $OpenBSD: gai_strerror.3,v 1.10 2017/05/03 01:58:33 deraadt 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: May 3 2017 $
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
32The
33.Fn gai_strerror
34function returns an error message string corresponding to the error code
35returned by
36.Xr getaddrinfo 3
37or
38.Xr getnameinfo 3 .
39.Pp
40The 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
45address family for
46.Fa name
47not supported
48.It Dv EAI_AGAIN
49temporary failure in name resolution
50.It Dv EAI_BADFLAGS
51invalid value for
52.Fa ai_flags
53.It Dv EAI_BADHINTS
54invalid value for
55.Fa hints
56.It Dv EAI_FAIL
57non-recoverable failure in name resolution
58.It Dv EAI_FAMILY
59.Fa ai_family
60not supported
61.It Dv EAI_MEMORY
62memory allocation failure
63.It Dv EAI_NODATA
64no address associated with
65.Fa name
66.It Dv EAI_NONAME
67.Fa name
68or
69.Fa service
70not provided, or not known
71.It Dv EAI_OVERFLOW
72argument buffer overflow
73.It Dv EAI_PROTOCOL
74resolved protocol is unknown
75.It Dv EAI_SERVICE
76.Fa service
77not supported for
78.Fa ai_socktype
79.It Dv EAI_SOCKTYPE
80.Fa ai_socktype
81not supported
82.It Dv EAI_SYSTEM
83system error (returned in
84.Va errno )
85.El
86.Sh RETURN VALUES
87.Fn gai_strerror
88returns a pointer to the error message string corresponding to
89.Fa ecode .
90If
91.Fa ecode
92is 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
deleted file mode 100644
index 1e9b585029..0000000000
--- a/src/lib/libc/net/gai_strerror.c
+++ /dev/null
@@ -1,79 +0,0 @@
1/* $OpenBSD: gai_strerror.c,v 1.8 2015/09/14 07:38:38 guenther 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
41const char *
42gai_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}
79DEF_WEAK(gai_strerror);
diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3
deleted file mode 100644
index 780c7a409f..0000000000
--- a/src/lib/libc/net/getaddrinfo.3
+++ /dev/null
@@ -1,477 +0,0 @@
1.\" $OpenBSD: getaddrinfo.3,v 1.61 2022/09/11 06:38:10 jmc 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: September 11 2022 $
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
36The
37.Fn getaddrinfo
38function is used to get a list of
39.Tn IP
40addresses and port numbers for host
41.Fa hostname
42and service
43.Fa servname .
44It is a replacement for and provides more flexibility than the
45.Xr gethostbyname 3
46and
47.Xr getservbyname 3
48functions.
49.Pp
50The
51.Fa hostname
52and
53.Fa servname
54arguments are either pointers to NUL-terminated strings or the null pointer.
55An acceptable value for
56.Fa hostname
57is either a valid host name or a numeric host address string consisting
58of a dotted decimal IPv4 address or an IPv6 address.
59The
60.Fa servname
61is either a decimal port number or a service name listed in
62.Xr services 5 .
63At least one of
64.Fa hostname
65and
66.Fa servname
67must be non-null.
68.Pp
69.Fa hints
70is an optional pointer to a
71.Vt struct addrinfo ,
72as defined by
73.In netdb.h :
74.Bd -literal
75struct addrinfo {
76 int ai_flags; /* input flags */
77 int ai_family; /* address 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
87This structure can be used to provide hints concerning the type of socket
88that the caller supports or wishes to use.
89The caller can supply the following structure elements in
90.Fa hints :
91.Bl -tag -width "ai_socktypeXX"
92.It Fa ai_family
93The address family that should be used.
94When
95.Fa ai_family
96is set to
97.Dv AF_UNSPEC ,
98it means the caller will accept any address family supported by the
99operating system.
100.It Fa ai_socktype
101Denotes the type of socket that is wanted:
102.Dv SOCK_STREAM ,
103.Dv SOCK_DGRAM ,
104or
105.Dv SOCK_RAW .
106When
107.Fa ai_socktype
108is zero, the caller will accept any socket type.
109.It Fa ai_protocol
110Indicates which transport protocol is desired,
111.Dv IPPROTO_UDP
112or
113.Dv IPPROTO_TCP .
114If
115.Fa ai_protocol
116is zero, the caller will accept any protocol.
117.It Fa ai_flags
118.Fa ai_flags
119is formed by
120.Tn OR Ns 'ing
121the following values:
122.Bl -tag -width "AI_CANONNAMEXX"
123.It Dv AI_ADDRCONFIG
124If the
125.Dv AI_ADDRCONFIG
126bit is set, IPv4 addresses will be returned only if an IPv4 address is
127configured on an interface, and IPv6 addresses will be returned only if an IPv6
128address is configured on an interface.
129Addresses on a loopback interface and link-local IPv6 addresses are not
130considered valid as configured addresses.
131This bit is only considered when determining whether a DNS query should
132be performed or not.
133.It Dv AI_CANONNAME
134If the
135.Dv AI_CANONNAME
136bit is set, a successful call to
137.Fn getaddrinfo
138will return a NUL-terminated string containing the canonical name
139of the specified host name in the
140.Fa ai_canonname
141element of the first
142.Vt addrinfo
143structure returned.
144.It Dv AI_FQDN
145If the
146.Dv AI_FQDN
147bit is set, a successful call to
148.Fn getaddrinfo
149will return a NUL-terminated string containing the fully qualified domain name
150of the specified host name in the
151.Fa ai_canonname
152element of the first
153.Vt addrinfo
154structure returned.
155.Pp
156This is different from the
157.Dv AI_CANONNAME
158bit flag that returns the canonical name registered in DNS,
159which may be different from the fully qualified domain name
160that the host name resolved to.
161Only one of the
162.Dv AI_FQDN
163and
164.Dv AI_CANONNAME
165bits can be set.
166.It Dv AI_NUMERICHOST
167If the
168.Dv AI_NUMERICHOST
169bit is set, it indicates that
170.Fa hostname
171should be treated as a numeric string defining an IPv4 or IPv6 address
172and no name resolution should be attempted.
173.It Dv AI_NUMERICSERV
174If the
175.Dv AI_NUMERICSERV
176bit is set, it indicates that
177.Fa servname
178should be treated as a numeric port string
179and no service name resolution should be attempted.
180.It Dv AI_PASSIVE
181If the
182.Dv AI_PASSIVE
183bit is set, it indicates that the returned socket address structure
184is intended for use in a call to
185.Xr bind 2 .
186In this case, if the
187.Fa hostname
188argument is the null pointer, then the IP address portion of the
189socket address structure will be set to
190.Dv INADDR_ANY
191for an IPv4 address or
192.Dv IN6ADDR_ANY_INIT
193for an IPv6 address.
194.Pp
195If the
196.Dv AI_PASSIVE
197bit is not set, the returned socket address structure will be ready
198for use in a call to
199.Xr connect 2
200for a connection-oriented protocol or
201.Xr connect 2 ,
202.Xr sendto 2 ,
203or
204.Xr sendmsg 2
205if a connectionless protocol was chosen.
206The
207.Tn IP
208address portion of the socket address structure will be set to the
209loopback address if
210.Fa hostname
211is the null pointer and
212.Dv AI_PASSIVE
213is not set.
214.El
215.El
216.Pp
217All other elements of the
218.Vt addrinfo
219structure passed via
220.Fa hints
221must be zero or the null pointer.
222.Pp
223If
224.Fa hints
225is the null pointer,
226.Fn getaddrinfo
227behaves as if the caller provided a
228.Vt struct addrinfo
229with
230.Fa ai_family
231set to
232.Dv AF_UNSPEC ,
233.Fa ai_flags
234set to
235.Dv AI_ADDRCONFIG ,
236and all other elements set to zero or
237.Dv NULL .
238.Pp
239After a successful call to
240.Fn getaddrinfo ,
241.Fa *res
242is a pointer to a linked list of one or more
243.Vt addrinfo
244structures.
245The list can be traversed by following the
246.Fa ai_next
247pointer in each
248.Vt addrinfo
249structure until a null pointer is encountered.
250The three members
251.Fa ai_family ,
252.Fa ai_socktype ,
253and
254.Fa ai_protocol
255in each returned
256.Vt addrinfo
257structure are suitable for a call to
258.Xr socket 2 .
259For each
260.Vt addrinfo
261structure in the list, the
262.Fa ai_addr
263member points to a filled-in socket address structure of length
264.Fa ai_addrlen .
265.Pp
266This implementation of
267.Fn getaddrinfo
268allows numeric IPv6 address notation with scope identifier,
269as documented in RFC 4007.
270By appending the percent character and scope identifier to addresses,
271one can fill the
272.Li sin6_scope_id
273field for addresses.
274This would make management of scoped addresses easier
275and allows cut-and-paste input of scoped addresses.
276.Pp
277At this moment the code supports only link-local addresses with the format.
278The scope identifier is hardcoded to the name of the hardware interface
279associated
280with the link
281.Po
282such as
283.Li ne0
284.Pc .
285An example is
286.Dq Li fe80::1%ne0 ,
287which means
288.Do
289.Li fe80::1
290on the link associated with the
291.Li ne0
292interface
293.Dc .
294.Pp
295The current implementation assumes a one-to-one relationship between
296the interface and link, which is not necessarily true from the specification.
297.Pp
298All of the information returned by
299.Fn getaddrinfo
300is dynamically allocated: the
301.Vt addrinfo
302structures themselves as well as the socket address structures and
303the canonical host name strings included in the
304.Vt addrinfo
305structures.
306.Pp
307Memory allocated for the dynamically allocated structures created by
308a successful call to
309.Fn getaddrinfo
310is released by the
311.Fn freeaddrinfo
312function.
313The
314.Fa ai
315pointer should be an
316.Vt addrinfo
317structure created by a call to
318.Fn getaddrinfo .
319.Sh RETURN VALUES
320.Fn getaddrinfo
321returns zero on success or one of the error codes listed in
322.Xr gai_strerror 3
323if an error occurs.
324If an error occurs, no memory is allocated by
325.Fn getaddrinfo ,
326therefore it is not necessary to release the
327.Vt addrinfo
328structure(s).
329.Sh EXAMPLES
330The following code tries to connect to
331.Dq Li www.kame.net
332service
333.Dq Li www
334via a stream socket.
335It loops through all the addresses available, regardless of address family.
336If the destination resolves to an IPv4 address, it will use an
337.Dv AF_INET
338socket.
339Similarly, if it resolves to IPv6, an
340.Dv AF_INET6
341socket is used.
342Observe that there is no hardcoded reference to a particular address family.
343The code works even if
344.Fn getaddrinfo
345returns addresses that are not IPv4/v6.
346.Bd -literal -offset indent
347struct addrinfo hints, *res, *res0;
348int error;
349int save_errno;
350int s;
351const char *cause = NULL;
352
353memset(&hints, 0, sizeof(hints));
354hints.ai_family = AF_UNSPEC;
355hints.ai_socktype = SOCK_STREAM;
356error = getaddrinfo("www.kame.net", "www", &hints, &res0);
357if (error)
358 errx(1, "%s", gai_strerror(error));
359s = -1;
360for (res = res0; res; res = res->ai_next) {
361 s = socket(res->ai_family, res->ai_socktype,
362 res->ai_protocol);
363 if (s == -1) {
364 cause = "socket";
365 continue;
366 }
367
368 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
369 cause = "connect";
370 save_errno = errno;
371 close(s);
372 errno = save_errno;
373 s = -1;
374 continue;
375 }
376
377 break; /* okay we got one */
378}
379if (s == -1)
380 err(1, "%s", cause);
381freeaddrinfo(res0);
382.Ed
383.Pp
384The following example tries to open a wildcard listening socket onto service
385.Dq Li www ,
386for all the address families available.
387.Bd -literal -offset indent
388struct addrinfo hints, *res, *res0;
389int error;
390int save_errno;
391int s[MAXSOCK];
392int nsock;
393const char *cause = NULL;
394
395memset(&hints, 0, sizeof(hints));
396hints.ai_family = AF_UNSPEC;
397hints.ai_socktype = SOCK_STREAM;
398hints.ai_flags = AI_PASSIVE;
399error = getaddrinfo(NULL, "www", &hints, &res0);
400if (error)
401 errx(1, "%s", gai_strerror(error));
402nsock = 0;
403for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {
404 s[nsock] = socket(res->ai_family, res->ai_socktype,
405 res->ai_protocol);
406 if (s[nsock] == -1) {
407 cause = "socket";
408 continue;
409 }
410
411 if (bind(s[nsock], res->ai_addr, res->ai_addrlen) == -1) {
412 cause = "bind";
413 save_errno = errno;
414 close(s[nsock]);
415 errno = save_errno;
416 continue;
417 }
418 (void) listen(s[nsock], 5);
419
420 nsock++;
421}
422if (nsock == 0)
423 err(1, "%s", cause);
424freeaddrinfo(res0);
425.Ed
426.Sh SEE ALSO
427.Xr bind 2 ,
428.Xr connect 2 ,
429.Xr send 2 ,
430.Xr socket 2 ,
431.Xr gai_strerror 3 ,
432.Xr gethostbyname 3 ,
433.Xr getnameinfo 3 ,
434.Xr getservbyname 3 ,
435.Xr res_init 3 ,
436.Xr hosts 5 ,
437.Xr resolv.conf 5 ,
438.Xr services 5 ,
439.Xr hostname 7
440.Rs
441.%A Craig Metz
442.%B Proceedings of the Freenix Track: 2000 USENIX Annual Technical Conference
443.%D June 2000
444.%T Protocol Independence Using the Sockets API
445.Re
446.Sh STANDARDS
447The
448.Fn getaddrinfo
449function is defined by the
450.St -p1003.1g-2000
451draft specification and documented in RFC 3493.
452.Pp
453The
454.Dv AI_FQDN
455flag bit first appeared in Windows 7.
456.Pp
457.Rs
458.%A R. Gilligan
459.%A S. Thomson
460.%A J. Bound
461.%A J. McCann
462.%A W. Stevens
463.%D February 2003
464.%R RFC 3493
465.%T Basic Socket Interface Extensions for IPv6
466.Re
467.Pp
468.Rs
469.%A S. Deering
470.%A B. Haberman
471.%A T. Jinmei
472.%A E. Nordmark
473.%A B. Zill
474.%D March 2005
475.%R RFC 4007
476.%T IPv6 Scoped Address Architecture
477.Re
diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3
deleted file mode 100644
index 06170c3c1a..0000000000
--- a/src/lib/libc/net/gethostbyname.3
+++ /dev/null
@@ -1,271 +0,0 @@
1.\" $OpenBSD: gethostbyname.3,v 1.34 2019/08/30 20:20:50 jmc 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: August 30 2019 $
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 gethostent void
50.Ft void
51.Fn sethostent "int stayopen"
52.Ft void
53.Fn endhostent void
54.Ft void
55.Fn herror "const char *string"
56.Ft const char *
57.Fn hstrerror "int err"
58.In sys/socket.h
59.In netdb.h
60.Ft struct hostent *
61.Fn gethostbyname2 "const char *name" "int af"
62.Ft struct hostent *
63.Fn gethostbyaddr "const void *addr" "socklen_t len" "int af"
64.Sh DESCRIPTION
65The
66.Fn gethostbyname ,
67.Fn gethostbyname2 ,
68and
69.Fn gethostbyaddr
70functions each return a pointer to an object with the following structure
71describing an Internet host referenced by
72.Fa name
73or
74.Fa addr ,
75respectively.
76This structure contains either information obtained from a name server,
77broken-out fields from a line in
78.Pa /etc/hosts ,
79or database entries supplied by the
80.Xr yp 8
81system.
82.Xr resolv.conf 5
83describes how the particular database is chosen.
84.Bd -literal -offset indent
85struct hostent {
86 char *h_name; /* official name of host */
87 char **h_aliases; /* alias list */
88 int h_addrtype; /* host address type */
89 int h_length; /* length of address */
90 char **h_addr_list; /* list of returned addresses */
91};
92#define h_addr h_addr_list[0] /* address, for backward compat */
93.Ed
94.Pp
95The members of this structure are:
96.Bl -tag -width h_addr_list
97.It Fa h_name
98Official name of the host.
99.It Fa h_aliases
100A
101.Dv NULL Ns -terminated
102array of alternate names for the host.
103.It Fa h_addrtype
104The type of address being returned.
105.It Fa h_length
106The length, in bytes, of the address.
107.It Fa h_addr_list
108A
109.Dv NULL Ns -terminated
110array of network addresses for the host.
111Host addresses are returned in network byte order.
112.It Fa h_addr
113The first address in
114.Fa h_addr_list ;
115this is for backward compatibility.
116.El
117.Pp
118The function
119.Fn gethostbyname
120will search for the named host in the current domain and its parents
121using the search lookup semantics detailed in
122.Xr resolv.conf 5
123and
124.Xr hostname 7 .
125.Pp
126.Fn gethostbyname2
127is similar to
128.Fn gethostbyname
129except that it supports an
130.Fa af
131of
132.Dv AF_INET6
133in addition to
134.Dv AF_INET .
135.Pp
136The
137.Fn gethostbyaddr
138function will search for the specified address of length
139.Fa len
140in the address family
141.Fa af .
142The only address family supported is
143.Dv AF_INET .
144.Pp
145The
146.Fn sethostent ,
147.Fn gethostent ,
148and
149.Fn endhostent
150functions are deprecated and no longer have any effect.
151They could be used in the past for queries over a persistent TCP
152connection or to iterate entries in the
153.Xr hosts 5
154file.
155.Pp
156The
157.Fn herror
158function prints an error message describing the failure.
159If its argument
160.Fa string
161is not
162.Dv NULL ,
163it is prepended to the message string and separated from it by a colon
164.Pq Ql \&:
165and a space.
166The error message is printed with a trailing newline.
167The contents of the error message is the same as that returned by
168.Fn hstrerror
169with argument
170.Va h_errno .
171.Sh ENVIRONMENT
172.Bl -tag -width RES_OPTIONS
173.It Ev RES_OPTIONS
174A list of options to override the resolver's internal defaults.
175See
176.Xr resolv.conf 5
177for more information.
178.El
179.Sh FILES
180.Bl -tag -width /etc/resolv.conf -compact
181.It Pa /etc/hosts
182.It Pa /etc/resolv.conf
183.El
184.Sh DIAGNOSTICS
185Error return status from
186.Fn gethostbyname ,
187.Fn gethostbyname2 ,
188and
189.Fn gethostbyaddr
190is indicated by return of a
191.Dv NULL
192pointer.
193The external integer
194.Va h_errno
195may then be checked to see whether this is a temporary failure
196or an invalid or unknown host.
197.Pp
198The variable
199.Va h_errno
200can have the following values:
201.Bl -tag -width HOST_NOT_FOUND
202.It Dv HOST_NOT_FOUND
203No such host is known.
204.It Dv TRY_AGAIN
205This is usually a temporary error
206and means that the local server did not receive
207a response from an authoritative server.
208A retry at some later time may succeed.
209.It Dv NO_RECOVERY
210Some unexpected server failure was encountered.
211This is a non-recoverable error.
212.It Dv NO_DATA
213The requested name is valid but does not have an IP address;
214this is not a temporary error.
215This means that the name is known to the name server but there is no address
216associated with this name.
217Another type of request to the name server using this domain name
218will result in an answer;
219for example, a mail-forwarder may be registered for this domain.
220.It Dv NETDB_INTERNAL
221An internal error occurred.
222This may occur when an address family other than
223.Dv AF_INET
224or
225.Dv AF_INET6
226is specified or when a resource is unable to be allocated.
227It is always set by
228.Fn gethostent .
229.It Dv NETDB_SUCCESS
230The function completed successfully.
231.El
232.Sh SEE ALSO
233.Xr getaddrinfo 3 ,
234.Xr getnameinfo 3 ,
235.Xr res_init 3 ,
236.Xr hosts 5 ,
237.Xr resolv.conf 5 ,
238.Xr hostname 7
239.Sh HISTORY
240The
241.Fn endhostent ,
242.Fn gethostbyaddr ,
243.Fn gethostbyname ,
244.Fn gethostent ,
245and
246.Fn sethostent
247functions appeared in
248.Bx 4.1c .
249The function
250.Fn herror
251was added in
252.Bx 4.3 Tahoe ,
253.Fn hstrerror
254in
255.Bx 4.4 ,
256and
257.Fn gethostbyname2
258in
259.Ox 2.1 .
260.Sh BUGS
261These functions use static data storage;
262if the data is needed for future use, it should be
263copied before any subsequent calls overwrite it.
264.Pp
265Only the Internet
266address formats are currently understood.
267.Pp
268YP does not support any address families other than
269.Dv AF_INET
270and uses
271the traditional database format.
diff --git a/src/lib/libc/net/getifaddrs.3 b/src/lib/libc/net/getifaddrs.3
deleted file mode 100644
index 8ea08fa3da..0000000000
--- a/src/lib/libc/net/getifaddrs.3
+++ /dev/null
@@ -1,157 +0,0 @@
1.\" $OpenBSD: getifaddrs.3,v 1.23 2022/03/29 18:15:52 naddy 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: March 29 2022 $
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
40The
41.Fn getifaddrs
42function stores a reference to a linked list of the network interfaces
43on the local machine in the memory referenced by
44.Fa ifap .
45The list consists of
46.Vt ifaddrs
47structures, as defined in the include file
48.In ifaddrs.h .
49The
50.Vt ifaddrs
51structure 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
64Contains a pointer to the next structure on the list.
65This field is set to
66.Dv NULL
67in the last structure on the list.
68.It Fa ifa_name
69Contains the interface name.
70.It Fa ifa_flags
71Contains the interface flags, as set by
72.Xr ifconfig 8 .
73.It Fa ifa_addr
74References either the address of the interface or the link level
75address of the interface, if one exists, otherwise it is
76.Dv NULL .
77(The
78.Fa sa_family
79field of the
80.Fa ifa_addr
81field should be consulted to determine the format of the
82.Fa ifa_addr
83address.)
84.It Fa ifa_netmask
85References the netmask associated with
86.Fa ifa_addr ,
87if one is set, otherwise it is
88.Dv NULL .
89.It Fa ifa_broadaddr
90This field, which should only be referenced for non-P2P interfaces,
91references the broadcast address associated with
92.Fa ifa_addr ,
93if one exists, otherwise it is
94.Dv NULL .
95.It Fa ifa_dstaddr
96This field, which should only be referenced for P2P interfaces,
97references the destination address on a P2P interface,
98if one exists, otherwise it is
99.Dv NULL .
100.It Fa ifa_data
101References address family specific data.
102For
103.Dv AF_LINK
104addresses it contains a pointer to the
105.Vt struct if_data
106(as defined in include file
107.In net/if.h )
108which contains various interface attributes and statistics.
109For all other address families,
110.Fa ifa_data
111is
112.Dv NULL .
113.El
114.Pp
115The data returned by
116.Fn getifaddrs
117is dynamically allocated and should be freed using
118.Fn freeifaddrs
119when no longer needed.
120.Sh RETURN VALUES
121.Rv -std
122.Sh ERRORS
123The
124.Fn getifaddrs
125function may fail and set
126.Va errno
127for any of the errors specified for the library routines
128.Xr ioctl 2 ,
129.Xr socket 2 ,
130.Xr malloc 3 ,
131or
132.Xr sysctl 2 .
133.Sh SEE ALSO
134.Xr ioctl 2 ,
135.Xr socket 2 ,
136.Xr sysctl 2 ,
137.Xr netintro 4 ,
138.Xr ifconfig 8
139.Sh HISTORY
140The
141.Fn getifaddrs
142function first appeared in BSDI
143.Bsx .
144The function has been available on
145.Ox
146since
147.Ox 2.7 .
148.Sh BUGS
149If both
150.In net/if.h
151and
152.In ifaddrs.h
153are being included,
154.In net/if.h
155.Em must
156be included before
157.In ifaddrs.h .
diff --git a/src/lib/libc/net/getifaddrs.c b/src/lib/libc/net/getifaddrs.c
deleted file mode 100644
index 069ee9afab..0000000000
--- a/src/lib/libc/net/getifaddrs.c
+++ /dev/null
@@ -1,298 +0,0 @@
1/* $OpenBSD: getifaddrs.c,v 1.14 2021/11/29 03:20:37 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/param.h> /* ALIGN ALIGNBYTES */
29#include <sys/types.h>
30#include <sys/ioctl.h>
31#include <sys/socket.h>
32#include <net/if.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
47int
48getifaddrs(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}
291DEF_WEAK(getifaddrs);
292
293void
294freeifaddrs(struct ifaddrs *ifp)
295{
296 free(ifp);
297}
298DEF_WEAK(freeifaddrs);
diff --git a/src/lib/libc/net/getnameinfo.3 b/src/lib/libc/net/getnameinfo.3
deleted file mode 100644
index 4e97b5d07b..0000000000
--- a/src/lib/libc/net/getnameinfo.3
+++ /dev/null
@@ -1,264 +0,0 @@
1.\" $OpenBSD: getnameinfo.3,v 1.49 2022/09/11 06:38:10 jmc 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: September 11 2022 $
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
33The
34.Fn getnameinfo
35function is used to convert a
36.Vt sockaddr
37structure to a pair of host name and service strings.
38It is a replacement for and provides more flexibility than the
39.Xr gethostbyaddr 3
40and
41.Xr getservbyport 3
42functions and is the converse of the
43.Xr getaddrinfo 3
44function.
45.Pp
46The
47.Vt sockaddr
48structure
49.Fa sa
50should point to either a
51.Vt sockaddr_in
52or
53.Vt sockaddr_in6
54structure (for IPv4 or IPv6 respectively) that is
55.Fa salen
56bytes long.
57.Pp
58The host and service names associated with
59.Fa sa
60are stored in
61.Fa host
62and
63.Fa serv
64which have length parameters
65.Fa hostlen
66and
67.Fa servlen .
68The maximum value for
69.Fa hostlen
70is
71.Dv NI_MAXHOST
72and
73the maximum value for
74.Fa servlen
75is
76.Dv NI_MAXSERV ,
77as defined by
78.In netdb.h .
79If a length parameter is zero, no string will be stored.
80Otherwise, enough space must be provided to store the
81host name or service string plus a byte for the NUL terminator.
82.Pp
83The
84.Fa flags
85argument is formed by
86.Tn OR Ns 'ing
87the following values:
88.Bl -tag -width "NI_NUMERICHOSTXX"
89.It Dv NI_NOFQDN
90A fully qualified domain name is not required for local hosts.
91The local part of the fully qualified domain name is returned instead.
92.It Dv NI_NUMERICHOST
93Return the address in numeric form, as if calling
94.Xr inet_ntop 3 ,
95instead of a host name.
96.It Dv NI_NAMEREQD
97A name is required.
98If the host name cannot be found in DNS and this flag is set,
99a non-zero error code is returned.
100If the host name is not found and the flag is not set, the
101address is returned in numeric form.
102.It NI_NUMERICSERV
103The service name is returned as a digit string representing the port number.
104.It NI_DGRAM
105Specifies that the service being looked up is a datagram
106service, and causes
107.Xr getservbyport 3
108to be called with a second argument of
109.Dq udp
110instead of its default of
111.Dq tcp .
112This is required for the few ports (512\-514) that have different services
113for
114.Tn UDP
115and
116.Tn TCP .
117.El
118.Pp
119This implementation allows numeric IPv6 address notation with scope identifier,
120as documented in RFC 4007.
121IPv6 link-local address will appear as a string like
122.Dq Li fe80::1%ne0 .
123Refer to
124.Xr getaddrinfo 3
125for more information.
126.Sh RETURN VALUES
127.Fn getnameinfo
128returns zero on success or one of the error codes listed in
129.Xr gai_strerror 3
130if an error occurs.
131.Sh EXAMPLES
132The following code tries to get a numeric host name, and service name,
133for a given socket address.
134Observe that there is no hardcoded reference to a particular address family.
135.Bd -literal -offset indent
136struct sockaddr *sa; /* input */
137char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
138
139if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
140 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV))
141 errx(1, "could not get numeric hostname");
142printf("host=%s, serv=%s\en", hbuf, sbuf);
143.Ed
144.Pp
145The following version checks if the socket address has a reverse address mapping:
146.Bd -literal -offset indent
147struct sockaddr *sa; /* input */
148char hbuf[NI_MAXHOST];
149
150if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
151 NI_NAMEREQD))
152 errx(1, "could not resolve hostname");
153printf("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 res_init 3 ,
162.Xr hosts 5 ,
163.Xr resolv.conf 5 ,
164.Xr services 5 ,
165.Xr hostname 7
166.Rs
167.%A Craig Metz
168.%T Protocol Independence Using the Sockets API
169.%B Proceedings of the Freenix Track: 2000 USENIX Annual Technical Conference
170.%D June 2000
171.Re
172.Sh STANDARDS
173The
174.Fn getnameinfo
175function is defined by the
176.St -p1003.1g-2000
177draft specification and documented in RFC 3493.
178.Pp
179.Rs
180.%A R. Gilligan
181.%A S. Thomson
182.%A J. Bound
183.%A J. McCann
184.%A W. Stevens
185.%D February 2003
186.%R RFC 3493
187.%T Basic Socket Interface Extensions for IPv6
188.Re
189.Pp
190.Rs
191.%A S. Deering
192.%A B. Haberman
193.%A T. Jinmei
194.%A E. Nordmark
195.%A B. Zill
196.%D March 2005
197.%R RFC 4007
198.%T IPv6 Scoped Address Architecture
199.Re
200.Sh CAVEATS
201.Fn getnameinfo
202can return both numeric and FQDN forms of the address specified in
203.Fa sa .
204There is no return value that indicates whether the string returned in
205.Fa host
206is a result of binary to numeric-text translation (like
207.Xr inet_ntop 3 ) ,
208or is the result of a DNS reverse lookup.
209Because of this, malicious parties could set up a PTR record as follows:
210.Bd -literal -offset indent
2111.0.0.127.in-addr.arpa. IN PTR 10.1.1.1
212.Ed
213.Pp
214and trick the caller of
215.Fn getnameinfo
216into believing that
217.Fa sa
218is
219.Li 10.1.1.1
220when it is actually
221.Li 127.0.0.1 .
222.Pp
223To prevent such attacks, the use of
224.Dv NI_NAMEREQD
225is recommended when the result of
226.Fn getnameinfo
227is used
228for access control purposes:
229.Bd -literal -offset indent
230struct sockaddr *sa;
231char addr[NI_MAXHOST];
232struct addrinfo hints, *res;
233int error;
234
235error = getnameinfo(sa, sa->sa_len, addr, sizeof(addr),
236 NULL, 0, NI_NAMEREQD);
237if (error == 0) {
238 memset(&hints, 0, sizeof(hints));
239 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
240 hints.ai_flags = AI_NUMERICHOST;
241 if (getaddrinfo(addr, "0", &hints, &res) == 0) {
242 /* malicious PTR record */
243 freeaddrinfo(res);
244 printf("bogus PTR record\en");
245 return -1;
246 }
247 /* addr is FQDN as a result of PTR lookup */
248} else {
249 /* addr is numeric string */
250 error = getnameinfo(sa, sa->sa_len, addr, sizeof(addr),
251 NULL, 0, NI_NUMERICHOST);
252}
253.Ed
254.Sh BUGS
255The implementation of
256.Fn getnameinfo
257is not thread-safe.
258.Pp
259.Ox
260intentionally uses a different
261.Dv NI_MAXHOST
262value from what
263.Tn "RFC 2553"
264suggests, to avoid buffer length handling mistakes.
diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3
deleted file mode 100644
index da2c4ef4a3..0000000000
--- a/src/lib/libc/net/getnetent.3
+++ /dev/null
@@ -1,147 +0,0 @@
1.\" $OpenBSD: getnetent.3,v 1.19 2019/08/30 20:20:51 jmc 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: August 30 2019 $
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
53The
54.Fn getnetbyname
55and
56.Fn getnetbyaddr
57functions return a pointer to an object with the following structure:
58.Bd -literal -offset indent
59struct netent {
60 char *n_name; /* official name of net */
61 char **n_aliases; /* alias list */
62 int n_addrtype; /* net number type */
63 in_addr_t n_net; /* net number */
64};
65.Ed
66.Pp
67The members of this structure are:
68.Bl -tag -width n_addrtype
69.It Fa n_name
70The official name of the network.
71.It Fa n_aliases
72A null-terminated list of alternate names for the network.
73.It Fa n_addrtype
74The type of the network number returned; it is always
75.Dv AF_INET .
76.It Fa n_net
77The network number.
78Network numbers are returned in machine byte order.
79.El
80.Pp
81On
82.Ox ,
83these legacy functions perform a lookup in a similar fashion as
84.Xr gethostbyname 3
85and
86.Xr gethostbyaddr 3 ,
87respectively.
88On other systems, they may use a separate network database file,
89.Pa /etc/networks .
90.Pp
91In contrast to
92.Xr gethostbyaddr 3 ,
93the
94.Fa net
95argument is expected in machine byte order.
96.Pp
97The
98.Fn setnetent ,
99.Fn getnetent ,
100and
101.Fn endnetent
102functions are deprecated and no longer have any effect.
103They could be used in the past to iterate over entries in the former file
104.Pa /etc/networks .
105.Sh RETURN VALUES
106The
107.Fn getnetbyaddr
108and
109.Fn getnetbyname
110functions return
111.Dv NULL
112if the requested entry is not found.
113.Pp
114The
115.Fn getnetent
116function always returns
117.Dv NULL .
118.Sh FILES
119.Bl -tag -width /etc/hosts -compact
120.It Pa /etc/hosts
121The local host and network name database.
122.El
123.Sh SEE ALSO
124.Xr getaddrinfo 3 ,
125.Xr gethostbyname 3 ,
126.Xr getnameinfo 3 ,
127.Xr res_init 3 ,
128.Xr hosts 5
129.Sh STANDARDS
130These functions conform to
131.St -p1003.1-2008 .
132.Sh HISTORY
133The
134.Fn getnetent ,
135.Fn getnetbyaddr ,
136.Fn getnetbyname ,
137.Fn setnetent ,
138and
139.Fn endnetent
140functions appeared in
141.Bx 4.2 .
142.Sh BUGS
143The data space used by these functions is static; if future use
144requires the data, it should be copied before any subsequent calls
145to these functions overwrite it.
146Only Internet network numbers are currently understood.
147Expecting 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
deleted file mode 100644
index b93e442fa9..0000000000
--- a/src/lib/libc/net/getnetent.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* $OpenBSD: getnetent.c,v 1.18 2018/04/28 15:05:40 schwarze 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 <stddef.h>
33
34void
35setnetent(int f)
36{
37}
38
39void
40endnetent(void)
41{
42}
43
44struct netent *
45getnetent(void)
46{
47 h_errno = NETDB_INTERNAL;
48 return NULL;
49}
diff --git a/src/lib/libc/net/getpeereid.3 b/src/lib/libc/net/getpeereid.3
deleted file mode 100644
index 4475bbd77d..0000000000
--- a/src/lib/libc/net/getpeereid.3
+++ /dev/null
@@ -1,121 +0,0 @@
1.\" $OpenBSD: getpeereid.3,v 1.4 2022/09/11 06:38:10 jmc 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: September 11 2022 $
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
42returns the effective user ID and group ID of the peer connected to
43a
44.Ux Ns -domain
45socket (see
46.Xr unix 4 ) .
47The argument
48.Fa s
49must be of type
50.Dv SOCK_STREAM
51or
52.Dv SOCK_SEQPACKET .
53.Pp
54One common use is for
55.Ux Ns -domain
56servers to determine the credentials of clients that have connected to it.
57.Pp
58.Fn getpeereid
59takes three parameters:
60.Bl -bullet
61.It
62.Fa s
63contains the file descriptor of the socket whose peer credentials
64should be looked up.
65.It
66.Fa euid
67points to a
68.Vt uid_t
69variable into which the effective user ID for the connected peer will
70be stored.
71.It
72.Fa egid
73points to a
74.Vt gid_t
75variable into which the effective group ID for the connected peer will
76be stored.
77.El
78.Sh RETURN VALUES
79If the call succeeds, a 0 is returned and
80.Fa euid
81and
82.Fa egid
83are set to the effective user ID and group ID of the connected peer.
84Otherwise,
85.Va errno
86is set and a value of \-1 is returned.
87.Sh ERRORS
88On failure,
89.Va errno
90is set to one of the following:
91.Bl -tag -width Er
92.It Bq Er EBADF
93The argument
94.Fa s
95is not a valid descriptor.
96.It Bq Er ENOTSOCK
97The argument
98.Fa s
99is a file, not a socket.
100.It Bq Er EOPNOTSUPP
101The socket is not in the
102.Ux Ns -domain .
103.It Bq Er ENOTCONN
104The socket is not connected.
105.It Bq Er ENOBUFS
106Insufficient resources were available in the system
107to 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
118The
119.Fn getpeereid
120function call appeared in
121.Ox 3.0 .
diff --git a/src/lib/libc/net/getpeereid.c b/src/lib/libc/net/getpeereid.c
deleted file mode 100644
index 208e541f17..0000000000
--- a/src/lib/libc/net/getpeereid.c
+++ /dev/null
@@ -1,36 +0,0 @@
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
22int
23getpeereid(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
deleted file mode 100644
index 8e080679d0..0000000000
--- a/src/lib/libc/net/getproto.c
+++ /dev/null
@@ -1,60 +0,0 @@
1/* $OpenBSD: getproto.c,v 1.8 2015/09/14 07:38:38 guenther 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
34int
35getprotobynumber_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}
49DEF_WEAK(getprotobynumber_r);
50
51struct protoent *
52getprotobynumber(int num)
53{
54 extern struct protoent_data _protoent_data;
55 static struct protoent proto;
56
57 if (getprotobynumber_r(num, &proto, &_protoent_data) != 0)
58 return (NULL);
59 return (&proto);
60}
diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3
deleted file mode 100644
index cc2c69836a..0000000000
--- a/src/lib/libc/net/getprotoent.3
+++ /dev/null
@@ -1,213 +0,0 @@
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
68The
69.Fn getprotoent ,
70.Fn getprotobyname ,
71and
72.Fn getprotobynumber
73functions each return a pointer to an object with the following structure
74containing the broken-out fields of a line in the network protocol database,
75.Pa /etc/protocols .
76.Bd -literal -offset indent
77.Pp
78struct 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
85The members of this structure are:
86.Bl -tag -width p_aliases
87.It Fa p_name
88The official name of the protocol.
89.It Fa p_aliases
90A null-terminated list of alternate names for the protocol.
91.It Fa p_proto
92The protocol number.
93.El
94.Pp
95The
96.Fn getprotoent
97function reads the next line of the file, opening the file if necessary.
98.Pp
99The
100.Fn setprotoent
101function opens and rewinds the file.
102If the
103.Fa stayopen
104flag is non-zero,
105the protocol database will not be closed after each call to
106.Fn getprotobyname
107or
108.Fn getprotobynumber .
109.Pp
110The
111.Fn endprotoent
112function closes the file.
113.Pp
114The
115.Fn getprotobyname
116and
117.Fn getprotobynumber
118functions sequentially search from the beginning of the file until a
119matching protocol name or protocol number is found, or until
120.Dv EOF
121is encountered.
122.Pp
123The
124.Fn getprotoent_r ,
125.Fn getprotobyport_r ,
126.Fn getprotobyname_r ,
127.Fn setprotoent_r ,
128and
129.Fn endprotoent_r
130functions are reentrant versions of the above functions that take a
131pointer to a
132.Vt protoent_data
133structure which is used to store state information.
134The structure must be zero-filled before it is used
135and should be considered opaque for the sake of portability.
136.Pp
137The
138.Fn getprotoent_r ,
139.Fn getprotobyport_r ,
140and
141.Fn getprotobyname_r
142functions
143also take a pointer to a
144.Vt protoent
145structure which is used to store the results of the database lookup.
146.Sh RETURN VALUES
147The
148.Fn getprotoent ,
149.Fn getprotobyport ,
150and
151.Fn getprotobyname
152functions return a pointer to a
153.Vt protoent
154structure on success or a null pointer if end-of-file
155is reached or an error occurs.
156.Pp
157The
158.Fn getprotoent_r ,
159.Fn getprotobyport_r ,
160and
161.Fn getprotobyname_r
162functions return 0 on success or \-1 if end-of-file
163is 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
171The
172.Fn getprotoent ,
173.Fn getprotobynumber ,
174.Fn getprotobyname ,
175.Fn setprotoent ,
176and
177.Fn endprotoent
178functions conform to
179.St -p1003.1-2004 .
180.Pp
181The
182.Fn getprotoent_r ,
183.Fn getprotobyport_r ,
184.Fn getprotobyname_r ,
185.Fn setprotoent_r ,
186and
187.Fn endprotoent_r
188functions are not currently standardized.
189This implementation follows the API used by HP, IBM, and Digital.
190.Sh HISTORY
191The
192.Fn getprotoent ,
193.Fn getprotobynumber ,
194.Fn getprotobyname ,
195.Fn setprotoent ,
196and
197.Fn endprotoent
198functions appeared in
199.Bx 4.2 .
200.Pp
201The
202.Fn getprotoent_r ,
203.Fn getprotobyport_r ,
204.Fn getprotobyname_r ,
205.Fn setprotoent_r ,
206and
207.Fn endprotoent_r
208functions appeared in
209.Ox 3.7 .
210.Sh BUGS
211The non-reentrant functions use a static data space; if the data is needed
212for future use, it should be copied before any subsequent calls overwrite it.
213Only the Internet protocols are currently understood.
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c
deleted file mode 100644
index a218863d07..0000000000
--- a/src/lib/libc/net/getprotoent.c
+++ /dev/null
@@ -1,169 +0,0 @@
1/* $OpenBSD: getprotoent.c,v 1.13 2015/09/14 07:38:38 guenther 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
41void
42setprotoent_r(int f, struct protoent_data *pd)
43{
44 if (pd->fp == NULL)
45 pd->fp = fopen(_PATH_PROTOCOLS, "re" );
46 else
47 rewind(pd->fp);
48 pd->stayopen |= f;
49}
50DEF_WEAK(setprotoent_r);
51
52void
53endprotoent_r(struct protoent_data *pd)
54{
55 if (pd->fp) {
56 fclose(pd->fp);
57 pd->fp = NULL;
58 }
59 free(pd->aliases);
60 pd->aliases = NULL;
61 pd->maxaliases = 0;
62 free(pd->line);
63 pd->line = NULL;
64 pd->stayopen = 0;
65}
66DEF_WEAK(endprotoent_r);
67
68int
69getprotoent_r(struct protoent *pe, struct protoent_data *pd)
70{
71 char *p, *cp, **q, *endp;
72 size_t len;
73 long l;
74 int serrno;
75
76 if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "re" )) == NULL)
77 return (-1);
78again:
79 if ((p = fgetln(pd->fp, &len)) == NULL)
80 return (-1);
81 if (len == 0 || *p == '#' || *p == '\n')
82 goto again;
83 if (p[len-1] == '\n')
84 len--;
85 if ((cp = memchr(p, '#', len)) != NULL)
86 len = cp - p;
87 cp = realloc(pd->line, len + 1);
88 if (cp == NULL)
89 return (-1);
90 pd->line = pe->p_name = memcpy(cp, p, len);
91 cp[len] = '\0';
92 cp = strpbrk(cp, " \t");
93 if (cp == NULL)
94 goto again;
95 *cp++ = '\0';
96 while (*cp == ' ' || *cp == '\t')
97 cp++;
98 p = strpbrk(cp, " \t");
99 if (p != NULL)
100 *p++ = '\0';
101 l = strtol(cp, &endp, 10);
102 if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX)
103 goto again;
104 pe->p_proto = l;
105 if (pd->aliases == NULL) {
106 pd->maxaliases = 5;
107 pd->aliases = calloc(pd->maxaliases, sizeof(char *));
108 if (pd->aliases == NULL) {
109 serrno = errno;
110 endprotoent_r(pd);
111 errno = serrno;
112 return (-1);
113 }
114 }
115 q = pe->p_aliases = pd->aliases;
116 if (p != NULL) {
117 cp = p;
118 while (cp && *cp) {
119 if (*cp == ' ' || *cp == '\t') {
120 cp++;
121 continue;
122 }
123 if (q == &pe->p_aliases[pd->maxaliases - 1]) {
124 p = reallocarray(pe->p_aliases,
125 pd->maxaliases, 2 * sizeof(char *));
126 if (p == NULL) {
127 serrno = errno;
128 endprotoent_r(pd);
129 errno = serrno;
130 return (-1);
131 }
132 pd->maxaliases *= 2;
133 q = (char **)p + (q - pe->p_aliases);
134 pe->p_aliases = pd->aliases = (char **)p;
135 }
136 *q++ = cp;
137 cp = strpbrk(cp, " \t");
138 if (cp != NULL)
139 *cp++ = '\0';
140 }
141 }
142 *q = NULL;
143 return (0);
144}
145DEF_WEAK(getprotoent_r);
146
147struct protoent_data _protoent_data; /* shared with getproto{,name}.c */
148
149void
150setprotoent(int f)
151{
152 setprotoent_r(f, &_protoent_data);
153}
154
155void
156endprotoent(void)
157{
158 endprotoent_r(&_protoent_data);
159}
160
161struct protoent *
162getprotoent(void)
163{
164 static struct protoent proto;
165
166 if (getprotoent_r(&proto, &_protoent_data) != 0)
167 return (NULL);
168 return (&proto);
169}
diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c
deleted file mode 100644
index f6ed4c49b1..0000000000
--- a/src/lib/libc/net/getprotoname.c
+++ /dev/null
@@ -1,69 +0,0 @@
1/* $OpenBSD: getprotoname.c,v 1.8 2015/09/14 07:38:38 guenther 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
35int
36getprotobyname_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 }
50found:
51 if (!pd->stayopen && pd->fp != NULL) {
52 fclose(pd->fp);
53 pd->fp = NULL;
54 }
55 return (error);
56}
57DEF_WEAK(getprotobyname_r);
58
59struct protoent *
60getprotobyname(const char *name)
61{
62 extern struct protoent_data _protoent_data;
63 static struct protoent proto;
64
65 if (getprotobyname_r(name, &proto, &_protoent_data) != 0)
66 return (NULL);
67 return (&proto);
68}
69DEF_WEAK(getprotobyname);
diff --git a/src/lib/libc/net/getrrsetbyname.3 b/src/lib/libc/net/getrrsetbyname.3
deleted file mode 100644
index 314f31665c..0000000000
--- a/src/lib/libc/net/getrrsetbyname.3
+++ /dev/null
@@ -1,164 +0,0 @@
1.\" $OpenBSD: getrrsetbyname.3,v 1.22 2022/09/11 06:38:10 jmc 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: September 11 2022 $
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
34gets a set of resource records associated with a
35.Fa hostname ,
36.Fa rdclass ,
37and
38.Fa rdtype .
39.Fa hostname
40is a pointer to a NUL-terminated string.
41The
42.Fa flags
43field is currently unused and must be zero.
44.Pp
45After a successful call to
46.Fn getrrsetbyname ,
47.Fa *res
48is a pointer to an
49.Vt rrsetinfo
50structure, containing a list of one or more
51.Vt rdatainfo
52structures containing resource records and potentially another list of
53.Vt rdatainfo
54structures containing SIG resource records associated with those records.
55The members
56.Li rri_rdclass
57and
58.Li rri_rdtype
59are copied from the parameters.
60.Li rri_ttl
61and
62.Li rri_name
63are properties of the obtained rrset.
64The resource records contained in
65.Li rri_rdatas
66and
67.Li rri_sigs
68are in uncompressed DNS wire format.
69Properties of the rdataset are represented in the
70.Li rri_flags
71bitfield.
72If the
73.Dv RRSET_VALIDATED
74bit is set, the data has been DNSSEC
75validated and the signatures verified.
76.Pp
77The following structures are used:
78.Bd -literal -offset indent
79struct rdatainfo {
80 unsigned int rdi_length; /* length of data */
81 unsigned char *rdi_data; /* record data */
82};
83
84struct 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
97All of the information returned by
98.Fn getrrsetbyname
99is dynamically allocated: the
100.Vt rrsetinfo
101and
102.Vt rdatainfo
103structures,
104and the canonical host name strings pointed to by the
105.Vt rrsetinfo
106structure.
107Memory allocated for the dynamically allocated structures created by
108a successful call to
109.Fn getrrsetbyname
110is released by
111.Fn freerrset .
112.Li rrset
113is a pointer to a
114.Vt struct rrsetinfo
115created 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
124returns zero on success, and one of the following error
125codes if an error occurred:
126.Bl -tag -width ERRSET_NOMEMORY
127.It Bq Er ERRSET_NONAME
128The name does not exist.
129.It Bq Er ERRSET_NODATA
130The name exists, but does not have data of the desired type.
131.It Bq Er ERRSET_NOMEMORY
132Memory could not be allocated.
133.It Bq Er ERRSET_INVAL
134A parameter is invalid.
135.It Bq Er ERRSET_FAIL
136Other failure.
137.El
138.Sh SEE ALSO
139.Xr res_init 3 ,
140.Xr resolv.conf 5
141.Sh HISTORY
142.Fn getrrsetbyname
143first appeared in
144.Ox 3.0 .
145The API first appeared in ISC BIND version 9.
146.Sh AUTHORS
147.An Jakob Schlyter Aq Mt jakob@openbsd.org
148.Sh CAVEATS
149The
150.Dv RRSET_VALIDATED
151flag in
152.Li rri_flags
153is set if the AD (authenticated data) bit in the DNS answer is
154set.
155This flag
156.Em should not
157be trusted unless the transport between the nameserver and the resolver
158is secure (e.g. IPsec, trusted network, loopback communication).
159.Sh BUGS
160The data in
161.Li *rdi_data
162should be returned in uncompressed wire format.
163Currently, the data is in compressed format and the caller can't
164uncompress since it doesn't have the full message.
diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c
deleted file mode 100644
index 80c7e62a41..0000000000
--- a/src/lib/libc/net/getservbyname.c
+++ /dev/null
@@ -1,72 +0,0 @@
1/* $OpenBSD: getservbyname.c,v 1.11 2015/09/14 07:38:38 guenther 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
35int
36getservbyname_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;
50gotname:
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}
60DEF_WEAK(getservbyname_r);
61
62struct servent *
63getservbyname(const char *name, const char *proto)
64{
65 extern struct servent_data _servent_data;
66 static struct servent serv;
67
68 if (getservbyname_r(name, proto, &serv, &_servent_data) != 0)
69 return (NULL);
70 return (&serv);
71}
72DEF_WEAK(getservbyname);
diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c
deleted file mode 100644
index 3584fbea3a..0000000000
--- a/src/lib/libc/net/getservbyport.c
+++ /dev/null
@@ -1,65 +0,0 @@
1/* $OpenBSD: getservbyport.c,v 1.8 2015/09/14 07:38:38 guenther 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
35int
36getservbyport_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}
54DEF_WEAK(getservbyport_r);
55
56struct servent *
57getservbyport(int port, const char *proto)
58{
59 extern struct servent_data _servent_data;
60 static struct servent serv;
61
62 if (getservbyport_r(port, proto, &serv, &_servent_data) != 0)
63 return (NULL);
64 return (&serv);
65}
diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3
deleted file mode 100644
index 29dd3eb5f4..0000000000
--- a/src/lib/libc/net/getservent.3
+++ /dev/null
@@ -1,220 +0,0 @@
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
68The
69.Fn getservent ,
70.Fn getservbyname ,
71and
72.Fn getservbyport
73functions each return a pointer to an object with the following structure
74containing the broken-out fields of a line in the network services database,
75.Pa /etc/services .
76.Bd -literal -offset indent
77struct 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
85The members of this structure are:
86.Bl -tag -width s_aliases
87.It Fa s_name
88The official name of the service.
89.It Fa s_aliases
90A null-terminated list of alternate names for the service.
91.It Fa s_port
92The port number at which the service resides.
93Port numbers are returned in network byte order.
94.It Fa s_proto
95The name of the protocol to use when contacting the service.
96.El
97.Pp
98The
99.Fn getservent
100function reads the next line of the file, opening the file if necessary.
101.Pp
102The
103.Fn setservent
104function opens and rewinds the file.
105If the
106.Fa stayopen
107flag is non-zero,
108the services database will not be closed after each call to
109.Fn getservbyname
110or
111.Fn getservbyport .
112.Pp
113The
114.Fn endservent
115function closes the file.
116.Pp
117The
118.Fn getservbyname
119and
120.Fn getservbyport
121functions sequentially search from the beginning of the file until a
122matching protocol name or port number (specified in network byte order)
123is found, or until
124.Dv EOF
125is encountered.
126If a protocol name is also supplied (non-null),
127searches must also match the protocol.
128.Pp
129The
130.Fn getservent_r ,
131.Fn getservbyport_r ,
132.Fn getservbyname_r ,
133.Fn setservent_r ,
134and
135.Fn endservent_r
136functions are reentrant versions of the above functions that take a
137pointer to a
138.Fa servent_data
139structure which is used to store state information.
140The structure must be zero-filled before it is used
141and should be considered opaque for the sake of portability.
142.Pp
143The
144.Fn getservent_r ,
145.Fn getservbyport_r ,
146and
147.Fn getservbyname_r
148functions
149also take a pointer to a
150.Fa servent
151structure which is used to store the results of the database lookup.
152.Sh RETURN VALUES
153The
154.Fn getservent ,
155.Fn getservbyport ,
156and
157.Fn getservbyname
158functions return a pointer to a
159.Fa servent
160structure on success or a null pointer if end-of-file
161is reached or an error occurs.
162.Pp
163The
164.Fn getservent_r ,
165.Fn getservbyport_r ,
166and
167.Fn getservbyname_r
168functions return 0 on success or \-1 if end-of-file
169is 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
178The
179.Fn getservent ,
180.Fn getservbynumber ,
181.Fn getservbyname ,
182.Fn setservent ,
183and
184.Fn endservent
185functions conform to
186.St -p1003.1-2004 .
187.Pp
188The
189.Fn getservent_r ,
190.Fn getservbyport_r ,
191.Fn getservbyname_r ,
192.Fn setservent_r ,
193and
194.Fn endservent_r
195functions are not currently standardized.
196This implementation follows the API used by HP, IBM, and Digital.
197.Sh HISTORY
198The
199.Fn getservent ,
200.Fn getservbyport ,
201.Fn getservbyname ,
202.Fn setservent ,
203and
204.Fn endservent
205functions appeared in
206.Bx 4.2 .
207.Pp
208The
209.Fn getservent_r ,
210.Fn getservbyport_r ,
211.Fn getservbyname_r ,
212.Fn setservent_r ,
213and
214.Fn endservent_r
215functions appeared in
216.Ox 3.7 .
217.Sh BUGS
218The non-reentrant functions use static data storage; if the data is needed
219for future use, it should be copied before any subsequent calls overwrite it.
220Expecting 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
deleted file mode 100644
index 220a5851ce..0000000000
--- a/src/lib/libc/net/getservent.c
+++ /dev/null
@@ -1,171 +0,0 @@
1/* $OpenBSD: getservent.c,v 1.15 2015/09/14 07:38:38 guenther 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
41void
42setservent_r(int f, struct servent_data *sd)
43{
44 if (sd->fp == NULL)
45 sd->fp = fopen(_PATH_SERVICES, "re" );
46 else
47 rewind(sd->fp);
48 sd->stayopen |= f;
49}
50DEF_WEAK(setservent_r);
51
52void
53endservent_r(struct servent_data *sd)
54{
55 if (sd->fp) {
56 fclose(sd->fp);
57 sd->fp = NULL;
58 }
59 free(sd->aliases);
60 sd->aliases = NULL;
61 sd->maxaliases = 0;
62 free(sd->line);
63 sd->line = NULL;
64 sd->stayopen = 0;
65}
66DEF_WEAK(endservent_r);
67
68int
69getservent_r(struct servent *se, struct servent_data *sd)
70{
71 char *p, *cp, **q, *endp;
72 size_t len;
73 long l;
74 int serrno;
75
76 if (sd->fp == NULL && (sd->fp = fopen(_PATH_SERVICES, "re" )) == NULL)
77 return (-1);
78again:
79 if ((p = fgetln(sd->fp, &len)) == NULL)
80 return (-1);
81 if (len == 0 || *p == '#' || *p == '\n')
82 goto again;
83 if (p[len-1] == '\n')
84 len--;
85 if ((cp = memchr(p, '#', len)) != NULL)
86 len = cp - p;
87 cp = realloc(sd->line, len + 1);
88 if (cp == NULL)
89 return (-1);
90 sd->line = se->s_name = memcpy(cp, p, len);
91 cp[len] = '\0';
92 p = strpbrk(cp, " \t");
93 if (p == NULL)
94 goto again;
95 *p++ = '\0';
96 while (*p == ' ' || *p == '\t')
97 p++;
98 cp = strpbrk(p, ",/");
99 if (cp == NULL)
100 goto again;
101 *cp++ = '\0';
102 l = strtol(p, &endp, 10);
103 if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX)
104 goto again;
105 se->s_port = htons((in_port_t)l);
106 se->s_proto = cp;
107 if (sd->aliases == NULL) {
108 sd->maxaliases = 10;
109 sd->aliases = calloc(sd->maxaliases, sizeof(char *));
110 if (sd->aliases == NULL) {
111 serrno = errno;
112 endservent_r(sd);
113 errno = serrno;
114 return (-1);
115 }
116 }
117 q = se->s_aliases = sd->aliases;
118 cp = strpbrk(cp, " \t");
119 if (cp != NULL)
120 *cp++ = '\0';
121 while (cp && *cp) {
122 if (*cp == ' ' || *cp == '\t') {
123 cp++;
124 continue;
125 }
126 if (q == &se->s_aliases[sd->maxaliases - 1]) {
127 p = reallocarray(se->s_aliases, sd->maxaliases,
128 2 * sizeof(char *));
129 if (p == NULL) {
130 serrno = errno;
131 endservent_r(sd);
132 errno = serrno;
133 return (-1);
134 }
135 sd->maxaliases *= 2;
136 q = (char **)p + (q - se->s_aliases);
137 se->s_aliases = sd->aliases = (char **)p;
138 }
139 *q++ = cp;
140 cp = strpbrk(cp, " \t");
141 if (cp != NULL)
142 *cp++ = '\0';
143 }
144 *q = NULL;
145 return (0);
146}
147DEF_WEAK(getservent_r);
148
149struct servent_data _servent_data; /* shared with getservby{name,port}.c */
150
151void
152setservent(int f)
153{
154 setservent_r(f, &_servent_data);
155}
156
157void
158endservent(void)
159{
160 endservent_r(&_servent_data);
161}
162
163struct servent *
164getservent(void)
165{
166 static struct servent serv;
167
168 if (getservent_r(&serv, &_servent_data) != 0)
169 return (NULL);
170 return (&serv);
171}
diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c
deleted file mode 100644
index 356c85fa1d..0000000000
--- a/src/lib/libc/net/herror.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/* $OpenBSD: herror.c,v 1.10 2015/09/14 07:38:38 guenther 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/uio.h>
56#include <netdb.h>
57#include <unistd.h>
58#include <string.h>
59
60const char * const h_errlist[] = {
61 "Resolver Error 0 (no error)",
62 "Unknown host", /* 1 HOST_NOT_FOUND */
63 "Host name lookup failure", /* 2 TRY_AGAIN */
64 "Unknown server error", /* 3 NO_RECOVERY */
65 "No address associated with name", /* 4 NO_ADDRESS */
66};
67const int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] };
68
69extern int h_errno;
70
71/*
72 * herror --
73 * print the error indicated by the h_errno value.
74 */
75void
76herror(const char *s)
77{
78 struct iovec iov[4];
79 struct iovec *v = iov;
80
81 if (s && *s) {
82 v->iov_base = (char *)s;
83 v->iov_len = strlen(s);
84 v++;
85 v->iov_base = ": ";
86 v->iov_len = 2;
87 v++;
88 }
89 v->iov_base = (char *)hstrerror(h_errno);
90 v->iov_len = strlen(v->iov_base);
91 v++;
92 v->iov_base = "\n";
93 v->iov_len = 1;
94 writev(STDERR_FILENO, iov, (v - iov) + 1);
95}
96
97const char *
98hstrerror(int err)
99{
100 if (err < 0)
101 return ("Resolver internal error");
102 else if (err < h_nerr)
103 return (h_errlist[err]);
104 return ("Unknown resolver error");
105}
106DEF_WEAK(hstrerror);
diff --git a/src/lib/libc/net/htobe64.3 b/src/lib/libc/net/htobe64.3
deleted file mode 100644
index e41c9124c4..0000000000
--- a/src/lib/libc/net/htobe64.3
+++ /dev/null
@@ -1,164 +0,0 @@
1.\" $OpenBSD: htobe64.3,v 1.2 2024/08/03 23:06:56 guenther 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: August 3 2024 $
31.Dt HTOBE64 3
32.Os
33.Sh NAME
34.Nm htobe64 ,
35.Nm htobe32 ,
36.Nm htobe16 ,
37.Nm be64toh ,
38.Nm be32toh ,
39.Nm be16toh ,
40.Nm betoh64 ,
41.Nm betoh32 ,
42.Nm betoh16 ,
43.Nm htole64 ,
44.Nm htole32 ,
45.Nm htole16 ,
46.Nm le64toh ,
47.Nm le32toh ,
48.Nm le16toh ,
49.Nm letoh64 ,
50.Nm letoh32 ,
51.Nm letoh16 ,
52.Nm swap64 ,
53.Nm swap32 ,
54.Nm swap16
55.Nd convert values between different byte orderings
56.Sh SYNOPSIS
57.In endian.h
58.Ft uint64_t
59.Fn htobe64 "uint64_t host64"
60.Ft uint32_t
61.Fn htobe32 "uint32_t host32"
62.Ft uint16_t
63.Fn htobe16 "uint16_t host16"
64.Ft uint64_t
65.Fn be64toh "uint64_t big64"
66.Ft uint32_t
67.Fn be32toh "uint32_t big32"
68.Ft uint16_t
69.Fn be16toh "uint16_t big16"
70.Ft uint64_t
71.Fn betoh64 "uint64_t big64"
72.Ft uint32_t
73.Fn betoh32 "uint32_t big32"
74.Ft uint16_t
75.Fn betoh16 "uint16_t big16"
76.Ft uint64_t
77.Fn htole64 "uint64_t host64"
78.Ft uint32_t
79.Fn htole32 "uint32_t host32"
80.Ft uint16_t
81.Fn htole16 "uint16_t host16"
82.Ft uint64_t
83.Fn letoh64 "uint64_t little64"
84.Ft uint64_t
85.Fn le64toh "uint64_t little64"
86.Ft uint32_t
87.Fn le32toh "uint32_t little32"
88.Ft uint16_t
89.Fn le16toh "uint16_t little16"
90.Ft uint32_t
91.Fn letoh32 "uint32_t little32"
92.Ft uint16_t
93.Fn letoh16 "uint16_t little16"
94.Ft uint64_t
95.Fn swap64 "uint64_t val64"
96.Ft uint32_t
97.Fn swap32 "uint32_t val32"
98.Ft uint16_t
99.Fn swap16 "uint16_t val16"
100.Sh DESCRIPTION
101These routines convert 16, 32 and 64-bit quantities between different
102byte orderings.
103The
104.Dq swap
105functions reverse the byte ordering of
106the given quantity; the others convert either from/to the native
107byte order used by the host to/from either little- or big-endian (a.k.a
108network) order.
109.Pp
110Apart from the swap functions,
111the names containing
112.Dq be
113convert between host and big-endian (most significant byte first) order
114of the given quantity, while the names containing
115.Dq le
116convert between host and little-endian (least significant byte first) order
117of the given quantity.
118.Pp
119All these functions use the numbers
12016, 32, or 64 for specifying the bitwidth of the quantities they operate on.
121Currently all supported architectures are either big- or little-endian
122so either the
123.Dq be
124or
125.Dq le
126variants are implemented as null macros.
127.Sh SEE ALSO
128.Xr htonl 3
129.Sh STANDARDS
130The
131.Fn htobe64 ,
132.Fn htobe32 ,
133.Fn htobe16 ,
134.Fn be64toh ,
135.Fn be32toh ,
136.Fn be16toh ,
137.Fn htole64 ,
138.Fn htole32 ,
139.Fn htole16 ,
140.Fn le64toh ,
141.Fn le32toh ,
142and
143.Fn le16toh
144functions conform to
145.St -p1003.1-2024 .
146The other functions are extensions that should not be used
147when portability is required.
148.Sh HISTORY
149The
150.Nm swap{size}
151and
152.Nm {src-order}to{dst-order}{size}
153functions appeared in
154.Bx 4.2 .
155The
156.Nm {src-order}{size}to{dst-order}
157functions appeared in
158.Ox 5.6 .
159.Sh BUGS
160The perceived antagonism between
161.Sq host
162and
163.Sq network
164byte order does not allow PDP-11 users to sleep soundly at night.
diff --git a/src/lib/libc/net/htonl.3 b/src/lib/libc/net/htonl.3
deleted file mode 100644
index 65fa9a53b5..0000000000
--- a/src/lib/libc/net/htonl.3
+++ /dev/null
@@ -1,102 +0,0 @@
1.\" $OpenBSD: htonl.3,v 1.6 2022/09/11 06:38:10 jmc 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: September 11 2022 $
31.Dt HTONL 3
32.Os
33.Sh NAME
34.Nm htonl ,
35.Nm htons ,
36.Nm ntohl ,
37.Nm ntohs
38.Nd convert values between host and network byte orderings
39.Sh SYNOPSIS
40.In arpa/inet.h
41.Ft uint32_t
42.Fn htonl "uint32_t host32"
43.Ft uint16_t
44.Fn htons "uint16_t host16"
45.Ft uint32_t
46.Fn ntohl "uint32_t net32"
47.Ft uint16_t
48.Fn ntohs "uint16_t net16"
49.Sh DESCRIPTION
50These routines convert 16 and 32-bit quantities between different
51byte orderings.
52.Pp
53The
54.Fn htonl
55and
56.Fn htons
57functions convert quantities from host to network byte order while the
58.Fn ntohl
59and
60.Fn ntohs
61functions convert in the other direction.
62.Pp
63The last letter
64.Pf ( Sq s
65or
66.Sq l )
67is a mnemonic
68for the traditional names for such quantities,
69short and long, respectively.
70Today, the C concept of
71.Vt short
72and
73.Vt long
74integers need not coincide with this traditional misunderstanding.
75On machines which have a byte order which is the same as the network
76order, routines are defined as null macros.
77.Pp
78These routines are most often used in conjunction with Internet
79addresses and ports as returned by
80.Xr gethostbyname 3
81and
82.Xr getservent 3 .
83.Sh SEE ALSO
84.Xr gethostbyname 3 ,
85.Xr getservent 3 ,
86.Xr htobe64 3
87.Sh STANDARDS
88The
89.Fn htonl ,
90.Fn htons ,
91.Fn ntohl ,
92and
93.Fn ntohs
94functions conform to
95.St -p1003.1 .
96.Sh HISTORY
97These functions appeared in
98.Bx 4.2 .
99.Sh BUGS
100On the alpha, amd64, i386, and some mips and arm architectures,
101bytes are handled backwards from most everyone else in the world.
102This is not expected to be fixed in the near future.
diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c
deleted file mode 100644
index 58bfb4699a..0000000000
--- a/src/lib/libc/net/htonl.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: htonl.c,v 1.8 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef htonl
10
11uint32_t
12htonl(uint32_t x)
13{
14 return htobe32(x);
15}
diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c
deleted file mode 100644
index 28b13cef98..0000000000
--- a/src/lib/libc/net/htons.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: htons.c,v 1.10 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef htons
10
11uint16_t
12htons(uint16_t x)
13{
14 return htobe16(x);
15}
diff --git a/src/lib/libc/net/if_indextoname.3 b/src/lib/libc/net/if_indextoname.3
deleted file mode 100644
index 25d2a2722f..0000000000
--- a/src/lib/libc/net/if_indextoname.3
+++ /dev/null
@@ -1,143 +0,0 @@
1.\" $OpenBSD: if_indextoname.3,v 1.16 2015/11/21 07:48:10 jmc 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: November 21 2015 $
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
53These functions map interface indexes to interface names (such as
54.Dq lo0 ) ,
55and vice versa.
56.Pp
57The
58.Fn if_nametoindex
59function converts an interface name specified by the
60.Fa ifname
61argument to an interface index (positive integer value).
62If the specified interface does not exist, 0 will be returned.
63.Pp
64.Fn if_indextoname
65converts an interface index specified by the
66.Fa ifindex
67argument to an interface name.
68The
69.Fa ifname
70argument must point to a buffer of at least
71.Dv IF_NAMESIZE
72bytes into which the interface name corresponding to the specified index is
73returned.
74.Pf ( Dv IF_NAMESIZE
75is also defined in
76.In net/if.h
77and its value includes a terminating NUL byte at the end of the
78interface name.)
79This pointer is also the return value of the function.
80If there is no interface corresponding to the specified index,
81.Dv NULL
82is returned.
83.Pp
84.Fn if_nameindex
85returns an array of
86.Vt if_nameindex
87structures.
88.Vt if_nameindex
89is also defined in
90.In net/if.h ,
91and is as follows:
92.Bd -literal -offset indent
93struct if_nameindex {
94 unsigned int if_index; /* 1, 2, ... */
95 char *if_name; /* NUL-terminated name */
96};
97.Ed
98.Pp
99The end of the array of structures is indicated by a structure with
100an
101.Fa if_index
102of 0 and an
103.Fa if_name
104of
105.Dv NULL .
106The function returns a null pointer on error.
107The memory used for this array of structures along with the interface
108names pointed to by the
109.Fa if_name
110members is obtained dynamically.
111This memory is freed by the
112.Fn if_freenameindex
113function.
114.Pp
115.Fn if_freenameindex
116takes a pointer that was returned by
117.Fn if_nameindex
118as argument
119.Pq Fa ptr ,
120and it reclaims the region allocated.
121.Sh DIAGNOSTICS
122.Fn if_nametoindex
123returns 0 on error, positive integer on success.
124.Fn if_indextoname
125and
126.Fn if_nameindex
127return
128.Dv NULL
129on errors.
130.Sh SEE ALSO
131.Xr getifaddrs 3 ,
132.Xr netintro 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
deleted file mode 100644
index 3c6e8681fb..0000000000
--- a/src/lib/libc/net/if_indextoname.c
+++ /dev/null
@@ -1,78 +0,0 @@
1/* $OpenBSD: if_indextoname.c,v 1.12 2018/05/10 13:44:43 tb Exp $ */
2/* $KAME: if_indextoname.c,v 1.6 2000/11/07 22:33:25 jinmei Exp $ */
3
4/*-
5 * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 1997, 2000
7 * Berkeley Software Design, Inc. 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 *
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp
28 */
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <net/if.h>
33#include <stdlib.h>
34#include <string.h>
35#include <errno.h>
36
37/*
38 * From RFC 2553:
39 *
40 * The second function maps an interface index into its corresponding
41 * name.
42 *
43 * #include <net/if.h>
44 *
45 * char *if_indextoname(unsigned int ifindex, char *ifname);
46 *
47 * The ifname argument must point to a buffer of at least IF_NAMESIZE
48 * bytes into which the interface name corresponding to the specified
49 * index is returned. (IF_NAMESIZE is also defined in <net/if.h> and
50 * its value includes a terminating null byte at the end of the
51 * interface name.) This pointer is also the return value of the
52 * function. If there is no interface corresponding to the specified
53 * index, NULL is returned, and errno is set to ENXIO, if there was a
54 * system error (such as running out of memory), if_indextoname returns
55 * NULL and errno would be set to the proper value (e.g., ENOMEM).
56 */
57
58char *
59if_indextoname(unsigned int ifindex, char *ifname)
60{
61 struct if_nameindex *ifni, *ifni2;
62
63 if ((ifni = if_nameindex()) == NULL)
64 return NULL;
65
66 for (ifni2 = ifni; ifni2->if_index != 0; ifni2++) {
67 if (ifni2->if_index == ifindex) {
68 strlcpy(ifname, ifni2->if_name, IFNAMSIZ);
69 if_freenameindex(ifni);
70 return ifname;
71 }
72 }
73
74 if_freenameindex(ifni);
75 errno = ENXIO;
76 return NULL;
77}
78DEF_WEAK(if_indextoname);
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c
deleted file mode 100644
index 6e5bc0809a..0000000000
--- a/src/lib/libc/net/if_nameindex.c
+++ /dev/null
@@ -1,145 +0,0 @@
1/* $OpenBSD: if_nameindex.c,v 1.13 2016/12/16 17:44:59 krw Exp $ */
2/* $KAME: if_nameindex.c,v 1.7 2000/11/24 08:17:20 itojun Exp $ */
3
4/*-
5 * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 1997, 2000
7 * Berkeley Software Design, Inc. 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 *
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
28 */
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <sys/sysctl.h>
33#include <net/if.h>
34#include <stdlib.h>
35#include <string.h>
36#include <errno.h>
37
38/*
39 * From RFC 2553:
40 *
41 * 4.3 Return All Interface Names and Indexes
42 *
43 * The if_nameindex structure holds the information about a single
44 * interface and is defined as a result of including the <net/if.h>
45 * header.
46 *
47 * struct if_nameindex {
48 * unsigned int if_index;
49 * char *if_name;
50 * };
51 *
52 * The final function returns an array of if_nameindex structures, one
53 * structure per interface.
54 *
55 * struct if_nameindex *if_nameindex(void);
56 *
57 * The end of the array of structures is indicated by a structure with
58 * an if_index of 0 and an if_name of NULL. The function returns a NULL
59 * pointer upon an error, and would set errno to the appropriate value.
60 *
61 * The memory used for this array of structures along with the interface
62 * names pointed to by the if_name members is obtained dynamically.
63 * This memory is freed by the next function.
64 *
65 * 4.4. Free Memory
66 *
67 * The following function frees the dynamic memory that was allocated by
68 * if_nameindex().
69 *
70 * #include <net/if.h>
71 *
72 * void if_freenameindex(struct if_nameindex *ptr);
73 *
74 * The argument to this function must be a pointer that was returned by
75 * if_nameindex().
76 */
77
78struct if_nameindex *
79if_nameindex(void)
80{
81 struct if_nameindex_msg *ifnm = NULL;
82 struct if_nameindex *ifni = NULL, *ifni2;
83 char *cp;
84 size_t needed;
85 unsigned int ni, i;
86 int mib[6];
87
88 mib[0] = CTL_NET;
89 mib[1] = PF_ROUTE;
90 mib[2] = 0; /* protocol */
91 mib[3] = 0; /* not used */
92 mib[4] = NET_RT_IFNAMES;
93 mib[5] = 0; /* no flags */
94 while (1) {
95 struct if_nameindex_msg *buf = NULL;
96
97 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
98 goto out;
99 if (needed == 0)
100 break;
101 if ((buf = realloc(ifnm, needed)) == NULL)
102 goto out;
103 ifnm = buf;
104 if (sysctl(mib, 6, ifnm, &needed, NULL, 0) == -1) {
105 if (errno == ENOMEM)
106 continue;
107 goto out;
108 }
109 break;
110 }
111
112 /*
113 * Allocate a chunk of memory, use the first part for the array of
114 * structures, and the last part for the strings.
115 */
116 ni = needed / sizeof(*ifnm);
117 ifni = calloc(ni + 1, sizeof(struct if_nameindex) + IF_NAMESIZE);
118 if (ifni == NULL)
119 goto out;
120 cp = (char *)(ifni + (ni + 1));
121
122 ifni2 = ifni;
123 for (i = 0; i < ni; i++) {
124 ifni2->if_index = ifnm[i].if_index;
125 /* don't care about truncation */
126 strlcpy(cp, ifnm[i].if_name, IF_NAMESIZE);
127 ifni2->if_name = cp;
128 ifni2++;
129 cp += IF_NAMESIZE;
130 }
131 /* Finally, terminate the array. */
132 ifni2->if_index = 0;
133 ifni2->if_name = NULL;
134out:
135 free(ifnm);
136 return ifni;
137}
138
139void
140if_freenameindex(struct if_nameindex *ptr)
141{
142 free(ptr);
143}
144DEF_WEAK(if_nameindex);
145DEF_WEAK(if_freenameindex);
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c
deleted file mode 100644
index 7a4492e37d..0000000000
--- a/src/lib/libc/net/if_nametoindex.c
+++ /dev/null
@@ -1,77 +0,0 @@
1/* $OpenBSD: if_nametoindex.c,v 1.10 2015/10/23 13:09:19 claudio Exp $ */
2/* $KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $ */
3
4/*-
5 * Copyright (c) 2015 Claudio Jeker <claudio@openbsd.org>
6 * Copyright (c) 1997, 2000
7 * Berkeley Software Design, Inc. 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 *
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp
28 */
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <net/if.h>
33#include <stdlib.h>
34#include <string.h>
35#include <errno.h>
36
37/*
38 * From RFC 2553:
39 *
40 * 4.1 Name-to-Index
41 *
42 *
43 * The first function maps an interface name into its corresponding
44 * index.
45 *
46 * #include <net/if.h>
47 *
48 * unsigned int if_nametoindex(const char *ifname);
49 *
50 * If the specified interface name does not exist, the return value is
51 * 0, and errno is set to ENXIO. If there was a system error (such as
52 * running out of memory), the return value is 0 and errno is set to the
53 * proper value (e.g., ENOMEM).
54 */
55
56unsigned int
57if_nametoindex(const char *ifname)
58{
59 struct if_nameindex *ifni, *ifni2;
60 unsigned int index;
61
62 if ((ifni = if_nameindex()) == NULL)
63 return(0);
64
65 for (ifni2 = ifni; ifni2->if_index != 0; ifni2++) {
66 if (strcmp(ifni2->if_name, ifname) == 0) {
67 index = ifni2->if_index;
68 if_freenameindex(ifni);
69 return index;
70 }
71 }
72
73 if_freenameindex(ifni);
74 errno = ENXIO;
75 return 0;
76}
77DEF_WEAK(if_nametoindex);
diff --git a/src/lib/libc/net/inet6_opt_init.3 b/src/lib/libc/net/inet6_opt_init.3
deleted file mode 100644
index 41ba842166..0000000000
--- a/src/lib/libc/net/inet6_opt_init.3
+++ /dev/null
@@ -1,328 +0,0 @@
1.\" $OpenBSD: inet6_opt_init.3,v 1.8 2022/03/31 17:27:16 naddy 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: March 31 2022 $
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
63Building and parsing the Hop-by-Hop and Destination options is
64complicated.
65The advanced sockets API defines a set of functions to
66help applications create and manipulate Hop-by-Hop and Destination
67options.
68These functions use the
69formatting rules specified in Appendix B in RFC 2460, i.e. that the
70largest field is placed last in the option.
71The function prototypes
72for these functions are all contained in the header file
73.In netinet/in.h .
74.\"
75.Ss inet6_opt_init
76The
77.Fn inet6_opt_init
78function
79returns the number of bytes needed for an empty
80extension header, one without any options.
81If the
82.Va extbuf
83argument points to a valid section of memory
84then the
85.Fn inet6_opt_init
86function also initializes the extension header's length field.
87When attempting to initialize an extension buffer passed in the
88.Va extbuf
89argument,
90.Fa extlen
91must be a positive multiple of 8 or else the function fails and
92returns \-1 to the caller.
93.\"
94.Ss inet6_opt_append
95The
96.Fn inet6_opt_append
97function can perform different jobs.
98When a valid
99.Fa extbuf
100argument is supplied, it appends an option to the extension buffer and
101returns the updated total length as well as a pointer to the newly
102created option in
103.Fa databufp .
104If the value
105of
106.Fa extbuf
107is
108.Dv NULL
109then the
110.Fn inet6_opt_append
111function only reports what the total length would
112be if the option were actually appended.
113The
114.Fa len
115and
116.Fa align
117arguments specify the length of the option and the required data
118alignment which must be used when appending the option.
119The
120.Fa offset
121argument should be the length returned by the
122.Fn inet6_opt_init
123function or a previous call to
124.Fn inet6_opt_append .
125.Pp
126The
127.Fa type
128argument is the 8-bit option type.
129.Pp
130After
131.Fn inet6_opt_append
132has been called, the application can use the buffer pointed to by
133.Fa databufp
134directly, or use
135.Fn inet6_opt_set_val
136to specify the data to be contained in the option.
137.Pp
138Option types of
139.Li 0
140and
141.Li 1
142are reserved for the
143.Li Pad1
144and
145.Li PadN
146options.
147All other values from 2 through 255 may be used by applications.
148.Pp
149The length of the option data is contained in an 8-bit value and so
150may contain any value from 0 through 255.
151.Pp
152The
153.Fa align
154parameter must have a value of 1, 2, 4, or 8 and cannot exceed the
155value of
156.Fa len .
157The alignment values represent no alignment, 16-bit, 32-bit and 64-bit
158alignments respectively.
159.\"
160.Ss inet6_opt_finish
161The
162.Fn inet6_opt_finish
163function calculates the final padding necessary to make the extension header a
164multiple of 8 bytes, as required by the IPv6 extension header
165specification, and returns the extension header's updated total
166length.
167The
168.Fa offset
169argument should be the length returned by
170.Fn inet6_opt_init
171or
172.Fn inet6_opt_append .
173When
174.Fa extbuf
175is not
176.Dv NULL ,
177the function also sets up the appropriate padding bytes by inserting a
178Pad1 or PadN option of the proper length.
179.Pp
180If the extension header is too small to contain the proper padding
181then an error of \-1 is returned to the caller.
182.\"
183.Ss inet6_opt_set_val
184The
185.Fn inet6_opt_set_val
186function inserts data items of various sizes into the data portion of
187the option.
188The
189.Fa databuf
190argument is a pointer to memory that was returned by the
191.Fn inet6_opt_append
192call and the
193.Fa offset
194argument specifies where the option should be placed in the
195data buffer.
196The
197.Fa val
198argument points to an area of memory containing the data to be
199inserted into the extension header, and the
200.Fa vallen
201argument indicates how much data to copy.
202.Pp
203The caller should ensure that each field is aligned on its natural
204boundaries as described in Appendix B of RFC 2460.
205.Pp
206The function returns the offset for the next field which is calculated as
207.Fa offset
208+
209.Fa vallen
210and is used when composing options with multiple fields.
211.\"
212.Ss inet6_opt_next
213The
214.Fn inet6_opt_next
215function parses received extension headers.
216The
217.Fa extbuf
218and
219.Fa extlen
220arguments specify the location and length of the extension header
221being parsed.
222The
223.Fa offset
224argument should either be zero, for the first option, or the length value
225returned by a previous call to
226.Fn inet6_opt_next
227or
228.Fn inet6_opt_find .
229The return value specifies the position where to continue scanning the
230extension buffer.
231The option is returned in the arguments
232.Fa typep , lenp ,
233and
234.Fa databufp .
235.Fa typep , lenp ,
236and
237.Fa databufp
238point to the 8-bit option type, the 8-bit option length and the option
239data respectively.
240This function does not return any PAD1 or PADN options.
241When an error occurs or there are no more options, the return
242value is \-1.
243.\"
244.Ss inet6_opt_find
245The
246.Fn inet6_opt_find
247function searches the extension buffer for a particular option type,
248passed in through the
249.Fa type
250argument.
251If the option is found then the
252.Fa lenp
253and
254.Fa databufp
255arguments are updated to point to the option's length and data
256respectively.
257.Fa extbuf
258and
259.Fa extlen
260must point to a valid extension buffer and give its length.
261The
262.Fa offset
263argument can be used to search from a location anywhere in the
264extension header.
265.Ss inet6_opt_get_val
266The
267.Fn inet6_opt_get_val
268function extracts data items of various sizes in the data portion of
269the option.
270The
271.Fa databuf
272is a pointer returned by the
273.Fn inet6_opt_next
274or
275.Fn inet6_opt_find
276functions.
277The
278.Fa val
279argument points to where the data will be extracted.
280The
281.Fa offset
282argument specifies from where in the data portion of the option the
283value should be extracted; the first byte of option data is specified
284by an offset of zero.
285.Pp
286It is expected that each field is aligned on its natural boundaries as
287described in Appendix B of RFC 2460.
288.Pp
289The function returns the offset for the next field
290by calculating
291.Fa offset
292+
293.Fa vallen
294which can be used when extracting option content with multiple fields.
295Robust receivers must verify alignment before calling this function.
296.\"
297.Sh EXAMPLES
298RFC 3542 gives comprehensive examples in Section 23.
299KAME also provides examples in the
300.Pa advapitest
301directory of its kit.
302.\"
303.Sh DIAGNOSTICS
304All the functions return
305\-1
306on 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
327The implementation first appeared in KAME advanced networking kit.
328.\"
diff --git a/src/lib/libc/net/inet6_rth_space.3 b/src/lib/libc/net/inet6_rth_space.3
deleted file mode 100644
index c40b45057e..0000000000
--- a/src/lib/libc/net/inet6_rth_space.3
+++ /dev/null
@@ -1,220 +0,0 @@
1.\" $OpenBSD: inet6_rth_space.3,v 1.8 2022/03/31 17:27:16 naddy 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: March 31 2022 $
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
60The IPv6 Advanced API, RFC 3542, defines the functions that an
61application calls to build and examine IPv6 Routing headers.
62Routing headers are used to perform source routing in IPv6 networks.
63The RFC uses the word
64.Dq segments
65to describe addresses and that is the term used here as well.
66All of the functions are defined in the header file
67.In netinet/in.h .
68The functions described in this manual page all operate
69on routing header structures which are defined in
70.In netinet/ip6.h
71but which should not need to be modified outside the use of this API.
72The size and shape of the route header structures may change, so using
73the APIs is a more portable, long term, solution.
74.Pp
75The functions in the API are split into two groups, those that build a
76routing header and those that parse a received routing header.
77The builder functions are described first, followed by the parser functions.
78.Ss inet6_rth_space
79The
80.Fn inet6_rth_space
81function returns the number of bytes required to hold a Routing Header
82of the type, specified in the
83.Fa type
84argument and containing the number of addresses specified in the
85.Fa segments
86argument.
87When the type is
88.Dv IPV6_RTHDR_TYPE_0 ,
89the number of segments must be from 0 through 127.
90The return value from this function is the number of bytes required to
91store the routing header.
92If the value 0 is returned then either the
93route header type was not recognized or another error occurred.
94.Ss inet6_rth_init
95The
96.Fn inet6_rth_init
97function initializes the pre-allocated buffer pointed to by
98.Fa bp
99to contain a routing header of the specified type.
100The
101.Fa bp_len
102argument is used to verify that the buffer is large enough.
103The caller must allocate the buffer pointed to by bp.
104The necessary buffer size should be determined by calling
105.Fn inet6_rth_space
106described in the previous sections.
107.Pp
108The
109.Fn inet6_rth_init
110function returns a pointer to
111.Fa bp
112on success and
113.Dv NULL
114when there is an error.
115.Ss inet6_rth_add
116The
117.Fn inet6_rth_add
118function adds the IPv6 address pointed to by
119.Fa addr
120to the end of the routing header being constructed.
121.Pp
122A successful addition results in the function returning 0, otherwise
123\-1 is returned.
124.Ss inet6_rth_reverse
125The
126.Fn inet6_rth_reverse
127function takes a routing header, pointed to by the
128argument
129.Fa in ,
130and writes a new routing header into the argument pointed to by
131.Fa out .
132The routing header at that sends datagrams along the reverse of that
133route.
134Both arguments are allowed to point to the same buffer meaning
135that the reversal can occur in place.
136.Pp
137The return value of the function is 0 on success, or \-1 when
138there is an error.
139.\"
140.Pp
141The next set of functions operate on a routing header that the
142application wants to parse.
143In the usual case such a routing header
144is received from the network, although these functions can also be
145used with routing headers that the application itself created.
146.Ss inet6_rth_segments
147The
148.Fn inet6_rth_segments
149function returns the number of segments contained in the
150routing header pointed to by
151.Fa bp .
152The return value is the number of segments contained in the routing
153header, or \-1 if an error occurred.
154It is not an error for 0 to be
155returned as a routing header may contain 0 segments.
156.\"
157.Ss inet6_rth_getaddr
158The
159.Fn inet6_rth_getaddr
160function is used to retrieve a single address from a routing header.
161The
162.Fa index
163is the location in the routing header from which the application wants
164to retrieve an address.
165The
166.Fa index
167parameter must have a value between 0 and one less than the number of
168segments present in the routing header.
169The
170.Fn inet6_rth_segments
171function, described in the last section, should be used to determine
172the total number of segments in the routing header.
173The
174.Fn inet6_rth_getaddr
175function returns a pointer to an IPv6 address on success or
176.Dv NULL
177when an error has occurred.
178.\"
179.Sh EXAMPLES
180RFC 3542 gives extensive examples in Section 21, Appendix B.
181KAME also provides examples in the advapitest directory of its kit.
182.\"
183.Sh DIAGNOSTICS
184The
185.Fn inet6_rth_space
186and
187.Fn inet6_rth_getaddr
188functions return 0 on errors.
189.Pp
190The
191.Fn inet6_rth_init
192function returns
193.Dv NULL
194on error.
195The
196.Fn inet6_rth_add
197and
198.Fn inet6_rth_reverse
199functions 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
220The implementation first appeared in KAME advanced networking kit.
diff --git a/src/lib/libc/net/inet_addr.3 b/src/lib/libc/net/inet_addr.3
deleted file mode 100644
index cbb9e14f6d..0000000000
--- a/src/lib/libc/net/inet_addr.3
+++ /dev/null
@@ -1,195 +0,0 @@
1.\" $OpenBSD: inet_addr.3,v 1.7 2024/03/06 07:29:37 bentley 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: March 6 2024 $
34.Dt INET_ADDR 3
35.Os
36.Sh NAME
37.Nm inet_aton ,
38.Nm inet_addr ,
39.Nm inet_network ,
40.Nm inet_ntoa
41.Nd Internet Protocol version 4 (IPv4) address manipulation routines
42.Sh SYNOPSIS
43.In arpa/inet.h
44.Ft int
45.Fn inet_aton "const char *cp" "struct in_addr *addr"
46.Ft in_addr_t
47.Fn inet_addr "const char *cp"
48.Ft in_addr_t
49.Fn inet_network "const char *cp"
50.Ft char *
51.Fn inet_ntoa "struct in_addr in"
52.Sh DESCRIPTION
53The functions presented here only support IPv4 addresses.
54In order to support IPv6 addresses as well,
55.Xr inet_ntop 3
56and
57.Xr inet_pton 3
58should be used rather than the functions presented here.
59Scoped IPv6 addresses are supported via
60.Xr getaddrinfo 3
61and
62.Xr getnameinfo 3 .
63.Pp
64The routines
65.Fn inet_aton ,
66.Fn inet_addr ,
67and
68.Fn inet_network
69interpret character strings representing
70numbers expressed in the Internet standard
71.Dq dot
72notation.
73.Pp
74The
75.Fn inet_aton
76routine interprets the specified character string as an Internet address,
77placing the address into the structure provided.
78It returns 1 if the string was successfully interpreted,
79or 0 if the string was invalid.
80.Pp
81The
82.Fn inet_addr
83and
84.Fn inet_network
85functions return numbers suitable for use
86as Internet addresses and Internet network
87numbers, respectively.
88Both functions return the constant
89.Dv INADDR_NONE
90if the specified character string is malformed.
91.Pp
92The routine
93.Fn inet_ntoa
94takes an Internet address and returns an
95ASCII string representing the address in dot notation.
96.Pp
97All Internet addresses are returned in network
98order (bytes ordered from left to right).
99All network numbers and local address parts are
100returned as machine format integer values.
101.Sh INTERNET ADDRESSES (IP VERSION 4)
102Values specified using dot notation take one of the following forms:
103.Bd -literal -offset indent
104a.b.c.d
105a.b.c
106a.b
107a
108.Ed
109.Pp
110When four parts are specified, each is interpreted
111as a byte of data and assigned, from left to right,
112to the four bytes of an Internet address.
113Note that when an Internet address is viewed as a 32-bit
114integer quantity on a system that uses little-endian
115byte order
116(such as AMD64 or ARM processors)
117the bytes referred to above appear as
118.Dq Li d.c.b.a .
119That is, little-endian bytes are ordered from right to left.
120.Pp
121When a three part address is specified, the last
122part is interpreted as a 16-bit quantity and placed
123in the rightmost two bytes of the network address.
124This makes the three part address format convenient
125for specifying Class B network addresses as
126.Dq Li 128.net.host .
127.Pp
128When a two part address is supplied, the last part
129is interpreted as a 24-bit quantity and placed in
130the rightmost three bytes of the network address.
131This makes the two part address format convenient
132for specifying Class A network addresses as
133.Dq Li net.host .
134.Pp
135When only one part is given, the value is stored
136directly in the network address without any byte
137rearrangement.
138.Pp
139All numbers supplied as
140.Dq parts
141in a dot notation
142may be decimal, octal, or hexadecimal, as specified
143in the C language (i.e., a leading 0x or 0X implies
144hexadecimal; a leading 0 implies octal;
145otherwise, the number is interpreted as decimal).
146.Sh SEE ALSO
147.Xr gethostbyname 3 ,
148.Xr htonl 3 ,
149.Xr inet_lnaof 3 ,
150.Xr inet_net_ntop 3 ,
151.Xr inet_ntop 3 ,
152.Xr hosts 5
153.Sh STANDARDS
154The
155.Nm inet_addr
156and
157.Nm inet_ntoa
158functions conform to
159.St -p1003.1-2008 .
160.Sh HISTORY
161The
162.Nm inet_addr
163and
164.Nm inet_network
165functions appeared in
166.Bx 4.2 .
167The
168.Nm inet_aton
169and
170.Nm inet_ntoa
171functions appeared in
172.Bx 4.3 .
173.Sh BUGS
174The value
175.Dv INADDR_NONE
176(0xffffffff) is a valid broadcast address, but
177.Fn inet_addr
178cannot return that value without indicating failure.
179Also,
180.Fn inet_addr
181should have been designed to return a
182.Vt struct in_addr .
183The newer
184.Fn inet_aton
185function does not share these problems, and almost all existing code
186should be modified to use
187.Fn inet_aton
188instead.
189.Pp
190The problem of host byte ordering versus network byte ordering is
191confusing.
192.Pp
193The string returned by
194.Fn inet_ntoa
195resides in a static memory area.
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c
deleted file mode 100644
index 62d46ad664..0000000000
--- a/src/lib/libc/net/inet_addr.c
+++ /dev/null
@@ -1,175 +0,0 @@
1/* $OpenBSD: inet_addr.c,v 1.12 2015/09/13 21:36:08 guenther 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 <netinet/in.h>
56#include <arpa/inet.h>
57#include <ctype.h>
58
59/*
60 * Ascii internet address interpretation routine.
61 * The value returned is in network order.
62 */
63in_addr_t
64inet_addr(const char *cp)
65{
66 struct in_addr val;
67
68 if (inet_aton(cp, &val))
69 return (val.s_addr);
70 return (INADDR_NONE);
71}
72
73/*
74 * Check whether "cp" is a valid ascii representation
75 * of an Internet address and convert to a binary address.
76 * Returns 1 if the address is valid, 0 if not.
77 * This replaces inet_addr, the return value from which
78 * cannot distinguish between failure and a local broadcast address.
79 */
80int
81inet_aton(const char *cp, struct in_addr *addr)
82{
83 in_addr_t val;
84 int base, n;
85 char c;
86 u_int parts[4];
87 u_int *pp = parts;
88
89 c = *cp;
90 for (;;) {
91 /*
92 * Collect number up to ``.''.
93 * Values are specified as for C:
94 * 0x=hex, 0=octal, isdigit=decimal.
95 */
96 if (!isdigit((unsigned char)c))
97 return (0);
98 val = 0; base = 10;
99 if (c == '0') {
100 c = *++cp;
101 if (c == 'x' || c == 'X')
102 base = 16, c = *++cp;
103 else
104 base = 8;
105 }
106 for (;;) {
107 if (isascii((unsigned char)c) &&
108 isdigit((unsigned char)c)) {
109 val = (val * base) + (c - '0');
110 c = *++cp;
111 } else if (base == 16 &&
112 isascii((unsigned char)c) &&
113 isxdigit((unsigned char)c)) {
114 val = (val << 4) |
115 (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
116 c = *++cp;
117 } else
118 break;
119 }
120 if (c == '.') {
121 /*
122 * Internet format:
123 * a.b.c.d
124 * a.b.c (with c treated as 16 bits)
125 * a.b (with b treated as 24 bits)
126 */
127 if (pp >= parts + 3)
128 return (0);
129 *pp++ = val;
130 c = *++cp;
131 } else
132 break;
133 }
134 /*
135 * Check for trailing characters.
136 */
137 if (c != '\0' &&
138 (!isascii((unsigned char)c) || !isspace((unsigned char)c)))
139 return (0);
140 /*
141 * Concoct the address according to
142 * the number of parts specified.
143 */
144 n = pp - parts + 1;
145 switch (n) {
146
147 case 0:
148 return (0); /* initial nondigit */
149
150 case 1: /* a -- 32 bits */
151 break;
152
153 case 2: /* a.b -- 8.24 bits */
154 if ((val > 0xffffff) || (parts[0] > 0xff))
155 return (0);
156 val |= parts[0] << 24;
157 break;
158
159 case 3: /* a.b.c -- 8.8.16 bits */
160 if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
161 return (0);
162 val |= (parts[0] << 24) | (parts[1] << 16);
163 break;
164
165 case 4: /* a.b.c.d -- 8.8.8.8 bits */
166 if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
167 return (0);
168 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
169 break;
170 }
171 if (addr)
172 addr->s_addr = htonl(val);
173 return (1);
174}
175DEF_WEAK(inet_aton);
diff --git a/src/lib/libc/net/inet_lnaof.3 b/src/lib/libc/net/inet_lnaof.3
deleted file mode 100644
index 6151f7fe34..0000000000
--- a/src/lib/libc/net/inet_lnaof.3
+++ /dev/null
@@ -1,89 +0,0 @@
1.\" $OpenBSD: inet_lnaof.3,v 1.4 2019/08/30 20:06:07 jmc 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: August 30 2019 $
34.Dt INET_LNAOF 3
35.Os
36.Sh NAME
37.Nm inet_makeaddr ,
38.Nm inet_netof ,
39.Nm inet_lnaof
40.Nd routines for manipulating classful Internet Protocol version 4 (IPv4) addresses
41.Sh SYNOPSIS
42.In arpa/inet.h
43.Ft struct in_addr
44.Fn inet_makeaddr "in_addr_t net" "in_addr_t lna"
45.Ft in_addr_t
46.Fn inet_netof "struct in_addr in"
47.Ft in_addr_t
48.Fn inet_lnaof "struct in_addr in"
49.Sh DESCRIPTION
50As originally designed,
51IP version 4 split each address into a network part and local network
52address part, encoding that split into the address itself.
53It is frequency-encoded;
54the most-significant bit is clear in Class A addresses,
55in which the high-order 8 bits are the network number.
56Class B addresses use the high-order 16 bits as the network field,
57and Class C addresses have a 24-bit network part.
58.Pp
59The routine
60.Fn inet_makeaddr
61takes an Internet network number and a local
62network address and constructs an Internet address
63from it.
64.Pp
65The routines
66.Fn inet_netof
67and
68.Fn inet_lnaof
69break apart Internet host addresses, returning
70the network number and local network address part,
71respectively.
72.Pp
73All Internet addresses are returned in network
74order (bytes ordered from left to right).
75All network numbers and local address parts are
76returned as machine format integer values.
77.Sh SEE ALSO
78.Xr gethostbyname 3 ,
79.Xr inet_addr 3 ,
80.Xr inet_net_ntop 3 ,
81.Xr hosts 5
82.Sh HISTORY
83The
84.Nm inet_makeaddr ,
85.Nm inet_lnaof ,
86and
87.Nm inet_netof
88functions appeared in
89.Bx 4.2 .
diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c
deleted file mode 100644
index 92845387d3..0000000000
--- a/src/lib/libc/net/inet_lnaof.c
+++ /dev/null
@@ -1,50 +0,0 @@
1/* $OpenBSD: inet_lnaof.c,v 1.7 2015/01/16 16:48:51 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 <netinet/in.h>
32#include <arpa/inet.h>
33
34/*
35 * Return the local network address portion of an
36 * internet address; handles class a/b/c network
37 * number formats.
38 */
39in_addr_t
40inet_lnaof(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_HOST);
46 else if (IN_CLASSB(i))
47 return ((i)&IN_CLASSB_HOST);
48 else
49 return ((i)&IN_CLASSC_HOST);
50}
diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c
deleted file mode 100644
index 88ddd2850e..0000000000
--- a/src/lib/libc/net/inet_makeaddr.c
+++ /dev/null
@@ -1,53 +0,0 @@
1/* $OpenBSD: inet_makeaddr.c,v 1.7 2015/01/16 16:48:51 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 <netinet/in.h>
32#include <arpa/inet.h>
33
34/*
35 * Formulate an Internet address from network + host. Used in
36 * building addresses stored in the ifnet structure.
37 */
38struct in_addr
39inet_makeaddr(in_addr_t net, in_addr_t host)
40{
41 in_addr_t addr;
42
43 if (net < 128)
44 addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
45 else if (net < 65536)
46 addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
47 else if (net < 16777216L)
48 addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
49 else
50 addr = net | host;
51 addr = htonl(addr);
52 return (*(struct in_addr *)&addr);
53}
diff --git a/src/lib/libc/net/inet_net_ntop.3 b/src/lib/libc/net/inet_net_ntop.3
deleted file mode 100644
index 4212af3bb6..0000000000
--- a/src/lib/libc/net/inet_net_ntop.3
+++ /dev/null
@@ -1,195 +0,0 @@
1.\" $OpenBSD: inet_net_ntop.3,v 1.4 2022/09/11 06:38:10 jmc 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: September 11 2022 $
32.Dt INET_NET_NTOP 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/socket.h
40.In arpa/inet.h
41.Ft char *
42.Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size"
43.Ft int
44.Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size"
45.Sh DESCRIPTION
46The
47.Fn inet_net_ntop
48function converts an Internet network number from network format (usually a
49.Vt struct in_addr
50or some other binary form, in network byte order) to CIDR presentation format
51(suitable for external display purposes).
52.Fa bits
53is the number of bits in
54.Fa src
55that are the network number.
56It returns
57.Dv NULL
58if a system error occurs (in which case,
59.Va errno
60will have been set), or it returns a pointer to the destination string.
61.Pp
62The
63.Fn inet_net_pton
64function converts a presentation format Internet network number (that is,
65printable form as held in a character string) to network format (usually a
66.Vt struct in_addr
67or some other internal binary representation, in network byte order).
68It returns the number of bits (either computed based on the class, or
69specified with /CIDR), or \-1 if a failure occurred
70(in which case
71.Va errno
72will have been set.
73It will be set to
74.Er ENOENT
75if the Internet network number was not valid).
76.Pp
77Caution:
78The
79.Fa dst
80field should be zeroed before calling
81.Fn inet_net_pton
82as the function will only fill the number of bytes necessary to
83encode the network number in network byte order.
84.Pp
85The only values for
86.Fa af
87currently supported are
88.Dv AF_INET
89and
90.Dv AF_INET6 .
91.Fa size
92is the size of the result buffer
93.Fa dst .
94.Sh NETWORK NUMBERS (IP VERSION 4)
95The external representation of Internet network numbers may be specified in
96one of the following forms:
97.Bd -literal -offset indent
98a
99a.b
100a.b.c
101a.b.c.d
102.Ed
103.Pp
104Any of the above four forms may have
105.Dq Li /bits
106appended where
107.Dq Li bits
108is in the range
109.Li 0-32
110and is used to explicitly specify the number of bits in the network address.
111When
112.Dq Li /bits
113is not specified, the number of bits in the network address is calculated
114as the larger of the number of bits in the class to which the address
115belongs and the number of bits provided rounded up modulo 8.
116Examples:
117.Pp
118.Bl -tag -width 10.1.2.3/24 -offset indent -compact
119.It Li 10
120an 8-bit network number (class A), value
121.Li 10.0.0.0 .
122.It Li 192
123a 24-bit network number (class C), value
124.Li 192.0.0.0 .
125.It Li 10.10
126a 16-bit network number, value
127.Li 10.10.0.0 .
128.It Li 10.1.2
129a 24-bit network number, value
130.Li 10.1.2.0 .
131.It Li 10.1.2.3
132a 32-bit network number, value
133.Li 10.1.2.3 .
134.It Li 10.1.2.3/24
135a 24-bit network number (explicit), value
136.Li 10.1.2.3 .
137.El
138.Pp
139Note that when the number of bits is specified using
140.Dq Li /bits
141notation, the value of the address still includes all bits supplied
142in the external representation, even those bits which are the host
143part of an Internet address.
144Also, unlike
145.Xr inet_pton 3
146where the external representation is assumed to be a host address, the
147external representation for
148.Fn inet_net_pton
149is assumed to be a network address.
150Thus
151.Dq Li 10.1
152is assumed to be
153.Dq Li 10.1.0.0
154not
155.Dq Li 10.0.0.1
156.Pp
157All numbers supplied as
158.Dq parts
159in a
160.Ql \&.
161notation
162may be decimal, octal, or hexadecimal, as specified
163in the C language (i.e., a leading 0x or 0X implies
164hexadecimal; otherwise, a leading 0 implies octal;
165otherwise, the number is interpreted as decimal).
166.Sh NETWORK NUMBERS (IP VERSION 6)
167See
168.Xr inet_pton 3
169for valid external representations of IP version 6 addresses.
170A valid external representation may have
171.Dq Li /bits
172appended where
173.Dq Li bits
174is in the range
175.Li 0-128
176and is used to explicitly specify the number of bits in the network address.
177When
178.Dq Li /bits
179is not specified, 128 is used.
180Note that when the number of bits is specified using
181.Dq Li /bits
182notation, the value of the address still includes all bits supplied
183in the external representation, even those bits which are the host
184part of an Internet address.
185.Sh SEE ALSO
186.Xr htonl 3 ,
187.Xr inet_pton 3 ,
188.Xr inet 4 ,
189.Xr hosts 5
190.Sh HISTORY
191The
192.Nm inet_net_ntop
193and
194.Nm inet_net_pton
195functions 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
deleted file mode 100644
index 1f1c4bed12..0000000000
--- a/src/lib/libc/net/inet_net_ntop.c
+++ /dev/null
@@ -1,160 +0,0 @@
1/* $OpenBSD: inet_net_ntop.c,v 1.9 2019/07/03 03:24:04 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 <errno.h>
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30
31static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t);
32static 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 */
44char *
45inet_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 */
71static char *
72inet_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 *dst++ = '.';
118 m = ((1 << b) - 1) << (8 - b);
119 advance = snprintf(dst, ep - dst, "%u", *src & m);
120 if (advance <= 0 || advance >= ep - dst)
121 goto emsgsize;
122 dst += advance;
123 }
124
125 /* Format CIDR /width. */
126 if (ep - dst < sizeof "/32")
127 goto emsgsize;
128 advance = snprintf(dst, ep - dst, "/%u", bits);
129 if (advance <= 0 || advance >= ep - dst)
130 goto emsgsize;
131 dst += advance;
132 return (odst);
133
134 emsgsize:
135 errno = EMSGSIZE;
136 return (NULL);
137}
138
139static char *
140inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)
141{
142 int ret;
143 char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")];
144
145 if (bits < 0 || bits > 128) {
146 errno = EINVAL;
147 return (NULL);
148 }
149
150 if (inet_ntop(AF_INET6, src, buf, size) == NULL)
151 return (NULL);
152
153 ret = snprintf(dst, size, "%s/%d", buf, bits);
154 if (ret < 0 || ret >= size) {
155 errno = EMSGSIZE;
156 return (NULL);
157 }
158
159 return (dst);
160}
diff --git a/src/lib/libc/net/inet_net_pton.c b/src/lib/libc/net/inet_net_pton.c
deleted file mode 100644
index c5baa4d6f4..0000000000
--- a/src/lib/libc/net/inet_net_pton.c
+++ /dev/null
@@ -1,246 +0,0 @@
1/* $OpenBSD: inet_net_pton.c,v 1.14 2022/12/27 17:10:06 jmc Exp $ */
2
3/*
4 * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org>
5 * Copyright (c) 1996,1999 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
33static int inet_net_pton_ipv4(const char *, u_char *, size_t);
34static 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 */
49int
50inet_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 */
79static int
80inet_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 tmp = 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 if (dirty == 0)
103 tmp = n;
104 else
105 tmp = (tmp << 4) | n;
106 if (++dirty == 2) {
107 if (size-- == 0)
108 goto emsgsize;
109 *dst++ = (u_char) tmp;
110 dirty = 0;
111 }
112 }
113 if (dirty) { /* Odd trailing nybble? */
114 if (size-- == 0)
115 goto emsgsize;
116 *dst++ = (u_char) (tmp << 4);
117 }
118 } else if (isascii(ch) && isdigit(ch)) {
119 /* Decimal: eat dotted digit string. */
120 for (;;) {
121 tmp = 0;
122 do {
123 n = strchr(digits, ch) - digits;
124 assert(n >= 0 && n <= 9);
125 tmp *= 10;
126 tmp += n;
127 if (tmp > 255)
128 goto enoent;
129 } while ((ch = (unsigned char)*src++) != '\0' &&
130 isascii(ch) && isdigit(ch));
131 if (size-- == 0)
132 goto emsgsize;
133 *dst++ = (u_char) tmp;
134 if (ch == '\0' || ch == '/')
135 break;
136 if (ch != '.')
137 goto enoent;
138 ch = (unsigned char)*src++;
139 if (!isascii(ch) || !isdigit(ch))
140 goto enoent;
141 }
142 } else
143 goto enoent;
144
145 bits = -1;
146 if (ch == '/' && isascii((unsigned char)src[0]) &&
147 isdigit((unsigned char)src[0]) && dst > odst) {
148 /* CIDR width specifier. Nothing can follow it. */
149 ch = (unsigned char)*src++; /* Skip over the /. */
150 bits = 0;
151 do {
152 n = strchr(digits, ch) - digits;
153 assert(n >= 0 && n <= 9);
154 bits *= 10;
155 bits += n;
156 if (bits > 32)
157 goto emsgsize;
158 } while ((ch = (unsigned char)*src++) != '\0' &&
159 isascii(ch) && isdigit(ch));
160 if (ch != '\0')
161 goto enoent;
162 }
163
164 /* Fiery death and destruction unless we prefetched EOS. */
165 if (ch != '\0')
166 goto enoent;
167
168 /* If nothing was written to the destination, we found no address. */
169 if (dst == odst)
170 goto enoent;
171 /* If no CIDR spec was given, infer width from net class. */
172 if (bits == -1) {
173 if (*odst >= 240) /* Class E */
174 bits = 32;
175 else if (*odst >= 224) /* Class D */
176 bits = 4;
177 else if (*odst >= 192) /* Class C */
178 bits = 24;
179 else if (*odst >= 128) /* Class B */
180 bits = 16;
181 else /* Class A */
182 bits = 8;
183 /* If imputed mask is narrower than specified octets, widen. */
184 if (bits < ((dst - odst) * 8))
185 bits = (dst - odst) * 8;
186 }
187 /* Extend network to cover the actual mask. */
188 while (bits > ((dst - odst) * 8)) {
189 if (size-- == 0)
190 goto emsgsize;
191 *dst++ = '\0';
192 }
193 return (bits);
194
195 enoent:
196 errno = ENOENT;
197 return (-1);
198
199 emsgsize:
200 errno = EMSGSIZE;
201 return (-1);
202}
203
204
205static int
206inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
207{
208 struct in6_addr in6;
209 int ret;
210 int bits;
211 size_t bytes;
212 char buf[INET6_ADDRSTRLEN + sizeof("/128")];
213 char *sep;
214 const char *errstr;
215
216 if (strlcpy(buf, src, sizeof buf) >= sizeof buf) {
217 errno = EMSGSIZE;
218 return (-1);
219 }
220
221 sep = strchr(buf, '/');
222 if (sep != NULL)
223 *sep++ = '\0';
224
225 ret = inet_pton(AF_INET6, buf, &in6);
226 if (ret != 1)
227 return (-1);
228
229 if (sep == NULL)
230 bits = 128;
231 else {
232 bits = strtonum(sep, 0, 128, &errstr);
233 if (errstr) {
234 errno = EINVAL;
235 return (-1);
236 }
237 }
238
239 bytes = (bits + 7) / 8;
240 if (bytes > size) {
241 errno = EMSGSIZE;
242 return (-1);
243 }
244 memcpy(dst, &in6.s6_addr, bytes);
245 return (bits);
246}
diff --git a/src/lib/libc/net/inet_neta.c b/src/lib/libc/net/inet_neta.c
deleted file mode 100644
index e3e7d0eb71..0000000000
--- a/src/lib/libc/net/inet_neta.c
+++ /dev/null
@@ -1,80 +0,0 @@
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 */
40char *
41inet_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
deleted file mode 100644
index 4efceede13..0000000000
--- a/src/lib/libc/net/inet_netof.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/* $OpenBSD: inet_netof.c,v 1.7 2015/01/16 16:48:51 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 <netinet/in.h>
32#include <arpa/inet.h>
33
34/*
35 * Return the network number from an internet
36 * address; handles class a/b/c network #'s.
37 */
38in_addr_t
39inet_netof(struct in_addr in)
40{
41 in_addr_t i = ntohl(in.s_addr);
42
43 if (IN_CLASSA(i))
44 return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
45 else if (IN_CLASSB(i))
46 return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
47 else
48 return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
49}
diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c
deleted file mode 100644
index 7b0cf50e79..0000000000
--- a/src/lib/libc/net/inet_network.c
+++ /dev/null
@@ -1,85 +0,0 @@
1/* $OpenBSD: inet_network.c,v 1.13 2015/10/22 23:55:51 mmcc 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 */
41in_addr_t
42inet_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
49again:
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((unsigned char)*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}
85DEF_WEAK(inet_network);
diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c
deleted file mode 100644
index ff5d93ded2..0000000000
--- a/src/lib/libc/net/inet_ntoa.c
+++ /dev/null
@@ -1,51 +0,0 @@
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
40char *
41inet_ntoa(struct in_addr in)
42{
43 static char b[18];
44 char *p;
45
46 p = (char *)&in;
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.3 b/src/lib/libc/net/inet_ntop.3
deleted file mode 100644
index e5c1c574f8..0000000000
--- a/src/lib/libc/net/inet_ntop.3
+++ /dev/null
@@ -1,213 +0,0 @@
1.\" $OpenBSD: inet_ntop.3,v 1.6 2022/09/11 06:38:10 jmc 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: September 11 2022 $
34.Dt INET_NTOP 3
35.Os
36.Sh NAME
37.Nm inet_ntop ,
38.Nm inet_pton
39.Nd convert Internet addresses between presentation and network formats
40.Sh SYNOPSIS
41.In sys/socket.h
42.In arpa/inet.h
43.Ft const char *
44.Fn inet_ntop "int af" "const void * restrict src" "char * restrict dst" "socklen_t size"
45.Ft int
46.Fn inet_pton "int af" "const char * restrict src" "void * restrict dst"
47.Sh DESCRIPTION
48The
49.Fn inet_pton
50function converts a presentation format address (that is, printable form
51as held in a character string) to network format (usually a
52.Vt struct in_addr
53or some other internal binary representation, in network byte order).
54It returns 1 if the address was valid for the specified address family;
550 if the address wasn't parseable in the specified address family; or \-1
56if some system error occurred (in which case
57.Va errno
58will have been set).
59This function is presently valid for
60.Dv AF_INET
61and
62.Dv AF_INET6 .
63.Pp
64The function
65.Fn inet_ntop
66converts an address from network format to presentation format.
67It returns
68.Dv NULL
69if a system
70error occurs (in which case,
71.Va errno
72will have been set), or it returns a pointer to the destination string.
73.Pp
74All Internet addresses are returned in network
75order (bytes ordered from left to right).
76.Sh INTERNET ADDRESSES (IP VERSION 4)
77Values must be specified using the standard dot notation:
78.Bd -literal -offset indent
79a.b.c.d
80.Ed
81.Pp
82All four parts must be decimal numbers between 0 and 255, inclusive,
83and are assigned, from left to right,
84to the four bytes of an Internet address.
85Note that when an Internet address is viewed as a 32-bit integer
86quantity on a system that uses little-endian byte order
87(such as AMD64 or ARM processors)
88the bytes referred to above appear as
89.Dq Li d.c.b.a .
90That is, little-endian bytes are ordered from right to left.
91.Sh INTERNET ADDRESSES (IP VERSION 6)
92In order to support scoped IPv6 addresses,
93.Xr getaddrinfo 3
94and
95.Xr getnameinfo 3
96are recommended rather than the functions presented here.
97.Pp
98The presentation format of an IPv6 address is given in RFC 4291:
99.Pp
100There are three conventional forms for representing IPv6 addresses as
101text strings:
102.Bl -enum
103.It
104The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the
105hexadecimal values of the eight 16-bit pieces of the address.
106Examples:
107.Bd -literal -offset indent
108FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
1091080:0:0:0:8:800:200C:417A
110.Ed
111.Pp
112Note that it is not necessary to write the leading zeros in an
113individual field, but there must be at least one numeral in
114every field (except for the case described in 2.).
115.It
116Due to the method of allocating certain styles of IPv6
117addresses, it will be common for addresses to contain long
118strings of zero bits.
119In order to make writing addresses
120containing zero bits easier, a special syntax is available to
121compress the zeros.
122The use of
123.Dq \&:\&:
124indicates multiple groups
125of 16 bits of zeros.
126The
127.Dq \&:\&:
128can only appear once in an
129address.
130The
131.Dq \&:\&:
132can also be used to compress the leading and/or trailing zeros in an address.
133.Pp
134For example the following addresses:
135.Bd -literal -offset indent
1361080:0:0:0:8:800:200C:417A a unicast address
137FF01:0:0:0:0:0:0:43 a multicast address
1380:0:0:0:0:0:0:1 the loopback address
1390:0:0:0:0:0:0:0 the unspecified addresses
140.Ed
141.Pp
142may be represented as:
143.Bd -literal -offset indent
1441080::8:800:200C:417A a unicast address
145FF01::43 a multicast address
146::1 the loopback address
147:: the unspecified addresses
148.Ed
149.It
150An alternative form that is sometimes more convenient when
151dealing with a mixed environment of IPv4 and IPv6 nodes is
152x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values
153of the six high-order 16-bit pieces of the address, and the 'd's
154are the decimal values of the four low-order 8-bit pieces of the
155address (standard IPv4 representation).
156Examples:
157.Bd -literal -offset indent
1580:0:0:0:0:0:13.1.68.3
1590:0:0:0:0:FFFF:129.144.52.38
160.Ed
161.Pp
162or in compressed form:
163.Bd -literal -offset indent
164::13.1.68.3
165::FFFF:129.144.52.38
166.Ed
167.El
168.Sh SEE ALSO
169.Xr gethostbyname 3 ,
170.Xr inet_addr 3 ,
171.Xr inet_net_ntop 3 ,
172.Xr hosts 5
173.Sh STANDARDS
174The
175.Nm inet_ntop
176and
177.Nm inet_pton
178functions conform to the IETF IPv6 BSD API and address formatting
179specifications, as well as
180.St -p1003.1-2008 .
181.Sh HISTORY
182The
183.Nm inet_pton
184and
185.Nm inet_ntop
186functions appeared in BIND 4.9.4.
187.Sh CAVEATS
188Note that
189.Nm inet_pton
190does not accept 1-, 2-, or 3-part dotted addresses;
191all four parts must be specified and must be in decimal
192(and not octal or hexadecimal).
193This is a narrower input set than that accepted by
194.Nm inet_aton .
195.Pp
196.Rs
197.%A R. Gilligan
198.%A S. Thomson
199.%A J. Bound
200.%A J. McCann
201.%A W. Stevens
202.%D February 2003
203.%R RFC 3493
204.%T Basic Socket Interface Extensions for IPv6
205.Re
206.Pp
207.Rs
208.%A R. Hinden
209.%A S. Deering
210.%D February 2006
211.%R RFC 4291
212.%T IP Version 6 Addressing Architecture
213.Re
diff --git a/src/lib/libc/net/inet_ntop.c b/src/lib/libc/net/inet_ntop.c
deleted file mode 100644
index 2bb11c2ab2..0000000000
--- a/src/lib/libc/net/inet_ntop.c
+++ /dev/null
@@ -1,205 +0,0 @@
1/* $OpenBSD: inet_ntop.c,v 1.13 2016/09/21 04:38:56 guenther 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/types.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <arpa/nameser.h>
24#include <string.h>
25#include <errno.h>
26#include <stdio.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
33static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
34static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
35
36/* const char *
37 * inet_ntop(af, src, dst, size)
38 * convert a network format address to presentation format.
39 * return:
40 * pointer to presentation format address (`dst'), or NULL (see errno).
41 * author:
42 * Paul Vixie, 1996.
43 */
44const char *
45inet_ntop(int af, const void *src, char *dst, socklen_t size)
46{
47 switch (af) {
48 case AF_INET:
49 return (inet_ntop4(src, dst, size));
50 case AF_INET6:
51 return (inet_ntop6(src, dst, size));
52 default:
53 errno = EAFNOSUPPORT;
54 return (NULL);
55 }
56 /* NOTREACHED */
57}
58DEF_WEAK(inet_ntop);
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 */
71static const char *
72inet_ntop4(const u_char *src, char *dst, size_t size)
73{
74 char tmp[sizeof "255.255.255.255"];
75 int l;
76
77 l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
78 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 */
93static const char *
94inet_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, 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
deleted file mode 100644
index 45bafd2ffc..0000000000
--- a/src/lib/libc/net/inet_pton.c
+++ /dev/null
@@ -1,213 +0,0 @@
1/* $OpenBSD: inet_pton.c,v 1.11 2024/09/03 17:05:59 deraadt 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/types.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <arpa/nameser.h>
24#include <string.h>
25#include <errno.h>
26
27/*
28 * WARNING: Don't even consider trying to compile this on a system where
29 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
30 */
31
32static int inet_pton4(const char *src, u_char *dst);
33static int inet_pton6(const char *src, u_char *dst);
34
35/* int
36 * inet_pton(af, src, dst)
37 * convert from presentation format (which usually means ASCII printable)
38 * to network format (which is usually some kind of binary format).
39 * return:
40 * 1 if the address was valid for the specified address family
41 * 0 if the address wasn't valid (`dst' is untouched in this case)
42 * -1 if some other error occurred (`dst' is untouched in this case, too)
43 * author:
44 * Paul Vixie, 1996.
45 */
46int
47inet_pton(int af, const char *src, void *dst)
48{
49 switch (af) {
50 case AF_INET:
51 return (inet_pton4(src, dst));
52 case AF_INET6:
53 return (inet_pton6(src, dst));
54 default:
55 errno = EAFNOSUPPORT;
56 return (-1);
57 }
58 /* NOTREACHED */
59}
60DEF_WEAK(inet_pton);
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 */
72static int
73inet_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 */
123static int
124inet_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
deleted file mode 100644
index 3837489448..0000000000
--- a/src/lib/libc/net/ip6opt.c
+++ /dev/null
@@ -1,285 +0,0 @@
1/* $OpenBSD: ip6opt.c,v 1.10 2020/01/22 07:52:37 deraadt 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/types.h>
34#include <sys/socket.h>
35
36#include <netinet/in.h>
37#include <netinet/ip6.h>
38
39#include <string.h>
40#include <stdio.h>
41
42static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
43
44/*
45 * Calculate the length of a given IPv6 option. Also checks
46 * if the option is safely stored in user's buffer according to the
47 * calculated length and the limitation of the buffer.
48 */
49static int
50ip6optlen(u_int8_t *opt, u_int8_t *lim)
51{
52 int optlen;
53
54 if (*opt == IP6OPT_PAD1)
55 optlen = 1;
56 else {
57 /* is there enough space to store type and len? */
58 if (opt + 2 > lim)
59 return (0);
60 optlen = *(opt + 1) + 2;
61 }
62 if (opt + optlen <= lim)
63 return (optlen);
64
65 return (0);
66}
67
68/*
69 * The following functions are defined in RFC3542, which is a successor
70 * of RFC2292.
71 */
72
73int
74inet6_opt_init(void *extbuf, socklen_t extlen)
75{
76 struct ip6_ext *ext = (struct ip6_ext *)extbuf;
77
78 if (ext) {
79 if (extlen <= 0 || (extlen % 8))
80 return (-1);
81 ext->ip6e_len = (extlen >> 3) - 1;
82 }
83
84 return (2); /* sizeof the next and the length fields */
85}
86
87int
88inet6_opt_append(void *extbuf, socklen_t extlen, int offset, u_int8_t type,
89 socklen_t len, u_int8_t align, void **databufp)
90{
91 int currentlen = offset, padlen = 0;
92
93 /*
94 * The option type must have a value from 2 to 255, inclusive.
95 * (0 and 1 are reserved for the Pad1 and PadN options, respectively.)
96 */
97#if 0 /* always false */
98 if (type < 2 || type > 255)
99#else
100 if (type < 2)
101#endif
102 return (-1);
103
104 /*
105 * The option data length must have a value between 0 and 255,
106 * inclusive, and is the length of the option data that follows.
107 */
108 if (len > 255)
109 return (-1);
110
111 /*
112 * The align parameter must have a value of 1, 2, 4, or 8.
113 * The align value can not exceed the value of len.
114 */
115 if (align != 1 && align != 2 && align != 4 && align != 8)
116 return (-1);
117 if (align > len)
118 return (-1);
119
120 /* Calculate the padding length. */
121 currentlen += 2 + len; /* 2 means "type + len" */
122 if (currentlen % align)
123 padlen = align - (currentlen % align);
124
125 /* The option must fit in the extension header buffer. */
126 currentlen += padlen;
127 if (extlen && /* XXX: right? */
128 currentlen > extlen)
129 return (-1);
130
131 if (extbuf) {
132 u_int8_t *optp = (u_int8_t *)extbuf + offset;
133
134 if (padlen == 1) {
135 /* insert a Pad1 option */
136 *optp = IP6OPT_PAD1;
137 optp++;
138 } else if (padlen > 0) {
139 /* insert a PadN option for alignment */
140 *optp++ = IP6OPT_PADN;
141 *optp++ = padlen - 2;
142 memset(optp, 0, padlen - 2);
143 optp += (padlen - 2);
144 }
145
146 *optp++ = type;
147 *optp++ = len;
148
149 *databufp = optp;
150 }
151
152 return (currentlen);
153}
154
155int
156inet6_opt_finish(void *extbuf, socklen_t extlen, int offset)
157{
158 int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0;
159
160 if (extbuf) {
161 u_int8_t *padp;
162 int padlen = updatelen - offset;
163
164 if (updatelen > extlen)
165 return (-1);
166
167 padp = (u_int8_t *)extbuf + offset;
168 if (padlen == 1)
169 *padp = IP6OPT_PAD1;
170 else if (padlen > 0) {
171 *padp++ = IP6OPT_PADN;
172 *padp++ = (padlen - 2);
173 memset(padp, 0, padlen - 2);
174 }
175 }
176
177 return (updatelen);
178}
179
180int
181inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
182{
183
184 memcpy((u_int8_t *)databuf + offset, val, vallen);
185 return (offset + vallen);
186}
187
188int
189inet6_opt_next(void *extbuf, socklen_t extlen, int offset, u_int8_t *typep,
190 socklen_t *lenp, void **databufp)
191{
192 u_int8_t *optp, *lim;
193 int optlen;
194
195 /* Validate extlen. XXX: is the variable really necessary?? */
196 if (extlen == 0 || (extlen % 8))
197 return (-1);
198 lim = (u_int8_t *)extbuf + extlen;
199
200 /*
201 * If this is the first time this function called for this options
202 * header, simply return the 1st option.
203 * Otherwise, search the option list for the next option.
204 */
205 if (offset == 0)
206 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
207 else
208 optp = (u_int8_t *)extbuf + offset;
209
210 /* Find the next option skipping any padding options. */
211 while (optp < lim) {
212 switch(*optp) {
213 case IP6OPT_PAD1:
214 optp++;
215 break;
216 case IP6OPT_PADN:
217 if ((optlen = ip6optlen(optp, lim)) == 0)
218 goto optend;
219 optp += optlen;
220 break;
221 default: /* found */
222 if ((optlen = ip6optlen(optp, lim)) == 0)
223 goto optend;
224 *typep = *optp;
225 *lenp = optlen - 2;
226 *databufp = optp + 2;
227 return (optp + optlen - (u_int8_t *)extbuf);
228 }
229 }
230
231 optend:
232 *databufp = NULL; /* for safety */
233 return (-1);
234}
235
236int
237inet6_opt_find(void *extbuf, socklen_t extlen, int offset, u_int8_t type,
238 socklen_t *lenp, void **databufp)
239{
240 u_int8_t *optp, *lim;
241 int optlen;
242
243 /* Validate extlen. XXX: is the variable really necessary?? */
244 if (extlen == 0 || (extlen % 8))
245 return (-1);
246 lim = (u_int8_t *)extbuf + extlen;
247
248 /*
249 * If this is the first time this function called for this options
250 * header, simply return the 1st option.
251 * Otherwise, search the option list for the next option.
252 */
253 if (offset == 0)
254 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
255 else
256 optp = (u_int8_t *)extbuf + offset;
257
258 /* Find the specified option */
259 while (optp < lim) {
260 if ((optlen = ip6optlen(optp, lim)) == 0)
261 goto optend;
262
263 if (*optp == type) { /* found */
264 *lenp = optlen - 2;
265 *databufp = optp + 2;
266 return (optp + optlen - (u_int8_t *)extbuf);
267 }
268
269 optp += optlen;
270 }
271
272 optend:
273 *databufp = NULL; /* for safety */
274 return (-1);
275}
276
277int
278inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
279{
280
281 /* we can't assume alignment here */
282 memcpy(val, (u_int8_t *)databuf + offset, vallen);
283
284 return (offset + vallen);
285}
diff --git a/src/lib/libc/net/link_ntoa.3 b/src/lib/libc/net/link_ntoa.3
deleted file mode 100644
index 8953677441..0000000000
--- a/src/lib/libc/net/link_ntoa.3
+++ /dev/null
@@ -1,78 +0,0 @@
1.\" $OpenBSD: link_ntoa.3,v 1.2 2015/09/10 10:14:20 jmc 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: September 10 2015 $
34.Dt LINK_NTOA 3
35.Os
36.Sh NAME
37.Nm link_ntoa
38.Nd elementary address specification routine for link level access
39.Sh SYNOPSIS
40.In sys/types.h
41.In sys/socket.h
42.In net/if_dl.h
43.Ft char *
44.Fn link_ntoa "const struct sockaddr_dl *sdl"
45.Sh DESCRIPTION
46The
47.Fn link_ntoa
48function takes
49a link-level
50address and returns an
51.Tn ASCII
52string representing some of the information present,
53including the link level address itself, and the interface name
54or number, if present.
55This facility is experimental and is
56still subject to change.
57.Sh RETURN VALUES
58.Fn link_ntoa
59always returns a NUL-terminated string.
60.Sh SEE ALSO
61.Xr ifconfig 8
62.Sh HISTORY
63The
64.Fn link_ntoa
65function appeared in
66.Bx 4.3 Reno .
67.Sh BUGS
68The returned values for
69.Fn link_ntoa
70reside in a static memory area.
71.Pp
72If the
73.Fa sdl_len
74field of the link socket address
75.Fa sdl
76is 0,
77.Fn link_ntoa
78will not insert a colon before the interface address bytes.
diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c
deleted file mode 100644
index 9101e23a86..0000000000
--- a/src/lib/libc/net/linkaddr.c
+++ /dev/null
@@ -1,85 +0,0 @@
1/* $OpenBSD: linkaddr.c,v 1.9 2016/12/08 03:20:50 millert 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.h>
34#include <net/if_dl.h>
35#include <string.h>
36
37static const char hexlist[] = "0123456789abcdef";
38
39char *
40link_ntoa(const struct sockaddr_dl *sdl)
41{
42 static char obuf[64];
43 char *out;
44 const u_char *in, *inlim;
45 int namelen, i, rem;
46
47 namelen = (sdl->sdl_nlen <= IFNAMSIZ) ? sdl->sdl_nlen : IFNAMSIZ;
48
49 out = obuf;
50 rem = sizeof(obuf);
51 if (namelen > 0) {
52 memcpy(out, sdl->sdl_data, namelen);
53 out += namelen;
54 rem -= namelen;
55 if (sdl->sdl_alen > 0) {
56 *out++ = ':';
57 rem--;
58 }
59 }
60
61 in = (const u_char *)sdl->sdl_data + sdl->sdl_nlen;
62 inlim = in + sdl->sdl_alen;
63
64 while (in < inlim && rem > 1) {
65 if (in != (const u_char *)sdl->sdl_data + sdl->sdl_nlen) {
66 *out++ = '.';
67 rem--;
68 }
69 i = *in++;
70 if (i > 0xf) {
71 if (rem < 3)
72 break;
73 *out++ = hexlist[i >> 4];
74 *out++ = hexlist[i & 0xf];
75 rem -= 2;
76 } else {
77 if (rem < 2)
78 break;
79 *out++ = hexlist[i];
80 rem--;
81 }
82 }
83 *out = 0;
84 return (obuf);
85}
diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c
deleted file mode 100644
index 7592398e8c..0000000000
--- a/src/lib/libc/net/ntohl.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: ntohl.c,v 1.8 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef ntohl
10
11uint32_t
12ntohl(uint32_t x)
13{
14 return be32toh(x);
15}
diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c
deleted file mode 100644
index ef22ea3068..0000000000
--- a/src/lib/libc/net/ntohs.c
+++ /dev/null
@@ -1,15 +0,0 @@
1/* $OpenBSD: ntohs.c,v 1.10 2024/04/15 14:30:48 naddy Exp $ */
2/*
3 * Public domain.
4 */
5
6#include <sys/types.h>
7#include <endian.h>
8
9#undef ntohs
10
11uint16_t
12ntohs(uint16_t x)
13{
14 return be16toh(x);
15}
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3
deleted file mode 100644
index b2a5cdea9b..0000000000
--- a/src/lib/libc/net/rcmd.3
+++ /dev/null
@@ -1,232 +0,0 @@
1.\" $OpenBSD: rcmd.3,v 1.34 2016/05/28 15:48:30 millert 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: May 28 2016 $
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 ruserok
39.Nd routines for returning a stream to a remote command
40.Sh SYNOPSIS
41.In unistd.h
42.Ft int
43.Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p"
44.Ft int
45.Fn rcmd_af "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" "int af"
46.Ft int
47.Fn rresvport "int *port"
48.Ft int
49.Fn rresvport_af "int *port" "int af"
50.Ft int
51.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser"
52.Sh DESCRIPTION
53The
54.Fn rcmd
55function is used by the superuser to execute a command on a remote
56machine using an authentication scheme based on reserved
57port numbers.
58If the calling process is not setuid, the
59.Ev RSH
60environment variable is set, and
61.Fa inport
62is
63.Dq shell/tcp ,
64.Xr rcmdsh 3
65is called instead with the value of
66.Ev RSH .
67Alternately, if the user is not the superuser,
68.Fn rcmd
69will invoke
70.Xr rcmdsh 3
71to run the command via
72.Xr ssh 1 .
73While
74.Fn rcmd
75can handle IPv4 cases only,
76the
77.Fn rcmd_af
78function can handle other cases as well.
79.Pp
80The
81.Fn rresvport
82and
83.Fn rresvport_af
84functions return a descriptor to a socket
85with an address in the privileged port space.
86The
87.Fn ruserok
88function is used by servers
89to authenticate clients requesting service with
90.Fn rcmd .
91.Pp
92The
93.Fn rcmd
94function looks up the host
95.Fa *ahost
96using
97.Xr getaddrinfo 3
98and, if the host exists,
99.Fa *ahost
100is set to the canonical name of the host.
101A connection is then established to a server
102residing at the well-known Internet port
103.Fa inport .
104If the user is not the superuser, the only valid port is
105.Dq shell/tcp
106(usually port 514).
107.Pp
108If the connection succeeds,
109a socket in the Internet domain of type
110.Dv SOCK_STREAM
111is returned to the caller, and given to the remote
112command as stdin and stdout.
113If
114.Fa fd2p
115is non-zero, then an auxiliary channel to a control
116process will be set up, and a descriptor for it will be placed
117in
118.Fa *fd2p .
119The control process will return diagnostic
120output from the command (unit 2) on this channel, and will also
121accept bytes on this channel as being
122.Tn UNIX
123signal numbers, to be
124forwarded to the process group of the command.
125If
126.Fa fd2p
127is
128.Va NULL ,
129then the standard error (unit 2 of the remote command) will be made
130the same as the standard output and no provision is made for sending
131arbitrary signals to the remote process, although you may be able to
132get its attention by using out-of-band data.
133Note that if the user is not the superuser,
134.Fa fd2p
135must be
136.Va NULL .
137.Pp
138.Fn rcmd_af
139takes address family in the last argument.
140If the last argument is
141.Dv AF_UNSPEC ,
142interpretation of
143.Fa *ahost
144will obey the underlying address resolution like DNS.
145.Pp
146The
147.Fn rresvport
148and
149.Fn rresvport_af
150functions are used to obtain a socket with a privileged
151address bound to it.
152This socket is suitable for use by
153.Fn rcmd
154and several other functions.
155Privileged Internet ports are those in the range 0 to
156.Va IPPORT_RESERVED - 1 ,
157which happens to be 1023.
158Only the superuser is allowed to bind an address of this sort to a socket.
159.Fn rresvport
160and
161.Fn rresvport_af
162need to be seeded with a port number; if that port
163is not available these functions will find another.
164.Pp
165The
166.Fn ruserok
167function takes a remote host's name, two user names,
168and a flag indicating whether the local user's
169name is that of the superuser.
170Then, if the user is
171.Em not
172the superuser, it checks the
173.Pa /etc/hosts.equiv
174file.
175If that lookup is not done, or is unsuccessful, the
176.Pa .rhosts
177in the local user's home directory is checked to see if the request for
178service is allowed.
179.Pp
180If this file does not exist, is not a regular file, is owned by anyone
181other than the user or the superuser, or is writeable by anyone other
182than the owner, the check automatically fails.
183Zero is returned if the machine name is listed in the
184.Pa hosts.equiv
185file, or the host and remote user name are found in the
186.Pa .rhosts
187file; otherwise
188.Fn ruserok
189returns \-1.
190If the local domain (as obtained from
191.Xr getaddrinfo 3 )
192is the same as the remote domain, only the machine name need be specified.
193.Pp
194.Fn ruserok
195implicitly requires trusting the DNS server for the remote host's domain.
196.Sh DIAGNOSTICS
197The
198.Fn rcmd
199function returns a valid socket descriptor on success.
200It returns \-1 on error and prints a diagnostic message on the standard error.
201.Pp
202The
203.Fn rresvport
204and
205.Fn rresvport_af
206functions return a valid, bound socket descriptor on success.
207It returns \-1 on error with the global value
208.Va errno
209set according to the reason for failure.
210The error code
211.Er EAGAIN
212is overloaded to mean
213.Dq all network ports in use .
214.Sh SEE ALSO
215.Xr ssh 1 ,
216.Xr intro 2 ,
217.Xr bindresvport 3 ,
218.Xr bindresvport_sa 3 ,
219.Xr rcmdsh 3
220.Sh HISTORY
221These
222functions appeared in
223.Bx 4.2 .
224.Pp
225The
226.Fn iruserok
227and
228.Fn iruserok_sa
229functions, IP address based versions of
230.Fn ruserok ,
231were removed in
232.Ox 6.0 .
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c
deleted file mode 100644
index bf68603649..0000000000
--- a/src/lib/libc/net/rcmd.c
+++ /dev/null
@@ -1,296 +0,0 @@
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 <signal.h>
38#include <fcntl.h>
39#include <netdb.h>
40#include <unistd.h>
41#include <limits.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 <time.h>
49#include <stdlib.h>
50#include <poll.h>
51
52int
53rcmd(char **ahost, int rport, const char *locuser, const char *remuser,
54 const char *cmd, int *fd2p)
55{
56 return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET);
57}
58
59int
60rcmd_af(char **ahost, int porta, const char *locuser, const char *remuser,
61 const char *cmd, int *fd2p, int af)
62{
63 static char hbuf[HOST_NAME_MAX+1];
64 char pbuf[NI_MAXSERV];
65 struct addrinfo hints, *res, *r;
66 int error;
67 struct sockaddr_storage from;
68 sigset_t oldmask, mask;
69 pid_t pid;
70 int s, lport;
71 struct timespec timo;
72 char c, *p;
73 int refused;
74 in_port_t rport = porta;
75 int numread;
76
77 /* call rcmdsh() with specified remote shell if appropriate. */
78 if (!issetugid() && (p = getenv("RSH")) && *p) {
79 struct servent *sp = getservbyname("shell", "tcp");
80
81 if (sp && sp->s_port == rport)
82 return (rcmdsh(ahost, rport, locuser, remuser,
83 cmd, p));
84 }
85
86 /* use rsh(1) if non-root and remote port is shell. */
87 if (geteuid()) {
88 struct servent *sp = getservbyname("shell", "tcp");
89
90 if (sp && sp->s_port == rport)
91 return (rcmdsh(ahost, rport, locuser, remuser,
92 cmd, NULL));
93 }
94
95 pid = getpid();
96 snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport));
97 memset(&hints, 0, sizeof(hints));
98 hints.ai_family = af;
99 hints.ai_socktype = SOCK_STREAM;
100 hints.ai_flags = AI_CANONNAME;
101 error = getaddrinfo(*ahost, pbuf, &hints, &res);
102 if (error) {
103 (void)fprintf(stderr, "rcmd: %s: %s\n", *ahost,
104 gai_strerror(error));
105 return (-1);
106 }
107 if (res->ai_canonname) {
108 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf));
109 *ahost = hbuf;
110 } else
111 ; /*XXX*/
112
113 r = res;
114 refused = 0;
115 timespecclear(&timo);
116 sigemptyset(&mask);
117 sigaddset(&mask, SIGURG);
118 sigprocmask(SIG_BLOCK, &mask, &oldmask);
119 for (timo.tv_sec = 1, lport = IPPORT_RESERVED - 1;;) {
120 s = rresvport_af(&lport, r->ai_family);
121 if (s < 0) {
122 if (errno == EAGAIN)
123 (void)fprintf(stderr,
124 "rcmd: socket: All ports in use\n");
125 else
126 (void)fprintf(stderr, "rcmd: socket: %s\n",
127 strerror(errno));
128 if (r->ai_next) {
129 r = r->ai_next;
130 continue;
131 } else {
132 sigprocmask(SIG_SETMASK, &oldmask, NULL);
133 freeaddrinfo(res);
134 return (-1);
135 }
136 }
137 fcntl(s, F_SETOWN, pid);
138 if (connect(s, r->ai_addr, r->ai_addrlen) >= 0)
139 break;
140 (void)close(s);
141 if (errno == EADDRINUSE) {
142 lport--;
143 continue;
144 }
145 if (errno == ECONNREFUSED)
146 refused++;
147 if (r->ai_next) {
148 int oerrno = errno;
149 char hbuf[NI_MAXHOST];
150 const int niflags = NI_NUMERICHOST;
151
152 hbuf[0] = '\0';
153 if (getnameinfo(r->ai_addr, r->ai_addrlen,
154 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
155 strlcpy(hbuf, "(invalid)", sizeof hbuf);
156 (void)fprintf(stderr, "connect to address %s: ", hbuf);
157 errno = oerrno;
158 perror(0);
159 r = r->ai_next;
160 hbuf[0] = '\0';
161 if (getnameinfo(r->ai_addr, r->ai_addrlen,
162 hbuf, sizeof(hbuf), NULL, 0, niflags) != 0)
163 strlcpy(hbuf, "(invalid)", sizeof hbuf);
164 (void)fprintf(stderr, "Trying %s...\n", hbuf);
165 continue;
166 }
167 if (refused && timo.tv_sec <= 16) {
168 (void)nanosleep(&timo, NULL);
169 timo.tv_sec *= 2;
170 r = res;
171 refused = 0;
172 continue;
173 }
174 (void)fprintf(stderr, "%s: %s\n", res->ai_canonname,
175 strerror(errno));
176 sigprocmask(SIG_SETMASK, &oldmask, NULL);
177 freeaddrinfo(res);
178 return (-1);
179 }
180 /* given "af" can be PF_UNSPEC, we need the real af for "s" */
181 af = r->ai_family;
182 freeaddrinfo(res);
183 if (fd2p == 0) {
184 write(s, "", 1);
185 lport = 0;
186 } else {
187 struct pollfd pfd[2];
188 char num[8];
189 int s2 = rresvport_af(&lport, af), s3;
190 socklen_t len = sizeof(from);
191
192 if (s2 < 0)
193 goto bad;
194
195 listen(s2, 1);
196 (void)snprintf(num, sizeof(num), "%d", lport);
197 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
198 (void)fprintf(stderr,
199 "rcmd: write (setting up stderr): %s\n",
200 strerror(errno));
201 (void)close(s2);
202 goto bad;
203 }
204again:
205 pfd[0].fd = s;
206 pfd[0].events = POLLIN;
207 pfd[1].fd = s2;
208 pfd[1].events = POLLIN;
209
210 errno = 0;
211 if (poll(pfd, 2, INFTIM) < 1 ||
212 (pfd[1].revents & (POLLIN|POLLHUP)) == 0) {
213 if (errno != 0)
214 (void)fprintf(stderr,
215 "rcmd: poll (setting up stderr): %s\n",
216 strerror(errno));
217 else
218 (void)fprintf(stderr,
219 "poll: protocol failure in circuit setup\n");
220 (void)close(s2);
221 goto bad;
222 }
223 s3 = accept(s2, (struct sockaddr *)&from, &len);
224 if (s3 < 0) {
225 (void)fprintf(stderr,
226 "rcmd: accept: %s\n", strerror(errno));
227 lport = 0;
228 close(s2);
229 goto bad;
230 }
231
232 /*
233 * XXX careful for ftp bounce attacks. If discovered, shut them
234 * down and check for the real auxiliary channel to connect.
235 */
236 switch (from.ss_family) {
237 case AF_INET:
238 case AF_INET6:
239 if (getnameinfo((struct sockaddr *)&from, len,
240 NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 &&
241 atoi(num) != 20) {
242 break;
243 }
244 close(s3);
245 goto again;
246 default:
247 break;
248 }
249 (void)close(s2);
250
251 *fd2p = s3;
252 switch (from.ss_family) {
253 case AF_INET:
254 case AF_INET6:
255 if (getnameinfo((struct sockaddr *)&from, len,
256 NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 ||
257 (atoi(num) >= IPPORT_RESERVED ||
258 atoi(num) < IPPORT_RESERVED / 2)) {
259 (void)fprintf(stderr,
260 "socket: protocol failure in circuit setup.\n");
261 goto bad2;
262 }
263 break;
264 default:
265 break;
266 }
267 }
268 (void)write(s, locuser, strlen(locuser)+1);
269 (void)write(s, remuser, strlen(remuser)+1);
270 (void)write(s, cmd, strlen(cmd)+1);
271 if ((numread = read(s, &c, 1)) != 1) {
272 (void)fprintf(stderr,
273 "rcmd: %s: %s\n", *ahost,
274 numread == -1 ? strerror(errno) : "Short read");
275 goto bad2;
276 }
277 if (c != 0) {
278 while (read(s, &c, 1) == 1) {
279 (void)write(STDERR_FILENO, &c, 1);
280 if (c == '\n')
281 break;
282 }
283 goto bad2;
284 }
285 sigprocmask(SIG_SETMASK, &oldmask, NULL);
286 return (s);
287bad2:
288 if (lport)
289 (void)close(*fd2p);
290bad:
291 (void)close(s);
292 sigprocmask(SIG_SETMASK, &oldmask, NULL);
293 return (-1);
294}
295DEF_WEAK(rcmd_af);
296
diff --git a/src/lib/libc/net/rcmdsh.3 b/src/lib/libc/net/rcmdsh.3
deleted file mode 100644
index 9092d19f81..0000000000
--- a/src/lib/libc/net/rcmdsh.3
+++ /dev/null
@@ -1,95 +0,0 @@
1.\" $OpenBSD: rcmdsh.3,v 1.18 2016/05/28 15:48:30 millert 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: May 28 2016 $
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
41The
42.Fn rcmdsh
43function is used by normal users to execute a command on a remote machine
44using an authentication scheme based on reserved port numbers using
45.Xr ssh 1
46or the value of
47.Fa rshprog
48(if non-null).
49.Fa rshprog
50may be a fully-qualified path, a non-qualified command, or a command containing
51space-separated command line arguments.
52.Pp
53The
54.Fn rcmdsh
55function looks up the host
56.Fa *ahost
57using
58.Xr getaddrinfo 3
59and, if the host exists,
60.Fa *ahost
61is set to the canonical name of the host.
62A connection is then established to
63a server residing at the well-known Internet port
64.Li shell/tcp
65(or whatever port is used by
66.Fa rshprog ) .
67The parameter
68.Fa inport
69is ignored; it is only included to provide an interface similar to
70.Xr rcmd 3 .
71.Pp
72If the connection succeeds, a socket in the
73.Ux Ns -domain
74of type
75.Dv SOCK_STREAM
76is returned to the caller, and given to the remote
77command as stdin and stdout, and stderr.
78.Sh DIAGNOSTICS
79The
80.Fn rcmdsh
81function returns a valid socket descriptor on success.
82It returns \-1 on error and prints a diagnostic message on the standard error.
83.Sh SEE ALSO
84.Xr ssh 1 ,
85.Xr socketpair 2 ,
86.Xr rcmd 3
87.Sh HISTORY
88The
89.Fn rcmdsh
90function first appeared in
91.Ox 2.0 .
92.Sh BUGS
93If
94.Xr ssh 1
95encounters 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
deleted file mode 100644
index 66caac3f3d..0000000000
--- a/src/lib/libc/net/rcmdsh.c
+++ /dev/null
@@ -1,194 +0,0 @@
1/* $OpenBSD: rcmdsh.c,v 1.20 2019/06/28 13:32:42 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 <limits.h>
42#include <netdb.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <pwd.h>
47#include <paths.h>
48#include <unistd.h>
49
50/*
51 * This is a replacement rcmd() function that uses the ssh(1)
52 * program in place of a direct rcmd(3) function call so as to
53 * avoid having to be root. Note that rport is ignored.
54 */
55int
56rcmdsh(char **ahost, int rport, const char *locuser, const char *remuser,
57 const char *cmd, char *rshprog)
58{
59 static char hbuf[HOST_NAME_MAX+1];
60 struct addrinfo hint, *res;
61 int sp[2];
62 pid_t cpid;
63 char *p, pwbuf[_PW_BUF_LEN];
64 struct passwd pwstore, *pw = NULL;
65
66 /* What rsh/shell to use. */
67 if (rshprog == NULL)
68 rshprog = _PATH_RSH;
69
70 /* locuser must exist on this host. */
71 getpwnam_r(locuser, &pwstore, pwbuf, sizeof(pwbuf), &pw);
72 if (pw == NULL) {
73 (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser);
74 return(-1);
75 }
76
77 /* Validate remote hostname. */
78 if (strcmp(*ahost, "localhost") != 0) {
79 memset(&hint, 0, sizeof(hint));
80 hint.ai_family = PF_UNSPEC;
81 hint.ai_flags = AI_CANONNAME;
82 if (getaddrinfo(*ahost, NULL, &hint, &res) == 0) {
83 if (res->ai_canonname) {
84 strlcpy(hbuf, res->ai_canonname, sizeof(hbuf));
85 *ahost = hbuf;
86 }
87 freeaddrinfo(res);
88 }
89 }
90
91 /* Get a socketpair we'll use for stdin and stdout. */
92 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) == -1) {
93 perror("rcmdsh: socketpair");
94 return(-1);
95 }
96
97 cpid = fork();
98 if (cpid == -1) {
99 perror("rcmdsh: fork failed");
100 return(-1);
101 } else if (cpid == 0) {
102 /*
103 * Child. We use sp[1] to be stdin/stdout, and close sp[0].
104 */
105 (void) close(sp[0]);
106 if (dup2(sp[1], 0) == -1 || dup2(0, 1) == -1) {
107 perror("rcmdsh: dup2 failed");
108 _exit(255);
109 }
110 /* Fork again to lose parent. */
111 cpid = fork();
112 if (cpid == -1) {
113 perror("rcmdsh: fork to lose parent failed");
114 _exit(255);
115 }
116 if (cpid > 0)
117 _exit(0);
118
119 /* In grandchild here. Become local user for rshprog. */
120 if (setuid(pw->pw_uid)) {
121 (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n",
122 pw->pw_uid, strerror(errno));
123 _exit(255);
124 }
125
126 /*
127 * If remote host is "localhost" and local and remote user
128 * are the same, avoid running remote shell for efficiency.
129 */
130 if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) {
131 char *argv[4];
132 if (pw->pw_shell[0] == '\0')
133 rshprog = _PATH_BSHELL;
134 else
135 rshprog = pw->pw_shell;
136 p = strrchr(rshprog, '/');
137 argv[0] = p ? p + 1 : rshprog;
138 argv[1] = "-c";
139 argv[2] = (char *)cmd;
140 argv[3] = NULL;
141 execvp(rshprog, argv);
142 } else if ((p = strchr(rshprog, ' ')) == NULL) {
143 /* simple case */
144 char *argv[6];
145 p = strrchr(rshprog, '/');
146 argv[0] = p ? p + 1 : rshprog;
147 argv[1] = "-l";
148 argv[2] = (char *)remuser;
149 argv[3] = *ahost;
150 argv[4] = (char *)cmd;
151 argv[5] = NULL;
152 execvp(rshprog, argv);
153 } else {
154 /* must pull args out of rshprog and dyn alloc argv */
155 char **argv, **ap;
156 int n;
157 for (n = 7; (p = strchr(++p, ' ')) != NULL; n++)
158 continue;
159 rshprog = strdup(rshprog);
160 ap = argv = calloc(sizeof(char *), n);
161 if (rshprog == NULL || argv == NULL) {
162 perror("rcmdsh");
163 _exit(255);
164 }
165 while ((p = strsep(&rshprog, " ")) != NULL) {
166 if (*p == '\0')
167 continue;
168 *ap++ = p;
169 }
170 if (ap != argv) /* all spaces?!? */
171 rshprog = argv[0];
172 if ((p = strrchr(argv[0], '/')) != NULL)
173 argv[0] = p + 1;
174 *ap++ = "-l";
175 *ap++ = (char *)remuser;
176 *ap++ = *ahost;
177 *ap++ = (char *)cmd;
178 *ap++ = NULL;
179 execvp(rshprog, argv);
180 }
181 (void) fprintf(stderr, "rcmdsh: execvp %s failed: %s\n",
182 rshprog, strerror(errno));
183 _exit(255);
184 } else {
185 /* Parent. close sp[1], return sp[0]. */
186 (void) close(sp[1]);
187 /* Reap child. */
188 while (waitpid(cpid, NULL, 0) == -1 && errno == EINTR)
189 ;
190 return(sp[0]);
191 }
192 /* NOTREACHED */
193}
194DEF_WEAK(rcmdsh);
diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c
deleted file mode 100644
index 1488ca26bd..0000000000
--- a/src/lib/libc/net/recv.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* $OpenBSD: recv.c,v 1.7 2023/03/25 19:16:34 otto 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
36ssize_t
37recv(int s, void *buf, size_t len, int flags)
38{
39 return (recvfrom(s, buf, len, flags, NULL, NULL));
40}
41DEF_WEAK(recv);
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c
deleted file mode 100644
index ce9f92ae77..0000000000
--- a/src/lib/libc/net/res_comp.c
+++ /dev/null
@@ -1,466 +0,0 @@
1/* $OpenBSD: res_comp.c,v 1.23 2023/03/15 22:12:00 millert 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 <netinet/in.h>
56#include <arpa/nameser.h>
57
58#include <stdio.h>
59#include <resolv.h>
60#include <ctype.h>
61
62#include <unistd.h>
63#include <limits.h>
64#include <string.h>
65
66static 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 beginning 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 */
75int
76dn_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 if (comp_dn < msg || comp_dn >= eomorig)
86 return (-1);
87
88 dn = exp_dn;
89 cp = comp_dn;
90 if (length > HOST_NAME_MAX)
91 length = HOST_NAME_MAX;
92 eom = exp_dn + length;
93 /*
94 * fetch next label in domain name
95 */
96 while ((n = *cp++)) {
97 if (cp >= eomorig) /* out of range */
98 return (-1);
99
100 /*
101 * Check for indirection
102 */
103 switch (n & INDIR_MASK) {
104 case 0:
105 if (dn != exp_dn) {
106 if (dn >= eom)
107 return (-1);
108 *dn++ = '.';
109 }
110 if (dn+n >= eom)
111 return (-1);
112 checked += n + 1;
113 while (--n >= 0) {
114 if (((c = *cp++) == '.') || (c == '\\')) {
115 if (dn + n + 2 >= eom)
116 return (-1);
117 *dn++ = '\\';
118 }
119 *dn++ = c;
120 if (cp >= eomorig) /* out of range */
121 return (-1);
122 }
123 break;
124
125 case INDIR_MASK:
126 if (len < 0)
127 len = cp - comp_dn + 1;
128 cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff));
129 if (cp < msg || cp >= eomorig) /* out of range */
130 return (-1);
131 checked += 2;
132 /*
133 * Check for loops in the compressed name;
134 * if we've looked at the whole message,
135 * there must be a loop.
136 */
137 if (checked >= eomorig - msg)
138 return (-1);
139 break;
140
141 default:
142 return (-1); /* flag error */
143 }
144 }
145 *dn = '\0';
146 if (len < 0)
147 len = cp - comp_dn;
148 return (len);
149}
150DEF_WEAK(dn_expand);
151
152/*
153 * Compress domain name 'exp_dn' into 'comp_dn'.
154 * Return the size of the compressed name or -1.
155 * 'length' is the size of the array pointed to by 'comp_dn'.
156 * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0]
157 * is a pointer to the beginning of the message. The list ends with NULL.
158 * 'lastdnptr' is a pointer to the end of the array pointed to
159 * by 'dnptrs'. Side effect is to update the list of pointers for
160 * labels inserted into the message as we compress the name.
161 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
162 * is NULL, we don't update the list.
163 */
164int
165dn_comp(const char *exp_dn, u_char *comp_dn, int length, u_char **dnptrs,
166 u_char **lastdnptr)
167{
168 u_char *cp, *dn;
169 int c, l;
170 u_char **cpp, **lpp, *sp, *eob;
171 u_char *msg;
172
173 dn = (u_char *)exp_dn;
174 cp = comp_dn;
175 eob = cp + length;
176 lpp = cpp = NULL;
177 if (dnptrs != NULL) {
178 if ((msg = *dnptrs++) != NULL) {
179 for (cpp = dnptrs; *cpp != NULL; cpp++)
180 ;
181 lpp = cpp; /* end of list to search */
182 }
183 } else
184 msg = NULL;
185 for (c = *dn++; c != '\0'; ) {
186 /* look to see if we can use pointers */
187 if (msg != NULL) {
188 if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) {
189 if (cp+1 >= eob)
190 return (-1);
191 *cp++ = (l >> 8) | INDIR_MASK;
192 *cp++ = l % 256;
193 return (cp - comp_dn);
194 }
195 /* not found, save it */
196 if (lastdnptr != NULL && cpp < lastdnptr-1) {
197 *cpp++ = cp;
198 *cpp = NULL;
199 }
200 }
201 sp = cp++; /* save ptr to length byte */
202 do {
203 if (c == '.') {
204 c = *dn++;
205 break;
206 }
207 if (c == '\\') {
208 if ((c = *dn++) == '\0')
209 break;
210 }
211 if (cp >= eob) {
212 if (msg != NULL)
213 *lpp = NULL;
214 return (-1);
215 }
216 *cp++ = c;
217 } while ((c = *dn++) != '\0');
218 /* catch trailing '.'s but not '..' */
219 if ((l = cp - sp - 1) == 0 && c == '\0') {
220 cp--;
221 break;
222 }
223 if (l <= 0 || l > MAXLABEL) {
224 if (msg != NULL)
225 *lpp = NULL;
226 return (-1);
227 }
228 *sp = l;
229 }
230 if (cp >= eob) {
231 if (msg != NULL)
232 *lpp = NULL;
233 return (-1);
234 }
235 *cp++ = '\0';
236 return (cp - comp_dn);
237}
238
239/*
240 * Skip over a compressed domain name. Return the size or -1.
241 */
242int
243__dn_skipname(const u_char *comp_dn, const u_char *eom)
244{
245 const u_char *cp;
246 int n;
247
248 cp = comp_dn;
249 while (cp < eom && (n = *cp++)) {
250 /*
251 * check for indirection
252 */
253 switch (n & INDIR_MASK) {
254 case 0: /* normal case, n == len */
255 cp += n;
256 continue;
257 case INDIR_MASK: /* indirection */
258 cp++;
259 break;
260 default: /* illegal type */
261 return (-1);
262 }
263 break;
264 }
265 if (cp > eom)
266 return (-1);
267 return (cp - comp_dn);
268}
269
270/*
271 * Search for expanded name from a list of previously compressed names.
272 * Return the offset from msg if found or -1.
273 * dnptrs is the pointer to the first name on the list,
274 * not the pointer to the start of the message.
275 */
276static int
277dn_find(u_char *exp_dn, u_char *msg, u_char **dnptrs, u_char **lastdnptr)
278{
279 u_char *dn, *cp, **cpp;
280 int n;
281 u_char *sp;
282
283 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
284 dn = exp_dn;
285 sp = cp = *cpp;
286 while ((n = *cp++)) {
287 /*
288 * check for indirection
289 */
290 switch (n & INDIR_MASK) {
291 case 0: /* normal case, n == len */
292 while (--n >= 0) {
293 if (*dn == '.')
294 goto next;
295 if (*dn == '\\')
296 dn++;
297 if (tolower((unsigned char)*dn++) !=
298 tolower((unsigned char)*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
346int
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}
370DEF_STRONG(__res_hnok);
371
372/*
373 * hostname-like (A, MX, WKS) owners can have "*" as their first label
374 * but must otherwise be as a host name.
375 */
376int
377res_ownok(const char *dn)
378{
379 if (asterchar(dn[0])) {
380 if (periodchar(dn[1]))
381 return (res_hnok(dn+2));
382 if (dn[1] == '\0')
383 return (1);
384 }
385 return (res_hnok(dn));
386}
387
388/*
389 * SOA RNAMEs and RP RNAMEs can have any printable character in their first
390 * label, but the rest of the name has to look like a host name.
391 */
392int
393res_mailok(const char *dn)
394{
395 int ch, escaped = 0;
396
397 /* "." is a valid missing representation */
398 if (*dn == '\0')
399 return(1);
400
401 /* otherwise <label>.<hostname> */
402 while ((ch = *dn++) != '\0') {
403 if (!domainchar(ch))
404 return (0);
405 if (!escaped && periodchar(ch))
406 break;
407 if (escaped)
408 escaped = 0;
409 else if (bslashchar(ch))
410 escaped = 1;
411 }
412 if (periodchar(ch))
413 return (res_hnok(dn));
414 return(0);
415}
416
417/*
418 * This function is quite liberal, since RFC 1034's character sets are only
419 * recommendations.
420 */
421int
422res_dnok(const char *dn)
423{
424 int ch;
425
426 while ((ch = *dn++) != '\0')
427 if (!domainchar(ch))
428 return (0);
429 return (1);
430}
431
432/*
433 * Routines to insert/extract short/long's.
434 */
435
436u_int16_t
437_getshort(const u_char *msgp)
438{
439 u_int16_t u;
440
441 GETSHORT(u, msgp);
442 return (u);
443}
444DEF_STRONG(_getshort);
445
446u_int32_t
447_getlong(const u_char *msgp)
448{
449 u_int32_t u;
450
451 GETLONG(u, msgp);
452 return (u);
453}
454DEF_STRONG(_getlong);
455
456void
457__putshort(u_int16_t s, u_char *msgp)
458{
459 PUTSHORT(s, msgp);
460}
461
462void
463__putlong(u_int32_t l, u_char *msgp)
464{
465 PUTLONG(l, msgp);
466}
diff --git a/src/lib/libc/net/res_data.c b/src/lib/libc/net/res_data.c
deleted file mode 100644
index 6e81b584a9..0000000000
--- a/src/lib/libc/net/res_data.c
+++ /dev/null
@@ -1,104 +0,0 @@
1/* $OpenBSD: res_data.c,v 1.4 2015/01/16 16:48:51 deraadt 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/socket.h>
56#include <sys/time.h>
57#include <netinet/in.h>
58#include <arpa/inet.h>
59#include <arpa/nameser.h>
60
61#include <stdio.h>
62#include <ctype.h>
63#include <resolv.h>
64#include <unistd.h>
65#include <stdlib.h>
66#include <string.h>
67
68const char *_res_opcodes[] = {
69 "QUERY",
70 "IQUERY",
71 "CQUERYM",
72 "CQUERYU", /* experimental */
73 "NOTIFY", /* experimental */
74 "5",
75 "6",
76 "7",
77 "8",
78 "UPDATEA",
79 "UPDATED",
80 "UPDATEDA",
81 "UPDATEM",
82 "UPDATEMA",
83 "ZONEINIT",
84 "ZONEREF",
85};
86
87const char *_res_resultcodes[] = {
88 "NOERROR",
89 "FORMERR",
90 "SERVFAIL",
91 "NXDOMAIN",
92 "NOTIMP",
93 "REFUSED",
94 "6",
95 "7",
96 "8",
97 "9",
98 "10",
99 "11",
100 "12",
101 "13",
102 "14",
103 "NOCHANGE",
104};
diff --git a/src/lib/libc/net/res_debug_syms.c b/src/lib/libc/net/res_debug_syms.c
deleted file mode 100644
index fc26f03e74..0000000000
--- a/src/lib/libc/net/res_debug_syms.c
+++ /dev/null
@@ -1,192 +0,0 @@
1/* $OpenBSD: res_debug_syms.c,v 1.2 2015/10/05 02:57:16 guenther 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 */
89const 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 */
103const 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
153const 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}
171DEF_STRONG(__sym_ntos);
172
173/*
174 * Return a string for the type
175 */
176const char *
177__p_type(int type)
178{
179 return (__sym_ntos (__p_type_syms, type, (int *)0));
180}
181DEF_STRONG(__p_type);
182
183/*
184 * Return a mnemonic for class
185 */
186const char *
187__p_class(int class)
188{
189 return (__sym_ntos (__p_class_syms, class, (int *)0));
190}
191DEF_STRONG(__p_class);
192
diff --git a/src/lib/libc/net/res_init.3 b/src/lib/libc/net/res_init.3
deleted file mode 100644
index 3e0cabc358..0000000000
--- a/src/lib/libc/net/res_init.3
+++ /dev/null
@@ -1,441 +0,0 @@
1.\" $OpenBSD: res_init.3,v 1.6 2021/11/24 20:06:32 jca 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: November 24 2021 $
31.Dt RES_INIT 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
101These routines are used for making, sending, and interpreting
102query and reply messages with Internet domain name servers.
103.Pp
104Global configuration and state information that is used by the
105resolver routines is kept in the structure
106.Va _res .
107Most of the values have reasonable defaults and can be ignored.
108Options stored in
109.Va _res.options
110are defined in
111.In resolv.h
112and are as follows.
113Options are stored as a simple bit mask containing the bitwise OR
114of the options enabled.
115.Bl -tag -width RES_USE_DNSSEC
116.It Dv RES_INIT
117True if the initial name server address and default domain name are
118initialized (i.e.\&
119.Fn res_init
120has been called).
121.It Dv RES_DEBUG
122Print debugging messages,
123if libc is compiled with
124.Dv DEBUG .
125By default on
126.Ox
127this option does nothing.
128.It Dv RES_AAONLY
129Accept authoritative answers only.
130With this option,
131.Fn res_send
132should continue until it finds an authoritative answer or finds an error.
133On
134.Ox
135this option does nothing.
136.It Dv RES_USEVC
137Use TCP connections for queries instead of UDP datagrams.
138.It Dv RES_PRIMARY
139Query the primary name server only.
140On
141.Ox
142this option does nothing.
143.It Dv RES_IGNTC
144Ignore truncation errors, i.e. don't retry with TCP.
145.It Dv RES_RECURSE
146Set the recursion-desired bit in queries.
147.Pf ( Fn res_send
148does not do iterative queries and expects the name server
149to handle recursion.)
150This option is enabled by default.
151.It Dv RES_DEFNAMES
152If set,
153.Fn res_search
154will append the default domain name to single-component names
155(those that do not contain a dot).
156This option is enabled by default.
157.It Dv RES_STAYOPEN
158Used with
159.Dv RES_USEVC
160to keep the TCP connection open between queries.
161This is useful only in programs that regularly do many queries.
162UDP should be the normal mode used.
163.It Dv RES_DNSRCH
164If this option is set,
165.Fn res_search
166will search for host names in the current domain and in parent domains; see
167.Xr hostname 7 .
168This is used by the standard host lookup routine
169.Xr gethostbyname 3 .
170This option is enabled by default.
171.It Dv RES_INSECURE_1
172Do not require the IP source address on the reply packet
173to be equal to the server's address.
174.It Dv RES_INSECURE_2
175Do not check if the query section of the reply packet
176is equal to that of the query packet.
177.It Dv RES_NOALIASES
178This option has no effect.
179In the past, it turned off the legacy
180.Ev HOSTALIASES
181feature.
182.It Dv RES_TRUSTAD
183If set, the resolver routines will set the AD flag in DNS queries and
184preserve the value of the AD flag in DNS replies.
185If not set, the resolver routines will clear the AD flag in responses.
186Direct use of this option to enable AD bit processing is discouraged.
187Instead the use of trusted name servers should be annotated with
188.Dq options trust-ad
189in
190.Xr resolv.conf 5 .
191This option is automatically enabled if
192.Xr resolv.conf 5
193only lists name servers on localhost.
194.It Dv RES_USE_INET6
195With this option
196.Xr gethostbyname 3
197will return IPv6 addresses if available.
198This option is deprecated; software should use the
199.Xr getaddrinfo 3
200interface instead of modifying the behavior of
201.Xr gethostbyname 3 .
202On some operating systems this option also causes IPv4 addresses to be
203returned as IPv4-mapped IPv6 addresses.
204For example, 10.1.1.1 will be returned as ::ffff:10.1.1.1.
205IPv4-mapped IPv6 addresses are not supported on
206.Ox .
207.It Dv RES_USE_EDNS0
208Attach an OPT pseudo-RR for the EDNS0 extension,
209as specified in RFC 2671.
210This informs DNS servers of a client's receive buffer size,
211allowing them to take advantage of a non-default receive buffer size,
212and thus to send larger replies.
213DNS query packets with the EDNS0 extension are not compatible with
214non-EDNS0 DNS servers.
215.Ox
216uses 4096 bytes as input buffer size.
217.It Dv RES_USE_DNSSEC
218Request that the resolver uses
219Domain Name System Security Extensions (DNSSEC),
220as defined in RFCs 4033, 4034, and 4035.
221The resolver routines will use the EDNS0 extension and set the DNSSEC DO
222flag in queries, asking the name server to signal validated records by
223setting the AD flag in the reply and to attach additional DNSSEC
224records.
225The resolver routines will clear the AD flag in replies unless the name
226servers are considered trusted.
227Also, client applications are often only interested in the value of the
228AD flag, making the additional DNSSEC records a waste of network
229bandwidth.
230See the description for
231.Dq options trust-ad
232in
233.Xr resolv.conf 5 .
234.It Dv RES_USE_CD
235Set the Checking Disabled flag on queries.
236.El
237.Pp
238The
239.Fn res_init
240routine reads the configuration file (if any; see
241.Xr resolv.conf 5 )
242to get the default domain name, search list, and the Internet address
243of the local name server(s).
244If no server is configured, the host running
245the resolver is tried.
246The current domain name is defined by the hostname
247if not specified in the configuration file;
248it can be overridden by the environment variable
249.Ev LOCALDOMAIN .
250This environment variable may contain several blank-separated
251tokens if you wish to override the search list on a per-process basis.
252This is similar to the
253.Ic search
254command in the configuration file.
255Another environment variable
256.Ev RES_OPTIONS
257can be set to override certain internal resolver options which
258are otherwise set by changing fields in the
259.Va _res
260structure or are inherited from the configuration file's
261.Ic options
262command.
263The syntax of the
264.Ev RES_OPTIONS
265environment variable is explained in
266.Xr resolv.conf 5 .
267Initialization normally occurs on the first call
268to one of the following routines.
269.Pp
270The
271.Fn res_query
272function provides an interface to the server query mechanism.
273It constructs a query, sends it to the local server,
274awaits a response, and makes preliminary checks on the reply.
275The query requests information of the specified
276.Fa type
277and
278.Fa class
279for the specified fully qualified domain name
280.Fa dname .
281The reply message is left in the
282.Fa answer
283buffer with length
284.Fa anslen
285supplied by the caller.
286Values for the
287.Fa class
288and
289.Fa type
290fields
291are defined in
292.In arpa/nameser.h .
293.Pp
294The
295.Fn res_search
296routine makes a query and awaits a response like
297.Fn res_query ,
298but in addition, it implements the default and search rules controlled by the
299.Dv RES_DEFNAMES
300and
301.Dv RES_DNSRCH
302options.
303It returns the first successful reply.
304.Pp
305The remaining routines are lower-level routines used by
306.Fn res_query .
307The
308.Fn res_mkquery
309function constructs a standard query message and places it in
310.Fa buf .
311It returns the size of the query, or \-1 if the query is larger than
312.Fa buflen .
313The query type
314.Fa op
315is usually
316.Dv QUERY ,
317but can be any of the query types defined in
318.In arpa/nameser.h .
319The domain name for the query is given by
320.Fa dname .
321.Fa newrr
322is currently unused but is intended for making update messages.
323.Pp
324The
325.Fn res_send
326routine sends a pre-formatted query and returns an answer.
327It will call
328.Fn res_init
329if
330.Dv RES_INIT
331is not set, send the query to the local name server, and
332handle timeouts and retries.
333The length of the reply message is returned, or \-1 if there were errors.
334.Pp
335The
336.Fn dn_comp
337function compresses the domain name
338.Fa exp_dn
339and stores it in
340.Fa comp_dn .
341The size of the compressed name is returned or \-1 if there were errors.
342The size of the array pointed to by
343.Fa comp_dn
344is given by
345.Fa length .
346The compression uses an array of pointers
347.Fa dnptrs
348to previously compressed names in the current message.
349The first pointer points
350to the beginning of the message and the list ends with
351.Dv NULL .
352The limit to the array is specified by
353.Fa lastdnptr .
354A side effect of
355.Fn dn_comp
356is to update the list of pointers for labels inserted into the message
357as the name is compressed.
358If
359.Fa dnptrs
360is
361.Dv NULL ,
362names are not compressed.
363If
364.Fa lastdnptr
365is
366.Dv NULL ,
367the list of labels is not updated.
368.Pp
369The
370.Fn dn_expand
371entry expands the compressed domain name
372.Fa comp_dn
373to a full domain name.
374The compressed name is contained in a query or reply message;
375.Fa msg
376is a pointer to the beginning of the message.
377The uncompressed name is placed in the buffer indicated by
378.Fa exp_dn
379which is of size
380.Fa length .
381The size of compressed name is returned or \-1 if there was an error.
382.Sh FILES
383.Bl -tag -width "/etc/resolv.confXX"
384.It Pa /etc/resolv.conf
385The configuration file.
386.El
387.Sh SEE ALSO
388.Xr gethostbyname 3 ,
389.Xr resolv.conf 5 ,
390.Xr hostname 7
391.Sh STANDARDS
392.Rs
393.%A M. Stahl
394.%D November 1987
395.%R RFC 1032
396.%T Domain Administrators Guide
397.Re
398.Pp
399.Rs
400.%A M. Lottor
401.%D November 1987
402.%R RFC 1033
403.%T Domain Administrators Operations Guide
404.Re
405.Pp
406.Rs
407.%A P. Mockapetris
408.%D November 1987
409.%R RFC 1034
410.%T Domain Names \(en Concepts and Facilities
411.Re
412.Pp
413.Rs
414.%A P. Mockapetris
415.%D November 1987
416.%R RFC 1035
417.%T Domain Names \(en Implementation and Specification
418.Re
419.Pp
420.Rs
421.%A J. Klensin
422.%D October 2008
423.%R RFC 5321
424.%T Simple Mail Transfer Protocol
425.Re
426.Sh HISTORY
427The functions
428.Fn res_mkquery ,
429.Fn res_send ,
430.Fn res_init ,
431.Fn dn_comp ,
432and
433.Fn dn_expand
434appeared in
435.Bx 4.3 .
436The functions
437.Fn res_query
438and
439.Fn res_search
440appeared in
441.Bx 4.3 Tahoe .
diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c
deleted file mode 100644
index 447a2409bd..0000000000
--- a/src/lib/libc/net/res_random.c
+++ /dev/null
@@ -1,281 +0,0 @@
1/* $OpenBSD: res_random.c,v 1.27 2024/09/03 18:21:55 op 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 * Effectively 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 which 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
81struct 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
90static const u_int16_t pfacts[PFAC_N] = {
91 2,
92 3,
93 2729
94};
95
96static u_int16_t ru_x;
97static u_int16_t ru_seed, ru_seed2;
98static u_int16_t ru_a, ru_b;
99static u_int16_t ru_g;
100static u_int16_t ru_counter = 0;
101static u_int16_t ru_msb = 0;
102static struct prf_ctx *ru_prf = NULL;
103static time_t ru_reseed;
104static pid_t ru_pid;
105
106static u_int16_t pmod(u_int16_t, u_int16_t, u_int16_t);
107static void res_initid(void);
108
109/*
110 * Do a fast modular exponation, returned value will be in the range
111 * of 0 - (mod-1)
112 */
113static u_int16_t
114pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod)
115{
116 u_int16_t s, t, u;
117
118 s = 1;
119 t = gen;
120 u = exp;
121
122 while (u) {
123 if (u & 1)
124 s = (s * t) % mod;
125 u >>= 1;
126 t = (t * t) % mod;
127 }
128 return (s);
129}
130
131/*
132 * 15-bit permutation based on Luby-Rackoff block cipher
133 */
134static u_int
135permute15(u_int in)
136{
137 int i;
138 u_int left, right, tmp;
139
140 if (ru_prf == NULL)
141 return in;
142
143 left = (in >> 8) & 0x7f;
144 right = in & 0xff;
145
146 /*
147 * Each round swaps the width of left and right. Even rounds have
148 * a 7-bit left, odd rounds have an 8-bit left. Since this uses an
149 * odd number of rounds, left is always 8 bits wide at the end.
150 */
151 for (i = 0; i < RU_ROUNDS; i++) {
152 if ((i & 1) == 0)
153 tmp = ru_prf->prf8[(i << (8 - 1)) | right] & 0x7f;
154 else
155 tmp = ru_prf->prf7[((i - 1) << (7 - 1)) | right];
156 tmp ^= left;
157 left = right;
158 right = tmp;
159 }
160
161 return (right << 8) | left;
162}
163
164/*
165 * Initializes the seed and chooses a suitable generator. Also toggles
166 * the msb flag. The msb flag is used to generate two distinct
167 * cycles of random numbers and thus avoiding reuse of ids.
168 *
169 * This function is called from res_randomid() when needed, an
170 * application does not have to worry about it.
171 */
172static void
173res_initid(void)
174{
175 u_int16_t j, i;
176 u_int32_t tmp;
177 int noprime = 1;
178 struct timespec ts;
179
180 ru_x = arc4random_uniform(RU_M);
181
182 /* 15 bits of random seed */
183 tmp = arc4random();
184 ru_seed = (tmp >> 16) & 0x7FFF;
185 ru_seed2 = tmp & 0x7FFF;
186
187 /* Determine the LCG we use */
188 tmp = arc4random();
189 ru_b = (tmp & 0xfffe) | 1;
190 ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M);
191 while (ru_b % 3 == 0)
192 ru_b += 2;
193
194 j = arc4random_uniform(RU_N);
195
196 /*
197 * Do a fast gcd(j,RU_N-1), so we can find a j with
198 * gcd(j, RU_N-1) == 1, giving a new generator for
199 * RU_GEN^j mod RU_N
200 */
201
202 while (noprime) {
203 for (i = 0; i < PFAC_N; i++)
204 if (j % pfacts[i] == 0)
205 break;
206
207 if (i >= PFAC_N)
208 noprime = 0;
209 else
210 j = (j + 1) % RU_N;
211 }
212
213 ru_g = pmod(RU_GEN, j, RU_N);
214 ru_counter = 0;
215
216 /* Initialise PRF for Luby-Rackoff permutation */
217 if (ru_prf == NULL)
218 ru_prf = malloc(sizeof(*ru_prf));
219 if (ru_prf != NULL)
220 arc4random_buf(ru_prf, sizeof(*ru_prf));
221
222 WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts);
223 ru_reseed = ts.tv_sec + RU_OUT;
224 ru_msb = ru_msb == 0x8000 ? 0 : 0x8000;
225}
226
227u_int
228__res_randomid(void)
229{
230 struct timespec ts;
231 pid_t pid;
232 u_int r;
233 static void *randomid_mutex;
234
235 WRAP(clock_gettime)(CLOCK_MONOTONIC, &ts);
236 pid = getpid();
237
238 _MUTEX_LOCK(&randomid_mutex);
239
240 if (ru_counter >= RU_MAX || ts.tv_sec > ru_reseed || pid != ru_pid) {
241 res_initid();
242 ru_pid = pid;
243 }
244
245 /* Linear Congruential Generator */
246 ru_x = (ru_a * ru_x + ru_b) % RU_M;
247 ru_counter++;
248
249 r = permute15(ru_seed ^ pmod(ru_g, ru_seed2 + ru_x, RU_N)) | ru_msb;
250
251 _MUTEX_UNLOCK(&randomid_mutex);
252
253 return (r);
254}
255DEF_STRONG(__res_randomid);
256
257#if 0
258int
259main(int argc, char **argv)
260{
261 int i, n;
262 u_int16_t wert;
263
264 res_initid();
265
266 printf("Generator: %u\n", ru_g);
267 printf("Seed: %u\n", ru_seed);
268 printf("Reseed at %ld\n", ru_reseed);
269 printf("Ru_X: %u\n", ru_x);
270 printf("Ru_A: %u\n", ru_a);
271 printf("Ru_B: %u\n", ru_b);
272
273 n = argc > 1 ? atoi(argv[1]) : 60001;
274 for (i=0;i<n;i++) {
275 wert = res_randomid();
276 printf("%u\n", wert);
277 }
278 return 0;
279}
280#endif
281
diff --git a/src/lib/libc/net/rresvport.c b/src/lib/libc/net/rresvport.c
deleted file mode 100644
index 72c27c3a3f..0000000000
--- a/src/lib/libc/net/rresvport.c
+++ /dev/null
@@ -1,107 +0,0 @@
1/* $OpenBSD: rresvport.c,v 1.12 2019/06/28 13:32:42 deraadt 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/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#include <netgroup.h>
50
51int
52rresvport(int *alport)
53{
54 return rresvport_af(alport, AF_INET);
55}
56
57
58int
59rresvport_af(int *alport, int af)
60{
61 struct sockaddr_storage ss;
62 struct sockaddr *sa;
63 u_int16_t *portp;
64 int s;
65
66 bzero(&ss, sizeof ss);
67 sa = (struct sockaddr *)&ss;
68
69 switch (af) {
70 case AF_INET:
71 sa->sa_len = sizeof(struct sockaddr_in);
72 portp = &((struct sockaddr_in *)sa)->sin_port;
73 break;
74 case AF_INET6:
75 sa->sa_len = sizeof(struct sockaddr_in6);
76 portp = &((struct sockaddr_in6 *)sa)->sin6_port;
77 break;
78 default:
79 errno = EPFNOSUPPORT;
80 return (-1);
81 }
82 sa->sa_family = af;
83
84 s = socket(af, SOCK_STREAM, 0);
85 if (s == -1)
86 return (-1);
87
88 *portp = htons(*alport);
89 if (*alport < IPPORT_RESERVED - 1) {
90 if (bind(s, sa, sa->sa_len) != -1)
91 return (s);
92 if (errno != EADDRINUSE) {
93 (void)close(s);
94 return (-1);
95 }
96 }
97
98 *portp = 0;
99 sa->sa_family = af;
100 if (bindresvport_sa(s, sa) == -1) {
101 (void)close(s);
102 return (-1);
103 }
104 *alport = ntohs(*portp);
105 return (s);
106}
107DEF_WEAK(rresvport_af);
diff --git a/src/lib/libc/net/rthdr.c b/src/lib/libc/net/rthdr.c
deleted file mode 100644
index 5ffb13fc87..0000000000
--- a/src/lib/libc/net/rthdr.c
+++ /dev/null
@@ -1,198 +0,0 @@
1/* $OpenBSD: rthdr.c,v 1.13 2022/12/27 17:10:06 jmc 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/types.h>
34#include <sys/socket.h>
35
36#include <netinet/in.h>
37#include <netinet/ip6.h>
38
39#include <string.h>
40#include <stdio.h>
41
42/*
43 * RFC3542 (2292bis) API
44 */
45
46socklen_t
47inet6_rth_space(int type, int segments)
48{
49 switch (type) {
50 case IPV6_RTHDR_TYPE_0:
51 return (((segments * 2) + 1) << 3);
52 default:
53 return (0); /* type not supported */
54 }
55}
56DEF_WEAK(inet6_rth_space);
57
58void *
59inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments)
60{
61 struct ip6_rthdr *rth = (struct ip6_rthdr *)bp;
62 struct ip6_rthdr0 *rth0;
63
64 switch (type) {
65 case IPV6_RTHDR_TYPE_0:
66 /* length validation */
67 if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments))
68 return (NULL);
69
70 memset(bp, 0, bp_len);
71 rth0 = (struct ip6_rthdr0 *)rth;
72 rth0->ip6r0_len = segments * 2;
73 rth0->ip6r0_type = IPV6_RTHDR_TYPE_0;
74 rth0->ip6r0_segleft = 0;
75 rth0->ip6r0_reserved = 0;
76 break;
77 default:
78 return (NULL); /* type not supported */
79 }
80
81 return (bp);
82}
83
84int
85inet6_rth_add(void *bp, const struct in6_addr *addr)
86{
87 struct ip6_rthdr *rth = (struct ip6_rthdr *)bp;
88 struct ip6_rthdr0 *rth0;
89 struct in6_addr *nextaddr;
90
91 switch (rth->ip6r_type) {
92 case IPV6_RTHDR_TYPE_0:
93 rth0 = (struct ip6_rthdr0 *)rth;
94 nextaddr = (struct in6_addr *)(rth0 + 1) + rth0->ip6r0_segleft;
95 *nextaddr = *addr;
96 rth0->ip6r0_segleft++;
97 break;
98 default:
99 return (-1); /* type not supported */
100 }
101
102 return (0);
103}
104
105int
106inet6_rth_reverse(const void *in, void *out)
107{
108 struct ip6_rthdr *rth_in = (struct ip6_rthdr *)in;
109 struct ip6_rthdr0 *rth0_in, *rth0_out;
110 int i, segments;
111
112 switch (rth_in->ip6r_type) {
113 case IPV6_RTHDR_TYPE_0:
114 rth0_in = (struct ip6_rthdr0 *)in;
115 rth0_out = (struct ip6_rthdr0 *)out;
116
117 /* parameter validation XXX too paranoid? */
118 if (rth0_in->ip6r0_len % 2)
119 return (-1);
120 segments = rth0_in->ip6r0_len / 2;
121
122 /* we can't use memcpy here, since in and out may overlap */
123 memmove(rth0_out, rth0_in, ((rth0_in->ip6r0_len) + 1) << 3);
124 rth0_out->ip6r0_segleft = segments;
125
126 /* reverse the addresses */
127 for (i = 0; i < segments / 2; i++) {
128 struct in6_addr addr_tmp, *addr1, *addr2;
129
130 addr1 = (struct in6_addr *)(rth0_out + 1) + i;
131 addr2 = (struct in6_addr *)(rth0_out + 1) +
132 (segments - i - 1);
133 addr_tmp = *addr1;
134 *addr1 = *addr2;
135 *addr2 = addr_tmp;
136 }
137
138 break;
139 default:
140 return (-1); /* type not supported */
141 }
142
143 return (0);
144}
145
146int
147inet6_rth_segments(const void *bp)
148{
149 struct ip6_rthdr *rh = (struct ip6_rthdr *)bp;
150 struct ip6_rthdr0 *rh0;
151 int addrs;
152
153 switch (rh->ip6r_type) {
154 case IPV6_RTHDR_TYPE_0:
155 rh0 = (struct ip6_rthdr0 *)bp;
156
157 /*
158 * Validation for a type-0 routing header.
159 * Is this too strict?
160 */
161 if ((rh0->ip6r0_len % 2) != 0 ||
162 (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft)
163 return (-1);
164
165 return (addrs);
166 default:
167 return (-1); /* unknown type */
168 }
169}
170
171struct in6_addr *
172inet6_rth_getaddr(const void *bp, int idx)
173{
174 struct ip6_rthdr *rh = (struct ip6_rthdr *)bp;
175 struct ip6_rthdr0 *rh0;
176 int addrs;
177
178 switch (rh->ip6r_type) {
179 case IPV6_RTHDR_TYPE_0:
180 rh0 = (struct ip6_rthdr0 *)bp;
181
182 /*
183 * Validation for a type-0 routing header.
184 * Is this too strict?
185 */
186 if ((rh0->ip6r0_len % 2) != 0 ||
187 (addrs = (rh0->ip6r0_len >> 1)) < rh0->ip6r0_segleft)
188 return (NULL);
189
190 if (idx < 0 || addrs <= idx)
191 return (NULL);
192
193 return (((struct in6_addr *)(rh0 + 1)) + idx);
194 default:
195 return (NULL); /* unknown type */
196 break;
197 }
198}
diff --git a/src/lib/libc/net/ruserok.c b/src/lib/libc/net/ruserok.c
deleted file mode 100644
index 291c702060..0000000000
--- a/src/lib/libc/net/ruserok.c
+++ /dev/null
@@ -1,392 +0,0 @@
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
51static int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t,
52 const char *, const char *);
53static int __icheckhost(struct sockaddr *, socklen_t, const char *);
54static char *__gethostloop(struct sockaddr *, socklen_t);
55static int iruserok_sa(const void *, int, int, const char *, const char *);
56
57int
58ruserok(const char *rhost, int superuser, const char *ruser, const char *luser)
59{
60 struct addrinfo hints, *res, *r;
61 int error;
62
63 memset(&hints, 0, sizeof(hints));
64 hints.ai_family = PF_UNSPEC;
65 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
66 error = getaddrinfo(rhost, "0", &hints, &res);
67 if (error)
68 return (-1);
69
70 for (r = res; r; r = r->ai_next) {
71 if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser,
72 luser) == 0) {
73 freeaddrinfo(res);
74 return (0);
75 }
76 }
77 freeaddrinfo(res);
78 return (-1);
79}
80
81int
82iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser,
83 const char *luser)
84{
85 struct sockaddr *sa;
86 char *cp;
87 struct stat sbuf;
88 struct passwd pwstore, *pwd;
89 FILE *hostf;
90 uid_t uid;
91 int first;
92 char pbuf[PATH_MAX], pwbuf[_PW_BUF_LEN];
93
94 sa = (struct sockaddr *)raddr;
95 first = 1;
96 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "re");
97again:
98 if (hostf) {
99 if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) {
100 (void)fclose(hostf);
101 return (0);
102 }
103 (void)fclose(hostf);
104 }
105 if (first == 1) {
106 int len;
107
108 first = 0;
109 pwd = NULL;
110 getpwnam_r(luser, &pwstore, pwbuf, sizeof(pwbuf), &pwd);
111 if (pwd == NULL)
112 return (-1);
113 len = snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir);
114 if (len < 0 || len >= sizeof pbuf)
115 return (-1);
116
117 /*
118 * Change effective uid while opening .rhosts. If root and
119 * reading an NFS mounted file system, can't read files that
120 * are protected read/write owner only.
121 */
122 uid = geteuid();
123 (void)seteuid(pwd->pw_uid);
124 hostf = fopen(pbuf, "re");
125 (void)seteuid(uid);
126
127 if (hostf == NULL)
128 return (-1);
129 /*
130 * If not a regular file, or is owned by someone other than
131 * user or root or if writeable by anyone but the owner, quit.
132 */
133 cp = NULL;
134 if (lstat(pbuf, &sbuf) == -1)
135 cp = ".rhosts lstat failed";
136 else if (!S_ISREG(sbuf.st_mode))
137 cp = ".rhosts not regular file";
138 else if (fstat(fileno(hostf), &sbuf) == -1)
139 cp = ".rhosts fstat failed";
140 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
141 cp = "bad .rhosts owner";
142 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
143 cp = ".rhosts writable by other than owner";
144 /* If there were any problems, quit. */
145 if (cp) {
146 (void)fclose(hostf);
147 return (-1);
148 }
149 goto again;
150 }
151 return (-1);
152}
153
154int
155__ivaliduser_sa(FILE *hostf, struct sockaddr *raddr, socklen_t salen,
156 const char *luser, const char *ruser)
157{
158 char *user, *p;
159 char *buf;
160 const char *auser, *ahost;
161 int hostok, userok;
162 char *rhost = (char *)-1;
163 char domain[HOST_NAME_MAX+1];
164 size_t buflen;
165
166 getdomainname(domain, sizeof(domain));
167
168 while ((buf = fgetln(hostf, &buflen))) {
169 p = buf;
170 if (*p == '#')
171 continue;
172 while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') {
173 if (!isprint((unsigned char)*p))
174 goto bail;
175 *p = isupper((unsigned char)*p) ?
176 tolower((unsigned char)*p) : *p;
177 p++;
178 }
179 if (p >= buf + buflen)
180 continue;
181 if (*p == ' ' || *p == '\t') {
182 *p++ = '\0';
183 while (p < buf + buflen && (*p == ' ' || *p == '\t'))
184 p++;
185 if (p >= buf + buflen)
186 continue;
187 user = p;
188 while (p < buf + buflen && *p != '\n' && *p != ' ' &&
189 *p != '\t') {
190 if (!isprint((unsigned char)*p))
191 goto bail;
192 p++;
193 }
194 } else
195 user = p;
196 *p = '\0';
197
198 if (p == buf)
199 continue;
200
201 auser = *user ? user : luser;
202 ahost = buf;
203
204 if (strlen(ahost) > HOST_NAME_MAX)
205 continue;
206
207 /*
208 * innetgr() must lookup a hostname (we do not attempt
209 * to change the semantics so that netgroups may have
210 * #.#.#.# addresses in the list.)
211 */
212 if (ahost[0] == '+')
213 switch (ahost[1]) {
214 case '\0':
215 hostok = 1;
216 break;
217 case '@':
218 if (rhost == (char *)-1)
219 rhost = __gethostloop(raddr, salen);
220 hostok = 0;
221 if (rhost)
222 hostok = innetgr(&ahost[2], rhost,
223 NULL, domain);
224 break;
225 default:
226 hostok = __icheckhost(raddr, salen, &ahost[1]);
227 break;
228 }
229 else if (ahost[0] == '-')
230 switch (ahost[1]) {
231 case '\0':
232 hostok = -1;
233 break;
234 case '@':
235 if (rhost == (char *)-1)
236 rhost = __gethostloop(raddr, salen);
237 hostok = 0;
238 if (rhost)
239 hostok = -innetgr(&ahost[2], rhost,
240 NULL, domain);
241 break;
242 default:
243 hostok = -__icheckhost(raddr, salen, &ahost[1]);
244 break;
245 }
246 else
247 hostok = __icheckhost(raddr, salen, ahost);
248
249
250 if (auser[0] == '+')
251 switch (auser[1]) {
252 case '\0':
253 userok = 1;
254 break;
255 case '@':
256 userok = innetgr(&auser[2], NULL, ruser,
257 domain);
258 break;
259 default:
260 userok = strcmp(ruser, &auser[1]) ? 0 : 1;
261 break;
262 }
263 else if (auser[0] == '-')
264 switch (auser[1]) {
265 case '\0':
266 userok = -1;
267 break;
268 case '@':
269 userok = -innetgr(&auser[2], NULL, ruser,
270 domain);
271 break;
272 default:
273 userok = strcmp(ruser, &auser[1]) ? 0 : -1;
274 break;
275 }
276 else
277 userok = strcmp(ruser, auser) ? 0 : 1;
278
279 /* Check if one component did not match */
280 if (hostok == 0 || userok == 0)
281 continue;
282
283 /* Check if we got a forbidden pair */
284 if (userok <= -1 || hostok <= -1)
285 return (-1);
286
287 /* Check if we got a valid pair */
288 if (hostok >= 1 && userok >= 1)
289 return (0);
290 }
291bail:
292 return (-1);
293}
294
295/*
296 * Returns "true" if match, 0 if no match. If we do not find any
297 * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work.
298 */
299static int
300__icheckhost(struct sockaddr *raddr, socklen_t salen, const char *lhost)
301{
302 struct addrinfo hints, *res, *r;
303 char h1[NI_MAXHOST], h2[NI_MAXHOST];
304 int error;
305 const int niflags = NI_NUMERICHOST;
306
307 h1[0] = '\0';
308 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
309 niflags) != 0)
310 return (0);
311
312 /* Resolve laddr into sockaddr */
313 memset(&hints, 0, sizeof(hints));
314 hints.ai_family = raddr->sa_family;
315 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
316 res = NULL;
317 error = getaddrinfo(lhost, "0", &hints, &res);
318 if (error)
319 return (0);
320
321 /*
322 * Try string comparisons between raddr and laddr.
323 */
324 for (r = res; r; r = r->ai_next) {
325 h2[0] = '\0';
326 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
327 NULL, 0, niflags) != 0)
328 continue;
329 if (strcmp(h1, h2) == 0) {
330 freeaddrinfo(res);
331 return (1);
332 }
333 }
334
335 /* No match. */
336 freeaddrinfo(res);
337 return (0);
338}
339
340/*
341 * Return the hostname associated with the supplied address.
342 * Do a reverse lookup as well for security. If a loop cannot
343 * be found, pack the result of inet_ntoa() into the string.
344 */
345static char *
346__gethostloop(struct sockaddr *raddr, socklen_t salen)
347{
348 static char remotehost[NI_MAXHOST];
349 char h1[NI_MAXHOST], h2[NI_MAXHOST];
350 struct addrinfo hints, *res, *r;
351 int error;
352 const int niflags = NI_NUMERICHOST;
353
354 h1[0] = remotehost[0] = '\0';
355 if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost),
356 NULL, 0, NI_NAMEREQD) != 0)
357 return (NULL);
358 if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0,
359 niflags) != 0)
360 return (NULL);
361
362 /*
363 * Look up the name and check that the supplied
364 * address is in the list
365 */
366 memset(&hints, 0, sizeof(hints));
367 hints.ai_family = raddr->sa_family;
368 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
369 hints.ai_flags = AI_CANONNAME;
370 res = NULL;
371 error = getaddrinfo(remotehost, "0", &hints, &res);
372 if (error)
373 return (NULL);
374
375 for (r = res; r; r = r->ai_next) {
376 h2[0] = '\0';
377 if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2),
378 NULL, 0, niflags) != 0)
379 continue;
380 if (strcmp(h1, h2) == 0) {
381 freeaddrinfo(res);
382 return (remotehost);
383 }
384 }
385
386 /*
387 * either the DNS administrator has made a configuration
388 * mistake, or someone has attempted to spoof us
389 */
390 freeaddrinfo(res);
391 return (NULL);
392}
diff --git a/src/lib/libc/net/send.c b/src/lib/libc/net/send.c
deleted file mode 100644
index 1fad3bd171..0000000000
--- a/src/lib/libc/net/send.c
+++ /dev/null
@@ -1,41 +0,0 @@
1/* $OpenBSD: send.c,v 1.6 2015/10/04 07:17:27 guenther 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
36ssize_t
37send(int s, const void *msg, size_t len, int flags)
38{
39 return (sendto(s, msg, len, flags, NULL, 0));
40}
41DEF_WEAK(send);
diff --git a/src/lib/libc/net/sockatmark.3 b/src/lib/libc/net/sockatmark.3
deleted file mode 100644
index 524536c12b..0000000000
--- a/src/lib/libc/net/sockatmark.3
+++ /dev/null
@@ -1,122 +0,0 @@
1.\" Copyright (c) 2002 William C. Fenner. All rights reserved.
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions
5.\" are met:
6.\" 1. Redistributions of source code must retain the above copyright
7.\" notice, this list of conditions and the following disclaimer.
8.\" 2. Redistributions in binary form must reproduce the above copyright
9.\" notice, this list of conditions and the following disclaimer in the
10.\" documentation and/or other materials provided with the distribution.
11.\"
12.\" THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22.\" SUCH DAMAGE.
23.\"
24.\" From FreeBSD: r108087 2002-12-19 01:40:28 -0800
25.\" $OpenBSD: sockatmark.3,v 1.2 2019/06/20 14:19:25 deraadt Exp $
26.\"
27.Dd $Mdocdate: June 20 2019 $
28.Dt SOCKATMARK 3
29.Os
30.Sh NAME
31.Nm sockatmark
32.Nd determine whether the read pointer is at the out-of-band data mark
33.Sh SYNOPSIS
34.In sys/socket.h
35.Ft int
36.Fn sockatmark "int s"
37.Sh DESCRIPTION
38The
39.Fn sockatmark
40function returns 1 if the read pointer for the socket
41.Fa s
42is currently at the out-of-band data mark.
43Otherwise, it returns 0 if the socket doesn't have an out-of-band
44data mark or if there is normal data to be received before the mark.
45.Sh RETURN VALUES
46Upon successful completion, the
47.Fn sockatmark
48function returns the value 1 if the read pointer is pointing at
49the out-of-band data mark, 0 if it is not.
50Otherwise the value \-1 is returned
51and the global variable
52.Va errno
53is set to
54indicate the error.
55.Sh EXAMPLES
56The routine used in the historical remote login process to flush
57output on receipt of an interrupt or quit signal is shown below.
58It reads the normal data up to the mark (to discard it),
59then reads the out-of-band byte.
60.Bd -literal -offset indent
61#include <sys/socket.h>
62\&...
63oob()
64{
65 int mark;
66 char waste[BUFSIZ];
67
68 for (;;) {
69 if ((mark = sockatmark(rem)) == -1) {
70 perror("sockatmark");
71 break;
72 }
73 if (mark)
74 break;
75 (void) read(rem, waste, sizeof (waste));
76 }
77 if (recv(rem, &mark, 1, MSG_OOB) == -1) {
78 perror("recv");
79 ...
80 }
81 ...
82}
83.Ed
84.Sh ERRORS
85The
86.Fn sockatmark
87call fails if:
88.Bl -tag -width Er
89.It Bq Er EBADF
90.Fa s
91is not a valid descriptor.
92.It Bq Er ENOTTY
93.Fa s
94is valid but does not refer to a socket.
95.El
96.Sh SEE ALSO
97.Xr recv 2 ,
98.Xr send 2
99.Sh STANDARDS
100The
101.Fn sockatmark
102function conforms to
103.St -p1003.1-2008 .
104.Sh HISTORY
105The
106.Fn sockatmark
107function was introduced by
108.St -p1003.1-2001
109to standardize the historical
110.Dv SIOCATMARK
111.Xr ioctl 2 .
112The
113.Fn sockatmark
114function appeared in
115.Ox 5.7 .
116.Pp
117The
118.Er ENOTTY
119error is returned instead of the usual
120.Er ENOTSOCK
121error to match the historical behavior of
122.Dv SIOCATMARK .
diff --git a/src/lib/libc/net/sockatmark.c b/src/lib/libc/net/sockatmark.c
deleted file mode 100644
index a43e7af3aa..0000000000
--- a/src/lib/libc/net/sockatmark.c
+++ /dev/null
@@ -1,40 +0,0 @@
1/*
2 * Copyright (c) 2002 William C. Fenner. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``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 THE REGENTS OR CONTRIBUTORS 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 * $FreeBSD$
26 */
27
28#include <sys/socket.h>
29#include <sys/sockio.h>
30#include <sys/ioctl.h>
31
32int
33sockatmark(int s)
34{
35 int atmark;
36
37 if (ioctl(s, SIOCATMARK, &atmark) == -1)
38 return (-1);
39 return (atmark);
40}
diff --git a/src/lib/libc/net/vars6.c b/src/lib/libc/net/vars6.c
deleted file mode 100644
index a12932a26e..0000000000
--- a/src/lib/libc/net/vars6.c
+++ /dev/null
@@ -1,42 +0,0 @@
1/* $OpenBSD: vars6.c,v 1.3 2014/08/31 19:20:44 bluhm 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 */
38const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
39const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
40const struct in6_addr in6addr_intfacelocal_allnodes = IN6ADDR_INTFACELOCAL_ALLNODES_INIT;
41const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
42const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;