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