From 5f9458e6254d1fe712bfbf4a07bda8f89a37faf5 Mon Sep 17 00:00:00 2001 From: Gregg Date: Tue, 25 Jul 2023 05:54:40 -0500 Subject: [PATCH] Converted static HTTP Buffer to dynamic TempBuffer Saved about 8K in RAM! --- src/HAP.cpp | 44 +++++++++++++++++++++++++++----------------- src/HAP.h | 5 ++--- src/Utils.h | 4 ++-- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index 39bed60..57cc938 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -154,16 +154,27 @@ void HAPClient::init(){ void HAPClient::processRequest(){ - int nBytes; + int nBytes, messageSize; + + messageSize=client.available(); + + if(messageSize>MAX_HTTP){ // exceeded maximum number of bytes allowed + badRequestError(); + LOG0("\n*** ERROR: HTTP message of %d bytes exceeds maximum allowed (%d)\n\n",messageSize,MAX_HTTP); + return; + } + + TempBuffer tempBuffer(messageSize+1); // leave room for null character added below + uint8_t *httpBuf=tempBuffer.buf; if(cPair){ // expecting encrypted message LOG2("<<<< #### "); LOG2(client.remoteIP()); LOG2(" #### <<<<\n"); - nBytes=receiveEncrypted(); // decrypt and return number of bytes + nBytes=receiveEncrypted(httpBuf,messageSize); // decrypt and return number of bytes read - if(!nBytes){ // decryption failed (error message already printed in function) + if(!nBytes){ // decryption failed (error message already printed in function) badRequestError(); return; } @@ -173,21 +184,21 @@ void HAPClient::processRequest(){ LOG2(client.remoteIP()); LOG2(" <<<<<<<<<\n"); - nBytes=client.read(httpBuf,MAX_HTTP+1); // read all available bytes up to maximum allowed+1 - - if(nBytes>MAX_HTTP){ // exceeded maximum number of bytes allowed + nBytes=client.read(httpBuf,messageSize); // read expected number of bytes + + if(nBytes!=messageSize || client.available()!=0){ badRequestError(); - LOG0("\n*** ERROR: Exceeded maximum HTTP message length\n\n"); + LOG0("\n*** ERROR: HTTP message not read correctly. Expected %d bytes, read %d bytes, %d bytes remaining\n\n",messageSize,nBytes,client.available()); return; } - + } // encrypted/plaintext httpBuf[nBytes]='\0'; // add null character to enable string functions char *body=(char *)httpBuf; // char pointer to start of HTTP Body - char *p; // char pointer used for searches - + char *p; // char pointer used for searches + if(!(p=strstr((char *)httpBuf,"\r\n\r\n"))){ badRequestError(); LOG0("\n*** ERROR: Malformed HTTP request (can't find blank line indicating end of BODY)\n\n"); @@ -233,7 +244,7 @@ void HAPClient::processRequest(){ tlv8.print(2); // print TLV records in form "TAG(INT) LENGTH(INT) VALUES(HEX)" LOG2("------------ END TLVS! ------------\n"); - postPairVerifyURL(); // process URL + postPairVerifyURL(); // process URL return; } @@ -243,7 +254,7 @@ void HAPClient::processRequest(){ tlv8.print(2); // print TLV records in form "TAG(INT) LENGTH(INT) VALUES(HEX)" LOG2("------------ END TLVS! ------------\n"); - postPairingsURL(); // process URL + postPairingsURL(); // process URL return; } @@ -878,7 +889,7 @@ int HAPClient::getAccessoriesURL(){ LOG2("\n"); sendEncrypted(body,(uint8_t *)jBuf.buf,nBytes); - + return(1); } // getAccessories @@ -1443,7 +1454,7 @@ void HAPClient::tlvRespond(){ ////////////////////////////////////// -int HAPClient::receiveEncrypted(){ +int HAPClient::receiveEncrypted(uint8_t *httpBuf, int messageSize){ uint8_t buf[1042]; // maximum size of encoded message = 2+1024+16 bytes (HAP Section 6.5.2) int nBytes=0; @@ -1452,8 +1463,8 @@ int HAPClient::receiveEncrypted(){ int n=buf[0]+buf[1]*256; // compute number of bytes expected in encoded message - if(nBytes+n>MAX_HTTP){ // exceeded maximum number of bytes allowed in plaintext message - LOG0("\n\n*** ERROR: Exceeded maximum HTTP message length\n\n"); + if(nBytes+n>messageSize){ // exceeded maximum number of bytes allowed in plaintext message + LOG0("\n\n*** ERROR: Decrypted message of %d bytes exceeded maximum expected message length of %d bytes\n\n",nBytes+n,messageSize); return(0); } @@ -1722,7 +1733,6 @@ void Nonce::inc(){ TLV HAPClient::tlv8; nvs_handle HAPClient::hapNVS; nvs_handle HAPClient::srpNVS; -uint8_t HAPClient::httpBuf[MAX_HTTP+1]; HKDF HAPClient::hkdf; pairState HAPClient::pairStatus; Accessory HAPClient::accessory; diff --git a/src/HAP.h b/src/HAP.h index 8c8b937..67dda3c 100644 --- a/src/HAP.h +++ b/src/HAP.h @@ -73,14 +73,13 @@ struct HAPClient { // common structures and data shared across all HAP Clients - static const int MAX_HTTP=8095; // max number of bytes in HTTP message buffer + static const int MAX_HTTP=8096; // max number of bytes allowed for HTTP message static const int MAX_CONTROLLERS=16; // maximum number of paired controllers (HAP requires at least 16) static const int MAX_ACCESSORIES=41; // maximum number of allowed Acessories (HAP limit=150, but not enough memory in ESP32 to run that many) static TLV tlv8; // TLV8 structure (HAP Section 14.1) with space for 10 TLV records of type kTLVType (HAP Table 5-6) static nvs_handle hapNVS; // handle for non-volatile-storage of HAP data static nvs_handle srpNVS; // handle for non-volatile-storage of SRP data - static uint8_t httpBuf[MAX_HTTP+1]; // buffer to store HTTP messages (+1 to leave room for storing an extra 'overflow' character) static HKDF hkdf; // generates (and stores) HKDF-SHA-512 32-byte keys derived from an inputKey of arbitrary length, a salt string, and an info string static pairState pairStatus; // tracks pair-setup status static SRP6A srp; // stores all SRP-6A keys used for Pair-Setup @@ -121,7 +120,7 @@ struct HAPClient { void tlvRespond(); // respond to client with HTTP OK header and all defined TLV data records (those with length>0) void sendEncrypted(char *body, uint8_t *dataBuf, int dataLen); // send client complete ChaCha20-Poly1305 encrypted HTTP mesage comprising a null-terminated 'body' and 'dataBuf' with 'dataLen' bytes - int receiveEncrypted(); // decrypt HTTP request (HAP Section 6.5) + int receiveEncrypted(uint8_t *httpBuf, int messageSize); // decrypt HTTP request (HAP Section 6.5) int notFoundError(); // return 404 error int badRequestError(); // return 400 error diff --git a/src/Utils.h b/src/Utils.h index 0a06462..818d98c 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -48,7 +48,7 @@ struct TempBuffer { TempBuffer(size_t len){ nBytes=len*sizeof(bufType); - buf=(bufType *)heap_caps_malloc(nBytes,MALLOC_CAP_8BIT); + buf=(bufType *)malloc(nBytes); if(buf==NULL){ Serial.print("\n\n*** FATAL ERROR: Requested allocation of "); Serial.print(nBytes); @@ -58,7 +58,7 @@ struct TempBuffer { } ~TempBuffer(){ - heap_caps_free(buf); + free(buf); } int len(){