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 { "callerid2", MATCH_CALLERID2,COND_TYPE_STRING,
30 "callerid2=<digits>[-<digits>][,...]", "Matches the second caller ID (network provided)."},
31 { "extension", MATCH_EXTENSION,COND_TYPE_STRING,
32 "extension=<digits>[-<digits>][,...]", "Matches if caller calls from given (range(s) of) extension(s)."},
33 { "dialing", MATCH_DIALING, COND_TYPE_STRING,
34 "dialing=<digits>[-<digits>][,...]", "Matches if caller has dialed the given (range(s) of) digits at least."},
35 { "enblock", MATCH_ENBLOCK, COND_TYPE_NULL,
36 "enblock", "Matches if caller dialed en block. (Dial the number before pick up.)"},
37 { "overlap", MATCH_OVERLAP, COND_TYPE_NULL,
38 "overlap", "Matches if caller dialed digit by digit. (Dial the number after pick up.)"},
39 { "anonymous", MATCH_ANONYMOUS,COND_TYPE_NULL,
40 "anonymous", "Matches if caller uses restricted caller ID or if not available."},
41 { "visible", MATCH_VISIBLE, COND_TYPE_NULL,
42 "visible", "Matches if caller ID is presented and if available."},
43 { "unknown", MATCH_UNKNOWN, COND_TYPE_NULL,
44 "unknown", "Matches if no ID is available from caller."},
45 { "available", MATCH_AVAILABLE,COND_TYPE_NULL,
46 "available", "Matches if ID is available from caller."},
47 { "fake", MATCH_FAKE, COND_TYPE_NULL,
48 "fake", "Matches if caller ID is not screened and may be faked by caller."},
49 { "real", MATCH_REAL, COND_TYPE_NULL,
50 "real", "Matches if caller ID is screend and so it is the real caller's ID."},
51 { "redirected", MATCH_REDIRECTED,COND_TYPE_NULL,
52 "redirected", "Matches if caller has been redirected."},
53 { "direct", MATCH_DIRECT ,COND_TYPE_NULL,
54 "direct", "Matches if caller did not come from an redirection."},
55 { "redirid", MATCH_REDIRID ,COND_TYPE_STRING,
56 "redirid=<digits>[-<digits>][,...]", "Matches if the caller has been redirected by the given (range(s) of) ID(s) or prefix(es))"},
57 { "time", MATCH_TIME, COND_TYPE_TIME,
58 "time=<minutes>[-<minutes>][,...]", "Matches if the caller calls within the given (range(s) of) time(s). (e.g. 0700-1900)"},
59 { "mday", MATCH_MDAY, COND_TYPE_MDAY,
60 "mday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) day(s) of the month. (1..31)"},
61 { "month", MATCH_MONTH, COND_TYPE_MONTH,
62 "month=<month>[-<month>][,...]", "Matches if the caller calls within the given (range(s) of) month(s). (1=January..12=December)"},
63 { "year", MATCH_YEAR, COND_TYPE_YEAR,
64 "year=<year>[-<year>][,...]", "Matches if the caller calls within the given (range(s) of) year(s). (1970..2106)"},
65 { "wday", MATCH_WDAY, COND_TYPE_WDAY,
66 "wday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) weekday(s). (1=Monday..7=Sunday)"},
67 { "capability", MATCH_CAPABILITY, COND_TYPE_CAPABILITY,
68 "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones[,...]", "Matches the given bearer capability(s)."},
69 { "infolayer1", MATCH_INFOLAYER1, COND_TYPE_INTEGER,
70 "infolayer1=<value>[,...]", "Matches the given information layer 1. (2=u-Law, 3=a-law, see info layer 1 in bearer capability.)"},
71 { "hlc", MATCH_HLC, COND_TYPE_INTEGER,
72 "hlc=<value>[,...]", "Matches the high layer capability(s)."},
73 { "file", MATCH_FILE, COND_TYPE_STRING,
74 "file=<path>[,...]", "Mathes is the given file exists and if the first character is '1'."},
75 { "execute", MATCH_EXECUTE, COND_TYPE_STRING,
76 "execute=<command>[,...]","Matches if the return value of the given command is greater 0."},
77 { "default", MATCH_DEFAULT, COND_TYPE_NULL,
78 "default","Matches if no further dialing could match."},
79 { "timeout", MATCH_TIMEOUT, COND_TYPE_INTEGER,
80 "timeout=<seconds>","Matches if the ruleset was entered AFTER given seconds."},
81 { "free", MATCH_FREE, COND_TYPE_IFATTR,
82 "free=<interface>:<channel>","Matches if the given minimum of channels are free."},
83 { "notfree", MATCH_NOTFREE, COND_TYPE_IFATTR,
84 "notfree=<interface>:<channel>","Matches if NOT the given minimum of channels are free."},
85 { "blocked", MATCH_DOWN, COND_TYPE_STRING,
86 "blocked=<interfaces>[,...]","Matches if all of the given interfaces are blocked."},
87 { "idle", MATCH_UP, COND_TYPE_STRING,
88 "idle=<interface>[,...]","Matches if any of the given interfaces is idle."},
89 { "busy", MATCH_BUSY, COND_TYPE_STRING,
90 "busy=<extension>[,...]","Matches if any of the given extension is busy."},
91 { "notbusy", MATCH_IDLE, COND_TYPE_STRING,
92 "notbusy=<extension>[,...]","Matches if any of the given extension is not busy."},
93 { "remote", MATCH_REMOTE, COND_TYPE_STRING,
94 "remote=<application name>","Matches if remote application is running."},
95 { "notremote", MATCH_NOTREMOTE,COND_TYPE_STRING,
96 "notremote=<application name>","Matches if remote application is not running."},
100 struct param_defs param_defs[] = {
102 "proceeding", PARAM_TYPE_NULL,
103 "proceeding", "Will set the call into 'proceeding' state to prevent dial timeout."},
105 "alerting", PARAM_TYPE_NULL,
106 "alerting", "Will set the call into 'altering' state."},
108 "connect", PARAM_TYPE_NULL,
109 "connect", "Will complete the call before processing the action. Audio path for external calls will be established."},
111 "extension", PARAM_TYPE_STRING,
112 "extension=<digits>", "Give extension name (digits) to relate this action to."},
114 "extensions", PARAM_TYPE_STRING,
115 "extensions=<extension>[,<extension>[,...]]", "One or more extensions may be given."},
117 "prefix", PARAM_TYPE_STRING,
118 "prefix=<digits>", "Add prefix in front of the dialed number."},
120 "capability", PARAM_TYPE_CAPABILITY,
121 "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones", "Alter the service type of the call."},
123 "bmode", PARAM_TYPE_BMODE,
124 "bmode=transparent|hdlc", "Alter the bchannel mode of the call. Use hdlc for data calls."},
126 "infolayer1", PARAM_TYPE_INTEGER,
127 "infolayer1=<value>", "Alter the layer 1 information of a call. Use 3 for ALAW or 2 for uLAW."},
129 "hlc", PARAM_TYPE_INTEGER,
130 "hlc=<value>", "Alter the HLC identification. Use 1 for telephony or omit."},
132 "exthlc", PARAM_TYPE_INTEGER,
133 "exthlc=<value>", "Alter extended HLC value. (Mainenance only, don't use it.)"},
135 "present", PARAM_TYPE_YESNO,
136 "present=yes|no", "Allow or restrict caller ID regardless what the caller wants."},
138 "diversion", PARAM_TYPE_DIVERSION,
139 "diversion=cfu|cfnr|cfb|cfp", "Set diversion type."},
141 "dest", PARAM_TYPE_DESTIN,
142 "dest=<string>", "Destination number to divert to. Use 'vbox' to divert to vbox. (cfu,cfnr,cfb only)"},
144 "select", PARAM_TYPE_NULL,
145 "select", "Lets the caller select the history of calls using keys '1'/'3' or '*'/'#'."},
147 "delay", PARAM_TYPE_INTEGER,
148 "delay=<seconds>", "Number of seconds to delay."},
150 "limit", PARAM_TYPE_INTEGER,
151 "limit=<retries>", "Number of maximum retries."},
153 "host", PARAM_TYPE_STRING,
154 "host=<string>", "Name of remote VoIP host."},
156 "port", PARAM_TYPE_STRING,
157 "port=<value>", "Alternate port to use if 'host' is given."},
159 "interfaces", PARAM_TYPE_STRING,
160 "interfaces=<interface>[,<interface>[,...]]", "Give one or a list of Interfaces to select a free channel from."},
162 "address", PARAM_TYPE_STRING,
163 "address=<string>", "Complete VoIP address. ( [user@]host[:port] )"},
165 "sample", PARAM_TYPE_STRING,
166 "sample=<file prefix>", "Filename of sample (current tone's dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
167 { PARAM_ANNOUNCEMENT,
168 "announcement",PARAM_TYPE_STRING,
169 "announcement=<file prefix>", "Filename of announcement (inside vbox recording dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
171 "ruleset", PARAM_TYPE_STRING,
172 "ruleset=<name>", "Ruleset to go to."},
174 "cause", PARAM_TYPE_INTEGER,
175 "cause=<cause value>", "Cause value when disconnecting. (21=reject 1=unassigned 63=service not available)"},
177 "location", PARAM_TYPE_INTEGER,
178 "location=<location value>", "Location of cause value when disconnecting. (0=user 1=private network sering local user)"},
180 "display", PARAM_TYPE_STRING,
181 "display=<text>", "Message to display on the caller's telephone. (internal only)"},
183 "ports", PARAM_TYPE_INTEGER,
184 "ports=<port>[,<port>[,...]]", "ISDN port[s] to use."},
186 "tpreset", PARAM_TYPE_INTEGER,
187 "tpreset=<seconds>", "Preset of countdown timer."},
189 "file", PARAM_TYPE_STRING,
190 "file=<full path>", "Full path to file name."},
192 "content", PARAM_TYPE_STRING,
193 "content=<string>", "Content to write into file."},
195 "append", PARAM_TYPE_NULL,
196 "append", "Will append to given file, rather than overwriting it."},
198 "execute", PARAM_TYPE_STRING,
199 "execute=<full path>", "Full path to script/command name. (Dialed digits are the argument 1.)"},
201 "param", PARAM_TYPE_STRING,
202 "param=<string>", "Optionally this parameter can be inserted as argument 1, others are shifted."},
204 "type", PARAM_TYPE_TYPE,
205 "type=unknown|subscriber|national|international", "Type of number to dial, default is 'unknown'."},
207 "complete", PARAM_TYPE_NULL,
208 "complete", "Indicates complete number as given by prefix. Proceeding of long distance calls may be faster."},
210 "callerid", PARAM_TYPE_STRING,
211 "callerid=<digits>", "Change caller ID to given string."},
212 { PARAM_CALLERIDTYPE,
213 "calleridtype",PARAM_TYPE_CALLERIDTYPE,
214 "calleridtype=[unknown|subscriber|national|international]", "Type of caller ID. For normal MSN use 'unknown'"},
216 "callto", PARAM_TYPE_STRING,
217 "callto=<digits>", "Where to call back. By default the caller ID is used."},
219 "room", PARAM_TYPE_INTEGER,
220 "room=<digits>", "Conference room number, must be greater 0, as in real life."},
222 "jingle", PARAM_TYPE_NULL,
223 "jingle", "Conference members will hear a jingle if a member joins."},
225 "timeout", PARAM_TYPE_INTEGER,
226 "timeout=<seconds>", "Timeout before continue with next action."},
228 "nopassword", PARAM_TYPE_NULL,
229 "nopassword", "Don't ask for password. Be sure to authenticate right via real caller ID."},
231 "strip", PARAM_TYPE_NULL,
232 "strip", "Remove digits that were required to match this rule."},
234 "application",PARAM_TYPE_STRING,
235 "application=<name>", "Name of remote application to make call to."},
237 "context", PARAM_TYPE_STRING,
238 "context=<context>", "Give context parameter to the remote application."},
240 "exten", PARAM_TYPE_STRING,
241 "exten=<extension>", "Give exten parameter to the remote application. (overrides dialed number)"},
242 { 0, NULL, 0, NULL, NULL}
245 struct action_defs action_defs[] = {
247 "extern", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
248 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,
249 "Call is routed to extern number as dialed."},
251 "intern", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_internal, &EndpointAppPBX::action_hangup_call,
252 PARAM_CONNECT | PARAM_EXTENSION | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_TIMEOUT,
253 "Call is routed to intern extension as given by the dialed number or specified by option."},
255 "outdial", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
256 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,
259 "remote", &EndpointAppPBX::action_init_remote, &EndpointAppPBX::action_dialing_remote, &EndpointAppPBX::action_hangup_call,
260 PARAM_CONNECT | PARAM_APPLICATION | PARAM_CONTEXT | PARAM_EXTEN | PARAM_TIMEOUT,
261 "Call is routed to Remote application, like Asterisk."},
262 { ACTION_VBOX_RECORD,
263 "vbox-record",&EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_vbox_record, &EndpointAppPBX::action_hangup_call,
264 PARAM_CONNECT | PARAM_EXTENSION | PARAM_ANNOUNCEMENT | PARAM_TIMEOUT,
265 "Caller is routed to the voice box of given extension."},
267 "partyline",&EndpointAppPBX::action_init_partyline, NULL, &EndpointAppPBX::action_hangup_call,
268 PARAM_ROOM | PARAM_JINGLE,
269 "Caller is participating the conference with the given room number."},
271 "login", NULL, &EndpointAppPBX::action_dialing_login, NULL,
272 PARAM_CONNECT | PARAM_EXTENSION | PARAM_NOPASSWORD,
273 "Log into the given extension. Password required."},
275 "callerid", &EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_callerid, NULL,
276 PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
277 "Caller changes the caller ID for all calls."},
278 { ACTION_CALLERIDNEXT,
279 "calleridnext",&EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_calleridnext, NULL,
280 PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
281 "Caller changes the caller ID for the next call."},
283 "forward", &EndpointAppPBX::action_init_change_forward, &EndpointAppPBX::action_dialing_forward, NULL,
284 PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_DELAY,
285 "Caller changes the diversion of given type to the given destination or voice box."},
287 "redial", &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_redial, NULL,
288 PARAM_CONNECT | PARAM_SELECT,
289 "Caller redials. (last outgoing call(s))"},
291 "reply", &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_reply, NULL,
292 PARAM_CONNECT | PARAM_SELECT,
293 "Caller replies. (last incoming call(s))"},
295 "powerdial", NULL, &EndpointAppPBX::action_dialing_powerdial, NULL,
296 PARAM_CONNECT | PARAM_DELAY | PARAM_LIMIT | PARAM_TIMEOUT,
297 "Caller redials using powerdialing."},
299 "callback", NULL, &EndpointAppPBX::action_dialing_callback, &EndpointAppPBX::action_hangup_callback,
300 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_EXTENSION | PARAM_DELAY | PARAM_CALLTO | PARAM_PREFIX,
301 "Caller will use the callback service. After disconnecting, the callback is triggered."},
303 "abbrev", NULL, &EndpointAppPBX::action_dialing_abbrev, NULL,
305 "Caller dials abbreviation."},
307 "test", NULL, &EndpointAppPBX::action_dialing_test, NULL,
308 PARAM_CONNECT | PARAM_PREFIX | PARAM_TIMEOUT,
309 "Caller dials test mode."},
311 "play", &EndpointAppPBX::action_init_play, NULL, NULL,
312 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_SAMPLE | PARAM_TIMEOUT,
313 "Plays the given sample."},
315 "vbox-play", &EndpointAppPBX::action_init_vbox_play, &EndpointAppPBX::action_dialing_vbox_play, NULL,
317 "Caller listens to her voice box or to given extension."},
319 "calculator", NULL, &EndpointAppPBX::action_dialing_calculator, NULL,
321 "Caller calls the calculator."},
323 "timer", NULL, &EndpointAppPBX::action_dialing_timer, NULL,
324 PARAM_CONNECT | PARAM_TPRESET | PARAM_TIMEOUT,
326 // "Caller calls the timer."},
328 "goto", NULL, &EndpointAppPBX::action_dialing_goto, NULL,
329 PARAM_CONNECT | PARAM_RULESET | PARAM_STRIP | PARAM_SAMPLE,
330 "Jump to given ruleset and optionally play sample. Dialed digits are not flushed."},
332 "menu", NULL, &EndpointAppPBX::action_dialing_menu, NULL,
333 PARAM_CONNECT | PARAM_RULESET | PARAM_SAMPLE,
334 "Same as 'goto', but flushes all digits dialed so far."},
336 "disconnect", NULL, &EndpointAppPBX::action_dialing_disconnect, NULL,
337 PARAM_CONNECT | PARAM_CAUSE | PARAM_LOCATION | PARAM_SAMPLE | PARAM_DISPLAY,
338 "Caller gets disconnected optionally with given cause and given sample and given display text."},
340 "deflect", NULL, &EndpointAppPBX::action_dialing_deflect, NULL,
343 // "External call is deflected to the given destination within the telephone network."},
345 "setforward", NULL, &EndpointAppPBX::action_dialing_setforward, NULL,
346 PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_PORT,
348 // "The call forward is set within the telephone network of the external line."},
350 "execute", NULL, NULL, &EndpointAppPBX::action_hangup_execute,
351 PARAM_CONNECT | PARAM_EXECUTE | PARAM_PARAM,
352 "Executes the given script file. The file must terminate quickly, because it will halt the PBX."},
354 "file", NULL, NULL, &EndpointAppPBX::action_hangup_file,
355 PARAM_CONNECT | PARAM_FILE | PARAM_CONTENT | PARAM_APPEND,
356 "Writes givent content to given file. If content is not given, the dialed digits are written."},
358 "pick", &EndpointAppPBX::action_init_pick, NULL, NULL,
360 "Pick up a call that is ringing on any phone. Extensions may be given to limit the picking ability."},
362 "password", NULL, &EndpointAppPBX::action_dialing_password, NULL,
365 { ACTION_PASSWORD_WRITE,
366 "password_wr",NULL, &EndpointAppPBX::action_dialing_password_wr, NULL,
370 "nothing", NULL, NULL, NULL,
371 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_TIMEOUT,
372 "does nothing. Usefull to wait for calls to be released completely, by giving timeout value."},
374 "efi", &EndpointAppPBX::action_init_efi, NULL, NULL,
375 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT,
376 "Elektronische Fernsprecher Identifikation - announces caller ID."},
378 NULL, NULL, NULL, NULL, 0, NULL}
382 /* display documentation of rules */
384 void doc_rules(const char *name)
391 while(action_defs[i].name)
393 if (!strcasecmp(action_defs[i].name, name))
397 if (!action_defs[i].name)
399 fprintf(stderr, "Given action '%s' unknown.\n", name);
402 name = action_defs[i].name;
405 printf("Syntax overview:\n");
406 printf("----------------\n\n");
407 printf("[ruleset]\n");
408 printf("<condition> ... : <action> [parameter ...] [timeout=X : <action> ...]\n");
410 printf("Please refer to the documentation for description on rule format.\n\n");
414 printf("Available conditions to match:\n");
415 printf("------------------------------\n\n");
417 while(cond_defs[i].name)
419 printf("Usage: %s\n", cond_defs[i].doc);
420 printf("%s\n\n", cond_defs[i].help);
424 printf("Available actions with their parameters:\n");
425 printf("----------------------------------------\n\n");
428 printf("Detailes parameter description of action:\n");
429 printf("-----------------------------------------\n\n");
432 while(action_defs[i].name)
434 if (name && !!strcmp(action_defs[i].name,name)) /* not selected */
439 if (!action_defs[i].help) /* not internal actions */
444 printf("Usage: %s", action_defs[i].name);
448 if ((1LL<<j) & action_defs[i].params)
449 printf(" [%s]", param_defs[j].doc);
452 printf("\n%s\n\n", action_defs[i].help);
453 if (name) /* only show parameter help for specific action */
458 if ((1LL<<j) & action_defs[i].params)
459 printf("%s:\n\t%s\n", param_defs[j].doc, param_defs[j].help);
468 void ruleset_free(struct route_ruleset *ruleset_start)
470 struct route_ruleset *ruleset;
471 struct route_rule *rule;
472 struct route_cond *cond;
473 struct route_action *action;
474 struct route_param *param;
478 ruleset = ruleset_start;
479 ruleset_start = ruleset->next;
480 while(ruleset->rule_first)
482 rule = ruleset->rule_first;
483 ruleset->rule_first = rule->next;
484 while(rule->cond_first)
486 cond = rule->cond_first;
487 if (cond->string_value)
489 FREE(cond->string_value, 0);
492 if (cond->string_value_to)
494 FREE(cond->string_value_to, 0);
497 rule->cond_first = cond->next;
498 FREE(cond, sizeof(struct route_cond));
501 while(rule->action_first)
503 action = rule->action_first;
504 rule->action_first = action->next;
505 while(action->param_first)
507 param = action->param_first;
508 action->param_first = param->next;
509 if (param->string_value)
511 FREE(param->string_value, 0);
514 FREE(param, sizeof(struct route_param));
517 FREE(action, sizeof(struct route_action));
520 FREE(rule, sizeof(struct route_rule));
523 FREE(ruleset, sizeof(struct route_ruleset));
528 void ruleset_debug(struct route_ruleset *ruleset_start)
530 struct route_ruleset *ruleset;
531 struct route_rule *rule;
532 struct route_cond *cond;
533 struct route_action *action;
534 struct route_param *param;
537 ruleset = ruleset_start;
540 printf("Ruleset: '%s'\n", ruleset->name);
541 rule = ruleset->rule_first;
546 cond = rule->cond_first;
550 printf(" Condition:");
554 printf(" %s", cond_defs[cond->index].name);
555 if (cond->value_type != VALUE_TYPE_NULL)
558 switch(cond->value_type)
560 case VALUE_TYPE_NULL:
563 case VALUE_TYPE_INTEGER:
564 printf("%d", cond->integer_value);
567 case VALUE_TYPE_INTEGER_RANGE:
568 printf("%d-%d", cond->integer_value, cond->integer_value_to);
571 case VALUE_TYPE_STRING:
572 printf("'%s'", cond->string_value);
575 case VALUE_TYPE_STRING_RANGE:
576 printf("'%s'-'%s'", cond->string_value, cond->string_value_to);
580 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", cond->value_type, __FUNCTION__, __LINE__);
582 if (cond->value_extension && cond->next)
586 goto next_cond_value;
594 action = rule->action_first;
597 printf(" Action: %s\n", action_defs[action->index].name);
600 param = action->param_first;
608 printf(" %s", param_defs[param->index].name);
609 if (param->value_type != VALUE_TYPE_NULL)
611 switch(param->value_type)
613 case VALUE_TYPE_NULL:
616 case VALUE_TYPE_INTEGER:
617 if (param_defs[param->index].type == PARAM_TYPE_CALLERIDTYPE)
619 switch(param->integer_value)
621 case INFO_NTYPE_UNKNOWN:
624 case INFO_NTYPE_SUBSCRIBER:
625 printf("subscriber");
627 case INFO_NTYPE_NATIONAL:
630 case INFO_NTYPE_INTERNATIONAL:
631 printf("international");
634 printf("unknown(%d)", param->integer_value);
638 if (param_defs[param->index].type == PARAM_TYPE_CAPABILITY)
640 switch(param->integer_value)
651 case INFO_BC_DATARESTRICTED:
652 printf("digital-restricted");
654 case INFO_BC_DATAUNRESTRICTED:
655 printf("digital-unrestricted");
657 case INFO_BC_DATAUNRESTRICTED_TONES:
658 printf("digital-unrestricted-tones");
661 printf("unknown(%d)", param->integer_value);
665 if (param_defs[param->index].type == PARAM_TYPE_DIVERSION)
667 switch(param->integer_value)
669 case INFO_DIVERSION_CFU:
672 case INFO_DIVERSION_CFNR:
675 case INFO_DIVERSION_CFB:
678 case INFO_DIVERSION_CFP:
682 printf("unknown(%d)", param->integer_value);
686 if (param_defs[param->index].type == PARAM_TYPE_TYPE)
688 switch(param->integer_value)
690 case INFO_NTYPE_UNKNOWN:
693 case INFO_NTYPE_SUBSCRIBER:
694 printf("subscriber");
696 case INFO_NTYPE_NATIONAL:
699 case INFO_NTYPE_INTERNATIONAL:
700 printf("international");
703 printf("unknown(%d)", param->integer_value);
707 if (param_defs[param->index].type == PARAM_TYPE_YESNO)
709 switch(param->integer_value)
718 printf("unknown(%d)", param->integer_value);
722 if (param_defs[param->index].type == PARAM_TYPE_NULL)
726 printf("%d", param->integer_value);
729 case VALUE_TYPE_STRING:
730 printf("'%s'", param->string_value);
734 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", param->value_type, __FUNCTION__, __LINE__);
741 printf(" Timeout: %d\n", action->timeout);
742 action = action->next;
748 ruleset = ruleset->next;
756 static char *read_string(char *p, char *key, int key_size, const char *special)
782 UPRINT(key, "\001String too long.");
787 UPRINT(key, "\001Unexpected end of line inside quotes.");
794 if (strchr(special, *p))
804 UPRINT(key, "\001Unexpected end of line.");
810 UPRINT(key, "\001String too long.");
818 char ruleset_error[256];
819 struct route_ruleset *ruleset_parse(void)
824 unsigned long long j;
828 FILE *fp[MAXNESTING];
829 char filename[MAXNESTING][256];
830 int line[MAXNESTING];
837 int expecting = 1; /* 1 = expecting ruleset */
841 integer_to; /* condition index, .. */
842 struct route_ruleset *ruleset_start = NULL, *ruleset;
843 struct route_ruleset **ruleset_pointer = &ruleset_start;
844 struct route_rule *rule;
845 struct route_rule **rule_pointer = NULL;
846 struct route_cond *cond;
847 struct route_cond **cond_pointer = NULL;
848 struct route_action *action;
849 struct route_action **action_pointer = NULL;
850 struct route_param *param;
851 struct route_param **param_pointer = NULL;
853 unsigned long long allowed_params;
855 /* check the integrity of IDs for ACTION_* and PARAM_* */
857 while(action_defs[i].name)
859 if (action_defs[i].id != i)
861 PERROR("Software Error action '%s' must have id of %d, but has %d.\n",
862 action_defs[i].name, i, action_defs[i].id);
868 while(param_defs[i].name)
870 if (param_defs[i].id != j)
872 PERROR("Software Error param '%s' must have id of 0x%llx, but has 0x%llx.\n",
873 param_defs[i].name, j, param_defs[i].id);
880 SPRINT(filename[0], "%s/routing.conf", CONFIG_DATA);
882 if (!(fp[0]=fopen(filename[0],"r")))
884 PERROR("Cannot open %s\n",filename[0]);
893 while((fgets(buffer,sizeof(buffer),fp[nesting])))
896 buffer[sizeof(buffer)-1]=0;
897 if (buffer[0]) buffer[strlen(buffer)-1]=0;
908 /* skip spaces, if any */
920 /* don't skip "define" */
921 if (!!strncmp(p, "define", 6))
934 p = read_string(p, key, sizeof(key), " ");
935 if (key[0] == 1) /* error */
937 SPRINT(failure, "Parsing Filename failed: %s", key+1);
940 if (nesting == MAXNESTING-1)
942 SPRINT(failure, "'include' is nesting too deep.\n");
946 SCPY(filename[nesting+1], key);
948 SPRINT(filename[nesting+1], "%s/%s", CONFIG_DATA, key);
949 if (!(fp[nesting+1]=fopen(filename[nesting+1],"r")))
951 PERROR("Cannot open %s\n", filename[nesting+1]);
958 if (*p == '/') if (p[1] == '/')
961 /* skip empty lines */
965 /* expecting ruleset */
972 SPRINT(failure, "Expecting ruleset name starting with '['.");
977 /* reading ruleset name text */
979 while(*p>' ' && *p<127 && *p!=']')
981 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
983 if (i == sizeof(key)) i--; /* limit */
986 if (key[0] == '\0') {
987 SPRINT(failure, "Missing ruleset name after '['.");
991 /* expecting ] and nothing more */
993 SPRINT(failure, "Expecting ']' after ruleset name.");
998 SPRINT(failure, "Unexpected character after ruleset name.");
1002 /* check for duplicate rulesets */
1003 ruleset = ruleset_start;
1006 if (!strcmp(ruleset->name, key))
1008 SPRINT(failure, "Duplicate ruleset '%s', already defined in file '%s' line %d.", key, ruleset->file, ruleset->line);
1011 ruleset = ruleset->next;
1014 /* create ruleset */
1015 ruleset = (struct route_ruleset *)MALLOC(sizeof(struct route_ruleset));
1017 *ruleset_pointer = ruleset;
1018 ruleset_pointer = &(ruleset->next);
1019 SCPY(ruleset->name, key);
1020 SCPY(ruleset->file, filename[nesting]);
1021 ruleset->line = line[nesting];
1022 rule_pointer = &(ruleset->rule_first);
1027 /* for new ruleset [ */
1033 /* Alloc memory for rule */
1034 rule = (struct route_rule *)MALLOC(sizeof(struct route_rule));
1036 *rule_pointer = rule;
1037 rule_pointer = &(rule->next);
1038 cond_pointer = &(rule->cond_first);
1039 action_pointer = &(rule->action_first);
1040 SCPY(rule->file, filename[nesting]);
1041 rule->line = line[nesting];
1043 /* loop CONDITIONS */
1044 while(*p!=':' && *p!='\0')
1046 /* read item text */
1048 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1050 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1052 if (i == sizeof(key)) i--; /* limit */
1057 SPRINT(failure, "Expecting condition item name or ':' for end of condition list.");
1060 if (*p!=' ' && *p!='=')
1062 SPRINT(failure, "Illegal character '%c' after condition name '%s'. Expecting '=' for equation or ' ' to seperate condition items.", *p, key);
1066 /* check if condition exists */
1068 while(cond_defs[index].name)
1070 if (!strcmp(cond_defs[index].name, key))
1074 if (cond_defs[index].name == NULL)
1076 SPRINT(failure, "Unknown condition item name '%s'.", key);
1080 /* items without values must not have any parameter */
1081 if (cond_defs[index].type == COND_TYPE_NULL)
1085 SPRINT(failure, "Condition item '%s' must not have any value. Don't use '=' for this type of condition.", key);
1090 SPRINT(failure, "Condition item '%s' must not have any value. Expecting ' ' or tab after item name.", key);
1098 SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected, and not a space.", key);
1103 SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected.", key);
1109 /* check for duplicate condition */
1110 cond = rule->cond_first;
1113 if (cond->index == index)
1115 SPRINT(failure, "Duplicate condition '%s', use ',' to give multiple values.", key);
1122 /* Alloc memory for item */
1123 cond = (struct route_cond *)MALLOC(sizeof(struct route_cond));
1125 *cond_pointer = cond;
1126 cond_pointer = &(cond->next);
1127 cond->index = index;
1128 cond->match = cond_defs[index].match;
1129 switch(cond_defs[index].type)
1131 case COND_TYPE_NULL:
1134 SPRINT(failure, "Expecting no value.");
1137 value_type = VALUE_TYPE_NULL;
1140 /* parse all integer values/ranges */
1141 case COND_TYPE_INTEGER:
1142 case COND_TYPE_TIME:
1143 case COND_TYPE_MDAY:
1144 case COND_TYPE_MONTH:
1145 case COND_TYPE_WDAY:
1146 case COND_TYPE_YEAR:
1147 integer = integer_to = 0;
1148 if (*p==',' || *p==' ' || *p=='\0')
1150 SPRINT(failure, "Missing integer value.");
1153 while(*p>='0' && *p<='9')
1155 integer = integer*10 + *p-'0';
1158 value_type = VALUE_TYPE_INTEGER;
1162 if (*p==',' || *p==' ' || *p=='\0')
1164 SPRINT(failure, "Missing integer value.");
1167 while(*p>='0' && *p<='9')
1169 integer_to = integer_to*10 + *p-'0';
1172 value_type = VALUE_TYPE_INTEGER_RANGE;
1174 if (cond_defs[index].type == COND_TYPE_TIME)
1176 // Simon: i store the time as decimal, later i compare it correctly:
1177 // hours * 100 + minutes
1178 if (integer == 2400)
1180 if (integer >= 2400)
1183 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer);
1186 if (integer%100 >= 60)
1187 goto timeoutofrange1;
1188 if (value_type == VALUE_TYPE_INTEGER)
1190 if (integer_to == 2400)
1192 if (integer_to >= 2400)
1195 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer_to);
1198 if (integer_to%100 >= 60)
1199 goto timeoutofrange2;
1201 if (cond_defs[index].type == COND_TYPE_MDAY)
1203 if (integer<1 || integer>31)
1205 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer);
1208 if (value_type == VALUE_TYPE_INTEGER)
1210 if (integer_to<1 || integer_to>31)
1212 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer_to);
1216 if (cond_defs[index].type == COND_TYPE_WDAY)
1218 if (integer<1 || integer>7)
1220 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer);
1223 if (value_type == VALUE_TYPE_INTEGER)
1225 if (integer_to<1 || integer_to>7)
1227 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer_to);
1231 if (cond_defs[index].type == COND_TYPE_MONTH)
1233 if (integer<1 || integer>12)
1235 SPRINT(failure, "Given month '%d' not in range 1..12", integer);
1238 if (value_type == VALUE_TYPE_INTEGER)
1240 if (integer_to<1 || integer_to>12)
1242 SPRINT(failure, "Given month '%d' not in range 1..12", integer_to);
1246 if (cond_defs[index].type == COND_TYPE_YEAR)
1248 if (integer<1970 || integer>2106)
1250 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer);
1253 if (value_type == VALUE_TYPE_INTEGER)
1255 if (integer_to<1970 || integer_to>2106)
1257 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer_to);
1262 cond->integer_value = integer;
1263 cond->integer_value_to = integer_to;
1264 cond->value_type = value_type;
1267 /* parse all string values/ranges */
1268 case COND_TYPE_STRING:
1269 key[0] = key_to[0] = '\0';
1270 if (*p==',' || *p==' ' || *p=='\0')
1272 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1275 p = read_string(p, key, sizeof(key), "-, ");
1276 if (key[0] == 1) /* error */
1278 SPRINT(failure, "Parsing String failed: %s", key+1);
1281 value_type = VALUE_TYPE_STRING;
1285 if (*p==',' || *p==' ' || *p=='\0')
1287 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1290 p = read_string(p, key_to, sizeof(key_to), "-, ");
1291 if (key_to[0] == 1) /* error */
1293 SPRINT(failure, "Parsing string failed: %s", key_to+1);
1296 value_type = VALUE_TYPE_STRING_RANGE;
1297 if (strlen(key) != strlen(key_to))
1299 SPRINT(failure, "Given range of strings \"%s\"-\"%s\" have unequal length.", key, key_to);
1304 SPRINT(failure, "Given range has no length.");
1309 cond->string_value = (char *)MALLOC(strlen(key)+1);
1311 UCPY(cond->string_value, key);
1312 if (value_type == VALUE_TYPE_STRING_RANGE)
1314 cond->string_value_to = (char *)MALLOC(strlen(key_to)+1);
1316 UCPY(cond->string_value_to, key_to);
1317 cond->comp_string = strcmp(key, key_to);
1319 cond->value_type = value_type;
1322 /* parse service value */
1323 case COND_TYPE_CAPABILITY:
1324 if (!strncasecmp("speech", p, 6))
1325 cond->integer_value = INFO_BC_SPEECH;
1326 else if (!strncasecmp("audio", p, 5))
1327 cond->integer_value = INFO_BC_AUDIO;
1328 else if (!strncasecmp("video", p, 5))
1329 cond->integer_value = INFO_BC_VIDEO;
1330 else if (!strncasecmp("digital-restricted", p, 18))
1331 cond->integer_value = INFO_BC_DATARESTRICTED;
1332 else if (!strncasecmp("digital-unrestricted", p, 20))
1333 cond->integer_value = INFO_BC_DATAUNRESTRICTED;
1334 else if (!strncasecmp("digital-unrestricted-tones", p, 26))
1335 cond->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1338 SPRINT(failure, "Given service type is invalid or misspelled.");
1341 cond->value_type = VALUE_TYPE_INTEGER;
1344 /* parse bmode value */
1345 case COND_TYPE_BMODE:
1346 if (!strncasecmp("transparent", p, 11))
1347 cond->integer_value = INFO_BMODE_CIRCUIT;
1348 else if (!strncasecmp("hdlc", p, 4))
1349 cond->integer_value = INFO_BMODE_PACKET;
1352 SPRINT(failure, "Given bchannel mode is invalid or misspelled.");
1355 cond->value_type = VALUE_TYPE_INTEGER;
1358 /* parse interface attribute <if>:<value> */
1359 case COND_TYPE_IFATTR:
1360 key[0] = key_to[0] = '\0';
1361 if (*p==':' || *p==',' || *p==' ' || *p=='\0')
1363 SPRINT(failure, "Missing interface name.");
1366 p = read_string(p, key, sizeof(key), ":-, ");
1367 if (key[0] == 1) /* error */
1369 SPRINT(failure, "Parsing interface failed: %s", key+1);
1374 SPRINT(failure, "Expeciting kolon to seperate value behind interface name.");
1378 while(*p>='0' && *p<='9')
1382 if (*p!=',' && *p!=' ' && *p!='\0')
1384 SPRINT(failure, "Invalid characters behind value.");
1387 value_type = VALUE_TYPE_STRING;
1392 SPRINT(failure, "Software error: COND_TYPE_* %d not parsed in function '%s'", cond_defs[index].type, __FUNCTION__);
1395 /* if we have another value for that item, we attach it */
1400 cond->value_extension = 1;
1403 /* to seperate the items, a space is required */
1406 SPRINT(failure, "Character '%c' not expected here. Use ',' to seperate multiple possible values.", *p);
1418 /* we are done with CONDITIONS, so we expect the ACTION */
1421 SPRINT(failure, "Expecting ':' after condition item(s).");
1427 /* skip spaces, if any */
1435 /* read action name */
1437 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9') || *p == '-')
1439 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1441 if (i == sizeof(key)) i--; /* limit */
1444 if (key[0] == '\0') {
1445 SPRINT(failure, "Expecting action name.");
1449 /* check if item exists */
1451 while(action_defs[index].name)
1453 if (!action_defs[index].help) /* not internal actions */
1458 if (!strcmp(action_defs[index].name, key))
1462 if (action_defs[index].name == NULL)
1464 SPRINT(failure, "Unknown action name '%s'.", key);
1467 allowed_params = action_defs[index].params;
1469 /* alloc memory for action */
1470 action = (struct route_action *)MALLOC(sizeof(struct route_action));
1472 *action_pointer = action;
1473 action_pointer = &(action->next);
1474 param_pointer = &(action->param_first);
1475 action->index = index;
1476 action->line = line[nesting];
1478 /* skip spaces after param name */
1489 /* read param text */
1491 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1493 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1495 if (i == sizeof(key)) i--; /* limit */
1498 if (key[0] == '\0') {
1499 SPRINT(failure, "Expecting parameter name.");
1503 /* check if item exists */
1505 while(param_defs[index].name)
1507 if (!strcmp(param_defs[index].name, key))
1511 if (param_defs[index].name == NULL)
1513 SPRINT(failure, "Unknown param name '%s'.", key);
1517 /* check if item is allowed for the action */
1518 if (!(param_defs[index].id & allowed_params))
1520 SPRINT(failure, "Param name '%s' exists, but not for this action.", key);
1524 /* params without values must not have any parameter */
1525 if (param_defs[index].type == PARAM_TYPE_NULL)
1527 if (*p!=' ' && *p!='\0')
1529 SPRINT(failure, "Parameter '%s' must not have any value.", key);
1536 SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected and not a space.", key);
1541 SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected.", key);
1547 /* special timeout value */
1548 if (!strcmp("timeout", key))
1550 if (action->timeout)
1552 SPRINT(failure, "Duplicate timeout value.");
1555 if (*p==',' || *p==' ' || *p=='\0')
1557 SPRINT(failure, "Missing integer value.");
1561 while(*p>='0' && *p<='9')
1563 integer = integer*10 + *p-'0';
1568 SPRINT(failure, "Expecting timeout value greater 0.");
1571 if (*p!=' ' && *p!='\0')
1573 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters.", *p);
1583 action->timeout = integer;
1584 /* check for next ACTION */
1593 /* check for duplicate parameters */
1594 param = action->param_first;
1597 if (param->index == index)
1599 SPRINT(failure, "Duplicate parameter '%s', use ',' to give multiple values.", key);
1602 param = param->next;
1606 /* Alloc memory for param */
1607 param = (struct route_param *)MALLOC(sizeof(struct route_param));
1609 *param_pointer = param;
1610 param_pointer = &(param->next);
1611 param->index = index;
1612 param->id = param_defs[index].id;
1614 switch(param_defs[index].type)
1616 /* parse null value */
1617 case PARAM_TYPE_NULL:
1618 param->value_type = VALUE_TYPE_NULL;
1621 /* parse integer value */
1622 case PARAM_TYPE_INTEGER:
1624 if (*p==',' || *p==' ' || *p=='\0')
1626 SPRINT(failure, "Missing integer value.");
1629 while(*p>='0' && *p<='9')
1631 integer = integer*10 + *p-'0';
1634 param->integer_value = integer;
1635 param->value_type = VALUE_TYPE_INTEGER;
1639 /* parse ports value */
1640 case PARAM_TYPE_PORTS:
1642 if (*p==',' || *p==' ' || *p=='\0')
1644 SPRINT(failure, "Missing port number, omit parameter or give port number.");
1650 while(*p>='0' && *p<='9')
1652 if (i < (int)sizeof(key)-1)
1657 integer = integer*10 + *p-'0';
1662 SPRINT(failure, "Port number too high.");
1667 if (i < (int)sizeof(key)-1)
1678 /* parse string value */
1679 case PARAM_TYPE_STRING:
1680 case PARAM_TYPE_CALLERIDTYPE:
1681 case PARAM_TYPE_CAPABILITY:
1682 case PARAM_TYPE_BMODE:
1683 case PARAM_TYPE_DIVERSION:
1684 case PARAM_TYPE_DESTIN:
1685 case PARAM_TYPE_TYPE:
1686 case PARAM_TYPE_YESNO:
1688 if (*p==',' || *p==' ' || *p=='\0')
1690 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1693 p = read_string(p, key, sizeof(key), " ");
1694 if (key[0] == 1) /* error */
1696 SPRINT(failure, "Parsing string failed: %s", key+1);
1699 if (param_defs[index].type == PARAM_TYPE_CALLERIDTYPE)
1701 param->value_type = VALUE_TYPE_INTEGER;
1702 if (!strcasecmp(key, "unknown"))
1704 param->integer_value = INFO_NTYPE_UNKNOWN;
1707 if (!strcasecmp(key, "subscriber"))
1709 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1712 if (!strcasecmp(key, "national"))
1714 param->integer_value = INFO_NTYPE_NATIONAL;
1717 if (!strcasecmp(key, "international"))
1719 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1722 SPRINT(failure, "Caller ID type '%s' unknown.", key);
1725 if (param_defs[index].type == PARAM_TYPE_CAPABILITY)
1727 param->value_type = VALUE_TYPE_INTEGER;
1728 if (!strcasecmp(key, "speech"))
1730 param->integer_value = INFO_BC_SPEECH;
1733 if (!strcasecmp(key, "audio"))
1735 param->integer_value = INFO_BC_AUDIO;
1738 if (!strcasecmp(key, "video"))
1740 param->integer_value = INFO_BC_VIDEO;
1743 if (!strcasecmp(key, "digital-restricted"))
1745 param->integer_value = INFO_BC_DATARESTRICTED;
1748 if (!strcasecmp(key, "digital-unrestricted"))
1750 param->integer_value = INFO_BC_DATAUNRESTRICTED;
1753 if (!strcasecmp(key, "digital-unrestricted-tones"))
1755 param->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1758 SPRINT(failure, "Service type '%s' unknown.", key);
1761 if (param_defs[index].type == PARAM_TYPE_BMODE)
1763 param->value_type = VALUE_TYPE_INTEGER;
1764 if (!strcasecmp(key, "transparent"))
1766 param->integer_value = INFO_BMODE_CIRCUIT;
1769 if (!strcasecmp(key, "hdlc"))
1771 param->integer_value = INFO_BMODE_PACKET;
1774 SPRINT(failure, "Bchannel mode '%s' unknown.", key);
1777 if (param_defs[index].type == PARAM_TYPE_DIVERSION)
1779 param->value_type = VALUE_TYPE_INTEGER;
1780 if (!strcasecmp(key, "cfu"))
1782 param->integer_value = INFO_DIVERSION_CFU;
1785 if (!strcasecmp(key, "cfb"))
1787 param->integer_value = INFO_DIVERSION_CFB;
1790 if (!strcasecmp(key, "cfnr"))
1792 param->integer_value = INFO_DIVERSION_CFNR;
1795 if (!strcasecmp(key, "cfp"))
1797 param->integer_value = INFO_DIVERSION_CFP;
1800 SPRINT(failure, "Diversion type '%s' unknown.", key);
1803 if (param_defs[index].type == PARAM_TYPE_TYPE)
1805 param->value_type = VALUE_TYPE_INTEGER;
1806 if (!strcasecmp(key, "unknown"))
1808 param->integer_value = INFO_NTYPE_UNKNOWN;
1811 if (!strcasecmp(key, "subscriber"))
1813 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1816 if (!strcasecmp(key, "national"))
1818 param->integer_value = INFO_NTYPE_NATIONAL;
1821 if (!strcasecmp(key, "international"))
1823 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1826 SPRINT(failure, "Number type '%s' unknown.", key);
1829 if (param_defs[index].type == PARAM_TYPE_YESNO)
1831 param->value_type = VALUE_TYPE_INTEGER;
1832 if (!strcasecmp(key, "yes"))
1834 param->integer_value = 1;
1837 if (!strcasecmp(key, "no"))
1839 param->integer_value = 0;
1842 SPRINT(failure, "Value '%s' unknown. ('yes' or 'no')", key);
1845 param->string_value = (char *)MALLOC(strlen(key)+1);
1847 UCPY(param->string_value, key);
1848 param->value_type = VALUE_TYPE_STRING;
1852 SPRINT(failure, "Software error: PARAM_TYPE_* %d not parsed in function '%s'", param_defs[index].type, __FUNCTION__);
1860 param->value_extension = 1;
1861 goto nextparamvalue;
1868 /* to seperate the items, a space is required */
1871 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters, or ',' for multiple values.", *p);
1882 /* check for next ACTION */
1891 fclose(fp[nesting--]);
1899 SPRINT(failure, "No ruleset defined.");
1901 return(ruleset_start);
1904 printf("While parsing %s, an error occurred in line %d:\n", filename[nesting], line[nesting]);
1905 printf("-> %s\n", buffer);
1906 memset(pointer, ' ', sizeof(pointer));
1907 pointer[p-buffer] = '^';
1908 pointer[p-buffer+1] = '\0';
1909 printf(" %s\n", pointer);
1910 printf("%s\n", failure);
1911 SPRINT(ruleset_error, "Error in file %s, line %d: %s", filename[nesting], line[nesting], failure);
1916 fclose(fp[nesting--]);
1920 ruleset_free(ruleset_start);
1925 * return ruleset by name
1927 struct route_ruleset *getrulesetbyname(const char *name)
1929 struct route_ruleset *ruleset = ruleset_first;
1933 if (!strcasecmp(name, ruleset->name))
1937 ruleset = ruleset->next;
1939 PDEBUG(DEBUG_ROUTE, "ruleset %s %s.\n", name, ruleset?"found":"not found");
1944 * parses the current ruleset and returns action
1946 struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
1949 couldmatch = 0, /* any rule could match */
1956 struct route_rule *rule = ruleset->rule_first;
1957 struct route_cond *cond;
1958 struct route_action *action = NULL;
1959 unsigned long comp_len;
1961 char callerid[64], callerid2[64], redirid[64];
1966 struct mISDNport *mISDNport;
1967 struct admin_list *admin;
1969 /* reset timeout action */
1970 e_match_timeout = 0; /* no timeout */
1971 e_match_to_action = NULL;
1973 SCPY(callerid, numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype, options.national, options.international));
1974 SCPY(callerid2, numberrize_callerinfo(e_callerinfo.id2, e_callerinfo.ntype2, options.national, options.international));
1975 SCPY(redirid, numberrize_callerinfo(e_redirinfo.id, e_redirinfo.ntype, options.national, options.international));
1977 PDEBUG(DEBUG_ROUTE, "parsing ruleset '%s'\n", ruleset->name);
1980 PDEBUG(DEBUG_ROUTE, "checking rule in line %d\n", rule->line);
1981 match = 1; /* this rule matches */
1982 dialing_required = 0;
1983 timeout = 0; /* timeout time */
1984 cond = rule->cond_first;
1987 condition = 0; /* any condition element is true (1) or could be true (2) */
1989 istrue = 0; /* this condition-element is true */
1990 couldbetrue = 0; /* this conditions-element could be true */
1994 if (!e_ext.number[0])
1999 if (e_ext.number[0])
2004 if (ea_endpoint->ep_portlist)
2005 if ((ea_endpoint->ep_portlist->port_type & PORT_CLASS_mISDN_MASK) != PORT_CLASS_mISDN_DSS1)
2007 integer = e_callerinfo.isdn_port;
2010 case MATCH_INTERFACE:
2011 if (!e_callerinfo.interface[0])
2013 string = e_callerinfo.interface;
2014 goto match_string_prefix;
2016 case MATCH_CALLERID:
2018 goto match_string_prefix;
2020 case MATCH_CALLERID2:
2022 goto match_string_prefix;
2024 case MATCH_EXTENSION:
2025 string = e_ext.name;
2029 string = e_dialinginfo.id;
2030 goto match_string_prefix;
2042 case MATCH_ANONYMOUS:
2043 if (e_callerinfo.present != INFO_PRESENT_ALLOWED)
2048 if (e_callerinfo.present == INFO_PRESENT_ALLOWED)
2053 if (e_callerinfo.present == INFO_PRESENT_NOTAVAIL)
2057 case MATCH_AVAILABLE:
2058 if (e_callerinfo.present != INFO_PRESENT_NOTAVAIL)
2063 if (e_callerinfo.screen == INFO_SCREEN_USER)
2068 if (e_callerinfo.screen != INFO_SCREEN_USER)
2072 case MATCH_REDIRECTED:
2073 if (e_redirinfo.ntype != INFO_NTYPE_NOTPRESENT)
2078 if (e_redirinfo.ntype == INFO_NTYPE_NOTPRESENT)
2084 goto match_string_prefix;
2087 integer = now_tm->tm_hour*100 + now_tm->tm_min;
2091 integer = now_tm->tm_mday;
2095 integer = now_tm->tm_mon+1;
2099 integer = now_tm->tm_year + 1900;
2103 integer = now_tm->tm_wday;
2104 integer = integer?integer:7; /* correct sunday */
2107 case MATCH_CAPABILITY:
2108 integer = e_capainfo.bearer_capa;
2111 case MATCH_INFOLAYER1:
2112 integer = e_capainfo.bearer_info1;
2116 integer = e_capainfo.hlc;
2120 tfp = fopen(cond->string_value, "r");
2125 if (fgetc(tfp) == '1')
2131 if (system(cond->string_value) == 0)
2141 timeout = now_d + cond->integer_value;
2147 if (!(comp_len = (unsigned long)strchr(cond->string_value, ':')))
2149 comp_len = comp_len-(unsigned long)cond->string_value;
2151 mISDNport = mISDNport_first;
2154 if (mISDNport->ifport)
2155 if (strlen(mISDNport->ifport->interface->name) == comp_len)
2156 if (!strncasecmp(mISDNport->ifport->interface->name, cond->string_value, comp_len))
2157 if (!mISDNport->l2hold || mISDNport->l2link>0)
2160 jj = mISDNport->b_num;
2164 if (mISDNport->b_state[j])
2169 mISDNport = mISDNport->next;
2171 if (cond->match == MATCH_FREE)
2173 if (avail >= atoi(cond->string_value + comp_len + 1))
2177 if (avail < atoi(cond->string_value + comp_len + 1))
2184 mISDNport = mISDNport_first;
2187 if (mISDNport->ifport)
2188 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2189 if (!mISDNport->l2hold || mISDNport->l2link>0)
2191 mISDNport = mISDNport->next;
2193 if (!mISDNport) /* all down */
2198 mISDNport = mISDNport_first;
2201 if (mISDNport->ifport)
2202 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2203 if (!mISDNport->l2hold || mISDNport->l2link>0)
2206 mISDNport = mISDNport->next;
2208 if (mISDNport) /* one link at least */
2215 mISDNport = mISDNport_first;
2218 if (mISDNport->ifport)
2219 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2220 if (mISDNport->use) /* break if in use */
2222 mISDNport = mISDNport->next;
2224 if (mISDNport && cond->match==MATCH_BUSY)
2226 if (!mISDNport && cond->match==MATCH_IDLE)
2231 case MATCH_NOTREMOTE:
2232 admin = admin_first;
2235 if (admin->remote_name[0] && !strcmp(cond->string_value, admin->remote_name))
2237 admin = admin->next;
2239 if (admin && cond->match==MATCH_REMOTE)
2241 if (!admin && cond->match==MATCH_NOTREMOTE)
2246 PERROR("Software error: MATCH_* %d not parsed in function '%s'", cond->match, __FUNCTION__);
2250 if (cond->value_type == VALUE_TYPE_INTEGER)
2252 if (integer != cond->integer_value)
2257 if (cond->value_type == VALUE_TYPE_INTEGER_RANGE)
2259 /* check if negative range (2100 - 700 o'clock) */
2260 if (cond->integer_value > cond->integer_value_to)
2262 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2266 /* range is positive */
2267 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2274 if (strlen(cond->string_value) != strlen(string))
2277 match_string_prefix:
2278 comp_len = strlen(cond->string_value); /* because we must reach value's length */
2279 /* we must have greater or equal length to values */
2280 if ((unsigned long)strlen(string) < comp_len)
2282 /* special case for unfinished dialing */
2283 if (cond->match == MATCH_DIALING)
2285 couldbetrue = 1; /* could match */
2286 comp_len = strlen(string);
2292 /* on single string match */
2293 if (cond->value_type == VALUE_TYPE_STRING)
2295 if (!strncmp(string, cond->string_value, comp_len))
2298 /* must be set for changing 'e_extdialing' */
2299 if (cond->match == MATCH_DIALING)
2300 dialing_required = comp_len;
2305 /* on range match */
2306 if (cond->value_type == VALUE_TYPE_STRING_RANGE)
2308 /* check if negative range ("55"-"22") */
2309 if (cond->comp_string > 0)
2311 if (strncmp(string, cond->string_value, comp_len) >= 0)
2314 /* must be set for changing 'e_extdialing' */
2315 if (cond->match == MATCH_DIALING)
2316 dialing_required = comp_len;
2319 if (strncmp(string, cond->string_value_to, comp_len) <= 0)
2321 /* must be set for changing 'e_extdialing' */
2323 if (cond->match == MATCH_DIALING)
2324 dialing_required = comp_len;
2329 /* range is positive */
2330 if (strncmp(string, cond->string_value, comp_len) < 0)
2332 if (strncmp(string, cond->string_value_to, comp_len) > 0)
2335 if (cond->match == MATCH_DIALING)
2336 dialing_required = comp_len;
2342 /* set current condition */
2343 if (istrue && !couldbetrue)
2344 condition = 1; /* element matches, so condition matches */
2345 if (istrue && couldbetrue && !condition)
2346 condition = 2; /* element could match and other elements don't match, so condition could match */
2348 /* if not matching or could match */
2351 /* if we have more values to check */
2352 if (cond->value_extension && cond->next)
2355 goto checkextension;
2361 /* skip exteded values, beacuse we already have one matching value */
2362 while(cond->value_extension && cond->next)
2367 if (timeout>now_d && match==1) /* the matching rule with timeout in the future */
2368 if (e_match_timeout<1 || timeout<e_match_timeout) /* first timeout or lower */
2370 /* set timeout in the furture */
2371 e_match_timeout = timeout;
2372 e_match_to_action = rule->action_first;
2373 e_match_to_extdialing = e_dialinginfo.id + dialing_required;
2374 match = 0; /* matches in the future */
2378 /* matching, we return first action */
2379 action = rule->action_first;
2380 e_match_timeout = 0; /* no timeout */
2381 e_match_to_action = NULL;
2382 e_extdialing = e_dialinginfo.id + dialing_required;
2387 /* rule could match if more is dialed */
2396 * parses the current action's parameters and return them
2398 struct route_param *EndpointAppPBX::routeparam(struct route_action *action, unsigned long long id)
2400 struct route_param *param = action->param_first;
2404 if (param->id == id)
2406 param = param->next;
2413 * internal rules that are not defined by route.conf
2415 struct route_action action_password = {
2423 struct route_action action_password_write = {
2426 ACTION_PASSWORD_WRITE,
2431 struct route_action action_external = {
2439 struct route_action action_internal = {
2447 struct route_action action_remote = {
2455 struct route_action action_vbox = {
2463 struct route_action action_partyline = {
2471 struct route_action action_disconnect = {