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