blob: e84d3781a15f9d193476806f61f568c959870def [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H
35#define GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
murgatroid997871f732016-09-23 13:49:05 -070037#include "src/core/lib/iomgr/resolve_address.h"
38
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <sys/socket.h>
Craig Tillerf40df232016-03-25 13:38:14 -070040#include <unistd.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041
Yuchen Zeng929f4c62016-10-10 16:02:43 -070042#include <grpc/impl/codegen/grpc_types.h>
Craig Tiller27f59af2016-04-28 14:19:48 -070043#include "src/core/lib/iomgr/error.h"
Yuchen Zengde3daf52016-10-13 17:26:26 -070044#include "src/core/lib/iomgr/socket_mutator.h"
Craig Tiller27f59af2016-04-28 14:19:48 -070045
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080046/* a wrapper for accept or accept4 */
murgatroid99dedb9232016-09-26 13:54:04 -070047int grpc_accept4(int sockfd, grpc_resolved_address *resolved_addr, int nonblock,
48 int cloexec);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049
50/* set a socket to non blocking mode */
Craig Tiller27f59af2016-04-28 14:19:48 -070051grpc_error *grpc_set_socket_nonblocking(int fd, int non_blocking);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052
53/* set a socket to close on exec */
Craig Tiller27f59af2016-04-28 14:19:48 -070054grpc_error *grpc_set_socket_cloexec(int fd, int close_on_exec);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055
56/* set a socket to reuse old addresses */
Craig Tiller27f59af2016-04-28 14:19:48 -070057grpc_error *grpc_set_socket_reuse_addr(int fd, int reuse);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058
59/* disable nagle */
Craig Tiller27f59af2016-04-28 14:19:48 -070060grpc_error *grpc_set_socket_low_latency(int fd, int low_latency);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080061
Craig Tilleref962642016-05-18 22:57:17 -070062/* set SO_REUSEPORT */
63grpc_error *grpc_set_socket_reuse_port(int fd, int reuse);
64
pmarksb7455062014-12-17 19:00:42 -080065/* Returns true if this system can create AF_INET6 sockets bound to ::1.
66 The value is probed once, and cached for the life of the process.
67
68 This is more restrictive than checking for socket(AF_INET6) to succeed,
69 because Linux with "net.ipv6.conf.all.disable_ipv6 = 1" is able to create
70 and bind IPv6 sockets, but cannot connect to a getsockname() of [::]:port
71 without a valid loopback interface. Rather than expose this half-broken
72 state to library users, we turn off IPv6 sockets. */
Craig Tiller32946d32015-01-15 11:37:30 -080073int grpc_ipv6_loopback_available(void);
pmarksb7455062014-12-17 19:00:42 -080074
Craig Tiller2da02962015-05-06 16:14:25 -070075/* Tries to set SO_NOSIGPIPE if available on this platform.
Craig Tiller2da02962015-05-06 16:14:25 -070076 If SO_NO_SIGPIPE is not available, returns 1. */
Craig Tiller27f59af2016-04-28 14:19:48 -070077grpc_error *grpc_set_socket_no_sigpipe_if_possible(int fd);
Craig Tiller2da02962015-05-06 16:14:25 -070078
ahedberg8d7cff42016-03-24 11:16:44 -040079/* Tries to set IP_PKTINFO if available on this platform.
ahedberg8d7cff42016-03-24 11:16:44 -040080 If IP_PKTINFO is not available, returns 1. */
Craig Tiller27f59af2016-04-28 14:19:48 -070081grpc_error *grpc_set_socket_ip_pktinfo_if_possible(int fd);
ahedberg8d7cff42016-03-24 11:16:44 -040082
83/* Tries to set IPV6_RECVPKTINFO if available on this platform.
ahedberg8d7cff42016-03-24 11:16:44 -040084 If IPV6_RECVPKTINFO is not available, returns 1. */
Craig Tiller27f59af2016-04-28 14:19:48 -070085grpc_error *grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd);
ahedberg8d7cff42016-03-24 11:16:44 -040086
Craig Tillerba947482016-06-09 08:03:49 -070087/* Tries to set the socket's send buffer to given size. */
88grpc_error *grpc_set_socket_sndbuf(int fd, int buffer_size_bytes);
Robbie Shade3f30e632016-06-08 08:40:56 -040089
Craig Tillerba947482016-06-09 08:03:49 -070090/* Tries to set the socket's receive buffer to given size. */
91grpc_error *grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes);
Robbie Shade3f30e632016-06-08 08:40:56 -040092
Yuchen Zenga4f708a2016-10-11 18:36:24 -070093/* Tries to set the socket using a grpc_socket_mutator */
94grpc_error *grpc_set_socket_with_mutator(int fd, grpc_socket_mutator *mutator);
Yuchen Zeng929f4c62016-10-10 16:02:43 -070095
nnoble0c475f02014-12-05 15:37:39 -080096/* An enum to keep track of IPv4/IPv6 socket modes.
97
98 Currently, this information is only used when a socket is first created, but
99 in the future we may wish to store it alongside the fd. This would let calls
100 like sendto() know which family to use without asking the kernel first. */
101typedef enum grpc_dualstack_mode {
102 /* Uninitialized, or a non-IP socket. */
103 GRPC_DSMODE_NONE,
104 /* AF_INET only. */
105 GRPC_DSMODE_IPV4,
106 /* AF_INET6 only, because IPV6_V6ONLY could not be cleared. */
107 GRPC_DSMODE_IPV6,
108 /* AF_INET6, which also supports ::ffff-mapped IPv4 addresses. */
109 GRPC_DSMODE_DUALSTACK
110} grpc_dualstack_mode;
111
112/* Only tests should use this flag. */
113extern int grpc_forbid_dualstack_sockets_for_testing;
114
115/* Creates a new socket for connecting to (or listening on) an address.
116
117 If addr is AF_INET6, this creates an IPv6 socket first. If that fails,
118 and addr is within ::ffff:0.0.0.0/96, then it automatically falls back to
119 an IPv4 socket.
120
121 If addr is AF_INET, AF_UNIX, or anything else, then this is similar to
122 calling socket() directly.
123
124 Returns an fd on success, otherwise returns -1 with errno set to the result
125 of a failed socket() call.
126
127 The *dsmode output indicates which address family was actually created.
128 The recommended way to use this is:
129 - First convert to IPv6 using grpc_sockaddr_to_v4mapped().
130 - Create the socket.
131 - If *dsmode is IPV4, use grpc_sockaddr_is_v4mapped() to convert back to
132 IPv4, so that bind() or connect() see the correct family.
133 Also, it's important to distinguish between DUALSTACK and IPV6 when
134 listening on the [::] wildcard address. */
murgatroid99dedb9232016-09-26 13:54:04 -0700135grpc_error *grpc_create_dualstack_socket(const grpc_resolved_address *addr,
136 int type, int protocol,
Craig Tiller27f59af2016-04-28 14:19:48 -0700137 grpc_dualstack_mode *dsmode,
138 int *newfd);
nnoble0c475f02014-12-05 15:37:39 -0800139
Craig Tiller9a4dddd2016-03-25 17:08:13 -0700140#endif /* GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H */