GRPC C++  1.26.0
socket_utils_posix.h
Go to the documentation of this file.
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 
23 
25 
26 #include <sys/socket.h>
27 #include <unistd.h>
28 
33 
34 /* a wrapper for accept or accept4 */
35 int grpc_accept4(int sockfd, grpc_resolved_address* resolved_addr, int nonblock,
36  int cloexec);
37 
38 /* set a socket to non blocking mode */
39 grpc_error* grpc_set_socket_nonblocking(int fd, int non_blocking);
40 
41 /* set a socket to close on exec */
42 grpc_error* grpc_set_socket_cloexec(int fd, int close_on_exec);
43 
44 /* set a socket to reuse old addresses */
46 
47 /* return true if SO_REUSEPORT is supported */
49 
50 /* disable nagle */
51 grpc_error* grpc_set_socket_low_latency(int fd, int low_latency);
52 
53 /* set SO_REUSEPORT */
55 
56 /* Configure the default values for TCP_USER_TIMEOUT */
57 void config_default_tcp_user_timeout(bool enable, int timeout, bool is_client);
58 
59 /* Set TCP_USER_TIMEOUT */
61  int fd, const grpc_channel_args* channel_args, bool is_client);
62 
63 /* Returns true if this system can create AF_INET6 sockets bound to ::1.
64  The value is probed once, and cached for the life of the process.
65 
66  This is more restrictive than checking for socket(AF_INET6) to succeed,
67  because Linux with "net.ipv6.conf.all.disable_ipv6 = 1" is able to create
68  and bind IPv6 sockets, but cannot connect to a getsockname() of [::]:port
69  without a valid loopback interface. Rather than expose this half-broken
70  state to library users, we turn off IPv6 sockets. */
72 
73 /* Tries to set SO_NOSIGPIPE if available on this platform.
74  If SO_NO_SIGPIPE is not available, returns 1. */
76 
77 /* Tries to set IP_PKTINFO if available on this platform.
78  If IP_PKTINFO is not available, returns 1. */
80 
81 /* Tries to set IPV6_RECVPKTINFO if available on this platform.
82  If IPV6_RECVPKTINFO is not available, returns 1. */
84 
85 /* Tries to set the socket's send buffer to given size. */
86 grpc_error* grpc_set_socket_sndbuf(int fd, int buffer_size_bytes);
87 
88 /* Tries to set the socket's receive buffer to given size. */
89 grpc_error* grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes);
90 
91 /* Tries to set the socket using a grpc_socket_mutator */
93 
94 /* Extracts the first socket mutator from args if any and applies on the fd. */
96  const grpc_channel_args* args);
97 
98 /* An enum to keep track of IPv4/IPv6 socket modes.
99 
100  Currently, this information is only used when a socket is first created, but
101  in the future we may wish to store it alongside the fd. This would let calls
102  like sendto() know which family to use without asking the kernel first. */
103 typedef enum grpc_dualstack_mode {
104  /* Uninitialized, or a non-IP socket. */
106  /* AF_INET only. */
108  /* AF_INET6 only, because IPV6_V6ONLY could not be cleared. */
110  /* AF_INET6, which also supports ::ffff-mapped IPv4 addresses. */
113 
114 /* Only tests should use this flag. */
116 
117 /* Creates a new socket for connecting to (or listening on) an address.
118 
119  If addr is AF_INET6, this creates an IPv6 socket first. If that fails,
120  and addr is within ::ffff:0.0.0.0/96, then it automatically falls back to
121  an IPv4 socket.
122 
123  If addr is AF_INET, AF_UNIX, or anything else, then this is similar to
124  calling socket() directly.
125 
126  Returns an fd on success, otherwise returns -1 with errno set to the result
127  of a failed socket() call.
128 
129  The *dsmode output indicates which address family was actually created.
130  The recommended way to use this is:
131  - First convert to IPv6 using grpc_sockaddr_to_v4mapped().
132  - Create the socket.
133  - If *dsmode is IPV4, use grpc_sockaddr_is_v4mapped() to convert back to
134  IPv4, so that bind() or connect() see the correct family.
135  Also, it's important to distinguish between DUALSTACK and IPV6 when
136  listening on the [::] wildcard address. */
138  int type, int protocol,
139  grpc_dualstack_mode* dsmode,
140  int* newfd);
141 
142 /* Same as grpc_create_dualstack_socket(), but use the given socket factory (if
143  non-null) to create the socket, rather than calling socket() directly. */
145  grpc_socket_factory* factory, const grpc_resolved_address* addr, int type,
146  int protocol, grpc_dualstack_mode* dsmode, int* newfd);
147 
148 #endif /* GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H */
grpc_dualstack_mode
Definition: socket_utils_posix.h:103
@ GRPC_DSMODE_IPV6
Definition: socket_utils_posix.h:109
@ GRPC_DSMODE_DUALSTACK
Definition: socket_utils_posix.h:111
@ GRPC_DSMODE_IPV4
Definition: socket_utils_posix.h:107
@ GRPC_DSMODE_NONE
Definition: socket_utils_posix.h:105
grpc_error * grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes)
void config_default_tcp_user_timeout(bool enable, int timeout, bool is_client)
grpc_error * grpc_set_socket_nonblocking(int fd, int non_blocking)
grpc_error * grpc_apply_socket_mutator_in_args(int fd, const grpc_channel_args *args)
int grpc_forbid_dualstack_sockets_for_testing
grpc_error * grpc_set_socket_reuse_port(int fd, int reuse)
grpc_error * grpc_set_socket_reuse_addr(int fd, int reuse)
grpc_error * grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd)
grpc_error * grpc_create_dualstack_socket(const grpc_resolved_address *addr, int type, int protocol, grpc_dualstack_mode *dsmode, int *newfd)
grpc_error * grpc_set_socket_low_latency(int fd, int low_latency)
grpc_error * grpc_set_socket_with_mutator(int fd, grpc_socket_mutator *mutator)
grpc_error * grpc_set_socket_ip_pktinfo_if_possible(int fd)
bool grpc_is_socket_reuse_port_supported()
grpc_error * grpc_set_socket_tcp_user_timeout(int fd, const grpc_channel_args *channel_args, bool is_client)
grpc_error * grpc_set_socket_cloexec(int fd, int close_on_exec)
grpc_error * grpc_create_dualstack_socket_using_factory(grpc_socket_factory *factory, const grpc_resolved_address *addr, int type, int protocol, grpc_dualstack_mode *dsmode, int *newfd)
int grpc_accept4(int sockfd, grpc_resolved_address *resolved_addr, int nonblock, int cloexec)
int grpc_ipv6_loopback_available(void)
grpc_error * grpc_set_socket_sndbuf(int fd, int buffer_size_bytes)
grpc_error * grpc_set_socket_no_sigpipe_if_possible(int fd)
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: error_internal.h:39
Definition: resolve_address.h:44
The Socket Factory interface allows changes on socket options.
Definition: socket_factory_posix.h:43
The Socket Mutator interface allows changes on socket options.
Definition: socket_mutator.h:40