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 "ip2c/entities/ip2ccountryinfo.h"
28 #include "global.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) { return lookupIP(ipaddress.toIPv4Address()); }
105 
106  int numKnownEntries() const { return database.size(); }
107 
111  IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress);
112  IP2CCountryInfo obtainCountryInfo(const QHostAddress& ipaddress) { return obtainCountryInfo(ipaddress.toIPv4Address()); }
113 
114  void setDataAccessLockEnabled(bool b) { bDataAccessLocked = b; }
115 
122  void setDatabase(const QList<IP2CData>& sortedCountryData)
123  {
124  QMutexLocker dataAccessMutexLocker(&dataAccessMutex);
125  database = sortedCountryData;
126  }
127 
128  protected:
129  inline bool isLANAddress(unsigned ipv4Address)
130  {
131  const static unsigned LAN_1_BEGIN = QHostAddress("10.0.0.0").toIPv4Address();
132  const static unsigned LAN_1_END = QHostAddress("10.255.255.255").toIPv4Address();
133  const static unsigned LAN_2_BEGIN = QHostAddress("172.16.0.0").toIPv4Address();
134  const static unsigned LAN_2_END = QHostAddress("172.31.255.255").toIPv4Address();
135  const static unsigned LAN_3_BEGIN = QHostAddress("192.168.0.0").toIPv4Address();
136  const static unsigned LAN_3_END = QHostAddress("192.168.255.255").toIPv4Address();
137 
138  return (
139  (ipv4Address >= LAN_1_BEGIN && ipv4Address <= LAN_1_END)
140  || (ipv4Address >= LAN_2_BEGIN && ipv4Address <= LAN_2_END)
141  || (ipv4Address >= LAN_3_BEGIN && ipv4Address <= LAN_3_END)
142  );
143  }
144 
145  inline bool isLocalhostAddress(unsigned ipv4Address)
146  {
147  const static unsigned LOCALHOST_BEGIN = QHostAddress("127.0.0.0").toIPv4Address();
148  const static unsigned LOCALHOST_END = QHostAddress("127.255.255.255").toIPv4Address();
149 
150  return (ipv4Address >= LOCALHOST_BEGIN && ipv4Address <= LOCALHOST_END);
151  }
152 
153  private:
154  static QMutex instanceMutex;
155  static IP2C *staticInstance;
156 
164  bool bDataAccessLocked;
165  QMutex dataAccessMutex;
166  QList<IP2CData> database;
167  QHash<QString, QPixmap> flags;
168  const IP2CData invalidData;
169 
170  IP2C();
171  ~IP2C();
172 };
173 
174 #endif /* __IP2C_H__ */
void setDatabase(const QList< IP2CData > &sortedCountryData)
Sets database contents to the list specified.
Definition: ip2c.h:122
IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress)
Definition: ip2c.cpp:157
void appendEntryToDatabase(const IP2CData &entry)
Adds new country entry to the database.
Definition: ip2c.cpp:70
const IP2CData & lookupIP(unsigned int ipaddress)
Definition: ip2c.cpp:122
IP to Country database handler.
Definition: ip2c.h:48
Flag and name of the country.
bool isValid() const
Definition: ip2c.h:72