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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#include<string.h>
#include<stdio.h>
#include<assert.h>
#include<ctype.h>
#include<time.h>
#include<errno.h>
#include<net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "mrl_utils.h"
#include "mrl_main.h"
extern struct mrl_global_instance mrl_instance;
int mrl_get_pkt_tuple(const char *raw_packet, struct mrl_tuple *five_tuple)
{
struct tcphdr *tcp_hdr = NULL;
struct udphdr *udp_hdr = NULL;
struct iphdr *ip_hdr = (struct iphdr*)raw_packet;
int iphdr_len = 4*ip_hdr->ihl;
five_tuple->protocol = ip_hdr->protocol;
inet_ntop(AF_INET,(void *)&(ip_hdr->saddr),five_tuple->sip,MRL_STR_IP_LEN);
inet_ntop(AF_INET,(void *)&(ip_hdr->daddr),five_tuple->dip,MRL_STR_IP_LEN);
switch(five_tuple->protocol)
{
case MRL_UDP_TYPE:
udp_hdr=(struct udphdr *)(raw_packet+iphdr_len);
five_tuple->sport = ntohs(udp_hdr->source);
five_tuple->dport = ntohs(udp_hdr->dest);
break;
case MRL_TCP_TYPE:
tcp_hdr=(struct tcphdr *)(raw_packet+iphdr_len);
five_tuple->sport = ntohs(tcp_hdr->source);
five_tuple->dport = ntohs(tcp_hdr->dest);
break;
default:
assert(0);
break;
}
return 0;
}
unsigned int mrl_get_ip_by_eth_name(const char *ifname)
{
int sockfd;
struct ifreq ifr;
unsigned int ip;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
goto error;
}
strcpy(ifr.ifr_name,ifname);
if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) {
goto error;
}
ip = ((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr.s_addr;
close(sockfd);
return ip;
error:
close(sockfd);
return -1;
}
/* ascii�ַ�ת16���� */
char mrl_ascii_to_hex(char ascii)
{
char c = 0;
switch(ascii)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
c = ascii - 0x30;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
c = 10 + ascii - 0x61;
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
c = 10 + ascii - 0x41;
break;
}
return c;
}
/* 2012-04-11 LiJia add,��MAC�ַ�����ʽת��Ϊ16����MAC��ַ.
����:
str: MAC��ַ�ַ���
delim: �ַ����ָ���������Ϊ':', '-'��,��: xx:xx:xx:xx:xx:xx
����ַ����ָ�����delim��Ϊ-1.
mac: �洢MAC��ַ������(ָ��),���Ϊ������,
������MAC��ַΪ11:22:33:44:55:66,��mac[0]Ϊ0x11,mac[5]Ϊ0x66.
����ֵ:
0: ����
-1:����
*/
int mrl_mac_pton(const char *str, int delim, uint8_t *mac)
{
#define MAC_STR_LEN_DELIM (17) /* length of "11:22:33:44:55:66" */
#define MAC_STR_LEN_NODELIM (12) /* length of "112233445566" */
const char *s = str;
int i;
/* �������Ϸ��� */
if(delim != -1)
{
if(strlen(str) != MAC_STR_LEN_DELIM)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mac_pton","MAC string length error!");
assert(0);
return -1;
}
}
else
{
if(strlen(str) != MAC_STR_LEN_NODELIM)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mac_pton","MAC string length error!");
assert(0);
return -1;
}
}
/* �������Ϸ��ԣ�ͬʱת����16����ֵ */
for(i = 0; i < 6; i++)
{
mac[i] = 0; /* �����㣬��ֵ��䶼�ǻ���� */
if(isxdigit(*s)==0)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mac_pton","MAC string type error!");
assert(0);
return -1;
}
mac[i] |= mrl_ascii_to_hex(*s) << 4;
s++;
if(isxdigit(*s)==0)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mac_pton","MAC string type error!");
assert(0);
return -1;
}
mac[i] |= mrl_ascii_to_hex(*s);
s++;
if((delim != -1) && i<5 && (*s++ != (char)delim))
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mac_pton","MAC string type error!");
assert(0);
return -1;
}
}
return 0;
}
unsigned int mrl_split_str(char *str, const char *delim, char **dest)
{
unsigned int i = 0;
char *temp;
size_t len = 0;
temp = strtok(str, delim);
while (temp != NULL)
{
len = strlen(temp) + 1;
/* While there are tokens in "string"*/
if(strncpy(dest[i], (const char*)temp, len) <0)
{
assert(0);
}
dest[i][len]='\0';
i++;
/* Get next token: */
temp = strtok(NULL, delim);
}
return i;
}
void mrl_get_cur_time(char *date)
{
time_t t;
struct tm *lt;
time(&t);
lt = localtime(&t);
snprintf(date, MRL_DATE_LEN,"%d/%d/%d/%d:%d:%d",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
}
unsigned short mrl_get_checksum(unsigned short * buffer, size_t len)
{
unsigned int sum = 0;
while (len > 1) {
sum += *buffer++;
len -= sizeof(unsigned short);
}
if (len) {
sum += *(unsigned char *)buffer;
}
while (sum >> 16) {
sum = (sum >> 16) + (sum & 0xffff);
}
return (unsigned short)(~sum);
}
void mrl_mmdb_init(const char *path)
{
int status =MMDB_open(path,MMDB_MODE_MMAP,&(mrl_instance.mrl_mmdb));
if(status != MMDB_SUCCESS)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mmdb_init","Can't open %s - %s!",path,MMDB_strerror(status));
if(status == MMDB_IO_ERROR)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mmdb_init","IO error: %s",strerror(errno));
}
assert(0);
}
}
int mrl_search_ip_country(MMDB_s mmdb, const char *ip_address, const char *mycountry)
{
int gai_error, mmdb_error;
MMDB_lookup_result_s result = MMDB_lookup_string(&mmdb, ip_address, &gai_error, &mmdb_error);
if (0 != gai_error)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mmdb_search","Error from getaddrinfo for %s - %s!",ip_address,gai_strerror(gai_error));
assert(0);
}
if (MMDB_SUCCESS != mmdb_error)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mmdb_search","Got an error from libmaxminddb: %s!",MMDB_strerror(mmdb_error));
assert(0);
}
MMDB_entry_data_s entry_data;
if (result.found_entry)
{
int status = MMDB_get_value(&result.entry, &entry_data, "COUNTRY", NULL);
if (MMDB_SUCCESS != status)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_mmdb_search","Got an error looking up the entry data - %s!",MMDB_strerror(status));
assert(0);
}
if (entry_data.has_data)
{
if(memcmp(mycountry, entry_data.utf8_string, entry_data.data_size) == 0)
{
return 0;
}
else
{
return 1;
}
}
}
return -1;
}
int mrl_is_inside_ip(const char *ip_addr)
{
int location = 0;
location = mrl_search_ip_country(mrl_instance.mrl_mmdb, ip_addr, mrl_instance.mrl_cfg.ip_location);
if(location < 0)
{
MESA_handle_runtime_log(mrl_instance.mrl_log_handle, RLOG_LV_FATAL,"mrl_is_inside_ip","cur ip addr %s not exist in mmdb",ip_addr);
return 0;
}
return location;
}
|