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