xref: /dragonfly/bin/hostname/hostname.c (revision de78d61c)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1988, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)hostname.c	8.1 (Berkeley) 5/31/93
31  * $FreeBSD: src/bin/hostname/hostname.c,v 1.10.2.1 2001/08/01 02:40:23 obrien Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/module.h>
39 #include <sys/linker.h>
40 
41 #include <net/ethernet.h>
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
48 
49 #include <err.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include <netdb.h>
56 #include <sys/types.h>
57 #include <arpa/inet.h>
58 
59 #include <errno.h>
60 
61 #define HST_IF    (1 << 0)
62 #define HST_IF_V6 (1 << 1)
63 #define HST_IF_V4 (1 << 2)
64 
65 /*
66  * Expand the compacted form of addresses as returned via the
67  * configuration read via sysctl().
68  * Lifted from getifaddrs(3)
69  */
70 
71 static void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
72 static void usage (void);
73 
74 #ifndef RT_ROUNDUP
75 #define RT_ROUNDUP(a) \
76 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
77 #endif
78 #ifndef RT_ADVANCE
79 #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
80 #endif
81 
82 static
83 void
84 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
85 {
86 	struct sockaddr *sa;
87 	int i;
88 
89 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
90 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
91 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
92 			continue;
93 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
94 		RT_ADVANCE(cp, sa);
95 	}
96 }
97 
98 int
99 main(int argc, char **argv)
100 {
101 	int ch, sflag, rflag, ret, flag6, iflag;
102 	int silen = 0;
103 	char hostname[MAXHOSTNAMELEN];
104 	char *srflag, *siflag;
105 	struct hostent *hst;
106 	struct in_addr ia;
107 	struct in6_addr ia6;
108 
109 	int mib[6];
110 	size_t needed;
111 	char *buf, *next, *p;
112 	int idx;
113 	struct sockaddr_dl *sdl;
114 	struct rt_msghdr *rtm;
115 	struct if_msghdr *ifm;
116 	struct ifa_msghdr *ifam;
117 	struct rt_addrinfo info;
118 	struct sockaddr_in *sai;
119 	struct sockaddr_in6 *sai6;
120 
121 	srflag = NULL;
122 	siflag = NULL;
123 	iflag = sflag = rflag = 0;
124 	flag6 = 0;
125 	hst = NULL;
126 
127 	while ((ch = getopt(argc, argv, "46fi:r:s")) != -1) {
128 		switch (ch) {
129 		case '4':
130 			iflag |= HST_IF_V4;
131 			break;
132 		case '6':
133 			iflag |= HST_IF_V6;
134 			break;
135 		case 'f':
136 			/* Printing the FQDN is default. */
137 			break;
138 		case 'i':
139 			siflag = optarg;
140 			silen = strlen(siflag);
141 			iflag |= HST_IF;
142 			break;
143 		case 'r':
144 			srflag = optarg;
145 			rflag = 1;
146 			break;
147 		case 's':
148 			sflag = 1;
149 			break;
150 		default:
151 			usage();
152 		}
153 	}
154 	argc -= optind;
155 	argv += optind;
156 
157 	if (argc > 1)
158 		usage();
159 
160 	if (iflag && *argv)
161 		usage();
162 
163 	if (rflag && *argv)
164 		usage();
165 
166 	if (rflag && (iflag & HST_IF))
167 		usage();
168 
169 	if ((iflag & HST_IF_V6) && (iflag & HST_IF_V4))
170 		usage();
171 
172 	if (!(iflag & HST_IF) && ((iflag & HST_IF_V6)||iflag & HST_IF_V4))
173 		usage();
174 
175 	if (iflag & HST_IF) {
176 		mib[0] = CTL_NET;
177 		mib[1] = PF_ROUTE;
178 		mib[2] = 0;
179 		mib[3] = 0;
180 		mib[4] = NET_RT_IFLIST;
181 		mib[5] = 0;
182 
183 		idx = 0;
184 		needed = 1;
185 
186 		if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
187 			err(1, "sysctl: iflist-sysctl-estimate");
188 		if ((buf = malloc(needed)) == NULL)
189 			err(1, "malloc failed");
190 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
191 			err(1, "sysctl: retrieval of interface table");
192 
193 		for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
194 			rtm = (struct rt_msghdr *)(void *)next;
195 			if (rtm->rtm_version != RTM_VERSION)
196 				continue;
197 			switch (rtm->rtm_type) {
198 			case RTM_IFINFO:
199 				ifm = (struct if_msghdr *)(void *)rtm;
200 
201 				if ((ifm->ifm_addrs & RTA_IFP) == 0)
202 					break;
203 				sdl = (struct sockaddr_dl *)(ifm + 1);
204 				if (silen != sdl->sdl_nlen)
205 					break;
206 				if (!strncmp(siflag, sdl->sdl_data, silen)) {
207 					idx = ifm->ifm_index;
208 				}
209 				break;
210 			case RTM_NEWADDR:
211 				ifam = (struct ifa_msghdr *)(void *)rtm;
212 
213 				if (ifam->ifam_index == idx) {
214 					info.rti_addrs = ifam->ifam_addrs;
215 					rt_xaddrs((char *)(ifam + 1),
216 						ifam->ifam_msglen + (char *)ifam, &info);
217 					sai = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
218 
219 					if (iflag & HST_IF_V6) {
220 						if (sai->sin_family == AF_INET6) {
221 							sai6 = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
222 							hst = gethostbyaddr(&sai6->sin6_addr,
223 									sizeof(sai6->sin6_addr),AF_INET6);
224 
225 							if (h_errno == NETDB_SUCCESS) {
226 								next = buf + needed;
227 								continue;
228 							}
229 						}
230 					} else {
231 						if ((sai->sin_family == AF_INET)) {
232 
233 							hst = gethostbyaddr(&sai->sin_addr,
234 									sizeof(sai->sin_addr),AF_INET);
235 
236 							if (h_errno == NETDB_SUCCESS) {
237 								next = buf + needed;
238 								continue;
239 							}
240 						}
241 					}
242 				}
243 				break;
244 			} /* switch */
245 		} /* loop */
246 
247 		free(buf);
248 
249 		if (idx == 0)
250 			errx(1,"interface not found");
251 		if (hst == NULL)
252 			errx(1, "ip not found on interface");
253 
254 		if (h_errno == NETDB_SUCCESS) {
255 			if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
256 				err(1, "sethostname");
257 		} else if (h_errno == HOST_NOT_FOUND) {
258 			errx(1,"hostname not found");
259 		} else {
260 			herror("gethostbyaddr");
261 			exit(1);
262 		}
263 	} else if (rflag) {
264 		ret = inet_pton(AF_INET, srflag, &ia);
265 		if (ret != 1) {
266 			/* check IPV6 */
267 			ret = inet_pton(AF_INET6, srflag, &ia6);
268 
269 			if (ret != 1) {
270 				errx(1, "invalid ip address");
271 			}
272 
273 			flag6 = 1;
274 		}
275 
276 		if (flag6 == 1)
277 			hst = gethostbyaddr(&ia6, sizeof(ia6), AF_INET6);
278 		else
279 			hst = gethostbyaddr(&ia, sizeof(ia), AF_INET);
280 		if (!hst) {
281 			if (h_errno == HOST_NOT_FOUND)
282 				errx(1,"host not found\n");
283 		}
284 
285 		if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
286 			err(1, "sethostname");
287 	} else if (*argv) {
288 		if (sethostname(*argv, (int)strlen(*argv)))
289 			err(1, "sethostname");
290 	} else {
291 		if (gethostname(hostname, (int)sizeof(hostname)))
292 			err(1, "gethostname");
293 		if (sflag && (p = strchr(hostname, '.')))
294 			*p = '\0';
295 		printf("%s\n", hostname);
296 	}
297 	exit(0);
298 }
299 
300 static void
301 usage(void)
302 {
303 	fprintf(stderr, "usage: hostname [-s] [name-of-host |"
304 			" -r ip-address | -i interface [-4 | -6]]\n");
305 	exit(1);
306 }
307 
308