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 
118  void setRangesDatabase(const QList<IP2CRange> &sortedRanges)
119  {
120  this->database = sortedRanges;
121  }
122 
123 private:
124  static QMutex instanceMutex;
125  static IP2C *staticInstance;
126 
134  bool bDataAccessLocked;
135  QList<IP2CRange> database;
136  QHash<QString, IP2CCountry> countries;
137 
138  // Caches all flags. It's important to not touch the flags structure
139  // anymore after it's been created as the program will refer to the flags
140  // by pointer.
141  const QHash<QString, QPixmap> flags;
142 
143  const IP2CRange invalidRange;
144 
145  QSet<QString> unknownCountries;
146  QSet<QString> unknownFlags;
147 
148  IP2C();
149  ~IP2C() override;
150  Q_DISABLE_COPY(IP2C);
151 
155  const IP2CRange &lookupIP(unsigned int ipaddress);
156  IP2CCountry unknownCountry() const;
157 
158  void logUnknownCountry(const QString &countryCode);
159  void logUnknownFlag(const QString &countryCode);
160 
161  inline bool isLANAddress(unsigned ipv4Address)
162  {
163  const static unsigned LAN_1_BEGIN = QHostAddress("10.0.0.0").toIPv4Address();
164  const static unsigned LAN_1_END = QHostAddress("10.255.255.255").toIPv4Address();
165  const static unsigned LAN_2_BEGIN = QHostAddress("172.16.0.0").toIPv4Address();
166  const static unsigned LAN_2_END = QHostAddress("172.31.255.255").toIPv4Address();
167  const static unsigned LAN_3_BEGIN = QHostAddress("192.168.0.0").toIPv4Address();
168  const static unsigned LAN_3_END = QHostAddress("192.168.255.255").toIPv4Address();
169 
170  return
171  (ipv4Address >= LAN_1_BEGIN && ipv4Address <= LAN_1_END)
172  || (ipv4Address >= LAN_2_BEGIN && ipv4Address <= LAN_2_END)
173  || (ipv4Address >= LAN_3_BEGIN && ipv4Address <= LAN_3_END)
174  ;
175  }
176 
177  inline bool isLocalhostAddress(unsigned ipv4Address)
178  {
179  const static unsigned LOCALHOST_BEGIN = QHostAddress("127.0.0.0").toIPv4Address();
180  const static unsigned LOCALHOST_END = QHostAddress("127.255.255.255").toIPv4Address();
181 
182  return ipv4Address >= LOCALHOST_BEGIN && ipv4Address <= LOCALHOST_END;
183  }
184 };
185 
186 #endif /* __IP2C_H__ */