Debian Codegraph @

testing/fulltests/unit-tests/T014gethostbyaddr_clib.c

raw
1 /*
2 * HEADER Testing netsnmp_gethostbyaddr()
3 */
4
5 SOCK_STARTUP;
6
7 {
8 int ran_test = 0;
9 #ifdef HAVE_GETHOSTBYADDR
10 struct hostent *h, *h2 = NULL;
11 struct in_addr v4loop;
12 struct sockaddr_in sin_addr;
13 int s;
14
15 v4loop.s_addr = htonl(INADDR_LOOPBACK);
16 memset(&sin_addr, 0, sizeof(sin_addr));
17 sin_addr.sin_family = AF_INET;
18 sin_addr.sin_addr = v4loop;
19 s = socket(AF_INET, SOCK_DGRAM, 0);
20 if (s >= 0) {
21 if (bind(s, (struct sockaddr *) &sin_addr, sizeof(sin_addr)) >= 0) {
22 h = netsnmp_gethostbyaddr(&v4loop, sizeof(v4loop), AF_INET);
23 if (h)
24 h2 = gethostbyname(h->h_name);
25 OKF(h && (strcmp(h->h_name, "localhost") == 0 ||
26 (h2 && memcmp(h2->h_addr, &v4loop.s_addr,
27 sizeof(v4loop.s_addr)) == 0)),
28 ("127.0.0.1 lookup (%s -> %s)", h ? h->h_name : "(failed)",
29 h2 ? inet_ntoa(*(struct in_addr *) h2->
30 h_addr) : "(failed)"));
31 ran_test = 1;
32 }
33 close(s);
34 }
35 #endif
36 if (!ran_test)
37 OKF(1, ("Skipped IPv4 test"));
38 }
39
40 {
41 struct hostent *h;
42 #if defined(cygwin) || defined(solaris2)
43 static const struct in6_addr v6loop = { { IN6ADDR_LOOPBACK_INIT } };
44 #else
45 static const struct in6_addr v6loop = IN6ADDR_LOOPBACK_INIT;
46 #endif
47 struct sockaddr_in6 sin6_addr;
48 struct addrinfo hints, *addr = NULL, *ap;
49 char buf[64];
50 int s, res, ran_test = 0;
51
52 memset(&sin6_addr, 0, sizeof(sin6_addr));
53 sin6_addr.sin6_family = AF_INET6;
54 sin6_addr.sin6_addr = v6loop;
55 s = socket(AF_INET6, SOCK_DGRAM, 0);
56 if (s >= 0) {
57 if (bind(s, (struct sockaddr *) &sin6_addr, sizeof(sin6_addr)) >=
58 0) {
59 addr = NULL;
60 strncpy(buf, "(failed)", sizeof(buf));
61 h = netsnmp_gethostbyaddr(&v6loop, sizeof(v6loop), AF_INET6);
62 if (h) {
63 memset(&hints, 0, sizeof(hints));
64 hints.ai_family = AF_INET6;
65 res = getaddrinfo(h->h_name, NULL, &hints, &addr);
66 if (res == 0) {
67 for (ap = addr; ap; ap = ap->ai_next) {
68 if (ap->ai_family == AF_INET6) {
69 inet_ntop(ap->ai_family,
70 &((struct sockaddr_in6 *) ap->
71 ai_addr)->sin6_addr, buf,
72 sizeof(buf));
73 break;
74 }
75 }
76 if (!ap)
77 strncpy(buf, "no AF_INET6 address found", sizeof(buf));
78 } else {
79 snprintf(buf, sizeof(buf), "getaddrinfo() failed: %s",
80 strerror(errno));
81 }
82 }
83 OKF(h && (strcmp(h->h_name, "localhost") == 0 ||
84 (res == 0 && addr && memcmp(addr->ai_addr, &sin6_addr,
85 sizeof(sin6_addr)) == 0)),
86 ("::1 lookup (%s -> %s)", h ? h->h_name : "(failed)",
87 buf));
88 if (addr)
89 freeaddrinfo(addr);
90 ran_test = 1;
91 }
92 close(s);
93 }
94 if (!ran_test)
95 OKF(1, ("Skipped IPv6 test"));
96 }
97
98 SOCK_CLEANUP;