e76fce2aa1b78e4966f52e1f2f2a2add64329156
[dragonfly.git] / usr.sbin / i4b / isdnd / isdnd.h
1 /*
2  * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b daemon - main header file
28  *      -----------------------------
29  *
30  * $FreeBSD: src/usr.sbin/i4b/isdnd/isdnd.h,v 1.6.2.5 2002/04/24 18:50:07 joerg Exp $
31  * $DragonFly: src/usr.sbin/i4b/isdnd/isdnd.h,v 1.2 2003/06/17 04:29:54 dillon Exp $
32  *
33  *      last edit-date: [Mon Dec 31 12:42:58 2001]
34  *
35  *---------------------------------------------------------------------------*/
36
37 #ifndef _ISDND_H_
38 #define _ISDND_H_
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <unistd.h>
44 #include <strings.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <syslog.h>
48 #include <regex.h>
49 #include <time.h>
50 #include <errno.h>
51
52 #ifdef USE_CURSES
53 #include <curses.h>
54 #endif
55
56 #include <fcntl.h>
57 #include <errno.h>
58 #include <signal.h>
59
60 #include <sys/queue.h>  /* TAILQ_ macros */
61 #include <sys/param.h>
62 #include <sys/stat.h>
63 #include <sys/time.h>
64 #include <sys/types.h>
65 #include <sys/ioctl.h>
66
67 #ifdef USE_RTPRIO
68 #include <sys/rtprio.h>
69 #endif
70
71 #include <net/if_sppp.h>
72
73 #include <machine/i4b_ioctl.h>
74 #include <machine/i4b_cause.h>
75
76 #include "config.h"             /* compile time configuration   */
77 #include "pathnames.h"          /* location of files            */
78 #include "alias.h"              /* alias file processing        */
79
80 /*---------------------------------------------------------------------------*
81  *      some general definitions
82  *---------------------------------------------------------------------------*/
83 #define GOOD    0               /* general "good" or "ok" return*/
84 #define ERROR   (-1)            /* general error return         */
85 #define WARNING (-2)            /* warning return               */
86 #define INVALID (-1)            /* an invalid integer           */
87
88 /*---------------------------------------------------------------------------*
89  *      misc
90  *---------------------------------------------------------------------------*/
91 #define RTPRIO_NOTUSED (-1)     /* rtprio is not used for isdnd */
92
93 /*---------------------------------------------------------------------------*
94  *      debug flag bits
95  *---------------------------------------------------------------------------*/
96 #define DL_MSG          0x0001  /* general debug messages               */
97 #define DL_RATES        0x0002  /* messages related to rates            */
98 #define DL_TIME         0x0004  /* messages related to timing           */
99 #define DL_STATE        0x0008  /* messages related to states changes   */
100 #define DL_RCVRY        0x0010  /* messages related to dial recovery    */
101 #define DL_DIAL         0x0020  /* messages related to dial recovery    */
102 #define DL_PROC         0x0040  /* messages related to process handling */
103 #define DL_DRVR         0x0080  /* messages related to kernel i4b msg i/o*/
104 #define DL_CNST         0x0100  /* messages related to controller state */
105 #define DL_RCCF         0x0200  /* messages related to isdnd.rc at boot */
106 #define DL_BDGT         0x0400  /* messages related to budgets          */
107 #define DL_VALID        0x0800  /* messages related to valid keyword    */
108
109 #ifdef DEBUG
110 #define DBGL(cond, dolog) if(cond & debug_flags) dolog
111 #else
112 #define DBGL(cond, dolog)
113 #endif
114
115 /*---------------------------------------------------------------------------*
116  *      curses fullscreen display definitions
117  *---------------------------------------------------------------------------*/
118
119 /* window dimensions */
120 #define UPPER_B         2               /* upper window start  */
121
122 /* horizontal positions for upper window */
123 #define H_CNTL          0               /* controller           */
124 #define H_TEI           2               /* TEI                  */
125 #define H_CHAN          (H_TEI+4)       /* channel              */
126 #define H_TELN          (H_CHAN+2)      /* telephone number     */
127 #define H_IFN           (H_TELN+23)     /* interfacename        */
128 #define H_IO            (H_IFN+7)       /* incoming or outgoing */
129 #define H_OUT           (H_IO+4)        /* # of bytes out       */
130 #define H_OUTBPS        (H_OUT+11)      /* bytes per second out */
131 #define H_IN            (H_OUTBPS+5)    /* # of bytes in        */
132 #define H_INBPS         (H_IN+11)       /* bytes per second in  */
133 #define H_UNITS         (H_INBPS+6)     /* # of charging units  */
134
135 /* fullscreen mode menu window */
136 #define WMENU_LEN       35              /* width of menu window */
137 #define WMENU_TITLE     "Command"       /* title string */
138 #define WMENU_POSLN     10              /* menu position, line */
139 #define WMENU_POSCO     5               /* menu position, col */
140 #define WMITEMS         6               /* no of menu items */
141 #define WMENU_HGT       (WMITEMS + 4)   /* menu window height */
142
143 #define WREFRESH        0
144 #define WHANGUP         1
145 #define WREREAD         2
146 #define WSHOW           3
147 #define WBUDGET         4
148 #define WQUIT           5
149
150 #define WMTIMEOUT       5               /* timeout in seconds */
151
152 /*---------------------------------------------------------------------------*
153  *      charging rates
154  *---------------------------------------------------------------------------*/
155 #define NDAYS   7       /* number of days in a week             */
156 #define NRATES  4       /* number of rate structures supported  */
157
158 /* struct for rates - each day has one or more */
159 struct rates
160 {
161         int start_time; /* hour and min at which this rate starts, e.g. 12:20 12*60+20*/
162         int end_time;   /* hour and min at which this rate ends, e.g. 19:10 19*60+10*/
163         int rate;       /* how long can I telephone at this price, seconds */
164         struct rates *next;
165 };
166
167 /*---------------------------------------------------------------------------*
168  * the internal identifiers for isdnd log levels. CAUTION: this has to stay
169  * in sync with the loglevel to text and sysloglevel table in log.c !!
170  *---------------------------------------------------------------------------*/
171 enum logids
172 {
173         LL_ERR, /* error conditions - everything which caused an error  */
174         LL_WRN, /* warning conditions - nonfatal abnormal conditions    */
175         LL_DMN, /* normal but significant condition - status of daemon  */
176         LL_CHD, /* informational - everything regarding call handling   */
177         LL_DBG, /* debug messages - everything which helps debugging    */
178         LL_MER, /* monitor error messages - not sent to remote          */
179         LL_PKT  /* packet logging - log the first few packets           */
180 };
181
182 /*---------------------------------------------------------------------------*
183  *      state machine events
184  *---------------------------------------------------------------------------*/
185 enum events
186 {
187         /* incoming messages */
188         
189         EV_MCI,         /* MSG_CONNECT_IND */
190         EV_MCAI,        /* MSG_CONNECT_ACTIVE_IND */
191         EV_MDI,         /* MSG_DISCONNECT_IND */
192         EV_MDO,         /* MSG_DIALOUT */
193
194         /* local requests */
195
196         EV_TIMO,        /* timer expired */
197         EV_DRQ,         /* disconnect request */
198         EV_CBRQ,        /* callback request */
199         EV_ALRT,        /* alerting request */
200         
201         /* illegal */
202         
203         EV_ILL          /* illegal event */
204 };
205
206 #define N_EVENTS (EV_ILL+1)     /* no of possible events */
207
208 /*---------------------------------------------------------------------------*
209  *      this struct describes the numbers to try to dial out
210  *---------------------------------------------------------------------------*/
211 typedef struct {
212         char number[TELNO_MAX]; /* remote number to dial        */
213         int  flag;              /* usage flag                   */
214 #define RNF_IDLE        0
215 #define RNF_SUCC        1       /* last dial was ok */
216 } remote_number_t;
217
218 /*---------------------------------------------------------------------------*
219  *      this struct describes numbers allowed to dial in
220  *---------------------------------------------------------------------------*/
221 typedef struct {
222         char number[TELNO_MAX]; /* calling party number         */
223 } incoming_number_t;
224
225 /*---------------------------------------------------------------------------*
226  *      this structure describes a prematurely aborted called-back dialout
227  *---------------------------------------------------------------------------*/
228 typedef struct {
229         int cdid;               /* call handle */
230         int controller;         /* the controller used to dial out */
231         int channel;            /* the channel assigned to the outgoing call */
232         /* XXX - timeout handling and error recovery? */
233 } phantom_t;
234
235 /*---------------------------------------------------------------------------*
236  *      this struct describes one complete configuration entry
237  *---------------------------------------------------------------------------*/
238 typedef struct cfg_entry {
239
240         /* ====== filled in at startup configuration, then static ===========*/
241
242         char name[32];                  /* id for this entry */
243
244         int isdncontroller;             /* controller to use 0 ... n */
245         int isdnchannel;                /* channel to use */
246
247         int isdntxdelin;                /* tx delay, incoming connections */
248         int isdntxdelout;               /* tx delay, outgoing connections */
249         
250         int usrdevicename;              /* userland device to use */
251         int usrdeviceunit;              /* userland unit to use */
252
253         int remote_numbers_count;       /* number of remote numbers     */
254 #define MAXRNUMBERS 8                   /* max remote numbers           */
255
256         remote_number_t remote_numbers[MAXRNUMBERS];    /* remote numbers to dial */
257
258         int remote_numbers_handling;    /* how to handle the remote dialing */
259 #define RNH_NEXT        0       /* use next number after last successfull   */
260 #define RNH_LAST        1       /* use last successfull for next call       */
261 #define RNH_FIRST       2       /* always use first number for next call    */
262
263         char local_phone_dialout[TELNO_MAX];    /* our number to tell remote*/
264         char local_phone_incoming[TELNO_MAX];   /* answer calls for this local number */
265
266 #define MAX_INCOMING    8
267         int incoming_numbers_count;     /* number of incoming allowed numbers */
268         incoming_number_t remote_phone_incoming[MAX_INCOMING];  /* answer calls from this remote machine */
269
270         int dialin_reaction;            /* what to do with incoming calls */
271 #define REACT_ACCEPT     0
272 #define REACT_REJECT     1
273 #define REACT_IGNORE     2
274 #define REACT_ANSWER     3
275 #define REACT_CALLBACK   4
276
277         int b1protocol;                 /* hdlc / raw */
278
279         int idle_time_in;               /* max idle time incoming calls */
280         int idle_time_out;              /* max idle time outgoing calls */
281
282         int shorthold_algorithm;        /* shorthold algorithm          */
283
284         int unitlength;                 /* length of a charging unit    */
285 #define UNITLENGTH_DEFAULT      60      /* last resort unit length      */
286
287         int earlyhangup;                /* time in seconds to hangup    */
288                                         /* before the next expected     */
289                                         /* charging unit                */
290 #define EARLYHANGUP_DEFAULT     5
291                                         
292         int ratetype;                   /* type of rate */
293 #define NO_RATE         (NRATES+1)
294 #define INVALID_RATE    (-1)
295
296         int  unitlengthsrc;     /* where we get the unit length from    */
297 #define ULSRC_NONE      0               /* nowhere specified            */
298 #define ULSRC_CMDL      1               /* specified on commandline     */
299 #define   ULSRC_CMDLMIN 5               /* minimum value from cmdl      */
300 #define   ULSRC_CMDLMAX 3600            /* minimum value from cmdl      */
301 #define ULSRC_CONF      2               /* get it from config file      */
302 #define ULSRC_RATE      3               /* get it dynamic from ratesfile*/
303 #define ULSRC_DYN       4               /* dynamic calculated from AOCD */
304
305         char *answerprog;               /* program to use for answering */
306         char *connectprog;      /* program run after negotiation finished */
307         char *disconnectprog;   /* program run after shutdown is complete */
308
309         int  callbackwait;              /* time to wait before calling back */
310 #define CALLBACKWAIT_MIN        1
311
312         int  calledbackwait;            /* time to wait for remote callback */  
313 #define CALLEDBACKWAIT_MIN      2
314
315         int  dialretries;               /* no. of dial tries            */
316 #define DIALRETRIES_DEF         1
317         
318         int  recoverytime;              /* time between 2 dial tries    */
319 #define RECOVERYTIME_MIN        1
320         
321         int  dialrandincr;              /* use random dial time incr    */
322         
323         int  usedown;                   /* set interface down yes/no    */
324         int  downtries;                 /* retries before i/f is set down */
325 #define DOWN_TRIES_MIN 2
326 #define DOWN_TRIES_MAX 20
327         int  downtime;                  /* time i/f is down             */
328 #define DOWN_TIME_MIN 10        /* 10 seconds */
329 #define DOWN_TIME_MAX 3600      /* 1 hour */
330
331         int  dialouttype;               /* type of outgoing connection  */
332 #define DIALOUT_NORMAL     0            /* normal dialout behaviour     */
333 #define DIALOUT_CALLEDBACK 1            /* remote is expected to callback */
334
335         int     alert;                  /* alert time in sec if nonzero */
336 #define MINALERT 5                      /* 5 secs min                   */
337 #define MAXALERT (3*60)                 /* 3 minutes max                */
338
339         int     inout;                  /* in/out, in-only or out-only  */
340 #define DIR_INOUT       0
341 #define DIR_INONLY      1
342 #define DIR_OUTONLY     2
343
344         int     budget_callbackperiod;  /* length of a budget period (s)*/
345         int     budget_callbackncalls;  /* call budget for a period     */
346         char    *budget_callbacks_file; /* filename to store callback stats */
347         char    budget_callbacksfile_rotate;
348
349         int     budget_calloutperiod;   /* length of a budget period (s)*/
350         int     budget_calloutncalls;   /* call budget for a period     */
351         char    *budget_callouts_file;  /* filename to store callout stats */
352         char    budget_calloutsfile_rotate;
353         
354         int     ppp_expect_auth;
355         int     ppp_send_auth;
356 #define AUTH_UNDEF      0
357 #define AUTH_NONE       1
358 #define AUTH_PAP        2
359 #define AUTH_CHAP       3
360
361         int     ppp_auth_flags;
362 #define AUTH_RECHALLENGE 0x01
363 #define AUTH_REQUIRED    0x02
364
365         char    ppp_expect_name[AUTHNAMELEN];   /* PPP PAP/CHAP login name */
366         char    ppp_send_name[AUTHNAMELEN];
367
368         char    ppp_expect_password[AUTHKEYLEN];
369         char    ppp_send_password[AUTHKEYLEN];
370
371         int     day;                            /* days valid */
372 #define         SU      0x01
373 #define         MO      0x02
374 #define         TU      0x04
375 #define         WE      0x08
376 #define         TH      0x10
377 #define         FR      0x20
378 #define         SA      0x40
379 #define         HD      0x80    /* holiday */
380         int     fromhr;                         /* time valid */
381         int     frommin;
382         int     tohr;
383         int     tomin;
384
385         time_t  maxconnecttime;                 /* maximum connection time */
386         
387 /*===========================================================================*/ 
388 /*============ filled in after start, then dynamic ==========================*/
389 /*===========================================================================*/ 
390
391         int cdid;                       /* cdid for call                */
392 #define CDID_RESERVED   (-1)
393
394         int isdncontrollerused;         /* the one we are using         */
395         int isdnchannelused;            /* the one we are using         */
396         
397         int fs_position;                /* fullscreen position          */
398
399         int     state;                  /* state of connection          */
400 #define ST_IDLE         0               /* connection is idle / disconnected  */
401
402                 /* normal dial out to remote */
403 #define ST_DIAL         1               /* dialing                            */
404 #define ST_DIALRTMRCHD  2               /* wait for dial retry time reached   */
405 #define ST_DIALRETRY    3               /* last/first dialing failed, retry   */
406
407                 /* PCB: passive callback, i'm being called back */
408 #define ST_PCB_DIAL     4               /* dialing, trigger a callback        */
409 #define ST_PCB_DIALFAIL 5               /* dialing failed triggering a callbk */
410 #define ST_PCB_WAITCALL 6               /* waiting for callback from remote   */
411
412                 /* ACB: active callback, i'm calling back */
413 #define ST_ACB_WAITDISC 7               /* got call, wait for disconnect      */
414 #define ST_ACB_WAITDIAL 8               /* wait until allowed to callback     */
415 #define ST_ACB_DIAL     9               /* callback to remote                 */
416 #define ST_ACB_DIALFAIL 10              /* callback to remote failed          */
417
418                 /* normal non-dialling states */
419 #define ST_ACCEPTED     11              /* remote accepted                    */
420 #define ST_CONNECTED    12              /* connected with remote              */
421 #define ST_WAITDISCI    13              /* tx disc req, wait for disc ind     */
422 #define ST_DOWN         14              /* interface is down                  */
423 #define ST_ALERT        15              /* interface is waiting for alert time*/
424
425                 /* illegal and pseudo states */
426 #define ST_ILL          16              /* illegal state                      */
427 #define ST_SUSE         17              /* subroutine sets new state          */
428
429 #define N_STATES        (ST_ILL+1)      /* max number of states               */
430
431         cause_t disc_cause;             /* cause from disconnect */
432
433         int local_disconnect;           /* flag, who disconnected */
434 #define DISCON_LOC      0
435 #define DISCON_REM      1
436
437         int timerval;                   /* value for timer, 0 if inactive */
438         int timerremain;                /* remaining time       */
439
440         int hangup;                     /* flag, hangup connection asap */
441
442         char real_phone_incoming[TELNO_MAX]; /* real remote telno in case of wildcard */
443
444         int last_remote_number;         /* index of last used dialout number*/
445
446         char remote_phone_dialout[TELNO_MAX]; /* used remote number to dial */
447         
448         int direction;                  /* incoming or outgoing */
449 #define DIR_IN  0
450 #define DIR_OUT 1
451         
452         int charge;                     /* charge in units */
453         int last_charge;                /* last charge in units */
454         
455         int inbytes;                    /* # of bytes from remote */
456         int iinbytes;                   /* # of bytes from remote on the line */
457         int inbps;                      /* bytes/sec from remote */
458         int outbytes;                   /* # of bytes to remote */
459         int ioutbytes;                  /* # of bytes to remote on the line */  
460         int outbps;                     /* bytes/sec to remote */
461         
462         time_t  connect_time;           /* time connection was established */
463
464         time_t aoc_last;                /* last AOCD timestamp          */
465         time_t aoc_now;                 /* current AOCD timestamp       */
466         time_t aoc_diff;                /* current unit length          */
467         time_t aoc_lastdiff;            /* last charge unit length      */
468         int aoc_valid;                  /* flag: time diff is valid     */
469 #define AOC_INVALID     0               /* aoc_diff is NOT valid        */
470 #define AOC_VALID       1               /* aoc_diff is valid            */
471
472         time_t  last_dial_time;         /* time of last dialing         */
473         time_t  last_release_time;      /* time of last hangup          */
474         
475         int     dial_count;             /* number of dialout tries      */
476         int     randomtime;             /* random() part of recoverytime*/
477 #define RANDOM_MASK 0x04                /* bits used from randomtime    */
478
479         int     down_retry_count;       /* retry cycle count for usedown*/
480         time_t  went_down_time;         /* time i/f went down           */
481         phantom_t saved_call;           /* outgoing call state if called
482                                            back too early */
483
484         int     alert_time;             /* count down of alert time     */      
485         char display[DISPLAY_MAX];
486
487         time_t  budget_callbackperiod_time; /* end of current period    */
488         int     budget_callbackncalls_cnt;  /* amount of calls left     */
489
490         int     budget_callback_req;    /* requests                     */
491         int     budget_callback_done;   /* call done                    */
492         int     budget_callback_rej;    /* call refused                 */      
493
494         time_t  budget_calloutperiod_time; /* end of current period     */
495         int     budget_calloutncalls_cnt;  /* amount of calls left      */
496
497         int     budget_callout_req;     /* requests                     */
498         int     budget_callout_done;    /* call done                    */
499         int     budget_callout_rej;     /* call refused                 */      
500         
501         int     budget_calltype;        /* type of call                 */
502 #define BUDGET_TYPE_CBACK 1
503 #define BUDGET_TYPE_COUT  2
504
505         char    keypad[KEYPAD_MAX];     /* keypad string                */
506 } cfg_entry_t;
507
508 /*---------------------------------------------------------------------------*
509  *      this struct describes state of controller with MAX_BCHAN b channels
510  *---------------------------------------------------------------------------*/
511 typedef struct isdn_ctrl_state {
512         int ctrl_type;                  /* type: active/passive         */
513         int card_type;                  /* manufacturer (CARD_XXXX)     */
514         int protocol;                   /* ISDN D-channel protocol      */      
515         char* firmware;                 /* loadable fimrware file name  */
516     
517         int state;                      /* controller state             */
518 #define  CTRL_DOWN      0               /* controller inoparable        */
519 #define  CTRL_UP        1               /* controller may be used       */
520 #define  MAX_BCHAN      30
521         int stateb[MAX_BCHAN];          /* b channel state */
522 #define  CHAN_IDLE      0               /* channel is free for usage    */
523 #define  CHAN_RUN       1               /* channel is occupied          */
524         int nbch;                       /* number of b channels */
525         int freechans;                  /* number of unused channels    */
526         int tei;                        /* tei or -1 if invalid         */
527         int l1stat;                     /* layer 1 state                */
528         int l2stat;                     /* layer 2 state                */
529 } isdn_ctrl_state_t;
530
531 /*---------------------------------------------------------------------------*
532  *      this struct describes a logging regular expression
533  *---------------------------------------------------------------------------*/
534 struct rarr {
535         int re_flg;             /* valid entry flag */
536         char *re_expr;          /* plain text expression */
537         regex_t re;             /* compiled expression */
538         char *re_prog;          /* the program to be executed */
539 };
540
541 #ifdef I4B_EXTERNAL_MONITOR
542 /* for each rights entry we keep one of this structures around: */
543 struct monitor_rights {
544         TAILQ_ENTRY(monitor_rights) list;       /* a list of this structures */
545         char name[FILENAME_MAX];        /* net/host spec or filename */
546         int rights;                     /* bitmask of allowed acces rights */
547         u_int32_t net;                  /* net/host address (host byte order!) */
548         u_int32_t mask;                 /* bitmask 1 = network, 0 = host (host byte order!) */
549         int local;                      /* zero if remote access via tcp/ip */
550 };
551 #endif
552
553 /*---------------------------------------------------------------------------*
554  *      global variables, storage allocation
555  *---------------------------------------------------------------------------*/
556 #ifdef MAIN
557
558 int isdnfd;                                     /* file handle, /dev/i4b */
559
560 char mailto[MAXPATHLEN] = "";                   /* panic mail address */
561 char mailer[MAXPATHLEN] = "";                   /* panic mail address */
562
563 char *configfile = CONFIG_FILE_DEF;             /* configuration filename */
564 int config_error_flag = 0;                      /* error counter */
565
566 #ifdef DEBUG
567 int do_debug = 0;                               /* debug mode flag      */
568 int debug_flags = 0;                            /* debug options        */
569 int debug_noscreen = 0;                         /* not on fullscreen    */
570 #endif
571
572 int do_bell = 0;                                /* bell on connect/disconnect */
573
574 int do_fork = 1;                                /* run as daemon/foreground */
575
576 int do_ttytype = 0;                             /* got new terminal type */
577 char *ttype = "";                               /* termcap entry name string */
578
579 int do_rdev = 0;                                /* redirect output      */
580 char *rdev = "";                                /* new device string */
581
582 int do_print = 0;                               /* config file printout */
583
584 int got_unitlen = 0;                            /* flag, got length of a unit */
585 time_t unit_length;                             /* length of a unit */
586
587 cfg_entry_t cfg_entry_tab[CFG_ENTRY_MAX];       /* configuration table */
588 isdn_ctrl_state_t isdn_ctrl_tab[ISDN_CTRL_MAX]; /* controller states table */
589
590 int ncontroller = 0;                            /* # of controllers available */
591 int nentries = 0;                               /* # of entries in config tab */
592
593 int uselogfile = 0;                             /* flag, use a logfile */
594 char logfile[MAXPATHLEN] = LOG_FILE_DEF;        /* log filename */
595 FILE *logfp = NULL;                             /* log file pointer */
596 int logfacility = LOG_LOCAL0;                   /* the syslog facility used */
597 int nregex = 0;                                 /* number of reg expr */
598 struct rarr rarr[MAX_RE];                       /* regexpr & progs table */
599
600 char ratesfile[MAXPATHLEN] = RATES_FILE_DEF;    /* rates filename */
601 char *rate_error = NULL;                        /* errorcase: error string */
602 int got_rate = 0;                               /* flag, ratesfile found */
603 struct rates *rates[NRATES][NDAYS];             /* the rates structure */
604
605 int useacctfile = 0;                            /* flag, write accounting */
606 char acctfile[MAXPATHLEN] = ACCT_FILE_DEF;      /* accounting  filename */
607 FILE *acctfp = NULL;                            /* accounting file pointer */
608 int acct_all = 1;                               /* account all connections */
609
610 int aliasing = 0;                               /* enable alias processing */
611 char aliasfile[MAXPATHLEN] = ALIASFILE;         /* alias file location */
612
613 int do_fullscreen = 0;                          /* fullscreen log       */
614 int curses_ready = 0;                           /* curses initialized */
615
616 #ifdef USE_CURSES
617 WINDOW *upper_w;                /* curses upper window pointer */
618 WINDOW *mid_w;                  /* curses mid window pointer */
619 WINDOW *lower_w;                /* curses lower window pointer */
620 #endif
621
622 int rt_prio = RTPRIO_NOTUSED;                   /* realtime priority */
623
624 /* monitor via network */
625
626 int do_monitor = 0;
627 int inhibit_monitor = 0;
628 #ifdef I4B_EXTERNAL_MONITOR
629 int monitorport = DEF_MONPORT;
630 #else
631 int monitorport = -1;
632 #endif
633 int accepted = 0;
634
635 int isdntime = 0;               /* flag, log time from exchange */
636 int extcallattr = 0;            /* flag, display extended caller attributes */
637
638 char tinainitprog[MAXPATHLEN] = TINA_FILE_DEF;
639
640 char rotatesuffix[MAXPATHLEN] = "";
641
642 time_t starttime = 0;
643
644 char holidayfile[MAXPATHLEN] = HOLIDAY_FILE_DEF; /* holiday filename */
645
646 #else /* !MAIN */
647
648 int isdnfd;
649
650 char mailto[MAXPATHLEN];
651 char mailer[MAXPATHLEN];
652
653 char *configfile;
654 int config_error_flag;
655
656 #ifdef DEBUG
657 int do_debug;
658 int debug_flags;
659 int debug_noscreen;
660 #endif
661
662 int do_bell;
663
664 int do_fork;
665
666 int do_ttytype;
667 char *ttype;
668
669 int do_rdev;
670 char *rdev;
671
672 int do_print;
673
674 int got_unitlen;
675 time_t unit_length;
676
677 cfg_entry_t cfg_entry_tab[CFG_ENTRY_MAX];       /* configuration table */
678 isdn_ctrl_state_t isdn_ctrl_tab[ISDN_CTRL_MAX]; /* controller states table */
679
680 int ncontroller;
681 int nentries;
682
683 int uselogfile;
684 char logfile[MAXPATHLEN];
685 FILE *logfp;
686 int logfacility;
687 int nregex;
688 struct rarr rarr[MAX_RE];
689
690 char ratesfile[MAXPATHLEN];
691 char *rate_error;
692 int got_rate;
693 struct rates *rates[NRATES][NDAYS];
694
695 int useacctfile;
696 char acctfile[MAXPATHLEN];
697 FILE *acctfp;
698 int acct_all;
699
700 int aliasing;
701 char aliasfile[MAXPATHLEN];
702
703 int do_fullscreen;
704 int curses_ready;
705
706 #ifdef USE_CURSES
707 WINDOW *upper_w;
708 WINDOW *mid_w;
709 WINDOW *lower_w;
710 #endif
711
712 int rt_prio;
713
714 int do_monitor;
715 int inhibit_monitor;
716 int monitorport;
717 int accepted;
718
719 int isdntime;
720 int extcallattr;
721
722 char tinainitprog[MAXPATHLEN];
723
724 char rotatesuffix[MAXPATHLEN];
725
726 time_t starttime;
727
728 char holidayfile[MAXPATHLEN];
729
730 #endif /* MAIN */
731
732 char * bdrivername ( int drivertype );
733 void cfg_setval ( int keyword );
734 void check_and_kill ( cfg_entry_t *cep );
735 void check_pid ( void );
736 void close_allactive ( void );
737 void configure ( char *filename, int reread );
738 void daemonize ( void );
739 void dialresponse(cfg_entry_t *cep, int dstat);
740 void display_acct ( cfg_entry_t *cep );
741 void display_bell ( void );
742 void display_ccharge ( cfg_entry_t *cep, int units );
743 void display_chans ( void );
744 void display_charge ( cfg_entry_t *cep );
745 void display_connect ( cfg_entry_t *cep );
746 void display_disconnect ( cfg_entry_t *cep );
747 void display_l12stat(int controller, int layer, int state);
748 void display_tei(int controller, int tei);
749 void display_updown ( cfg_entry_t *cep, int updown );
750 void do_exit ( int exitval );
751 void do_menu ( void );
752 int exec_answer ( cfg_entry_t *cep );
753 int exec_connect_prog ( cfg_entry_t *cep, const char *prog, int link_down );
754 pid_t exec_prog ( char *prog, char **arglist );
755 cfg_entry_t * find_by_device_for_dialout ( int drivertype, int driverunit );
756 cfg_entry_t *find_by_device_for_dialoutnumber(int drivertype, int driverunit, int cmdlen, char *cmd);
757 cfg_entry_t *find_by_device_for_keypad(int drivertype, int driverunit, int cmdlen, char *cmd);
758 cfg_entry_t * find_matching_entry_incoming ( msg_connect_ind_t *mp );
759 cfg_entry_t * find_active_entry_by_driver ( int drivertype, int driverunit );
760 void finish_log ( void );
761 char * getlogdatetime ( void );
762 int get_cdid ( void );
763 cfg_entry_t * get_cep_by_cc ( int ctrlr, int chan );
764 cfg_entry_t * get_cep_by_driver ( int drivertype, int driverunit );
765 cfg_entry_t * get_cep_by_cdid ( int cdid );
766 int get_current_rate ( cfg_entry_t *cep, int logit );
767 void handle_charge ( cfg_entry_t *cep );
768 void handle_recovery ( void );
769 void handle_scrprs(int cdid, int scr, int prs, char *caller);
770 void if_up(cfg_entry_t *cep);
771 void if_down(cfg_entry_t *cep);
772 void init_controller ( void );
773 void init_controller_protocol ( void );
774 void init_log ( void );
775 void init_screen ( void );
776 void log ( int what, const char *fmt, ... );
777 int main ( int argc, char **argv );
778 void msg_accounting ( msg_accounting_ind_t *mp );
779 void msg_alert_ind ( msg_alert_ind_t *mp );
780 void msg_charging_ind ( msg_charging_ind_t *mp );
781 void msg_connect_active_ind ( msg_connect_active_ind_t *mp );
782 void msg_connect_ind ( msg_connect_ind_t *mp );
783 void msg_pdeact_ind(msg_pdeact_ind_t *md);
784 void msg_negcomplete_ind(msg_negcomplete_ind_t *ind);
785 void msg_ifstatechg_ind(msg_ifstatechg_ind_t *ind);
786 void msg_drvrdisc_req(msg_drvrdisc_req_t *mp);
787 void msg_dialout ( msg_dialout_ind_t *mp );
788 void msg_dialoutnumber(msg_dialoutnumber_ind_t *mp);
789 void msg_disconnect_ind ( msg_disconnect_ind_t *mp );
790 void msg_idle_timeout_ind ( msg_idle_timeout_ind_t *mp );
791 void msg_l12stat_ind(msg_l12stat_ind_t *ml);
792 void msg_teiasg_ind(msg_teiasg_ind_t *mt);
793 void msg_proceeding_ind ( msg_proceeding_ind_t *mp );
794 void msg_packet_ind( msg_packet_ind_t *mp );
795 const char * name_of_controller(int ctrl_type, int card_type);
796 void next_state ( cfg_entry_t *cep, int event );
797 char * print_i4b_cause( cause_t code );
798 char * printstate ( cfg_entry_t *cep );
799 int readrates ( char *filename );
800 int ret_channel_state(int controller, int channel);
801 void reopenfiles ( int dummy );
802 void rereadconfig ( int dummy );
803 void select_first_dialno ( cfg_entry_t *cep );
804 void select_next_dialno ( cfg_entry_t *cep );
805 void select_this_dialno ( cfg_entry_t *cep );
806 int sendm_alert_req ( cfg_entry_t *cep );
807 int sendm_connect_req ( cfg_entry_t *cep );
808 int sendm_connect_resp ( cfg_entry_t *cep, int cdid, int response, cause_t cause );
809 int sendm_disconnect_req ( cfg_entry_t *cep, cause_t cause );
810 int set_channel_busy(int controller, int channel);
811 int set_channel_idle(int controller, int channel);
812 int setup_dialout(cfg_entry_t *cep);
813 void sigchild_handler ( int sig );
814 void start_timer ( cfg_entry_t *cep, int seconds );
815 void stop_timer ( cfg_entry_t *cep );
816 void unitlen_chkupd( cfg_entry_t *cep );
817 void write_pid ( void );
818 void yyerror ( const char *msg );
819
820 void error_exit(int exitval, const char *fmt, ...);
821
822 /* montior server module */
823 void monitor_init();
824 void monitor_exit();
825 void monitor_clear_rights();
826 void monitor_fixup_rights();
827 int monitor_start_rights(const char *clientspec);
828 void monitor_add_rights(int rights);
829
830 /* possible return codes from monitor_start_rights: */
831 #define I4BMAR_OK       0       /* rights added successfully */
832 #define I4BMAR_LENGTH   1       /* local socket name to long */
833 #define I4BMAR_DUP      2       /* entry already exists */
834 #define I4BMAR_CIDR     3       /* cidr netmask is invalid */
835 #define I4BMAR_NOIP     4       /* host/net could not be resolved */
836
837 int monitor_create_local_socket();
838
839 #ifndef I4B_NOTCPIP_MONITOR
840 int monitor_create_remote_socket(int portno);
841 #endif
842
843 void monitor_prepselect(fd_set *selset, int *max_fd);
844 void monitor_handle_input(fd_set *selset);
845 void monitor_handle_connect(int sockfd, int is_local);
846 void monitor_evnt_charge(cfg_entry_t *cep, int units, int estimated);
847 void monitor_evnt_connect(cfg_entry_t *cep);
848 void monitor_evnt_disconnect(cfg_entry_t *cep);
849 void monitor_evnt_updown(cfg_entry_t *cep, int up);
850 void monitor_evnt_log(int prio, const char * what, const char * msg);
851
852 void monitor_evnt_l12stat(int controller, int layer, int state);
853 void monitor_evnt_tei(int controller, int tei);
854 void monitor_evnt_acct(cfg_entry_t *cep);
855
856 /* controller.c */
857
858 void init_active_controller(void);
859 int set_controller_state(int controller, int state);
860 int get_controller_state(int controller);
861 int decr_free_channels(int controller);
862 int incr_free_channels(int controller);
863 int get_free_channels(int controller);
864 int set_channel_busy(int controller, int channel);
865 int set_channel_idle(int controller, int channel);
866 int ret_channel_state(int controller, int channel);
867
868 /* alias.c */
869
870 void init_alias(char *filename);
871 void free_aliases(void);
872 char *get_alias(char *number);
873
874 void upd_callstat_file(char *filename, int rotateflag);
875
876 /* holiday.c */
877
878 void init_holidays(char *filename);
879 void free_holidays(void);
880 int isholiday(int d, int m, int y);
881
882 #endif /* _ISDND_H_ */