1 /* Copyright (c) 2015 Google, Inc.
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details. */
7 #include <parlib/parlib.h>
11 #include <sys/socket.h>
13 #include <sys/types.h>
14 #include <arpa/inet.h>
16 static void print_gai(struct addrinfo *ai, const char *info)
18 struct sockaddr_in *ipv4sa;
19 printf("%s: fam %d, sock %d, prot %d ", info, ai->ai_family,
20 ai->ai_socktype, ai->ai_protocol);
22 ipv4sa = (struct sockaddr_in*)ai->ai_addr;
23 const char *ipv4n = inet_ntop(AF_INET, &ipv4sa->sin_addr, buf, 128);
25 printf("addr %s, port %d\n", buf, ntohs(ipv4sa->sin_port));
28 static void test_gai(const char *node, const char *serv, struct addrinfo *hints,
31 struct addrinfo *_ai_res;
32 int ret = getaddrinfo(node, serv, hints, &_ai_res);
34 printf("%s: GAI failed, %d, %d %s\n", info, ret, errno, errstr());
36 print_gai(_ai_res, info);
37 freeaddrinfo(_ai_res);
41 int main(int argc, char **argv)
46 struct addrinfo hints = {0};
48 test_gai("10.0.2.1", "80", 0, "IP and 80");
50 test_gai("10.0.2.1.dicks", "80", 0, "Non-number name");
52 test_gai("10.0.2.2", "http", 0, "http serv");
54 hints.ai_family = AF_UNSPEC;
55 hints.ai_socktype = SOCK_DGRAM;
56 hints.ai_protocol = 0;
57 test_gai("10.0.2.3", "12345", &hints, "SOCK_DGRAM");
59 hints.ai_family = AF_UNSPEC;
60 hints.ai_socktype = SOCK_RAW;
61 hints.ai_protocol = 0;
62 test_gai("10.0.2.3", "12345", &hints, "SOCK_RAW");
64 hints.ai_family = AF_UNSPEC;
65 hints.ai_socktype = 0;
66 hints.ai_protocol = IPPROTO_ICMP;
67 test_gai("10.0.2.3", "12345", &hints, "ICMP");
69 hints.ai_family = AF_UNSPEC;
70 hints.ai_socktype = SOCK_DGRAM;
71 hints.ai_protocol = IPPROTO_TCP;
72 test_gai("10.0.2.3", "12345", &hints, "Impossible hint");