OLD | NEW |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Routing sockets and messages | 5 // Routing sockets and messages |
6 | 6 |
7 package syscall | 7 package syscall |
8 | 8 |
9 import ( | 9 import ( |
10 "unsafe" | 10 "unsafe" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 const anyMessageLen = int(unsafe.Sizeof(anyMessage{})) | 60 const anyMessageLen = int(unsafe.Sizeof(anyMessage{})) |
61 | 61 |
62 type anyMessage struct { | 62 type anyMessage struct { |
63 Msglen uint16 | 63 Msglen uint16 |
64 Version uint8 | 64 Version uint8 |
65 Type uint8 | 65 Type uint8 |
66 } | 66 } |
67 | 67 |
68 func (any *anyMessage) toRoutingMessage(buf []byte) RoutingMessage { | |
69 switch any.Type { | |
70 case RTM_ADD, RTM_DELETE, RTM_CHANGE, RTM_GET, RTM_LOSING, RTM_REDIRECT,
RTM_MISS, RTM_LOCK, RTM_RESOLVE: | |
71 p := (*RouteMessage)(unsafe.Pointer(any)) | |
72 rtm := &RouteMessage{} | |
73 rtm.Header = p.Header | |
74 rtm.Data = buf[SizeofRtMsghdr:any.Msglen] | |
75 return rtm | |
76 case RTM_IFINFO: | |
77 p := (*InterfaceMessage)(unsafe.Pointer(any)) | |
78 ifm := &InterfaceMessage{} | |
79 ifm.Header = p.Header | |
80 ifm.Data = buf[SizeofIfMsghdr:any.Msglen] | |
81 return ifm | |
82 case RTM_NEWADDR, RTM_DELADDR: | |
83 p := (*InterfaceAddrMessage)(unsafe.Pointer(any)) | |
84 ifam := &InterfaceAddrMessage{} | |
85 ifam.Header = p.Header | |
86 ifam.Data = buf[SizeofIfaMsghdr:any.Msglen] | |
87 return ifam | |
88 case RTM_NEWMADDR, RTM_DELMADDR: | |
89 // TODO: implement this in the near future | |
90 } | |
91 return nil | |
92 } | |
93 | |
94 // RouteMessage represents a routing message containing routing | 68 // RouteMessage represents a routing message containing routing |
95 // entries. | 69 // entries. |
96 type RouteMessage struct { | 70 type RouteMessage struct { |
97 Header RtMsghdr | 71 Header RtMsghdr |
98 Data []byte | 72 Data []byte |
99 } | 73 } |
100 | 74 |
101 func (m *RouteMessage) sockaddr() (sas []Sockaddr) { | 75 func (m *RouteMessage) sockaddr() (sas []Sockaddr) { |
102 // TODO: implement this in the near future | 76 // TODO: implement this in the near future |
103 return nil | 77 return nil |
(...skipping 17 matching lines...) Expand all Loading... |
121 return append(sas, sa) | 95 return append(sas, sa) |
122 } | 96 } |
123 | 97 |
124 // InterfaceAddrMessage represents a routing message containing | 98 // InterfaceAddrMessage represents a routing message containing |
125 // network interface address entries. | 99 // network interface address entries. |
126 type InterfaceAddrMessage struct { | 100 type InterfaceAddrMessage struct { |
127 Header IfaMsghdr | 101 Header IfaMsghdr |
128 Data []byte | 102 Data []byte |
129 } | 103 } |
130 | 104 |
131 const rtaMask = RTA_IFA | RTA_NETMASK | RTA_BRD | 105 const rtaIfaMask = RTA_IFA | RTA_NETMASK | RTA_BRD |
132 | 106 |
133 func (m *InterfaceAddrMessage) sockaddr() (sas []Sockaddr) { | 107 func (m *InterfaceAddrMessage) sockaddr() (sas []Sockaddr) { |
134 » if m.Header.Addrs&rtaMask == 0 { | 108 » if m.Header.Addrs&rtaIfaMask == 0 { |
135 return nil | 109 return nil |
136 } | 110 } |
137 | 111 |
138 buf := m.Data[:] | 112 buf := m.Data[:] |
139 for i := uint(0); i < RTAX_MAX; i++ { | 113 for i := uint(0); i < RTAX_MAX; i++ { |
140 » » if m.Header.Addrs&rtaMask&(1<<i) == 0 { | 114 » » if m.Header.Addrs&rtaIfaMask&(1<<i) == 0 { |
141 continue | 115 continue |
142 } | 116 } |
143 rsa := (*RawSockaddr)(unsafe.Pointer(&buf[0])) | 117 rsa := (*RawSockaddr)(unsafe.Pointer(&buf[0])) |
144 switch i { | 118 switch i { |
145 case RTAX_IFA: | 119 case RTAX_IFA: |
146 sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(
rsa))) | 120 sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(
rsa))) |
147 if e != 0 { | 121 if e != 0 { |
148 return nil | 122 return nil |
149 } | 123 } |
150 sas = append(sas, sa) | 124 sas = append(sas, sa) |
151 case RTAX_NETMASK, RTAX_BRD: | 125 case RTAX_NETMASK, RTAX_BRD: |
152 // nothing to do | 126 // nothing to do |
153 } | 127 } |
154 buf = buf[rsaAlignOf(int(rsa.Len)):] | 128 buf = buf[rsaAlignOf(int(rsa.Len)):] |
155 } | 129 } |
156 | 130 |
157 return sas | 131 return sas |
158 } | 132 } |
159 | 133 |
| 134 const rtaIfmaMask = RTA_GATEWAY | RTA_IFP | RTA_IFA |
| 135 |
| 136 func (m *InterfaceMulticastAddrMessage) sockaddr() (sas []Sockaddr) { |
| 137 if m.Header.Addrs&rtaIfmaMask == 0 { |
| 138 return nil |
| 139 } |
| 140 |
| 141 buf := m.Data[:] |
| 142 for i := uint(0); i < RTAX_MAX; i++ { |
| 143 if m.Header.Addrs&rtaIfmaMask&(1<<i) == 0 { |
| 144 continue |
| 145 } |
| 146 rsa := (*RawSockaddr)(unsafe.Pointer(&buf[0])) |
| 147 switch i { |
| 148 case RTAX_IFA: |
| 149 sa, e := anyToSockaddr((*RawSockaddrAny)(unsafe.Pointer(
rsa))) |
| 150 if e != 0 { |
| 151 return nil |
| 152 } |
| 153 sas = append(sas, sa) |
| 154 case RTAX_GATEWAY, RTAX_IFP: |
| 155 // nothing to do |
| 156 } |
| 157 buf = buf[rsaAlignOf(int(rsa.Len)):] |
| 158 } |
| 159 |
| 160 return sas |
| 161 } |
| 162 |
160 // ParseRoutingMessage parses buf as routing messages and returns | 163 // ParseRoutingMessage parses buf as routing messages and returns |
161 // the slice containing the RoutingMessage interfaces. | 164 // the slice containing the RoutingMessage interfaces. |
162 func ParseRoutingMessage(buf []byte) (msgs []RoutingMessage, errno int) { | 165 func ParseRoutingMessage(buf []byte) (msgs []RoutingMessage, errno int) { |
163 for len(buf) >= anyMessageLen { | 166 for len(buf) >= anyMessageLen { |
164 any := (*anyMessage)(unsafe.Pointer(&buf[0])) | 167 any := (*anyMessage)(unsafe.Pointer(&buf[0])) |
165 if any.Version != RTM_VERSION { | 168 if any.Version != RTM_VERSION { |
166 return nil, EINVAL | 169 return nil, EINVAL |
167 } | 170 } |
168 msgs = append(msgs, any.toRoutingMessage(buf)) | 171 msgs = append(msgs, any.toRoutingMessage(buf)) |
169 buf = buf[any.Msglen:] | 172 buf = buf[any.Msglen:] |
170 } | 173 } |
171 return msgs, 0 | 174 return msgs, 0 |
172 } | 175 } |
173 | 176 |
174 // ParseRoutingMessage parses msg's payload as raw sockaddrs and | 177 // ParseRoutingMessage parses msg's payload as raw sockaddrs and |
175 // returns the slice containing the Sockaddr interfaces. | 178 // returns the slice containing the Sockaddr interfaces. |
176 func ParseRoutingSockaddr(msg RoutingMessage) (sas []Sockaddr, errno int) { | 179 func ParseRoutingSockaddr(msg RoutingMessage) (sas []Sockaddr, errno int) { |
177 return append(sas, msg.sockaddr()...), 0 | 180 return append(sas, msg.sockaddr()...), 0 |
178 } | 181 } |
OLD | NEW |