-
Notifications
You must be signed in to change notification settings - Fork 13
/
flux.c
129 lines (108 loc) · 3.19 KB
/
flux.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
/* domainanalyzer, by udi shamir
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <GeoIP.h>
#include "libdoma.h"
int get_flux(const char * host, struct flux_entry ** results)
{
struct addrinfo hints, *res, *cur;
int errcode;
int count;
uint32_t isflux=0;
char * addr_str=NULL;
void * addr_ptr;
GeoIP * gi;
*results = NULL;
memset (&hints, 0, sizeof (hints));
// memset (&addr_str, 0, sizeof(addr_str));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
errcode = getaddrinfo (host, NULL, &hints, &res);
if (errcode < 0)
{
return -1;
}
/* count hints and allocate return buffer
*/
for (count = 1, cur = res; cur; cur = cur->ai_next, ++count)
;
if (count == 1)
{
/* no entries found, so just fail here
*/
return -1;
}
*results = calloc(count, sizeof(struct flux_entry));
if (!*results)
{
return -1;
}
// memset(*results, 0, sizeof(struct flux_entry) * count);
// init GeoIP //
gi = GeoIP_new(GEOIP_STANDARD);
for (isflux = 0, cur = res; cur; cur = cur ->ai_next, ++isflux)
{
int size;
switch (cur->ai_family)
{
case AF_INET:
addr_ptr = &((struct sockaddr_in *) cur->ai_addr)->sin_addr;
size = sizeof ("xxx.xxx.xxx.xxx");
break;
case AF_INET6:
addr_ptr = &((struct sockaddr_in6 *) cur->ai_addr)->sin6_addr;
size = sizeof ("xx:xx:xx:xx:xx:xx:xx:xx");
break;
default:
continue;
}
addr_str = malloc(size);
if (!addr_str) continue;
inet_ntop (cur->ai_family, addr_ptr, addr_str, size);
(*results)[isflux].addr_str = addr_str;
if((GeoIP_country_code_by_name(gi, addr_str)) == NULL)
{
break;
}
else
{
memcpy( &(*results)[isflux].cc,
GeoIP_country_code_by_name(gi, addr_str),
sizeof (**results).cc);
}
}
(*results)[isflux].addr_str = NULL;
(*results)[isflux].cc[0] = 0;
(*results)[isflux].cc[1] = 0;
GeoIP_delete(gi);
return isflux;
}
void release_flux(struct flux_entry * flux)
{
if (NULL == flux)
return;
if (NULL != flux->addr_str)
{
free(flux->addr_str);
flux->addr_str = NULL;
}
free(flux);
}