📄 netutil.c
字号:
/* Network utility 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 28/10/99
** v0.02 JPB 15/11/99 Added support for Tx and Rx circular buffers
** v0.03 JPB 17/11/99 Fixed non-reentrant use of min() in circ buffer code
** v0.04 JPB 30/12/99 Added SLIP_SUPPORT option for DJGPP compatibility
** v0.05 JPB 31/12/99 Added support for IEEE 802.3 SNAP
** v0.06 JPB 7/1/00 Added 'maxlen' to net initialisation
** v0.07 JPB 17/1/00 Removed 'maxlen' again!
** v0.08 JPB 18/1/00 Moved get_frame and put_frame into this file
** v0.09 JPB 19/1/00 Added logfile
** Split off network functions into NET.C
** v0.10 JPB 21/3/00 Added directory search functions
** v0.11 JPB 23/3/00 Moved 'atoip' here from IP.C
** v0.12 JPB 29/3/00 Moved 'csum' here from IP.C
** v0.13 JPB 11/4/00 Added buff_infile()
** v0.14 JPB 12/4/00 Added find_filesize();
** v0.15 JPB 3/7/00 Revised header for book CD
*/
#include <stdio.h>
//#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "netutil.h"
#include "net.h"
#include "ether.h"
#include "uart.h"
#include "timer.h"
#define CFGDELIMS " \t\r\n" /* Config item delimiters */
#define MAXCFGLINE 90 /* Max length of line in CFG file */
//#define MACLEN 6
BYTE bcast[MACLEN] = {BCASTADDR}; /* Broadcast MAC addr */
BYTE zermac[MACLEN]; /* All-zero MAC addr */
extern int netdebug; /* Net packet display flags */
extern GENFRAME genframe;
LWORD ticks; //时间变量
extern TIME tm0;
//struct ffblk dir_block;
/* Return maximum length of the given frame */
int getframe_maxlen(GENFRAME *gfp)
{
return(gfp->g.dtype&DTYPE_ETHER ? MAXFRAME : MAXSLIP);
}
/* Return the Maximum Transmission Unit (i.e. data size) for the given frame */
WORD getframe_mtu(GENFRAME *gfp)
{
WORD mtu;
mtu = (WORD)getframe_maxlen(gfp);
if (gfp->g.dtype & DTYPE_ETHER)
{
mtu -= sizeof(ETHERHDR);
if (gfp->g.dtype & DTYPE_SNAP)
mtu -= sizeof(SNAPHDR);
}
return(mtu);
}
/* Return frame header length, given driver type */
WORD dtype_hdrlen(WORD dtype)
{
return(dtype&DTYPE_ETHER ? sizeof(ETHERHDR) : 0);
}
/* Get pointer to the data area of the given frame */
void *getframe_datap(GENFRAME *gfp)
{
return(&(gfp->buff[0xe]));
}
/* Get pointer to the source address of the given frame, 0 if none */
BYTE *getframe_srcep(GENFRAME *gfp)
{
ETHERHDR *ehp;
BYTE *srce=0;
if (gfp->g.dtype & DTYPE_ETHER) /* Only Ethernet has address */
{
ehp = (ETHERHDR *)gfp->buff;
srce = ehp->srce;
}
return(srce);
}
/* Copy the source MAC addr of the given frame; use broadcast if no addr */
BYTE *getframe_srce(GENFRAME *gfp, BYTE *buff)
{
BYTE *p;
p = getframe_srcep(gfp);
if (p)
memcpy(buff, p, MACLEN);
else
memcpy(buff, bcast, MACLEN);
return(p);
}
/* Get pointer to the destination address of the given frame, 0 if none */
BYTE *getframe_destp(GENFRAME *gfp)
{
ETHERHDR *ehp;
BYTE *dest=0;
if (gfp->g.dtype & DTYPE_ETHER) /* Only Ethernet has address */
{
ehp = (ETHERHDR *)gfp->buff;
dest = ehp->dest;
}
return(dest);
}
/* Copy the destination MAC addr of the given frame; use broadcast if no addr */
BYTE *getframe_dest(GENFRAME *gfp, BYTE *buff)
{
BYTE *p;
p = getframe_destp(gfp);
if (p)
memcpy(buff, p, MACLEN);
else
memcpy(buff, bcast, MACLEN);
return(p);
}
/* Get the protocol for the given frame; if unknown , return 0 */
WORD getframe_pcol(GENFRAME *gfp)
{
ETHERHDR *ehp;
WORD pcol=0;
if (gfp->g.dtype & DTYPE_ETHER) /* Only Ethernet has protocol */
{
ehp = (ETHERHDR *)gfp->buff;
//pcol = ehp->ptype;
pcol =gfp->buff[0xc]; //ren
pcol = (pcol<<8)+gfp->buff[0xd];
}
return(pcol);
}
/* Return non-zero if frame has a broadcast address */
int is_bcast(GENFRAME *gfp)
{
return(gfp->g.dtype&DTYPE_ETHER && !memcmp(gfp->buff, bcast, MACLEN));
}
/* Check Ethernet frame, given frame pointer & length, return non-0 if OK */
int is_ether(GENFRAME *gfp, int len)
{
int dlen=0;
if (gfp && (gfp->g.dtype & DTYPE_ETHER) && len>=sizeof(ETHERHDR))
{
dlen = len - sizeof(ETHERHDR);
swap_ether(gfp);
}
return(dlen);
}
/* Make a frame, given data length. Return length of complete frame
** If Ethernet, set dest addr & protocol type; if SLIP, ignore these */
int make_frame(GENFRAME *gfp, BYTE dest[], BYTE scr[], WORD pcol, WORD dlen)
{
ETHERHDR *ehp;
if (gfp->g.dtype & DTYPE_ETHER)
{
ehp =(ETHERHDR *)gfp->buff;
ehp->ptype = pcol;
memcpy(ehp->dest, dest, MACLEN);
memcpy(ehp->srce, scr, MACLEN);
swap_ether(gfp);
dlen += sizeof(ETHERHDR);
}
return(dlen);
}
/* Byte-swap an Ethernet frame, return header length */
void swap_ether(GENFRAME *gfp)
{
ETHERFRAME *efp;
efp = (ETHERFRAME *)gfp->buff;
efp->h.ptype = swapw(efp->h.ptype);
}
/* Check SLIP frame, return non-zero if OK */
int is_slip(GENFRAME *gfp, int len)
{
return((gfp->g.dtype & DTYPE_SLIP) && len>0);
}
/* Display SLIP or Ethernet frame (must be in network byte order) */
void disp_frame(GENFRAME *gfp, int dlen, int tx)
{
char temps[20];
BYTE *data, pcol;
WORD type=0x0800;
int i;
printf(tx ? "Tx%u /" : "Rx%u \\", gfp->g.dtype & NETNUM_MASK);
printf("len %u ", dlen);
data = (BYTE *)getframe_datap(gfp);
if (netdebug & 1) /* Verbose display?*/
{
if (gfp->g.dtype & DTYPE_ETHER)
{
printf("%s ", ethstr(&gfp->buff[tx ? 0 : MACLEN], temps));
// type = swapw(*(WORD *)&gfp->buff[MACLEN*2]);
type = ((WORD )gfp->buff[MACLEN*2]<<8)+gfp->buff[MACLEN*2+1];
if (type == 0x0806)
{
printf("ARP %d.%d.%d.%d",gfp->buff[0x1c],gfp->buff[0x1d],
gfp->buff[0x1e],gfp->buff[0x1f]);
printf("-> %d.%d.%d.%d\n",gfp->buff[0x26],gfp->buff[0x27],
gfp->buff[0x28],gfp->buff[0x29]);
// printf("ARP %s ", ipstr(swapl(*(long *)&data[14]), temps));
// printf("-> %s ", ipstr(swapl(*(long *)&data[24]), temps));
}
}
else
printf("------SLIP------- ");
if (type == 0x0800)
{
printf("IP %d.%d.%d.%d",gfp->buff[0x1a],gfp->buff[0x1b],
gfp->buff[0x1c],gfp->buff[0x1d]);
printf("-> %d.%d.%d.%d\n",gfp->buff[0x1e],gfp->buff[0x1f],
gfp->buff[0x20],gfp->buff[0x21]);
// printf("IP %s ", ipstr(swapl(*(long *)&data[12]), temps));
// printf("-> %s ", ipstr(swapl(*(long *)&data[16]), temps));
pcol = *(data+9);
printf(pcol==1 ? "ICMP" : pcol==6 ? "TCP" : pcol==17 ? "UDP" : "");
}
printf("\n");
}
if (netdebug & 2) /* Hex display? */
{
for (i=0; i<dlen; i++)
{ printf(" %02X",gfp->buff[i]);
if ((i%16)==15)
printf("\n");
}
printf("\n");
}
}
/* Convert IP address into a string */
char *ipstr(LWORD ip, char *s)
{
sprintf(s, "%lu.%lu.%lu.%lu",(ip>>24)&255,(ip>>16)&255,(ip>>8)&255,ip&255);
return(s);
}
/* Convert Ethernet address into a string (max 17 chars plus null) */
char *ethstr(BYTE *addr, char *str)
{
int i;
char *s=str;
if (!memcmp(addr, bcast, MACLEN))
strcpy(s, "----BROADCAST----");
else for (i=0; i<MACLEN; i++)
s += sprintf(s, i>0 ? ":%02x" : "%02x", *addr++);
return(str);
}
/* Convert string to IP addr: first digits form most-significant byte */
/*LWORD atoip(char *str)
{
LWORD ip=0L;
int i=4, n;
char c=1;
while (--i>=0 && c)
{
n = 0;
while (isdigit(c=*str++))
n = n*10 + c-'0';
ip += (LWORD)n << (i*8);
}
return(ip);
}*/
/* Retrieve the integer value of a config item. Return 0 if not found */
/*int read_cfgval(char *fname, char *item, int *valp)
{
char str[10];
int ok=0;
if ((ok = read_cfgstr(fname, item, str, sizeof(str)-1)) != 0)
{
if (valp)
*valp = atoi(str);
}
return(ok);
}*/
/* Return the config string for the given item. Return 0 if not found */
/*int read_cfgstr(char *fname, char *item, char *dest, int destlen)
{
return(read_cfgstr_n(fname, 0, item, dest, destlen));
}*/
/* Return the config string for the n'th item (1st item if n=0).
** Return 0 if not found */
/* Return non-zero if config item has option set (or 'all' options set) */
/*int read_cfgopt(char *fname, char *item, char *opt)
{
char *s, buff[MAXCFGLINE];
int n, ok=0;
if (read_cfgstr(fname, item, buff, MAXCFGLINE-1))
{
s = buff;
while (!ok && *s)
{
n = strcspn(s, CFGDELIMS); Get length of next option
ok = !strncmp(s, opt, n) || !strncmp(s, "all", n);
s = skippunct(s + n); /* Match option, skip to next
}
}
return(ok);
}*/
/* Check the given token is at the start of the string
** Return pointer to the first char after the token, 0 if token not found */
/* Return a pointer to the first char after any whitespace */
/*char *skipspace(char *str)
{
while (isspace(*str))
str++;
return(str);
}*/
/* Return a pointer to the first char after any whitespace or punctuation */
/*char *skippunct(char *str)
{
while (isspace(*str) || ispunct(*str))
str++;
return(str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -