📄 net.c
字号:
/* Network interface functions
Debug option: reverse the order in which IP fragments are sent */
#define SUBFIRST 0 /* Set non-zero to transmit subframes first */
/* Debug options to drop frames on transmit or receive */
#define TXDROP 0 /* Set non-zero to drop 1-in-N transmit frames */
#define RXDROP 0 /* Set non-zero to drop 1-in-N receive frames */
/* All drivers are supported under Borland C
** Packet driver isn't supported under Visual C
** Packet and SLIP drivers aren't supported under DJGPP */
//#ifdef __BORLANDC__/
//#define PKTD_SUPPORT 1
//#else
#define PKTD_SUPPORT 0
//#endif
//#ifdef DJGPP
#define SLIP_SUPPORT 0
//#else
//#define SLIP_SUPPORT 1
//#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/* Override the default circular buffer size used in TCPFS.H */
#define _CBUFFLEN_ 0x2000 /* Buffer size: MUST be power of 2 */
#include "netutil.h"
#include "net.h"
#include "ether.h"
//#include "serpc.h"
#if PKTD_SUPPORT
#include "pktd.h"
#else /* Dummy functions if PKTD not installed */
#define open_pktd(x) no_driver("packet")
#define close_pktd(x) no_driver("packet")
#define put_pktd(x, y, z) no_driver("packet")
#endif
#if !SLIP_SUPPORT /* Dummy functions if SLIP not installed */
#define open_slip(x, y) no_driver("SLIP")
#define close_slip(x) no_driver("SLIP")
#define put_slip(x, y, z) no_driver("SLIP")
#define receive_slip(x) no_driver("SLIP")
#endif
#define SLIP_END 0xc0 /* SLIP escape codes */
#define SLIP_ESC 0xdb
#define ESC_END 0xdc
#define ESC_ESC 0xdd
CBUFF rxpkts = {_CBUFFLEN_}; /* Rx and Tx circular packet buffers */
CBUFF txpkts = {_CBUFFLEN_};
//BYTE slipbuffs[MAXCOMMS][MAXSLIP]; /* SLIP buffers */
//WORD sliplens[MAXCOMMS]; /* ..and data lengths */
int netdebug; /* Flag to enable net packet display */
FILE *logfile; /* Handle for logfile */
int ndrivers; /* Number of initialised drivers */
int txfcount, rxfcount; /* Counts for 'frame drop' options */
/* Private prototypes */
WORD no_driver(char *s);
void do_modem(char c);
extern void Uart_Printf(char *fmt,...);
/* Get a frame from the network, return length excl. hardware header */
int get_frame(GENFRAME *gfp)
{
int rxlen;
if ((rxlen=get_net(gfp)) > 0) /* If any packet received.. */
{
if (is_ether(gfp, rxlen)) /* If Ethernet.. */
rxlen -= ETHHDRLEN;//sizeof(ETHERHDR); /* Subtract header length */
else if (!is_slip(gfp, rxlen)) /* If not SLIP.. */
rxlen = 0; /* Discard if unrecognised */
}
return(rxlen);
}
/* Put frame out onto the network; if sub-frame (fragment), send it as well */
int put_frame(GENFRAME *gfp, int len)
{
int ret=0, len1, len2;
GENFRAME *sfp;
len1 = gfp->g.fragoff ? gfp->g.fragoff : len; /* Get len of 2 fragments */
len2 = len - len1;
sfp = (GENFRAME *)&gfp->buff[gfp->g.fragoff]; /* ..and ptr to 2nd frag */
#if SUBFIRST
if (len2 > 0) /* Send sub-frame first.. */
ret = put_net(sfp, (WORD)len2);
if (len1 > 0) /* ..then main frame */
ret += put_net(gfp, (WORD)len1);
#else /* Or send main frame first */
if (len1 > 0)
ret = put_net(gfp, (WORD)len1);
if (len2 > 0)
ret += put_net(sfp, (WORD)len2);
#endif
return(ret);
}
/* Open network driver
** Return driver type, which is the driver config number (1st driver is 0)
** plus flags to indicate frame type (Ethernet, SLIP etc.). Return 0 if error */
WORD open_net(char *cfgstr)
{
WORD dtype=0, etype, stype;
int append;
char *s;
etype = ndrivers + DTYPE_ETHER; /* Ethernet driver type */
stype = ndrivers + DTYPE_SLIP; /* ..and SLIP driver type */
if (ndrivers >= MAXNETS)
Uart_Printf("ERROR: exceeded maximum number of network interfaces\n");
else if ((s = skiptoken(cfgstr, "ether")) != 0) /* Ethernet? */
{
cfgstr = skippunct(s);
if ((s = skiptoken(cfgstr, "snap")) != 0) /* ..with 802.3 SNAP hdr? */
{
etype |= DTYPE_SNAP;
cfgstr = skippunct(s);
} /* Direct hardware drive? */
if ((s = skiptoken(cfgstr, "ne")) != 0) /* ..NE2000-compatible */
{
dtype = open_etherne(s, (WORD)(etype|DTYPE_NE));
}
/*else if ((s = skiptoken(cfgstr, "3c")) != 0)// ..3COM 3C509
dtype = open_ether3c(s, (WORD)(etype|DTYPE_3C));
else if ((s = skiptoken(cfgstr, "pktd")) != 0)
dtype = open_pktd(etype | DTYPE_PKTD); // Ether Packet Driver? */
}
else if ((s = skiptoken(cfgstr, "slip")) != 0) /* SLIP interface? */
{
cfgstr = skippunct(s);
if ((s = skiptoken(cfgstr, "pc")) != 0) /* PC ser (direct/Win32) */
dtype = open_slip(s, stype);
else if ((s = skiptoken(cfgstr, "pktd")) != 0)
dtype = open_pktd(stype | DTYPE_PKTD); /* Packet driver SLIP */
}
if (dtype && (s=strchr(s, '>'))!=0) /* Log net Tx/Tx to file? */
{
if ((append = (*++s == '>'))!=0) /* If '>>' append to file */
s++;
s = skipspace(s); /* Open logfile */
Uart_Printf("%s logfile '%s'\n", append ? "Appending to" : "Creating", s);
if ((logfile=fopen(s, append ? "ab" : "wb"))==0)
Uart_Printf("ERROR: can't open logfile\n");
}
if (dtype) /* Bump up driver count */
ndrivers++;
return(dtype);
}
/* Close network driver */
void close_net(WORD dtype)
{
if (dtype & DTYPE_NE) /* Close NE2000-compat */
close_etherne(dtype);
/* else if (dtype & DTYPE_3C) // ..or 3C509
close_ether3c(dtype);
else if (dtype == DTYPE_SLIP) // ..or SLIP
close_slip(dtype);
else if (dtype & DTYPE_PKTD) // ..or packet driver
close_pktd(dtype);*/
if (logfile) /* Close logfile */
{
Uart_Printf("Closing logfile\n");
if (ferror(logfile))
Uart_Printf("\nERROR writing logfile\n");
fclose(logfile);
logfile = 0;
}
}
/* Dummy initialisation function for unsupported drivers */
WORD no_driver(char *s)
{
Uart_Printf("ERROR: %s driver not included in this version\n", s);
return(0);
}
/* Return pointer to my Ethernet addr, given driver type */
BYTE *ether_addr(WORD dtype)
{
BYTE *addr=(BYTE *)"-SLIP-";
if (dtype & DTYPE_NE)
addr = etherne_addr(dtype);
/* else if (dtype & DTYPE_3C)
addr = ether3c_addr(dtype);*/
#if PKTD_SUPPORT
else if (dtype & DTYPE_PKTD)
addr = etherpktd_addr(dtype);
#endif
return(addr);
}
/* Get frame from receive buffer, given pointer to a frame buffer.
** If driver type field in the frame buffer is non-zero, match that driver type
** If zero, match any driver type. Return data length (0 if no packet) */
WORD get_net(GENFRAME *gfp)
{
WORD len=0;
SNAPFRAME *sfp;
GENHDR gh={0,0,0};
LWORD mstim;
if (buff_dlen(&rxpkts) >= sizeof(GENHDR))
{ /* If frame in Rx pkt buffer.. */
buff_out(&rxpkts, (BYTE *)&gh, sizeof(gh)); /* Check frame hdr */
len = gh.len; /* ..ensure length is sensible */
if (len<1 || len>MAXFRAME)
{
rxpkts.out = rxpkts.trial = rxpkts.in;
Uart_Printf("\nERROR Rx frame buffer corrupt!\n");
Uart_Printf("Frame len=%u, type=%u\n", len, gh.dtype);
len = 0;
}
else if (gh.dtype==gfp->g.dtype || !gfp->g.dtype)
{ /* Driver type matches or is 0 */
buff_out(&rxpkts, gfp->buff, len); /* Store frame in buffer */
if (!gfp->g.dtype) /* If no driver type in buffer.. */
gfp->g = gh; /* ..load in complete header */
gfp->g.len = len; /* Set length in header */
if (netdebug) /* Display raw frame */
disp_frame(gfp, len, 0);
if (logfile) /* Log raw frame */
{
//mstim = mstime();
fwrite("GET ", 1, 4, logfile);
fwrite(&mstim, 1, 4, logfile);
fwrite(gfp, 1, len+sizeof(GENHDR), logfile);
}
if (gfp->g.dtype&DTYPE_ETHER && /* If Ethernet frame.. */
gfp->buff[MACLEN*2] < 6) /* ..and Pcol less than 600h */
{ /* ..might be 802.3 SNAP */
sfp = (SNAPFRAME *)gfp->buff;
if (len>sizeof(SNAPHDR) && sfp->s.lsap==0xaaaa)
{ /* If so, convert to DIX */
len -= sizeof(SNAPHDR);
memmove(&sfp->e.ptype, &sfp->s.ptype, len);
}
gfp->g.len = len; /* Re-set length in header */
}
}
else
{ /* Wrong driver type: discard pkt */
buff_out(&rxpkts, 0, len);
len = 0;
}
#if RXDROP
if (++txfcount % RXDROP == 0)
{
Uart_Printf(" Rx frame dropped for debug\n");
len = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -