Back to index

glibc  2.9
tst-rfc3484.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 (0xc0a86d1d) } },
00068   { .sin_family = AF_INET, .sin_addr = { h (0xc0a85d03) } },
00069   { .sin_family = AF_INET, .sin_addr = { h (0xc0a82c3d) } },
00070   { .sin_family = AF_INET, .sin_addr = { h (0xc0a86002) } },
00071   { .sin_family = AF_INET, .sin_addr = { h (0xc0a802f3) } },
00072   { .sin_family = AF_INET, .sin_addr = { h (0xc0a80810) } },
00073   { .sin_family = AF_INET, .sin_addr = { h (0xc0a85e02) } }
00074 };
00075 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
00076 static struct addrinfo ais[naddrs];
00077 static struct sort_result results[naddrs];
00078 static size_t order[naddrs];
00079 
00080 static int expected[naddrs] =
00081   {
00082     6, 1, 0, 3, 2, 4, 5
00083   };
00084 
00085 
00086 ssize_t
00087 __getline (char **lineptr, size_t *n, FILE *s)
00088 {
00089   *lineptr = NULL;
00090   *n = 0;
00091   return 0;
00092 }
00093 
00094 
00095 static int
00096 do_test (void)
00097 {
00098   labels = default_labels;
00099   precedence = default_precedence;
00100   scopes= default_scopes;
00101 
00102   struct sockaddr_in so;
00103   so.sin_family = AF_INET;
00104   so.sin_addr.s_addr = h (0xc0a85f19);
00105 
00106   for (int i = 0; i < naddrs; ++i)
00107     {
00108       ais[i].ai_family = AF_INET;
00109       ais[i].ai_addr = (struct sockaddr *) &addrs[i];
00110       results[i].dest_addr = &ais[i];
00111       results[i].got_source_addr = true;
00112       memcpy(&results[i].source_addr, &so, sizeof (so));
00113       results[i].source_addr_len = sizeof (so);
00114       results[i].source_addr_flags = 0;
00115       results[i].prefixlen = 8;
00116       results[i].index = 0;
00117 
00118       order[i] = i;
00119     }
00120 
00121   struct sort_result_combo combo = { .results = results, .nresults = naddrs };
00122   qsort_r (order, naddrs, sizeof (order[0]), rfc3484_sort, &combo);
00123 
00124   int result = 0;
00125   for (int i = 0; i < naddrs; ++i)
00126     {
00127       struct in_addr addr = ((struct sockaddr_in *) (results[order[i]].dest_addr->ai_addr))->sin_addr;
00128 
00129       int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
00130                       sizeof (struct in_addr));
00131       printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
00132       result |= here;
00133     }
00134 
00135   return result;
00136 }
00137 
00138 #define TEST_FUNCTION do_test ()
00139 #include "../test-skeleton.c"