/*--------------------------------------------------------------------------

			Networking for SICS
			
	This module implements some networking functionality based on 
	TCP/IP sockets.
	
	
	Mark Koennecke, October 1996
	
	Free for non-commercial use, no warranties taken!

	Added functions:
	    NETavailable
            NETReadTillTerm
        in order to support RS-232 connection through a terminal server.

	Mark Koennecke, October 2001
	
----------------------------------------------------------------------------*/
#ifndef NNNET
#define NNNET
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

	typedef struct __MKCHANNEL{
	                 int sockid;
	                 int iType;
	                 struct sockaddr_in adresse;
	                 long lMagic;
	               } mkChannel;
	               
#define NETMAGIC 29121993

/*========================= exported functions ============================*/

/*               C O N N E C T I O N    O R I E N T E D                    */

/**********************   OPENING  **************************************  */

	mkChannel *NETOpenPort(int iPort);
	/* opens a ServerPort for listening, returns NULL if failure,
	   else a valid mkChannel structure for the port
	*/
	
	mkChannel *NETAccept(mkChannel *self, int timeout);
	/* tries to accept a new connection on the Channel self
	   until timeout. If a connection can be built a new mkChannel
	   structure is returned, else NULL. With a negative value or 0 for
	   timeout this function blocks for an accept.
	*/
	
	mkChannel *NETConnect(char *name, int port);
	/* tries to open a client connection to the server specified by name 
	   and port. Returns NULL on failure, a struct else
	*/

        int NETInfo(mkChannel *self, char *pComposter, int iBufLen);
        /* Once a socket is connected it is possible to figure out
           which host the connection came from. Maximum iBufLen characters
           of hostname are copied to pComposter
        */                		                 

/* *********************** DATA TRANSFER ******************************** */

	int NETWrite(mkChannel *self, char *buffer, long lLen);
	/* writes data to socket self, returns True if succes,
	   false otherwise.
	*/
	
	long NETRead(mkChannel *self, char *buffer, long lLen, int timeout);
	/* reads data from socket self into buffer with max length lLen
	   waits maximum timeout for data. Returns -1 on error, 0 on no
	   data, and else the length of the data read. With a negative value
	   or 0 for timeout this function blocks for the read. 
	*/
        int NETAvailable(mkChannel *self, int timeout);
        /*
           returns 1 if data is pending on the port, 0 if none is
           pending.
	*/
        int NETReadTillTerm(mkChannel *self, int timeout, 
                    char *pTerm, char *pBuffer, int iBufLen);
        /*
	  reads data until one of the terminators defined in pTerm has
          been found. The data is copied into the buffer pBuffer. A
          maximum length of iBufLen characters is observed. The timeout
          parameter defines a maximum time to wait for a terminator to
          appear. NETReadTillTerm returns 1 on success, 0 on a timeout,
	  and a negative value if a network error occurred. Beware that 
          this may not work correctly if the wrong terminator is given.
          The last one is really needed.
	*/
/*  ********************* KILLING FIELD ******************************** */
	int NETClosePort(mkChannel *self);
	/* closes a port, do not forget to free the channel data-
	   structure afterwards, returns True on success, False else
	*/
	 
/*################## ConnectionLess functions ##########################*/
       mkChannel *UDPOpen(int iPort);
       /* opens a port connectionless communications.
       */
       mkChannel *UDPConnect(char *name, int iPort);
       /* connects a client for connectionless communication
       */
       
       /* can use NETClosePort */
       
       long UDPRead(mkChannel *self, char *buffer, long lLen, int timeout);
       int UDPWrite(mkChannel *self, char *buffer, long lLen); 

#endif
