Browse Source

bugfix free memory

Leon Urbas 11 years ago
parent
commit
af02efd1b7

+ 4 - 4
examples/src/opcuaServer.c

@@ -59,7 +59,7 @@ void server_run()
 
         UA_ByteString slMessage = {-1,UA_NULL};
 
-	char optval = 1;
+	int optval = 1;
 	int sockfd, newsockfd, portno, clilen;
 	char buffer[BUFFER_SIZE];
 	struct sockaddr_in serv_addr, cli_addr;
@@ -84,7 +84,7 @@ void server_run()
 	serv_addr.sin_addr.s_addr = INADDR_ANY;
 	serv_addr.sin_port = htons(portno);
 
-	if (setsockopt(sockfd,SOL_SOCKET,(SO_REUSEADDR),(char*)&optval,sizeof(int)) == -1) {
+	if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&optval,sizeof optval) == -1) {
 	    perror("setsockopt");
 	    exit(1);
 	}
@@ -111,7 +111,7 @@ void server_run()
 		exit(1);
 	}
 
-	printf("One connection accepted");
+	printf("One connection accepted\n");
 	while(1)
 	{
 		/* If connection is established then start communicating */
@@ -121,10 +121,10 @@ void server_run()
 
 		if (n > 0)
 		{
-			printf("received: %s\n",buffer);
 			connection.readData.data = buffer;
 			connection.readData.length = n;
 			connection.newDataToRead = 1;
+			UA_ByteString_printx("server_run - received=",&connection.readData);
 
 			//TL_receive(&connection, &slMessage);
 			SL_receive(&connection, &slMessage);

+ 4 - 0
src/UA_stackInternalTypes.c

@@ -37,6 +37,10 @@ UA_Int32 UA_MessageType_decode(UA_Byte const * src, UA_Int32* pos, UA_MessageTyp
 }
 UA_TYPE_METHOD_DELETE_FREE(UA_MessageType)
 UA_TYPE_METHOD_DELETEMEMBERS_NOACTION(UA_MessageType)
+UA_Int32 UA_MessageType_printf(char *label, UA_MessageType* p) {
+	UA_Byte* b = (UA_Byte*) p;
+	printf("%s{%c%c%c}\n", label, b[2],b[1],b[0]);
+}
 
 UA_Int32 UA_OPCUATcpMessageHeader_calcSize(UA_OPCUATcpMessageHeader const * ptr) {
 	if(ptr==UA_NULL){return sizeof(UA_OPCUATcpMessageHeader);}

+ 4 - 2
src/opcua_basictypes.c

@@ -88,7 +88,9 @@ UA_Int32 UA_Array_new(void **p,UA_Int32 noElements, UA_Int32 type) {
 
 UA_Int32 _UA_free(void * ptr,char* f,int l){
 	printf("UA_free;%p;;%s;%d\n",ptr,f,l); fflush(stdout);
-	free(ptr);
+	if (UA_NULL != ptr) {
+		free(ptr);
+	}
 	return UA_SUCCESS;
 }
 
@@ -399,7 +401,7 @@ void UA_String_printx(char* label, UA_String* string) {
 	if (string->length > 0) {
 		for (i = 0; i < string->length; i++) {
 			printf("%c%d", i == 0 ? '{' : ',', (string->data)[i]);
-			if (i > 0 && !(i%20)) { printf("\n\t"); }
+			// if (i > 0 && !(i%20)) { printf("\n\t"); }
 		}
 	} else {
 		printf("{");

+ 3 - 0
src/opcua_namespace.c

@@ -273,6 +273,7 @@ static hash_t hash_string (const UA_Byte * data, UA_Int32 len) {
 	case 1: hash += (signed char)*data;
 		hash ^= hash << 10;
 		hash += hash >> 1;
+		break;
     }
 
     /* Force "avalanching" of final 127 bits */
@@ -385,6 +386,7 @@ static UA_Int32 find_slot (const namespace *ns, UA_Node **slot, UA_NodeId *nodei
 			return UA_SUCCESS;
 		}
     }
+	return UA_SUCCESS;
 }
 
 /* Always returns an empty slot. This is inevitable if the entries are not
@@ -407,6 +409,7 @@ static UA_Node ** find_empty_slot(const namespace *ns, hash_t h) {
 		if (*slot == UA_NULL)
 			return slot;
 	}
+	return UA_NULL;
 }
 
 /* The following function changes size of memory allocated for the

+ 6 - 8
src/opcua_secureLayer.c

@@ -462,7 +462,6 @@ void SL_receive(UA_connection *connection, UA_ByteString *serviceMessage) {
 			UA_ByteString_printf("SL_receive - AAS_Header.SenderCertificate=",
 					&(asymAlgSecHeader.senderCertificate));
 			if (secureConvHeader.secureChannelId != 0) {
-
 				iTmp = UA_ByteString_compare(
 						&(connection->secureLayer.remoteAsymAlgSettings.SenderCertificate),
 						&(asymAlgSecHeader.senderCertificate));
@@ -470,9 +469,6 @@ void SL_receive(UA_connection *connection, UA_ByteString *serviceMessage) {
 					printf("SL_receive - UA_ERROR_BadSecureChannelUnknown \n");
 					//TODO return UA_ERROR_BadSecureChannelUnknown
 				}
-
-			//FIXME: destroy decodeAASHeader (to prevent memleak)
-			// UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymAlgSecHeader);
 			}
 			else
 			{
@@ -496,9 +492,12 @@ void SL_receive(UA_connection *connection, UA_ByteString *serviceMessage) {
 			UA_ByteString_printx("SL_receive - message=",&message);
 
 			SL_processMessage(connection, message);
+			// Clean up
+			UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymAlgSecHeader);
 
 			break;
 		case UA_MESSAGETYPE_MSG: /* secure Channel Message received */
+			UA_ByteString_printx("SL_receive - MSG, msg=", serviceMessage);
 			if (connection->secureLayer.connectionState
 					== connectionState_ESTABLISHED) {
 
@@ -512,18 +511,17 @@ void SL_receive(UA_connection *connection, UA_ByteString *serviceMessage) {
 
 			break;
 		case UA_MESSAGETYPE_CLO: /* closeSecureChannel Message received */
+			UA_ByteString_printx("SL_receive - CLO, msg=", serviceMessage);
 			if (SL_check(connection, secureChannelPacket) == UA_NO_ERROR) {
 
 			}
 			break;
 		}
-
+		// Clean up
+		UA_SecureConversationMessageHeader_deleteMembers(&secureConvHeader);
 	} else {
 		printf("SL_receive - no data received \n");
 	}
-	// Clean up
-	UA_SecureConversationMessageHeader_deleteMembers(&secureConvHeader);
-	UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymAlgSecHeader);
 	/*
 	 Int32 readPosition = 0;
 

+ 11 - 18
src/opcua_transportLayer.c

@@ -26,26 +26,21 @@ UA_Int32 TL_check(UA_connection *connection)
 {
 	UA_Int32 position = 4;
 	UA_Int32 messageLength = 0;
+	UA_Int32 retval = UA_SUCCESS;
 
 
 	printf("TL_check - entered \n");
 
-	UA_ByteString_printf("received data:",&(connection->readData));
 	UA_Int32_decode(connection->readData.data,&position,&messageLength);
-
 	printf("TL_check - messageLength = %d \n",messageLength);
 
-	if (messageLength != -1 && messageLength == connection->readData.length &&
-			messageLength < (UA_Int32) connection->transportLayer.localConf.maxMessageSize)
+	if (messageLength == -1 || messageLength != connection->readData.length ||
+			messageLength > (UA_Int32) connection->transportLayer.localConf.maxMessageSize)
 	{
-		printf("TL_check - no error \n");
-		return UA_NO_ERROR;
+		printf("TL_check - length error \n");
+		retval = UA_ERR_INCONSISTENT;
 	}
-
-	printf("TL_check - length error \n");
-
-	return UA_ERROR;
-
+	return retval;
 }
 
 
@@ -72,7 +67,7 @@ UA_Int32 TL_receive(UA_connection *connection, UA_ByteString *packet)
 		printf("TL_receive - connection->readData.length %d \n",connection->readData.length);
 
 
-		printf("TL_receive - MessageType = %d \n",tcpMessageHeader->messageType);
+		UA_MessageType_printf("TL_receive - messageType=",&(tcpMessageHeader->messageType));
 		switch(tcpMessageHeader->messageType)
 		{
 		case UA_MESSAGETYPE_MSG:
@@ -198,7 +193,6 @@ UA_Int32 TL_process(UA_connection *connection,UA_Int32 packetType, UA_Int32 *pos
 			printf("TL_process - localConf.maxMessageSize = %d \n", connection->transportLayer.localConf.maxMessageSize);
 			//maximum chunk count
 			printf("TL_process - localConf.maxChunkCount = %d \n", connection->transportLayer.localConf.maxChunkCount);
-			UA_ByteString_printf("encoded data",&tmpMessage);
 			TL_send(connection, &tmpMessage);
 			// do not delete tmpMessage - this is the responsibility of the send thread
 		}
@@ -220,22 +214,21 @@ UA_Int32 TL_process(UA_connection *connection,UA_Int32 packetType, UA_Int32 *pos
 
 UA_Int32 TL_send(UA_connection* connection, UA_ByteString* packet)
 {
+	UA_Int32 retval = UA_SUCCESS;
 	printf("TL_send - entered \n");
 	connection->newDataToWrite = 1;
 	if(packet->length != -1 && packet->length < (UA_Int32) connection->transportLayer.remoteConf.maxMessageSize)
 	{
+		UA_ByteString_printx("TL_send - data=", packet);
 		connection->writeData.data = packet->data;
 		connection->writeData.length = packet->length;
-		printf("TL_send - packet length = %d \n", packet->length);
 	}
 	else
 	{
 		printf("TL_send - ERROR: packet size greater than remote buffer size");
-		//server error
+		retval = UA_ERROR;
 	}
-
-	return UA_SUCCESS;
-
+	return retval;
 }