-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqqwry_query.c
44 lines (33 loc) · 937 Bytes
/
qqwry_query.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
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include "qqwry.h"
static uint32_t ip_atoi(const char *ip_str) {
const char *p = ip_str;
uint32_t parts[4], i;
for (i=0; i<4; i++) {
parts[i] = atoi(p);
while (*p != '.' && *p) p++;
/* skip dot */
if (*p == '.') p++;
}
return parts[3] | parts[2] << 8 | parts[1] << 16 | parts[0] << 24;
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s ip_addr [qqwry.dat file]\n", argv[0]);
return 1;
}
char *path = argc < 3 ? "qqwry.dat" : argv[2];
uint32_t ip = ip_atoi(argv[1]);
qqwry_data_t data;
if (qqwry_init(path, &data) != 0) {
perror(path);
return 1;
}
qqwry_result_t result;
qqwry_query(data, ip, &result);
qqwry_clean(data);
printf("%s | %s\n", result.country, result.area);
return 0;
}