2011-05-26 18:02:03 -06:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2013-08-23 09:51:25 -06:00
|
|
|
// +build darwin dragonfly freebsd netbsd openbsd
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-05-26 18:02:03 -06:00
|
|
|
// Routing sockets and messages
|
|
|
|
|
|
|
|
package syscall
|
|
|
|
|
2013-02-23 20:36:44 -07:00
|
|
|
import "unsafe"
|
2011-05-26 18:02:03 -06:00
|
|
|
|
2011-05-30 02:02:59 -06:00
|
|
|
// Round the length of a raw sockaddr up to align it properly.
|
2011-05-26 18:02:03 -06:00
|
|
|
func rsaAlignOf(salen int) int {
|
|
|
|
salign := sizeofPtr
|
2013-08-24 17:44:31 -06:00
|
|
|
// NOTE: It seems like 64-bit Darwin kernel still requires
|
|
|
|
// 32-bit aligned access to BSD subsystem. Also NetBSD 6
|
|
|
|
// kernel and beyond require 64-bit aligned access to routing
|
|
|
|
// facilities.
|
|
|
|
if darwin64Bit {
|
2011-05-26 18:02:03 -06:00
|
|
|
salign = 4
|
2013-08-24 17:44:31 -06:00
|
|
|
} else if netbsd32Bit {
|
|
|
|
salign = 8
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
|
|
|
if salen == 0 {
|
|
|
|
return salign
|
|
|
|
}
|
|
|
|
return (salen + salign - 1) & ^(salign - 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteRIB returns routing information base, as known as RIB,
|
|
|
|
// which consists of network facility information, states and
|
|
|
|
// parameters.
|
2011-11-13 20:44:52 -07:00
|
|
|
func RouteRIB(facility, param int) ([]byte, error) {
|
2011-05-26 18:02:03 -06:00
|
|
|
mib := []_C_int{CTL_NET, AF_ROUTE, 0, 0, _C_int(facility), _C_int(param)}
|
|
|
|
// Find size.
|
|
|
|
n := uintptr(0)
|
2011-11-13 20:44:52 -07:00
|
|
|
if err := sysctl(mib, nil, &n, nil, 0); err != nil {
|
|
|
|
return nil, err
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
|
|
|
if n == 0 {
|
2011-11-13 20:44:52 -07:00
|
|
|
return nil, nil
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
tab := make([]byte, n)
|
|
|
|
if err := sysctl(mib, &tab[0], &n, nil, 0); err != nil {
|
|
|
|
return nil, err
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return tab[:n], nil
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// RoutingMessage represents a routing message.
|
|
|
|
type RoutingMessage interface {
|
|
|
|
sockaddr() []Sockaddr
|
|
|
|
}
|
|
|
|
|
2011-06-17 15:07:21 -06:00
|
|
|
const anyMessageLen = int(unsafe.Sizeof(anyMessage{}))
|
2011-05-26 18:02:03 -06:00
|
|
|
|
|
|
|
type anyMessage struct {
|
|
|
|
Msglen uint16
|
|
|
|
Version uint8
|
|
|
|
Type uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouteMessage represents a routing message containing routing
|
|
|
|
// entries.
|
|
|
|
type RouteMessage struct {
|
|
|
|
Header RtMsghdr
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2011-08-31 11:05:49 -06:00
|
|
|
const rtaRtMask = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_GENMASK
|
|
|
|
|
|
|
|
func (m *RouteMessage) sockaddr() []Sockaddr {
|
|
|
|
var (
|
|
|
|
af int
|
|
|
|
sas [4]Sockaddr
|
|
|
|
)
|
2013-02-23 20:36:44 -07:00
|
|
|
b := m.Data[:]
|
2011-08-31 11:05:49 -06:00
|
|
|
for i := uint(0); i < RTAX_MAX; i++ {
|
|
|
|
if m.Header.Addrs&rtaRtMask&(1<<i) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2013-02-23 20:36:44 -07:00
|
|
|
rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
|
2011-08-31 11:05:49 -06:00
|
|
|
switch i {
|
|
|
|
case RTAX_DST, RTAX_GATEWAY:
|
2011-12-21 05:39:00 -07:00
|
|
|
sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa)))
|
|
|
|
if err != nil {
|
2011-08-31 11:05:49 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if i == RTAX_DST {
|
|
|
|
af = int(rsa.Family)
|
|
|
|
}
|
|
|
|
sas[i] = sa
|
|
|
|
case RTAX_NETMASK, RTAX_GENMASK:
|
|
|
|
switch af {
|
|
|
|
case AF_INET:
|
2013-02-23 20:36:44 -07:00
|
|
|
rsa4 := (*RawSockaddrInet4)(unsafe.Pointer(&b[0]))
|
2011-08-31 11:05:49 -06:00
|
|
|
sa := new(SockaddrInet4)
|
|
|
|
for j := 0; rsa4.Len > 0 && j < int(rsa4.Len)-int(unsafe.Offsetof(rsa4.Addr)); j++ {
|
|
|
|
sa.Addr[j] = rsa4.Addr[j]
|
|
|
|
}
|
|
|
|
sas[i] = sa
|
|
|
|
case AF_INET6:
|
2013-02-23 20:36:44 -07:00
|
|
|
rsa6 := (*RawSockaddrInet6)(unsafe.Pointer(&b[0]))
|
2011-08-31 11:05:49 -06:00
|
|
|
sa := new(SockaddrInet6)
|
|
|
|
for j := 0; rsa6.Len > 0 && j < int(rsa6.Len)-int(unsafe.Offsetof(rsa6.Addr)); j++ {
|
|
|
|
sa.Addr[j] = rsa6.Addr[j]
|
|
|
|
}
|
|
|
|
sas[i] = sa
|
|
|
|
}
|
|
|
|
}
|
2013-02-23 20:36:44 -07:00
|
|
|
b = b[rsaAlignOf(int(rsa.Len)):]
|
2011-08-31 11:05:49 -06:00
|
|
|
}
|
|
|
|
return sas[:]
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceMessage represents a routing message containing
|
|
|
|
// network interface entries.
|
|
|
|
type InterfaceMessage struct {
|
|
|
|
Header IfMsghdr
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InterfaceMessage) sockaddr() (sas []Sockaddr) {
|
|
|
|
if m.Header.Addrs&RTA_IFP == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2011-12-21 05:39:00 -07:00
|
|
|
sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(&m.Data[0])))
|
|
|
|
if err != nil {
|
2011-05-26 18:02:03 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return append(sas, sa)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InterfaceAddrMessage represents a routing message containing
|
|
|
|
// network interface address entries.
|
|
|
|
type InterfaceAddrMessage struct {
|
|
|
|
Header IfaMsghdr
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2011-08-03 22:22:48 -06:00
|
|
|
const rtaIfaMask = RTA_IFA | RTA_NETMASK | RTA_BRD
|
2011-05-26 18:02:03 -06:00
|
|
|
|
|
|
|
func (m *InterfaceAddrMessage) sockaddr() (sas []Sockaddr) {
|
2011-08-03 22:22:48 -06:00
|
|
|
if m.Header.Addrs&rtaIfaMask == 0 {
|
2011-05-26 18:02:03 -06:00
|
|
|
return nil
|
|
|
|
}
|
2013-02-23 20:36:44 -07:00
|
|
|
b := m.Data[:]
|
2013-08-24 17:44:31 -06:00
|
|
|
// We still see AF_UNSPEC in socket addresses on some
|
|
|
|
// platforms. To identify each address family correctly, we
|
|
|
|
// will use the address family of RTAX_NETMASK as a preferred
|
|
|
|
// one on the 32-bit NetBSD kernel, also use the length of
|
|
|
|
// RTAX_NETMASK socket address on the FreeBSD kernel.
|
|
|
|
preferredFamily := uint8(AF_UNSPEC)
|
2011-05-26 18:02:03 -06:00
|
|
|
for i := uint(0); i < RTAX_MAX; i++ {
|
2014-10-28 09:35:00 -06:00
|
|
|
if m.Header.Addrs&(1<<i) == 0 {
|
2011-05-26 18:02:03 -06:00
|
|
|
continue
|
|
|
|
}
|
2013-02-23 20:36:44 -07:00
|
|
|
rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
|
2011-05-26 18:02:03 -06:00
|
|
|
switch i {
|
|
|
|
case RTAX_IFA:
|
2013-08-24 17:44:31 -06:00
|
|
|
if rsa.Family == AF_UNSPEC {
|
|
|
|
rsa.Family = preferredFamily
|
|
|
|
}
|
2011-12-21 05:39:00 -07:00
|
|
|
sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa)))
|
|
|
|
if err != nil {
|
2011-05-26 18:02:03 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sas = append(sas, sa)
|
2011-12-21 05:39:00 -07:00
|
|
|
case RTAX_NETMASK:
|
2013-08-24 17:44:31 -06:00
|
|
|
switch rsa.Family {
|
|
|
|
case AF_UNSPEC:
|
2013-08-06 09:25:23 -06:00
|
|
|
switch rsa.Len {
|
|
|
|
case SizeofSockaddrInet4:
|
|
|
|
rsa.Family = AF_INET
|
|
|
|
case SizeofSockaddrInet6:
|
|
|
|
rsa.Family = AF_INET6
|
|
|
|
default:
|
2013-08-24 17:44:31 -06:00
|
|
|
rsa.Family = AF_INET // an old fashion, AF_UNSPEC means AF_INET
|
2013-08-06 09:25:23 -06:00
|
|
|
}
|
2013-08-24 17:44:31 -06:00
|
|
|
case AF_INET, AF_INET6:
|
|
|
|
preferredFamily = rsa.Family
|
|
|
|
default:
|
|
|
|
return nil
|
2011-12-21 05:39:00 -07:00
|
|
|
}
|
|
|
|
sa, err := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa)))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sas = append(sas, sa)
|
|
|
|
case RTAX_BRD:
|
2011-05-26 18:02:03 -06:00
|
|
|
// nothing to do
|
|
|
|
}
|
2013-02-23 20:36:44 -07:00
|
|
|
b = b[rsaAlignOf(int(rsa.Len)):]
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
|
|
|
return sas
|
|
|
|
}
|
|
|
|
|
2013-02-23 20:36:44 -07:00
|
|
|
// ParseRoutingMessage parses b as routing messages and returns the
|
|
|
|
// slice containing the RoutingMessage interfaces.
|
|
|
|
func ParseRoutingMessage(b []byte) (msgs []RoutingMessage, err error) {
|
2013-12-10 06:03:46 -07:00
|
|
|
msgCount := 0
|
2013-02-23 20:36:44 -07:00
|
|
|
for len(b) >= anyMessageLen {
|
2013-12-10 06:03:46 -07:00
|
|
|
msgCount++
|
2013-02-23 20:36:44 -07:00
|
|
|
any := (*anyMessage)(unsafe.Pointer(&b[0]))
|
2011-05-26 18:02:03 -06:00
|
|
|
if any.Version != RTM_VERSION {
|
2013-12-10 06:03:46 -07:00
|
|
|
b = b[any.Msglen:]
|
|
|
|
continue
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
2013-02-23 20:36:44 -07:00
|
|
|
msgs = append(msgs, any.toRoutingMessage(b))
|
|
|
|
b = b[any.Msglen:]
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
2013-12-10 06:03:46 -07:00
|
|
|
// We failed to parse any of the messages - version mismatch?
|
|
|
|
if msgCount > 0 && len(msgs) == 0 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
2011-11-13 20:44:52 -07:00
|
|
|
return msgs, nil
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRoutingMessage parses msg's payload as raw sockaddrs and
|
|
|
|
// returns the slice containing the Sockaddr interfaces.
|
2011-11-13 20:44:52 -07:00
|
|
|
func ParseRoutingSockaddr(msg RoutingMessage) (sas []Sockaddr, err error) {
|
|
|
|
return append(sas, msg.sockaddr()...), nil
|
2011-05-26 18:02:03 -06:00
|
|
|
}
|