4 Fetch an ip and/or ethernet address and print it on one line.
6 Created: Jan 27, 1992 by Philip Homburg
10 #include <sys/ioctl.h>
11 #include <sys/utsname.h>
19 #include <net/netlib.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>
37 char DHCPCACHE
[]=_PATH_DHCPCACHE
;
39 void main
_ARGS(( int argc
, char *argv
[] ));
40 void usage
_ARGS(( void ));
48 int a_flag
, e_flag
, i_flag
, h_flag
;
50 int do_ether
, do_ip
, do_asc_ip
, do_hostname
;
51 char *eth_device
, *ip_device
;
54 nwio_ethstat_t nwio_ethstat
;
55 nwio_ipconf_t nwio_ipconf
;
56 struct hostent
*hostent
;
57 char *hostname
, *domain
;
64 a_flag
= e_flag
= h_flag
= i_flag
= 0;
67 while((c
= getopt(argc
, argv
, "?aheE:iI:")) != -1)
115 eth_device
= getenv("ETH_DEVICE");
117 eth_device
= ETH_DEVICE
;
127 ip_device
= getenv("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;
137 eth_fd
= open(eth_device
, O_RDWR
);
140 fprintf(stderr
, "%s: Unable to open '%s': %s\n",
141 prog_name
, eth_device
, strerror(errno
));
144 result
= ioctl(eth_fd
, NWIOGETHSTAT
, &nwio_ethstat
);
148 "%s: Unable to fetch ethernet address: %s\n",
149 prog_name
, strerror(errno
));
152 printf("%s%s", first_print
? "" : " ",
153 ether_ntoa(&nwio_ethstat
.nwes_addr
));
156 if (do_ip
|| do_asc_ip
|| do_hostname
)
158 ip_fd
= open(ip_device
, O_RDWR
);
161 fprintf(stderr
, "%s: Unable to open '%s': %s\n",
162 prog_name
, ip_device
, strerror(errno
));
165 result
= ioctl(ip_fd
, NWIOGIPCONF
, &nwio_ipconf
);
169 "%s: Unable to fetch IP address: %s\n",
180 printf("%s%s", first_print
? "" : " ",
181 inet_ntoa(nwio_ipconf
.nwic_ipaddr
));
184 if (do_asc_ip
|| do_hostname
)
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)
210 fprintf(stderr
, "%s: %s: %s\n",
211 prog_name
, DHCPCACHE
, strerror(errno
));
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;
224 fprintf(stderr
, "%s: %s: %s\n",
225 prog_name
, DHCPCACHE
, strerror(errno
));
232 if (dhcp_gettag(&d
, DHCP_TAG_HOSTNAME
,
234 hostname
= (char *) data
;
236 if (dhcp_gettag(&d
, DHCP_TAG_DOMAIN
,
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
, '.');
255 /* No host name anywhere. Use the IP address. */
256 hostname
= inet_ntoa(nwio_ipconf
.nwic_ipaddr
);
260 strcpy(nodename
, hostname
);
263 strcat(nodename
, ".");
264 strcat(nodename
, domain
);
269 printf("%s%s", first_print
? "" : " ", nodename
);
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
));
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
));
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
));
303 if (!first_print
) printf("\n");
310 "Usage: %s -[eiah] [-E <eth-device>] [-I <ip-device>]\n",