xref: /dragonfly/bin/hostname/hostname.c (revision 25a2db75)
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  * 4. 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 
67 /*
68  * Expand the compacted form of addresses as returned via the
69  * configuration read via sysctl().
70  * Lifted from getifaddrs(3)
71  */
72 
73 static void rt_xaddrs(caddr_t, caddr_t, struct rt_addrinfo *);
74 static void usage (void);
75 
76 #define ROUNDUP(a) \
77 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
78 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
79 
80 static
81 void
82 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
83 {
84 	struct sockaddr *sa;
85 	int i;
86 
87 	memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
88 	for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
89 		if ((rtinfo->rti_addrs & (1 << i)) == 0)
90 			continue;
91 		rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
92 		ADVANCE(cp, sa);
93 	}
94 }
95 
96 int
97 main(int argc, char **argv)
98 {
99 	int ch, sflag, rflag, ret, flag6, iflag;
100 	int silen = 0;
101 	char hostname[MAXHOSTNAMELEN];
102 	char *srflag, *siflag;
103 	struct hostent *hst;
104 	struct in_addr ia;
105 	struct in6_addr ia6;
106 
107 	int mib[6];
108 	size_t needed;
109 	char *buf, *next, *p;
110 	int idx;
111 	struct sockaddr_dl *sdl;
112 	struct rt_msghdr *rtm;
113 	struct if_msghdr *ifm;
114 	struct ifa_msghdr *ifam;
115 	struct rt_addrinfo info;
116 	struct sockaddr_in *sai;
117 	struct sockaddr_in6 *sai6;
118 
119 	srflag = NULL;
120 	siflag = NULL;
121 	iflag = sflag = rflag = 0;
122 	flag6 = 0;
123 	hst = NULL;
124 
125 	while ((ch = getopt(argc, argv, "46i:r:s")) != -1) {
126 		switch (ch) {
127 		case '4':
128 			iflag |= HST_IF_V4;
129 			break;
130 		case '6':
131 			iflag |= HST_IF_V6;
132 			break;
133 		case 'i':
134 			siflag = optarg;
135 			silen = strlen(siflag);
136 			iflag |= HST_IF;
137 			break;
138 		case 'r':
139 			srflag = optarg;
140 			rflag = 1;
141 			break;
142 		case 's':
143 			sflag = 1;
144 			break;
145 		default:
146 			usage();
147 		}
148 	}
149 	argc -= optind;
150 	argv += optind;
151 
152 	if (argc > 1)
153 		usage();
154 
155 	if (iflag && *argv)
156 		usage();
157 
158 	if (rflag && *argv)
159 		usage();
160 
161 	if (rflag && (iflag & HST_IF))
162 		usage();
163 
164 	if ((iflag & HST_IF_V6) && (iflag & HST_IF_V4))
165 		usage();
166 
167 	if (!(iflag & HST_IF) && ((iflag & HST_IF_V6)||iflag & HST_IF_V4))
168 		usage();
169 
170 	if (iflag & HST_IF) {
171 		mib[0] = CTL_NET;
172 		mib[1] = PF_ROUTE;
173 		mib[2] = 0;
174 		mib[3] = 0;
175 		mib[4] = NET_RT_IFLIST;
176 		mib[5] = 0;
177 
178 		idx = 0;
179 		needed = 1;
180 
181 		if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
182 			err(1, "sysctl: iflist-sysctl-estimate");
183 		if ((buf = malloc(needed)) == NULL)
184 			err(1, "malloc failed");
185 		if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
186 			err(1, "sysctl: retrieval of interface table");
187 
188 		for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
189 			rtm = (struct rt_msghdr *)(void *)next;
190 			if (rtm->rtm_version != RTM_VERSION)
191 				continue;
192 			switch (rtm->rtm_type) {
193 			case RTM_IFINFO:
194 				ifm = (struct if_msghdr *)(void *)rtm;
195 
196 				if ((ifm->ifm_addrs & RTA_IFP) == 0)
197 					break;
198 				sdl = (struct sockaddr_dl *)(ifm + 1);
199 				if (silen != sdl->sdl_nlen)
200 					break;
201 				if (!strncmp(siflag, sdl->sdl_data, silen)) {
202 					idx = ifm->ifm_index;
203 				}
204 				break;
205 			case RTM_NEWADDR:
206 				ifam = (struct ifa_msghdr *)(void *)rtm;
207 
208 				if (ifam->ifam_index == idx) {
209 					info.rti_addrs = ifam->ifam_addrs;
210 					rt_xaddrs((char *)(ifam + 1),
211 						ifam->ifam_msglen + (char *)ifam, &info);
212 					sai = (struct sockaddr_in *)info.rti_info[RTAX_IFA];
213 
214 					if (iflag & HST_IF_V6) {
215 						if (sai->sin_family == AF_INET6) {
216 							sai6 = (struct sockaddr_in6 *)info.rti_info[RTAX_IFA];
217 							hst = gethostbyaddr(&sai6->sin6_addr,
218 									sizeof(sai6->sin6_addr),AF_INET6);
219 
220 							if (h_errno == NETDB_SUCCESS) {
221 								next = buf + needed;
222 								continue;
223 							}
224 						}
225 					} else {
226 						if ((sai->sin_family == AF_INET)) {
227 
228 							hst = gethostbyaddr(&sai->sin_addr,
229 									sizeof(sai->sin_addr),AF_INET);
230 
231 							if (h_errno == NETDB_SUCCESS) {
232 								next = buf + needed;
233 								continue;
234 							}
235 						}
236 					}
237 				}
238 				break;
239 			} /* switch */
240 		} /* loop */
241 
242 		free(buf);
243 
244 		if (idx == 0)
245 			errx(1,"interface not found");
246 		if (hst == NULL)
247 			errx(1, "ip not found on interface");
248 
249 		if (h_errno == NETDB_SUCCESS) {
250 			if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
251 				err(1, "sethostname");
252 		} else if (h_errno == HOST_NOT_FOUND) {
253 			errx(1,"hostname not found");
254 		} else {
255 			herror("gethostbyaddr");
256 			exit(1);
257 		}
258 	} else if (rflag) {
259 		ret = inet_pton(AF_INET, srflag, &ia);
260 		if (ret != 1) {
261 			/* check IPV6 */
262 			ret = inet_pton(AF_INET6, srflag, &ia6);
263 
264 			if (ret != 1) {
265 				errx(1, "invalid ip address");
266 			}
267 
268 			flag6 = 1;
269 		}
270 
271 		if (flag6 == 1)
272 			hst = gethostbyaddr(&ia6, sizeof(ia6), AF_INET6);
273 		else
274 			hst = gethostbyaddr(&ia, sizeof(ia), AF_INET);
275 		if (!hst) {
276 			if (h_errno == HOST_NOT_FOUND)
277 				errx(1,"host not found\n");
278 		}
279 
280 		if (sethostname(hst->h_name, (int)strlen(hst->h_name)))
281 			err(1, "sethostname");
282 	} else if (*argv) {
283 		if (sethostname(*argv, (int)strlen(*argv)))
284 			err(1, "sethostname");
285 	} else {
286 		if (gethostname(hostname, (int)sizeof(hostname)))
287 			err(1, "gethostname");
288 		if (sflag && (p = strchr(hostname, '.')))
289 			*p = '\0';
290 		printf("%s\n", hostname);
291 	}
292 	exit(0);
293 }
294 
295 static void
296 usage(void)
297 {
298 	fprintf(stderr, "usage: hostname [-s] [name-of-host |"
299 			" -r ip-address | -i interface [-4 | -6]]\n");
300 	exit(1);
301 }
302 
303