unified socket application interface (for asterisk and maybe other apps)
[lcr.git] / route.c
1 /*****************************************************************************\ 
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** match processing of routing configuration                                 **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include "main.h"
17
18
19 struct route_ruleset    *ruleset_first;         /* first entry */
20 struct route_ruleset    *ruleset_main;          /* pointer to main ruleset */
21
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."},
95         { "remote",     MATCH_REMOTE,   COND_TYPE_STRING,
96           "remote=<application name>","Matches if remote application is running."},
97         { "notremote",  MATCH_NOTREMOTE,COND_TYPE_NULL,
98           "notremote=<application name>","Matches if remote application is not running."},
99         { NULL, 0, 0, NULL}
100 };
101
102 struct param_defs param_defs[] = {
103         { PARAM_PROCEEDING,
104           "proceeding", PARAM_TYPE_NULL,
105           "proceeding", "Will set the call into 'proceeding' state to prevent dial timeout."},
106         { PARAM_ALERTING,
107           "alerting",   PARAM_TYPE_NULL,
108           "alerting", "Will set the call into 'altering' state."},
109         { PARAM_CONNECT,
110           "connect",    PARAM_TYPE_NULL,
111           "connect", "Will complete the call before processing the action. Audio path for external calls will be established."},
112         { PARAM_EXTENSION,
113           "extension",  PARAM_TYPE_STRING,
114           "extension=<digits>", "Give extension name (digits) to relate this action to."},
115         { PARAM_EXTENSIONS,
116           "extensions", PARAM_TYPE_STRING,
117           "extensions=<extension>[,<extension>[,...]]", "One or more extensions may be given."},
118         { PARAM_PREFIX,
119           "prefix",     PARAM_TYPE_STRING,
120           "prefix=<digits>", "Add prefix in front of the dialed number."},
121         { PARAM_CAPA,
122           "capability", PARAM_TYPE_CAPABILITY,
123           "capability=speech|audio|video|digital-restricted|digital-unrestricted|digital-unrestricted-tones", "Alter the service type of the call."},
124         { PARAM_BMODE,
125           "bmode",      PARAM_TYPE_BMODE,
126           "capability=transparent|hdlc", "Alter the bchannel mode of the call. Use hdlc for data calls."},
127         { PARAM_INFO1,
128           "infolayer1", PARAM_TYPE_INTEGER,
129           "infolayer1=<value>", "Alter the layer 1 information of a call. Use 3 for ALAW or 2 for uLAW."},
130         { PARAM_HLC,
131           "hlc",        PARAM_TYPE_INTEGER,
132           "hlc=<value>", "Alter the HLC identification. Use 1 for telephony or omit."},
133         { PARAM_EXTHLC,
134           "exthlc",     PARAM_TYPE_INTEGER,
135           "exthlc=<value>", "Alter extended HLC value. (Mainenance only, don't use it.)"},
136         { PARAM_PRESENT,
137           "present",    PARAM_TYPE_YESNO,
138           "present=yes|no", "Allow or restrict caller ID regardless what the caller wants."},
139         { PARAM_DIVERSION,
140           "diversion",  PARAM_TYPE_DIVERSION,
141           "diversion=cfu|cfnr|cfb|cfp", "Set diversion type."},
142         { PARAM_DEST,
143           "dest",       PARAM_TYPE_DESTIN,
144           "dest=<string>", "Destination number to divert to. Use 'vbox' to divert to vbox. (cfu,cfnr,cfb only)"},
145         { PARAM_SELECT,
146           "select",     PARAM_TYPE_NULL,
147           "select", "Lets the caller select the history of calls using keys '1'/'3' or '*'/'#'."},
148         { PARAM_DELAY,
149           "delay",      PARAM_TYPE_INTEGER,
150           "delay=<seconds>", "Number of seconds to delay."},
151         { PARAM_LIMIT,
152           "limit",      PARAM_TYPE_INTEGER,
153           "limit=<retries>", "Number of maximum retries."},
154         { PARAM_HOST,
155           "host",       PARAM_TYPE_STRING,
156           "host=<string>", "Name of remote VoIP host."},
157         { PARAM_PORT,
158           "port",       PARAM_TYPE_STRING,
159           "port=<value>", "Alternate port to use if 'host' is given."},
160         { PARAM_INTERFACES,
161           "interfaces", PARAM_TYPE_STRING,
162           "interfaces=<interface>[,<interface>[,...]]", "Give one or a list of Interfaces to select a free channel from."},
163         { PARAM_ADDRESS,
164           "address",    PARAM_TYPE_STRING,
165           "address=<string>", "Complete VoIP address. ( [user@]host[:port] )"},
166         { PARAM_SAMPLE,
167           "sample",     PARAM_TYPE_STRING,
168           "sample=<file prefix>", "Filename of sample (current tone's dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
169         { PARAM_ANNOUNCEMENT,
170           "announcement",PARAM_TYPE_STRING,
171           "announcement=<file prefix>", "Filename of announcement (inside vbox recording dir) or full path to sample. ('.wav'/'.wave'/'.isdn' is added automatically."},
172         { PARAM_RULESET,
173           "ruleset",    PARAM_TYPE_STRING,
174           "ruleset=<name>", "Ruleset to go to."},
175         { PARAM_CAUSE,
176           "cause",      PARAM_TYPE_INTEGER,
177           "cause=<cause value>", "Cause value when disconnecting. (21=reject 1=unassigned 63=service not available)"},
178         { PARAM_LOCATION,
179           "location",   PARAM_TYPE_INTEGER,
180           "location=<location value>", "Location of cause value when disconnecting. (0=user 1=private network sering local user)"},
181         { PARAM_DISPLAY,
182           "display",    PARAM_TYPE_STRING,
183           "display=<text>", "Message to display on the caller's telephone. (internal only)"},
184         { PARAM_PORTS,
185           "ports",      PARAM_TYPE_INTEGER,
186           "ports=<port>[,<port>[,...]]", "ISDN port[s] to use."},
187         { PARAM_TPRESET,
188           "tpreset",    PARAM_TYPE_INTEGER,
189           "tpreset=<seconds>", "Preset of countdown timer."},
190         { PARAM_FILE,
191           "file",       PARAM_TYPE_STRING,
192           "file=<full path>", "Full path to file name."},
193         { PARAM_CONTENT,
194           "content",    PARAM_TYPE_STRING,
195           "content=<string>", "Content to write into file."},
196         { PARAM_APPEND,
197           "append",     PARAM_TYPE_NULL,
198           "append", "Will append to given file, rather than overwriting it."},
199         { PARAM_EXECUTE,
200           "execute",    PARAM_TYPE_STRING,
201           "execute=<full path>", "Full path to script/command name. (Dialed digits are the argument 1.)"},
202         { PARAM_PARAM,
203           "param",      PARAM_TYPE_STRING,
204           "param=<string>", "Optionally this parameter can be inserted as argument 1, others are shifted."},
205         { PARAM_TYPE,
206           "type",       PARAM_TYPE_TYPE,
207           "type=unknown|subscriber|national|international", "Type of number to dial, default is 'unknown'."},
208         { PARAM_COMPLETE,
209           "complete",   PARAM_TYPE_NULL,
210           "complete", "Indicates complete number as given by prefix. Proceeding of long distance calls may be faster."},
211         { PARAM_CALLERID,
212           "callerid",   PARAM_TYPE_STRING,
213           "callerid=<digits>", "Change caller ID to given string."},
214         { PARAM_CALLERIDTYPE,
215           "calleridtype",PARAM_TYPE_CALLERIDTYPE,
216           "calleridtype=[unknown|subscriber|national|international]", "Type of caller ID. For normal MSN use 'unknown'"},
217         { PARAM_CALLTO,
218           "callto",     PARAM_TYPE_STRING,
219           "callto=<digits>", "Where to call back. By default the caller ID is used."},
220         { PARAM_ROOM,
221           "room",       PARAM_TYPE_INTEGER,
222           "room=<digits>", "Conference room number, must be greater 0, as in real life."},
223         { PARAM_TIMEOUT,
224           "timeout",    PARAM_TYPE_INTEGER,
225           "timeout=<seconds>", "Timeout before continue with next action."},
226         { PARAM_NOPASSWORD,
227           "nopassword", PARAM_TYPE_NULL,
228           "nopassword", "Don't ask for password. Be sure to authenticate right via real caller ID."},
229         { PARAM_STRIP,
230           "strip",      PARAM_TYPE_NULL,
231           "strip", "Remove digits that were required to match this rule."},
232         { PARAM_APPLICATION,
233           "application",PARAM_TYPE_STRING,
234           "application", "Name of remote application to make call to."},
235         { 0, NULL, 0, NULL, NULL}
236 };
237
238 struct action_defs action_defs[] = {
239         { ACTION_EXTERNAL,
240           "extern",     &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
241           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           "Call is routed to extern number as dialed."},
243         { ACTION_INTERNAL,
244           "intern",     &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_internal, &EndpointAppPBX::action_hangup_call,
245           PARAM_CONNECT | PARAM_EXTENSION | PARAM_TYPE | PARAM_CAPA | PARAM_BMODE | PARAM_INFO1 | PARAM_HLC | PARAM_EXTHLC | PARAM_PRESENT | PARAM_TIMEOUT,
246           "Call is routed to intern extension as given by the dialed number or specified by option."},
247         { ACTION_OUTDIAL,
248           "outdial",    &EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_external, &EndpointAppPBX::action_hangup_call,
249           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,
250           "Same as 'extern'"},
251         { ACTION_REMOTE,
252           "remote",     &EndpointAppPBX::action_init_remote, &EndpointAppPBX::action_dialing_remote, &EndpointAppPBX::action_hangup_call,
253           PARAM_CONNECT | PARAM_APPLICATION | PARAM_TIMEOUT,
254           "Call is routed to Remote application, like Asterisk."},
255         { ACTION_VBOX_RECORD,
256           "vbox-record",&EndpointAppPBX::action_init_call, &EndpointAppPBX::action_dialing_vbox_record, &EndpointAppPBX::action_hangup_call,
257           PARAM_CONNECT | PARAM_EXTENSION | PARAM_ANNOUNCEMENT | PARAM_TIMEOUT,
258           "Caller is routed to the voice box of given extension."},
259         { ACTION_PARTYLINE,
260           "partyline",&EndpointAppPBX::action_init_partyline, NULL, &EndpointAppPBX::action_hangup_call,
261           PARAM_ROOM,
262           "Caller is participating the conference with the given room number."},
263         { ACTION_LOGIN,
264           "login",      NULL, &EndpointAppPBX::action_dialing_login, NULL,
265           PARAM_CONNECT | PARAM_EXTENSION | PARAM_NOPASSWORD,
266           "Log into the given extension. Password required."},
267         { ACTION_CALLERID,
268           "callerid",   &EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_callerid, NULL,
269           PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
270           "Caller changes the caller ID for all calls."},
271         { ACTION_CALLERIDNEXT,
272           "calleridnext",&EndpointAppPBX::action_init_change_callerid, &EndpointAppPBX::action_dialing_calleridnext, NULL,
273           PARAM_CONNECT | PARAM_CALLERID | PARAM_CALLERIDTYPE | PARAM_PRESENT,
274           "Caller changes the caller ID for the next call."},
275         { ACTION_FORWARD,
276           "forward",    &EndpointAppPBX::action_init_change_forward, &EndpointAppPBX::action_dialing_forward, NULL,
277           PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_DELAY,
278           "Caller changes the diversion of given type to the given destination or voice box."},
279         { ACTION_REDIAL,
280           "redial",     &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_redial, NULL,
281           PARAM_CONNECT | PARAM_SELECT,
282           "Caller redials. (last outgoing call(s))"},
283         { ACTION_REPLY,
284           "reply",      &EndpointAppPBX::action_init_redial_reply, &EndpointAppPBX::action_dialing_reply, NULL,
285           PARAM_CONNECT | PARAM_SELECT,
286           "Caller replies. (last incomming call(s))"},
287         { ACTION_POWERDIAL,
288           "powerdial",  NULL, &EndpointAppPBX::action_dialing_powerdial, NULL,
289           PARAM_CONNECT | PARAM_DELAY | PARAM_LIMIT | PARAM_TIMEOUT,
290           "Caller redials using powerdialing."},
291         { ACTION_CALLBACK,
292           "callback",   NULL, &EndpointAppPBX::action_dialing_callback, &EndpointAppPBX::action_hangup_callback,
293           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_EXTENSION | PARAM_DELAY | PARAM_CALLTO | PARAM_PREFIX,
294           "Caller will use the callback service. After disconnecting, the callback is triggered."},
295         { ACTION_ABBREV,
296           "abbrev",     NULL, &EndpointAppPBX::action_dialing_abbrev, NULL,
297           PARAM_CONNECT,
298           "Caller dials abbreviation."},
299         { ACTION_TEST,
300           "test",       NULL, &EndpointAppPBX::action_dialing_test, NULL,
301           PARAM_CONNECT | PARAM_PREFIX | PARAM_TIMEOUT,
302           "Caller dials test mode."},
303         { ACTION_PLAY,
304           "play",       &EndpointAppPBX::action_init_play, NULL, NULL,
305           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_SAMPLE | PARAM_TIMEOUT,
306           "Plays the given sample."},
307         { ACTION_VBOX_PLAY,
308           "vbox-play",  &EndpointAppPBX::action_init_vbox_play, &EndpointAppPBX::action_dialing_vbox_play, NULL,
309           PARAM_EXTENSION,
310           "Caller listens to her voice box or to given extension."},
311         { ACTION_CALCULATOR,
312           "calculator", NULL, &EndpointAppPBX::action_dialing_calculator, NULL,
313           PARAM_CONNECT,
314           "Caller calls the calculator."},
315         { ACTION_TIMER,
316           "timer",      NULL, &EndpointAppPBX::action_dialing_timer, NULL,
317           PARAM_CONNECT | PARAM_TPRESET | PARAM_TIMEOUT,
318           NULL},
319 //        "Caller calls the timer."},
320         { ACTION_GOTO,
321           "goto",       NULL, &EndpointAppPBX::action_dialing_goto, NULL,
322           PARAM_CONNECT | PARAM_RULESET | PARAM_STRIP | PARAM_SAMPLE,
323           "Jump to given ruleset and optionally play sample. Dialed digits are not flushed."},
324         { ACTION_MENU,
325           "menu",       NULL, &EndpointAppPBX::action_dialing_menu, NULL,
326           PARAM_CONNECT | PARAM_RULESET | PARAM_SAMPLE,
327           "Same as 'goto', but flushes all digits dialed so far."},
328         { ACTION_DISCONNECT,
329           "disconnect", NULL, &EndpointAppPBX::action_dialing_disconnect, NULL,
330           PARAM_CONNECT | PARAM_CAUSE | PARAM_LOCATION | PARAM_SAMPLE | PARAM_DISPLAY,
331           "Caller gets disconnected optionally with given cause and given sample and given display text."},
332         { ACTION_HELP,
333           "help",       NULL, &EndpointAppPBX::action_dialing_help, NULL,
334           PARAM_CONNECT | PARAM_TIMEOUT,
335           NULL},
336 //        "Caller will be able to select from current rules that would match. (using * and #)"},
337         { ACTION_DEFLECT,
338           "deflect",    NULL, &EndpointAppPBX::action_dialing_deflect, NULL,
339           PARAM_DEST,
340           NULL},
341 //        "External call is deflected to the given destination within the telephone network."},
342         { ACTION_SETFORWARD,
343           "setforward", NULL, &EndpointAppPBX::action_dialing_setforward, NULL,
344           PARAM_CONNECT | PARAM_DIVERSION | PARAM_DEST | PARAM_PORT,
345           NULL},
346 //        "The call forward is set within the telephone network of the external line."},
347         { ACTION_EXECUTE,
348           "execute",    NULL, NULL, &EndpointAppPBX::action_hangup_execute,
349           PARAM_CONNECT | PARAM_EXECUTE | PARAM_PARAM,
350           "Executes the given script file. The file must terminate quickly, because it will halt the PBX."},
351         { ACTION_FILE,
352           "file",       NULL, NULL, &EndpointAppPBX::action_hangup_file,
353           PARAM_CONNECT | PARAM_FILE | PARAM_CONTENT | PARAM_APPEND,
354           "Writes givent content to given file. If content is not given, the dialed digits are written."},
355         { ACTION_PICK,
356           "pick",       &EndpointAppPBX::action_init_pick, NULL, NULL,
357           PARAM_EXTENSIONS,
358           "Pick up a call that is ringing on any phone. Extensions may be given to limit the picking ability."},
359         { ACTION_PASSWORD,
360           "password",   NULL, &EndpointAppPBX::action_dialing_password, NULL,
361           0,
362           NULL},
363         { ACTION_PASSWORD_WRITE,
364           "password_wr",NULL, &EndpointAppPBX::action_dialing_password_wr, NULL,
365           0,
366           NULL},
367         { ACTION_NOTHING,
368           "nothing",    NULL, NULL, NULL,
369           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT | PARAM_TIMEOUT,
370           "does nothing. Usefull to wait for calls to be released completely, by giving timeout value."},
371         { ACTION_EFI,
372           "efi",        &EndpointAppPBX::action_init_efi, NULL, NULL,
373           PARAM_PROCEEDING | PARAM_ALERTING | PARAM_CONNECT,
374           "Elektronische Fernsprecher Identifikation - announces caller ID."},
375         { -1,
376           NULL, NULL, NULL, NULL, 0, NULL}
377 };
378
379
380 /* display documentation of rules */
381
382 void doc_rules(const char *name)
383 {
384         int i, j;
385
386         if (name)
387         {
388                 i = 0;
389                 while(action_defs[i].name)
390                 {
391                         if (!strcasecmp(action_defs[i].name, name))
392                                 break;
393                         i++;
394                 }
395                 if (!action_defs[i].name)
396                 {
397                         fprintf(stderr, "Given action '%s' unknown.\n", name);
398                         return;
399                 }
400                 name = action_defs[i].name;
401         }
402
403         printf("Syntax overview:\n");
404         printf("----------------\n\n");
405         printf("[ruleset]\n");
406         printf("<condition> ...   : <action> [parameter ...]   [timeout=X : <action> ...]\n");
407         printf("...\n");
408         printf("Please refer to the documentation for description on rule format.\n\n");
409
410         if (!name)
411         {
412                 printf("Available conditions to match:\n");
413                 printf("------------------------------\n\n");
414                 i = 0;
415                 while(cond_defs[i].name)
416                 {
417                         printf("Usage: %s\n", cond_defs[i].doc);
418                         printf("%s\n\n", cond_defs[i].help);
419                         i++;
420                 }
421
422                 printf("Available actions with their parameters:\n");
423                 printf("----------------------------------------\n\n");
424         } else
425         {
426                 printf("Detailes parameter description of action:\n");
427                 printf("-----------------------------------------\n\n");
428         }
429         i = 0;
430         while(action_defs[i].name)
431         {
432                 if (name && !!strcmp(action_defs[i].name,name)) /* not selected */
433                 {
434                         i++;
435                         continue;
436                 }
437                 if (!action_defs[i].help) /* not internal actions */
438                 {
439                         i++;
440                         continue;
441                 }
442                 printf("Usage: %s", action_defs[i].name);
443                 j = 0;
444                 while(j < 64)
445                 {
446                         if ((1LL<<j) & action_defs[i].params)
447                                 printf(" [%s]", param_defs[j].doc);
448                         j++;
449                 }
450                 printf("\n%s\n\n", action_defs[i].help);
451                 if (name) /* only show parameter help for specific action */
452                 {
453                         j = 0;
454                         while(j < 64)
455                         {
456                                 if ((1LL<<j) & action_defs[i].params)
457                                         printf("%s:\n\t%s\n", param_defs[j].doc, param_defs[j].help);
458                                 j++;
459                         }
460                         printf("\n");
461                 }
462                 i++;
463         }
464 }
465
466 void ruleset_free(struct route_ruleset *ruleset_start)
467 {
468         struct route_ruleset *ruleset;
469         struct route_rule *rule;
470         struct route_cond *cond;
471         struct route_action *action;
472         struct route_param *param;
473
474         while(ruleset_start)
475         {
476                 ruleset = ruleset_start;
477                 ruleset_start = ruleset->next;
478                 while(ruleset->rule_first)
479                 {
480                         rule = ruleset->rule_first;
481                         ruleset->rule_first = rule->next;
482                         while(rule->cond_first)
483                         {
484                                 cond = rule->cond_first;
485                                 if (cond->string_value)
486                                 {
487                                         FREE(cond->string_value, 0);
488                                         rmemuse--;
489                                 }
490                                 if (cond->string_value_to)
491                                 {
492                                         FREE(cond->string_value_to, 0);
493                                         rmemuse--;
494                                 }
495                                 rule->cond_first = cond->next;
496                                 FREE(cond, sizeof(struct route_cond));
497                                 rmemuse--;
498                         }
499                         while(rule->action_first)
500                         {
501                                 action = rule->action_first;
502                                 rule->action_first = action->next;
503                                 while(action->param_first)
504                                 {
505                                         param = action->param_first;
506                                         action->param_first = param->next;
507                                         if (param->string_value)
508                                         {
509                                                 FREE(param->string_value, 0);
510                                                 rmemuse--;
511                                         }
512                                         FREE(param, sizeof(struct route_param));
513                                         rmemuse--;
514                                 }
515                                 FREE(action, sizeof(struct route_action));
516                                 rmemuse--;
517                         }
518                         FREE(rule, sizeof(struct route_rule));
519                         rmemuse--;
520                 }
521                 FREE(ruleset, sizeof(struct route_ruleset));
522                 rmemuse--;
523         }
524 }
525
526 void ruleset_debug(struct route_ruleset *ruleset_start)
527 {
528         struct route_ruleset    *ruleset;
529         struct route_rule       *rule;
530         struct route_cond       *cond;
531         struct route_action     *action;
532         struct route_param      *param;
533         int                     first;
534
535         ruleset = ruleset_start;
536         while(ruleset)
537         {
538                 printf("Ruleset: '%s'\n", ruleset->name);
539                 rule = ruleset->rule_first;
540                 while(rule)
541                 {
542                         /* CONDITION */
543                         first = 1;
544                         cond = rule->cond_first;
545                         while(cond)
546                         {
547                                 if (first)
548                                         printf("    Condition:");
549                                 else
550                                         printf("    and       ");
551                                 first = 0;
552                                 printf(" %s", cond_defs[cond->index].name);
553                                 if (cond->value_type != VALUE_TYPE_NULL)
554                                         printf(" = ");
555                                 next_cond_value:
556                                 switch(cond->value_type)
557                                 {
558                                         case VALUE_TYPE_NULL:
559                                         break;
560
561                                         case VALUE_TYPE_INTEGER:
562                                         printf("%d", cond->integer_value);
563                                         break;
564
565                                         case VALUE_TYPE_INTEGER_RANGE:
566                                         printf("%d-%d", cond->integer_value, cond->integer_value_to);
567                                         break;
568
569                                         case VALUE_TYPE_STRING:
570                                         printf("'%s'", cond->string_value);
571                                         break;
572
573                                         case VALUE_TYPE_STRING_RANGE:
574                                         printf("'%s'-'%s'", cond->string_value, cond->string_value_to);
575                                         break;
576
577                                         default:
578                                         printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", cond->value_type, __FUNCTION__, __LINE__);
579                                 }
580                                 if (cond->value_extension && cond->next)
581                                 {
582                                         cond = cond->next;
583                                         printf(" or ");
584                                         goto next_cond_value;
585                                 }
586
587                                 cond = cond->next;
588                                 printf("\n");
589                         }
590
591                         /* ACTION */
592                         action = rule->action_first;
593                         while(action)
594                         {
595                                 printf("    Action: %s\n", action_defs[action->index].name);
596                                 /* PARAM */
597                                 first = 1;
598                                 param = action->param_first;
599                                 while(param)
600                                 {
601                                         if (first)
602                                                 printf("    Param:");
603                                         else
604                                                 printf("          ");
605                                         first = 0;
606                                         printf(" %s", param_defs[param->index].name);
607                                         if (param->value_type != VALUE_TYPE_NULL)
608                                                 printf(" = ");
609                                         switch(param->value_type)
610                                         {
611                                                 case VALUE_TYPE_NULL:
612                                                 break;
613
614                                                 case VALUE_TYPE_INTEGER:
615                                                 if (param_defs[param->index].type == PARAM_TYPE_CALLERIDTYPE)
616                                                 {
617                                                         switch(param->integer_value)
618                                                         {
619                                                                 case INFO_NTYPE_UNKNOWN:
620                                                                 printf("unknown");
621                                                                 break;
622                                                                 case INFO_NTYPE_SUBSCRIBER:
623                                                                 printf("subscriber");
624                                                                 break;
625                                                                 case INFO_NTYPE_NATIONAL:
626                                                                 printf("national");
627                                                                 break;
628                                                                 case INFO_NTYPE_INTERNATIONAL:
629                                                                 printf("international");
630                                                                 break;
631                                                                 default:
632                                                                 printf("unknown(%d)", param->integer_value);
633                                                         }
634                                                         break;
635                                                 }
636                                                 if (param_defs[param->index].type == PARAM_TYPE_CAPABILITY)
637                                                 {
638                                                         switch(param->integer_value)
639                                                         {
640                                                                 case INFO_BC_SPEECH:
641                                                                 printf("speech");
642                                                                 break;
643                                                                 case INFO_BC_AUDIO:
644                                                                 printf("audio");
645                                                                 break;
646                                                                 case INFO_BC_VIDEO:
647                                                                 printf("video");
648                                                                 break;
649                                                                 case INFO_BC_DATARESTRICTED:
650                                                                 printf("digital-restricted");
651                                                                 break;
652                                                                 case INFO_BC_DATAUNRESTRICTED:
653                                                                 printf("digital-unrestricted");
654                                                                 break;
655                                                                 case INFO_BC_DATAUNRESTRICTED_TONES:
656                                                                 printf("digital-unrestricted-tones");
657                                                                 break;
658                                                                 default:
659                                                                 printf("unknown(%d)", param->integer_value);
660                                                         }
661                                                         break;
662                                                 }
663                                                 if (param_defs[param->index].type == PARAM_TYPE_DIVERSION)
664                                                 {
665                                                         switch(param->integer_value)
666                                                         {
667                                                                 case INFO_DIVERSION_CFU:
668                                                                 printf("cfu");
669                                                                 break;
670                                                                 case INFO_DIVERSION_CFNR:
671                                                                 printf("cfnr");
672                                                                 break;
673                                                                 case INFO_DIVERSION_CFB:
674                                                                 printf("cfb");
675                                                                 break;
676                                                                 case INFO_DIVERSION_CFP:
677                                                                 printf("cfp");
678                                                                 break;
679                                                                 default:
680                                                                 printf("unknown(%d)", param->integer_value);
681                                                         }
682                                                         break;
683                                                 }
684                                                 if (param_defs[param->index].type == PARAM_TYPE_TYPE)
685                                                 {
686                                                         switch(param->integer_value)
687                                                         {
688                                                                 case INFO_NTYPE_UNKNOWN:
689                                                                 printf("unknown");
690                                                                 break;
691                                                                 case INFO_NTYPE_SUBSCRIBER:
692                                                                 printf("subscriber");
693                                                                 break;
694                                                                 case INFO_NTYPE_NATIONAL:
695                                                                 printf("national");
696                                                                 break;
697                                                                 case INFO_NTYPE_INTERNATIONAL:
698                                                                 printf("international");
699                                                                 break;
700                                                                 default:
701                                                                 printf("unknown(%d)", param->integer_value);
702                                                         }
703                                                         break;
704                                                 }
705                                                 if (param_defs[param->index].type == PARAM_TYPE_YESNO)
706                                                 {
707                                                         switch(param->integer_value)
708                                                         {
709                                                                 case 1:
710                                                                 printf("yes");
711                                                                 break;
712                                                                 case 0:
713                                                                 printf("no");
714                                                                 break;
715                                                                 default:
716                                                                 printf("unknown(%d)", param->integer_value);
717                                                         }
718                                                         break;
719                                                 }
720                                                 if (param_defs[param->index].type == PARAM_TYPE_NULL)
721                                                 {
722                                                         break;
723                                                 }
724                                                 printf("%d", param->integer_value);
725                                                 break;
726
727                                                 case VALUE_TYPE_STRING:
728                                                 printf("'%s'", param->string_value);
729                                                 break;
730
731                                                 default:
732                                                 printf("Software error: VALUE_TYPE_* %d not known in function '%s' line=%d", param->value_type, __FUNCTION__, __LINE__);
733                                         }
734                                         param = param->next;
735                                         printf("\n");
736                                 }
737                                 /* TIMEOUT */
738                                 if (action->timeout)
739                                         printf("    Timeout: %d\n", action->timeout);
740                                 action = action->next;
741                         }
742                         printf("\n");
743                         rule = rule->next;
744                 }
745                 printf("\n");
746                 ruleset = ruleset->next;
747         }
748 }
749
750
751 /*
752  * parse ruleset
753  */
754 static char *read_string(char *p, char *key, int key_size, char *special)
755 {
756         key[0] = 0;
757
758         if (*p == '\"')
759         {
760                 p++;
761                 /* quote */
762                 while(*p)
763                 {
764                         if (*p == '\"')
765                         {
766                                 p++;
767                                 *key = '\0';
768                                 return(p);
769                         }
770                         if (*p == '\\')
771                         {
772                                 p++;
773                                 if (*p == '\0')
774                                 {
775                                         break;
776                                 }
777                         }
778                         if (--key_size == 0)
779                         {
780                                 UPRINT(key, "\001String too long.");
781                                 return(p);
782                         }
783                         *key++ = *p++;
784                 }
785                 UPRINT(key, "\001Unexpected end of line inside quotes.");
786                 return(p);
787         }
788
789         /* no quote */
790         while(*p)
791         {
792                 if (strchr(special, *p))
793                 {
794                         *key = '\0';
795                         return(p);
796                 }
797                 if (*p == '\\')
798                 {
799                         p++;
800                         if (*p == '\0')
801                         {
802                                 UPRINT(key, "\001Unexpected end of line.");
803                                 return(p);
804                         }
805                 }
806                 if (--key_size == 0)
807                 {
808                         UPRINT(key, "\001String too long.");
809                         return(p);
810                 }
811                 *key++ = *p++;
812         }
813         *key = '\0';
814         return(p);
815 }
816 char ruleset_error[256];
817 struct route_ruleset *ruleset_parse(void)
818 {
819 //      char                    from[128];
820 //      char                    to[128];
821         int                     i;
822         unsigned long long      j;
823 //      int                     a,
824 //                              b;
825         #define                 MAXNESTING 8
826         FILE                    *fp[MAXNESTING];
827         char                    filename[MAXNESTING][256];
828         int                     line[MAXNESTING];
829         int                     nesting = -1;
830         char                    buffer[1024],
831                                 key[1024],
832                                 key_to[1024],
833                                 pointer[1024+1],
834                                 *p;
835         int                     expecting = 1; /* 1 = expecting ruleset */
836         int                     index,
837                                 value_type,
838                                 integer,
839                                 integer_to; /* condition index, .. */
840         struct route_ruleset    *ruleset_start = NULL, *ruleset;
841         struct route_ruleset    **ruleset_pointer = &ruleset_start;
842         struct route_rule       *rule;
843         struct route_rule       **rule_pointer = NULL;
844         struct route_cond       *cond;
845         struct route_cond       **cond_pointer = NULL;
846         struct route_action     *action;
847         struct route_action     **action_pointer = NULL;
848         struct route_param      *param;
849         struct route_param      **param_pointer = NULL;
850         char                    failure[256];
851
852         /* check the integrity of IDs for ACTION_* and PARAM_* */
853         i = 0;
854         while(action_defs[i].name)
855         {
856                 if (action_defs[i].id != i)
857                 {
858                         PERROR("Software Error action '%s' must have id of %d, but has %d.\n",
859                                 action_defs[i].name, i, action_defs[i].id);
860                         goto openerror;
861                 }
862                 i++;
863         }
864         i = 0; j = 1;
865         while(param_defs[i].name)
866         {
867                 if (param_defs[i].id != j)
868                 {
869                         PERROR("Software Error param '%s' must have id of 0x%llx, but has 0x%llx.\n",
870                                 param_defs[i].name, j, param_defs[i].id);
871                         goto openerror;
872                 }
873                 i++;
874                 j<<=1;
875         }
876
877         SPRINT(filename[0], "%s/routing.conf", INSTALL_DATA);
878
879         if (!(fp[0]=fopen(filename[0],"r")))
880         {
881                 PERROR("Cannot open %s\n",filename[0]);
882                 goto openerror;
883         }
884         nesting++;
885         fduse++;
886
887         go_leaf:
888         line[nesting]=0;
889         go_root:
890         while((fgets(buffer,sizeof(buffer),fp[nesting])))
891         {
892                 line[nesting]++;
893                 buffer[sizeof(buffer)-1]=0;
894                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
895                 p = buffer;
896
897                 /* remove tabs */
898                 while(*p) {
899                         if (*p < 32)
900                                 *p = 32;
901                         p++;
902                 } 
903                 p = buffer;
904
905                 /* skip spaces, if any */
906                 while(*p == 32)
907                 {
908                         if (*p == 0)
909                                 break;
910                         p++;
911                 }
912
913                 /* skip comments */
914                 if (*p == '#')
915                 {
916                         p++;
917                         /* don't skip "define" */
918                         if (!!strncmp(p, "define", 6))
919                                 continue;
920                         p+=6;
921                         if (*p != 32)
922                                 continue;
923                         /* skip spaces */
924                         while(*p == 32)
925                         {
926                                 if (*p == 0)
927                                         break;
928                                 p++;
929                         }
930                         p++;
931                         p = read_string(p, key, sizeof(key), " ");
932                         if (key[0] == 1) /* error */
933                         {
934                                 SPRINT(failure, "Parsing Filename failed: %s", key+1);
935                                 goto parse_error;
936                         }
937                         if (nesting == MAXNESTING-1)
938                         {
939                                 SPRINT(failure, "'include' is nesting too deep.\n");
940                                 goto parse_error;
941                         }
942                         if (key[0] == '/')
943                                 SCPY(filename[nesting+1], key);
944                         else
945                                 SPRINT(filename[nesting+1], "%s/%s", INSTALL_DATA, key);
946                         if (!(fp[nesting+1]=fopen(filename[nesting+1],"r")))
947                         {
948                                 PERROR("Cannot open %s\n", filename[nesting+1]);
949                                 goto parse_error;
950                         }
951                         fduse++;
952                         nesting++;
953                         goto go_leaf;
954                 }
955                 if (*p == '/') if (p[1] == '/')
956                         continue;
957
958                 /* skip empty lines */
959                 if (*p == 0)
960                         continue;
961
962                 /* expecting ruleset */
963                 if (expecting)
964                 {
965                         new_ruleset:
966                         /* expecting [ */
967                         if (*p != '[')
968                         {
969                                 SPRINT(failure, "Expecting ruleset name starting with '['.");
970                                 goto parse_error;
971                         }
972                         p++;
973
974                         /* reading ruleset name text */
975                         i = 0;
976                         while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
977                         {
978                                 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
979                                 key[i++] = *p++;
980                                 if (i == sizeof(key)) i--; /* limit */
981                         }
982                         key[i] = 0;
983                         if (key[0] == '\0') {
984                                 SPRINT(failure, "Missing ruleset name after '['.");
985                                 goto parse_error;
986                         }
987
988                         /* expecting ] and nothing more */
989                         if (*p != ']') {
990                                 SPRINT(failure, "Expecting ']' after ruleset name.");
991                                 goto parse_error;
992                         }
993                         p++;
994                         if (*p != 0) {
995                                 SPRINT(failure, "Unexpected character after ruleset name.");
996                                 goto parse_error;
997                         }
998
999                         /* check for duplicate rulesets */
1000                         ruleset = ruleset_start;
1001                         while(ruleset)
1002                         {
1003                                 if (!strcmp(ruleset->name, key))
1004                                 {
1005                                         SPRINT(failure, "Duplicate ruleset '%s', already defined in file '%s' line %d.", key, ruleset->file, ruleset->line);
1006                                         goto parse_error;
1007                                 }
1008                                 ruleset = ruleset->next;
1009                         }
1010
1011                         /* create ruleset */
1012                         ruleset = (struct route_ruleset *)MALLOC(sizeof(struct route_ruleset));
1013                         rmemuse++;
1014                         *ruleset_pointer = ruleset;
1015                         ruleset_pointer = &(ruleset->next);
1016                         SCPY(ruleset->name, key);
1017                         SCPY(ruleset->file, filename[nesting]);
1018                         ruleset->line = line[nesting];
1019                         rule_pointer = &(ruleset->rule_first);
1020                         expecting = 0;
1021                         continue;
1022                 }
1023
1024                 /* for new ruleset [ */
1025                 if (*p == '[')
1026                 {
1027                         goto new_ruleset;
1028                 }
1029
1030                 /* Alloc memory for rule */
1031                 rule = (struct route_rule *)MALLOC(sizeof(struct route_rule));
1032                 rmemuse++;
1033                 *rule_pointer = rule;
1034                 rule_pointer = &(rule->next);
1035                 cond_pointer = &(rule->cond_first);
1036                 action_pointer = &(rule->action_first);
1037                 SCPY(rule->file, filename[nesting]);
1038                 rule->line = line[nesting];
1039
1040                 /* loop CONDITIONS */
1041                 while(*p!=':' && *p!='\0')
1042                 {
1043                         /* read item text */
1044                         i = 0;
1045                         while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1046                         {
1047                                 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1048                                 key[i++] = *p++;
1049                                 if (i == sizeof(key)) i--; /* limit */
1050                         }
1051                         key[i] = 0;
1052                         if (key[0] == '\0')
1053                         {
1054                                 SPRINT(failure, "Expecting condition item name or ':' for end of condition list.");
1055                                 goto parse_error;
1056                         }
1057                         if (*p!=' ' && *p!='=')
1058                         {
1059                                 SPRINT(failure, "Illegal character '%c' after condition name '%s'. Expecting '=' for equation or ' ' to seperate condition items.", *p, key);
1060                                 goto parse_error;
1061                         }
1062
1063                         /* check if condition exists */
1064                         index = 0;
1065                         while(cond_defs[index].name)
1066                         {
1067                                 if (!strcmp(cond_defs[index].name, key))
1068                                         break;
1069                                 index++;
1070                         }
1071                         if (cond_defs[index].name == NULL)
1072                         {
1073                                 SPRINT(failure, "Unknown condition item name '%s'.", key);
1074                                 goto parse_error;
1075                         }
1076
1077                         /* items without values must not have any parameter */
1078                         if (cond_defs[index].type == COND_TYPE_NULL)
1079                         {
1080                                 if (*p == '=')
1081                                 {
1082                                         SPRINT(failure, "Condition item '%s' must not have any value. Don't use '=' for this type of condition.", key);
1083                                         goto parse_error;
1084                                 }
1085                                 if (*p != ' ')
1086                                 {
1087                                         SPRINT(failure, "Condition item '%s' must not have any value. Expecting ' ' or tab after item name.", key);
1088                                         goto parse_error;
1089                                 }
1090 //                              p++;
1091                         } else
1092                         {
1093                                 if (*p == ' ')
1094                                 {
1095                                         SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected, and not a space.", key);
1096                                         goto parse_error;
1097                                 }
1098                                 if (*p != '=')
1099                                 {
1100                                         SPRINT(failure, "Condition item '%s' must have at least one value, '=' expected.", key);
1101                                         goto parse_error;
1102                                 }
1103                                 p++;
1104                         }
1105
1106                         /* check for duplicate condition */
1107                         cond = rule->cond_first;
1108                         while(cond)
1109                         {
1110                                 if (cond->index == index)
1111                                 {
1112                                         SPRINT(failure, "Duplicate condition '%s', use ',' to give multiple values.", key);
1113                                         goto parse_error;
1114                                 }
1115                                 cond = cond->next;
1116                         }
1117
1118                         nextcondvalue:
1119                         /* Alloc memory for item */
1120                         cond = (struct route_cond *)MALLOC(sizeof(struct route_cond));
1121                         rmemuse++;
1122                         *cond_pointer = cond;
1123                         cond_pointer = &(cond->next);
1124                         cond->index = index;
1125                         cond->match = cond_defs[index].match;
1126                         switch(cond_defs[index].type)
1127                         {
1128                                 case COND_TYPE_NULL:
1129                                 if (*p=='=')
1130                                 {
1131                                         SPRINT(failure, "Expecting no value.");
1132                                         goto parse_error;
1133                                 }
1134                                 value_type = VALUE_TYPE_NULL;
1135                                 break;
1136
1137                                 /* parse all integer values/ranges */
1138                                 case COND_TYPE_INTEGER:
1139                                 case COND_TYPE_TIME:
1140                                 case COND_TYPE_MDAY:
1141                                 case COND_TYPE_MONTH:
1142                                 case COND_TYPE_WDAY:
1143                                 case COND_TYPE_YEAR:
1144                                 integer = integer_to = 0;
1145                                 if (*p==',' || *p==' ' || *p=='\0')
1146                                 {
1147                                         SPRINT(failure, "Missing integer value.");
1148                                         goto parse_error;
1149                                 }
1150                                 while(*p>='0' && *p<='9')
1151                                 {
1152                                         integer = integer*10 + *p-'0';
1153                                         p++;
1154                                 }
1155                                 value_type = VALUE_TYPE_INTEGER;
1156                                 if (*p == '-')
1157                                 {
1158                                         p++;
1159                                         if (*p==',' || *p==' ' || *p=='\0')
1160                                         {
1161                                                 SPRINT(failure, "Missing integer value.");
1162                                                 goto parse_error;
1163                                         }
1164                                         while(*p>='0' && *p<='9')
1165                                         {
1166                                                 integer_to = integer_to*10 + *p-'0';
1167                                                 p++;
1168                                         }
1169                                         value_type = VALUE_TYPE_INTEGER_RANGE;
1170                                 }
1171                                 if (cond_defs[index].type == COND_TYPE_TIME)
1172                                 {
1173                                         // Simon: i store the time as decimal, later i compare it correctly:
1174                                         // hours * 100 + minutes
1175                                         if (integer == 2400)
1176                                                 integer = 0;
1177                                         if (integer >= 2400)
1178                                         {
1179                                                 timeoutofrange1:
1180                                                 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer);
1181                                                 goto parse_error;
1182                                         }
1183                                         if (integer%100 >= 60)
1184                                                 goto timeoutofrange1;
1185                                         if (value_type == VALUE_TYPE_INTEGER)
1186                                                 goto integer_done;
1187                                         if (integer_to == 2400)
1188                                                 integer_to = 0;
1189                                         if (integer_to >= 2400)
1190                                         {
1191                                                 timeoutofrange2:
1192                                                 SPRINT(failure, "Given time '%d' not in range 0000..2359 (or 2400 for 0000)", integer_to);
1193                                                 goto parse_error;
1194                                         }
1195                                         if (integer_to%100 >= 60)
1196                                                 goto timeoutofrange2;
1197                                 }
1198                                 if (cond_defs[index].type == COND_TYPE_MDAY)
1199                                 {
1200                                         if (integer<1 || integer>31)
1201                                         {
1202                                                 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer);
1203                                                 goto parse_error;
1204                                         } 
1205                                         if (value_type == VALUE_TYPE_INTEGER)
1206                                                 goto integer_done;
1207                                         if (integer_to<1 || integer_to>31)
1208                                         {
1209                                                 SPRINT(failure, "Given day-of-month '%d' not in range 1..31", integer_to);
1210                                                 goto parse_error;
1211                                         } 
1212                                 }
1213                                 if (cond_defs[index].type == COND_TYPE_WDAY)
1214                                 {
1215                                         if (integer<1 || integer>7)
1216                                         {
1217                                                 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer);
1218                                                 goto parse_error;
1219                                         } 
1220                                         if (value_type == VALUE_TYPE_INTEGER)
1221                                                 goto integer_done;
1222                                         if (integer_to<1 || integer_to>7)
1223                                         {
1224                                                 SPRINT(failure, "Given day-of-week '%d' not in range 1..7", integer_to);
1225                                                 goto parse_error;
1226                                         } 
1227                                 }
1228                                 if (cond_defs[index].type == COND_TYPE_MONTH)
1229                                 {
1230                                         if (integer<1 || integer>12)
1231                                         {
1232                                                 SPRINT(failure, "Given month '%d' not in range 1..12", integer);
1233                                                 goto parse_error;
1234                                         } 
1235                                         if (value_type == VALUE_TYPE_INTEGER)
1236                                                 goto integer_done;
1237                                         if (integer_to<1 || integer_to>12)
1238                                         {
1239                                                 SPRINT(failure, "Given month '%d' not in range 1..12", integer_to);
1240                                                 goto parse_error;
1241                                         } 
1242                                 }
1243                                 if (cond_defs[index].type == COND_TYPE_YEAR)
1244                                 {
1245                                         if (integer<1970 || integer>2106)
1246                                         {
1247                                                 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer);
1248                                                 goto parse_error;
1249                                         } 
1250                                         if (value_type == VALUE_TYPE_INTEGER)
1251                                                 goto integer_done;
1252                                         if (integer_to<1970 || integer_to>2106)
1253                                         {
1254                                                 SPRINT(failure, "Given year '%d' not in range 1970..2106", integer_to);
1255                                                 goto parse_error;
1256                                         } 
1257                                 }
1258                                 integer_done:
1259                                 cond->integer_value = integer;
1260                                 cond->integer_value_to = integer_to;
1261                                 cond->value_type = value_type;
1262                                 break;
1263
1264                                 /* parse all string values/ranges */
1265                                 case COND_TYPE_STRING:
1266                                 key[0] = key_to[0] = '\0';
1267                                 if (*p==',' || *p==' ' || *p=='\0')
1268                                 {
1269                                         SPRINT(failure, "Missing string value, use \"\" for empty string.");
1270                                         goto parse_error;
1271                                 }
1272                                 p = read_string(p, key, sizeof(key), "-, ");
1273                                 if (key[0] == 1) /* error */
1274                                 {
1275                                         SPRINT(failure, "Parsing String failed: %s", key+1);
1276                                         goto parse_error;
1277                                 }
1278                                 value_type = VALUE_TYPE_STRING;
1279                                 if (*p == '-')
1280                                 {
1281                                         p++;
1282                                         if (*p==',' || *p==' ' || *p=='\0')
1283                                         {
1284                                                 SPRINT(failure, "Missing string value, use \"\" for empty string.");
1285                                                 goto parse_error;
1286                                         }
1287                                         p = read_string(p, key_to, sizeof(key_to), "-, ");
1288                                         if (key_to[0] == 1) /* error */
1289                                         {
1290                                                 SPRINT(failure, "Parsing string failed: %s", key_to+1);
1291                                                 goto parse_error;
1292                                         }
1293                                         value_type = VALUE_TYPE_STRING_RANGE;
1294                                         if (strlen(key) != strlen(key_to))
1295                                         {
1296                                                 SPRINT(failure, "Given range of strings \"%s\"-\"%s\" have unequal length.", key, key_to);
1297                                                 goto parse_error;
1298                                         }
1299                                         if (key[0] == '\0')
1300                                         {
1301                                                 SPRINT(failure, "Given range has no length.");
1302                                                 goto parse_error;
1303                                         }
1304                                 }
1305                                 alloc_string:
1306                                 cond->string_value = (char *)MALLOC(strlen(key)+1);
1307                                 rmemuse++;
1308                                 UCPY(cond->string_value, key);
1309                                 if (value_type == VALUE_TYPE_STRING_RANGE)
1310                                 {
1311                                         cond->string_value_to = (char *)MALLOC(strlen(key_to)+1);
1312                                         rmemuse++;
1313                                         UCPY(cond->string_value_to, key_to);
1314                                         cond->comp_string = strcmp(key, key_to);
1315                                 }
1316                                 cond->value_type = value_type;
1317                                 break;
1318
1319                                 /* parse service value */
1320                                 case COND_TYPE_CAPABILITY:
1321                                 if (!strncasecmp("speech", p, 6))
1322                                         cond->integer_value = INFO_BC_SPEECH;
1323                                 else if (!strncasecmp("audio", p, 5))
1324                                         cond->integer_value = INFO_BC_AUDIO;
1325                                 else if (!strncasecmp("video", p, 5))
1326                                         cond->integer_value = INFO_BC_VIDEO;
1327                                 else if (!strncasecmp("digital-restricted", p, 18))
1328                                         cond->integer_value = INFO_BC_DATARESTRICTED;
1329                                 else if (!strncasecmp("digital-unrestricted", p, 20))
1330                                         cond->integer_value = INFO_BC_DATAUNRESTRICTED;
1331                                 else if (!strncasecmp("digital-unrestricted-tones", p, 26))
1332                                         cond->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1333                                 else
1334                                 {
1335                                         SPRINT(failure, "Given service type is invalid or misspelled.");
1336                                         goto parse_error;
1337                                 }
1338                                 cond->value_type = VALUE_TYPE_INTEGER;
1339                                 break;
1340
1341                                 /* parse bmode value */
1342                                 case COND_TYPE_BMODE:
1343                                 if (!strncasecmp("transparent", p, 11))
1344                                         cond->integer_value = INFO_BMODE_CIRCUIT;
1345                                 else if (!strncasecmp("hdlc", p, 4))
1346                                         cond->integer_value = INFO_BMODE_PACKET;
1347                                 else
1348                                 {
1349                                         SPRINT(failure, "Given bchannel mode is invalid or misspelled.");
1350                                         goto parse_error;
1351                                 }
1352                                 cond->value_type = VALUE_TYPE_INTEGER;
1353                                 break;
1354
1355                                 /* parse interface attribute <if>:<value> */
1356                                 case COND_TYPE_IFATTR:
1357                                 key[0] = key_to[0] = '\0';
1358                                 if (*p==':' || *p==',' || *p==' ' || *p=='\0')
1359                                 {
1360                                         SPRINT(failure, "Missing interface name.");
1361                                         goto parse_error;
1362                                 }
1363                                 p = read_string(p, key, sizeof(key), ":-, ");
1364                                 if (key[0] == 1) /* error */
1365                                 {
1366                                         SPRINT(failure, "Parsing interface failed: %s", key+1);
1367                                         goto parse_error;
1368                                 }
1369                                 if (*p != ':')
1370                                 {
1371                                         SPRINT(failure, "Expeciting kolon to seperate value behind interface name.");
1372                                         goto parse_error;
1373                                 }
1374                                 SCCAT(key, *p++);
1375                                 while(*p>='0' && *p<='9')
1376                                 {
1377                                         SCCAT(key, *p++);
1378                                 }
1379                                 if (*p!=',' && *p!=' ' && *p!='\0')
1380                                 {
1381                                         SPRINT(failure, "Invalid characters behind value.");
1382                                         goto parse_error;
1383                                 }
1384                                 value_type = VALUE_TYPE_STRING;
1385                                 goto alloc_string;
1386                                 break;
1387
1388                                 default:
1389                                 SPRINT(failure, "Software error: COND_TYPE_* %d not parsed in function '%s'", cond_defs[index].type, __FUNCTION__);
1390                                 goto parse_error;
1391                         }
1392                         /* if we have another value for that item, we attach it */
1393                         if (*p == ',')
1394                         {
1395                                 p++;
1396                                 /* next item */
1397                                 cond->value_extension = 1;
1398                                 goto nextcondvalue;
1399                         }
1400                         /* to seperate the items, a space is required */
1401                         if (*p != ' ')
1402                         {
1403                                 SPRINT(failure, "Character '%c' not expected here. Use ',' to seperate multiple possible values.", *p);
1404                                 goto parse_error;
1405                         }
1406                         /* skip spaces */
1407                         while(*p == 32)
1408                         {
1409                                 if (*p == 0)
1410                                         break;
1411                                 p++;
1412                         }
1413                 }
1414
1415                 /* we are done with CONDITIONS, so we expect the ACTION */
1416                 if (*p != ':')
1417                 {
1418                         SPRINT(failure, "Expecting ':' after condition item(s).");
1419                         goto parse_error;
1420                 }
1421                 p++;
1422
1423                 nextaction:
1424                 /* skip spaces, if any */
1425                 while(*p == 32)
1426                 {
1427                         if (*p == 0)
1428                                 break;
1429                         p++;
1430                 }
1431
1432                 /* read action name */
1433                 i = 0;
1434                 while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9') || *p == '-')
1435                 {
1436                         if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1437                         key[i++] = *p++;
1438                         if (i == sizeof(key)) i--; /* limit */
1439                 }
1440                 key[i] = 0;
1441                 if (key[0] == '\0') {
1442                         SPRINT(failure, "Expecting action name.");
1443                         goto parse_error;
1444                 }
1445
1446                 /* check if item exists */
1447                 index = 0;
1448                 while(action_defs[index].name)
1449                 {
1450                         if (!action_defs[index].help) /* not internal actions */
1451                         {
1452                                 index++;
1453                                 continue;
1454                         }
1455                         if (!strcmp(action_defs[index].name, key))
1456                                 break;
1457                         index++;
1458                 }
1459                 if (action_defs[index].name == NULL)
1460                 {
1461                         SPRINT(failure, "Unknown action name '%s'.", key);
1462                         goto parse_error;
1463                 }
1464
1465                 /* alloc memory for action */
1466                 action = (struct route_action *)MALLOC(sizeof(struct route_action));
1467                 rmemuse++;
1468                 *action_pointer = action;
1469                 action_pointer = &(action->next);
1470                 param_pointer = &(action->param_first);
1471                 action->index = index;
1472                 action->line = line[nesting];
1473
1474                 /* skip spaces after param name */
1475                 while(*p == 32)
1476                 {
1477                         if (*p == 0)
1478                                 break;
1479                         p++;
1480                 }
1481
1482                 /* loop PARAMS */
1483                 while(*p != 0)
1484                 {
1485                         /* read param text */
1486                         i = 0;
1487                         while((*p>='a' && *p<='z') || (*p>='A' && *p<='Z') || (*p>='0' && *p<='9'))
1488                         {
1489                                 if (*p>='A' && *p<='Z') *p = *p-'A'+'a'; /* lower case */
1490                                 key[i++] = *p++;
1491                                 if (i == sizeof(key)) i--; /* limit */
1492                         }
1493                         key[i] = 0;
1494                         if (key[0] == '\0') {
1495                                 SPRINT(failure, "Expecting parameter name.");
1496                                 goto parse_error;
1497                         }
1498
1499                         /* check if item exists */
1500                         index = 0;
1501                         while(param_defs[index].name)
1502                         {
1503                                 if (!strcmp(param_defs[index].name, key))
1504                                         break;
1505                                 index++;
1506                         }
1507                         if (param_defs[index].name == NULL)
1508                         {
1509                                 SPRINT(failure, "Unknown param name '%s'.", key);
1510                                 goto parse_error;
1511                         }
1512
1513                         /* params without values must not have any parameter */
1514                         if (param_defs[index].type == PARAM_TYPE_NULL)
1515                         {
1516                                 if (*p!=' ' && *p!='\0')
1517                                 {
1518                                         SPRINT(failure, "Parameter '%s' must not have any value.", key);
1519                                         goto parse_error;
1520                                 }
1521                         } else
1522                         {
1523                                 if (*p == ' ')
1524                                 {
1525                                         SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected and not a space.", key);
1526                                         goto parse_error;
1527                                 }
1528                                 if (*p != '=')
1529                                 {
1530                                         SPRINT(failure, "Parameter '%s' must have at least one value, '=' expected.", key);
1531                                         goto parse_error;
1532                                 }
1533                                 p++;
1534                         }
1535
1536                         /* special timeout value */
1537                         if (!strcmp("timeout", key))
1538                         {
1539                                 if (action->timeout)
1540                                 {
1541                                         SPRINT(failure, "Duplicate timeout value.");
1542                                         goto parse_error;
1543                                 }
1544                                 if (*p==',' || *p==' ' || *p=='\0')
1545                                 {
1546                                         SPRINT(failure, "Missing integer value.");
1547                                         goto parse_error;
1548                                 }
1549                                 integer = 0;
1550                                 while(*p>='0' && *p<='9')
1551                                 {
1552                                         integer = integer*10 + *p-'0';
1553                                         p++;
1554                                 }
1555                                 if (integer < 1)
1556                                 {
1557                                         SPRINT(failure, "Expecting timeout value greater 0.");
1558                                         goto parse_error;
1559                                 }
1560                                 if (*p!=' ' && *p!='\0')
1561                                 {
1562                                         SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters.", *p);
1563                                         goto parse_error;
1564                                 }
1565                                 /* skip spaces */
1566                                 while(*p == 32)
1567                                 {
1568                                         if (*p == 0)
1569                                                 break;
1570                                         p++;
1571                                 }
1572                                 action->timeout = integer;
1573                                 /* check for next ACTION */
1574                                 if (*p == ':')
1575                                 {
1576                                         p++;
1577                                         goto nextaction;
1578                                 }
1579                                 continue;
1580                         }
1581
1582                         /* check for duplicate parameters */
1583                         param = action->param_first;
1584                         while(param)
1585                         {
1586                                 if (param->index == index)
1587                                 {
1588                                         SPRINT(failure, "Duplicate parameter '%s', use ',' to give multiple values.", key);
1589                                         goto parse_error;
1590                                 }
1591                                 param = param->next;
1592                         }
1593
1594                         nextparamvalue:
1595                         /* Alloc memory for param */
1596                         param = (struct route_param *)MALLOC(sizeof(struct route_param));
1597                         rmemuse++;
1598                         *param_pointer = param;
1599                         param_pointer = &(param->next);
1600                         param->index = index;
1601                         param->id = param_defs[index].id;
1602
1603                         switch(param_defs[index].type)
1604                         {
1605                                 /* parse null value */
1606                                 case PARAM_TYPE_NULL:
1607                                 param->value_type = VALUE_TYPE_NULL;
1608                                 break;
1609
1610                                 /* parse integer value */
1611                                 case PARAM_TYPE_INTEGER:
1612                                 integer = 0;
1613                                 if (*p==',' || *p==' ' || *p=='\0')
1614                                 {
1615                                         SPRINT(failure, "Missing integer value.");
1616                                         goto parse_error;
1617                                 }
1618                                 while(*p>='0' && *p<='9')
1619                                 {
1620                                         integer = integer*10 + *p-'0';
1621                                         p++;
1622                                 }
1623                                 param->integer_value = integer;
1624                                 param->value_type = VALUE_TYPE_INTEGER;
1625                                 break;
1626
1627 #if 0
1628                                 /* parse ports value */
1629                                 case PARAM_TYPE_PORTS:
1630                                 key[0] = '\0';
1631                                 if (*p==',' || *p==' ' || *p=='\0')
1632                                 {
1633                                         SPRINT(failure, "Missing port number, omit parameter or give port number.");
1634                                         goto parse_error;
1635                                 }
1636                                 i = 0;
1637                                 nextport:
1638                                 integer = 0;
1639                                 while(*p>='0' && *p<='9')
1640                                 {
1641                                         if (i < (int)sizeof(key)-1)
1642                                         {
1643                                                 key[i] = *p;
1644                                                 key[i++] = '\0';
1645                                         }
1646                                         integer = integer*10 + *p-'0';
1647                                         p++;
1648                                 }
1649                                 if (integer > 255)
1650                                 {
1651                                         SPRINT(failure, "Port number too high.");
1652                                         goto parse_error;
1653                                 }
1654                                 if (*p==',')
1655                                 {
1656                                         if (i < (int)sizeof(key)-1)
1657                                         {
1658                                                 key[i] = *p;
1659                                                 key[i++] = '\0';
1660                                         }
1661                                         p++;
1662                                         goto nextport;
1663                                 }
1664                                 goto mallocstring;
1665 #endif
1666
1667                                 /* parse string value */
1668                                 case PARAM_TYPE_STRING:
1669                                 case PARAM_TYPE_CALLERIDTYPE:
1670                                 case PARAM_TYPE_CAPABILITY:
1671                                 case PARAM_TYPE_BMODE:
1672                                 case PARAM_TYPE_DIVERSION:
1673                                 case PARAM_TYPE_DESTIN:
1674                                 case PARAM_TYPE_TYPE:
1675                                 case PARAM_TYPE_YESNO:
1676                                 key[0] = '\0';
1677                                 if (*p==',' || *p==' ' || *p=='\0')
1678                                 {
1679                                         SPRINT(failure, "Missing string value, use \"\" for empty string.");
1680                                         goto parse_error;
1681                                 }
1682                                 p = read_string(p, key, sizeof(key), " ");
1683                                 if (key[0] == 1) /* error */
1684                                 {
1685                                         SPRINT(failure, "Parsing string failed: %s", key+1);
1686                                         goto parse_error;
1687                                 }
1688                                 if (param_defs[index].type == PARAM_TYPE_CALLERIDTYPE)
1689                                 {
1690                                         param->value_type = VALUE_TYPE_INTEGER;
1691                                         if (!strcasecmp(key, "unknown"))
1692                                         {
1693                                                 param->integer_value = INFO_NTYPE_UNKNOWN;
1694                                                 break;
1695                                         }
1696                                         if (!strcasecmp(key, "subscriber"))
1697                                         {
1698                                                 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1699                                                 break;
1700                                         }
1701                                         if (!strcasecmp(key, "national"))
1702                                         {
1703                                                 param->integer_value = INFO_NTYPE_NATIONAL;
1704                                                 break;
1705                                         }
1706                                         if (!strcasecmp(key, "international"))
1707                                         {
1708                                                 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1709                                                 break;
1710                                         }
1711                                         SPRINT(failure, "Caller ID type '%s' unknown.", key);
1712                                         goto parse_error;
1713                                 }
1714                                 if (param_defs[index].type == PARAM_TYPE_CAPABILITY)
1715                                 {
1716                                         param->value_type = VALUE_TYPE_INTEGER;
1717                                         if (!strcasecmp(key, "speech"))
1718                                         {
1719                                                 param->integer_value = INFO_BC_SPEECH;
1720                                                 break;
1721                                         }
1722                                         if (!strcasecmp(key, "audio"))
1723                                         {
1724                                                 param->integer_value = INFO_BC_AUDIO;
1725                                                 break;
1726                                         }
1727                                         if (!strcasecmp(key, "video"))
1728                                         {
1729                                                 param->integer_value = INFO_BC_VIDEO;
1730                                                 break;
1731                                         }
1732                                         if (!strcasecmp(key, "digital-restricted"))
1733                                         {
1734                                                 param->integer_value = INFO_BC_DATARESTRICTED;
1735                                                 break;
1736                                         }
1737                                         if (!strcasecmp(key, "digital-unrestricted"))
1738                                         {
1739                                                 param->integer_value = INFO_BC_DATAUNRESTRICTED;
1740                                                 break;
1741                                         }
1742                                         if (!strcasecmp(key, "digital-unrestricted-tones"))
1743                                         {
1744                                                 param->integer_value = INFO_BC_DATAUNRESTRICTED_TONES;
1745                                                 break;
1746                                         }
1747                                         SPRINT(failure, "Service type '%s' unknown.", key);
1748                                         goto parse_error;
1749                                 }
1750                                 if (param_defs[index].type == PARAM_TYPE_BMODE)
1751                                 {
1752                                         param->value_type = VALUE_TYPE_INTEGER;
1753                                         if (!strcasecmp(key, "transparent"))
1754                                         {
1755                                                 param->integer_value = INFO_BMODE_CIRCUIT;
1756                                                 break;
1757                                         }
1758                                         if (!strcasecmp(key, "hdlc"))
1759                                         {
1760                                                 param->integer_value = INFO_BMODE_PACKET;
1761                                                 break;
1762                                         }
1763                                         SPRINT(failure, "Bchannel mode '%s' unknown.", key);
1764                                         goto parse_error;
1765                                 }
1766                                 if (param_defs[index].type == PARAM_TYPE_DIVERSION)
1767                                 {
1768                                         param->value_type = VALUE_TYPE_INTEGER;
1769                                         if (!strcasecmp(key, "cfu"))
1770                                         {
1771                                                 param->integer_value = INFO_DIVERSION_CFU;
1772                                                 break;
1773                                         }
1774                                         if (!strcasecmp(key, "cfb"))
1775                                         {
1776                                                 param->integer_value = INFO_DIVERSION_CFB;
1777                                                 break;
1778                                         }
1779                                         if (!strcasecmp(key, "cfnr"))
1780                                         {
1781                                                 param->integer_value = INFO_DIVERSION_CFNR;
1782                                                 break;
1783                                         }
1784                                         if (!strcasecmp(key, "cfp"))
1785                                         {
1786                                                 param->integer_value = INFO_DIVERSION_CFP;
1787                                                 break;
1788                                         }
1789                                         SPRINT(failure, "Diversion type '%s' unknown.", key);
1790                                         goto parse_error;
1791                                 }
1792                                 if (param_defs[index].type == PARAM_TYPE_TYPE)
1793                                 {
1794                                         param->value_type = VALUE_TYPE_INTEGER;
1795                                         if (!strcasecmp(key, "unknown"))
1796                                         {
1797                                                 param->integer_value = INFO_NTYPE_UNKNOWN;
1798                                                 break;
1799                                         }
1800                                         if (!strcasecmp(key, "subscriber"))
1801                                         {
1802                                                 param->integer_value = INFO_NTYPE_SUBSCRIBER;
1803                                                 break;
1804                                         }
1805                                         if (!strcasecmp(key, "national"))
1806                                         {
1807                                                 param->integer_value = INFO_NTYPE_NATIONAL;
1808                                                 break;
1809                                         }
1810                                         if (!strcasecmp(key, "international"))
1811                                         {
1812                                                 param->integer_value = INFO_NTYPE_INTERNATIONAL;
1813                                                 break;
1814                                         }
1815                                         SPRINT(failure, "Number type '%s' unknown.", key);
1816                                         goto parse_error;
1817                                 }
1818                                 if (param_defs[index].type == PARAM_TYPE_YESNO)
1819                                 {
1820                                         param->value_type = VALUE_TYPE_INTEGER;
1821                                         if (!strcasecmp(key, "yes"))
1822                                         {
1823                                                 param->integer_value = 1;
1824                                                 break;
1825                                         }
1826                                         if (!strcasecmp(key, "no"))
1827                                         {
1828                                                 param->integer_value = 0;
1829                                                 break;
1830                                         }
1831                                         SPRINT(failure, "Value '%s' unknown. ('yes' or 'no')", key);
1832                                         goto parse_error;
1833                                 }
1834                                 param->string_value = (char *)MALLOC(strlen(key)+1);
1835                                 rmemuse++;
1836                                 UCPY(param->string_value, key);
1837                                 param->value_type = VALUE_TYPE_STRING;
1838                                 break;
1839
1840                                 default:
1841                                 SPRINT(failure, "Software error: PARAM_TYPE_* %d not parsed in function '%s'", param_defs[index].type, __FUNCTION__);
1842                                 goto parse_error;
1843                         }
1844
1845                         if (*p == ',')
1846                         {
1847                                 p++;
1848                                 /* next item */
1849                                 param->value_extension = 1;
1850                                 goto nextparamvalue;
1851                         }
1852
1853                         /* end of line */
1854                         if (*p == '\0')
1855                                 break;
1856
1857                         /* to seperate the items, a space is required */
1858                         if (*p != ' ')
1859                         {
1860                                 SPRINT(failure, "Character '%c' not expected here. Use ' ' to seperate parameters, or ',' for multiple values.", *p);
1861                                 goto parse_error;
1862                         }
1863                         /* skip spaces */
1864                         while(*p == 32)
1865                         {
1866                                 if (*p == 0)
1867                                         break;
1868                                 p++;
1869                         }
1870
1871                         /* check for next ACTION */
1872                         if (*p == ':')
1873                         {
1874                                 p++;
1875                                 goto nextaction;
1876                         }
1877                 }
1878         }
1879
1880         fclose(fp[nesting--]);
1881         fduse--;
1882
1883         if (nesting >= 0)
1884                 goto go_root;
1885
1886         if (!ruleset_start)
1887         {
1888                 SPRINT(failure, "No ruleset defined.");
1889         }
1890         return(ruleset_start);
1891
1892         parse_error:
1893         printf("While parsing %s, an error occurred in line %d:\n", filename[nesting], line[nesting]);
1894         printf("-> %s\n", buffer);
1895         memset(pointer, ' ', sizeof(pointer));
1896         pointer[p-buffer] = '^';
1897         pointer[p-buffer+1] = '\0';
1898         printf("   %s\n", pointer);
1899         printf("%s\n", failure);
1900         SPRINT(ruleset_error, "Error in file %s, line %d: %s",  filename[nesting], line[nesting], failure);
1901
1902         openerror:
1903         while(nesting >= 0)
1904         {
1905                 fclose(fp[nesting--]);
1906                 fduse--;
1907         }
1908
1909         ruleset_free(ruleset_start);
1910         return(NULL);
1911 }
1912
1913 /*
1914  * return ruleset by name
1915  */
1916 struct route_ruleset *getrulesetbyname(char *name)
1917 {
1918         struct route_ruleset *ruleset = ruleset_first;
1919
1920         while(ruleset)
1921         {
1922                 if (!strcasecmp(name, ruleset->name))
1923                 {
1924                         break;
1925                 }
1926                 ruleset = ruleset->next;
1927         }
1928         PDEBUG(DEBUG_ROUTE, "ruleset %s %s.\n", name, ruleset?"found":"not found");
1929         return(ruleset);
1930 }
1931
1932 /*
1933  * parses the current ruleset and returns action
1934  */
1935 struct route_action *EndpointAppPBX::route(struct route_ruleset *ruleset)
1936 {
1937         int                     match,
1938                                 couldmatch = 0, /* any rule could match */
1939                                 istrue,
1940                                 couldbetrue,
1941                                 condition,
1942                                 dialing_required,
1943                                 avail,
1944                                 any;
1945         struct route_rule       *rule = ruleset->rule_first;
1946         struct route_cond       *cond;
1947         struct route_action     *action = NULL;
1948         unsigned long           comp_len;
1949         int                     j, jj;
1950         char                    callerid[64],   redirid[64];
1951         int                     integer;
1952         char                    *string;
1953         FILE                    *tfp;
1954         double                  timeout;
1955         struct mISDNport        *mISDNport;
1956         struct admin_list       *admin;
1957
1958         /* reset timeout action */
1959         e_match_timeout = 0; /* no timeout */
1960         e_match_to_action = NULL;
1961
1962         SCPY(callerid, numberrize_callerinfo(e_callerinfo.id, e_callerinfo.ntype));
1963         SCPY(redirid, numberrize_callerinfo(e_redirinfo.id, e_redirinfo.ntype));
1964         
1965         PDEBUG(DEBUG_ROUTE, "parsing ruleset '%s'\n", ruleset->name);
1966         while(rule)
1967         {
1968                 PDEBUG(DEBUG_ROUTE, "checking rule in line %d\n", rule->line);
1969                 match = 1; /* this rule matches */
1970                 dialing_required = 0;
1971                 timeout = 0; /* timeout time */
1972                 cond = rule->cond_first;
1973                 while(cond)
1974                 {
1975                         condition = 0; /* any condition element is true (1) or could be true (2) */
1976                         checkextension:
1977                         istrue = 0; /* this condition-element is true */
1978                         couldbetrue = 0; /* this conditions-element could be true */
1979                         switch(cond->match)
1980                         {
1981                                 case MATCH_EXTERN:
1982                                 if (!e_ext.number[0])
1983                                         istrue = 1;      
1984                                 break;
1985
1986                                 case MATCH_INTERN:
1987                                 if (e_ext.number[0])
1988                                         istrue = 1;      
1989                                 break;
1990
1991                                 case MATCH_PORT:
1992                                 if (ea_endpoint->ep_portlist)
1993                                 if ((ea_endpoint->ep_portlist->port_type & PORT_CLASS_mISDN_MASK) != PORT_CLASS_mISDN_DSS1)
1994                                         break;
1995                                 integer = e_callerinfo.isdn_port;
1996                                 goto match_integer;
1997
1998                                 case MATCH_INTERFACE:
1999                                 if (!e_callerinfo.interface[0])
2000                                         break;
2001                                 string = e_callerinfo.interface;
2002                                 goto match_string_prefix;
2003
2004                                 case MATCH_CALLERID:
2005                                 string = callerid;
2006                                 goto match_string_prefix;
2007
2008                                 case MATCH_EXTENSION:
2009                                 string = e_ext.name;
2010                                 goto match_string;
2011
2012                                 case MATCH_DIALING:
2013                                 string = e_dialinginfo.id;
2014                                 goto match_string_prefix;
2015
2016                                 case MATCH_ENBLOCK:
2017                                 if (!e_overlap)
2018                                         istrue = 1;
2019                                 break;
2020
2021                                 case MATCH_OVERLAP:
2022                                 if (e_overlap)
2023                                         istrue = 1;
2024                                 break;
2025
2026                                 case MATCH_ANONYMOUS:
2027                                 if (e_callerinfo.present != INFO_PRESENT_ALLOWED)
2028                                         istrue = 1;
2029                                 break;
2030
2031                                 case MATCH_VISIBLE:
2032                                 if (e_callerinfo.present == INFO_PRESENT_ALLOWED)
2033                                         istrue = 1;
2034                                 break;
2035
2036                                 case MATCH_UNKNOWN:
2037                                 if (e_callerinfo.present == INFO_PRESENT_NOTAVAIL)
2038                                         istrue = 1;
2039                                 break;
2040
2041                                 case MATCH_AVAILABLE:
2042                                 if (e_callerinfo.present != INFO_PRESENT_NOTAVAIL)
2043                                         istrue = 1;
2044                                 break;
2045
2046                                 case MATCH_FAKE:
2047                                 if (e_callerinfo.screen == INFO_SCREEN_USER)
2048                                         istrue = 1;
2049                                 break;
2050
2051                                 case MATCH_REAL:
2052                                 if (e_callerinfo.screen != INFO_SCREEN_USER)
2053                                         istrue = 1;
2054                                 break;
2055
2056                                 case MATCH_REDIRECTED:
2057                                 if (e_redirinfo.present != INFO_PRESENT_NULL)
2058                                         istrue = 1;
2059                                 break;
2060
2061                                 case MATCH_DIRECT:
2062                                 if (e_redirinfo.present == INFO_PRESENT_NULL)
2063                                         istrue = 1;
2064                                 break;
2065
2066                                 case MATCH_REDIRID:
2067                                 string = redirid;
2068                                 goto match_string_prefix;
2069
2070                                 case MATCH_TIME:
2071                                 integer = now_tm->tm_hour*100 + now_tm->tm_min;
2072                                 goto match_integer;
2073
2074                                 case MATCH_MDAY:
2075                                 integer = now_tm->tm_mday;
2076                                 goto match_integer;
2077
2078                                 case MATCH_MONTH:
2079                                 integer = now_tm->tm_mon+1;
2080                                 goto match_integer;
2081
2082                                 case MATCH_YEAR:
2083                                 integer = now_tm->tm_year + 1900;
2084                                 goto match_integer;
2085
2086                                 case MATCH_WDAY:
2087                                 integer = now_tm->tm_wday;
2088                                 integer = integer?integer:7; /* correct sunday */
2089                                 goto match_integer;
2090
2091                                 case MATCH_CAPABILITY:
2092                                 integer = e_capainfo.bearer_capa;
2093                                 goto match_integer;
2094         
2095                                 case MATCH_INFOLAYER1:
2096                                 integer = e_capainfo.bearer_info1;
2097                                 goto match_integer;
2098
2099                                 case MATCH_HLC:
2100                                 integer = e_capainfo.hlc;
2101                                 goto match_integer;
2102
2103                                 case MATCH_FILE:
2104                                 tfp = fopen(cond->string_value, "r");
2105                                 if (!tfp)
2106                                 {
2107                                         break;
2108                                 }
2109                                 if (fgetc(tfp) == '1')
2110                                         istrue = 1;
2111                                 fclose(tfp);
2112                                 break;
2113
2114                                 case MATCH_EXECUTE:
2115                                 if (system(cond->string_value) == 0)
2116                                         istrue = 1;
2117                                 break;
2118
2119                                 case MATCH_DEFAULT:
2120                                 if (!couldmatch)
2121                                         istrue = 1;
2122                                 break;
2123
2124                                 case MATCH_TIMEOUT:
2125                                 timeout = now_d + cond->integer_value;
2126                                 istrue = 1;
2127                                 break;
2128
2129                                 case MATCH_FREE:
2130                                 case MATCH_NOTFREE:
2131                                 if (!(comp_len = (unsigned long)strchr(cond->string_value, ':')))
2132                                         break;
2133                                 comp_len = comp_len-(unsigned long)cond->string_value;
2134                                 avail = 0;
2135                                 mISDNport = mISDNport_first;
2136                                 while(mISDNport)
2137                                 {
2138                                         if (mISDNport->ifport)
2139                                         if (strlen(mISDNport->ifport->interface->name) == comp_len)
2140                                         if (!strncasecmp(mISDNport->ifport->interface->name, cond->string_value, comp_len)) 
2141                                         if (!mISDNport->ptp || mISDNport->l2link)
2142                                         {
2143                                                 j = 0;
2144                                                 jj = mISDNport->b_num;
2145                                                 avail += jj;
2146                                                 while(j < jj)
2147                                                 {
2148                                                         if (mISDNport->b_state[j])
2149                                                                 avail--;
2150                                                         j++;
2151                                                 }
2152                                         }
2153                                         mISDNport = mISDNport->next;
2154                                 }
2155                                 if (cond->match == MATCH_FREE)
2156                                 {
2157                                         if (avail >= atoi(cond->string_value + comp_len + 1))
2158                                                 istrue = 1;
2159                                 } else
2160                                 {
2161                                         if (avail < atoi(cond->string_value + comp_len + 1))
2162                                                 istrue = 1;
2163                                 }
2164                                 break;
2165
2166
2167                                 case MATCH_DOWN:
2168                                 mISDNport = mISDNport_first;
2169                                 while(mISDNport)
2170                                 {
2171                                         if (mISDNport->ifport)
2172                                         if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value)) 
2173                                         if (!mISDNport->ptp || mISDNport->l2link) /* break if one is up */
2174                                                 break;
2175                                         mISDNport = mISDNport->next;
2176                                 }
2177                                 if (!mISDNport) /* all down */
2178                                         istrue = 1;
2179                                 break;
2180
2181                                 case MATCH_UP:
2182                                 mISDNport = mISDNport_first;
2183                                 while(mISDNport)
2184                                 {
2185                                         if (mISDNport->ifport)
2186                                         if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value)) 
2187                                         if (!mISDNport->ptp || mISDNport->l2link) /* break if one is up */
2188                                                 break;
2189                                         
2190                                         mISDNport = mISDNport->next;
2191                                 }
2192                                 if (mISDNport) /* one link at least */
2193                                         istrue = 1;
2194                                 break;
2195
2196                                 case MATCH_BUSY:
2197                                 case MATCH_IDLE:
2198                                 any = 0;
2199                                 mISDNport = mISDNport_first;
2200                                 while(mISDNport)
2201                                 {
2202                                         if (mISDNport->ifport)
2203                                         if (!strcasecmp(mISDNport->ifport->interface->name, cond->string_value)) 
2204                                         if (mISDNport->use) /* break if in use */
2205                                                 break;
2206                                         mISDNport = mISDNport->next;
2207                                 }
2208                                 if (mISDNport && cond->match==MATCH_BUSY)
2209                                         istrue = 1;
2210                                 if (!mISDNport && cond->match==MATCH_IDLE)
2211                                         istrue = 1;
2212                                 break;
2213
2214                                 case MATCH_REMOTE:
2215                                 case MATCH_NOTREMOTE:
2216                                 admin = admin_first;
2217                                 while(admin)
2218                                 {
2219                                         if (admin->remote[0] && !strcmp(cond->string_value, admin->remote))
2220                                                 break;
2221                                         admin = admin->next;
2222                                 }
2223                                 if (admin && cond->match==MATCH_REMOTE)
2224                                         istrue = 1;
2225                                 if (!admin && cond->match==MATCH_NOTREMOTE)
2226                                         istrue = 1;
2227                                 break;
2228
2229                                 default:
2230                                 PERROR("Software error: MATCH_* %d not parsed in function '%s'", cond->match, __FUNCTION__);
2231                                 break;
2232
2233                                 match_integer:
2234                                 if (cond->value_type == VALUE_TYPE_INTEGER)
2235                                 {
2236                                         if (integer != cond->integer_value)
2237                                                 break;
2238                                         istrue = 1;
2239                                         break;
2240                                 }
2241                                 if (cond->value_type == VALUE_TYPE_INTEGER_RANGE)
2242                                 {
2243                                         /* check if negative range (2100 - 700 o'clock) */
2244                                         if (cond->integer_value > cond->integer_value_to)
2245                                         {
2246                                                 if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2247                                                         istrue = 1;
2248                                                 break;
2249                                         }
2250                                         /* range is positive */
2251                                         if (integer>=cond->integer_value && integer<=cond->integer_value_to)
2252                                                 istrue = 1;
2253                                         break;
2254                                 }
2255                                 break;
2256
2257                                 match_string:
2258                                 if (strlen(cond->string_value) != strlen(string))
2259                                         break;
2260                                 /* fall through */
2261                                 match_string_prefix:
2262                                 comp_len = strlen(cond->string_value); /* because we must reach value's length */
2263                                 /* we must have greater or equal length to values */
2264                                 if ((unsigned long)strlen(string) < comp_len)
2265                                 {
2266                                         /* special case for unfinished dialing */
2267                                         if (cond->match == MATCH_DIALING)
2268                                         {
2269                                                 couldbetrue = 1; /* could match */
2270                                                 comp_len = strlen(string);
2271                                         } else
2272                                         {
2273                                                 break;
2274                                         }
2275                                 }
2276                                 /* on single string match */
2277                                 if (cond->value_type == VALUE_TYPE_STRING)
2278                                 {
2279                                         if (!strncmp(string, cond->string_value, comp_len))
2280                                         {
2281                                                 istrue = 1;
2282                                                 /* must be set for changing 'e_extdialing' */
2283                                                 if (cond->match == MATCH_DIALING)
2284                                                         dialing_required = comp_len;
2285                                                 break;
2286                                         }
2287                                         break;
2288                                 }
2289                                 /* on range match */
2290                                 if (cond->value_type == VALUE_TYPE_STRING_RANGE)
2291                                 {
2292                                         /* check if negative range ("55"-"22") */
2293                                         if (cond->comp_string > 0)
2294                                         {
2295                                                 if (strncmp(string, cond->string_value, comp_len) >= 0)
2296                                                 {
2297                                                         istrue = 1;
2298                                                         /* must be set for changing 'e_extdialing' */
2299                                                         if (cond->match == MATCH_DIALING)
2300                                                                 dialing_required = comp_len;
2301                                                         break;
2302                                                 }
2303                                                 if (strncmp(string, cond->string_value_to, comp_len) <= 0)
2304                                                 {
2305                                                         /* must be set for changing 'e_extdialing' */
2306                                                         istrue = 1;
2307                                                         if (cond->match == MATCH_DIALING)
2308                                                                 dialing_required = comp_len;
2309                                                         break;
2310                                                 }
2311                                                 break;
2312                                         }
2313                                         /* range is positive */
2314                                         if (strncmp(string, cond->string_value, comp_len) < 0)
2315                                                 break;
2316                                         if (strncmp(string, cond->string_value_to, comp_len) > 0)
2317                                                 break;
2318                                         istrue = 1;
2319                                         if (cond->match == MATCH_DIALING)
2320                                                 dialing_required = comp_len;
2321                                         break;
2322                                 }
2323                                 break;
2324                         }
2325
2326                         /* set current condition */
2327                         if (istrue && !couldbetrue)
2328                                 condition = 1; /* element matches, so condition matches */
2329                         if (istrue && couldbetrue && !condition)
2330                                 condition = 2; /* element could match and other elements don't match, so condition could match */
2331
2332                         /* if not matching or could match */
2333                         if (condition != 1)
2334                         {
2335                                 /* if we have more values to check */
2336                                 if (cond->value_extension && cond->next)
2337                                 {
2338                                         cond = cond->next;
2339                                         goto checkextension;
2340                                 }
2341                                 match = condition;
2342                                 break;
2343                         }
2344                         
2345                         /* skip exteded values, beacuse we already have one matching value */
2346                         while(cond->value_extension && cond->next)
2347                                 cond = cond->next;
2348
2349                         cond = cond->next;
2350                 }
2351                 if (timeout>now_d && match==1) /* the matching rule with timeout in the future */
2352                 if (e_match_timeout<1 || timeout<e_match_timeout) /* first timeout or lower */
2353                 {
2354                         /* set timeout in the furture */
2355                         e_match_timeout = timeout;
2356                         e_match_to_action = rule->action_first;
2357                         e_match_to_extdialing = e_dialinginfo.id + dialing_required;
2358                         match = 0; /* matches in the future */
2359                 }
2360                 if (match == 1)
2361                 {
2362                         /* matching, we return first action */
2363                         action = rule->action_first;
2364                         e_match_timeout = 0; /* no timeout */
2365                         e_match_to_action = NULL;
2366                         e_extdialing = e_dialinginfo.id + dialing_required;
2367                         break;
2368                 }
2369                 if (match == 2)
2370                 {
2371                         /* rule could match if more is dialed */
2372                         couldmatch = 1;
2373                 }
2374                 rule = rule->next;
2375         }
2376         return(action);
2377 }
2378
2379 /*
2380  * parses the current action's parameters and return them
2381  */
2382 struct route_param *EndpointAppPBX::routeparam(struct route_action *action, unsigned long long id)
2383 {
2384         struct route_param *param = action->param_first;
2385
2386         while(param)
2387         {
2388                 if (param->id == id)
2389                         break;
2390                 param = param->next;
2391         }
2392         return(param);
2393 }
2394
2395
2396 /*
2397  * internal rules that are not defined by route.conf
2398  */
2399 struct route_action action_password = {
2400         NULL,
2401         NULL,
2402         ACTION_PASSWORD,
2403         0,
2404         0,
2405 };
2406
2407 struct route_action action_password_write = {
2408         NULL,
2409         NULL,
2410         ACTION_PASSWORD_WRITE,
2411         0,
2412         0,
2413 };
2414
2415 struct route_action action_external = {
2416         NULL,
2417         NULL,
2418         ACTION_EXTERNAL,
2419         0,
2420         0,
2421 };
2422
2423 struct route_action action_internal = {
2424         NULL,
2425         NULL,
2426         ACTION_INTERNAL,
2427         0,
2428         0,
2429 };
2430
2431 struct route_action action_remote = {
2432         NULL,
2433         NULL,
2434         ACTION_REMOTE,
2435         0,
2436         0,
2437 };
2438
2439 struct route_action action_vbox = {
2440         NULL,
2441         NULL,
2442         ACTION_VBOX_RECORD,
2443         0,
2444         0,
2445 };
2446
2447 struct route_action action_partyline = {
2448         NULL,
2449         NULL,
2450         ACTION_PARTYLINE,
2451         0,
2452         0,
2453 };
2454
2455 struct route_action action_disconnect = {
2456         NULL,
2457         NULL,
2458         ACTION_DISCONNECT,
2459         0,
2460         0,
2461 };
2462
2463