Source File
route_bsd.go
Belonging Package
syscall
package syscall
import (
)
var (
freebsdConfArch string // "machine $arch" line in kern.conftxt on freebsd
minRoutingSockaddrLen = rsaAlignOf(0)
)
= 4
= 8
if freebsdConfArch == "amd64" {
= 8
}
}
if == 0 {
return
}
return ( + - 1) & ^( - 1)
}
func ( []byte, byte) (Sockaddr, error) {
switch {
case AF_INET:
if len() < SizeofSockaddrInet4 {
return nil, EINVAL
}
:= (*RawSockaddrAny)(unsafe.Pointer(&[0]))
return anyToSockaddr()
case AF_INET6:
if len() < SizeofSockaddrInet6 {
return nil, EINVAL
}
:= (*RawSockaddrAny)(unsafe.Pointer(&[0]))
return anyToSockaddr()
default:
return nil, EINVAL
}
}
const (
offsetofInet4 = int(unsafe.Offsetof(RawSockaddrInet4{}.Addr))
offsetofInet6 = int(unsafe.Offsetof(RawSockaddrInet6{}.Addr))
)
switch {
case [0] == SizeofSockaddrInet6:
:= &SockaddrInet6{}
copy(.Addr[:], [offsetofInet6:])
return , nil
case == AF_INET6:
:= &SockaddrInet6{}
if -1 < offsetofInet6 {
copy(.Addr[:], [1:])
} else {
copy(.Addr[:], [-offsetofInet6:])
}
return , nil
case [0] == SizeofSockaddrInet4:
:= &SockaddrInet4{}
copy(.Addr[:], [offsetofInet4:])
return , nil
default: // an old fashion, AF_UNSPEC or unknown means AF_INET
:= &SockaddrInet4{}
if -1 < offsetofInet4 {
copy(.Addr[:], [1:])
} else {
copy(.Addr[:], [-offsetofInet4:])
}
return , nil
}
}
type RoutingMessage interface {
sockaddr() ([]Sockaddr, error)
}
const anyMessageLen = int(unsafe.Sizeof(anyMessage{}))
type anyMessage struct {
Msglen uint16
Version uint8
Type uint8
}
type RouteMessage struct {
Header RtMsghdr
Data []byte
}
func ( *RouteMessage) () ([]Sockaddr, error) {
var [RTAX_MAX]Sockaddr
:= .Data[:]
:= uint8(AF_UNSPEC)
for := uint(0); < RTAX_MAX && len() >= minRoutingSockaddrLen; ++ {
if .Header.Addrs&(1<<) == 0 {
continue
}
:= (*RawSockaddr)(unsafe.Pointer(&[0]))
switch .Family {
case AF_LINK:
, := parseSockaddrLink()
if != nil {
return nil,
}
[] =
= [rsaAlignOf(int(.Len)):]
case AF_INET, AF_INET6:
, := parseSockaddrInet(, .Family)
if != nil {
return nil,
}
[] =
= [rsaAlignOf(int(.Len)):]
= .Family
default:
, := parseNetworkLayerAddr(, )
if != nil {
return nil,
}
[] =
= [rsaAlignOf(int([0])):]
}
}
return [:], nil
}
type InterfaceAddrMessage struct {
Header IfaMsghdr
Data []byte
}
func ( *InterfaceAddrMessage) () ([]Sockaddr, error) {
var [RTAX_MAX]Sockaddr
:= .Data[:]
:= uint8(AF_UNSPEC)
for := uint(0); < RTAX_MAX && len() >= minRoutingSockaddrLen; ++ {
if .Header.Addrs&(1<<) == 0 {
continue
}
:= (*RawSockaddr)(unsafe.Pointer(&[0]))
switch .Family {
case AF_LINK:
, := parseSockaddrLink()
if != nil {
return nil,
}
[] =
= [rsaAlignOf(int(.Len)):]
case AF_INET, AF_INET6:
, := parseSockaddrInet(, .Family)
if != nil {
return nil,
}
[] =
= [rsaAlignOf(int(.Len)):]
= .Family
default:
, := parseNetworkLayerAddr(, )
if != nil {
return nil,
}
[] =
= [rsaAlignOf(int([0])):]
}
}
return [:], nil
}
func ( []byte) ( []RoutingMessage, error) {
, := 0, 0
for len() >= anyMessageLen {
++
:= (*anyMessage)(unsafe.Pointer(&[0]))
if .Version != RTM_VERSION {
= [.Msglen:]
continue
}
if := .toRoutingMessage(); == nil {
++
} else {
= append(, )
}
= [.Msglen:]
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |