ip2c.h
1 //------------------------------------------------------------------------------
2 // ip2c.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #ifndef __IP2C_H__
25 #define __IP2C_H__
26 
27 #include "global.h"
28 #include "ip2c/ip2ccountry.h"
29 #include "ip2c/ip2crange.h"
30 
31 #include <QHash>
32 #include <QHostAddress>
33 #include <QList>
34 #include <QMutex>
35 #include <QPixmap>
36 #include <QString>
37 
38 class Server;
39 
48 class IP2C : public QObject
49 {
50  Q_OBJECT
51 
52 public:
56  enum Lookup
57  {
64  };
65 
66  static IP2C *instance();
67  static void deinstantiate();
68 
73  static Lookup howLookup(const QString &countryCode);
74 
75  const QPixmap flagLan;
76  const QPixmap flagLocalhost;
77  const QPixmap flagUnknown;
78 
79  const QPixmap &flag(const QString &countryCode);
80  bool hasData() const;
81 
82  bool isDataAccessLocked() const
83  {
84  return bDataAccessLocked;
85  }
86 
87  int numKnownEntries() const
88  {
89  return database.size();
90  }
91 
95  IP2CCountry countryInfo(const QString &countryCode);
96 
100  IP2CCountry countryInfoForIPv4(unsigned int ipaddress);
101  IP2CCountry countryInfoForAddress(const QHostAddress &ipaddress)
102  {
103  return countryInfoForIPv4(ipaddress.toIPv4Address());
104  }
105  IP2CCountry countryInfoForServer(const Server &server);
106 
107  void setDataAccessLockEnabled(bool b)
108  {
109  bDataAccessLocked = b;
110  }
111 
112  const QMap<QString, QString> &licenceNotice() const {return licences;}
113  const QString &urlDatabase() const {return databaseUrl;}
114 
121  void setRangesDatabase(const QList<IP2CRange> &sortedRanges)
122  {
123  this->database = sortedRanges;
124  }
125 
126  void setLicence(const QMap<QString, QString> &licences)
127  {
128  this->licences = licences;
129  }
130 
131  void setUrl(const QString &url)
132  {
133  this->databaseUrl = url;
134  }
135 
136 private:
137  static QMutex instanceMutex;
138  static IP2C *staticInstance;
139 
147  bool bDataAccessLocked;
148  QList<IP2CRange> database;
149  QHash<QString, IP2CCountry> countries;
150 
151  // Caches all flags. It's important to not touch the flags structure
152  // anymore after it's been created as the program will refer to the flags
153  // by pointer.
154  const QHash<QString, QPixmap> flags;
155 
156  const IP2CRange invalidRange;
157 
158  QSet<QString> unknownCountries;
159  QSet<QString> unknownFlags;
160 
161  QMap<QString, QString> licences;
162  QString databaseUrl;
163 
164  IP2C();
165  ~IP2C() override;
166  Q_DISABLE_COPY(IP2C);
167 
171  const IP2CRange &lookupIP(unsigned int ipaddress);
172  IP2CCountry unknownCountry() const;
173 
174  void logUnknownCountry(const QString &countryCode);
175  void logUnknownFlag(const QString &countryCode);
176 
177  inline bool isLANAddress(unsigned ipv4Address)
178  {
179  const static unsigned LAN_1_BEGIN = QHostAddress("10.0.0.0").toIPv4Address();
180  const static unsigned LAN_1_END = QHostAddress("10.255.255.255").toIPv4Address();
181  const static unsigned LAN_2_BEGIN = QHostAddress("172.16.0.0").toIPv4Address();
182  const static unsigned LAN_2_END = QHostAddress("172.31.255.255").toIPv4Address();
183  const static unsigned LAN_3_BEGIN = QHostAddress("192.168.0.0").toIPv4Address();
184  const static unsigned LAN_3_END = QHostAddress("192.168.255.255").toIPv4Address();
185 
186  return
187  (ipv4Address >= LAN_1_BEGIN && ipv4Address <= LAN_1_END)
188  || (ipv4Address >= LAN_2_BEGIN && ipv4Address <= LAN_2_END)
189  || (ipv4Address >= LAN_3_BEGIN && ipv4Address <= LAN_3_END)
190  ;
191  }
192 
193  inline bool isLocalhostAddress(unsigned ipv4Address)
194  {
195  const static unsigned LOCALHOST_BEGIN = QHostAddress("127.0.0.0").toIPv4Address();
196  const static unsigned LOCALHOST_END = QHostAddress("127.255.255.255").toIPv4Address();
197 
198  return ipv4Address >= LOCALHOST_BEGIN && ipv4Address <= LOCALHOST_END;
199  }
200 };
201 
202 #endif /* __IP2C_H__ */