Advertisement
ringneckparrot

ip_finder undocumented

Apr 9th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. /****************************************************************************
  2.     ringneckparrot (c)
  3.     License: http://creativecommons.org/licenses/by-nc-sa/3.0/
  4.    
  5.     Contact Me:
  6.     Email: ringneckparrot@hotmail.com
  7.     Facebook: http://www.facebook.com/ringneckparrot
  8.     Twitter ID: pp4rr0t
  9.     SecurityTube: http://www.securitytube.net/user/ringneckparrot
  10.  
  11. ****************************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17. #include <netdb.h>
  18. #include <strings.h>
  19. #include <arpa/inet.h>
  20.  
  21. #define ErrorValue -1
  22.  
  23. main(int argc, char **argv)
  24. {
  25.     struct addrinfo hints;
  26.     struct addrinfo *res;
  27.     struct addrinfo *p;
  28.     int GetInfo;
  29.     char *host;
  30.     char ipstr[INET6_ADDRSTRLEN];
  31.  
  32.     if ( argc != 2 )
  33.     {
  34.         printf("usage: %s [host]\n",argv[0]);
  35.     }
  36.    
  37.     else
  38.     {
  39.         bzero(&hints, sizeof hints);  
  40.         hints.ai_family = AF_UNSPEC;
  41.         hints.ai_socktype = SOCK_STREAM;
  42.  
  43.         host = argv[1];
  44.    
  45.         GetInfo = getaddrinfo(host, NULL, &hints, &res);
  46.        
  47.         if (GetInfo == ErrorValue)
  48.         {
  49.             fprintf(stderr, "Error: %s\n", gai_strerror(GetInfo));
  50.         }
  51.  
  52.         printf("IP Addresses for the given host: %s\n\n",host);
  53.  
  54.         for( p = res; p != NULL; p = p->ai_next)   
  55.         {
  56.             void *addr;
  57.             char *ipversion;
  58.  
  59.             if( p->ai_family == AF_INET)
  60.             {
  61.                 struct sockaddr_in *ipv4;  
  62.                 ipv4 = (struct sockaddr_in *)p->ai_addr;
  63.                 addr = &(ipv4->sin_addr);
  64.                 ipversion = "IPv4";
  65.             }
  66.  
  67.             else
  68.             {
  69.                 struct sockaddr_in6 *ipv6;
  70.                 ipv6 = (struct sockaddr_in6 *)p->ai_addr;
  71.                 addr = &(ipv6->sin6_addr);
  72.                 ipversion = "IPv6";
  73.             }
  74.    
  75.             inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
  76.    
  77.             printf("%s: %s\n", ipversion, ipstr);
  78.     }
  79.     freeaddrinfo(res);
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement