dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.sbin / hostconfig.c
blob44f98700407afd0e95db9bbe31af50d9619d68bd
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <locale.h>
32 #include <sys/utsname.h>
33 #include <sys/systeminfo.h>
34 #include <netdb.h>
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <sys/file.h>
39 #include <sys/ioctl.h>
40 #include <sys/signal.h>
41 #include <sys/wait.h>
42 #include <sys/time.h>
43 #include <sys/socket.h>
44 #include <sys/stropts.h>
45 #include <sys/resource.h>
46 #include <net/if.h>
47 #include <net/if_arp.h>
48 #include <sys/stream.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
52 #include <netinet/if_ether.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/udp.h>
55 #include <netinet/udp_var.h>
56 #include <rpc/rpc.h>
57 #include <rpcsvc/bootparam_prot.h>
59 #define MAXIFS 256
61 /* command line flags */
62 int debug = 0; /* do debug printfs */
63 int echo_host = 0; /* just echo hostname, don't set it */
64 int verbose = 0; /* do verbose printfs */
65 int safe = 0; /* don't change anything */
66 int multiple = 0; /* take multiple replies */
68 static ulong_t if_netmask;
70 void notsupported(), usage(), bp_whoami();
71 int get_ifdata(); /* get IP addr, subnet mask from IF */
72 extern char *inet_ntoa();
73 extern int getopt(), setdomainname();
75 struct prototab {
76 char *name;
77 void (*func)();
78 } prototab[] = {
79 { "bootparams", bp_whoami },
80 { "bootp", notsupported },
81 { 0, 0 }
87 * usage: hostconfig [-p <protocol>] [-v] [-n] [-h] [<ifname>] [-f <hostname>]
89 * options:
90 * -d Debug mode.
91 * -v Verbose mode.
92 * -n Don't change anything.
93 * -h Don't set hostname, just echo to standard out.
94 * -m Wait for multiple answers (best used with the "-n"
95 * and "-v" flags).
96 * -f <hostname> Fake mode - get bootparams for <hostname> (also
97 * best used with the "-n" and "-v" flags).
98 * <ifname> Use IP address of <interface> in whoami request.
100 * If no interface name is specified, bp_whoami will cycle through the
101 * interfaces, using the IP address of each in turn until an answer is
102 * received. Note that rpc_broadcast() broadcasts the RPC call on all
103 * interfaces, so the <ifname> argument doesn't restrict the request
104 * to that interface, it just uses that interface to determine the IP
105 * address to put into the request. If "-f <hostname>" is specified,
106 * we put the IP address of <hostname> in the whoami request. Otherwise,
107 * we put the IP address of the interface in the whoami request.
113 main(argc, argv)
114 int argc;
115 char **argv;
117 struct ifreq *reqbuf;
118 struct ifreq *ifr;
119 struct ifconf ifc;
120 struct in_addr targetaddr;
121 struct hostent *hp;
122 char *targethost = NULL;
123 char *cmdname;
124 int c;
125 int n;
126 struct prototab *ptp;
127 void (*protofunc)() = NULL;
128 int numifs;
129 unsigned bufsize;
131 extern char *optarg;
132 extern int optind;
134 cmdname = argv[0];
136 while ((c = getopt(argc, argv, "dhvnmf:p:")) != -1) {
138 switch ((char)c) {
139 case 'd':
140 debug++;
141 break;
143 case 'h':
144 echo_host++;
145 break;
146 case 'v':
147 verbose++;
148 break;
150 case 'm':
151 multiple++;
152 break;
154 case 'n':
155 safe++;
156 break;
158 case 'f':
159 targethost = optarg;
160 break;
162 case 'p':
163 protofunc = NULL;
164 for (ptp = &prototab[0]; ptp->func; ptp++)
165 if (strcmp(optarg, ptp->name) == 0) {
166 protofunc = ptp->func;
167 break;
169 if (protofunc == NULL)
170 usage(cmdname);
171 break;
173 case '?':
174 usage(cmdname);
178 if (protofunc == NULL)
179 usage(cmdname);
181 if (targethost) {
182 /* we are faking it */
183 if (debug)
184 fprintf(stdout, "targethost = %s\n", targethost);
186 if ((hp = gethostbyname(targethost)) == NULL) {
187 if ((targetaddr.s_addr = inet_addr(targethost)) ==
188 (ulong_t)(-1)) {
189 (void) fprintf(stderr,
190 "%s: cannot get IP address for %s\n",
191 cmdname, targethost);
192 return (1);
194 } else {
195 if (hp->h_length != sizeof (targetaddr)) {
196 (void) fprintf(stderr,
197 "%s: cannot find host entry for %s\n",
198 cmdname, targethost);
199 return (1);
200 } else
201 (void) memcpy(&targetaddr.s_addr, hp->h_addr,
202 sizeof (targetaddr));
204 } else
205 targetaddr.s_addr = 0;
207 if (optind < argc) {
208 /* interface names were specified */
209 for (; optind < argc; optind++) {
210 if (debug)
211 fprintf(stdout, "Trying arg %s\n",
212 argv[optind]);
213 (*protofunc)(argv[optind], targetaddr);
215 } else {
216 /* no interface names specified - try them all */
217 int ifcount = 0; /* count of useable interfaces */
218 int s;
220 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
221 perror("socket");
222 return (1);
224 #ifdef SIOCGIFNUM
225 if (ioctl(s, SIOCGIFNUM, (char *)&numifs) < 0) {
226 numifs = MAXIFS;
228 #else
229 numifs = MAXIFS;
230 #endif
231 bufsize = numifs * sizeof (struct ifreq);
232 reqbuf = (struct ifreq *)malloc(bufsize);
233 if (reqbuf == NULL) {
234 fprintf(stderr, "out of memory\n");
235 return (1);
237 ifc.ifc_buf = (caddr_t)&reqbuf[0];
238 ifc.ifc_len = bufsize;
239 if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
240 perror("ioctl(SIOCGIFCONF)");
241 return (1);
243 ifr = ifc.ifc_req;
244 n = ifc.ifc_len/sizeof (struct ifreq);
245 for (; n > 0; n--, ifr++) {
246 if (ioctl(s, SIOCGIFFLAGS, (char *)ifr) < 0) {
247 perror("ioctl(SIOCGIFFLAGS)");
248 return (1);
250 if ((ifr->ifr_flags & IFF_LOOPBACK) ||
251 !(ifr->ifr_flags & IFF_BROADCAST) ||
252 !(ifr->ifr_flags & IFF_UP) ||
253 (ifr->ifr_flags & IFF_NOARP) ||
254 (ifr->ifr_flags & IFF_POINTOPOINT)) {
255 if (debug)
256 fprintf(stdout, "If %s not suitable\n",
257 ifr->ifr_name);
258 continue;
259 } else {
260 if (debug)
261 fprintf(stdout, "Trying device %s\n",
262 ifr->ifr_name);
263 (*protofunc)(ifr->ifr_name, targetaddr);
264 ifcount++;
267 if (verbose && ifcount == 0) {
268 fprintf(stderr, "No useable interfaces found.\n");
269 return (1);
271 (void) close(s);
272 (void) free((char *)reqbuf);
274 return (0);
278 void
279 add_default_route(router_addr)
280 struct in_addr router_addr;
282 struct rtentry route;
283 struct sockaddr_in *sin;
284 int s;
286 (void) memset(&route, 0, sizeof (route));
288 /* route destination is "default" - zero */
289 /* LINTED - alignment OK (32bit) */
290 sin = (struct sockaddr_in *)&route.rt_dst;
291 sin->sin_family = AF_INET;
293 /* LINTED - alignment OK (32bit) */
294 sin = (struct sockaddr_in *)&route.rt_gateway;
295 sin->sin_family = AF_INET;
296 sin->sin_addr.s_addr = router_addr.s_addr;
298 route.rt_flags = RTF_GATEWAY | RTF_UP;
300 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
301 perror("socket");
302 return;
304 if (ioctl(s, SIOCADDRT, (char *)&route) == -1) {
305 perror("add default route");
306 return;
308 (void) close(s);
313 bpanswer(res, nb)
314 struct bp_whoami_res *res;
315 struct netbuf *nb;
317 struct in_addr router_addr;
318 static int set;
319 int len;
320 char errbuf[MAX_MACHINE_NAME + 28];
321 /* MAX_MACHINE_NAME + strlen ("sysinfo(SI_SET_HOSTNAME)()") + null */
323 (void) memcpy(&router_addr, &res->router_address.bp_address_u.ip_addr,
324 sizeof (router_addr));
326 if (verbose) {
327 struct sockaddr_in *addr;
329 if (nb) {
330 /* LINTED - alignment (32bit) OK */
331 addr = (struct sockaddr_in *)nb->buf;
332 fprintf(stdout, "From [%s]: ",
333 inet_ntoa(addr->sin_addr));
334 } else {
335 fprintf(stdout, "Reply:\\t\\t");
337 fprintf(stdout, "hostname = %s\n", res->client_name);
338 fprintf(stdout, "\t\typdomain = %s\n", res->domain_name);
339 fprintf(stdout, "\t\trouter = %s\n", inet_ntoa(router_addr));
342 if (!safe && !set) {
344 * Stuff the values from the RPC reply into the kernel.
345 * Only allow one pass through this code; There's no reason
346 * why all replies should tweak the kernel.
348 set++;
350 len = strlen(res->client_name);
351 if (len != 0) {
352 if (!echo_host) {
353 if (sysinfo(SI_SET_HOSTNAME, res->client_name,
354 len) < 0) {
355 (void) snprintf(errbuf, sizeof (errbuf),
356 "sysinfo(SI_SET_HOSTNAME)(%s)",
357 res->client_name);
358 perror(errbuf);
360 } else
361 (void) fprintf(stdout, "%s\n",
362 res->client_name);
365 len = strlen(res->domain_name);
366 if (len != 0) {
367 if (setdomainname(res->domain_name, len) == -1) {
368 (void) snprintf(errbuf, sizeof (errbuf),
369 "setdomainname(%s)", res->domain_name);
370 perror(errbuf);
374 /* we really should validate this router value */
375 if (router_addr.s_addr != 0)
376 add_default_route(router_addr);
379 if (multiple)
380 return (0);
382 /* our job is done */
383 exit(0);
384 /* NOTREACHED */
387 void
388 bp_whoami(device, addr)
389 char *device;
390 struct in_addr addr;
392 struct bp_whoami_arg req;
393 struct bp_whoami_res res;
394 struct in_addr lookupaddr;
395 enum clnt_stat stat;
396 int val = 1;
398 if (debug)
399 fprintf(stdout, "bp_whoami on interface %s addr %s\n", device,
400 inet_ntoa(addr));
402 if (addr.s_addr == 0) {
403 if (get_ifdata(device, &lookupaddr, &if_netmask) == -1)
404 exit(1);
405 } else
406 (void) memcpy(&lookupaddr, &addr, sizeof (addr));
408 lookupaddr.s_addr = ntohl(lookupaddr.s_addr);
410 if (debug)
411 fprintf(stdout, "lookup address is %s\n",
412 inet_ntoa(lookupaddr));
414 (void) memset(&req, 0, sizeof (req));
415 (void) memset(&res, 0, sizeof (res));
417 req.client_address.address_type = IP_ADDR_TYPE;
418 (void) memcpy(&req.client_address.bp_address_u.ip_addr, &lookupaddr,
419 sizeof (lookupaddr));
422 * Broadcast using portmap version number 2 ONLY to
423 * prevent broadcast storm
426 (void) __rpc_control(CLCR_SET_LOWVERS, &val);
428 stat = rpc_broadcast(BOOTPARAMPROG, BOOTPARAMVERS, BOOTPARAMPROC_WHOAMI,
429 xdr_bp_whoami_arg, (caddr_t)&req, xdr_bp_whoami_res, (caddr_t)&res,
430 (resultproc_t)bpanswer, "udp");
432 /* Now try version 3 as well */
434 val = 0;
435 (void) __rpc_control(CLCR_SET_LOWVERS, &val);
437 stat = rpc_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
438 BOOTPARAMPROC_WHOAMI, xdr_bp_whoami_arg, (caddr_t)&req,
439 xdr_bp_whoami_res, (caddr_t)&res, (resultproc_t)bpanswer, "udp");
441 if (stat != RPC_SUCCESS) {
442 clnt_perrno(stat);
443 exit(1);
449 * Get IP address of an interface. As long as we are looking, get the
450 * netmask as well.
453 get_ifdata(dev, ipp, maskp)
454 char *dev;
455 ulong_t *ipp, *maskp;
457 struct ifreq ifr;
458 /* LINTED - alignment OK (32bit) */
459 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
460 int s;
462 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
463 perror("socket");
464 return (-1);
467 if (strlcpy(ifr.ifr_name, dev, sizeof (ifr.ifr_name)) >=
468 sizeof (ifr.ifr_name)) {
469 (void) fprintf(stderr, "Device name too long %s\n",
470 dev);
471 return (-1);
474 if (ipp) {
475 if (ioctl(s, SIOCGIFADDR, (caddr_t)&ifr) < 0) {
476 perror("ioctl(SIOCGIFADDR)");
477 return (-1);
479 *ipp = ntohl(sin->sin_addr.s_addr);
481 if (debug)
482 (void) fprintf(stderr, "Interface '%s' address %s\n",
483 dev, inet_ntoa(sin->sin_addr));
486 if (maskp) {
487 if (ioctl(s, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
488 perror("SIOCGIFNETMASK");
489 return (-1);
491 *maskp = ntohl(sin->sin_addr.s_addr);
493 if (debug)
494 (void) fprintf(stderr,
495 "Interface '%s' subnet mask %s\n", dev,
496 inet_ntoa(sin->sin_addr));
499 (void) close(s);
500 return (0);
503 void
504 notsupported()
506 fprintf(stderr, "requested protocol is not supported\n");
507 exit(1);
510 void
511 usage(cmdname)
512 char *cmdname;
514 (void) fprintf(stderr, "usage: %s [-v] [-n] [-m] [-h] [<ifname>] "
515 "[-f <hostname>] -p bootparams|bootp\n", cmdname);
516 (void) fflush(stderr);
517 exit(1);