diff options
Diffstat (limited to 'src/lib/libc/net/getnameinfo.3')
-rw-r--r-- | src/lib/libc/net/getnameinfo.3 | 264 |
1 files changed, 0 insertions, 264 deletions
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 | ||
33 | The | ||
34 | .Fn getnameinfo | ||
35 | function is used to convert a | ||
36 | .Vt sockaddr | ||
37 | structure to a pair of host name and service strings. | ||
38 | It is a replacement for and provides more flexibility than the | ||
39 | .Xr gethostbyaddr 3 | ||
40 | and | ||
41 | .Xr getservbyport 3 | ||
42 | functions and is the converse of the | ||
43 | .Xr getaddrinfo 3 | ||
44 | function. | ||
45 | .Pp | ||
46 | The | ||
47 | .Vt sockaddr | ||
48 | structure | ||
49 | .Fa sa | ||
50 | should point to either a | ||
51 | .Vt sockaddr_in | ||
52 | or | ||
53 | .Vt sockaddr_in6 | ||
54 | structure (for IPv4 or IPv6 respectively) that is | ||
55 | .Fa salen | ||
56 | bytes long. | ||
57 | .Pp | ||
58 | The host and service names associated with | ||
59 | .Fa sa | ||
60 | are stored in | ||
61 | .Fa host | ||
62 | and | ||
63 | .Fa serv | ||
64 | which have length parameters | ||
65 | .Fa hostlen | ||
66 | and | ||
67 | .Fa servlen . | ||
68 | The maximum value for | ||
69 | .Fa hostlen | ||
70 | is | ||
71 | .Dv NI_MAXHOST | ||
72 | and | ||
73 | the maximum value for | ||
74 | .Fa servlen | ||
75 | is | ||
76 | .Dv NI_MAXSERV , | ||
77 | as defined by | ||
78 | .In netdb.h . | ||
79 | If a length parameter is zero, no string will be stored. | ||
80 | Otherwise, enough space must be provided to store the | ||
81 | host name or service string plus a byte for the NUL terminator. | ||
82 | .Pp | ||
83 | The | ||
84 | .Fa flags | ||
85 | argument is formed by | ||
86 | .Tn OR Ns 'ing | ||
87 | the following values: | ||
88 | .Bl -tag -width "NI_NUMERICHOSTXX" | ||
89 | .It Dv NI_NOFQDN | ||
90 | A fully qualified domain name is not required for local hosts. | ||
91 | The local part of the fully qualified domain name is returned instead. | ||
92 | .It Dv NI_NUMERICHOST | ||
93 | Return the address in numeric form, as if calling | ||
94 | .Xr inet_ntop 3 , | ||
95 | instead of a host name. | ||
96 | .It Dv NI_NAMEREQD | ||
97 | A name is required. | ||
98 | If the host name cannot be found in DNS and this flag is set, | ||
99 | a non-zero error code is returned. | ||
100 | If the host name is not found and the flag is not set, the | ||
101 | address is returned in numeric form. | ||
102 | .It NI_NUMERICSERV | ||
103 | The service name is returned as a digit string representing the port number. | ||
104 | .It NI_DGRAM | ||
105 | Specifies that the service being looked up is a datagram | ||
106 | service, and causes | ||
107 | .Xr getservbyport 3 | ||
108 | to be called with a second argument of | ||
109 | .Dq udp | ||
110 | instead of its default of | ||
111 | .Dq tcp . | ||
112 | This is required for the few ports (512\-514) that have different services | ||
113 | for | ||
114 | .Tn UDP | ||
115 | and | ||
116 | .Tn TCP . | ||
117 | .El | ||
118 | .Pp | ||
119 | This implementation allows numeric IPv6 address notation with scope identifier, | ||
120 | as documented in RFC 4007. | ||
121 | IPv6 link-local address will appear as a string like | ||
122 | .Dq Li fe80::1%ne0 . | ||
123 | Refer to | ||
124 | .Xr getaddrinfo 3 | ||
125 | for more information. | ||
126 | .Sh RETURN VALUES | ||
127 | .Fn getnameinfo | ||
128 | returns zero on success or one of the error codes listed in | ||
129 | .Xr gai_strerror 3 | ||
130 | if an error occurs. | ||
131 | .Sh EXAMPLES | ||
132 | The following code tries to get a numeric host name, and service name, | ||
133 | for a given socket address. | ||
134 | Observe that there is no hardcoded reference to a particular address family. | ||
135 | .Bd -literal -offset indent | ||
136 | struct sockaddr *sa; /* input */ | ||
137 | char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; | ||
138 | |||
139 | if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf, | ||
140 | sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) | ||
141 | errx(1, "could not get numeric hostname"); | ||
142 | printf("host=%s, serv=%s\en", hbuf, sbuf); | ||
143 | .Ed | ||
144 | .Pp | ||
145 | The following version checks if the socket address has a reverse address mapping: | ||
146 | .Bd -literal -offset indent | ||
147 | struct sockaddr *sa; /* input */ | ||
148 | char hbuf[NI_MAXHOST]; | ||
149 | |||
150 | if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0, | ||
151 | NI_NAMEREQD)) | ||
152 | errx(1, "could not resolve hostname"); | ||
153 | printf("host=%s\en", hbuf); | ||
154 | .Ed | ||
155 | .Sh SEE ALSO | ||
156 | .Xr gai_strerror 3 , | ||
157 | .Xr getaddrinfo 3 , | ||
158 | .Xr gethostbyaddr 3 , | ||
159 | .Xr getservbyport 3 , | ||
160 | .Xr inet_ntop 3 , | ||
161 | .Xr 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 | ||
173 | The | ||
174 | .Fn getnameinfo | ||
175 | function is defined by the | ||
176 | .St -p1003.1g-2000 | ||
177 | draft 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 | ||
202 | can return both numeric and FQDN forms of the address specified in | ||
203 | .Fa sa . | ||
204 | There is no return value that indicates whether the string returned in | ||
205 | .Fa host | ||
206 | is a result of binary to numeric-text translation (like | ||
207 | .Xr inet_ntop 3 ) , | ||
208 | or is the result of a DNS reverse lookup. | ||
209 | Because of this, malicious parties could set up a PTR record as follows: | ||
210 | .Bd -literal -offset indent | ||
211 | 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1 | ||
212 | .Ed | ||
213 | .Pp | ||
214 | and trick the caller of | ||
215 | .Fn getnameinfo | ||
216 | into believing that | ||
217 | .Fa sa | ||
218 | is | ||
219 | .Li 10.1.1.1 | ||
220 | when it is actually | ||
221 | .Li 127.0.0.1 . | ||
222 | .Pp | ||
223 | To prevent such attacks, the use of | ||
224 | .Dv NI_NAMEREQD | ||
225 | is recommended when the result of | ||
226 | .Fn getnameinfo | ||
227 | is used | ||
228 | for access control purposes: | ||
229 | .Bd -literal -offset indent | ||
230 | struct sockaddr *sa; | ||
231 | char addr[NI_MAXHOST]; | ||
232 | struct addrinfo hints, *res; | ||
233 | int error; | ||
234 | |||
235 | error = getnameinfo(sa, sa->sa_len, addr, sizeof(addr), | ||
236 | NULL, 0, NI_NAMEREQD); | ||
237 | if (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 | ||
255 | The implementation of | ||
256 | .Fn getnameinfo | ||
257 | is not thread-safe. | ||
258 | .Pp | ||
259 | .Ox | ||
260 | intentionally uses a different | ||
261 | .Dv NI_MAXHOST | ||
262 | value from what | ||
263 | .Tn "RFC 2553" | ||
264 | suggests, to avoid buffer length handling mistakes. | ||