diff --git a/BUILD b/BUILD
index bad6f3f0755..9d9cd49c170 100644
--- a/BUILD
+++ b/BUILD
@@ -206,6 +206,7 @@ cc_library(
"src/core/lib/iomgr/sockaddr_posix.h",
"src/core/lib/iomgr/sockaddr_utils.h",
"src/core/lib/iomgr/sockaddr_windows.h",
+ "src/core/lib/iomgr/socket_mutator.h",
"src/core/lib/iomgr/socket_utils_posix.h",
"src/core/lib/iomgr/socket_windows.h",
"src/core/lib/iomgr/tcp_client.h",
@@ -363,6 +364,7 @@ cc_library(
"src/core/lib/iomgr/resolve_address_posix.c",
"src/core/lib/iomgr/resolve_address_windows.c",
"src/core/lib/iomgr/sockaddr_utils.c",
+ "src/core/lib/iomgr/socket_mutator.c",
"src/core/lib/iomgr/socket_utils_common_posix.c",
"src/core/lib/iomgr/socket_utils_linux.c",
"src/core/lib/iomgr/socket_utils_posix.c",
@@ -607,6 +609,7 @@ cc_library(
"src/core/lib/iomgr/sockaddr_posix.h",
"src/core/lib/iomgr/sockaddr_utils.h",
"src/core/lib/iomgr/sockaddr_windows.h",
+ "src/core/lib/iomgr/socket_mutator.h",
"src/core/lib/iomgr/socket_utils_posix.h",
"src/core/lib/iomgr/socket_windows.h",
"src/core/lib/iomgr/tcp_client.h",
@@ -749,6 +752,7 @@ cc_library(
"src/core/lib/iomgr/resolve_address_posix.c",
"src/core/lib/iomgr/resolve_address_windows.c",
"src/core/lib/iomgr/sockaddr_utils.c",
+ "src/core/lib/iomgr/socket_mutator.c",
"src/core/lib/iomgr/socket_utils_common_posix.c",
"src/core/lib/iomgr/socket_utils_linux.c",
"src/core/lib/iomgr/socket_utils_posix.c",
@@ -963,6 +967,7 @@ cc_library(
"src/core/lib/iomgr/sockaddr_posix.h",
"src/core/lib/iomgr/sockaddr_utils.h",
"src/core/lib/iomgr/sockaddr_windows.h",
+ "src/core/lib/iomgr/socket_mutator.h",
"src/core/lib/iomgr/socket_utils_posix.h",
"src/core/lib/iomgr/socket_windows.h",
"src/core/lib/iomgr/tcp_client.h",
@@ -1097,6 +1102,7 @@ cc_library(
"src/core/lib/iomgr/resolve_address_posix.c",
"src/core/lib/iomgr/resolve_address_windows.c",
"src/core/lib/iomgr/sockaddr_utils.c",
+ "src/core/lib/iomgr/socket_mutator.c",
"src/core/lib/iomgr/socket_utils_common_posix.c",
"src/core/lib/iomgr/socket_utils_linux.c",
"src/core/lib/iomgr/socket_utils_posix.c",
@@ -1860,6 +1866,7 @@ objc_library(
"src/core/lib/iomgr/resolve_address_posix.c",
"src/core/lib/iomgr/resolve_address_windows.c",
"src/core/lib/iomgr/sockaddr_utils.c",
+ "src/core/lib/iomgr/socket_mutator.c",
"src/core/lib/iomgr/socket_utils_common_posix.c",
"src/core/lib/iomgr/socket_utils_linux.c",
"src/core/lib/iomgr/socket_utils_posix.c",
@@ -2083,6 +2090,7 @@ objc_library(
"src/core/lib/iomgr/sockaddr_posix.h",
"src/core/lib/iomgr/sockaddr_utils.h",
"src/core/lib/iomgr/sockaddr_windows.h",
+ "src/core/lib/iomgr/socket_mutator.h",
"src/core/lib/iomgr/socket_utils_posix.h",
"src/core/lib/iomgr/socket_windows.h",
"src/core/lib/iomgr/tcp_client.h",
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c4191521bd1..4ff0180d0bf 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -330,6 +330,7 @@ add_library(grpc
src/core/lib/iomgr/resolve_address_posix.c
src/core/lib/iomgr/resolve_address_windows.c
src/core/lib/iomgr/sockaddr_utils.c
+ src/core/lib/iomgr/socket_mutator.c
src/core/lib/iomgr/socket_utils_common_posix.c
src/core/lib/iomgr/socket_utils_linux.c
src/core/lib/iomgr/socket_utils_posix.c
@@ -589,6 +590,7 @@ add_library(grpc_cronet
src/core/lib/iomgr/resolve_address_posix.c
src/core/lib/iomgr/resolve_address_windows.c
src/core/lib/iomgr/sockaddr_utils.c
+ src/core/lib/iomgr/socket_mutator.c
src/core/lib/iomgr/socket_utils_common_posix.c
src/core/lib/iomgr/socket_utils_linux.c
src/core/lib/iomgr/socket_utils_posix.c
@@ -820,6 +822,7 @@ add_library(grpc_unsecure
src/core/lib/iomgr/resolve_address_posix.c
src/core/lib/iomgr/resolve_address_windows.c
src/core/lib/iomgr/sockaddr_utils.c
+ src/core/lib/iomgr/socket_mutator.c
src/core/lib/iomgr/socket_utils_common_posix.c
src/core/lib/iomgr/socket_utils_linux.c
src/core/lib/iomgr/socket_utils_posix.c
diff --git a/Makefile b/Makefile
index 62c65822b0f..abdbbc343cf 100644
--- a/Makefile
+++ b/Makefile
@@ -2574,6 +2574,7 @@ LIBGRPC_SRC = \
src/core/lib/iomgr/resolve_address_posix.c \
src/core/lib/iomgr/resolve_address_windows.c \
src/core/lib/iomgr/sockaddr_utils.c \
+ src/core/lib/iomgr/socket_mutator.c \
src/core/lib/iomgr/socket_utils_common_posix.c \
src/core/lib/iomgr/socket_utils_linux.c \
src/core/lib/iomgr/socket_utils_posix.c \
@@ -2851,6 +2852,7 @@ LIBGRPC_CRONET_SRC = \
src/core/lib/iomgr/resolve_address_posix.c \
src/core/lib/iomgr/resolve_address_windows.c \
src/core/lib/iomgr/sockaddr_utils.c \
+ src/core/lib/iomgr/socket_mutator.c \
src/core/lib/iomgr/socket_utils_common_posix.c \
src/core/lib/iomgr/socket_utils_linux.c \
src/core/lib/iomgr/socket_utils_posix.c \
@@ -3118,6 +3120,7 @@ LIBGRPC_TEST_UTIL_SRC = \
src/core/lib/iomgr/resolve_address_posix.c \
src/core/lib/iomgr/resolve_address_windows.c \
src/core/lib/iomgr/sockaddr_utils.c \
+ src/core/lib/iomgr/socket_mutator.c \
src/core/lib/iomgr/socket_utils_common_posix.c \
src/core/lib/iomgr/socket_utils_linux.c \
src/core/lib/iomgr/socket_utils_posix.c \
@@ -3312,6 +3315,7 @@ LIBGRPC_UNSECURE_SRC = \
src/core/lib/iomgr/resolve_address_posix.c \
src/core/lib/iomgr/resolve_address_windows.c \
src/core/lib/iomgr/sockaddr_utils.c \
+ src/core/lib/iomgr/socket_mutator.c \
src/core/lib/iomgr/socket_utils_common_posix.c \
src/core/lib/iomgr/socket_utils_linux.c \
src/core/lib/iomgr/socket_utils_posix.c \
diff --git a/binding.gyp b/binding.gyp
index 58edda2e63e..dcc4fae4553 100644
--- a/binding.gyp
+++ b/binding.gyp
@@ -605,6 +605,7 @@
'src/core/lib/iomgr/resolve_address_posix.c',
'src/core/lib/iomgr/resolve_address_windows.c',
'src/core/lib/iomgr/sockaddr_utils.c',
+ 'src/core/lib/iomgr/socket_mutator.c',
'src/core/lib/iomgr/socket_utils_common_posix.c',
'src/core/lib/iomgr/socket_utils_linux.c',
'src/core/lib/iomgr/socket_utils_posix.c',
diff --git a/build.yaml b/build.yaml
index 584084ff865..79b00d8c655 100644
--- a/build.yaml
+++ b/build.yaml
@@ -210,6 +210,7 @@ filegroups:
- src/core/lib/iomgr/sockaddr_posix.h
- src/core/lib/iomgr/sockaddr_utils.h
- src/core/lib/iomgr/sockaddr_windows.h
+ - src/core/lib/iomgr/socket_mutator.h
- src/core/lib/iomgr/socket_utils_posix.h
- src/core/lib/iomgr/socket_windows.h
- src/core/lib/iomgr/tcp_client.h
@@ -290,6 +291,7 @@ filegroups:
- src/core/lib/iomgr/resolve_address_posix.c
- src/core/lib/iomgr/resolve_address_windows.c
- src/core/lib/iomgr/sockaddr_utils.c
+ - src/core/lib/iomgr/socket_mutator.c
- src/core/lib/iomgr/socket_utils_common_posix.c
- src/core/lib/iomgr/socket_utils_linux.c
- src/core/lib/iomgr/socket_utils_posix.c
diff --git a/config.m4 b/config.m4
index 0f103655a84..b7ef4a991df 100644
--- a/config.m4
+++ b/config.m4
@@ -124,6 +124,7 @@ if test "$PHP_GRPC" != "no"; then
src/core/lib/iomgr/resolve_address_posix.c \
src/core/lib/iomgr/resolve_address_windows.c \
src/core/lib/iomgr/sockaddr_utils.c \
+ src/core/lib/iomgr/socket_mutator.c \
src/core/lib/iomgr/socket_utils_common_posix.c \
src/core/lib/iomgr/socket_utils_linux.c \
src/core/lib/iomgr/socket_utils_posix.c \
diff --git a/gRPC-Core.podspec b/gRPC-Core.podspec
index 6886bdfb5aa..5bd76ab10e4 100644
--- a/gRPC-Core.podspec
+++ b/gRPC-Core.podspec
@@ -293,6 +293,7 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/sockaddr_posix.h',
'src/core/lib/iomgr/sockaddr_utils.h',
'src/core/lib/iomgr/sockaddr_windows.h',
+ 'src/core/lib/iomgr/socket_mutator.h',
'src/core/lib/iomgr/socket_utils_posix.h',
'src/core/lib/iomgr/socket_windows.h',
'src/core/lib/iomgr/tcp_client.h',
@@ -454,6 +455,7 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/resolve_address_posix.c',
'src/core/lib/iomgr/resolve_address_windows.c',
'src/core/lib/iomgr/sockaddr_utils.c',
+ 'src/core/lib/iomgr/socket_mutator.c',
'src/core/lib/iomgr/socket_utils_common_posix.c',
'src/core/lib/iomgr/socket_utils_linux.c',
'src/core/lib/iomgr/socket_utils_posix.c',
@@ -666,6 +668,7 @@ Pod::Spec.new do |s|
'src/core/lib/iomgr/sockaddr_posix.h',
'src/core/lib/iomgr/sockaddr_utils.h',
'src/core/lib/iomgr/sockaddr_windows.h',
+ 'src/core/lib/iomgr/socket_mutator.h',
'src/core/lib/iomgr/socket_utils_posix.h',
'src/core/lib/iomgr/socket_windows.h',
'src/core/lib/iomgr/tcp_client.h',
diff --git a/grpc.gemspec b/grpc.gemspec
index 6079ea2aef5..c3601ec3129 100755
--- a/grpc.gemspec
+++ b/grpc.gemspec
@@ -213,6 +213,7 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/iomgr/sockaddr_posix.h )
s.files += %w( src/core/lib/iomgr/sockaddr_utils.h )
s.files += %w( src/core/lib/iomgr/sockaddr_windows.h )
+ s.files += %w( src/core/lib/iomgr/socket_mutator.h )
s.files += %w( src/core/lib/iomgr/socket_utils_posix.h )
s.files += %w( src/core/lib/iomgr/socket_windows.h )
s.files += %w( src/core/lib/iomgr/tcp_client.h )
@@ -374,6 +375,7 @@ Gem::Specification.new do |s|
s.files += %w( src/core/lib/iomgr/resolve_address_posix.c )
s.files += %w( src/core/lib/iomgr/resolve_address_windows.c )
s.files += %w( src/core/lib/iomgr/sockaddr_utils.c )
+ s.files += %w( src/core/lib/iomgr/socket_mutator.c )
s.files += %w( src/core/lib/iomgr/socket_utils_common_posix.c )
s.files += %w( src/core/lib/iomgr/socket_utils_linux.c )
s.files += %w( src/core/lib/iomgr/socket_utils_posix.c )
diff --git a/include/grpc++/support/channel_arguments.h b/include/grpc++/support/channel_arguments.h
index ae243939e9a..a17557f59d7 100644
--- a/include/grpc++/support/channel_arguments.h
+++ b/include/grpc++/support/channel_arguments.h
@@ -77,6 +77,9 @@ class ChannelArguments {
/// Set the compression algorithm for the channel.
void SetCompressionAlgorithm(grpc_compression_algorithm algorithm);
+ /// Set the socket mutator for the channel.
+ void SetSocketMutator(grpc_socket_mutator* mutator);
+
/// The given string will be sent at the front of the user agent string.
void SetUserAgentPrefix(const grpc::string& user_agent_prefix);
diff --git a/include/grpc/impl/codegen/grpc_types.h b/include/grpc/impl/codegen/grpc_types.h
index 6621119d255..800d9fe3d3a 100644
--- a/include/grpc/impl/codegen/grpc_types.h
+++ b/include/grpc/impl/codegen/grpc_types.h
@@ -83,6 +83,9 @@ typedef struct grpc_server grpc_server;
can have messages written to it and read from it. */
typedef struct grpc_call grpc_call;
+/** The Socket Mutator interface allows changes on socket options */
+typedef struct grpc_socket_mutator grpc_socket_mutator;
+
/** Type specifier for grpc_arg */
typedef enum {
GRPC_ARG_STRING,
@@ -205,16 +208,6 @@ typedef struct {
#define GRPC_ARG_SOCKET_MUTATOR "grpc.socket_mutator"
/** \} */
-typedef struct grpc_socket_mutator grpc_socket_mutator;
-
-typedef struct grpc_socket_mutator_vtable {
- bool (*mutate_fd)(int fd, grpc_socket_mutator *mutator);
-} grpc_socket_mutator_vtable;
-
-struct grpc_socket_mutator {
- const grpc_socket_mutator_vtable *vtable;
-};
-
/** Result of a grpc call. If the caller satisfies the prerequisites of a
particular operation, the grpc_call_error returned will be GRPC_CALL_OK.
Receiving any other value listed here is an indication of a bug in the
diff --git a/package.xml b/package.xml
index 7bac6957871..2827faa22d3 100644
--- a/package.xml
+++ b/package.xml
@@ -220,6 +220,7 @@
+
@@ -381,6 +382,7 @@
+
diff --git a/src/core/lib/channel/channel_args.c b/src/core/lib/channel/channel_args.c
index 3a56b1ff205..770b4e0a791 100644
--- a/src/core/lib/channel/channel_args.c
+++ b/src/core/lib/channel/channel_args.c
@@ -261,6 +261,12 @@ uint32_t grpc_channel_args_compression_algorithm_get_states(
}
}
+grpc_channel_args *grpc_channel_args_set_socket_mutator(
+ grpc_channel_args *a, grpc_socket_mutator *mutator) {
+ grpc_arg tmp = grpc_socket_mutator_to_arg(mutator);
+ return grpc_channel_args_copy_and_add(a, &tmp, 1);
+}
+
int grpc_channel_args_compare(const grpc_channel_args *a,
const grpc_channel_args *b) {
int c = GPR_ICMP(a->num_args, b->num_args);
diff --git a/src/core/lib/channel/channel_args.h b/src/core/lib/channel/channel_args.h
index 586a296d1f6..8a3576247c5 100644
--- a/src/core/lib/channel/channel_args.h
+++ b/src/core/lib/channel/channel_args.h
@@ -23,8 +23,8 @@
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER 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
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* 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.
@@ -36,6 +36,7 @@
#include
#include
+#include "src/core/lib/iomgr/socket_mutator.h"
// Channel args are intentionally immutable, to avoid the need for locking.
@@ -89,6 +90,13 @@ uint32_t grpc_channel_args_compression_algorithm_get_states(
int grpc_channel_args_compare(const grpc_channel_args *a,
const grpc_channel_args *b);
+/** Returns a channel arg instance with socket mutator added. The socket mutator
+ * will perform its mutate_fd method on all file descriptors used by the
+ * channel.
+ * If \a a is non-MULL, its args are copied. */
+grpc_channel_args *grpc_channel_args_set_socket_mutator(
+ grpc_channel_args *a, grpc_socket_mutator *mutator);
+
typedef struct grpc_integer_options {
int default_value; // Return this if value is outside of expected bounds.
int min_value;
diff --git a/src/core/lib/iomgr/socket_mutator.c b/src/core/lib/iomgr/socket_mutator.c
new file mode 100644
index 00000000000..3f66cb100da
--- /dev/null
+++ b/src/core/lib/iomgr/socket_mutator.c
@@ -0,0 +1,81 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Google Inc. 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 COPYRIGHT HOLDERS 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 COPYRIGHT
+ * OWNER 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.
+ *
+ */
+
+#include "src/core/lib/iomgr/socket_mutator.h"
+
+#include
+#include
+#include
+
+void grpc_socket_mutator_init(grpc_socket_mutator *mutator,
+ const grpc_socket_mutator_vtable *vtable) {
+ mutator->vtable = vtable;
+ gpr_ref_init(&mutator->refcount, 1);
+}
+
+grpc_socket_mutator *grpc_socket_mutator_ref(grpc_socket_mutator *mutator) {
+ gpr_ref(&mutator->refcount);
+ return mutator;
+}
+
+bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator *mutator, int fd) {
+ return mutator->vtable->mutate_fd(fd, mutator);
+}
+
+void grpc_socket_mutator_unref(grpc_socket_mutator *mutator) {
+ if (gpr_unref(&mutator->refcount)) {
+ mutator->vtable->destory(mutator);
+ }
+}
+
+static void *socket_mutator_arg_copy(void *p) {
+ return grpc_socket_mutator_ref(p);
+}
+
+static void socket_mutator_arg_destroy(void *p) {
+ grpc_socket_mutator_unref(p);
+}
+
+static int socket_mutator_cmp(void *a, void *b) { return GPR_ICMP(a, b); }
+
+static const grpc_arg_pointer_vtable socket_mutator_arg_vtable = {
+ socket_mutator_arg_copy, socket_mutator_arg_destroy, socket_mutator_cmp};
+
+grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator *mutator) {
+ grpc_arg arg;
+ arg.type = GRPC_ARG_POINTER;
+ arg.key = GRPC_ARG_SOCKET_MUTATOR;
+ arg.value.pointer.vtable = &socket_mutator_arg_vtable;
+ arg.value.pointer.p = mutator;
+ return arg;
+}
diff --git a/src/core/lib/iomgr/socket_mutator.h b/src/core/lib/iomgr/socket_mutator.h
new file mode 100644
index 00000000000..5200f52d3a1
--- /dev/null
+++ b/src/core/lib/iomgr/socket_mutator.h
@@ -0,0 +1,75 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of Google Inc. 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 COPYRIGHT HOLDERS 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 COPYRIGHT
+ * OWNER 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.
+ *
+ */
+
+#ifndef GRPC_CORE_LIB_IOMGR_SOCKET_MUTATOR_H
+#define GRPC_CORE_LIB_IOMGR_SOCKET_MUTATOR_H
+
+#include
+#include
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** The virtual table of grpc_socket_mutator */
+typedef struct {
+ /** Mutates the socket opitons of \a fd */
+ bool (*mutate_fd)(int fd, grpc_socket_mutator *mutator);
+ /** Destroys the socket mutator instance */
+ void (*destory)(grpc_socket_mutator *mutator);
+} grpc_socket_mutator_vtable;
+
+/** The Socket Mutator interface allows changes on socket options */
+struct grpc_socket_mutator {
+ const grpc_socket_mutator_vtable *vtable;
+ gpr_refcount refcount;
+};
+
+/** called by concrete implementations to initialize the base struct */
+void grpc_socket_mutator_init(grpc_socket_mutator *mutator,
+ const grpc_socket_mutator_vtable *vtable);
+
+/** Wrap \a mutator as a grpc_arg */
+grpc_arg grpc_socket_mutator_to_arg(grpc_socket_mutator *mutator);
+
+/** Perform the file descriptor mutation operation of \a mutator on \a fd */
+bool grpc_socket_mutator_mutate_fd(grpc_socket_mutator *mutator, int fd);
+
+grpc_socket_mutator *grpc_socket_mutator_ref(grpc_socket_mutator *mutator);
+void grpc_socket_mutator_unref(grpc_socket_mutator *mutator);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GRPC_CORE_LIB_IOMGR_SOCKET_MUTATOR_H */
diff --git a/src/core/lib/iomgr/socket_utils_common_posix.c b/src/core/lib/iomgr/socket_utils_common_posix.c
index 8c33ba37546..9c67ef89401 100644
--- a/src/core/lib/iomgr/socket_utils_common_posix.c
+++ b/src/core/lib/iomgr/socket_utils_common_posix.c
@@ -211,7 +211,7 @@ grpc_error *grpc_set_socket_low_latency(int fd, int low_latency) {
/* set a socker using a grpc_socket_mutator */
grpc_error *grpc_set_socket_with_mutator(int fd, grpc_socket_mutator *mutator) {
GPR_ASSERT(mutator);
- if (!mutator->vtable->mutate_fd(fd, mutator)) {
+ if (!grpc_socket_mutator_mutate_fd(mutator, fd)) {
return GRPC_ERROR_CREATE("grpc_socket_mutator failed.");
}
return GRPC_ERROR_NONE;
diff --git a/src/core/lib/iomgr/socket_utils_posix.h b/src/core/lib/iomgr/socket_utils_posix.h
index d84a95d3d39..0ad2d39497a 100644
--- a/src/core/lib/iomgr/socket_utils_posix.h
+++ b/src/core/lib/iomgr/socket_utils_posix.h
@@ -39,6 +39,7 @@
#include
#include "src/core/lib/iomgr/error.h"
+#include "src/core/lib/iomgr/socket_mutator.h"
/* a wrapper for accept or accept4 */
int grpc_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
diff --git a/src/core/lib/iomgr/tcp_client_posix.c b/src/core/lib/iomgr/tcp_client_posix.c
index 84eb41ecd28..9089751d94e 100644
--- a/src/core/lib/iomgr/tcp_client_posix.c
+++ b/src/core/lib/iomgr/tcp_client_posix.c
@@ -50,6 +50,7 @@
#include "src/core/lib/iomgr/ev_posix.h"
#include "src/core/lib/iomgr/iomgr_posix.h"
#include "src/core/lib/iomgr/sockaddr_utils.h"
+#include "src/core/lib/iomgr/socket_mutator.h"
#include "src/core/lib/iomgr/socket_utils_posix.h"
#include "src/core/lib/iomgr/tcp_posix.h"
#include "src/core/lib/iomgr/timer.h"
diff --git a/src/cpp/common/channel_arguments.cc b/src/cpp/common/channel_arguments.cc
index f297ae85870..ae56409a54a 100644
--- a/src/cpp/common/channel_arguments.cc
+++ b/src/cpp/common/channel_arguments.cc
@@ -37,7 +37,7 @@
#include
#include
#include "src/core/lib/channel/channel_args.h"
-
+#include "src/core/lib/iomgr/socket_mutator.h"
namespace grpc {
ChannelArguments::ChannelArguments() {
@@ -88,6 +88,24 @@ void ChannelArguments::SetCompressionAlgorithm(
SetInt(GRPC_COMPRESSION_CHANNEL_DEFAULT_ALGORITHM, algorithm);
}
+void ChannelArguments::SetSocketMutator(grpc_socket_mutator* mutator) {
+ if (!mutator) {
+ return;
+ }
+ grpc_arg mutator_arg = grpc_socket_mutator_to_arg(mutator);
+ bool replaced = false;
+ for (auto it = args_.begin(); it != args_.end(); ++it) {
+ if (it->type == mutator_arg.type &&
+ grpc::string(it->key) == grpc::string(mutator_arg.key)) {
+ it->value.pointer.vtable->destroy(it->value.pointer.p);
+ it->value.pointer = mutator_arg.value.pointer;
+ }
+ }
+ if (!replaced) {
+ args_.push_back(mutator_arg);
+ }
+}
+
// Note: a second call to this will add in front the result of the first call.
// An example is calling this on a copy of ChannelArguments which already has a
// prefix. The user can build up a prefix string by calling this multiple times,
diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py
index 2bebf48c70a..b4add7bb26e 100644
--- a/src/python/grpcio/grpc_core_dependencies.py
+++ b/src/python/grpcio/grpc_core_dependencies.py
@@ -118,6 +118,7 @@ CORE_SOURCE_FILES = [
'src/core/lib/iomgr/resolve_address_posix.c',
'src/core/lib/iomgr/resolve_address_windows.c',
'src/core/lib/iomgr/sockaddr_utils.c',
+ 'src/core/lib/iomgr/socket_mutator.c',
'src/core/lib/iomgr/socket_utils_common_posix.c',
'src/core/lib/iomgr/socket_utils_linux.c',
'src/core/lib/iomgr/socket_utils_posix.c',
diff --git a/test/core/channel/channel_args_test.c b/test/core/channel/channel_args_test.c
index 8ef1bff22e7..d3eb969f098 100644
--- a/test/core/channel/channel_args_test.c
+++ b/test/core/channel/channel_args_test.c
@@ -134,12 +134,26 @@ static void test_compression_algorithm_states(void) {
grpc_channel_args_destroy(ch_args);
}
+static void test_set_socket_mutator(void) {
+ grpc_channel_args *ch_args;
+ grpc_socket_mutator mutator;
+ grpc_socket_mutator_init(&mutator, NULL);
+
+ ch_args = grpc_channel_args_set_socket_mutator(NULL, &mutator);
+ GPR_ASSERT(ch_args->num_args == 1);
+ GPR_ASSERT(strcmp(ch_args->args[0].key, GRPC_ARG_SOCKET_MUTATOR) == 0);
+ GPR_ASSERT(ch_args->args[0].type == GRPC_ARG_POINTER);
+
+ grpc_channel_args_destroy(ch_args);
+}
+
int main(int argc, char **argv) {
grpc_test_init(argc, argv);
grpc_init();
test_create();
test_set_compression_algorithm();
test_compression_algorithm_states();
+ test_set_socket_mutator();
grpc_shutdown();
return 0;
}
diff --git a/test/core/iomgr/socket_utils_test.c b/test/core/iomgr/socket_utils_test.c
index 113668b4e94..0b0dfcffbef 100644
--- a/test/core/iomgr/socket_utils_test.c
+++ b/test/core/iomgr/socket_utils_test.c
@@ -38,7 +38,10 @@
#include
#include
+#include
#include
+#include
+#include "src/core/lib/iomgr/socket_mutator.h"
#include "test/core/util/test_config.h"
struct test_socket_mutator {
@@ -46,7 +49,7 @@ struct test_socket_mutator {
int option_value;
};
-static bool mutate_fd(int fd, grpc_socket_mutator* mutator) {
+static bool mutate_fd(int fd, grpc_socket_mutator *mutator) {
int newval;
socklen_t intlen = sizeof(newval);
struct test_socket_mutator* m = (struct test_socket_mutator*)mutator;
@@ -64,7 +67,13 @@ static bool mutate_fd(int fd, grpc_socket_mutator* mutator) {
return true;
}
-static const grpc_socket_mutator_vtable mutator_vtable = {mutate_fd};
+static void destroy_test_mutator(grpc_socket_mutator *mutator) {
+ struct test_socket_mutator *m = (struct test_socket_mutator *)mutator;
+ gpr_free(m);
+}
+
+static const grpc_socket_mutator_vtable mutator_vtable = {mutate_fd,
+ destroy_test_mutator};
int main(int argc, char **argv) {
int sock;
@@ -91,7 +100,7 @@ int main(int argc, char **argv) {
grpc_set_socket_low_latency(sock, 0)));
struct test_socket_mutator mutator;
- mutator.base.vtable = &mutator_vtable;
+ grpc_socket_mutator_init(&mutator.base, &mutator_vtable);
mutator.option_value = IPTOS_LOWDELAY;
GPR_ASSERT(GRPC_LOG_IF_ERROR(
diff --git a/test/cpp/common/channel_arguments_test.cc b/test/cpp/common/channel_arguments_test.cc
index 1443eb2f68d..0d1e06b1e99 100644
--- a/test/cpp/common/channel_arguments_test.cc
+++ b/test/cpp/common/channel_arguments_test.cc
@@ -34,11 +34,53 @@
#include
#include
+#include
#include
+#include "src/core/lib/iomgr/socket_mutator.h"
namespace grpc {
namespace testing {
+namespace {
+
+// A simple grpc_socket_mutator to be used to test SetSocketMutator
+class TestSocketMutator : public grpc_socket_mutator {
+ public:
+ TestSocketMutator();
+
+ bool MutateFd(int fd) {
+ // Do nothing on the fd
+ return true;
+ }
+};
+
+//
+// C API for TestSocketMutator
+//
+
+bool test_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) {
+ TestSocketMutator* tsm = (TestSocketMutator*)mutator;
+ return tsm->MutateFd(fd);
+}
+
+void test_mutator_destroy(grpc_socket_mutator* mutator) {
+ TestSocketMutator* tsm = (TestSocketMutator*)mutator;
+ gpr_log(GPR_ERROR, "destroy is called");
+ delete tsm;
+}
+
+grpc_socket_mutator_vtable test_mutator_vtable = {test_mutator_mutate_fd,
+ test_mutator_destroy};
+
+//
+// TestSocketMutator implementation
+//
+
+TestSocketMutator::TestSocketMutator() {
+ grpc_socket_mutator_init(this, &test_mutator_vtable);
+}
+}
+
class ChannelArgumentsTest : public ::testing::Test {
protected:
ChannelArgumentsTest()
@@ -165,6 +207,26 @@ TEST_F(ChannelArgumentsTest, SetPointer) {
EXPECT_TRUE(HasArg(arg0));
}
+TEST_F(ChannelArgumentsTest, SetSocketMutator) {
+ VerifyDefaultChannelArgs();
+ grpc_arg arg0, arg1;
+ TestSocketMutator* mutator0 = new TestSocketMutator();
+ TestSocketMutator* mutator1 = new TestSocketMutator();
+ arg0 = grpc_socket_mutator_to_arg(mutator0);
+ arg1 = grpc_socket_mutator_to_arg(mutator1);
+
+ channel_args_.SetSocketMutator(mutator0);
+ EXPECT_TRUE(HasArg(arg0));
+
+ channel_args_.SetSocketMutator(mutator1);
+ EXPECT_TRUE(HasArg(arg1));
+ // arg0 is replaced by arg1
+ EXPECT_FALSE(HasArg(arg0));
+
+ // arg0 is destroyed by grpc_socket_mutator_to_arg(mutator1)
+ arg1.value.pointer.vtable->destroy(arg1.value.pointer.p);
+}
+
TEST_F(ChannelArgumentsTest, SetUserAgentPrefix) {
VerifyDefaultChannelArgs();
grpc::string prefix("prefix");
diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal
index 44f147aeb18..8640de48116 100644
--- a/tools/doxygen/Doxyfile.core.internal
+++ b/tools/doxygen/Doxyfile.core.internal
@@ -830,6 +830,7 @@ src/core/lib/iomgr/sockaddr.h \
src/core/lib/iomgr/sockaddr_posix.h \
src/core/lib/iomgr/sockaddr_utils.h \
src/core/lib/iomgr/sockaddr_windows.h \
+src/core/lib/iomgr/socket_mutator.h \
src/core/lib/iomgr/socket_utils_posix.h \
src/core/lib/iomgr/socket_windows.h \
src/core/lib/iomgr/tcp_client.h \
@@ -991,6 +992,7 @@ src/core/lib/iomgr/pollset_windows.c \
src/core/lib/iomgr/resolve_address_posix.c \
src/core/lib/iomgr/resolve_address_windows.c \
src/core/lib/iomgr/sockaddr_utils.c \
+src/core/lib/iomgr/socket_mutator.c \
src/core/lib/iomgr/socket_utils_common_posix.c \
src/core/lib/iomgr/socket_utils_linux.c \
src/core/lib/iomgr/socket_utils_posix.c \
diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json
index c05d194e19e..ce111303a69 100644
--- a/tools/run_tests/sources_and_headers.json
+++ b/tools/run_tests/sources_and_headers.json
@@ -6394,6 +6394,7 @@
"src/core/lib/iomgr/sockaddr_posix.h",
"src/core/lib/iomgr/sockaddr_utils.h",
"src/core/lib/iomgr/sockaddr_windows.h",
+ "src/core/lib/iomgr/socket_mutator.h",
"src/core/lib/iomgr/socket_utils_posix.h",
"src/core/lib/iomgr/socket_windows.h",
"src/core/lib/iomgr/tcp_client.h",
@@ -6529,6 +6530,8 @@
"src/core/lib/iomgr/sockaddr_utils.c",
"src/core/lib/iomgr/sockaddr_utils.h",
"src/core/lib/iomgr/sockaddr_windows.h",
+ "src/core/lib/iomgr/socket_mutator.c",
+ "src/core/lib/iomgr/socket_mutator.h",
"src/core/lib/iomgr/socket_utils_common_posix.c",
"src/core/lib/iomgr/socket_utils_linux.c",
"src/core/lib/iomgr/socket_utils_posix.c",
diff --git a/vsprojects/vcxproj/grpc/grpc.vcxproj b/vsprojects/vcxproj/grpc/grpc.vcxproj
index 965d06d1d5c..bb503ccdd41 100644
--- a/vsprojects/vcxproj/grpc/grpc.vcxproj
+++ b/vsprojects/vcxproj/grpc/grpc.vcxproj
@@ -339,6 +339,7 @@
+
@@ -543,6 +544,8 @@
+
+
diff --git a/vsprojects/vcxproj/grpc/grpc.vcxproj.filters b/vsprojects/vcxproj/grpc/grpc.vcxproj.filters
index dddd4ecce51..6ed4ae0d8f6 100644
--- a/vsprojects/vcxproj/grpc/grpc.vcxproj.filters
+++ b/vsprojects/vcxproj/grpc/grpc.vcxproj.filters
@@ -124,6 +124,9 @@
src\core\lib\iomgr
+
+ src\core\lib\iomgr
+
src\core\lib\iomgr
@@ -800,6 +803,9 @@
src\core\lib\iomgr
+
+ src\core\lib\iomgr
+
src\core\lib\iomgr
diff --git a/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj b/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj
index b724c217ed4..d18009ee23e 100644
--- a/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj
+++ b/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj
@@ -232,6 +232,7 @@
+
@@ -391,6 +392,8 @@
+
+
diff --git a/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj.filters b/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj.filters
index 92806fa04a2..21aec591973 100644
--- a/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj.filters
+++ b/vsprojects/vcxproj/grpc_test_util/grpc_test_util.vcxproj.filters
@@ -178,6 +178,9 @@
src\core\lib\iomgr
+
+ src\core\lib\iomgr
+
src\core\lib\iomgr
@@ -587,6 +590,9 @@
src\core\lib\iomgr
+
+ src\core\lib\iomgr
+
src\core\lib\iomgr
diff --git a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj
index b46773632cb..948be2c20c8 100644
--- a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj
+++ b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj
@@ -329,6 +329,7 @@
+
@@ -511,6 +512,8 @@
+
+
diff --git a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters
index 6ffc1eab708..74be00343e4 100644
--- a/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters
+++ b/vsprojects/vcxproj/grpc_unsecure/grpc_unsecure.vcxproj.filters
@@ -127,6 +127,9 @@
src\core\lib\iomgr
+
+ src\core\lib\iomgr
+
src\core\lib\iomgr
@@ -710,6 +713,9 @@
src\core\lib\iomgr
+
+ src\core\lib\iomgr
+
src\core\lib\iomgr