xref: /openbsd-src/usr.bin/systat/inetname.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: inetname.c,v 1.1 2010/07/22 12:33:29 giovanni 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/param.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 *
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_len = sizeof(struct sockaddr_in6);
56         sin6.sin6_addr = *in6;
57         if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
58             line, sizeof(line), NULL, 0, flags) == 0)
59                 return line;
60         return "?";
61 }
62 
63 const char *
64 inetname(struct in_addr in)
65 {
66         static char line[NI_MAXHOST];
67         struct sockaddr_in si;
68         int flags, e;
69 
70         flags = nflag ? NI_NUMERICHOST : 0;
71         if (in.s_addr == INADDR_ANY)
72                 return "*";
73 
74         memset(&si, 0, sizeof(si));
75         si.sin_family = AF_INET;
76         si.sin_len = sizeof(struct sockaddr_in);
77         si.sin_addr = in;
78 
79         e = getnameinfo((struct sockaddr *)&si, si.sin_len,
80                         line, sizeof(line), NULL, 0, flags);
81 
82         if (e == 0)
83                 return line;
84 
85         error("Lookup: %s", gai_strerror(e));
86 
87         return "?";
88 }
89