Ver código fonte

fixes for reported issues by coverity

Florian Palm 10 anos atrás
pai
commit
e9ae08247c

+ 1 - 1
examples/src/opcuaServer.c

@@ -47,7 +47,7 @@ int main(int argc, char** argv) {
 	NL_data* nl = NL_init(&NL_Description_TcpBinary, 16664);
 	UA_String endpointUrl;
 	UA_String_copycstring("no endpoint url",&endpointUrl);
-	SL_ChannelManager_init(4,36000,244,2,&endpointUrl);
+	SL_ChannelManager_init(10,36000,244,2,&endpointUrl);
 	UA_SessionManager_init(10,3600000,25);
 	struct timeval tv = {1, 0}; // 1 second
 	NL_msgLoop(nl, &tv, serverCallback, argv[0], &running);

+ 8 - 7
examples/src/opcuaServerACPLT.c

@@ -51,7 +51,7 @@ void server_run() {
 
 	tmpTestFunction();
 #endif
-	UA_TL_Connection connection;// = UA_NULL;
+//	UA_TL_Connection connection;// = UA_NULL;
 	TL_Buffer localBuffers;
 	UA_Int32 connectionState;
 	//connection.connectionState = CONNECTIONSTATE_CLOSED;
@@ -118,14 +118,15 @@ void server_run() {
 		UA_TL_ConnectionManager_getConnectionByHandle(newsockfd, &tmpConnection);
 		if(tmpConnection == UA_NULL)
 		{
-			UA_TL_Connection_new(&connection, localBuffers, (TL_Writer)NL_TCP_writer,NL_Connection_close,newsockfd,UA_NULL);
+			UA_TL_Connection_new(&tmpConnection, localBuffers, (TL_Writer)NL_TCP_writer,NL_Connection_close,newsockfd,UA_NULL);
 		}
-		UA_TL_Connection_getState(connection, &connectionState);
+
+		UA_TL_Connection_getState(tmpConnection, &connectionState);
 
 		printf("server_run - connection accepted: %i, state: %i\n", newsockfd, connectionState);
 
-		UA_TL_Connection_bind(connection, newsockfd);
-		//connection.connectionHandle = newsockfd;
+		UA_TL_Connection_bind(tmpConnection, newsockfd);
+
 		do {
             memset(buffer, 0, BUFFER_SIZE);
 			n = read(newsockfd, buffer, BUFFER_SIZE);
@@ -135,12 +136,12 @@ void server_run() {
 #ifdef DEBUG
 				UA_ByteString_printx("server_run - received=",&slMessage);
 #endif
-				TL_Process(connection, &slMessage);
+				TL_Process(tmpConnection, &slMessage);
 			} else if (n <= 0) {
 				perror("ERROR reading from socket1");
 		//		exit(1);
 			}
-			UA_TL_Connection_getState(connection, &connectionState);
+			UA_TL_Connection_getState(tmpConnection, &connectionState);
 		} while(connectionState != CONNECTIONSTATE_CLOSE);
 		shutdown(newsockfd,2);
 		close(newsockfd);

+ 1 - 0
src/ua_application.c

@@ -536,6 +536,7 @@ void appMockup_init() {
 	state->displayName = UA_LOCALIZEDTEXT_STATIC("State");
 	state->description = UA_LOCALIZEDTEXT_STATIC("State");
 	state->value.vt = &UA_borrowed_.types[UA_SERVERSTATE];
+	state->value.arrayDimensionsLength = 1; // added to ensure encoding in readreponse
 	state->value.arrayLength = 1;
 	state->value.data = &status->state; // points into the other object.
 	Namespace_insert(ns0,(UA_Node**)&state, NAMESPACE_INSERT_UNIQUE);

+ 24 - 14
src/ua_services_discovery.c

@@ -1,40 +1,50 @@
 #include "ua_services.h"
-UA_Int32 Service_GetEndpoints(SL_Channel channel, const UA_GetEndpointsRequest* request, UA_GetEndpointsResponse *response) {
+UA_Int32 Service_GetEndpoints(SL_Channel channel,
+		const UA_GetEndpointsRequest* request,
+		UA_GetEndpointsResponse *response) {
 #ifdef DEBUG
 	UA_String_printx("endpointUrl=", &request->endpointUrl);
 #endif
 
 	response->endpointsSize = 1;
-	UA_Array_new((void**) &response->endpoints,response->endpointsSize,&UA_.types[UA_ENDPOINTDESCRIPTION]);
+	UA_Array_new((void**) &response->endpoints, response->endpointsSize,
+			&UA_.types[UA_ENDPOINTDESCRIPTION]);
 
 	//Security issues:
 	//The policy should be 'http://opcfoundation.org/UA/SecurityPolicy#None'
 	//FIXME String or ByteString
 	UA_AsymmetricAlgorithmSecurityHeader *asymSettings;
 	SL_Channel_getLocalAsymAlgSettings(channel, &asymSettings);
-	UA_String_copy((UA_String *)&(asymSettings->securityPolicyUri), &response->endpoints[0].securityPolicyUri);
+	UA_String_copy((UA_String *) &(asymSettings->securityPolicyUri),
+			&response->endpoints[0].securityPolicyUri);
 	//FIXME hard-coded code
 	response->endpoints[0].securityMode = UA_MESSAGESECURITYMODE_NONE;
-	UA_String_copycstring("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary", &response->endpoints[0].transportProfileUri);
+	UA_String_copycstring(
+			"http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary",
+			&response->endpoints[0].transportProfileUri);
 
 	response->endpoints[0].userIdentityTokensSize = 1;
-	UA_Array_new((void**) &response->endpoints[0].userIdentityTokens, response->endpoints[0].userIdentityTokensSize,
-				 &UA_.types[UA_USERTOKENPOLICY]);
+	UA_Array_new((void**) &response->endpoints[0].userIdentityTokens,
+			response->endpoints[0].userIdentityTokensSize,
+			&UA_.types[UA_USERTOKENPOLICY]);
 	UA_UserTokenPolicy *token = &response->endpoints[0].userIdentityTokens[0];
 	UA_String_copycstring("my-anonymous-policy", &token->policyId); // defined per server
 	token->tokenType = UA_USERTOKENTYPE_ANONYMOUS;
-	token->issuerEndpointUrl = (UA_String) {-1, UA_NULL};
-	token->issuedTokenType = (UA_String) {-1, UA_NULL};
-	token->securityPolicyUri = (UA_String) {-1, UA_NULL};
+	token->issuerEndpointUrl = (UA_String ) { -1, UA_NULL };
+	token->issuedTokenType = (UA_String ) { -1, UA_NULL };
+	token->securityPolicyUri = (UA_String ) { -1, UA_NULL };
 
-	UA_String_copy(&request->endpointUrl,&response->endpoints[0].endpointUrl);
-	UA_String_copycstring("http://open62541.info/product/release",&(response->endpoints[0].server.productUri));
+	UA_String_copy(&request->endpointUrl, &response->endpoints[0].endpointUrl);
+	UA_String_copycstring("http://open62541.info/product/release",
+			&(response->endpoints[0].server.productUri));
 	// FIXME: This information should be provided by the application, preferably in the address space
-	UA_String_copycstring("http://open62541.info/applications/4711",&(response->endpoints[0].server.applicationUri));
-	UA_LocalizedText_copycstring("The open62541 application",&(response->endpoints[0].server.applicationName));
+	UA_String_copycstring("http://open62541.info/applications/4711",
+			&(response->endpoints[0].server.applicationUri));
+	UA_LocalizedText_copycstring("The open62541 application",
+			&(response->endpoints[0].server.applicationName));
 	// FIXME: This should be a feature of the application and an enum
 	response->endpoints[0].server.applicationType = UA_APPLICATIONTYPE_SERVER;
 	// all the other strings are empty by initialization
-	
+
 	return UA_SUCCESS;
 }

+ 10 - 10
src/ua_stack_channel.c

@@ -39,9 +39,10 @@ UA_Int32 SL_Channel_setRemoteSecuritySettings(SL_Channel channel,
 	UA_Int32 retval = UA_SUCCESS;
 	retval |= UA_AsymmetricAlgorithmSecurityHeader_copy(asymSecHeader,
 			&((SL_ChannelType*) channel)->remoteAsymAlgSettings);
-
+	//set starting sequence number from remote partner
 	((SL_ChannelType*) channel)->sequenceNumber =
 			sequenceHeader->sequenceNumber;
+	//set starting request id from remote partner
 	((SL_ChannelType*) channel)->requestId = sequenceHeader->requestId;
 	return retval;
 }
@@ -268,6 +269,7 @@ UA_Int32 SL_Channel_processTokenRequest(SL_Channel channel,
 		return ((SL_ChannelType*) channel)->tokenProvider(channel,
 				requestedLifetime, requestType,
 				&((SL_ChannelType*) channel)->securityToken);
+
 	}
 	printf("SL_Channel_processTokenRequest - no Token provider registered");
 	return UA_ERROR;
@@ -283,10 +285,8 @@ UA_Int32 SL_Channel_renewToken(SL_Channel channel, UA_UInt32 tokenId,
 
 UA_Int32 SL_Channel_checkSequenceNumber(SL_Channel channel,
 		UA_UInt32 sequenceNumber) {
-	((SL_ChannelType*) channel)->sequenceNumber = sequenceNumber; //TODO mock up, to be removed;
-	return UA_SUCCESS;
-
-	if (((SL_ChannelType*) channel)->sequenceNumber + 1 == sequenceNumber) {
+	//TODO review checking of sequence
+	if (((SL_ChannelType*) channel)->sequenceNumber+1  == sequenceNumber) {
 		((SL_ChannelType*) channel)->sequenceNumber++;
 
 		return UA_SUCCESS;
@@ -299,9 +299,8 @@ UA_Int32 SL_Channel_checkSequenceNumber(SL_Channel channel,
 }
 
 UA_Int32 SL_Channel_checkRequestId(SL_Channel channel, UA_UInt32 requestId) {
-	((SL_ChannelType*) channel)->requestId = requestId; //TODO mock up, to be removed;
-	return UA_SUCCESS;
-	if (((SL_ChannelType*) channel)->requestId + 1 == requestId) {
+	//TODO review checking of request id
+	if (((SL_ChannelType*) channel)->requestId+1  == requestId) {
 		((SL_ChannelType*) channel)->requestId++;
 
 		return UA_SUCCESS;
@@ -357,10 +356,11 @@ UA_Int32 SL_Channel_processOpenRequest(SL_Channel channel,
 					request->requestedLifetime, request->requestType);
 			if (retval != UA_SUCCESS) {
 				printf(
-						"SL_Channel_processOpenRequest - creating new token for an existing SecureChannel\n");
+						"SL_Channel_processOpenRequest - cannot create new token for an existing SecureChannel\n");
+
 			} else {
 				printf(
-						"SL_Channel_processOpenRequest - cannot create new token for an existing SecureChannel\n");
+						"SL_Channel_processOpenRequest - creating new token for an existing SecureChannel\n");
 			}
 
 			break;

+ 0 - 37
src/ua_stack_channel_manager.c

@@ -99,43 +99,6 @@ UA_Int32 SL_ChannelManager_generateToken(SL_Channel channel, UA_Int32 requestedL
 	return UA_ERROR;
 }
 
-UA_Int32 SL_ChannelManager_updateChannels()
-{
-	//TODO lock access
-	UA_Int32 channelLifetime;
-	UA_UInt32 channelId;
-	UA_list_Element* current = channelManager->channels.first;
-	while (current)
-	{
-		if (current->payload)
-		{
-			UA_indexedList_Element* elem =
-					(UA_indexedList_Element*) current->payload;
-			SL_Channel *channel = (SL_Channel*) elem->payload;
-			//remove channels with expired lifetime, close linked list
-			if (channel)
-			{
-				UA_list_addPayloadToBack(&channelManager->channels,(void*)channel);
-				SL_Channel_getRemainingLifetime(*channel,&channelLifetime);
-				if(channelLifetime <= 0)
-				{
-					//removeChannel
-				}
-			}
-			else
-			{
-				SL_Channel_getChannelId(*channel, &channelId);
-				//not possible to remove channel
-				printf(
-						"UA_SL_ChannelManager_updateChannels - could not remove channel with id: %i \n",
-						channelId);
-				return UA_SUCCESS;
-			}
-		}
-	}
-	return UA_SUCCESS;
-}
-
 UA_Int32 SL_ChannelManager_removeChannel(UA_Int32 channelId)
 {
 	//TODO lock access

+ 1 - 1
src/ua_stack_channel_manager.h

@@ -26,7 +26,7 @@ UA_Int32 SL_ChannelManager_addChannel(SL_Channel *channel);
 UA_Int32 SL_ChannelManager_removeChannel(UA_Int32 channelId);
 UA_Int32 SL_ChannelManager_getChannel(UA_UInt32 channelId, SL_Channel *channel);
 
-UA_Int32 SL_ChannelManager_updateChannels();
+
 
 UA_Int32 SL_ChannelManager_getChannelLifeTime(UA_DateTime *lifeTime);
 

+ 1 - 1
src/ua_stack_session_manager.c

@@ -67,7 +67,7 @@ UA_Boolean UA_SessionManager_sessionExists(UA_Session *session)
 
 UA_Int32 UA_SessionManager_getSessionById(UA_NodeId *sessionId, UA_Session *session)
 {
-	if(sessionManager != UA_NULL)
+	if(sessionManager == UA_NULL)
 	{
 		*session = UA_NULL;
 		return UA_ERROR;

+ 19 - 9
src/ua_transport_binary.c

@@ -61,17 +61,27 @@ static UA_Int32 TL_handleHello(UA_TL_Connection connection, const UA_ByteString*
 static UA_Int32 TL_handleOpen(UA_TL_Connection connection, const UA_ByteString* msg, UA_UInt32* pos) {
 	UA_Int32 retval = UA_SUCCESS;
 	UA_Int32 state;
-	SL_Channel *channel;
-
+	SL_Channel channel;
+	UA_UInt32 secureChannelId;
 	retval |= UA_TL_Connection_getState(connection,&state);
 	if (state == CONNECTIONSTATE_ESTABLISHED) {
-		retval |= SL_Channel_new(&channel);//just create channel
-		retval |= SL_Channel_init(*channel,connection,
-				SL_ChannelManager_generateChannelId,
-			SL_ChannelManager_generateToken);
-		retval |= SL_Channel_bind(*channel,connection);
-		retval |= SL_ProcessOpenChannel(*channel, msg, pos);
-		retval |= SL_ChannelManager_addChannel(channel);
+		UA_UInt32_decodeBinary(msg, pos, &secureChannelId);
+		SL_ChannelManager_getChannel(secureChannelId, &channel);
+		if(channel == UA_NULL)
+		{
+			SL_Channel *newChannel;
+			//create new channel
+			retval |= SL_Channel_new(&newChannel);//just create channel
+			retval |= SL_Channel_init(*newChannel,connection,
+					SL_ChannelManager_generateChannelId,
+				SL_ChannelManager_generateToken);
+			retval |= SL_Channel_bind(*newChannel,connection);
+			retval |= SL_ProcessOpenChannel(*newChannel, msg, pos);
+			retval |= SL_ChannelManager_addChannel(newChannel);
+			return retval;
+		}
+		// channel already exists, renew token?
+		retval |= SL_ProcessOpenChannel(channel, msg, pos);
 		return retval;
 	}else{
 		printf("TL_handleOpen - ERROR: could not create new secureChannel");

+ 6 - 6
src/ua_transport_binary_secure.c

@@ -405,7 +405,6 @@ UA_Int32 SL_ProcessOpenChannel(SL_Channel channel, const UA_ByteString* msg,
 	UA_Int32 retval = UA_SUCCESS;
 	UA_SequenceHeader sequenceHeader;
 	UA_AsymmetricAlgorithmSecurityHeader asymHeader;
-	*pos+=4; //skip secure channel id
 	UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(msg,pos,&asymHeader);
 	UA_SequenceHeader_decodeBinary(msg,pos,&sequenceHeader);
 
@@ -413,11 +412,12 @@ UA_Int32 SL_ProcessOpenChannel(SL_Channel channel, const UA_ByteString* msg,
 	SL_Channel_setRemoteSecuritySettings(channel,&asymHeader,&sequenceHeader);
 	return SL_handleRequest(channel, msg, pos) | retval;
 }
-UA_Int32 SL_ProcessCloseChannel(SL_Channel channel, const UA_ByteString* msg,
-		UA_UInt32 *pos)
-{
-	return SL_handleRequest(channel, msg, pos);
-}
+/* not used anymore */
+//UA_Int32 SL_ProcessCloseChannel(SL_Channel channel, const UA_ByteString* msg,
+//		UA_UInt32 *pos)
+//{
+//	return SL_handleRequest(channel, msg, pos);
+//}
 UA_Int32 SL_Process(const UA_ByteString* msg,
 		UA_UInt32* pos)
 {

+ 5 - 7
src/ua_transport_connection_manager.c

@@ -22,18 +22,16 @@ static UA_TL_ConnectionManager *connectionManager = UA_NULL;
 UA_Int32 UA_TL_ConnectionManager_init(UA_UInt32 maxConnectionCount)
 {
 	UA_Int32 retval = UA_SUCCESS;
-	if(connectionManager)
-	{
-		//connectionManager already exists;
-	}
-	else
+	if(connectionManager == UA_NULL)
 	{
-		retval |= UA_alloc((void**)connectionManager,sizeof(UA_TL_ConnectionManager));
+		retval |= UA_alloc((void**)&connectionManager,sizeof(UA_TL_ConnectionManager));
 		connectionManager->maxConnectionCount = maxConnectionCount;
 		connectionManager->currentConnectionCount = 0;
 		retval |= UA_indexedList_init(&connectionManager->connections);
+		return UA_SUCCESS;
 	}
-	return UA_SUCCESS;
+	return UA_ERROR; //connection Manager already exists
+
 }
 UA_Int32 UA_TL_ConnectionManager_addConnection(UA_TL_Connection *connection)
 {