helper_posix.go 819 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 darwin freebsd linux netbsd openbsd windows
  5. package ipv4
  6. import (
  7. "bytes"
  8. "net"
  9. "syscall"
  10. )
  11. func setSyscallIPMreq(mreq *syscall.IPMreq, ifi *net.Interface) error {
  12. if ifi == nil {
  13. return nil
  14. }
  15. ifat, err := ifi.Addrs()
  16. if err != nil {
  17. return err
  18. }
  19. for _, ifa := range ifat {
  20. switch v := ifa.(type) {
  21. case *net.IPAddr:
  22. if a := v.IP.To4(); a != nil {
  23. copy(mreq.Interface[:], a)
  24. goto done
  25. }
  26. case *net.IPNet:
  27. if a := v.IP.To4(); a != nil {
  28. copy(mreq.Interface[:], a)
  29. goto done
  30. }
  31. }
  32. }
  33. done:
  34. if bytes.Equal(mreq.Multiaddr[:], net.IPv4zero.To4()) {
  35. return errNoSuchMulticastInterface
  36. }
  37. return nil
  38. }