DiskBrowser/readtcp.c
Stephen Heumann 37b26592d0 Work around Marinetti bug 57.
This could cause Marinetti to return more data than it should, starting with valid data but then including corrupt data.

The workaround I'm using is to have Marinetti return a new handle and then copy the data out of it, using the size of the handle as the true size of the data that was read. Because of details of how Marinetti works, that size is correct.
2019-04-15 19:50:36 -05:00

61 lines
1.5 KiB
C

#pragma noroot
#include "readtcp.h"
#include "session.h"
#include <string.h>
#include <tcpip.h>
#include <memory.h>
#include <misctool.h>
#include <orca.h>
#define buffTypePointer 0x0000 /* For TCPIPReadTCP() */
#define buffTypeHandle 0x0001
#define buffTypeNewHandle 0x0002
/* Time out if no new data is received for this long */
#define READ_TIMEOUT 15 /* seconds */
void InitReadTCP(Session *sess, LongWord readCount, void *readPtr) {
sess->readCount = readCount;
sess->readPtr = readPtr;
sess->lastReadTime = GetTick();
}
ReadStatus TryReadTCP(Session *sess) {
rrBuff rrBuff;
TCPIPPoll();
sess->tcperr = TCPIPReadTCP(sess->ipid, buffTypeNewHandle, NULL,
sess->readCount, &rrBuff);
sess->toolerr = toolerror();
if (sess->tcperr || sess->toolerr) {
return rsError;
}
if (rrBuff.rrBuffCount != 0) {
/* Work around Marinetti bug #57 */
rrBuff.rrBuffCount = GetHandleSize(rrBuff.rrBuffHandle);
HLock(rrBuff.rrBuffHandle);
memcpy(sess->readPtr, *rrBuff.rrBuffHandle, rrBuff.rrBuffCount);
DisposeHandle(rrBuff.rrBuffHandle);
sess->readCount -= rrBuff.rrBuffCount;
sess->readPtr += rrBuff.rrBuffCount;
}
if (sess->readCount == 0) {
return rsDone;
} else {
if (rrBuff.rrBuffCount != 0) {
sess->lastReadTime = GetTick();
} else if (GetTick() - sess->lastReadTime > READ_TIMEOUT * 60) {
return rsTimedOut;
}
return rsWaiting;
}
}