[comp.protocols.tcp-ip] Asynchronous Framing Technique Software: AFT.C

n2dsy@hou2d.UUCP (G.BEATTIE) (04/08/88)

/*
	Machine Independent Asynchronous Framing Technique (AFT)


            Version 1.0, by John Howell (N2FVN), Copyright August 7, 1987
               
            This software is free for non-commercial use.  All commercial
            rights are retained by the author or his designees.  Commercial 
            use without the explicit written permission of the author is
            prohibited.  This package is provided on an 'as is' basis 
            without warranty.
	

*/

		/* Macros */

#define	FLAG		0x007E
/* 
	This is the flag character which is used to start and end
	frames.  The send generates separate starting and ending flag
	characters, but the receiver can accept a single flag to end one
	frame and start another.
*/


#define	LEAD_IN		0x007D
/*
	This is the lead in character which is used when transparency is
	required.  When the receiver detects this character it takes the
	following character and exclusive ors it with 0x20 to produce
	the proper character.  This is used to allow the transmission of
	characters which would not otherwise be received properly.
*/


#define	TRANSPARENT(c)	(c ^ 0x20)
/*
	This is the function to be performed to convert a character into
	one which may be transmitted transparently.  This is also used
	to return a character to its original value since the function
	is its own inverse.
*/


#define	IDLE_CODE	(0x0100 + FLAG)
/*
	This is the idle code with is returned by aft_tx_char when it
	has no frame to send.  The high order byte indicates completion
	and the low order byte is a flag which might be sent if flags
	are to be used between frames as filler.
*/


#define	GENERATOR	0x8408
/*
	This is the generator polynomial for the frame check sequence.
	The polynomial is the standard CCITT polynomial.  It is in
	reverse bit order for faster calculation.  The polynomial is
	X**16 + X**12 + X**5 + 1.
*/


#define	EXPECT_FCS	0xF0B8
/*
	This is the expected final FCS for a correctly received frame.
*/


#define	OK		1
/*
	This is the return code used by some routines to indicate
	successful completion.
*/


#define	NO_GOOD		0
/*
	This is the return code used by some routines to indicate
	unsuccessful completion.
*/



/* Global declarations */


static char opt_ebdt;
/*
	This is a flag controlling the use of eight-bit data
	transparency on transmitted and receive frames.
*/


static char opt_transparency_level;
/*
	This is the transparency level to be used when transmitting
	frames (0-2).  Each increasing level causes more characters to
	be avoided when sending frames which allows them to be sent in
	situations where certain characters are not allowed.
*/


static char opt_suffix_len;
static unsigned char opt_suffix[3];
/*
	This is the suffix string to be added to the end of each frame
	sent.  Any character including a null is allowed.
*/


static unsigned int opt_max_rx_len;
/*
	This is the maximum size of a receive buffer.  It is used when
	receiving to make sure that a received frame does not go past
	the end of the buffer.  A received frame which is too long will
	be discarded.
*/

static unsigned char transparency_level [256] = {
	
	2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 2,

	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
	9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
/*
	For each possible character this is the transparency level at
	which it should be encoded for transparency.  A value of nine is
	used for character which should never be encoded.
*/


		/* Variables and macros used by aft_tx_char */

static char tx_char_state;
/*
	This is the state of the transmission routine which handles
	transparency, delimiting of frames, and frame abort.  It is used
	to determine the next action to be performed by aft_tx_char when
	it is called.  State names are given by the 'TCS_' macros.
*/


#define TCS_IDLE		0
/*
	This state