Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>//printf
  2. #include <string.h>//memset
  3. #include <stdlib.h>//for exit(0);
  4. #include <sys/socket.h>
  5. #include <errno.h>//For errno - the error number
  6. #include <netdb.h>//hostent
  7. #include <arpa/inet.h>
  8.  
  9. int hostname_to_ip(char * , char *);
  10.  
  11. int main(int argc , char *argv[])
  12. {
  13. if(argc <2)
  14. {
  15. printf("Please provide a hostname to resolve");
  16. exit(1);
  17. }
  18.  
  19. char *hostname = argv[1];
  20. char ip[100];
  21.  
  22. hostname_to_ip(hostname , ip);
  23. printf("%s resolved to %s" , hostname , ip);
  24.  
  25. printf("\n");
  26.  
  27. }
  28. /*
  29. Get ip from domain name
  30. */
  31.  
  32. int hostname_to_ip(char * hostname , char* ip)
  33. {
  34. struct hostent *he;
  35. struct in_addr **addr_list;
  36. int i;
  37.  
  38. if ( (he = gethostbyname( hostname ) ) == NULL)
  39. {
  40. // get the host info
  41. herror("gethostbyname");
  42. return 1;
  43. }
  44.  
  45. addr_list = (struct in_addr **) he->h_addr_list;
  46.  
  47. for(i = 0; addr_list[i] != NULL; i++)
  48. {
  49. //Return the first one;
  50. strcpy(ip , inet_ntoa(*addr_list[i]) );
  51. return 0;
  52. }
  53.  
  54. return 1;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement