sys_asmreqn.go 967 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. "unsafe"
  9. "golang.org/x/net/internal/socket"
  10. )
  11. func (so *sockOpt) getIPMreqn(c *socket.Conn) (*net.Interface, error) {
  12. b := make([]byte, so.Len)
  13. if _, err := so.Get(c, b); err != nil {
  14. return nil, err
  15. }
  16. mreqn := (*ipMreqn)(unsafe.Pointer(&b[0]))
  17. if mreqn.Ifindex == 0 {
  18. return nil, nil
  19. }
  20. ifi, err := net.InterfaceByIndex(int(mreqn.Ifindex))
  21. if err != nil {
  22. return nil, err
  23. }
  24. return ifi, nil
  25. }
  26. func (so *sockOpt) setIPMreqn(c *socket.Conn, ifi *net.Interface, grp net.IP) error {
  27. var mreqn ipMreqn
  28. if ifi != nil {
  29. mreqn.Ifindex = int32(ifi.Index)
  30. }
  31. if grp != nil {
  32. mreqn.Multiaddr = [4]byte{grp[0], grp[1], grp[2], grp[3]}
  33. }
  34. b := (*[sizeofIPMreqn]byte)(unsafe.Pointer(&mreqn))[:sizeofIPMreqn]
  35. return so.Set(c, b)
  36. }