2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)hostname.c 8.1 (Berkeley) 5/31/93
35 * $FreeBSD: src/bin/hostname/hostname.c,v 1.10.2.1 2001/08/01 02:40:23 obrien Exp $
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/module.h>
43 #include <sys/linker.h>
45 #include <net/ethernet.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
60 #include <sys/types.h>
61 #include <arpa/inet.h>
65 #define HST_IF (1 << 0)
66 #define HST_IF_V6 (1 << 1)
67 #define HST_IF_V4 (1 << 2)
72 * Expand the compacted form of addresses as returned via the
73 * configuration read via sysctl().
74 * Lifted from getifaddrs(3)
77 static void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
78 static void usage (void);
81 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
82 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
86 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
91 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
92 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
93 if ((rtinfo->rti_addrs & (1 << i)) == 0)
95 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
101 main(int argc, char **argv)
103 int ch, sflag, rflag, ret, flag6, iflag;
105 char hostname[MAXHOSTNAMELEN];
106 char *srflag, *siflag;
113 char *buf, *next, *p;
115 struct sockaddr_dl *sdl;
116 struct rt_msghdr *rtm;
117 struct if_msghdr *ifm;
118 struct ifa_msghdr *ifam;
119 struct rt_addrinfo info;
120 struct sockaddr_in *sai;
121 struct sockaddr_in6 *sai6;
125 iflag = sflag = rflag = 0;
129 while ((ch = getopt(argc, argv, "46i:r:s")) != -1) {
139 silen = strlen(siflag);
165 if (rflag && (iflag & HST_IF))
168 if ((iflag & HST_IF_V6) && (iflag & HST_IF_V4))
171 if (!(iflag & HST_IF) && ((iflag & HST_IF_V6)||iflag & HST_IF_V4))
174 if (iflag & HST_IF) {
179 mib[4] = NET_RT_IFLIST;
185 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
186 err(1, "sysctl: iflist-sysctl-estimate");
187 if ((buf = malloc(needed)) == NULL)
188 err(1, "malloc failed");
189 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
190 err(1, "sysctl: retrieval of interface table");
192 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
193 rtm = (struct rt_msghdr *)(void *)next;
194 if (rtm->rtm_version != RTM_VERSION)
196 switch (rtm->rtm_type) {
198 ifm = (struct if_msghdr *)(void *)rtm;
200 if ((ifm->ifm_addrs & RTA_IFP) == 0)
202 sdl = (struct sockaddr_dl *)(ifm + 1);
203 if (silen != sdl->sdl_nlen)
205 if (!strncmp(siflag, sdl->sdl_data, silen)) {
206 idx = ifm->ifm_index;
210 ifam = (struct ifa_msghdr *)(void *)rtm;
212 if (ifam->ifam_index == idx) {
213 info.rti_addrs = ifam->ifam_addrs;
214 rt_xaddrs((char *)(ifam + 1),
215 ifam->ifam_msglen + (char *)ifam, &info);
216 sai = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
218 if (iflag & HST_IF_V6) {
219 if (sai->sin_family == AF_INET6) {
220 sai6 = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
221 hst = gethostbyaddr(&sai6->sin6_addr,
222 sizeof(sai6->sin6_addr),AF_INET6);
224 if (h_errno == NETDB_SUCCESS) {
230 if ((sai->sin_family == AF_INET)) {
232 hst = gethostbyaddr(&sai->sin_addr,
233 sizeof(sai->sin_addr),AF_INET);
235 if (h_errno == NETDB_SUCCESS) {
249 errx(1,"interface not found");
251 errx(1, "ip not found on interface");
253 if (h_errno == NETDB_SUCCESS) {
254 if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
255 err(1, "sethostname");
256 } else if (h_errno == HOST_NOT_FOUND) {
257 errx(1,"hostname not found");
259 herror("gethostbyaddr");
263 ret = inet_pton(AF_INET, srflag, &ia);
266 ret = inet_pton(AF_INET6, srflag, &ia6);
269 errx(1, "invalid ip address");
276 hst = gethostbyaddr(&ia6, sizeof(ia6), AF_INET6);
278 hst = gethostbyaddr(&ia, sizeof(ia), AF_INET);
280 if (h_errno == HOST_NOT_FOUND)
281 errx(1,"host not found\n");
284 if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
285 err(1, "sethostname");
287 if (sethostname(*argv, (int)strlen(*argv)))
288 err(1, "sethostname");
290 if (gethostname(hostname, (int)sizeof(hostname)))
291 err(1, "gethostname");
292 if (sflag && (p = strchr(hostname, '.')))
294 printf("%s\n", hostname);
302 fprintf(stderr, "usage: hostname [-s] [name-of-host |"
303 " -r ip-address | -i interface [-4 | -6]]\n");