rawconn_msg.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 darwin dragonfly freebsd linux netbsd openbsd solaris windows
  5. package socket
  6. import (
  7. "os"
  8. "syscall"
  9. )
  10. func (c *Conn) recvMsg(m *Message, flags int) error {
  11. m.raceWrite()
  12. var h msghdr
  13. vs := make([]iovec, len(m.Buffers))
  14. var sa []byte
  15. if c.network != "tcp" {
  16. sa = make([]byte, sizeofSockaddrInet6)
  17. }
  18. h.pack(vs, m.Buffers, m.OOB, sa)
  19. var operr error
  20. var n int
  21. fn := func(s uintptr) bool {
  22. n, operr = recvmsg(s, &h, flags)
  23. if operr == syscall.EAGAIN {
  24. return false
  25. }
  26. return true
  27. }
  28. if err := c.c.Read(fn); err != nil {
  29. return err
  30. }
  31. if operr != nil {
  32. return os.NewSyscallError("recvmsg", operr)
  33. }
  34. if c.network != "tcp" {
  35. var err error
  36. m.Addr, err = parseInetAddr(sa[:], c.network)
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. m.N = n
  42. m.NN = h.controllen()
  43. m.Flags = h.flags()
  44. return nil
  45. }
  46. func (c *Conn) sendMsg(m *Message, flags int) error {
  47. m.raceRead()
  48. var h msghdr
  49. vs := make([]iovec, len(m.Buffers))
  50. var sa []byte
  51. if m.Addr != nil {
  52. sa = marshalInetAddr(m.Addr)
  53. }
  54. h.pack(vs, m.Buffers, m.OOB, sa)
  55. var operr error
  56. var n int
  57. fn := func(s uintptr) bool {
  58. n, operr = sendmsg(s, &h, flags)
  59. if operr == syscall.EAGAIN {
  60. return false
  61. }
  62. return true
  63. }
  64. if err := c.c.Write(fn); err != nil {
  65. return err
  66. }
  67. if operr != nil {
  68. return os.NewSyscallError("sendmsg", operr)
  69. }
  70. m.N = n
  71. m.NN = len(m.OOB)
  72. return nil
  73. }