📄 netutil.c
字号:
/*replace sizeof(ETHERHDR) with ETHHDRLEN!!*/
#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 "..\Target\44blib.h"
#include "..\target\def.h"
#define CFGDELIMS " \t\r\n" /* Config item delimiters */
#define MAXCFGLINE 90 /* Max length of line in CFG file */
BYTE bcast[MACLEN] = {BCASTADDR}; /* Broadcast MAC addr */
BYTE zermac[MACLEN]; /* All-zero MAC addr */
extern int netdebug; /* Net packet display flags */
/*#if WIN32
long dir_handle=-1L;
struct _finddata_t dir_block;
#else
struct ffblk dir_block;
#endif*/
/* 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 -= ETHHDRLEN;
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 ? ETHHDRLEN : 0);
}
/* Get pointer to the data area of the given frame */
void *getframe_datap(GENFRAME *gfp)
{
return(&gfp->buff[dtype_hdrlen(gfp->g.dtype)]);
}
/* 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;
}
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>=ETHHDRLEN)
{
dlen = len - ETHHDRLEN;
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[], WORD pcol, WORD dlen)
{
ETHERHDR *ehp;
if (gfp->g.dtype & DTYPE_ETHER)
{
ehp = (ETHERHDR *)gfp->buff;
ehp->ptype = pcol;
memcpy(ehp->dest, dest, MACLEN);
swap_ether(gfp);
dlen += ETHHDRLEN;
}
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;
LWORD longtemp;
Uart_Printf(tx ? "\nTx%u /" : "\nRx%u \\", gfp->g.dtype & NETNUM_MASK);
Uart_Printf("len %u ", dlen);
data = (BYTE *)getframe_datap(gfp);
if (netdebug & 1) /* Verbose display? */
{
if (gfp->g.dtype & DTYPE_ETHER)
{
Uart_Printf("%s ", ethstr(&gfp->buff[tx ? 0 : MACLEN], temps));
type = swapw(*(WORD *)&gfp->buff[MACLEN*2]);
Delay(100);
if (type == 0x0806)
{
longtemp=((LWORD)(data[14])<<24)+((LWORD)(data[15])<<16)+((LWORD)(data[16])<<8)+(LWORD)(data[17]);
Uart_Printf("ARP %s ", ipstr(longtemp, temps));//ipstr(swapl(*(long *)&data[14]), temps));
longtemp=((LWORD)(data[24])<<24)+((LWORD)(data[25])<<16)+((LWORD)(data[26])<<8)+(LWORD)(data[27]);
Uart_Printf("-> %s ", ipstr(longtemp, temps));//ipstr(swapl(*(long *)&data[24]), temps));
}
}
else
Uart_Printf("------SLIP------- ");
if (type == 0x0800)
{
longtemp=((LWORD)(data[12])<<24)+((LWORD)(data[13])<<16)+((LWORD)(data[14])<<8)+(LWORD)(data[15]);
Uart_Printf("IP %s ", ipstr(longtemp,temps));//(swapl(*(long *)&data[12]), temps));
longtemp=((LWORD)(data[16])<<24)+((LWORD)(data[17])<<16)+((LWORD)(data[18])<<8)+(LWORD)(data[19]);
Uart_Printf("-> %s ", ipstr(longtemp,temps));//(swapl*(long *)&data[16]), temps));
pcol = *(data+9);
Uart_Printf(pcol==1 ? "ICMP" : pcol==6 ? "TCP" : pcol==17 ? "UDP" : "");
}
//Uart_Printf("\n");
}
if (netdebug & 2) /* Hex display? */
{
for (i=0; i<dlen; i++)
Uart_Printf(" %02X", data[i]);
Uart_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))
while((c=*str++)&&(c>=0x30)&&(c<=0x39))
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 */
/*int read_cfgstr_n(char *fname, int n, char *item, char *dest, int destlen)
{
FILE *in;
int ok=-1, len;
char *s, buff[MAXCFGLINE];
if ((in=fopen(fname, "rt")) != 0) // Open as text file
{ // Read a line at a time
while (ok<n && fgets(buff, MAXCFGLINE, in))
{
strlwr(buff); // Force to lower case
len = strcspn(buff, CFGDELIMS); // Get length of config ID
if (len==(int)strlen(item) && !strncmp(buff, item, len))
{ // If it matches my item..
s = skipspace(&buff[len]); // ..skip whitespace..
if (dest)
{ // ..get length excl. EOL chars
len = mini(strcspn(s, "\r\n"), destlen);
strncpy(dest, s, len); // Copy into destination
dest[len] = 0;
}
ok++;
}
}
fclose(in);
}
return(ok>=n);
}*/
/* 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 */
char *skiptoken(char *str, char *tok)
{
int n;
char *s=0;
n = strlen(tok);
if (n>0 && str && !strncmp(str, tok, n))
s = str + n;
return(s);
}
/* Return a pointer to the first char after any whitespace */
char *skipspace(char *str)
{
while (isspace(*str))
str++;
return(str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -