packet.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. package ipv4
  5. import (
  6. "net"
  7. "syscall"
  8. )
  9. // BUG(mikio): On Windows, the ReadFrom and WriteTo methods of RawConn
  10. // are not implemented.
  11. // A packetHandler represents the IPv4 datagram handler.
  12. type packetHandler struct {
  13. c *net.IPConn
  14. rawOpt
  15. }
  16. func (c *packetHandler) ok() bool { return c != nil && c.c != nil }
  17. // ReadFrom reads an IPv4 datagram from the endpoint c, copying the
  18. // datagram into b. It returns the received datagram as the IPv4
  19. // header h, the payload p and the control message cm.
  20. func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
  21. if !c.ok() {
  22. return nil, nil, nil, syscall.EINVAL
  23. }
  24. oob := newControlMessage(&c.rawOpt)
  25. n, oobn, _, src, err := c.c.ReadMsgIP(b, oob)
  26. if err != nil {
  27. return nil, nil, nil, err
  28. }
  29. var hs []byte
  30. if hs, p, err = slicePacket(b[:n]); err != nil {
  31. return nil, nil, nil, err
  32. }
  33. if h, err = ParseHeader(hs); err != nil {
  34. return nil, nil, nil, err
  35. }
  36. if cm, err = parseControlMessage(oob[:oobn]); err != nil {
  37. return nil, nil, nil, err
  38. }
  39. if src != nil && cm != nil {
  40. cm.Src = src.IP
  41. }
  42. return
  43. }
  44. func slicePacket(b []byte) (h, p []byte, err error) {
  45. if len(b) < HeaderLen {
  46. return nil, nil, errHeaderTooShort
  47. }
  48. hdrlen := int(b[0]&0x0f) << 2
  49. return b[:hdrlen], b[hdrlen:], nil
  50. }
  51. // WriteTo writes an IPv4 datagram through the endpoint c, copying the
  52. // datagram from the IPv4 header h and the payload p. The control
  53. // message cm allows the datagram path and the outgoing interface to be
  54. // specified. Currently only Darwin and Linux support this. The cm
  55. // may be nil if control of the outgoing datagram is not required.
  56. //
  57. // The IPv4 header h must contain appropriate fields that include:
  58. //
  59. // Version = <must be specified>
  60. // Len = <must be specified>
  61. // TOS = <must be specified>
  62. // TotalLen = <must be specified>
  63. // ID = platform sets an appropriate value if ID is zero
  64. // FragOff = <must be specified>
  65. // TTL = <must be specified>
  66. // Protocol = <must be specified>
  67. // Checksum = platform sets an appropriate value if Checksum is zero
  68. // Src = platform sets an appropriate value if Src is nil
  69. // Dst = <must be specified>
  70. // Options = optional
  71. func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
  72. if !c.ok() {
  73. return syscall.EINVAL
  74. }
  75. oob := marshalControlMessage(cm)
  76. wh, err := h.Marshal()
  77. if err != nil {
  78. return err
  79. }
  80. dst := &net.IPAddr{}
  81. if cm != nil {
  82. if ip := cm.Dst.To4(); ip != nil {
  83. dst.IP = ip
  84. }
  85. }
  86. if dst.IP == nil {
  87. dst.IP = h.Dst
  88. }
  89. wh = append(wh, p...)
  90. _, _, err = c.c.WriteMsgIP(wh, oob, dst)
  91. return err
  92. }