| 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 init() { |
| 10 |
conf, _ := Sysctl("kern.conftxt") |
| 11 |
for i, j := 0, 0; j < len(conf); j++ { |
| 12 |
if conf[j] != '\n' { |
| 13 |
continue |
| 14 |
} |
| 15 |
s := conf[i:j] |
| 16 |
i = j + 1 |
| 17 |
if len(s) > len("machine") && s[:len("machine")] == "machine" { |
| 18 |
s = s[len("machine"):] |
| 19 |
for k := 0; k < len(s); k++ { |
| 20 |
if s[k] == ' ' || s[k] == '\t' { |
| 21 |
s = s[1:] |
| 22 |
} |
| 23 |
break |
| 24 |
} |
| 25 |
freebsdConfArch = s |
| 26 |
break |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func (any *anyMessage) toRoutingMessage(b []byte) RoutingMessage { |
| 32 |
switch any.Type { |
| 33 |
case RTM_ADD, RTM_DELETE, RTM_CHANGE, RTM_GET, RTM_LOSING, RTM_REDIRECT, RTM_MISS, RTM_LOCK, RTM_RESOLVE: |
| 34 |
return any.parseRouteMessage(b) |
| 35 |
case RTM_IFINFO: |
| 36 |
return any.parseInterfaceMessage(b) |
| 37 |
case RTM_IFANNOUNCE: |
| 38 |
p := (*InterfaceAnnounceMessage)(unsafe.Pointer(any)) |
| 39 |
return &InterfaceAnnounceMessage{Header: p.Header} |
| 40 |
case RTM_NEWADDR, RTM_DELADDR: |
| 41 |
p := (*InterfaceAddrMessage)(unsafe.Pointer(any)) |
| 42 |
return &InterfaceAddrMessage{Header: p.Header, Data: b[SizeofIfaMsghdr:any.Msglen]} |
| 43 |
case RTM_NEWMADDR, RTM_DELMADDR: |
| 44 |
p := (*InterfaceMulticastAddrMessage)(unsafe.Pointer(any)) |
| 45 |
return &InterfaceMulticastAddrMessage{Header: p.Header, Data: b[SizeofIfmaMsghdr:any.Msglen]} |
| 46 |
} |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
// InterfaceAnnounceMessage represents a routing message containing |
| 51 |
// network interface arrival and departure information. |
| 52 |
// |
| 53 |
// Deprecated: Use golang.org/x/net/route instead. |
| 54 |
type InterfaceAnnounceMessage struct { |
| 55 |
Header IfAnnounceMsghdr |
| 56 |
} |
| 57 |
|
| 58 |
func (m *InterfaceAnnounceMessage) sockaddr() ([]Sockaddr, error) { return nil, nil } |
| 59 |
|
| 60 |
// InterfaceMulticastAddrMessage represents a routing message |
| 61 |
// containing network interface address entries. |
| 62 |
// |
| 63 |
// Deprecated: Use golang.org/x/net/route instead. |
| 64 |
type InterfaceMulticastAddrMessage struct { |
| 65 |
Header IfmaMsghdr |
| 66 |
Data []byte |
| 67 |
} |
| 68 |
|
| 69 |
func (m *InterfaceMulticastAddrMessage) sockaddr() ([]Sockaddr, error) { |
| 70 |
var sas [RTAX_MAX]Sockaddr |
| 71 |
b := m.Data[:] |
| 72 |
for i := uint(0); i < RTAX_MAX && len(b) >= minRoutingSockaddrLen; i++ { |
| 73 |
if m.Header.Addrs&(1<<i) == 0 { |
| 74 |
continue |
| 75 |
} |
| 76 |
rsa := (*RawSockaddr)(unsafe.Pointer(&b[0])) |
| 77 |
switch rsa.Family { |
| 78 |
case AF_LINK: |
| 79 |
sa, err := parseSockaddrLink(b) |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
sas[i] = sa |
| 84 |
b = b[rsaAlignOf(int(rsa.Len)):] |
| 85 |
case AF_INET, AF_INET6: |
| 86 |
sa, err := parseSockaddrInet(b, rsa.Family) |
| 87 |
if err != nil { |
| 88 |
return nil, err |
| 89 |
} |
| 90 |
sas[i] = sa |
| 91 |
b = b[rsaAlignOf(int(rsa.Len)):] |
| 92 |
default: |
| 93 |
sa, l, err := parseLinkLayerAddr(b) |
| 94 |
if err != nil { |
| 95 |
return nil, err |
| 96 |
} |
| 97 |
sas[i] = sa |
| 98 |
b = b[l:] |
| 99 |
} |
| 100 |
} |
| 101 |
return sas[:], nil |
| 102 |
} |