header.go 4.6 KB

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