/*
 * NAME:
 *	rwnet - read/write network
 *
 * DESCRIPTION:
 *	These routines provide a means of instantiating crypto or
 *	other hooks.  They are much the same routines I used in stelnet.
 *
 *
 * AUTHOR:
 *	Simon J. Gerraty <sjg@quick.com.au>
 */
/*
 *      @(#)Copyright (c) 1996 Simon J. Gerraty.
 *      
 *      This is free software.  It comes with NO WARRANTY.
 *      Permission to use, modify and distribute this source code 
 *      is granted subject to the following conditions.
 *      1/ that the above copyright notice and this notice 
 *      are preserved in all copies and that due credit be given 
 *      to the author.  
 *      2/ that any changes to this code are clearly commented 
 *      as such so that the author does not get blamed for bugs 
 *      other than his own.
 *      
 *      Please send copies of changes and bug-fixes to:
 *      sjg@quick.com.au
 */
#ifndef lint
static char RCSid[] = "$Id: rwnet.c,v 1.8 1997/02/08 07:13:03 sjg Exp $";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef USE_SSL
# ifdef SYS_SELECT_H
#   include <sys/select.h>
# endif

#include "x509.h"
#include "ssl.h"

#include "sslfd.h"

static SSL *ssl_con;
#endif

int (*readnet_hook)() = 0;
int (*writenet_hook)() = 0;


int
recv_net(fd, buf, nbytes, flags)
	int fd;
	unsigned char *buf;
	int nbytes;
	int flags;
{
	int n = 0;
#ifdef USE_SSL
	if (ssl_con = get_sslcon(fd))
		n = SSL_read(ssl_con, buf, nbytes);
	else
#endif
	n = recv(fd, buf, nbytes, flags);
	if (readnet_hook)
		readnet_hook(fd, buf, n, flags);
	return n;
}


int
send_net(fd, buf, nbytes, flags)
	int fd;
	unsigned char *buf;
	int nbytes;
	int flags;
{
	if (writenet_hook)
		writenet_hook(fd, buf, nbytes, flags);
	
#ifdef USE_SSL
	if (ssl_con = get_sslcon(fd))
		return SSL_write(ssl_con, buf, nbytes);
#endif
	return send(fd, buf, nbytes, flags);
}


int
read_net(fd, buf, nbytes)
	int fd;
	unsigned char *buf;
	int nbytes;
{
	int n = 0;
#ifdef USE_SSL
	if (ssl_con = get_sslcon(fd))
		n = SSL_read(ssl_con, buf, nbytes);
	else
#endif
	n = read(fd, buf, nbytes);
	if (readnet_hook)
		readnet_hook(fd, buf, n, 0);
	return n;
}


int
write_net(fd, buf, nbytes)
	int fd;
	unsigned char *buf;
	int nbytes;
{
	if (writenet_hook)
		writenet_hook(fd, buf, nbytes, 0);
	
#ifdef USE_SSL
	if (ssl_con = get_sslcon(fd))
		return SSL_write(ssl_con, buf, nbytes);
#endif
	return write(fd, buf, nbytes);
}
