gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/ip_socket_test_util.h (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  #ifndef GVISOR_TEST_SYSCALLS_IP_SOCKET_TEST_UTIL_H_
    16  #define GVISOR_TEST_SYSCALLS_IP_SOCKET_TEST_UTIL_H_
    17  
    18  #include <arpa/inet.h>
    19  #include <ifaddrs.h>
    20  #include <sys/types.h>
    21  
    22  #include <string>
    23  
    24  #include "test/util/socket_util.h"
    25  
    26  namespace gvisor {
    27  namespace testing {
    28  
    29  // Extracts the IP address from an inet sockaddr in network byte order.
    30  uint32_t IPFromInetSockaddr(const struct sockaddr* addr);
    31  
    32  // Extracts the port from an inet sockaddr in host byte order.
    33  uint16_t PortFromInetSockaddr(const struct sockaddr* addr);
    34  
    35  // InterfaceIndex returns the index of the named interface.
    36  PosixErrorOr<int> InterfaceIndex(std::string name);
    37  
    38  // GetLoopbackIndex returns the index of the loopback interface.
    39  inline PosixErrorOr<int> GetLoopbackIndex() { return InterfaceIndex("lo"); }
    40  
    41  // IPv6TCPAcceptBindSocketPair returns a SocketPairKind that represents
    42  // SocketPairs created with bind() and accept() syscalls with AF_INET6 and the
    43  // given type bound to the IPv6 loopback.
    44  SocketPairKind IPv6TCPAcceptBindSocketPair(int type);
    45  
    46  // IPv4TCPAcceptBindSocketPair returns a SocketPairKind that represents
    47  // SocketPairs created with bind() and accept() syscalls with AF_INET and the
    48  // given type bound to the IPv4 loopback.
    49  SocketPairKind IPv4TCPAcceptBindSocketPair(int type);
    50  
    51  // DualStackTCPAcceptBindSocketPair returns a SocketPairKind that represents
    52  // SocketPairs created with bind() and accept() syscalls with AF_INET6 and the
    53  // given type bound to the IPv4 loopback.
    54  SocketPairKind DualStackTCPAcceptBindSocketPair(int type);
    55  
    56  // IPv6TCPAcceptBindPersistentListenerSocketPair is like
    57  // IPv6TCPAcceptBindSocketPair except it uses a persistent listening socket to
    58  // create all socket pairs.
    59  SocketPairKind IPv6TCPAcceptBindPersistentListenerSocketPair(int type);
    60  
    61  // IPv4TCPAcceptBindPersistentListenerSocketPair is like
    62  // IPv4TCPAcceptBindSocketPair except it uses a persistent listening socket to
    63  // create all socket pairs.
    64  SocketPairKind IPv4TCPAcceptBindPersistentListenerSocketPair(int type);
    65  
    66  // DualStackTCPAcceptBindPersistentListenerSocketPair is like
    67  // DualStackTCPAcceptBindSocketPair except it uses a persistent listening socket
    68  // to create all socket pairs.
    69  SocketPairKind DualStackTCPAcceptBindPersistentListenerSocketPair(int type);
    70  
    71  // IPv6UDPBidirectionalBindSocketPair returns a SocketPairKind that represents
    72  // SocketPairs created with bind() and connect() syscalls with AF_INET6 and the
    73  // given type bound to the IPv6 loopback.
    74  SocketPairKind IPv6UDPBidirectionalBindSocketPair(int type);
    75  
    76  // IPv4UDPBidirectionalBindSocketPair returns a SocketPairKind that represents
    77  // SocketPairs created with bind() and connect() syscalls with AF_INET and the
    78  // given type bound to the IPv4 loopback.
    79  SocketPairKind IPv4UDPBidirectionalBindSocketPair(int type);
    80  
    81  // DualStackUDPBidirectionalBindSocketPair returns a SocketPairKind that
    82  // represents SocketPairs created with bind() and connect() syscalls with
    83  // AF_INET6 and the given type bound to the IPv4 loopback.
    84  SocketPairKind DualStackUDPBidirectionalBindSocketPair(int type);
    85  
    86  // IPv4UDPUnboundSocketPair returns a SocketPairKind that represents
    87  // SocketPairs created with AF_INET and the given type.
    88  SocketPairKind IPv4UDPUnboundSocketPair(int type);
    89  
    90  // ICMPUnboundSocket returns a SocketKind that represents a SimpleSocket created
    91  // with AF_INET, SOCK_DGRAM, IPPROTO_ICMP, and the given type.
    92  SocketKind ICMPUnboundSocket(int type);
    93  
    94  // ICMPv6UnboundSocket returns a SocketKind that represents a SimpleSocket
    95  // created with AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6, and the given type.
    96  SocketKind ICMPv6UnboundSocket(int type);
    97  
    98  // IPv4RawUDPUnboundSocket returns a SocketKind that represents a SimpleSocket
    99  // created with AF_INET, SOCK_RAW, IPPROTO_UDP, and the given type.
   100  SocketKind IPv4RawUDPUnboundSocket(int type);
   101  
   102  // IPv4UDPUnboundSocket returns a SocketKind that represents a SimpleSocket
   103  // created with AF_INET, SOCK_DGRAM, IPPROTO_UDP, and the given type.
   104  SocketKind IPv4UDPUnboundSocket(int type);
   105  
   106  // IPv6UDPUnboundSocket returns a SocketKind that represents a SimpleSocket
   107  // created with AF_INET6, SOCK_DGRAM, IPPROTO_UDP, and the given type.
   108  SocketKind IPv6UDPUnboundSocket(int type);
   109  
   110  // IPv4TCPUnboundSocket returns a SocketKind that represents a SimpleSocket
   111  // created with AF_INET, SOCK_STREAM, IPPROTO_TCP and the given type.
   112  SocketKind IPv4TCPUnboundSocket(int type);
   113  
   114  // IPv6TCPUnboundSocket returns a SocketKind that represents a SimpleSocket
   115  // created with AF_INET6, SOCK_STREAM, IPPROTO_TCP and the given type.
   116  SocketKind IPv6TCPUnboundSocket(int type);
   117  
   118  // GetAddr4Str returns the given IPv4 network address structure as a string.
   119  std::string GetAddr4Str(const in_addr* a);
   120  
   121  // GetAddr6Str returns the given IPv6 network address structure as a string.
   122  std::string GetAddr6Str(const in6_addr* a);
   123  
   124  // GetAddrStr returns the given IPv4 or IPv6 network address structure as a
   125  // string.
   126  std::string GetAddrStr(const sockaddr* a);
   127  
   128  // RecvTOS attempts to read buf_size bytes into buf, and then updates buf_size
   129  // with the numbers of bytes actually read. It expects the IP_TOS cmsg to be
   130  // received. The buffer must already be allocated with at least buf_size size.
   131  void RecvTOS(int sock, char buf[], size_t* buf_size, uint8_t* out_tos);
   132  
   133  // SendTOS sends a message using buf as payload and tos as IP_TOS control
   134  // message.
   135  void SendTOS(int sock, char buf[], size_t buf_size, uint8_t tos);
   136  
   137  // RecvTClass attempts to read buf_size bytes into buf, and then updates
   138  // buf_size with the numbers of bytes actually read. It expects the IPV6_TCLASS
   139  // cmsg to be received. The buffer must already be allocated with at least
   140  // buf_size size.
   141  void RecvTClass(int sock, char buf[], size_t* buf_size, int* out_tclass);
   142  
   143  // SendTClass sends a message using buf as payload and tclass as IP_TOS control
   144  // message.
   145  void SendTClass(int sock, char buf[], size_t buf_size, int tclass);
   146  
   147  // RecvTTL attempts to read buf_size bytes into buf, and then updates buf_size
   148  // with the numbers of bytes actually read. It expects the IP_TTL cmsg to be
   149  // received. The buffer must already be allocated with at least buf_size size.
   150  void RecvTTL(int sock, char buf[], size_t* buf_size, int* out_ttl);
   151  
   152  // SendTTL sends a message using buf as payload and ttl as IP_TOS control
   153  // message.
   154  void SendTTL(int sock, char buf[], size_t buf_size, int ttl);
   155  
   156  // RecvHopLimit attempts to read buf_size bytes into buf, and then updates
   157  // buf_size with the numbers of bytes actually read. It expects the
   158  // IPV6_HOPLIMIT cmsg to be received. The buffer must already be allocated with
   159  // at least buf_size size.
   160  void RecvHopLimit(int sock, char buf[], size_t* buf_size, int* out_hoplimit);
   161  
   162  // SendHopLimit sends a message using buf as payload and hoplimit as IP_HOPLIMIT
   163  // control message.
   164  void SendHopLimit(int sock, char buf[], size_t buf_size, int hoplimit);
   165  // RecvPktInfo attempts to read buf_size bytes into buf, and then updates
   166  // buf_size with the numbers of bytes actually read. It expects the
   167  // IP_PKTINFO cmsg to be received. The buffer must already be allocated with
   168  // at least buf_size size.
   169  void RecvPktInfo(int sock, char buf[], size_t* buf_size,
   170                   in_pktinfo* out_pktinfo);
   171  
   172  // RecvIPv6PktInfo attempts to read buf_size bytes into buf, and then updates
   173  // buf_size with the numbers of bytes actually read. It expects the
   174  // IPV6_PKTINFO cmsg to be received. The buffer must already be allocated with
   175  // at least buf_size size.
   176  void RecvIPv6PktInfo(int sock, char buf[], size_t* buf_size,
   177                       in6_pktinfo* out_pktinfo);
   178  
   179  }  // namespace testing
   180  }  // namespace gvisor
   181  
   182  #endif  // GVISOR_TEST_SYSCALLS_IP_SOCKET_TEST_UTIL_H_