Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- ringneckparrot (c)
- License: http://creativecommons.org/licenses/by-nc-sa/3.0/
- Contact Me:
- Email: ringneckparrot@hotmail.com
- Facebook: http://www.facebook.com/ringneckparrot
- Twitter ID: pp4rr0t
- SecurityTube: http://www.securitytube.net/user/ringneckparrot
- ****************************************************************************/
- // Typical header declaration
- #include <stdio.h>
- #include <stdlib.h>
- // getaddrinfo() headers
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- // bzero() header
- #include <strings.h>
- // This is where we fine the inet_ntop() function
- #include <arpa/inet.h>
- #define ErrorValue -1 // We define the the ErrorValue as -1, because almost every error in sockets return a -1
- main(int argc, char **argv) // We create the main(). We get arguments from the command line
- {
- struct addrinfo hints; // We create an addrinfo structure for later use as the 3th parameter of getaddrinfo()
- struct addrinfo *res; // We create a pointer res to an addrinfo structure for later use as the 4th parameter
- struct addrinfo *p; // Again, a pointer p to an addrinfo structure for later use in printing the results
- int GetInfo; // Where we will later assign the getaddrinfo() function
- char *host; // The host will be a pointer to the 1st command line argument
- char ipstr[INET6_ADDRSTRLEN]; // We create an area where we can store the converted IP (we will see in a while)
- if ( argc != 2 ) // Check if the right number of arguments is given
- {
- printf("usage: %s [host]\n",argv[0]); // If not, we print some simple usage instructions
- }
- else
- {
- bzero(&hints, sizeof hints); // We empty the structure with the bzero() function
- hints.ai_family = AF_UNSPEC;// We set the Address Family field of the addrinfo structure as AF_UNSPEC
- // Which means that we do not care if it is IPv4 or IPv6
- // We can force it to be IPv4 with the AF_INET and IPv6 with AF_INET6
- hints.ai_socktype = SOCK_STREAM; // We specify the Socket Type as SOCK_STREAM (TCP)
- host = argv[1]; // We make the host be the first command line argument
- // remember the getaddrinfo structure:
- // int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
- GetInfo = getaddrinfo(host, NULL, &hints, &res);
- /*
- 1st Parameter: The address of host, which points to the first argument of the commandi line,
- thus it is the same as argv[1]
- 2nd Parameter: We leave the port number uninitialized
- 3rd Parameter: We give the address of hints,
- where our options are stored, and as required by the getaddrinfo() function
- 4th Parameter: We give the address of res, which points to a linked list of the addrinfo structure,
- with this way, we can later use the res to do a lot of interesting things.
- In our case, we will limit ourselves on printing stuff on the screen
- */
- // We check for an Error Value (-1) the GetInfo Variable which was previously assigned the getaddrinfo() function
- if (GetInfo == ErrorValue)
- {
- fprintf(stderr, "Error: %s\n", gai_strerror(GetInfo));
- // If yes, we print to screen that we have a problem, using the gai_stererror() function
- }
- // We print to screen the message below
- printf("IP Addresses for the given host: %s\n\n",host);
- for( p = res; p != NULL; p = p->ai_next)
- /*
- 1st Parameter: We assign the p pointer to point to res
- 2nd Parameter: As long as the p pointer is not NULL we do the action in the 3rd Parameter
- 3rd Parameter: ai_next links the items of the linked list
- */
- {
- void *addr; // We create a variable address, to store the address
- char *ipversion; // We create a pointer ipversion, to store the IP version of the host
- if( p->ai_family == AF_INET) // if the address family is AF_INET = IPv4
- {
- struct sockaddr_in *ipv4; // We create a sockaddr_in structure pointer called ipv4
- ipv4 = (struct sockaddr_in *)p->ai_addr;
- // We then, assign the ipv4 with the ai_next field of the p pointer
- // which is a type of (struct sockaddr_in *)
- // In this case the address equals to the address of the sin_addr field,
- // pointed by the structure pointer ipv4
- addr = &(ipv4->sin_addr);
- ipversion = "IPv4"; // We assign the ipversion with the string IPv4
- }
- else // In any other case, which will be IPv6
- {
- struct sockaddr_in6 *ipv6; // We create a sockaddr_in structure pointer called ipv6
- ipv6 = (struct sockaddr_in6 *)p->ai_addr;
- // We then, assign the ipv6 with the ai_next field of the p pointer
- // which is a type of (struct sockaddr_in *)
- // In this case the address equals to the address of the sin_addr field,
- // pointed by the structure pointer ipv6
- addr = &(ipv6->sin6_addr);
- ipversion = "IPv6"; // We assign the ipversion with the string IPv6
- }
- // We now convert the IP into something printable and human understandable
- inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
- // For more on this function use the man pages (man inet_ntop)
- /*
- 1st Parameter: The Address Family
- 2nd Parameter: The network IP address
- 3rd Parameter: The place where the converted presentable IP will be stored
- 4th Parameter: The size of the ipstr variable
- */
- // We print to screen the IP version followed by the IP
- // e.g IPv4: 182.232.234.122
- printf("%s: %s\n", ipversion, ipstr);
- }
- // Then we free the res structure using the freeaddrinfo();
- freeaddrinfo(res);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement