mirror of https://github.com/tidwall/evio.git
40 lines
868 B
Go
40 lines
868 B
Go
// Copyright 2018 Joshua J Baker. All rights reserved.
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package internal
|
|
|
|
import (
|
|
"net"
|
|
"syscall"
|
|
)
|
|
|
|
// SockaddrToAddr returns a go/net friendly address
|
|
func SockaddrToAddr(sa syscall.Sockaddr) net.Addr {
|
|
var a net.Addr
|
|
switch sa := sa.(type) {
|
|
case *syscall.SockaddrInet4:
|
|
a = &net.TCPAddr{
|
|
IP: append([]byte{}, sa.Addr[:]...),
|
|
Port: sa.Port,
|
|
}
|
|
case *syscall.SockaddrInet6:
|
|
var zone string
|
|
if sa.ZoneId != 0 {
|
|
if ifi, err := net.InterfaceByIndex(int(sa.ZoneId)); err == nil {
|
|
zone = ifi.Name
|
|
}
|
|
}
|
|
if zone == "" && sa.ZoneId != 0 {
|
|
}
|
|
a = &net.TCPAddr{
|
|
IP: append([]byte{}, sa.Addr[:]...),
|
|
Port: sa.Port,
|
|
Zone: zone,
|
|
}
|
|
case *syscall.SockaddrUnix:
|
|
a = &net.UnixAddr{Net: "unix", Name: sa.Name}
|
|
}
|
|
return a
|
|
}
|