1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H
20 #define GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include "src/core/lib/iomgr/resolve_address.h"
25 
26 #include <sys/socket.h>
27 #include <unistd.h>
28 
29 #include <grpc/impl/codegen/grpc_types.h>
30 #include "src/core/lib/iomgr/error.h"
31 #include "src/core/lib/iomgr/socket_factory_posix.h"
32 #include "src/core/lib/iomgr/socket_mutator.h"
33 
34 #ifdef GRPC_LINUX_ERRQUEUE
35 #ifndef SO_ZEROCOPY
36 #define SO_ZEROCOPY 60
37 #endif
38 #ifndef SO_EE_ORIGIN_ZEROCOPY
39 #define SO_EE_ORIGIN_ZEROCOPY 5
40 #endif
41 #endif /* ifdef GRPC_LINUX_ERRQUEUE */
42 
43 /* a wrapper for accept or accept4 */
44 int grpc_accept4(int sockfd, grpc_resolved_address* resolved_addr, int nonblock,
45                  int cloexec);
46 
47 /* set a socket to use zerocopy */
48 grpc_error* grpc_set_socket_zerocopy(int fd);
49 
50 /* set a socket to non blocking mode */
51 grpc_error* grpc_set_socket_nonblocking(int fd, int non_blocking);
52 
53 /* set a socket to close on exec */
54 grpc_error* grpc_set_socket_cloexec(int fd, int close_on_exec);
55 
56 /* set a socket to reuse old addresses */
57 grpc_error* grpc_set_socket_reuse_addr(int fd, int reuse);
58 
59 /* return true if SO_REUSEPORT is supported */
60 bool grpc_is_socket_reuse_port_supported();
61 
62 /* disable nagle */
63 grpc_error* grpc_set_socket_low_latency(int fd, int low_latency);
64 
65 /* set SO_REUSEPORT */
66 grpc_error* grpc_set_socket_reuse_port(int fd, int reuse);
67 
68 /* Configure the default values for TCP_USER_TIMEOUT */
69 void config_default_tcp_user_timeout(bool enable, int timeout, bool is_client);
70 
71 /* Set TCP_USER_TIMEOUT */
72 grpc_error* grpc_set_socket_tcp_user_timeout(
73     int fd, const grpc_channel_args* channel_args, bool is_client);
74 
75 /* Returns true if this system can create AF_INET6 sockets bound to ::1.
76    The value is probed once, and cached for the life of the process.
77 
78    This is more restrictive than checking for socket(AF_INET6) to succeed,
79    because Linux with "net.ipv6.conf.all.disable_ipv6 = 1" is able to create
80    and bind IPv6 sockets, but cannot connect to a getsockname() of [::]:port
81    without a valid loopback interface.  Rather than expose this half-broken
82    state to library users, we turn off IPv6 sockets. */
83 int grpc_ipv6_loopback_available(void);
84 
85 /* Tries to set SO_NOSIGPIPE if available on this platform.
86    If SO_NO_SIGPIPE is not available, returns 1. */
87 grpc_error* grpc_set_socket_no_sigpipe_if_possible(int fd);
88 
89 /* Tries to set IP_PKTINFO if available on this platform.
90    If IP_PKTINFO is not available, returns 1. */
91 grpc_error* grpc_set_socket_ip_pktinfo_if_possible(int fd);
92 
93 /* Tries to set IPV6_RECVPKTINFO if available on this platform.
94    If IPV6_RECVPKTINFO is not available, returns 1. */
95 grpc_error* grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd);
96 
97 /* Tries to set the socket's send buffer to given size. */
98 grpc_error* grpc_set_socket_sndbuf(int fd, int buffer_size_bytes);
99 
100 /* Tries to set the socket's receive buffer to given size. */
101 grpc_error* grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes);
102 
103 /* Tries to set the socket using a grpc_socket_mutator */
104 grpc_error* grpc_set_socket_with_mutator(int fd, grpc_socket_mutator* mutator);
105 
106 /* Extracts the first socket mutator from args if any and applies on the fd. */
107 grpc_error* grpc_apply_socket_mutator_in_args(int fd,
108                                               const grpc_channel_args* args);
109 
110 /* An enum to keep track of IPv4/IPv6 socket modes.
111 
112    Currently, this information is only used when a socket is first created, but
113    in the future we may wish to store it alongside the fd.  This would let calls
114    like sendto() know which family to use without asking the kernel first. */
115 typedef enum grpc_dualstack_mode {
116   /* Uninitialized, or a non-IP socket. */
117   GRPC_DSMODE_NONE,
118   /* AF_INET only. */
119   GRPC_DSMODE_IPV4,
120   /* AF_INET6 only, because IPV6_V6ONLY could not be cleared. */
121   GRPC_DSMODE_IPV6,
122   /* AF_INET6, which also supports ::ffff-mapped IPv4 addresses. */
123   GRPC_DSMODE_DUALSTACK
124 } grpc_dualstack_mode;
125 
126 /* Only tests should use this flag. */
127 extern int grpc_forbid_dualstack_sockets_for_testing;
128 
129 /* Tries to set the socket to dualstack. Returns 1 on success. */
130 int grpc_set_socket_dualstack(int fd);
131 
132 /* Creates a new socket for connecting to (or listening on) an address.
133 
134    If addr is AF_INET6, this creates an IPv6 socket first.  If that fails,
135    and addr is within ::ffff:0.0.0.0/96, then it automatically falls back to
136    an IPv4 socket.
137 
138    If addr is AF_INET, AF_UNIX, or anything else, then this is similar to
139    calling socket() directly.
140 
141    Returns an fd on success, otherwise returns -1 with errno set to the result
142    of a failed socket() call.
143 
144    The *dsmode output indicates which address family was actually created.
145    The recommended way to use this is:
146    - First convert to IPv6 using grpc_sockaddr_to_v4mapped().
147    - Create the socket.
148    - If *dsmode is IPV4, use grpc_sockaddr_is_v4mapped() to convert back to
149      IPv4, so that bind() or connect() see the correct family.
150    Also, it's important to distinguish between DUALSTACK and IPV6 when
151    listening on the [::] wildcard address. */
152 grpc_error* grpc_create_dualstack_socket(const grpc_resolved_address* addr,
153                                          int type, int protocol,
154                                          grpc_dualstack_mode* dsmode,
155                                          int* newfd);
156 
157 /* Same as grpc_create_dualstack_socket(), but use the given socket factory (if
158    non-null) to create the socket, rather than calling socket() directly. */
159 grpc_error* grpc_create_dualstack_socket_using_factory(
160     grpc_socket_factory* factory, const grpc_resolved_address* addr, int type,
161     int protocol, grpc_dualstack_mode* dsmode, int* newfd);
162 
163 #endif /* GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H */
164