Back to index

glibc  2.9
tst-rfc3484-2.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 
00066 ssize_t
00067 __getline (char **lineptr, size_t *n, FILE *s)
00068 {
00069   *lineptr = NULL;
00070   *n = 0;
00071   return 0;
00072 }
00073 
00074 
00075 static int
00076 do_test (void)
00077 {
00078   labels = default_labels;
00079   precedence = default_precedence;
00080   scopes = default_scopes;
00081 
00082   struct sockaddr_in so1;
00083   so1.sin_family = AF_INET;
00084   so1.sin_addr.s_addr = h (0xc0a85f19);
00085 
00086   struct sockaddr_in sa1;
00087   sa1.sin_family = AF_INET;
00088   sa1.sin_addr.s_addr = h (0xe0a85f19);
00089 
00090   struct addrinfo ai1;
00091   ai1.ai_family = AF_INET;
00092   ai1.ai_addr = (struct sockaddr *) &sa1;
00093 
00094   struct sockaddr_in6 so2;
00095   so2.sin6_family = AF_INET6;
00096   so2.sin6_addr.s6_addr32[0] = h (0xfec01234);
00097   so2.sin6_addr.s6_addr32[1] = 1;
00098   so2.sin6_addr.s6_addr32[2] = 1;
00099   so2.sin6_addr.s6_addr32[3] = 1;
00100 
00101   struct sockaddr_in6 sa2;
00102   sa2.sin6_family = AF_INET6;
00103   sa2.sin6_addr.s6_addr32[0] = h (0x07d10001);
00104   sa2.sin6_addr.s6_addr32[1] = 1;
00105   sa2.sin6_addr.s6_addr32[2] = 1;
00106   sa2.sin6_addr.s6_addr32[3] = 1;
00107 
00108   struct addrinfo ai2;
00109   ai2.ai_family = AF_INET6;
00110   ai2.ai_addr = (struct sockaddr *) &sa2;
00111 
00112 
00113   struct sort_result results[2];
00114   size_t order[2];
00115 
00116   results[0].dest_addr = &ai1;
00117   results[0].got_source_addr = true;
00118   results[0].source_addr_len = sizeof (so1);
00119   results[0].source_addr_flags = 0;
00120   results[0].prefixlen = 16;
00121   results[0].index = 0;
00122   memcpy (&results[0].source_addr, &so1, sizeof (so1));
00123   order[0] = 0;
00124 
00125   results[1].dest_addr = &ai2;
00126   results[1].got_source_addr = true;
00127   results[1].source_addr_len = sizeof (so2);
00128   results[1].source_addr_flags = 0;
00129   results[1].prefixlen = 16;
00130   results[1].index = 0;
00131   memcpy (&results[1].source_addr, &so2, sizeof (so2));
00132   order[1] = 1;
00133 
00134 
00135   struct sort_result_combo combo = { .results = results, .nresults = 2 };
00136   qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
00137 
00138   int result = 0;
00139   if (results[order[0]].dest_addr->ai_family == AF_INET6)
00140     {
00141       puts ("wrong order in first test");
00142       result |= 1;
00143     }
00144 
00145 
00146   /* And again, this time with the reverse starting order.  */
00147   results[1].dest_addr = &ai1;
00148   results[1].got_source_addr = true;
00149   results[1].source_addr_len = sizeof (so1);
00150   results[1].source_addr_flags = 0;
00151   results[1].prefixlen = 16;
00152   results[1].index = 0;
00153   memcpy (&results[1].source_addr, &so1, sizeof (so1));
00154   order[1] = 1;
00155 
00156   results[0].dest_addr = &ai2;
00157   results[0].got_source_addr = true;
00158   results[0].source_addr_len = sizeof (so2);
00159   results[0].source_addr_flags = 0;
00160   results[0].prefixlen = 16;
00161   results[0].index = 0;
00162   memcpy (&results[0].source_addr, &so2, sizeof (so2));
00163   order[0] = 0;
00164 
00165 
00166   qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
00167 
00168   if (results[order[0]].dest_addr->ai_family == AF_INET6)
00169     {
00170       puts ("wrong order in second test");
00171       result |= 1;
00172     }
00173 
00174   return result;
00175 }
00176 
00177 #define TEST_FUNCTION do_test ()
00178 #include "../test-skeleton.c"