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