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 		// We don't support sockaddr_mpls for now.
     14 		p.Header.Addrs &= RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_GENMASK | RTA_IFA | RTA_IFP | RTA_BRD | RTA_AUTHOR
     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_IFANNOUNCE:
     20 		p := (*InterfaceAnnounceMessage)(unsafe.Pointer(any))
     21 		return &InterfaceAnnounceMessage{Header: p.Header}
     22 	case RTM_NEWADDR, RTM_DELADDR:
     23 		p := (*InterfaceAddrMessage)(unsafe.Pointer(any))
     24 		return &InterfaceAddrMessage{Header: p.Header, Data: b[SizeofIfaMsghdr:any.Msglen]}
     25 	case RTM_NEWMADDR, RTM_DELMADDR:
     26 		p := (*InterfaceMulticastAddrMessage)(unsafe.Pointer(any))
     27 		return &InterfaceMulticastAddrMessage{Header: p.Header, Data: b[SizeofIfmaMsghdr:any.Msglen]}
     28 	}
     29 	return nil
     30 }
     31 
     32 // InterfaceAnnounceMessage represents a routing message containing
     33 // network interface arrival and departure information.
     34 type InterfaceAnnounceMessage struct {
     35 	Header IfAnnounceMsghdr
     36 }
     37 
     38 func (m *InterfaceAnnounceMessage) sockaddr() ([]Sockaddr, error) { return nil, nil }
     39 
     40 // InterfaceMulticastAddrMessage represents a routing message
     41 // containing network interface address entries.
     42 type InterfaceMulticastAddrMessage struct {
     43 	Header IfmaMsghdr
     44 	Data   []byte
     45 }
     46 
     47 func (m *InterfaceMulticastAddrMessage) sockaddr() ([]Sockaddr, error) {
     48 	var sas [RTAX_MAX]Sockaddr
     49 	b := m.Data[:]
     50 	for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ {
     51 		if m.Header.Addrs&(1<<i) == 0 {
     52 			continue
     53 		}
     54 		rsa := (*RawSockaddr)(unsafe.Pointer(&b[0]))
     55 		switch rsa.Family {
     56 		case AF_LINK:
     57 			sa, err := parseSockaddrLink(b)
     58 			if err != nil {
     59 				return nil, err
     60 			}
     61 			sas[i] = sa
     62 			b = b[rsaAlignOf(int(rsa.Len)):]
     63 		case AF_INET, AF_INET6:
     64 			sa, err := parseSockaddrInet(b, rsa.Family)
     65 			if err != nil {
     66 				return nil, err
     67 			}
     68 			sas[i] = sa
     69 			b = b[rsaAlignOf(int(rsa.Len)):]
     70 		default:
     71 			sa, l, err := parseLinkLayerAddr(b)
     72 			if err != nil {
     73 				return nil, err
     74 			}
     75 			sas[i] = sa
     76 			b = b[l:]
     77 		}
     78 	}
     79 	return sas[:], nil
     80 }
     81