- MMS server: gracefully reject connection when not enought memory can be allocated

pull/147/head
Michael Zillgith 7 years ago
parent 472514cc63
commit d24f0c44c5

@ -55,6 +55,11 @@ Memory_calloc(size_t nmemb, size_t size);
void * void *
Memory_realloc(void *ptr, size_t size); Memory_realloc(void *ptr, size_t size);
/**
* \brief Free memory block
*
* \param address of memory block to release of NULL for no operation
*/
void void
Memory_free(void* memb); Memory_free(void* memb);

@ -90,34 +90,50 @@ struct sIsoConnection
}; };
static void static void
finalizeIsoConnection(IsoConnection self) IsoConnection_releaseAllocatedMemory(IsoConnection self)
{ {
if (DEBUG_ISO_SERVER) if (self->socket)
printf("ISO_SERVER: finalizeIsoConnection --> close transport connection\n"); Socket_destroy(self->socket);
#if (CONFIG_MMS_SUPPORT_TLS == 1)
if (IsoServer_getTLSConfiguration(self->isoServer) != NULL) {
TLSSocket_close(self->tlsSocket);
}
#endif /* (CONFIG_MMS_SUPPORT_TLS == 1) */
IsoServer_closeConnection(self->isoServer, self); GLOBAL_FREEMEM(self->session);
if (self->socket != NULL) GLOBAL_FREEMEM(self->presentation);
Socket_destroy(self->socket); AcseConnection_destroy(self->acseConnection);
GLOBAL_FREEMEM(self->acseConnection);
GLOBAL_FREEMEM(self->session); GLOBAL_FREEMEM(self->cotpReadBuf);
GLOBAL_FREEMEM(self->presentation); GLOBAL_FREEMEM(self->cotpWriteBuf);
AcseConnection_destroy(self->acseConnection);
GLOBAL_FREEMEM(self->acseConnection);
GLOBAL_FREEMEM(self->cotpReadBuf); GLOBAL_FREEMEM(self->cotpConnection);
GLOBAL_FREEMEM(self->cotpWriteBuf);
GLOBAL_FREEMEM(self->cotpConnection); #if (CONFIG_MMS_THREADLESS_STACK != 1)
if (self->conMutex)
Semaphore_destroy(self->conMutex);
#endif
#if (CONFIG_MMS_THREADLESS_STACK != 1) GLOBAL_FREEMEM(self->receiveBuffer);
Semaphore_destroy(self->conMutex); GLOBAL_FREEMEM(self->sendBuffer);
#endif GLOBAL_FREEMEM(self->clientAddress);
GLOBAL_FREEMEM(self);
}
static void
finalizeIsoConnection(IsoConnection self)
{
if (DEBUG_ISO_SERVER)
printf("ISO_SERVER: finalizeIsoConnection --> close transport connection\n");
GLOBAL_FREEMEM(self->receiveBuffer);
GLOBAL_FREEMEM(self->sendBuffer);
GLOBAL_FREEMEM(self->clientAddress);
IsoServer isoServer = self->isoServer; IsoServer isoServer = self->isoServer;
GLOBAL_FREEMEM(self);
IsoServer_closeConnection(isoServer, self);
IsoConnection_releaseAllocatedMemory(self);
if (DEBUG_ISO_SERVER) if (DEBUG_ISO_SERVER)
printf("ISO_SERVER: connection %p closed\n", self); printf("ISO_SERVER: connection %p closed\n", self);
@ -441,6 +457,10 @@ IsoConnection
IsoConnection_create(Socket socket, IsoServer isoServer) IsoConnection_create(Socket socket, IsoServer isoServer)
{ {
IsoConnection self = (IsoConnection) GLOBAL_CALLOC(1, sizeof(struct sIsoConnection)); IsoConnection self = (IsoConnection) GLOBAL_CALLOC(1, sizeof(struct sIsoConnection));
if (self == NULL)
return NULL;
self->socket = socket; self->socket = socket;
#if (CONFIG_MMS_SUPPORT_TLS == 1) #if (CONFIG_MMS_SUPPORT_TLS == 1)
@ -461,6 +481,10 @@ IsoConnection_create(Socket socket, IsoServer isoServer)
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);
if ((self->receiveBuffer == NULL) || (self->sendBuffer == NULL))
goto exit_error;
self->msgRcvdHandler = NULL; self->msgRcvdHandler = NULL;
self->msgRcvdHandlerParameter = NULL; self->msgRcvdHandlerParameter = NULL;
self->isoServer = isoServer; self->isoServer = isoServer;
@ -476,10 +500,17 @@ IsoConnection_create(Socket socket, IsoServer isoServer)
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);
if ((self->cotpReadBuf == NULL) || (self->cotpWriteBuf == NULL))
goto exit_error;
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));
if (self->cotpConnection == NULL)
goto exit_error;
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)
@ -488,13 +519,24 @@ IsoConnection_create(Socket socket, IsoServer isoServer)
#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));
if (self->session == NULL)
goto exit_error;
IsoSession_init(self->session); IsoSession_init(self->session);
self->presentation = (IsoPresentation*) GLOBAL_CALLOC(1, sizeof(IsoPresentation)); self->presentation = (IsoPresentation*) GLOBAL_CALLOC(1, sizeof(IsoPresentation));
if (self->presentation == NULL)
goto exit_error;
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 (self->acseConnection == NULL)
goto exit_error;
#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);
@ -510,11 +552,23 @@ IsoConnection_create(Socket socket, IsoServer isoServer)
#if (CONFIG_MMS_THREADLESS_STACK == 0) #if (CONFIG_MMS_THREADLESS_STACK == 0)
self->thread = Thread_create((ThreadExecutionFunction) handleTcpConnection, self, true); self->thread = Thread_create((ThreadExecutionFunction) handleTcpConnection, self, true);
Thread_start(self->thread); if (self->thread)
Thread_start(self->thread);
else
goto exit_error;
#endif #endif
#endif #endif
return self; return self;
exit_error:
if (DEBUG_ISO_SERVER)
printf("ISO_SERVER: Failed to allocate memory for new connection\n");
IsoConnection_releaseAllocatedMemory(self);
return NULL;
} }
void void

Loading…
Cancel
Save