-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgetaddrinfo.c
47 lines (39 loc) · 1.16 KB
/
getaddrinfo.c
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
#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 <arpa/inet.h>
#include <netdb.h>
int
main(int argc, char **argv){
char *ptr;
struct addrinfo hints, *res, *bob;
int n;
char addrstring[100];
/* What are we looking for */
bzero(&hints, sizeof(hints));
hints.ai_family =0; AF_INET6;
hints.ai_socktype=0; SOCK_STREAM;
hints.ai_protocol=0;
// hints.ai_flags = AI_ALL; //AI_CANONNAME;
hints.ai_flags |= AI_CANONNAME;
hints.ai_flags |= AI_ALL;
while (--argc > 0) {
ptr = *++argv;
if ( (n = getaddrinfo(ptr, "domain", &hints, &res) ) != 0) {
fprintf(stderr,"getaddrinfo error for host: %s: %s",ptr, gai_strerror(n));
continue;
}
bob = res;
do {
inet_ntop(bob->ai_family, &((struct sockaddr_in *)bob->ai_addr)->sin_addr,addrstring,100);
printf(" hostname: %s IP; %s Protocol; %d (Port : %d ) \n", bob->ai_canonname,addrstring,bob->ai_protocol, ntohs(((struct sockaddr_in *)bob->ai_addr)->sin_port));
bob = bob->ai_next;
} while (bob != NULL );
}
exit(0);
}