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

			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!
	
----------------------------------------------------------------------------*/
#ifndef NNNET
#define NNNET
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

	typedef struct {
	                 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. 
	*/
	
/*  ********************* 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
