Advertisement
ringneckparrot

ip_finder documented

Apr 9th, 2012
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.58 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. // Typical header declaration
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. // getaddrinfo() headers
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <netdb.h>
  21.  
  22. // bzero() header
  23. #include <strings.h>
  24.  
  25. // This is where we fine the inet_ntop() function
  26. #include <arpa/inet.h>
  27.  
  28. #define ErrorValue -1 // We define the the ErrorValue as -1, because almost every error in sockets return a -1
  29.  
  30. main(int argc, char **argv) // We create the main(). We get arguments from the command line
  31. {
  32.     struct addrinfo hints; // We create an addrinfo structure for later use as the 3th parameter of getaddrinfo()
  33.     struct addrinfo *res; // We create a pointer res to an addrinfo structure for later use as the 4th parameter
  34.     struct addrinfo *p; // Again, a pointer p to an addrinfo structure for later use in printing the results
  35.     int GetInfo; // Where we will later assign the getaddrinfo() function
  36.     char *host; // The host will be a pointer to the 1st command line argument 
  37.     char ipstr[INET6_ADDRSTRLEN]; // We create an area where we can store the converted IP (we will see in a while)
  38.  
  39.  
  40.  
  41.     if ( argc != 2 ) // Check if the right number of arguments is given
  42.     {
  43.         printf("usage: %s [host]\n",argv[0]); // If not, we print some simple usage instructions
  44.     }
  45.    
  46.     else
  47.     {
  48.     bzero(&hints, sizeof hints); // We empty the structure with the bzero() function
  49.  
  50.     hints.ai_family = AF_UNSPEC;// We set the Address Family field of the addrinfo structure as AF_UNSPEC
  51.                     // Which means that we do not care if it is IPv4 or IPv6
  52.                     // We can force it to be IPv4 with the AF_INET and IPv6 with AF_INET6
  53.        
  54.     hints.ai_socktype = SOCK_STREAM; // We specify the Socket Type as SOCK_STREAM (TCP)
  55.  
  56.     host = argv[1]; // We make the host be the first command line argument
  57.    
  58.     // remember the getaddrinfo structure:
  59.     //  int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
  60.     GetInfo = getaddrinfo(host, NULL, &hints, &res);
  61.     /*
  62.         1st Parameter: The address of host, which points to the first argument of the commandi line,
  63.                    thus it is the same as argv[1]
  64.  
  65.         2nd Parameter: We leave the port number uninitialized
  66.  
  67.         3rd Parameter: We give the address of hints,
  68.                    where our options are stored, and as required by the getaddrinfo() function
  69.        
  70.         4th Parameter: We give the address of res, which points to a linked list of the addrinfo structure,
  71.                    with this way, we can later use the res to do a lot of interesting things.
  72.                    In our case, we will limit ourselves on printing stuff on the screen
  73.     */
  74.  
  75.  
  76.      // We check for an Error Value (-1) the GetInfo Variable which was previously assigned the getaddrinfo() function
  77.     if (GetInfo == ErrorValue)
  78.     {
  79.         fprintf(stderr, "Error: %s\n", gai_strerror(GetInfo));
  80.         // If yes, we print to screen that we have a problem, using the gai_stererror() function
  81.     }
  82.  
  83.     // We print to screen the message below
  84.     printf("IP Addresses for the given host: %s\n\n",host);
  85.  
  86.     for( p = res; p != NULL; p = p->ai_next)   
  87.     /*
  88.         1st Parameter: We assign the p pointer to point to res
  89.         2nd Parameter: As long as the p pointer is not NULL we do the action in the 3rd Parameter
  90.         3rd Parameter: ai_next links the items of the linked list
  91.     */
  92.     {
  93.         void *addr; // We create a variable address, to store the address
  94.         char *ipversion; // We create a pointer ipversion, to store the IP version of the host
  95.  
  96.         if( p->ai_family == AF_INET) // if the address family is AF_INET = IPv4
  97.         {
  98.             struct sockaddr_in *ipv4;  // We create a sockaddr_in structure pointer called ipv4
  99.            
  100.             ipv4 = (struct sockaddr_in *)p->ai_addr;
  101.             // We then, assign the ipv4 with the ai_next field of the p pointer
  102.             // which is a type of (struct sockaddr_in *)
  103.        
  104.             // In this case the address equals to the address of the sin_addr field,
  105.             // pointed by the structure pointer ipv4
  106.                         addr = &(ipv4->sin_addr);
  107.        
  108.             ipversion = "IPv4"; // We assign the ipversion with the string IPv4
  109.  
  110.         }
  111.  
  112.         else // In any other case, which will be IPv6
  113.         {
  114.             struct sockaddr_in6 *ipv6;  // We create a sockaddr_in structure pointer called ipv6
  115.  
  116.             ipv6 = (struct sockaddr_in6 *)p->ai_addr;
  117.             // We then, assign the ipv6 with the ai_next field of the p pointer
  118.             // which is a type of (struct sockaddr_in *)
  119.            
  120.             // In this case the address equals to the address of the sin_addr field,
  121.             // pointed by the structure pointer ipv6
  122.             addr = &(ipv6->sin6_addr);
  123.                
  124.             ipversion = "IPv6"; // We assign the ipversion with the string IPv6
  125.         }
  126.    
  127.         // We now convert the IP into something printable and human understandable
  128.         inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
  129.         // For more on this function use the man pages (man inet_ntop)
  130.         /*
  131.             1st Parameter: The Address Family
  132.             2nd Parameter: The network IP address
  133.             3rd Parameter: The place where the converted presentable IP will be stored
  134.             4th Parameter: The size of the ipstr variable
  135.         */
  136.            
  137.         // We print to screen the IP version followed by the IP
  138.         // e.g IPv4: 182.232.234.122
  139.    
  140.         printf("%s: %s\n", ipversion, ipstr);
  141.     }
  142.  
  143.     // Then we free the res structure using the freeaddrinfo();
  144.     freeaddrinfo(res);
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement