From 7c5f1e308b4691bea5c5d4faeddf62b752b34cc2 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Fri, 4 May 2018 20:42:58 +0200 Subject: [PATCH] - CS 104 master: fixed potential race condition when new connection closes because of a protocol error --- .../cs101_master_balanced/master_example.c | 3 +++ lib60870-C/src/iec60870/cs104/cs104_slave.c | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib60870-C/examples/cs101_master_balanced/master_example.c b/lib60870-C/examples/cs101_master_balanced/master_example.c index 2e405781..cf7a350c 100644 --- a/lib60870-C/examples/cs101_master_balanced/master_example.c +++ b/lib60870-C/examples/cs101_master_balanced/master_example.c @@ -106,6 +106,9 @@ main(int argc, char** argv) CS101_Master master = CS101_Master_create(port, NULL, NULL, IEC60870_LINK_LAYER_BALANCED); + + CS101_Master_setOwnAddress(master, 3); + /* Set the address of the slave (optional for balanced master */ CS101_Master_useSlaveAddress(master, 3); diff --git a/lib60870-C/src/iec60870/cs104/cs104_slave.c b/lib60870-C/src/iec60870/cs104/cs104_slave.c index d7cdcb22..2bf78b12 100644 --- a/lib60870-C/src/iec60870/cs104/cs104_slave.c +++ b/lib60870-C/src/iec60870/cs104/cs104_slave.c @@ -1968,17 +1968,21 @@ MasterConnection_create(CS104_Slave slave, Socket socket, MessageQueue lowPrioQu self->highPrioQueue = highPrioQueue; self->outstandingTestFRConMessages = 0; - - Thread newThread = - Thread_create((ThreadExecutionFunction) connectionHandlingThread, - (void*) self, true); - - Thread_start(newThread); } return self; } +static void +MasterConnection_start(MasterConnection self) +{ + Thread newThread = + Thread_create((ThreadExecutionFunction) connectionHandlingThread, + (void*) self, true); + + Thread_start(newThread); +} + void MasterConnection_close(MasterConnection self) { @@ -2077,6 +2081,9 @@ serverThread (void* parameter) #if (CONFIG_USE_THREADS) Semaphore_post(self->openConnectionsLock); #endif + + /* now start the connection handling (thread) */ + MasterConnection_start(connection); } else DEBUG_PRINT("Connection attempt failed!");