Explorar o código

shouldn't play with pointers while traveling

Leon Urbas %!s(int64=11) %!d(string=hai) anos
pai
achega
fabd62b788
Modificáronse 5 ficheiros con 39 adicións e 30 borrados
  1. 23 20
      src/UA_stack.c
  2. 2 1
      src/opcua_secureLayer.c
  3. 1 1
      src/opcua_secureLayer.h
  4. 9 7
      src/opcua_transportLayer.c
  5. 4 1
      src/opcua_transportLayer.h

+ 23 - 20
src/UA_stack.c

@@ -10,11 +10,14 @@
 #include <netinet/in.h>
 #include <sys/socketvar.h>
 
+#include <unistd.h> // read, write, close
 #include <errno.h> // errno, EINTR
 
+#include <memory.h> // memset
 #include <pthread.h>
 
 #include "UA_stack.h"
+#include "opcua_transportLayer.h"
 
 UA_TL_Description UA_TransportLayerDescriptorTcpBinary  = {
 		UA_TL_ENCODING_BINARY,
@@ -27,26 +30,13 @@ UA_TL_Description UA_TransportLayerDescriptorTcpBinary  = {
 UA_TL_data theTL;
 
 
-/** checks arguments and dispatches to worker or refuses to init */
-UA_Int32 UA_TL_init(UA_TL_Description* tlDesc, UA_Int32 port) {
-	UA_Int32 retval = UA_SUCCESS;
-	if (tlDesc->connectionType == UA_TL_CONNECTIONTYPE_TCPV4 && tlDesc->encoding == UA_TL_ENCODING_BINARY) {
-		theTL.tld = &tlDesc;
-		UA_list_init(&theTL.connections);
-		retval |= UA_TL_TCP_init(theTL,port);
-	} else {
-		retval = UA_ERR_NOT_IMPLEMENTED;
-	}
-	return retval;
-}
-
 
 /** the tcp reader thread **/
 void* UA_TL_TCP_reader(void *p) {
 	UA_TL_connection* c = (UA_TL_connection*) p;
 
 	UA_ByteString readBuffer;
-	UA_alloc(&(readBuffer.data),c->localConf.recvBufferSize);
+	UA_alloc((void**)&(readBuffer.data),c->localConf.recvBufferSize);
 
 	while (c->connectionState != connectionState_CLOSE) {
 		readBuffer.length = read(c->socket, readBuffer.data, c->localConf.recvBufferSize);
@@ -54,7 +44,7 @@ void* UA_TL_TCP_reader(void *p) {
 		UA_ByteString_printx("server_run - received=",&readBuffer);
 
 		if (readBuffer.length  > 0) {
-			TL_process(&c,&readBuffer);
+			TL_process(c,&readBuffer);
 		} else if (readBuffer.length < 0) {
 			perror("ERROR reading from socket1");
 			break;
@@ -104,15 +94,15 @@ void* UA_TL_TCP_listen(void *p) {
 		// accept only if not max number of connections exceeded
 		if (tld->tld->maxConnections == -1 || tld->connections.size < tld->tld->maxConnections) {
 			struct sockaddr_in cli_addr;
-			int cli_len = sizeof(cli_addr);
+			socklen_t cli_len = sizeof(cli_addr);
 			int newsockfd = accept(tld->listenerHandle, (struct sockaddr *) &cli_addr, &cli_len);
 			if (newsockfd < 0) {
 				perror("ERROR on accept");
 			} else {
 				UA_TL_connection* c;
 				UA_Int32 retval = UA_SUCCESS;
-				retval |= UA_alloc(&c,sizeof(UA_TL_connection));
-				TL_Connection_init(c, tld);
+				retval |= UA_alloc((void**)&c,sizeof(UA_TL_connection));
+				TL_Connection_init(c, tld->tld);
 				c->socket = newsockfd;
 				c->UA_TL_writer = UA_TL_TCP_write;
 				// add to list
@@ -130,7 +120,6 @@ void* UA_TL_TCP_listen(void *p) {
 UA_Int32 UA_TL_TCP_init(UA_TL_data* tld, UA_Int32 port) {
 	UA_Int32 retval = UA_SUCCESS;
 	// socket variables
-	int sockfd;
 	int optval = 1;
 	struct sockaddr_in serv_addr;
 
@@ -151,12 +140,13 @@ UA_Int32 UA_TL_TCP_init(UA_TL_data* tld, UA_Int32 port) {
 			retval = UA_ERROR;
 		} else {
 			// bind to port
-			if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
+			if (bind(tld->listenerHandle, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
 				perror("ERROR on binding");
 				retval = UA_ERROR;
 			} else {
 				// TODO: implement
 				// UA_String_sprintf("opc.tpc://localhost:%d", &(tld->endpointUrl), port);
+				UA_String_copycstring("opc.tpc://localhost:16664/", &(tld->endpointUrl));
 			}
 		}
 	}
@@ -168,6 +158,19 @@ UA_Int32 UA_TL_TCP_init(UA_TL_data* tld, UA_Int32 port) {
 	return retval;
 }
 
+/** checks arguments and dispatches to worker or refuses to init */
+UA_Int32 UA_TL_init(UA_TL_Description* tlDesc, UA_Int32 port) {
+	UA_Int32 retval = UA_SUCCESS;
+	if (tlDesc->connectionType == UA_TL_CONNECTIONTYPE_TCPV4 && tlDesc->encoding == UA_TL_ENCODING_BINARY) {
+		theTL.tld = tlDesc;
+		UA_list_init(&theTL.connections);
+		retval |= UA_TL_TCP_init(&theTL,port);
+	} else {
+		retval = UA_ERR_NOT_IMPLEMENTED;
+	}
+	return retval;
+}
+
 /** checks arguments and dispatches to worker or refuses to init */
 UA_Int32 UA_Stack_init(UA_TL_Description* tlDesc, UA_Int32 port) {
 	UA_Int32 retval = UA_SUCCESS;

+ 2 - 1
src/opcua_secureLayer.c

@@ -428,7 +428,7 @@ UA_Int32 SL_processMessage(UA_SL_Channel *sc, UA_ByteString* msg) {
 UA_SL_Channel slc;
 
 /** process data as we've got it from the transport layer */
-void SL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 messageType) {
+UA_Int32 SL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 messageType) {
 	UA_SecureConversationMessageHeader secureConvHeader;
 	UA_AsymmetricAlgorithmSecurityHeader asymAlgSecHeader;
 	UA_SequenceHeader sequenceHeader;
@@ -518,5 +518,6 @@ void SL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 messa
 //			} // check connection state
 			break;
 	}
+	return UA_SUCCESS;
 }
 

+ 1 - 1
src/opcua_secureLayer.h

@@ -27,6 +27,6 @@ UA_Int32 SL_initConnectionObject(UA_SL_Channel *connection);
 UA_Int32 SL_openSecureChannel_responseMessage_get(UA_SL_Channel *connection,
 UA_SL_Response *response, UA_Int32* sizeInOut);
 
-void SL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 messageType);
+UA_Int32 SL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 messageType);
 
 #endif /* OPCUA_SECURECHANNELLAYER_H_ */

+ 9 - 7
src/opcua_transportLayer.c

@@ -9,6 +9,7 @@
 #include "UA_connection.h"
 #include "opcua_transportLayer.h"
 
+#include "opcua_secureLayer.h" // SL_process
 
 UA_Int32 TL_Connection_init(UA_TL_connection *c, UA_TL_Description* tld)
 {
@@ -31,7 +32,7 @@ UA_Int32 TL_check(UA_TL_connection *connection, UA_ByteString* msg, int checkLoc
 
 	DBG_VERBOSE_printf("TL_check - entered \n");
 
-	UA_Int32_decode(msg,&position,&messageLength);
+	UA_Int32_decode(msg->data,&position,&messageLength);
 	DBG_VERBOSE_printf("TL_check - messageLength = %d \n",messageLength);
 
 	if (messageLength == -1 || messageLength != msg->length ||
@@ -46,9 +47,10 @@ UA_Int32 TL_check(UA_TL_connection *connection, UA_ByteString* msg, int checkLoc
 
 #define Cmp3Byte(data,pos,a,b,c) (*((Int32*) ((data)+(pos))) & 0xFFFFFF) == (Int32)(((Byte)(a))|((Byte)(b))<<8|((Byte)(c))<<16)
 
-UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 packetType, UA_Int32 *pos)
+UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString* msg)
 {
 	UA_Int32 retval = UA_SUCCESS;
+	UA_Int32 pos = 0;
 	UA_Int32 tmpPos = 0;
 	UA_ByteString tmpMessage;
 	UA_OPCUATcpMessageHeader tcpMessageHeader;
@@ -58,7 +60,7 @@ UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 p
 
 	DBG_VERBOSE_printf("TL_process - entered \n");
 
-	retval = UA_OPCUATcpMessageHeader_decode(&msg, &pos, &tcpMessageHeader);
+	retval = UA_OPCUATcpMessageHeader_decode(msg->data, &pos, &tcpMessageHeader);
 
 	if (retval == UA_SUCCESS) {
 	switch(tcpMessageHeader.messageType)
@@ -68,7 +70,7 @@ UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 p
 		{
 			DBG_VERBOSE_printf("TL_process - extracting header information \n");
 
-			UA_OPCUATcpHelloMessage_decode(&msg->data,pos,&helloMessage);
+			UA_OPCUATcpHelloMessage_decode(msg->data,&pos,&helloMessage);
 
 			/* extract information from received header */
 			connection->remoteConf.protocolVersion = helloMessage.protocolVersion;
@@ -114,7 +116,7 @@ UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 p
 			DBG_VERBOSE_printf("TL_process - Size messageToSend = %d, pos=%d\n",ackHeader.messageSize, tmpPos);
 			connection->connectionState = connectionState_OPENING;
 			TL_send(connection, &tmpMessage);
-			UA_ByteString_delete(&tmpMessage);
+			UA_ByteString_deleteMembers(&tmpMessage);
 		}
 		else
 		{
@@ -122,7 +124,7 @@ UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 p
 			retval = UA_ERROR_MULTIPLY_HEL;
 		}
 		break;
-	default:
+	default: // dispatch processing to secureLayer
 		if ((connection->connectionState != connectionState_CLOSED)) {
 			retval = SL_process(connection, msg, tcpMessageHeader.messageType);
 		} else {
@@ -145,7 +147,7 @@ UA_Int32 TL_send(UA_TL_connection* connection, UA_ByteString* msg)
 	UA_Int32 retval = UA_SUCCESS;
 	DBG_VERBOSE_printf("TL_send - entered \n");
 
-	if (TL_check(connection,msg,UA_TL_CHECK_REMOTE)) {
+	if (TL_check(connection,msg,UA_TL_CHECK_REMOTE) == UA_SUCCESS) {
 		connection->UA_TL_writer(connection,msg);
 	}
 	else

+ 4 - 1
src/opcua_transportLayer.h

@@ -10,6 +10,7 @@
 #include <stdio.h>
 
 #include "opcua.h"
+#include "UA_stack.h"
 #include "UA_connection.h"
 #include "UA_stackInternalTypes.h"
 
@@ -102,7 +103,9 @@ UA_Int32 TL_check(UA_TL_connection *connection, UA_ByteString* msg, UA_Int32 loc
 UA_Int32 TL_receive(UA_TL_connection *connection,UA_ByteString *packet);
 UA_Int32 TL_send(UA_TL_connection *connection, UA_ByteString *packet);
 UA_Int32 TL_getPacketType(UA_ByteString *packet, UA_Int32 *pos);
-UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString *packet, UA_Int32 packetType, UA_Int32 *pos);
+UA_Int32 TL_process(UA_TL_connection *connection, UA_ByteString *packet);
 
 
+UA_Int32 TL_Connection_init(UA_TL_connection *c, UA_TL_Description* tld);
+
 #endif /* OPCUA_TRANSPORTLAYER_H_ */