OSDN Git Service

syscall: Additional constants, some type corrections.
[pf3gnuchains/gcc-fork.git] / libgo / go / syscall / route_freebsd.go
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 // Routing sockets and messages for FreeBSD
6
7 package syscall
8
9 import (
10         "unsafe"
11 )
12
13 func (any *anyMessage) toRoutingMessage(buf []byte) RoutingMessage {
14         switch any.Type {
15         case RTM_ADD, RTM_DELETE, RTM_CHANGE, RTM_GET, RTM_LOSING, RTM_REDIRECT, RTM_MISS, RTM_LOCK, RTM_RESOLVE:
16                 p := (*RouteMessage)(unsafe.Pointer(any))
17                 rtm := &RouteMessage{}
18                 rtm.Header = p.Header
19                 rtm.Data = buf[SizeofRtMsghdr:any.Msglen]
20                 return rtm
21         case RTM_IFINFO:
22                 p := (*InterfaceMessage)(unsafe.Pointer(any))
23                 ifm := &InterfaceMessage{}
24                 ifm.Header = p.Header
25                 ifm.Data = buf[SizeofIfMsghdr:any.Msglen]
26                 return ifm
27         case RTM_NEWADDR, RTM_DELADDR:
28                 p := (*InterfaceAddrMessage)(unsafe.Pointer(any))
29                 ifam := &InterfaceAddrMessage{}
30                 ifam.Header = p.Header
31                 ifam.Data = buf[SizeofIfaMsghdr:any.Msglen]
32                 return ifam
33         case RTM_NEWMADDR, RTM_DELMADDR:
34                 p := (*InterfaceMulticastAddrMessage)(unsafe.Pointer(any))
35                 ifmam := &InterfaceMulticastAddrMessage{}
36                 ifmam.Header = p.Header
37                 ifmam.Data = buf[SizeofIfmaMsghdr:any.Msglen]
38                 return ifmam
39         }
40         return nil
41 }
42
43 // InterfaceMulticastAddrMessage represents a routing message
44 // containing network interface address entries.
45 type InterfaceMulticastAddrMessage struct {
46         Header IfmaMsghdr
47         Data   []byte
48 }
49
50 const rtaIfmaMask = RTA_GATEWAY | RTA_IFP | RTA_IFA
51
52 func (m *InterfaceMulticastAddrMessage) sockaddr() (sas []Sockaddr) {
53         if m.Header.Addrs&rtaIfmaMask == 0 {
54                 return nil
55         }
56
57         buf := m.Data[:]
58         for i := uint(0); i < RTAX_MAX; i++ {
59                 if m.Header.Addrs&rtaIfmaMask&(1<<i) == 0 {
60                         continue
61                 }
62                 rsa := (*RawSockaddr)(unsafe.Pointer(&buf[0]))
63                 switch i {
64                 case RTAX_IFA:
65                         sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(rsa)))
66                         if e != nil {
67                                 return nil
68                         }
69                         sas = append(sas, sa)
70                 case RTAX_GATEWAY, RTAX_IFP:
71                         // nothing to do
72                 }
73                 buf = buf[rsaAlignOf(int(rsa.Len)):]
74         }
75
76         return sas
77 }