From 610a638e5c38244879af4a81d524ec9fb9d2cabe Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sat, 11 Aug 2018 01:53:53 -0500 Subject: [PATCH] Switch to unified set of error codes. Also, do more checks for 2IMG files. --- driver.c | 42 +++++++++++++++++++++++++++++------------- http.c | 8 ++++---- http.h | 16 ++-------------- httptest.c | 10 +++++----- netdiskerror.h | 37 +++++++++++++++++++++++++++++++++++++ session.h | 13 +------------ seturl.c | 4 ++-- seturl.h | 18 ++---------------- tcpconnection.c | 10 +++++----- tcpconnection.h | 3 ++- twoimg.h | 23 +++++++++++++++++++++++ 11 files changed, 112 insertions(+), 72 deletions(-) create mode 100644 netdiskerror.h create mode 100644 twoimg.h diff --git a/driver.c b/driver.c index 0840342..8dd5186 100644 --- a/driver.c +++ b/driver.c @@ -11,7 +11,7 @@ #include "asmglue.h" #include "version.h" -#define TWO_IMG_MAGIC ('2' | 'I'<<8 | (LongWord)'M'<<16 | (LongWord)'G'<<24) +#define BLOCK_SIZE 512 struct DIB dibs[NDIBS] = {0}; struct DIBList dibList = {NDIBS}; @@ -192,20 +192,36 @@ Word DriverDispatch(Word callNum, struct GSOSDP *dp) { } #pragma databank 0 - -static Word CheckTwoImg(Session *sess) { +/* + * Check for a 2IMG file, and process its header if one is found. + * Returns a non-zero error code for an invalid or unsupported 2IMG file, + * OPERATION_SUCCESSFUL otherwise (whether it's a 2IMG file or not). + */ +static enum NetDiskError CheckTwoImg(Session *sess) { struct TwoImgHeader *hdr = &sess->fileHeader.twoImgHeader; + + /* Check if it's a 2IMG file */ if (hdr->twoImgID != TWO_IMG_MAGIC) - return 0; + return OPERATION_SUCCESSFUL; + + /* It's a 2IMG file, but is it one we can handle? */ + if (hdr->version > 1) + return UNSUPPORTED_2IMG_FILE; + if (hdr->imgFormat != IMAGE_FORMAT_PRODOS_ORDER) + return UNSUPPORTED_2IMG_FILE; + if (hdr->dataLength % BLOCK_SIZE != 0) + return UNSUPPORTED_2IMG_FILE; // TODO more checks sess->dataOffset = hdr->dataOffset; - return 0; + return OPERATION_SUCCESSFUL; } static Word DoMountURL(struct GSOSDP *dp) { + enum NetDiskError err; + if (dp->dibPointer->extendedDIBPtr != NULL) { dp->transferCount = 0; return drvrBusy; @@ -217,16 +233,16 @@ static Word DoMountURL(struct GSOSDP *dp) { return drvrNoResrc; } - enum SetURLResult setResult = SetURL(sess, (char*)dp->controlListPtr, TRUE, FALSE); - if (setResult != SETURL_SUCCESSFUL) { + err = SetURL(sess, (char*)dp->controlListPtr, TRUE, FALSE); + if (err != OPERATION_SUCCESSFUL) { // TODO arrange for more detailed error reporting EndNetDiskSession(sess); dp->transferCount = 0; return drvrIOError; } - enum RequestResult requestResult = DoHTTPRequest(sess, 0, sizeof(sess->fileHeader) - 1); - if (requestResult != REQUEST_SUCCESSFUL) { + err = DoHTTPRequest(sess, 0, sizeof(sess->fileHeader) - 1); + if (err != OPERATION_SUCCESSFUL) { // TODO arrange for more detailed error reporting EndNetDiskSession(sess); dp->transferCount = 0; @@ -239,8 +255,8 @@ static Word DoMountURL(struct GSOSDP *dp) { /* keep reading */ ; //TODO detect errors - Word checkResult = CheckTwoImg(sess); - if (checkResult != 0) { + err = CheckTwoImg(sess); + if (err != OPERATION_SUCCESSFUL) { EndNetDiskSession(sess); // TODO error } @@ -265,8 +281,8 @@ static Word DoRead(struct GSOSDP *dp) { unsigned long readStart = dp->blockNum * dp->blockSize + sess->dataOffset; unsigned long readEnd = readStart + dp->requestCount - 1; - enum RequestResult requestResult = DoHTTPRequest(sess, readStart, readEnd); - if (requestResult != REQUEST_SUCCESSFUL) { + enum NetDiskError err = DoHTTPRequest(sess, readStart, readEnd); + if (err != OPERATION_SUCCESSFUL) { // TODO arrange for more detailed error reporting dp->transferCount = 0; return drvrIOError; diff --git a/http.c b/http.c index d086c80..83c86e9 100644 --- a/http.c +++ b/http.c @@ -90,13 +90,13 @@ Boolean BuildHTTPRequest(Session *sess, char *resourceStr) { return TRUE; } -enum RequestResult +enum NetDiskError DoHTTPRequest(Session *sess, unsigned long start, unsigned long end) { top:; rlrBuff rlrBuff = {0}; Word tcpError; Boolean wantRedirect = FALSE, gotRedirect = FALSE; - enum RequestResult result; + enum NetDiskError result; UpdateRequestRange(sess, start, end); @@ -300,7 +300,7 @@ netRetry: goto errorReturn; if (wantRedirect) { *endPtr = '\0'; - if (SetURL(sess, response, FALSE, TRUE) != SETURL_SUCCESSFUL) { + if (SetURL(sess, response, FALSE, TRUE) != OPERATION_SUCCESSFUL) { result = REDIRECT_ERROR; goto errorReturn; } @@ -354,7 +354,7 @@ netRetry: && sess->contentLength != (sess->desiredEnd - sess->desiredStart + 1)) goto errorReturn; - result = REQUEST_SUCCESSFUL; + result = OPERATION_SUCCESSFUL; DisposeHandle(rlrBuff.rlrBuffHandle); return result; diff --git a/http.h b/http.h index 1b367f2..f18d1d2 100644 --- a/http.h +++ b/http.h @@ -3,21 +3,9 @@ #include #include "session.h" - -enum RequestResult { - REQUEST_SUCCESSFUL = 0, - NETWORK_ERROR, - NO_RESPONSE, - INVALID_RESPONSE, - EXCESSIVE_REDIRECTS, - UNSUPPORTED_RESPONSE, - UNSUPPORTED_HEADER_VALUE, - REDIRECT_ERROR, - NOT_DESIRED_CONTENT, - DIFFERENT_LENGTH, /* Might be considered successful later */ -}; +#include "netdiskerror.h" Boolean BuildHTTPRequest(Session *sess, char *resourceStr); -enum RequestResult DoHTTPRequest(Session *sess, unsigned long start, unsigned long end); +enum NetDiskError DoHTTPRequest(Session *sess, unsigned long start, unsigned long end); #endif \ No newline at end of file diff --git a/httptest.c b/httptest.c index 55b3c05..d96ec68 100644 --- a/httptest.c +++ b/httptest.c @@ -39,9 +39,9 @@ int main(int argc, char **argv) { char *url = (argc > 1) ? argv[1] : defaultURL; - enum SetURLResult result = SetURL(&sess, url, TRUE, FALSE); + enum NetDiskError result = SetURL(&sess, url, TRUE, FALSE); - if (result != SETURL_SUCCESSFUL) { + if (result != OPERATION_SUCCESSFUL) { printf("SetURL error %i\n", (int)result); goto exit; } @@ -62,12 +62,12 @@ int main(int argc, char **argv) { printf("=========\n"); printf("\n"); - enum RequestResult requestResult; + enum NetDiskError requestResult; requestResult = DoHTTPRequest(&sess, startByte, startByte + 511); printf("RequestResult %i\n", requestResult); printf("Response code %lu\n", sess.responseCode); - if (requestResult == REQUEST_SUCCESSFUL) { + if (requestResult == OPERATION_SUCCESSFUL) { printf("rangeStart = %lu\n", sess.rangeStart); printf("rangeEnd = %lu\n", sess.rangeEnd); printf("totalLength = %lu\n", sess.totalLength); @@ -87,7 +87,7 @@ int main(int argc, char **argv) { printf("RequestResult %i\n", requestResult); printf("Response code %lu\n", sess.responseCode); - if (requestResult == REQUEST_SUCCESSFUL) { + if (requestResult == OPERATION_SUCCESSFUL) { printf("rangeStart = %lu\n", sess.rangeStart); printf("rangeEnd = %lu\n", sess.rangeEnd); printf("totalLength = %lu\n", sess.totalLength); diff --git a/netdiskerror.h b/netdiskerror.h new file mode 100644 index 0000000..282b7c0 --- /dev/null +++ b/netdiskerror.h @@ -0,0 +1,37 @@ +#ifndef NETDISKERROR_H +#define NETDISKERROR_H + +enum NetDiskError { + OPERATION_SUCCESSFUL = 0, + + /* SetURL errors */ + URL_TOO_LONG = 100, + INVALID_CHARACTER_IN_URL, + BAD_URL_SYNTAX, + UNSUPPORTED_URL_SCHEME, + AUTHENTICATION_NOT_SUPPORTED, + FRAGMENT_NOT_SUPPORTED, + INVALID_PORT_NUMBER, + NO_HOST_SPECIFIED, + IPV6_NOT_SUPPORTED, + HOSTNAME_TOO_LONG, + NAME_LOOKUP_FAILED, + OUT_OF_MEMORY, + + /* StartTCPConnection and DoHTTPRequest errors */ + NETWORK_ERROR = 200, + NO_RESPONSE, + INVALID_RESPONSE, + EXCESSIVE_REDIRECTS, + UNSUPPORTED_RESPONSE, + UNSUPPORTED_HEADER_VALUE, + REDIRECT_ERROR, + NOT_DESIRED_CONTENT, + DIFFERENT_LENGTH, /* Might be considered successful later */ + + /* File format errors */ + UNSUPPORTED_2IMG_FILE = 300, + +}; + +#endif diff --git a/session.h b/session.h index 06c112a..3b5e08b 100644 --- a/session.h +++ b/session.h @@ -2,18 +2,7 @@ #define SESSION_H #include - -struct TwoImgHeader { - LongWord twoImgID; - LongWord appTag; - Word headerLength; - Word version; - LongWord imgFormat; - LongWord flag; - LongWord nBlocks; - LongWord dataOffset; - LongWord dataLength; -}; +#include "twoimg.h" typedef struct Session { /* Marinetti TCP connection status */ diff --git a/seturl.c b/seturl.c index 270553e..cdd1352 100644 --- a/seturl.c +++ b/seturl.c @@ -18,7 +18,7 @@ #define MAX_URL_LENGTH 30000 -enum SetURLResult +enum NetDiskError SetURL(Session *sess, char *url, Boolean permissive, Boolean partialOK) { if (strlen(url) > MAX_URL_LENGTH) { return URL_TOO_LONG; @@ -95,5 +95,5 @@ SetURL(Session *sess, char *url, Boolean permissive, Boolean partialOK) { /* End any existing TCP connection to old URL */ EndTCPConnection(sess); - return SETURL_SUCCESSFUL; + return OPERATION_SUCCESSFUL; } diff --git a/seturl.h b/seturl.h index 652428e..e7f5f11 100644 --- a/seturl.h +++ b/seturl.h @@ -1,23 +1,9 @@ #ifndef SETURL_H #define SETURL_H -enum SetURLResult { - SETURL_SUCCESSFUL = 0, - URL_TOO_LONG, - INVALID_CHARACTER_IN_URL, - BAD_URL_SYNTAX, - UNSUPPORTED_URL_SCHEME, - AUTHENTICATION_NOT_SUPPORTED, - FRAGMENT_NOT_SUPPORTED, - INVALID_PORT_NUMBER, - NO_HOST_SPECIFIED, - IPV6_NOT_SUPPORTED, - HOSTNAME_TOO_LONG, - NAME_LOOKUP_FAILED, - OUT_OF_MEMORY -}; +#include "netdiskerror.h" -enum SetURLResult +enum NetDiskError SetURL(Session *sess, char *url, Boolean permissive, Boolean partialOK); #endif diff --git a/tcpconnection.c b/tcpconnection.c index 275502c..32ea2b8 100644 --- a/tcpconnection.c +++ b/tcpconnection.c @@ -15,7 +15,7 @@ * On success, returns 0 and sets sess->ipid. * On failure, returns an error code. */ -Word StartTCPConnection(Session *sess) { +enum NetDiskError StartTCPConnection(Session *sess) { Word tcperr; srBuff mySRBuff; LongWord initialTime; @@ -26,15 +26,15 @@ Word StartTCPConnection(Session *sess) { sess->ipid = TCPIPLogin(userid(), sess->ipAddr, sess->port, 0, 0x40); if (toolerror()) - return NETWORK_ERR; + return NETWORK_ERROR; tcperr = TCPIPOpenTCP(sess->ipid); if (toolerror()) { TCPIPLogout(sess->ipid); - return NETWORK_ERR; + return NETWORK_ERROR; } else if (tcperr != tcperrOK) { TCPIPLogout(sess->ipid); - return NO_RESP_ERR; + return NO_RESPONSE; } initialTime = GetTick(); @@ -45,7 +45,7 @@ Word StartTCPConnection(Session *sess) { if (mySRBuff.srState != TCPSESTABLISHED) { TCPIPAbortTCP(sess->ipid); TCPIPLogout(sess->ipid); - return NO_RESP_ERR; + return NO_RESPONSE; } sess->tcpLoggedIn = TRUE; diff --git a/tcpconnection.h b/tcpconnection.h index 5e6933f..877f6bd 100644 --- a/tcpconnection.h +++ b/tcpconnection.h @@ -2,8 +2,9 @@ #define TCPCONNECTION_H #include "session.h" +#include "netdiskerror.h" -Word StartTCPConnection(Session *sess); +enum NetDiskError StartTCPConnection(Session *sess); void EndTCPConnection(Session *sess); #endif diff --git a/twoimg.h b/twoimg.h new file mode 100644 index 0000000..361155e --- /dev/null +++ b/twoimg.h @@ -0,0 +1,23 @@ +#ifndef TWOIMG_H +#define TWOIMG_H + +#define TWO_IMG_MAGIC ('2' | 'I'<<8 | (LongWord)'M'<<16 | (LongWord)'G'<<24) + +#define IMAGE_FORMAT_DOS_33_ORDER 0 +#define IMAGE_FORMAT_PRODOS_ORDER 1 +#define IMAGE_FORMAT_NIBBLIZED 2 + +struct TwoImgHeader { + LongWord twoImgID; + LongWord appTag; + Word headerLength; + Word version; + LongWord imgFormat; + LongWord flag; + LongWord nBlocks; + LongWord dataOffset; + LongWord dataLength; + /* Remaining header fields are unimportant for us */ +}; + +#endif