qBittorrent
geoipdatabase.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #pragma once
30 
31 #include <QCoreApplication>
32 #include <QtGlobal>
33 
34 class QByteArray;
35 class QDateTime;
36 class QHostAddress;
37 class QString;
38 
39 struct DataFieldDescriptor;
40 
42 {
43  Q_DECLARE_TR_FUNCTIONS(GeoIPDatabase)
44 
45 public:
46  static GeoIPDatabase *load(const QString &filename, QString &error);
47  static GeoIPDatabase *load(const QByteArray &data, QString &error);
48 
50 
51  QString type() const;
52  quint16 ipVersion() const;
53  QDateTime buildEpoch() const;
54  QString lookup(const QHostAddress &hostAddr) const;
55 
56 private:
57  explicit GeoIPDatabase(quint32 size);
58 
59  bool parseMetadata(const QVariantHash &metadata, QString &error);
60  bool loadDB(QString &error) const;
61  QVariantHash readMetadata() const;
62 
63  QVariant readDataField(quint32 &offset) const;
64  bool readDataFieldDescriptor(quint32 &offset, DataFieldDescriptor &out) const;
65  void fromBigEndian(uchar *buf, quint32 len) const;
66  QVariant readMapValue(quint32 &offset, quint32 count) const;
67  QVariant readArrayValue(quint32 &offset, quint32 count) const;
68 
69  template<typename T>
70  QVariant readPlainValue(quint32 &offset, quint8 len) const
71  {
72  T value = 0;
73  const uchar *const data = m_data + offset;
74  const quint32 availSize = m_size - offset;
75 
76  if ((len > 0) && (len <= sizeof(T) && (availSize >= len)))
77  {
78  // copy input data to last 'len' bytes of 'value'
79  uchar *dst = reinterpret_cast<uchar *>(&value) + (sizeof(T) - len);
80  memcpy(dst, data, len);
81  fromBigEndian(reinterpret_cast<uchar *>(&value), sizeof(T));
82  offset += len;
83  }
84 
85  return QVariant::fromValue(value);
86  }
87 
88  // Metadata
89  quint16 m_ipVersion;
90  quint16 m_recordSize;
91  quint32 m_nodeCount;
95  QDateTime m_buildEpoch;
96  QString m_dbType;
97  // Search data
98  mutable QHash<quint32, QString> m_countries;
99  quint32 m_size;
100  uchar *m_data;
101 };
bool loadDB(QString &error) const
quint16 ipVersion() const
void fromBigEndian(uchar *buf, quint32 len) const
QString m_dbType
Definition: geoipdatabase.h:96
quint16 m_recordSize
Definition: geoipdatabase.h:90
QVariant readArrayValue(quint32 &offset, quint32 count) const
QDateTime buildEpoch() const
QVariant readDataField(quint32 &offset) const
quint32 m_size
Definition: geoipdatabase.h:99
QVariant readPlainValue(quint32 &offset, quint8 len) const
Definition: geoipdatabase.h:70
bool readDataFieldDescriptor(quint32 &offset, DataFieldDescriptor &out) const
QString lookup(const QHostAddress &hostAddr) const
quint16 m_ipVersion
Definition: geoipdatabase.h:89
QHash< quint32, QString > m_countries
Definition: geoipdatabase.h:98
GeoIPDatabase(quint32 size)
QVariantHash readMetadata() const
static GeoIPDatabase * load(const QString &filename, QString &error)
QDateTime m_buildEpoch
Definition: geoipdatabase.h:95
bool parseMetadata(const QVariantHash &metadata, QString &error)
quint32 m_nodeCount
Definition: geoipdatabase.h:91
QString type() const
QVariant readMapValue(quint32 &offset, quint32 count) const
T value(const QString &key, const T &defaultValue={})
Definition: preferences.cpp:64