-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow_IP.cpp
More file actions
67 lines (56 loc) · 1.61 KB
/
Show_IP.cpp
File metadata and controls
67 lines (56 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// print the IP addresses for whatever host you specify on the command line:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int ingc, char* argv[])
{
struct addrinfo hints, *res, *p;
int status;
char ipstr[INET6_ADDRSTRLEN];
if(ingc != 2)
{
fprintf(stderr, "usage: show ip hostname");
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, "getaddrininfo: %s\n", gai_strerror(status));
return 2;
}
printf("IP addresses for %s:\n\n", argv[1]);
for(p=res; p!=NULL; p=p->ai_next)
{
void *addr;
char *ipver;
if(p->ai_family == AF_INET) //IPV4
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPV4";
}
else //IPV6
{
struct sockaddr_in6 *ipv4 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv4->sin6_addr);
ipver = "IPV6";
}
//Convert IP to string and print
inet_ntop(p->ai_family,addr,ipstr, sizeof (ipstr));
printf(" %s: %s\n", ipver, ipstr);
}
freeaddrinfo(res); // free the linked list
return 0;
}
/* Output:
swapnil@swapnil-pc /media/swapnil/Network-Programming-CPP $ ./Show_IP www.google.com
IP addresses for www.google.com:
IPV4: 142.250.77.36
IPV6: 2404:6800:4009:81e::2004
*/