1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <ifaddrs.h>
4 #include <stdint.h>
5
6 /* Internal definitions used in the libc code. */
7 #define __getservbyname_r getservbyname_r
8 #define __socket socket
9 #define __getsockname getsockname
10 #define __inet_aton inet_aton
11 #define __gethostbyaddr_r gethostbyaddr_r
12 #define __gethostbyname2_r gethostbyname2_r
13 #define __qsort_r qsort_r
14 #define __stat64 stat64
15
16 void
17 attribute_hidden
__check_pf(bool * p1,bool * p2,struct in6addrinfo ** in6ai,size_t * in6ailen)18 __check_pf (bool *p1, bool *p2, struct in6addrinfo **in6ai, size_t *in6ailen)
19 {
20 *p1 = *p2 = true;
21 *in6ai = NULL;
22 *in6ailen = 0;
23 }
24
25 void
26 attribute_hidden
__free_in6ai(struct in6addrinfo * ai)27 __free_in6ai (struct in6addrinfo *ai)
28 {
29 }
30
31 void
32 attribute_hidden
__check_native(uint32_t a1_index,int * a1_native,uint32_t a2_index,int * a2_native)33 __check_native (uint32_t a1_index, int *a1_native,
34 uint32_t a2_index, int *a2_native)
35 {
36 }
37
38 int
39 attribute_hidden
__idna_to_ascii_lz(const char * input,char ** output,int flags)40 __idna_to_ascii_lz (const char *input, char **output, int flags)
41 {
42 return 0;
43 }
44
45 int
46 attribute_hidden
__idna_to_unicode_lzlz(const char * input,char ** output,int flags)47 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
48 {
49 *output = NULL;
50 return 0;
51 }
52
53 void
54 attribute_hidden
_res_hconf_init(void)55 _res_hconf_init (void)
56 {
57 }
58
59 #undef USE_NSCD
60 #include "../sysdeps/posix/getaddrinfo.c"
61
62 nss_action_list __nss_hosts_database attribute_hidden;
63
64 /* This is the beginning of the real test code. The above defines
65 (among other things) the function rfc3484_sort. */
66
67
68 #if __BYTE_ORDER == __BIG_ENDIAN
69 # define h(n) n
70 #else
71 # define h(n) __bswap_constant_32 (n)
72 #endif
73
74
75 ssize_t
__getline(char ** lineptr,size_t * n,FILE * s)76 __getline (char **lineptr, size_t *n, FILE *s)
77 {
78 *lineptr = NULL;
79 *n = 0;
80 return 0;
81 }
82
83
84 static int
do_test(void)85 do_test (void)
86 {
87 labels = default_labels;
88 precedence = default_precedence;
89 scopes = default_scopes;
90
91 struct sockaddr_in so1;
92 so1.sin_family = AF_INET;
93 so1.sin_addr.s_addr = h (0xc0a85f19);
94 /* Clear the rest of the structure to avoid warnings. */
95 memset (so1.sin_zero, '\0', sizeof (so1.sin_zero));
96
97 struct sockaddr_in sa1;
98 sa1.sin_family = AF_INET;
99 sa1.sin_addr.s_addr = h (0xe0a85f19);
100
101 struct addrinfo ai1;
102 ai1.ai_family = AF_INET;
103 ai1.ai_addr = (struct sockaddr *) &sa1;
104
105 struct sockaddr_in6 so2;
106 so2.sin6_family = AF_INET6;
107 so2.sin6_addr.s6_addr32[0] = h (0xfec01234);
108 so2.sin6_addr.s6_addr32[1] = 1;
109 so2.sin6_addr.s6_addr32[2] = 1;
110 so2.sin6_addr.s6_addr32[3] = 1;
111
112 struct sockaddr_in6 sa2;
113 sa2.sin6_family = AF_INET6;
114 sa2.sin6_addr.s6_addr32[0] = h (0x07d10001);
115 sa2.sin6_addr.s6_addr32[1] = 1;
116 sa2.sin6_addr.s6_addr32[2] = 1;
117 sa2.sin6_addr.s6_addr32[3] = 1;
118
119 struct addrinfo ai2;
120 ai2.ai_family = AF_INET6;
121 ai2.ai_addr = (struct sockaddr *) &sa2;
122
123
124 struct sort_result results[2];
125 size_t order[2];
126
127 results[0].dest_addr = &ai1;
128 results[0].got_source_addr = true;
129 results[0].source_addr_len = sizeof (so1);
130 results[0].source_addr_flags = 0;
131 results[0].prefixlen = 16;
132 results[0].index = 0;
133 memcpy (&results[0].source_addr, &so1, sizeof (so1));
134 order[0] = 0;
135
136 results[1].dest_addr = &ai2;
137 results[1].got_source_addr = true;
138 results[1].source_addr_len = sizeof (so2);
139 results[1].source_addr_flags = 0;
140 results[1].prefixlen = 16;
141 results[1].index = 0;
142 memcpy (&results[1].source_addr, &so2, sizeof (so2));
143 order[1] = 1;
144
145
146 struct sort_result_combo combo = { .results = results, .nresults = 2 };
147 qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
148
149 int result = 0;
150 if (results[order[0]].dest_addr->ai_family == AF_INET6)
151 {
152 puts ("wrong order in first test");
153 result |= 1;
154 }
155
156
157 /* And again, this time with the reverse starting order. */
158 results[1].dest_addr = &ai1;
159 results[1].got_source_addr = true;
160 results[1].source_addr_len = sizeof (so1);
161 results[1].source_addr_flags = 0;
162 results[1].prefixlen = 16;
163 results[1].index = 0;
164 memcpy (&results[1].source_addr, &so1, sizeof (so1));
165 order[1] = 1;
166
167 results[0].dest_addr = &ai2;
168 results[0].got_source_addr = true;
169 results[0].source_addr_len = sizeof (so2);
170 results[0].source_addr_flags = 0;
171 results[0].prefixlen = 16;
172 results[0].index = 0;
173 memcpy (&results[0].source_addr, &so2, sizeof (so2));
174 order[0] = 0;
175
176
177 qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
178
179 if (results[order[0]].dest_addr->ai_family == AF_INET6)
180 {
181 puts ("wrong order in second test");
182 result |= 1;
183 }
184
185 return result;
186 }
187
188 #define TEST_FUNCTION do_test ()
189 #include "../test-skeleton.c"
190