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.
5 // Network interface identification for Linux
16 // If the ifindex is zero, interfaceTable returns mappings of all
17 // network interfaces. Otheriwse it returns a mapping of a specific
19 func interfaceTable(ifindex int) ([]Interface, os.Error) {
23 msgs []syscall.NetlinkMessage
27 tab, e = syscall.NetlinkRIB(syscall.RTM_GETLINK, syscall.AF_UNSPEC)
29 return nil, os.NewSyscallError("netlink rib", e)
32 msgs, e = syscall.ParseNetlinkMessage(tab)
34 return nil, os.NewSyscallError("netlink message", e)
37 for _, m := range msgs {
38 switch m.Header.Type {
39 case syscall.NLMSG_DONE:
41 case syscall.RTM_NEWLINK:
42 ifim := (*syscall.IfInfomsg)(unsafe.Pointer(&m.Data[0]))
43 if ifindex == 0 || ifindex == int(ifim.Index) {
44 attrs, e := syscall.ParseNetlinkRouteAttr(&m)
46 return nil, os.NewSyscallError("netlink routeattr", e)
48 ifi := newLink(attrs, ifim)
49 ift = append(ift, ifi)
58 func newLink(attrs []syscall.NetlinkRouteAttr, ifim *syscall.IfInfomsg) Interface {
59 ifi := Interface{Index: int(ifim.Index), Flags: linkFlags(ifim.Flags)}
60 for _, a := range attrs {
62 case syscall.IFLA_ADDRESS:
64 for _, b := range a.Value {
70 ifi.HardwareAddr = a.Value[:]
72 case syscall.IFLA_IFNAME:
73 ifi.Name = string(a.Value[:len(a.Value)-1])
74 case syscall.IFLA_MTU:
75 ifi.MTU = int(uint32(a.Value[3])<<24 | uint32(a.Value[2])<<16 | uint32(a.Value[1])<<8 | uint32(a.Value[0]))
81 func linkFlags(rawFlags uint32) Flags {
83 if rawFlags&syscall.IFF_UP != 0 {
86 if rawFlags&syscall.IFF_BROADCAST != 0 {
89 if rawFlags&syscall.IFF_LOOPBACK != 0 {
92 if rawFlags&syscall.IFF_POINTOPOINT != 0 {
95 if rawFlags&syscall.IFF_MULTICAST != 0 {
101 // If the ifindex is zero, interfaceAddrTable returns addresses
102 // for all network interfaces. Otherwise it returns addresses
103 // for a specific interface.
104 func interfaceAddrTable(ifindex int) ([]Addr, os.Error) {
111 msgs4 []syscall.NetlinkMessage
112 msgs6 []syscall.NetlinkMessage
115 tab, e = syscall.NetlinkRIB(syscall.RTM_GETADDR, syscall.AF_INET)
117 return nil, os.NewSyscallError("netlink rib", e)
119 msgs4, e = syscall.ParseNetlinkMessage(tab)
121 return nil, os.NewSyscallError("netlink message", e)
123 ifat4, err = addrTable(msgs4, ifindex)
128 tab, e = syscall.NetlinkRIB(syscall.RTM_GETADDR, syscall.AF_INET6)
130 return nil, os.NewSyscallError("netlink rib", e)
132 msgs6, e = syscall.ParseNetlinkMessage(tab)
134 return nil, os.NewSyscallError("netlink message", e)
136 ifat6, err = addrTable(msgs6, ifindex)
141 return append(ifat4, ifat6...), nil
144 func addrTable(msgs []syscall.NetlinkMessage, ifindex int) ([]Addr, os.Error) {
147 for _, m := range msgs {
148 switch m.Header.Type {
149 case syscall.NLMSG_DONE:
151 case syscall.RTM_NEWADDR:
152 ifam := (*syscall.IfAddrmsg)(unsafe.Pointer(&m.Data[0]))
153 if ifindex == 0 || ifindex == int(ifam.Index) {
154 attrs, e := syscall.ParseNetlinkRouteAttr(&m)
156 return nil, os.NewSyscallError("netlink routeattr", e)
158 ifat = append(ifat, newAddr(attrs, int(ifam.Family))...)
167 func newAddr(attrs []syscall.NetlinkRouteAttr, family int) []Addr {
170 for _, a := range attrs {
172 case syscall.IFA_ADDRESS:
174 case syscall.AF_INET:
175 ifa := &IPAddr{IP: IPv4(a.Value[0], a.Value[1], a.Value[2], a.Value[3])}
176 ifat = append(ifat, ifa.toAddr())
177 case syscall.AF_INET6:
178 ifa := &IPAddr{IP: make(IP, IPv6len)}
179 copy(ifa.IP, a.Value[:])
180 ifat = append(ifat, ifa.toAddr())
188 // If the ifindex is zero, interfaceMulticastAddrTable returns
189 // addresses for all network interfaces. Otherwise it returns
190 // addresses for a specific interface.
191 func interfaceMulticastAddrTable(ifindex int) ([]Addr, os.Error) {
198 ifi, err = InterfaceByIndex(ifindex)
204 ifmat4 := parseProcNetIGMP(ifi)
205 ifmat6 := parseProcNetIGMP6(ifi)
207 return append(ifmat4, ifmat6...), nil
210 func parseProcNetIGMP(ifi *Interface) []Addr {
216 fd, err := open("/proc/net/igmp")
222 fd.readLine() // skip first line
223 b := make([]byte, IPv4len)
224 for l, ok := fd.readLine(); ok; l, ok = fd.readLine() {
228 if ifi == nil || name == ifi.Name {
229 fmt.Sscanf(f[0], "%08x", &b)
230 ifma := IPAddr{IP: IPv4(b[3], b[2], b[1], b[0])}
231 ifmat = append(ifmat, ifma.toAddr())
241 func parseProcNetIGMP6(ifi *Interface) []Addr {
244 fd, err := open("/proc/net/igmp6")
250 b := make([]byte, IPv6len)
251 for l, ok := fd.readLine(); ok; l, ok = fd.readLine() {
253 if ifi == nil || f[1] == ifi.Name {
254 fmt.Sscanf(f[2], "%32x", &b)
255 ifma := IPAddr{IP: IP{b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]}}
256 ifmat = append(ifmat, ifma.toAddr())