Source file src/syscall/route_darwin.go

     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  package syscall
     6  
     7  import "unsafe"
     8  
     9  func (any *anyMessage) toRoutingMessage(b []byte) RoutingMessage {
    10  	switch any.Type {
    11  	case RTM_ADD, RTM_DELETE, RTM_CHANGE, RTM_GET, RTM_LOSING, RTM_REDIRECT, RTM_MISS, RTM_LOCK, RTM_RESOLVE:
    12  		p := (*RouteMessage)(unsafe.Pointer(any))
    13  		return &RouteMessage{Header: p.Header, Data: b[SizeofRtMsghdr:any.Msglen]}
    14  	case RTM_IFINFO:
    15  		p := (*InterfaceMessage)(unsafe.Pointer(any))
    16  		return &InterfaceMessage{Header: p.Header, Data: b[SizeofIfMsghdr:any.Msglen]}
    17  	case RTM_NEWADDR, RTM_DELADDR:
    18  		p := (*InterfaceAddrMessage)(unsafe.Pointer(any))
    19  		return &InterfaceAddrMessage{Header: p.Header, Data: b[SizeofIfaMsghdr:any.Msglen]}
    20  	case RTM_NEWMADDR2, RTM_DELMADDR:
    21  		p := (*InterfaceMulticastAddrMessage)(unsafe.Pointer(any))
    22  		return &InterfaceMulticastAddrMessage{Header: p.Header, Data: b[SizeofIfmaMsghdr2:any.Msglen]}
    23  	}
    24  	return nil
    25  }
    26  
    27  // InterfaceMulticastAddrMessage represents a routing message
    28  // containing network interface address entries.
    29  //
    30  // Deprecated: Use golang.org/x/net/route instead.
    31  type InterfaceMulticastAddrMessage struct {
    32  	Header IfmaMsghdr2
    33  	Data   []byte
    34  }
    35  
    36  func (m *InterfaceMulticastAddrMessage) sockaddr() ([]Sockaddr, error) {
    37  	var sas [RTAX_MAX]Sockaddr
    38  	b := m.Data[:]
    39  	for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ {
    40  		if m.Header.Addrs&(1<<i) == 0 {
    41  			continue
    42  		}
    43  		rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
    44  		switch rsa.Family {
    45  		case AF_LINK:
    46  			sa, err := parseSockaddrLink(b)
    47  			if err != nil {
    48  				return nil, err
    49  			}
    50  			sas[i] = sa
    51  			b = b[rsaAlignOf(int(rsa.Len)):]
    52  		case AF_INET, AF_INET6:
    53  			sa, err := parseSockaddrInet(b, rsa.Family)
    54  			if err != nil {
    55  				return nil, err
    56  			}
    57  			sas[i] = sa
    58  			b = b[rsaAlignOf(int(rsa.Len)):]
    59  		default:
    60  			sa, l, err := parseLinkLayerAddr(b)
    61  			if err != nil {
    62  				return nil, err
    63  			}
    64  			sas[i] = sa
    65  			b = b[l:]
    66  		}
    67  	}
    68  	return sas[:], nil
    69  }
    70  

View as plain text