header.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "errors"
  7. "fmt"
  8. "net"
  9. "runtime"
  10. "syscall"
  11. "unsafe"
  12. )
  13. var (
  14. errMissingAddress = errors.New("missing address")
  15. errMissingHeader = errors.New("missing header")
  16. errHeaderTooShort = errors.New("header too short")
  17. errBufferTooShort = errors.New("buffer too short")
  18. errInvalidConnType = errors.New("invalid conn type")
  19. )
  20. // References:
  21. //
  22. // RFC 791 Internet Protocol
  23. // http://tools.ietf.org/html/rfc791
  24. // RFC 1112 Host Extensions for IP Multicasting
  25. // http://tools.ietf.org/html/rfc1112
  26. // RFC 1122 Requirements for Internet Hosts
  27. // http://tools.ietf.org/html/rfc1122
  28. // RFC 3678 Socket Interface Extensions for Multicast Source Filters
  29. // http://tools.ietf.org/html/rfc3678
  30. // RFC 4607 Source-Specific Multicast for IP
  31. // http://tools.ietf.org/html/rfc4607
  32. const (
  33. Version = 4 // protocol version
  34. HeaderLen = 20 // header length without extension headers
  35. maxHeaderLen = 60 // sensible default, revisit if later RFCs define new usage of version and header length fields
  36. )
  37. type HeaderFlags int
  38. const (
  39. MoreFragments HeaderFlags = 1 << iota // more fragments flag
  40. DontFragment // don't fragment flag
  41. )
  42. // A Header represents an IPv4 header.
  43. type Header struct {
  44. Version int // protocol version
  45. Len int // header length
  46. TOS int // type-of-service
  47. TotalLen int // packet total length
  48. ID int // identification
  49. Flags HeaderFlags // flags
  50. FragOff int // fragment offset
  51. TTL int // time-to-live
  52. Protocol int // next protocol
  53. Checksum int // checksum
  54. Src net.IP // source address
  55. Dst net.IP // destination address
  56. Options []byte // options, extension headers
  57. }
  58. func (h *Header) String() string {
  59. if h == nil {
  60. return "<nil>"
  61. }
  62. return fmt.Sprintf("ver: %v, hdrlen: %v, tos: %#x, totallen: %v, id: %#x, flags: %#x, fragoff: %#x, ttl: %v, proto: %v, 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)
  63. }
  64. // Marshal returns the binary encoding of the IPv4 header h.
  65. func (h *Header) Marshal() ([]byte, error) {
  66. if h == nil {
  67. return nil, syscall.EINVAL
  68. }
  69. if h.Len < HeaderLen {
  70. return nil, errHeaderTooShort
  71. }
  72. hdrlen := HeaderLen + len(h.Options)
  73. b := make([]byte, hdrlen)
  74. b[0] = byte(Version<<4 | (hdrlen >> 2 & 0x0f))
  75. b[1] = byte(h.TOS)
  76. flagsAndFragOff := (h.FragOff & 0x1fff) | int(h.Flags<<13)
  77. switch runtime.GOOS {
  78. case "darwin", "dragonfly", "freebsd", "netbsd":
  79. // TODO(mikio): fix potential misaligned memory access
  80. *(*uint16)(unsafe.Pointer(&b[2:3][0])) = uint16(h.TotalLen)
  81. *(*uint16)(unsafe.Pointer(&b[6:7][0])) = uint16(flagsAndFragOff)
  82. default:
  83. b[2], b[3] = byte(h.TotalLen>>8), byte(h.TotalLen)
  84. b[6], b[7] = byte(flagsAndFragOff>>8), byte(flagsAndFragOff)
  85. }
  86. b[4], b[5] = byte(h.ID>>8), byte(h.ID)
  87. b[8] = byte(h.TTL)
  88. b[9] = byte(h.Protocol)
  89. b[10], b[11] = byte(h.Checksum>>8), byte(h.Checksum)
  90. if ip := h.Src.To4(); ip != nil {
  91. copy(b[12:16], ip[:net.IPv4len])
  92. }
  93. if ip := h.Dst.To4(); ip != nil {
  94. copy(b[16:20], ip[:net.IPv4len])
  95. } else {
  96. return nil, errMissingAddress
  97. }
  98. if len(h.Options) > 0 {
  99. copy(b[HeaderLen:], h.Options)
  100. }
  101. return b, nil
  102. }
  103. // See http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html.
  104. var freebsdVersion uint32
  105. // ParseHeader parses b as an IPv4 header.
  106. func ParseHeader(b []byte) (*Header, error) {
  107. if len(b) < HeaderLen {
  108. return nil, errHeaderTooShort
  109. }
  110. hdrlen := int(b[0]&0x0f) << 2
  111. if hdrlen > len(b) {
  112. return nil, errBufferTooShort
  113. }
  114. h := &Header{
  115. Version: int(b[0] >> 4),
  116. Len: hdrlen,
  117. TOS: int(b[1]),
  118. ID: int(b[4])<<8 | int(b[5]),
  119. TTL: int(b[8]),
  120. Protocol: int(b[9]),
  121. Checksum: int(b[10])<<8 | int(b[11]),
  122. Src: net.IPv4(b[12], b[13], b[14], b[15]),
  123. Dst: net.IPv4(b[16], b[17], b[18], b[19]),
  124. }
  125. switch runtime.GOOS {
  126. case "darwin", "dragonfly", "netbsd":
  127. // TODO(mikio): fix potential misaligned memory access
  128. h.TotalLen = int(*(*uint16)(unsafe.Pointer(&b[2:3][0]))) + hdrlen
  129. // TODO(mikio): fix potential misaligned memory access
  130. h.FragOff = int(*(*uint16)(unsafe.Pointer(&b[6:7][0])))
  131. case "freebsd":
  132. // TODO(mikio): fix potential misaligned memory access
  133. h.TotalLen = int(*(*uint16)(unsafe.Pointer(&b[2:3][0])))
  134. if freebsdVersion < 1000000 {
  135. h.TotalLen += hdrlen
  136. }
  137. // TODO(mikio): fix potential misaligned memory access
  138. h.FragOff = int(*(*uint16)(unsafe.Pointer(&b[6:7][0])))
  139. default:
  140. h.TotalLen = int(b[2])<<8 | int(b[3])
  141. h.FragOff = int(b[6])<<8 | int(b[7])
  142. }
  143. h.Flags = HeaderFlags(h.FragOff&0xe000) >> 13
  144. h.FragOff = h.FragOff & 0x1fff
  145. if hdrlen-HeaderLen > 0 {
  146. h.Options = make([]byte, hdrlen-HeaderLen)
  147. copy(h.Options, b[HeaderLen:])
  148. }
  149. return h, nil
  150. }