xref: /dragonfly/bin/hostname/hostname.c (revision 9348a738)
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, "46i: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 'i':
136 			siflag = optarg;
137 			silen = strlen(siflag);
138 			iflag |= HST_IF;
139 			break;
140 		case 'r':
141 			srflag = optarg;
142 			rflag = 1;
143 			break;
144 		case 's':
145 			sflag = 1;
146 			break;
147 		default:
148 			usage();
149 		}
150 	}
151 	argc -= optind;
152 	argv += optind;
153 
154 	if (argc > 1)
155 		usage();
156 
157 	if (iflag && *argv)
158 		usage();
159 
160 	if (rflag && *argv)
161 		usage();
162 
163 	if (rflag && (iflag & HST_IF))
164 		usage();
165 
166 	if ((iflag & HST_IF_V6) && (iflag & HST_IF_V4))
167 		usage();
168 
169 	if (!(iflag & HST_IF) && ((iflag & HST_IF_V6)||iflag & HST_IF_V4))
170 		usage();
171 
172 	if (iflag & HST_IF) {
173 		mib[0] = CTL_NET;
174 		mib[1] = PF_ROUTE;
175 		mib[2] = 0;
176 		mib[3] = 0;
177 		mib[4] = NET_RT_IFLIST;
178 		mib[5] = 0;
179 
180 		idx = 0;
181 		needed = 1;
182 
183 		if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
184 			err(1, "sysctl: iflist-sysctl-estimate");
185 		if ((buf = malloc(needed)) == NULL)
186 			err(1, "malloc failed");
187 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
188 			err(1, "sysctl: retrieval of interface table");
189 
190 		for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
191 			rtm = (struct rt_msghdr *)(void *)next;
192 			if (rtm->rtm_version != RTM_VERSION)
193 				continue;
194 			switch (rtm->rtm_type) {
195 			case RTM_IFINFO:
196 				ifm = (struct if_msghdr *)(void *)rtm;
197 
198 				if ((ifm->ifm_addrs & RTA_IFP) == 0)
199 					break;
200 				sdl = (struct sockaddr_dl *)(ifm + 1);
201 				if (silen != sdl->sdl_nlen)
202 					break;
203 				if (!strncmp(siflag, sdl->sdl_data, silen)) {
204 					idx = ifm->ifm_index;
205 				}
206 				break;
207 			case RTM_NEWADDR:
208 				ifam = (struct ifa_msghdr *)(void *)rtm;
209 
210 				if (ifam->ifam_index == idx) {
211 					info.rti_addrs = ifam->ifam_addrs;
212 					rt_xaddrs((char *)(ifam + 1),
213 						ifam->ifam_msglen + (char *)ifam, &info);
214 					sai = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
215 
216 					if (iflag & HST_IF_V6) {
217 						if (sai->sin_family == AF_INET6) {
218 							sai6 = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
219 							hst = gethostbyaddr(&sai6->sin6_addr,
220 									sizeof(sai6->sin6_addr),AF_INET6);
221 
222 							if (h_errno == NETDB_SUCCESS) {
223 								next = buf + needed;
224 								continue;
225 							}
226 						}
227 					} else {
228 						if ((sai->sin_family == AF_INET)) {
229 
230 							hst = gethostbyaddr(&sai->sin_addr,
231 									sizeof(sai->sin_addr),AF_INET);
232 
233 							if (h_errno == NETDB_SUCCESS) {
234 								next = buf + needed;
235 								continue;
236 							}
237 						}
238 					}
239 				}
240 				break;
241 			} /* switch */
242 		} /* loop */
243 
244 		free(buf);
245 
246 		if (idx == 0)
247 			errx(1,"interface not found");
248 		if (hst == NULL)
249 			errx(1, "ip not found on interface");
250 
251 		if (h_errno == NETDB_SUCCESS) {
252 			if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
253 				err(1, "sethostname");
254 		} else if (h_errno == HOST_NOT_FOUND) {
255 			errx(1,"hostname not found");
256 		} else {
257 			herror("gethostbyaddr");
258 			exit(1);
259 		}
260 	} else if (rflag) {
261 		ret = inet_pton(AF_INET, srflag, &ia);
262 		if (ret != 1) {
263 			/* check IPV6 */
264 			ret = inet_pton(AF_INET6, srflag, &ia6);
265 
266 			if (ret != 1) {
267 				errx(1, "invalid ip address");
268 			}
269 
270 			flag6 = 1;
271 		}
272 
273 		if (flag6 == 1)
274 			hst = gethostbyaddr(&ia6, sizeof(ia6), AF_INET6);
275 		else
276 			hst = gethostbyaddr(&ia, sizeof(ia), AF_INET);
277 		if (!hst) {
278 			if (h_errno == HOST_NOT_FOUND)
279 				errx(1,"host not found\n");
280 		}
281 
282 		if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
283 			err(1, "sethostname");
284 	} else if (*argv) {
285 		if (sethostname(*argv, (int)strlen(*argv)))
286 			err(1, "sethostname");
287 	} else {
288 		if (gethostname(hostname, (int)sizeof(hostname)))
289 			err(1, "gethostname");
290 		if (sflag && (p = strchr(hostname, '.')))
291 			*p = '\0';
292 		printf("%s\n", hostname);
293 	}
294 	exit(0);
295 }
296 
297 static void
298 usage(void)
299 {
300 	fprintf(stderr, "usage: hostname [-s] [name-of-host |"
301 			" -r ip-address | -i interface [-4 | -6]]\n");
302 	exit(1);
303 }
304 
305