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/entities/ip2ccountryinfo.h"
29 
30 #include <QHash>
31 #include <QHostAddress>
32 #include <QList>
33 #include <QMutex>
34 #include <QPixmap>
35 #include <QProgressBar>
36 #include <QStatusBar>
37 #include <QString>
38 #include <QUrl>
39 
48 class IP2C : public QObject
49 {
50  Q_OBJECT
51 
52 public:
53  class IP2CData
54  {
55  public:
56  unsigned int ipStart;
57  unsigned int ipEnd;
58  QString country;
59  QString countryFullName;
60 
61  IP2CData()
62  {
63  ipStart = ipEnd = 0;
64  }
65 
66  bool isCountryKnown() const;
67 
72  bool isValid() const
73  {
74  return ipStart != ipEnd;
75  }
76  };
77 
78  static IP2C *instance();
79  static void deinstantiate();
80 
81  const QPixmap flagLan;
82  const QPixmap flagLocalhost;
83  const QPixmap flagUnknown;
84 
90  void appendEntryToDatabase(const IP2CData &entry);
91 
92  const QPixmap &flag(const QString &countryShortName);
93  bool hasData() const;
94 
95  bool isDataAccessLocked() const
96  {
97  return bDataAccessLocked;
98  }
99 
103  const IP2CData &lookupIP(unsigned int ipaddress);
104  const IP2CData &lookupIP(const QHostAddress &ipaddress)
105  {
106  return lookupIP(ipaddress.toIPv4Address());
107  }
108 
109  int numKnownEntries() const
110  {
111  return database.size();
112  }
113 
117  IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress);
118  IP2CCountryInfo obtainCountryInfo(const QHostAddress &ipaddress)
119  {
120  return obtainCountryInfo(ipaddress.toIPv4Address());
121  }
122 
123  void setDataAccessLockEnabled(bool b)
124  {
125  bDataAccessLocked = b;
126  }
127 
134  void setDatabase(const QList<IP2CData> &sortedCountryData)
135  {
136  QMutexLocker dataAccessMutexLocker(&dataAccessMutex);
137  database = sortedCountryData;
138  }
139 
140 protected:
141  inline bool isLANAddress(unsigned ipv4Address)
142  {
143  const static unsigned LAN_1_BEGIN = QHostAddress("10.0.0.0").toIPv4Address();
144  const static unsigned LAN_1_END = QHostAddress("10.255.255.255").toIPv4Address();
145  const static unsigned LAN_2_BEGIN = QHostAddress("172.16.0.0").toIPv4Address();
146  const static unsigned LAN_2_END = QHostAddress("172.31.255.255").toIPv4Address();
147  const static unsigned LAN_3_BEGIN = QHostAddress("192.168.0.0").toIPv4Address();
148  const static unsigned LAN_3_END = QHostAddress("192.168.255.255").toIPv4Address();
149 
150  return
151  (ipv4Address >= LAN_1_BEGIN && ipv4Address <= LAN_1_END)
152  || (ipv4Address >= LAN_2_BEGIN && ipv4Address <= LAN_2_END)
153  || (ipv4Address >= LAN_3_BEGIN && ipv4Address <= LAN_3_END)
154  ;
155  }
156 
157  inline bool isLocalhostAddress(unsigned ipv4Address)
158  {
159  const static unsigned LOCALHOST_BEGIN = QHostAddress("127.0.0.0").toIPv4Address();
160  const static unsigned LOCALHOST_END = QHostAddress("127.255.255.255").toIPv4Address();
161 
162  return ipv4Address >= LOCALHOST_BEGIN && ipv4Address <= LOCALHOST_END;
163  }
164 
165 private:
166  static QMutex instanceMutex;
167  static IP2C *staticInstance;
168 
176  bool bDataAccessLocked;
177  QMutex dataAccessMutex;
178  QList<IP2CData> database;
179  QHash<QString, QPixmap> flags;
180  const IP2CData invalidData;
181 
182  IP2C();
183  ~IP2C() override;
184 };
185 
186 #endif /* __IP2C_H__ */
void setDatabase(const QList< IP2CData > &sortedCountryData)
Sets database contents to the list specified.
Definition: ip2c.h:134
IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress)
Definition: ip2c.cpp:147
void appendEntryToDatabase(const IP2CData &entry)
Adds new country entry to the database.
Definition: ip2c.cpp:68
const IP2CData & lookupIP(unsigned int ipaddress)
Definition: ip2c.cpp:114
IP to Country database handler.
Definition: ip2c.h:48
Flag and name of the country.
bool isValid() const
Definition: ip2c.h:72