payload_cmsg.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 || linux || netbsd || openbsd || solaris || zos
  5. // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
  6. package ipv4
  7. import (
  8. "net"
  9. "golang.org/x/net/internal/socket"
  10. )
  11. // ReadFrom reads a payload of the received IPv4 datagram, from the
  12. // endpoint c, copying the payload into b. It returns the number of
  13. // bytes copied into b, the control message cm and the source address
  14. // src of the received datagram.
  15. func (c *payloadHandler) ReadFrom(b []byte) (n int, cm *ControlMessage, src net.Addr, err error) {
  16. if !c.ok() {
  17. return 0, nil, nil, errInvalidConn
  18. }
  19. c.rawOpt.RLock()
  20. m := socket.Message{
  21. OOB: NewControlMessage(c.rawOpt.cflags),
  22. }
  23. c.rawOpt.RUnlock()
  24. switch c.PacketConn.(type) {
  25. case *net.UDPConn:
  26. m.Buffers = [][]byte{b}
  27. if err := c.RecvMsg(&m, 0); err != nil {
  28. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  29. }
  30. case *net.IPConn:
  31. h := make([]byte, HeaderLen)
  32. m.Buffers = [][]byte{h, b}
  33. if err := c.RecvMsg(&m, 0); err != nil {
  34. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  35. }
  36. hdrlen := int(h[0]&0x0f) << 2
  37. if hdrlen > len(h) {
  38. d := hdrlen - len(h)
  39. copy(b, b[d:])
  40. m.N -= d
  41. } else {
  42. m.N -= hdrlen
  43. }
  44. default:
  45. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: errInvalidConnType}
  46. }
  47. if m.NN > 0 {
  48. if compatFreeBSD32 {
  49. adjustFreeBSD32(&m)
  50. }
  51. cm = new(ControlMessage)
  52. if err := cm.Parse(m.OOB[:m.NN]); err != nil {
  53. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  54. }
  55. cm.Src = netAddrToIP4(m.Addr)
  56. }
  57. return m.N, cm, m.Addr, nil
  58. }
  59. // WriteTo writes a payload of the IPv4 datagram, to the destination
  60. // address dst through the endpoint c, copying the payload from b. It
  61. // returns the number of bytes written. The control message cm allows
  62. // the datagram path and the outgoing interface to be specified.
  63. // Currently only Darwin and Linux support this. The cm may be nil if
  64. // control of the outgoing datagram is not required.
  65. func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
  66. if !c.ok() {
  67. return 0, errInvalidConn
  68. }
  69. m := socket.Message{
  70. Buffers: [][]byte{b},
  71. OOB: cm.Marshal(),
  72. Addr: dst,
  73. }
  74. err = c.SendMsg(&m, 0)
  75. if err != nil {
  76. err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
  77. }
  78. return m.N, err
  79. }