?? ecusea.c
字號:
char *revision = "3.20"; /* cannot be longer than 7 chars (blk0.sender) *//* #define TABLE_CRC16 */#ifdef M_XENIX#define NO_SELECT#endif/*+------------------------------------------------------------------------- ecusea.c - SEAlink - Sliding window file transfer protocol Defined functions: Nap(msec) arg_token(parsestr,termchars) cancel_transaction(sig) crc_update(c,crc) fname_split(cmd,arg,arg_max_quan,narg_rtn) fname_too_long(fname) fname_truncated() getspeed(code) lgetc_timeout(tenths) lgetc_timeout_SIGALRM() main(argc,argv,envp) rdchk(fd) receive_block(buf) receive_file() send_comm_block(blk,blknum) send_file(name) send_file_block(fp,blknum) set_sf_state(place,new_state) set_utime_1980(filename,secs_since_1980) sf_state_text(state) wait_for_rcvr_response() xmit_ack(blknum) xmit_cancel() xmit_nak(blknum)ecu adaptation by W. Tuckermodelled after MSDOS sealink.c, which carried the following proviso: MS-DOS Version 1.20, created on 08/05/87 at 17:51:40 (C) COPYRIGHT 1986,87 by System Enhancement Associates; ALL RIGHTS RESERVED By: Thom Henderson You are granted a license to use this code in your programs, and to adapt it to your particular situation and needs, subject only to the following conditions: 1) You must refer to it as the SEAlink protocol, and you must give credit to System Enhancement Associates. 2) If you modify it in such a way that your version cannot converse with the original code as supplied by us, then you should refer to it as "SEAlink derived", or as a "variation of SEAlink", or words to that effect. In short, we're not asking for any money, but we'd like to get some credit for our work.--------------------------------------------------------------------------*//*+:EDITS:*//*:09-10-1992-14:00-wht@n4hgf-ECU release 3.20 *//*:08-22-1992-15:39-wht@n4hgf-ECU release 3.20 BETA *//*:08-16-1992-03:08-wht@n4hgf-head off another POSIX plot *//*:08-10-1992-04:01-wht@n4hgf-use init_Nap *//*:07-20-1992-13:30-wht@n4hgf-put hzmsec in AGAIN ?? *//*:07-17-1992-18:28-wht@n4hgf-remove Nap() and use common ../nap.o *//*:05-11-1992-16:43-wht@gyro-fix WORKING_SELECT nap once and for all *//*:05-08-1992-02:42-wht@n4hgf-select-based Nap was buggy *//*:09-01-1991-14:22-wht@n4hgf2-on sun, use termios *//*:08-30-1991-20:09-wht@n4hgf2-sun Nap was not returning a value *//*:08-30-1991-02:34-jdeitch@jadpc.cts.com-fix no hzmsec *//*:07-25-1991-12:59-wht@n4hgf-ECU release 3.10 *//*:03-18-1991-22:49-wht@n4hgf-ISC 2.2 has struct timeval in sys/time.h *//*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */#include <stdio.h>#include <string.h>#include <ctype.h>#include <signal.h>#include <setjmp.h>#include <fcntl.h>#include <errno.h>#if defined(sun)#include <termios.h>#define termio termios#undef TCGETA#undef TCSETA#undef TCSETAW#define TCGETA TCGETS#define TCSETA TCSETS#define TCSETAW TCSETSW#undef ECHO#undef NL0#undef NL1#undef TAB0#undef TAB1#undef TAB2#undef XTABS#undef CR0#undef CR1#undef CR2#undef CR3#undef FF0#undef FF1#undef BS0#undef BS1#undef TOSTOP#undef FLUSHO#undef PENDIN#undef NOFLSH#else#include <termio.h>#endif /* sun */#include <sys/param.h>#include <sys/ioctl.h>#include "../ecu_types.h"#include "../ecu_stat.h"#include <time.h>#include <memory.h>#if !defined(NO_SELECT)#if defined(M_SYSV) && !defined(SCO32v4) /* SCO pre 3.2v4 */# include <sys/select.h>#else# include <sys/time.h>#endif#endif#ifdef USE_PROTOS# include "protos.h"#endifextern int errno;/* Various system constants */#define WINDOW 6 /* maximum size of window */#define TIMEOUT 0x0FFF#define OFFSET_1980 (time_t)315547200 /* time offset for 1970 <-> 1980 *//* * The section of code that is compiled when NAKEOT is defined is in the * original MS-DOS version 1.16 routine. Its purpose is to send a NAK when * an EOT is received during receive_file(), apparently to confirm that this is * indeed the end of file. However, in certain (apparently non - standard) * versions of the protocol, it is possible that the program will report an * error when in fact there isn't one. Comment this out at your discretion. */#define NAKEOT/* SEAlink block zero data structure */typedef struct blk0{ long length; /* length */ time_t secs_since_1980; /* creation/last mod in secs since 1/1/80 */ char filename[17]; /* file name */ char sender[15]; /* sending program */ char send_no_acks; /* true if rcvr need not ack */ char filler[87]; /* fill to 128 bytes */} BLK0;/* protocol characters */#define SOH 0x01#define EOT 0x04#define ACK 0x06#define NAK 0x15#define CAN 0x18/* send_file state (sf_state) values */#define SFS_GND 0 /* Ground state, ACK or NAK expected */#define SFS_ACK 1 /* ACK received */#define SFS_NAK 2 /* NAK received */#define SFS_ACKW 3 /* ACK, block# received */#define SFS_NAKW 4 /* NAK, block# received */#define SFS_RGND 5 /* Returning to ground state */int sf_state;int allow_slide = 1; /* sliding windows allowed */int crc_in_use; /* check type, 1 = CRC, 0 = checksum */char *dfile = "/tmp/ecuSEA.log";int error_count = 0; /* total number of errors */int iofd = 0; /* file descriptor to use */int no_ack_mode = 1; /* true of ACKs not required */int rf_done = 0; /* receive file done */int sf_ackw_count; /* count of sliding ACKs seen */int sf_ackblk; /* number of last block ACKed */int sf_blknum; /* number of next block to send */int sf_lastnum; /* number of last block sent */int sf_nakquan; /* number of sequential NAKs */int sf_slide; /* true if sliding window */int sigint = 0; /* dummy for nap.c */int sending_flag = -1; /* send == 1, receive == 0, bad usage == -1 */int log_packets = 0;long rx_char_count = 0;long tx_char_count = 0;int Filcnt = 0;int npaths = 0;char curr_dir[256];char s128[128];unsigned baud_rate;int exit_code;int sent_EOT = 0;struct termio tio;struct termio tio0;jmp_buf lgetc_timeout_setjmp;/* CRC16 routine; finish CRC calculation for compare */#ifdef TABLE_CRC16/* crctab calculated by Mark G. Mendel,Network Systems Corporation */unsigned short crctab[256] = { 0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7, 0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef, 0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6, 0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de, 0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485, 0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d, 0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4, 0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc, 0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823, 0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b, 0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12, 0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a, 0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41, 0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49, 0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70, 0xFF9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78, 0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f, 0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067, 0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e, 0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256, 0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d, 0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405, 0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c, 0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634, 0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab, 0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3, 0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a, 0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92, 0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9, 0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1, 0xef1f,0xFF3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8, 0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0};/*+------------------------------------------------------------------------- updcrc macro derived from article Copyright (C) 1986 Stephen Satchell. NOTE: First argument must be in range 0 to 255. Second argument is referenced twice. Programmers may incorporate any or all code into their programs, giving proper credit within the source. Publication of the source routines is permitted so long as proper credit is given to Stephen Satchell, Satchell Evaluations and Chuck Forsberg, Omen Technology.--------------------------------------------------------------------------*/#define crc_update(ch,crc) ( crctab[((crc >> 8) & 255)] ^ (crc << 8) ^ ch)#else /* calculated crc *//*+------------------------------------------------------------------------- crc_update(c,crc)--------------------------------------------------------------------------*/unsigned shortcrc_update(c,crc)register c;register unsigned crc;{register count; for(count = 8; --count >= 0;) { if(crc & 0x8000) { crc <<= 1; crc += (((c <<= 1) & 0400) != 0); crc ^= 0x1021; } else { crc <<= 1; crc += (((c <<= 1) & 0400) != 0); } } return(crc);} /* end of crc_update */#endif /* crc calc selection *//*+------------------------------------------------------------------------- rdchk(fd) - for systems without it but with FIONREAD--------------------------------------------------------------------------*/#if defined(sun) || defined(NO_RDCHK)intrdchk(fd)int fd;{int chars_waiting; if(ioctl(fd,FIONREAD,&chars_waiting)) return(0); else return(!!chars_waiting);} /* end of rdchk */#endif/*+----------------------------------------------------------------------- arg_token(parsestr,termchars)Get next token from string parsestr ((char *)0 on 2nd, 3rd, etc.calls), where tokens are nonempty strings separated by runs of charsfrom termchars. Writes nulls into parsestr to end tokens.termchars need not remain constant from call to call.Treats multiple occurrences of a termchar as one delimiter (does notallow null fields).------------------------------------------------------------------------*/#if defined(M_UNIX)static char *arg_token_static = (char *)0;char *arg_token(parsestr,termchars)char *parsestr;char *termchars;{register char *parseptr;char *token; if(parsestr == (char *)0 && arg_token_static == (char *)0) return((char *)0); if(parsestr) parseptr = parsestr; else parseptr = arg_token_static; while(*parseptr) { if(!strchr(termchars,*parseptr)) break; parseptr++; } if(!*parseptr) { arg_token_static = (char *)0; return((char *)0); } token = parseptr; if(*token == '\'') { token++; parseptr++; while(*parseptr) { if(*parseptr == '\'') { arg_token_static = parseptr + 1; *parseptr = 0; return(token); } parseptr++; } arg_token_static = (char *)0; return(token); } while(*parseptr) { if(strchr(termchars,*parseptr)) { *parseptr = 0; arg_token_static = parseptr + 1; while(*arg_token_static) { if(!strchr(termchars,*arg_token_static)) break; arg_token_static++; } return(token); } parseptr++; } arg_token_static = (char *)0; return(token);} /* end of arg_token */#endif/*+------------------------------------------------------------------------- fname_split(cmd,arg,arg_max_quan,&narg)--------------------------------------------------------------------------*/#if defined(M_UNIX)voidfname_split(cmd,arg,arg_max_quan,narg_rtn)char *cmd;char **arg;int arg_max_quan;int *narg_rtn;{register itmp;register narg; for(itmp = 0; itmp < arg_max_quan; itmp++) arg[itmp] = (char *)0; arg[0] = arg_token(cmd,"/"); for(narg = 1; narg < arg_max_quan; ++narg) { if((arg[narg] = arg_token((char *)0,"/")) == (char *)0) break; } *narg_rtn = narg;} /* end of fname_split */#endif#if defined(M_UNIX)#define MAX_COMPONENT_LEN 14#define MAX_PATH_COMPONENTS 16static char trunc_fname[257];static char *trunc_components[MAX_PATH_COMPONENTS];static int trunc_components_quan;static int trunc_absolute_path;#endif/*+------------------------------------------------------------------------- fname_too_long(fname) - check for any pathname component too long--------------------------------------------------------------------------*/#if defined(M_UNIX)intfname_too_long(fname)register char *fname;{register int itmp;register char **cpptr; if(trunc_absolute_path = (*fname == '/')) fname++; strncpy(trunc_fname,fname,sizeof(trunc_fname) - 1); fname_split(trunc_fname,trunc_components, MAX_PATH_COMPONENTS,&trunc_components_quan); itmp = trunc_components_quan; cpptr = trunc_components; while(itmp--) { if(strlen(*cpptr) > MAX_COMPONENT_LEN) return(1); cpptr++; } return(0);} /* end of fname_too_long */#endif/*+------------------------------------------------------------------------- fname_truncated() - build truncated path last checked by fname_too_long--------------------------------------------------------------------------*/#if defined(M_UNIX)char *fname_truncated(){register int icomp;char new_fname[257];register char *cptr = new_fname; if(trunc_absolute_path) { *cptr = '/'; *(cptr + 1) = 0; } else *cptr = 0; for(icomp = 0; icomp < trunc_components_quan; icomp++) { if(strlen(trunc_components[icomp]) > MAX_COMPONENT_LEN) *(trunc_components[icomp] + MAX_COMPONENT_LEN) = 0; strcat(cptr,trunc_components[icomp]); if(icomp < trunc_components_quan - 1) strcat(cptr,"/"); } strcpy(trunc_fname,cptr); return(trunc_fname);} /* end of fname_truncated */#endif/*+------------------------------------------------------------------------- xmit_cancel()--------------------------------------------------------------------------*/voidxmit_cancel(){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -