
/*
 *  NET.C
 *
 *	DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
 *
 *  NetWork raw device interface.  Replace with whatever interface you
 *  want.
 */

#include "dnet.h"
#include <sys/stat.h>

RcvInt()
{
    int n = read(0, RcvBuf + RcvData, RCVBUF - RcvData);
    if (n >= 0)
	RcvData += n;
    if (n <= 0)         /* disallow infinite fast-timeout select loops */
	RExpect = 0;
    if (DDebug && n < 0) {
	write(2, "RcvInt ERR\n", 11);
    }
}

static struct sgttyb	ttym;
static struct stat	Stat;

NetOpen()
{
    int async = 1;

    fstat(0, &Stat);
    fchmod(0, 0600);
    /*
    signal(SIGIO, RcvInt);
    */
    ioctl (0, TIOCGETP, &ttym);
    ttym.sg_flags |= RAW;
    ttym.sg_flags &= ~CBREAK;
    ttym.sg_flags &= ~ECHO;
    ioctl (0, TIOCSETP, &ttym);
    /*
    ioctl (0, FIOASYNC, &async);
    */
    ioctl (0, FIONBIO, &async);
}

NetClose()
{
    int async = 0;

    fchmod(0, Stat.st_mode);
    ioctl (0, FIONBIO, &async);
    /*
    ioctl (0, FIOASYNC, &async);
    */
    ioctl (0, TIOCGETP, &ttym);
    ttym.sg_flags &= ~RAW;
    ttym.sg_flags |= ECHO;
    ioctl (0, TIOCSETP, &ttym);
}

NetWrite(buf, bytes, expectreply)
ubyte *buf;
{
    if (expectreply)
	DidWrite = 1;
    if (DDebug)
	fprintf(stderr, "NETWRITE %08lx %ld\n", buf, bytes);
    gwrite(0, buf, bytes);
}

gwrite(fd, buf, bytes)
register char *buf;
register long bytes;
{
    register long n;
    while (bytes) {
	n = write(fd, buf, bytes);
	if (n > 0) {
	    bytes -= n;
	    buf += n;
	    continue;
	}
	if (errno == EINTR)
	    continue;
	if (errno == EWOULDBLOCK) {
	    fd_set fd_wr;
	    fd_set fd_ex;
	    FD_ZERO(&fd_wr);
	    FD_ZERO(&fd_ex);
	    FD_SET(fd, &fd_wr);
	    FD_SET(fd, &fd_ex);
	    select(fd+1, NULL, &fd_wr, &fd_ex, NULL);
	    continue;
	}
	if (errno == EPIPE)
	    return;
	dneterror("gwrite");
    }
}

