Fixed memory leak [needed to free `body` in tlvRespond()]

This hopefully addresses memory leak that occurs when not using a Home Hub and connections constantly drop and re-establish.

To do: must add back logic for notifications using new hap linked-list structure, and then delete static hap array code.
This commit is contained in:
Gregg 2024-06-02 20:57:41 -05:00
parent dbfad7d222
commit 9d29b73dac
4 changed files with 26 additions and 15 deletions

View File

@ -324,7 +324,7 @@ int HAPClient::postPairSetupURL(uint8_t *content, size_t len){
iosTLV.print(); iosTLV.print();
LOG2("------------ END TLVS! ------------\n"); LOG2("------------ END TLVS! ------------\n");
LOG1("In Pair Setup #%d (%s)...",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); LOG1("In Pair Setup #%d (%s)...",clientNumber,client.remoteIP().toString().c_str());
auto itState=iosTLV.find(kTLVType_State); auto itState=iosTLV.find(kTLVType_State);
@ -574,7 +574,7 @@ int HAPClient::postPairVerifyURL(uint8_t *content, size_t len){
iosTLV.print(); iosTLV.print();
LOG2("------------ END TLVS! ------------\n"); LOG2("------------ END TLVS! ------------\n");
LOG1("In Pair Verify #%d (%s)...",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); LOG1("In Pair Verify #%d (%s)...",clientNumber,client.remoteIP().toString().c_str());
auto itState=iosTLV.find(kTLVType_State); auto itState=iosTLV.find(kTLVType_State);
@ -756,7 +756,7 @@ int HAPClient::postPairingsURL(uint8_t *content, size_t len){
iosTLV.print(); iosTLV.print();
LOG2("------------ END TLVS! ------------\n"); LOG2("------------ END TLVS! ------------\n");
LOG1("In Post Pairings #%d (%s)...",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); LOG1("In Post Pairings #%d (%s)...",clientNumber,client.remoteIP().toString().c_str());
auto itState=iosTLV.find(kTLVType_State); auto itState=iosTLV.find(kTLVType_State);
auto itMethod=iosTLV.find(kTLVType_Method); auto itMethod=iosTLV.find(kTLVType_Method);
@ -882,7 +882,7 @@ int HAPClient::getAccessoriesURL(){
return(0); return(0);
} }
LOG1("In Get Accessories #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); LOG1("In Get Accessories #%d (%s)...\n",clientNumber,client.remoteIP().toString().c_str());
homeSpan.printfAttributes(); homeSpan.printfAttributes();
size_t nBytes=hapOut.getSize(); size_t nBytes=hapOut.getSize();
@ -910,7 +910,7 @@ int HAPClient::getCharacteristicsURL(char *urlBuf){
return(0); return(0);
} }
LOG1("In Get Characteristics #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); LOG1("In Get Characteristics #%d (%s)...\n",clientNumber,client.remoteIP().toString().c_str());
int len=strlen(urlBuf); // determine number of IDs specified by counting commas in URL int len=strlen(urlBuf); // determine number of IDs specified by counting commas in URL
int numIDs=1; int numIDs=1;
@ -978,7 +978,7 @@ int HAPClient::putCharacteristicsURL(char *json){
return(0); return(0);
} }
LOG1("In Put Characteristics #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); LOG1("In Put Characteristics #%d (%s)...\n",clientNumber,client.remoteIP().toString().c_str());
int n=homeSpan.countCharacteristics(json); // count number of objects in JSON request int n=homeSpan.countCharacteristics(json); // count number of objects in JSON request
if(n==0) // if no objects found, return if(n==0) // if no objects found, return
@ -1031,7 +1031,7 @@ int HAPClient::putPrepareURL(char *json){
return(0); return(0);
} }
LOG1("In Put Prepare #%d (%s)...\n",client.fd()-LWIP_SOCKET_OFFSET,client.remoteIP().toString().c_str()); LOG1("In Put Prepare #%d (%s)...\n",clientNumber,client.remoteIP().toString().c_str());
char ttlToken[]="\"ttl\":"; char ttlToken[]="\"ttl\":";
char pidToken[]="\"pid\":"; char pidToken[]="\"pid\":";
@ -1292,6 +1292,8 @@ void HAPClient::tlvRespond(TLV8 &tlv8){
else else
LOG2("-------- SENT ENCRYPTED! --------\n"); LOG2("-------- SENT ENCRYPTED! --------\n");
free(body);
} // tlvRespond } // tlvRespond
////////////////////////////////////// //////////////////////////////////////
@ -1562,6 +1564,9 @@ HapOut::HapStreamBuffer::~HapStreamBuffer(){
sync(); sync();
free(buffer); free(buffer);
free(encBuf);
free(hash);
free(ctx);
} }
////////////////////////////////////// //////////////////////////////////////

View File

@ -92,6 +92,7 @@ struct HAPClient {
// individual structures and data defined for each Hap Client connection // individual structures and data defined for each Hap Client connection
WiFiClient client; // handle to client WiFiClient client; // handle to client
int clientNumber; // client number
Controller *cPair=NULL; // pointer to info on current, session-verified Paired Controller (NULL=un-verified, and therefore un-encrypted, connection) Controller *cPair=NULL; // pointer to info on current, session-verified Paired Controller (NULL=un-verified, and therefore un-encrypted, connection)
// These temporary Curve25519 keys are generated in the first call to pair-verify and used in the second call to pair-verify so must persist for a short period // These temporary Curve25519 keys are generated in the first call to pair-verify and used in the second call to pair-verify so must persist for a short period

View File

@ -235,13 +235,14 @@ void Span::pollTask() {
auto it=hapList.emplace(hapList.begin()); auto it=hapList.emplace(hapList.begin());
(*it).client=hapServer->available(); (*it).client=hapServer->available();
(*it).clientNumber=(*it).client.fd()-LWIP_SOCKET_OFFSET;
// homeSpan.clearNotify(socket); // clear all notification requests for this connection // homeSpan.clearNotify(socket); // clear all notification requests for this connection
HAPClient::pairStatus=pairState_M1; // reset starting PAIR STATE (which may be needed if Accessory failed in middle of pair-setup) HAPClient::pairStatus=pairState_M1; // reset starting PAIR STATE (which may be needed if Accessory failed in middle of pair-setup)
LOG2("=======================================\n"); LOG2("=======================================\n");
LOG1("** Client #%d Connected (%lu sec): %s\n",(*it).client.fd()-LWIP_SOCKET_OFFSET,millis()/1000,(*it).client.remoteIP().toString().c_str()); LOG1("** Client #%d Connected (%lu sec): %s\n",(*it).clientNumber,millis()/1000,(*it).client.remoteIP().toString().c_str());
LOG2("\n"); LOG2("\n");
} }
@ -257,7 +258,7 @@ void Span::pollTask() {
} }
it++; it++;
} else { } else {
LOG1("** Client #%d DISCONNECTED (%lu sec)\n",(*it).client.fd()-LWIP_SOCKET_OFFSET,millis()/1000); LOG1("** Client #%d DISCONNECTED (%lu sec)\n",(*it).clientNumber,millis()/1000);
(*it).client.stop(); (*it).client.stop();
delay(5); delay(5);
it=hapList.erase(it); it=hapList.erase(it);
@ -557,9 +558,11 @@ void Span::processSerialCommand(const char *c){
switch(c[0]){ switch(c[0]){
case 'Z': { case 'Z': {
for(int i=0;i<CONFIG_LWIP_MAX_SOCKETS;i++) for(auto it=hapList.begin(); it!=hapList.end(); ++it){
if(hap[i] && hap[i]->client) (*it).client.stop();
hap[i]->client.stop(); delay(5);
}
} }
break; break;
@ -580,11 +583,11 @@ void Span::processSerialCommand(const char *c){
LOG0("\n"); LOG0("\n");
for(auto it=hapList.begin(); it!=hapList.end(); ++it){ for(auto it=hapList.begin(); it!=hapList.end(); ++it){
LOG0("Client #%d: %s",(*it).client.fd()-LWIP_SOCKET_OFFSET,(*it).client.remoteIP().toString().c_str()); LOG0("Client #%d: %s",(*it).clientNumber,(*it).client.remoteIP().toString().c_str());
if((*it).cPair){ if((*it).cPair){
LOG0(" ID="); LOG0(" ID=");
HAPClient::charPrintRow((*it).cPair->getID(),36); HAPClient::charPrintRow((*it).cPair->getID(),36);
LOG0((*it).cPair->isAdmin()?" (admin)":" (regular)\n"); LOG0((*it).cPair->isAdmin()?" (admin)\n":" (regular)\n");
} else { } else {
LOG0(" (unverified)\n"); LOG0(" (unverified)\n");
} }

View File

@ -36,6 +36,8 @@ void setup() {
homeSpan.begin(Category::Lighting,"HomeSpan LightBulb"); homeSpan.begin(Category::Lighting,"HomeSpan LightBulb");
new SpanUserCommand('D', " - disconnect WiFi", [](const char *buf){WiFi.disconnect();});
new SpanAccessory(); new SpanAccessory();
new Service::AccessoryInformation(); new Service::AccessoryInformation();
new Characteristic::Identify(); new Characteristic::Identify();