control_bsd.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. //go:build aix || darwin || dragonfly || freebsd || netbsd || openbsd
  5. // +build aix darwin dragonfly freebsd netbsd openbsd
  6. package ipv4
  7. import (
  8. "net"
  9. "syscall"
  10. "unsafe"
  11. "golang.org/x/net/internal/iana"
  12. "golang.org/x/net/internal/socket"
  13. "golang.org/x/sys/unix"
  14. )
  15. func marshalDst(b []byte, cm *ControlMessage) []byte {
  16. m := socket.ControlMessage(b)
  17. m.MarshalHeader(iana.ProtocolIP, unix.IP_RECVDSTADDR, net.IPv4len)
  18. return m.Next(net.IPv4len)
  19. }
  20. func parseDst(cm *ControlMessage, b []byte) {
  21. if len(cm.Dst) < net.IPv4len {
  22. cm.Dst = make(net.IP, net.IPv4len)
  23. }
  24. copy(cm.Dst, b[:net.IPv4len])
  25. }
  26. func marshalInterface(b []byte, cm *ControlMessage) []byte {
  27. m := socket.ControlMessage(b)
  28. m.MarshalHeader(iana.ProtocolIP, sockoptReceiveInterface, syscall.SizeofSockaddrDatalink)
  29. return m.Next(syscall.SizeofSockaddrDatalink)
  30. }
  31. func parseInterface(cm *ControlMessage, b []byte) {
  32. var sadl syscall.SockaddrDatalink
  33. copy((*[unsafe.Sizeof(sadl)]byte)(unsafe.Pointer(&sadl))[:], b)
  34. cm.IfIndex = int(sadl.Index)
  35. }