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 $
36 * $DragonFly: src/bin/hostname/hostname.c,v 1.7 2004/03/19 17:17:46 cpressey Exp $
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
44 #include <sys/module.h>
45 #include <sys/linker.h>
48 #include <net/ethernet.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
64 #include <sys/types.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
70 #define HST_IF (1 << 0)
71 #define HST_IF_V6 (1 << 1)
72 #define HST_IF_V4 (1 << 2)
77 * Expand the compacted form of addresses as returned via the
78 * configuration read via sysctl().
79 * Lifted from getifaddrs(3)
82 static void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
83 static void usage (void);
86 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
87 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
91 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
96 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
97 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
98 if ((rtinfo->rti_addrs & (1 << i)) == 0)
100 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
106 main(int argc, char **argv)
108 int ch,sflag,rflag,ret,flag6,iflag;
109 char hostname[MAXHOSTNAMELEN];
110 char *srflag, *siflag = NULL;
117 char *buf,*lim,*next,*p;
119 struct sockaddr_dl *sdl;
120 struct rt_msghdr *rtm;
121 struct if_msghdr *ifm;
122 struct ifa_msghdr *ifam;
123 struct rt_addrinfo info;
124 struct sockaddr_in *sai;
125 struct sockaddr_in6 *sai6;
128 iflag = sflag = rflag = 0;
132 while ((ch = getopt(argc, argv, "46i:r:s")) != -1) {
141 siflag = (char*)calloc(1,sizeof(char) * (strlen((char*)optarg)+1));
144 strlcpy(siflag, (char*)optarg, strlen((char*)optarg)+1);
150 srflag = (char*)calloc(1,sizeof(char) * (strlen((char*)optarg)+1));
153 strlcpy(srflag, (char*)optarg, strlen((char*)optarg)+1);
172 if (iflag && *argv) {
177 if (rflag && *argv) {
182 if (rflag && (iflag & HST_IF)) {
188 if ((iflag & HST_IF_V6) && (iflag & HST_IF_V4)) {
193 if (!(iflag & HST_IF) && ((iflag & HST_IF_V6)||iflag & HST_IF_V4)) {
198 if (iflag & HST_IF) {
203 mib[4] = NET_RT_IFLIST;
209 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
211 errx(1, "iflist-sysctl-estimate:%i",errno);
213 if ((buf = malloc(needed)) == NULL) {
217 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
219 errx(1, "actual retrieval of interface table");
224 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
225 rtm = (struct rt_msghdr *)(void *)next;
226 if (rtm->rtm_version != RTM_VERSION)
228 switch (rtm->rtm_type) {
230 ifm = (struct if_msghdr *)(void *)rtm;
232 if (ifm->ifm_addrs & RTA_IFP) {
233 sdl = (struct sockaddr_dl *)(ifm+1);
234 if (strcmp(siflag,sdl->sdl_data) == 0) {
235 idx = ifm->ifm_index;
240 ifam = (struct ifa_msghdr *)(void *)rtm;
242 if (ifam->ifam_index == idx) {
243 info.rti_addrs = ifam->ifam_addrs;
244 rt_xaddrs((char *)(ifam + 1),
245 ifam->ifam_msglen + (char *)ifam, &info);
246 sai = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
248 if (iflag & HST_IF_V6) {
249 if (sai->sin_family == AF_INET6) {
250 sai6 = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
251 hst = gethostbyaddr((const char*)&sai6->sin6_addr,
252 sizeof(sai6->sin6_addr),AF_INET6);
254 if (h_errno == NETDB_SUCCESS) {
260 if ((sai->sin_family == AF_INET)) {
262 hst = gethostbyaddr((const char*)&sai->sin_addr,
263 sizeof(sai->sin_addr),AF_INET);
265 if (h_errno == NETDB_SUCCESS) {
280 errx(1,"interface not found");
283 errx(1, "ip not found on interface");
286 if (h_errno == NETDB_SUCCESS) {
287 if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
288 errx(1, "sethostname");
289 } else if (h_errno == HOST_NOT_FOUND) {
290 errx(1,"hostname not found");
292 errx(1,"gethostbyaddr");
295 ret = inet_pton(AF_INET, srflag, &ia);
298 ret = inet_pton(AF_INET6, srflag, &ia6);
302 errx(1, "invalid ip address");
309 hst = gethostbyaddr((const char*)&ia6, sizeof(ia6), AF_INET6);
311 hst = gethostbyaddr((const char*)&ia, sizeof(ia), AF_INET);
314 if(h_errno == HOST_NOT_FOUND)
315 errx(1,"host not found\n");
318 if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
319 err(1, "sethostname");
321 if (sethostname(*argv, (int)strlen(*argv)))
322 err(1, "sethostname");
324 if (gethostname(hostname, (int)sizeof(hostname)))
325 err(1, "gethostname");
326 if (sflag && (p = strchr(hostname, '.')))
328 (void)printf("%s\n", hostname);
336 fprintf(stderr, "usage: hostname [-s] [name-of-host |"
337 " -r ip-address | -i interface [-4 | -6]]\n");