source: trunk/minix/commands/simple/hostaddr.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 5.8 KB
Line 
1/*
2hostaddr.c
3
4Fetch an ip and/or ethernet address and print it on one line.
5
6Created: Jan 27, 1992 by Philip Homburg
7*/
8
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>
18
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>
32
33#include <minix/paths.h>
34
35char *prog_name;
36
37char DHCPCACHE[]=_PATH_DHCPCACHE;
38
39void main _ARGS(( int argc, char *argv[] ));
40void usage _ARGS(( void ));
41
42void main(argc, argv)
43int argc;
44char *argv[];
45{
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;
60
61 first_print= 1;
62 prog_name= argv[0];
63
64 a_flag= e_flag= h_flag = i_flag= 0;
65 E_arg= I_arg= NULL;
66
67 while((c= getopt(argc, argv, "?aheE:iI:")) != -1)
68 {
69 switch(c)
70 {
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();
105 }
106 }
107 if(optind != argc)
108 usage();
109
110 do_ether= e_flag;
111 if (E_arg)
112 eth_device= E_arg;
113 else
114 {
115 eth_device= getenv("ETH_DEVICE");
116 if (!eth_device)
117 eth_device= ETH_DEVICE;
118 }
119
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
126 {
127 ip_device= getenv("IP_DEVICE");
128 if (!ip_device)
129 ip_device= IP_DEVICE;
130 }
131
132 if (!do_ether && !do_ip && !do_asc_ip && !do_hostname)
133 do_ether= do_ip= do_asc_ip= 1;
134
135 if (do_ether)
136 {
137 eth_fd= open(eth_device, O_RDWR);
138 if (eth_fd == -1)
139 {
140 fprintf(stderr, "%s: Unable to open '%s': %s\n",
141 prog_name, eth_device, strerror(errno));
142 exit(1);
143 }
144 result= ioctl(eth_fd, NWIOGETHSTAT, &nwio_ethstat);
145 if (result == -1)
146 {
147 fprintf(stderr,
148 "%s: Unable to fetch ethernet address: %s\n",
149 prog_name, strerror(errno));
150 exit(1);
151 }
152 printf("%s%s", first_print ? "" : " ",
153 ether_ntoa(&nwio_ethstat.nwes_addr));
154 first_print= 0;
155 }
156 if (do_ip || do_asc_ip || do_hostname)
157 {
158 ip_fd= open(ip_device, O_RDWR);
159 if (ip_fd == -1)
160 {
161 fprintf(stderr, "%s: Unable to open '%s': %s\n",
162 prog_name, ip_device, strerror(errno));
163 exit(1);
164 }
165 result= ioctl(ip_fd, NWIOGIPCONF, &nwio_ipconf);
166 if (result == -1)
167 {
168 fprintf(stderr,
169 "%s: Unable to fetch IP address: %s\n",
170 prog_name,
171 strerror(errno));
172 exit(1);
173 }
174 }
175
176 setuid(getuid());
177
178 if (do_ip)
179 {
180 printf("%s%s", first_print ? "" : " ",
181 inet_ntoa(nwio_ipconf.nwic_ipaddr));
182 first_print= 0;
183 }
184 if (do_asc_ip || do_hostname)
185 {
186 int fd;
187 int r;
188 dhcp_t d;
189 u8_t *data;
190 size_t hlen, dlen;
191
192 hostname= NULL;
193 domain= NULL;
194
195 /* Use a reverse DNS lookup to get the host name. This is
196 * the preferred method, but often fails due to lazy admins.
197 */
198 hostent= gethostbyaddr((char *)&nwio_ipconf.nwic_ipaddr,
199 sizeof(nwio_ipconf.nwic_ipaddr), AF_INET);
200 if (hostent != NULL) hostname= hostent->h_name;
201
202 if (hostname != NULL)
203 {
204 /* Reverse DNS works. */
205 }
206 else if ((fd= open(DHCPCACHE, O_RDONLY)) == -1)
207 {
208 if (errno != ENOENT)
209 {
210 fprintf(stderr, "%s: %s: %s\n",
211 prog_name, DHCPCACHE, strerror(errno));
212 exit(1);
213 }
214 }
215 else
216 {
217 /* Try to get the hostname from the DHCP data. */
218 while ((r= read(fd, &d, sizeof(d))) == sizeof(d))
219 {
220 if (d.yiaddr == nwio_ipconf.nwic_ipaddr) break;
221 }
222 if (r < 0)
223 {
224 fprintf(stderr, "%s: %s: %s\n",
225 prog_name, DHCPCACHE, strerror(errno));
226 exit(1);
227 }
228 close(fd);
229
230 if (r == sizeof(d))
231 {
232 if (dhcp_gettag(&d, DHCP_TAG_HOSTNAME,
233 &data, &hlen))
234 hostname= (char *) data;
235
236 if (dhcp_gettag(&d, DHCP_TAG_DOMAIN,
237 &data, &dlen))
238 domain= (char *) data;
239
240 if (hostname != NULL) hostname[hlen] = 0;
241 if (domain != NULL) domain[dlen] = 0;
242 }
243 }
244
245 if (hostname != NULL)
246 {
247 if (strchr(hostname, '.') != NULL)
248 {
249 domain= strchr(hostname, '.');
250 *domain++ = 0;
251 }
252 }
253 else
254 {
255 /* No host name anywhere. Use the IP address. */
256 hostname= inet_ntoa(nwio_ipconf.nwic_ipaddr);
257 domain= NULL;
258 }
259
260 strcpy(nodename, hostname);
261 if (domain != NULL)
262 {
263 strcat(nodename, ".");
264 strcat(nodename, domain);
265 }
266 }
267 if (do_asc_ip)
268 {
269 printf("%s%s", first_print ? "" : " ", nodename);
270 first_print= 0;
271 }
272 if (do_hostname)
273 {
274#if __minix_vmd
275 if (sysuname(_UTS_SET, _UTS_NODENAME,
276 nodename, strlen(nodename)+1) == -1)
277 {
278 fprintf(stderr, "%s: Unable to set nodename: %s\n",
279 prog_name, strerror(errno));
280 exit(1);
281 }
282
283 if (sysuname(_UTS_SET, _UTS_HOSTNAME,
284 hostname, strlen(hostname)+1) == -1)
285 {
286 fprintf(stderr, "%s: Unable to set hostname: %s\n",
287 prog_name, strerror(errno));
288 exit(1);
289 }
290#else
291 FILE *fp;
292
293 if ((fp= fopen("/etc/hostname.file", "w")) == NULL
294 || fprintf(fp, "%s\n", nodename) == EOF
295 || fclose(fp) == EOF)
296 {
297 fprintf(stderr, "%s: /etc/hostname.file: %s\n",
298 prog_name, strerror(errno));
299 exit(1);
300 }
301#endif
302 }
303 if (!first_print) printf("\n");
304 exit(0);
305}
306
307void usage()
308{
309 fprintf(stderr,
310 "Usage: %s -[eiah] [-E <eth-device>] [-I <ip-device>]\n",
311 prog_name);
312 exit(1);
313}
Note: See TracBrowser for help on using the repository browser.