Home | History | Annotate | Download | only in syscall
      1 // Copyright 2015 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 // +build darwin dragonfly freebsd
      6 
      7 package syscall_test
      8 
      9 import (
     10 	"fmt"
     11 	"syscall"
     12 )
     13 
     14 func parseRoutingMessageHeader(m syscall.RoutingMessage) (addrFlags, error) {
     15 	switch m := m.(type) {
     16 	case *syscall.RouteMessage:
     17 		errno := syscall.Errno(uintptr(m.Header.Errno))
     18 		if errno != 0 {
     19 			return 0, fmt.Errorf("%T: %v, %#v", m, errno, m.Header)
     20 		}
     21 		return addrFlags(m.Header.Addrs), nil
     22 	case *syscall.InterfaceMessage:
     23 		return addrFlags(m.Header.Addrs), nil
     24 	case *syscall.InterfaceAddrMessage:
     25 		return addrFlags(m.Header.Addrs), nil
     26 	case *syscall.InterfaceMulticastAddrMessage:
     27 		return addrFlags(m.Header.Addrs), nil
     28 	default:
     29 		panic(fmt.Sprintf("unknown routing message type: %T", m))
     30 	}
     31 }
     32 
     33 func parseRoutingSockaddrs(m syscall.RoutingMessage) ([]syscall.Sockaddr, error) {
     34 	switch m := m.(type) {
     35 	case *syscall.RouteMessage:
     36 		sas, err := syscall.ParseRoutingSockaddr(m)
     37 		if err != nil {
     38 			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
     39 		}
     40 		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
     41 			return nil, err
     42 		}
     43 		return sas, nil
     44 	case *syscall.InterfaceMessage:
     45 		sas, err := syscall.ParseRoutingSockaddr(m)
     46 		if err != nil {
     47 			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
     48 		}
     49 		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
     50 			return nil, err
     51 		}
     52 		return sas, nil
     53 	case *syscall.InterfaceAddrMessage:
     54 		sas, err := syscall.ParseRoutingSockaddr(m)
     55 		if err != nil {
     56 			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
     57 		}
     58 		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
     59 			return nil, err
     60 		}
     61 		return sas, nil
     62 	case *syscall.InterfaceMulticastAddrMessage:
     63 		sas, err := syscall.ParseRoutingSockaddr(m)
     64 		if err != nil {
     65 			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
     66 		}
     67 		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
     68 			return nil, err
     69 		}
     70 		return sas, nil
     71 	default:
     72 		panic(fmt.Sprintf("unknown routing message type: %T", m))
     73 	}
     74 }
     75