#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"
#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 -= 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[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>=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[], 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 += 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]);
if (type == 0x0806)
{
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 %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", data[i]);
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 */
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. EO