mmsghdr_unix.go 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2017 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 aix linux netbsd
  5. package socket
  6. import "net"
  7. type mmsghdrs []mmsghdr
  8. func (hs mmsghdrs) pack(ms []Message, parseFn func([]byte, string) (net.Addr, error), marshalFn func(net.Addr) []byte) error {
  9. for i := range hs {
  10. vs := make([]iovec, len(ms[i].Buffers))
  11. var sa []byte
  12. if parseFn != nil {
  13. sa = make([]byte, sizeofSockaddrInet6)
  14. }
  15. if marshalFn != nil {
  16. sa = marshalFn(ms[i].Addr)
  17. }
  18. hs[i].Hdr.pack(vs, ms[i].Buffers, ms[i].OOB, sa)
  19. }
  20. return nil
  21. }
  22. func (hs mmsghdrs) unpack(ms []Message, parseFn func([]byte, string) (net.Addr, error), hint string) error {
  23. for i := range hs {
  24. ms[i].N = int(hs[i].Len)
  25. ms[i].NN = hs[i].Hdr.controllen()
  26. ms[i].Flags = hs[i].Hdr.flags()
  27. if parseFn != nil {
  28. var err error
  29. ms[i].Addr, err = parseFn(hs[i].Hdr.name(), hint)
  30. if err != nil {
  31. return err
  32. }
  33. }
  34. }
  35. return nil
  36. }