github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/link/rawfile/errors.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  // +build linux
    16  
    17  package rawfile
    18  
    19  import (
    20  	"golang.org/x/sys/unix"
    21  	"github.com/SagerNet/gvisor/pkg/tcpip"
    22  )
    23  
    24  const maxErrno = 134
    25  
    26  // TranslateErrno translate an errno from the syscall package into a
    27  // tcpip.Error.
    28  //
    29  // Valid, but unrecognized errnos will be translated to
    30  // *tcpip.ErrInvalidEndpointState (EINVAL).
    31  func TranslateErrno(e unix.Errno) tcpip.Error {
    32  	switch e {
    33  	case unix.EEXIST:
    34  		return &tcpip.ErrDuplicateAddress{}
    35  	case unix.ENETUNREACH:
    36  		return &tcpip.ErrNoRoute{}
    37  	case unix.EINVAL:
    38  		return &tcpip.ErrInvalidEndpointState{}
    39  	case unix.EALREADY:
    40  		return &tcpip.ErrAlreadyConnecting{}
    41  	case unix.EISCONN:
    42  		return &tcpip.ErrAlreadyConnected{}
    43  	case unix.EADDRINUSE:
    44  		return &tcpip.ErrPortInUse{}
    45  	case unix.EADDRNOTAVAIL:
    46  		return &tcpip.ErrBadLocalAddress{}
    47  	case unix.EPIPE:
    48  		return &tcpip.ErrClosedForSend{}
    49  	case unix.EWOULDBLOCK:
    50  		return &tcpip.ErrWouldBlock{}
    51  	case unix.ECONNREFUSED:
    52  		return &tcpip.ErrConnectionRefused{}
    53  	case unix.ETIMEDOUT:
    54  		return &tcpip.ErrTimeout{}
    55  	case unix.EINPROGRESS:
    56  		return &tcpip.ErrConnectStarted{}
    57  	case unix.EDESTADDRREQ:
    58  		return &tcpip.ErrDestinationRequired{}
    59  	case unix.ENOTSUP:
    60  		return &tcpip.ErrNotSupported{}
    61  	case unix.ENOTTY:
    62  		return &tcpip.ErrQueueSizeNotSupported{}
    63  	case unix.ENOTCONN:
    64  		return &tcpip.ErrNotConnected{}
    65  	case unix.ECONNRESET:
    66  		return &tcpip.ErrConnectionReset{}
    67  	case unix.ECONNABORTED:
    68  		return &tcpip.ErrConnectionAborted{}
    69  	case unix.EMSGSIZE:
    70  		return &tcpip.ErrMessageTooLong{}
    71  	case unix.ENOBUFS:
    72  		return &tcpip.ErrNoBufferSpace{}
    73  	default:
    74  		return &tcpip.ErrInvalidEndpointState{}
    75  	}
    76  }