header.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. )
  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 the IPv4 header h.
  45. func (h *Header) Marshal() ([]byte, error) {
  46. if h == nil {
  47. return nil, syscall.EINVAL
  48. }
  49. if h.Len < HeaderLen {
  50. return nil, errHeaderTooShort
  51. }
  52. hdrlen := HeaderLen + len(h.Options)
  53. b := make([]byte, hdrlen)
  54. b[0] = byte(Version<<4 | (hdrlen >> 2 & 0x0f))
  55. b[1] = byte(h.TOS)
  56. flagsAndFragOff := (h.FragOff & 0x1fff) | int(h.Flags<<13)
  57. switch runtime.GOOS {
  58. case "darwin", "dragonfly", "netbsd":
  59. nativeEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  60. nativeEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  61. case "freebsd":
  62. if freebsdVersion < 1100000 {
  63. nativeEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  64. nativeEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  65. } else {
  66. binary.BigEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  67. binary.BigEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  68. }
  69. default:
  70. binary.BigEndian.PutUint16(b[2:4], uint16(h.TotalLen))
  71. binary.BigEndian.PutUint16(b[6:8], uint16(flagsAndFragOff))
  72. }
  73. binary.BigEndian.PutUint16(b[4:6], uint16(h.ID))
  74. b[8] = byte(h.TTL)
  75. b[9] = byte(h.Protocol)
  76. binary.BigEndian.PutUint16(b[10:12], uint16(h.Checksum))
  77. if ip := h.Src.To4(); ip != nil {
  78. copy(b[12:16], ip[:net.IPv4len])
  79. }
  80. if ip := h.Dst.To4(); ip != nil {
  81. copy(b[16:20], ip[:net.IPv4len])
  82. } else {
  83. return nil, errMissingAddress
  84. }
  85. if len(h.Options) > 0 {
  86. copy(b[HeaderLen:], h.Options)
  87. }
  88. return b, nil
  89. }
  90. // ParseHeader parses b as an IPv4 header.
  91. func ParseHeader(b []byte) (*Header, error) {
  92. if len(b) < HeaderLen {
  93. return nil, errHeaderTooShort
  94. }
  95. hdrlen := int(b[0]&0x0f) << 2
  96. if hdrlen > len(b) {
  97. return nil, errBufferTooShort
  98. }
  99. h := &Header{
  100. Version: int(b[0] >> 4),
  101. Len: hdrlen,
  102. TOS: int(b[1]),
  103. ID: int(binary.BigEndian.Uint16(b[4:6])),
  104. TTL: int(b[8]),
  105. Protocol: int(b[9]),
  106. Checksum: int(binary.BigEndian.Uint16(b[10:12])),
  107. Src: net.IPv4(b[12], b[13], b[14], b[15]),
  108. Dst: net.IPv4(b[16], b[17], b[18], b[19]),
  109. }
  110. switch runtime.GOOS {
  111. case "darwin", "dragonfly", "netbsd":
  112. h.TotalLen = int(nativeEndian.Uint16(b[2:4])) + hdrlen
  113. h.FragOff = int(nativeEndian.Uint16(b[6:8]))
  114. case "freebsd":
  115. if freebsdVersion < 1100000 {
  116. h.TotalLen = int(nativeEndian.Uint16(b[2:4]))
  117. if freebsdVersion < 1000000 {
  118. h.TotalLen += hdrlen
  119. }
  120. h.FragOff = int(nativeEndian.Uint16(b[6:8]))
  121. } else {
  122. h.TotalLen = int(binary.BigEndian.Uint16(b[2:4]))
  123. h.FragOff = int(binary.BigEndian.Uint16(b[6:8]))
  124. }
  125. default:
  126. h.TotalLen = int(binary.BigEndian.Uint16(b[2:4]))
  127. h.FragOff = int(binary.BigEndian.Uint16(b[6:8]))
  128. }
  129. h.Flags = HeaderFlags(h.FragOff&0xe000) >> 13
  130. h.FragOff = h.FragOff & 0x1fff
  131. if hdrlen-HeaderLen > 0 {
  132. h.Options = make([]byte, hdrlen-HeaderLen)
  133. copy(h.Options, b[HeaderLen:])
  134. }
  135. return h, nil
  136. }