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
|
#include <stdio.h>
#include <string.h>
#include <MESA/stream.h>
#include <MESA/MESA_prof_load.h>
#include <MESA/MESA_handle_logger.h>
#include "tsg_log.h"
#include "tsg_rule_internal.h"
#include "tsg_gtp_signaling.h"
MESA_htable_handle g_gtp_signaling_hash_handle=NULL;
enum SIGNALING_ORIGIN
{
SIGNALING_ORIGIN_NO=0,
SIGNALING_ORIGIN_HASH=1,
SIGNALING_ORIGIN_REDIS=2,
};
static int is_gtp_tunnel(const struct streaminfo *a_stream)
{
int ret=0;
unsigned short is_tunnel=0;
int size=sizeof(unsigned short);
ret=MESA_get_stream_opt(a_stream, MSO_STREAM_TUNNEL_TYPE, (void *)&is_tunnel, &size);
if(ret>=0 && is_tunnel==STREAM_TUNNEL_GPRS_TUNNEL)
{
return 1;
}
return 0;
}
static int get_gtp_teid(const struct streaminfo *a_stream, unsigned int *uplink, unsigned int *downlink)
{
const struct streaminfo *p=a_stream;
while(p)
{
if(p->addr.addrtype==ADDR_TYPE_GPRS_TUNNEL)
{
*uplink=ntohl(p->addr.gtp->teid_c2s);
*downlink=ntohl(p->addr.gtp->teid_s2c);
return 1;
}
p=p->pfather;
}
return 0;
}
static int copy_one_field(char **dst, char *src, int src_len)
{
if(src!=NULL && src_len>0)
{
*dst=(char *)calloc(1, src_len+1);
memcpy(*dst, src, src_len);
return 1;
}
return 0;
}
static long copy_user_info(void *data, const uchar *key, uint size, void *user_arg)
{
int num=0;
struct gtp_signaling_field *signal=(struct gtp_signaling_field *)data;
struct umts_user_info *user_info=(struct umts_user_info *)user_arg;
if(signal!=NULL)
{
num+=copy_one_field(&(user_info->apn), (char *)(signal->ie_unit[GTP_FIELD_APN].value), signal->ie_unit[GTP_FIELD_APN].len);
num+=copy_one_field(&(user_info->imsi), (char *)(signal->ie_unit[GTP_FIELD_IMSI].value), signal->ie_unit[GTP_FIELD_IMSI].len);
num+=copy_one_field(&(user_info->imei), (char *)(signal->ie_unit[GTP_FIELD_IMEI].value), signal->ie_unit[GTP_FIELD_IMEI].len);
num+=copy_one_field(&(user_info->msisdn), (char *)(signal->ie_unit[GTP_FIELD_MSISDN].value), signal->ie_unit[GTP_FIELD_MSISDN].len);
}
return num;
}
unsigned int gtp_c_key2index(const MESA_htable_handle table, const uchar * key, uint size)
{
if(size==sizeof(unsigned int))
{
return *(unsigned int *)key;
}
return 0;
}
void free_gtp_signaling_field(void *data)
{
int i=0;
struct gtp_signaling_field *signal=(struct gtp_signaling_field *)data;
if(data==NULL)
{
return ;
}
for(i=0; i<GTP_FIELD_MAX; i++)
{
if(signal->ie_unit[i].value!=NULL)
{
free(signal->ie_unit[i].value);
signal->ie_unit[i].value=NULL;
}
}
free(data);
data=NULL;
return ;
}
void tsg_free_gtp_signaling_field(void *data)
{
if(data!=NULL)
{
free_gtp_signaling_field(data);
data=NULL;
}
}
static int get_umts_user_info_form_hash(struct umts_user_info **user_info, unsigned int teid, int thread_seq)
{
long cb_ret=0;
struct umts_user_info tmp_user_info={0};
MESA_htable_search_cb(g_gtp_signaling_hash_handle, (unsigned char *)&(teid), sizeof(unsigned int), copy_user_info, (void *)&tmp_user_info, &cb_ret);
if(cb_ret>0)
{
tmp_user_info.ref_cnt=1;
*user_info=(struct umts_user_info *)calloc(1, sizeof(struct umts_user_info));
memcpy(*user_info, &tmp_user_info, sizeof(struct umts_user_info));
return 1;
}
return 0;
}
static int get_umts_user_info_form_redis(struct umts_user_info **user_info, unsigned int teid, int thread_seq)
{
(*user_info) = tsg_get_umts_user_info_form_redis(g_tsg_maat_feather, teid);
if((*user_info)!=NULL)
{
return 1;
}
return 0;
}
int session_runtine_attribute_get_umts_user_info(const struct streaminfo *a_stream, struct umts_user_info **user_info)
{
int ret=0;
unsigned int uplink=0,downlink=0;
if(g_tsg_para.scan_signaling_switch==SIGNALING_ORIGIN_NO)
{
return 0;
}
if(*user_info!=NULL)
{
return 1;
}
ret=is_gtp_tunnel(a_stream);
if(ret==0)
{
return 0;
}
ret=get_gtp_teid(a_stream, &uplink, &downlink);
if(ret==0)
{
return 0;
}
switch(g_tsg_para.scan_signaling_switch)
{
case SIGNALING_ORIGIN_HASH:
ret=get_umts_user_info_form_hash(user_info, uplink, a_stream->threadnum);
if(ret==1)
{
return 1;
}
ret=get_umts_user_info_form_hash(user_info, downlink, a_stream->threadnum);
if(ret==1)
{
return 1;
}
break;
case SIGNALING_ORIGIN_REDIS:
ret=get_umts_user_info_form_redis(user_info, uplink, a_stream->threadnum);
if(ret==1)
{
return 1;
}
ret=get_umts_user_info_form_redis(user_info, downlink, a_stream->threadnum);
if(ret==1)
{
return 1;
}
break;
default:
break;
}
return 0;
}
void tsg_gtp_signaling_hash_destroy()
{
if(g_gtp_signaling_hash_handle == NULL)return;
MESA_htable_destroy(g_gtp_signaling_hash_handle, free_gtp_signaling_field);
g_gtp_signaling_hash_handle=NULL;
}
int tsg_gtp_signaling_hash_init(const char* conffile, void *logger)
{
MESA_load_profile_int_def(conffile, "GTP_SIGNALING", "HASH_TIMEOUT", &g_tsg_para.hash_timeout, 300);
MESA_load_profile_int_def(conffile, "GTP_SIGNALING", "HASH_THREAD_SAFE", &g_tsg_para.hash_thread_safe, 128);
MESA_load_profile_int_def(conffile, "GTP_SIGNALING", "HASH_SLOT_SIZE", &g_tsg_para.hash_slot_size, 1024*1024*32);
MESA_htable_create_args_t args;
memset(&args, 0, sizeof(args));
args.thread_safe=g_tsg_para.hash_thread_safe;
args.recursive=1;
args.max_elem_num=0;
args.data_free=free_gtp_signaling_field;
args.eliminate_type=HASH_ELIMINATE_ALGO_LRU;
args.expire_time=g_tsg_para.hash_timeout;
args.hash_slot_size=g_tsg_para.hash_slot_size;
args.key2index=gtp_c_key2index;
g_gtp_signaling_hash_handle=MESA_htable_create(&args, sizeof(MESA_htable_create_args_t));
if(g_gtp_signaling_hash_handle==NULL)
{
MASTER_LOG(g_tsg_para.logger, RLOG_LV_FATAL, LOG_MODULE_INIT, "GTP_SIGNALING MESA_htable_create failed");
return -1;
}
char signaling_origin[32]={0};
MESA_load_profile_string_def(conffile, "GTP_SIGNALING", "SIGNALING_ORIGIN", signaling_origin, sizeof(signaling_origin), "HASH");
int length=strlen(signaling_origin);
if(length==2 && (strncasecmp("NO", signaling_origin, 2)==0))
{
g_tsg_para.scan_signaling_switch=SIGNALING_ORIGIN_NO;
}
else if(length==4 && (strncasecmp("HASH", signaling_origin, 4)==0))
{
g_tsg_para.scan_signaling_switch=SIGNALING_ORIGIN_HASH;
}
else if(length==5 && (strncasecmp("REDIS", signaling_origin, 5)==0))
{
g_tsg_para.scan_signaling_switch=SIGNALING_ORIGIN_REDIS;
}
return 0;
}
|