sys_asmreq.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2012 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. // +build aix darwin dragonfly freebsd netbsd openbsd solaris windows
  5. package ipv4
  6. import (
  7. "errors"
  8. "net"
  9. "unsafe"
  10. "golang.org/x/net/internal/socket"
  11. )
  12. var errNoSuchInterface = errors.New("no such interface")
  13. func (so *sockOpt) setIPMreq(c *socket.Conn, ifi *net.Interface, grp net.IP) error {
  14. mreq := ipMreq{Multiaddr: [4]byte{grp[0], grp[1], grp[2], grp[3]}}
  15. if err := setIPMreqInterface(&mreq, ifi); err != nil {
  16. return err
  17. }
  18. b := (*[sizeofIPMreq]byte)(unsafe.Pointer(&mreq))[:sizeofIPMreq]
  19. return so.Set(c, b)
  20. }
  21. func (so *sockOpt) getMulticastIf(c *socket.Conn) (*net.Interface, error) {
  22. var b [4]byte
  23. if _, err := so.Get(c, b[:]); err != nil {
  24. return nil, err
  25. }
  26. ifi, err := netIP4ToInterface(net.IPv4(b[0], b[1], b[2], b[3]))
  27. if err != nil {
  28. return nil, err
  29. }
  30. return ifi, nil
  31. }
  32. func (so *sockOpt) setMulticastIf(c *socket.Conn, ifi *net.Interface) error {
  33. ip, err := netInterfaceToIP4(ifi)
  34. if err != nil {
  35. return err
  36. }
  37. var b [4]byte
  38. copy(b[:], ip)
  39. return so.Set(c, b[:])
  40. }
  41. func setIPMreqInterface(mreq *ipMreq, ifi *net.Interface) error {
  42. if ifi == nil {
  43. return nil
  44. }
  45. ifat, err := ifi.Addrs()
  46. if err != nil {
  47. return err
  48. }
  49. for _, ifa := range ifat {
  50. switch ifa := ifa.(type) {
  51. case *net.IPAddr:
  52. if ip := ifa.IP.To4(); ip != nil {
  53. copy(mreq.Interface[:], ip)
  54. return nil
  55. }
  56. case *net.IPNet:
  57. if ip := ifa.IP.To4(); ip != nil {
  58. copy(mreq.Interface[:], ip)
  59. return nil
  60. }
  61. }
  62. }
  63. return errNoSuchInterface
  64. }
  65. func netIP4ToInterface(ip net.IP) (*net.Interface, error) {
  66. ift, err := net.Interfaces()
  67. if err != nil {
  68. return nil, err
  69. }
  70. for _, ifi := range ift {
  71. ifat, err := ifi.Addrs()
  72. if err != nil {
  73. return nil, err
  74. }
  75. for _, ifa := range ifat {
  76. switch ifa := ifa.(type) {
  77. case *net.IPAddr:
  78. if ip.Equal(ifa.IP) {
  79. return &ifi, nil
  80. }
  81. case *net.IPNet:
  82. if ip.Equal(ifa.IP) {
  83. return &ifi, nil
  84. }
  85. }
  86. }
  87. }
  88. return nil, errNoSuchInterface
  89. }
  90. func netInterfaceToIP4(ifi *net.Interface) (net.IP, error) {
  91. if ifi == nil {
  92. return net.IPv4zero.To4(), nil
  93. }
  94. ifat, err := ifi.Addrs()
  95. if err != nil {
  96. return nil, err
  97. }
  98. for _, ifa := range ifat {
  99. switch ifa := ifa.(type) {
  100. case *net.IPAddr:
  101. if ip := ifa.IP.To4(); ip != nil {
  102. return ip, nil
  103. }
  104. case *net.IPNet:
  105. if ip := ifa.IP.To4(); ip != nil {
  106. return ip, nil
  107. }
  108. }
  109. }
  110. return nil, errNoSuchInterface
  111. }