sys_mreq.go 738 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2014 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 dragonfly freebsd netbsd openbsd windows
  5. package ipv4
  6. import (
  7. "net"
  8. "syscall"
  9. )
  10. func setSysIPMreqInterface(mreq *syscall.IPMreq, ifi *net.Interface) error {
  11. if ifi == nil {
  12. return nil
  13. }
  14. ifat, err := ifi.Addrs()
  15. if err != nil {
  16. return err
  17. }
  18. for _, ifa := range ifat {
  19. switch v := ifa.(type) {
  20. case *net.IPAddr:
  21. if ip := v.IP.To4(); ip != nil {
  22. copy(mreq.Interface[:], ip)
  23. return nil
  24. }
  25. case *net.IPNet:
  26. if ip := v.IP.To4(); ip != nil {
  27. copy(mreq.Interface[:], ip)
  28. return nil
  29. }
  30. }
  31. }
  32. return errNoSuchInterface
  33. }