This commit is contained in:
impact-eintr
2021-09-02 11:47:37 +08:00
parent 12014e60e5
commit 347bc5217b
3 changed files with 158 additions and 14 deletions

View File

@@ -1 +1,50 @@
package rawfile
import (
"fmt"
"syscall"
"github.com/impact-eintr/netstack/tcpip"
)
const maxErrno = 134
var translations [maxErrno]*tcpip.Error
func TranslationErrno(s syscall.Errno) *tcpip.Error {
if err := translations[e]; err != nil {
return err
}
return tcpip.ErrInvalidEndpointState
}
func addTranslation(host syscall.Errno, trans *tcpip.Error) {
if translations[host] != nil {
panic(fmt.Sprintf("duplicate translation for host errno %q(%d)",
host.Error(), host))
}
translations[host] = trans
}
func init() {
addTranslation(syscall.EEXIST, tcpip.ErrDuplicateAddress)
addTranslation(syscall.ENETUNREACH, tcpip.ErrNoRoute)
addTranslation(syscall.EINVAL, tcpip.ErrInvalidEndpointState)
addTranslation(syscall.EALREADY, tcpip.ErrAlreadyConnecting)
addTranslation(syscall.EISCONN, tcpip.ErrAlreadyConnected)
addTranslation(syscall.EADDRINUSE, tcpip.ErrPortInUse)
addTranslation(syscall.EADDRNOTAVAIL, tcpip.ErrBadLocalAddress)
addTranslation(syscall.EPIPE, tcpip.ErrClosedForSend)
addTranslation(syscall.EWOULDBLOCK, tcpip.ErrWouldBlock)
addTranslation(syscall.ECONNREFUSED, tcpip.ErrConnectionRefused)
addTranslation(syscall.ETIMEDOUT, tcpip.ErrTimeout)
addTranslation(syscall.EINPROGRESS, tcpip.ErrConnectStarted)
addTranslation(syscall.EDESTADDRREQ, tcpip.ErrDestinationRequired)
addTranslation(syscall.ENOTSUP, tcpip.ErrNotSupported)
addTranslation(syscall.ENOTTY, tcpip.ErrQueueSizeNotSupported)
addTranslation(syscall.ENOTCONN, tcpip.ErrNotConnected)
addTranslation(syscall.ECONNRESET, tcpip.ErrConnectionReset)
addTranslation(syscall.ECONNABORTED, tcpip.ErrConnectionAborted)
addTranslation(syscall.EMSGSIZE, tcpip.ErrMessageTooLong)
addTranslation(syscall.ENOBUFS, tcpip.ErrNoBufferSpace)
}

View File

@@ -0,0 +1,76 @@
package rawfile
import (
"syscall"
"unsafe"
"github.com/impact-eintr/netstack/tcpip"
)
func GetMTU(name string) (uint32, error) {
fd, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0)
if err != nil {
return 0, err
}
defer syscall.Close(fd)
var ifreq struct {
name [16]byte
mtu int32
_ [20]byte
}
copy(ifreq.name[:], name)
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(fd), syscall.SIOCGIFMTU, uintptr(&ifreq))
if errno != 0 {
return 0, errno
}
return uint32(ifreq.mtu), nil
}
func NonbolockingWrite(fd int, buf []byte) *tcpip.Error {
var ptr unsafe.Pointer
if len(buf) > 0 {
ptr = unsafe.Pointer(&buf[0])
}
_, _, e := syscall.RawSyscall(syscall.SYS_WRITE,
uintptr(fd), uintptr(ptr), uintptr(len(buf)))
if e != 0 {
return TranslationErrno(e)
}
return nil
}
func NonBolckingWrite2(fd int, b1, b2 []byte) *tcpip.Error {
if len(b2) == 0 {
return NonbolockingWrite(fd, b1)
}
iovec := [...]syscall.Iovec{
{
Base: &b1[0],
Len: uint64(len(b1)),
},
{
Base: &b2[0],
Len: uint64(len(b2)),
},
}
_, _, e := syscall.RawSyscall(syscall.SYS_WRITEV,
uintptr(fd), uintptr(unsafe.Pointer(&iovec[0])), uintptr(len(iovec)))
if e != 0 {
return TranslationErrno(e)
}
return nil
}
type pollEvent struct {
fd int32
events int16
revents int16
}

View File

@@ -27,5 +27,24 @@ var (
ErrAlreadConnected = &Error{msg: "endpoint is already connected", ignoreStats: true}
ErrNoPortAvailable = &Error{msg: "no port are available"}
ErrPortInUse = &Error{msg: "port is in use"}
ErrBadLocalAddress = &
ErrBadLocalAddress = &Error{msg: "bad local address"}
ErrClosedForSend = &Error{msg: "endpoint is closed for send"}
ErrClosedForReceive = &Error{msg: "endpoint is closed for receive"}
ErrWouldBlock = &Error{msg: "operation would block", ignoreStats: true}
ErrConnectionRefused = &Error{msg: "connection was refused"}
ErrTimeout = &Error{msg: "operation timed out"}
ErrAborted = &Error{msg: "operation aborted"}
ErrConnectStarted = &Error{msg: "connection address is required"}
ErrDestinationRequired = &Error{msg: "destination address is required"}
ErrNotSupported = &Error{msg: "operation not supported"}
ErrQueueSizeNotSupported = &Error{msg: "queue size querying not supported"}
ErrNotConnected = &Error{msg: "endpoint not connected"}
ErrConnectionReset = &Error{msg: "connection reset by peer"}
ErrConnectionAborted = &Error{msg: "connection aborted"}
ErrNoSuchFile = &Error{msg: "invalid option value specified"}
ErrNoLinkAddress = &Error{msg: "no remote link address"}
ErrBadAddress = &Error{msg: "bad adress"}
ErrNetworkUnreachable = &Error{msg: "network is unreachable"}
ErrMessageTooLong = &Error{msg: "message too long"}
ErrNoBufferSpace = &Error{msg: "no buffer space available"}
)