📄 scratchp.c
字号:
}
else
{
printf("\nEcho response incorrect!\n");
connsig = SIG_STOP; /* If error, close connection */
}
}
}
}
else if (appstate == APP_ECHO_SERVER) /* If I'm an echo server.. */
{
if (usersig == SIG_USER_CLOSE) /* User closing connection? */
newappstate(APP_IDLE);
else if ((len = minw(buff_freelen(txb), TESTLEN))>0 &&
(len = buff_out(rxb, apptemp, len)) > 0)
buff_in(txb, apptemp, len); /* Else copy I/P data to O/P */
}
else if (appstate == APP_FILE_RECEIVER) /* If I'm receiving a file.. */
{
while (buff_try(rxb, &lenb, 1)) /* Get length byte */
{ /* If rest of block absent.. */
if (buff_untriedlen(rxb) < lenb)
{
buff_retry(rxb, 1); /* .. push length byte back */
break;
}
else
{
filelen += lenb;
buff_out(rxb, 0, 1); /* Check length */
if (lenb == 0) /* If null, end of file */
{
if (!fhandle || ferror(fhandle))
printf("ERROR writing file\n");
fclose(fhandle);
fhandle = 0;
newappstate(APP_IDLE);
}
else /* If not null, get block */
{
buff_out(rxb, apptemp, (WORD)lenb);
if (fhandle)
fwrite(apptemp, 1, lenb, fhandle);
}
}
}
}
else if (appstate == APP_FILE_SENDER) /* If I'm sending a file.. */
{ /* While room for another block.. */
while (fhandle && buff_freelen(txb)>=BLOCKLEN+2)
{ /* Get block from disk */
lenb = (BYTE)fread(apptemp, 1, BLOCKLEN, fhandle);
filelen += lenb;
buff_in(txb, &lenb, 1); /* Send length byte */
buff_in(txb, apptemp, lenb); /* ..and data */
if (lenb < BLOCKLEN) /* If end of file.. */
{ /* ..send null length */
buff_in(txb, (BYTE *)"\0", 1);
fclose(fhandle);
fhandle = 0;
newappstate(APP_IDLE);
}
}
}
return(connsig);
}
/* Do a directory of files, putting 1 name per line into O/P buffer
** Return number of files */
int do_dir(CBUFF *txb)
{
int done=0, count=0;
#ifndef WIN32
struct ffblk ffb;
done = findfirst("*.*", &ffb, 0);
while (!done)
{
count++;
buff_instr(txb, ffb.ff_name);
buff_instr(txb, "\r\n");
done = findnext(&ffb);
}
#else
long h;
struct _finddata_t fd;
h = _findfirst("*.*", &fd);
while (h!=-1L && !done)
{
count++;
buff_instr(txb, fd.name);
buff_instr(txb, "\r\n");
done = _findnext(h, &fd);
}
_findclose(h);
#endif
return(count);
}
/* Do a connection state transition, refresh timer, do diagnostic printout */
void newconnstate(int state)
{
if (state!=connstate)
{
if (statedebug)
printf("connstate %s\n", connstates[state]);
if (state != STATE_CONNECTED)
newappstate(APP_IDLE); /* If not connected, stop app. */
}
connstate = state;
errcount = 0;
timeout(&errtimer, 0); /* Refresh timeout timer */
}
/* Do an application state transition, do diagnostic printout */
void newappstate(int state)
{
if (statedebug && state!=appstate)
printf("appstate %s\n", appstates[state]);
appstate = state;
errcount = 0;
timeout(&errtimer, 0); /* Refresh timeout timer */
}
/* Display a signal name */
void disp_sig(int sig)
{
printf("%s ", signames[sig]);
}
/* Make a SCRATCHP packet given command, flags and string data */
int make_scratchpds(GENFRAME *nfp, BYTE *dest, char *cmd, BYTE flags, char *str)
{
return(make_scratchp(nfp, dest, cmd, flags, str, strlen(str)+1));
}
/* Make a SCRATCHP packet given command, flags and data */
int make_scratchp(GENFRAME *nfp, BYTE *dest, char *cmd, BYTE flags,
void *data, int dlen)
{
SCRATCHPKT *sp;
ETHERHDR *ehp;
int cmdlen=0;
sp = (SCRATCHPKT *)getframe_datap(&genframe);
sp->h.ver = SCRATCHPVER; /* Fill in the blanks.. */
sp->h.flags = flags;
sp->h.seq = txbuff.trial; /* Direct seq/ack mapping.. */
sp->h.ack = rxbuff.in; /* ..to my circ buffer pointers! */
if (cmd)
{
strcpy((char *)sp->data, cmd); /* Copy command string */
cmdlen = strlen(cmd) + 1;
}
sp->h.dlen = cmdlen + dlen; /* Add command to data length */
if (dlen && data) /* Copy data */
memcpy(&sp->data[cmdlen], data, dlen);
if (nfp->g.dtype & DTYPE_ETHER)
{
ehp = (ETHERHDR *)nfp->buff;
ehp->ptype = PCOL_SCRATCHP; /* Fill in more blanks */
memcpy(ehp->dest, dest, MACLEN);
}
diaghdrs[diagidx] = sp->h; /* Copy hdr into diagnostic log */
diaghdrs[diagidx].ver = DIAG_TX;
diagidx = (diagidx + 1) % NDIAGS;
return(sp->h.dlen+sizeof(SCRATCHPHDR)); /* Return length incl header */
}
/* Transmit a SCRATCHP packet. given length incl. SCRATCHP header */
int put_scratchp(GENFRAME *nfp, WORD txlen)
{
int len=0;
if (txlen >= sizeof(SCRATCHPHDR)) /* Check for min length */
{
if (pktdebug)
{
printf ("Tx ");
disp_scratchp(nfp);
printf(" ");
}
swap_scratchp(nfp); /* Byte-swap SCRATCHP header */
if (is_ether(nfp, txlen+sizeof(ETHERHDR)))
txlen += sizeof(ETHERHDR);
txcount++;
len = put_net(nfp, txlen); /* Transmit packet */
}
return(len);
}
/* Check for SCRATCHP, given frame pointer & length */
int is_scratchp(GENFRAME *nfp, int len)
{
WORD pcol;
/* SLIP has no protocol field.. */
pcol = getframe_pcol(nfp); /* ..so assume 0 value is correct */
return((pcol==0 || pcol==PCOL_SCRATCHP) && len>=sizeof(SCRATCHPHDR));
}
/* Byte-swap an SCRATCHP packet, return header length */
int swap_scratchp(GENFRAME *nfp)
{
SCRATCHPKT *sp;
sp = getframe_datap(nfp);
sp->h.dlen = swapw(sp->h.dlen);
sp->h.seq = swapl(sp->h.seq);
sp->h.ack = swapl(sp->h.ack);
return(sizeof(SCRATCHPHDR));
}
/* Display SCRATCHP packet */
void disp_scratchp(GENFRAME *nfp)
{
printf("SCRATCHP ");
disp_scratchphdr(getframe_datap(nfp));
printf("\n");
}
/* Display SCRATCHP header */
void disp_scratchphdr(SCRATCHPHDR *sph)
{
printf("F %02X S %04X A %04X Dlen %4Xh ",
sph->flags, (WORD)sph->seq, (WORD)sph->ack, sph->dlen);
}
/* Dump out the diagnostic headers */
void dump_diags(void)
{
SCRATCHPHDR *sph;
int i, lastype;
LWORD rxack=0, unacked, percent, txack=0;
for (i=lastype=0; i<NDIAGS; i++)
{
sph = &diaghdrs[diagidx];
diagidx = (diagidx + 1) % NDIAGS;
if (sph->ver == DIAG_TX) /* Tx packet? */
{
if (lastype == DIAG_TX)
printf("\n");
printf("Tx: ");
disp_scratchphdr(sph);
txack = sph->ack;
unacked = rxack ? sph->seq + sph->dlen - rxack : 0;
percent = (unacked * 100L) / txbuff.len;
printf("%2ld%%", percent);
}
else if (sph->ver == DIAG_RX) /* Rx packet? */
{
if (lastype != DIAG_TX)
printf(" ");
printf(" Rx: ");
disp_scratchphdr(sph);
rxack = sph->ack;
unacked = txack ? sph->seq + sph->dlen - txack : 0;
percent = (unacked * 100L) / rxbuff.len;
printf("%2ld%%\n", percent);
}
lastype = sph->ver ? sph->ver : lastype;
}
if (lastype == DIAG_TX)
printf("\n");
}
/* Read the config file into global storage */
int read_cfg(char *fname)
{
int ok;
if (fname)
{
strncpy(cfgfile, fname, MAXPATH);
cfgfile[MAXPATH] = 0;
strlwr(cfgfile);
if (!strchr(cfgfile, '.'))
strcat(cfgfile, ".cfg");
}
printf("Reading '%s'\n", cfgfile);
ok = read_cfgstr(cfgfile, "net", netcfg, sizeof(netcfg)-1);
if (ok)
{
printf("\nNet: %s\n", netcfg);
ok += read_cfgstr(cfgfile, "id", locid, IDLEN-1);
printf("Ident: %s\n", locid);
}
return(ok);
}
/* A rewrite of 'gets' to provide a max text length, and allow user to escape
** Returns length of string obtained */
int mygets(char *buff, int maxlen)
{
char c=0;
int n=0;
do
{
c = getch();
if (c=='\b' && n>0)
{
putch('\b');
putch(' ');
putch('\b');
n--;
}
else if (c == 0x1b)
{
n = 0;
break;
}
else if (c>=' ' && n<maxlen)
{
putch(c);
buff[n++] = c;
}
buff[n] = 0;
} while (c != '\r');
printf("\n");
return(n);
}
/* Ctrl-break handler: set flag and return */
void break_handler(int sig)
{
breakflag = sig;
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -