xref: /dragonfly/bin/hostname/hostname.c (revision ce7a3582)
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. 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.
20  *
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
31  * SUCH DAMAGE.
32  *
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  */
37 
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>
44 
45 #include <net/ethernet.h>
46 #include <net/if.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>
52 
53 #include <err.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include <netdb.h>
60 #include <sys/types.h>
61 #include <arpa/inet.h>
62 
63 #include <errno.h>
64 
65 #define HST_IF    (1 << 0)
66 #define HST_IF_V6 (1 << 1)
67 #define HST_IF_V4 (1 << 2)
68 
69 
70 
71 /*
72  * Expand the compacted form of addresses as returned via the
73  * configuration read via sysctl().
74  * Lifted from getifaddrs(3)
75  */
76 
77 static void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
78 static void usage (void);
79 
80 #define ROUNDUP(a) \
81 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
82 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
83 
84 static
85 void
86 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
87 {
88 	struct sockaddr *sa;
89 	int i;
90 
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)
94 			continue;
95 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
96 		ADVANCE(cp, sa);
97 	}
98 }
99 
100 int
101 main(int argc, char **argv)
102 {
103 	int ch, sflag, rflag, ret, flag6, iflag;
104 	int silen = 0;
105 	char hostname[MAXHOSTNAMELEN];
106 	char *srflag, *siflag;
107 	struct hostent *hst;
108 	struct in_addr ia;
109 	struct in6_addr ia6;
110 
111 	int mib[6];
112 	size_t needed;
113 	char *buf, *next, *p;
114 	int idx;
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;
122 
123 	srflag = NULL;
124 	siflag = NULL;
125 	iflag = sflag = rflag = 0;
126 	flag6 = 0;
127 	hst = NULL;
128 
129 	while ((ch = getopt(argc, argv, "46i:r:s")) != -1) {
130 		switch (ch) {
131 		case '4':
132 			iflag |= HST_IF_V4;
133 			break;
134 		case '6':
135 			iflag |= HST_IF_V6;
136 			break;
137 		case 'i':
138 			siflag = optarg;
139 			silen = strlen(siflag);
140 			iflag |= HST_IF;
141 			break;
142 		case 'r':
143 			srflag = optarg;
144 			rflag = 1;
145 			break;
146 		case 's':
147 			sflag = 1;
148 			break;
149 		default:
150 			usage();
151 		}
152 	}
153 	argc -= optind;
154 	argv += optind;
155 
156 	if (argc > 1)
157 		usage();
158 
159 	if (iflag && *argv)
160 		usage();
161 
162 	if (rflag && *argv)
163 		usage();
164 
165 	if (rflag && (iflag & HST_IF))
166 		usage();
167 
168 	if ((iflag & HST_IF_V6) && (iflag & HST_IF_V4))
169 		usage();
170 
171 	if (!(iflag & HST_IF) && ((iflag & HST_IF_V6)||iflag & HST_IF_V4))
172 		usage();
173 
174 	if (iflag & HST_IF) {
175 		mib[0] = CTL_NET;
176 		mib[1] = PF_ROUTE;
177 		mib[2] = 0;
178 		mib[3] = 0;
179 		mib[4] = NET_RT_IFLIST;
180 		mib[5] = 0;
181 
182 		idx = 0;
183 		needed = 1;
184 
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");
191 
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)
195 				continue;
196 			switch (rtm->rtm_type) {
197 			case RTM_IFINFO:
198 				ifm = (struct if_msghdr *)(void *)rtm;
199 
200 				if ((ifm->ifm_addrs & RTA_IFP) == 0)
201 					break;
202 				sdl = (struct sockaddr_dl *)(ifm + 1);
203 				if (silen != sdl->sdl_nlen)
204 					break;
205 				if (!strncmp(siflag, sdl->sdl_data, silen)) {
206 					idx = ifm->ifm_index;
207 				}
208 				break;
209 			case RTM_NEWADDR:
210 				ifam = (struct ifa_msghdr *)(void *)rtm;
211 
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];
217 
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);
223 
224 							if (h_errno == NETDB_SUCCESS) {
225 								next = buf + needed;
226 								continue;
227 							}
228 						}
229 					} else {
230 						if ((sai->sin_family == AF_INET)) {
231 
232 							hst = gethostbyaddr(&sai->sin_addr,
233 									sizeof(sai->sin_addr),AF_INET);
234 
235 							if (h_errno == NETDB_SUCCESS) {
236 								next = buf + needed;
237 								continue;
238 							}
239 						}
240 					}
241 				}
242 				break;
243 			} /* switch */
244 		} /* loop */
245 
246 		free(buf);
247 
248 		if (idx == 0)
249 			errx(1,"interface not found");
250 		if (hst == NULL)
251 			errx(1, "ip not found on interface");
252 
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");
258 		} else {
259 			herror("gethostbyaddr");
260 			exit(1);
261 		}
262 	} else if (rflag) {
263 		ret = inet_pton(AF_INET, srflag, &ia);
264 		if (ret != 1) {
265 			/* check IPV6 */
266 			ret = inet_pton(AF_INET6, srflag, &ia6);
267 
268 			if (ret != 1) {
269 				errx(1, "invalid ip address");
270 			}
271 
272 			flag6 = 1;
273 		}
274 
275 		if (flag6 == 1)
276 			hst = gethostbyaddr(&ia6, sizeof(ia6), AF_INET6);
277 		else
278 			hst = gethostbyaddr(&ia, sizeof(ia), AF_INET);
279 		if (!hst) {
280 			if (h_errno == HOST_NOT_FOUND)
281 				errx(1,"host not found\n");
282 		}
283 
284 		if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
285 			err(1, "sethostname");
286 	} else if (*argv) {
287 		if (sethostname(*argv, (int)strlen(*argv)))
288 			err(1, "sethostname");
289 	} else {
290 		if (gethostname(hostname, (int)sizeof(hostname)))
291 			err(1, "gethostname");
292 		if (sflag && (p = strchr(hostname, '.')))
293 			*p = '\0';
294 		printf("%s\n", hostname);
295 	}
296 	exit(0);
297 }
298 
299 static void
300 usage(void)
301 {
302 	fprintf(stderr, "usage: hostname [-s] [name-of-host |"
303 			" -r ip-address | -i interface [-4 | -6]]\n");
304 	exit(1);
305 }
306 
307