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 
24 #include <sys/socket.h>
25 #include <unistd.h>
26 
28 
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 */
49 
50 /* set a socket to non blocking mode */
51 grpc_error_handle grpc_set_socket_nonblocking(int fd, int non_blocking);
52 
53 /* set a socket to close on exec */
54 grpc_error_handle grpc_set_socket_cloexec(int fd, int close_on_exec);
55 
56 /* set a socket to reuse old addresses */
58 
59 /* return true if SO_REUSEPORT is supported */
61 
62 /* disable nagle */
63 grpc_error_handle grpc_set_socket_low_latency(int fd, int low_latency);
64 
65 /* set SO_REUSEPORT */
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 */
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. */
84 
85 /* Tries to set SO_NOSIGPIPE if available on this platform.
86  If SO_NO_SIGPIPE is not available, returns 1. */
88 
89 /* Tries to set IP_PKTINFO if available on this platform.
90  If IP_PKTINFO is not available, returns 1. */
92 
93 /* Tries to set IPV6_RECVPKTINFO if available on this platform.
94  If IPV6_RECVPKTINFO is not available, returns 1. */
96 
97 /* Tries to set the socket's send buffer to given size. */
98 grpc_error_handle 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_handle grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes);
102 
103 /* Tries to set the socket using a grpc_socket_mutator */
105  grpc_socket_mutator* mutator);
106 
107 /* Extracts the first socket mutator from args if any and applies on the fd. */
109  int fd, grpc_fd_usage usage, const grpc_channel_args* args);
110 
111 /* An enum to keep track of IPv4/IPv6 socket modes.
112 
113  Currently, this information is only used when a socket is first created, but
114  in the future we may wish to store it alongside the fd. This would let calls
115  like sendto() know which family to use without asking the kernel first. */
116 typedef enum grpc_dualstack_mode {
117  /* Uninitialized, or a non-IP socket. */
119  /* AF_INET only. */
121  /* AF_INET6 only, because IPV6_V6ONLY could not be cleared. */
123  /* AF_INET6, which also supports ::ffff-mapped IPv4 addresses. */
126 
127 /* Only tests should use this flag. */
129 
130 /* Tries to set the socket to dualstack. Returns 1 on success. */
131 int grpc_set_socket_dualstack(int fd);
132 
133 /* Creates a new socket for connecting to (or listening on) an address.
134 
135  If addr is AF_INET6, this creates an IPv6 socket first. If that fails,
136  and addr is within ::ffff:0.0.0.0/96, then it automatically falls back to
137  an IPv4 socket.
138 
139  If addr is AF_INET, AF_UNIX, or anything else, then this is similar to
140  calling socket() directly.
141 
142  Returns an fd on success, otherwise returns -1 with errno set to the result
143  of a failed socket() call.
144 
145  The *dsmode output indicates which address family was actually created.
146  The recommended way to use this is:
147  - First convert to IPv6 using grpc_sockaddr_to_v4mapped().
148  - Create the socket.
149  - If *dsmode is IPV4, use grpc_sockaddr_is_v4mapped() to convert back to
150  IPv4, so that bind() or connect() see the correct family.
151  Also, it's important to distinguish between DUALSTACK and IPV6 when
152  listening on the [::] wildcard address. */
154  const grpc_resolved_address* addr, int type, int protocol,
155  grpc_dualstack_mode* dsmode, 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. */
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 */
grpc_set_socket_reuse_addr
grpc_error_handle grpc_set_socket_reuse_addr(int fd, int reuse)
grpc_socket_factory
Definition: socket_factory_posix.h:43
grpc_create_dualstack_socket_using_factory
grpc_error_handle 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)
grpc_set_socket_rcvbuf
grpc_error_handle grpc_set_socket_rcvbuf(int fd, int buffer_size_bytes)
GRPC_DSMODE_NONE
@ GRPC_DSMODE_NONE
Definition: socket_utils_posix.h:118
GRPC_DSMODE_IPV6
@ GRPC_DSMODE_IPV6
Definition: socket_utils_posix.h:122
grpc_set_socket_low_latency
grpc_error_handle grpc_set_socket_low_latency(int fd, int low_latency)
grpc_set_socket_zerocopy
grpc_error_handle grpc_set_socket_zerocopy(int fd)
grpc_set_socket_no_sigpipe_if_possible
grpc_error_handle grpc_set_socket_no_sigpipe_if_possible(int fd)
resolve_address.h
protocol
Protocol protocol
Definition: client_callback_end2end_test.cc:67
grpc_resolved_address
Definition: resolved_address.h:34
grpc_channel_args
Definition: grpc_types.h:132
grpc_types.h
grpc_ipv6_loopback_available
int grpc_ipv6_loopback_available(void)
grpc_set_socket_cloexec
grpc_error_handle grpc_set_socket_cloexec(int fd, int close_on_exec)
GRPC_DSMODE_DUALSTACK
@ GRPC_DSMODE_DUALSTACK
Definition: socket_utils_posix.h:124
grpc_is_socket_reuse_port_supported
bool grpc_is_socket_reuse_port_supported()
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_accept4
int grpc_accept4(int sockfd, grpc_resolved_address *resolved_addr, int nonblock, int cloexec)
error.h
grpc_set_socket_reuse_port
grpc_error_handle grpc_set_socket_reuse_port(int fd, int reuse)
grpc_create_dualstack_socket
grpc_error_handle grpc_create_dualstack_socket(const grpc_resolved_address *addr, int type, int protocol, grpc_dualstack_mode *dsmode, int *newfd)
grpc_socket_mutator
Definition: socket_mutator.h:62
grpc_set_socket_ipv6_recvpktinfo_if_possible
grpc_error_handle grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd)
grpc_set_socket_with_mutator
grpc_error_handle grpc_set_socket_with_mutator(int fd, grpc_fd_usage usage, grpc_socket_mutator *mutator)
grpc_forbid_dualstack_sockets_for_testing
int grpc_forbid_dualstack_sockets_for_testing
bloaty::usage
const char usage[]
Definition: bloaty.cc:1843
GRPC_DSMODE_IPV4
@ GRPC_DSMODE_IPV4
Definition: socket_utils_posix.h:120
grpc_fd_usage
grpc_fd_usage
Definition: socket_mutator.h:30
grpc_set_socket_dualstack
int grpc_set_socket_dualstack(int fd)
grpc_dualstack_mode
grpc_dualstack_mode
Definition: socket_utils_posix.h:116
config_default_tcp_user_timeout
void config_default_tcp_user_timeout(bool enable, int timeout, bool is_client)
socket_factory_posix.h
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
grpc_set_socket_nonblocking
grpc_error_handle grpc_set_socket_nonblocking(int fd, int non_blocking)
grpc_error
Definition: error_internal.h:42
grpc_set_socket_tcp_user_timeout
grpc_error_handle grpc_set_socket_tcp_user_timeout(int fd, const grpc_channel_args *channel_args, bool is_client)
grpc_set_socket_sndbuf
grpc_error_handle grpc_set_socket_sndbuf(int fd, int buffer_size_bytes)
socket_mutator.h
grpc_apply_socket_mutator_in_args
grpc_error_handle grpc_apply_socket_mutator_in_args(int fd, grpc_fd_usage usage, const grpc_channel_args *args)
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
timeout
uv_timer_t timeout
Definition: libuv/docs/code/uvwget/main.c:9
port_platform.h
grpc_set_socket_ip_pktinfo_if_possible
grpc_error_handle grpc_set_socket_ip_pktinfo_if_possible(int fd)


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:19