work on unfinished asterisk channel driver (bchannel handling)
[lcr.git] / apppbx.h
1 /*****************************************************************************\
2 **                                                                           **
3 ** Linux Call Router                                                         **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** EndpointAppPBX header file                                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12
13 enum { /* release actions: see epoint.release */
14         RELEASE_NONE,
15         RELEASE_JOIN,           /* join, hold */
16         RELEASE_PORT_JOINONLY,  /* join, port */
17         RELEASE_ALL,            /* join, hold, port */
18 };
19
20 enum { /* states as viewed from io port (state of calls are always connected) */
21         EPOINT_STATE_IDLE,              /* no call */
22         EPOINT_STATE_IN_SETUP,          /* setup sent */
23         EPOINT_STATE_OUT_SETUP,         /* setup sent */
24         EPOINT_STATE_IN_OVERLAP,        /* more information */
25         EPOINT_STATE_OUT_OVERLAP,       /* more information */
26         EPOINT_STATE_IN_PROCEEDING,     /* proceeding */
27         EPOINT_STATE_OUT_PROCEEDING,    /* proceeding */
28         EPOINT_STATE_IN_ALERTING,       /* ringing */
29         EPOINT_STATE_OUT_ALERTING,      /* ringing */
30         EPOINT_STATE_CONNECT,           /* connected */
31         EPOINT_STATE_IN_DISCONNECT,     /* disconnected receiving tones */
32         EPOINT_STATE_OUT_DISCONNECT,    /* disconnected sending tones */
33 };
34
35 #define EPOINT_STATE_NAMES \
36 static char *state_name[] = { \
37         "EPOINT_STATE_IDLE", \
38         "EPOINT_STATE_IN_SETUP", \
39         "EPOINT_STATE_OUT_SETUP", \
40         "EPOINT_STATE_IN_OVERLAP", \
41         "EPOINT_STATE_OUT_OVERLAP", \
42         "EPOINT_STATE_IN_PROCEEDING", \
43         "EPOINT_STATE_OUT_PROCEEDING", \
44         "EPOINT_STATE_IN_ALERTING", \
45         "EPOINT_STATE_OUT_ALERTING", \
46         "EPOINT_STATE_CONNECT", \
47         "EPOINT_STATE_IN_DISCONNECT", \
48         "EPOINT_STATE_OUT_DISCONNECT", \
49 }; \
50 int state_name_num = sizeof(state_name) / sizeof(char *);
51
52 extern class EndpointAppPBX *apppbx_first;
53
54 /* structure of an EndpointAppPBX */
55 class EndpointAppPBX : public EndpointApp
56 {
57         public:
58         EndpointAppPBX(class Endpoint *epoint, int origin);
59         ~EndpointAppPBX();
60
61         class EndpointAppPBX    *next;
62         int                     handler(void);
63
64         int                     e_hold;                 /* is this endpoint on hold ? */
65         char                    e_tone[256];            /* save tone for resuming ports */
66
67         unsigned long           e_adminid;
68
69         /* states */
70         int                     e_state;                /* state of endpoint */
71         char                    e_extension_interface[32];/* current internal isdn interface (usefull for callback to internal phone) */
72         struct caller_info      e_callerinfo;           /* information about the caller */
73         struct dialing_info     e_dialinginfo;          /* information about dialing */
74         struct connect_info     e_connectinfo;          /* information about connected line */
75         struct redir_info       e_redirinfo;            /* info on redirection (to the calling user) */
76         struct capa_info        e_capainfo;             /* info on l3,l2 capacity */
77         time_t                  e_start, e_stop;        /* time */
78         int                     e_origin;               /* origin of call 0=incoming 1=outgoing */
79         struct route_ruleset    *e_ruleset;             /* current ruleset pointer (NULL=no ruleset) */
80         struct route_rule       *e_rule;                /* current rule pointer (NULL=no rule) */
81         struct route_action     *e_action;              /* current action pointer (NULL=no action) */
82         double                  e_action_timeout;       /* when to timeout */
83         int                     e_rule_nesting;         /* 'goto'/'menu' recrusion counter to prevent infinie loops */
84         double                  e_match_timeout;        /* set for the next possible timeout time */
85         struct route_action     *e_match_to_action;     /* what todo when timeout */
86         char                    *e_match_to_extdialing; /* dialing after matching timeout rule */
87         int                     e_select;               /* current selection for various selector options */
88         char                    *e_extdialing;          /* dialing after matching rule */
89         int             e_overlap;              /* is set if additional information is/are received after setup */
90         struct extension e_ext;                 /* extension information */
91
92 //      int e_knocking;                         /* true, if knocking */
93 //      double e_knocktime;                     /* next time to knock */
94
95 //      char e_join_tone[64], e_hold_tone[64];  /* current tone */
96         int e_join_pattern/*, e_hold_pattern*/; /* pattern available */
97
98         /* action */
99         char e_dialing_queue[32];               /* holds dialing during setup state */
100         double e_redial;                        /* time when redialing 0=off */
101         double e_powerdialing;                  /* on disconnect redial! 0=off, >0=redial time, -1=on */
102         double e_powerdelay;                    /* delay when to redial */
103         int e_powercount;                       /* power count */
104         int e_powerlimit;                       /* power limit */
105         double e_callback;                      /* time when callback (when idle reached) 0=off */
106         signed long e_cfnr_release;             /* time stamp when to do the release for call forward on no response */
107         signed long e_cfnr_call;                /* time stamp when to do the call for call forward on no response */
108         signed long e_password_timeout;         /* time stamp when to do the release for password timeout */
109
110         /* port relation */
111         int e_multipoint_cause;                 /* cause value of disconnected multiport calls (highest priority) */
112         int e_multipoint_location;              /* location of cause with highest priority */
113
114         /* join relation */
115         int e_join_cause;
116         int e_join_location;    
117
118         /* callback */
119         char e_cbdialing[256];                  /* dialing information after callback */
120         char e_cbcaller[256];                   /* extension for the epoint which calls back */
121         char e_cbto[32];                        /* override callerid to call back to */
122         struct caller_info e_callbackinfo;      /* information about the callback caller */
123
124         /* dtmf stuff */
125         int e_connectedmode;                    /* if the port should stay connected if the enpoint disconnects or releases (usefull for callback) */
126         int e_dtmf;                             /* allow dtmf */
127         /* read doc/keypad.txt for more information */
128         int e_dtmf_time;                        /* time when the last key was received. */
129         int e_dtmf_last;                        /* last dtmf key */
130
131         /* transmit and receive state */
132         int e_tx_state;                         /* current endpoint's state */
133         int e_rx_state;                         /* current endpoint's state */
134
135         /* vbox playback variables */
136         char e_vbox[32];                        /* current vbox extension (during playback) */
137         int e_vbox_state;                       /* state of vbox during playback */
138         int e_vbox_menu;                        /* currently selected menu using '*' and '#' */
139         char e_vbox_display[128];               /* current display message */
140         int e_vbox_display_refresh;             /* display must be refreshed du to change */
141         int e_vbox_counter;                     /* current playback counter in seconds */
142         int e_vbox_counter_max;                 /* size of file in seconds */
143         int e_vbox_counter_last;                /* temp variable to recognise a change in seconds */
144         int e_vbox_play;                        /* current file that is played */
145         int e_vbox_speed;                       /* current speed to play */
146         int e_vbox_index_num;                   /* number of files */
147         char e_vbox_index_file[128];            /* current file name */
148         int e_vbox_index_hour;                  /* current time the file recorded... */
149         int e_vbox_index_min;
150         int e_vbox_index_mon;
151         int e_vbox_index_mday;
152         int e_vbox_index_year;
153         char e_vbox_index_callerid[128];        /* current caller id */
154         int e_vbox_index_callerid_index;        /* next digit to speak */
155
156         /* efi */
157         int e_efi_state;                        /* current spoken sample */
158         int e_efi_digit;                        /* current spoken digit */
159         
160         /* crypt states and vars */
161         int e_crypt;                            /* current user level crypt state */
162         int e_crypt_state;                      /* current crypt manager state */
163         char e_crypt_info[33];                  /* last information text */
164         int e_crypt_timeout_sec;                /* timer */
165         int e_crypt_timeout_usec;               /* timer */
166         unsigned long e_crypt_random;           /* current random number for ident */
167         unsigned long e_crypt_bogomips;         /* bogomips for ident */
168         unsigned char e_crypt_key[256];         /* the session key */
169         int e_crypt_key_len;
170         unsigned char e_crypt_ckey[256];        /* the encrypted session key */
171         int e_crypt_ckey_len;
172         unsigned char e_crypt_rsa_n[512];       /* rsa key */
173         unsigned char e_crypt_rsa_e[16];
174         unsigned char e_crypt_rsa_d[512];
175         unsigned char e_crypt_rsa_p[512];
176         unsigned char e_crypt_rsa_q[512];
177         unsigned char e_crypt_rsa_dmp1[512];
178         unsigned char e_crypt_rsa_dmq1[512];
179         unsigned char e_crypt_rsa_iqmp[512];
180         int e_crypt_rsa_n_len;
181         int e_crypt_rsa_e_len;
182         int e_crypt_rsa_d_len;
183         int e_crypt_rsa_p_len;
184         int e_crypt_rsa_q_len;
185         int e_crypt_rsa_dmp1_len;
186         int e_crypt_rsa_dmq1_len;
187         int e_crypt_rsa_iqmp_len;
188         int e_crypt_keyengine_busy;             /* current job and busy state */
189         int e_crypt_keyengine_return;           /* return */
190
191         /* messages */
192         void hookflash(void);
193         void ea_message_port(unsigned long port_id, int message, union parameter *param);
194         void port_setup(struct port_list *portlist, int message_type, union parameter *param);
195         void port_information(struct port_list *portlist, int message_type, union parameter *param);
196         void port_dtmf(struct port_list *portlist, int message_type, union parameter *param);
197         void port_crypt(struct port_list *portlist, int message_type, union parameter *param);
198         void port_overlap(struct port_list *portlist, int message_type, union parameter *param);
199         void port_proceeding(struct port_list *portlist, int message_type, union parameter *param);
200         void port_alerting(struct port_list *portlist, int message_type, union parameter *param);
201         void port_connect(struct port_list *portlist, int message_type, union parameter *param);
202         void port_disconnect_release(struct port_list *portlist, int message_type, union parameter *param);
203         void port_timeout(struct port_list *portlist, int message_type, union parameter *param);
204         void port_notify(struct port_list *portlist, int message_type, union parameter *param);
205         void port_facility(struct port_list *portlist, int message_type, union parameter *param);
206         void port_suspend(struct port_list *portlist, int message_type, union parameter *param);
207         void port_resume(struct port_list *portlist, int message_type, union parameter *param);
208         void ea_message_join(unsigned long join_id, int message, union parameter *param);
209         void join_crypt(struct port_list *portlist, int message_type, union parameter *param);
210         void join_mISDNsignal(struct port_list *portlist, int message_type, union parameter *param);
211         void join_setup(struct port_list *portlist, int message_type, union parameter *param);
212         void join_information(struct port_list *portlist, int message_type, union parameter *param);
213         void join_overlap(struct port_list *portlist, int message_type, union parameter *param);
214         void join_proceeding(struct port_list *portlist, int message_type, union parameter *param);
215         void join_alerting(struct port_list *portlist, int message_type, union parameter *param);
216         void join_connect(struct port_list *portlist, int message_type, union parameter *param);
217         void join_disconnect_release(int message_type, union parameter *param);
218         void join_notify(struct port_list *portlist, int message_type, union parameter *param);
219         void join_facility(struct port_list *portlist, int message_type, union parameter *param);
220
221         /* epoint */
222         void new_state(int state);
223         void release(int release, int joinlocation, int joincause, int portlocation, int portcause);
224         void notify_active(void);
225         void keypad_function(char digit);
226         void set_tone(struct port_list *portlist, char *tone);
227         void out_setup(void);
228         struct mISDNport *hunt_port(char *ifname, int *channel);
229         char *apply_callerid_display(char *id, int itype, int ntype, int present, int screen, char *extension, char *name);
230         void auth(int job, int bit_num);
231
232         /* vbox playback stuff */
233         void vbox_init(void);
234         void vbox_index_read(int num);
235         void vbox_index_remove(int num);
236         void vbox_handler(void);
237         void efi_message_eof(void);
238         void vbox_message_eof(void);
239         void set_tone_vbox(char *tone);
240         void set_play_vbox(char *file, int offset);
241         void set_play_speed(int speed);
242
243         /* efi */
244         void set_tone_efi(char *tone);
245         
246         /* routing */
247         struct route_ruleset *rulesetbyname(char *name);
248         struct route_action *route(struct route_ruleset *ruleset);
249         struct route_param *routeparam(struct route_action *action, unsigned long long id);
250
251         /* init / dialing / hangup */
252         void _action_init_call(char *remote);
253         void action_init_call(void);
254         void action_init_remote(void);
255         void action_dialing_internal(void);
256         void action_dialing_external(void);
257         void action_dialing_h323(void);
258         void action_dialing_remote(void);
259         void action_dialing_vbox_record(void);
260         void action_init_partyline(void);
261         void action_hangup_call(void);
262         void action_dialing_login(void);
263         void action_init_change_callerid(void);
264         void _action_callerid_calleridnext(int next);
265         void action_dialing_callerid(void);
266         void action_dialing_calleridnext(void);
267         void action_init_change_forward(void);
268         void action_dialing_forward(void);
269         void action_init_redial_reply(void);
270         void _action_redial_reply(int in);
271         void action_dialing_redial(void);
272         void action_dialing_reply(void);
273         void action_dialing_powerdial(void);
274         void action_dialing_callback(void);
275         void action_hangup_callback(void);
276         void action_dialing_abbrev(void);
277         void action_dialing_test(void);
278         void action_init_play(void);
279         void action_init_vbox_play(void);
280         void action_init_efi(void);
281         void action_dialing_vbox_play(void);
282         void action_dialing_calculator(void);
283         void action_dialing_timer(void);
284         void _action_goto_menu(int mode);
285         void action_dialing_goto(void);
286         void action_dialing_menu(void);
287         void action_dialing_disconnect(void);
288         void action_dialing_help(void);
289         void action_dialing_deflect(void);
290         void action_dialing_setforward(void);
291         void action_hangup_execute(void);
292         void action_hangup_file(void);
293         void action_init_pick(void);
294         void action_dialing_password(void);
295         void action_dialing_password_wr(void);
296         void process_dialing(void);
297         void process_hangup(int cause, int location);
298
299         /* facility function */
300         void pick_join(char *extension);
301         void join_join(void);
302         void encrypt_shared(void);
303         void encrypt_keyex(void);
304         void encrypt_off(void);
305         void encrypt_result(int message, char *text);
306         int check_external(char **errstr, class Port **port);
307
308         /* crypt */
309         void cryptman_keyengine(int job);
310         void cryptman_handler(void);
311         void cr_ident(int message, unsigned char *param, int len);
312         void cr_activate(int message, unsigned char *param, int len);
313         void cr_deactivate(int message, unsigned char *param, int len);
314         void cr_master(int message, unsigned char *param, int len);
315         void cr_slave(int message, unsigned char *param, int len);
316         void cr_looped(int message, unsigned char *param, int len);
317         void cr_abort(int message, unsigned char *param, int len);
318         void cr_abort_engine(int message, unsigned char *param, int len);
319         void cr_abort_wait(int message, unsigned char *param, int len);
320         void cr_genrsa(int message, unsigned char *param, int len);
321         void cr_keyerror(int message, unsigned char *param, int len);
322         void cr_pubkey(int message, unsigned char *param, int len);
323         void cr_cptrsa(int message, unsigned char *param, int len);
324         void cr_cskey(int message, unsigned char *param, int len);
325         void cr_decrsa(int message, unsigned char *param, int len);
326         void cr_waitdelay(int message, unsigned char *param, int len);
327         void cr_bfactive(int message, unsigned char *param, int len);
328         void cr_crypterror(int message, unsigned char *param, int len);
329         void cr_release(int message, unsigned char *param, int len);
330         void cr_sactivate(int message, unsigned char *param, int len);
331         void cr_sdeactivate(int message, unsigned char *param, int len);
332         void cr_sbfactive(int message, unsigned char *param, int len);
333         void cr_scrypterror(int message, unsigned char *param, int len);
334         void cr_sabort(int message, unsigned char *param, int len);
335         void cr_info(int message, unsigned char *param, int len);
336         void cryptman_message(int message, unsigned char *param, int len);
337         void cryptman_msg2man(unsigned char *param, int len);
338         void cryptman_addinf(unsigned char *buf, int buf_size, int element, int len, void *data);
339         int cryptman_sizeofinf(unsigned char *buf, int element);
340         unsigned char *cryptman_getinf(unsigned char *buf, int element, unsigned char *to);
341         void cryptman_msg2peer(unsigned char *buf);
342         void cryptman_msg2user(int msg, char *text);
343         void cryptman_msg2crengine(int msg, unsigned char *buf, int len);
344         void cryptman_state(int state);
345         void cryptman_timeout(int secs);
346
347         void message_disconnect_port(struct port_list *portlist, int cause, int location, char *display);
348         void logmessage(int message_type, union parameter *param, unsigned long port_id, int dir);
349         void trace_header(char *name, int direction);
350 };
351
352
353 char *nationalize_callerinfo(char *string, int *type);
354 char *numberrize_callerinfo(char *string, int type);
355 void apply_callerid_restriction(struct extension *ext, char *id, int *ntype, int *present, int *screen, char *extension, char *name);
356 void send_mail(char *filename, char *callerid, char *callerintern, char *callername, char *vbox_email, int vbox_year, int vbox_mon, int vbox_mday, int vbox_hour, int vbox_min, char *terminal);
357
358