github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/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/syscalls/linux/socket_test_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 // IPv6TCPAcceptBindSocketPair returns a SocketPairKind that represents 39 // SocketPairs created with bind() and accept() syscalls with AF_INET6 and the 40 // given type bound to the IPv6 loopback. 41 SocketPairKind IPv6TCPAcceptBindSocketPair(int type); 42 43 // IPv4TCPAcceptBindSocketPair returns a SocketPairKind that represents 44 // SocketPairs created with bind() and accept() syscalls with AF_INET and the 45 // given type bound to the IPv4 loopback. 46 SocketPairKind IPv4TCPAcceptBindSocketPair(int type); 47 48 // DualStackTCPAcceptBindSocketPair returns a SocketPairKind that represents 49 // SocketPairs created with bind() and accept() syscalls with AF_INET6 and the 50 // given type bound to the IPv4 loopback. 51 SocketPairKind DualStackTCPAcceptBindSocketPair(int type); 52 53 // IPv6TCPAcceptBindPersistentListenerSocketPair is like 54 // IPv6TCPAcceptBindSocketPair except it uses a persistent listening socket to 55 // create all socket pairs. 56 SocketPairKind IPv6TCPAcceptBindPersistentListenerSocketPair(int type); 57 58 // IPv4TCPAcceptBindPersistentListenerSocketPair is like 59 // IPv4TCPAcceptBindSocketPair except it uses a persistent listening socket to 60 // create all socket pairs. 61 SocketPairKind IPv4TCPAcceptBindPersistentListenerSocketPair(int type); 62 63 // DualStackTCPAcceptBindPersistentListenerSocketPair is like 64 // DualStackTCPAcceptBindSocketPair except it uses a persistent listening socket 65 // to create all socket pairs. 66 SocketPairKind DualStackTCPAcceptBindPersistentListenerSocketPair(int type); 67 68 // IPv6UDPBidirectionalBindSocketPair returns a SocketPairKind that represents 69 // SocketPairs created with bind() and connect() syscalls with AF_INET6 and the 70 // given type bound to the IPv6 loopback. 71 SocketPairKind IPv6UDPBidirectionalBindSocketPair(int type); 72 73 // IPv4UDPBidirectionalBindSocketPair returns a SocketPairKind that represents 74 // SocketPairs created with bind() and connect() syscalls with AF_INET and the 75 // given type bound to the IPv4 loopback. 76 SocketPairKind IPv4UDPBidirectionalBindSocketPair(int type); 77 78 // DualStackUDPBidirectionalBindSocketPair returns a SocketPairKind that 79 // represents SocketPairs created with bind() and connect() syscalls with 80 // AF_INET6 and the given type bound to the IPv4 loopback. 81 SocketPairKind DualStackUDPBidirectionalBindSocketPair(int type); 82 83 // IPv4UDPUnboundSocketPair returns a SocketPairKind that represents 84 // SocketPairs created with AF_INET and the given type. 85 SocketPairKind IPv4UDPUnboundSocketPair(int type); 86 87 // ICMPUnboundSocket returns a SocketKind that represents a SimpleSocket created 88 // with AF_INET, SOCK_DGRAM, IPPROTO_ICMP, and the given type. 89 SocketKind ICMPUnboundSocket(int type); 90 91 // ICMPv6UnboundSocket returns a SocketKind that represents a SimpleSocket 92 // created with AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6, and the given type. 93 SocketKind ICMPv6UnboundSocket(int type); 94 95 // IPv4UDPUnboundSocket returns a SocketKind that represents a SimpleSocket 96 // created with AF_INET, SOCK_DGRAM, IPPROTO_UDP, and the given type. 97 SocketKind IPv4UDPUnboundSocket(int type); 98 99 // IPv6UDPUnboundSocket returns a SocketKind that represents a SimpleSocket 100 // created with AF_INET6, SOCK_DGRAM, IPPROTO_UDP, and the given type. 101 SocketKind IPv6UDPUnboundSocket(int type); 102 103 // IPv4TCPUnboundSocket returns a SocketKind that represents a SimpleSocket 104 // created with AF_INET, SOCK_STREAM, IPPROTO_TCP and the given type. 105 SocketKind IPv4TCPUnboundSocket(int type); 106 107 // IPv6TCPUnboundSocket returns a SocketKind that represents a SimpleSocket 108 // created with AF_INET6, SOCK_STREAM, IPPROTO_TCP and the given type. 109 SocketKind IPv6TCPUnboundSocket(int type); 110 111 // IfAddrHelper is a helper class that determines the local interfaces present 112 // and provides functions to obtain their names, index numbers, and IP address. 113 class IfAddrHelper { 114 public: 115 IfAddrHelper() : ifaddr_(nullptr) {} 116 ~IfAddrHelper() { Release(); } 117 118 PosixError Load(); 119 void Release(); 120 121 std::vector<std::string> InterfaceList(int family) const; 122 123 const sockaddr* GetAddr(int family, std::string name) const; 124 PosixErrorOr<int> GetIndex(std::string name) const; 125 126 private: 127 struct ifaddrs* ifaddr_; 128 }; 129 130 // GetAddr4Str returns the given IPv4 network address structure as a string. 131 std::string GetAddr4Str(const in_addr* a); 132 133 // GetAddr6Str returns the given IPv6 network address structure as a string. 134 std::string GetAddr6Str(const in6_addr* a); 135 136 // GetAddrStr returns the given IPv4 or IPv6 network address structure as a 137 // string. 138 std::string GetAddrStr(const sockaddr* a); 139 140 } // namespace testing 141 } // namespace gvisor 142 143 #endif // GVISOR_TEST_SYSCALLS_IP_SOCKET_TEST_UTIL_H_