| 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 |
#include "net/util/ip2location.h" |
|---|
| 33 |
#include "net/util/third-party/IP2Location.h" |
|---|
| 34 |
|
|---|
| 35 |
namespace net { |
|---|
| 36 |
|
|---|
| 37 |
Ip2Location::Ip2Location(const char* db_file) |
|---|
| 38 |
: iploc_(NULL) { |
|---|
| 39 |
iploc_ = IP2Location_open(db_file); |
|---|
| 40 |
CHECK(iploc_ != NULL) |
|---|
| 41 |
<< " Cannot initialize IP2Location structure from file: " << db_file; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
Ip2Location::~Ip2Location() { |
|---|
| 45 |
CHECK(iploc_ != NULL); |
|---|
| 46 |
const uint32 err = IP2Location_close(iploc_); |
|---|
| 47 |
LOG_INFO << " Closed IP2Location structure. Error code: " << err; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
#define DEFINE_LOOKUP_FUNCTION(member) \ |
|---|
| 51 |
IP2LocationRecord* const ret = IP2Location_get_##member(iploc_, addr); \ |
|---|
| 52 |
if ( ret == NULL ) { \ |
|---|
| 53 |
LOG_ERROR << " Error looking up IP: " << addr; \ |
|---|
| 54 |
return string(""); \ |
|---|
| 55 |
} \ |
|---|
| 56 |
const string ret_str(ret->member); \ |
|---|
| 57 |
IP2Location_free_record(ret); \ |
|---|
| 58 |
return ret_str |
|---|
| 59 |
|
|---|
| 60 |
string Ip2Location::LookupCountry(const char* addr) { |
|---|
| 61 |
DEFINE_LOOKUP_FUNCTION(country_short); |
|---|
| 62 |
} |
|---|
| 63 |
string Ip2Location::LookupCountryLong(const char* addr) { |
|---|
| 64 |
DEFINE_LOOKUP_FUNCTION(country_long); |
|---|
| 65 |
} |
|---|
| 66 |
string Ip2Location::LookupRegion(const char* addr) { |
|---|
| 67 |
DEFINE_LOOKUP_FUNCTION(region); |
|---|
| 68 |
} |
|---|
| 69 |
string Ip2Location::LookupCity(const char* addr) { |
|---|
| 70 |
DEFINE_LOOKUP_FUNCTION(city); |
|---|
| 71 |
} |
|---|
| 72 |
string Ip2Location::LookupIsp(const char* addr) { |
|---|
| 73 |
DEFINE_LOOKUP_FUNCTION(isp); |
|---|
| 74 |
} |
|---|
| 75 |
#undef DEFINE_LOOKUP_FUNCTION |
|---|
| 76 |
|
|---|
| 77 |
bool Ip2Location::LookupInternal(const char* addr, Record* record) { |
|---|
| 78 |
IP2LocationRecord* const ret = IP2Location_get_all(iploc_, addr); |
|---|
| 79 |
if ( ret == NULL ) { |
|---|
| 80 |
LOG_ERROR << " Error looking up IP: " << addr; |
|---|
| 81 |
return false; |
|---|
| 82 |
} |
|---|
| 83 |
record->country_short_ = ret->country_short; |
|---|
| 84 |
record->country_long_ = ret->country_long; |
|---|
| 85 |
record->region_ = ret->region; |
|---|
| 86 |
record->city_ = ret->city; |
|---|
| 87 |
record->isp_ = ret->isp; |
|---|
| 88 |
|
|---|
| 89 |
if ( cache_.size() > kMaxCacheSize ) { |
|---|
| 90 |
delete cache_.begin()->second; |
|---|
| 91 |
cache_.erase(cache_.begin()); |
|---|
| 92 |
} |
|---|
| 93 |
cache_.insert(make_pair(IpAddress(addr).ipv4(), new Record(*record))); |
|---|
| 94 |
|
|---|
| 95 |
return true; |
|---|
| 96 |
} |
|---|
| 97 |
} |
|---|