control_unix.go 912 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 linux netbsd openbsd
  5. package ipv4
  6. import (
  7. "os"
  8. "syscall"
  9. )
  10. func newControlMessage(opt *rawOpt) (oob []byte) {
  11. opt.Lock()
  12. defer opt.Unlock()
  13. return opt.marshalControlMessage()
  14. }
  15. func parseControlMessage(b []byte) (*ControlMessage, error) {
  16. if len(b) == 0 {
  17. return nil, nil
  18. }
  19. cmsgs, err := syscall.ParseSocketControlMessage(b)
  20. if err != nil {
  21. return nil, os.NewSyscallError("parse socket control message", err)
  22. }
  23. cm := &ControlMessage{}
  24. for _, m := range cmsgs {
  25. if m.Header.Level != ianaProtocolIP {
  26. continue
  27. }
  28. cm.parseControlMessage(&m)
  29. }
  30. return cm, nil
  31. }
  32. func marshalControlMessage(cm *ControlMessage) (oob []byte) {
  33. if cm == nil {
  34. return nil
  35. }
  36. return cm.marshalPacketInfo()
  37. }