-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRMaxmind.c
182 lines (129 loc) · 5.48 KB
/
RMaxmind.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright (c) ICANS GmbH and individual contributors.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of ICANS nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <R.h>
#include <Rdefines.h>
#include "GeoIP-1.4.6/libGeoIP/GeoIP.h"
#include "GeoIP-1.4.6/libGeoIP/GeoIPCity.h"
static const char * _mk_NA( const char * p ){
return p ? p : "N/A";
}
/* returns character vectors of geolocated countries based on IP addresses
in long integer format */
SEXP fetchMaxmindCountryName(SEXP ipVect) {
unsigned int vectLen;
vectLen = length(ipVect);
SEXP countryStrs;
PROTECT(countryStrs = allocVector(STRSXP, vectLen));
/* Lookup Maxmind GeoIP */
const char *str = NULL;
GeoIP *gi = NULL;
/* change the path of the GeoIP.dat file, if necessary */
gi = GeoIP_open("GeoIP.dat", 0);
if (gi == NULL) {
Rprintf("RMaxmind GeoIP Country - ERROR: Could not open GeoIP.dat!\n");
} else {
for (unsigned int i = 0; i < vectLen; i++) {
str = GeoIP_country_name_by_ipnum(gi, (unsigned long)REAL(ipVect)[i]);
SET_STRING_ELT(countryStrs, i, mkChar(_mk_NA(str)));
}
}
GeoIP_delete(gi);
UNPROTECT(1);
return countryStrs;
}
/* returns a list of vectors with geolocated informations (name of country,
country code, region, city, timezone, latitude, and longitude) based on
IP addresses in long integer format. */
SEXP fetchMaxmindGeoDataName(SEXP ipVect) {
const char *time_zone = NULL;
unsigned int vectLen;
vectLen = length(ipVect);
char *names[7] = {"country_name", "country_short", "region", "city", "timezone", "latitude", "longitude"};
SEXP list, list_names;
double *p_latitude, *p_longitude;
SEXP latitudeVect;
SEXP longitudeVect;
SEXP countryStrs;
SEXP countryCodes;
SEXP regionStrs;
SEXP cityStrs;
SEXP timeZoneStrs;
PROTECT(countryStrs = allocVector(STRSXP, vectLen));
PROTECT(countryCodes = allocVector(STRSXP, vectLen));
PROTECT(regionStrs = allocVector(STRSXP, vectLen));
PROTECT(cityStrs = allocVector(STRSXP, vectLen));
PROTECT(timeZoneStrs = allocVector(STRSXP, vectLen));
PROTECT(latitudeVect = NEW_NUMERIC(vectLen));
PROTECT(longitudeVect = NEW_NUMERIC(vectLen));
p_latitude = NUMERIC_POINTER(latitudeVect);
p_longitude = NUMERIC_POINTER(longitudeVect);
/* set list names */
PROTECT(list_names = allocVector(STRSXP,7));
for (unsigned int i = 0; i < 7; i++)
SET_STRING_ELT(list_names, i, mkChar(names[i]));
/* create list*/
PROTECT(list = allocVector(VECSXP, 7));
/* Lookup Maxmind GeoIP */
GeoIPRecord *gir = NULL;
GeoIP *gi = NULL;
gi = GeoIP_open("GeoIPCity.dat", 0);
if (gi == NULL) {
Rprintf("RMaxmind GeoIP Data - ERROR: Could not open GeoIPCity.dat!\n");
} else {
for (unsigned int i = 0; i < vectLen; i++) {
gir = GeoIP_record_by_ipnum(gi, (unsigned long)REAL(ipVect)[i]);
if (gir != NULL) {
time_zone = GeoIP_time_zone_by_country_and_region(gir->country_code, gir->region);
SET_STRING_ELT(countryStrs, i, mkChar(_mk_NA(gir->country_name)));
SET_STRING_ELT(countryCodes, i, mkChar(_mk_NA(gir->country_code)));
SET_STRING_ELT(regionStrs, i, mkChar(_mk_NA(GeoIP_region_name_by_code(gir->country_code, gir->region))));
SET_STRING_ELT(cityStrs, i, mkChar(_mk_NA(gir->city)));
SET_STRING_ELT(timeZoneStrs, i, mkChar(_mk_NA(time_zone)));
p_latitude[i] = gir->latitude;
p_longitude[i] = gir->longitude;
GeoIPRecord_delete(gir);
} else {
SET_STRING_ELT(countryStrs, i, mkChar(_mk_NA(NULL)));
SET_STRING_ELT(countryCodes, i, mkChar(_mk_NA(NULL)));
SET_STRING_ELT(regionStrs, i, mkChar(_mk_NA(NULL)));
SET_STRING_ELT(cityStrs, i, mkChar(_mk_NA(NULL)));
SET_STRING_ELT(timeZoneStrs, i, mkChar(_mk_NA(NULL)));
}
}
/* attach vectors to list */
SET_VECTOR_ELT(list, 0, countryStrs);
SET_VECTOR_ELT(list, 1, countryCodes);
SET_VECTOR_ELT(list, 2, regionStrs);
SET_VECTOR_ELT(list, 3, cityStrs);
SET_VECTOR_ELT(list, 4, timeZoneStrs);
SET_VECTOR_ELT(list, 5, latitudeVect);
SET_VECTOR_ELT(list, 6, longitudeVect);
/* attach names to list */
setAttrib(list, R_NamesSymbol, list_names);
}
GeoIP_delete(gi);
UNPROTECT(9);
return list;
}