header.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. "encoding/binary"
  7. "fmt"
  8. "net"
  9. "runtime"
  10. "golang.org/x/net/internal/socket"
  11. )
  12. const (
  13. Version = 4 // protocol version
  14. HeaderLen = 20 // header length without extension headers
  15. maxHeaderLen = 60 // sensible default, revisit if later RFCs define new usage of version and header length fields
  16. )
  17. type HeaderFlags int
  18. const (
  19. MoreFragments HeaderFlags = 1 << iota // more fragments flag
  20. DontFragment // don't fragment flag
  21. )
  22. // A Header represents an IPv4 header.
  23. type Header struct {
  24. Version int // protocol version
  25. Len int // header length
  26. TOS int // type-of-service
  27. TotalLen int // packet total length
  28. ID int // identification
  29. Flags HeaderFlags // flags
  30. FragOff int // fragment offset
  31. TTL int // time-to-live
  32. Protocol int // next protocol
  33. Checksum int // checksum
  34. Src net.IP // source address
  35. Dst net.IP // destination address
  36. Options []byte // options, extension headers
  37. }
  38. func (h *Header) String() string {
  39. if h == nil {
  40. return "<nil>"
  41. }
  42. return fmt.Sprintf("ver=%d hdrlen=%d tos=%#x totallen=%d id=%#x flags=%#x fragoff=%#x ttl=%d proto=%d cksum=%#x src=%v dst=%v", h.Version, h.Len, h.TOS, h.TotalLen, h.ID, h.Flags, h.FragOff, h.TTL, h.Protocol, h.Checksum, h.Src, h.Dst)
  43. }
  44. // Marshal returns the binary encoding of h.
  45. //
  46. // The returned slice is in the format used by a raw IP socket on the
  47. // local system.
  48. // This may differ from the wire format, depending on the system.
  49. func (h *Header) Marshal() ([]byte, error) {
  50. if h == nil {
  51. return nil, errInvalidConn
  52. }
  53. if h.Len < HeaderLen {
  54. return nil, errHeaderTooShort
  55. }
  56. hdrlen := HeaderLen + len(h.Options)
  57. b := make([]byte, hdrlen)
  58. b[0] = byte(Version<<4 | (hdrlen >> 2 & 0x0f))
  59. b[1] = byte(h.TOS)
  60. flagsAndFragOff := (h.FragOff & 0x1fff) | int(h.Flags<<13)
  61. switch runtime.GOOS {
  62. case "darwin", "dragonfly", "netbsd":
  63. socket.NativeEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  64. socket.NativeEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  65. case "freebsd":
  66. if freebsdVersion < 1100000 {
  67. socket.NativeEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  68. socket.NativeEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  69. } else {
  70. binary.BigEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  71. binary.BigEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  72. }
  73. default:
  74. binary.BigEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  75. binary.BigEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  76. }
  77. binary.BigEndian.PutUint16(b[4:6], uint16(h.ID))
  78. b[8] = byte(h.TTL)
  79. b[9] = byte(h.Protocol)
  80. binary.BigEndian.PutUint16(b[10:12], uint16(h.Checksum))
  81. if ip := h.Src.To4(); ip != nil {
  82. copy(b[12:16], ip[:net.IPv4len])
  83. }
  84. if ip := h.Dst.To4(); ip != nil {
  85. copy(b[16:20], ip[:net.IPv4len])
  86. } else {
  87. return nil, errMissingAddress
  88. }
  89. if len(h.Options) > 0 {
  90. copy(b[HeaderLen:], h.Options)
  91. }
  92. return b, nil
  93. }
  94. // Parse parses b as an IPv4 header and stores the result in h.
  95. //
  96. // The provided b must be in the format used by a raw IP socket on the
  97. // local system.
  98. // This may differ from the wire format, depending on the system.
  99. func (h *Header) Parse(b []byte) error {
  100. if h == nil || len(b) < HeaderLen {
  101. return errHeaderTooShort
  102. }
  103. hdrlen := int(b[0]&0x0f) << 2
  104. if hdrlen > len(b) {
  105. return errBufferTooShort
  106. }
  107. h.Version = int(b[0] >> 4)
  108. h.Len = hdrlen
  109. h.TOS = int(b[1])
  110. h.ID = int(binary.BigEndian.Uint16(b[4:6]))
  111. h.TTL = int(b[8])
  112. h.Protocol = int(b[9])
  113. h.Checksum = int(binary.BigEndian.Uint16(b[10:12]))
  114. h.Src = net.IPv4(b[12], b[13], b[14], b[15])
  115. h.Dst = net.IPv4(b[16], b[17], b[18], b[19])
  116. switch runtime.GOOS {
  117. case "darwin", "dragonfly", "netbsd":
  118. h.TotalLen = int(socket.NativeEndian.Uint16(b[2:4])) + hdrlen
  119. h.FragOff = int(socket.NativeEndian.Uint16(b[6:8]))
  120. case "freebsd":
  121. if freebsdVersion < 1100000 {
  122. h.TotalLen = int(socket.NativeEndian.Uint16(b[2:4]))
  123. if freebsdVersion < 1000000 {
  124. h.TotalLen += hdrlen
  125. }
  126. h.FragOff = int(socket.NativeEndian.Uint16(b[6:8]))
  127. } else {
  128. h.TotalLen = int(binary.BigEndian.Uint16(b[2:4]))
  129. h.FragOff = int(binary.BigEndian.Uint16(b[6:8]))
  130. }
  131. default:
  132. h.TotalLen = int(binary.BigEndian.Uint16(b[2:4]))
  133. h.FragOff = int(binary.BigEndian.Uint16(b[6:8]))
  134. }
  135. h.Flags = HeaderFlags(h.FragOff&0xe000) >> 13
  136. h.FragOff = h.FragOff & 0x1fff
  137. optlen := hdrlen - HeaderLen
  138. if optlen > 0 && len(b) >= hdrlen {
  139. if cap(h.Options) < optlen {
  140. h.Options = make([]byte, optlen)
  141. } else {
  142. h.Options = h.Options[:optlen]
  143. }
  144. copy(h.Options, b[HeaderLen:hdrlen])
  145. }
  146. return nil
  147. }
  148. // ParseHeader parses b as an IPv4 header.
  149. //
  150. // The provided b must be in the format used by a raw IP socket on the
  151. // local system.
  152. // This may differ from the wire format, depending on the system.
  153. func ParseHeader(b []byte) (*Header, error) {
  154. h := new(Header)
  155. if err := h.Parse(b); err != nil {
  156. return nil, err
  157. }
  158. return h, nil
  159. }