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 struct sockaddr_in addrs[] =
75 {
76   { .sin_family = AF_INET, .sin_addr = { h (0x0aa85f19) } },
77   { .sin_family = AF_INET, .sin_addr = { h (0xac105f19) } },
78   { .sin_family = AF_INET, .sin_addr = { h (0xc0000219) } },
79   { .sin_family = AF_INET, .sin_addr = { h (0xc0a86d1d) } },
80   { .sin_family = AF_INET, .sin_addr = { h (0xc0a85d03) } },
81   { .sin_family = AF_INET, .sin_addr = { h (0xc0a82c3d) } },
82   { .sin_family = AF_INET, .sin_addr = { h (0xc0a86002) } },
83   { .sin_family = AF_INET, .sin_addr = { h (0xc0a802f3) } },
84   { .sin_family = AF_INET, .sin_addr = { h (0xc0a80810) } },
85   { .sin_family = AF_INET, .sin_addr = { h (0xc0a85e02) } }
86 };
87 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
88 static struct addrinfo ais[naddrs];
89 static struct sort_result results[naddrs];
90 static size_t order[naddrs];
91 
92 static int expected[naddrs] =
93   {
94     9, 4, 3, 6, 5, 7, 8, 2, 0, 1
95   };
96 
97 
98 ssize_t
__getline(char ** lineptr,size_t * n,FILE * s)99 __getline (char **lineptr, size_t *n, FILE *s)
100 {
101   *lineptr = NULL;
102   *n = 0;
103   return 0;
104 }
105 
106 
107 static int
do_test(void)108 do_test (void)
109 {
110   labels = default_labels;
111   precedence = default_precedence;
112   scopes= default_scopes;
113 
114   struct sockaddr_in so;
115   so.sin_family = AF_INET;
116   so.sin_addr.s_addr = h (0xc0a85f19);
117   /* Clear the rest of the structure to avoid warnings.  */
118   memset (so.sin_zero, '\0', sizeof (so.sin_zero));
119 
120   for (int i = 0; i < naddrs; ++i)
121     {
122       ais[i].ai_family = AF_INET;
123       ais[i].ai_addr = (struct sockaddr *) &addrs[i];
124       results[i].dest_addr = &ais[i];
125       results[i].got_source_addr = true;
126       memcpy(&results[i].source_addr, &so, sizeof (so));
127       results[i].source_addr_len = sizeof (so);
128       results[i].source_addr_flags = 0;
129       results[i].prefixlen = 8;
130       results[i].index = 0;
131 
132       order[i] = i;
133     }
134 
135   struct sort_result_combo combo = { .results = results, .nresults = naddrs };
136   qsort_r (order, naddrs, sizeof (order[0]), rfc3484_sort, &combo);
137 
138   int result = 0;
139   for (int i = 0; i < naddrs; ++i)
140     {
141       struct in_addr addr = ((struct sockaddr_in *) (results[order[i]].dest_addr->ai_addr))->sin_addr;
142 
143       int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
144 			 sizeof (struct in_addr));
145       printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
146       result |= here;
147     }
148 
149   return result;
150 }
151 
152 #define TEST_FUNCTION do_test ()
153 #include "../test-skeleton.c"
154