PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

commands/hostaddr/hostaddr.c

http://www.minix3.org/
C | 317 lines | 276 code | 28 blank | 13 comment | 73 complexity | c07970cdd491e7a7d940d4183b1af7d0 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  1. /*
  2. hostaddr.c
  3. Fetch an ip and/or ethernet address and print it on one line.
  4. Created: Jan 27, 1992 by Philip Homburg
  5. */
  6. #include <sys/types.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/utsname.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #ifdef __NBSD_LIBC
  16. #include <netinet/in.h>
  17. #endif
  18. #include <net/netlib.h>
  19. #include <net/hton.h>
  20. #include <net/gen/ether.h>
  21. #include <net/gen/eth_io.h>
  22. #include <net/gen/if_ether.h>
  23. #include <net/gen/in.h>
  24. #include <net/gen/inet.h>
  25. #include <net/gen/ip_io.h>
  26. #include <net/gen/netdb.h>
  27. #include <net/gen/socket.h>
  28. #include <net/gen/nameser.h>
  29. #include <net/gen/resolv.h>
  30. #include <net/gen/dhcp.h>
  31. #include <paths.h>
  32. char *prog_name;
  33. char DHCPCACHE[]=_PATH_DHCPCACHE;
  34. int main _ARGS(( int argc, char *argv[] ));
  35. void usage _ARGS(( void ));
  36. int main(argc, argv)
  37. int argc;
  38. char *argv[];
  39. {
  40. int c;
  41. int first_print;
  42. int a_flag, e_flag, i_flag, h_flag;
  43. char *E_arg, *I_arg;
  44. int do_ether, do_ip, do_asc_ip, do_hostname;
  45. char *eth_device, *ip_device;
  46. int eth_fd, ip_fd;
  47. int result;
  48. nwio_ethstat_t nwio_ethstat;
  49. nwio_ipconf_t nwio_ipconf;
  50. struct hostent *hostent;
  51. char *hostname, *domain;
  52. char nodename[2*256];
  53. dhcp_t dhcp;
  54. first_print= 1;
  55. prog_name= argv[0];
  56. a_flag= e_flag= h_flag = i_flag= 0;
  57. E_arg= I_arg= NULL;
  58. while((c= getopt(argc, argv, "?aheE:iI:")) != -1)
  59. {
  60. switch(c)
  61. {
  62. case '?':
  63. usage();
  64. case 'a':
  65. if (a_flag)
  66. usage();
  67. a_flag= 1;
  68. break;
  69. case 'h':
  70. if (h_flag)
  71. usage();
  72. h_flag= 1;
  73. break;
  74. case 'e':
  75. if (e_flag)
  76. usage();
  77. e_flag= 1;
  78. break;
  79. case 'E':
  80. if (E_arg)
  81. usage();
  82. E_arg= optarg;
  83. break;
  84. case 'i':
  85. if (i_flag)
  86. usage();
  87. i_flag= 1;
  88. break;
  89. case 'I':
  90. if (I_arg)
  91. usage();
  92. I_arg= optarg;
  93. break;
  94. default:
  95. usage();
  96. }
  97. }
  98. if(optind != argc)
  99. usage();
  100. do_ether= e_flag;
  101. if (E_arg)
  102. eth_device= E_arg;
  103. else
  104. {
  105. eth_device= getenv("ETH_DEVICE");
  106. if (!eth_device)
  107. eth_device= ETH_DEVICE;
  108. }
  109. do_ip= i_flag;
  110. do_asc_ip= a_flag;
  111. do_hostname= h_flag;
  112. if (I_arg)
  113. ip_device= I_arg;
  114. else
  115. {
  116. ip_device= getenv("IP_DEVICE");
  117. if (!ip_device)
  118. ip_device= IP_DEVICE;
  119. }
  120. if (!do_ether && !do_ip && !do_asc_ip && !do_hostname)
  121. do_ether= do_ip= do_asc_ip= 1;
  122. if (do_ether)
  123. {
  124. eth_fd= open(eth_device, O_RDWR);
  125. if (eth_fd == -1)
  126. {
  127. fprintf(stderr, "%s: Unable to open '%s': %s\n",
  128. prog_name, eth_device, strerror(errno));
  129. exit(1);
  130. }
  131. result= ioctl(eth_fd, NWIOGETHSTAT, &nwio_ethstat);
  132. if (result == -1)
  133. {
  134. fprintf(stderr,
  135. "%s: Unable to fetch ethernet address: %s\n",
  136. prog_name, strerror(errno));
  137. exit(1);
  138. }
  139. printf("%s%s", first_print ? "" : " ",
  140. ether_ntoa(&nwio_ethstat.nwes_addr));
  141. first_print= 0;
  142. }
  143. if (do_ip || do_asc_ip || do_hostname)
  144. {
  145. ip_fd= open(ip_device, O_RDWR);
  146. if (ip_fd == -1)
  147. {
  148. fprintf(stderr, "%s: Unable to open '%s': %s\n",
  149. prog_name, ip_device, strerror(errno));
  150. exit(1);
  151. }
  152. result= ioctl(ip_fd, NWIOGIPCONF, &nwio_ipconf);
  153. if (result == -1)
  154. {
  155. fprintf(stderr,
  156. "%s: Unable to fetch IP address: %s\n",
  157. prog_name,
  158. strerror(errno));
  159. exit(1);
  160. }
  161. }
  162. setuid(getuid());
  163. if (do_ip)
  164. {
  165. printf("%s%s", first_print ? "" : " ",
  166. inet_ntoa(nwio_ipconf.nwic_ipaddr));
  167. first_print= 0;
  168. }
  169. if (do_asc_ip || do_hostname)
  170. {
  171. int fd;
  172. int r;
  173. dhcp_t d;
  174. u8_t *data;
  175. size_t hlen, dlen;
  176. hostname= NULL;
  177. domain= NULL;
  178. /* Use a reverse DNS lookup to get the host name. This is
  179. * the preferred method, but often fails due to lazy admins.
  180. */
  181. hostent= gethostbyaddr((char *)&nwio_ipconf.nwic_ipaddr,
  182. sizeof(nwio_ipconf.nwic_ipaddr), AF_INET);
  183. if (hostent != NULL) hostname= hostent->h_name;
  184. if (hostname != NULL)
  185. {
  186. /* Reverse DNS works. */
  187. }
  188. else if ((fd= open(DHCPCACHE, O_RDONLY)) == -1)
  189. {
  190. if (errno != ENOENT)
  191. {
  192. fprintf(stderr, "%s: %s: %s\n",
  193. prog_name, DHCPCACHE, strerror(errno));
  194. exit(1);
  195. }
  196. }
  197. else
  198. {
  199. /* Try to get the hostname from the DHCP data. */
  200. while ((r= read(fd, &d, sizeof(d))) == sizeof(d))
  201. {
  202. if (d.yiaddr == nwio_ipconf.nwic_ipaddr) break;
  203. }
  204. if (r < 0)
  205. {
  206. fprintf(stderr, "%s: %s: %s\n",
  207. prog_name, DHCPCACHE, strerror(errno));
  208. exit(1);
  209. }
  210. close(fd);
  211. if (r == sizeof(d))
  212. {
  213. if (dhcp_gettag(&d, DHCP_TAG_HOSTNAME,
  214. &data, &hlen))
  215. hostname= (char *) data;
  216. if (dhcp_gettag(&d, DHCP_TAG_DOMAIN,
  217. &data, &dlen))
  218. domain= (char *) data;
  219. if (hostname != NULL) hostname[hlen] = 0;
  220. if (domain != NULL) domain[dlen] = 0;
  221. }
  222. }
  223. if (hostname != NULL)
  224. {
  225. if (strchr(hostname, '.') != NULL)
  226. {
  227. domain= strchr(hostname, '.');
  228. *domain++ = 0;
  229. }
  230. }
  231. else
  232. {
  233. /* No host name anywhere. Use the IP address. */
  234. hostname= inet_ntoa(nwio_ipconf.nwic_ipaddr);
  235. domain= NULL;
  236. }
  237. strcpy(nodename, hostname);
  238. if (domain != NULL)
  239. {
  240. strcat(nodename, ".");
  241. strcat(nodename, domain);
  242. }
  243. }
  244. if (do_asc_ip)
  245. {
  246. printf("%s%s", first_print ? "" : " ", nodename);
  247. first_print= 0;
  248. }
  249. if (do_hostname)
  250. {
  251. #if __minix_vmd
  252. if (sysuname(_UTS_SET, _UTS_NODENAME,
  253. nodename, strlen(nodename)+1) == -1)
  254. {
  255. fprintf(stderr, "%s: Unable to set nodename: %s\n",
  256. prog_name, strerror(errno));
  257. exit(1);
  258. }
  259. if (sysuname(_UTS_SET, _UTS_HOSTNAME,
  260. hostname, strlen(hostname)+1) == -1)
  261. {
  262. fprintf(stderr, "%s: Unable to set hostname: %s\n",
  263. prog_name, strerror(errno));
  264. exit(1);
  265. }
  266. #else
  267. FILE *fp;
  268. if ((fp= fopen("/etc/hostname.file", "w")) == NULL
  269. || fprintf(fp, "%s\n", nodename) == EOF
  270. || fclose(fp) == EOF)
  271. {
  272. fprintf(stderr, "%s: /etc/hostname.file: %s\n",
  273. prog_name, strerror(errno));
  274. exit(1);
  275. }
  276. #endif
  277. }
  278. if (!first_print) printf("\n");
  279. exit(0);
  280. }
  281. void usage()
  282. {
  283. fprintf(stderr,
  284. "Usage: %s -[eiah] [-E <eth-device>] [-I <ip-device>]\n",
  285. prog_name);
  286. exit(1);
  287. }