sockopt_asmreqn_unix.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 freebsd linux
  5. package ipv4
  6. import (
  7. "net"
  8. "os"
  9. "unsafe"
  10. "golang.org/x/net/internal/iana"
  11. )
  12. func getsockoptIPMreqn(s uintptr, name int) (*net.Interface, error) {
  13. var mreqn ipMreqn
  14. l := uint32(sizeofIPMreqn)
  15. if err := getsockopt(s, iana.ProtocolIP, name, unsafe.Pointer(&mreqn), &l); err != nil {
  16. return nil, os.NewSyscallError("getsockopt", err)
  17. }
  18. if mreqn.Ifindex == 0 {
  19. return nil, nil
  20. }
  21. ifi, err := net.InterfaceByIndex(int(mreqn.Ifindex))
  22. if err != nil {
  23. return nil, err
  24. }
  25. return ifi, nil
  26. }
  27. func setsockoptIPMreqn(s uintptr, name int, ifi *net.Interface, grp net.IP) error {
  28. var mreqn ipMreqn
  29. if ifi != nil {
  30. mreqn.Ifindex = int32(ifi.Index)
  31. }
  32. if grp != nil {
  33. mreqn.Multiaddr = [4]byte{grp[0], grp[1], grp[2], grp[3]}
  34. }
  35. return os.NewSyscallError("setsockopt", setsockopt(s, iana.ProtocolIP, name, unsafe.Pointer(&mreqn), sizeofIPMreqn))
  36. }