payload_cmsg.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 aix 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. adjustFreeBSD32(&m)
  49. }
  50. cm = new(ControlMessage)
  51. if err := cm.Parse(m.OOB[:m.NN]); err != nil {
  52. return 0, nil, nil, &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
  53. }
  54. cm.Src = netAddrToIP4(m.Addr)
  55. }
  56. return m.N, cm, m.Addr, nil
  57. }
  58. // WriteTo writes a payload of the IPv4 datagram, to the destination
  59. // address dst through the endpoint c, copying the payload from b. It
  60. // returns the number of bytes written. The control message cm allows
  61. // the datagram path and the outgoing interface to be specified.
  62. // Currently only Darwin and Linux support this. The cm may be nil if
  63. // control of the outgoing datagram is not required.
  64. func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
  65. if !c.ok() {
  66. return 0, errInvalidConn
  67. }
  68. m := socket.Message{
  69. Buffers: [][]byte{b},
  70. OOB: cm.Marshal(),
  71. Addr: dst,
  72. }
  73. err = c.SendMsg(&m, 0)
  74. if err != nil {
  75. err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Addr: opAddr(dst), Err: err}
  76. }
  77. return m.N, err
  78. }