/
topology_hiding.c
283 lines (240 loc) · 7.77 KB
/
topology_hiding.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
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
/**
* Topology Hiding Module
*
* Copyright (C) 2015 OpenSIPS Foundation
*
* This file is part of opensips, a free SIP server.
*
* opensips 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 2 of the License, or
* (at your option) any later version
*
* opensips 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History
* -------
* 2015-02-17 initial version (Vlad Paiu)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "topo_hiding_logic.h"
struct tm_binds tm_api;
struct dlg_binds dlg_api;
int force_dialog = 0;
str topo_hiding_ct_params = {0,0};
str topo_hiding_ct_hdr_params = {0,0};
str topo_hiding_prefix = str_init("DLGCH_");
str topo_hiding_seed = str_init("OpenSIPS");
str topo_hiding_ct_encode_pw = str_init("ToPoCtPaSS");
str th_contact_encode_param = str_init("thinfo");
static int mod_init(void);
static void mod_destroy(void);
static int fixup_mmode(void **param);
int w_topology_hiding(struct sip_msg *req, str *flags_s);
int w_topology_hiding_match(struct sip_msg *req, void *seq_match_mode_val);
static int pv_topo_callee_callid(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
static cmd_export_t cmds[]={
{"topology_hiding",(cmd_function)w_topology_hiding, {
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0}, {0,0,0}},
REQUEST_ROUTE},
{"topology_hiding_match",(cmd_function)w_topology_hiding_match, {
{CMD_PARAM_STR|CMD_PARAM_OPT, fixup_mmode, 0}, {0,0,0}},
REQUEST_ROUTE},
{0,0,{{0,0,0}},0}
};
/* Exported parameters */
static param_export_t params[] = {
{ "force_dialog", INT_PARAM, &force_dialog },
{ "th_passed_contact_uri_params",STR_PARAM, &topo_hiding_ct_params.s },
{ "th_passed_contact_params", STR_PARAM, &topo_hiding_ct_hdr_params.s },
{ "th_callid_passwd", STR_PARAM, &topo_hiding_seed.s },
{ "th_callid_prefix", STR_PARAM, &topo_hiding_prefix.s },
{ "th_contact_encode_passwd", STR_PARAM, &topo_hiding_ct_encode_pw.s },
{ "th_contact_encode_param", STR_PARAM, &th_contact_encode_param.s },
{0, 0, 0}
};
static pv_export_t pvars[] = {
{ {"TH_callee_callid", sizeof("TH_callee_callid")-1}, 1000,
pv_topo_callee_callid,0,0, 0, 0, 0},
{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
};
static module_dependency_t *get_deps_dialog(param_export_t *param)
{
int force = *(int *)param->param_pointer;
if (force == 0)
return NULL;
return alloc_module_dep(MOD_TYPE_DEFAULT, "dialog", DEP_ABORT);
}
static dep_export_t deps = {
{ /* OpenSIPS module dependencies */
{ MOD_TYPE_DEFAULT, "tm", DEP_ABORT },
{ MOD_TYPE_DEFAULT, "dialog", DEP_SILENT },
{ MOD_TYPE_NULL, NULL, 0 },
},
{ /* modparam dependencies */
{ "force_dialog", get_deps_dialog },
{ NULL, NULL },
},
};
struct module_exports exports= {
"topology_hiding",
MOD_TYPE_DEFAULT, /* class of this module */
MODULE_VERSION,
DEFAULT_DLFLAGS, /* dlopen flags */
&deps, /* OpenSIPS module dependencies */
cmds, /* exported functions */
0, /* exported async functions */
params, /* param exports */
0, /* exported statistics */
0, /* exported MI functions */
pvars, /* exported pseudo-variables */
0, /* exported transformations */
0, /* extra processes */
mod_init, /* module initialization function */
(response_function) 0,
mod_destroy,
0 /* per-child init function */
};
static int mod_init(void)
{
LM_INFO("initializing...\n");
/* param handling */
topo_hiding_prefix.len = strlen(topo_hiding_prefix.s);
topo_hiding_seed.len = strlen(topo_hiding_seed.s);
th_contact_encode_param.len = strlen(th_contact_encode_param.s);
topo_hiding_ct_encode_pw.len = strlen(topo_hiding_ct_encode_pw.s);
if (topo_hiding_ct_params.s) {
topo_hiding_ct_params.len = strlen(topo_hiding_ct_params.s);
topo_parse_passed_ct_params(&topo_hiding_ct_params);
}
if (topo_hiding_ct_hdr_params.s) {
topo_hiding_ct_hdr_params.len = strlen(topo_hiding_ct_hdr_params.s);
topo_parse_passed_hdr_ct_params(&topo_hiding_ct_hdr_params);
}
/* loading dependencies */
if (load_tm_api(&tm_api)!=0) {
LM_ERR("can't load TM API\n");
goto error;
}
if (load_dlg_api(&dlg_api)!=0) {
if (force_dialog) {
LM_ERR("cannot force dialog. dialog module not loaded\n");
goto error;
}
}
if (register_pre_raw_processing_cb(topo_callid_pre_raw,
PRE_RAW_PROCESSING, 0/*no free*/) < 0) {
LM_ERR("failed to initialize pre raw support\n");
return -1;
}
if (register_post_raw_processing_cb(topo_callid_post_raw,
POST_RAW_PROCESSING, 0/*no free*/) < 0) {
LM_ERR("failed to initialize post raw support\n");
return -1;
}
/* restore dialog callbacks when restart */
if (dlg_api.register_dlgcb && dlg_api.register_dlgcb(NULL,
DLGCB_LOADED,th_loaded_callback, NULL, NULL) < 0)
LM_ERR("cannot register callback for dialog loaded - topology "
"hiding signalling for ongoing calls will be lost after "
"restart\n");
return 0;
error:
return -1;
}
static void mod_destroy(void)
{
return;
}
static int fixup_mmode(void **param)
{
*param = (void*)(unsigned long)dlg_match_mode_str_to_int((str*)*param);
return 0;
}
int w_topology_hiding(struct sip_msg *req, str *flags_s)
{
int flags=0;
char *p;
if (flags_s)
for (p=flags_s->s;p<flags_s->s+flags_s->len;p++)
{
switch (*p)
{
case 'U':
flags |= TOPOH_KEEP_USER;
LM_DBG("Will preserve usernames while doing topo hiding\n");
break;
case 'C':
flags |= TOPOH_HIDE_CALLID;
LM_DBG("Will change callid while doing topo hiding\n");
break;
case 'D':
flags |= TOPOH_DID_IN_USER;
LM_DBG("Will push DID into contact username\n");
break;
default:
LM_DBG("unknown topology_hiding flag : [%c] . Skipping\n",*p);
}
}
return topology_hiding(req,flags);
}
int w_topology_hiding_match(struct sip_msg *req, void *seq_match_mode_val)
{
int mm;
/* copy-paste from w_match_dialog() */
if (!seq_match_mode_val)
mm = SEQ_MATCH_DEFAULT;
else
mm = (int)seq_match_mode_val;
if (!dlg_api.match_dialog || dlg_api.match_dialog(req, mm) < 0)
return topology_hiding_match(req);
else
/* we went to the dlg module, which triggered us back, all good */
return 1;
}
static char *callid_buf=NULL;
static int callid_buf_len=0;
static int pv_topo_callee_callid(struct sip_msg *msg, pv_param_t *param, pv_value_t *res)
{
struct dlg_cell *dlg;
int req_len = 0,i;
char *p;
if(res==NULL)
return -1;
if ( (dlg=dlg_api.get_dlg())==NULL ||
(!dlg_api.is_mod_flag_set(dlg,TOPOH_HIDE_CALLID))) {
return pv_get_null( msg, param, res);
}
req_len = calc_base64_encode_len(dlg->callid.len) + topo_hiding_prefix.len;
if (req_len*2 > callid_buf_len) {
callid_buf = pkg_realloc(callid_buf,req_len*2);
if (callid_buf == NULL) {
LM_ERR("No more pkg\n");
return pv_get_null( msg, param, res);
}
callid_buf_len = req_len*2;
}
memcpy(callid_buf+req_len,topo_hiding_prefix.s,topo_hiding_prefix.len);
for (i=0;i<dlg->callid.len;i++)
callid_buf[i] = dlg->callid.s[i] ^ topo_hiding_seed.s[i%topo_hiding_seed.len];
base64encode((unsigned char *)(callid_buf+topo_hiding_prefix.len+req_len),
(unsigned char *)(callid_buf),dlg->callid.len);
p = callid_buf+ 2*req_len - 1;
while (*p == '=') {
*p = '-';
p--;
}
res->rs.s = callid_buf+req_len;
res->rs.len = req_len;
res->flags = PV_VAL_STR;
return 0;
}