Home | History | Annotate | Download | only in iomgr
      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 /* 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 */
     45 grpc_error* grpc_set_socket_reuse_addr(int fd, int reuse);
     46 
     47 /* return true if SO_REUSEPORT is supported */
     48 bool grpc_is_socket_reuse_port_supported();
     49 
     50 /* disable nagle */
     51 grpc_error* grpc_set_socket_low_latency(int fd, int low_latency);
     52 
     53 /* set SO_REUSEPORT */
     54 grpc_error* grpc_set_socket_reuse_port(int fd, int reuse);
     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 */
     60 grpc_error* grpc_set_socket_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. */
     71 int grpc_ipv6_loopback_available(void);
     72 
     73 /* Tries to set SO_NOSIGPIPE if available on this platform.
     74    If SO_NO_SIGPIPE is not available, returns 1. */
     75 grpc_error* grpc_set_socket_no_sigpipe_if_possible(int fd);
     76 
     77 /* Tries to set IP_PKTINFO if available on this platform.
     78    If IP_PKTINFO is not available, returns 1. */
     79 grpc_error* grpc_set_socket_ip_pktinfo_if_possible(int fd);
     80 
     81 /* Tries to set IPV6_RECVPKTINFO if available on this platform.
     82    If IPV6_RECVPKTINFO is not available, returns 1. */
     83 grpc_error* grpc_set_socket_ipv6_recvpktinfo_if_possible(int fd);
     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 */
     92 grpc_error* grpc_set_socket_with_mutator(int fd, grpc_socket_mutator* mutator);
     93 
     94 /* An enum to keep track of IPv4/IPv6 socket modes.
     95 
     96    Currently, this information is only used when a socket is first created, but
     97    in the future we may wish to store it alongside the fd.  This would let calls
     98    like sendto() know which family to use without asking the kernel first. */
     99 typedef enum grpc_dualstack_mode {
    100   /* Uninitialized, or a non-IP socket. */
    101   GRPC_DSMODE_NONE,
    102   /* AF_INET only. */
    103   GRPC_DSMODE_IPV4,
    104   /* AF_INET6 only, because IPV6_V6ONLY could not be cleared. */
    105   GRPC_DSMODE_IPV6,
    106   /* AF_INET6, which also supports ::ffff-mapped IPv4 addresses. */
    107   GRPC_DSMODE_DUALSTACK
    108 } grpc_dualstack_mode;
    109 
    110 /* Only tests should use this flag. */
    111 extern int grpc_forbid_dualstack_sockets_for_testing;
    112 
    113 /* Creates a new socket for connecting to (or listening on) an address.
    114 
    115    If addr is AF_INET6, this creates an IPv6 socket first.  If that fails,
    116    and addr is within ::ffff:0.0.0.0/96, then it automatically falls back to
    117    an IPv4 socket.
    118 
    119    If addr is AF_INET, AF_UNIX, or anything else, then this is similar to
    120    calling socket() directly.
    121 
    122    Returns an fd on success, otherwise returns -1 with errno set to the result
    123    of a failed socket() call.
    124 
    125    The *dsmode output indicates which address family was actually created.
    126    The recommended way to use this is:
    127    - First convert to IPv6 using grpc_sockaddr_to_v4mapped().
    128    - Create the socket.
    129    - If *dsmode is IPV4, use grpc_sockaddr_is_v4mapped() to convert back to
    130      IPv4, so that bind() or connect() see the correct family.
    131    Also, it's important to distinguish between DUALSTACK and IPV6 when
    132    listening on the [::] wildcard address. */
    133 grpc_error* grpc_create_dualstack_socket(const grpc_resolved_address* addr,
    134                                          int type, int protocol,
    135                                          grpc_dualstack_mode* dsmode,
    136                                          int* newfd);
    137 
    138 /* Same as grpc_create_dualstack_socket(), but use the given socket factory (if
    139    non-null) to create the socket, rather than calling socket() directly. */
    140 grpc_error* grpc_create_dualstack_socket_using_factory(
    141     grpc_socket_factory* factory, const grpc_resolved_address* addr, int type,
    142     int protocol, grpc_dualstack_mode* dsmode, int* newfd);
    143 
    144 #endif /* GRPC_CORE_LIB_IOMGR_SOCKET_UTILS_POSIX_H */
    145