1 /* $NetBSD: hostname.c,v 1.20 2013/07/19 15:53:00 christos Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)hostname.c 8.2 (Berkeley) 4/28/95";
42 __RCSID("$NetBSD: hostname.c,v 1.20 2013/07/19 15:53:00 christos Exp $");
46 #include <sys/param.h>
47 #include <sys/socket.h>
50 #include <netinet/in.h>
60 __dead
static void usage(void);
63 main(int argc
, char *argv
[])
65 int ch
, Aflag
, aflag
, dflag
, Iflag
, iflag
, fflag
, sflag
, i
;
66 char *p
, hostname
[MAXHOSTNAMELEN
+ 1];
67 struct addrinfo hints
, *ainfos
, *ai
;
69 struct ifaddrs
*ifa
, *ifp
;
70 struct sockaddr_in6
*sin6
;
71 char buf
[MAX(MAXHOSTNAMELEN
+ 1, INET6_ADDRSTRLEN
)];
74 Aflag
= aflag
= dflag
= Iflag
= iflag
= fflag
= sflag
= 0;
75 while ((ch
= getopt(argc
, argv
, "AadIifs")) != -1)
109 if (sethostname(*argv
, strlen(*argv
)))
110 err(1, "sethostname");
111 } else if (Aflag
|| Iflag
) {
112 if (getifaddrs(&ifa
) == -1)
113 err(1, "getifaddrs");
114 for (ifp
= ifa
; ifp
; ifp
= ifp
->ifa_next
) {
115 if (ifp
->ifa_addr
== NULL
||
116 #if !defined(__minix)
117 ifp
->ifa_flags
& IFF_LOOPBACK
||
119 !(ifp
->ifa_flags
& IFF_UP
))
122 switch(ifp
->ifa_addr
->sa_family
) {
126 /* Skip link local addresses */
127 sin6
= (struct sockaddr_in6
*)ifp
->ifa_addr
;
128 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
129 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
->sin6_addr
))
133 /* We only translate IPv4 or IPv6 addresses */
136 i
= getnameinfo(ifp
->ifa_addr
, ifp
->ifa_addr
->sa_len
,
137 buf
, sizeof(buf
), NULL
, 0,
138 Iflag
? NI_NUMERICHOST
: NI_NAMEREQD
);
140 if (Iflag
&& i
!= EAI_NONAME
)
141 errx(1, "getnameinfo: %s",
148 if (gethostname(hostname
, sizeof(hostname
)))
149 err(1, "gethostname");
150 hostname
[sizeof(hostname
) - 1] = '\0';
152 if ((hent
= gethostbyname(hostname
)) == NULL
)
153 errx(1, "gethostbyname: %s",
155 for (i
= 0; hent
->h_aliases
[i
]; i
++)
156 printf("%s\n", hent
->h_aliases
[i
]);
157 } else if (dflag
|| iflag
|| fflag
) {
158 memset(&hints
, 0, sizeof(hints
));
159 hints
.ai_family
= AF_UNSPEC
;
160 hints
.ai_socktype
= SOCK_DGRAM
;
161 hints
.ai_flags
= AI_CANONNAME
;
162 i
= getaddrinfo(hostname
, NULL
, &hints
, &ainfos
);
164 errx(1, "getaddrinfo: %s", gai_strerror(i
));
167 if ((p
= strchr(ainfos
->ai_canonname
,
169 printf("%s\n", p
+ 1);
171 for (ai
= ainfos
; ai
; ai
= ai
->ai_next
)
173 i
= getnameinfo(ai
->ai_addr
,
175 buf
, sizeof(buf
), NULL
, 0,
185 (p
= strchr(ainfos
->ai_canonname
,
188 printf("%s\n", ainfos
->ai_canonname
);
190 freeaddrinfo(ainfos
);
193 if (sflag
&& (p
= strchr(hostname
, '.')))
195 printf("%s\n", hostname
);
205 (void)fprintf(stderr
, "usage: %s [-AadfIis] [name-of-host]\n",