#ifndef KEYLIB_H
#define KEYLIB_H 1

#ifdef __cplusplus
extern "C" {
#endif
//#define TEST_LAP 1

#ifdef TEST_LAP
#define KEYSERVER "lap:7025"
#else
#define KEYSERVER "netwinsite.com:80"
#endif

/*
README, - Instructions on implementing keylib in your application

#define SECRET_FILE "kfile.dat"  REPLACE WITH A FILE YOU HAVE WRITE ACCESS TO
#define PROD_NAME "dnews"  REPLACE WITH YOUR PRODUCT NAME
 
Call	
	keylib_imsg(imsg)
on startup, imsg is a logging function.

On startup and when each user logs in your application should call:
	    r = keylib_check_file(SECRET_FILE,PROD_NAME,&ulimit,reason);
	    if (!r) printf("License key faulty %s\n",reason);
	    if (nusers>ulimit) printf("User limit exceeded\n");

When a user wants to activate a registration number they will give you a 'regstring' and 
'emailstring' you process it like this:
	if (!keylib_activate_get(SECRET_FILE,PROD_NAME,regstring,emailstring,key,reason)) {
		printf("Unable to get license key from KEY server, reason follows:\n");
		printf("%s\n",reason);
		return 0;
	}
	printf("Loaded license key from server (%s) (%s)\n",key,reason);
	r = keylib_check_file(SECRET_FILE,PROD_NAME,&ulimit,reason);
	if (!r) printf("License key faulty %s\n",reason);
*/

enum {
	KEYLIB_DNEWS=1,
	KEYLIB_DMAIL,
	KEYLIB_SURGEMAIL,
	KEYLIB_SURGEFTP,
	KEYLIB_WEBMAIL_NOT_IMPLEMENTED,
	KEYLIB_CWMAIL,
	KEYLIB_DMAILWEB,
	KEYLIB_WATCHDOG,
	KEYLIB_WEBNEWS,
	KEYLIB_DBABBLE,

	KEYLIB_FERNGATE,
	KEYLIB_SMSGATE,
	KEYLIB_SURGELDAP,
	KEYLIB_SURGENEWS,
	KEYLIB_AVAST,

	KEYLIB_SURGEPLUS,

	KEYLIB_LAST
};


typedef struct KNAMES {char *name; int id;} Knames;
static Knames knames[]={
	"DNews",KEYLIB_DNEWS,
//	"DMail",KEYLIB_DMAIL,
	"SurgeMail",KEYLIB_SURGEMAIL,
//	"SurgeFTP",KEYLIB_SURGEFTP,
//	"WebMail",KEYLIB_WEBMAIL,
	"WebNews",KEYLIB_WEBNEWS,
	"CWMail",KEYLIB_CWMAIL,
	"DMailWeb",KEYLIB_DMAILWEB,
	"WatchDog",KEYLIB_WATCHDOG,
//	"DBabble",KEYLIB_DBABBLE,
	"Ferngate",KEYLIB_FERNGATE,
	"SmsGate",KEYLIB_SMSGATE,
	"SurgeLDAP",KEYLIB_SURGELDAP,
	"SurgeNews",KEYLIB_SURGENEWS,
	"avast",KEYLIB_AVAST,
	"SurgePlus",KEYLIB_SURGEPLUS,
	NULL,KEYLIB_LAST
};

/*
Feature table	Mailling list, ftp,  Officemessenger, smsnotify.
Feature Table		Usenet	XSpam	Virus,  SSL,  OfficeMessage,  WebMail,  Domain Limit, User Limit

SurgeMail-Enterprise	y	y	y	y	y		y		none	none
SurgeMail-ISP		y	y	n	y	y		y		none	10 million
SurgeMail-Home		n	n	n	n	y		y		1	100
SurgeMail-Workplace 


  ------------------
User limit		2,000	6,000	2,0000	Unlimited
SurgeMail Enterprise   $4997	$5997	$9997	$17000

User Limit	1,000	5,000	10,000	100,000	1,000,000  10,000,000
SurgeMail ISP	$997	$1497	$1997	$4997	$9997	   $19997	

User Limit	5	100	500	1000
SurgeMail Work	$497	$997	$1997	$2997
		
User Limit	5	10
SurgeMail Home	$Free	$95

  -------------------------------------------------------------------------------------
			

  Usenet - low volume non binary sucking feed $x/month
  XSpam - weekly auto updated spam filter 

They download one of those three.
	The download form contains the 'name' only - surgemail
	The purchase form selects the actiual product code. and lists all
	with that name in the 'desc'

	When license generated, it will be for the matching product 
		name matches, and  list is empty, and code is in list of this bndle

*/

enum {
	KF_NOSSL = 1,		/* Disable SSL features */
	KF_DOMLIMIT = 2,	/* Limit virtual domains */
	KF_FREE = 4,		/* Free registration - enforce advertising features */
	KF_2GIG = 8,		/* 2 GIG USER LIMIT for dnews */
	KF_VIRUS = 16		/* User has purchased anti virus license */
};
/* Use these functions to determine if flags were set or not */
int keylib_nossl(void);
int keylib_domlimit(void);
int keylib_free(void);
int keylib_gethostid(char *kfile,char *reason);
int keylib_imsg(int (*ximsg)(const char *arg,...));
char *keylib_two(char *p1, char *p2);
int keylib_isflag(int x);



typedef struct PRODUCT {
	char *code;  char *list; char *name; char *desc; 
	int flags; int ulimit; int price, support,upgrade;} Product;
static Product products[]={

	 "su_5", ""	,"SurgeMail", "SurgeMail Home (Mail+SSL+WebMail+Domains)"
		,  0, 5,0,45,0
	 ,"su_5a", ""	,"SurgeMail", "SurgeMail Home etc + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 5,87,45,45
	 ,"su_10", ""	,"SurgeMail", "SurgeMail (Mail+SSL+WebMail+Domains)"
		,  0, 10,145,87,58
	 ,"su_10a", ""	,"SurgeMail", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 10,292,175,116
	 ,"su_100", ""	,"SurgeMail", "SurgeMail (Mail+SSL+WebMail+Domains)"
		,  0, 100,370,222,148
	 ,"su_100a", ""	,"SurgeMail", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 100,717,430,172
	 ,"su_1000", ""	,"SurgeMail", "SurgeMail (Mail+SSL+WebMail+Domains)"
		,  0, 1000,470,282,188
	 ,"su_1000a", ""	,"SurgeMail", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 1000,917,550,366
	 ,"su_10000", ""	,"SurgeMail", "SurgeMail (Mail+SSL+WebMail+Domains)"
		,  0, 10000,570,342,228
	 ,"su_10000a", ""	,"SurgeMail", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 10000,1317,790,526
	 ,"su_100000", ""	,"SurgeMail", "SurgeMail (Mail+SSL+WebMail+Domains)"
		,  0, 100000,670,402,268
	 ,"su_100000a", ""	,"SurgeMail", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 100000,1517,910,606
	 ,"su_inf", ""	,"SurgeMail", "SurgeMail (Mail+SSL+WebMail+Domains)"
		,  0, 0,770,462,308
	 ,"su_infa", ""	,"SurgeMail", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 0,1717,1030,686
	 ,"su_cluster", ""	,"SurgeMail", "SurgeMail Cluster (Mail+SSL+WebMail+Domains)"
		,  0, 0,1700,1020,680		
	 ,"su_test", ""	,"SurgeMail", "SurgeMail (internal use only)"
		,  KF_VIRUS, 0,5000,5000,5000		
//	,"db1", "","DBabble","DBabble"	,0,0,385,320,160
//	,"db2", "","DBabble","DBabble"	,0,100,185,170,90
//	,"db3", "","DBabble","DBabble"	,0,5,0,0,0
/*	,"wm1", "","WebMail","WebMail"	,0,0,485,320,160*/
	,"dn1", "","DNews","DNews"	,0,0,485,320,160
	,"dn2", "","DNews","DNews-2GIG"	,KF_2GIG,0,345,230,115
	,"dn3", "","DNews","DNews"	,0,2,220,145,72
	,"dn4", "","DNews","DNews"	,0,1,95,60,30
	,"dn5", "","DNews","DNews-Feed"	,0,10,345,230,115
	,"sdn1", "","DNews","DNews-Support-Upgrade",0,0,320,320,160
	,"sdn2", "","DNews","DNews-Upgrade-Only",0,0,160,320,160
	,"es3", "","DNews","DNEWS+Priority-Phone-Support",0,0,2995,2995,2995

	,"fg1", "","Ferngate","Ferngate (FG Server+FernWeb+Fern+Fernphone)"	,0,1000,2995,400,200
	
	,"sg1", "","SmsGate","SmsGate",0,5,75,50,25
	,"sg2", "","SmsGate","SmsGate",0,1000,270,170,70
	,"sg3", "","SmsGate","SmsGate",0,0,370,270,170

	,"sl1", "","SurgeLDAP","SurgeLDAP",0,0,770,370,170
	,"wd1", "","Watchdog","Watchdog",0,0,97,47,47

/* Root product codes used for bundles -HIDE means never show as a real product 
flags must be set correctly !!! */
	,"su5", "","SurgeMail","SurgeMail-HIDE",0,5,485,320,160
	,"su10", "","SurgeMail","SurgeMail-HIDE",0,10,485,320,160
	,"su20", "","SurgeMail","SurgeMail-HIDE",0,20,485,320,160
	,"su200", "","SurgeMail","SurgeMail-HIDE",0,200,485,320,160
	,"su500", "","SurgeMail","SurgeMail-HIDE",0,500,485,320,160
	,"su2000", "","SurgeMail","SurgeMail-HIDE",0,2000,485,320,160
	,"su6000", "","SurgeMail","SurgeMail-HIDE",0,2000,485,320,160
	,"su10000", "","SurgeMail","SurgeMail-HIDE",0,10000,485,320,160
	,"su20000", "","SurgeMail","SurgeMail-HIDE",0,10000,485,320,160
	,"su100000", "","SurgeMail","SurgeMail-HIDE",0,100000,485,320,160
	,"su1000000", "","SurgeMail","SurgeMail-HIDE",0,1000000,485,320,160
	,"su0", "","SurgeMail","SurgeMail-HIDE",0,0,485,320,160

	,"sn0", "","SurgeNews","SurgeNews",0,0,670,320,160
	,"sn1000", "","SurgeNews","SurgeNews",1000,0,470,320,160
	,"sn100", "","SurgeNews","SurgeNews",0,100,370,220,160
	,"sn10", "","SurgeNews","SurgeNews",0,10,270,170,80
	,"sn2", "","SurgeNews","SurgeNews",0,2,97,60,30

	 ,"av_5a", ""	,"avast", "SurgeMail Home etc + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 5,87,52,26
	 ,"av_10a", ""	,"avast", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 10,147,88,44
	 ,"av_100a", ""	,"avast", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 100,347,208,104
	 ,"av_1000a", "","avast", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 1000,447,268,134
	 ,"av_10000a", "","avast", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 10000,747,447,223
	 ,"av_100000a", ""	,"avast", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 100000,847,508,254
	 ,"av_infa", ""	,"avast", "SurgeMail + avast! Anti Virus (windows only)"
		,  KF_VIRUS, 0,947,567,270	

		
	 ,"sp_5", ""	,"SurgePlus", "SurgePlus Calender File sharing"
		,  0, 5,87,52,26
	 ,"sp_10", ""	,"SurgePlus", "SurgePlus Calender File sharing"
		,  0, 10,167,100,50
	 ,"sp_100", ""	,"SurgePlus", "SurgePlus Calender File sharing"
		,  0, 100,527,208,104
	 ,"sp_1000", ""	,"SurgePlus", "SurgePlus Calender File sharing"
		,  0, 1000,977,268,134
	 ,"sp_10000", ""	,"SurgePlus", "SurgePlus Calender File sharing"
		,  0, 10000,1077,447,223
	 ,"sp_100000", ""	,"SurgePlus", "SurgePlus Calender File sharing"
		,  0, 100000,1177,508,254
	 ,"sp_inf", ""	,"SurgePlus", "SurgePlus Calender File sharing"
		,  0, 0,1277,567,270	

	,NULL,NULL,NULL,0,0
};


typedef struct AKEY {int regid,regdate,host,prodid,flags,ulimit; } Akey;
int keylib_prodid(char *prod);
int keylib_activate_get(char *kfile,char *prod, char *regid, char *email, char *key, char *reason);
int keylib_setdebug(int x);
int keylib_recheck(char *prod, char *regid, char *reason);
int keylib_recheck_now(char *kfile, char *reason);
int keylib_regid(void);
int keylib_check_file(char *kfile, char *product, int *ulimit, char *reason);
int keylib_isok(char *reason);
int keylib_istemp(char *reason);
int keylib_expired(void);
int keylib_get_host(char *host);
int keylib_uncache(void);
time_t keylib_regdate(void);

int keylib_init(char *path,int prodid, int (*imsg)(char *s,...));
int keylib_fail(void); 
int keylib_warning(void);
int keylib_nusers(void);
int keylib_regid(void);
char *keylib_nice(char *product);
int keylib_imsg(int (*ximsg)(const char *arg,...));
char *keylib_encode(char *in, char *tmp);

int keylib_set_key(char *keystring);
int keylib_set_reg(int regid); /* Manually set by user, -2 = give me a new one anyway  */
int keylib_set_email(char *email); 
int keylib_from_file(char *kfile,char *key, char *prod, char *regid, char *email);

int keylib_get_reg(char *reason); /* Get local regid if exists */
char *keylib_get_email(char *email); 

int keylib_fetch_reg(char *reason); /* IF more than one offer list including 'new' one */
int keylib_fetch_key(char *reason); /* Try and fetch a key for this registration number */
Product *keylib_prod_find(char *prodcode);
char *keylib_regids(void);
int keylib_install(char *prod, char *email, char *regid, char *reason, char *newspass);
int keylib_pre_install(char *fname, char *email, char *regid, char *newspass);
int keylib_ask_install(char *fname, char *prod, char *dfltemail);

char *keylib_error(void);
char *keylib_hostname(void);

int keylib_store(char *kfile, char *key, char *email, char *product, char *reason);
char *keylib_nice(char *product);
int keylib_new(char *key, int regid, int prodid, int months, char *host, int xtime, int ulimit, int flags);
int keylib_check(char *key, int *ulimit, char *host, int nusers, int *regid, int *flags, char *product, char *reason);
char * keylib_ctime(time_t t, char *tmp);
int keylib_decode(char *key, Akey *k, time_t *reg_time, time_t *expire_time, int *badcrc);
void keylib_fromdate(int inr, time_t *t, int *months);
int keylib_todate(time_t t, int months);
int keylib_test(void);
int keylib_pack(Akey *k, unsigned char *keybin);
int keylib_unpack(unsigned char *keybin, Akey *k);
int keylib_cur_key(char *bf);

char *keylib_url(void);
char *keylib_email(void);

char *keylib_bintohex(char *bin, int binlen, char *out, int outlen);
char *keylib_hextobin(char *s, int *xlen, unsigned char *bf);
int keylib_nibtobin(char *s);
int keylib_ulimit_encode(int n);
int keylib_ulimit_decode(int n);
int keylib_host(char *xhost);
unsigned int keylib_crc(unsigned char *xin,int len);
#ifdef IS_KEYLIB_C
#include <stdarg.h>
#ifndef WIN32
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <sys/time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define socket_close close
#define socket_open open
#define socket_read read
#define socket_ioctl ioctl
#define socket_write write
#define NEED_BCOPY TRUE
#ifndef SOCKET
#define SOCKET short
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif

#else /* Windows */

#include <winsock.h>
#define socket_close closesocket
#define socket_ioctl ioctlsocket
#define socket_read(s,b,l) recv(s,b,l,0)
#define socket_write(s,b,l) send(s,b,l,0)

#endif

#define socket_shutdown shutdown



typedef struct CHAN {
	SOCKET sk;/* the socket */
	int id; /* socket's id */
	int block; /* Last state of channel (blocking true or false) */
	int noblock;/*Set(ie call kkk_noblock)before opening socket to make nonblocking socket*/
	char *inbf;
	int maxinbf;
	int inupto;/*Number of characters that have been read from the start of the channel 
	             buffer(inbf). This must be incremented or the buffer space cannot be
				 reused*/
	int inlen;/*The total number of characters in the channel buffer (inbf)*/
	int canclose;/* A flag that the socket should not be closed */
	int xerrno;/* Error number associated with last socket call */
	int lclport;/* The prefered port for the socket to bind to(set using kkk_lclport)*/
	int write_timeout; /* timeout in milliseconds for kkk_printf */
	int read_timeout; /* timeout in milliseconds for readline functions */
	char *buffer;	/* output buffering kkk_set_buffer... */
	int buffer_len;
	int isbuffer; 
	char myname[100];
	char theirname[100];
	char erradd[200];
	struct CHAN *next;/* Link your channels and make a list if you're that way inclined*/
} Chan;


/*kkk_hostname: gets the name of the local host
  Arguements: host: buffer to read name into(upto 190 chars can be read)
  Return: pointer to the buffer. 
  Remarks:returns 127.0.0.1 if error in determining host name occurs       */
char *kkk_hostname(char *host);

/*kkk_init: initiates use of socket DLL, must be called before using other tcp functions
  Arguements: vemsg: function by which to output initialization error messages
  Return: none.
  Remarks: only required on WIN32                                          */
void kkk_init(int (*vemsg)( const char *format ,...));

/*kkk_new: allocates new CHAN structure for a channel
  Arguements: c: pointer to channel to allocate structure for (*c must == NULL)
  Return: none.
  Remarks: error messages encountered are output through emsg function      */
void kkk_new(Chan **c);

/*kkk_accept: accepts an incoming connection attempt on a channel
  Arguements: listen:a channel that has been placed in a listening state
			  newchan: pointer to a new channel(*newchan must == NULL) that the new
					   connection will be made to
  Return: 1 if new connection successfully setup
          0 otherwise ( error number is returned in c->xerrno)
  Remarks: incoming connection attempts to connect at channel 'listen'. If a connection
		   attempt is detected the new channel is created and returned as newchan.											
           This is a blocking call.                                           */
int kkk_accept(Chan *listen, Chan **newchan);

/*kkk_listen: creates a new channel and places it in a state where it is listening 
			  for an incoming connection.
  Arguements: cc: pointer to new channel(*cc must == NULL) to be set in listening state
		      port: port to listen on
			  bindin: unused
			  reuse: if non zero, allow socket to be bound to a local address that
			         is already in use
  Return: 1 if socket successfully created and placed in listening state
          0 otherwise                                        					*/
int kkk_listen(Chan **cc, int port, char *bindin, int reuse);

/*kkk_lasterror: gets the last socket error that occured
  Arguements: c: c->xerrno is set to the value of the last error
  Return: value of the last socket error that occured	
  Remarks: the other functions call this when an error occurs, it is not required to
           call this function directly                    					*/
int kkk_lasterror(Chan *c);

/*kkk_release: puts channel into state where it may be closed
  Arguement: c: channel to be put into closable state
  Return: 1 if channel successfully put into state where it may be closed
          0 otherwise														*/
int kkk_release(Chan *c);

/*kkk_free: closes a channel and frees memory associated with it
  Arguements: cc: pointer to channel to be freed
  Return: 0 if cc is an invalid channel or contains and invalid socket
          1 otherwise                                                       */
int kkk_free(Chan **cc);

/*kkk_write: sends data on a connected channel
  Arguements: c:channel to send data over
              s:a buffer containing the data to be sent
			  len:length of buffer containing data
  Return: the number of bytes sent.
          This may block if len exceeds out going buffer size              */
int kkk_write(Chan *c, char *s, int len);
int kkk_write_timed(Chan *c, char *s, int len, int msecs);

/*kkk_read: reads incoming data on a channel
  Arguements: c: channel to read data from
	          s: buffer to store data
			  len: maximum number of bytes to read
  Return: the number of bytes read  
  Remarks: By default this is a blocking call.								*/
int kkk_read(Chan *c, char *s, int len);

/*kkk_read_timed: puts a time limit on an attempt to read incoming data on a channel
  Arguements: c: channel to read data from
			  s: buffer to store data
			  len: maximum number of bytes to read
			  msecs: maximum number of milliseconds to wait before receiving data
  Return:  the number of bytes read
		   -2 if the function timed out								    	*/
int kkk_read_timed(Chan *c, char *s, int len, int msecs);
/*kkk_peek_line_timed: puts a time limit on an attempt to read incoming data on a channel
                       data can be reread from subsequent calls.
  Arguements: c: channel to read data from
			  s: buffer to store data
			  len: maximum number of bytes to read
			  msecs: maximum number of milliseconds to wait before receiving data
  Return:  the number of bytes read
		   -2 if the function timed out								    	*/
int kkk_peek_line_timed(Chan *c, char *s, int len, int msecs);

/*kkk_printf: sends data on a connected channel using same format as printf
  Arguements: c: channel to send data over
              arglist: printf arguements 
  Return: 1 if data successfully sent
          0 otherwise
  Remarks: errors are output through emsg.
           All kkk_printf's are logged using emsg if kkk_setdebug(TRUE) has been called */
int kkk_printf(Chan *c, char * arg_list, ...);

/*kkk_open: opens a connection to a remote machine
  Arguements: cc: pointer to channel structure to connect with
              hostname: name of machine to connect to
			  port: port number on remote machine to connect to
			  bindout: unused
  Return: 1 if valid channel created
          0 otherwise
  Remarks: errors are variously outputed using emsg or printf               */
int kkk_open(Chan *cc, char *hostname,  int port, char *bindout);

/*kkk_readline: reads upto the first newline encountered in incoming data on a channel
Arguements: c: the channel to read from
              out: a buffer to read data into(data is also read into channel buffer)
			  len: maximum number of characters to read into out buffer(less may be read 
				   if there is insufficient room in the channel buffer)
Return: 0 if a time out was encountered before receiving a new line(data read so far
            will be in channel buffer, but not in 'out' arguement).
          1 otherwise
Remarks: This is nonblocking, but time outs may be significant if no data is queued ie
           in the order of seconds.
           The actual newline char and any directly proceding cariage returns are removed*/
int kkk_readline(Chan *c, char *out, int len);

/*kkk_error: gets a text message detailing a socket error number
  Arguements: c:channel on which a socket error has occured
              bf: buffer to read error message into
  Return: a pointer to bf                                                               */
char *kkk_error(Chan *c, char *bf);

/*kkk_getip: gets ip address of the peer to which a channel is connected
  Arguements c: connected channel from which to get the peers ip from
             ipbf: buffer to store ip address
  Return: 1 if peer ip successfully determined
          0 otherwise
  Remarks: Errors outputed through emsg                                                  */
int kkk_getip(Chan *c, char *ipbf);

/*kkk_getports: get local and remote port through which a channel is connected
  Arguements: c: connected channel
              local: buffer to store local port
			  remote: buffer to store remote port
  Return: 0 if either of the ports could not be determined
          1 otherwise
  Remarks: Error messages output through emsg										     */
int kkk_getports(Chan *c, int *local, int *remote);

/*kkk_getport: get local port through which a channel is connected
  Arguements: c: connected channel
	          local: buffer to store local port number in
  Return: 0 if port could not be determined
          1 otherwise
  Remarks: Error messages output through emsg                                            */
int kkk_getport(Chan *c, int *local);

/*kkk_isopen: determines if a channel believes it is connected
  Arguements: c: the channel to be queried
  Return: 0 if channel has invalid socket or is NULL
          1 otherwise	                                                                 */
int kkk_isopen(Chan *c);

/*kkk_nodelay: switches on/off the tcp Nagle algorithm for a given channel
  Arguements: c: the channel
              x: 1 for on, 0 for off
  Return: 1 if Nagle algorithm successfully switched on or off
          0 otherwise
  Remarks: Nagle algorithm buffers incoming data if previous data is still in transit and 
           buffers out going data until a full packet can be sent.                        */
int kkk_nodelay(Chan *c, int x);

/*kkk_getmyip: gets local ip of a channel
  Arguements: c: connected channel to get ip of
              ipbf: buffer to store ip
  Return: 1 if ip successfully determined
          0 otherwise   
  Remarks: errors outputed via emsg                                                      */
int kkk_getmyip(Chan *c, char *ipbf);

/*kkk_waitfor: wait a given length of time or until their is data queued on a socket for reading
  Arguements: sk: socket to wait on
              msec: number of millseconds to wait before timing out
  Return: 0 if times out
          1 if their is data queued to be read                                           */
int kkk_waitfor(Chan *c, int msec);

/*kkk_printf_nolog: sends data on a connected channel using same format as printf
  Arguements: c: channel to send data over
              arglist: printf arguements 
  Return: 1 if data successfully sent
          0 otherwise
  Remarks: errors are output through emsg.                                               */
int kkk_printf_nolog(Chan *c, char * arg_list, ...);

/*kkk_wait_write: waits for a channel to enter a writeable state
  Arguements: c: a connected channel
              msec: number of milliseconds to wait
  Return: 1 if check was successful
          0 if check failed or time out occured                                           */
int kkk_wait_write(Chan *c, int msec);

/*kkk_noblock: if called before openning channel, will create a non-blocking channel
  Arguements: c: the channel
  Return: 1 if channel successfully set to nonblocking
          0 otherwise                                                                     */
int kkk_noblock(Chan *c);

/*kkk_lookup: find ip address of a domain name
  Arguements: s: domain name(may contain port in name:port format)
              ip: buffer to return ip address in
  Return: 1 if ip successfully determined
          0 otherwise                                                                     */
int kkk_lookup(char *s, char *ip);

/*kkk_setdebug: adds extra debug functionality to tcp functions
  Arguements: x: 1 for debug on, 0 for off                                                */
void kkk_setdebug(int x);

/*kkk_setlclport: sets the desired port a channel should use
  Arguements: c: the channel
              x: port number
  Return: 1 if lclport successfully set
          0 otherwise	                                                                  */
int kkk_setlclport(Chan *c, int x);
int kkk_getname(char *ipnumber, char *ipname);
int kkk_open_reuse(Chan *cc, char *hostname,  int port, char *bindout);
void kkk_setlinger(Chan *c, int xon, int hunds);
int kkk_setblock(Chan *c, int t);
int kkk_handle_count(void);
int kkk_ssl_server(Chan *c, char *reason);
int kkk_waitfor(Chan *c, int msec);
int kkk_ssl_init(Chan *c, int isserver, Chan *inherit, char *reason);
int kkk_ssl_cmp(Chan *a, Chan *b);
int kkk_ssl_setkey(char *pk, char *keyfile);
char *kkk_read_dot(Chan *c, int *len, int *isend, int *isnulls, int secs, int isstart);
int kkk_readbf(Chan *c, int msec, int max);
int kkk_close_nice(Chan *c);
int kkk_wouldblock(Chan *c);
int kkk_check_open(Chan *c);
int kkk_not_connected(Chan *c);

int kkk_buffer_add(Chan *c, char *bf, int len);
void kkk_buffer_flush(Chan *c);
void kkk_buffer_set(Chan *c, int x);

int kkk_waitfor_list(Chan *first, int msec);
int kkk_set_read_timeout(Chan *c, int msec);
void kkk_init_dmsg(int (*vemsg)( const char *format ,...));

/* For reading long command lines, free vout using kkk_readline_free, max can
safely be a silly number, it's a limit not a preallocated amount */
int kkk_readline_big(Chan *c, char **vout, int max);
int kkk_readline_free(char *out);

#endif
#ifdef __cplusplus
}
#endif
#endif
