rawconn_msg.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 go1.9
  5. // +build darwin dragonfly freebsd linux netbsd openbsd solaris windows
  6. package socket
  7. import (
  8. "os"
  9. "syscall"
  10. )
  11. func (c *Conn) recvMsg(m *Message, flags int) error {
  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. var h msghdr
  48. vs := make([]iovec, len(m.Buffers))
  49. var sa []byte
  50. if m.Addr != nil {
  51. sa = marshalInetAddr(m.Addr)
  52. }
  53. h.pack(vs, m.Buffers, m.OOB, sa)
  54. var operr error
  55. var n int
  56. fn := func(s uintptr) bool {
  57. n, operr = sendmsg(s, &h, flags)
  58. if operr == syscall.EAGAIN {
  59. return false
  60. }
  61. return true
  62. }
  63. if err := c.c.Write(fn); err != nil {
  64. return err
  65. }
  66. if operr != nil {
  67. return os.NewSyscallError("sendmsg", operr)
  68. }
  69. m.N = n
  70. m.NN = len(m.OOB)
  71. return nil
  72. }