-
Notifications
You must be signed in to change notification settings - Fork 3
/
binding.cc
198 lines (156 loc) · 4.75 KB
/
binding.cc
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <math.h>
#include <string.h>
#include <node.h>
#include <nan.h>
#include "sc/dict_int_chr.h" //https://github.com/S-YOU/sc
#include "data.h"
namespace {
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::String;
using v8::Number;
using v8::Value;
using v8::Handle;
unsigned char *direct; //first 24 bit direct mapping, cover 75% ranges
DictIntChr map; //remaining ranges as hashtable
static inline const char *_lookup(unsigned int ip) {
if (ip < 0x01000000 || ip >= 0xe0000000) return countries[0];
unsigned char range_ip = direct[ip >> 8];
if (range_ip < 255) return countries[range_ip]; //75% match here
//search between bit 32 down to 25, and lookup in hashtable each
DictIntChrItem e;
unsigned mask = 0xffffffff;
while (mask > 0xffffff00) {
e = DictIntChrSearch(map, ip);
if (e) {
return countries[e->val];
}
mask <<= 1;
ip &= mask;
}
return countries[0];
}
static inline unsigned char _lookupn(unsigned int ip) {
if (ip < 0x01000000 || ip >= 0xe0000000) return 0;
unsigned char range_ip = direct[ip >> 8];
if (range_ip < 255) return range_ip; //75% match here
//search between bit 32 down to 25, and lookup in hashtable each
DictIntChrItem e;
unsigned mask = 0xffffffff;
while (mask > 0xffffff00) {
e = DictIntChrSearch(map, ip);
if (e) {
return e->val;
}
mask <<= 1;
ip &= mask;
}
return 0;
}
#define ATOI(BUF, BUF_END, NUM) \
NUM = 0, mul = 1, sTmp = BUF_END - 1;\
while (sTmp >= BUF) {\
NUM += (*sTmp - '0') * mul;\
mul *= 10;\
sTmp--;\
}
static inline unsigned _pton(char *a) {
unsigned mul, num, ret; char *s = a, *sTmp;
while (*s >= '0' && *s <= '9') s++;
if (s <= a || *s != '.') return 0;
ATOI(a, s, num); ret = num << 24; s++; a = s;
while (*s >= '0' && *s <= '9') s++;
if (s <= a || *s != '.') return 0;
ATOI(a, s, num); ret += num << 16; s++; a = s;
while (*s >= '0' && *s <= '9') s++;
if (s <= a || *s != '.') return 0;
ATOI(a, s, num); ret += num << 8; s++; a = s;
while (*s >= '0' && *s <= '9') s++;
if (s <= a) return 0;
ATOI(a, s, num); ret += num;
return ret;
}
void pton(const Nan::FunctionCallbackInfo<v8::Value>& args) {
String::Utf8Value str(args[0]->ToObject());
char *ip = *str;
const char* err_msg = nullptr;
if (args.Length() != 1) {
err_msg = "Wrong number of arguments: expected 1";
goto err;
}
if (!args[0]->IsString()) {
err_msg = "Input must be String";
goto err;
}
return args.GetReturnValue().Set(Nan::New<Number>(_pton(ip)));
err:
Nan::ThrowError(Nan::New<String>(err_msg).ToLocalChecked());
}
void lookup(const Nan::FunctionCallbackInfo<v8::Value>& args) {
unsigned ip = 0;
const char* err_msg = nullptr;
if (args.Length() != 1) {
err_msg = "Wrong number of arguments: expected 1";
goto err;
}
if (args[0]->IsNumber()) {
ip = args[0]->Uint32Value();
return args.GetReturnValue().Set(Nan::New<String>(_lookup(ip)).ToLocalChecked());
} else if (args[0]->IsString()) {
String::Utf8Value str(args[0]->ToObject());
char *ip = *str;
return args.GetReturnValue().Set(Nan::New<String>(_lookup(_pton(ip))).ToLocalChecked());
} else {
err_msg = "Input must be Number or String";
goto err;
}
err:
Nan::ThrowError(Nan::New<String>(err_msg).ToLocalChecked());
}
void lookupn(const Nan::FunctionCallbackInfo<v8::Value>& args) {
unsigned ip = 0;
const char* err_msg = nullptr;
if (args.Length() != 1) {
err_msg = "Wrong number of arguments: expected 1";
goto err;
}
if (args[0]->IsNumber()) {
ip = args[0]->Uint32Value();
return args.GetReturnValue().Set(Nan::New<Number>(_lookupn(ip)));
} else if (args[0]->IsString()) {
String::Utf8Value str(args[0]->ToObject());
char *ip = *str;
return args.GetReturnValue().Set(Nan::New<Number>(_lookupn(_pton(ip))));
} else {
err_msg = "Input must be Number or String";
goto err;
}
err:
Nan::ThrowError(Nan::New<String>(err_msg).ToLocalChecked());
}
NAN_MODULE_INIT(Init) {
unsigned i;
direct = (unsigned char *) calloc(0xe00000, 1);
if (DictIntChrInit(&map, 0x100000, 1)) return;
for (i = 0; i < 0x98a7; i++) {
direct[r1[i][0]] = r1[i][1];
}
for (i = 0; i < 0x19817; i++) {
memset(direct + rx[i][0], rx[i][1], rx[i][2]);
}
for (i = 0; i < 0xd042; i++) {
DictIntChrInsert(map, maps[i][0], maps[i][1]);
}
v8::Local<v8::Function> ptonFn = Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(pton)).ToLocalChecked();
v8::Local<v8::Function> lookupFn = Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(lookup)).ToLocalChecked();
v8::Local<v8::Function> lookupnFn = Nan::GetFunction(
Nan::New<v8::FunctionTemplate>(lookupn)).ToLocalChecked();
Nan::Set(target, Nan::New("pton").ToLocalChecked(), ptonFn);
Nan::Set(target, Nan::New("lookup").ToLocalChecked(), lookupFn);
Nan::Set(target, Nan::New("lookupn").ToLocalChecked(), lookupnFn);
}
NODE_MODULE(addon, Init)
}