github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/src/syscall/route_darwin.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 Darwin
     6  
     7  package syscall
     8  
     9  import "unsafe"
    10  
    11  func (any *anyMessage) toRoutingMessage(b []byte) RoutingMessage {
    12  	switch any.Type {
    13  	case RTM_ADD, RTM_DELETE, RTM_CHANGE, RTM_GET, RTM_LOSING, RTM_REDIRECT, RTM_MISS, RTM_LOCK, RTM_RESOLVE:
    14  		p := (*RouteMessage)(unsafe.Pointer(any))
    15  		return &RouteMessage{Header: p.Header, Data: b[SizeofRtMsghdr:any.Msglen]}
    16  	case RTM_IFINFO:
    17  		p := (*InterfaceMessage)(unsafe.Pointer(any))
    18  		return &InterfaceMessage{Header: p.Header, Data: b[SizeofIfMsghdr:any.Msglen]}
    19  	case RTM_NEWADDR, RTM_DELADDR:
    20  		p := (*InterfaceAddrMessage)(unsafe.Pointer(any))
    21  		return &InterfaceAddrMessage{Header: p.Header, Data: b[SizeofIfaMsghdr:any.Msglen]}
    22  	case RTM_NEWMADDR2, RTM_DELMADDR:
    23  		p := (*InterfaceMulticastAddrMessage)(unsafe.Pointer(any))
    24  		return &InterfaceMulticastAddrMessage{Header: p.Header, Data: b[SizeofIfmaMsghdr2:any.Msglen]}
    25  	}
    26  	return nil
    27  }
    28  
    29  // InterfaceMulticastAddrMessage represents a routing message
    30  // containing network interface address entries.
    31  type InterfaceMulticastAddrMessage struct {
    32  	Header IfmaMsghdr2
    33  	Data   []byte
    34  }
    35  
    36  const rtaIfmaMask = RTA_GATEWAY | RTA_IFP | RTA_IFA
    37  
    38  func (m *InterfaceMulticastAddrMessage) sockaddr() (sas []Sockaddr) {
    39  	if m.Header.Addrs&rtaIfmaMask == 0 {
    40  		return nil
    41  	}
    42  	b := m.Data[:]
    43  	for i := uint(0); i < RTAX_MAX; i++ {
    44  		if m.Header.Addrs&rtaIfmaMask&(1<<i) == 0 {
    45  			continue
    46  		}
    47  		rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
    48  		switch i {
    49  		case RTAX_IFA:
    50  			sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa)))
    51  			if e != nil {
    52  				return nil
    53  			}
    54  			sas = append(sas, sa)
    55  		case RTAX_GATEWAY, RTAX_IFP:
    56  			// nothing to do
    57  		}
    58  		b = b[rsaAlignOf(int(rsa.Len)):]
    59  	}
    60  	return sas
    61  }