From d21ee150765c73876992bbf06c716b4600ba047f Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Thu, 28 Dec 2017 18:09:19 -0800 Subject: Stop relying on certain ipv6 macros. Also bazel build updates. Fix error just made in bazel/grpc_build_system.bzl Fix merge conflict support/env -> gpr/env --- third_party/address_sorting/BUILD | 5 ++- third_party/address_sorting/address_sorting.bzl | 38 +++++++++++++++++ third_party/address_sorting/address_sorting.c | 49 +++++++++++++++++----- .../address_sorting/address_sorting_internal.h | 3 ++ 4 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 third_party/address_sorting/address_sorting.bzl (limited to 'third_party') diff --git a/third_party/address_sorting/BUILD b/third_party/address_sorting/BUILD index 9462829766..defa0851aa 100644 --- a/third_party/address_sorting/BUILD +++ b/third_party/address_sorting/BUILD @@ -36,11 +36,13 @@ package( ], ) +load(":address_sorting.bzl", "address_sorting_cc_library") + licenses(["notice"]) # BSD exports_files(["LICENSE"]) -cc_library( +address_sorting_cc_library( name = "address_sorting", srcs = [ "address_sorting.c", @@ -51,6 +53,7 @@ cc_library( "address_sorting.h", "address_sorting_internal.h", ], + copts = ["-std=c99"], includes = [ ".", ], diff --git a/third_party/address_sorting/address_sorting.bzl b/third_party/address_sorting/address_sorting.bzl new file mode 100644 index 0000000000..25d008442b --- /dev/null +++ b/third_party/address_sorting/address_sorting.bzl @@ -0,0 +1,38 @@ +# $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */ +# $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ +# +# Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. Neither the name of the project nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE. + +def address_sorting_cc_library(name, srcs, hdrs, copts, includes): + native.cc_library( + name = name, + srcs = srcs, + hdrs = hdrs, + copts = copts, + includes = includes, + ) diff --git a/third_party/address_sorting/address_sorting.c b/third_party/address_sorting/address_sorting.c index 977567f36a..ec46099bec 100644 --- a/third_party/address_sorting/address_sorting.c +++ b/third_party/address_sorting/address_sorting.c @@ -79,6 +79,33 @@ static int ipv6_prefix_match_length(const struct sockaddr_in6* sa, return cur_bit; } +static int in6_is_addr_loopback(const struct in6_addr* ipv6_address) { + uint32_t* bits32 = (uint32_t*)ipv6_address; + return bits32[0] == 0 && bits32[1] == 0 && bits32[2] == 0 && + bits32[3] == htonl(1); +} + +static int in6_is_addr_v4mapped(const struct in6_addr* ipv6_address) { + uint32_t* bits32 = (uint32_t*)ipv6_address; + return bits32[0] == 0 && bits32[1] == 0 && bits32[2] == htonl(0x0000ffff); +} + +static int in6_is_addr_v4compat(const struct in6_addr* ipv6_address) { + uint32_t* bits32 = (uint32_t*)ipv6_address; + return bits32[0] == 0 && bits32[1] == 0 && bits32[2] == 0 && bits32[3] != 0 && + bits32[3] != htonl(1); +} + +static int in6_is_addr_sitelocal(const struct in6_addr* ipv6_address) { + uint8_t* bytes = (uint8_t*)ipv6_address; + return bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0xc0; +} + +static int in6_is_addr_linklocal(const struct in6_addr* ipv6_address) { + uint8_t* bytes = (uint8_t*)ipv6_address; + return bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0x80; +} + static int in6_is_addr_6to4(const struct in6_addr* ipv6_address) { uint8_t* bytes = (uint8_t*)ipv6_address; return bytes[0] == 0x20 && bytes[1] == 0x02; @@ -121,9 +148,9 @@ static int get_label_value(const address_sorting_address* resolved_addr) { return 1; } struct sockaddr_in6* ipv6_addr = (struct sockaddr_in6*)&resolved_addr->addr; - if (IN6_IS_ADDR_LOOPBACK(&ipv6_addr->sin6_addr)) { + if (in6_is_addr_loopback(&ipv6_addr->sin6_addr)) { return 0; - } else if (IN6_IS_ADDR_V4MAPPED(&ipv6_addr->sin6_addr)) { + } else if (in6_is_addr_v4mapped(&ipv6_addr->sin6_addr)) { return 4; } else if (in6_is_addr_6to4(&ipv6_addr->sin6_addr)) { return 2; @@ -131,9 +158,9 @@ static int get_label_value(const address_sorting_address* resolved_addr) { return 5; } else if (in6_is_addr_ula(&ipv6_addr->sin6_addr)) { return 13; - } else if (IN6_IS_ADDR_V4COMPAT(&ipv6_addr->sin6_addr)) { + } else if (in6_is_addr_v4compat(&ipv6_addr->sin6_addr)) { return 3; - } else if (IN6_IS_ADDR_SITELOCAL(&ipv6_addr->sin6_addr)) { + } else if (in6_is_addr_sitelocal(&ipv6_addr->sin6_addr)) { return 11; } else if (in6_is_addr_6bone(&ipv6_addr->sin6_addr)) { return 12; @@ -150,9 +177,9 @@ static int get_precedence_value(const address_sorting_address* resolved_addr) { return 1; } struct sockaddr_in6* ipv6_addr = (struct sockaddr_in6*)&resolved_addr->addr; - if (IN6_IS_ADDR_LOOPBACK(&ipv6_addr->sin6_addr)) { + if (in6_is_addr_loopback(&ipv6_addr->sin6_addr)) { return 50; - } else if (IN6_IS_ADDR_V4MAPPED(&ipv6_addr->sin6_addr)) { + } else if (in6_is_addr_v4mapped(&ipv6_addr->sin6_addr)) { return 35; } else if (in6_is_addr_6to4(&ipv6_addr->sin6_addr)) { return 30; @@ -160,8 +187,8 @@ static int get_precedence_value(const address_sorting_address* resolved_addr) { return 5; } else if (in6_is_addr_ula(&ipv6_addr->sin6_addr)) { return 3; - } else if (IN6_IS_ADDR_V4COMPAT(&ipv6_addr->sin6_addr) || - IN6_IS_ADDR_SITELOCAL(&ipv6_addr->sin6_addr) || + } else if (in6_is_addr_v4compat(&ipv6_addr->sin6_addr) || + in6_is_addr_sitelocal(&ipv6_addr->sin6_addr) || in6_is_addr_6bone(&ipv6_addr->sin6_addr)) { return 1; } @@ -175,11 +202,11 @@ static int sockaddr_get_scope(const address_sorting_address* resolved_addr) { } else if (address_sorting_abstract_get_family(resolved_addr) == ADDRESS_SORTING_AF_INET6) { struct sockaddr_in6* ipv6_addr = (struct sockaddr_in6*)&resolved_addr->addr; - if (IN6_IS_ADDR_LOOPBACK(&ipv6_addr->sin6_addr) || - IN6_IS_ADDR_LINKLOCAL(&ipv6_addr->sin6_addr)) { + if (in6_is_addr_loopback(&ipv6_addr->sin6_addr) || + in6_is_addr_linklocal(&ipv6_addr->sin6_addr)) { return kIPv6AddrScopeLinkLocal; } - if (IN6_IS_ADDR_SITELOCAL(&ipv6_addr->sin6_addr)) { + if (in6_is_addr_sitelocal(&ipv6_addr->sin6_addr)) { return kIPv6AddrScopeSiteLocal; } return kIPv6AddrScopeGlobal; diff --git a/third_party/address_sorting/address_sorting_internal.h b/third_party/address_sorting/address_sorting_internal.h index 9712c96599..ee366fef1c 100644 --- a/third_party/address_sorting/address_sorting_internal.h +++ b/third_party/address_sorting/address_sorting_internal.h @@ -51,7 +51,10 @@ /* Workaround for issue described in * * https://bugs.launchpad.net/ubuntu/+source/eglibc/+bug/1187301 */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif +#include #include #include #define ADDRESS_SORTING_POSIX 1 -- cgit v1.2.3