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.
+build darwin dragonfly freebsd netbsd openbsd

package syscall

import (
	
	
)

var (
	freebsdConfArch       string // "machine $arch" line in kern.conftxt on freebsd
	minRoutingSockaddrLen = rsaAlignOf(0)
)
Round the length of a raw sockaddr up to align it properly.
func ( int) int {
	 := sizeofPtr
Darwin kernels require 32-bit aligned access to routing facilities.
		 = 4
NetBSD 6 and beyond kernels require 64-bit aligned access to routing facilities.
		 = 8
In the case of kern.supported_archs="amd64 i386", we need to know the underlying kernel's architecture because the alignment for routing facilities are set at the build time of the kernel.
		if freebsdConfArch == "amd64" {
			 = 8
		}
	}
	if  == 0 {
		return 
	}
	return ( +  - 1) & ^( - 1)
}
parseSockaddrLink parses b as a datalink socket address.
func ( []byte) (*SockaddrDatalink, error) {
	if len() < 8 {
		return nil, EINVAL
	}
	, ,  := parseLinkLayerAddr([4:])
	if  != nil {
		return nil, 
	}
	 := (*RawSockaddrDatalink)(unsafe.Pointer(&[0]))
	.Len = .Len
	.Family = .Family
	.Index = .Index
	return , nil
}
parseLinkLayerAddr parses b as a datalink socket address in conventional BSD kernel form.
The encoding looks like the following: +----------------------------+ | Type (1 octet) | +----------------------------+ | Name length (1 octet) | +----------------------------+ | Address length (1 octet) | +----------------------------+ | Selector length (1 octet) | +----------------------------+ | Data (variable) | +----------------------------+
	type  struct {
		 byte
		 byte
		 byte
		 byte
	}
	 := (*)(unsafe.Pointer(&[0]))
	 := 4 + int(.) + int(.) + int(.)
	if len() <  {
		return nil, 0, EINVAL
	}
	 = [4:]
	 := &SockaddrDatalink{Type: ., Nlen: ., Alen: ., Slen: .}
	for  := 0; len(.Data) >  &&  < -4; ++ {
		.Data[] = int8([])
	}
	return , rsaAlignOf(), nil
}
parseSockaddrInet parses b as an internet socket address.
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))
)
parseNetworkLayerAddr parses b as an internet socket address in conventional BSD kernel form.
The encoding looks similar to the NLRI encoding. +----------------------------+ | Length (1 octet) | +----------------------------+ | Address prefix (variable) | +----------------------------+ The differences between the kernel form and the NLRI encoding are: - The length field of the kernel form indicates the prefix length in bytes, not in bits - In the kernel form, zero value of the length field doesn't mean 0.0.0.0/0 or ::/0 - The kernel form appends leading bytes to the prefix field to make the <length, prefix> tuple to be conformed with the routing message boundary
	 := int(rsaAlignOf(int([0])))
	if len() <  {
		return nil, EINVAL
Don't reorder case expressions. The case expressions for IPv6 must come first.
	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
	}
}
RouteRIB returns routing information base, as known as RIB, which consists of network facility information, states and parameters. Deprecated: Use golang.org/x/net/route instead.
func (,  int) ([]byte, error) {
Find size.
	 := uintptr(0)
	if  := sysctl(, nil, &, nil, 0);  != nil {
		return nil, 
	}
	if  == 0 {
		return nil, nil
	}
	 := make([]byte, )
	if  := sysctl(, &[0], &, nil, 0);  != nil {
		return nil, 
	}
	return [:], nil
}
RoutingMessage represents a routing message. Deprecated: Use golang.org/x/net/route instead.
type RoutingMessage interface {
	sockaddr() ([]Sockaddr, error)
}

const anyMessageLen = int(unsafe.Sizeof(anyMessage{}))

type anyMessage struct {
	Msglen  uint16
	Version uint8
	Type    uint8
}
RouteMessage represents a routing message containing routing entries. Deprecated: Use golang.org/x/net/route instead.
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
}
InterfaceMessage represents a routing message containing network interface entries. Deprecated: Use golang.org/x/net/route instead.
type InterfaceMessage struct {
	Header IfMsghdr
	Data   []byte
}

func ( *InterfaceMessage) () ([]Sockaddr, error) {
	var  [RTAX_MAX]Sockaddr
	if .Header.Addrs&RTA_IFP == 0 {
		return nil, nil
	}
	,  := parseSockaddrLink(.Data[:])
	if  != nil {
		return nil, 
	}
	[RTAX_IFP] = 
	return [:], nil
}
InterfaceAddrMessage represents a routing message containing network interface address entries. Deprecated: Use golang.org/x/net/route instead.
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
}
ParseRoutingMessage parses b as routing messages and returns the slice containing the RoutingMessage interfaces. Deprecated: Use golang.org/x/net/route instead.
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:]
We failed to parse any of the messages - version mismatch?
	if  != len()+ {
		return nil, EINVAL
	}
	return , nil
}
ParseRoutingSockaddr parses msg's payload as raw sockaddrs and returns the slice containing the Sockaddr interfaces. Deprecated: Use golang.org/x/net/route instead.
func ( RoutingMessage) ([]Sockaddr, error) {
	,  := .sockaddr()
	if  != nil {
		return nil, 
	}
	return , nil