Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/lib/libc/net/getnameinfo.c
39476 views
1
/* $KAME: getnameinfo.c,v 1.61 2002/06/27 09:25:47 itojun Exp $ */
2
3
/*-
4
* SPDX-License-Identifier: BSD-3-Clause
5
*
6
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7
* Copyright (c) 2000 Ben Harris.
8
* All rights reserved.
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
* 3. Neither the name of the project nor the names of its 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 PROJECT 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 PROJECT 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
/*
36
* Issues to be discussed:
37
* - Thread safe-ness must be checked
38
* - RFC2553 says that we should raise error on short buffer. X/Open says
39
* we need to truncate the result. We obey RFC2553 (and X/Open should be
40
* modified). ipngwg rough consensus seems to follow RFC2553.
41
* - What is "local" in NI_FQDN?
42
* - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
43
* - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
44
* sin6_scope_id is filled - standardization status?
45
* XXX breaks backward compat for code that expects no scopeid.
46
* beware on merge.
47
*/
48
49
#include <sys/types.h>
50
#include <sys/socket.h>
51
#include <sys/un.h>
52
#include <net/if.h>
53
#include <net/if_dl.h>
54
#include <net/if_types.h>
55
#include <net/firewire.h>
56
#include <netinet/in.h>
57
#include <arpa/inet.h>
58
#include <arpa/nameser.h>
59
#include <netdb.h>
60
#include <resolv.h>
61
#include <string.h>
62
#include <stddef.h>
63
#include <errno.h>
64
65
static const struct afd *find_afd(int);
66
static int getnameinfo_inet(const struct afd *,
67
const struct sockaddr *, socklen_t, char *,
68
size_t, char *, size_t, int);
69
#ifdef INET6
70
static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
71
size_t, int);
72
static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
73
#endif
74
static int getnameinfo_link(const struct afd *,
75
const struct sockaddr *, socklen_t, char *,
76
size_t, char *, size_t, int);
77
static int hexname(const u_int8_t *, size_t, char *, size_t);
78
static int getnameinfo_un(const struct afd *,
79
const struct sockaddr *, socklen_t, char *,
80
size_t, char *, size_t, int);
81
82
static const struct afd {
83
int a_af;
84
size_t a_addrlen;
85
socklen_t a_socklen;
86
int a_off;
87
int (*a_func)(const struct afd *,
88
const struct sockaddr *, socklen_t, char *,
89
size_t, char *, size_t, int);
90
} afdl [] = {
91
#ifdef INET6
92
{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
93
offsetof(struct sockaddr_in6, sin6_addr),
94
getnameinfo_inet},
95
#endif
96
{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
97
offsetof(struct sockaddr_in, sin_addr),
98
getnameinfo_inet},
99
#define sizeofmember(type, member) (sizeof(((type *)0)->member))
100
{PF_LOCAL, sizeofmember(struct sockaddr_un, sun_path),
101
sizeof(struct sockaddr_un),
102
offsetof(struct sockaddr_un, sun_path),
103
getnameinfo_un},
104
{PF_LINK, sizeofmember(struct sockaddr_dl, sdl_data),
105
sizeof(struct sockaddr_dl),
106
offsetof(struct sockaddr_dl, sdl_data),
107
getnameinfo_link},
108
{0, 0, 0},
109
};
110
111
int
112
getnameinfo(const struct sockaddr *sa, socklen_t salen,
113
char *host, size_t hostlen, char *serv, size_t servlen,
114
int flags)
115
{
116
const struct afd *afd;
117
118
if (sa == NULL)
119
return (EAI_FAIL);
120
121
afd = find_afd(sa->sa_family);
122
if (afd == NULL)
123
return (EAI_FAMILY);
124
/*
125
* getnameinfo() accepts an salen of sizeof(struct sockaddr_storage)
126
* at maximum as shown in RFC 4038 Sec.6.2.3.
127
*/
128
if (salen > sizeof(struct sockaddr_storage))
129
return (EAI_FAMILY);
130
131
switch (sa->sa_family) {
132
case PF_LOCAL:
133
/*
134
* PF_LOCAL uses variable salen depending on the
135
* content length of sun_path. Require 1 byte in
136
* sun_path at least.
137
*/
138
if (salen <= afd->a_socklen -
139
sizeofmember(struct sockaddr_un, sun_path))
140
return (EAI_FAMILY);
141
else if (salen > afd->a_socklen)
142
salen = afd->a_socklen;
143
break;
144
case PF_LINK:
145
if (salen <= afd->a_socklen -
146
sizeofmember(struct sockaddr_dl, sdl_data))
147
return (EAI_FAMILY);
148
break;
149
default:
150
if (salen < afd->a_socklen)
151
return (EAI_FAMILY);
152
else
153
salen = afd->a_socklen;
154
break;
155
}
156
157
return ((*afd->a_func)(afd, sa, salen, host, hostlen,
158
serv, servlen, flags));
159
}
160
161
static const struct afd *
162
find_afd(int af)
163
{
164
const struct afd *afd;
165
166
if (af == PF_UNSPEC)
167
return (NULL);
168
for (afd = &afdl[0]; afd->a_af > 0; afd++) {
169
if (afd->a_af == af)
170
return (afd);
171
}
172
return (NULL);
173
}
174
175
static int
176
getnameinfo_inet(const struct afd *afd,
177
const struct sockaddr *sa, socklen_t salen,
178
char *host, size_t hostlen, char *serv, size_t servlen,
179
int flags)
180
{
181
struct servent *sp;
182
struct hostent *hp;
183
u_short port;
184
const char *addr;
185
u_int32_t v4a;
186
int h_error;
187
char numserv[512];
188
char numaddr[512];
189
190
/* network byte order */
191
port = ((const struct sockaddr_in *)sa)->sin_port;
192
addr = (const char *)sa + afd->a_off;
193
194
if (serv == NULL || servlen == 0) {
195
/*
196
* do nothing in this case.
197
* in case you are wondering if "&&" is more correct than
198
* "||" here: rfc2553bis-03 says that serv == NULL OR
199
* servlen == 0 means that the caller does not want the result.
200
*/
201
} else {
202
if (flags & NI_NUMERICSERV)
203
sp = NULL;
204
else {
205
sp = getservbyport(port,
206
(flags & NI_DGRAM) ? "udp" : "tcp");
207
}
208
if (sp) {
209
if (strlen(sp->s_name) + 1 > servlen)
210
return EAI_MEMORY;
211
strlcpy(serv, sp->s_name, servlen);
212
} else {
213
snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
214
if (strlen(numserv) + 1 > servlen)
215
return EAI_MEMORY;
216
strlcpy(serv, numserv, servlen);
217
}
218
}
219
220
switch (sa->sa_family) {
221
case AF_INET:
222
v4a = (u_int32_t)
223
ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr);
224
if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a) ||
225
IN_ZERONET(v4a))
226
flags |= NI_NUMERICHOST;
227
break;
228
#ifdef INET6
229
case AF_INET6:
230
{
231
const struct sockaddr_in6 *sin6;
232
233
sin6 = (const struct sockaddr_in6 *)sa;
234
235
/*
236
* https://pubs.opengroup.org/onlinepubs/9699919799/functions/getnameinfo.html
237
* "[IP6] [Option Start] If the socket address structure
238
* contains an IPv4-mapped IPv6 address or an IPv4-compatible
239
* IPv6 address, the implementation shall extract the embedded
240
* IPv4 address and lookup the node name for that IPv4 address.
241
* [Option End]"
242
* => getipnodebyaddr() handles this case for us.
243
* => in case of NI_NUMERICHOST being set, inet_ntop[6] will
244
* handle it too.
245
*
246
* "If the address is the IPv6 unspecified address ( "::" ),
247
* a lookup shall not be performed and the behavior shall be
248
* the same as when the node's name cannot be located."
249
* => getipnodebyaddr() handles this case for us.
250
* => in case of NI_NUMERICHOST being set,
251
* ip6_parsenumeric() -> inet_ntop[6] will handle it too.
252
*/
253
254
/*
255
* We used to exclude link-local from lookups.
256
* Even though calles in the resolver chain may not (yet)
257
* properly deal with them, we no longer do as for link-local
258
* there is a path to resolve these. See:
259
* RFC 6303 4.5. IPv6 Link-Local Addresses
260
* RFC 6762 4. Reverse Address Mapping
261
*
262
* XXX For IPv6 MC the only reference found was
263
* https://www.ietf.org/archive/id/draft-michaelson-as112-ipv6-02.html
264
* but there are also no "empty zone"s for x.0.f.f.ip6.arpa
265
* in DNS servers. Keep catching it here for now and
266
* do not attempt name resolution but return the address string.
267
*/
268
if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
269
flags |= NI_NUMERICHOST;
270
}
271
break;
272
#endif
273
}
274
if (host == NULL || hostlen == 0) {
275
/*
276
* do nothing in this case.
277
* in case you are wondering if "&&" is more correct than
278
* "||" here: rfc2553bis-03 says that host == NULL or
279
* hostlen == 0 means that the caller does not want the result.
280
*/
281
} else if (flags & NI_NUMERICHOST) {
282
size_t numaddrlen;
283
284
/* NUMERICHOST and NAMEREQD conflicts with each other */
285
if (flags & NI_NAMEREQD)
286
return EAI_NONAME;
287
288
switch(afd->a_af) {
289
#ifdef INET6
290
case AF_INET6:
291
{
292
int error;
293
294
if ((error = ip6_parsenumeric(sa, addr, host,
295
hostlen, flags)) != 0)
296
return(error);
297
break;
298
}
299
#endif
300
default:
301
if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
302
== NULL)
303
return EAI_SYSTEM;
304
numaddrlen = strlen(numaddr);
305
if (numaddrlen + 1 > hostlen) /* don't forget terminator */
306
return EAI_MEMORY;
307
strlcpy(host, numaddr, hostlen);
308
break;
309
}
310
} else {
311
hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error);
312
313
if (hp) {
314
#if 0
315
/*
316
* commented out, since "for local host" is not
317
* implemented here - see RFC2553 p30
318
*/
319
if (flags & NI_NOFQDN) {
320
char *p;
321
p = strchr(hp->h_name, '.');
322
if (p)
323
*p = '\0';
324
}
325
#endif
326
if (strlen(hp->h_name) + 1 > hostlen) {
327
freehostent(hp);
328
return EAI_MEMORY;
329
}
330
strlcpy(host, hp->h_name, hostlen);
331
freehostent(hp);
332
} else {
333
if (flags & NI_NAMEREQD)
334
return EAI_NONAME;
335
switch(afd->a_af) {
336
#ifdef INET6
337
case AF_INET6:
338
{
339
int error;
340
341
if ((error = ip6_parsenumeric(sa, addr, host,
342
hostlen,
343
flags)) != 0)
344
return(error);
345
break;
346
}
347
#endif
348
default:
349
if (inet_ntop(afd->a_af, addr, host,
350
hostlen) == NULL)
351
return EAI_SYSTEM;
352
break;
353
}
354
}
355
}
356
return(0);
357
}
358
359
#ifdef INET6
360
static int
361
ip6_parsenumeric(const struct sockaddr *sa, const char *addr,
362
char *host, size_t hostlen, int flags)
363
{
364
size_t numaddrlen;
365
char numaddr[512];
366
367
if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
368
return EAI_SYSTEM;
369
370
numaddrlen = strlen(numaddr);
371
if (numaddrlen + 1 > hostlen) /* don't forget terminator */
372
return EAI_OVERFLOW;
373
strlcpy(host, numaddr, hostlen);
374
375
if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) {
376
char zonebuf[MAXHOSTNAMELEN];
377
int zonelen;
378
379
zonelen = ip6_sa2str(
380
(const struct sockaddr_in6 *)(const void *)sa,
381
zonebuf, sizeof(zonebuf), flags);
382
if (zonelen < 0)
383
return EAI_OVERFLOW;
384
if (zonelen + 1 + numaddrlen + 1 > hostlen)
385
return EAI_OVERFLOW;
386
387
/* construct <numeric-addr><delim><zoneid> */
388
memcpy(host + numaddrlen + 1, zonebuf,
389
(size_t)zonelen);
390
host[numaddrlen] = SCOPE_DELIMITER;
391
host[numaddrlen + 1 + zonelen] = '\0';
392
}
393
394
return 0;
395
}
396
397
/* ARGSUSED */
398
static int
399
ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
400
{
401
unsigned int ifindex;
402
const struct in6_addr *a6;
403
int n;
404
405
ifindex = (unsigned int)sa6->sin6_scope_id;
406
a6 = &sa6->sin6_addr;
407
408
if ((flags & NI_NUMERICSCOPE) != 0) {
409
n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
410
if (n < 0 || n >= bufsiz)
411
return -1;
412
else
413
return n;
414
}
415
416
/* if_indextoname() does not take buffer size. not a good api... */
417
if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) ||
418
IN6_IS_ADDR_MC_NODELOCAL(a6)) && bufsiz >= IF_NAMESIZE) {
419
char *p = if_indextoname(ifindex, buf);
420
if (p) {
421
return(strlen(p));
422
}
423
}
424
425
/* last resort */
426
n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
427
if (n < 0 || (size_t)n >= bufsiz)
428
return -1;
429
else
430
return n;
431
}
432
#endif /* INET6 */
433
434
/*
435
* getnameinfo_link():
436
* Format a link-layer address into a printable format, paying attention to
437
* the interface type.
438
*/
439
/* ARGSUSED */
440
static int
441
getnameinfo_link(const struct afd *afd,
442
const struct sockaddr *sa, socklen_t salen,
443
char *host, size_t hostlen, char *serv, size_t servlen, int flags)
444
{
445
const struct sockaddr_dl *sdl =
446
(const struct sockaddr_dl *)(const void *)sa;
447
const struct fw_hwaddr *iha;
448
int n;
449
450
if (serv != NULL && servlen > 0)
451
*serv = '\0';
452
453
if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
454
n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
455
if (n >= hostlen) {
456
*host = '\0';
457
return (EAI_MEMORY);
458
}
459
return (0);
460
}
461
462
if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) {
463
n = sdl->sdl_nlen;
464
if (n >= hostlen) {
465
*host = '\0';
466
return (EAI_MEMORY);
467
}
468
memcpy(host, sdl->sdl_data, sdl->sdl_nlen);
469
host[n] = '\0';
470
return (0);
471
}
472
473
switch (sdl->sdl_type) {
474
case IFT_IEEE1394:
475
if (sdl->sdl_alen < sizeof(iha->sender_unique_ID_hi) +
476
sizeof(iha->sender_unique_ID_lo))
477
return EAI_FAMILY;
478
iha = (const struct fw_hwaddr *)(const void *)LLADDR(sdl);
479
return hexname((const u_int8_t *)&iha->sender_unique_ID_hi,
480
sizeof(iha->sender_unique_ID_hi) +
481
sizeof(iha->sender_unique_ID_lo),
482
host, hostlen);
483
/*
484
* The following have zero-length addresses.
485
* IFT_GIF (net/if_gif.c)
486
* IFT_LOOP (net/if_loop.c)
487
* IFT_PPP (net/if_tuntap.c)
488
* IFT_SLIP (net/if_sl.c, net/if_strip.c)
489
* IFT_STF (net/if_stf.c)
490
* IFT_L2VLAN (net/if_vlan.c)
491
* IFT_BRIDGE (net/if_bridge.h>
492
*/
493
/*
494
* The following use IPv4 addresses as link-layer addresses:
495
* IFT_OTHER (net/if_gre.c)
496
* IFT_OTHER (netinet/ip_ipip.c)
497
*/
498
/* default below is believed correct for all these. */
499
case IFT_ETHER:
500
case IFT_FDDI:
501
case IFT_HIPPI:
502
case IFT_ISO88025:
503
default:
504
return hexname((u_int8_t *)LLADDR(sdl), (size_t)sdl->sdl_alen,
505
host, hostlen);
506
}
507
}
508
509
static int
510
hexname(const u_int8_t *cp, size_t len, char *host, size_t hostlen)
511
{
512
int i, n;
513
char *outp = host;
514
515
*outp = '\0';
516
for (i = 0; i < len; i++) {
517
n = snprintf(outp, hostlen, "%s%02x",
518
i ? ":" : "", cp[i]);
519
if (n < 0 || n >= hostlen) {
520
*host = '\0';
521
return EAI_MEMORY;
522
}
523
outp += n;
524
hostlen -= n;
525
}
526
return 0;
527
}
528
529
/*
530
* getnameinfo_un():
531
* Format a UNIX IPC domain address (pathname).
532
*/
533
/* ARGSUSED */
534
static int
535
getnameinfo_un(const struct afd *afd,
536
const struct sockaddr *sa, socklen_t salen,
537
char *host, size_t hostlen, char *serv, size_t servlen, int flags)
538
{
539
size_t pathlen;
540
541
if (serv != NULL && servlen > 0)
542
*serv = '\0';
543
if (host != NULL && hostlen > 0) {
544
pathlen = salen - afd->a_off;
545
546
if (pathlen + 1 > hostlen) {
547
*host = '\0';
548
return (EAI_MEMORY);
549
}
550
strlcpy(host, (const char *)sa + afd->a_off, pathlen + 1);
551
}
552
553
return (0);
554
}
555
556