packet.go 2.7 KB

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