sockopt_asmreq_windows.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. package ipv4
  5. import (
  6. "net"
  7. "os"
  8. "syscall"
  9. "unsafe"
  10. "golang.org/x/net/internal/iana"
  11. )
  12. func setsockoptIPMreq(fd syscall.Handle, name int, ifi *net.Interface, grp net.IP) error {
  13. mreq := sysIPMreq{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", syscall.Setsockopt(fd, iana.ProtocolIP, int32(name), (*byte)(unsafe.Pointer(&mreq)), int32(sysSizeofIPMreq)))
  18. }
  19. func getsockoptInterface(fd syscall.Handle, name int) (*net.Interface, error) {
  20. var b [4]byte
  21. l := int32(4)
  22. if err := syscall.Getsockopt(fd, iana.ProtocolIP, int32(name), (*byte)(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(fd syscall.Handle, 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", syscall.Setsockopt(fd, iana.ProtocolIP, int32(name), (*byte)(unsafe.Pointer(&b[0])), 4))
  39. }