📄 net.c
字号:
/* Network interface functions for 'TCP/IP Lean' (c) Iosoft Ltd. 2000
This software is only licensed for distribution with the book 'TCP/IP Lean',
and may only be used for personal experimentation by the purchaser
of that book, on condition that this copyright notice is retained.
For commercial licensing, contact license@iosoft.co.uk
This is experimental software; use it entirely at your own risk. The author
offers no warranties of any kind, including its fitness for purpose. */
/*
** v0.01 JPB 19/1/00 Network code split off from TCPFS.C v0.08
** v0.02 JPB 20/1/00 Added support for multiple net drivers
** v0.03 JPB 20/1/00 Improved packet demultiplexing
** v0.04 JPB 28/1/00 Changed 'dispnet' to 'netdebug'
** v0.05 JPB 29/2/00 Fixed bug in non-Ethernet logging
** v0.06 JPB 20/3/00 Added LiteLink and modem support
** v0.07 JPB 23/3/00 Added support for multiple SLIP links
** v0.08 JPB 24/3/00 Fixed multi-channel SLIP interference
** v0.09 JPB 6/4/00 Fixed type mismatch in SLIP length
** v0.10 JPB 3/7/00 Revised header for book CD
*/
/* 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"
#include "mac.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
//extern int SendPacket(unsigned char*, int);
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=3; /* Flag to enable net packet display */
//int netdebug=0;
FILE *logfile; /* Handle for logfile */
int ndrivers; /* Number of initialised drivers */
int txfcount, rxfcount; /* Counts for 'frame drop' options */
extern UserBuff UserReceiveBuff;
extern GENFRAME genframe;
/* Private prototypes */
WORD no_driver(char *s);
void do_modem(char c);
/* Get a frame from the network, return length excl. hardware header */
int get_frame(GENFRAME *gfp)
{
// int rxlen;
if (UserReceiveBuff.head!=UserReceiveBuff.rear) //从缓冲区读数据
{
genframe.g.len=UserReceiveBuff.length[UserReceiveBuff.head];
genframe.g.dtype=DTYPE_ETHER; //以太网类型数据包
genframe.g.fragoff=0;
memcpy(genframe.buff,(char *)(&UserReceiveBuff.frame[UserReceiveBuff.head]),genframe.g.len);
//复制数据
// ShowFrame(&UserReceiveBuff.frame[UserReceiveBuff.head]); //打印以太网数据包
UserReceiveBuff.head=(UserReceiveBuff.head+1)%UserBuffSize;
return( genframe.g.len);
}
else
return(0);
// if ((rxlen=get_net(gfp)) > 0) /* If any packet received.. */
// {
// if (is_ether(gfp, rxlen)) /* If Ethernet.. */
// rxlen -= 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
if(SendPacket(gfp->buff,len))
return(len);
else
return(0);
}
/* Dummy initialisation function for unsupported drivers */
WORD no_driver(char *s)
{
printf("ERROR: %s driver not included in this version\n", s);
return(0);
}
/* 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;
printf("\nERROR Rx frame buffer corrupt!\n");
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)
{
printf(" Rx frame dropped for debug\n");
len = 0;
}
#endif
}
return(len);
}
/* Put packet onto network, given length */
WORD put_net(GENFRAME *gfp, WORD len)
{
if(SendPacket(gfp->buff,gfp->g.len))
return(len);
else
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -