1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** match processing of routing configuration **
10 \*****************************************************************************/
19 struct route_ruleset *ruleset_first; /* first entry */
20 struct route_ruleset *ruleset_main; /* pointer to main ruleset */
22 struct cond_defs cond_defs[] = {
23 { "extern", MATCH_EXTERN, COND_TYPE_NULL,
24 "extern", "Matches if call is from external port (no extension)."},
25 { "intern", MATCH_INTERN,COND_TYPE_NULL,
26 "intern", "Matches if call is from an extension."},
27 { "port", MATCH_PORT, COND_TYPE_INTEGER,
28 "port=<number>[-<number>][,...]", "Matches if call is received from given port(s). NOT INTERFACE!"},
29 { "interface", MATCH_INTERFACE,COND_TYPE_STRING,
30 "interface=<interface>[,...]", "Matches if call is received from given interface(s). NOT PORTS!"},
31 { "callerid", MATCH_CALLERID, COND_TYPE_STRING,
32 "callerid=<digits>[-<digits>][,...]", "Matches if caller ID matches or begins with the given (range(s) of) prefixes(s)."},
33 { "extension", MATCH_EXTENSION,COND_TYPE_STRING,
34 "extension=<digits>[-<digits>][,...]", "Matches if caller calls from given (range(s) of) extension(s)."},
35 { "dialing", MATCH_DIALING, COND_TYPE_STRING,
36 "dialing=<digits>[-<digits>][,...]", "Matches if caller has dialed the given (range(s) of) digits at least."},
37 { "enblock", MATCH_ENBLOCK, COND_TYPE_NULL,
38 "enblock", "Matches if caller dialed en block. (Dial the number before pick up.)"},
39 { "overlap", MATCH_OVERLAP, COND_TYPE_NULL,
40 "overlap", "Matches if caller dialed digit by digit. (Dial the number after pick up.)"},
41 { "anonymous", MATCH_ANONYMOUS,COND_TYPE_NULL,
42 "anonymous", "Matches if caller uses restricted caller ID or if not available."},
43 { "visible", MATCH_VISIBLE, COND_TYPE_NULL,
44 "visible", "Matches if caller ID is presented and if available."},
45 { "unknown", MATCH_UNKNOWN, COND_TYPE_NULL,
46 "unknown", "Matches if no ID is available from caller."},
47 { "available", MATCH_AVAILABLE,COND_TYPE_NULL,
48 "available", "Matches if ID is available from caller."},
49 { "fake", MATCH_FAKE, COND_TYPE_NULL,
50 "fake", "Matches if caller ID is not screened and may be faked by caller."},
51 { "real", MATCH_REAL, COND_TYPE_NULL,
52 "real", "Matches if caller ID is screend and so it is the real caller's ID."},
53 { "redirected", MATCH_REDIRECTED,COND_TYPE_NULL,
54 "redirected", "Matches if caller has been redirected."},
55 { "direct", MATCH_DIRECT ,COND_TYPE_NULL,
56 "direct", "Matches if caller did not come from an redirection."},
57 { "redirid", MATCH_REDIRID ,COND_TYPE_STRING,
58 "redirid=<digits>[-<digits>][,...]", "Matches if the caller has been redirected by the given (range(s) of) ID(s) or prefix(es))"},
59 { "time", MATCH_TIME, COND_TYPE_TIME,
60 "time=<minutes>[-<minutes>][,...]", "Matches if the caller calls within the given (range(s) of) time(s). (e.g. 0700-1900)"},
61 { "mday", MATCH_MDAY, COND_TYPE_MDAY,
62 "mday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) day(s) of the month. (1..31)"},
63 { "month", MATCH_MONTH, COND_TYPE_MONTH,
64 "month=<month>[-<month>][,...]", "Matches if the caller calls within the given (range(s) of) month(s). (1=January..12=December)"},
65 { "year", MATCH_YEAR, COND_TYPE_YEAR,
66 "year=<year>[-<year>][,...]", "Matches if the caller calls within the given (range(s) of) year(s). (1970..2106)"},
67 { "wday", MATCH_WDAY, COND_TYPE_WDAY,
68 "wday=<day>[-<day>][,...]", "Matches if the caller calls within the given (range(s) of) weekday(s). (1=Monday..7=Sunday)"},
69 { "capability", MATCH_CAPABILITY, COND_TYPE_CAPABILITY,
70 "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones[,...]", "Matches the given bearer capability(s)."},
71 { "infolayer1", MATCH_INFOLAYER1, COND_TYPE_INTEGER,
72 "infolayer1=<value>[,...]", "Matches the given information layer 1. (2=u-Law, 3=a-law, see info layer 1 in bearer capability.)"},
73 { "hlc", MATCH_HLC, COND_TYPE_INTEGER,
74 "hlc=<value>[,...]", "Matches the high layer capability(s)."},
75 { "file", MATCH_FILE, COND_TYPE_STRING,
76 "file=<path>[,...]", "Mathes is the given file exists and if the first character is '1'."},
77 { "execute", MATCH_EXECUTE, COND_TYPE_STRING,
78 "execute=<command>[,...]","Matches if the return value of the given command is greater 0."},
79 { "default", MATCH_DEFAULT, COND_TYPE_NULL,
80 "default","Matches if no further dialing could match."},
81 { "timeout", MATCH_TIMEOUT, COND_TYPE_INTEGER,
82 "timeout=<seconds>","Matches if the ruleset was entered AFTER given seconds."},
83 { "free", MATCH_FREE, COND_TYPE_IFATTR,
84 "free=<interface>:<channel>","Matches if the given minimum of channels are free."},
85 { "notfree", MATCH_NOTFREE, COND_TYPE_IFATTR,
86 "notfree=<interface>:<channel>","Matches if NOT the given minimum of channels are free."},
87 { "blocked", MATCH_DOWN, COND_TYPE_STRING,
88 "blocked=<interfaces>[,...]","Matches if all of the given interfaces are blocked."},
89 { "idle", MATCH_UP, COND_TYPE_STRING,
90 "idle=<interface>[,...]","Matches if any of the given interfaces is idle."},
91 { "busy", MATCH_BUSY, COND_TYPE_STRING,
92 "busy=<extension>[,...]","Matches if any of the given extension is busy."},
93 { "notbusy", MATCH_IDLE, COND_TYPE_STRING,
94 "notbusy<extension>[,...]","Matches if any of the given extension is not busy."},
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 "capability=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 "timeout", PARAM_TYPE_INTEGER,
221 "timeout=<seconds>", "Timeout before continue with next action."},
223 "nopassword", PARAM_TYPE_NULL,
224 "nopassword", "Don't ask for password. Be sure to authenticate right via real caller ID."},
225 { 0, NULL, 0, NULL, NULL}
228 struct action_defs action_defs[] = {
230 "extern", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
231 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,
232 "Call is routed to extern number as dialed."},
234 "intern", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_internal, &EndpointAppPBX::action_hangup_call,
235 PARAM_CONNECT | PARAM_EXTENSION | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_TIMEOUT,
236 "Call is routed to intern extension as given by the dialed number or specified by option."},
238 "outdial", &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
239 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,
242 "asterisk", &EndpointAppPBX::action_init_chan, &EndpointAppPBX::action_dialing_chan, &EndpointAppPBX::action_hangup_call,
243 PARAM_CONNECT | PARAM_TIMEOUT,
244 "Call is routed to Asterisk via channel driver."},
245 { ACTION_VBOX_RECORD,
246 "vbox-record",&EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_vbox_record, &EndpointAppPBX::action_hangup_call,
247 PARAM_CONNECT | PARAM_EXTENSION | PARAM_ANNOUNCEMENT | PARAM_TIMEOUT,
248 "Caller is routed to the voice box of given extension."},
250 "partyline",&EndpointAppPBX::action_init_partyline, NULL, &EndpointAppPBX::action_hangup_call,
252 "Caller is participating the conference with the given room number."},
254 "login", NULL, &EndpointAppPBX::action_dialing_login, NULL,
255 PARAM_CONNECT | PARAM_EXTENSION | PARAM_NOPASSWORD,
256 "Log into the given extension. Password required."},
258 "callerid", &EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_callerid, NULL,
259 PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
260 "Caller changes the caller ID for all calls."},
261 { ACTION_CALLERIDNEXT,
262 "calleridnext",&EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_calleridnext, NULL,
263 PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
264 "Caller changes the caller ID for the next call."},
266 "forward", &EndpointAppPBX::action_init_change_forward, &EndpointAppPBX::action_dialing_forward, NULL,
267 PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_DELAY,
268 "Caller changes the diversion of given type to the given destination or voice box."},
270 "redial", &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_redial, NULL,
271 PARAM_CONNECT | PARAM_SELECT,
272 "Caller redials. (last outgoing call(s))"},
274 "reply", &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_reply, NULL,
275 PARAM_CONNECT | PARAM_SELECT,
276 "Caller replies. (last incomming call(s))"},
278 "powerdial", NULL, &EndpointAppPBX::action_dialing_powerdial, NULL,
279 PARAM_CONNECT | PARAM_DELAY | PARAM_LIMIT | PARAM_TIMEOUT,
280 "Caller redials using powerdialing."},
282 "callback", NULL, &EndpointAppPBX::action_dialing_callback, &EndpointAppPBX::action_hangup_callback,
283 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_EXTENSION | PARAM_DELAY | PARAM_CALLTO | PARAM_PREFIX,
284 "Caller will use the callback service. After disconnecting, the callback is triggered."},
286 "abbrev", NULL, &EndpointAppPBX::action_dialing_abbrev, NULL,
288 "Caller dials abbreviation."},
290 "test", NULL, &EndpointAppPBX::action_dialing_test, NULL,
291 PARAM_CONNECT | PARAM_PREFIX | PARAM_TIMEOUT,
292 "Caller dials test mode."},
294 "play", &EndpointAppPBX::action_init_play, NULL, NULL,
295 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_SAMPLE | PARAM_TIMEOUT,
296 "Plays the given sample."},
298 "vbox-play", &EndpointAppPBX::action_init_vbox_play, &EndpointAppPBX::action_dialing_vbox_play, NULL,
300 "Caller listens to her voice box or to given extension."},
302 "calculator", NULL, &EndpointAppPBX::action_dialing_calculator, NULL,
304 "Caller calls the calculator."},
306 "timer", NULL, &EndpointAppPBX::action_dialing_timer, NULL,
307 PARAM_CONNECT | PARAM_TPRESET | PARAM_TIMEOUT,
309 // "Caller calls the timer."},
311 "goto", NULL, &EndpointAppPBX::action_dialing_goto, NULL,
312 PARAM_CONNECT | PARAM_RULESET | PARAM_SAMPLE,
313 "Jump to given ruleset and optionally play sample. Dialed digits are not flushed."},
315 "menu", NULL, &EndpointAppPBX::action_dialing_menu, NULL,
316 PARAM_CONNECT | PARAM_RULESET | PARAM_SAMPLE,
317 "Same as 'goto', but flushes all digits dialed so far."},
319 "disconnect", NULL, &EndpointAppPBX::action_dialing_disconnect, NULL,
320 PARAM_CONNECT | PARAM_CAUSE | PARAM_LOCATION | PARAM_SAMPLE | PARAM_DISPLAY,
321 "Caller gets disconnected optionally with given cause and given sample and given display text."},
323 "help", NULL, &EndpointAppPBX::action_dialing_help, NULL,
324 PARAM_CONNECT | PARAM_TIMEOUT,
326 // "Caller will be able to select from current rules that would match. (using * and #)"},
328 "deflect", NULL, &EndpointAppPBX::action_dialing_deflect, NULL,
331 // "External call is deflected to the given destination within the telephone network."},
333 "setforward", NULL, &EndpointAppPBX::action_dialing_setforward, NULL,
334 PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_PORT,
336 // "The call forward is set within the telephone network of the external line."},
338 "execute", NULL, NULL, &EndpointAppPBX::action_hangup_execute,
339 PARAM_CONNECT | PARAM_EXECUTE | PARAM_PARAM,
340 "Executes the given script file. The file must terminate quickly, because it will halt the PBX."},
342 "file", NULL, NULL, &EndpointAppPBX::action_hangup_file,
343 PARAM_CONNECT | PARAM_FILE | PARAM_CONTENT | PARAM_APPEND,
344 "Writes givent content to given file. If content is not given, the dialed digits are written."},
346 "pick", &EndpointAppPBX::action_init_pick, NULL, NULL,
348 "Pick up a call that is ringing on any phone. Extensions may be given to limit the picking ability."},
350 "password", NULL, &EndpointAppPBX::action_dialing_password, NULL,
353 { ACTION_PASSWORD_WRITE,
354 "password_wr",NULL, &EndpointAppPBX::action_dialing_password_wr, NULL,
358 "nothing", NULL, NULL, NULL,
359 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_TIMEOUT,
360 "does nothing. Usefull to wait for calls to be released completely, by giving timeout value."},
362 "efi", &EndpointAppPBX::action_init_efi, NULL, NULL,
363 PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT,
364 "Elektronische Fernsprecher Identifikation."},
366 NULL, NULL, NULL, NULL, 0, NULL}
370 /* display documentation of rules */
372 void doc_rules(const char *name)
379 while(action_defs[i].name)
381 if (!strcasecmp(action_defs[i].name, name))
385 if (!action_defs[i].name)
387 fprintf(stderr, "Given action '%s' unknown.\n", name);
390 name = action_defs[i].name;
393 printf("Syntax overview:\n");
394 printf("----------------\n\n");
395 printf("[ruleset]\n");
396 printf("<condition> ... : <action> [parameter ...] [timeout=X : <action> ...]\n");
398 printf("Please refer to the documentation for description on rule format.\n\n");
402 printf("Available conditions to match:\n");
403 printf("------------------------------\n\n");
405 while(cond_defs[i].name)
407 printf("Usage: %s\n", cond_defs[i].doc);
408 printf("%s\n\n", cond_defs[i].help);
412 printf("Available actions with their parameters:\n");
413 printf("----------------------------------------\n\n");
416 printf("Detailes parameter description of action:\n");
417 printf("-----------------------------------------\n\n");
420 while(action_defs[i].name)
422 if (name && !!strcmp(action_defs[i].name,name)) /* not selected */
427 if (!action_defs[i].help) /* not internal actions */
432 printf("Usage: %s", action_defs[i].name);
436 if ((1LL<<j) & action_defs[i].params)
437 printf(" [%s]", param_defs[j].doc);
440 printf("\n%s\n\n", action_defs[i].help);
441 if (name) /* only show parameter help for specific action */
446 if ((1LL<<j) & action_defs[i].params)
447 printf("%s:\n\t%s\n", param_defs[j].doc, param_defs[j].help);
456 void ruleset_free(struct route_ruleset *ruleset_start)
458 struct route_ruleset *ruleset;
459 struct route_rule *rule;
460 struct route_cond *cond;
461 struct route_action *action;
462 struct route_param *param;
466 ruleset = ruleset_start;
467 ruleset_start = ruleset->next;
468 while(ruleset->rule_first)
470 rule = ruleset->rule_first;
471 ruleset->rule_first = rule->next;
472 while(rule->cond_first)
474 cond = rule->cond_first;
475 if (cond->string_value)
477 free(cond->string_value);
480 if (cond->string_value_to)
482 free(cond->string_value_to);
485 rule->cond_first = cond->next;
489 while(rule->action_first)
491 action = rule->action_first;
492 rule->action_first = action->next;
493 while(action->param_first)
495 param = action->param_first;
496 action->param_first = param->next;
497 if (param->string_value)
499 free(param->string_value);
516 void ruleset_debug(struct route_ruleset *ruleset_start)
518 struct route_ruleset *ruleset;
519 struct route_rule *rule;
520 struct route_cond *cond;
521 struct route_action *action;
522 struct route_param *param;
525 ruleset = ruleset_start;
528 printf("Ruleset: '%s'\n", ruleset->name);
529 rule = ruleset->rule_first;
534 cond = rule->cond_first;
538 printf(" Condition:");
542 printf(" %s", cond_defs[cond->index].name);
543 if (cond->value_type != VALUE_TYPE_NULL)
546 switch(cond->value_type)
548 case VALUE_TYPE_NULL:
551 case VALUE_TYPE_INTEGER:
552 printf("%d", cond->integer_value);
555 case VALUE_TYPE_INTEGER_RANGE:
556 printf("%d-%d", cond->integer_value, cond->integer_value_to);
559 case VALUE_TYPE_STRING:
560 printf("'%s'", cond->string_value);
563 case VALUE_TYPE_STRING_RANGE:
564 printf("'%s'-'%s'", cond->string_value, cond->string_value_to);
568 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", cond->value_type, __FUNCTION__, __LINE__);
570 if (cond->value_extension && cond->next)
574 goto next_cond_value;
582 action = rule->action_first;
585 printf(" Action: %s\n", action_defs[action->index].name);
588 param = action->param_first;
596 printf(" %s", param_defs[param->index].name);
597 if (param->value_type != VALUE_TYPE_NULL)
599 switch(param->value_type)
601 case VALUE_TYPE_NULL:
604 case VALUE_TYPE_INTEGER:
605 if (param_defs[param->index].type == PARAM_TYPE_CALLERIDTYPE)
607 switch(param->integer_value)
609 case INFO_NTYPE_UNKNOWN:
612 case INFO_NTYPE_SUBSCRIBER:
613 printf("subscriber");
615 case INFO_NTYPE_NATIONAL:
618 case INFO_NTYPE_INTERNATIONAL:
619 printf("international");
622 printf("unknown(%d)", param->integer_value);
626 if (param_defs[param->index].type == PARAM_TYPE_CAPABILITY)
628 switch(param->integer_value)
639 case INFO_BC_DATARESTRICTED:
640 printf("digital-restricted");
642 case INFO_BC_DATAUNRESTRICTED:
643 printf("digital-unrestricted");
645 case INFO_BC_DATAUNRESTRICTED_TONES:
646 printf("digital-unrestricted-tones");
649 printf("unknown(%d)", param->integer_value);
653 if (param_defs[param->index].type == PARAM_TYPE_DIVERSION)
655 switch(param->integer_value)
657 case INFO_DIVERSION_CFU:
660 case INFO_DIVERSION_CFNR:
663 case INFO_DIVERSION_CFB:
666 case INFO_DIVERSION_CFP:
670 printf("unknown(%d)", param->integer_value);
674 if (param_defs[param->index].type == PARAM_TYPE_TYPE)
676 switch(param->integer_value)
678 case INFO_NTYPE_UNKNOWN:
681 case INFO_NTYPE_SUBSCRIBER:
682 printf("subscriber");
684 case INFO_NTYPE_NATIONAL:
687 case INFO_NTYPE_INTERNATIONAL:
688 printf("international");
691 printf("unknown(%d)", param->integer_value);
695 if (param_defs[param->index].type == PARAM_TYPE_YESNO)
697 switch(param->integer_value)
706 printf("unknown(%d)", param->integer_value);
710 if (param_defs[param->index].type == PARAM_TYPE_NULL)
714 printf("%d", param->integer_value);
717 case VALUE_TYPE_STRING:
718 printf("'%s'", param->string_value);
722 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", param->value_type, __FUNCTION__, __LINE__);
729 printf(" Timeout: %d\n", action->timeout);
730 action = action->next;
736 ruleset = ruleset->next;
744 static char *read_string(char *p, char *key, int key_size, char *special)
770 UPRINT(key, "\001String too long.");
775 UPRINT(key, "\001Unexpected end of line inside quotes.");
782 if (strchr(special, *p))
792 UPRINT(key, "\001Unexpected end of line.");
798 UPRINT(key, "\001String too long.");
806 char ruleset_error[256];
807 struct route_ruleset *ruleset_parse(void)
812 unsigned long long j;
816 FILE *fp[MAXNESTING];
817 char filename[MAXNESTING][256];
818 int line[MAXNESTING];
825 int expecting = 1; /* 1 = expecting ruleset */
829 integer_to; /* condition index, .. */
830 struct route_ruleset *ruleset_start = NULL, *ruleset;
831 struct route_ruleset **ruleset_pointer = &ruleset_start;
832 struct route_rule *rule;
833 struct route_rule **rule_pointer = NULL;
834 struct route_cond *cond;
835 struct route_cond **cond_pointer = NULL;
836 struct route_action *action;
837 struct route_action **action_pointer = NULL;
838 struct route_param *param;
839 struct route_param **param_pointer = NULL;
842 /* check the integrity of IDs for ACTION_* and PARAM_* */
844 while(action_defs[i].name)
846 if (action_defs[i].id != i)
848 PERROR("Software Error action '%s' must have id of %d, but has %d.\n",
849 action_defs[i].name, i, action_defs[i].id);
855 while(param_defs[i].name)
857 if (param_defs[i].id != j)
859 PERROR("Software Error param '%s' must have id of 0x%llx, but has 0x%llx.\n",
860 param_defs[i].name, j, param_defs[i].id);
867 SPRINT(filename[0], "%s/routing.conf", INSTALL_DATA);
869 if (!(fp[0]=fopen(filename[0],"r")))
871 PERROR("Cannot open %s\n",filename[0]);
880 while((fgets(buffer,sizeof(buffer),fp[nesting])))
883 buffer[sizeof(buffer)-1]=0;
884 if (buffer[0]) buffer[strlen(buffer)-1]=0;
895 /* skip spaces, if any */
907 /* don't skip "define" */
908 if (!!strncmp(p, "define", 6))
921 p = read_string(p, key, sizeof(key), " ");
922 if (key[0] == 1) /* error */
924 SPRINT(failure, "Parsing Filename failed: %s", key+1);
927 if (nesting == MAXNESTING-1)
929 SPRINT(failure, "'include' is nesting too deep.\n");
933 SCPY(filename[nesting+1], key);
935 SPRINT(filename[nesting+1], "%s/%s", INSTALL_DATA, key);
936 if (!(fp[nesting+1]=fopen(filename[nesting+1],"r")))
938 PERROR("Cannot open %s\n", filename[nesting+1]);
945 if (*p == '/') if (p[1] == '/')
948 /* skip empty lines */
952 /* expecting ruleset */
959 SPRINT(failure, "Expecting ruleset name starting with '['.");
964 /* reading ruleset name text */
966 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
968 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
970 if (i == sizeof(key)) i--; /* limit */
973 if (key[0] == '\0') {
974 SPRINT(failure, "Missing ruleset name after '['.");
978 /* expecting ] and nothing more */
980 SPRINT(failure, "Expecting ']' after ruleset name.");
985 SPRINT(failure, "Unexpected character after ruleset name.");
989 /* check for duplicate rulesets */
990 ruleset = ruleset_start;
993 if (!strcmp(ruleset->name, key))
995 SPRINT(failure, "Duplicate ruleset '%s', already defined in file '%s' line %d.", key, ruleset->file, ruleset->line);
998 ruleset = ruleset->next;
1001 /* create ruleset */
1002 ruleset = (struct route_ruleset *)malloc(sizeof(struct route_ruleset));
1003 if (ruleset == NULL)
1005 SPRINT(failure, "Out of memory.");
1009 memset(ruleset, 0, sizeof(struct route_ruleset));
1010 *ruleset_pointer = ruleset;
1011 ruleset_pointer = &(ruleset->next);
1012 SCPY(ruleset->name, key);
1013 SCPY(ruleset->file, filename[nesting]);
1014 ruleset->line = line[nesting];
1015 rule_pointer = &(ruleset->rule_first);
1020 /* for new ruleset [ */
1026 /* alloc memory for rule */
1027 rule = (struct route_rule *)malloc(sizeof(struct route_rule));
1030 SPRINT(failure, "Out of memory.");
1034 memset(rule, 0, sizeof(struct route_rule));
1035 *rule_pointer = rule;
1036 rule_pointer = &(rule->next);
1037 cond_pointer = &(rule->cond_first);
1038 action_pointer = &(rule->action_first);
1039 SCPY(rule->file, filename[nesting]);
1040 rule->line = line[nesting];
1042 /* loop CONDITIONS */
1043 while(*p!=':' && *p!='\0')
1045 /* read item text */
1047 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1049 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1051 if (i == sizeof(key)) i--; /* limit */
1056 SPRINT(failure, "Expecting condition item name or ':' for end of condition list.");
1059 if (*p!=' ' && *p!='=')
1061 SPRINT(failure, "Illegal character '%c' after condition name '%s'. Expecting '=' for equation or ' ' to seperate condition items.", *p, key);
1065 /* check if condition exists */
1067 while(cond_defs[index].name)
1069 if (!strcmp(cond_defs[index].name, key))
1073 if (cond_defs[index].name == NULL)
1075 SPRINT(failure, "Unknown condition item name '%s'.", key);
1079 /* items without values must not have any parameter */
1080 if (cond_defs[index].type == COND_TYPE_NULL)
1084 SPRINT(failure, "Condition item '%s' must not have any value. Don't use '=' for this type of condition.", key);
1089 SPRINT(failure, "Condition item '%s' must not have any value. Expecting ' ' or tab after item name.", key);
1097 SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected, and not a space.", key);
1102 SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected.", key);
1108 /* check for duplicate condition */
1109 cond = rule->cond_first;
1112 if (cond->index == index)
1114 SPRINT(failure, "Duplicate condition '%s', use ',' to give multiple values.", key);
1121 /* alloc memory for item */
1122 cond = (struct route_cond *)malloc(sizeof(struct route_cond));
1125 SPRINT(failure, "Out of memory.");
1129 memset(cond, 0, sizeof(struct route_cond));
1130 *cond_pointer = cond;
1131 cond_pointer = &(cond->next);
1132 cond->index = index;
1133 cond->match = cond_defs[index].match;
1134 switch(cond_defs[index].type)
1136 case COND_TYPE_NULL:
1139 SPRINT(failure, "Expecting no value.");
1142 value_type = VALUE_TYPE_NULL;
1145 /* parse all integer values/ranges */
1146 case COND_TYPE_INTEGER:
1147 case COND_TYPE_TIME:
1148 case COND_TYPE_MDAY:
1149 case COND_TYPE_MONTH:
1150 case COND_TYPE_WDAY:
1151 case COND_TYPE_YEAR:
1152 integer = integer_to = 0;
1153 if (*p==',' || *p==' ' || *p=='\0')
1155 SPRINT(failure, "Missing integer value.");
1158 while(*p>='0' && *p<='9')
1160 integer = integer*10 + *p-'0';
1163 value_type = VALUE_TYPE_INTEGER;
1167 if (*p==',' || *p==' ' || *p=='\0')
1169 SPRINT(failure, "Missing integer value.");
1172 while(*p>='0' && *p<='9')
1174 integer_to = integer_to*10 + *p-'0';
1177 value_type = VALUE_TYPE_INTEGER_RANGE;
1179 if (cond_defs[index].type == COND_TYPE_TIME)
1181 // Simon: i store the time as decimal, later i compare it correctly:
1182 // hours * 100 + minutes
1183 if (integer == 2400)
1185 if (integer >= 2400)
1188 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer);
1191 if (integer%100 >= 60)
1192 goto timeoutofrange1;
1193 if (value_type == VALUE_TYPE_INTEGER)
1195 if (integer_to == 2400)
1197 if (integer_to >= 2400)
1200 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer_to);
1203 if (integer_to%100 >= 60)
1204 goto timeoutofrange2;
1206 if (cond_defs[index].type == COND_TYPE_MDAY)
1208 if (integer<1 || integer>31)
1210 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer);
1213 if (value_type == VALUE_TYPE_INTEGER)
1215 if (integer_to<1 || integer_to>31)
1217 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer_to);
1221 if (cond_defs[index].type == COND_TYPE_WDAY)
1223 if (integer<1 || integer>7)
1225 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer);
1228 if (value_type == VALUE_TYPE_INTEGER)
1230 if (integer_to<1 || integer_to>7)
1232 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer_to);
1236 if (cond_defs[index].type == COND_TYPE_MONTH)
1238 if (integer<1 || integer>12)
1240 SPRINT(failure, "Given month '%d' not in range 1..12", integer);
1243 if (value_type == VALUE_TYPE_INTEGER)
1245 if (integer_to<1 || integer_to>12)
1247 SPRINT(failure, "Given month '%d' not in range 1..12", integer_to);
1251 if (cond_defs[index].type == COND_TYPE_YEAR)
1253 if (integer<1970 || integer>2106)
1255 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer);
1258 if (value_type == VALUE_TYPE_INTEGER)
1260 if (integer_to<1970 || integer_to>2106)
1262 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer_to);
1267 cond->integer_value = integer;
1268 cond->integer_value_to = integer_to;
1269 cond->value_type = value_type;
1272 /* parse all string values/ranges */
1273 case COND_TYPE_STRING:
1274 key[0] = key_to[0] = '\0';
1275 if (*p==',' || *p==' ' || *p=='\0')
1277 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1280 p = read_string(p, key, sizeof(key), "-, ");
1281 if (key[0] == 1) /* error */
1283 SPRINT(failure, "Parsing String failed: %s", key+1);
1286 value_type = VALUE_TYPE_STRING;
1290 if (*p==',' || *p==' ' || *p=='\0')
1292 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1295 p = read_string(p, key_to, sizeof(key_to), "-, ");
1296 if (key_to[0] == 1) /* error */
1298 SPRINT(failure, "Parsing string failed: %s", key_to+1);
1301 value_type = VALUE_TYPE_STRING_RANGE;
1302 if (strlen(key) != strlen(key_to))
1304 SPRINT(failure, "Given range of strings \"%s\"-\"%s\" have unequal length.", key, key_to);
1309 SPRINT(failure, "Given range has no length.");
1314 cond->string_value = (char *)malloc(strlen(key)+1);
1315 if (cond->string_value == NULL)
1317 SPRINT(failure, "Out of memory.");
1321 UCPY(cond->string_value, key);
1322 if (value_type == VALUE_TYPE_STRING_RANGE)
1324 cond->string_value_to = (char *)malloc(strlen(key_to)+1);
1325 if (cond->string_value_to == NULL)
1327 SPRINT(failure, "Out of memory.");
1331 UCPY(cond->string_value_to, key_to);
1332 cond->comp_string = strcmp(key, key_to);
1334 cond->value_type = value_type;
1337 /* parse service value */
1338 case COND_TYPE_CAPABILITY:
1339 if (!strncasecmp("speech", p, 6))
1340 cond->integer_value = INFO_BC_SPEECH;
1341 else if (!strncasecmp("audio", p, 5))
1342 cond->integer_value = INFO_BC_AUDIO;
1343 else if (!strncasecmp("video", p, 5))
1344 cond->integer_value = INFO_BC_VIDEO;
1345 else if (!strncasecmp("digital-restricted", p, 18))
1346 cond->integer_value = INFO_BC_DATARESTRICTED;
1347 else if (!strncasecmp("digital-unrestricted", p, 20))
1348 cond->integer_value = INFO_BC_DATAUNRESTRICTED;
1349 else if (!strncasecmp("digital-unrestricted-tones", p, 26))
1350 cond->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1353 SPRINT(failure, "Given service type is invalid or misspelled.");
1356 cond->value_type = VALUE_TYPE_INTEGER;
1359 /* parse bmode value */
1360 case COND_TYPE_BMODE:
1361 if (!strncasecmp("transparent", p, 11))
1362 cond->integer_value = INFO_BMODE_CIRCUIT;
1363 else if (!strncasecmp("hdlc", p, 4))
1364 cond->integer_value = INFO_BMODE_PACKET;
1367 SPRINT(failure, "Given bchannel mode is invalid or misspelled.");
1370 cond->value_type = VALUE_TYPE_INTEGER;
1373 /* parse interface attribute <if>:<value> */
1374 case COND_TYPE_IFATTR:
1375 key[0] = key_to[0] = '\0';
1376 if (*p==':' || *p==',' || *p==' ' || *p=='\0')
1378 SPRINT(failure, "Missing interface name.");
1381 p = read_string(p, key, sizeof(key), ":-, ");
1382 if (key[0] == 1) /* error */
1384 SPRINT(failure, "Parsing interface failed: %s", key+1);
1389 SPRINT(failure, "Expeciting kolon to seperate value behind interface name.");
1393 while(*p>='0' && *p<='9')
1397 if (*p!=',' && *p!=' ' && *p!='\0')
1399 SPRINT(failure, "Invalid characters behind value.");
1402 value_type = VALUE_TYPE_STRING;
1407 SPRINT(failure, "Software error: COND_TYPE_* %d not parsed in function '%s'", cond_defs[index].type, __FUNCTION__);
1410 /* if we have another value for that item, we attach it */
1415 cond->value_extension = 1;
1418 /* to seperate the items, a space is required */
1421 SPRINT(failure, "Character '%c' not expected here. Use ',' to seperate multiple possible values.", *p);
1433 /* we are done with CONDITIONS, so we expect the ACTION */
1436 SPRINT(failure, "Expecting ':' after condition item(s).");
1442 /* skip spaces, if any */
1450 /* read action name */
1452 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9') || *p == '-')
1454 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1456 if (i == sizeof(key)) i--; /* limit */
1459 if (key[0] == '\0') {
1460 SPRINT(failure, "Expecting action name.");
1464 /* check if item exists */
1466 while(action_defs[index].name)
1468 if (!action_defs[index].help) /* not internal actions */
1473 if (!strcmp(action_defs[index].name, key))
1477 if (action_defs[index].name == NULL)
1479 SPRINT(failure, "Unknown action name '%s'.", key);
1483 /* alloc memory for action */
1484 action = (struct route_action *)malloc(sizeof(struct route_action));
1487 SPRINT(failure, "Out of memory.");
1491 memset(action, 0, sizeof(struct route_action));
1492 *action_pointer = action;
1493 action_pointer = &(action->next);
1494 param_pointer = &(action->param_first);
1495 action->index = index;
1496 action->line = line[nesting];
1498 /* skip spaces after param name */
1509 /* read param text */
1511 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1513 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1515 if (i == sizeof(key)) i--; /* limit */
1518 if (key[0] == '\0') {
1519 SPRINT(failure, "Expecting parameter name.");
1523 /* check if item exists */
1525 while(param_defs[index].name)
1527 if (!strcmp(param_defs[index].name, key))
1531 if (param_defs[index].name == NULL)
1533 SPRINT(failure, "Unknown param name '%s'.", key);
1537 /* params without values must not have any parameter */
1538 if (param_defs[index].type == PARAM_TYPE_NULL)
1540 if (*p!=' ' && *p!='\0')
1542 SPRINT(failure, "Parameter '%s' must not have any value.", key);
1549 SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected and not a space.", key);
1554 SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected.", key);
1560 /* special timeout value */
1561 if (!strcmp("timeout", key))
1563 if (action->timeout)
1565 SPRINT(failure, "Duplicate timeout value.");
1568 if (*p==',' || *p==' ' || *p=='\0')
1570 SPRINT(failure, "Missing integer value.");
1574 while(*p>='0' && *p<='9')
1576 integer = integer*10 + *p-'0';
1581 SPRINT(failure, "Expecting timeout value greater 0.");
1584 if (*p!=' ' && *p!='\0')
1586 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters.", *p);
1596 action->timeout = integer;
1597 /* check for next ACTION */
1606 /* check for duplicate parameters */
1607 param = action->param_first;
1610 if (param->index == index)
1612 SPRINT(failure, "Duplicate parameter '%s', use ',' to give multiple values.", key);
1615 param = param->next;
1619 /* alloc memory for param */
1620 param = (struct route_param *)malloc(sizeof(struct route_param));
1623 SPRINT(failure, "Out of memory.");
1627 memset(param, 0, sizeof(struct route_param));
1628 *param_pointer = param;
1629 param_pointer = &(param->next);
1630 param->index = index;
1631 param->id = param_defs[index].id;
1633 switch(param_defs[index].type)
1635 /* parse null value */
1636 case PARAM_TYPE_NULL:
1637 param->value_type = VALUE_TYPE_NULL;
1640 /* parse integer value */
1641 case PARAM_TYPE_INTEGER:
1643 if (*p==',' || *p==' ' || *p=='\0')
1645 SPRINT(failure, "Missing integer value.");
1648 while(*p>='0' && *p<='9')
1650 integer = integer*10 + *p-'0';
1653 param->integer_value = integer;
1654 param->value_type = VALUE_TYPE_INTEGER;
1658 /* parse ports value */
1659 case PARAM_TYPE_PORTS:
1661 if (*p==',' || *p==' ' || *p=='\0')
1663 SPRINT(failure, "Missing port number, omit parameter or give port number.");
1669 while(*p>='0' && *p<='9')
1671 if (i < (int)sizeof(key)-1)
1676 integer = integer*10 + *p-'0';
1681 SPRINT(failure, "Port number too high.");
1686 if (i < (int)sizeof(key)-1)
1697 /* parse string value */
1698 case PARAM_TYPE_STRING:
1699 case PARAM_TYPE_CALLERIDTYPE:
1700 case PARAM_TYPE_CAPABILITY:
1701 case PARAM_TYPE_BMODE:
1702 case PARAM_TYPE_DIVERSION:
1703 case PARAM_TYPE_DESTIN:
1704 case PARAM_TYPE_TYPE:
1705 case PARAM_TYPE_YESNO:
1707 if (*p==',' || *p==' ' || *p=='\0')
1709 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1712 p = read_string(p, key, sizeof(key), " ");
1713 if (key[0] == 1) /* error */
1715 SPRINT(failure, "Parsing string failed: %s", key+1);
1718 if (param_defs[index].type == PARAM_TYPE_CALLERIDTYPE)
1720 param->value_type = VALUE_TYPE_INTEGER;
1721 if (!strcasecmp(key, "unknown"))
1723 param->integer_value = INFO_NTYPE_UNKNOWN;
1726 if (!strcasecmp(key, "subscriber"))
1728 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1731 if (!strcasecmp(key, "national"))
1733 param->integer_value = INFO_NTYPE_NATIONAL;
1736 if (!strcasecmp(key, "international"))
1738 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1741 SPRINT(failure, "Caller ID type '%s' unknown.", key);
1744 if (param_defs[index].type == PARAM_TYPE_CAPABILITY)
1746 param->value_type = VALUE_TYPE_INTEGER;
1747 if (!strcasecmp(key, "speech"))
1749 param->integer_value = INFO_BC_SPEECH;
1752 if (!strcasecmp(key, "audio"))
1754 param->integer_value = INFO_BC_AUDIO;
1757 if (!strcasecmp(key, "video"))
1759 param->integer_value = INFO_BC_VIDEO;
1762 if (!strcasecmp(key, "digital-restricted"))
1764 param->integer_value = INFO_BC_DATARESTRICTED;
1767 if (!strcasecmp(key, "digital-unrestricted"))
1769 param->integer_value = INFO_BC_DATAUNRESTRICTED;
1772 if (!strcasecmp(key, "digital-unrestricted-tones"))
1774 param->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1777 SPRINT(failure, "Service type '%s' unknown.", key);
1780 if (param_defs[index].type == PARAM_TYPE_BMODE)
1782 param->value_type = VALUE_TYPE_INTEGER;
1783 if (!strcasecmp(key, "transparent"))
1785 param->integer_value = INFO_BMODE_CIRCUIT;
1788 if (!strcasecmp(key, "hdlc"))
1790 param->integer_value = INFO_BMODE_PACKET;
1793 SPRINT(failure, "Bchannel mode '%s' unknown.", key);
1796 if (param_defs[index].type == PARAM_TYPE_DIVERSION)
1798 param->value_type = VALUE_TYPE_INTEGER;
1799 if (!strcasecmp(key, "cfu"))
1801 param->integer_value = INFO_DIVERSION_CFU;
1804 if (!strcasecmp(key, "cfb"))
1806 param->integer_value = INFO_DIVERSION_CFB;
1809 if (!strcasecmp(key, "cfnr"))
1811 param->integer_value = INFO_DIVERSION_CFNR;
1814 if (!strcasecmp(key, "cfp"))
1816 param->integer_value = INFO_DIVERSION_CFP;
1819 SPRINT(failure, "Diversion type '%s' unknown.", key);
1822 if (param_defs[index].type == PARAM_TYPE_TYPE)
1824 param->value_type = VALUE_TYPE_INTEGER;
1825 if (!strcasecmp(key, "unknown"))
1827 param->integer_value = INFO_NTYPE_UNKNOWN;
1830 if (!strcasecmp(key, "subscriber"))
1832 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1835 if (!strcasecmp(key, "national"))
1837 param->integer_value = INFO_NTYPE_NATIONAL;
1840 if (!strcasecmp(key, "international"))
1842 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1845 SPRINT(failure, "Number type '%s' unknown.", key);
1848 if (param_defs[index].type == PARAM_TYPE_YESNO)
1850 param->value_type = VALUE_TYPE_INTEGER;
1851 if (!strcasecmp(key, "yes"))
1853 param->integer_value = 1;
1856 if (!strcasecmp(key, "no"))
1858 param->integer_value = 0;
1861 SPRINT(failure, "Value '%s' unknown. ('yes' or 'no')", key);
1864 param->string_value = (char *)malloc(strlen(key)+1);
1865 if (param->string_value == NULL)
1867 SPRINT(failure, "Out of memory.");
1871 UCPY(param->string_value, key);
1872 param->value_type = VALUE_TYPE_STRING;
1876 SPRINT(failure, "Software error: PARAM_TYPE_* %d not parsed in function '%s'", param_defs[index].type, __FUNCTION__);
1884 param->value_extension = 1;
1885 goto nextparamvalue;
1892 /* to seperate the items, a space is required */
1895 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters, or ',' for multiple values.", *p);
1906 /* check for next ACTION */
1915 fclose(fp[nesting--]);
1923 SPRINT(failure, "No ruleset defined.");
1925 return(ruleset_start);
1928 printf("While parsing %s, an error occurred in line %d:\n", filename[nesting], line[nesting]);
1929 printf("-> %s\n", buffer);
1930 memset(pointer, ' ', sizeof(pointer));
1931 pointer[p-buffer] = '^';
1932 pointer[p-buffer+1] = '\0';
1933 printf(" %s\n", pointer);
1934 printf("%s\n", failure);
1935 SPRINT(ruleset_error, "Error in file %s, line %d: %s", filename[nesting], line[nesting], failure);
1940 fclose(fp[nesting--]);
1944 ruleset_free(ruleset_start);
1949 * return ruleset by name
1951 struct route_ruleset *getrulesetbyname(char *name)
1953 struct route_ruleset *ruleset = ruleset_first;
1957 if (!strcasecmp(name, ruleset->name))
1961 ruleset = ruleset->next;
1963 PDEBUG(DEBUG_ROUTE, "ruleset %s %s.\n", name, ruleset?"found":"not found");
1968 * parses the current ruleset and returns action
1970 struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
1973 couldmatch = 0, /* any rule could match */
1980 struct route_rule *rule = ruleset->rule_first;
1981 struct route_cond *cond;
1982 struct route_action *action = NULL;
1983 unsigned long comp_len;
1985 char callerid[64], redirid[64];
1990 struct mISDNport *mISDNport;
1992 /* reset timeout action */
1993 e_match_timeout = 0; /* no timeout */
1994 e_match_to_action = NULL;
1996 SCPY(callerid, numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype));
1997 SCPY(redirid, numberrize_callerinfo(e_redirinfo.id, e_redirinfo.ntype));
1999 PDEBUG(DEBUG_ROUTE, "parsing ruleset '%s'\n", ruleset->name);
2002 PDEBUG(DEBUG_ROUTE, "checking rule in line %d\n", rule->line);
2003 match = 1; /* this rule matches */
2004 dialing_required = 0;
2005 timeout = 0; /* timeout time */
2006 cond = rule->cond_first;
2009 condition = 0; /* any condition element is true (1) or could be true (2) */
2011 istrue = 0; /* this condition-element is true */
2012 couldbetrue = 0; /* this conditions-element could be true */
2016 if (!e_ext.number[0])
2021 if (e_ext.number[0])
2026 if (ea_endpoint->ep_portlist)
2027 if ((ea_endpoint->ep_portlist->port_type & PORT_CLASS_mISDN_MASK) != PORT_CLASS_mISDN_DSS1)
2029 integer = e_callerinfo.isdn_port;
2032 case MATCH_INTERFACE:
2033 if (!e_callerinfo.interface[0])
2035 string = e_callerinfo.interface;
2036 goto match_string_prefix;
2038 case MATCH_CALLERID:
2040 goto match_string_prefix;
2042 case MATCH_EXTENSION:
2043 string = e_ext.name;
2047 string = e_dialinginfo.number;
2048 goto match_string_prefix;
2060 case MATCH_ANONYMOUS:
2061 if (e_callerinfo.present != INFO_PRESENT_ALLOWED)
2066 if (e_callerinfo.present == INFO_PRESENT_ALLOWED)
2071 if (e_callerinfo.present == INFO_PRESENT_NOTAVAIL)
2075 case MATCH_AVAILABLE:
2076 if (e_callerinfo.present != INFO_PRESENT_NOTAVAIL)
2081 if (e_callerinfo.screen == INFO_SCREEN_USER)
2086 if (e_callerinfo.screen != INFO_SCREEN_USER)
2090 case MATCH_REDIRECTED:
2091 if (e_redirinfo.present != INFO_PRESENT_NULL)
2096 if (e_redirinfo.present == INFO_PRESENT_NULL)
2102 goto match_string_prefix;
2105 integer = now_tm->tm_hour*100 + now_tm->tm_min;
2109 integer = now_tm->tm_mday;
2113 integer = now_tm->tm_mon+1;
2117 integer = now_tm->tm_year + 1900;
2121 integer = now_tm->tm_wday;
2122 integer = integer?integer:7; /* correct sunday */
2125 case MATCH_CAPABILITY:
2126 integer = e_capainfo.bearer_capa;
2129 case MATCH_INFOLAYER1:
2130 integer = e_capainfo.bearer_info1;
2134 integer = e_capainfo.hlc;
2138 tfp = fopen(cond->string_value, "r");
2143 if (fgetc(tfp) == '1')
2149 if (system(cond->string_value) == 0)
2159 timeout = now_d + cond->integer_value;
2165 if (!(comp_len = (unsigned long)strchr(cond->string_value, ':')))
2167 comp_len = comp_len-(unsigned long)cond->string_value;
2169 mISDNport = mISDNport_first;
2172 if (mISDNport->ifport)
2173 if (strlen(mISDNport->ifport->interface->name) == comp_len)
2174 if (!strncasecmp(mISDNport->ifport->interface->name, cond->string_value, comp_len))
2175 if (!mISDNport->ptp || mISDNport->l2link)
2178 jj = mISDNport->b_num;
2182 if (mISDNport->b_state[j])
2187 mISDNport = mISDNport->next;
2189 if (cond->match == MATCH_FREE)
2191 if (avail >= atoi(cond->string_value + comp_len + 1))
2195 if (avail < atoi(cond->string_value + comp_len + 1))
2202 mISDNport = mISDNport_first;
2205 if (mISDNport->ifport)
2206 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2207 if (!mISDNport->ptp || mISDNport->l2link) /* break if one is up */
2209 mISDNport = mISDNport->next;
2211 if (!mISDNport) /* all down */
2216 mISDNport = mISDNport_first;
2219 if (mISDNport->ifport)
2220 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2221 if (!mISDNport->ptp || mISDNport->l2link) /* break if one is up */
2224 mISDNport = mISDNport->next;
2226 if (mISDNport) /* one link at least */
2232 mISDNport = mISDNport_first;
2235 if (mISDNport->ifport)
2236 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2237 if (mISDNport->use) /* break if in use */
2239 mISDNport = mISDNport->next;
2246 mISDNport = mISDNport_first;
2249 if (mISDNport->ifport)
2250 if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value))
2251 if (mISDNport->use) /* break if in use */
2253 mISDNport = mISDNport->next;
2260 PERROR("Software error: MATCH_* %d not parsed in function '%s'", cond->match, __FUNCTION__);
2264 if (cond->value_type == VALUE_TYPE_INTEGER)
2266 if (integer != cond->integer_value)
2271 if (cond->value_type == VALUE_TYPE_INTEGER_RANGE)
2273 /* check if negative range (2100 - 700 o'clock) */
2274 if (cond->integer_value > cond->integer_value_to)
2276 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2280 /* range is positive */
2281 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2288 if (strlen(cond->string_value) != strlen(string))
2291 match_string_prefix:
2292 comp_len = strlen(cond->string_value); /* because we must reach value's length */
2293 /* we must have greater or equal length to values */
2294 if ((unsigned long)strlen(string) < comp_len)
2296 /* special case for unfinished dialing */
2297 if (cond->match == MATCH_DIALING)
2299 couldbetrue = 1; /* could match */
2300 comp_len = strlen(string);
2306 /* on single string match */
2307 if (cond->value_type == VALUE_TYPE_STRING)
2309 if (!strncmp(string, cond->string_value, comp_len))
2312 /* must be set for changing 'e_extdialing' */
2313 if (cond->match == MATCH_DIALING)
2314 dialing_required = comp_len;
2319 /* on range match */
2320 if (cond->value_type == VALUE_TYPE_STRING_RANGE)
2322 /* check if negative range ("55"-"22") */
2323 if (cond->comp_string > 0)
2325 if (strncmp(string, cond->string_value, comp_len) >= 0)
2328 /* must be set for changing 'e_extdialing' */
2329 if (cond->match == MATCH_DIALING)
2330 dialing_required = comp_len;
2333 if (strncmp(string, cond->string_value_to, comp_len) <= 0)
2335 /* must be set for changing 'e_extdialing' */
2337 if (cond->match == MATCH_DIALING)
2338 dialing_required = comp_len;
2343 /* range is positive */
2344 if (strncmp(string, cond->string_value, comp_len) < 0)
2346 if (strncmp(string, cond->string_value_to, comp_len) > 0)
2349 if (cond->match == MATCH_DIALING)
2350 dialing_required = comp_len;
2356 /* set current condition */
2357 if (istrue && !couldbetrue)
2358 condition = 1; /* element matches, so condition matches */
2359 if (istrue && couldbetrue && !condition)
2360 condition = 2; /* element could match and other elements don't match, so condition could match */
2362 /* if not matching or could match */
2365 /* if we have more values to check */
2366 if (cond->value_extension && cond->next)
2369 goto checkextension;
2375 /* skip exteded values, beacuse we already have one matching value */
2376 while(cond->value_extension && cond->next)
2381 if (timeout>now_d && match==1) /* the matching rule with timeout in the future */
2382 if (e_match_timeout<1 || timeout<e_match_timeout) /* first timeout or lower */
2384 /* set timeout in the furture */
2385 e_match_timeout = timeout;
2386 e_match_to_action = rule->action_first;
2387 e_match_to_extdialing = e_dialinginfo.number + dialing_required;
2388 match = 0; /* matches in the future */
2392 /* matching, we return first action */
2393 action = rule->action_first;
2394 e_match_timeout = 0; /* no timeout */
2395 e_match_to_action = NULL;
2396 e_extdialing = e_dialinginfo.number + dialing_required;
2401 /* rule could match if more is dialed */
2410 * parses the current action's parameters and return them
2412 struct route_param *EndpointAppPBX::routeparam(struct route_action *action, unsigned long long id)
2414 struct route_param *param = action->param_first;
2418 if (param->id == id)
2420 param = param->next;
2427 * internal rules that are not defined by route.conf
2429 struct route_action action_password = {
2437 struct route_action action_password_write = {
2440 ACTION_PASSWORD_WRITE,
2445 struct route_action action_external = {
2453 struct route_action action_internal = {
2461 struct route_action action_chan = {
2469 struct route_action action_vbox = {
2477 struct route_action action_partyline = {
2485 struct route_action action_disconnect = {