Browse Source

still hunting the memory-bug that crashes the exampleServer

Leon Urbas 11 years ago
parent
commit
637d6f7c09

+ 2 - 1
examples/src/opcuaServer.c

@@ -69,7 +69,8 @@ void server_run()
 	SL_initConnectionObject(&connection);
 
 	/* First call to socket() function */
-	sockfd = socket(AF_INET, SOCK_STREAM, 0);
+	// sockfd = socket(AF_INET, SOCK_STREAM, 0);
+	sockfd = socket(PF_INET, SOCK_STREAM, 0);
 	if (sockfd < 0)
 	{
 		perror("ERROR opening socket");

+ 4 - 1
include/opcua_basictypes.h

@@ -55,6 +55,8 @@ UA_Int32 UA_Array_calcSize(UA_Int32 noElements, UA_Int32 type, void const ** ptr
 UA_Int32 UA_Array_encode(void const **src, UA_Int32 noElements, UA_Int32 type, UA_Int32* pos, UA_Byte * dst);
 UA_Int32 UA_Array_decode(UA_Byte const * src,UA_Int32 noElements, UA_Int32 type, UA_Int32* pos, void const **dst);
 UA_Int32 UA_Array_delete(void **p,UA_Int32 noElements);
+UA_Int32 UA_Array_init(void **p,UA_Int32 noElements, UA_Int32 type);
+UA_Int32 UA_Array_new(void **p,UA_Int32 noElements, UA_Int32 type);
 
 #define UA_TYPE_METHOD_PROTOTYPES(TYPE) \
 UA_Int32 TYPE##_calcSize(TYPE const * ptr);\
@@ -193,7 +195,8 @@ typedef struct T_UA_ByteString
 UA_ByteString;
 UA_TYPE_METHOD_PROTOTYPES (UA_ByteString)
 UA_Int32 UA_ByteString_compare(UA_ByteString *string1, UA_ByteString *string2);
-extern UA_String UA_String_securityPoliceNone;
+UA_Int32 UA_ByteString_copy(UA_ByteString const * src, UA_ByteString* dst);
+extern UA_ByteString UA_ByteString_securityPoliceNone;
 
 /** LocalizedTextBinaryEncoding - Part: 6, Chapter: 5.2.2.14, Page: 21 */
 typedef struct T_UA_LocalizedText

+ 2 - 2
src/UA_connection.h

@@ -84,10 +84,10 @@ struct SL_connection
 
 struct SS_connection
 {
-
+	UA_Int32 dummy;
 };
 
-typedef struct
+typedef struct T_UA_connection
 {
 	struct TL_connection transportLayer;
 	struct SL_connection secureLayer;

+ 9 - 1
src/opcua_basictypes.c

@@ -76,6 +76,14 @@ UA_Int32 UA_Array_delete(void **p,UA_Int32 noElements) {
 	retval |= UA_free(p);
 	return retval;
 }
+// FIXME: Implement
+UA_Int32 UA_Array_init(void **p,UA_Int32 noElements, UA_Int32 type) {
+	return UA_ERR_NOT_IMPLEMENTED;
+}
+// FIXME: Implement
+UA_Int32 UA_Array_new(void **p,UA_Int32 noElements, UA_Int32 type) {
+	return UA_ERR_NOT_IMPLEMENTED;
+}
 
 UA_Int32 UA_free(void * ptr){
 	printf("UA_free - ptr=%p\n",ptr);
@@ -433,7 +441,7 @@ void UA_ByteString_printx_hex(char* label, UA_ByteString* string) {
 }
 
 UA_Byte UA_Byte_securityPoliceNoneData[] = "http://opcfoundation.org/UA/SecurityPolicy#None";
-UA_String UA_String_securityPoliceNone = { sizeof(UA_Byte_securityPoliceNoneData), UA_Byte_securityPoliceNoneData };
+UA_ByteString UA_ByteString_securityPoliceNone = { sizeof(UA_Byte_securityPoliceNoneData), UA_Byte_securityPoliceNoneData };
 UA_Int32 UA_ByteString_copy(UA_ByteString const * src, UA_ByteString* dst) {
 	return UA_String_copy((UA_String const*)src,(UA_String*)dst);
 }

+ 17 - 17
src/opcua_secureLayer.c

@@ -21,7 +21,7 @@ UA_Int32 SL_initConnectionObject(UA_connection *connection)
 
 	//TODO: fill with valid information
 	UA_ByteString_init(&(connection->secureLayer.localAsymAlgSettings.ReceiverCertificateThumbprint));
-	UA_ByteString_copy(&(connection->secureLayer.localAsymAlgSettings.SecurityPolicyUri), UA_String_securityPoliceNone);
+	UA_ByteString_copy(&UA_ByteString_securityPoliceNone, &(connection->secureLayer.localAsymAlgSettings.SecurityPolicyUri));
 	UA_ByteString_init(&(connection->secureLayer.localAsymAlgSettings.SenderCertificate));
 	UA_ByteString_init(&(connection->secureLayer.remoteNonce));
 
@@ -46,7 +46,7 @@ UA_Int32 SL_initConnectionObject(UA_connection *connection)
 	return UA_NO_ERROR;
 }
 
-UA_Int32 SL_send(UA_connection *connection, UA_ByteString responseMessage, UA_Int32 type)
+UA_Int32 SL_send(UA_connection* connection, UA_ByteString* responseMessage, UA_Int32 type)
 {
 	UA_UInt32 sequenceNumber;
 	UA_UInt32 requestId;
@@ -78,7 +78,7 @@ UA_Int32 SL_send(UA_connection *connection, UA_ByteString responseMessage, UA_In
 		packetSize = SIZE_SECURECHANNEL_HEADER +
 				SIZE_SEQHEADER_HEADER +
 				sizeAsymAlgHeader +
-				responseMessage.length +
+				responseMessage->length +
 				sizePadding +
 				sizeSignature;
 
@@ -116,7 +116,7 @@ UA_Int32 SL_send(UA_connection *connection, UA_ByteString responseMessage, UA_In
 	UA_UInt32_encode(&requestId,&pos,responsePacket.data);
 
 	/*---add encoded Message ---*/
-	memcpy(&(responsePacket.data[pos]), responseMessage.data, responseMessage.length);
+	UA_memcpy(&(responsePacket.data[pos]), responseMessage->data, responseMessage->length);
 
 	/* sign Data*/
 
@@ -124,7 +124,8 @@ UA_Int32 SL_send(UA_connection *connection, UA_ByteString responseMessage, UA_In
 
 	/* send Data */
 	TL_send(connection,&responsePacket);
-
+	// do not delete here, memory still needed for actual writing  in top-level procedure
+	// UA_ByteString_deleteMembers(&responsePacket);
 
 	return UA_NO_ERROR;
 }
@@ -145,7 +146,7 @@ UA_Int32 SL_openSecureChannel(UA_connection *connection,
 	UA_ByteString serverNonce;
 	UA_NodeId responseType;
 	//sizes for memory allocation
-	UA_Int32 sizeResponse;
+	// UA_Int32 sizeResponse;
 	UA_Int32 sizeRespHeader;
 	UA_Int32 sizeResponseType;
 	UA_Int32 sizeRespMessage;
@@ -287,9 +288,8 @@ UA_Int32 SL_openSecureChannel(UA_connection *connection,
 	//FIXME: Sten: here is a problem
 
 	//449 = openSecureChannelResponse
-	SL_send(connection, response, 449);
-
-	UA_free(response.data);
+	SL_send(connection, &response, 449);
+	UA_ByteString_deleteMembers(&response);
 
 	return UA_SUCCESS;
 }
@@ -317,17 +317,17 @@ UA_Int32 SL_createSecurityToken(UA_connection *connection, UA_Int32 lifeTime) {
 }
 
 UA_Int32 SL_processMessage(UA_connection *connection, UA_ByteString message) {
-	UA_DiagnosticInfo serviceDiagnostics;
+	// UA_DiagnosticInfo serviceDiagnostics;
 
 	UA_Int32 pos = 0;
-	UA_RequestHeader requestHeader;
-	UA_UInt32 clientProtocolVersion;
+	// UA_RequestHeader requestHeader;
+	// UA_UInt32 clientProtocolVersion;
 	UA_NodeId serviceRequestType;
-	UA_Int32 requestType;
-	UA_Int32 securityMode;
-	UA_Int32 requestedLifetime;
-	UA_ByteString clientNonce;
-	UA_StatusCode serviceResult;
+	// UA_Int32 requestType;
+	// UA_Int32 securityMode;
+	// UA_Int32 requestedLifetime;
+	// UA_ByteString clientNonce;
+	// UA_StatusCode serviceResult;
 
 	// Every Message starts with a NodeID which names the serviceRequestType
 	UA_NodeId_decode(message.data, &pos,

+ 3 - 3
src/opcua_transportLayer.c

@@ -36,7 +36,7 @@ UA_Int32 TL_check(UA_connection *connection)
 	printf("TL_check - messageLength = %d \n",messageLength);
 
 	if (messageLength != -1 && messageLength == connection->readData.length &&
-			messageLength < (connection->transportLayer.localConf.maxMessageSize))
+			messageLength < (UA_Int32) connection->transportLayer.localConf.maxMessageSize)
 	{
 		printf("TL_check - no error \n");
 		return UA_NO_ERROR;
@@ -214,11 +214,11 @@ 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 TL_send(UA_connection* connection, UA_ByteString* packet)
 {
 	printf("TL_send - entered \n");
 	connection->newDataToWrite = 1;
-	if(packet->length < connection->transportLayer.remoteConf.maxMessageSize)
+	if(packet->length != -1 && packet->length < (UA_Int32) connection->transportLayer.remoteConf.maxMessageSize)
 	{
 		connection->writeData.data = packet->data;
 		connection->writeData.length = packet->length;