tools/llvm: Do not build with symbols
[minix3.git] / minix / commands / hostaddr / hostaddr.c
blob362a2d51278e25a6a81b499ee6bab082d5bd7643
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 <netinet/in.h>
21 #include <net/netlib.h>
22 #include <net/hton.h>
23 #include <net/gen/ether.h>
24 #include <net/gen/eth_io.h>
25 #include <net/gen/if_ether.h>
26 #include <net/gen/in.h>
27 #include <net/gen/inet.h>
28 #include <net/gen/ip_io.h>
29 #include <netdb.h>
30 #include <net/gen/socket.h>
32 #include <arpa/nameser.h>
33 #include <resolv.h>
34 #include <net/gen/dhcp.h>
36 #include <minix/paths.h>
38 char *prog_name;
40 char DHCPCACHE[]=_PATH_DHCPCACHE;
42 int main( int argc, char *argv[] );
43 void usage( void );
45 int main(argc, argv)
46 int argc;
47 char *argv[];
49 int c;
50 int first_print;
51 int a_flag, e_flag, i_flag, h_flag;
52 char *E_arg, *I_arg;
53 int do_ether, do_ip, do_asc_ip, do_hostname;
54 char *eth_device, *ip_device;
55 int eth_fd, ip_fd;
56 int result;
57 nwio_ethstat_t nwio_ethstat;
58 nwio_ipconf_t nwio_ipconf;
59 struct hostent *hostent;
60 char *hostname, *domain;
61 char nodename[2*256];
62 dhcp_t dhcp;
64 first_print= 1;
65 prog_name= argv[0];
67 a_flag= e_flag= h_flag = i_flag= 0;
68 E_arg= I_arg= NULL;
70 while((c= getopt(argc, argv, "?aheE:iI:")) != -1)
72 switch(c)
74 case '?':
75 usage();
76 case 'a':
77 if (a_flag)
78 usage();
79 a_flag= 1;
80 break;
81 case 'h':
82 if (h_flag)
83 usage();
84 h_flag= 1;
85 break;
86 case 'e':
87 if (e_flag)
88 usage();
89 e_flag= 1;
90 break;
91 case 'E':
92 if (E_arg)
93 usage();
94 E_arg= optarg;
95 break;
96 case 'i':
97 if (i_flag)
98 usage();
99 i_flag= 1;
100 break;
101 case 'I':
102 if (I_arg)
103 usage();
104 I_arg= optarg;
105 break;
106 default:
107 usage();
110 if(optind != argc)
111 usage();
113 do_ether= e_flag;
114 if (E_arg)
115 eth_device= E_arg;
116 else
118 eth_device= getenv("ETH_DEVICE");
119 if (!eth_device)
120 eth_device= ETH_DEVICE;
123 do_ip= i_flag;
124 do_asc_ip= a_flag;
125 do_hostname= h_flag;
126 if (I_arg)
127 ip_device= I_arg;
128 else
130 ip_device= getenv("IP_DEVICE");
131 if (!ip_device)
132 ip_device= IP_DEVICE;
135 if (!do_ether && !do_ip && !do_asc_ip && !do_hostname)
136 do_ether= do_ip= do_asc_ip= 1;
138 if (do_ether)
140 eth_fd= open(eth_device, O_RDWR);
141 if (eth_fd == -1)
143 fprintf(stderr, "%s: Unable to open '%s': %s\n",
144 prog_name, eth_device, strerror(errno));
145 exit(1);
147 result= ioctl(eth_fd, NWIOGETHSTAT, &nwio_ethstat);
148 if (result == -1)
150 fprintf(stderr,
151 "%s: Unable to fetch ethernet address: %s\n",
152 prog_name, strerror(errno));
153 exit(1);
155 printf("%s%s", first_print ? "" : " ",
156 ether_ntoa(&nwio_ethstat.nwes_addr));
157 first_print= 0;
159 if (do_ip || do_asc_ip || do_hostname)
161 ip_fd= open(ip_device, O_RDWR);
162 if (ip_fd == -1)
164 fprintf(stderr, "%s: Unable to open '%s': %s\n",
165 prog_name, ip_device, strerror(errno));
166 exit(1);
168 result= ioctl(ip_fd, NWIOGIPCONF, &nwio_ipconf);
169 if (result == -1)
171 fprintf(stderr,
172 "%s: Unable to fetch IP address: %s\n",
173 prog_name,
174 strerror(errno));
175 exit(1);
179 setuid(getuid());
181 if (do_ip)
183 printf("%s%s", first_print ? "" : " ",
184 inet_ntoa(nwio_ipconf.nwic_ipaddr));
185 first_print= 0;
187 if (do_asc_ip || do_hostname)
189 int fd;
190 int r;
191 dhcp_t d;
192 u8_t *data;
193 size_t hlen, dlen;
195 hostname= NULL;
196 domain= NULL;
198 /* Use a reverse DNS lookup to get the host name. This is
199 * the preferred method, but often fails due to lazy admins.
201 hostent= gethostbyaddr((char *)&nwio_ipconf.nwic_ipaddr,
202 sizeof(nwio_ipconf.nwic_ipaddr), AF_INET);
203 if (hostent != NULL) hostname= hostent->h_name;
205 if (hostname != NULL)
207 /* Reverse DNS works. */
209 else if ((fd= open(DHCPCACHE, O_RDONLY)) == -1)
211 if (errno != ENOENT)
213 fprintf(stderr, "%s: %s: %s\n",
214 prog_name, DHCPCACHE, strerror(errno));
215 exit(1);
218 else
220 /* Try to get the hostname from the DHCP data. */
221 while ((r= read(fd, &d, sizeof(d))) == sizeof(d))
223 if (d.yiaddr == nwio_ipconf.nwic_ipaddr) break;
225 if (r < 0)
227 fprintf(stderr, "%s: %s: %s\n",
228 prog_name, DHCPCACHE, strerror(errno));
229 exit(1);
231 close(fd);
233 if (r == sizeof(d))
235 if (dhcp_gettag(&d, DHCP_TAG_HOSTNAME,
236 &data, &hlen))
237 hostname= (char *) data;
239 if (dhcp_gettag(&d, DHCP_TAG_DOMAIN,
240 &data, &dlen))
241 domain= (char *) data;
243 if (hostname != NULL) hostname[hlen] = 0;
244 if (domain != NULL) domain[dlen] = 0;
248 if (hostname != NULL)
250 if (strchr(hostname, '.') != NULL)
252 domain= strchr(hostname, '.');
253 *domain++ = 0;
256 else
258 /* No host name anywhere. Use the IP address. */
259 hostname= inet_ntoa(nwio_ipconf.nwic_ipaddr);
260 domain= NULL;
263 strcpy(nodename, hostname);
264 if (domain != NULL)
266 strcat(nodename, ".");
267 strcat(nodename, domain);
270 if (do_asc_ip)
272 printf("%s%s", first_print ? "" : " ", nodename);
273 first_print= 0;
275 if (do_hostname)
277 #if __minix_vmd
278 if (sysuname(_UTS_SET, _UTS_NODENAME,
279 nodename, strlen(nodename)+1) == -1)
281 fprintf(stderr, "%s: Unable to set nodename: %s\n",
282 prog_name, strerror(errno));
283 exit(1);
286 if (sysuname(_UTS_SET, _UTS_HOSTNAME,
287 hostname, strlen(hostname)+1) == -1)
289 fprintf(stderr, "%s: Unable to set hostname: %s\n",
290 prog_name, strerror(errno));
291 exit(1);
293 #else
294 FILE *fp;
296 if ((fp= fopen("/etc/hostname.file", "w")) == NULL
297 || fprintf(fp, "%s\n", nodename) == EOF
298 || fclose(fp) == EOF)
300 fprintf(stderr, "%s: /etc/hostname.file: %s\n",
301 prog_name, strerror(errno));
302 exit(1);
304 #endif
306 if (!first_print) printf("\n");
307 exit(0);
310 void usage()
312 fprintf(stderr,
313 "Usage: %s -[eiah] [-E <eth-device>] [-I <ip-device>]\n",
314 prog_name);
315 exit(1);