25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

115 lines
3.5 KiB

  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. "golang.org/x/net/internal/socket"
  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. *net.IPConn
  14. *socket.Conn
  15. rawOpt
  16. }
  17. func (c *packetHandler) ok() bool { return c != nil && c.IPConn != nil && c.Conn != nil }
  18. // ReadFrom reads an IPv4 datagram from the endpoint c, copying the
  19. // datagram into b. It returns the received datagram as the IPv4
  20. // header h, the payload p and the control message cm.
  21. func (c *packetHandler) ReadFrom(b []byte) (h *Header, p []byte, cm *ControlMessage, err error) {
  22. if !c.ok() {
  23. return nil, nil, nil, errInvalidConn
  24. }
  25. c.rawOpt.RLock()
  26. m := socket.Message{
  27. Buffers: [][]byte{b},
  28. OOB: NewControlMessage(c.rawOpt.cflags),
  29. }
  30. c.rawOpt.RUnlock()
  31. if err := c.RecvMsg(&m, 0); err != nil {
  32. return nil, nil, nil, &net.OpError{Op: "read", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
  33. }
  34. var hs []byte
  35. if hs, p, err = slicePacket(b[:m.N]); err != nil {
  36. return nil, nil, nil, &net.OpError{Op: "read", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
  37. }
  38. if h, err = ParseHeader(hs); err != nil {
  39. return nil, nil, nil, &net.OpError{Op: "read", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
  40. }
  41. if m.NN > 0 {
  42. cm = new(ControlMessage)
  43. if err := cm.Parse(m.OOB[:m.NN]); err != nil {
  44. return nil, nil, nil, &net.OpError{Op: "read", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Err: err}
  45. }
  46. }
  47. if src, ok := m.Addr.(*net.IPAddr); ok && cm != nil {
  48. cm.Src = src.IP
  49. }
  50. return
  51. }
  52. func slicePacket(b []byte) (h, p []byte, err error) {
  53. if len(b) < HeaderLen {
  54. return nil, nil, errHeaderTooShort
  55. }
  56. hdrlen := int(b[0]&0x0f) << 2
  57. return b[:hdrlen], b[hdrlen:], nil
  58. }
  59. // WriteTo writes an IPv4 datagram through the endpoint c, copying the
  60. // datagram from the IPv4 header h and the payload p. The control
  61. // message cm allows the datagram path and the outgoing interface to be
  62. // specified. Currently only Darwin and Linux support this. The cm
  63. // may be nil if control of the outgoing datagram is not required.
  64. //
  65. // The IPv4 header h must contain appropriate fields that include:
  66. //
  67. // Version = <must be specified>
  68. // Len = <must be specified>
  69. // TOS = <must be specified>
  70. // TotalLen = <must be specified>
  71. // ID = platform sets an appropriate value if ID is zero
  72. // FragOff = <must be specified>
  73. // TTL = <must be specified>
  74. // Protocol = <must be specified>
  75. // Checksum = platform sets an appropriate value if Checksum is zero
  76. // Src = platform sets an appropriate value if Src is nil
  77. // Dst = <must be specified>
  78. // Options = optional
  79. func (c *packetHandler) WriteTo(h *Header, p []byte, cm *ControlMessage) error {
  80. if !c.ok() {
  81. return errInvalidConn
  82. }
  83. m := socket.Message{
  84. OOB: cm.Marshal(),
  85. }
  86. wh, err := h.Marshal()
  87. if err != nil {
  88. return err
  89. }
  90. m.Buffers = [][]byte{wh, p}
  91. dst := new(net.IPAddr)
  92. if cm != nil {
  93. if ip := cm.Dst.To4(); ip != nil {
  94. dst.IP = ip
  95. }
  96. }
  97. if dst.IP == nil {
  98. dst.IP = h.Dst
  99. }
  100. m.Addr = dst
  101. if err := c.SendMsg(&m, 0); err != nil {
  102. return &net.OpError{Op: "write", Net: c.IPConn.LocalAddr().Network(), Source: c.IPConn.LocalAddr(), Addr: opAddr(dst), Err: err}
  103. }
  104. return nil
  105. }