payload_cmsg.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 !plan9,!windows
  5. package ipv4
  6. import (
  7. "net"
  8. "syscall"
  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, syscall.EINVAL
  17. }
  18. oob := newControlMessage(&c.rawOpt)
  19. var oobn int
  20. switch c := c.PacketConn.(type) {
  21. case *net.UDPConn:
  22. if n, oobn, _, src, err = c.ReadMsgUDP(b, oob); err != nil {
  23. return 0, nil, nil, err
  24. }
  25. case *net.IPConn:
  26. if sockOpts[ssoStripHeader].name > 0 {
  27. if n, oobn, _, src, err = c.ReadMsgIP(b, oob); err != nil {
  28. return 0, nil, nil, err
  29. }
  30. } else {
  31. nb := make([]byte, maxHeaderLen+len(b))
  32. if n, oobn, _, src, err = c.ReadMsgIP(nb, oob); err != nil {
  33. return 0, nil, nil, err
  34. }
  35. hdrlen := int(nb[0]&0x0f) << 2
  36. copy(b, nb[hdrlen:])
  37. n -= hdrlen
  38. }
  39. default:
  40. return 0, nil, nil, errInvalidConnType
  41. }
  42. if cm, err = parseControlMessage(oob[:oobn]); err != nil {
  43. return 0, nil, nil, err
  44. }
  45. if cm != nil {
  46. cm.Src = netAddrToIP4(src)
  47. }
  48. return
  49. }
  50. // WriteTo writes a payload of the IPv4 datagram, to the destination
  51. // address dst through the endpoint c, copying the payload from b. It
  52. // returns the number of bytes written. The control message cm allows
  53. // the datagram path and the outgoing interface to be specified.
  54. // Currently only Darwin and Linux support this. The cm may be nil if
  55. // control of the outgoing datagram is not required.
  56. func (c *payloadHandler) WriteTo(b []byte, cm *ControlMessage, dst net.Addr) (n int, err error) {
  57. if !c.ok() {
  58. return 0, syscall.EINVAL
  59. }
  60. oob := marshalControlMessage(cm)
  61. if dst == nil {
  62. return 0, errMissingAddress
  63. }
  64. switch c := c.PacketConn.(type) {
  65. case *net.UDPConn:
  66. n, _, err = c.WriteMsgUDP(b, oob, dst.(*net.UDPAddr))
  67. case *net.IPConn:
  68. n, _, err = c.WriteMsgIP(b, oob, dst.(*net.IPAddr))
  69. default:
  70. return 0, errInvalidConnType
  71. }
  72. if err != nil {
  73. return 0, err
  74. }
  75. return
  76. }