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 // Network interface identification for Darwin | 5 // Network interface identification for Darwin |
6 | 6 |
7 package net | 7 package net |
8 | 8 |
9 import ( | 9 import ( |
10 "os" | 10 "os" |
11 "syscall" | 11 "syscall" |
12 ) | 12 ) |
13 | 13 |
14 // If the ifindex is zero, interfaceMulticastAddrTable returns | 14 // If the ifindex is zero, interfaceMulticastAddrTable returns |
15 // addresses for all network interfaces. Otherwise it returns | 15 // addresses for all network interfaces. Otherwise it returns |
16 // addresses for a specific interface. | 16 // addresses for a specific interface. |
17 func interfaceMulticastAddrTable(ifindex int) ([]Addr, os.Error) { | 17 func interfaceMulticastAddrTable(ifindex int) ([]Addr, error) { |
18 var ( | 18 var ( |
19 tab []byte | 19 tab []byte |
20 e int | 20 e int |
21 msgs []syscall.RoutingMessage | 21 msgs []syscall.RoutingMessage |
22 ifmat []Addr | 22 ifmat []Addr |
23 ) | 23 ) |
24 | 24 |
25 tab, e = syscall.RouteRIB(syscall.NET_RT_IFLIST2, ifindex) | 25 tab, e = syscall.RouteRIB(syscall.NET_RT_IFLIST2, ifindex) |
26 if e != 0 { | 26 if e != 0 { |
27 return nil, os.NewSyscallError("route rib", e) | 27 return nil, os.NewSyscallError("route rib", e) |
(...skipping 13 matching lines...) Expand all Loading... |
41 return nil, err | 41 return nil, err |
42 } | 42 } |
43 ifmat = append(ifmat, ifma...) | 43 ifmat = append(ifmat, ifma...) |
44 } | 44 } |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 return ifmat, nil | 48 return ifmat, nil |
49 } | 49 } |
50 | 50 |
51 func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, os.Erro
r) { | 51 func newMulticastAddr(m *syscall.InterfaceMulticastAddrMessage) ([]Addr, error)
{ |
52 var ifmat []Addr | 52 var ifmat []Addr |
53 | 53 |
54 sas, e := syscall.ParseRoutingSockaddr(m) | 54 sas, e := syscall.ParseRoutingSockaddr(m) |
55 if e != 0 { | 55 if e != 0 { |
56 return nil, os.NewSyscallError("route sockaddr", e) | 56 return nil, os.NewSyscallError("route sockaddr", e) |
57 } | 57 } |
58 | 58 |
59 for _, s := range sas { | 59 for _, s := range sas { |
60 switch v := s.(type) { | 60 switch v := s.(type) { |
61 case *syscall.SockaddrInet4: | 61 case *syscall.SockaddrInet4: |
62 ifma := &IPAddr{IP: IPv4(v.Addr[0], v.Addr[1], v.Addr[2]
, v.Addr[3])} | 62 ifma := &IPAddr{IP: IPv4(v.Addr[0], v.Addr[1], v.Addr[2]
, v.Addr[3])} |
63 ifmat = append(ifmat, ifma.toAddr()) | 63 ifmat = append(ifmat, ifma.toAddr()) |
64 case *syscall.SockaddrInet6: | 64 case *syscall.SockaddrInet6: |
65 ifma := &IPAddr{IP: make(IP, IPv6len)} | 65 ifma := &IPAddr{IP: make(IP, IPv6len)} |
66 copy(ifma.IP, v.Addr[:]) | 66 copy(ifma.IP, v.Addr[:]) |
67 // NOTE: KAME based IPv6 protcol stack usually embeds | 67 // NOTE: KAME based IPv6 protcol stack usually embeds |
68 // the interface index in the interface-local or link- | 68 // the interface index in the interface-local or link- |
69 // local address as the kernel-internal form. | 69 // local address as the kernel-internal form. |
70 if ifma.IP.IsInterfaceLocalMulticast() || | 70 if ifma.IP.IsInterfaceLocalMulticast() || |
71 ifma.IP.IsLinkLocalMulticast() { | 71 ifma.IP.IsLinkLocalMulticast() { |
72 // remove embedded scope zone ID | 72 // remove embedded scope zone ID |
73 ifma.IP[2], ifma.IP[3] = 0, 0 | 73 ifma.IP[2], ifma.IP[3] = 0, 0 |
74 } | 74 } |
75 ifmat = append(ifmat, ifma.toAddr()) | 75 ifmat = append(ifmat, ifma.toAddr()) |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 return ifmat, nil | 79 return ifmat, nil |
80 } | 80 } |
OLD | NEW |