nslookup.c 972 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2016-2018 K. Lange
  5. *
  6. * nslookup - perform nameserver lookups
  7. *
  8. */
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <getopt.h>
  14. #include <sys/socket.h>
  15. static void ip_ntoa(uint32_t src_addr, char * out) {
  16. sprintf(out, "%d.%d.%d.%d",
  17. (unsigned int)((src_addr & 0xFF000000) >> 24),
  18. (unsigned int)((src_addr & 0xFF0000) >> 16),
  19. (unsigned int)((src_addr & 0xFF00) >> 8),
  20. (unsigned int)((src_addr & 0xFF)));
  21. }
  22. int main(int argc, char * argv[]) {
  23. if (argc < 2) return 1;
  24. struct hostent * host = gethostbyname(argv[1]);
  25. if (!host) {
  26. fprintf(stderr, "%s: not found\n", argv[1]);
  27. return 1;
  28. }
  29. char addr[16] = {0};
  30. ip_ntoa(*(uint32_t *)host->h_addr_list[0], addr);
  31. fprintf(stderr, "%s: %s\n", host->h_name, addr);
  32. return 0;
  33. }