github.com/nicocha30/gvisor-ligolo@v0.0.0-20230726075806-989fa2c0a413/pkg/sentry/inet/test_stack.go (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 package inet 16 17 import ( 18 "bytes" 19 "fmt" 20 "time" 21 22 "github.com/nicocha30/gvisor-ligolo/pkg/tcpip" 23 "github.com/nicocha30/gvisor-ligolo/pkg/tcpip/stack" 24 ) 25 26 // TestStack is a dummy implementation of Stack for tests. 27 type TestStack struct { 28 InterfacesMap map[int32]Interface 29 InterfaceAddrsMap map[int32][]InterfaceAddr 30 RouteList []Route 31 SupportsIPv6Flag bool 32 TCPRecvBufSize TCPBufferSize 33 TCPSendBufSize TCPBufferSize 34 TCPSACKFlag bool 35 Recovery TCPLossRecovery 36 IPForwarding bool 37 } 38 39 // NewTestStack returns a TestStack with no network interfaces. The value of 40 // all other options is unspecified; tests that rely on specific values must 41 // set them explicitly. 42 func NewTestStack() *TestStack { 43 return &TestStack{ 44 InterfacesMap: make(map[int32]Interface), 45 InterfaceAddrsMap: make(map[int32][]InterfaceAddr), 46 } 47 } 48 49 // Interfaces implements Stack. 50 func (s *TestStack) Interfaces() map[int32]Interface { 51 return s.InterfacesMap 52 } 53 54 // Destroy implements Stack. 55 func (s *TestStack) Destroy() { 56 } 57 58 // RemoveInterface implements Stack. 59 func (s *TestStack) RemoveInterface(idx int32) error { 60 delete(s.InterfacesMap, idx) 61 return nil 62 } 63 64 // InterfaceAddrs implements Stack. 65 func (s *TestStack) InterfaceAddrs() map[int32][]InterfaceAddr { 66 return s.InterfaceAddrsMap 67 } 68 69 // AddInterfaceAddr implements Stack. 70 func (s *TestStack) AddInterfaceAddr(idx int32, addr InterfaceAddr) error { 71 s.InterfaceAddrsMap[idx] = append(s.InterfaceAddrsMap[idx], addr) 72 return nil 73 } 74 75 // RemoveInterfaceAddr implements Stack. 76 func (s *TestStack) RemoveInterfaceAddr(idx int32, addr InterfaceAddr) error { 77 interfaceAddrs, ok := s.InterfaceAddrsMap[idx] 78 if !ok { 79 return fmt.Errorf("unknown idx: %d", idx) 80 } 81 82 var filteredAddrs []InterfaceAddr 83 for _, interfaceAddr := range interfaceAddrs { 84 if !bytes.Equal(interfaceAddr.Addr, addr.Addr) { 85 filteredAddrs = append(filteredAddrs, addr) 86 } 87 } 88 s.InterfaceAddrsMap[idx] = filteredAddrs 89 90 return nil 91 } 92 93 // SupportsIPv6 implements Stack. 94 func (s *TestStack) SupportsIPv6() bool { 95 return s.SupportsIPv6Flag 96 } 97 98 // TCPReceiveBufferSize implements Stack. 99 func (s *TestStack) TCPReceiveBufferSize() (TCPBufferSize, error) { 100 return s.TCPRecvBufSize, nil 101 } 102 103 // SetTCPReceiveBufferSize implements Stack. 104 func (s *TestStack) SetTCPReceiveBufferSize(size TCPBufferSize) error { 105 s.TCPRecvBufSize = size 106 return nil 107 } 108 109 // TCPSendBufferSize implements Stack. 110 func (s *TestStack) TCPSendBufferSize() (TCPBufferSize, error) { 111 return s.TCPSendBufSize, nil 112 } 113 114 // SetTCPSendBufferSize implements Stack. 115 func (s *TestStack) SetTCPSendBufferSize(size TCPBufferSize) error { 116 s.TCPSendBufSize = size 117 return nil 118 } 119 120 // TCPSACKEnabled implements Stack. 121 func (s *TestStack) TCPSACKEnabled() (bool, error) { 122 return s.TCPSACKFlag, nil 123 } 124 125 // SetTCPSACKEnabled implements Stack. 126 func (s *TestStack) SetTCPSACKEnabled(enabled bool) error { 127 s.TCPSACKFlag = enabled 128 return nil 129 } 130 131 // TCPRecovery implements Stack. 132 func (s *TestStack) TCPRecovery() (TCPLossRecovery, error) { 133 return s.Recovery, nil 134 } 135 136 // SetTCPRecovery implements Stack. 137 func (s *TestStack) SetTCPRecovery(recovery TCPLossRecovery) error { 138 s.Recovery = recovery 139 return nil 140 } 141 142 // Statistics implements Stack. 143 func (s *TestStack) Statistics(stat any, arg string) error { 144 return nil 145 } 146 147 // RouteTable implements Stack. 148 func (s *TestStack) RouteTable() []Route { 149 return s.RouteList 150 } 151 152 // Pause implements Stack. 153 func (s *TestStack) Pause() {} 154 155 // Resume implements Stack. 156 func (s *TestStack) Resume() {} 157 158 // RegisteredEndpoints implements Stack. 159 func (s *TestStack) RegisteredEndpoints() []stack.TransportEndpoint { 160 return nil 161 } 162 163 // CleanupEndpoints implements Stack. 164 func (s *TestStack) CleanupEndpoints() []stack.TransportEndpoint { 165 return nil 166 } 167 168 // RestoreCleanupEndpoints implements Stack. 169 func (s *TestStack) RestoreCleanupEndpoints([]stack.TransportEndpoint) {} 170 171 // SetForwarding implements Stack. 172 func (s *TestStack) SetForwarding(protocol tcpip.NetworkProtocolNumber, enable bool) error { 173 s.IPForwarding = enable 174 return nil 175 } 176 177 // PortRange implements Stack. 178 func (*TestStack) PortRange() (uint16, uint16) { 179 // Use the default Linux values per net/ipv4/af_inet.c:inet_init_net(). 180 return 32768, 60999 181 } 182 183 // SetPortRange implements Stack. 184 func (*TestStack) SetPortRange(start uint16, end uint16) error { 185 // No-op. 186 return nil 187 } 188 189 // GROTimeout implements Stack. 190 func (*TestStack) GROTimeout(NICID int32) (time.Duration, error) { 191 // No-op. 192 return 0, nil 193 } 194 195 // SetGROTimeout implements Stack. 196 func (*TestStack) SetGROTimeout(NICID int32, timeout time.Duration) error { 197 // No-op. 198 return nil 199 }