pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / simple / hostaddr.c
blob989f3660318ddd07f8891473c4a74e4203a25abf
1 /*
2 hostaddr.c
4 Fetch an ip and/or ethernet address and print it on one line.
6 Created: Jan 27, 1992 by Philip Homburg
7 */
9 #include <sys/types.h>
10 #include <sys/ioctl.h>
11 #include <sys/utsname.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
19 #include <net/netlib.h>
20 #include <net/hton.h>
21 #include <net/gen/ether.h>
22 #include <net/gen/eth_io.h>
23 #include <net/gen/if_ether.h>
24 #include <net/gen/in.h>
25 #include <net/gen/inet.h>
26 #include <net/gen/ip_io.h>
27 #include <net/gen/netdb.h>
28 #include <net/gen/socket.h>
29 #include <net/gen/nameser.h>
30 #include <net/gen/resolv.h>
31 #include <net/gen/dhcp.h>
33 #include <minix/paths.h>
35 char *prog_name;
37 char DHCPCACHE[]=_PATH_DHCPCACHE;
39 void main _ARGS(( int argc, char *argv[] ));
40 void usage _ARGS(( void ));
42 void main(argc, argv)
43 int argc;
44 char *argv[];
46 int c;
47 int first_print;
48 int a_flag, e_flag, i_flag, h_flag;
49 char *E_arg, *I_arg;
50 int do_ether, do_ip, do_asc_ip, do_hostname;
51 char *eth_device, *ip_device;
52 int eth_fd, ip_fd;
53 int result;
54 nwio_ethstat_t nwio_ethstat;
55 nwio_ipconf_t nwio_ipconf;
56 struct hostent *hostent;
57 char *hostname, *domain;
58 char nodename[2*256];
59 dhcp_t dhcp;
61 first_print= 1;
62 prog_name= argv[0];
64 a_flag= e_flag= h_flag = i_flag= 0;
65 E_arg= I_arg= NULL;
67 while((c= getopt(argc, argv, "?aheE:iI:")) != -1)
69 switch(c)
71 case '?':
72 usage();
73 case 'a':
74 if (a_flag)
75 usage();
76 a_flag= 1;
77 break;
78 case 'h':
79 if (h_flag)
80 usage();
81 h_flag= 1;
82 break;
83 case 'e':
84 if (e_flag)
85 usage();
86 e_flag= 1;
87 break;
88 case 'E':
89 if (E_arg)
90 usage();
91 E_arg= optarg;
92 break;
93 case 'i':
94 if (i_flag)
95 usage();
96 i_flag= 1;
97 break;
98 case 'I':
99 if (I_arg)
100 usage();
101 I_arg= optarg;
102 break;
103 default:
104 usage();
107 if(optind != argc)
108 usage();
110 do_ether= e_flag;
111 if (E_arg)
112 eth_device= E_arg;
113 else
115 eth_device= getenv("ETH_DEVICE");
116 if (!eth_device)
117 eth_device= ETH_DEVICE;
120 do_ip= i_flag;
121 do_asc_ip= a_flag;
122 do_hostname= h_flag;
123 if (I_arg)
124 ip_device= I_arg;
125 else
127 ip_device= getenv("IP_DEVICE");
128 if (!ip_device)
129 ip_device= IP_DEVICE;
132 if (!do_ether && !do_ip && !do_asc_ip && !do_hostname)
133 do_ether= do_ip= do_asc_ip= 1;
135 if (do_ether)
137 eth_fd= open(eth_device, O_RDWR);
138 if (eth_fd == -1)
140 fprintf(stderr, "%s: Unable to open '%s': %s\n",
141 prog_name, eth_device, strerror(errno));
142 exit(1);
144 result= ioctl(eth_fd, NWIOGETHSTAT, &nwio_ethstat);
145 if (result == -1)
147 fprintf(stderr,
148 "%s: Unable to fetch ethernet address: %s\n",
149 prog_name, strerror(errno));
150 exit(1);
152 printf("%s%s", first_print ? "" : " ",
153 ether_ntoa(&nwio_ethstat.nwes_addr));
154 first_print= 0;
156 if (do_ip || do_asc_ip || do_hostname)
158 ip_fd= open(ip_device, O_RDWR);
159 if (ip_fd == -1)
161 fprintf(stderr, "%s: Unable to open '%s': %s\n",
162 prog_name, ip_device, strerror(errno));
163 exit(1);
165 result= ioctl(ip_fd, NWIOGIPCONF, &nwio_ipconf);
166 if (result == -1)
168 fprintf(stderr,
169 "%s: Unable to fetch IP address: %s\n",
170 prog_name,
171 strerror(errno));
172 exit(1);
176 setuid(getuid());
178 if (do_ip)
180 printf("%s%s", first_print ? "" : " ",
181 inet_ntoa(nwio_ipconf.nwic_ipaddr));
182 first_print= 0;
184 if (do_asc_ip || do_hostname)
186 int fd;
187 int r;
188 dhcp_t d;
189 u8_t *data;
190 size_t hlen, dlen;
192 hostname= NULL;
193 domain= NULL;
195 /* Use a reverse DNS lookup to get the host name. This is
196 * the preferred method, but often fails due to lazy admins.
198 hostent= gethostbyaddr((char *)&nwio_ipconf.nwic_ipaddr,
199 sizeof(nwio_ipconf.nwic_ipaddr), AF_INET);
200 if (hostent != NULL) hostname= hostent->h_name;
202 if (hostname != NULL)
204 /* Reverse DNS works. */
206 else if ((fd= open(DHCPCACHE, O_RDONLY)) == -1)
208 if (errno != ENOENT)
210 fprintf(stderr, "%s: %s: %s\n",
211 prog_name, DHCPCACHE, strerror(errno));
212 exit(1);
215 else
217 /* Try to get the hostname from the DHCP data. */
218 while ((r= read(fd, &d, sizeof(d))) == sizeof(d))
220 if (d.yiaddr == nwio_ipconf.nwic_ipaddr) break;
222 if (r < 0)
224 fprintf(stderr, "%s: %s: %s\n",
225 prog_name, DHCPCACHE, strerror(errno));
226 exit(1);
228 close(fd);
230 if (r == sizeof(d))
232 if (dhcp_gettag(&d, DHCP_TAG_HOSTNAME,
233 &data, &hlen))
234 hostname= (char *) data;
236 if (dhcp_gettag(&d, DHCP_TAG_DOMAIN,
237 &data, &dlen))
238 domain= (char *) data;
240 if (hostname != NULL) hostname[hlen] = 0;
241 if (domain != NULL) domain[dlen] = 0;
245 if (hostname != NULL)
247 if (strchr(hostname, '.') != NULL)
249 domain= strchr(hostname, '.');
250 *domain++ = 0;
253 else
255 /* No host name anywhere. Use the IP address. */
256 hostname= inet_ntoa(nwio_ipconf.nwic_ipaddr);
257 domain= NULL;
260 strcpy(nodename, hostname);
261 if (domain != NULL)
263 strcat(nodename, ".");
264 strcat(nodename, domain);
267 if (do_asc_ip)
269 printf("%s%s", first_print ? "" : " ", nodename);
270 first_print= 0;
272 if (do_hostname)
274 #if __minix_vmd
275 if (sysuname(_UTS_SET, _UTS_NODENAME,
276 nodename, strlen(nodename)+1) == -1)
278 fprintf(stderr, "%s: Unable to set nodename: %s\n",
279 prog_name, strerror(errno));
280 exit(1);
283 if (sysuname(_UTS_SET, _UTS_HOSTNAME,
284 hostname, strlen(hostname)+1) == -1)
286 fprintf(stderr, "%s: Unable to set hostname: %s\n",
287 prog_name, strerror(errno));
288 exit(1);
290 #else
291 FILE *fp;
293 if ((fp= fopen("/etc/hostname.file", "w")) == NULL
294 || fprintf(fp, "%s\n", nodename) == EOF
295 || fclose(fp) == EOF)
297 fprintf(stderr, "%s: /etc/hostname.file: %s\n",
298 prog_name, strerror(errno));
299 exit(1);
301 #endif
303 if (!first_print) printf("\n");
304 exit(0);
307 void usage()
309 fprintf(stderr,
310 "Usage: %s -[eiah] [-E <eth-device>] [-I <ip-device>]\n",
311 prog_name);
312 exit(1);