-
Notifications
You must be signed in to change notification settings - Fork 17
/
enumroute.cpp
343 lines (321 loc) · 9.98 KB
/
enumroute.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
* enumroute.cpp
* This file is part of the YATE Project http://YATE.null.ro
*
* ENUM routing module
*
* Yet Another Telephony Engine - a fully featured software PBX and IVR
* Copyright (C) 2004-2023 Null Team
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* 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.
*/
#include <yatephone.h>
using namespace TelEngine;
namespace { // anonymous
#define ENUM_DEF_TIMEOUT 3
#define ENUM_DEF_RETRIES 2
#define ENUM_DEF_MINLEN 8
#define ENUM_DEF_MAXCALL 30000
class EnumModule : public Module
{
public:
inline EnumModule()
: Module("enumroute","route"), m_init(false)
{ }
virtual void initialize();
virtual void statusParams(String& str);
void genUpdate(Message& msg);
private:
bool m_init;
};
static String s_prefix;
static String s_forkStop;
static String s_domains;
static unsigned int s_minlen;
static int s_timeout;
static int s_retries;
static int s_maxcall;
static bool s_success;
static bool s_redirect;
static bool s_autoFork;
static bool s_sipUsed;
static bool s_iaxUsed;
static bool s_h323Used;
static bool s_xmppUsed;
static bool s_telUsed;
static bool s_voiceUsed;
static bool s_pstnUsed;
static bool s_voidUsed;
static Mutex s_mutex(false,"EnumRoute");
static int s_queries = 0;
static int s_routed = 0;
static int s_reroute = 0;
static EnumModule emodule;
class EnumHandler : public MessageHandler
{
public:
inline EnumHandler(unsigned int prio = 90)
: MessageHandler("call.route",prio,emodule.name())
{ }
virtual bool received(Message& msg);
private:
static bool resolve(Message& msg,bool canRedirect,const ObjList* domains);
static void addRoute(String& dest,const String& src);
};
// Routing message handler, performs checks and calls resolve method
bool EnumHandler::received(Message& msg)
{
if (!msg.getBoolValue(YSTRING("enumroute"),true))
return false;
// perform per-thread initialization of resolver and timeout settings
if (!Resolver::init(s_timeout,s_retries))
return false;
const String* d = msg.getParam(YSTRING("enum_domains"));
s_mutex.lock();
if (!d)
d = &s_domains;
ObjList* domains = d->split(',',false);
s_mutex.unlock();
bool ok = domains && resolve(msg,s_telUsed,domains);
TelEngine::destruct(domains);
// return false to allow further routing if configured or asked so
if (ok && !msg.getBoolValue(YSTRING("enum_success"),s_success)) {
msg.setParam("success_enum",String::boolText(true));
ok = false;
}
return ok;
}
// Resolver function, may call itself recursively at most once
bool EnumHandler::resolve(Message& msg,bool canRedirect,const ObjList* domains)
{
// give preference to full (e164) called number if exists
String called(msg.getValue("calledfull"));
if (called.null())
called = msg.getValue("called");
if (called.null())
return false;
// check if the called starts with international prefix, remove it
if (!(called.startSkip("+",false) ||
(s_prefix && called.startSkip(s_prefix,false))))
return false;
if (called.length() < s_minlen)
return false;
bool rval = false;
// put the standard international prefix in front
called = "+" + called;
String tmp;
for (int i = called.length()-1; i > 0; i--)
tmp << called.at(i) << ".";
u_int64_t dt = Time::now();
ObjList res;
for (const ObjList* l = domains; l; l = l->next()) {
const String* s = static_cast<const String*>(l->get());
if (!s || s->null())
continue;
int result = Resolver::naptrQuery(tmp + *s,res);
if ((result == 0) && res.skipNull())
break;
}
dt = Time::now() - dt;
Debug(&emodule,DebugInfo,"Returned %d NAPTR records in %u.%06u s",
res.count(),(unsigned int)(dt / 1000000),(unsigned int)(dt % 1000000));
bool reroute = false;
bool unassigned = false;
if (res.skipNull()) {
msg.retValue().clear();
bool autoFork = msg.getBoolValue("autofork",s_autoFork);
for (ObjList* cur = res.skipNull(); cur; cur = cur->skipNext()) {
NaptrRecord* ptr = static_cast<NaptrRecord*>(cur->get());
DDebug(&emodule,DebugAll,"order=%d pref=%d '%s'",
ptr->order(),ptr->pref(),ptr->serv().c_str());
String serv = ptr->serv();
serv.toUpper();
String callto = called;
if (s_sipUsed && (serv == "E2U+SIP") && ptr->replace(callto)) {
addRoute(msg.retValue(),"sip/" + callto);
rval = true;
if (autoFork)
continue;
break;
}
if (s_iaxUsed && (serv == "E2U+IAX2") && ptr->replace(callto)) {
addRoute(msg.retValue(),"iax/" + callto);
rval = true;
if (autoFork)
continue;
break;
}
if (s_h323Used && (serv == "E2U+H323") && ptr->replace(callto)) {
addRoute(msg.retValue(),"h323/" + callto);
rval = true;
if (autoFork)
continue;
break;
}
if (s_xmppUsed && (serv == "E2U+XMPP") && ptr->replace(callto)) {
addRoute(msg.retValue(),"jingle/" + callto);
rval = true;
if (autoFork)
continue;
break;
}
if (s_pstnUsed && serv.startsWith("E2U+PSTN") && ptr->replace(callto)) {
addRoute(msg.retValue(),"pstn/" + callto);
rval = true;
if (autoFork)
continue;
break;
}
if (s_voiceUsed && serv.startsWith("E2U+VOICE") && ptr->replace(callto)) {
addRoute(msg.retValue(),"voice/" + callto);
rval = true;
if (autoFork)
continue;
break;
}
if (canRedirect && (serv == "E2U+TEL") && ptr->replace(callto)) {
if (callto.startSkip("tel:",false) ||
callto.startSkip("TEL:",false) ||
callto.startSkip("e164:",false) ||
callto.startSkip("E164:",false))
{
reroute = true;
rval = false;
msg.setParam("called",callto);
msg.clearParam("calledfull");
if (msg.retValue()) {
Debug(&emodule,DebugMild,"Redirect drops collected route: %s",
msg.retValue().c_str());
msg.retValue().clear();
}
break;
}
continue;
}
if (s_voidUsed && serv.startsWith("E2U+VOID") && ptr->replace(callto)) {
// remember it's unassigned but still continue scanning
unassigned = true;
}
}
}
s_mutex.lock();
if (rval) {
if (msg.retValue().startsWith("fork",true)) {
msg.setParam("maxcall",String(s_maxcall));
msg.setParam("fork.stop",s_forkStop);
}
else if (s_redirect)
msg.setParam("redirect",String::boolText(true));
}
s_queries++;
if (rval)
s_routed++;
if (reroute)
s_reroute++;
emodule.changed();
s_mutex.unlock();
if (reroute)
return resolve(msg,false,domains);
if (unassigned && !rval) {
rval = true;
msg.retValue() = "-";
msg.setParam("error","unallocated");
}
return rval;
}
// Add one route to the result, take care of forking
void EnumHandler::addRoute(String& dest,const String& src)
{
if (dest.null())
dest = src;
else {
if (!dest.startsWith("fork",true))
dest = "fork " + dest;
dest << " | " << src;
}
}
void EnumModule::statusParams(String& str)
{
str.append("queries=",",") << s_queries << ",routed=" << s_routed << ",rerouted=" << s_reroute;
}
void EnumModule::genUpdate(Message& msg)
{
msg.setParam("queries",String(s_queries));
msg.setParam("routed",String(s_routed));
msg.setParam("rerouted",String(s_reroute));
}
void EnumModule::initialize()
{
Module::initialize();
Configuration cfg(Engine::configFile("enumroute"));
int prio = cfg.getIntValue("general","priority",0);
if ((prio <= 0) && !m_init)
return;
Output("Initializing ENUM routing");
s_mutex.lock();
// in most of the world this default international prefix should work
s_prefix = cfg.getValue("general","prefix","00");
s_domains = cfg.getValue("general","domains");
if (s_domains.null()) {
// old style, just for compatibility
s_domains = cfg.getValue("general","domain","e164.arpa");
s_domains.append(cfg.getValue("general","backup","e164.org"),",");
}
s_forkStop = cfg.getValue("general","forkstop","busy");
s_mutex.unlock();
DDebug(&emodule,DebugInfo,"Domain list: %s",s_domains.c_str());
s_minlen = cfg.getIntValue("general","minlen",ENUM_DEF_MINLEN);
int tmp = cfg.getIntValue("general","timeout",ENUM_DEF_TIMEOUT);
// limit between 1 and 10 seconds
if (tmp < 1)
tmp = 1;
if (tmp > 10)
tmp = 10;
s_timeout = tmp;
tmp = cfg.getIntValue("general","retries",ENUM_DEF_RETRIES);
// limit between 1 and 5 retries
if (tmp < 1)
tmp = 1;
if (tmp > 5)
tmp = 5;
s_retries = tmp;
// overall a resolve attempt will take at most 50s per domain
tmp = cfg.getIntValue("general","maxcall",ENUM_DEF_MAXCALL);
// limit between 2 and 120 seconds
if (tmp < 2000)
tmp = 2000;
if (tmp > 120000)
tmp = 120000;
s_maxcall = tmp;
s_success = cfg.getBoolValue("general","success",true);
s_redirect = cfg.getBoolValue("general","redirect");
s_autoFork = cfg.getBoolValue("general","autofork");
s_sipUsed = cfg.getBoolValue("protocols","sip",true);
s_iaxUsed = cfg.getBoolValue("protocols","iax",true);
s_h323Used = cfg.getBoolValue("protocols","h323",true);
s_xmppUsed = cfg.getBoolValue("protocols","jingle",true);
s_voidUsed = cfg.getBoolValue("protocols","void",true);
// by default don't support the number rerouting
s_telUsed = cfg.getBoolValue("protocols","tel",false);
// also don't enable gateways by default as more setup is needed
s_pstnUsed = cfg.getBoolValue("protocols","pstn",false);
s_voiceUsed= cfg.getBoolValue("protocols","voice",false);
if (m_init || (prio <= 0))
return;
m_init = true;
if (Resolver::available(Resolver::Naptr))
Engine::install(new EnumHandler(cfg.getIntValue("general","priority",prio)));
else
Debug(&emodule,DebugCrit,"NAPTR resolver is not available on this platform");
}
}; // anonymous namespace
/* vi: set ts=8 sw=4 sts=4 noet: */