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>
20 #include <netinet/in.h>
23 #include <net/netlib.h>
25 #include <net/gen/ether.h>
26 #include <net/gen/eth_io.h>
27 #include <net/gen/if_ether.h>
28 #include <net/gen/in.h>
29 #include <net/gen/inet.h>
30 #include <net/gen/ip_io.h>
31 #include <net/gen/netdb.h>
32 #include <net/gen/socket.h>
33 #include <net/gen/nameser.h>
34 #include <net/gen/resolv.h>
35 #include <net/gen/dhcp.h>
41 char DHCPCACHE
[]=_PATH_DHCPCACHE
;
43 int main( int argc
, char *argv
[] );
52 int a_flag
, e_flag
, i_flag
, h_flag
;
54 int do_ether
, do_ip
, do_asc_ip
, do_hostname
;
55 char *eth_device
, *ip_device
;
58 nwio_ethstat_t nwio_ethstat
;
59 nwio_ipconf_t nwio_ipconf
;
60 struct hostent
*hostent
;
61 char *hostname
, *domain
;
68 a_flag
= e_flag
= h_flag
= i_flag
= 0;
71 while((c
= getopt(argc
, argv
, "?aheE:iI:")) != -1)
119 eth_device
= getenv("ETH_DEVICE");
121 eth_device
= ETH_DEVICE
;
131 ip_device
= getenv("IP_DEVICE");
133 ip_device
= IP_DEVICE
;
136 if (!do_ether
&& !do_ip
&& !do_asc_ip
&& !do_hostname
)
137 do_ether
= do_ip
= do_asc_ip
= 1;
141 eth_fd
= open(eth_device
, O_RDWR
);
144 fprintf(stderr
, "%s: Unable to open '%s': %s\n",
145 prog_name
, eth_device
, strerror(errno
));
148 result
= ioctl(eth_fd
, NWIOGETHSTAT
, &nwio_ethstat
);
152 "%s: Unable to fetch ethernet address: %s\n",
153 prog_name
, strerror(errno
));
156 printf("%s%s", first_print
? "" : " ",
157 ether_ntoa(&nwio_ethstat
.nwes_addr
));
160 if (do_ip
|| do_asc_ip
|| do_hostname
)
162 ip_fd
= open(ip_device
, O_RDWR
);
165 fprintf(stderr
, "%s: Unable to open '%s': %s\n",
166 prog_name
, ip_device
, strerror(errno
));
169 result
= ioctl(ip_fd
, NWIOGIPCONF
, &nwio_ipconf
);
173 "%s: Unable to fetch IP address: %s\n",
184 printf("%s%s", first_print
? "" : " ",
185 inet_ntoa(nwio_ipconf
.nwic_ipaddr
));
188 if (do_asc_ip
|| do_hostname
)
199 /* Use a reverse DNS lookup to get the host name. This is
200 * the preferred method, but often fails due to lazy admins.
202 hostent
= gethostbyaddr((char *)&nwio_ipconf
.nwic_ipaddr
,
203 sizeof(nwio_ipconf
.nwic_ipaddr
), AF_INET
);
204 if (hostent
!= NULL
) hostname
= hostent
->h_name
;
206 if (hostname
!= NULL
)
208 /* Reverse DNS works. */
210 else if ((fd
= open(DHCPCACHE
, O_RDONLY
)) == -1)
214 fprintf(stderr
, "%s: %s: %s\n",
215 prog_name
, DHCPCACHE
, strerror(errno
));
221 /* Try to get the hostname from the DHCP data. */
222 while ((r
= read(fd
, &d
, sizeof(d
))) == sizeof(d
))
224 if (d
.yiaddr
== nwio_ipconf
.nwic_ipaddr
) break;
228 fprintf(stderr
, "%s: %s: %s\n",
229 prog_name
, DHCPCACHE
, strerror(errno
));
236 if (dhcp_gettag(&d
, DHCP_TAG_HOSTNAME
,
238 hostname
= (char *) data
;
240 if (dhcp_gettag(&d
, DHCP_TAG_DOMAIN
,
242 domain
= (char *) data
;
244 if (hostname
!= NULL
) hostname
[hlen
] = 0;
245 if (domain
!= NULL
) domain
[dlen
] = 0;
249 if (hostname
!= NULL
)
251 if (strchr(hostname
, '.') != NULL
)
253 domain
= strchr(hostname
, '.');
259 /* No host name anywhere. Use the IP address. */
260 hostname
= inet_ntoa(nwio_ipconf
.nwic_ipaddr
);
264 strcpy(nodename
, hostname
);
267 strcat(nodename
, ".");
268 strcat(nodename
, domain
);
273 printf("%s%s", first_print
? "" : " ", nodename
);
279 if (sysuname(_UTS_SET
, _UTS_NODENAME
,
280 nodename
, strlen(nodename
)+1) == -1)
282 fprintf(stderr
, "%s: Unable to set nodename: %s\n",
283 prog_name
, strerror(errno
));
287 if (sysuname(_UTS_SET
, _UTS_HOSTNAME
,
288 hostname
, strlen(hostname
)+1) == -1)
290 fprintf(stderr
, "%s: Unable to set hostname: %s\n",
291 prog_name
, strerror(errno
));
297 if ((fp
= fopen("/etc/hostname.file", "w")) == NULL
298 || fprintf(fp
, "%s\n", nodename
) == EOF
299 || fclose(fp
) == EOF
)
301 fprintf(stderr
, "%s: /etc/hostname.file: %s\n",
302 prog_name
, strerror(errno
));
307 if (!first_print
) printf("\n");
314 "Usage: %s -[eiah] [-E <eth-device>] [-I <ip-device>]\n",