sockopt_asmreq_posix.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 dragonfly freebsd netbsd openbsd solaris windows
  5. package ipv4
  6. import (
  7. "net"
  8. "os"
  9. "unsafe"
  10. "golang.org/x/net/internal/iana"
  11. )
  12. func setsockoptIPMreq(s uintptr, name int, ifi *net.Interface, grp net.IP) error {
  13. mreq := ipMreq{Multiaddr: [4]byte{grp[0], grp[1], grp[2], grp[3]}}
  14. if err := setIPMreqInterface(&mreq, ifi); err != nil {
  15. return err
  16. }
  17. return os.NewSyscallError("setsockopt", setsockopt(s, iana.ProtocolIP, name, unsafe.Pointer(&mreq), sizeofIPMreq))
  18. }
  19. func getsockoptInterface(s uintptr, name int) (*net.Interface, error) {
  20. var b [4]byte
  21. l := uint32(4)
  22. if err := getsockopt(s, iana.ProtocolIP, name, unsafe.Pointer(&b[0]), &l); err != nil {
  23. return nil, os.NewSyscallError("getsockopt", err)
  24. }
  25. ifi, err := netIP4ToInterface(net.IPv4(b[0], b[1], b[2], b[3]))
  26. if err != nil {
  27. return nil, err
  28. }
  29. return ifi, nil
  30. }
  31. func setsockoptInterface(s uintptr, name int, ifi *net.Interface) error {
  32. ip, err := netInterfaceToIP4(ifi)
  33. if err != nil {
  34. return err
  35. }
  36. var b [4]byte
  37. copy(b[:], ip)
  38. return os.NewSyscallError("setsockopt", setsockopt(s, iana.ProtocolIP, name, unsafe.Pointer(&b[0]), uint32(4)))
  39. }