forked from Markus-Go/ip-countryside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip2country.cpp
131 lines (124 loc) · 4.01 KB
/
ip2country.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Copyright 2007 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
* or its licensors, as applicable.
* Copyright 2008-2015 Markus Goldstein
*
* You may not use this file except under the terms of the accompanying license.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project: ip-countryside
* File: ip2country.cpp
* Purpose: Country lookup demo client
* Responsible: Markus Goldstein
* Primary Repository: https://github.com/Markus-Go/ip-countryside/
* Web Sites: madm.dfki.de, www.goldiges.de/ip-countryside
*
*/
#include "ip2country.h"
int main(int argc, char* argv[]){
vConfigFile = readConfig( "config" );
char cBuf[LINE_LENGTH];
vector <string> sIP;
vector <DBEntries> vRanges;
vector <string> sEntry;
string sStr , sStr1, errorStr;
bool bMatch = false;
ifstream in ( (findValue("IpToCountry")).c_str() );
if (!in ) {
cout << "Database file " << (findValue("IpToCountry")).c_str() << " could not be found! \n";
return 1;
}
if ( argc < 2 ) {
cout << "Usage: " << argv[0] << " <query-ip>" << endl;
return 1;
}
sStr1 = argv[1];
errorStr = "Please enter a valid IP address.\n";
int iDotNum = 0, iDigitNum = 0;
for (uint i = 0; i < sStr1.length(); i++ ) {
if ( isdigit (sStr1[i]) ) {
iDigitNum++;
if ( iDigitNum > 3 ) {
cout << errorStr;
return 1;
}
}
else if ( sStr1[i] == '.' ) {
iDotNum++;
if ( iDigitNum == 0 || iDigitNum > 3 ) {
cout << errorStr;
return 1;
}
iDigitNum = 0;
if ( iDotNum == 3 ) {
if ( i == sStr1.length()-1 ) {
cout << errorStr;
return 1;
}
}
}
else if ( isalpha(sStr1[i]) ) {
cout << errorStr;
return 1;
}
}
if ( iDotNum != 3 ) {
cout << errorStr;
return 1;
}
sIP = split( sStr1 , '.' );
if (! ( (atoi(sIP[0].c_str()) >= 0 && atoi(sIP[0].c_str()) <= 255)) ||
( !(atoi(sIP[1].c_str()) >= 0 && atoi(sIP[1].c_str()) <= 255)) ||
( !(atoi(sIP[2].c_str()) >= 0 && atoi(sIP[2].c_str()) <= 255)) ||
( !(atoi(sIP[3].c_str()) >= 0 && atoi(sIP[3].c_str()) <= 255)) ){
cout << errorStr;
return 1;
}
stringstream sstream;
while ( !(in.getline(cBuf,LINE_LENGTH).eof()) ) {
sEntry.clear();
sStr = cBuf;
if( !sStr.empty() ) {
sEntry = split( sStr , ' ' );
DBEntries *sRange = new DBEntries();
sstream.clear();
sstream << sEntry[0];
sstream >> sRange->ipFrom;
sstream.clear();
sstream << sEntry[1];
sstream >> sRange->ipTo;
sRange->country = sEntry[2];
vRanges.push_back( *sRange );
}
}
unsigned int ip = 0;
ip = convertToInt( sIP );
int lowerb = 0 , upperb = vRanges.size()-1,mid = 0;
while ( lowerb <= upperb ) {
mid = ( lowerb + upperb ) / 2;
if(vRanges[mid].ipFrom <= ip && vRanges[mid].ipTo >= ip ) {
cout << convertIP(vRanges[mid].ipFrom) << "-" << convertIP(vRanges[mid].ipTo)
<< "-" << vRanges[mid].country << endl;
cout<< convertIP(ip) << " " << vRanges[mid].country << endl;
bMatch = true;
break;
}
else if(vRanges[mid].ipTo < ip) {
lowerb = mid + 1;
}
else if( vRanges[mid].ipFrom > ip ) {
upperb = mid - 1;
}
}
if (! bMatch) {
cout << convertIP(ip) << " " << "Unknown" << endl;
}
bMatch = false;
in.close();
return 0;
}