errors_linux.go raw

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