tst-rfc3484-2.c 4.1 KB

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