GRPC Core 0.10.0.0

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
socket_utils_posix.h
Go to the documentation of this file.
1 /*
2 *
3 * Copyright 2015, Google Inc.
4 * 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
34 #ifndef GRPC_INTERNAL_CORE_IOMGR_SOCKET_UTILS_POSIX_H
35 #define GRPC_INTERNAL_CORE_IOMGR_SOCKET_UTILS_POSIX_H
36
37 #include <unistd.h>
38 #include <sys/socket.h>
39
40 /* a wrapper for accept or accept4 */
41 int grpc_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
42 int nonblock, int cloexec);
43
44 /* set a socket to non blocking mode */
45 int grpc_set_socket_nonblocking(int fd, int non_blocking);
46
47 /* set a socket to close on exec */
48 int grpc_set_socket_cloexec(int fd, int close_on_exec);
49
50 /* set a socket to reuse old addresses */
51 int grpc_set_socket_reuse_addr(int fd, int reuse);
52
53 /* disable nagle */
54 int grpc_set_socket_low_latency(int fd, int low_latency);
55
56 /* Returns true if this system can create AF_INET6 sockets bound to ::1.
57 The value is probed once, and cached for the life of the process.
58
59 This is more restrictive than checking for socket(AF_INET6) to succeed,
60 because Linux with "net.ipv6.conf.all.disable_ipv6 = 1" is able to create
61 and bind IPv6 sockets, but cannot connect to a getsockname() of [::]:port
62 without a valid loopback interface. Rather than expose this half-broken
63 state to library users, we turn off IPv6 sockets. */
64 int grpc_ipv6_loopback_available(void);
65
66 /* Tries to set SO_NOSIGPIPE if available on this platform.
67 Returns 1 on success, 0 on failure.
68 If SO_NO_SIGPIPE is not available, returns 1. */
69 int grpc_set_socket_no_sigpipe_if_possible(int fd);
70
71 /* An enum to keep track of IPv4/IPv6 socket modes.
72
73 Currently, this information is only used when a socket is first created, but
74 in the future we may wish to store it alongside the fd. This would let calls
75 like sendto() know which family to use without asking the kernel first. */
76 typedef enum grpc_dualstack_mode {
77 /* Uninitialized, or a non-IP socket. */
78 GRPC_DSMODE_NONE,
79 /* AF_INET only. */
80 GRPC_DSMODE_IPV4,
81 /* AF_INET6 only, because IPV6_V6ONLY could not be cleared. */
82 GRPC_DSMODE_IPV6,
83 /* AF_INET6, which also supports ::ffff-mapped IPv4 addresses. */
84 GRPC_DSMODE_DUALSTACK
85 } grpc_dualstack_mode;
86
87 /* Only tests should use this flag. */
88 extern int grpc_forbid_dualstack_sockets_for_testing;
89
90 /* Creates a new socket for connecting to (or listening on) an address.
91
92 If addr is AF_INET6, this creates an IPv6 socket first. If that fails,
93 and addr is within ::ffff:0.0.0.0/96, then it automatically falls back to
94 an IPv4 socket.
95
96 If addr is AF_INET, AF_UNIX, or anything else, then this is similar to
97 calling socket() directly.
98
99 Returns an fd on success, otherwise returns -1 with errno set to the result
100 of a failed socket() call.
101
102 The *dsmode output indicates which address family was actually created.
103 The recommended way to use this is:
104 - First convert to IPv6 using grpc_sockaddr_to_v4mapped().
105 - Create the socket.
106 - If *dsmode is IPV4, use grpc_sockaddr_is_v4mapped() to convert back to
107 IPv4, so that bind() or connect() see the correct family.
108 Also, it's important to distinguish between DUALSTACK and IPV6 when
109 listening on the [::] wildcard address. */
110 int grpc_create_dualstack_socket(const struct sockaddr *addr, int type,
111 int protocol, grpc_dualstack_mode *dsmode);
112
113 #endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKET_UTILS_POSIX_H */
grpc_accept4
int grpc_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int nonblock, int cloexec)
grpc_dualstack_mode
grpc_dualstack_mode
Definition: socket_utils_posix.h:76
grpc_set_socket_reuse_addr
int grpc_set_socket_reuse_addr(int fd, int reuse)
grpc_ipv6_loopback_available
int grpc_ipv6_loopback_available(void)
GRPC_DSMODE_IPV6
Definition: socket_utils_posix.h:82
grpc_set_socket_low_latency
int grpc_set_socket_low_latency(int fd, int low_latency)
grpc_set_socket_cloexec
int grpc_set_socket_cloexec(int fd, int close_on_exec)
grpc_forbid_dualstack_sockets_for_testing
int grpc_forbid_dualstack_sockets_for_testing
GRPC_DSMODE_IPV4
Definition: socket_utils_posix.h:80
grpc_set_socket_no_sigpipe_if_possible
int grpc_set_socket_no_sigpipe_if_possible(int fd)
grpc_set_socket_nonblocking
int grpc_set_socket_nonblocking(int fd, int non_blocking)
GRPC_DSMODE_DUALSTACK
Definition: socket_utils_posix.h:84
GRPC_DSMODE_NONE
Definition: socket_utils_posix.h:78
grpc_create_dualstack_socket
int grpc_create_dualstack_socket(const struct sockaddr *addr, int type, int protocol, grpc_dualstack_mode *dsmode)