english
version "1.0"
identify "wxyz"

# Copyright (c) 1998-2004 by Wayne C. Gramlich.
# All rights reserved.
#
# Permission to use, copy, modify, distribute, and sell this software
# for any purpose is hereby granted without fee provided that the above
# copyright notice and this permission are retained.  The author makes
# no representations about the suitability of this software for any purpose.
# It is provided "as is" without express or implied warranty.

module tcp_monitor

#: This module monitors the traffic between a client and a server
#, communicating via a TCP stream.

define options
    record
	debug logical			#: {true}=>debug mode on
	listen_port string		#: Port to listen on
	listen_port_number unsigned	#: Converted listen port number
	output_file string		#: Output file name string
	queue_length string		#: Lenght of queue
	queue_length_number unsigned	#: Converted queue length
	server_host_name string		#: Server host name
	server_port string		#: Server port number
	server_port_number unsigned	#: Converted server port number
    generate allocate, erase, print

define common				#: Common stuff
    record
	buffer_size unsigned		#: Size of buffers
	debug_stream out_stream		#: Debugging stream
	host_internet_address unsigned	#: Server internet address
	id_counter unsigned		#: Connection id counter
	options options			#: Command line options
	monitor_channel write_channel[connection] #: The monitor log channel
	monitor_buffer string		#: Temporary buffer for monitor stream
	this_internet_address unsigned	#: Internet address of this host
    generate address_get, allocate, erase, identical, print

define connection			#: One client server connection object
    record
	client_channel_pair channel_pair[connection]  #: Client channel pair
	common common			#: Common storage
	id unsigned			#: Connection id
	server_channel_pair channel_pair[connection]  #: Server channel pair
    generate address_get, allocate, erase, identical, print



procedure main
    takes
	system system
    returns unsigned

    #: This procedure will monitor some TCP connections between multiple
    #, clients of one server.


#: {common} routines:

procedure create@common
    takes
	buffer_size unsigned
	debug_stream out_stream
	options options
	this_internet_address unsigned
	host_internet_address unsigned
    returns common

    #: This procedure will create and return a common storage object
    #, that contains {buffer_size}, {debug_stream}, {options},
    #, {this_internet_address}, and {host_internet_address}.


#: {connection} routines:

procedure client_accept@connection
    takes
	client_channel_pair channel_pair[connection]
	listen_connection connection
    returns_nothing

    #: This procedure is the callback routine that establishes a
    #, a client server connection.
    

procedure client_read@connection
    takes
	client_read_channel read_channel[connection]
    returns_nothing

    #: This procedure will take care of any client read activity.


procedure client_write@connection
    takes
	client_write_channel write_channel[connection]
    returns_nothing

    #: This procedure will take care of any client write activity.


procedure close@connection
    takes
	connection connection
    returns_nothing

    #: This procedure will close down {connection}


procedure create@connection
    takes
	common common
	client_channel_pair channel_pair[connection]
	server_channel_pair channel_pair[connection]
    returns connection

    #: This procedure will create and return an empty {connection} object
    #, containing {common}.


procedure monitor_write@connection
    takes
	monitor_channel write_channel[connection]
    returns_nothing

    #: This procedure will take care of any monitor log channel activity.


procedure server_read@connection
    takes
	server_read_channel read_channel[connection]
    returns_nothing

    #: This procedure will take care of any server read activity.


procedure server_write@connection
    takes
	server_write_channel write_channel[connection]
    returns_nothing

    #: This procedure will take care of any server write activity.


procedure transfer@connection
    takes
	connection connection
    returns_nothing

    #: This predure will cause data to be transfered between
    #, the client and server.


procedure transfer_some@connection
    takes
	connection connection
	write_channel write_channel[connection]
	read_channel read_channel[connection]
	tag string
    returns_nothing

    #: This procedure will cause some data to be transfered from {read_channel}
    #, to {write_channel} with logging to the monitor channel.  {tag}
    #, specifies the direction character for the monitor log.