aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib/iomgr
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2016-03-29 07:46:45 -0700
committerGravatar Craig Tiller <ctiller@google.com>2016-03-29 07:46:45 -0700
commitd335945b76d516fcfbec1aa428633a5191bcd88a (patch)
tree6c75fe3552981097f672e02fca74beeb975bb9ec /src/core/lib/iomgr
parentafbae1f583b9b34b60a975c0cee7b336399ade8b (diff)
parentc91504ed57c93d30f654d9f3a0fee62c1236fad8 (diff)
Merge github.com:grpc/grpc into cleaner-posix2
Diffstat (limited to 'src/core/lib/iomgr')
-rw-r--r--src/core/lib/iomgr/socket_utils_common_posix.c22
-rw-r--r--src/core/lib/iomgr/socket_utils_posix.h10
-rw-r--r--src/core/lib/iomgr/udp_server.c13
3 files changed, 35 insertions, 10 deletions
diff --git a/src/core/lib/iomgr/socket_utils_common_posix.c b/src/core/lib/iomgr/socket_utils_common_posix.c
index 9dbc2784e4..b433aee7fa 100644
--- a/src/core/lib/iomgr/socket_utils_common_posix.c
+++ b/src/core/lib/iomgr/socket_utils_common_posix.c
@@ -89,6 +89,28 @@ int grpc_set_socket_no_sigpipe_if_possible(int fd) {
#endif
}
+int grpc_set_socket_ip_pktinfo_if_possible(int fd) {
+#ifdef GPR_HAVE_IP_PKTINFO
+ int get_local_ip = 1;
+ return 0 == setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &get_local_ip,
+ sizeof(get_local_ip));
+#else
+ (void)fd;
+ return 1;
+#endif
+}
+
+int grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd) {
+#ifdef GPR_HAVE_IPV6_RECVPKTINFO
+ int get_local_ip = 1;
+ return 0 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &get_local_ip,
+ sizeof(get_local_ip));
+#else
+ (void)fd;
+ return 1;
+#endif
+}
+
/* set a socket to close on exec */
int grpc_set_socket_cloexec(int fd, int close_on_exec) {
int oldflags = fcntl(fd, F_GETFD, 0);
diff --git a/src/core/lib/iomgr/socket_utils_posix.h b/src/core/lib/iomgr/socket_utils_posix.h
index 063f298d72..f73ad6317d 100644
--- a/src/core/lib/iomgr/socket_utils_posix.h
+++ b/src/core/lib/iomgr/socket_utils_posix.h
@@ -68,6 +68,16 @@ int grpc_ipv6_loopback_available(void);
If SO_NO_SIGPIPE is not available, returns 1. */
int grpc_set_socket_no_sigpipe_if_possible(int fd);
+/* Tries to set IP_PKTINFO if available on this platform.
+ Returns 1 on success, 0 on failure.
+ If IP_PKTINFO is not available, returns 1. */
+int grpc_set_socket_ip_pktinfo_if_possible(int fd);
+
+/* Tries to set IPV6_RECVPKTINFO if available on this platform.
+ Returns 1 on success, 0 on failure.
+ If IPV6_RECVPKTINFO is not available, returns 1. */
+int grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd);
+
/* An enum to keep track of IPv4/IPv6 socket modes.
Currently, this information is only used when a socket is first created, but
diff --git a/src/core/lib/iomgr/udp_server.c b/src/core/lib/iomgr/udp_server.c
index deab40ef82..08164166ce 100644
--- a/src/core/lib/iomgr/udp_server.c
+++ b/src/core/lib/iomgr/udp_server.c
@@ -206,8 +206,6 @@ static int prepare_socket(int fd, const struct sockaddr *addr,
size_t addr_len) {
struct sockaddr_storage sockname_temp;
socklen_t sockname_len;
- int get_local_ip;
- int rc;
if (fd < 0) {
goto error;
@@ -218,14 +216,9 @@ static int prepare_socket(int fd, const struct sockaddr *addr,
strerror(errno));
}
- get_local_ip = 1;
- rc = setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &get_local_ip,
- sizeof(get_local_ip));
- if (rc == 0 && addr->sa_family == AF_INET6) {
-#if !defined(__APPLE__)
- rc = setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &get_local_ip,
- sizeof(get_local_ip));
-#endif
+ if (grpc_set_socket_ip_pktinfo_if_possible(fd) &&
+ addr->sa_family == AF_INET6) {
+ grpc_set_socket_ipv6_recvpktinfo_if_possible(fd);
}
GPR_ASSERT(addr_len < ~(socklen_t)0);