root/trunk/whisperlib/net/util/ip2location.h

Revision 7, 4.0 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31 //
32 // One layer over ip2location library ..
33 //
34 #ifndef __NET_UTIL_IP2LOCATION_H__
35 #define __NET_UTIL_IP2LOCATION_H__
36
37 #include <whisperlib/common/base/types.h>
38 #include WHISPER_HASH_MAP_HEADER
39 #include <whisperlib/net/base/address.h>
40
41 struct IP2Location;
42 namespace net {
43
44 class Ip2Location {
45  public:
46   Ip2Location(const char* db_file);
47   ~Ip2Location();
48
49   string LookupCountry(const char* addr);
50   string LookupCountryLong(const char* addr);
51   string LookupRegion(const char* addr);
52   string LookupCity(const char* addr);
53   string LookupIsp(const char* addr);
54
55   string LookupCountry(const IpAddress& addr) {
56     return LookupCountry(addr.ToString().c_str());
57   }
58   string LookupCountryLong(const IpAddress& addr) {
59     return LookupCountryLong(addr.ToString().c_str());
60   }
61   string LookupRegion(const IpAddress& addr) {
62     return LookupRegion(addr.ToString().c_str());
63   }
64   string LookupCity(const IpAddress& addr) {
65     return LookupCity(addr.ToString().c_str());
66   }
67   string LookupIsp(const IpAddress& addr) {
68     return LookupIsp(addr.ToString().c_str());
69   }
70
71   struct Record {
72     string country_short_;
73     string country_long_;
74     string region_;
75     string city_;
76     string isp_;
77     Record() {
78     }
79     Record(const Record& r) {
80       *this = r;
81     }
82     const Record& operator=(const Record& r) {
83       country_short_ = r.country_short_;
84       country_long_ = r.country_long_;
85       region_ = r.region_;
86       city_ = r.city_;
87       isp_ = r.isp_;
88       return *this;
89     }
90   };
91   bool LookupAll(const IpAddress& addr, Record* record) {
92     if ( !addr.is_ipv4() ) return false;
93     const CacheMap::const_iterator it = cache_.find(addr.ipv4());
94     if ( it != cache_.end() ) {
95       *record = *it->second;
96       return true;
97     }
98     return LookupInternal(addr.ToString().c_str(), record);
99   }
100   bool LookupAll(const char* addr, Record* record) {
101     net::IpAddress ip_ob(addr);
102     if ( !ip_ob.is_ipv4() ) {
103       return false;
104     }
105     const int32 ip = ip_ob.ipv4();
106     const CacheMap::const_iterator it = cache_.find(ip);
107     if ( it != cache_.end() ) {
108       *record = *it->second;
109       return true;
110     }
111     return LookupInternal(addr, record);
112   }
113
114  private:
115   bool LookupInternal(const char* addr, Record* record);
116
117   IP2Location* iploc_;
118   // A cache of last lookups
119   typedef hash_map<int32, Record*> CacheMap;
120   CacheMap cache_;
121   static const int kMaxCacheSize = 1000;
122   DISALLOW_EVIL_CONSTRUCTORS(Ip2Location);
123 };
124 }
125
126 #endif  // __NET_UTIL_IP2LOCATION_H__
Note: See TracBrowser for help on using the browser.