interface_solaris.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2016 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. package net
  5. import (
  6. "syscall"
  7. "golang.org/x/net/lif"
  8. )
  9. // If the ifindex is zero, interfaceTable returns mappings of all
  10. // network interfaces. Otherwise it returns a mapping of a specific
  11. // interface.
  12. func interfaceTable(ifindex int) ([]Interface, error) {
  13. lls, err := lif.Links(syscall.AF_UNSPEC, "")
  14. if err != nil {
  15. return nil, err
  16. }
  17. var ift []Interface
  18. for _, ll := range lls {
  19. if ifindex != 0 && ifindex != ll.Index {
  20. continue
  21. }
  22. ifi := Interface{Index: ll.Index, MTU: ll.MTU, Name: ll.Name, Flags: linkFlags(ll.Flags)}
  23. if len(ll.Addr) > 0 {
  24. ifi.HardwareAddr = HardwareAddr(ll.Addr)
  25. }
  26. ift = append(ift, ifi)
  27. }
  28. return ift, nil
  29. }
  30. func linkFlags(rawFlags int) Flags {
  31. var f Flags
  32. if rawFlags&syscall.IFF_UP != 0 {
  33. f |= FlagUp
  34. }
  35. if rawFlags&syscall.IFF_BROADCAST != 0 {
  36. f |= FlagBroadcast
  37. }
  38. if rawFlags&syscall.IFF_LOOPBACK != 0 {
  39. f |= FlagLoopback
  40. }
  41. if rawFlags&syscall.IFF_POINTOPOINT != 0 {
  42. f |= FlagPointToPoint
  43. }
  44. if rawFlags&syscall.IFF_MULTICAST != 0 {
  45. f |= FlagMulticast
  46. }
  47. return f
  48. }
  49. // If the ifi is nil, interfaceAddrTable returns addresses for all
  50. // network interfaces. Otherwise it returns addresses for a specific
  51. // interface.
  52. func interfaceAddrTable(ifi *Interface) ([]Addr, error) {
  53. var name string
  54. if ifi != nil {
  55. name = ifi.Name
  56. }
  57. as, err := lif.Addrs(syscall.AF_UNSPEC, name)
  58. if err != nil {
  59. return nil, err
  60. }
  61. var ifat []Addr
  62. for _, a := range as {
  63. var ip IP
  64. var mask IPMask
  65. switch a := a.(type) {
  66. case *lif.Inet4Addr:
  67. ip = IPv4(a.IP[0], a.IP[1], a.IP[2], a.IP[3])
  68. mask = CIDRMask(a.PrefixLen, 8*IPv4len)
  69. case *lif.Inet6Addr:
  70. ip = make(IP, IPv6len)
  71. copy(ip, a.IP[:])
  72. mask = CIDRMask(a.PrefixLen, 8*IPv6len)
  73. }
  74. ifat = append(ifat, &IPNet{IP: ip, Mask: mask})
  75. }
  76. return ifat, nil
  77. }
  78. // interfaceMulticastAddrTable returns addresses for a specific
  79. // interface.
  80. func interfaceMulticastAddrTable(ifi *Interface) ([]Addr, error) {
  81. return nil, nil
  82. }