1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** match processing of routing configuration **
10 \*****************************************************************************/
15 struct route_ruleset *ruleset_first; /* first entry */
16 struct route_ruleset *ruleset_main; /* pointer to main ruleset */
18 struct cond_defs cond_defs[] = {
19 { "extern", MATCH_EXTERN, COND_TYPE_NULL,
20 "extern", "Matches if call is from external port (no extension)."},
21 { "intern", MATCH_INTERN,COND_TYPE_NULL,
22 "intern", "Matches if call is from an extension."},
23 { "port", MATCH_PORT, COND_TYPE_INTEGER,
24 "port=<number>[-<number>][,...]", "Matches if call is received from given port(s). NOT INTERFACE!"},
25 { "interface", MATCH_INTERFACE,COND_TYPE_STRING,
26 "interface=<interface>[,...]", "Matches if call is received from given interface(s). NOT PORTS!"},
27 { "callerid", MATCH_CALLERID, COND_TYPE_STRING,
28 "callerid=<digits>[-<digits>][,...]", "Matches if caller ID matches or begins with the given (range(s) of) prefixes(s)."},
29 { "extension", MATCH_EXTENSION,COND_TYPE_STRING,
30 "extension=<digits>[-<digits>][,...]", "Matches if caller calls from given (range(s) of) extension(s)."},
31 { "dialing", MATCH_DIALING, COND_TYPE_STRING,
32 "dialing=<digits>[-<digits>][,...]", "Matches if caller has dialed the given (range(s) of) digits at least."},
33 { "enblock", MATCH_ENBLOCK, COND_TYPE_NULL,
34 "enblock", "Matches if caller dialed en block. (Dial the number before pick up.)"},
35 { "overlap", MATCH_OVERLAP, COND_TYPE_NULL,
36 "overlap", "Matches if caller dialed digit by digit. (Dial the number after pick up.)"},
37 { "anonymous", MATCH_ANONYMOUS,COND_TYPE_NULL,
38 "anonymous", "Matches if caller uses restricted caller ID or if not available."},
39 { "visible", MATCH_VISIBLE, COND_TYPE_NULL,
40 "visible", "Matches if caller ID is presented and if available."},
41 { "unknown", MATCH_UNKNOWN, COND_TYPE_NULL,
42 "unknown", "Matches if no ID is available from caller."},
43 { "available", MATCH_AVAILABLE,COND_TYPE_NULL,
44 "available", "Matches if ID is available from caller."},
45 { "fake", MATCH_FAKE, COND_TYPE_NULL,
46 "fake", "Matches if caller ID is not screened and may be faked by caller."},
47 { "real", MATCH_REAL, COND_TYPE_NULL,
48 "real", "Matches if caller ID is screend and so it is the real caller's ID."},
49 { "redirected", MATCH_REDIRECTED,COND_TYPE_NULL,
50 "redirected", "Matches if caller has been redirected."},
51 { "direct", MATCH_DIRECT ,COND_TYPE_NULL,
52 "direct", "Matches if caller did not come from an redirection."},
53 { "redirid", MATCH_REDIRID ,COND_TYPE_STRING,
54 "redirid=<digits>[-<digits>][,...]", "Matches if the caller has been redirected by the given (range(s) of) ID(s) or prefix(es))"},
55 { "time", MATCH_TIME, COND_TYPE_TIME,
56 "time=<minutes>[-<minutes>][,...]", "Matches if the caller calls within the given (range(s) of) time(s). (e.g. 0700-1900)"},
57 { "mday", MATCH_MDAY, COND_TYPE_MDAY,
58 "mday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) day(s) of the month. (1..31)"},
59 { "month", MATCH_MONTH, COND_TYPE_MONTH,
60 "month=<month>[-<month>][,...]", "Matches if the caller calls within the given (range(s) of) month(s). (1=January..12=December)"},
61 { "year", MATCH_YEAR, COND_TYPE_YEAR,
62 "year=<year>[-<year>][,...]", "Matches if the caller calls within the given (range(s) of) year(s). (1970..2106)"},
63 { "wday", MATCH_WDAY, COND_TYPE_WDAY,
64 "wday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) weekday(s). (1=Monday..7=Sunday)"},
65 { "capability", MATCH_CAPABILITY, COND_TYPE_CAPABILITY,
66 "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones[,...]", "Matches the given bearer capability(s)."},
67 { "infolayer1", MATCH_INFOLAYER1, COND_TYPE_INTEGER,
68 "infolayer1=<value>[,...]", "Matches the given information layer 1. (2=u-Law, 3=a-law, see info layer 1 in bearer capability.)"},
69 { "hlc", MATCH_HLC, COND_TYPE_INTEGER,
70 "hlc=<value>[,...]", "Matches the high layer capability(s)."},
71 { "file", MATCH_FILE, COND_TYPE_STRING,
72 "file=<path>[,...]", "Mathes is the given file exists and if the first character is '1'."},
73 { "execute", MATCH_EXECUTE, COND_TYPE_STRING,
74 "execute=<command>[,...]","Matches if the return value of the given command is greater 0."},
75 { "default", MATCH_DEFAULT, COND_TYPE_NULL,
76 "default","Matches if no further dialing could match."},
77 { "timeout", MATCH_TIMEOUT, COND_TYPE_INTEGER,
78 "timeout=<seconds>","Matches if the ruleset was entered AFTER given seconds."},
79 { "free", MATCH_FREE, COND_TYPE_IFATTR,
80 "free=<interface>:<channel>","Matches if the given minimum of channels are free."},
81 { "notfree", MATCH_NOTFREE, COND_TYPE_IFATTR,
82 "notfree=<interface>:<channel>","Matches if NOT the given minimum of channels are free."},
83 { "blocked", MATCH_DOWN, COND_TYPE_STRING,
84 "blocked=<interfaces>[,...]","Matches if all of the given interfaces are blocked."},
85 { "idle", MATCH_UP, COND_TYPE_STRING,
86 "idle=<interface>[,...]","Matches if any of the given interfaces is idle."},
87 { "busy", MATCH_BUSY, COND_TYPE_STRING,
88 "busy=<extension>[,...]","Matches if any of the given extension is busy."},
89 { "notbusy", MATCH_IDLE, COND_TYPE_STRING,
90 "notbusy=<extension>[,...]","Matches if any of the given extension is not busy."},
91 { "remote", MATCH_REMOTE, COND_TYPE_STRING,
92 "remote=<application name>","Matches if remote application is running."},
93 { "notremote", MATCH_NOTREMOTE,COND_TYPE_NULL,
94 "notremote=<application name>","Matches if remote application is not running."},
98 struct param_defs param_defs[] = {
100 "proceeding", PARAM_TYPE_NULL,
101 "proceeding", "Will set the call into 'proceeding' state to prevent dial timeout."},
103 "alerting", PARAM_TYPE_NULL,
104 "alerting", "Will set the call into 'altering' state."},
106 "connect", PARAM_TYPE_NULL,
107 "connect", "Will complete the call before processing the action. Audio path for external calls will be established."},
109 "extension", PARAM_TYPE_STRING,
110 "extension=<digits>", "Give extension name (digits) to relate this action to."},
112 "extensions", PARAM_TYPE_STRING,
113 "extensions=<extension>[,<extension>[,...]]", "One or more extensions may be given."},
115 "prefix", PARAM_TYPE_STRING,
116 "prefix=<digits>", "Add prefix in front of the dialed number."},
118 "capability", PARAM_TYPE_CAPABILITY,
119 "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones", "Alter the service type of the call."},
121 "bmode", PARAM_TYPE_BMODE,
122 "bmode=transparent|hdlc", "Alter the bchannel mode of the call. Use hdlc for data calls."},
124 "infolayer1", PARAM_TYPE_INTEGER,
125 "infolayer1=<value>", "Alter the layer 1 information of a call. Use 3 for ALAW or 2 for uLAW."},
127 "hlc", PARAM_TYPE_INTEGER,
128 "hlc=<value>", "Alter the HLC identification. Use 1 for telephony or omit."},
130 "exthlc", PARAM_TYPE_INTEGER,
131 "exthlc=<value>", "Alter extended HLC value. (Mainenance only, don't use it.)"},
133 "present", PARAM_TYPE_YESNO,
134 "present=yes|no", "Allow or restrict caller ID regardless what the caller wants."},
136 "diversion", PARAM_TYPE_DIVERSION,
137 "diversion=cfu|cfnr|cfb|cfp", "Set diversion type."},
139 "dest", PARAM_TYPE_DESTIN,
140 "dest=<string>", "Destination number to divert to. Use 'vbox' to divert to vbox. (cfu,cfnr,cfb only)"},
142 "select", PARAM_TYPE_NULL,
143 "select", "Lets the caller select the history of calls using keys '1'/'3' or '*'/'#'."},
145 "delay", PARAM_TYPE_INTEGER,
146 "delay=<seconds>", "Number of seconds to delay."},
148 "limit", PARAM_TYPE_INTEGER,
149 "limit=<retries>", "Number of maximum retries."},
151 "host", PARAM_TYPE_STRING,
152 "host=<string>", "Name of remote VoIP host."},
154 "port", PARAM_TYPE_STRING,
155 "port=<value>", "Alternate port to use if 'host' is given."},
157 "interfaces", PARAM_TYPE_STRING,
158 "interfaces=<interface>[,<interface>[,...]]", "Give one or a list of Interfaces to select a free channel from."},
160 "address", PARAM_TYPE_STRING,
161 "address=<string>", "Complete VoIP address. ( [user@]host[:port] )"},
163 "sample", PARAM_TYPE_STRING,
164 "sample=<file prefix>", "Filename of sample (current tone's dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
165 { PARAM_ANNOUNCEMENT,
166 "announcement",PARAM_TYPE_STRING,
167 "announcement=<file prefix>", "Filename of announcement (inside vbox recording dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
169 "ruleset", PARAM_TYPE_STRING,
170 "ruleset=<name>", "Ruleset to go to."},
172 "cause", PARAM_TYPE_INTEGER,
173 "cause=<cause value>", "Cause value when disconnecting. (21=reject 1=unassigned 63=service not available)"},
175 "location", PARAM_TYPE_INTEGER,
176 "location=<location value>", "Location of cause value when disconnecting. (0=user 1=private network sering local user)"},
178 "display", PARAM_TYPE_STRING,
179 "display=<text>", "Message to display on the caller's telephone. (internal only)"},
181 "ports", PARAM_TYPE_INTEGER,
182 "ports=<port>[,<port>[,...]]", "ISDN port[s] to use."},
184 "tpreset", PARAM_TYPE_INTEGER,
185 "tpreset=<seconds>", "Preset of countdown timer."},
187 "file", PARAM_TYPE_STRING,
188 "file=<full path>", "Full path to file name."},
190 "content", PARAM_TYPE_STRING,
191 "content=<string>", "Content to write into file."},
193 "append", PARAM_TYPE_NULL,
194 "append", "Will append to given file, rather than overwriting it."},
196 "execute", PARAM_TYPE_STRING,
197 "execute=<full path>", "Full path to script/command name. (Dialed digits are the argument 1.)"},
199 "param", PARAM_TYPE_STRING,
200 "param=<string>", "Optionally this parameter can be inserted as argument 1, others are shifted."},
202 "type", PARAM_TYPE_TYPE,
203 "type=unknown|subscriber|national|international", "Type of number to dial, default is 'unknown'."},
205 "complete", PARAM_TYPE_NULL,
206 "complete", "Indicates complete number as given by prefix. Proceeding of long distance calls may be faster."},
208 "callerid", PARAM_TYPE_STRING,
209 "callerid=<digits>", "Change caller ID to given string."},
210 { PARAM_CALLERIDTYPE,
211 "calleridtype",PARAM_TYPE_CALLERIDTYPE,
212 "calleridtype=[unknown|subscriber|national|international]", "Type of caller ID. For normal MSN use 'unknown'"},
214 "callto", PARAM_TYPE_STRING,
215 "callto=<digits>", "Where to call back. By default the caller ID is used."},
217 "room", PARAM_TYPE_INTEGER,
218 "room=<digits>", "Conference room number, must be greater 0, as in real life."},
220 "jingle", PARAM_TYPE_NULL,
221 "jingle", "Conference members will hear a jingle if a member joins."},
223 "timeout", PARAM_TYPE_INTEGER,
224 "timeout=<seconds>", "Timeout before continue with next action."},
226 "nopassword", PARAM_TYPE_NULL,
227 "nopassword", "Don't ask for password. Be sure to authenticate right via real caller ID."},
229 "strip", PARAM_TYPE_NULL,
230 "strip", "Remove digits that were required to match this rule."},
232 "application",PARAM_TYPE_STRING,
233 "application=<name>", "Name of remote application to make call to."},
235 "exten", PARAM_TYPE_STRING,
236 "exten=<extension>", "Give exten parameter to the remote application."},
237 { 0, NULL, 0, NULL, NULL}
240 struct action_defs action_defs[] = {
242 "extern", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
243 PARAM_CONNECT | PARAM_PREFIX | PARAM_COMPLETE | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_INTERFACES | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_TIMEOUT,
244 "Call is routed to extern number as dialed."},
246 "intern", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_internal, &EndpointAppPBX::action_hangup_call,
247 PARAM_CONNECT | PARAM_EXTENSION | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_TIMEOUT,
248 "Call is routed to intern extension as given by the dialed number or specified by option."},
250 "outdial", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
251 PARAM_CONNECT | PARAM_PREFIX | PARAM_COMPLETE | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_INTERFACES | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_TIMEOUT,
254 "remote", &EndpointAppPBX::action_init_remote, &EndpointAppPBX::action_dialing_remote, &EndpointAppPBX::action_hangup_call,
255 PARAM_CONNECT | PARAM_APPLICATION | PARAM_EXTEN | PARAM_TIMEOUT,
256 "Call is routed to Remote application, like Asterisk."},
257 { ACTION_VBOX_RECORD,
258 "vbox-record",&EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_vbox_record, &EndpointAppPBX::action_hangup_call,
259 PARAM_CONNECT | PARAM_EXTENSION | PARAM_ANNOUNCEMENT | PARAM_TIMEOUT,
260 "Caller is routed to the voice box of given extension."},
262 "partyline",&EndpointAppPBX::action_init_partyline, NULL, &EndpointAppPBX::action_hangup_call,
263 PARAM_ROOM | PARAM_JINGLE,
264 "Caller is participating the conference with the given room number."},
266 "login", NULL, &EndpointAppPBX::action_dialing_login, NULL,
267 PARAM_CONNECT | PARAM_EXTENSION | PARAM_NOPASSWORD,
268 "Log into the given extension. Password required."},
270 "callerid", &EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_callerid, NULL,
271 PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
272 "Caller changes the caller ID for all calls."},
273 { ACTION_CALLERIDNEXT,
274 "calleridnext",&EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_calleridnext, NULL,
275 PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
276 "Caller changes the caller ID for the next call."},
278 "forward", &EndpointAppPBX::action_init_change_forward, &EndpointAppPBX::action_dialing_forward, NULL,
279 PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_DELAY,
280 "Caller changes the diversion of given type to the given destination or voice box."},
282 "redial", &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_redial, NULL,
283 PARAM_CONNECT | PARAM_SELECT,
284 "Caller redials. (last outgoing call(s))"},
286 "reply", &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_reply, NULL,
287 PARAM_CONNECT | PARAM_SELECT,
288 "Caller replies. (last incoming call(s))"},
290 "powerdial", NULL, &EndpointAppPBX::action_dialing_powerdial, NULL,
291 PARAM_CONNECT | PARAM_DELAY | PARAM_LIMIT | PARAM_TIMEOUT,
292 "Caller redials using powerdialing."},
294 "callback", NULL, &EndpointAppPBX::action_dialing_callback, &EndpointAppPBX::action_hangup_callback,
295 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_EXTENSION | PARAM_DELAY | PARAM_CALLTO | PARAM_PREFIX,
296 "Caller will use the callback service. After disconnecting, the callback is triggered."},
298 "abbrev", NULL, &EndpointAppPBX::action_dialing_abbrev, NULL,
300 "Caller dials abbreviation."},
302 "test", NULL, &EndpointAppPBX::action_dialing_test, NULL,
303 PARAM_CONNECT | PARAM_PREFIX | PARAM_TIMEOUT,
304 "Caller dials test mode."},
306 "play", &EndpointAppPBX::action_init_play, NULL, NULL,
307 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_SAMPLE | PARAM_TIMEOUT,
308 "Plays the given sample."},
310 "vbox-play", &EndpointAppPBX::action_init_vbox_play, &EndpointAppPBX::action_dialing_vbox_play, NULL,
312 "Caller listens to her voice box or to given extension."},
314 "calculator", NULL, &EndpointAppPBX::action_dialing_calculator, NULL,
316 "Caller calls the calculator."},
318 "timer", NULL, &EndpointAppPBX::action_dialing_timer, NULL,
319 PARAM_CONNECT | PARAM_TPRESET | PARAM_TIMEOUT,
321 // "Caller calls the timer."},
323 "goto", NULL, &EndpointAppPBX::action_dialing_goto, NULL,
324 PARAM_CONNECT | PARAM_RULESET | PARAM_STRIP | PARAM_SAMPLE,
325 "Jump to given ruleset and optionally play sample. Dialed digits are not flushed."},
327 "menu", NULL, &EndpointAppPBX::action_dialing_menu, NULL,
328 PARAM_CONNECT | PARAM_RULESET | PARAM_SAMPLE,
329 "Same as 'goto', but flushes all digits dialed so far."},
331 "disconnect", NULL, &EndpointAppPBX::action_dialing_disconnect, NULL,
332 PARAM_CONNECT | PARAM_CAUSE | PARAM_LOCATION | PARAM_SAMPLE | PARAM_DISPLAY,
333 "Caller gets disconnected optionally with given cause and given sample and given display text."},
335 "deflect", NULL, &EndpointAppPBX::action_dialing_deflect, NULL,
338 // "External call is deflected to the given destination within the telephone network."},
340 "setforward", NULL, &EndpointAppPBX::action_dialing_setforward, NULL,
341 PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_PORT,
343 // "The call forward is set within the telephone network of the external line."},
345 "execute", NULL, NULL, &EndpointAppPBX::action_hangup_execute,
346 PARAM_CONNECT | PARAM_EXECUTE | PARAM_PARAM,
347 "Executes the given script file. The file must terminate quickly, because it will halt the PBX."},
349 "file", NULL, NULL, &EndpointAppPBX::action_hangup_file,
350 PARAM_CONNECT | PARAM_FILE | PARAM_CONTENT | PARAM_APPEND,
351 "Writes givent content to given file. If content is not given, the dialed digits are written."},
353 "pick", &EndpointAppPBX::action_init_pick, NULL, NULL,
355 "Pick up a call that is ringing on any phone. Extensions may be given to limit the picking ability."},
357 "password", NULL, &EndpointAppPBX::action_dialing_password, NULL,
360 { ACTION_PASSWORD_WRITE,
361 "password_wr",NULL, &EndpointAppPBX::action_dialing_password_wr, NULL,
365 "nothing", NULL, NULL, NULL,
366 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_TIMEOUT,
367 "does nothing. Usefull to wait for calls to be released completely, by giving timeout value."},
369 "efi", &EndpointAppPBX::action_init_efi, NULL, NULL,
370 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT,
371 "Elektronische Fernsprecher Identifikation - announces caller ID."},
373 NULL, NULL, NULL, NULL, 0, NULL}
377 /* display documentation of rules */
379 void doc_rules(const char *name)
386 while(action_defs[i].name)
388 if (!strcasecmp(action_defs[i].name, name))
392 if (!action_defs[i].name)
394 fprintf(stderr, "Given action '%s' unknown.\n", name);
397 name = action_defs[i].name;
400 printf("Syntax overview:\n");
401 printf("----------------\n\n");
402 printf("[ruleset]\n");
403 printf("<condition> ... : <action> [parameter ...] [timeout=X : <action> ...]\n");
405 printf("Please refer to the documentation for description on rule format.\n\n");
409 printf("Available conditions to match:\n");
410 printf("------------------------------\n\n");
412 while(cond_defs[i].name)
414 printf("Usage: %s\n", cond_defs[i].doc);
415 printf("%s\n\n", cond_defs[i].help);
419 printf("Available actions with their parameters:\n");
420 printf("----------------------------------------\n\n");
423 printf("Detailes parameter description of action:\n");
424 printf("-----------------------------------------\n\n");
427 while(action_defs[i].name)
429 if (name && !!strcmp(action_defs[i].name,name)) /* not selected */
434 if (!action_defs[i].help) /* not internal actions */
439 printf("Usage: %s", action_defs[i].name);
443 if ((1LL<<j) & action_defs[i].params)
444 printf(" [%s]", param_defs[j].doc);
447 printf("\n%s\n\n", action_defs[i].help);
448 if (name) /* only show parameter help for specific action */
453 if ((1LL<<j) & action_defs[i].params)
454 printf("%s:\n\t%s\n", param_defs[j].doc, param_defs[j].help);
463 void ruleset_free(struct route_ruleset *ruleset_start)
465 struct route_ruleset *ruleset;
466 struct route_rule *rule;
467 struct route_cond *cond;
468 struct route_action *action;
469 struct route_param *param;
473 ruleset = ruleset_start;
474 ruleset_start = ruleset->next;
475 while(ruleset->rule_first)
477 rule = ruleset->rule_first;
478 ruleset->rule_first = rule->next;
479 while(rule->cond_first)
481 cond = rule->cond_first;
482 if (cond->string_value)
484 FREE(cond->string_value, 0);
487 if (cond->string_value_to)
489 FREE(cond->string_value_to, 0);
492 rule->cond_first = cond->next;
493 FREE(cond, sizeof(struct route_cond));
496 while(rule->action_first)
498 action = rule->action_first;
499 rule->action_first = action->next;
500 while(action->param_first)
502 param = action->param_first;
503 action->param_first = param->next;
504 if (param->string_value)
506 FREE(param->string_value, 0);
509 FREE(param, sizeof(struct route_param));
512 FREE(action, sizeof(struct route_action));
515 FREE(rule, sizeof(struct route_rule));
518 FREE(ruleset, sizeof(struct route_ruleset));
523 void ruleset_debug(struct route_ruleset *ruleset_start)
525 struct route_ruleset *ruleset;
526 struct route_rule *rule;
527 struct route_cond *cond;
528 struct route_action *action;
529 struct route_param *param;
532 ruleset = ruleset_start;
535 printf("Ruleset: '%s'\n", ruleset->name);
536 rule = ruleset->rule_first;
541 cond = rule->cond_first;
545 printf(" Condition:");
549 printf(" %s", cond_defs[cond->index].name);
550 if (cond->value_type != VALUE_TYPE_NULL)
553 switch(cond->value_type)
555 case VALUE_TYPE_NULL:
558 case VALUE_TYPE_INTEGER:
559 printf("%d", cond->integer_value);
562 case VALUE_TYPE_INTEGER_RANGE:
563 printf("%d-%d", cond->integer_value, cond->integer_value_to);
566 case VALUE_TYPE_STRING:
567 printf("'%s'", cond->string_value);
570 case VALUE_TYPE_STRING_RANGE:
571 printf("'%s'-'%s'", cond->string_value, cond->string_value_to);
575 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", cond->value_type, __FUNCTION__, __LINE__);
577 if (cond->value_extension && cond->next)
581 goto next_cond_value;
589 action = rule->action_first;
592 printf(" Action: %s\n", action_defs[action->index].name);
595 param = action->param_first;
603 printf(" %s", param_defs[param->index].name);
604 if (param->value_type != VALUE_TYPE_NULL)
606 switch(param->value_type)
608 case VALUE_TYPE_NULL:
611 case VALUE_TYPE_INTEGER:
612 if (param_defs[param->index].type == PARAM_TYPE_CALLERIDTYPE)
614 switch(param->integer_value)
616 case INFO_NTYPE_UNKNOWN:
619 case INFO_NTYPE_SUBSCRIBER:
620 printf("subscriber");
622 case INFO_NTYPE_NATIONAL:
625 case INFO_NTYPE_INTERNATIONAL:
626 printf("international");
629 printf("unknown(%d)", param->integer_value);
633 if (param_defs[param->index].type == PARAM_TYPE_CAPABILITY)
635 switch(param->integer_value)
646 case INFO_BC_DATARESTRICTED:
647 printf("digital-restricted");
649 case INFO_BC_DATAUNRESTRICTED:
650 printf("digital-unrestricted");
652 case INFO_BC_DATAUNRESTRICTED_TONES:
653 printf("digital-unrestricted-tones");
656 printf("unknown(%d)", param->integer_value);
660 if (param_defs[param->index].type == PARAM_TYPE_DIVERSION)
662 switch(param->integer_value)
664 case INFO_DIVERSION_CFU:
667 case INFO_DIVERSION_CFNR:
670 case INFO_DIVERSION_CFB:
673 case INFO_DIVERSION_CFP:
677 printf("unknown(%d)", param->integer_value);
681 if (param_defs[param->index].type == PARAM_TYPE_TYPE)
683 switch(param->integer_value)
685 case INFO_NTYPE_UNKNOWN:
688 case INFO_NTYPE_SUBSCRIBER:
689 printf("subscriber");
691 case INFO_NTYPE_NATIONAL:
694 case INFO_NTYPE_INTERNATIONAL:
695 printf("international");
698 printf("unknown(%d)", param->integer_value);
702 if (param_defs[param->index].type == PARAM_TYPE_YESNO)
704 switch(param->integer_value)
713 printf("unknown(%d)", param->integer_value);
717 if (param_defs[param->index].type == PARAM_TYPE_NULL)
721 printf("%d", param->integer_value);
724 case VALUE_TYPE_STRING:
725 printf("'%s'", param->string_value);
729 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", param->value_type, __FUNCTION__, __LINE__);
736 printf(" Timeout: %d\n", action->timeout);
737 action = action->next;
743 ruleset = ruleset->next;
751 static char *read_string(char *p, char *key, int key_size, char *special)
777 UPRINT(key, "\001String too long.");
782 UPRINT(key, "\001Unexpected end of line inside quotes.");
789 if (strchr(special, *p))
799 UPRINT(key, "\001Unexpected end of line.");
805 UPRINT(key, "\001String too long.");
813 char ruleset_error[256];
814 struct route_ruleset *ruleset_parse(void)
819 unsigned long long j;
823 FILE *fp[MAXNESTING];
824 char filename[MAXNESTING][256];
825 int line[MAXNESTING];
832 int expecting = 1; /* 1 = expecting ruleset */
836 integer_to; /* condition index, .. */
837 struct route_ruleset *ruleset_start = NULL, *ruleset;
838 struct route_ruleset **ruleset_pointer = &ruleset_start;
839 struct route_rule *rule;
840 struct route_rule **rule_pointer = NULL;
841 struct route_cond *cond;
842 struct route_cond **cond_pointer = NULL;
843 struct route_action *action;
844 struct route_action **action_pointer = NULL;
845 struct route_param *param;
846 struct route_param **param_pointer = NULL;
848 unsigned long long allowed_params;
850 /* check the integrity of IDs for ACTION_* and PARAM_* */
852 while(action_defs[i].name)
854 if (action_defs[i].id != i)
856 PERROR("Software Error action '%s' must have id of %d, but has %d.\n",
857 action_defs[i].name, i, action_defs[i].id);
863 while(param_defs[i].name)
865 if (param_defs[i].id != j)
867 PERROR("Software Error param '%s' must have id of 0x%llx, but has 0x%llx.\n",
868 param_defs[i].name, j, param_defs[i].id);
875 SPRINT(filename[0], "%s/routing.conf", INSTALL_DATA);
877 if (!(fp[0]=fopen(filename[0],"r")))
879 PERROR("Cannot open %s\n",filename[0]);
888 while((fgets(buffer,sizeof(buffer),fp[nesting])))
891 buffer[sizeof(buffer)-1]=0;
892 if (buffer[0]) buffer[strlen(buffer)-1]=0;
903 /* skip spaces, if any */
915 /* don't skip "define" */
916 if (!!strncmp(p, "define", 6))
929 p = read_string(p, key, sizeof(key), " ");
930 if (key[0] == 1) /* error */
932 SPRINT(failure, "Parsing Filename failed: %s", key+1);
935 if (nesting == MAXNESTING-1)
937 SPRINT(failure, "'include' is nesting too deep.\n");
941 SCPY(filename[nesting+1], key);
943 SPRINT(filename[nesting+1], "%s/%s", INSTALL_DATA, key);
944 if (!(fp[nesting+1]=fopen(filename[nesting+1],"r")))
946 PERROR("Cannot open %s\n", filename[nesting+1]);
953 if (*p == '/') if (p[1] == '/')
956 /* skip empty lines */
960 /* expecting ruleset */
967 SPRINT(failure, "Expecting ruleset name starting with '['.");
972 /* reading ruleset name text */
974 while(*p>' ' && *p<127 && *p!=']')
976 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
978 if (i == sizeof(key)) i--; /* limit */
981 if (key[0] == '\0') {
982 SPRINT(failure, "Missing ruleset name after '['.");
986 /* expecting ] and nothing more */
988 SPRINT(failure, "Expecting ']' after ruleset name.");
993 SPRINT(failure, "Unexpected character after ruleset name.");
997 /* check for duplicate rulesets */
998 ruleset = ruleset_start;
1001 if (!strcmp(ruleset->name, key))
1003 SPRINT(failure, "Duplicate ruleset '%s', already defined in file '%s' line %d.", key, ruleset->file, ruleset->line);
1006 ruleset = ruleset->next;
1009 /* create ruleset */
1010 ruleset = (struct route_ruleset *)MALLOC(sizeof(struct route_ruleset));
1012 *ruleset_pointer = ruleset;
1013 ruleset_pointer = &(ruleset->next);
1014 SCPY(ruleset->name, key);
1015 SCPY(ruleset->file, filename[nesting]);
1016 ruleset->line = line[nesting];
1017 rule_pointer = &(ruleset->rule_first);
1022 /* for new ruleset [ */
1028 /* Alloc memory for rule */
1029 rule = (struct route_rule *)MALLOC(sizeof(struct route_rule));
1031 *rule_pointer = rule;
1032 rule_pointer = &(rule->next);
1033 cond_pointer = &(rule->cond_first);
1034 action_pointer = &(rule->action_first);
1035 SCPY(rule->file, filename[nesting]);
1036 rule->line = line[nesting];
1038 /* loop CONDITIONS */
1039 while(*p!=':' && *p!='\0')
1041 /* read item text */
1043 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1045 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1047 if (i == sizeof(key)) i--; /* limit */
1052 SPRINT(failure, "Expecting condition item name or ':' for end of condition list.");
1055 if (*p!=' ' && *p!='=')
1057 SPRINT(failure, "Illegal character '%c' after condition name '%s'. Expecting '=' for equation or ' ' to seperate condition items.", *p, key);
1061 /* check if condition exists */
1063 while(cond_defs[index].name)
1065 if (!strcmp(cond_defs[index].name, key))
1069 if (cond_defs[index].name == NULL)
1071 SPRINT(failure, "Unknown condition item name '%s'.", key);
1075 /* items without values must not have any parameter */
1076 if (cond_defs[index].type == COND_TYPE_NULL)
1080 SPRINT(failure, "Condition item '%s' must not have any value. Don't use '=' for this type of condition.", key);
1085 SPRINT(failure, "Condition item '%s' must not have any value. Expecting ' ' or tab after item name.", key);
1093 SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected, and not a space.", key);
1098 SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected.", key);
1104 /* check for duplicate condition */
1105 cond = rule->cond_first;
1108 if (cond->index == index)
1110 SPRINT(failure, "Duplicate condition '%s', use ',' to give multiple values.", key);
1117 /* Alloc memory for item */
1118 cond = (struct route_cond *)MALLOC(sizeof(struct route_cond));
1120 *cond_pointer = cond;
1121 cond_pointer = &(cond->next);
1122 cond->index = index;
1123 cond->match = cond_defs[index].match;
1124 switch(cond_defs[index].type)
1126 case COND_TYPE_NULL:
1129 SPRINT(failure, "Expecting no value.");
1132 value_type = VALUE_TYPE_NULL;
1135 /* parse all integer values/ranges */
1136 case COND_TYPE_INTEGER:
1137 case COND_TYPE_TIME:
1138 case COND_TYPE_MDAY:
1139 case COND_TYPE_MONTH:
1140 case COND_TYPE_WDAY:
1141 case COND_TYPE_YEAR:
1142 integer = integer_to = 0;
1143 if (*p==',' || *p==' ' || *p=='\0')
1145 SPRINT(failure, "Missing integer value.");
1148 while(*p>='0' && *p<='9')
1150 integer = integer*10 + *p-'0';
1153 value_type = VALUE_TYPE_INTEGER;
1157 if (*p==',' || *p==' ' || *p=='\0')
1159 SPRINT(failure, "Missing integer value.");
1162 while(*p>='0' && *p<='9')
1164 integer_to = integer_to*10 + *p-'0';
1167 value_type = VALUE_TYPE_INTEGER_RANGE;
1169 if (cond_defs[index].type == COND_TYPE_TIME)
1171 // Simon: i store the time as decimal, later i compare it correctly:
1172 // hours * 100 + minutes
1173 if (integer == 2400)
1175 if (integer >= 2400)
1178 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer);
1181 if (integer%100 >= 60)
1182 goto timeoutofrange1;
1183 if (value_type == VALUE_TYPE_INTEGER)
1185 if (integer_to == 2400)
1187 if (integer_to >= 2400)
1190 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer_to);
1193 if (integer_to%100 >= 60)
1194 goto timeoutofrange2;
1196 if (cond_defs[index].type == COND_TYPE_MDAY)
1198 if (integer<1 || integer>31)
1200 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer);
1203 if (value_type == VALUE_TYPE_INTEGER)
1205 if (integer_to<1 || integer_to>31)
1207 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer_to);
1211 if (cond_defs[index].type == COND_TYPE_WDAY)
1213 if (integer<1 || integer>7)
1215 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer);
1218 if (value_type == VALUE_TYPE_INTEGER)
1220 if (integer_to<1 || integer_to>7)
1222 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer_to);
1226 if (cond_defs[index].type == COND_TYPE_MONTH)
1228 if (integer<1 || integer>12)
1230 SPRINT(failure, "Given month '%d' not in range 1..12", integer);
1233 if (value_type == VALUE_TYPE_INTEGER)
1235 if (integer_to<1 || integer_to>12)
1237 SPRINT(failure, "Given month '%d' not in range 1..12", integer_to);
1241 if (cond_defs[index].type == COND_TYPE_YEAR)
1243 if (integer<1970 || integer>2106)
1245 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer);
1248 if (value_type == VALUE_TYPE_INTEGER)
1250 if (integer_to<1970 || integer_to>2106)
1252 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer_to);
1257 cond->integer_value = integer;
1258 cond->integer_value_to = integer_to;
1259 cond->value_type = value_type;
1262 /* parse all string values/ranges */
1263 case COND_TYPE_STRING:
1264 key[0] = key_to[0] = '\0';
1265 if (*p==',' || *p==' ' || *p=='\0')
1267 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1270 p = read_string(p, key, sizeof(key), "-, ");
1271 if (key[0] == 1) /* error */
1273 SPRINT(failure, "Parsing String failed: %s", key+1);
1276 value_type = VALUE_TYPE_STRING;
1280 if (*p==',' || *p==' ' || *p=='\0')
1282 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1285 p = read_string(p, key_to, sizeof(key_to), "-, ");
1286 if (key_to[0] == 1) /* error */
1288 SPRINT(failure, "Parsing string failed: %s", key_to+1);
1291 value_type = VALUE_TYPE_STRING_RANGE;
1292 if (strlen(key) != strlen(key_to))
1294 SPRINT(failure, "Given range of strings \"%s\"-\"%s\" have unequal length.", key, key_to);
1299 SPRINT(failure, "Given range has no length.");
1304 cond->string_value = (char *)MALLOC(strlen(key)+1);
1306 UCPY(cond->string_value, key);
1307 if (value_type == VALUE_TYPE_STRING_RANGE)
1309 cond->string_value_to = (char *)MALLOC(strlen(key_to)+1);
1311 UCPY(cond->string_value_to, key_to);
1312 cond->comp_string = strcmp(key, key_to);
1314 cond->value_type = value_type;
1317 /* parse service value */
1318 case COND_TYPE_CAPABILITY:
1319 if (!strncasecmp("speech", p, 6))
1320 cond->integer_value = INFO_BC_SPEECH;
1321 else if (!strncasecmp("audio", p, 5))
1322 cond->integer_value = INFO_BC_AUDIO;
1323 else if (!strncasecmp("video", p, 5))
1324 cond->integer_value = INFO_BC_VIDEO;
1325 else if (!strncasecmp("digital-restricted", p, 18))
1326 cond->integer_value = INFO_BC_DATARESTRICTED;
1327 else if (!strncasecmp("digital-unrestricted", p, 20))
1328 cond->integer_value = INFO_BC_DATAUNRESTRICTED;
1329 else if (!strncasecmp("digital-unrestricted-tones", p, 26))
1330 cond->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1333 SPRINT(failure, "Given service type is invalid or misspelled.");
1336 cond->value_type = VALUE_TYPE_INTEGER;
1339 /* parse bmode value */
1340 case COND_TYPE_BMODE:
1341 if (!strncasecmp("transparent", p, 11))
1342 cond->integer_value = INFO_BMODE_CIRCUIT;
1343 else if (!strncasecmp("hdlc", p, 4))
1344 cond->integer_value = INFO_BMODE_PACKET;
1347 SPRINT(failure, "Given bchannel mode is invalid or misspelled.");
1350 cond->value_type = VALUE_TYPE_INTEGER;
1353 /* parse interface attribute <if>:<value> */
1354 case COND_TYPE_IFATTR:
1355 key[0] = key_to[0] = '\0';
1356 if (*p==':' || *p==',' || *p==' ' || *p=='\0')
1358 SPRINT(failure, "Missing interface name.");
1361 p = read_string(p, key, sizeof(key), ":-, ");
1362 if (key[0] == 1) /* error */
1364 SPRINT(failure, "Parsing interface failed: %s", key+1);
1369 SPRINT(failure, "Expeciting kolon to seperate value behind interface name.");
1373 while(*p>='0' && *p<='9')
1377 if (*p!=',' && *p!=' ' && *p!='\0')
1379 SPRINT(failure, "Invalid characters behind value.");
1382 value_type = VALUE_TYPE_STRING;
1387 SPRINT(failure, "Software error: COND_TYPE_* %d not parsed in function '%s'", cond_defs[index].type, __FUNCTION__);
1390 /* if we have another value for that item, we attach it */
1395 cond->value_extension = 1;
1398 /* to seperate the items, a space is required */
1401 SPRINT(failure, "Character '%c' not expected here. Use ',' to seperate multiple possible values.", *p);
1413 /* we are done with CONDITIONS, so we expect the ACTION */
1416 SPRINT(failure, "Expecting ':' after condition item(s).");
1422 /* skip spaces, if any */
1430 /* read action name */
1432 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9') || *p == '-')
1434 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1436 if (i == sizeof(key)) i--; /* limit */
1439 if (key[0] == '\0') {
1440 SPRINT(failure, "Expecting action name.");
1444 /* check if item exists */
1446 while(action_defs[index].name)
1448 if (!action_defs[index].help) /* not internal actions */
1453 if (!strcmp(action_defs[index].name, key))
1457 if (action_defs[index].name == NULL)
1459 SPRINT(failure, "Unknown action name '%s'.", key);
1462 allowed_params = action_defs[index].params;
1464 /* alloc memory for action */
1465 action = (struct route_action *)MALLOC(sizeof(struct route_action));
1467 *action_pointer = action;
1468 action_pointer = &(action->next);
1469 param_pointer = &(action->param_first);
1470 action->index = index;
1471 action->line = line[nesting];
1473 /* skip spaces after param name */
1484 /* read param text */
1486 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1488 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1490 if (i == sizeof(key)) i--; /* limit */
1493 if (key[0] == '\0') {
1494 SPRINT(failure, "Expecting parameter name.");
1498 /* check if item exists */
1500 while(param_defs[index].name)
1502 if (!strcmp(param_defs[index].name, key))
1506 if (param_defs[index].name == NULL)
1508 SPRINT(failure, "Unknown param name '%s'.", key);
1512 /* check if item is allowed for the action */
1513 if (!(param_defs[index].id & allowed_params))
1515 SPRINT(failure, "Param name '%s' exists, but not for this action.", key);
1519 /* params without values must not have any parameter */
1520 if (param_defs[index].type == PARAM_TYPE_NULL)
1522 if (*p!=' ' && *p!='\0')
1524 SPRINT(failure, "Parameter '%s' must not have any value.", key);
1531 SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected and not a space.", key);
1536 SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected.", key);
1542 /* special timeout value */
1543 if (!strcmp("timeout", key))
1545 if (action->timeout)
1547 SPRINT(failure, "Duplicate timeout value.");
1550 if (*p==',' || *p==' ' || *p=='\0')
1552 SPRINT(failure, "Missing integer value.");
1556 while(*p>='0' && *p<='9')
1558 integer = integer*10 + *p-'0';
1563 SPRINT(failure, "Expecting timeout value greater 0.");
1566 if (*p!=' ' && *p!='\0')
1568 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters.", *p);
1578 action->timeout = integer;
1579 /* check for next ACTION */
1588 /* check for duplicate parameters */
1589 param = action->param_first;
1592 if (param->index == index)
1594 SPRINT(failure, "Duplicate parameter '%s', use ',' to give multiple values.", key);
1597 param = param->next;
1601 /* Alloc memory for param */
1602 param = (struct route_param *)MALLOC(sizeof(struct route_param));
1604 *param_pointer = param;
1605 param_pointer = &(param->next);
1606 param->index = index;
1607 param->id = param_defs[index].id;
1609 switch(param_defs[index].type)
1611 /* parse null value */
1612 case PARAM_TYPE_NULL:
1613 param->value_type = VALUE_TYPE_NULL;
1616 /* parse integer value */
1617 case PARAM_TYPE_INTEGER:
1619 if (*p==',' || *p==' ' || *p=='\0')
1621 SPRINT(failure, "Missing integer value.");
1624 while(*p>='0' && *p<='9')
1626 integer = integer*10 + *p-'0';
1629 param->integer_value = integer;
1630 param->value_type = VALUE_TYPE_INTEGER;
1634 /* parse ports value */
1635 case PARAM_TYPE_PORTS:
1637 if (*p==',' || *p==' ' || *p=='\0')
1639 SPRINT(failure, "Missing port number, omit parameter or give port number.");
1645 while(*p>='0' && *p<='9')
1647 if (i < (int)sizeof(key)-1)
1652 integer = integer*10 + *p-'0';
1657 SPRINT(failure, "Port number too high.");
1662 if (i < (int)sizeof(key)-1)
1673 /* parse string value */
1674 case PARAM_TYPE_STRING:
1675 case PARAM_TYPE_CALLERIDTYPE:
1676 case PARAM_TYPE_CAPABILITY:
1677 case PARAM_TYPE_BMODE:
1678 case PARAM_TYPE_DIVERSION:
1679 case PARAM_TYPE_DESTIN:
1680 case PARAM_TYPE_TYPE:
1681 case PARAM_TYPE_YESNO:
1683 if (*p==',' || *p==' ' || *p=='\0')
1685 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1688 p = read_string(p, key, sizeof(key), " ");
1689 if (key[0] == 1) /* error */
1691 SPRINT(failure, "Parsing string failed: %s", key+1);
1694 if (param_defs[index].type == PARAM_TYPE_CALLERIDTYPE)
1696 param->value_type = VALUE_TYPE_INTEGER;
1697 if (!strcasecmp(key, "unknown"))
1699 param->integer_value = INFO_NTYPE_UNKNOWN;
1702 if (!strcasecmp(key, "subscriber"))
1704 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1707 if (!strcasecmp(key, "national"))
1709 param->integer_value = INFO_NTYPE_NATIONAL;
1712 if (!strcasecmp(key, "international"))
1714 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1717 SPRINT(failure, "Caller ID type '%s' unknown.", key);
1720 if (param_defs[index].type == PARAM_TYPE_CAPABILITY)
1722 param->value_type = VALUE_TYPE_INTEGER;
1723 if (!strcasecmp(key, "speech"))
1725 param->integer_value = INFO_BC_SPEECH;
1728 if (!strcasecmp(key, "audio"))
1730 param->integer_value = INFO_BC_AUDIO;
1733 if (!strcasecmp(key, "video"))
1735 param->integer_value = INFO_BC_VIDEO;
1738 if (!strcasecmp(key, "digital-restricted"))
1740 param->integer_value = INFO_BC_DATARESTRICTED;
1743 if (!strcasecmp(key, "digital-unrestricted"))
1745 param->integer_value = INFO_BC_DATAUNRESTRICTED;
1748 if (!strcasecmp(key, "digital-unrestricted-tones"))
1750 param->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1753 SPRINT(failure, "Service type '%s' unknown.", key);
1756 if (param_defs[index].type == PARAM_TYPE_BMODE)
1758 param->value_type = VALUE_TYPE_INTEGER;
1759 if (!strcasecmp(key, "transparent"))
1761 param->integer_value = INFO_BMODE_CIRCUIT;
1764 if (!strcasecmp(key, "hdlc"))
1766 param->integer_value = INFO_BMODE_PACKET;
1769 SPRINT(failure, "Bchannel mode '%s' unknown.", key);
1772 if (param_defs[index].type == PARAM_TYPE_DIVERSION)
1774 param->value_type = VALUE_TYPE_INTEGER;
1775 if (!strcasecmp(key, "cfu"))
1777 param->integer_value = INFO_DIVERSION_CFU;
1780 if (!strcasecmp(key, "cfb"))
1782 param->integer_value = INFO_DIVERSION_CFB;
1785 if (!strcasecmp(key, "cfnr"))
1787 param->integer_value = INFO_DIVERSION_CFNR;
1790 if (!strcasecmp(key, "cfp"))
1792 param->integer_value = INFO_DIVERSION_CFP;
1795 SPRINT(failure, "Diversion type '%s' unknown.", key);
1798 if (param_defs[index].type == PARAM_TYPE_TYPE)
1800 param->value_type = VALUE_TYPE_INTEGER;
1801 if (!strcasecmp(key, "unknown"))
1803 param->integer_value = INFO_NTYPE_UNKNOWN;
1806 if (!strcasecmp(key, "subscriber"))
1808 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1811 if (!strcasecmp(key, "national"))
1813 param->integer_value = INFO_NTYPE_NATIONAL;
1816 if (!strcasecmp(key, "international"))
1818 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1821 SPRINT(failure, "Number type '%s' unknown.", key);
1824 if (param_defs[index].type == PARAM_TYPE_YESNO)
1826 param->value_type = VALUE_TYPE_INTEGER;
1827 if (!strcasecmp(key, "yes"))
1829 param->integer_value = 1;
1832 if (!strcasecmp(key, "no"))
1834 param->integer_value = 0;
1837 SPRINT(failure, "Value '%s' unknown. ('yes' or 'no')", key);
1840 param->string_value = (char *)MALLOC(strlen(key)+1);
1842 UCPY(param->string_value, key);
1843 param->value_type = VALUE_TYPE_STRING;
1847 SPRINT(failure, "Software error: PARAM_TYPE_* %d not parsed in function '%s'", param_defs[index].type, __FUNCTION__);
1855 param->value_extension = 1;
1856 goto nextparamvalue;
1863 /* to seperate the items, a space is required */
1866 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters, or ',' for multiple values.", *p);
1877 /* check for next ACTION */
1886 fclose(fp[nesting--]);
1894 SPRINT(failure, "No ruleset defined.");
1896 return(ruleset_start);
1899 printf("While parsing %s, an error occurred in line %d:\n", filename[nesting], line[nesting]);
1900 printf("-> %s\n", buffer);
1901 memset(pointer, ' ', sizeof(pointer));
1902 pointer[p-buffer] = '^';
1903 pointer[p-buffer+1] = '\0';
1904 printf(" %s\n", pointer);
1905 printf("%s\n", failure);
1906 SPRINT(ruleset_error, "Error in file %s, line %d: %s", filename[nesting], line[nesting], failure);
1911 fclose(fp[nesting--]);
1915 ruleset_free(ruleset_start);
1920 * return ruleset by name
1922 struct route_ruleset *getrulesetbyname(char *name)
1924 struct route_ruleset *ruleset = ruleset_first;
1928 if (!strcasecmp(name, ruleset->name))
1932 ruleset = ruleset->next;
1934 PDEBUG(DEBUG_ROUTE, "ruleset %s %s.\n", name, ruleset?"found":"not found");
1939 * parses the current ruleset and returns action
1941 struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
1944 couldmatch = 0, /* any rule could match */
1951 struct route_rule *rule = ruleset->rule_first;
1952 struct route_cond *cond;
1953 struct route_action *action = NULL;
1954 unsigned long comp_len;
1956 char callerid[64], redirid[64];
1961 struct mISDNport *mISDNport;
1962 struct admin_list *admin;
1964 /* reset timeout action */
1965 e_match_timeout = 0; /* no timeout */
1966 e_match_to_action = NULL;
1968 SCPY(callerid, numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international));
1969 SCPY(redirid, numberrize_callerinfo(e_redirinfo.id, e_redirinfo.ntype, options.national, options.international));
1971 PDEBUG(DEBUG_ROUTE, "parsing ruleset '%s'\n", ruleset->name);
1974 PDEBUG(DEBUG_ROUTE, "checking rule in line %d\n", rule->line);
1975 match = 1; /* this rule matches */
1976 dialing_required = 0;
1977 timeout = 0; /* timeout time */
1978 cond = rule->cond_first;
1981 condition = 0; /* any condition element is true (1) or could be true (2) */
1983 istrue = 0; /* this condition-element is true */
1984 couldbetrue = 0; /* this conditions-element could be true */
1988 if (!e_ext.number[0])
1993 if (e_ext.number[0])
1998 if (ea_endpoint->ep_portlist)
1999 if ((ea_endpoint->ep_portlist->port_type & PORT_CLASS_mISDN_MASK) != PORT_CLASS_mISDN_DSS1)
2001 integer = e_callerinfo.isdn_port;
2004 case MATCH_INTERFACE:
2005 if (!e_callerinfo.interface[0])
2007 string = e_callerinfo.interface;
2008 goto match_string_prefix;
2010 case MATCH_CALLERID:
2012 goto match_string_prefix;
2014 case MATCH_EXTENSION:
2015 string = e_ext.name;
2019 string = e_dialinginfo.id;
2020 goto match_string_prefix;
2032 case MATCH_ANONYMOUS:
2033 if (e_callerinfo.present != INFO_PRESENT_ALLOWED)
2038 if (e_callerinfo.present == INFO_PRESENT_ALLOWED)
2043 if (e_callerinfo.present == INFO_PRESENT_NOTAVAIL)
2047 case MATCH_AVAILABLE:
2048 if (e_callerinfo.present != INFO_PRESENT_NOTAVAIL)
2053 if (e_callerinfo.screen == INFO_SCREEN_USER)
2058 if (e_callerinfo.screen != INFO_SCREEN_USER)
2062 case MATCH_REDIRECTED:
2063 if (e_redirinfo.present != INFO_PRESENT_NULL)
2068 if (e_redirinfo.present == INFO_PRESENT_NULL)
2074 goto match_string_prefix;
2077 integer = now_tm->tm_hour*100 + now_tm->tm_min;
2081 integer = now_tm->tm_mday;
2085 integer = now_tm->tm_mon+1;
2089 integer = now_tm->tm_year + 1900;
2093 integer = now_tm->tm_wday;
2094 integer = integer?integer:7; /* correct sunday */
2097 case MATCH_CAPABILITY:
2098 integer = e_capainfo.bearer_capa;
2101 case MATCH_INFOLAYER1:
2102 integer = e_capainfo.bearer_info1;
2106 integer = e_capainfo.hlc;
2110 tfp = fopen(cond->string_value, "r");
2115 if (fgetc(tfp) == '1')
2121 if (system(cond->string_value) == 0)
2131 timeout = now_d + cond->integer_value;
2137 if (!(comp_len = (unsigned long)strchr(cond->string_value, ':')))
2139 comp_len = comp_len-(unsigned long)cond->string_value;
2141 mISDNport = mISDNport_first;
2144 if (mISDNport->ifport)
2145 if (strlen(mISDNport->ifport->interface->name) == comp_len)
2146 if (!strncasecmp(mISDNport->ifport->interface->name, cond->string_value, comp_len))
2147 if (!mISDNport->l2hold || mISDNport->l2link)
2150 jj = mISDNport->b_num;
2154 if (mISDNport->b_state[j])
2159 mISDNport = mISDNport->next;
2161 if (cond->match == MATCH_FREE)
2163 if (avail >= atoi(cond->string_value + comp_len + 1))
2167 if (avail < atoi(cond->string_value + comp_len + 1))
2174 mISDNport = mISDNport_first;
2177 if (mISDNport->ifport)
2178 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2179 if (!mISDNport->l2hold || mISDNport->l2link)
2181 mISDNport = mISDNport->next;
2183 if (!mISDNport) /* all down */
2188 mISDNport = mISDNport_first;
2191 if (mISDNport->ifport)
2192 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2193 if (!mISDNport->l2hold || mISDNport->l2link)
2196 mISDNport = mISDNport->next;
2198 if (mISDNport) /* one link at least */
2205 mISDNport = mISDNport_first;
2208 if (mISDNport->ifport)
2209 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2210 if (mISDNport->use) /* break if in use */
2212 mISDNport = mISDNport->next;
2214 if (mISDNport && cond->match==MATCH_BUSY)
2216 if (!mISDNport && cond->match==MATCH_IDLE)
2221 case MATCH_NOTREMOTE:
2222 admin = admin_first;
2225 if (admin->remote_name[0] && !strcmp(cond->string_value, admin->remote_name))
2227 admin = admin->next;
2229 if (admin && cond->match==MATCH_REMOTE)
2231 if (!admin && cond->match==MATCH_NOTREMOTE)
2236 PERROR("Software error: MATCH_* %d not parsed in function '%s'", cond->match, __FUNCTION__);
2240 if (cond->value_type == VALUE_TYPE_INTEGER)
2242 if (integer != cond->integer_value)
2247 if (cond->value_type == VALUE_TYPE_INTEGER_RANGE)
2249 /* check if negative range (2100 - 700 o'clock) */
2250 if (cond->integer_value > cond->integer_value_to)
2252 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2256 /* range is positive */
2257 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2264 if (strlen(cond->string_value) != strlen(string))
2267 match_string_prefix:
2268 comp_len = strlen(cond->string_value); /* because we must reach value's length */
2269 /* we must have greater or equal length to values */
2270 if ((unsigned long)strlen(string) < comp_len)
2272 /* special case for unfinished dialing */
2273 if (cond->match == MATCH_DIALING)
2275 couldbetrue = 1; /* could match */
2276 comp_len = strlen(string);
2282 /* on single string match */
2283 if (cond->value_type == VALUE_TYPE_STRING)
2285 if (!strncmp(string, cond->string_value, comp_len))
2288 /* must be set for changing 'e_extdialing' */
2289 if (cond->match == MATCH_DIALING)
2290 dialing_required = comp_len;
2295 /* on range match */
2296 if (cond->value_type == VALUE_TYPE_STRING_RANGE)
2298 /* check if negative range ("55"-"22") */
2299 if (cond->comp_string > 0)
2301 if (strncmp(string, cond->string_value, comp_len) >= 0)
2304 /* must be set for changing 'e_extdialing' */
2305 if (cond->match == MATCH_DIALING)
2306 dialing_required = comp_len;
2309 if (strncmp(string, cond->string_value_to, comp_len) <= 0)
2311 /* must be set for changing 'e_extdialing' */
2313 if (cond->match == MATCH_DIALING)
2314 dialing_required = comp_len;
2319 /* range is positive */
2320 if (strncmp(string, cond->string_value, comp_len) < 0)
2322 if (strncmp(string, cond->string_value_to, comp_len) > 0)
2325 if (cond->match == MATCH_DIALING)
2326 dialing_required = comp_len;
2332 /* set current condition */
2333 if (istrue && !couldbetrue)
2334 condition = 1; /* element matches, so condition matches */
2335 if (istrue && couldbetrue && !condition)
2336 condition = 2; /* element could match and other elements don't match, so condition could match */
2338 /* if not matching or could match */
2341 /* if we have more values to check */
2342 if (cond->value_extension && cond->next)
2345 goto checkextension;
2351 /* skip exteded values, beacuse we already have one matching value */
2352 while(cond->value_extension && cond->next)
2357 if (timeout>now_d && match==1) /* the matching rule with timeout in the future */
2358 if (e_match_timeout<1 || timeout<e_match_timeout) /* first timeout or lower */
2360 /* set timeout in the furture */
2361 e_match_timeout = timeout;
2362 e_match_to_action = rule->action_first;
2363 e_match_to_extdialing = e_dialinginfo.id + dialing_required;
2364 match = 0; /* matches in the future */
2368 /* matching, we return first action */
2369 action = rule->action_first;
2370 e_match_timeout = 0; /* no timeout */
2371 e_match_to_action = NULL;
2372 e_extdialing = e_dialinginfo.id + dialing_required;
2377 /* rule could match if more is dialed */
2386 * parses the current action's parameters and return them
2388 struct route_param *EndpointAppPBX::routeparam(struct route_action *action, unsigned long long id)
2390 struct route_param *param = action->param_first;
2394 if (param->id == id)
2396 param = param->next;
2403 * internal rules that are not defined by route.conf
2405 struct route_action action_password = {
2413 struct route_action action_password_write = {
2416 ACTION_PASSWORD_WRITE,
2421 struct route_action action_external = {
2429 struct route_action action_internal = {
2437 struct route_action action_remote = {
2445 struct route_action action_vbox = {
2453 struct route_action action_partyline = {
2461 struct route_action action_disconnect = {