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 
85  const QPixmap &flag(const QString &countryShortName);
86  bool hasData() const;
87 
88  bool isDataAccessLocked() const
89  {
90  return bDataAccessLocked;
91  }
92 
96  const IP2CData &lookupIP(unsigned int ipaddress);
97  const IP2CData &lookupIP(const QHostAddress &ipaddress)
98  {
99  return lookupIP(ipaddress.toIPv4Address());
100  }
101 
102  int numKnownEntries() const
103  {
104  return database.size();
105  }
106 
110  IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress);
111  IP2CCountryInfo obtainCountryInfo(const QHostAddress &ipaddress)
112  {
113  return obtainCountryInfo(ipaddress.toIPv4Address());
114  }
115 
116  void setDataAccessLockEnabled(bool b)
117  {
118  bDataAccessLocked = b;
119  }
120 
127  void setDatabase(const QList<IP2CData> &sortedCountryData)
128  {
129  database = sortedCountryData;
130  }
131 
132 protected:
133  inline bool isLANAddress(unsigned ipv4Address)
134  {
135  const static unsigned LAN_1_BEGIN = QHostAddress("10.0.0.0").toIPv4Address();
136  const static unsigned LAN_1_END = QHostAddress("10.255.255.255").toIPv4Address();
137  const static unsigned LAN_2_BEGIN = QHostAddress("172.16.0.0").toIPv4Address();
138  const static unsigned LAN_2_END = QHostAddress("172.31.255.255").toIPv4Address();
139  const static unsigned LAN_3_BEGIN = QHostAddress("192.168.0.0").toIPv4Address();
140  const static unsigned LAN_3_END = QHostAddress("192.168.255.255").toIPv4Address();
141 
142  return
143  (ipv4Address >= LAN_1_BEGIN && ipv4Address <= LAN_1_END)
144  || (ipv4Address >= LAN_2_BEGIN && ipv4Address <= LAN_2_END)
145  || (ipv4Address >= LAN_3_BEGIN && ipv4Address <= LAN_3_END)
146  ;
147  }
148 
149  inline bool isLocalhostAddress(unsigned ipv4Address)
150  {
151  const static unsigned LOCALHOST_BEGIN = QHostAddress("127.0.0.0").toIPv4Address();
152  const static unsigned LOCALHOST_END = QHostAddress("127.255.255.255").toIPv4Address();
153 
154  return ipv4Address >= LOCALHOST_BEGIN && ipv4Address <= LOCALHOST_END;
155  }
156 
157 private:
158  static QMutex instanceMutex;
159  static IP2C *staticInstance;
160 
168  bool bDataAccessLocked;
169  QList<IP2CData> database;
170  QHash<QString, QPixmap> flags;
171  const IP2CData invalidData;
172 
173  IP2C();
174  ~IP2C() override;
175 };
176 
177 #endif /* __IP2C_H__ */