github.com/FlowerWrong/netstack@v0.0.0-20191009141956-e5848263af28/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  package rawfile
    16  
    17  import (
    18  	"fmt"
    19  	"syscall"
    20  
    21  	"github.com/FlowerWrong/netstack/tcpip"
    22  )
    23  
    24  const maxErrno = 134
    25  
    26  var translations [maxErrno]*tcpip.Error
    27  
    28  // TranslateErrno translate an errno from the syscall package into a
    29  // *tcpip.Error.
    30  //
    31  // Valid, but unrecognized errnos will be translated to
    32  // tcpip.ErrInvalidEndpointState (EINVAL). Panics on invalid errnos.
    33  func TranslateErrno(e syscall.Errno) *tcpip.Error {
    34  	if err := translations[e]; err != nil {
    35  		return err
    36  	}
    37  	return tcpip.ErrInvalidEndpointState
    38  }
    39  
    40  func addTranslation(host syscall.Errno, trans *tcpip.Error) {
    41  	if translations[host] != nil {
    42  		panic(fmt.Sprintf("duplicate translation for host errno %q (%d)", host.Error(), host))
    43  	}
    44  	translations[host] = trans
    45  }
    46  
    47  func init() {
    48  	addTranslation(syscall.EEXIST, tcpip.ErrDuplicateAddress)
    49  	addTranslation(syscall.ENETUNREACH, tcpip.ErrNoRoute)
    50  	addTranslation(syscall.EINVAL, tcpip.ErrInvalidEndpointState)
    51  	addTranslation(syscall.EALREADY, tcpip.ErrAlreadyConnecting)
    52  	addTranslation(syscall.EISCONN, tcpip.ErrAlreadyConnected)
    53  	addTranslation(syscall.EADDRINUSE, tcpip.ErrPortInUse)
    54  	addTranslation(syscall.EADDRNOTAVAIL, tcpip.ErrBadLocalAddress)
    55  	addTranslation(syscall.EPIPE, tcpip.ErrClosedForSend)
    56  	addTranslation(syscall.EWOULDBLOCK, tcpip.ErrWouldBlock)
    57  	addTranslation(syscall.ECONNREFUSED, tcpip.ErrConnectionRefused)
    58  	addTranslation(syscall.ETIMEDOUT, tcpip.ErrTimeout)
    59  	addTranslation(syscall.EINPROGRESS, tcpip.ErrConnectStarted)
    60  	addTranslation(syscall.EDESTADDRREQ, tcpip.ErrDestinationRequired)
    61  	addTranslation(syscall.ENOTSUP, tcpip.ErrNotSupported)
    62  	addTranslation(syscall.ENOTTY, tcpip.ErrQueueSizeNotSupported)
    63  	addTranslation(syscall.ENOTCONN, tcpip.ErrNotConnected)
    64  	addTranslation(syscall.ECONNRESET, tcpip.ErrConnectionReset)
    65  	addTranslation(syscall.ECONNABORTED, tcpip.ErrConnectionAborted)
    66  	addTranslation(syscall.EMSGSIZE, tcpip.ErrMessageTooLong)
    67  	addTranslation(syscall.ENOBUFS, tcpip.ErrNoBufferSpace)
    68  }