Guest User

Untitled

a guest
Jan 23rd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <netdb.h>
  5. #include <regex.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10.  
  11. int main(int argc, char *argv[]) {
  12. int i;
  13. struct hostent *host;
  14. struct in_addr **addr_list;
  15. struct sockaddr_in sin;
  16. char domain[512];
  17. regex_t er;
  18.  
  19. if (argc != 2) {
  20. fprintf(stderr,"usage: %s {hostname|ip_address}\n",argv[0]);
  21. return 1;
  22. }
  23.  
  24. regcomp(&er, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}", REG_EXTENDED|REG_NOSUB);
  25. if ((regexec(&er, argv[1],0,NULL,0))==0) { // is a IP Address
  26. sin.sin_addr.s_addr = inet_addr(argv[1]);
  27. host = gethostbyaddr((char *)&sin.sin_addr.s_addr,sizeof(struct in_addr), AF_INET);
  28. if (host!=(struct hostent *)0) {
  29. strcpy(domain,host->h_name);
  30. printf("%s domain name pointer %s\n", argv[1], domain);
  31. } else {
  32. printf("gethostbyaddr failed\n");
  33. }
  34.  
  35. } else { // is a hostname
  36. if ((host = gethostbyname(argv[1])) != NULL) {
  37. printf("IP Addresses:\n");
  38. addr_list = (struct in_addr **)host->h_addr_list;
  39. for(i = 0; addr_list[i] != NULL; i++)
  40. printf("%s has address %s\n", argv[1], inet_ntoa(*addr_list[i]));
  41. } else {
  42. printf("gethostbyname failed\n");
  43. }
  44. }
  45. return 0;
  46. }
Add Comment
Please, Sign In to add comment