69 lines
1.6 KiB
C++
69 lines
1.6 KiB
C++
// #include <stdio.h>
|
|
// #include <cstring>
|
|
// #include <sys/types.h>
|
|
// #include <sys/socket.h>
|
|
// #include <netdb.h>
|
|
// #include <arpa/inet.h>
|
|
// #include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <netdb.h>
|
|
#include <arpa/inet.h>
|
|
#include <sys/wait.h>
|
|
#include <signal.h>
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
struct addrinfo hints, *res, *p;
|
|
int status;
|
|
char ipstr[INET6_ADDRSTRLEN];
|
|
|
|
if (argc != 2)
|
|
{
|
|
fprintf(stderr, "usage: showip hostname\n");
|
|
return 1;
|
|
}
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
if ((status = getaddrinfo(argv[1], NULL, &hints, &res)) != 0)
|
|
{
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
|
|
return 2;
|
|
}
|
|
|
|
printf("IP addresses for %s: \n\n", argv[1]);
|
|
|
|
for (p = res; p != nullptr; p = p->ai_next)
|
|
{
|
|
void *addr;
|
|
char *ipver;
|
|
|
|
// get the pointer to the address
|
|
if (p->ai_family == AF_INET)
|
|
{
|
|
struct sockaddr_in *ipv4 = (struct sockaddr_in*)p->ai_addr;
|
|
addr = &(ipv4->sin_addr);
|
|
ipver = "IPv4";
|
|
} else
|
|
{
|
|
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6*)p->ai_addr;
|
|
addr = &(ipv6->sin6_addr);
|
|
ipver = "IPv6";
|
|
}
|
|
inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
|
|
printf(" %s: %s\n", ipver, ipstr);
|
|
}
|
|
freeaddrinfo(res);
|
|
|
|
return 0;
|
|
} |