header.go 5.0 KB

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