aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party
diff options
context:
space:
mode:
authorGravatar apolcyn <apolcyn@google.com>2018-07-30 13:49:09 -0700
committerGravatar GitHub <noreply@github.com>2018-07-30 13:49:09 -0700
commit8fc60d3f309ed53dddc4fcc911c880951a98e601 (patch)
treeaefb1ac001bdfd555d30512dd8066202fe5fa71b /third_party
parenta87b1fb3ef9859528f93d7792afaa121453d91c9 (diff)
parent9896c641860a70e41b006a982e44f40bb3f41053 (diff)
Merge pull request #16163 from apolcyn/revert_revert_windows_breakage
Revert "Revert "Merge pull request #15797 from apolcyn/windows_compile_and_sort""
Diffstat (limited to 'third_party')
-rw-r--r--third_party/address_sorting/address_sorting.c9
-rw-r--r--third_party/address_sorting/address_sorting_windows.c46
-rw-r--r--third_party/address_sorting/include/address_sorting/address_sorting.h3
3 files changed, 53 insertions, 5 deletions
diff --git a/third_party/address_sorting/address_sorting.c b/third_party/address_sorting/address_sorting.c
index e4f3b53799..9aee0a5419 100644
--- a/third_party/address_sorting/address_sorting.c
+++ b/third_party/address_sorting/address_sorting.c
@@ -55,12 +55,17 @@ static const int kIPv6AddrScopeGlobal = 3;
static address_sorting_source_addr_factory* g_current_source_addr_factory =
NULL;
-static int address_sorting_get_source_addr(const address_sorting_address* dest,
- address_sorting_address* source) {
+static bool address_sorting_get_source_addr(const address_sorting_address* dest,
+ address_sorting_address* source) {
return g_current_source_addr_factory->vtable->get_source_addr(
g_current_source_addr_factory, dest, source);
}
+bool address_sorting_get_source_addr_for_testing(
+ const address_sorting_address* dest, address_sorting_address* source) {
+ return address_sorting_get_source_addr(dest, source);
+}
+
static int ipv6_prefix_match_length(const struct sockaddr_in6* sa,
const struct sockaddr_in6* sb) {
unsigned char* a = (unsigned char*)&sa->sin6_addr;
diff --git a/third_party/address_sorting/address_sorting_windows.c b/third_party/address_sorting/address_sorting_windows.c
index b2f5708649..662a88248e 100644
--- a/third_party/address_sorting/address_sorting_windows.c
+++ b/third_party/address_sorting/address_sorting_windows.c
@@ -42,14 +42,54 @@
#if defined(ADDRESS_SORTING_WINDOWS)
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
-/* TODO : Add address sorting functionality to work on windows. */
+static bool windows_source_addr_factory_get_source_addr(
+ address_sorting_source_addr_factory* factory,
+ const address_sorting_address* dest_addr,
+ address_sorting_address* source_addr) {
+ bool source_addr_exists = false;
+ SOCKET s = socket(((struct sockaddr_in6*)dest_addr)->sin6_family, SOCK_DGRAM,
+ IPPROTO_UDP);
+ if (s != INVALID_SOCKET) {
+ if (connect(s, (struct sockaddr*)dest_addr, (int)dest_addr->len) == 0) {
+ address_sorting_address found_source_addr;
+ memset(&found_source_addr, 0, sizeof(found_source_addr));
+ found_source_addr.len = sizeof(found_source_addr.addr);
+ if (getsockname(s, (struct sockaddr*)&found_source_addr.addr,
+ (socklen_t*)&found_source_addr.len) == 0) {
+ source_addr_exists = true;
+ *source_addr = found_source_addr;
+ }
+ }
+ closesocket(s);
+ }
+ return source_addr_exists;
+}
+
+static void windows_source_addr_factory_destroy(
+ address_sorting_source_addr_factory* self) {
+ free(self);
+}
+
+static const address_sorting_source_addr_factory_vtable
+ windows_source_addr_factory_vtable = {
+ windows_source_addr_factory_get_source_addr,
+ windows_source_addr_factory_destroy,
+};
address_sorting_source_addr_factory*
address_sorting_create_source_addr_factory_for_current_platform() {
- abort();
- return NULL;
+ address_sorting_source_addr_factory* factory =
+ malloc(sizeof(address_sorting_source_addr_factory));
+ memset(factory, 0, sizeof(address_sorting_source_addr_factory));
+ factory->vtable = &windows_source_addr_factory_vtable;
+ return factory;
}
#endif // defined(ADDRESS_SORTING_WINDOWS)
diff --git a/third_party/address_sorting/include/address_sorting/address_sorting.h b/third_party/address_sorting/include/address_sorting/address_sorting.h
index f11cd424b5..c58fafe3f7 100644
--- a/third_party/address_sorting/include/address_sorting/address_sorting.h
+++ b/third_party/address_sorting/include/address_sorting/address_sorting.h
@@ -103,6 +103,9 @@ address_sorting_family address_sorting_abstract_get_family(
void address_sorting_override_source_addr_factory_for_testing(
address_sorting_source_addr_factory* factory);
+bool address_sorting_get_source_addr_for_testing(
+ const address_sorting_address* dest, address_sorting_address* source);
+
#ifdef __cplusplus
}
#endif