github.com/sagernet/gvisor@v0.0.0-20240428053021-e691de28565f/pkg/tcpip/transport/internal/noop/endpoint.go (about) 1 // Copyright 2021 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 noop contains an endpoint that implements all tcpip.Endpoint 16 // functions as noops. 17 package noop 18 19 import ( 20 "fmt" 21 "io" 22 23 "github.com/sagernet/gvisor/pkg/tcpip" 24 "github.com/sagernet/gvisor/pkg/tcpip/stack" 25 "github.com/sagernet/gvisor/pkg/waiter" 26 ) 27 28 // endpoint can be created, but all interactions have no effect or 29 // return errors. 30 // 31 // +stateify savable 32 type endpoint struct { 33 tcpip.DefaultSocketOptionsHandler 34 ops tcpip.SocketOptions 35 } 36 37 // New returns an initialized noop endpoint. 38 func New(stk *stack.Stack) tcpip.Endpoint { 39 // ep.ops must be in a valid, initialized state for callers of 40 // ep.SocketOptions. 41 var ep endpoint 42 ep.ops.InitHandler(&ep, stk, tcpip.GetStackSendBufferLimits, tcpip.GetStackReceiveBufferLimits) 43 return &ep 44 } 45 46 // Abort implements stack.TransportEndpoint.Abort. 47 func (*endpoint) Abort() { 48 // No-op. 49 } 50 51 // Close implements tcpip.Endpoint.Close. 52 func (*endpoint) Close() { 53 // No-op. 54 } 55 56 // ModerateRecvBuf implements tcpip.Endpoint.ModerateRecvBuf. 57 func (*endpoint) ModerateRecvBuf(int) { 58 // No-op. 59 } 60 61 func (*endpoint) SetOwner(tcpip.PacketOwner) { 62 // No-op. 63 } 64 65 // Read implements tcpip.Endpoint.Read. 66 func (*endpoint) Read(io.Writer, tcpip.ReadOptions) (tcpip.ReadResult, tcpip.Error) { 67 return tcpip.ReadResult{}, &tcpip.ErrNotPermitted{} 68 } 69 70 // Write implements tcpip.Endpoint.Write. 71 func (*endpoint) Write(tcpip.Payloader, tcpip.WriteOptions) (int64, tcpip.Error) { 72 return 0, &tcpip.ErrNotPermitted{} 73 } 74 75 // Disconnect implements tcpip.Endpoint.Disconnect. 76 func (*endpoint) Disconnect() tcpip.Error { 77 return &tcpip.ErrNotSupported{} 78 } 79 80 // Connect implements tcpip.Endpoint.Connect. 81 func (*endpoint) Connect(tcpip.FullAddress) tcpip.Error { 82 return &tcpip.ErrNotPermitted{} 83 } 84 85 // Shutdown implements tcpip.Endpoint.Shutdown. 86 func (*endpoint) Shutdown(tcpip.ShutdownFlags) tcpip.Error { 87 return &tcpip.ErrNotPermitted{} 88 } 89 90 // Listen implements tcpip.Endpoint.Listen. 91 func (*endpoint) Listen(int) tcpip.Error { 92 return &tcpip.ErrNotSupported{} 93 } 94 95 // Accept implements tcpip.Endpoint.Accept. 96 func (*endpoint) Accept(*tcpip.FullAddress) (tcpip.Endpoint, *waiter.Queue, tcpip.Error) { 97 return nil, nil, &tcpip.ErrNotSupported{} 98 } 99 100 // Bind implements tcpip.Endpoint.Bind. 101 func (*endpoint) Bind(tcpip.FullAddress) tcpip.Error { 102 return &tcpip.ErrNotPermitted{} 103 } 104 105 // GetLocalAddress implements tcpip.Endpoint.GetLocalAddress. 106 func (*endpoint) GetLocalAddress() (tcpip.FullAddress, tcpip.Error) { 107 return tcpip.FullAddress{}, &tcpip.ErrNotSupported{} 108 } 109 110 // GetRemoteAddress implements tcpip.Endpoint.GetRemoteAddress. 111 func (*endpoint) GetRemoteAddress() (tcpip.FullAddress, tcpip.Error) { 112 return tcpip.FullAddress{}, &tcpip.ErrNotConnected{} 113 } 114 115 // Readiness implements tcpip.Endpoint.Readiness. 116 func (*endpoint) Readiness(waiter.EventMask) waiter.EventMask { 117 return 0 118 } 119 120 // SetSockOpt implements tcpip.Endpoint.SetSockOpt. 121 func (*endpoint) SetSockOpt(tcpip.SettableSocketOption) tcpip.Error { 122 return &tcpip.ErrUnknownProtocolOption{} 123 } 124 125 func (*endpoint) SetSockOptInt(tcpip.SockOptInt, int) tcpip.Error { 126 return &tcpip.ErrUnknownProtocolOption{} 127 } 128 129 // GetSockOpt implements tcpip.Endpoint.GetSockOpt. 130 func (*endpoint) GetSockOpt(tcpip.GettableSocketOption) tcpip.Error { 131 return &tcpip.ErrUnknownProtocolOption{} 132 } 133 134 // GetSockOptInt implements tcpip.Endpoint.GetSockOptInt. 135 func (*endpoint) GetSockOptInt(tcpip.SockOptInt) (int, tcpip.Error) { 136 return 0, &tcpip.ErrUnknownProtocolOption{} 137 } 138 139 // HandlePacket implements stack.RawTransportEndpoint.HandlePacket. 140 func (*endpoint) HandlePacket(pkt *stack.PacketBuffer) { 141 panic(fmt.Sprintf("unreachable: noop.endpoint should never be registered, but got packet: %+v", pkt)) 142 } 143 144 // State implements socket.Socket.State. 145 func (*endpoint) State() uint32 { 146 return 0 147 } 148 149 // Wait implements stack.TransportEndpoint.Wait. 150 func (*endpoint) Wait() { 151 // No-op. 152 } 153 154 // Release implements stack.TransportEndpoint.Release. 155 func (*endpoint) Release() { 156 // No-op. 157 } 158 159 // LastError implements tcpip.Endpoint.LastError. 160 func (*endpoint) LastError() tcpip.Error { 161 return nil 162 } 163 164 // SocketOptions implements tcpip.Endpoint.SocketOptions. 165 func (ep *endpoint) SocketOptions() *tcpip.SocketOptions { 166 return &ep.ops 167 } 168 169 // Info implements tcpip.Endpoint.Info. 170 func (*endpoint) Info() tcpip.EndpointInfo { 171 return &stack.TransportEndpointInfo{} 172 } 173 174 // Stats returns a pointer to the endpoint stats. 175 func (*endpoint) Stats() tcpip.EndpointStats { 176 return &tcpip.TransportEndpointStats{} 177 }