net-11.2_dgd-1.1.97.gz

snippets · 26.3 KB · 2010-03-30

DGD networking package version 11 (net-11.2) for DGD 1.1.97.

Download   « Back to the snippets list

                 A Networking Package for DGD, Version 11

This package extends the IP networking features of DGD, at the cost of some
security. (A driver that can initiate connections or send udp packets can, if
its internal security fails, be used as a platform for further attacks by a
cracker or other malicious user.)

Note: After you install this package, any object will be able to use the
network functions.  To change this, you will have to modify the auto object.

To install it, you need the "patch" utility, written by Larry Wall.
If you do not have it, you can find a copy at the following URL:

    ftp://ftp.gnu.org/gnu/patch/

Patch your DGD 1.1.75 sources by changing to the directory containing
the DGD src and doc directories, then typing

    patch -c -s --suffix=.nonet --fuzz=0 -p1 < filename

where "filename" is the name of the file containing this package.  If you
have a different DGD version, watch the output from "patch".  If it reports
any problems (or leaves any *.rej files in your DGD directory), your version
is probably incompatible.  Patch will have left the original (unpatched)
source files in copies with .nonet extensions.

Read the doc/Networking file for further information.
Current maintenance done by Neil McBride <sarak@castinian.org>

diff -crN dgd-net/Copyright dgd+net/Copyright
*** dgd-net/Copyright	Thu Sep 21 12:06:59 2000
--- dgd+net/Copyright	Thu Jan  1 10:00:00 1970
***************
*** 1,21 ****
- Copyright 1993 - 2000 BeeHive Internet Technologies, Inc.  All Rights Reserved.
- 
- Permission is granted to copy the source and executables made therefrom, but
- any commercial distribution or use whatsoever is not allowed.  Commercial
- distribution or use refers to any distribution or use from which any form of
- income is received regardless of profit therefrom, or from which any revenue
- or promotional value is received, as well as any distribution to or use in a
- corporate environment.  Use of the source and executables made therefrom to
- promote or support a commercial venture is included in this restriction.
- Exclusive worldwide rights to this program for use as a server are held by
- Acuity Corporation.  If you wish to purchase a commercial license, please send
- email to sales@acuity.com.  For more information please look at the Acuity web
- site which can be found at http://www.acuity.com.
- 
- Any modifications of this program lacking an implicit copyright notice are
- subject to the copyright stated herein.  Any explicit copyright notices added
- to this program shall not contradict the terms stated herein, but may further
- restrict distribution or use.  Any modifications should be made in such a way
- that it is clear that this copyright notice applies to the source, executables
- made therefrom and any derivatives thereof and in such a way that it is
- possible to regain the unmodified source.
--- 0 ----
diff -crN dgd-net/Copyright.DGD dgd+net/Copyright.DGD
*** dgd-net/Copyright.DGD	Thu Jan  1 10:00:00 1970
--- dgd+net/Copyright.DGD	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,21 ----
+ Copyright 1993 - 2000 BeeHive Internet Technologies, Inc.  All Rights Reserved.
+ 
+ Permission is granted to copy the source and executables made therefrom, but
+ any commercial distribution or use whatsoever is not allowed.  Commercial
+ distribution or use refers to any distribution or use from which any form of
+ income is received regardless of profit therefrom, or from which any revenue
+ or promotional value is received, as well as any distribution to or use in a
+ corporate environment.  Use of the source and executables made therefrom to
+ promote or support a commercial venture is included in this restriction.
+ Exclusive worldwide rights to this program for use as a server are held by
+ Acuity Corporation.  If you wish to purchase a commercial license, please send
+ email to sales@acuity.com.  For more information please look at the Acuity web
+ site which can be found at http://www.acuity.com.
+ 
+ Any modifications of this program lacking an implicit copyright notice are
+ subject to the copyright stated herein.  Any explicit copyright notices added
+ to this program shall not contradict the terms stated herein, but may further
+ restrict distribution or use.  Any modifications should be made in such a way
+ that it is clear that this copyright notice applies to the source, executables
+ made therefrom and any derivatives thereof and in such a way that it is
+ possible to regain the unmodified source.
diff -crN dgd-net/Copyright.NET dgd+net/Copyright.NET
*** dgd-net/Copyright.NET	Thu Jan  1 10:00:00 1970
--- dgd+net/Copyright.NET	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,14 ----
+ The networking-DGD package is Copyright 1994-1996 by Richard Braakman,
+ Copyright 1996-1999 by Erwin Harte, and Copyright 2000 by Neil McBride.
+ 
+ The package is also a derived work of Dworkin's Generic Driver, which
+ is copyrighted by BeeHive Internet Technologies, Inc.  Therefore, its
+ copyright applies to it in addition to mine.  Its license is included
+ in the file Copyright.DGD.
+ 
+ I grant permission for any use of this package which does not conflict
+ with the terms of the DGD copyright mentioned above.
+ 
+ Richard Braakman <dark@xs4all.nl>
+ Erwin Harte <harte+netpackage@xs4all.nl>
+ Neil McBride <sarak@castinian.org>
diff -crN dgd-net/doc/Introduction dgd+net/doc/Introduction
*** dgd-net/doc/Introduction	Thu Sep 21 12:06:59 2000
--- dgd+net/doc/Introduction	Thu Sep 21 12:06:23 2000
***************
*** 13,20 ****
  that DGD provides, and it can also define standard properties that
  every object should have.  DGD is made in such a way that some kfuns
  <have> to be redeclared in an environment with several users
! programming together; for instance, by default there is no file security.
! The auto object has some other special properties; static functions
  declared in the auto object cannot be called using this_object()->func()
  (i.e. they behave like kfuns).
     The driver object defines several functions which are called by the
--- 13,20 ----
  that DGD provides, and it can also define standard properties that
  every object should have.  DGD is made in such a way that some kfuns
  <have> to be redeclared in an environment with several users
! programming together; for instance, by default there is no file or network
! security.  The auto object has some other special properties; static functions
  declared in the auto object cannot be called using this_object()->func()
  (i.e. they behave like kfuns).
     The driver object defines several functions which are called by the
***************
*** 66,79 ****
  	destruct B; if this is done, A will inherit the most recent
  	versions of B and C.
  
-     object telnet_connect()
- 
- 	Return an object for a new connection on the telnet port.
- 
-     object binary_connect()
- 
- 	Return an object for a new connection on the binary port.
- 
      void interrupt()
  
  	Called if the driver receives a kill signal.  The function should
--- 66,71 ----
***************
*** 143,161 ****
  	clone, just before a function is called in it for the first time.
  	The actual name of this function is configurable.
  
!  - in the user object (returned by driver->telnet_connect() or
!    driver->binary_connect()):
  
      int open()
  
  	A connection has just been opened for this object.
- 	For a binary connection, if the return value of this function is
- 	non-zero, a UDP channel will be opened for the same object.  UDP
- 	datagrams can be received from the same host and port as the TCP
- 	connection is on, and after at least one datagram has been
- 	received, send_datagram() can be used to send datagrams to that
- 	destination.
- 	For a telnet connection, the return value will be ignored.
  
      void close(int flag)
  
--- 135,176 ----
  	clone, just before a function is called in it for the first time.
  	The actual name of this function is configurable.
  
!  - in a port object (made with kfun open_port()):
! 
!     void open()
! 
! 	The port has just been opened.
! 
!     void close()
! 
! 	The port for this object has just been closed.  It is also called
! 	when the port object is destructed.
! 
!     object connection(string fromhost, int fromport)
! 
! 	A client seeks a connection to this port.  Return a connection
! 	object for that client.
! 
!  - extra in UDP port objects (made with open_port(), protocol "udp"):
! 
!     int receive_datagram(string data, string fromhost, int fromport)
! 
! 	A UDP datagram has arrived on the port.  Return 0 if the datagram
! 	has been processed completely, or 1 if the object would like an
! 	opportunity to continue processing when it is its turn again.
! 
!     int process_datagram()
! 
! 	The previous call to receive_datagram() or process_datagram()
! 	returned 1, and no datagram has arrived during this input round.
! 	Return 0 if processing is complete, otherwise 1.
! 
!  - in connection objects (returned by port_object->connection() or
!    made with kfun connect()):
  
      int open()
  
  	A connection has just been opened for this object.
  
      void close(int flag)
  
***************
*** 164,192 ****
  	destructed.  The flag argument is 1 if the object is destructed,
  	0 otherwise.
  
!     void receive_message(string message)
  
  	This function is called with the text the user typed.  For telnet
  	connections, this is always an entire line, with the newline at
! 	the end filtered out.  For binary connections, this is whatever
  	the other side of the connection saw fit to send, without any
! 	filtering or processing.
  
      void message_done()
  
! 	This function is called when a buffered string that could not be
! 	sent entirely when send_message was called, was fully transmitted.
  
     On startup, the system is configured with a configuration file, which
  must specify the following configuration parameters:
  
  param		type	meaning
  --------------------------------------------------------------------------
! telnet_port	number	The port number on which the driver will accept
! 			telnet connections.
! binary_port	number	The port number on which the driver will accept
! 			binary connections.  Communication on the binary
! 			port is raw TCP/IP without additional protocol.
  directory	string	The base directory for the system.
  users		number	The maximum number of active telnet and binary
  			connections.
--- 179,214 ----
  	destructed.  The flag argument is 1 if the object is destructed,
  	0 otherwise.
  
!     int receive_message(string message)
  
  	This function is called with the text the user typed.  For telnet
  	connections, this is always an entire line, with the newline at
! 	the end filtered out.  For tcp connections, this is whatever
  	the other side of the connection saw fit to send, without any
! 	filtering or processing.  Return 0 if the message has been
! 	completely processed, or 1 if the object would like an opportunity
! 	to continue processing when it is its turn again.
! 
!     int process_message()
! 
! 	The previous call to receive_message() or process_message()
! 	returned 1, and no new data has arrived during this input round.
! 	Return 0 if processing is complete, otherwise 1.
  
      void message_done()
  
! 	Messages from previous calls to kfun send_message() have been sent,
! 	and the output buffers are empty.  This function is only called
! 	in specific circumstances; see the documentation for send_message().
! 
  
     On startup, the system is configured with a configuration file, which
  must specify the following configuration parameters:
  
  param		type	meaning
  --------------------------------------------------------------------------
! telnet_port	number	Ignored for compatibility with normal dgd.
! binary_port	number	Ignored for compatibility with normal dgd.
  directory	string	The base directory for the system.
  users		number	The maximum number of active telnet and binary
  			connections.
diff -crN dgd-net/doc/Networking dgd+net/doc/Networking
*** dgd-net/doc/Networking	Thu Jan  1 10:00:00 1970
--- dgd+net/doc/Networking	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,61 ----
+ 	General overview of the facilities of the DGD Networking package
+ 
+ This package extends the IP networking features of DGD, at the cost of some
+ security.  (A driver that can initiate connections or send udp packets can,
+ if its internal security fails, be used as a platform for further attacks
+ by a cracker or other malicious user.)
+ 
+ Note: Like the file access functions, the network functions are
+ unprotected.  If you install this package, you must redefine the functions
+ connect() and open_port() in the auto object to implement the desired
+ security.  A common method is to allow any object in /sys/net/ (and no
+ others) to call these functions.
+ 
+ The networking package introduces three special kinds of objects:
+ 	- tcp and telnet port objects
+ 	- tcp and telnet connection objects
+ 	- udp port objects
+ 
+ Tcp and telnet port objects are associated with a tcp port.
+ Whenever a client seeks a connection with a port DGD has opened, 
+ 	connection(string clienthost, int clientport)
+ is called in the port object associated with that port.  This function is
+ expected to return an object that will function as the connection object
+ for that client.
+ 
+ Udp port objects are associated with a udp port.  There are no udp
+ connection objects.  When a udp datagram arrives on a port DGD has opened,
+ 	receive_datagram(string data, string fromhost, int fromport)
+ is called in the udp port object associated with it.
+ 
+ Tcp and telnet connection objects are associated with a client.  Whenever
+ data arrives on a tcp connection, receive_message(string data) is called in
+ the connection object.  When data arrives on a telnet connection, the
+ driver parses it according to the telnet protocol.  Whenever a full line is
+ parsed, receive_message(string text) is called in the connection object.
+ 
+ See the Introduction text for a more complete explanation of what functions
+ are called, when they are called, and what they mean.
+ 
+ There are kfuns for opening ports, sending udp datagrams and tcp/telnet
+ data, and initiating tcp or telnet connections. See kfun/open_port,
+ kfun/send_datagram, kfun/send_message, and kfun/connect.
+ 
+ The config-file entries `telnet_port' and `binary_port' are still valid but
+ will be ignored.  The driver object should take care of opening the
+ appropriate ports during initialization.
+ 
+ The kfun users() returns all connection objects.  The kfun ports() returns
+ all port objects.  The kfun query_ip_number() returns 0 for port objects.
+ The config-file entry `users' also counts port objects.
+ 
+ The driver-object functions telnet_connect() and binary_connect() are
+ no longer used.
+ 
+ Acknowledgements:
+ I would like to thank Robert Leslie, Erwin Harte, and Adam David for
+ their willingness to run real projects on test versions of this package,
+ for many excellent suggestions, and for their help in hunting down and
+ killing some nasty bugs.
+ 
+ Richard Braakman <dark@xs4all.nl>
diff -crN dgd-net/doc/kfun/block_input dgd+net/doc/kfun/block_input
*** dgd-net/doc/kfun/block_input	Thu Sep 21 12:06:50 2000
--- dgd+net/doc/kfun/block_input	Thu Sep 21 12:06:23 2000
***************
*** 6,11 ****
  
  
  DESCRIPTION
! 	Block or unblock input from the user associated with the current
! 	object, depending on whether the argument is non-zero or zero,
! 	respectively.
--- 6,15 ----
  
  
  DESCRIPTION
! 	Block or unblock input from the port or connection associated with
!         the current object, depending on whether the argument is non-zero or
!         zero, respectively.
!         For port-objects blocking input means no new connections will be
!         accepted, so no connection() calls.   With connection-objects this
!         means no receive_message(), process_message(), receive_datagram(),
!         or process_datagram() calls.
diff -crN dgd-net/doc/kfun/connect dgd+net/doc/kfun/connect
*** dgd-net/doc/kfun/connect	Thu Jan  1 10:00:00 1970
--- dgd+net/doc/kfun/connect	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,27 ----
+ NAME
+ 	connect - initiate a tcp connection
+ 
+ SYNOPSIS
+ 	void connect(string ipnumber, int port, varargs string protocol)
+ 
+ 
+ DESCRIPTION
+ 	Initiate a telnet or tcp connection to the specified host and port,
+ 	and associate the connection with this object when it is established.
+ 	The protocol may be "telnet" or "tcp", or it may be omitted to
+ 	default to "tcp".
+ 
+ 	When the connection succeeds, open() will be called in obj.  If it
+ 	fails or times out, close() will be called in obj.  After the
+ 	connection is established, obj will be a normal user object.
+ 
+ ERRORS
+ 	An error will result if the object was already has a connection,
+ 	port, or editor associated with it.
+ 	If the number of existing port and connection objects is equal
+ 	to the value of status()[ST_UTABSIZE], calling connect() results
+ 	in an error.
+ 
+ 
+ SEE ALSO
+ 	kfun/open_port, kfun/send_message, kfun/send_datagram
diff -crN dgd-net/doc/kfun/open_port dgd+net/doc/kfun/open_port
*** dgd-net/doc/kfun/open_port	Thu Jan  1 10:00:00 1970
--- dgd+net/doc/kfun/open_port	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,45 ----
+ NAME
+ 	open_port - associate an object with an internet port
+ 
+ SYNOPSIS
+ 	void open_port(string protocol, varargs int port)
+ 
+ 
+ DESCRIPTION
+ 	Open a port on the host machine and connect it to this object, which
+ 	is then known as a "port object".  The protocol may be any of "tcp",
+ 	"udp", or "telnet".  Port objects behave differently, depending on
+ 	the protocol. If the port number is omitted (meaning that the kfun
+ 	is called with only _one_ argument) a random unused port will be
+ 	selected.
+ 
+ 	When a port is first opened, open(int port) is called in its port
+ 	object.  When it is closed for any reason, close() is called in
+         its port object.  Destructing a port object will close its port
+         first.
+ 
+ 	In a telnet or tcp port,
+ 	     connection(string ipnumber, int port)
+ 	will be called whenever a client connects to it.  This function
+ 	should return an object that will then be known as a "user object".
+ 
+ 	In an udp port, 
+ 	    receive_datagram(string data, string ipnumber, int port)
+ 	will be called whenever a datagram arrives on its port.	 The
+ 	ipnumber and port will be those of the sender.	UDP port objects
+ 	may also send datagrams.
+ 
+ 
+ ERRORS
+ 	An error will result if the object was already has a connection,
+ 	port, or editor associated with it.
+ 	If the number of existing port and connection objects is equal
+ 	to the value of status()[ST_UTABSIZE], calling open_port() results
+ 	in an error.
+ 	If it is immediately apparent that the port cannot be opened,
+ 	an error will be reported.  In such a case, neither open() nor
+ 	close() will be called in the port object.
+ 
+ 
+ SEE ALSO
+ 	kfun/ports, kfun/send_datagram, kfun/send_message, kfun/connect
diff -crN dgd-net/doc/kfun/ports dgd+net/doc/kfun/ports
*** dgd-net/doc/kfun/ports	Thu Jan  1 10:00:00 1970
--- dgd+net/doc/kfun/ports	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,13 ----
+ NAME
+ 	ports - return the array of port objects
+ 
+ SYNOPSIS
+ 	object *ports()
+ 
+ 
+ DESCRIPTION
+ 	Return an array containing the objects associated with the
+ 	currently open ports.
+ 
+ SEE ALSO
+ 	kfun/open_port, kfun/users
diff -crN dgd-net/doc/kfun/query_ip_name dgd+net/doc/kfun/query_ip_name
*** dgd-net/doc/kfun/query_ip_name	Thu Sep 21 12:06:54 2000
--- dgd+net/doc/kfun/query_ip_name	Thu Jan  1 10:00:00 1970
***************
*** 1,15 ****
- NAME
- 	query_ip_name - get the ip name of a user
- 
- SYNOPSIS
- 	string query_ip_name(object user)
- 
- 
- DESCRIPTION
- 	Return the IP name of a user, as a string, or nil if the given object
- 	is not a user object.  If the IP name could not be resolved, the
- 	IP number is returned, instead.
- 
- SEE ALSO
- 	kfun/query_ip_number, kfun/send_datagram, kfun/send_message,
- 	kfun/this_user, kfun/users
--- 0 ----
diff -crN dgd-net/doc/kfun/query_ip_number dgd+net/doc/kfun/query_ip_number
*** dgd-net/doc/kfun/query_ip_number	Thu Sep 21 12:06:54 2000
--- dgd+net/doc/kfun/query_ip_number	Thu Sep 21 12:06:23 2000
***************
*** 10,14 ****
  	is not a user object.
  
  SEE ALSO
! 	kfun/query_ip_name, kfun/send_datagram, kfun/send_message,
! 	kfun/this_user, kfun/users
--- 10,13 ----
  	is not a user object.
  
  SEE ALSO
! 	kfun/send_message, kfun/this_user, kfun/users
diff -crN dgd-net/doc/kfun/send_datagram dgd+net/doc/kfun/send_datagram
*** dgd-net/doc/kfun/send_datagram	Thu Sep 21 12:06:52 2000
--- dgd+net/doc/kfun/send_datagram	Thu Sep 21 12:06:23 2000
***************
*** 1,20 ****
  NAME
! 	send_datagram - send a message on the datagram channel
  
  SYNOPSIS
! 	int send_datagram(string message)
  
  
  DESCRIPTION
! 	Send a message on the datagram channel of a binary connection object.
! 	At least one message must have been received on the same channel
! 	before this function can be used.
! 	The return value is the length of the datagram sent if no error
! 	occurred, or 0 otherwise.
  
  ERRORS
! 	An error will result if the current object has no datagram channel.
  
  SEE ALSO
! 	kfun/query_ip_name, kfun/query_ip_number, kfun/send_message,
! 	kfun/this_user, kfun/users
--- 1,19 ----
  NAME
! 	send_datagram - send a datagram
  
  SYNOPSIS
! 	void send_datagram(string data, string destination, int port)
  
  
  DESCRIPTION
! 	Send a UDP datagram to the specified host and udp port.	 No hostname
! 	lookup is done so the destination must be an ip number.
! 	Only udp port objects can use this function.
! 
  
  ERRORS
! 	An error will result if the object is not an udp port object.
! 
  
  SEE ALSO
! 	kfun/open_port, kfun/send_message, kfun/connect
diff -crN dgd-net/doc/kfun/send_message dgd+net/doc/kfun/send_message
*** dgd-net/doc/kfun/send_message	Thu Sep 21 12:06:55 2000
--- dgd+net/doc/kfun/send_message	Thu Sep 21 12:06:23 2000
***************
*** 3,26 ****
  
  SYNOPSIS
  	int send_message(string message)
! 	int send_message(int echo)
  
  
  DESCRIPTION
  	In the first form, send a message to the user associated with the
! 	current object, returning the number of bytes that could be sent
! 	immediately.  When used by the driver object, the string will be
! 	written to stderr.  In the second form, user input echoing will be
! 	turned off or on, depending on the value of the argument being zero or
! 	non-zero, respectively; the return value is 1 if input echoing could be
! 	turned on, or 0 otherwise.
! 	If only part of the string could be sent right away, the remainder of
! 	the string is buffered, and message_done() will be called in the user
! 	object when all data in the buffer has been sent.
! 	If send_message() is used to send a new string before all of the
! 	buffered data from a previous call has been sent, the buffered data is
! 	discarded.
  
  SEE ALSO
! 	kfun/query_ip_name, kfun/query_ip_number, kfun/send_datagram,
! 	kfun/this_user, kfun/users
--- 3,38 ----
  
  SYNOPSIS
  	int send_message(string message)
! 	int send_message(int code)
  
  
  DESCRIPTION
  	In the first form, send a message to the user associated with the
! 	current object.	 Used by the driver object, send_message() will write
! 	the message on stderr.  In the second form, send_message(0) means
! 	turn input echoing off, and send_message(1) means turn input echoing
! 	on; they are only meaningful for telnet connections.  Use of
! 	send_message(-1) is described below.
! 
!         The function will return 0 if there is room for more messages,
!         and 1 if not.  If it returns 1, the driver will call message_done()
! 	in this object when all messages have been sent; if this object
! 	calls send_message() again before message_done() has been called,
! 	the remainder of the previous message will be discarded.
! 
! 	Calling send_message(-1) will return 0 if all messages have been
! 	sent, and 1 if not.  If it returns 1, the driver will call
! 	message_done() in this object when all messages have been sent.
! 
! 	Destructing a connection object before all messages have been sent
! 	may cause the messages to be lost.
! 
! 
! ERRORS
! 	Calling send_message() in an object that is not connected will
! 	result in an error.
! 
  
  SEE ALSO
! 	kfun/query_ip_number, kfun/this_user, kfun/connect, kfun/open_port,
! 	kfun/users
diff -crN dgd-net/doc/kfun/this_user dgd+net/doc/kfun/this_user
*** dgd-net/doc/kfun/this_user	Thu Sep 21 12:06:54 2000
--- dgd+net/doc/kfun/this_user	Thu Sep 21 12:06:23 2000
***************
*** 10,14 ****
  	execution started from a delayed function call, nil is returned.
  
  SEE ALSO
! 	kfun/query_ip_name, kfun/query_ip_number, kfun/send_datagram,
! 	kfun/send_message, kfun/users
--- 10,13 ----
  	execution started from a delayed function call, nil is returned.
  
  SEE ALSO
! 	kfun/query_ip_number, kfun/send_message, kfun/users
diff -crN dgd-net/doc/kfun/users dgd+net/doc/kfun/users
*** dgd-net/doc/kfun/users	Thu Sep 21 12:06:52 2000
--- dgd+net/doc/kfun/users	Thu Sep 21 12:06:23 2000
***************
*** 10,14 ****
  	users.
  
  SEE ALSO
! 	kfun/query_ip_name, kfun/query_ip_number, kfun/send_datagram,
! 	kfun/send_message, kfun/this_user
--- 10,13 ----
  	users.
  
  SEE ALSO
! 	kfun/query_ip_number, kfun/send_message, kfun/this_user
diff -crN dgd-net/mud/include/config.h dgd+net/mud/include/config.h
*** dgd-net/mud/include/config.h	Thu Sep 21 12:07:00 2000
--- dgd+net/mud/include/config.h	Thu Sep 21 12:06:23 2000
***************
*** 3,9 ****
  # undef SYS_CONTINUOUS		/* off by default */
  # undef SYS_DATAGRAMS		/* off by default */
  
! # ifdef __SKOTOS__
  #  define SYS_NETWORKING	/* Skotos server has networking capabilities */
  # endif
  
--- 3,9 ----
  # undef SYS_CONTINUOUS		/* off by default */
  # undef SYS_DATAGRAMS		/* off by default */
  
! # if defined(__SKOTOS__) || defined(__NET__)
  #  define SYS_NETWORKING	/* Skotos server has networking capabilities */
  # endif
  
diff -crN dgd-net/mud/include/std.h dgd+net/mud/include/std.h
*** dgd-net/mud/include/std.h	Thu Mar 27 12:03:12 1997
--- dgd+net/mud/include/std.h	Thu Sep 21 12:06:23 2000
***************
*** 1,2 ****
--- 1,4 ----
  # define TRUE	1
  # define FALSE	0
+ 
+ # define __NET__
diff -crN dgd-net/mud/kernel/lib/connection.c dgd+net/mud/kernel/lib/connection.c
*** dgd-net/mud/kernel/lib/connection.c	Thu Sep 21 12:07:00 2000
--- dgd+net/mud/kernel/lib/connection.c	Thu Sep 21 12:06:23 2000
***************
*** 233,239 ****
--- 233,243 ----
  int message(string str)
  {
      if (previous_object() == user) {
+ #ifdef __NET__
+ 	return !send_message(str);
+ #else
  	return (send_message(str) == strlen(str));
+ #endif
      }
  }
  
diff -crN dgd-net/mud/kernel/lib/port.c dgd+net/mud/kernel/lib/port.c
*** dgd-net/mud/kernel/lib/port.c	Thu Sep 21 12:07:00 2000
--- dgd+net/mud/kernel/lib/port.c	Thu Sep 21 12:06:23 2000
***************
*** 122,126 ****
   */
  static int datagram(string str, string host, int port)
  {
!     return send_datagram(str, host, port);
  }
--- 122,127 ----
   */
  static int datagram(string str, string host, int port)
  {
!     send_datagram(str, host, port);
!     return 1;
  }
diff -crN dgd-net/src/Changelog.Net dgd+net/src/Changelog.Net
*** dgd-net/src/Changelog.Net	Thu Jan  1 10:00:00 1970
--- dgd+net/src/Changelog.Net	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,158 ----
+ Every package has two version indicators: A dgd-net release number,
+ and a DGD version number. There is a loose correspondence between them,
+ because support for older DGD versions is often dropped in later releases.
+ Contact me if you need a particular release / version combination that
+ is not supported.
+ 
+ Release 1:
+    - comm_receive condensed to a single loop
+    - cleaned up buffer allocation
+    - put SO_REUSEADDR back
+ 
+ Release 2:
+    - fixed the call to setsockopt
+    - made it compatible with precompilation
+ 
+ Release 3:
+    - properly initialized usr->newlines (fixes crashing bug)
+    - fixed bug where commands would only be executed if several were queued.
+ 
+ Release 4:
+    - added LINEMODE patches from DGD 1.0.8.3 (+minor fix to SB/SE handling)
+    - fixed send_message() for when the driver object doubles as a port object
+    - reversed order of checking for errors and writability in conn_connected
+    - an error is raised if a connect() fails right away (used to be silent)
+ 
+ Release 5:
+    - Added config.c and comp/comp.c patches from DGD 1.0.8.8
+    - Doc fix: accept() has been connection() since release 0.
+    - Added checks for nusers < maxusers before calling comm_new
+    - Made some error messages clearer
+    - Made sure no writes are done to not yet connected sockets
+ 
+ Release 6:
+    - Fixed O_USER check in kfun_send_datagram().  (Operator precedence)
+    - Added SO_ERROR check to select-on-exception check for conn_connected()
+    - It seems some hosts report EAGAIN rather than EINPROGRESS for
+      non-blocking connect() calls.  Added check for EAGAIN.
+    - Made conn_write() retry if it receives EINTR.
+    - this_user() is now set during port_object->open().
+    - Peeked in tinyfugue sources to see how it handles nonblocking
+      connect().  conn_connected() rewritten accordingly.
+    - Flow control:  When a message will not fit in the output buffer,
+      allocate the space to hold it and return 1.  The driver will call
+      message_done() in the connection object when the entire message has
+      been sent.  If the connection object uses send_message() before
+      the previous message was done, the unsent part of the previous
+      message will be discarded.  This should be compatible with 1.0.19's
+      handling of flow control.
+ 
+ Release 7:
+    - Automatically try to extend the socket output buffer when a write
+      can not send everything or a sendto fails with EMSGSIZE.
+    - Do not send WONT ECHO when a new connection is accepted.  Clients
+      must start in that mode anyway.  To help users with broken clients,
+      the connection object can toggle echoing off and then on.  Also,
+      every telnet client I have seen has some way to specify which side
+      should echo.
+    - Do not send DO LINEMODE when a new connection is accepted.  It was
+      sent to help certain BSD telnet clients, which do not normally
+      negotiate LINEMODE when contacting a non-standard port, but (I've heard)
+      cannot function properly without it.  The user of such a client can
+      specify the port as a negative number to tell it to always start
+      negotiations.
+    - Because no telnet-specific options are sent when a new connection
+      is accepted, the "telnet" protocol has become useful for tasks like
+      ftp-control and smtp.
+    - *** Any object can do open_port() or connect() on itself.
+    - Moved telnet-negotiation flags to a separate field.
+    - Telnet protocol will now refuse unknown options.
+ 
+ Release 8:
+    - Increased input buffers for "tcp" receive_message() and
+      "udp" receive_datagram() to 8192 bytes.
+    - IAC IP and IAC BREAK are now ignored.  (Used to echo DEL for IP
+      and FS for BREAK.)
+    - Changed reply to DO TM to WONT TM.  WILL TM implies that the WILL TM
+      has been inserted at the correct place in the output stream, which is
+      not true.
+    - Added support for IAC EC (erase character) and IAC EL (erase line)
+    - Set socket option OOBINLINE on telnet/tcp connections to receive
+      out-of-band data inline.
+    - When receiving a Data Mark (IAC DM) or Interrupt (IAC IP), purge the
+      input buffer.  This assumes that a Data Mark is always part of a telnet
+      "Synch" sequence.  Since DGD effectively treats all telnet input as
+      'urgent', discarding previous input should be all that is necessary.
+    - conn_accept() checks read-select before trying accept().
+    - Added ports() efun.  Efun users() no longer returns ports.
+      query_ip_number() returns 0 for ports.
+    - Speedup when maxusers is much larger than nusers.
+    - Introduced comm_confirm(), so that message_done() and close() are
+      called during a separate round.
+    - Use 'flush' and 'callbacks' to keep track of whether a scan of
+      all users is necessary in comm_flush() and comm_confirm(), respectively.
+    - Patched up some cases where bad connections were not closed.
+    - Moved call to comm_init() to a better place.
+    - send_message(-1) can be used to ask if the previous message is done.
+      It will return 0 if all output has been written.  Otherwise, it will
+      return 1 and the driver will call message_done() when all output has
+      been written.
+    - If receive_message() or receive_datagram() returns 1, and there is
+      no more data available during the next input round, the driver will
+      call process_message() or process_datagram() in the object.
+    - Memory allocation in comm_send() rounds up to nearest 8k boundary,
+      to help the static memory allocator.
+ 
+ Release 9:
+    - process_message() and process_datagram() now actually work.
+ 
+ Release 10:
+    - Avoided memmove().
+    - Made "protocol" argument of connect() optional.
+    - Switched arguments of open_port() and made the port number optional.
+    - Bugfix: comm_new() now calls conn_del(conn) if there is an error. [rare]
+    - Started adding telnet flow control.
+    - Fixed infinite loop bug in comm_write().
+    - Added user->optbuf and introduced comm_telopt.
+    - Added telnet_fill() and made comm_send() and comm_write() use it.
+    - Completed telnet flow control.
+    - Simplification: Added comm_user_call() and made comm_receive() use it.
+    - Instead of errors in kfun connect(), call close() later on.
+    - Checked for destructed user object after receive_message() and friends.
+    - Added comm_add_goahead() and made callers of comm_flush() use it.
+      comm_flush() no longer has a "prompt" parameter.
+    - Bugfix: Calling kfun connect() from port->connection() while
+      number of users is at maximum will no longer crash the driver.
+    - Made comm_confirm() be less eager to push and pop error contexts.
+ 
+    This package is now complete.  Any further changes will be to fix bugs
+    or to track new DGD versions.
+ 
+ Maintenance of package now done by by Erwin Harte.
+ 
+    - Work-around for Linux-bug with non-blocking connections.
+    - Fixed possible crasher in call to connection().
+    - Prevented open_port() or connect() from having any effect if done from
+      within close() call in the kfun destruct_object().
+    - Added netpackage-specific version of block_input() that also works on
+      port-objects.
+    - Added validity checks for the port-number values of open_port() and
+      connect() and the ipnumber string of connect().
+    - Changed the kernel-lib to work with the network-package.
+ 
+ Release 11.0:
+    - Bumped release one up to avoid confusion with previous net-package
+      released for DGD 1.1.61.  Let's make this symbolical for an increased
+      effort in improving/maintaining the package.
+    - Fixed open_port() and connect() kfun prototypes.
+    - Changed documentation of both functions to show more clearly how they
+      should be used.
+ 
+ Release 11.1
+    - The ipnumber checking in connect() kfun was incorrect.
+    - Added port and ipnumber checks for send_datagram().
+ 
+ Release 11.2
+    - Modified the send_datagram calls in the kernel lib to work with the
+      net-package's version of the same function.
+    - Modified package to work with version 1.1.91 of DGD.
diff -crN dgd-net/src/Makefile dgd+net/src/Makefile
*** dgd-net/src/Makefile	Thu Sep 21 12:06:55 2000
--- dgd+net/src/Makefile	Thu Sep 21 12:06:23 2000
***************
*** 14,20 ****
  BIN=	../bin
  
  OBJ=	alloc.o error.o hash.o swap.o str.o array.o object.o data.o path.o \
! 	editor.o comm.o call_out.o interpret.o config.o dgd.o
  EDOBJ=	alloc.o error.o
  LEXOBJ=	alloc.o hash.o
  COMPOBJ=alloc.o error.o hash.o path.o str.o array.o object.o data.o \
--- 14,20 ----
  BIN=	../bin
  
  OBJ=	alloc.o error.o hash.o swap.o str.o array.o object.o data.o path.o \
! 	editor.o comm.net.o call_out.o interpret.o config.o dgd.o
  EDOBJ=	alloc.o error.o
  LEXOBJ=	alloc.o hash.o
  COMPOBJ=alloc.o error.o hash.o path.o str.o array.o object.o data.o \
***************
*** 83,94 ****
  interpret.o: kfun/table.h
  
  $(OBJ):	dgd.h config.h host.h alloc.h error.h
! error.o str.o array.o object.o data.o path.o comm.o: str.h array.h object.h
  editor.o call_out.o interpret.o config.o dgd.o: str.h array.h object.h
  array.o call_out.o interpret.o path.o config.o dgd.o: xfloat.h
! error.o array.o object.o data.o path.o editor.o comm.o call_out.o: interpret.h
  interpret.o config.o dgd.o: interpret.h
! str.o array.o object.o data.o path.o comm.o call_out.o interpret.o: data.h
  config.o dgd.o: data.h
  path.o config.o: path.h
  hash.o str.o: hash.h
--- 83,94 ----
  interpret.o: kfun/table.h
  
  $(OBJ):	dgd.h config.h host.h alloc.h error.h
! error.o str.o array.o object.o data.o path.o comm.net.o: str.h array.h object.h
  editor.o call_out.o interpret.o config.o dgd.o: str.h array.h object.h
  array.o call_out.o interpret.o path.o config.o dgd.o: xfloat.h
! error.o array.o object.o data.o path.o editor.o comm.net.o call_out.o: interpret.h
  interpret.o config.o dgd.o: interpret.h
! str.o array.o object.o data.o path.o comm.net.o call_out.o interpret.o: data.h
  config.o dgd.o: data.h
  path.o config.o: path.h
  hash.o str.o: hash.h
diff -crN dgd-net/src/comm.net.c dgd+net/src/comm.net.c
*** dgd-net/src/comm.net.c	Thu Jan  1 10:00:00 1970
--- dgd+net/src/comm.net.c	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,1292 ----
+ # include "host/telnet.h"
+ 
+ # include "dgd.h"
+ # include "str.h"
+ # include "array.h"
+ # include "object.h"
+ # include "interpret.h"
+ # include "data.h"
+ # include "comm.net.h"
+ 
+ typedef struct _user_ {
+     object *obj;		/* associated object */
+     connection *conn;		/* connection */
+     int inbufsz;		/* bytes in input buffer */
+     int outbufsz;		/* bytes in output buffer */
+     int optbufsz;               /* bytes in option buffer */
+     int extrabufsz;		/* bytes in extra buffer */
+     int newlines;		/* telnet: # of newlines in input buffer */
+     short flags;		/* connection flags */
+     char tflags;                /* telnet negotiation flags */
+     char state;			/* telnet state */
+     char *inbuf;		/* input buffer */
+     char *outbuf;		/* output buffer */
+     char *optbuf;               /* buffer for telnet options */
+     char *extrabuf;		/* buffer to hold overflow from outbuf */
+     char *extrabufp;		/* points to start of data in extrabuf */
+ } user;
+ 
+ /* flags */
+ # define CF_TELNET	0x0001	/* telnet connection */
+ # define CF_PORT	0x0002	/* port connection */
+ # define CF_DATAGRAM	0x0004	/* datagram connection */
+ # define CF_CONNECTING	0x0008	/* connection being initiated */
+ # define CF_WAITING	0x0010	/* waiting for write select */
+ # define CF_CONFIRM     0x0020  /* remember to call message_done() */
+ # define CF_ERROR	0x0040	/* connection no longer usable */
+ # define CF_EAGER	0x0080	/* object was not done with previous input */
+ # define CF_BLOCKED     0x0100  /* do not handle incoming data/connections */
+ 
+ /* tflags */
+ # define TF_FAKEECHO	0x01	/* server is pretending to echo input */
+ # define TF_GA		0x02	/* send GA after prompt */
+ # define TF_SEENCR	0x04	/* just seen a CR */
+ # define TF_LINEMODE	0x08	/* confirmed LINEMODE */
+ 
+ /* state */
+ # define TS_DATA	0
+ # define TS_IAC		1
+ # define TS_DO		2
+ # define TS_DONT	3
+ # define TS_WILL	4
+ # define TS_WONT	5
+ # define TS_SB		6
+ # define TS_SE		7
+ 
+ static user **users;		/* array of users */
+ static int maxusers;		/* max # of users */
+ static int nusers;		/* # of users */
+ static int nports;		/* # of ports */
+ static int limusers;		/* highest used user index + 1 */
+ static int newlines;		/* # of newlines in all input buffers */
+ static int waiting;		/* # of waiting users */
+ static int neager;		/* # of eager users */
+ static int callbacks;           /* true if comm_confirm() has work to do */
+ static int flush;		/* true if comm_flush() should flush */
+ static object *this_user;	/* current user */
+ 
+ /*
+  * NAME:	comm->init()
+  * DESCRIPTION:	initialize communications
+  */
+ void comm_init(cf_users)
+ int cf_users;
+ {
+     register int i;
+     register user **usr;
+ 
+     conn_init(cf_users);
+     users = ALLOC(user*, maxusers = cf_users);
+     for (i = cf_users, usr = users; i > 0; --i, usr++) {
+ 	*usr = (user *) NULL;
+     }
+ }
+ 
+ /*
+  * NAME:	comm->finish()
+  * DESCRIPTION:	terminate connections
+  */
+ void comm_finish()
+ {
+     comm_flush();
+     conn_finish();
+ }
+ 
+ /*
+  * NAME:	comm->wait()
+  * DESCRIPTION:	mark or unmark a user as waiting for write select
+  */
+ static void comm_wait(usr, wait)
+ user *usr;
+ int wait;
+ {
+     if (wait) {
+ 	if (!(usr->flags & CF_WAITING)) {
+ 	    usr->flags |= CF_WAITING;
+ 	    waiting++;
+ 	    conn_wait(usr->conn, wait);
+ 	}
+     } else {
+ 	if (usr->flags & CF_WAITING) {
+ 	    usr->flags &= ~CF_WAITING;
+ 	    waiting--;
+ 	    conn_wait(usr->conn, wait);
+ 	}
+     }
+ }
+ 
+ /*
+  * NAME:	comm->new()
+  * DESCRIPTION:	allocate and init a new user struct (assumes nusers < maxusers)
+  */
+ static void comm_new(obj, conn, flags)
+ object *obj;
+ connection *conn;
+ int flags;
+ {
+     register user *usr;
+     register int n, size;
+ 
+     if (obj->flags & (O_USER | O_EDITOR)) {
+ 	conn_del(conn);
+ 	error("User object is already used for user or editor");
+     }
+ 
+     if (flags & CF_PORT) {
+ 	size = sizeof(user);
+     } else if (flags & CF_TELNET) {
+ 	size = sizeof(user) + INBUF_SIZE + OUTBUF_SIZE + OPTBUF_SIZE;
+     } else {
+ 	size = sizeof(user) + OUTBUF_SIZE;
+     }
+ 
+     for (n = 0; users[n] != (user *) NULL; n++) ;
+     m_static();
+     usr = users[n] = (user *)ALLOC(char, size);
+     m_dynamic();
+     usr->obj = obj;
+     obj->flags |= O_USER;
+     obj->etabi = n;
+     usr->inbufsz = 0;
+     usr->outbufsz = 0;
+     usr->optbufsz = 0;
+     usr->extrabufsz = 0;
+     usr->newlines = 0;
+     usr->conn = conn;
+     usr->flags = flags;
+     usr->tflags = 0;
+     usr->extrabuf = (char *) NULL;
+     usr->extrabufp = (char *) NULL;
+     if (flags & CF_PORT) {
+ 	usr->inbuf = (char *) NULL;
+ 	usr->outbuf = (char *) NULL;
+ 	nports++;
+     } else if (flags & CF_TELNET) {
+ 	usr->inbuf = (char *) usr + sizeof(user);
+ 	usr->outbuf = usr->inbuf + INBUF_SIZE;
+ 	usr->optbuf = usr->outbuf + OUTBUF_SIZE;
+ 	usr->state = TS_DATA;
+     } else {
+ 	usr->inbuf = (char *) NULL;
+ 	usr->outbuf = (char *) usr + sizeof(user);
+     }
+     nusers++;
+     if (n + 1 > limusers) {
+ 	limusers = n + 1;
+     }
+ }
+ 
+ /*
+  * NAME:	comm->del()
+  * DESCRIPTION:	delete a connection
+  */
+ static void comm_del(f, usr, force)
+ register frame *f;
+ register user **usr;
+ bool force;
+ {
+     object *obj, *olduser;
+ 
+     comm_wait(*usr, 0);
+     conn_del((*usr)->conn);
+     if (!((*usr)->flags & CF_BLOCKED)) {
+         if ((*usr)->flags & CF_TELNET) {
+             newlines -= (*usr)->newlines;
+         }
+         if ((*usr)->flags & CF_EAGER) {
+             neager--;
+         }
+     }
+     nports -= ((*usr)->flags & CF_PORT) != 0;
+     obj = (*usr)->obj;
+     obj->flags &= ~O_USER;
+     if ((*usr)->extrabuf) {
+ 	FREE((*usr)->extrabuf);
+     }
+     FREE(*usr);
+     *usr = (user *) NULL;
+     --nusers;
+     while (limusers > 0 && users[limusers-1] == (user *) NULL) {
+ 	limusers--;
+     }
+ 
+     olduser = this_user;
+     this_user = obj;
+     if (ec_push((ec_ftn) NULL)) {
+ 	if (obj == olduser) {
+ 	    this_user = (object *) NULL;
+ 	} else {
+ 	    this_user = olduser;
+ 	}
+ 	error((char *) NULL);
+     } else {
+ 	(--f->sp)->type = T_INT;
+ 	f->sp->u.number = force;
+ 	if (i_call(f, obj, "close", 5, TRUE, 1)) {
+ 	    i_del_value(f->sp++);
+ 	}
+ 	if (obj == olduser) {
+ 	    this_user = (object *) NULL;
+ 	} else {
+ 	    this_user = olduser;
+ 	}
+ 	ec_pop();
+     }
+ }
+ 
+ /*
+  * NAME:	comm->listen()
+  * DESCRIPTION:	have an object listen to a port
+  */
+ void comm_listen(f, obj, port, protocol)
+ frame *f;
+ object *obj;
+ Int port;
+ int protocol;
+ {
+     register connection *conn;
+     int flags;
+     object *olduser;
+ 
+     if (nusers >= maxusers)
+ 	error("Max number of connection/port objects exceeded");
+ 
+     if (protocol == PRC_TELNET) {
+ 	flags = CF_TELNET | CF_PORT;
+ 	protocol = PRC_TCP;
+     } else if (protocol == PRC_UDP) {
+ 	flags = CF_DATAGRAM | CF_PORT;
+     } else {
+ 	flags = CF_PORT;
+     }
+     conn = conn_listen(port, protocol);
+     if (conn != (connection *) NULL) {
+ 	comm_new(obj, conn, flags);
+     } else {
+         error("Error opening port");
+     }
+     olduser = this_user;
+     this_user = obj;
+     (--f->sp)->type = T_INT;
+     f->sp->u.number = conn_port(conn);
+     if (i_call(f, this_user, "open", 4, TRUE, 1)) {
+         i_del_value(f->sp++);
+     }
+     this_user = olduser;
+ }
+ 
+ /*
+  * NAME:	comm->connect()
+  * DESCRIPTION:	initiate a telnet or tcp connection
+  */
+ void comm_connect(obj, host, port, protocol)
+ object *obj;
+ char *host;
+ int port, protocol;
+ {
+     register connection *conn;
+     int flags;
+ 
+     if (nusers >= maxusers)
+ 	error("Max number of connection/port objects exceeded");
+ 
+     if (protocol == PRC_TELNET) {
+ 	flags = CF_TELNET | CF_CONNECTING;
+ 	protocol = PRC_TCP;
+     } else /* if (protocol == PRC_TCP) */ {
+ 	flags = CF_CONNECTING;
+     }
+     conn = conn_connect(host, port, protocol);
+     if (conn != (connection *) NULL) {
+ 	comm_new(obj, conn, flags);
+     } else {
+ 	error("Bad connect()");
+     }
+ }
+ 
+ /*
+  * NAME:	telnet_fill()
+  * DESCRIPTION:	process telnet output.
+  */
+ static Uint telnet_fill(inbuf, inbuflen, outbuf, outbuflen, outbufsize)
+ char *inbuf;
+ char *outbuf;
+ Uint inbuflen, outbuflen;
+ Uint *outbufsize;
+ {
+     register char *p, *q, *outbufend, *inbufend;
+ 
+     p = inbuf;
+     q = outbuf;
+     outbufend = outbuf + outbuflen;
+     inbufend = inbuf + inbuflen;
+     while (p < inbufend) {
+ 	if (UCHAR(*p) == IAC) {
+ 	    /* double the telnet IAC character */
+ 	    if (q >= outbufend - 1) {
+ 		break;
+ 	    }
+ 	    *q++ = IAC;
+ 	    *q++ = IAC;
+ 	    p++;
+ 	} else if (*p == LF) {
+ 	    /* insert CR before LF */
+ 	    if (q >= outbufend - 1) {
+ 		break;
+ 	    }
+ 	    *q++ = CR;
+ 	    *q++ = LF;
+ 	    p++;
+ 	} else if ((*p & 0x7f) < ' ' && *p != HT && *p != BEL && *p != BS) {
+ 	    /* illegal character */
+ 	    p++;
+ 	} else {
+ 	    if (q == outbufend) {
+ 		break;
+ 	    }
+ 	    *q++ = *p++;
+ 	}
+     }
+     *outbufsize += q - outbuf;
+     return p - inbuf;
+ }
+ 
+ /*
+  * NAME:	comm->write()
+  * DESCRIPTION:	try to write out the output buffers
+  */
+ static void comm_write(usr)
+ register user *usr;
+ {
+     register Int i;
+ 
+     if (usr->optbufsz > 0) {
+ 	i = conn_write(usr->conn, usr->optbuf, usr->optbufsz);
+ 	if (i < 0) {
+ 	    usr->flags |= CF_ERROR;
+ 	    callbacks = TRUE;
+ 	    return;
+ 	} else if ((usr->optbufsz -= i) > 0) {
+ 	    char *p, *q;
+ 	    int n;
+ 
+ 	    p = usr->optbuf;
+ 	    q = p + i;
+ 	    n = usr->optbufsz;
+ 	    while (n--) {
+ 		*p++ = *q++;
+ 	    }
+ 	    comm_wait(usr, TRUE);
+ 	    return;
+ 	}
+     }
+ 
+     while (usr->outbufsz > 0) {
+ 	i = conn_write(usr->conn, usr->outbuf, usr->outbufsz);
+ 	if (i < 0) {
+ 	    usr->flags |= CF_ERROR;
+ 	    callbacks = TRUE;
+ 	    return;
+ 	} else if ((usr->outbufsz -= i) > 0) {
+ 	    char *p, *q;
+ 	    int n;
+ 
+ 	    p = usr->outbuf;
+ 	    q = p + i;
+ 	    n = usr->outbufsz;
+ 	    while (n--) {
+ 		*p++ = *q++;
+ 	    }
+ 	    comm_wait(usr, TRUE);
+ 	    return;
+ 	}
+ 	if ((usr->flags & CF_TELNET) && usr->extrabufsz > 0) {
+ 	    /* Refill outbuf */
+ 	    i = telnet_fill(usr->extrabufp, usr->extrabufsz,
+ 			    usr->outbuf, OUTBUF_SIZE, &(usr->outbufsz));
+ 	    if (i == usr->extrabufsz) {
+ 		FREE(usr->extrabuf);
+ 		usr->extrabuf = (char *)NULL;
+ 		usr->extrabufp = (char *)NULL;
+ 		usr->extrabufsz = 0;
+ 	    } else {
+ 		usr->extrabufp += i;
+ 		usr->extrabufsz -= i;
+ 	    }
+ 	    /* outbufsz can be 0 here if extrabuf consisted solely of
+              * characters skipped by telnet_fill().  
+              */
+ 	    if (usr->outbufsz == 0) {
+ 		comm_wait(usr, FALSE);
+ 		return;
+ 	    }
+ 	}
+     }
+ 
+     /* Only binary connections will reach this statement. */
+     if (usr->extrabufsz > 0) {
+ 	i = conn_write(usr->conn, usr->extrabufp, usr->extrabufsz);
+ 	if (i < 0) {
+ 	    usr->flags |= CF_ERROR;
+ 	    callbacks = TRUE;
+ 	    return;
+ 	} else if (i < usr->extrabufsz) {
+ 	    usr->extrabufp += i;
+ 	    usr->extrabufsz -= i;
+ 	    comm_wait(usr, TRUE);
+ 	    return;
+ 	} else {
+ 	    FREE(usr->extrabuf);
+ 	    usr->extrabuf = (char *)NULL;
+ 	    usr->extrabufp = (char *)NULL;
+ 	    usr->extrabufsz = 0;
+ 	}
+     }
+ 
+     comm_wait(usr, FALSE);
+ }
+ 
+ /*
+  * NAME:	comm->telopt()
+  * DESCRIPTION:	send a telnet option negotiation string
+  */
+ static void comm_telopt(usr, opt, len)
+ user *usr;
+ char *opt;
+ int len;
+ {
+     /* It is possible for options to be dropped if the connection is blocked,
+      * but I expect that to be rare. */
+ 
+     if (usr->optbufsz + len > OPTBUF_SIZE) {
+ 	comm_write(usr);
+     }
+     if (usr->optbufsz + len <= OPTBUF_SIZE) {
+ 	memcpy(usr->optbuf + usr->optbufsz, opt, len);
+ 	usr->optbufsz += len;
+ 	if (!(usr->flags & CF_WAITING)) {
+ 	    comm_write(usr);
+ 	}
+     }
+ }
+ 
+ /*
+  * NAME:	comm->check()
+  * DESCRIPTION:	check if previous message has been sent
+  */
+ int comm_check(obj)
+ object *obj;
+ {
+     register user *usr;
+ 
+     usr = users[UCHAR(obj->etabi)];
+     if (usr->flags & CF_PORT) {
+ 	error("Object is not a stream connection");
+     }
+     if (usr->flags & CF_CONNECTING) {
+ 	error("Connection is not yet ready");
+     }
+     if (usr->optbufsz > 0 || usr->outbufsz > 0 || usr->extrabufsz > 0 ||
+ 	(usr->flags & CF_ERROR)) {
+ 	callbacks = TRUE;
+ 	usr->flags |= CF_CONFIRM;
+ 	return 1;
+     }
+     return 0;
+ }
+     
+ /*
+  * NAME:	comm->send()
+  * DESCRIPTION:	send a message to a user
+  */
+ int comm_send(obj, str)
+ object *obj;
+ string *str;
+ {
+     register user *usr;
+     register char *p;
+     register Uint len, size;
+ 
+     usr = users[UCHAR(obj->etabi)];
+     if (usr->flags & CF_PORT) {
+         error("Object is not a stream connection");
+     }
+     if (usr->flags & CF_CONNECTING) {
+ 	error("Connection is not yet ready");
+     }
+     if (usr->extrabuf) {
+ 	/*
+ 	 * Discard overflow from previous message.
+          */
+ 	FREE(usr->extrabuf);
+ 	usr->extrabuf = (char *) NULL;
+ 	usr->extrabufp = (char *) NULL;
+ 	usr->extrabufsz = 0;
+ 	comm_wait(usr, 0);
+     }
+     p = str->text;
+     len = str->len;
+     if (len == 0) {
+ 	return 0;
+     }
+     if (usr->flags & CF_TELNET) {
+ 	size = telnet_fill(p, len, usr->outbuf + usr->outbufsz,
+ 			   OUTBUF_SIZE - usr->outbufsz, &(usr->outbufsz));
+ 	if (len == size) {
+ 	    flush = TRUE;
+ 	    return 0;
+ 	}
+ 	len -= size;
+ 	p += size;
+     } else {
+ 	if (usr->outbufsz + len <= OUTBUF_SIZE) {
+ 	    memcpy(usr->outbuf + usr->outbufsz, p, len);
+ 	    usr->outbufsz += len;
+ 	    flush = TRUE;
+ 	    return 0;
+ 	}
+     }
+ 
+     if (len < 2048) {
+ 	size = 2048;
+     } else if (len < 4096) {
+ 	size = 4096;
+     } else {
+ 	size = (len & 0xE000) + 8192;  /* Round up to multiple of 8192 */
+     }
+ 
+     m_static();
+     usr->extrabuf = ALLOC(char, size);
+     m_dynamic();
+     usr->extrabufp = usr->extrabuf;
+     usr->extrabufsz = len;
+     memcpy(usr->extrabuf, p, len);
+ 
+     comm_write(usr);
+     
+     if (usr->extrabufsz > 0 || (usr->flags & CF_ERROR)) {
+ 	callbacks = TRUE;
+ 	usr->flags |= CF_CONFIRM;
+ 	return 1;
+     }
+     return 0;
+ }
+ 
+ /*
+  * NAME:	comm->sendto()
+  * DESCRIPTION:	send a datagram
+  */
+ void comm_sendto(obj, data, host, port)
+ object *obj;
+ string *data;
+ char *host;
+ int port;
+ {
+     register user *usr;
+ 
+     usr = users[UCHAR(obj->etabi)];
+     if (!(usr->flags & CF_DATAGRAM)) {
+ 	error("Object is not a datagram object");
+     }
+     conn_sendto(usr->conn, data->text, data->len, host, port);
+ }
+ 
+ 
+ /*
+  * NAME:	comm->echo()
+  * DESCRIPTION:	turn on/off input echoing for a user
+  */
+ void comm_echo(obj, echo)
+ object *obj;
+ int echo;
+ {
+     register user *usr;
+     char buf[3];
+ 
+     usr = users[UCHAR(obj->etabi)];
+     if (usr->flags & CF_CONNECTING)
+ 	error("Connection is not yet ready");
+     if ((usr->flags & CF_TELNET) && !echo != (usr->tflags & TF_FAKEECHO)) {
+ 	buf[0] = IAC;
+ 	buf[1] = (echo) ? WONT : WILL;
+ 	buf[2] = TELOPT_ECHO;
+ 	comm_telopt(usr, buf, 3);
+ 	usr->tflags ^= TF_FAKEECHO;
+     }
+ }
+ 
+ /*
+  * NAME:	comm->add_goahead()
+  * DESCRIPTION:	add an IAC GA sequence to the current user's buffer.
+  */
+ void comm_add_goahead()
+ {
+     register user *usr;
+     register int n, i, size;
+ 
+     for (n = 0, i = nusers; i > 0; n++) {
+ 	usr = users[n];
+ 	if (usr != (user *) NULL) {
+ 	    --i;
+ 	    if ((usr->flags & CF_TELNET) && (usr->tflags & TF_GA) &&
+ 		(((size=usr->outbufsz) > 0 && usr->outbuf[size - 1] != LF &&
+ 		  !usr->extrabuf) ||
+ 		 usr->obj == this_user)) {
+ 		/*
+ 		 * Append "go ahead" to indicate that the prompt
+ 		 * has been sent.  Don't try too hard if it won't
+ 		 * fit. 
+ 		 */
+ 		if (!(usr->flags & CF_WAITING) &&
+ 		    (size >= OUTBUF_SIZE - 2 || usr->extrabuf)) {
+ 		    comm_write(usr);
+ 		}
+ 		if (size < OUTBUF_SIZE - 2 && !usr->extrabuf) {
+ 		    usr->outbuf[size++] = IAC;
+ 		    usr->outbuf[size++] = GA;
+ 		    usr->outbufsz += 2;
+ 		}
+ 	    }
+ 	}
+     }
+ }
+ 
+ /*
+  * NAME:	comm->flush()
+  * DESCRIPTION:	flush output to all users
+  */
+ void comm_flush()
+ {
+     register user *usr;
+     register int i, n;
+ 
+     if (!flush)
+ 	return;
+     flush = FALSE;
+ 
+     for (n = 0, i = nusers; i > 0; n++) {
+ 	usr = users[n];
+ 	if (usr != (user *) NULL) {
+ 	    --i;
+ 	    if (usr->outbufsz > 0 && !(usr->flags & CF_WAITING)) {
+ 		comm_write(usr);
+ 	    }
+ 	}
+     }
+     this_user = (object *) NULL;
+ }
+ 
+ /*
+  * NAME:	comm->confirm()
+  * DESCRIPTION:	call close() and message_done() when appropriate
+  */
+ void comm_confirm(f)
+ frame *f;
+ {
+     static int n;
+     register user *usr;
+ 
+     if (!callbacks)
+ 	return;
+     callbacks = FALSE;
+ 
+     for (n = 0; n < limusers; n++) {
+ 	usr = users[n];
+ 	if (usr == (user *) NULL) {
+ 	    continue;
+ 	}
+ 	if (usr->flags & CF_ERROR) {
+ 	    if (!ec_push((ec_ftn) NULL)) {
+ 		comm_del(f, &users[n], FALSE);
+ 		ec_pop();
+ 	    }
+ 	    endthread();
+ 	} else if ((usr->flags & (CF_CONFIRM|CF_WAITING)) == CF_CONFIRM) {
+ 	    if (!ec_push((ec_ftn) NULL)) {
+ 		usr->flags &= ~CF_CONFIRM;
+ 		this_user = usr->obj;
+ 		if (!i_call(f, this_user, "message_done", 12, TRUE, 0)) {
+ 		    ec_pop();
+ 		    continue;
+ 		}
+ 		i_del_value(f->sp++);
+ 		ec_pop();
+ 	    }
+ 	    endthread();
+ 	    comm_add_goahead();
+ 	    comm_flush();
+ 	}
+     }
+ }
+ 	    
+ /*
+  * NAME:	comm->user_call()
+  * DESCRIPTION:	call a receive function in a user object
+  */
+ static void comm_user_call(f, usr, func, args, prompt)
+ register frame *f;
+ register user *usr;
+ char *func;
+ int args;
+ bool prompt;
+ {
+     this_user = usr->obj;
+     if (i_call(f, this_user, func, strlen(func), TRUE, args)) {
+ 	if (this_user != (object *) NULL
+ 	    && f->sp->type == T_INT && f->sp->u.number != 0) {
+ 	    usr->flags |= CF_EAGER;
+             if (!(usr->flags & CF_BLOCKED)) {
+                 neager++;
+             }
+ 	}
+ 	i_del_value(f->sp++);
+ 	if (prompt)
+ 	    comm_add_goahead();
+ 	comm_flush();
+ 	endthread();
+     }
+ }
+ 
+ /*
+  * NAME:	comm->receive()
+  * DESCRIPTION:	receive a message from a user
+  */
+ void comm_receive(f, timeout, mtime)
+ register frame *f;
+ Uint timeout;
+ unsigned int mtime;
+ {
+     static int lastuser;
+     connection *conn;
+     object *o;
+     register int n;
+     register char *p, *q;
+ 
+     this_user = (object *) NULL;
+     if (newlines != 0 || neager != 0) {
+ 	timeout = mtime = 0;
+     }
+     n = conn_select(timeout, mtime);
+     if (n > 0 || newlines > 0 || neager > 0) {
+ 	static char ayt[] =       { CR, LF, '[', 'Y', 'e', 's', ']', CR, LF };
+ 	static char mode_edit[] = { IAC, SB, TELOPT_LINEMODE, LM_MODE,
+ 				    MODE_EDIT, IAC, SE };
+ 	register user *usr;
+ 	char buf[TMPBUF_SIZE];
+ 	Int size;
+ 	register int cnt;
+ 	register int eager;
+ 
+ 	for (n = 0, cnt = waiting; cnt > 0; n++) {
+ 	    usr = users[n];
+ 	    if (usr != (user *) NULL && (usr->flags & CF_WAITING)) {
+ 		cnt--;
+ 		if (conn_writable(usr->conn)) {
+ 		    comm_write(usr);
+ 		    callbacks |= (usr->flags & (CF_WAITING|CF_CONFIRM))
+ 				  == CF_CONFIRM;
+ 		}
+ 	    }
+ 	}
+ 
+ 	if (lastuser >= limusers) {
+ 	    lastuser = 0;
+ 	}
+ 	n = lastuser;
+ 	for (;;) {
+ 	    n = (n + 1) % limusers;
+ 	    usr = users[n];
+ 	    if (usr != (user *) NULL && !(usr->flags & CF_BLOCKED)) {
+ 		if ((eager = usr->flags & CF_EAGER)) {
+ 		    neager--;
+ 		    usr->flags &= ~CF_EAGER;
+ 		}
+ 		    
+ 		if (usr->flags & CF_CONNECTING) {
+ 		    int r;
+ 
+ 		    r = conn_connected(usr->conn);
+ 		    if (r == 1) {
+ 			usr->flags &= ~CF_CONNECTING;
+ 			this_user = usr->obj;
+ 			if (i_call(f, this_user, "open", 4, TRUE, 0)) {
+ 			    i_del_value(f->sp++);
+ 			    comm_add_goahead();
+ 			    comm_flush();
+ 			    endthread();
+ 			}
+ 		    } else if (r == -1) {
+ 			comm_del(f, &users[n], FALSE);
+ 			endthread();
+ 		    }
+ 		} else if (usr->flags & CF_DATAGRAM) {
+ 		    size = conn_recvfrom(usr->conn, buf, TMPBUF_SIZE);
+ 		    if (size < 0) {
+ 			comm_del(f, &users[n], FALSE);
+ 			endthread();
+ 		    } else if (size > 0) {
+ 			lastuser = n;
+ 			(--f->sp)->type = T_STRING;
+ 			str_ref(f->sp->u.string = str_new(buf, (long) size));
+ 			(--f->sp)->type = T_STRING;
+ 			str_ref(f->sp->u.string = conn_ipnum(usr->conn));
+ 			(--f->sp)->type = T_INT;
+ 			f->sp->u.number = conn_port(usr->conn);
+ 			comm_user_call(f, usr, "receive_datagram", 3, FALSE);
+ 			return;
+ 		    } else if (eager) {
+ 			lastuser = n;
+ 			this_user = usr->obj;
+ 			comm_user_call(f, usr, "process_datagram", 0, FALSE);
+ 			return;
+ 		    }
+ 		} else if (usr->flags & CF_PORT) {
+ 		    if (nusers < maxusers) {
+ 			conn = conn_accept(usr->conn);
+ 			if (conn != (connection *) NULL) {
+ 			    if (ec_push((ec_ftn) NULL)) {
+ 				conn_del(conn);
+ 				error((char *) NULL);   /* pass on error */
+ 			    }
+ 			    (--f->sp)->type = T_STRING;
+ 			    str_ref(f->sp->u.string = conn_ipnum(conn));
+ 			    (--f->sp)->type = T_INT;
+ 			    f->sp->u.number = conn_port(conn);
+ 			    /* Problem: during port->connection(),
+                              * nusers does not match number of allocated
+                              * connection structs. */
+ 			    this_user = usr->obj;
+ 			    if (!i_call(f, this_user, "connection", 10, TRUE, 2)) {
+ 			        error("missing connection() function");
+ 			    }
+ 			    ec_pop();
+ 			    if (f->sp->type != T_OBJECT) {
+ 				conn_del(conn);
+ 				error("connection() did not return an object");
+ 			    }
+ 			    o = &otable[f->sp->oindex];
+ 			    f->sp++;
+ 			    endthread();
+ 			    comm_new(o, conn, usr->flags & ~CF_PORT);
+ 			    this_user = o;
+ 			    if (i_call(f, this_user, "open", 4, TRUE, 0)) {
+ 				i_del_value(f->sp++);
+ 				endthread();
+ 			    }
+ 			    comm_add_goahead();
+ 			    comm_flush();
+ 			}
+ 		    }
+ 		} else if (usr->flags & CF_TELNET) {
+ 		    p = usr->inbuf + usr->inbufsz;
+ 		    size = conn_read(usr->conn, p, INBUF_SIZE - usr->inbufsz);
+ 		    if (size < 0) {
+ 			comm_del(f, &users[n], FALSE);
+ 			endthread();
+ 			continue;
+ 		    } else if (size > 0) {
+ 			register int state, tflags, nls, seencr;
+ 			char optbuf[20];
+ 
+ 			tflags = usr->tflags & ~TF_SEENCR;
+ 			seencr = usr->tflags & TF_SEENCR;
+ 			state = usr->state;
+ 			nls = usr->newlines;
+ 			q = p;
+ 			while (size > 0) {
+ 			    switch (state) {
+ 			    case TS_DATA:
+ 				switch (UCHAR(*p)) {
+ 				case 0:
+ 				    seencr = 0;
+ 				    break;
+ 
+ 				case IAC:
+ 				    state = TS_IAC;
+ 				    break;
+ 
+ 				case BS:
+ 			        case 0x7f:
+ 				    if (q != usr->inbuf && q[-1] != LF) {
+ 					--q;
+ 				    }
+ 				    seencr = 0;
+ 				    break;
+ 
+ 			        case CR:
+ 				    nls++;
+ 				    newlines++;
+ 				    *q++ = LF;
+ 				    seencr = TF_SEENCR;
+ 				    break;
+ 
+ 				case LF:
+ 				    if (seencr) {
+ 					seencr = 0;
+ 				    } else {
+ 					nls++;
+ 					newlines++;
+ 					*q++ = *p;
+ 				    }
+ 				    break;
+ 
+ 			        default:
+ 				    *q++ = *p;
+ 				    seencr = 0;
+ 				    break;
+ 			        }
+ 				break;
+ 
+ 			    case TS_IAC:
+ 				switch (UCHAR(*p)) {
+ 				case IAC:
+ 				    *q++ = *p;
+ 				    state = TS_DATA;
+ 				    break;
+ 
+ 			        case DO:
+ 				    state = TS_DO;
+ 				    break;
+ 
+ 			        case DONT:
+ 				    state = TS_DONT;
+ 				    break;
+ 
+ 			        case WILL:
+ 				    state = TS_WILL;
+ 				    break;
+ 
+ 				case WONT:
+ 				    state = TS_WONT;
+ 				    break;
+ 
+ 				case SB:
+ 				    state = TS_SB;
+ 				    break;
+ 
+ 				case AYT:
+ 				    comm_telopt(usr, ayt, 9);
+ 				    state = TS_DATA;
+ 				    break;
+ 
+ 				case EC:
+ 				    if (q != usr->inbuf && q[-1] != LF) {
+ 					--q;
+ 				    }
+ 				    state = TS_DATA;
+ 				    break;
+ 
+ 				case EL:
+ 				    while (q != usr->inbuf && q[-1] !=LF) {
+ 					--q;
+ 				    }
+ 				    state = TS_DATA;
+ 				    break;
+ 
+ 				case IP:
+ 				case DM:
+ 				    q = usr->inbuf;
+ 				    newlines -= nls;
+ 				    nls = 0;
+ 				    state = TS_DATA;
+ 				    break;
+ 
+ 				case BREAK:
+ 			        default:
+ 				    state = TS_DATA;
+ 				    break;
+ 				}
+ 				break;
+ 
+ 			    case TS_DO:
+ 				optbuf[0] = IAC;
+ 				optbuf[2] = UCHAR(*p);
+ 				switch(UCHAR(*p)) {
+ 				case TELOPT_SGA:
+ 				    if (tflags & TF_GA) {
+ 					tflags &= ~TF_GA;
+ 					optbuf[1] = WILL;
+ 					comm_telopt(usr, optbuf, 3);
+ 				    }
+ 				    break;
+ 
+ 				case TELOPT_ECHO:
+ 				    if (!(tflags & TF_FAKEECHO)) {
+ 					optbuf[1] = WONT;
+ 					comm_telopt(usr, optbuf, 3);
+ 				    }
+ 				    break;
+ 
+ 				default:
+ 				    optbuf[1] = WONT;
+ 				    comm_telopt(usr, optbuf, 3);
+ 				    break;
+ 				}
+ 				state = TS_DATA;
+ 				break;
+ 
+ 			    case TS_DONT:
+ 				if (UCHAR(*p) == TELOPT_SGA
+ 				    && !(tflags & TF_GA)) {
+ 				    tflags |= TF_GA;
+ 				    optbuf[0] = IAC;
+ 				    optbuf[1] = WONT;
+ 				    optbuf[2] = TELOPT_SGA;
+ 				    comm_telopt(usr, optbuf, 3);
+ 			        }
+ 				state = TS_DATA;
+ 				break;
+ 
+ 			    case TS_WILL:
+ 				optbuf[0] = IAC;
+ 				optbuf[2] = UCHAR(*p);
+ 				if (UCHAR(*p) == TELOPT_LINEMODE) {
+ 				    if (!(tflags & TF_LINEMODE)) {
+ 					tflags |= TF_LINEMODE;
+ 					optbuf[1] = DO;
+ 					comm_telopt(usr, optbuf, 3);
+ 				    }
+ 				    comm_telopt(usr, mode_edit, 7);
+ 				} else {
+ 				    optbuf[1] = DONT;
+ 				    comm_telopt(usr, optbuf, 3);
+ 				}
+ 				state = TS_DATA;
+ 				break;
+ 
+ 			    case TS_WONT:
+ 				if (UCHAR(*p) == TELOPT_LINEMODE) {
+ 				    tflags &= ~TF_LINEMODE;
+ 				    optbuf[0] = IAC;
+ 				    optbuf[1] = DONT;
+ 				    optbuf[2] = TELOPT_LINEMODE;
+ 				    comm_telopt(usr, optbuf, 3);
+ 				}
+ 				state = TS_DATA;
+ 				break;
+ 
+ 			    case TS_SB:
+ 				/* skip to the end */
+ 				if (UCHAR(*p) == IAC) {
+ 				    state = TS_SE;
+ 				}
+ 				break;
+ 
+ 			    case TS_SE:
+ 				if (UCHAR(*p) == SE) {
+ 				    state = TS_DATA;
+ 				} else if (UCHAR(*p) == IAC) {
+ 				    /* doubled IAC counts as data */
+ 				    state = TS_SB;
+ 				}
+ 				break;
+ 			    }
+ 			    p++;
+ 			    --size;
+ 			}
+ 			usr->tflags = tflags | seencr;
+ 			usr->state = state;
+ 			usr->newlines = nls;
+ 			usr->inbufsz = q - usr->inbuf;
+ 		    }
+ 		    if (usr->newlines > 0) {
+ 			p = (char *) memchr(usr->inbuf, LF, usr->inbufsz);
+ 			--newlines;
+ 			--(usr->newlines);
+ 			lastuser = n;
+ 			size = n = p - usr->inbuf;
+ 			if (size != 0) {
+ 			    memcpy(buf, usr->inbuf, size);
+ 			}
+ 			p++;   /* skip \n */
+ 			n++;
+ 			usr->inbufsz -= size + 1;
+ 			for (q = usr->inbuf, n = usr->inbufsz; n > 0; --n) {
+ 			    *q++ = *p++;
+ 			}
+ 			(--f->sp)->type = T_STRING;
+ 			str_ref(f->sp->u.string = str_new(buf, (long) size));
+ 			comm_user_call(f, usr, "receive_message", 1, TRUE);
+ 			return;
+ 		    } else if (usr->inbufsz == INBUF_SIZE) {
+ 			/*
+ 			 * input buffer full
+ 			 */
+ 			lastuser = n;
+ 			(--f->sp)->type = T_STRING;
+ 			str_ref(f->sp->u.string = 
+ 				str_new(usr->inbuf, (long) INBUF_SIZE));
+ 			usr->inbufsz = 0;
+ 			comm_user_call(f, usr, "receive_message", 1, TRUE);
+ 			return;
+ 		    } else if (eager) {
+ 			lastuser = n;
+ 			comm_user_call(f, usr, "process_message", 0, TRUE);
+ 			return;
+ 		    }
+ 		} else {
+ 		    /*
+ 		     * tcp (binary) connection
+ 		     */
+ 		    size = conn_read(usr->conn, buf, TMPBUF_SIZE);
+ 		    if (size < 0) {
+ 			comm_del(f, &users[n], FALSE);
+ 			endthread();
+ 		    } else if (size > 0) {
+ 			lastuser = n;
+ 			(--f->sp)->type = T_STRING;
+ 			str_ref(f->sp->u.string = str_new(buf, (long) size));
+ 			comm_user_call(f, usr, "receive_message", 1, FALSE);
+ 			return;
+ 		    } else if (eager) {
+ 			lastuser = n;
+ 			comm_user_call(f, usr, "process_message", 0, FALSE);
+ 			return;
+ 		    }
+ 		}
+ 	    }
+ 	    if (n == lastuser || lastuser >= limusers) {
+ 		return;
+ 	    }
+ 	}
+     }
+ }
+ 
+ /*
+  * NAME:	comm->ip_number()
+  * DESCRIPTION:	return the ip number of a user (as a string)
+  */
+ string *comm_ip_number(obj)
+ object *obj;
+ {
+     register user *usr;
+ 
+     usr = users[UCHAR(obj->etabi)];
+     if (usr->flags & CF_PORT) {
+ 	return (string *)NULL;
+     } else {
+ 	return conn_ipnum(usr->conn);
+     }
+ }
+ 
+ /*
+  * NAME:	comm->close()
+  * DESCRIPTION:	remove a user
+  */
+ void comm_close(f, obj)
+ frame *f;
+ object *obj;
+ {
+     register user **usr;
+ 
+     usr = &users[UCHAR(obj->etabi)];
+     if ((*usr)->outbufsz != 0) {
+ 	/*
+ 	 * flush last bit of output
+ 	 */
+ 	conn_write((*usr)->conn, (*usr)->outbuf, (*usr)->outbufsz);
+     }
+     comm_del(f, usr, TRUE);
+ }
+ 
+ /*
+  * NAME:	comm->user()
+  * DESCRIPTION:	return the current user
+  */
+ object *comm_user()
+ {
+     return this_user;
+ }
+ 
+ /*
+  * NAME:	comm->ports()
+  * DESCRIPTION:	return an array with all port objects
+  */
+ array *comm_ports(data)
+ dataspace *data;
+ {
+     array *a;
+     register int i;
+     register user **usr;
+     register value *v;
+ 
+     a = arr_new(data, (long) nports);
+     v = a->elts;
+     for (i = nports, usr = users; i > 0; usr++) {
+ 	if (*usr != (user *) NULL && ((*usr)->flags & CF_PORT)) {
+ 	    v->type = T_OBJECT;
+ 	    v->oindex = (*usr)->obj->index;
+ 	    v->u.objcnt = (*usr)->obj->count;
+ 	    v++;
+ 	    --i;
+ 	}
+     }
+     return a;
+ }
+ 
+ /*
+  * NAME:	comm->connections()
+  * DESCRIPTION:	return an array with all connection objects
+  */
+ array *comm_connections(data)
+ dataspace *data;
+ {
+     array *a;
+     register int i;
+     register user **usr;
+     register value *v;
+ 
+     a = arr_new(data, (long) (nusers - nports));
+     v = a->elts;
+     for (i = nusers - nports, usr = users; i > 0; usr++) {
+ 	if (*usr != (user *) NULL && !((*usr)->flags & CF_PORT)) {
+ 	    v->type = T_OBJECT;
+ 	    v->oindex = (*usr)->obj->index;
+ 	    v->u.objcnt = (*usr)->obj->count;
+ 	    v++;
+ 	    --i;
+ 	}
+     }
+     return a;
+ }
+ 
+ /*
+  * NAME:        comm->block()
+  * DESCRIPTION: suspend or release input from a connection or port
+  */
+ void comm_block(obj, flag)
+ object *obj;
+ int flag;
+ {
+     register user *usr;
+ 
+     usr = users[UCHAR(obj->etabi)];
+     if (flag) {
+         if (!(usr->flags & CF_BLOCKED)) {
+             conn_block(usr->conn, flag);
+             usr->flags |= CF_BLOCKED;
+             if (usr->flags & CF_EAGER) {
+                 neager--;
+             }
+             newlines -= usr->newlines;
+         }
+     } else {
+         if (usr->flags & CF_BLOCKED) {
+             conn_block(usr->conn, flag);
+             usr->flags &= ~CF_BLOCKED;
+             if (usr->flags & CF_EAGER) {
+                 neager++;
+             }
+             newlines += usr->newlines;
+         }
+     }
+ }
+ 
+ /*
+  * NAME:        comm->active()
+  * DESCRIPTION: return TRUE if there is any pending comm activity
+  */
+ bool comm_active()
+ {
+     return newlines != 0 || neager != 0 || conn_select(0, 0) > 0;
+ }
diff -crN dgd-net/src/comm.net.h dgd+net/src/comm.net.h
*** dgd-net/src/comm.net.h	Thu Jan  1 10:00:00 1970
--- dgd+net/src/comm.net.h	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,47 ----
+ # define INBUF_SIZE	2048
+ # define OUTBUF_SIZE	2048
+ # define TMPBUF_SIZE	8192
+ # define OPTBUF_SIZE    256
+ 
+ # define  PRC_TCP      1
+ # define  PRC_UDP      2
+ # define  PRC_TELNET   3
+ 
+ typedef struct _connection_ connection;
+ 
+ extern void	   conn_init	  P((int));
+ extern void	   conn_finish	  P((void));
+ extern connection *conn_listen	  P((int, int));
+ extern connection *conn_accept	  P((connection*));
+ extern connection *conn_connect	  P((char*, int, int));
+ extern int	   conn_connected P((connection*));
+ extern int	   conn_writable  P((connection*));
+ extern void	   conn_del	  P((connection*));
+ extern void	   conn_wait	  P((connection*, int));
+ extern void        conn_block     P((connection*, int));
+ extern int	   conn_select	  P((Uint, unsigned int));
+ extern int	   conn_recvfrom  P((connection*, char*, int));
+ extern int	   conn_read	  P((connection*, char*, int));
+ extern void	   conn_sendto	  P((connection*, char*, int, char*, int)); 
+ extern int	   conn_write	  P((connection*, char*, int));
+ extern string	  *conn_ipnum	  P((connection*));
+ extern Int	   conn_port	  P((connection*));
+ 
+ extern void	comm_init		P((int));
+ extern void	comm_finish		P((void));
+ extern void	comm_listen		P((frame*, object*, int, int));
+ extern void	comm_connect		P((object*, char*, int, int));
+ extern int	comm_check		P((object*));
+ extern int	comm_send		P((object*, string*));
+ extern void	comm_sendto		P((object*, string*, char*, int));
+ extern void	comm_echo		P((object*, int));
+ extern void	comm_add_goahead	P((void));
+ extern void	comm_flush		P((void));
+ extern void     cmm_block               P((object*, int));
+ extern void	comm_receive		P((frame*, Uint, unsigned int));
+ extern void	comm_confirm		P((frame*));
+ extern string  *comm_ip_number		P((object*));
+ extern void	comm_close		P((frame*, object*));
+ extern object  *comm_user		P((void));
+ extern array   *comm_connections	P((dataspace*));
+ extern array   *comm_ports		P((dataspace*));
diff -crN dgd-net/src/comp/Makefile dgd+net/src/comp/Makefile
*** dgd-net/src/comp/Makefile	Thu Sep 21 12:06:51 2000
--- dgd+net/src/comp/Makefile	Thu Sep 21 12:06:23 2000
***************
*** 48,54 ****
  $(OBJ) comp.o: ../str.h ../array.h ../object.h ../xfloat.h ../interpret.h
  control.o optimize.o codegeni.o codegenc.o compile.o csupport.o: ../data.h
  control.o comp.o: ../hash.h ../path.h
! comp.o: ../data.h ../swap.h ../comm.h ../editor.h ../call_out.h
  
  node.o parser.o compile.o: ../lex/macro.h ../lex/token.h
  parser.o compile.o: ../lex/ppcontrol.h
--- 48,54 ----
  $(OBJ) comp.o: ../str.h ../array.h ../object.h ../xfloat.h ../interpret.h
  control.o optimize.o codegeni.o codegenc.o compile.o csupport.o: ../data.h
  control.o comp.o: ../hash.h ../path.h
! comp.o: ../data.h ../swap.h ../comm.net.h ../editor.h ../call_out.h
  
  node.o parser.o compile.o: ../lex/macro.h ../lex/token.h
  parser.o compile.o: ../lex/ppcontrol.h
diff -crN dgd-net/src/comp/comp.c dgd+net/src/comp/comp.c
*** dgd-net/src/comp/comp.c	Thu Sep 21 12:07:00 2000
--- dgd+net/src/comp/comp.c	Thu Sep 21 12:06:23 2000
***************
*** 9,15 ****
  # include "path.h"
  # include "hash.h"
  # include "swap.h"
! # include "comm.h"
  # include "editor.h"
  # include "call_out.h"
  # include "node.h"
--- 9,15 ----
  # include "path.h"
  # include "hash.h"
  # include "swap.h"
! # include "comm.net.h"
  # include "editor.h"
  # include "call_out.h"
  # include "node.h"
***************
*** 539,549 ****
   * NAME:	comm->init()
   * DESCRIPTION:	pretend to initialize communications
   */
! bool comm_init(nusers, telnet_port, binary_port)
  int nusers;
- unsigned int telnet_port, binary_port;
  {
-     return TRUE;
  }
  
  /*
--- 539,547 ----
   * NAME:	comm->init()
   * DESCRIPTION:	pretend to initialize communications
   */
! void comm_init(nusers)
  int nusers;
  {
  }
  
  /*
***************
*** 555,568 ****
  }
  
  /*
-  * NAME:        comm->listen()
-  * DESCRIPTION: pretend to start listening on telnet port and binary port
-  */
- void comm_listen()
- {
- }
- 
- /*
   * NAME:	comm->send()
   * DESCRIPTION:	pretend to send a message to a user
   */
--- 553,558 ----
***************
*** 574,610 ****
  }
  
  /*
!  * NAME:	comm->udpsend()
!  * DESCRIPTION:	pretend to send a message on the UDP channel of a connection
   */
! int comm_udpsend(obj, str)
  object *obj;
! string *str;
  {
-     return 0;
  }
  
  /*
!  * NAME:	comm->echo()
!  * DESCRIPTION:	pretend to turn on/off input echoing for a user
   */
! bool comm_echo(obj, echo)
  object *obj;
! int echo;
  {
-     return FALSE;
  }
  
  /*
!  * NAME:	comm->flush()
!  * DESCRIPTION:	pretend to flush output to all users
   */
! void comm_flush(flag)
! int flag;
  {
  }
  
  /*
   * NAME:	comm->block()
   * DESCRIPTION:	pretend to suspend or release input from a user
   */
--- 564,651 ----
  }
  
  /*
!  * NAME:	comm->echo()
!  * DESCRIPTION:	pretend to turn on/off input echoing for a user
   */
! void comm_echo(obj, echo)
  object *obj;
! int echo;
  {
  }
  
  /*
!  * NAME:	comm->flush()
!  * DESCRIPTION:	pretend to flush output to all users
   */
! void comm_flush()
! {
! }
! 
! /*
!  * NAME:        comm->listen()
!  * DESCRIPTION: pretend to open a port for listening
!  */
! void comm_listen(f, obj, port, protocol)
! frame *f;
  object *obj;
! int port, protocol;
  {
  }
  
  /*
!  * NAME:        comm->connect()
!  * DESCRIPTION: pretend to initiate a telnet or tcp connection
   */
! void comm_connect(obj, host, port, protocol)
! object *obj;
! char *host;
! int port, protocol;
  {
  }
  
  /*
+  * NAME:        comm->sendto()
+  * DESCRIPTION: pretend to send a datagram
+  */
+ void comm_sendto(obj, data, host, port)
+ object *obj;
+ string *data;
+ char *host;
+ int port;
+ {
+ }
+ 
+ /*
+  * NAME:	comm->connections()
+  * DESCRIPTION:	pretend to return an array with all connection objects
+  */
+ array *comm_connections(data)
+ dataspace *data;
+ {
+     return (array *) NULL;
+ }
+ 
+ /*
+  * NAME:	comm->ports()
+  * DESCRIPTION:	pretend to return an array with all port objects
+  */
+ array *comm_ports(data)
+ dataspace *data;
+ {
+     return (array *) NULL;
+ }
+ 
+ /*
+  * NAME:	comm->check()
+  * DESCRIPTION:	pretend to check if previous message has been sent
+  */
+ int comm_check(obj)
+ object *obj;
+ {
+     return 0;
+ }
+ 
+ /*
   * NAME:	comm->block()
   * DESCRIPTION:	pretend to suspend or release input from a user
   */
***************
*** 619,634 ****
   * DESCRIPTION:	pretend to return the ip number of a user (as a string)
   */
  string *comm_ip_number(obj)
- object *obj;
- {
-     return (string *) NULL;
- }
- 
- /*
-  * NAME:	comm->ip_name()
-  * DESCRIPTION:	pretend to return the ip name of a user
-  */
- string *comm_ip_name(obj)
  object *obj;
  {
      return (string *) NULL;
--- 660,665 ----
diff -crN dgd-net/src/config.c dgd+net/src/config.c
*** dgd-net/src/config.c	Thu Sep 21 12:07:00 2000
--- dgd+net/src/config.c	Thu Sep 21 12:06:23 2000
***************
*** 9,15 ****
  # include "path.h"
  # include "editor.h"
  # include "call_out.h"
! # include "comm.h"
  # include "version.h"
  # include "macro.h"
  # include "token.h"
--- 9,15 ----
  # include "path.h"
  # include "editor.h"
  # include "call_out.h"
! # include "comm.net.h"
  # include "version.h"
  # include "macro.h"
  # include "token.h"
***************
*** 973,990 ****
  
      m_static();
  
-     /* initialize communications */
-     if (!comm_init((int) conf[USERS].u.num,
- 		   (unsigned int) conf[TELNET_PORT].u.num,
- 		   (unsigned int) conf[BINARY_PORT].u.num)) {
- 	comm_finish();
- 	if (dumpfile != (char *) NULL) {
- 	    P_close(fd);
- 	}
- 	m_finish();
- 	return FALSE;
-     }
- 
      /* initialize strings */
      str_init();
  
--- 973,978 ----
***************
*** 1026,1035 ****
--- 1014,1037 ----
  
      m_dynamic();
  
+     if (!init) {
+ 	comm_finish();
+ 	if (dumpfile != (char *) NULL) {
+ 	    P_close(fd);
+ 	}
+ 	m_finish();
+ 	return FALSE;
+     }
+ 
      /* initialize memory manager */
      m_init((size_t) conf[STATIC_CHUNK].u.num,
      	   (size_t) conf[DYNAMIC_CHUNK].u.num);
  
+     /* initialize communications */
+     m_static();
+     comm_init((int) conf[USERS].u.num);
+     m_dynamic();
+ 
      /*
       * create include files
       */
***************
*** 1074,1082 ****
      ec_pop();
      i_del_value(cframe->sp++);
      endthread();
- 
-     /* start accepting connections */
-     comm_listen();
  
      return TRUE;
  }
--- 1076,1081 ----
diff -crN dgd-net/src/dgd.c dgd+net/src/dgd.c
*** dgd-net/src/dgd.c	Thu Sep 21 12:07:00 2000
--- dgd+net/src/dgd.c	Thu Sep 21 12:06:23 2000
***************
*** 7,13 ****
  # include "data.h"
  # include "editor.h"
  # include "call_out.h"
! # include "comm.h"
  # include "node.h"
  # include "compile.h"
  
--- 7,13 ----
  # include "data.h"
  # include "editor.h"
  # include "call_out.h"
! # include "comm.net.h"
  # include "node.h"
  # include "compile.h"
  
***************
*** 165,183 ****
      }
  
      for (;;) {
  	/* interrupts */
  	if (intr) {
  	    intr = FALSE;
! 	    if (ec_push((ec_ftn) errhandler)) {
! 		endthread();
! 		comm_flush(FALSE);
! 	    } else {
! 		call_driver_object(cframe, "interrupt", 0);
! 		i_del_value(cframe->sp++);
! 		ec_pop();
! 		endthread();
! 		comm_flush(FALSE);
! 	    }
  	}
  
  	/* rebuild swapfile */
--- 165,183 ----
      }
  
      for (;;) {
+ 	while (ec_push((ec_ftn) errhandler)) {
+ 	    endthread();
+ 	    comm_add_goahead();
+ 	    comm_flush();
+ 	}
+ 
  	/* interrupts */
  	if (intr) {
  	    intr = FALSE;
! 	    call_driver_object(cframe, "interrupt", 0);
! 	    i_del_value(cframe->sp++);
! 	    endthread();
! 	    comm_flush();
  	}
  
  	/* rebuild swapfile */
***************
*** 186,194 ****
  	/* handle user input */
  	timeout = co_delay(&mtime);
  	comm_receive(cframe, timeout, mtime);
  
  	/* callouts */
  	co_call(cframe);
! 	comm_flush(FALSE);
      }
  }
--- 186,196 ----
  	/* handle user input */
  	timeout = co_delay(&mtime);
  	comm_receive(cframe, timeout, mtime);
+ 	comm_confirm(cframe);
+ 	ec_pop();
  
  	/* callouts */
  	co_call(cframe);
! 	comm_flush();
      }
  }
diff -crN dgd-net/src/error.c dgd+net/src/error.c
*** dgd-net/src/error.c	Thu Sep 21 12:06:58 2000
--- dgd+net/src/error.c	Thu Sep 21 12:06:23 2000
***************
*** 4,10 ****
  # include "array.h"
  # include "object.h"
  # include "interpret.h"
! # include "comm.h"
  
  typedef struct _context_ {
      jmp_buf env;			/* error context */
--- 4,10 ----
  # include "array.h"
  # include "object.h"
  # include "interpret.h"
! # include "comm.net.h"
  
  typedef struct _context_ {
      jmp_buf env;			/* error context */
diff -crN dgd-net/src/host/Makefile dgd+net/src/host/Makefile
*** dgd-net/src/host/Makefile	Thu Sep 21 12:07:00 2000
--- dgd+net/src/host/Makefile	Thu Sep 21 12:06:23 2000
***************
*** 8,15 ****
  CFLAGS=	-I. -I.. $(CCFLAGS)
  CC=	gcc
  
! SRC=	local.c random.c dirent.c time.c connect.c xfloat.c
! OBJ=	local.o random.o dirent.o time.o connect.o crypt.o xfloat.o
  SUBOBJ=	local.o random.o dirent.o time.o crypt.o xfloat.o
  
  dgd:	$(OBJ)
--- 8,15 ----
  CFLAGS=	-I. -I.. $(CCFLAGS)
  CC=	gcc
  
! SRC=	local.c random.c dirent.c time.c connect.net.c xfloat.c
! OBJ=	local.o random.o dirent.o time.o connect.net.o crypt.o xfloat.o
  SUBOBJ=	local.o random.o dirent.o time.o crypt.o xfloat.o
  
  dgd:	$(OBJ)
***************
*** 34,45 ****
  time.c: unix/time.c
  	cp unix/$@ $@
  
! connect.c: unix/connect.c
  	cp unix/$@ $@
  
  xfloat.c: simfloat.c
  	cp simfloat.c $@
  
  $(OBJ):	../dgd.h ../host.h ../config.h ../alloc.h ../error.h
! connect.o: ../comm.h
  xfloat.o: ../xfloat.h
--- 34,45 ----
  time.c: unix/time.c
  	cp unix/$@ $@
  
! connect.net.c: unix/connect.net.c
  	cp unix/$@ $@
  
  xfloat.c: simfloat.c
  	cp simfloat.c $@
  
  $(OBJ):	../dgd.h ../host.h ../config.h ../alloc.h ../error.h
! connect.net.o: ../comm.net.h
  xfloat.o: ../xfloat.h
diff -crN dgd-net/src/host/Makefile.bsd dgd+net/src/host/Makefile.bsd
*** dgd-net/src/host/Makefile.bsd	Thu Sep 21 12:07:00 2000
--- dgd+net/src/host/Makefile.bsd	Thu Sep 21 12:06:23 2000
***************
*** 8,15 ****
  CFLAGS=	-I. -I.. $(CCFLAGS)
  CC=	gcc
  
! SRC=	local.c random.c dirent.c time.c connect.c xfloat.c
! OBJ=	local.o random.o dirent.o time.o connect.o crypt.o xfloat.o
  SUBOBJ=	local.o random.o dirent.o time.o crypt.o xfloat.o
  
  dgd:	$(OBJ)
--- 8,15 ----
  CFLAGS=	-I. -I.. $(CCFLAGS)
  CC=	gcc
  
! SRC=	local.c random.c dirent.c time.c connect.net.c xfloat.c
! OBJ=	local.o random.o dirent.o time.o connect.net.o crypt.o xfloat.o
  SUBOBJ=	local.o random.o dirent.o time.o crypt.o xfloat.o
  
  dgd:	$(OBJ)
***************
*** 34,45 ****
  time.c: unix/time.c
  	cp unix/$@ $@
  
! connect.c: unix/connect.c
  	cp unix/$@ $@
  
  xfloat.c: simfloat.c
  	cp simfloat.c $@
  
  $(OBJ):	../dgd.h ../host.h ../config.h ../alloc.h ../error.h
! connect.o: ../comm.h
  xfloat.o: ../xfloat.h
--- 34,45 ----
  time.c: unix/time.c
  	cp unix/$@ $@
  
! connect.net.c: unix/connect.net.c
  	cp unix/$@ $@
  
  xfloat.c: simfloat.c
  	cp simfloat.c $@
  
  $(OBJ):	../dgd.h ../host.h ../config.h ../alloc.h ../error.h
! connect.net.o: ../comm.net.h
  xfloat.o: ../xfloat.h
diff -crN dgd-net/src/host/Makefile.sysv dgd+net/src/host/Makefile.sysv
*** dgd-net/src/host/Makefile.sysv	Thu Sep 21 12:07:00 2000
--- dgd+net/src/host/Makefile.sysv	Thu Sep 21 12:06:23 2000
***************
*** 8,15 ****
  CFLAGS=	-I. -I.. $(CCFLAGS)
  CC=	gcc
  
! SRC=	local.c lrand48.c dirent.c time.c connect.c xfloat.c
! OBJ=	local.o lrand48.o dirent.o time.o connect.o crypt.o xfloat.o
  SUBOBJ=	local.o lrand48.o dirent.o time.o crypt.o xfloat.o
  
  dgd:	$(OBJ)
--- 8,15 ----
  CFLAGS=	-I. -I.. $(CCFLAGS)
  CC=	gcc
  
! SRC=	local.c lrand48.c dirent.c time.c connect.net.c xfloat.c
! OBJ=	local.o lrand48.o dirent.o time.o connect.net.o crypt.o xfloat.o
  SUBOBJ=	local.o lrand48.o dirent.o time.o crypt.o xfloat.o
  
  dgd:	$(OBJ)
***************
*** 34,45 ****
  time.c: unix/time.c
  	cp unix/$@ $@
  
! connect.c: unix/connect.c
  	cp unix/$@ $@
  
  xfloat.c: simfloat.c
  	cp simfloat.c $@
  
  $(OBJ):	../dgd.h ../host.h ../config.h ../alloc.h ../error.h
! connect.o: ../comm.h
  xfloat.o: ../xfloat.h
--- 34,45 ----
  time.c: unix/time.c
  	cp unix/$@ $@
  
! connect.net.c: unix/connect.net.c
  	cp unix/$@ $@
  
  xfloat.c: simfloat.c
  	cp simfloat.c $@
  
  $(OBJ):	../dgd.h ../host.h ../config.h ../alloc.h ../error.h
! connect.net.o: ../comm.net.h
  xfloat.o: ../xfloat.h
diff -crN dgd-net/src/host/pc/connect.net.c dgd+net/src/host/pc/connect.net.c
*** dgd-net/src/host/pc/connect.net.c	Thu Jan  1 10:00:00 1970
--- dgd+net/src/host/pc/connect.net.c	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,462 ----
+ # define INCLUDE_FILE_IO
+ # include "dgd.h"
+ # define FD_SETSIZE   1024
+ # include <winsock.h>
+ # include "comm.net.h"
+ 
+ struct _connection_ {
+     SOCKET fd;				/* file descriptor */
+     struct sockaddr_in addr;		/* internet address of connection */
+     struct _connection_ *next;		/* next in list */
+     char host[16];			/* ascii ip number of peer */
+     char hostlen;			/* length of host string */
+ };
+ 
+ static int nusers;			/* # of users */
+ static connection *connections;		/* connections array */
+ static connection *flist;		/* list of free connections */
+ static fd_set rfds;			/* read file descriptor bitmap */
+ static fd_set wfds;			/* write file descriptor bitmap */
+ static fd_set readfds;			/* fds selected for reading */
+ static fd_set writefds;			/* fds selected for writing */
+ static fd_set xxxfds;
+ static fd_set ufds;                     /* identical to rfds, minus those that
+                                            are blocked. */
+ 
+ /*
+  * NAME:	conn->init()
+  * DESCRIPTION:	initialize connections
+  */
+ void conn_init(int maxusers)
+ {
+     int n;
+     connection *conn;
+     WSADATA wsadata;
+ 
+     /* initialize winsock */
+     if (WSAStartup(MAKEWORD(1, 1), &wsadata) != 0) {
+ 	P_message("WSAStartup failed (no winsock?)\n");
+ 	exit(2);
+     }
+     if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) {
+ 	WSACleanup();
+ 	P_message("Winsock 1.1 not supported\n");
+ 	exit(2);
+     }
+ 
+     connections = ALLOC(connection, nusers = maxusers);
+     for (n = nusers, conn = connections; n > 0; --n, conn++) {
+ 	conn->fd = INVALID_SOCKET;
+ 	conn->next = flist;
+ 	flist = conn;
+     }
+ 
+     FD_ZERO(&rfds);
+     FD_ZERO(&wfds);
+     FD_ZERO(&ufds);
+ }
+ 
+ /*
+  * NAME:	conn->finish()
+  * DESCRIPTION:	terminate connections
+  */
+ void conn_finish(void)
+ {
+     WSACleanup();
+ }
+ 
+ /*
+  * NAME:	conn->new()
+  * DESCRIPTION:	initialize a new connection struct
+  */
+ static connection *conn_new(SOCKET fd, struct sockaddr_in *sin)
+ {
+     connection *conn;
+ 
+     conn = flist;
+     flist = conn->next;
+     conn->fd = fd;
+     memcpy(&conn->addr, sin, sizeof(conn->addr));
+     strcpy(conn->host, inet_ntoa(sin->sin_addr));
+     conn->hostlen = strlen(conn->host);
+     if (fd != INVALID_SOCKET) {
+ 	FD_SET(fd, &xxxfds);
+     }
+ 
+     return conn;
+ }
+ 
+ /*
+  * NAME:	conn->listen()
+  * DESCRIPTION:	bind a connection to a local port
+  */
+ connection *conn_listen(int port, int protocol)
+ {
+     SOCKET fd;
+     static struct sockaddr_in sin;
+     int size;
+     int on;
+     unsigned long nonblock;
+ 
+     if (port < 0) {
+ 	/* Request automatic port assignment */
+ 	port = 0;
+     }
+ 
+     /* No free connections:  see the comment about nusers in comm_receive. */
+     if (flist == (connection *) NULL) {
+ 	return (connection *) NULL;
+     }
+ 
+     if (protocol == PRC_UDP) {
+ 	fd = socket(PF_INET, SOCK_DGRAM, 0);
+     } else {
+ 	fd = socket(PF_INET, SOCK_STREAM, 0);
+     }
+     if (fd == INVALID_SOCKET) {
+ 	return (connection *)NULL;
+     }
+ 
+     on = 1;
+     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, 
+ 		   sizeof(on)) != 0) {
+         closesocket(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     sin.sin_family = AF_INET;
+     sin.sin_port = htons((u_short) port);
+     sin.sin_addr.s_addr = INADDR_ANY;  /* htonl? */
+     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
+ 	closesocket(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     size = sizeof(sin);
+     if (getsockname(fd, (struct sockaddr *)&sin, &size) != 0) {
+ 	closesocket(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     if (protocol != PRC_UDP && listen(fd, 64) != 0) {
+ 	closesocket(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     nonblock = TRUE;
+     if (ioctlsocket(fd, FIONBIO, &nonblock) != 0) {
+ 	closesocket(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     FD_SET(fd, &rfds);
+     FD_SET(fd, &ufds);
+     return conn_new(fd, &sin);
+ }
+ 
+ /*
+  * NAME:	conn->accept()
+  * DESCRIPTION:	accept a connection
+  */
+ connection *conn_accept(connection *conn)
+ {
+     SOCKET fd;
+     struct sockaddr_in sin;
+     int len, on;
+     
+     if (!FD_ISSET(conn->fd, &readfds)) {
+ 	return (connection *) NULL;
+     }
+ 
+     len = sizeof(sin);
+     fd = accept(conn->fd, (struct sockaddr *) &sin, &len);
+     if (fd == INVALID_SOCKET) {
+ 	return (connection *) NULL;
+     }
+ 
+     on = 1;
+     setsockopt(conn->fd, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on));
+ 
+     FD_SET(fd, &rfds);
+     FD_SET(fd, &ufds);
+     return conn_new(fd, &sin);
+ }
+ 
+ /*
+  * NAME:	conn->connect()
+  * DESCRIPTION:	initiate a tcp connection
+  */
+ connection *conn_connect(char *host, int port, int protocol)
+ {
+     SOCKET fd;
+     static struct sockaddr_in sin;
+     unsigned long nonblock;
+     int err;
+ 
+     if (protocol != PRC_TCP) {
+         return (connection *) NULL;
+     }
+ 
+     /* No free connections:  see the comment about nusers in comm_receive(). */
+     if (flist == (connection *) NULL) {
+ 	return (connection *) NULL;
+     }
+ 
+     sin.sin_family = AF_INET;
+     sin.sin_port = htons((u_short) port);
+     sin.sin_addr.s_addr = inet_addr(host);
+ 
+     fd = socket(PF_INET, SOCK_STREAM, 0);
+     if (fd == INVALID_SOCKET ) {
+ 	return conn_new(INVALID_SOCKET, &sin);
+     }
+ 
+     nonblock = TRUE;
+     if (ioctlsocket(fd, FIONBIO, &nonblock) != 0) {
+ 	closesocket(fd);
+ 	return conn_new(INVALID_SOCKET, &sin);
+     }
+ 
+     if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR) {
+ 	err = WSAGetLastError();
+ 	if (err != WSAEWOULDBLOCK) {
+ 	    closesocket(fd);
+ 	    return conn_new(INVALID_SOCKET, &sin);
+ 	}
+     }
+ 
+     FD_SET(fd, &wfds);
+     return conn_new(fd, &sin);
+ }
+ 
+ /*
+  * NAME:	conn->connected()
+  * DESCRIPTION:	mark a connecting socket as active
+  */
+ int conn_connected(connection *conn)
+ {
+     struct sockaddr sa;
+     int salen = sizeof(sa);
+ 
+     if (conn->fd == INVALID_SOCKET)
+ 	return -1;
+ 
+     if (!FD_ISSET(conn->fd, &writefds))
+ 	return 0;
+ 
+     /* Use getpeername() solely to check for errors. */
+     if (getpeername(conn->fd, &sa, &salen) != 0) {
+ 	FD_CLR(conn->fd, &wfds);
+ 	return -1;
+     }
+ 
+     FD_CLR(conn->fd, &wfds);
+     FD_SET(conn->fd, &rfds);
+     FD_SET(conn->fd, &ufds);
+     return 1;
+ }
+     
+ /*
+  * NAME:	conn->writable()
+  * DESCRIPTION:	check if a connection has been selected for writing
+  */
+ int conn_writable(connection *conn)
+ {
+     return conn->fd != INVALID_SOCKET && FD_ISSET(conn->fd, &writefds);
+ }
+ 
+ /*
+  * NAME:	conn->del()
+  * DESCRIPTION:	delete a connection
+  */
+ void conn_del(connection *conn)
+ {
+     if (conn->fd != INVALID_SOCKET) {
+ 	closesocket(conn->fd);
+ 	FD_CLR(conn->fd, &rfds);
+         FD_CLR(conn->fd, &ufds);
+ 	FD_CLR(conn->fd, &wfds);
+ 	FD_CLR(conn->fd, &readfds);
+ 	FD_CLR(conn->fd, &writefds);
+ 	conn->fd = INVALID_SOCKET;
+     }
+     conn->next = flist;
+     flist = conn;
+ }
+ 
+ /*
+  * NAME:	conn->wait()
+  * DESCRIPTION:	mark a connection as waiting for writability
+  */
+ void conn_wait(connection *conn, int wait)
+ {
+     if (conn->fd == INVALID_SOCKET)
+ 	return;
+ 
+     if (wait) {
+ 	FD_SET(conn->fd, &wfds);
+     } else {
+ 	FD_CLR(conn->fd, &wfds);
+     }
+ }
+ 
+ /*
+  * NAME:        conn->block()
+  * DESCRIPTION: temporarily unselect (block) a connection with respect to
+  *              incoming data
+  */
+ void conn_block(conn, flag)
+ register connection *conn;
+ register int flag;
+ {
+     if (conn->fd < 0) {
+         return;
+     }
+ 
+     if (flag) {
+         FD_CLR(conn->fd, &ufds);
+         FD_CLR(conn->fd, &readfds);
+     } else {
+         FD_SET(conn->fd, &ufds);
+     }
+ }
+ 
+ /*
+  * NAME:	conn->select()
+  * DESCRIPTION:	wait for input from connections
+  */
+ int conn_select(Uint t, unsigned int mtime)
+ {
+     struct timeval timeout;
+     int retval;
+ 
+     /*
+      * First, check readability and writability for binary sockets with pending
+      * data only.
+      */
+     memcpy(&readfds, &ufds, sizeof(fd_set));
+     memcpy(&writefds, &wfds, sizeof(fd_set));
+     if (mtime != 0xffff) {
+ 	timeout.tv_sec = t;
+ 	timeout.tv_usec = mtime * 1000;
+ 	retval = select(0, &readfds, &writefds, (fd_set *) NULL, &timeout);
+     } else {
+ 	retval = select(0, &readfds, &writefds, (fd_set *) NULL,
+ 			(struct timeval *) NULL);
+     }
+     /*
+      * Now check writability for all sockets in a polling call.
+      */
+     memcpy(&xxxfds, &rfds, sizeof(fd_set));
+     timeout.tv_sec = 0;
+     timeout.tv_usec = 0;
+     select(0, (fd_set *) NULL, &xxxfds, (fd_set *) NULL, &timeout);
+     return retval;
+ }
+ 
+ /*
+  * NAME:	conn->recvfrom()
+  * DESCRIPTION: read a datagram
+  */
+ int conn_recvfrom(connection *conn, char *buf, int size)
+ {
+     struct sockaddr_in sin;
+     int len = sizeof(sin);
+     
+     if (conn->fd == INVALID_SOCKET) {
+ 	return -1;
+     }
+     if (!FD_ISSET(conn->fd, &readfds)) {
+ 	return 0;
+     }
+     size = recvfrom(conn->fd, buf, size, 0, (struct sockaddr *)&sin, &len);
+     memcpy(&(conn->addr), &sin, len);
+     strcpy(conn->host, inet_ntoa(sin.sin_addr));
+     conn->hostlen = strlen(conn->host);
+     return size;  /* Size may legitimately be 0 */
+ }
+ 
+ /*
+  * NAME:	conn->read()
+  * DESCRIPTION:	read from a connection
+  */
+ int conn_read(connection *conn, char *buf, int size)
+ {
+     if (conn->fd == INVALID_SOCKET) {
+ 	return -1;
+     }
+     if (!FD_ISSET(conn->fd, &readfds) || size == 0) {
+ 	return 0;
+     }
+     size = recv(conn->fd, buf, size, 0);
+     return (size == 0) ? -1 : size;
+ }
+ 
+ /*
+  * NAME:	conn->sendto()
+  * DESCRIPTION: send a datagram
+  */
+ void conn_sendto(connection *conn, char *data, int size, char *host, int port)
+ {
+     static struct sockaddr_in sin;
+     if (conn->fd != INVALID_SOCKET) {
+ 	sin.sin_family = AF_INET;
+ 	sin.sin_addr.s_addr = inet_addr(host);
+ 	sin.sin_port = htons((unsigned short) port);
+ 	sendto(conn->fd, data, size, 0, (struct sockaddr *)&sin, sizeof(sin));
+     }
+ }
+ 
+ /*
+  * NAME:	conn->write()
+  * DESCRIPTION:	write to a connection; return the number of bytes written
+  */
+ int conn_write(connection *conn, char *buf, int size)
+ {
+     int len;
+     int err;
+ 
+     if (conn->fd == INVALID_SOCKET) {
+ 	return -1;
+     } else if (!FD_ISSET(conn->fd, &xxxfds)) {
+ 	return 0;
+     } else {
+ 	/* I'm assuming Win32 writes are not interruptible. */
+ 	len = send(conn->fd, buf, size, 0);
+ 	if (len == SOCKET_ERROR) {
+ 	    FD_CLR(conn->fd, &xxxfds);
+ 	    err = WSAGetLastError();
+ 	    if (err == WSAEWOULDBLOCK) {
+ 		return 0;
+ 	    } else {
+ 		closesocket(conn->fd);
+ 		FD_CLR(conn->fd, &rfds);
+                 FD_CLR(conn->fd, &ufds);
+ 		FD_CLR(conn->fd, &wfds);
+ 		FD_CLR(conn->fd, &readfds);
+ 		FD_CLR(conn->fd, &writefds);
+ 		conn->fd = INVALID_SOCKET;
+ 		return -1;
+ 	    }
+ 	}
+ 	return len;
+     }
+ }
+ 
+ /*
+  * NAME:	conn->ipnum()
+  * DESCRIPTION:	return the ip number of a connection
+  */
+ string *conn_ipnum(connection *conn)
+ {
+     return str_new(conn->host, conn->hostlen);
+ }
+ 
+ /*
+  * NAME:	conn->port()
+  * DESCRIPTION:	return the port number of a connection
+  */
+ Int conn_port(connection *conn)
+ {
+     return ntohs(conn->addr.sin_port);
+ }
diff -crN dgd-net/src/host/pc/windgd.mak dgd+net/src/host/pc/windgd.mak
*** dgd-net/src/host/pc/windgd.mak	Thu Sep 21 12:06:51 2000
--- dgd+net/src/host/pc/windgd.mak	Thu Sep 21 12:06:23 2000
***************
*** 61,67 ****
  	-@erase ".\Release\config.obj"
  	-@erase ".\Release\ppcontrol.obj"
  	-@erase ".\Release\swap.obj"
! 	-@erase ".\Release\comm.obj"
  	-@erase ".\Release\debug.obj"
  	-@erase ".\Release\dfa.obj"
  	-@erase ".\Release\str.obj"
--- 61,67 ----
  	-@erase ".\Release\config.obj"
  	-@erase ".\Release\ppcontrol.obj"
  	-@erase ".\Release\swap.obj"
! 	-@erase ".\Release\comm.net.obj"
  	-@erase ".\Release\debug.obj"
  	-@erase ".\Release\dfa.obj"
  	-@erase ".\Release\str.obj"
***************
*** 91,97 ****
  	-@erase ".\Release\vars.obj"
  	-@erase ".\Release\edcmd.obj"
  	-@erase ".\Release\cmdsub.obj"
! 	-@erase ".\Release\connect.obj"
  	-@erase ".\Release\editor.obj"
  	-@erase ".\Release\line.obj"
  	-@erase ".\Release\grammar.obj"
--- 91,97 ----
  	-@erase ".\Release\vars.obj"
  	-@erase ".\Release\edcmd.obj"
  	-@erase ".\Release\cmdsub.obj"
! 	-@erase ".\Release\connect.net.obj"
  	-@erase ".\Release\editor.obj"
  	-@erase ".\Release\line.obj"
  	-@erase ".\Release\grammar.obj"
***************
*** 302,308 ****
  	-@erase ".\Debug\csupport.obj"
  	-@erase ".\Debug\codegeni.obj"
  	-@erase ".\Debug\alloc.obj"
! 	-@erase ".\Debug\connect.obj"
  	-@erase ".\Debug\time.obj"
  	-@erase ".\Debug\parse.obj"
  	-@erase ".\Debug\ppcontrol.obj"
--- 302,308 ----
  	-@erase ".\Debug\csupport.obj"
  	-@erase ".\Debug\codegeni.obj"
  	-@erase ".\Debug\alloc.obj"
! 	-@erase ".\Debug\connect.net.obj"
  	-@erase ".\Debug\time.obj"
  	-@erase ".\Debug\parse.obj"
  	-@erase ".\Debug\ppcontrol.obj"
***************
*** 327,333 ****
  	-@erase ".\Debug\crypt.obj"
  	-@erase ".\Debug\lpc.obj"
  	-@erase ".\Debug\swap.obj"
! 	-@erase ".\Debug\comm.obj"
  	-@erase ".\Debug\ppstr.obj"
  	-@erase ".\Debug\object.obj"
  	-@erase ".\Debug\windgd.res"
--- 327,333 ----
  	-@erase ".\Debug\crypt.obj"
  	-@erase ".\Debug\lpc.obj"
  	-@erase ".\Debug\swap.obj"
! 	-@erase ".\Debug\comm.net.obj"
  	-@erase ".\Debug\ppstr.obj"
  	-@erase ".\Debug\object.obj"
  	-@erase ".\Debug\windgd.res"
***************
*** 373,379 ****
  	"$(INTDIR)/csupport.sbr" \
  	"$(INTDIR)/codegeni.sbr" \
  	"$(INTDIR)/alloc.sbr" \
! 	"$(INTDIR)/connect.sbr" \
  	"$(INTDIR)/time.sbr" \
  	"$(INTDIR)/parse.sbr" \
  	"$(INTDIR)/ppcontrol.sbr" \
--- 373,379 ----
  	"$(INTDIR)/csupport.sbr" \
  	"$(INTDIR)/codegeni.sbr" \
  	"$(INTDIR)/alloc.sbr" \
! 	"$(INTDIR)/connect.net.sbr" \
  	"$(INTDIR)/time.sbr" \
  	"$(INTDIR)/parse.sbr" \
  	"$(INTDIR)/ppcontrol.sbr" \
***************
*** 398,404 ****
  	"$(INTDIR)/crypt.sbr" \
  	"$(INTDIR)/lpc.sbr" \
  	"$(INTDIR)/swap.sbr" \
! 	"$(INTDIR)/comm.sbr" \
  	"$(INTDIR)/ppstr.sbr" \
  	"$(INTDIR)/object.sbr" \
  	"$(INTDIR)/table.sbr" \
--- 398,404 ----
  	"$(INTDIR)/crypt.sbr" \
  	"$(INTDIR)/lpc.sbr" \
  	"$(INTDIR)/swap.sbr" \
! 	"$(INTDIR)/comm.net.sbr" \
  	"$(INTDIR)/ppstr.sbr" \
  	"$(INTDIR)/object.sbr" \
  	"$(INTDIR)/table.sbr" \
***************
*** 713,719 ****
  ################################################################################
  # Begin Source File
  
! SOURCE=.\..\..\comm.c
  DEP_CPP_COMM_=\
  	".\..\..\dgd.h"\
  	".\..\..\str.h"\
--- 713,719 ----
  ################################################################################
  # Begin Source File
  
! SOURCE=.\..\..\comm.net.c
  DEP_CPP_COMM_=\
  	".\..\..\dgd.h"\
  	".\..\..\str.h"\
***************
*** 721,727 ****
  	".\..\..\object.h"\
  	".\..\..\interpret.h"\
  	".\..\..\data.h"\
! 	".\..\..\comm.h"\
  	".\..\..\config.h"\
  	".\..\..\alloc.h"\
  	".\..\..\host.h"\
--- 721,727 ----
  	".\..\..\object.h"\
  	".\..\..\interpret.h"\
  	".\..\..\data.h"\
! 	".\..\..\comm.net.h"\
  	".\..\..\config.h"\
  	".\..\..\alloc.h"\
  	".\..\..\host.h"\
***************
*** 739,745 ****
  !IF  "$(CFG)" == "windgd - Win32 Release"
  
  
! "$(INTDIR)\comm.obj" : $(SOURCE) $(DEP_CPP_COMM_) "$(INTDIR)"
     $(CPP) $(CPP_PROJ) $(SOURCE)
  
  
--- 739,745 ----
  !IF  "$(CFG)" == "windgd - Win32 Release"
  
  
! "$(INTDIR)\comm.net.obj" : $(SOURCE) $(DEP_CPP_COMM_) "$(INTDIR)"
     $(CPP) $(CPP_PROJ) $(SOURCE)
  
  
***************
*** 750,759 ****
  	$(CPP) $(CPP_PROJ) $(SOURCE) \
  	
  
! "$(INTDIR)\comm.obj" : $(SOURCE) $(DEP_CPP_COMM_) "$(INTDIR)"
     $(BuildCmds)
  
! "$(INTDIR)\comm.sbr" : $(SOURCE) $(DEP_CPP_COMM_) "$(INTDIR)"
     $(BuildCmds)
  
  !ENDIF 
--- 750,759 ----
  	$(CPP) $(CPP_PROJ) $(SOURCE) \
  	
  
! "$(INTDIR)\comm.net.obj" : $(SOURCE) $(DEP_CPP_COMM_) "$(INTDIR)"
     $(BuildCmds)
  
! "$(INTDIR)\comm.net.sbr" : $(SOURCE) $(DEP_CPP_COMM_) "$(INTDIR)"
     $(BuildCmds)
  
  !ENDIF 
***************
*** 774,780 ****
  	".\..\..\path.h"\
  	".\..\..\editor.h"\
  	".\..\..\call_out.h"\
! 	".\..\..\comm.h"\
  	".\..\..\version.h"\
  	".\..\..\lex\macro.h"\
  	".\..\..\lex\token.h"\
--- 774,780 ----
  	".\..\..\path.h"\
  	".\..\..\editor.h"\
  	".\..\..\call_out.h"\
! 	".\..\..\comm.net.h"\
  	".\..\..\version.h"\
  	".\..\..\lex\macro.h"\
  	".\..\..\lex\token.h"\
***************
*** 886,892 ****
  	".\..\..\data.h"\
  	".\..\..\editor.h"\
  	".\..\..\call_out.h"\
! 	".\..\..\comm.h"\
  	".\..\..\comp\node.h"\
  	".\..\..\comp\compile.h"\
  	".\..\..\config.h"\
--- 886,892 ----
  	".\..\..\data.h"\
  	".\..\..\editor.h"\
  	".\..\..\call_out.h"\
! 	".\..\..\comm.net.h"\
  	".\..\..\comp\node.h"\
  	".\..\..\comp\compile.h"\
  	".\..\..\config.h"\
***************
*** 989,995 ****
  	".\..\..\array.h"\
  	".\..\..\object.h"\
  	".\..\..\interpret.h"\
! 	".\..\..\comm.h"\
  	".\..\..\config.h"\
  	".\..\..\alloc.h"\
  	".\..\..\host.h"\
--- 989,995 ----
  	".\..\..\array.h"\
  	".\..\..\object.h"\
  	".\..\..\interpret.h"\
! 	".\..\..\comm.net.h"\
  	".\..\..\config.h"\
  	".\..\..\alloc.h"\
  	".\..\..\host.h"\
***************
*** 2112,2118 ****
  	".\..\..\hash.h"\
  	".\..\..\swap.h"\
  	".\..\..\path.h"\
! 	".\..\..\comm.h"\
  	".\..\..\call_out.h"\
  	".\..\..\editor.h"\
  	".\..\..\comp\node.h"\
--- 2112,2118 ----
  	".\..\..\hash.h"\
  	".\..\..\swap.h"\
  	".\..\..\path.h"\
! 	".\..\..\comm.net.h"\
  	".\..\..\call_out.h"\
  	".\..\..\editor.h"\
  	".\..\..\comp\node.h"\
***************
*** 2366,2372 ****
  DEP_CPP_STD_C=\
  	".\..\..\kfun\kfun.h"\
  	".\..\..\path.h"\
! 	".\..\..\comm.h"\
  	".\..\..\call_out.h"\
  	".\..\..\editor.h"\
  	".\..\..\comp\node.h"\
--- 2366,2372 ----
  DEP_CPP_STD_C=\
  	".\..\..\kfun\kfun.h"\
  	".\..\..\path.h"\
! 	".\..\..\comm.net.h"\
  	".\..\..\call_out.h"\
  	".\..\..\editor.h"\
  	".\..\..\comp\node.h"\
***************
*** 2893,2905 ****
  ################################################################################
  # Begin Source File
  
! SOURCE=.\connect.c
  DEP_CPP_CONNE=\
  	".\..\..\dgd.h"\
  	".\..\..\str.h"\
  	".\..\..\array.h"\
  	".\..\..\object.h"\
! 	".\..\..\comm.h"\
  	".\..\..\config.h"\
  	".\..\..\alloc.h"\
  	".\..\..\host.h"\
--- 2893,2905 ----
  ################################################################################
  # Begin Source File
  
! SOURCE=.\connect.net.c
  DEP_CPP_CONNE=\
  	".\..\..\dgd.h"\
  	".\..\..\str.h"\
  	".\..\..\array.h"\
  	".\..\..\object.h"\
! 	".\..\..\comm.net.h"\
  	".\..\..\config.h"\
  	".\..\..\alloc.h"\
  	".\..\..\host.h"\
***************
*** 2917,2931 ****
  !IF  "$(CFG)" == "windgd - Win32 Release"
  
  
! "$(INTDIR)\connect.obj" : $(SOURCE) $(DEP_CPP_CONNE) "$(INTDIR)"
  
  
  !ELSEIF  "$(CFG)" == "windgd - Win32 Debug"
  
  
! "$(INTDIR)\connect.obj" : $(SOURCE) $(DEP_CPP_CONNE) "$(INTDIR)"
  
! "$(INTDIR)\connect.sbr" : $(SOURCE) $(DEP_CPP_CONNE) "$(INTDIR)"
  
  
  !ENDIF 
--- 2917,2931 ----
  !IF  "$(CFG)" == "windgd - Win32 Release"
  
  
! "$(INTDIR)\connect.net.obj" : $(SOURCE) $(DEP_CPP_CONNE) "$(INTDIR)"
  
  
  !ELSEIF  "$(CFG)" == "windgd - Win32 Debug"
  
  
! "$(INTDIR)\connect.net.obj" : $(SOURCE) $(DEP_CPP_CONNE) "$(INTDIR)"
  
! "$(INTDIR)\connect.net.sbr" : $(SOURCE) $(DEP_CPP_CONNE) "$(INTDIR)"
  
  
  !ENDIF 
diff -crN dgd-net/src/host/telnet.h dgd+net/src/host/telnet.h
*** dgd-net/src/host/telnet.h	Mon Nov 13 14:44:48 1995
--- dgd+net/src/host/telnet.h	Thu Sep 21 12:06:23 2000
***************
*** 9,20 ****
--- 9,30 ----
  # define WILL		251	/* will */
  # define SB		250	/* begin subnegotiation */
  # define GA		249	/* go ahead */
+ # define EL		248	/* erase line */
+ # define EC		247	/* erase character */
  # define AYT		246	/* are you there */
+ # define AO		245	/* abort output */
  # define IP		244	/* interrupt process */
  # define BREAK		243	/* break */
+ # define DM		242	/* data mark */
  # define SE		240	/* end subnegotiation */
  
  /* options */
  # define TELOPT_ECHO	1	/* echo */
  # define TELOPT_SGA	3	/* suppress go ahead */
  # define TELOPT_TM	6	/* timing mark */
+ # define TELOPT_LINEMODE	34	/* linemode option */
+ 
+ /* subnegotiations for linemode */
+ # define LM_MODE	1
+ # define MODE_EDIT	0x01
+ 
diff -crN dgd-net/src/host/unix/connect.net.c dgd+net/src/host/unix/connect.net.c
*** dgd-net/src/host/unix/connect.net.c	Thu Jan  1 10:00:00 1970
--- dgd+net/src/host/unix/connect.net.c	Thu Sep 21 12:06:23 2000
***************
*** 0 ****
--- 1,509 ----
+ # include <sys/time.h>
+ # include <sys/socket.h>
+ # include <netinet/in.h>
+ # include <arpa/inet.h>
+ # include <netdb.h>
+ # include <errno.h>
+ # define INCLUDE_FILE_IO
+ # include "dgd.h"
+ # include "comm.net.h"
+ 
+ # define EXT_SIZE 16384   /* size requested when extending a buffer */
+ 
+ struct _connection_ {
+     int fd;				/* file descriptor */
+     struct sockaddr_in sin;		/* peer's address */
+     struct _connection_ *next;		/* next in list */
+     int extended;                       /* output buffer has been extended */
+     char host[16];			/* ascii ip number of peer */
+     char hostlen;			/* length of host string */
+ };
+ 
+ static connection *connections;		/* connections array */
+ static connection *flist;		/* list of free connections */
+ static fd_set rfds;			/* read file descriptor bitmap */
+ static fd_set wfds;			/* write file descriptor bitmap */
+ static fd_set readfds;			/* fds selected for reading */
+ static fd_set writefds;			/* fds selected for writing */
+ static fd_set xxxfds;
+ static fd_set ufds;                     /* identical to rfds, minus those that
+                                            are blocked */
+ static int maxfd;			/* largest fd opened yet */
+ 
+ /*
+  * NAME:	conn->init()
+  * DESCRIPTION:	initialize connections
+  */
+ void conn_init(maxusers)
+ int maxusers;
+ {
+     register int n;
+     register connection *conn;
+ 
+     connections = ALLOC(connection, maxusers);
+     for (n = maxusers, conn = connections; n > 0; --n, conn++) {
+ 	conn->fd = -1;
+ 	conn->next = flist;
+ 	flist = conn;
+     }
+ 
+     FD_ZERO(&rfds);
+     FD_ZERO(&wfds);
+     FD_ZERO(&ufds);
+ }
+ 
+ /*
+  * NAME:	conn->finish()
+  * DESCRIPTION:	terminate connections
+  */
+ void conn_finish()
+ {
+ }
+ 
+ /*
+  * NAME:	conn->new()
+  * DESCRIPTION:	initialize a new connection struct
+  */
+ static connection *conn_new(fd, sin)
+ int fd;
+ struct sockaddr_in *sin;
+ {
+     register connection *conn;
+ 
+     conn = flist;
+     flist = conn->next;
+     conn->fd = fd;
+     memcpy(&conn->sin, sin, sizeof(conn->sin));
+     strcpy(conn->host, inet_ntoa(sin->sin_addr));
+     conn->hostlen = strlen(conn->host);
+     if (fd >= 0) {
+ 	FD_SET(fd, &xxxfds);
+ 	if (fd > maxfd) {
+ 	    maxfd = fd;
+ 	}
+     }
+     return conn;
+ }
+ 
+ /*
+  * NAME:	conn->extend()
+  * DESCRIPTION:	enlarge a socket's output buffer
+  */
+ static void conn_extend(conn)
+ connection *conn;
+ {
+     int bufsize;
+     int sz = sizeof(bufsize);
+ 
+     conn->extended = 1;
+     if (getsockopt(conn->fd, SOL_SOCKET, SO_SNDBUF, (void *)&bufsize, &sz) >= 0
+ 	&& sz == sizeof(bufsize) && bufsize < EXT_SIZE) {
+ 	bufsize = EXT_SIZE;
+ 	setsockopt(conn->fd, SOL_SOCKET, SO_SNDBUF,
+ 		   (void *)&bufsize, sizeof(bufsize));
+     }
+ }
+ 
+ 
+ /*
+  * NAME:	conn->listen()
+  * DESCRIPTION:	bind a connection to a local port
+  */
+ connection *conn_listen(port, protocol)
+ int port, protocol;
+ {
+     int fd;
+     static struct sockaddr_in sin;
+     int size;
+     int on = 1;
+ 
+     if (port < 0) {
+ 	/* Request automatic port assignment */
+ 	port = 0;
+     }
+ 
+     /* No free connections:  see the comment about nusers in comm_receive. */
+     if (flist == (connection *) NULL) {
+ 	return (connection *) NULL;
+     }
+ 
+     if (protocol == PRC_UDP) {
+ 	fd = socket(PF_INET, SOCK_DGRAM, 0);
+     } else {
+ 	fd = socket(PF_INET, SOCK_STREAM, 0);
+     }
+     if (fd < 0) {
+ 	return (connection *)NULL;
+     }
+     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, 
+ 		   sizeof(on)) < 0) {
+         close(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     sin.sin_family = AF_INET;
+     sin.sin_port = htons(port);
+     sin.sin_addr.s_addr = INADDR_ANY;  /* htonl? */
+     if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
+ 	close(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     size = sizeof(sin);
+     if (getsockname(fd, (struct sockaddr *)&sin, &size) < 0) {
+ 	close(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     if (protocol != PRC_UDP && listen(fd, 64) < 0) {
+ 	close(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     if (fcntl(fd, F_SETFL, FNDELAY) < 0) {
+ 	close(fd);
+ 	return (connection *)NULL;
+     }
+ 
+     FD_SET(fd, &rfds);
+     FD_SET(fd, &ufds);
+     return conn_new(fd, &sin);
+ }
+ 
+ /*
+  * NAME:	conn->accept()
+  * DESCRIPTION:	accept a connection
+  */
+ connection *conn_accept(conn)
+ connection *conn;
+ {
+     int fd;
+     struct sockaddr_in sin;
+     int len, on;
+     
+     if (!FD_ISSET(conn->fd, &readfds)) {
+ 	return (connection *) NULL;
+     }
+ 
+     len = sizeof(sin);
+     fd = accept(conn->fd, (struct sockaddr *) &sin, &len);
+     if (fd < 0) {
+ 	return (connection *) NULL;
+     }
+ 
+     on = 1;
+     setsockopt(conn->fd, SOL_SOCKET, SO_OOBINLINE, (void *)&on, sizeof(on));
+ 
+     FD_SET(fd, &rfds);
+     FD_SET(fd, &ufds);
+     return conn_new(fd, &sin);
+ }
+ 
+ /*
+  * NAME:	conn->connect()
+  * DESCRIPTION:	initiate a tcp connection
+  */
+ connection *conn_connect(host, port, protocol)
+ char *host;
+ int port, protocol;
+ {
+     int fd;
+     static struct sockaddr_in sin;
+ 
+     if (protocol != PRC_TCP) {
+         return (connection *) NULL;
+     }
+ 
+     /* No free connections:  see the comment about nusers in comm_receive(). */
+     if (flist == (connection *) NULL) {
+ 	return (connection *) NULL;
+     }
+ 
+     sin.sin_family = AF_INET;
+     sin.sin_port = htons(port);
+     sin.sin_addr.s_addr = inet_addr(host);
+ 
+     fd = socket(PF_INET, SOCK_STREAM, 0);
+     if (fd < 0) {
+ 	return conn_new(-1, &sin);
+     }
+ 
+     if (fcntl(fd, F_SETFL, FNDELAY) < 0) {
+ 	close(fd);
+ 	return conn_new(-1, &sin);
+     }
+ 
+     if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0 &&
+ 	!(errno == EINPROGRESS || errno == EAGAIN)) {
+ 	close(fd);
+ 	return conn_new(-1, &sin);
+     }
+ 
+     FD_SET(fd, &wfds);
+     return conn_new(fd, &sin);
+ }
+ 
+ /*
+  * NAME:	conn->connected()
+  * DESCRIPTION:	mark a connecting socket as active
+  */
+ int conn_connected(conn)
+ register connection *conn;
+ {
+     struct sockaddr sa;
+     int salen = sizeof(sa);
+ 
+     if (conn->fd == -1)
+ 	return -1;
+ 
+     if (!FD_ISSET(conn->fd, &writefds))
+ 	return 0;
+ 
+     /* Use getpeername() solely to check for errors. */
+     if (getpeername(conn->fd, &sa, &salen) < 0) {
+ 	FD_CLR(conn->fd, &wfds);
+ 	return -1;
+     }
+ 
+     FD_CLR(conn->fd, &wfds);
+     FD_SET(conn->fd, &rfds);
+     FD_SET(conn->fd, &ufds);
+     return 1;
+ }
+     
+ /*
+  * NAME:	conn->writable()
+  * DESCRIPTION:	check if a connection has been selected for writing
+  */
+ int conn_writable(conn)
+ register connection *conn;
+ {
+     return conn->fd >= 0 && FD_ISSET(conn->fd, &writefds);
+ }
+ 
+ /*
+  * NAME:	conn->del()
+  * DESCRIPTION:	delete a connection
+  */
+ void conn_del(conn)
+ register connection *conn;
+ {
+     if (conn->fd >= 0) {
+ 	close(conn->fd);
+ 	FD_CLR(conn->fd, &rfds);
+         FD_CLR(conn->fd, &ufds);
+ 	FD_CLR(conn->fd, &wfds);
+ 	FD_CLR(conn->fd, &readfds);
+ 	FD_CLR(conn->fd, &writefds);
+ 	conn->fd = -1;
+     }
+     conn->next = flist;
+     flist = conn;
+ }
+ 
+ /*
+  * NAME:	conn->wait()
+  * DESCRIPTION:	mark a connection as waiting for writability
+  */
+ void conn_wait(conn, wait)
+ register connection *conn;
+ register int wait;
+ {
+     if (conn->fd < 0) {
+ 	return;
+     }
+ 
+     if (wait) {
+ 	FD_SET(conn->fd, &wfds);
+     } else {
+ 	FD_CLR(conn->fd, &wfds);
+     }
+ }
+ 
+ /*
+  * NAME:        conn->block()
+  * DESCRIPTION: temporarily unselect (block) a connection with respect to
+  *              incoming data
+  */
+ void conn_block(conn, flag)
+ register connection *conn;
+ register int flag;
+ {
+     if (conn->fd < 0) {
+         return;
+     }
+ 
+     if (flag) {
+         FD_CLR(conn->fd, &ufds);
+         FD_CLR(conn->fd, &readfds);
+     } else {
+         FD_SET(conn->fd, &ufds);
+     }
+ }
+ 
+ /*
+  * NAME:	conn->select()
+  * DESCRIPTION:	wait for input from connections
+  */
+ int conn_select(t, mtime)
+ Uint t;
+ unsigned int mtime;
+ {
+     struct timeval timeout;
+     int retval;
+ 
+     /*
+      * First, check readability and writability for binary sockets with pending
+      * data only.
+      */
+     memcpy(&readfds, &ufds, sizeof(fd_set));
+     memcpy(&writefds, &wfds, sizeof(fd_set));
+     if (mtime != 0xffff) {
+ 	timeout.tv_sec = t;
+ 	timeout.tv_usec = mtime * 1000L;
+ 	retval = select(maxfd + 1, &readfds, &writefds, (fd_set *) NULL,
+ 			&timeout);
+     } else {
+ 	retval = select(maxfd + 1, &readfds, &writefds, (fd_set *) NULL,
+ 			(struct timeval *) NULL);
+     }
+     if (retval < 0) {
+         FD_ZERO(&readfds);
+     }
+ 
+     /*
+      * Now check writability for all sockets in a polling call.
+      */
+     memcpy(&xxxfds, &rfds, sizeof(fd_set));
+     timeout.tv_sec = 0;
+     timeout.tv_usec = 0;
+     select(maxfd + 1, (fd_set *) NULL, &xxxfds, (fd_set *) NULL, &timeout);
+     return retval;
+ }
+ 
+ /*
+  * NAME:	conn->recvfrom()
+  * DESCRIPTION: read a datagram
+  */
+ int conn_recvfrom(conn, buf, size)
+ connection *conn;
+ char *buf;
+ int size;
+ {
+     struct sockaddr_in sin;
+     int len = sizeof(sin);
+     
+     if (conn->fd < 0) {
+ 	return -1;
+     }
+     if (!FD_ISSET(conn->fd, &readfds)) {
+ 	return 0;
+     }
+     size = recvfrom(conn->fd, buf, size, 0, (struct sockaddr *)&sin, &len);
+     memcpy(&(conn->sin), &sin, len);
+     strcpy(conn->host, inet_ntoa(sin.sin_addr));
+     conn->hostlen = strlen(conn->host);
+     return size;  /* Size may legitimately be 0 */
+ }
+ 
+ /*
+  * NAME:	conn->read()
+  * DESCRIPTION:	read from a connection
+  */
+ int conn_read(conn, buf, size)
+ connection *conn;
+ char *buf;
+ int size;
+ {
+     if (conn->fd < 0) {
+ 	return -1;
+     }
+     if (!FD_ISSET(conn->fd, &readfds) || size == 0) {
+ 	return 0;
+     }
+     size = read(conn->fd, buf, size);
+     return (size == 0) ? -1 : size;
+ }
+ 
+ /*
+  * NAME:	conn->sendto()
+  * DESCRIPTION: send a datagram
+  */
+ void conn_sendto(conn, data, size, host, port)
+ connection *conn;
+ char *data, *host;
+ int   size,  port;
+ {
+     static struct sockaddr_in sin;
+     if (conn->fd >= 0) {
+ 	sin.sin_family = AF_INET;
+ 	sin.sin_addr.s_addr = inet_addr(host);
+ 	sin.sin_port = htons(port);
+ 	if (sendto(conn->fd, data, size, 0, (struct sockaddr *)&sin,
+ 		   sizeof(sin)) < 0
+ 	    && errno == EMSGSIZE && !conn->extended) {
+ 	    conn_extend(conn);
+ 	    sendto(conn->fd, data, size, 0, (struct sockaddr *)&sin,
+ 		   sizeof(sin));
+ 	}
+     }
+ }
+ 
+ /*
+  * NAME:	conn->write()
+  * DESCRIPTION:	write to a connection; return the number of bytes written
+  */
+ int conn_write(conn, buf, size)
+ connection *conn;
+ char *buf;
+ register int size;
+ {
+     int len;
+ 
+     if (conn->fd < 0) {
+ 	return -1;
+     } else if (!FD_ISSET(conn->fd, &xxxfds)) {
+ 	return 0;
+     } else {
+ 	len = write(conn->fd, buf, size);
+ 	if (len < 0) {
+ 	    FD_CLR(conn->fd, &xxxfds);
+ 	    if (errno == EWOULDBLOCK) {
+ 		return 0;
+ 	    } else {
+ 		close(conn->fd);
+ 		FD_CLR(conn->fd, &rfds);
+                 FD_CLR(conn->fd, &ufds);
+ 		FD_CLR(conn->fd, &wfds);
+ 		FD_CLR(conn->fd, &readfds);
+ 		FD_CLR(conn->fd, &writefds);
+ 		conn->fd = -1;
+ 		return -1;
+ 	    }
+ 	}
+ 	if (len < size && !conn->extended) {
+ 	    conn_extend(conn);
+ 	}
+ 	return len;
+     }
+ }
+ 
+ /*
+  * NAME:	conn->ipnum()
+  * DESCRIPTION:	return the ip number of a connection
+  */
+ string *conn_ipnum(conn)
+ connection *conn;
+ {
+     return str_new(conn->host, conn->hostlen);
+ }
+ 
+ /*
+  * NAME:	conn->port()
+  * DESCRIPTION:	return the port number of a connection
+  */
+ Int conn_port(conn)
+ connection *conn;
+ {
+     return ntohs(conn->sin.sin_port);
+ }
diff -crN dgd-net/src/kfun/Makefile dgd+net/src/kfun/Makefile
*** dgd-net/src/kfun/Makefile	Thu Sep 21 12:06:51 2000
--- dgd+net/src/kfun/Makefile	Thu Sep 21 12:06:23 2000
***************
*** 23,29 ****
  $(OBJ): ../dgd.h ../config.h ../host.h ../alloc.h ../error.h ../str.h ../array.h
  $(OBJ): ../object.h ../xfloat.h ../interpret.h ../data.h
  std.o file.o: ../path.h ../editor.h
! std.o: ../comm.h ../call_out.h
  file.o: ../editor.h
  
  std.o: ../comp/node.h ../comp/control.h ../comp/compile.h
--- 23,29 ----
  $(OBJ): ../dgd.h ../config.h ../host.h ../alloc.h ../error.h ../str.h ../array.h
  $(OBJ): ../object.h ../xfloat.h ../interpret.h ../data.h
  std.o file.o: ../path.h ../editor.h
! std.o: ../comm.net.h ../call_out.h
  file.o: ../editor.h
  
  std.o: ../comp/node.h ../comp/control.h ../comp/compile.h
diff -crN dgd-net/src/kfun/std.c dgd+net/src/kfun/std.c
*** dgd-net/src/kfun/std.c	Thu Sep 21 12:07:00 2000
--- dgd+net/src/kfun/std.c	Thu Sep 21 12:06:23 2000
***************
*** 1,7 ****
  # ifndef FUNCDEF
  # include "kfun.h"
  # include "path.h"
! # include "comm.h"
  # include "call_out.h"
  # include "editor.h"
  # include "node.h"
--- 1,7 ----
  # ifndef FUNCDEF
  # include "kfun.h"
  # include "path.h"
! # include "comm.net.h"
  # include "call_out.h"
  # include "editor.h"
  # include "node.h"
***************
*** 274,291 ****
  
      obj = OBJW(f->sp->oindex);
      if (obj->flags & O_USER) {
! 	if (f->data->plane->level != 0) {
! 	    error("User object destructed in atomic function (cannot undo yet)");
! 	}
  	comm_close(f, obj);
      }
      if (obj->flags & O_EDITOR) {
! 	if (f->data->plane->level != 0) {
! 	    error("Editor object destructed in atomic function");
! 	}
  	ed_del(obj);
      }
      o_del(obj, f);
      return 0;
  }
  # endif
--- 274,294 ----
  
      obj = OBJW(f->sp->oindex);
      if (obj->flags & O_USER) {
!         if (f->data->plane->level != 0) {
!             error("User object destructed in atomic function (cannot undo yet)");
!         }
  	comm_close(f, obj);
      }
      if (obj->flags & O_EDITOR) {
!         if (f->data->plane->level != 0) {
!             error("Editor object destructed in atomic function");
!         }
  	ed_del(obj);
      }
      o_del(obj, f);
+     if (obj->flags & O_USER) {
+ 	comm_close(f, obj);
+     }
      return 0;
  }
  # endif
***************
*** 430,464 ****
  register frame *f;
  {
      object *obj;
  
      obj = OBJR(f->sp->oindex);
!     if (obj->flags & O_USER) {
! 	PUT_STRVAL(f->sp, comm_ip_number(obj));
!     } else {
! 	*f->sp = nil_value;
!     }
!     return 0;
! }
! # endif
! 
! 
! # ifdef FUNCDEF
! FUNCDEF("query_ip_name", kf_query_ip_name, pt_query_ip_name)
! # else
! char pt_query_ip_name[] = { C_TYPECHECKED | C_STATIC, T_STRING, 1, T_OBJECT };
! 
! /*
!  * NAME:	kfun->query_ip_name()
!  * DESCRIPTION:	return the ip name of a user
!  */
! int kf_query_ip_name(f)
! register frame *f;
! {
!     object *obj;
! 
!     obj = OBJR(f->sp->oindex);
!     if (obj->flags & O_USER) {
! 	PUT_STRVAL(f->sp, comm_ip_name(obj));
      } else {
  	*f->sp = nil_value;
      }
--- 433,443 ----
  register frame *f;
  {
      object *obj;
+     register string *ipnum;
  
      obj = OBJR(f->sp->oindex);
!     if ((obj->flags & O_USER) && (ipnum = comm_ip_number(obj))) {
! 	PUT_STRVAL(f->sp, ipnum);
      } else {
  	*f->sp = nil_value;
      }
***************
*** 468,492 ****
  
  
  # ifdef FUNCDEF
- FUNCDEF("users", kf_users, pt_users)
- # else
- char pt_users[] = { C_STATIC, T_OBJECT | (1 << REFSHIFT), 0 };
- 
- /*
-  * NAME:	kfun->users()
-  * DESCRIPTION:	return the array of users
-  */
- int kf_users(f)
- register frame *f;
- {
-     PUSH_ARRVAL(f, comm_users(f->data));
-     i_add_ticks(f, f->sp->u.array->size);
-     return 0;
- }
- # endif
- 
- 
- # ifdef FUNCDEF
  FUNCDEF("strlen", kf_strlen, pt_strlen)
  # else
  char pt_strlen[] = { C_TYPECHECKED | C_STATIC, T_INT, 1, T_STRING };
--- 447,452 ----
***************
*** 733,812 ****
  
  
  # ifdef FUNCDEF
- FUNCDEF("send_message", kf_send_message, pt_send_message)
- # else
- char pt_send_message[] = { C_STATIC, T_INT, 1, T_MIXED };
- 
- /*
-  * NAME:	kfun->send_message()
-  * DESCRIPTION:	send a message to a user
-  */
- int kf_send_message(f)
- register frame *f;
- {
-     register object *obj;
-     int num;
- 
-     if (f->sp->type == T_STRING) {
- 	num = f->sp->u.string->len;
-     } else if (f->sp->type == T_INT) {
- 	num = 0;
-     } else {
- 	return 1;
-     }
- 
-     obj = f->obj;
-     if (obj->count != 0) {
- 	if (obj->flags & O_USER) {
- 	    if (f->data->plane->level != 0) {
- 		error("send_message() within atomic function (cannot undo yet)");
- 	    }
- 	    if (f->sp->type == T_INT) {
- 		num = comm_echo(obj, f->sp->u.number != 0);
- 	    } else {
- 		num = comm_send(obj, f->sp->u.string);
- 	    }
- 	} else if ((obj->flags & O_DRIVER) && f->sp->type == T_STRING) {
- 	    P_message(f->sp->u.string->text);
- 	}
-     }
-     if (f->sp->type == T_STRING) {
- 	str_del(f->sp->u.string);
-     }
-     PUT_INTVAL(f->sp, num);
-     return 0;
- }
- # endif
- 
- 
- # ifdef FUNCDEF
- FUNCDEF("send_datagram", kf_send_datagram, pt_send_datagram)
- # else
- char pt_send_datagram[] = { C_TYPECHECKED | C_STATIC, T_INT, 1, T_STRING };
- 
- /*
-  * NAME:	kfun->send_datagram()
-  * DESCRIPTION:	send a datagram to a user
-  */
- int kf_send_datagram(f)
- register frame *f;
- {
-     int num;
- 
-     if (f->obj->count != 0 && (f->obj->flags & O_USER)) {
- 	if (f->data->plane->level != 0) {
- 	    error("send_datagram() within atomic function (cannot undo yet)");
- 	}
- 	num = comm_udpsend(f->obj, f->sp->u.string);
-     }
-     str_del(f->sp->u.string);
-     PUT_INTVAL(f->sp, num);
-     return 0;
- }
- # endif
- 
- 
- # ifdef FUNCDEF
  FUNCDEF("block_input", kf_block_input, pt_block_input)
  # else
  char pt_block_input[] = { C_TYPECHECKED | C_STATIC, T_VOID, 1, T_INT };
--- 693,698 ----
***************
*** 1046,1051 ****
--- 932,1203 ----
  	a = conf_object(f->data, OBJR(f->sp->oindex));
      }
      PUT_ARRVAL(f->sp, a);
+     return 0;
+ }
+ # endif
+ 
+ 
+ # ifdef FUNCDEF
+ FUNCDEF("users", kf_users, pt_users)
+ # else
+ char pt_users[] = { C_STATIC, T_OBJECT | (1 << REFSHIFT), 0 };
+ 
+ /*
+  * NAME:      kfun->users()
+  * DESCRIPTION:       return the array of connection objects
+  */
+ int kf_users(f)
+ register frame *f;
+ {
+     PUSH_ARRVAL(f, comm_connections(f->data));
+     i_add_ticks(f, f->sp->u.array->size);
+     return 0;
+ }
+ # endif
+ 
+ 
+ # ifdef FUNCDEF
+ FUNCDEF("ports", kf_ports, pt_ports)
+ # else
+ char pt_ports[] = { C_STATIC, T_OBJECT | (1 << REFSHIFT), 0 };
+ 
+ /*
+  * NAME:      kfun->ports()
+  * DESCRIPTION:       return the array of ports
+  */
+ int kf_ports(f)
+ register frame *f;
+ {
+     PUSH_ARRVAL(f, comm_ports(f->data));
+     i_add_ticks(f, f->sp->u.array->size);
+     return 0;
+ }
+ # endif
+ 
+ # ifdef FUNCDEF
+ FUNCDEF("send_message", kf_send_message, pt_send_message)
+ # else
+ char pt_send_message[] = { C_STATIC, T_INT, 1, T_MIXED };
+ 
+ /*
+  * NAME:      kfun->send_message()
+  * DESCRIPTION:       send a message to a user
+  */
+ int kf_send_message(f)
+ register frame *f;
+ {
+     register object *obj;
+     int result;
+ 
+     if (f->sp->type != T_STRING && f->sp->type != T_INT) {
+ 	return 1;
+     }
+ 
+     result = 0;
+     obj = cframe->obj;
+     if (obj->count != 0) {
+ 	if ((obj->flags & O_DRIVER) && f->sp->type == T_STRING) {
+ 	    P_message(f->sp->u.string->text);
+ 	} else if (obj->flags & O_USER) {
+             if (f->data->plane->level != 0) {
+                 error("send_message() within atomic function (cannot undo yet)");
+             }
+ 	    if (f->sp->type == T_INT) {
+ 		if (f->sp->u.number == -1) {
+ 		    result = comm_check(obj);
+ 		} else {
+ 		    comm_echo(obj, f->sp->u.number != 0);
+ 		}
+ 	    } else {
+ 		result = comm_send(obj, f->sp->u.string);
+ 	    }
+ 	}
+     }
+     if (f->sp->type == T_STRING) {
+ 	str_del(f->sp->u.string);
+     }
+     PUT_INTVAL(f->sp, result);
+     return 0;
+ }
+ # endif
+ 
+ # ifdef FUNCDEF
+ FUNCDEF("open_port", kf_open_port, pt_open_port)
+ # else
+ char pt_open_port[] = { C_TYPECHECKED | C_STATIC | C_KFUN_VARARGS,
+ 			T_VOID, 2, T_STRING | T_VARARGS, T_INT };
+ 
+ /*
+  * NAME:      kfun->open_port()
+  * DESCRIPTION:       start listening on a port
+  */
+ int kf_open_port(f, nargs)
+ register frame *f;
+ int nargs;
+ {
+     object *obj;
+     char *str;
+     int protocol;
+     Int port;
+ 
+     if (nargs == 0) {
+ 	return -1;
+     }
+ 
+     obj = cframe->obj;
+     if (obj->count == 0) {
+ 	error("open_port() from destructed object");
+     }
+ 
+     if (nargs == 2) {
+ 	port = (f->sp++)->u.number;
+ 	/*
+ 	* Verify the port-number for being in the right range.
+ 	*/
+ 	if (port < 1 || port > 0xffff) {
+ 	    return 2;
+ 	}
+     } else {
+ 	port = -1;
+     }
+ 
+     str = f->sp->u.string->text;
+     if (strcmp(str, "tcp") == 0) {
+ 	protocol = PRC_TCP;
+     } else if (strcmp(str, "udp") == 0) {
+ 	protocol = PRC_UDP;
+     } else if (strcmp(str, "telnet") == 0) {
+ 	protocol = PRC_TELNET;
+     } else {
+ 	return 1;
+     }
+ 
+     if (obj->flags & (O_USER | O_EDITOR)) {
+ 	error("object already used for socket or editor");
+     }
+     comm_listen(f, obj, port, protocol);
+     str_del(f->sp->u.string);
+     *f->sp = nil_value;
+     return 0;
+ }
+ # endif
+ 
+ # ifdef FUNCDEF
+ FUNCDEF("connect", kf_connect, pt_connect)
+ # else
+ char pt_connect[] = { C_TYPECHECKED | C_STATIC | C_KFUN_VARARGS, T_VOID, 3,
+ 		      T_STRING, T_INT | T_VARARGS, T_STRING };
+ 
+ /*
+  * NAME:      kfun->connect()
+  * DESCRIPTION:       initiate an ip connection
+  */
+ int kf_connect(f, nargs)
+ register frame *f;
+ int nargs;
+ {
+     object *obj;
+     char *str, *ipnr;
+     int protocol, d1, d2, d3, d4;
+     Int port;
+ 
+     if (nargs < 2) {
+ 	return -1;
+     }
+ 
+     obj = cframe->obj;
+     if (obj->count == 0) {
+ 	error("connect() from destructed object");
+     }
+ 
+     if (nargs == 3) {
+ 	str = f->sp->u.string->text;
+ 	if (strcmp(str, "tcp") == 0) {
+ 	    protocol = PRC_TCP;
+ 	} else if (strcmp(str, "telnet") == 0) {
+ 	    protocol = PRC_TELNET;
+ 	} else {
+ 	    return 3;
+ 	}
+ 	str_del(f->sp->u.string);
+ 	f->sp++;
+     } else {
+ 	protocol = PRC_TCP;
+     }
+ 
+     /*
+      * Verify the ipnumber for having the right format.
+      */
+     ipnr = f->sp[1].u.string->text;
+     if (sscanf(ipnr, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) < 4) {
+ 	return 1;
+     }
+     if (d1 < 0 || d1 > 0xff || d2 < 0 || d2 > 0xff ||
+ 	d3 < 0 || d3 > 0xff || d4 < 0 || d4 > 0xff) {
+ 	return 1;
+     }
+ 
+     /*
+      * Verify the port-number for being in the right range.
+      */
+     port = f->sp->u.number;
+     if (port < 1 || port > 0xffff) {
+ 	return 2;
+     }
+ 
+     if (obj->flags & (O_USER | O_EDITOR)) {
+ 	error("object already used for socket or editor");
+     }
+     comm_connect(obj, f->sp[1].u.string->text, port, protocol);
+     f->sp++;
+     str_del(f->sp->u.string);
+     *f->sp = nil_value;
+     return 0;
+ }
+ # endif
+ 
+ # ifdef FUNCDEF
+ FUNCDEF("send_datagram", kf_send_datagram, pt_send_datagram)
+ # else
+ char pt_send_datagram[] = { C_TYPECHECKED | C_STATIC, T_VOID, 3,
+ 			    T_STRING, T_STRING, T_INT };
+ 
+ /*
+  * NAME:      kfun->send_datagram()
+  * DESCRIPTION:       send a packet to a specific host and port (udp only)
+  */
+ int kf_send_datagram(f)
+ register frame *f;
+ {
+     object *obj;
+ 
+     obj = cframe->obj;
+     if (obj->count != 0 && (obj->flags & O_USER)) {
+ 	int   d1, d2, d3, d4;
+ 	char *ipnr;
+ 	Int  port;
+ 
+         if (f->data->plane->level != 0) {
+             error("send_datagram() within atomic function (cannot undo yet)");
+         }
+ 	ipnr = f->sp[1].u.string->text;
+ 	if (sscanf(ipnr, "%d.%d.%d.%d", &d1, &d2, &d3, &d4) < 4) {
+ 	    return 2;
+ 	}
+ 	if (d1 < 0 || d1 > 0xff || d2 < 0 || d2 > 0xff ||
+ 	    d3 < 0 || d3 > 0xff || d4 < 0 || d4 > 0xff) {
+ 	    return 2;
+ 	}
+ 	port = f->sp->u.number;
+ 	if (port < 1 || port > 0xffff) {
+ 	    return 3;
+ 	}
+ 	comm_sendto(obj, f->sp[2].u.string, f->sp[1].u.string->text,
+ 		    f->sp->u.number);
+     }
+     str_del((++(f->sp))->u.string);
+     str_del((++(f->sp))->u.string);
+     *f->sp = nil_value;
      return 0;
  }
  # endif

Site Stats

Snippets: 1791
Codebases: 571