payload_cmsg.go 2.7 KB

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