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) {
110 msgs []syscall.NetlinkMessage
113 tab, e = syscall.NetlinkRIB(syscall.RTM_GETADDR, syscall.AF_UNSPEC)
115 return nil, os.NewSyscallError("netlink rib", e)
118 msgs, e = syscall.ParseNetlinkMessage(tab)
120 return nil, os.NewSyscallError("netlink message", e)
123 ifat, err = addrTable(msgs, ifindex)
131 func addrTable(msgs []syscall.NetlinkMessage, ifindex int) ([]Addr, os.Error) {
134 for _, m := range msgs {
135 switch m.Header.Type {
136 case syscall.NLMSG_DONE:
138 case syscall.RTM_NEWADDR:
139 ifam := (*syscall.IfAddrmsg)(unsafe.Pointer(&m.Data[0]))
140 if ifindex == 0 || ifindex == int(ifam.Index) {
141 attrs, e := syscall.ParseNetlinkRouteAttr(&m)
143 return nil, os.NewSyscallError("netlink routeattr", e)
145 ifat = append(ifat, newAddr(attrs, int(ifam.Family))...)
154 func newAddr(attrs []syscall.NetlinkRouteAttr, family int) []Addr {
157 for _, a := range attrs {
159 case syscall.IFA_ADDRESS:
161 case syscall.AF_INET:
162 ifa := &IPAddr{IP: IPv4(a.Value[0], a.Value[1], a.Value[2], a.Value[3])}
163 ifat = append(ifat, ifa.toAddr())
164 case syscall.AF_INET6:
165 ifa := &IPAddr{IP: make(IP, IPv6len)}
166 copy(ifa.IP, a.Value[:])
167 ifat = append(ifat, ifa.toAddr())
175 // If the ifindex is zero, interfaceMulticastAddrTable returns
176 // addresses for all network interfaces. Otherwise it returns
177 // addresses for a specific interface.
178 func interfaceMulticastAddrTable(ifindex int) ([]Addr, os.Error) {
185 ifi, err = InterfaceByIndex(ifindex)
191 ifmat4 := parseProcNetIGMP(ifi)
192 ifmat6 := parseProcNetIGMP6(ifi)
194 return append(ifmat4, ifmat6...), nil
197 func parseProcNetIGMP(ifi *Interface) []Addr {
203 fd, err := open("/proc/net/igmp")
209 fd.readLine() // skip first line
210 b := make([]byte, IPv4len)
211 for l, ok := fd.readLine(); ok; l, ok = fd.readLine() {
215 if ifi == nil || name == ifi.Name {
216 fmt.Sscanf(f[0], "%08x", &b)
217 ifma := IPAddr{IP: IPv4(b[3], b[2], b[1], b[0])}
218 ifmat = append(ifmat, ifma.toAddr())
228 func parseProcNetIGMP6(ifi *Interface) []Addr {
231 fd, err := open("/proc/net/igmp6")
237 b := make([]byte, IPv6len)
238 for l, ok := fd.readLine(); ok; l, ok = fd.readLine() {
240 if ifi == nil || f[1] == ifi.Name {
241 fmt.Sscanf(f[2], "%32x", &b)
242 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]}}
243 ifmat = append(ifmat, ifma.toAddr())