Debian Codegraph @

tests/test_multi_ip.c

raw
1 // Specific test for multiple IP resolution
2 // This uses a hostname that typically returns multiple IPs
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 #ifndef _WIN32
9 #include <unistd.h>
10 #include <sys/socket.h>
11 #include <netdb.h>
12 #include <arpa/inet.h>
13 #else
14 #include <winsock2.h>
15 #include <ws2tcpip.h>
16 #endif
17
18 #define MAX_IP_RESULTS 10
19
20 static int
21 resolve_host(const char *hostname, char *ipaddr_list, int max_results,
22 int req_ai_family)
23 {
24 struct addrinfo hints;
25 struct addrinfo *result;
26 char ipaddr[INET6_ADDRSTRLEN];
27 int error;
28
29 // Truncate ipaddr_list to an empty string.
30 ipaddr_list[0] = '\0';
31
32 memset(&hints, 0, sizeof(struct addrinfo));
33
34 hints.ai_family = req_ai_family;
35 hints.ai_socktype = SOCK_STREAM;
36
37 error = getaddrinfo(hostname, NULL, &hints, &result);
38 if (error) return error;
39 int i = 0;
40 size_t offset = 0;
41 for(struct addrinfo *ai = result; ai != NULL && i < max_results; ai = ai->ai_next, i++) {
42 getnameinfo(ai->ai_addr, ai->ai_addrlen, ipaddr, sizeof(ipaddr), NULL, 0,
43 NI_NUMERICHOST);
44 if (offset > 0) {
45 ipaddr_list[offset++] = ';';
46 }
47 size_t len = strlen(ipaddr);
48 strcpy(ipaddr_list + offset, ipaddr);
49 offset += len;
50 }
51 freeaddrinfo(result);
52 return 0;
53 }
54
55 int main() {
56 char ipaddr_list[INET6_ADDRSTRLEN * MAX_IP_RESULTS + MAX_IP_RESULTS];
57
58 // Try multiple hostnames that might return multiple IPs
59 const char *hosts[] = {
60 "www.google.com", // Usually returns multiple IPs
61 "facebook.com", // Often returns multiple IPs
62 "amazon.com", // Usually returns multiple IPs
63 "cloudflare.com", // May return multiple IPs
64 };
65
66 printf("Testing multiple IP resolution:\n\n");
67
68 for (int h = 0; h < 4; h++) {
69 printf("--- Testing: %s ---\n", hosts[h]);
70
71 // Test with AF_UNSPEC which may return both IPv4 and IPv6
72 int result = resolve_host(hosts[h], ipaddr_list, 10, AF_UNSPEC);
73
74 if (result != 0) {
75 printf("Failed to resolve: %s\n\n", gai_strerror(result));
76 continue;
77 }
78
79 printf("Results: %s\n", ipaddr_list);
80
81 // Count IPs
82 int count = 1;
83 for (char *p = ipaddr_list; *p; p++) {
84 if (*p == ';') count++;
85 }
86 printf("Number of IPs: %d\n", count);
87
88 if (count > 1) {
89 printf("✓ Multiple IPs returned! Testing semicolon separation:\n");
90
91 // Parse and display each IP
92 char *copy = strdup(ipaddr_list);
93 char *token = strtok(copy, ";");
94 int idx = 1;
95 while (token) {
96 printf(" IP %d: %s\n", idx++, token);
97 token = strtok(NULL, ";");
98 }
99 free(copy);
100
101 // Verify format
102 if (ipaddr_list[0] == ';') {
103 printf("✗ ERROR: Starts with semicolon\n");
104 } else {
105 printf("✓ Doesn't start with semicolon\n");
106 }
107
108 size_t len = strlen(ipaddr_list);
109 if (len > 0 && ipaddr_list[len-1] == ';') {
110 printf("✗ ERROR: Ends with semicolon\n");
111 } else {
112 printf("✓ Doesn't end with semicolon\n");
113 }
114
115 if (strstr(ipaddr_list, ";;")) {
116 printf("✗ ERROR: Contains double semicolons\n");
117 } else {
118 printf("✓ No double semicolons\n");
119 }
120
121 printf("\n** SUCCESSFULLY TESTED MULTIPLE IP CASE **\n");
122 break; // Found a multi-IP case, we're done
123 }
124 printf("\n");
125 }
126
127 // Test max_results limiting
128 printf("\n--- Testing max_results limiting ---\n");
129 for (int max = 1; max <= 5; max++) {
130 int result = resolve_host("www.google.com", ipaddr_list, max, AF_UNSPEC);
131 if (result == 0) {
132 int count = 1;
133 for (char *p = ipaddr_list; *p; p++) {
134 if (*p == ';') count++;
135 }
136 printf("max_results=%d: got %d IPs\n", max, count);
137 if (count > max) {
138 printf("✗ ERROR: Returned more IPs than max_results!\n");
139 } else {
140 printf("✓ Correctly limited to %d\n", count);
141 }
142 }
143 }
144
145 return 0;
146 }