Create a macro to allow custom dualstack socket

pull/22235/head
yang-g 5 years ago
parent 49ede8e078
commit 976fff1797
  1. 1
      BUILD
  2. 1
      BUILD.gn
  3. 2
      CMakeLists.txt
  4. 2
      Makefile
  5. 2
      build_autogenerated.yaml
  6. 1
      config.m4
  7. 1
      config.w32
  8. 1
      gRPC-Core.podspec
  9. 1
      grpc.gemspec
  10. 2
      grpc.gyp
  11. 1
      package.xml
  12. 47
      src/core/lib/iomgr/dualstack_socket_posix.cc
  13. 19
      src/core/lib/iomgr/socket_utils_common_posix.cc
  14. 3
      src/core/lib/iomgr/socket_utils_posix.h
  15. 1
      src/python/grpcio/grpc_core_dependencies.py
  16. 1
      tools/doxygen/Doxyfile.c++.internal
  17. 1
      tools/doxygen/Doxyfile.core.internal

@ -714,6 +714,7 @@ grpc_cc_library(
"src/core/lib/iomgr/call_combiner.cc",
"src/core/lib/iomgr/cfstream_handle.cc",
"src/core/lib/iomgr/combiner.cc",
"src/core/lib/iomgr/dualstack_socket_posix.cc",
"src/core/lib/iomgr/endpoint.cc",
"src/core/lib/iomgr/endpoint_cfstream.cc",
"src/core/lib/iomgr/endpoint_pair_posix.cc",

@ -582,6 +582,7 @@ config("grpc_config") {
"src/core/lib/iomgr/closure.h",
"src/core/lib/iomgr/combiner.cc",
"src/core/lib/iomgr/combiner.h",
"src/core/lib/iomgr/dualstack_socket_posix.cc",
"src/core/lib/iomgr/dynamic_annotations.h",
"src/core/lib/iomgr/endpoint.cc",
"src/core/lib/iomgr/endpoint.h",

@ -1495,6 +1495,7 @@ add_library(grpc
src/core/lib/iomgr/call_combiner.cc
src/core/lib/iomgr/cfstream_handle.cc
src/core/lib/iomgr/combiner.cc
src/core/lib/iomgr/dualstack_socket_posix.cc
src/core/lib/iomgr/endpoint.cc
src/core/lib/iomgr/endpoint_cfstream.cc
src/core/lib/iomgr/endpoint_pair_posix.cc
@ -2141,6 +2142,7 @@ add_library(grpc_unsecure
src/core/lib/iomgr/call_combiner.cc
src/core/lib/iomgr/cfstream_handle.cc
src/core/lib/iomgr/combiner.cc
src/core/lib/iomgr/dualstack_socket_posix.cc
src/core/lib/iomgr/endpoint.cc
src/core/lib/iomgr/endpoint_cfstream.cc
src/core/lib/iomgr/endpoint_pair_posix.cc

@ -3840,6 +3840,7 @@ LIBGRPC_SRC = \
src/core/lib/iomgr/call_combiner.cc \
src/core/lib/iomgr/cfstream_handle.cc \
src/core/lib/iomgr/combiner.cc \
src/core/lib/iomgr/dualstack_socket_posix.cc \
src/core/lib/iomgr/endpoint.cc \
src/core/lib/iomgr/endpoint_cfstream.cc \
src/core/lib/iomgr/endpoint_pair_posix.cc \
@ -4461,6 +4462,7 @@ LIBGRPC_UNSECURE_SRC = \
src/core/lib/iomgr/call_combiner.cc \
src/core/lib/iomgr/cfstream_handle.cc \
src/core/lib/iomgr/combiner.cc \
src/core/lib/iomgr/dualstack_socket_posix.cc \
src/core/lib/iomgr/endpoint.cc \
src/core/lib/iomgr/endpoint_cfstream.cc \
src/core/lib/iomgr/endpoint_pair_posix.cc \

@ -923,6 +923,7 @@ libs:
- src/core/lib/iomgr/call_combiner.cc
- src/core/lib/iomgr/cfstream_handle.cc
- src/core/lib/iomgr/combiner.cc
- src/core/lib/iomgr/dualstack_socket_posix.cc
- src/core/lib/iomgr/endpoint.cc
- src/core/lib/iomgr/endpoint_cfstream.cc
- src/core/lib/iomgr/endpoint_pair_posix.cc
@ -1745,6 +1746,7 @@ libs:
- src/core/lib/iomgr/call_combiner.cc
- src/core/lib/iomgr/cfstream_handle.cc
- src/core/lib/iomgr/combiner.cc
- src/core/lib/iomgr/dualstack_socket_posix.cc
- src/core/lib/iomgr/endpoint.cc
- src/core/lib/iomgr/endpoint_cfstream.cc
- src/core/lib/iomgr/endpoint_pair_posix.cc

@ -273,6 +273,7 @@ if test "$PHP_GRPC" != "no"; then
src/core/lib/iomgr/call_combiner.cc \
src/core/lib/iomgr/cfstream_handle.cc \
src/core/lib/iomgr/combiner.cc \
src/core/lib/iomgr/dualstack_socket_posix.cc \
src/core/lib/iomgr/endpoint.cc \
src/core/lib/iomgr/endpoint_cfstream.cc \
src/core/lib/iomgr/endpoint_pair_posix.cc \

@ -242,6 +242,7 @@ if (PHP_GRPC != "no") {
"src\\core\\lib\\iomgr\\call_combiner.cc " +
"src\\core\\lib\\iomgr\\cfstream_handle.cc " +
"src\\core\\lib\\iomgr\\combiner.cc " +
"src\\core\\lib\\iomgr\\dualstack_socket_posix.cc " +
"src\\core\\lib\\iomgr\\endpoint.cc " +
"src\\core\\lib\\iomgr\\endpoint_cfstream.cc " +
"src\\core\\lib\\iomgr\\endpoint_pair_posix.cc " +

@ -632,6 +632,7 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/closure.h',
'src/core/lib/iomgr/combiner.cc',
'src/core/lib/iomgr/combiner.h',
'src/core/lib/iomgr/dualstack_socket_posix.cc',
'src/core/lib/iomgr/dynamic_annotations.h',
'src/core/lib/iomgr/endpoint.cc',
'src/core/lib/iomgr/endpoint.h',

@ -554,6 +554,7 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/iomgr/closure.h )
s.files += %w( src/core/lib/iomgr/combiner.cc )
s.files += %w( src/core/lib/iomgr/combiner.h )
s.files += %w( src/core/lib/iomgr/dualstack_socket_posix.cc )
s.files += %w( src/core/lib/iomgr/dynamic_annotations.h )
s.files += %w( src/core/lib/iomgr/endpoint.cc )
s.files += %w( src/core/lib/iomgr/endpoint.h )

@ -626,6 +626,7 @@
'src/core/lib/iomgr/call_combiner.cc',
'src/core/lib/iomgr/cfstream_handle.cc',
'src/core/lib/iomgr/combiner.cc',
'src/core/lib/iomgr/dualstack_socket_posix.cc',
'src/core/lib/iomgr/endpoint.cc',
'src/core/lib/iomgr/endpoint_cfstream.cc',
'src/core/lib/iomgr/endpoint_pair_posix.cc',
@ -1108,6 +1109,7 @@
'src/core/lib/iomgr/call_combiner.cc',
'src/core/lib/iomgr/cfstream_handle.cc',
'src/core/lib/iomgr/combiner.cc',
'src/core/lib/iomgr/dualstack_socket_posix.cc',
'src/core/lib/iomgr/endpoint.cc',
'src/core/lib/iomgr/endpoint_cfstream.cc',
'src/core/lib/iomgr/endpoint_pair_posix.cc',

@ -534,6 +534,7 @@
<file baseinstalldir="/" name="src/core/lib/iomgr/closure.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/combiner.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/combiner.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/dualstack_socket_posix.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/dynamic_annotations.h" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/endpoint.cc" role="src" />
<file baseinstalldir="/" name="src/core/lib/iomgr/endpoint.h" role="src" />

@ -0,0 +1,47 @@
/*
*
* Copyright 2020 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <grpc/support/port_platform.h>
#include "src/core/lib/iomgr/port.h"
#ifdef GRPC_POSIX_SOCKET_UTILS_COMMON
#include <netinet/in.h>
#include "src/core/lib/iomgr/socket_utils_posix.h"
#ifndef GRPC_SET_SOCKET_DUALSTACK_CUSTOM
/* This should be 0 in production, but it may be enabled for testing or
debugging purposes, to simulate an environment where IPv6 sockets can't
also speak IPv4. */
int grpc_forbid_dualstack_sockets_for_testing = 0;
int grpc_set_socket_dualstack(int fd) {
if (!grpc_forbid_dualstack_sockets_for_testing) {
const int off = 0;
return 0 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
} else {
/* Force an IPv6-only socket, for testing purposes. */
const int on = 1;
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
return 0;
}
}
#endif // GRPC_SET_SOCKET_DUALSTACK_CUSTOM
#endif // GRPC_POSIX_SOCKET_UTILS_COMMON

@ -396,23 +396,6 @@ int grpc_ipv6_loopback_available(void) {
return g_ipv6_loopback_available;
}
/* This should be 0 in production, but it may be enabled for testing or
debugging purposes, to simulate an environment where IPv6 sockets can't
also speak IPv4. */
int grpc_forbid_dualstack_sockets_for_testing = 0;
static int set_socket_dualstack(int fd) {
if (!grpc_forbid_dualstack_sockets_for_testing) {
const int off = 0;
return 0 == setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
} else {
/* Force an IPv6-only socket, for testing purposes. */
const int on = 1;
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
return 0;
}
}
static grpc_error* error_for_fd(int fd, const grpc_resolved_address* addr) {
if (fd >= 0) return GRPC_ERROR_NONE;
char* addr_str;
@ -452,7 +435,7 @@ grpc_error* grpc_create_dualstack_socket_using_factory(
errno = EAFNOSUPPORT;
}
/* Check if we've got a valid dualstack socket. */
if (*newfd >= 0 && set_socket_dualstack(*newfd)) {
if (*newfd >= 0 && grpc_set_socket_dualstack(*newfd)) {
*dsmode = GRPC_DSMODE_DUALSTACK;
return GRPC_ERROR_NONE;
}

@ -126,6 +126,9 @@ typedef enum grpc_dualstack_mode {
/* Only tests should use this flag. */
extern int grpc_forbid_dualstack_sockets_for_testing;
/* Tries to set the socket to dualstack. Returns 1 on success. */
int grpc_set_socket_dualstack(int fd);
/* Creates a new socket for connecting to (or listening on) an address.
If addr is AF_INET6, this creates an IPv6 socket first. If that fails,

@ -251,6 +251,7 @@ CORE_SOURCE_FILES = [
'src/core/lib/iomgr/call_combiner.cc',
'src/core/lib/iomgr/cfstream_handle.cc',
'src/core/lib/iomgr/combiner.cc',
'src/core/lib/iomgr/dualstack_socket_posix.cc',
'src/core/lib/iomgr/endpoint.cc',
'src/core/lib/iomgr/endpoint_cfstream.cc',
'src/core/lib/iomgr/endpoint_pair_posix.cc',

@ -1517,6 +1517,7 @@ src/core/lib/iomgr/cfstream_handle.h \
src/core/lib/iomgr/closure.h \
src/core/lib/iomgr/combiner.cc \
src/core/lib/iomgr/combiner.h \
src/core/lib/iomgr/dualstack_socket_posix.cc \
src/core/lib/iomgr/dynamic_annotations.h \
src/core/lib/iomgr/endpoint.cc \
src/core/lib/iomgr/endpoint.h \

@ -1329,6 +1329,7 @@ src/core/lib/iomgr/cfstream_handle.h \
src/core/lib/iomgr/closure.h \
src/core/lib/iomgr/combiner.cc \
src/core/lib/iomgr/combiner.h \
src/core/lib/iomgr/dualstack_socket_posix.cc \
src/core/lib/iomgr/dynamic_annotations.h \
src/core/lib/iomgr/endpoint.cc \
src/core/lib/iomgr/endpoint.h \

Loading…
Cancel
Save