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 <netinet/in.h>
21 #include <net/netlib.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>
30 #include <net/gen/socket.h>
32 #include <arpa/nameser.h>
34 #include <net/gen/dhcp.h>
36 #include <minix/paths.h>
40 char DHCPCACHE
[]=_PATH_DHCPCACHE
;
42 int main( int argc
, char *argv
[] );
51 int a_flag
, e_flag
, i_flag
, h_flag
;
53 int do_ether
, do_ip
, do_asc_ip
, do_hostname
;
54 char *eth_device
, *ip_device
;
57 nwio_ethstat_t nwio_ethstat
;
58 nwio_ipconf_t nwio_ipconf
;
59 struct hostent
*hostent
;
60 char *hostname
, *domain
;
67 a_flag
= e_flag
= h_flag
= i_flag
= 0;
70 while((c
= getopt(argc
, argv
, "?aheE:iI:")) != -1)
118 eth_device
= getenv("ETH_DEVICE");
120 eth_device
= ETH_DEVICE
;
130 ip_device
= getenv("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;
140 eth_fd
= open(eth_device
, O_RDWR
);
143 fprintf(stderr
, "%s: Unable to open '%s': %s\n",
144 prog_name
, eth_device
, strerror(errno
));
147 result
= ioctl(eth_fd
, NWIOGETHSTAT
, &nwio_ethstat
);
151 "%s: Unable to fetch ethernet address: %s\n",
152 prog_name
, strerror(errno
));
155 printf("%s%s", first_print
? "" : " ",
156 ether_ntoa(&nwio_ethstat
.nwes_addr
));
159 if (do_ip
|| do_asc_ip
|| do_hostname
)
161 ip_fd
= open(ip_device
, O_RDWR
);
164 fprintf(stderr
, "%s: Unable to open '%s': %s\n",
165 prog_name
, ip_device
, strerror(errno
));
168 result
= ioctl(ip_fd
, NWIOGIPCONF
, &nwio_ipconf
);
172 "%s: Unable to fetch IP address: %s\n",
183 printf("%s%s", first_print
? "" : " ",
184 inet_ntoa(nwio_ipconf
.nwic_ipaddr
));
187 if (do_asc_ip
|| do_hostname
)
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)
213 fprintf(stderr
, "%s: %s: %s\n",
214 prog_name
, DHCPCACHE
, strerror(errno
));
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;
227 fprintf(stderr
, "%s: %s: %s\n",
228 prog_name
, DHCPCACHE
, strerror(errno
));
235 if (dhcp_gettag(&d
, DHCP_TAG_HOSTNAME
,
237 hostname
= (char *) data
;
239 if (dhcp_gettag(&d
, DHCP_TAG_DOMAIN
,
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
, '.');
258 /* No host name anywhere. Use the IP address. */
259 hostname
= inet_ntoa(nwio_ipconf
.nwic_ipaddr
);
263 strcpy(nodename
, hostname
);
266 strcat(nodename
, ".");
267 strcat(nodename
, domain
);
272 printf("%s%s", first_print
? "" : " ", nodename
);
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
));
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
));
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
));
306 if (!first_print
) printf("\n");
313 "Usage: %s -[eiah] [-E <eth-device>] [-I <ip-device>]\n",