summaryrefslogtreecommitdiff
path: root/libgo/go/syscall/route_ifma_test.go
blob: af2b67dc24492e2f68814c0e55a19112a49195d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build darwin dragonfly freebsd

package syscall_test

import (
	"fmt"
	"syscall"
)

func parseRoutingMessageHeader(m syscall.RoutingMessage) (addrFlags, error) {
	switch m := m.(type) {
	case *syscall.RouteMessage:
		errno := syscall.Errno(uintptr(m.Header.Errno))
		if errno != 0 {
			return 0, fmt.Errorf("%T: %v, %#v", m, errno, m.Header)
		}
		return addrFlags(m.Header.Addrs), nil
	case *syscall.InterfaceMessage:
		return addrFlags(m.Header.Addrs), nil
	case *syscall.InterfaceAddrMessage:
		return addrFlags(m.Header.Addrs), nil
	case *syscall.InterfaceMulticastAddrMessage:
		return addrFlags(m.Header.Addrs), nil
	default:
		panic(fmt.Sprintf("unknown routing message type: %T", m))
	}
}

func parseRoutingSockaddrs(m syscall.RoutingMessage) ([]syscall.Sockaddr, error) {
	switch m := m.(type) {
	case *syscall.RouteMessage:
		sas, err := syscall.ParseRoutingSockaddr(m)
		if err != nil {
			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
		}
		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
			return nil, err
		}
		return sas, nil
	case *syscall.InterfaceMessage:
		sas, err := syscall.ParseRoutingSockaddr(m)
		if err != nil {
			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
		}
		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
			return nil, err
		}
		return sas, nil
	case *syscall.InterfaceAddrMessage:
		sas, err := syscall.ParseRoutingSockaddr(m)
		if err != nil {
			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
		}
		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
			return nil, err
		}
		return sas, nil
	case *syscall.InterfaceMulticastAddrMessage:
		sas, err := syscall.ParseRoutingSockaddr(m)
		if err != nil {
			return nil, fmt.Errorf("%T: %v, %#v", m, err, m.Data)
		}
		if err = sockaddrs(sas).match(addrFlags(m.Header.Addrs)); err != nil {
			return nil, err
		}
		return sas, nil
	default:
		panic(fmt.Sprintf("unknown routing message type: %T", m))
	}
}