AmendHub

Download:

jcs

/

subtext

/

amendments

/

189

zmodem: Bug fixes, remove send/recv abstraction

This doesn't really need to be portable, just call session functions
directly.
 
Reduce state timeouts to 10 seconds, but leave at 30 for the initial
Init state when receiving to give the user time to choose a file to
send us.
 
Fix IAC escaping on the way out.

jcs made amendment 189 over 2 years ago
--- zmodem.c Mon Jul 11 10:33:26 2022 +++ zmodem.c Mon Jul 11 22:56:19 2022 @@ -236,41 +236,37 @@ state_name(struct zmodem_session *zs) #endif /* ZMODEM_DEBUG */ struct zmodem_session * -ZCreateSender(FILE *fp) +ZCreateSender(struct session *session, FILE *fp, char *file_name) { struct zmodem_session *zs; if (!fp) return NULL; - - zs = malloc(sizeof(struct zmodem_session)); - if (zs == NULL) { - return NULL; - } - memset(zs, 0, sizeof(*zs)); + zs = xmalloczero(sizeof(struct zmodem_session)); + zs->session = session; zs->file = fp; + strlcpy(zs->file_name, file_name, sizeof(zs->file_name)); + fseek(zs->file, 0, SEEK_END); zs->file_size = ftell(zs->file); + fseek(zs->file, 0, SEEK_SET); + zs->BinFlag = true; - zs->ZMode = IdZAutoS; return zs; } struct zmodem_session * -ZCreateReceiver(void) +ZCreateReceiver(struct session *session, char *path) { struct zmodem_session *zs; - zs = malloc(sizeof(struct zmodem_session)); - if (zs == NULL) { - return NULL; - } - memset(zs, 0, sizeof(*zs)); - + zs = xmalloczero(sizeof(struct zmodem_session)); + zs->session = session; zs->ZMode = IdZAutoR; + zs->upload_file_path = path; return zs; } @@ -284,7 +280,7 @@ ZRead1Byte(struct zmodem_session *zs, unsigned char *b return 1; } - if (zs->recv(zs->cookie, b) == 0) + if (session_get_char(zs->session, b) == 0) return 0; if (zs->DoIACEscape && zs->LastIAC) { @@ -422,6 +418,12 @@ ZPutBin(struct zmodem_session *zs, short *i, unsigned (*i)++; b = b ^ 0x40; break; + case 0xff: + if (zs->DoIACEscape) { + zs->PktOut[*i] = 0xff; + (*i)++; + } + /* FALLTHROUGH */ default: if (zs->CtlEsc && ((b & 0x60) == 0)) { zs->PktOut[*i] = ZDLE; @@ -744,6 +746,8 @@ ZSendDataDat(struct zmodem_session *zs) else zs->ZState = Z_SendDataDat; + ZResetTimeout(zs, zs->TimeOut); + #ifdef ZMODEM_DEBUG logger_printf(logger, "[%s] ZSendDataDat: pos=%ld", state_name(zs), zs->Pos); @@ -760,15 +764,15 @@ ZInit(struct zmodem_session *zs) if (zs->ZMode == IdZAutoR || zs->ZMode == IdZAutoS) { if (zs->ZMode == IdZAutoR) { zs->ZMode = IdZReceive; - zs->send(zs->cookie, (unsigned char *)'0', 1); + session_output(zs->session, (const char *)'0', 1); } else { zs->ZMode = IdZSend; - zs->send(zs->cookie, (unsigned char *)'1', 1); + session_output(zs->session, (const char *)'1', 1); } - zs->send(zs->cookie, (unsigned char *)'0', 1); - zs->send(zs->cookie, (unsigned char *)'B', 1); - zs->send(zs->cookie, (unsigned char *)ZDLE, 1); - zs->send(zs->cookie, (unsigned char *)ZPAD, 1); + session_output(zs->session, (const char *)'0', 1); + session_output(zs->session, (const char *)'B', 1); + session_output(zs->session, (const char *)ZDLE, 1); + session_output(zs->session, (const char *)ZPAD, 1); } zs->PktOutCount = 0; @@ -786,7 +790,7 @@ ZInit(struct zmodem_session *zs) if (zs->MaxDataLen > ZMODEM_BLOCK_SIZE) zs->MaxDataLen = ZMODEM_BLOCK_SIZE; - zs->TimeOut = 30; + zs->TimeOut = 10; ZResetTimeout(zs, zs->TimeOut); #ifdef ZMODEM_DEBUG @@ -797,6 +801,8 @@ ZInit(struct zmodem_session *zs) switch (zs->ZMode) { case IdZReceive: zs->ZState = Z_RecvInit; + /* give the user extra time to pick a file */ + ZResetTimeout(zs, zs->TimeOut * 3); ZSendRInit(zs); break; case IdZSend: @@ -1036,7 +1042,7 @@ ZParseHdr(struct zmodem_session *zs) ZResetTimeout(zs, zs->TimeOut); } else { zs->ZState = Z_End; - zs->send(zs->cookie, (unsigned char *)"OO", 2); + session_output(zs->session, (const char *)"OO", 2); } break; case ZRPOS: @@ -1060,10 +1066,9 @@ ZParseHdr(struct zmodem_session *zs) if (zs->Pos != ZRclHdr(zs)) { ZSendRPOS(zs); return; - } else { - ZResetTimeout(zs, zs->TimeOut); - zs->ZPktState = Z_PktGetData; } + ZResetTimeout(zs, zs->TimeOut); + zs->ZPktState = Z_PktGetData; break; case ZEOF: if (zs->Pos != ZRclHdr(zs)) { @@ -1344,8 +1349,9 @@ ZParse(struct zmodem_session *zs) if (zs->Sending) { c = 1; while ((c > 0) && (zs->PktOutCount > 0)) { - c = zs->send(zs->cookie, &(zs->PktOut[zs->PktOutPtr]), - zs->PktOutCount); + c = session_output(zs->session, + (const char *)&(zs->PktOut[zs->PktOutPtr]), + zs->PktOutCount); zs->PktOutPtr = zs->PktOutPtr + c; zs->PktOutCount = zs->PktOutCount - c; } --- zmodem.h Mon Jul 11 10:33:11 2022 +++ zmodem.h Mon Jul 11 22:23:09 2022 @@ -30,6 +30,7 @@ #ifndef __ZMODEM_H__ #define __ZMODEM_H__ +#include "session.h" #include "util.h" #define ZMODEM_BLOCK_SIZE 1024 @@ -46,14 +47,12 @@ enum { }; struct zmodem_session { - void *cookie; - short (*send)(void *cookie, unsigned char *data, size_t size); - short (*recv)(void *cookie, unsigned char *data); + struct session *session; FILE *file; unsigned long file_size; char file_name[256]; - char upload_file_path[256]; + char *upload_file_path; unsigned long file_mtime; unsigned char RxHdr[4], TxHdr[4]; @@ -74,8 +73,9 @@ struct zmodem_session { unsigned long TimeOutAt; }; -struct zmodem_session * ZCreateSender(FILE *fp); -struct zmodem_session * ZCreateReceiver(void); +struct zmodem_session * ZCreateSender(struct session *session, FILE *fp, + char *file_name); +struct zmodem_session * ZCreateReceiver(struct session *session, char *path); bool ZInit(struct zmodem_session *zs); void ZDestroy(struct zmodem_session *zs);