Back to index

glibc  2.9
tst-rfc3484-3.c
Go to the documentation of this file.
00001 #include <stdbool.h>
00002 #include <stdio.h>
00003 #include <ifaddrs.h>
00004 
00005 /* Internal definitions used in the libc code.  */
00006 #define __getservbyname_r getservbyname_r
00007 #define __socket socket
00008 #define __getsockname getsockname
00009 #define __inet_aton inet_aton
00010 #define __gethostbyaddr_r gethostbyaddr_r
00011 #define __gethostbyname2_r gethostbyname2_r
00012 
00013 void
00014 attribute_hidden
00015 __check_pf (bool *p1, bool *p2, struct in6addrinfo **in6ai, size_t *in6ailen)
00016 {
00017   *p1 = *p2 = true;
00018   *in6ai = NULL;
00019   *in6ailen = 0;
00020 }
00021 
00022 void
00023 attribute_hidden
00024 __check_native (uint32_t a1_index, int *a1_native,
00025               uint32_t a2_index, int *a2_native)
00026 {
00027 }
00028 
00029 int
00030 attribute_hidden
00031 __idna_to_ascii_lz (const char *input, char **output, int flags)
00032 {
00033   return 0;
00034 }
00035 
00036 int
00037 attribute_hidden
00038 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
00039 {
00040   *output = NULL;
00041   return 0;
00042 }
00043 
00044 void
00045 attribute_hidden
00046 _res_hconf_init (void)
00047 {
00048 }
00049 
00050 #include "../sysdeps/posix/getaddrinfo.c"
00051 
00052 service_user *__nss_hosts_database attribute_hidden;
00053 
00054 
00055 /* This is the beginning of the real test code.  The above defines
00056    (among other things) the function rfc3484_sort.  */
00057 
00058 
00059 #if __BYTE_ORDER == __BIG_ENDIAN
00060 # define h(n) n
00061 #else
00062 # define h(n) __bswap_constant_32 (n)
00063 #endif
00064 
00065 struct sockaddr_in addrs[] =
00066 {
00067   { .sin_family = AF_INET, .sin_addr = { h (0xa0a86d1d) } },
00068   { .sin_family = AF_INET, .sin_addr = { h (0xa0a85d03) } },
00069   { .sin_family = AF_INET, .sin_addr = { h (0xa0a82c3d) } },
00070   { .sin_family = AF_INET, .sin_addr = { h (0xa0a86002) } },
00071   { .sin_family = AF_INET, .sin_addr = { h (0xa0a802f3) } },
00072   { .sin_family = AF_INET, .sin_addr = { h (0xa0a80810) } },
00073   { .sin_family = AF_INET, .sin_addr = { h (0xa0a85e02) } },
00074   { .sin_family = AF_INET, .sin_addr = { h (0xac162311) } },
00075   { .sin_family = AF_INET, .sin_addr = { h (0x0a324572) } }
00076 };
00077 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
00078 static struct addrinfo ais[naddrs];
00079 static struct sort_result results[naddrs];
00080 static size_t order[naddrs];
00081 
00082 static const int expected[naddrs] =
00083   {
00084     8, 0, 1, 2, 3, 4, 5, 6, 7
00085   };
00086 
00087 static const struct scopeentry new_scopes[] =
00088   {
00089     { { { 169, 254, 0, 0 } }, h (0xffff0000), 2 },
00090     { { { 127, 0, 0, 0 } }, h (0xff000000), 2 },
00091     { { { 10, 0, 0, 0 } }, h (0xff000000), 5 },
00092     { { { 192, 168, 0, 0 } }, h(0xffff0000), 5 },
00093     { { { 0, 0, 0, 0 } }, h (0x00000000), 14 }
00094   };
00095 
00096 
00097 ssize_t
00098 __getline (char **lineptr, size_t *n, FILE *s)
00099 {
00100   *lineptr = NULL;
00101   *n = 0;
00102   return 0;
00103 }
00104 
00105 
00106 static int
00107 do_test (void)
00108 {
00109   labels = default_labels;
00110   precedence = default_precedence;
00111   scopes= new_scopes;
00112 
00113   struct sockaddr_in so;
00114   so.sin_family = AF_INET;
00115   so.sin_addr.s_addr = h (0x0aa85f19);
00116 
00117   for (int i = 0; i < naddrs; ++i)
00118     {
00119       ais[i].ai_family = AF_INET;
00120       ais[i].ai_addr = (struct sockaddr *) &addrs[i];
00121       results[i].dest_addr = &ais[i];
00122       results[i].got_source_addr = true;
00123       memcpy(&results[i].source_addr, &so, sizeof (so));
00124       results[i].source_addr_len = sizeof (so);
00125       results[i].source_addr_flags = 0;
00126       results[i].prefixlen = 8;
00127       results[i].index = 0;
00128 
00129       order[i] = i;
00130     }
00131 
00132   struct sort_result_combo combo = { .results = results, .nresults = naddrs };
00133   qsort_r (order, naddrs, sizeof (order[0]), rfc3484_sort, &combo);
00134 
00135   int result = 0;
00136   for (int i = 0; i < naddrs; ++i)
00137     {
00138       struct in_addr addr = ((struct sockaddr_in *) (results[order[i]].dest_addr->ai_addr))->sin_addr;
00139 
00140       int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
00141                       sizeof (struct in_addr));
00142       printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
00143       result |= here;
00144     }
00145 
00146   return result;
00147 }
00148 
00149 #define TEST_FUNCTION do_test ()
00150 #include "../test-skeleton.c"