test-ip6-addr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #include <stdio.h>
  24. #include <string.h>
  25. #ifdef __linux__
  26. # include <sys/socket.h>
  27. # include <net/if.h>
  28. #endif
  29. TEST_IMPL(ip6_addr_link_local) {
  30. #ifdef UV_PLATFORM_HAS_IP6_LINK_LOCAL_ADDRESS
  31. char string_address[INET6_ADDRSTRLEN];
  32. uv_interface_address_t* addresses;
  33. uv_interface_address_t* address;
  34. struct sockaddr_in6 addr;
  35. unsigned int iface_index;
  36. const char* device_name;
  37. /* 40 bytes address, 16 bytes device name, plus reserve. */
  38. char scoped_addr[128];
  39. int count;
  40. int ix;
  41. ASSERT(0 == uv_interface_addresses(&addresses, &count));
  42. for (ix = 0; ix < count; ix++) {
  43. address = addresses + ix;
  44. if (address->address.address6.sin6_family != AF_INET6)
  45. continue;
  46. ASSERT(0 == uv_inet_ntop(AF_INET6,
  47. &address->address.address6.sin6_addr,
  48. string_address,
  49. sizeof(string_address)));
  50. /* Skip addresses that are not link-local. */
  51. if (strncmp(string_address, "fe80::", 6) != 0)
  52. continue;
  53. iface_index = address->address.address6.sin6_scope_id;
  54. device_name = address->name;
  55. #ifdef _WIN32
  56. snprintf(scoped_addr,
  57. sizeof(scoped_addr),
  58. "%s%%%d",
  59. string_address,
  60. iface_index);
  61. #else
  62. snprintf(scoped_addr,
  63. sizeof(scoped_addr),
  64. "%s%%%s",
  65. string_address,
  66. device_name);
  67. #endif
  68. LOGF("Testing link-local address %s "
  69. "(iface_index: 0x%02x, device_name: %s)\n",
  70. scoped_addr,
  71. iface_index,
  72. device_name);
  73. ASSERT(0 == uv_ip6_addr(scoped_addr, TEST_PORT, &addr));
  74. LOGF("Got scope_id 0x%02x\n", addr.sin6_scope_id);
  75. ASSERT(iface_index == addr.sin6_scope_id);
  76. }
  77. uv_free_interface_addresses(addresses, count);
  78. MAKE_VALGRIND_HAPPY();
  79. return 0;
  80. #else
  81. RETURN_SKIP("Qualified link-local addresses are not supported.");
  82. #endif
  83. }