github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/src/syscall/route_freebsd.go (about)

     1  // Copyright 2011 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Routing sockets and messages for FreeBSD
     6  
     7  package syscall
     8  
     9  import "unsafe"
    10  
    11  // See http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html.
    12  var freebsdVersion uint32
    13  
    14  func init() {
    15  	freebsdVersion, _ = SysctlUint32("kern.osreldate")
    16  }
    17  
    18  func (any *anyMessage) toRoutingMessage(b []byte) RoutingMessage {
    19  	switch any.Type {
    20  	case RTM_ADD, RTM_DELETE, RTM_CHANGE, RTM_GET, RTM_LOSING, RTM_REDIRECT, RTM_MISS, RTM_LOCK, RTM_RESOLVE:
    21  		p := (*RouteMessage)(unsafe.Pointer(any))
    22  		return &RouteMessage{Header: p.Header, Data: b[SizeofRtMsghdr:any.Msglen]}
    23  	case RTM_IFINFO:
    24  		return any.parseInterfaceMessage(b)
    25  	case RTM_IFANNOUNCE:
    26  		p := (*InterfaceAnnounceMessage)(unsafe.Pointer(any))
    27  		return &InterfaceAnnounceMessage{Header: p.Header}
    28  	case RTM_NEWADDR, RTM_DELADDR:
    29  		p := (*InterfaceAddrMessage)(unsafe.Pointer(any))
    30  		return &InterfaceAddrMessage{Header: p.Header, Data: b[SizeofIfaMsghdr:any.Msglen]}
    31  	case RTM_NEWMADDR, RTM_DELMADDR:
    32  		p := (*InterfaceMulticastAddrMessage)(unsafe.Pointer(any))
    33  		return &InterfaceMulticastAddrMessage{Header: p.Header, Data: b[SizeofIfmaMsghdr:any.Msglen]}
    34  	}
    35  	return nil
    36  }
    37  
    38  // InterfaceAnnounceMessage represents a routing message containing
    39  // network interface arrival and departure information.
    40  type InterfaceAnnounceMessage struct {
    41  	Header IfAnnounceMsghdr
    42  }
    43  
    44  func (m *InterfaceAnnounceMessage) sockaddr() (sas []Sockaddr) { return nil }
    45  
    46  // InterfaceMulticastAddrMessage represents a routing message
    47  // containing network interface address entries.
    48  type InterfaceMulticastAddrMessage struct {
    49  	Header IfmaMsghdr
    50  	Data   []byte
    51  }
    52  
    53  const rtaIfmaMask = RTA_GATEWAY | RTA_IFP | RTA_IFA
    54  
    55  func (m *InterfaceMulticastAddrMessage) sockaddr() (sas []Sockaddr) {
    56  	if m.Header.Addrs&rtaIfmaMask == 0 {
    57  		return nil
    58  	}
    59  	b := m.Data[:]
    60  	for i := uint(0); i < RTAX_MAX; i++ {
    61  		if m.Header.Addrs&rtaIfmaMask&(1<<i) == 0 {
    62  			continue
    63  		}
    64  		rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
    65  		switch i {
    66  		case RTAX_IFA:
    67  			sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa)))
    68  			if e != nil {
    69  				return nil
    70  			}
    71  			sas = append(sas, sa)
    72  		case RTAX_GATEWAY, RTAX_IFP:
    73  			// nothing to do
    74  		}
    75  		b = b[rsaAlignOf(int(rsa.Len)):]
    76  	}
    77  	return sas
    78  }