/*
**  Copyright (c) 1991 Bolt Beranek and Newman, Inc.
**  All rights reserved.
**
**  Redistribution and use in source and binary forms are permitted
**  provided that: (1) source distributions retain this entire copyright
**  notice and comment, and (2) distributions including binaries display
**  the following acknowledgement:  ``This product includes software
**  developed by Bolt Beranek and Newman, Inc. and CREN/CSNET'' in the
**  documentation or other materials provided with the distribution and in
**  all advertising materials mentioning features or use of this software.
**  Neither the name of Bolt Beranek and Newman nor CREN/CSNET may be used
**  to endorse or promote products derived from this software without
**  specific prior written permission.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
**  WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
 * Copyright (c) 1992 Purdue University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by Purdue University.  The name of the University may not be used
 * to endorse or promote products derived * from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Note: this copyright applies to portions of this software developed
 * at Purdue beyond the software covered by the original copyright.
 */
#define MAXHOSTS	25		/* Number of known hosts	*/
#define MAXDEVLEN 	8		/* Length of a device name	*/
#define MAXDEVICES	8		/* Number of devices/config	*/
#define MAXFIELDSIZE	30		/* Length of script parameters	*/
#define OPEN_TIMEOUT	10		/* How long to wait for modem	*/


#ifndef	MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif	/* MAXHOSTNAMELEN */


/*
**  Get the word and bit within the REMOTE.Protocols bitarray (see below).
**  Assumes 32-bit longs.
*/
#define P_WORD(i)		((i) >> 5)
#define P_BIT(i)		(1L << (i & 31))
#define P_SET			1  /* to signal to set the protocol bit */
#define P_RESET			0

/*
**  Return codes from makecall.
*/
#define CALL_SUCCESS	 0	/* Call was made successfully		*/
#define CALL_COMPLETED	 1	/* Call was completed normally		*/
#define CALL_NOMODEM	 2	/* No modem available			*/
#define CALL_PERMISSION  3	/* File or device access/protection problem*/
#define CALL_FAIL	 4	/* Call failed (busy for example)	*/
#define CALL_NOCONNECT   5	/* No connection at far end		*/
#define CALL_NOSETUID	 6	/* Program does not have setuid root	*/
#define CALL_IOCTL_ERR	 7	/* Problem setting IOCTL parameters	*/
#define CALL_BAD_IF	 8	/* Interface name is mis-formed		*/
#define CALL_TTYTIMEOUT	 9	/* Open on Modem line timed out		*/
#define CALL_NOSOCKET	10	/* Unable to get a socket		*/


/*
**  Configuration data for a remote host.
*/
typedef struct _REMOTE {
    /*
     * These fields are basically set from the IF entries.
     */
    char		Device[MAXDEVLEN];	/* dp device to use	*/
    int			Unit;			/* DP Unit number	*/
    int			DlpiFd;			/* Dev File Descriptor  */
    struct in_addr	Address;		/* Interface address	*/
    struct in_addr	DstAddress;		/* Destination address	*/
    char		*Sitename;		/* Host name		*/
    char		*Login;			/* Login id (incoming)	*/
    char		**Phone;		/* Phone number		*/
    char		*Script;		/* Login script		*/
    char		**ScriptArgs;		/* Login script	args	*/
    char		*Transcript;		/* Dialing transcript	*/
    int			Transtyle;		/* Transcript style	*/
    char		**PppArgs;		/* Arguments to PPP	*/
    struct _MODEM	**Modems;		/* Modem rotaries	*/
    int			NModems;		/* # of modems/rotaries */
    char		*PreAuxProgram;		/* Auxilliary program	*/
    char		**PreAuxArgs;		/* Args for Aux program	*/
    char		*PostAuxProgram;	/* Auxilliary program	*/
    char		**PostAuxArgs;		/* Args for Aux program	*/
    char		*FailAuxProgram;	/* Auxilliary program	*/
    char		**FailAuxArgs;		/* Args for Aux program	*/
    int			LogLevel;		/* Logging Level	*/
    int			AsyncMap;		/* Async Map for PPP    */
    struct access	*Access;		/* Access information   */
} REMOTE;

	
typedef struct range {
    struct range *Next;
    long Min,
	 Max;
} range_t;

typedef struct pattern {
    struct pattern *NextLevel;
    range_t *RangeList;
} pattern_t;

typedef	struct condition {
    int Type;
    struct condition *Left, *Right;
    pattern_t *Pattern;
} condition_t;

#define	COND_SRC	0x0001
#define	COND_DST	0x0002

#define	COND_PROTO	0x0010
#define	COND_SRCPROTO	(COND_PROTO|COND_SRC)
#define	COND_DSTPROTO	(COND_PROTO|COND_DST)

#define	COND_ADDR	0x0020
#define	COND_SRCADDR	(COND_ADDR|COND_SRC)
#define	COND_DSTADDR	(COND_ADDR|COND_DST)

#define	COND_TIME	0x0040

#define	COND_TRUE	0x0100
#define	COND_FALSE	0x0200

#define	COND_QUAL(t)	((t) & 0x000f)
#define	COND_COND(t)	((t) & 0x0ff0)
#define	COND_LEAF(t)	((t) & 0x0fff)

#define	COND_OR		0x1000
#define	COND_AND	0x2000
#define	COND_NOT	0x4000
#define	COND_BRANCH(t)	((t) & 0xf000)

#define	OP_AND		COND_AND
#define	OP_OR		COND_OR
#define	OP_NOT		COND_NOT
#define	OP_TRUE		COND_TRUE
#define	OP_FALSE	COND_FALSE
#define	OP_LPAR		0x00010000
#define	OP_RPAR		0x00020000
#define	OP_LCBK		0x00040000
#define	OP_RCBK		0x00080000

#define	REL_OP(t)	((t)&(OP_AND|OP_OR))
#define	UN_OP(t)	((t)&(OP_NOT))

#define	TO_ACTIVE	1
#define	TO_ACTIVE_C	2
#define	TO_ACTIVE_U	3
#define	TO_WAITING	4
#define	TO_FAILCALL	5

typedef struct condition_list {
    struct condition_list *Next;
    condition_t *Conditions;
    dp_timeo_t Timeouts;
} condition_list_t;

typedef struct timeout {
    struct timeout *Next;
    condition_list_t *ConditionList,
		     **LastConditionList,
		     *DefaultCondition;
    int Flag;
} timeout_t;

typedef struct path {
    int Type;
    int NLevels;
    long *Levels;
} path_t;

typedef struct pkt_info {
    path_t *DstProto,		/* Destination Protocol */
	   *SrcProto,		/* Source Protocol */
	   *DstAddr,		/* Destination Address */
	   *SrcAddr,		/* Source Address */
	   *CurrentTime;	/* Current Time */
} pkt_info_t;
	
typedef struct access {
    /*
     * The following fields are set from the associated Access file.
     */
    struct access	*Next;			/* Linked list		*/
    char		*Path;			/* Path to this file	*/

    condition_t		*Protos,		/* Protocols */
			*Addrs,			/* Addresses */
			*CallTimes,		/* Times to place calls */
			*ConnectTimes;		/* Times to remain connected */
    timeout_t		*DTimeouts,		/* Dialer Timeouts	*/
			*ATimeouts,		/* Answer Timeouts	*/
			*Timeouts;		/* Timeouts for both	*/
} access_t;

/*
 * A Rotary can have one or more Modems that are used on a "next available"
 * basis (as opposed to "first available"). This structure can hold information
 * about modems or rotaries depending on the fields as given.
 */

typedef struct _MODEM {
    char *Name;			/* Name of modem or rotary */
    int Flag;			/* Currently unused */
    int NModems;		/* # of modems in rotary (0 if not rotary) */
    int LastUsed;		/* Last modem used in rotary */
    char *Line;			/* Device (modem only) */
    int Speed;			/* Baud rate */
    char *DialScript;		/* Dialing Script */
    char *DialCharMap;		/* Phone Number Character Translations */
    int	AsyncMap;		/* Async Map for PPP */
    struct _MODEM **Modems;	/* List of modems (rotary only) */
} MODEM;

typedef struct pkt_trace {
    time_t time;
    ulong ppa;
    int dlu_len;
    int pkt_len;
} pkt_trace_t;

#define TS_LOW		0			/* Low-level transcript	*/
#define TS_HIGH		1			/* Hi-level transcript	*/

#define	SCRIPT_NO_ERR		0		/* Successful scripts */
#define	SCRIPT_DIAL_ERR		-1		/* Error in dial script */
#define	SCRIPT_LOGIN_ERR	-2		/* Error in login script */

extern REMOTE		*findconfig();
extern REMOTE		*findfdconfig();
extern void		failcall();

extern char		*optarg;
extern int		optind;

#define	AUX_PRE		0
#define	AUX_POST	1
#define	AUX_FAIL	2
