- IED client: fixed memory leak when reusing IedConnection

pull/331/head
Michael Zillgith 4 years ago
parent cda2eba93b
commit abd26eedca

@ -147,7 +147,6 @@ IsoClientConnection_create(IsoConnectionParameters parameters, IsoIndicationCall
IsoClientConnection self = (IsoClientConnection) GLOBAL_CALLOC(1, sizeof(struct sIsoClientConnection)); IsoClientConnection self = (IsoClientConnection) GLOBAL_CALLOC(1, sizeof(struct sIsoClientConnection));
if (self) { if (self) {
self->parameters = parameters; self->parameters = parameters;
self->callback = callback; self->callback = callback;
self->callbackParameter = callbackParameter; self->callbackParameter = callbackParameter;
@ -194,6 +193,13 @@ IsoClientConnection_create(IsoConnectionParameters parameters, IsoIndicationCall
static bool static bool
sendConnectionRequestMessage(IsoClientConnection self) sendConnectionRequestMessage(IsoClientConnection self)
{ {
if (self->cotpConnection) {
/* Destroy existing handle set when connection is reused */
if (self->cotpConnection->handleSet)
Handleset_destroy(self->cotpConnection->handleSet);
self->cotpConnection->handleSet = NULL;
}
/* COTP (ISO transport) handshake */ /* COTP (ISO transport) handshake */
CotpConnection_init(self->cotpConnection, self->socket, self->receiveBuffer, self->cotpReadBuffer, self->cotpWriteBuffer); CotpConnection_init(self->cotpConnection, self->socket, self->receiveBuffer, self->cotpReadBuffer, self->cotpWriteBuffer);
@ -280,10 +286,10 @@ releaseSocket(IsoClientConnection self)
if (self->socket) { if (self->socket) {
#if (CONFIG_MMS_SUPPORT_TLS == 1) #if (CONFIG_MMS_SUPPORT_TLS == 1)
if (self->cotpConnection->tlsSocket) { if (self->cotpConnection->tlsSocket) {
TLSSocket_close(self->cotpConnection->tlsSocket); TLSSocket_close(self->cotpConnection->tlsSocket);
self->cotpConnection->tlsSocket = NULL; self->cotpConnection->tlsSocket = NULL;
} }
#endif #endif
Socket_destroy(self->socket); Socket_destroy(self->socket);
@ -750,7 +756,7 @@ void
IsoClientConnection_destroy(IsoClientConnection self) IsoClientConnection_destroy(IsoClientConnection self)
{ {
if (DEBUG_ISO_CLIENT) if (DEBUG_ISO_CLIENT)
printf("ISO_CLIENT: IsoClientConnection_destroy\n"); printf("ISO_CLIENT: IsoClientConnection_destroy(%p)\n", self);
int state = getState(self); int state = getState(self);

@ -451,8 +451,9 @@ CotpConnection_init(CotpConnection* self, Socket socket,
{ {
self->state = 0; self->state = 0;
self->socket = socket; self->socket = socket;
self->handleSet = Handleset_new( ); self->handleSet = Handleset_new();
Handleset_addSocket( self->handleSet, self->socket ); Handleset_addSocket( self->handleSet, self->socket );
#if (CONFIG_MMS_SUPPORT_TLS == 1) #if (CONFIG_MMS_SUPPORT_TLS == 1)
self->tlsSocket = NULL; self->tlsSocket = NULL;
#endif #endif

@ -485,82 +485,84 @@ IsoConnection
IsoConnection_create(Socket socket, IsoServer isoServer, bool isSingleThread) IsoConnection_create(Socket socket, IsoServer isoServer, bool isSingleThread)
{ {
IsoConnection self = (IsoConnection) GLOBAL_CALLOC(1, sizeof(struct sIsoConnection)); IsoConnection self = (IsoConnection) GLOBAL_CALLOC(1, sizeof(struct sIsoConnection));
self->socket = socket;
if (self) {
self->socket = socket;
#if (CONFIG_MMS_SUPPORT_TLS == 1) #if (CONFIG_MMS_SUPPORT_TLS == 1)
if (IsoServer_getTLSConfiguration(isoServer) != NULL) { if (IsoServer_getTLSConfiguration(isoServer) != NULL) {
self->tlsSocket = TLSSocket_create(socket, IsoServer_getTLSConfiguration(isoServer), true); self->tlsSocket = TLSSocket_create(socket, IsoServer_getTLSConfiguration(isoServer), true);
if (self->tlsSocket == NULL) { if (self->tlsSocket == NULL) {
if (DEBUG_ISO_SERVER) if (DEBUG_ISO_SERVER)
printf("ISO_SERVER: IsoConnection - TLS initialization failed\n"); printf("ISO_SERVER: IsoConnection - TLS initialization failed\n");
GLOBAL_FREEMEM(self); GLOBAL_FREEMEM(self);
return NULL; return NULL;
}
} }
}
#endif /* (CONFIG_MMS_SUPPORT_TLS == 1) */ #endif /* (CONFIG_MMS_SUPPORT_TLS == 1) */
self->receiveBuffer = (uint8_t*) GLOBAL_MALLOC(RECEIVE_BUF_SIZE);
self->receiveBuffer = (uint8_t*) GLOBAL_MALLOC(RECEIVE_BUF_SIZE); self->sendBuffer = (uint8_t*) GLOBAL_MALLOC(SEND_BUF_SIZE);
self->sendBuffer = (uint8_t*) GLOBAL_MALLOC(SEND_BUF_SIZE); self->msgRcvdHandler = NULL;
self->msgRcvdHandler = NULL; self->tickHandler = NULL;
self->tickHandler = NULL; self->handlerParameter = NULL;
self->handlerParameter = NULL; self->isoServer = isoServer;
self->isoServer = isoServer; self->state = ISO_CON_STATE_RUNNING;
self->state = ISO_CON_STATE_RUNNING; self->clientAddress = Socket_getPeerAddress(self->socket);
self->clientAddress = Socket_getPeerAddress(self->socket); self->localAddress = Socket_getLocalAddress(self->socket);
self->localAddress = Socket_getLocalAddress(self->socket);
#if (CONFIG_MMS_THREADLESS_STACK != 1) #if (CONFIG_MMS_THREADLESS_STACK != 1)
self->conMutex = Semaphore_create(1); self->conMutex = Semaphore_create(1);
#endif #endif
ByteBuffer_wrap(&(self->rcvBuffer), self->receiveBuffer, 0, RECEIVE_BUF_SIZE); ByteBuffer_wrap(&(self->rcvBuffer), self->receiveBuffer, 0, RECEIVE_BUF_SIZE);
self->cotpReadBuf = (uint8_t*) GLOBAL_MALLOC(CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE); self->cotpReadBuf = (uint8_t*) GLOBAL_MALLOC(CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE);
self->cotpWriteBuf = (uint8_t*) GLOBAL_MALLOC(CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE); self->cotpWriteBuf = (uint8_t*) GLOBAL_MALLOC(CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE);
ByteBuffer_wrap(&(self->cotpReadBuffer), self->cotpReadBuf, 0, CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE); ByteBuffer_wrap(&(self->cotpReadBuffer), self->cotpReadBuf, 0, CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE);
ByteBuffer_wrap(&(self->cotpWriteBuffer), self->cotpWriteBuf, 0, CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE); ByteBuffer_wrap(&(self->cotpWriteBuffer), self->cotpWriteBuf, 0, CONFIG_COTP_MAX_TPDU_SIZE + TPKT_RFC1006_HEADER_SIZE);
self->cotpConnection = (CotpConnection*) GLOBAL_CALLOC(1, sizeof(CotpConnection)); self->cotpConnection = (CotpConnection*) GLOBAL_CALLOC(1, sizeof(CotpConnection));
CotpConnection_init(self->cotpConnection, self->socket, &(self->rcvBuffer), &(self->cotpReadBuffer), &(self->cotpWriteBuffer)); CotpConnection_init(self->cotpConnection, self->socket, &(self->rcvBuffer), &(self->cotpReadBuffer), &(self->cotpWriteBuffer));
#if (CONFIG_MMS_SUPPORT_TLS == 1) #if (CONFIG_MMS_SUPPORT_TLS == 1)
if (self->tlsSocket) if (self->tlsSocket)
self->cotpConnection->tlsSocket = self->tlsSocket; self->cotpConnection->tlsSocket = self->tlsSocket;
#endif /* (CONFIG_MMS_SUPPORT_TLS == 1) */ #endif /* (CONFIG_MMS_SUPPORT_TLS == 1) */
self->session = (IsoSession*) GLOBAL_CALLOC(1, sizeof(IsoSession)); self->session = (IsoSession*) GLOBAL_CALLOC(1, sizeof(IsoSession));
IsoSession_init(self->session); IsoSession_init(self->session);
self->presentation = (IsoPresentation*) GLOBAL_CALLOC(1, sizeof(IsoPresentation)); self->presentation = (IsoPresentation*) GLOBAL_CALLOC(1, sizeof(IsoPresentation));
IsoPresentation_init(self->presentation); IsoPresentation_init(self->presentation);
self->acseConnection = (AcseConnection*) GLOBAL_CALLOC(1, sizeof(AcseConnection)); self->acseConnection = (AcseConnection*) GLOBAL_CALLOC(1, sizeof(AcseConnection));
#if (CONFIG_MMS_SUPPORT_TLS == 1) #if (CONFIG_MMS_SUPPORT_TLS == 1)
AcseConnection_init(self->acseConnection, IsoServer_getAuthenticator(self->isoServer), AcseConnection_init(self->acseConnection, IsoServer_getAuthenticator(self->isoServer),
IsoServer_getAuthenticatorParameter(self->isoServer), self->tlsSocket); IsoServer_getAuthenticatorParameter(self->isoServer), self->tlsSocket);
#else #else
AcseConnection_init(self->acseConnection, IsoServer_getAuthenticator(self->isoServer), AcseConnection_init(self->acseConnection, IsoServer_getAuthenticator(self->isoServer),
IsoServer_getAuthenticatorParameter(self->isoServer), NULL); IsoServer_getAuthenticatorParameter(self->isoServer), NULL);
#endif #endif
if (DEBUG_ISO_SERVER) if (DEBUG_ISO_SERVER)
printf("ISO_SERVER: IsoConnection: Start to handle connection for client %s\n", self->clientAddress); printf("ISO_SERVER: IsoConnection: Start to handle connection for client %s\n", self->clientAddress);
#if (CONFIG_MMS_SINGLE_THREADED == 0) #if (CONFIG_MMS_SINGLE_THREADED == 0)
#if (CONFIG_MMS_THREADLESS_STACK == 0) #if (CONFIG_MMS_THREADLESS_STACK == 0)
if (isSingleThread == false) { if (isSingleThread == false) {
self->handleSet = Handleset_new(); self->handleSet = Handleset_new();
Handleset_addSocket(self->handleSet, self->socket); Handleset_addSocket(self->handleSet, self->socket);
self->thread = Thread_create((ThreadExecutionFunction) handleTcpConnection, self, false); self->thread = Thread_create((ThreadExecutionFunction) handleTcpConnection, self, false);
} }
#endif #endif
#endif #endif
}
return self; return self;
} }
@ -592,6 +594,11 @@ IsoConnection_destroy(IsoConnection self)
if (self->socket != NULL) if (self->socket != NULL)
Socket_destroy(self->socket); Socket_destroy(self->socket);
#if (CONFIG_MMS_SINGLE_THREADED != 1) || (CONFIG_MMS_THREADLESS_STACK == 1)
if (self->handleSet)
Handleset_destroy(self->handleSet);
#endif
if (self->cotpConnection) { if (self->cotpConnection) {
if (self->cotpConnection->handleSet) if (self->cotpConnection->handleSet)
Handleset_destroy(self->cotpConnection->handleSet); Handleset_destroy(self->cotpConnection->handleSet);

Loading…
Cancel
Save