1 /* $OpenBSD: inetname.c,v 1.3 2017/01/21 11:32:04 guenther Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1992, 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
32 #include <sys/signal.h>
33 #include <sys/socket.h>
34
35 #include <netinet/in.h>
36
37 #include <netdb.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "systat.h"
42
43 const char *
inet6name(struct in6_addr * in6)44 inet6name(struct in6_addr *in6)
45 {
46 static char line[NI_MAXHOST];
47 struct sockaddr_in6 sin6;
48 int flags;
49
50 flags = nflag ? NI_NUMERICHOST : 0;
51 if (IN6_IS_ADDR_UNSPECIFIED(in6))
52 return "*";
53 memset(&sin6, 0, sizeof(sin6));
54 sin6.sin6_family = AF_INET6;
55 sin6.sin6_addr = *in6;
56 if (getnameinfo((struct sockaddr *)&sin6, sizeof(struct sockaddr_in6),
57 line, sizeof(line), NULL, 0, flags) == 0)
58 return line;
59 return "?";
60 }
61
62 const char *
inetname(struct in_addr in)63 inetname(struct in_addr in)
64 {
65 static char line[NI_MAXHOST];
66 struct sockaddr_in si;
67 int flags, e;
68
69 flags = nflag ? NI_NUMERICHOST : 0;
70 if (in.s_addr == INADDR_ANY)
71 return "*";
72
73 memset(&si, 0, sizeof(si));
74 si.sin_family = AF_INET;
75 si.sin_addr = in;
76
77 e = getnameinfo((struct sockaddr *)&si, sizeof(struct sockaddr_in),
78 line, sizeof(line), NULL, 0, flags);
79
80 if (e == 0)
81 return line;
82
83 error("Lookup: %s", gai_strerror(e));
84
85 return "?";
86 }
87