reader.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package maxminddb
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "net"
  7. "reflect"
  8. )
  9. const (
  10. // NotFound is returned by LookupOffset when a matched root record offset
  11. // cannot be found.
  12. NotFound = ^uintptr(0)
  13. dataSectionSeparatorSize = 16
  14. )
  15. var metadataStartMarker = []byte("\xAB\xCD\xEFMaxMind.com")
  16. // Reader holds the data corresponding to the MaxMind DB file. Its only public
  17. // field is Metadata, which contains the metadata from the MaxMind DB file.
  18. type Reader struct {
  19. hasMappedFile bool
  20. buffer []byte
  21. nodeReader nodeReader
  22. decoder decoder
  23. Metadata Metadata
  24. ipv4Start uint
  25. ipv4StartBitDepth int
  26. nodeOffsetMult uint
  27. }
  28. // Metadata holds the metadata decoded from the MaxMind DB file. In particular
  29. // in has the format version, the build time as Unix epoch time, the database
  30. // type and description, the IP version supported, and a slice of the natural
  31. // languages included.
  32. type Metadata struct {
  33. BinaryFormatMajorVersion uint `maxminddb:"binary_format_major_version"`
  34. BinaryFormatMinorVersion uint `maxminddb:"binary_format_minor_version"`
  35. BuildEpoch uint `maxminddb:"build_epoch"`
  36. DatabaseType string `maxminddb:"database_type"`
  37. Description map[string]string `maxminddb:"description"`
  38. IPVersion uint `maxminddb:"ip_version"`
  39. Languages []string `maxminddb:"languages"`
  40. NodeCount uint `maxminddb:"node_count"`
  41. RecordSize uint `maxminddb:"record_size"`
  42. }
  43. // FromBytes takes a byte slice corresponding to a MaxMind DB file and returns
  44. // a Reader structure or an error.
  45. func FromBytes(buffer []byte) (*Reader, error) {
  46. metadataStart := bytes.LastIndex(buffer, metadataStartMarker)
  47. if metadataStart == -1 {
  48. return nil, newInvalidDatabaseError("error opening database: invalid MaxMind DB file")
  49. }
  50. metadataStart += len(metadataStartMarker)
  51. metadataDecoder := decoder{buffer[metadataStart:]}
  52. var metadata Metadata
  53. rvMetdata := reflect.ValueOf(&metadata)
  54. _, err := metadataDecoder.decode(0, rvMetdata, 0)
  55. if err != nil {
  56. return nil, err
  57. }
  58. searchTreeSize := metadata.NodeCount * metadata.RecordSize / 4
  59. dataSectionStart := searchTreeSize + dataSectionSeparatorSize
  60. dataSectionEnd := uint(metadataStart - len(metadataStartMarker))
  61. if dataSectionStart > dataSectionEnd {
  62. return nil, newInvalidDatabaseError("the MaxMind DB contains invalid metadata")
  63. }
  64. d := decoder{
  65. buffer[searchTreeSize+dataSectionSeparatorSize : metadataStart-len(metadataStartMarker)],
  66. }
  67. nodeBuffer := buffer[:searchTreeSize]
  68. var nodeReader nodeReader
  69. switch metadata.RecordSize {
  70. case 24:
  71. nodeReader = nodeReader24{buffer: nodeBuffer}
  72. case 28:
  73. nodeReader = nodeReader28{buffer: nodeBuffer}
  74. case 32:
  75. nodeReader = nodeReader32{buffer: nodeBuffer}
  76. default:
  77. return nil, newInvalidDatabaseError("unknown record size: %d", metadata.RecordSize)
  78. }
  79. reader := &Reader{
  80. buffer: buffer,
  81. nodeReader: nodeReader,
  82. decoder: d,
  83. Metadata: metadata,
  84. ipv4Start: 0,
  85. nodeOffsetMult: metadata.RecordSize / 4,
  86. }
  87. reader.setIPv4Start()
  88. return reader, err
  89. }
  90. func (r *Reader) setIPv4Start() {
  91. if r.Metadata.IPVersion != 6 {
  92. return
  93. }
  94. nodeCount := r.Metadata.NodeCount
  95. node := uint(0)
  96. i := 0
  97. for ; i < 96 && node < nodeCount; i++ {
  98. node = r.nodeReader.readLeft(node * r.nodeOffsetMult)
  99. }
  100. r.ipv4Start = node
  101. r.ipv4StartBitDepth = i
  102. }
  103. // Lookup retrieves the database record for ip and stores it in the value
  104. // pointed to by result. If result is nil or not a pointer, an error is
  105. // returned. If the data in the database record cannot be stored in result
  106. // because of type differences, an UnmarshalTypeError is returned. If the
  107. // database is invalid or otherwise cannot be read, an InvalidDatabaseError
  108. // is returned.
  109. func (r *Reader) Lookup(ip net.IP, result interface{}) error {
  110. if r.buffer == nil {
  111. return errors.New("cannot call Lookup on a closed database")
  112. }
  113. pointer, _, _, err := r.lookupPointer(ip)
  114. if pointer == 0 || err != nil {
  115. return err
  116. }
  117. return r.retrieveData(pointer, result)
  118. }
  119. // LookupNetwork retrieves the database record for ip and stores it in the
  120. // value pointed to by result. The network returned is the network associated
  121. // with the data record in the database. The ok return value indicates whether
  122. // the database contained a record for the ip.
  123. //
  124. // If result is nil or not a pointer, an error is returned. If the data in the
  125. // database record cannot be stored in result because of type differences, an
  126. // UnmarshalTypeError is returned. If the database is invalid or otherwise
  127. // cannot be read, an InvalidDatabaseError is returned.
  128. func (r *Reader) LookupNetwork(ip net.IP, result interface{}) (network *net.IPNet, ok bool, err error) {
  129. if r.buffer == nil {
  130. return nil, false, errors.New("cannot call Lookup on a closed database")
  131. }
  132. pointer, prefixLength, ip, err := r.lookupPointer(ip)
  133. network = r.cidr(ip, prefixLength)
  134. if pointer == 0 || err != nil {
  135. return network, false, err
  136. }
  137. return network, true, r.retrieveData(pointer, result)
  138. }
  139. // LookupOffset maps an argument net.IP to a corresponding record offset in the
  140. // database. NotFound is returned if no such record is found, and a record may
  141. // otherwise be extracted by passing the returned offset to Decode. LookupOffset
  142. // is an advanced API, which exists to provide clients with a means to cache
  143. // previously-decoded records.
  144. func (r *Reader) LookupOffset(ip net.IP) (uintptr, error) {
  145. if r.buffer == nil {
  146. return 0, errors.New("cannot call LookupOffset on a closed database")
  147. }
  148. pointer, _, _, err := r.lookupPointer(ip)
  149. if pointer == 0 || err != nil {
  150. return NotFound, err
  151. }
  152. return r.resolveDataPointer(pointer)
  153. }
  154. func (r *Reader) cidr(ip net.IP, prefixLength int) *net.IPNet {
  155. // This is necessary as the node that the IPv4 start is at may
  156. // be at a bit depth that is less that 96, i.e., ipv4Start points
  157. // to a leaf node. For instance, if a record was inserted at ::/8,
  158. // the ipv4Start would point directly at the leaf node for the
  159. // record and would have a bit depth of 8. This would not happen
  160. // with databases currently distributed by MaxMind as all of them
  161. // have an IPv4 subtree that is greater than a single node.
  162. if r.Metadata.IPVersion == 6 &&
  163. len(ip) == net.IPv4len &&
  164. r.ipv4StartBitDepth != 96 {
  165. return &net.IPNet{IP: net.ParseIP("::"), Mask: net.CIDRMask(r.ipv4StartBitDepth, 128)}
  166. }
  167. mask := net.CIDRMask(prefixLength, len(ip)*8)
  168. return &net.IPNet{IP: ip.Mask(mask), Mask: mask}
  169. }
  170. // Decode the record at |offset| into |result|. The result value pointed to
  171. // must be a data value that corresponds to a record in the database. This may
  172. // include a struct representation of the data, a map capable of holding the
  173. // data or an empty interface{} value.
  174. //
  175. // If result is a pointer to a struct, the struct need not include a field
  176. // for every value that may be in the database. If a field is not present in
  177. // the structure, the decoder will not decode that field, reducing the time
  178. // required to decode the record.
  179. //
  180. // As a special case, a struct field of type uintptr will be used to capture
  181. // the offset of the value. Decode may later be used to extract the stored
  182. // value from the offset. MaxMind DBs are highly normalized: for example in
  183. // the City database, all records of the same country will reference a
  184. // single representative record for that country. This uintptr behavior allows
  185. // clients to leverage this normalization in their own sub-record caching.
  186. func (r *Reader) Decode(offset uintptr, result interface{}) error {
  187. if r.buffer == nil {
  188. return errors.New("cannot call Decode on a closed database")
  189. }
  190. return r.decode(offset, result)
  191. }
  192. func (r *Reader) decode(offset uintptr, result interface{}) error {
  193. rv := reflect.ValueOf(result)
  194. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  195. return errors.New("result param must be a pointer")
  196. }
  197. _, err := r.decoder.decode(uint(offset), rv, 0)
  198. return err
  199. }
  200. func (r *Reader) lookupPointer(ip net.IP) (uint, int, net.IP, error) {
  201. if ip == nil {
  202. return 0, 0, ip, errors.New("IP passed to Lookup cannot be nil")
  203. }
  204. ipV4Address := ip.To4()
  205. if ipV4Address != nil {
  206. ip = ipV4Address
  207. }
  208. if len(ip) == 16 && r.Metadata.IPVersion == 4 {
  209. return 0, 0, ip, fmt.Errorf("error looking up '%s': you attempted to look up an IPv6 address in an IPv4-only database", ip.String())
  210. }
  211. bitCount := uint(len(ip) * 8)
  212. var node uint
  213. if bitCount == 32 {
  214. node = r.ipv4Start
  215. }
  216. nodeCount := r.Metadata.NodeCount
  217. i := uint(0)
  218. for ; i < bitCount && node < nodeCount; i++ {
  219. bit := uint(1) & (uint(ip[i>>3]) >> (7 - (i % 8)))
  220. offset := node * r.nodeOffsetMult
  221. if bit == 0 {
  222. node = r.nodeReader.readLeft(offset)
  223. } else {
  224. node = r.nodeReader.readRight(offset)
  225. }
  226. }
  227. if node == nodeCount {
  228. // Record is empty
  229. return 0, int(i), ip, nil
  230. } else if node > nodeCount {
  231. return node, int(i), ip, nil
  232. }
  233. return 0, int(i), ip, newInvalidDatabaseError("invalid node in search tree")
  234. }
  235. func (r *Reader) retrieveData(pointer uint, result interface{}) error {
  236. offset, err := r.resolveDataPointer(pointer)
  237. if err != nil {
  238. return err
  239. }
  240. return r.decode(offset, result)
  241. }
  242. func (r *Reader) resolveDataPointer(pointer uint) (uintptr, error) {
  243. var resolved = uintptr(pointer - r.Metadata.NodeCount - dataSectionSeparatorSize)
  244. if resolved > uintptr(len(r.buffer)) {
  245. return 0, newInvalidDatabaseError("the MaxMind DB file's search tree is corrupt")
  246. }
  247. return resolved, nil
  248. }