Home | History | Annotate | Download | only in syscall
      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 type InterfaceMulticastAddrMessage struct {
     30 	Header IfmaMsghdr2
     31 	Data   []byte
     32 }
     33 
     34 func (m *InterfaceMulticastAddrMessage) sockaddr() ([]Sockaddr, error) {
     35 	var sas [RTAX_MAX]Sockaddr
     36 	b := m.Data[:]
     37 	for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ {
     38 		if m.Header.Addrs&(1<<i) == 0 {
     39 			continue
     40 		}
     41 		rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
     42 		switch rsa.Family {
     43 		case AF_LINK:
     44 			sa, err := parseSockaddrLink(b)
     45 			if err != nil {
     46 				return nil, err
     47 			}
     48 			sas[i] = sa
     49 			b = b[rsaAlignOf(int(rsa.Len)):]
     50 		case AF_INET, AF_INET6:
     51 			sa, err := parseSockaddrInet(b, rsa.Family)
     52 			if err != nil {
     53 				return nil, err
     54 			}
     55 			sas[i] = sa
     56 			b = b[rsaAlignOf(int(rsa.Len)):]
     57 		default:
     58 			sa, l, err := parseLinkLayerAddr(b)
     59 			if err != nil {
     60 				return nil, err
     61 			}
     62 			sas[i] = sa
     63 			b = b[l:]
     64 		}
     65 	}
     66 	return sas[:], nil
     67 }
     68