Browse Source

opcuaServerACPLT is working.. ongoing debugging for OpenSecureChannel and upwards

Julius Pfrommer 11 years ago
parent
commit
b41bffbcc1

+ 6 - 6
examples/src/networklayer.c

@@ -52,7 +52,7 @@ void* NL_TCP_reader(NL_Connection *c) {
 	UA_ByteString readBuffer;
 	UA_alloc((void**)&(readBuffer.data),c->connection.localConf.recvBufferSize);
 
-	if (c->connection.connectionState != connectionState_CLOSE) {
+	if (c->connection.connectionState != CONNECTIONSTATE_CLOSE) {
 		do {
 			DBG_VERBOSE(printf("NL_TCP_reader - enter read\n"));
 			readBuffer.length = read(c->connection.connectionHandle, readBuffer.data, c->connection.localConf.recvBufferSize);
@@ -64,18 +64,18 @@ void* NL_TCP_reader(NL_Connection *c) {
 			if (readBuffer.length  > 0) {
 				TL_Process(&(c->connection),&readBuffer);
 			} else {
-				c->connection.connectionState = connectionState_CLOSE;
+				c->connection.connectionState = CONNECTIONSTATE_CLOSE;
 				perror("ERROR reading from socket1");
 			}
-		} while (c->connection.connectionState != connectionState_CLOSE);
+		} while (c->connection.connectionState != CONNECTIONSTATE_CLOSE);
 	}
-	if (c->connection.connectionState == connectionState_CLOSE) {
+	if (c->connection.connectionState == CONNECTIONSTATE_CLOSE) {
 		DBG_VERBOSE(printf("NL_TCP_reader - enter shutdown\n"));
 		shutdown(c->connection.connectionHandle,2);
 		DBG_VERBOSE(printf("NL_TCP_reader - enter close\n"));
 		close(c->connection.connectionHandle);
 		DBG_VERBOSE(printf("NL_TCP_reader - leave close\n"));
-		c->connection.connectionState = connectionState_CLOSED;
+		c->connection.connectionState = CONNECTIONSTATE_CLOSED;
 
 		UA_ByteString_deleteMembers(&readBuffer);
 		DBG_VERBOSE(printf("NL_TCP_reader - search element to remove\n"));
@@ -131,7 +131,7 @@ void* NL_Connection_init(NL_Connection* c, NL_data* tld, UA_Int32 connectionHand
 {
 	// connection layer of UA stack
 	c->connection.connectionHandle = connectionHandle;
-	c->connection.connectionState = connectionState_CLOSED;
+	c->connection.connectionState = CONNECTIONSTATE_CLOSED;
 	c->connection.writerCallback = writer;
 	memcpy(&(c->connection.localConf),&(tld->tld->localConf),sizeof(TL_Buffer));
 	memset(&(c->connection.remoteConf),0,sizeof(TL_Buffer));

+ 14 - 19
examples/src/opcuaServerACPLT.c

@@ -51,24 +51,23 @@ UA_Int32 server_writer(TL_Connection* connection, UA_ByteString** gather_buf, UA
 	for(UA_UInt32 i=0;i<gather_len;i++) {
 		total_len += gather_buf[i]->length;
 	}
-    UA_ByteString *msg;
-	UA_alloc((void **)&msg, sizeof(UA_ByteString));
-	UA_ByteString_newMembers(msg, total_len);
+    UA_ByteString msg;
+	UA_ByteString_newMembers(&msg, total_len);
 
 	UA_UInt32 pos = 0;
 	for(UA_UInt32 i=0;i<gather_len;i++) {
-		memcpy(msg->data+pos, gather_buf[i]->data, gather_buf[i]->length);
+		memcpy(msg.data+pos, gather_buf[i]->data, gather_buf[i]->length);
 		pos += gather_buf[i]->length;
 	}
-	server.writeData = *msg;
+	server.writeData.data = msg.data;
+	server.writeData.length = msg.length;
 	server.newDataToWrite = 1;
-	UA_free(msg);
 	return UA_SUCCESS;
 }
 
 void server_run() {
 	TL_Connection connection;
-	connection.connectionState = connectionState_CLOSE;
+	connection.connectionState = CONNECTIONSTATE_CLOSED;
 	connection.writerCallback = (TL_Writer)server_writer;
 	connection.localConf.maxChunkCount = 1;
 	connection.localConf.maxMessageSize = BUFFER_SIZE;
@@ -121,39 +120,35 @@ void server_run() {
 			exit(1);
 		}
 
-		printf("connection accepted\n");
+		printf("connection accepted: %i, state: %i\n", newsockfd, connection.connectionState);
 		/* communication loop */
-		while (connection.connectionState != connectionState_CLOSE) {
+		int i = 0;
+		do {
 			/* If connection is established then start communicating */
             memset(buffer, 0, BUFFER_SIZE);
 
 			n = read(newsockfd, buffer, BUFFER_SIZE);
-
 			if (n > 0) {
                 slMessage.data = (UA_Byte*) buffer;
 				slMessage.length = n;
 				UA_ByteString_printx("server_run - received=",&slMessage);
 				TL_Process(&connection, &slMessage);
-			} else if (n < 0) {
+			} else if (n <= 0) {
 				perror("ERROR reading from socket1");
 				exit(1);
 			}
-
 			if (server.newDataToWrite) {
 				UA_ByteString_printx("Send data:", &server.writeData);
-				n = write(newsockfd, server.writeData.data,
-						server.writeData.length);
+				n = write(newsockfd, server.writeData.data, server.writeData.length);
 				printf("written %d bytes \n", n);
 				server.newDataToWrite = 0;
 				UA_ByteString_deleteMembers(&server.writeData);
-
-				server.writeData.data = UA_NULL;
-				server.writeData.length = 0;
 			}
-		}
+			i++;
+		} while(connection.connectionState != CONNECTIONSTATE_CLOSE);
 		shutdown(newsockfd,2);
 		close(newsockfd);
-		connection.connectionState = connectionState_CLOSED;
+		connection.connectionState = CONNECTIONSTATE_CLOSED;
 	}
 	shutdown(sockfd,2);
 	close(sockfd);

+ 4 - 4
src/ua_services_securechannel.c

@@ -9,7 +9,7 @@ UA_Int32 Service_OpenSecureChannel(SL_Channel *channel, const UA_OpenSecureChann
 	UA_UInt32 retval = UA_SUCCESS;
 	switch (request->requestType) {
 	case UA_SECURITYTOKEN_ISSUE:
-		if (channel->connectionState == connectionState_ESTABLISHED) {
+		if (channel->connectionState == CONNECTIONSTATE_ESTABLISHED) {
 			printf("SL_processMessage - multiple security token request");
 			//TODO return ERROR
 			retval = UA_ERROR;
@@ -19,7 +19,7 @@ UA_Int32 Service_OpenSecureChannel(SL_Channel *channel, const UA_OpenSecureChann
 		//	SL_createNewToken(connection);
 		break;
 	case UA_SECURITYTOKEN_RENEW:
-		if (channel->connectionState == connectionState_CLOSED) {
+		if (channel->connectionState == CONNECTIONSTATE_CLOSED) {
 			printf("SL_processMessage - renew token request received, but no secureChannel was established before");
 			//TODO return ERROR
 			retval = UA_ERROR;
@@ -47,7 +47,7 @@ UA_Int32 Service_OpenSecureChannel(SL_Channel *channel, const UA_OpenSecureChann
 		break;
 	}
 
-	channel->connectionState = connectionState_ESTABLISHED;
+	channel->connectionState = CONNECTIONSTATE_ESTABLISHED;
 
 	if (request->requestHeader.returnDiagnostics != 0) {
 		printf("SL_openSecureChannel - diagnostics demanded by the client\n");
@@ -69,6 +69,6 @@ UA_Int32 Service_OpenSecureChannel(SL_Channel *channel, const UA_OpenSecureChann
 
 UA_Int32 Service_CloseSecureChannel(SL_Channel *channel, const UA_CloseSecureChannelRequest *request, UA_CloseSecureChannelResponse *response) {
 	// 62451 Part 6 Chapter 7.1.4 - The server does not send a CloseSecureChannel response
-	channel->connectionState = connectionState_CLOSE;
+	channel->connectionState = CONNECTIONSTATE_CLOSE;
 	return UA_SUCCESS;
 }

+ 5 - 5
src/ua_transport.h

@@ -6,11 +6,11 @@
 
 static const UA_Int32 SL_HEADER_LENGTH = 0;
 
-enum connectionState {
-	connectionState_CLOSED,
-	connectionState_OPENING,
-	connectionState_ESTABLISHED,
-	connectionState_CLOSE,
+enum ConnectionState {
+	CONNECTIONSTATE_CLOSED,
+	CONNECTIONSTATE_OPENING,
+	CONNECTIONSTATE_ESTABLISHED,
+	CONNECTIONSTATE_CLOSE
 };
 
 typedef struct Session_T {

+ 20 - 18
src/ua_transport_binary.c

@@ -13,15 +13,12 @@ static UA_Int32 TL_check(TL_Connection* connection, UA_ByteString* msg) {
 
 static UA_Int32 TL_handleHello(TL_Connection* connection, UA_ByteString* msg, UA_Int32* pos) {
 	UA_Int32 retval = UA_SUCCESS;
-
 	UA_Int32 tmpPos = 0;
-	UA_ByteString tmpMessage;
-	UA_ByteString *tmpMessage_ptr = &tmpMessage;
 	UA_OPCUATcpHelloMessage helloMessage;
-	UA_OPCUATcpAcknowledgeMessage ackMessage;
-	UA_OPCUATcpMessageHeader ackHeader;
 
-	if (connection->connectionState == connectionState_CLOSED) {
+	printf("\nstate: %i", connection->connectionState);
+	printf("\nwanted state: %i", CONNECTIONSTATE_CLOSED);
+	if (connection->connectionState == CONNECTIONSTATE_CLOSED) {
 		DBG_VERBOSE(printf("TL_process - extracting header information \n"));
 		UA_OPCUATcpHelloMessage_decodeBinary(msg,pos,&helloMessage);
 
@@ -32,8 +29,6 @@ static UA_Int32 TL_handleHello(TL_Connection* connection, UA_ByteString* msg, UA
 		connection->remoteConf.maxMessageSize = helloMessage.maxMessageSize;
 		connection->remoteConf.maxChunkCount = helloMessage.maxChunkCount;
 		UA_String_copy(&(helloMessage.endpointUrl), &(connection->remoteEndpointUrl));
-		connection->connectionState = connectionState_ESTABLISHED;
-		// clean up
 		UA_OPCUATcpHelloMessage_deleteMembers(&helloMessage);
 
 		DBG_VERBOSE(printf("TL_process - protocolVersion = %d \n",connection->remoteConf.protocolVersion));
@@ -41,27 +36,34 @@ static UA_Int32 TL_handleHello(TL_Connection* connection, UA_ByteString* msg, UA
 		DBG_VERBOSE(printf("TL_process - sendBufferSize = %d \n",connection->remoteConf.sendBufferSize));
 		DBG_VERBOSE(printf("TL_process - maxMessageSize = %d \n",connection->remoteConf.maxMessageSize));
 		DBG_VERBOSE(printf("TL_process - maxChunkCount = %d \n",connection->remoteConf.maxChunkCount));
+		connection->connectionState = CONNECTIONSTATE_ESTABLISHED;
 
 		// build acknowledge response
+		UA_OPCUATcpAcknowledgeMessage ackMessage;
 		ackMessage.protocolVersion = connection->localConf.protocolVersion;
 		ackMessage.receiveBufferSize = connection->localConf.recvBufferSize;
 		ackMessage.sendBufferSize = connection->localConf.sendBufferSize;
 		ackMessage.maxMessageSize = connection->localConf.maxMessageSize;
 		ackMessage.maxChunkCount = connection->localConf.maxChunkCount;
 
+		UA_OPCUATcpMessageHeader ackHeader;
 		ackHeader.messageType = UA_MESSAGETYPE_ACK;
 		ackHeader.isFinal = 'F';
 
 		// encode header and message to buffer
-		ackHeader.messageSize = UA_OPCUATcpAcknowledgeMessage_calcSize(&ackMessage)
-		+ UA_OPCUATcpMessageHeader_calcSize(&ackHeader);
-		UA_ByteString_newMembers(&tmpMessage, ackHeader.messageSize);
-		UA_OPCUATcpMessageHeader_encodeBinary(&ackHeader,&tmpPos,&tmpMessage);
-		UA_OPCUATcpAcknowledgeMessage_encodeBinary(&ackMessage,&tmpPos,&tmpMessage);
+		tmpPos = 0;
+		ackHeader.messageSize = UA_OPCUATcpAcknowledgeMessage_calcSize(&ackMessage) + UA_OPCUATcpMessageHeader_calcSize(&ackHeader);
+		UA_ByteString *ack_msg;
+		UA_alloc((void **)&ack_msg, sizeof(UA_ByteString));
+		UA_ByteString_newMembers(ack_msg, ackHeader.messageSize);
+		UA_OPCUATcpMessageHeader_encodeBinary(&ackHeader,&tmpPos,ack_msg);
+		UA_OPCUATcpAcknowledgeMessage_encodeBinary(&ackMessage,&tmpPos,ack_msg);
 
 		DBG_VERBOSE(printf("TL_process - Size messageToSend = %d, pos=%d\n",ackHeader.messageSize, tmpPos));
-		TL_Send(connection, &tmpMessage_ptr, 1);
-		UA_ByteString_deleteMembers(&tmpMessage);
+		UA_ByteString_printx("ack: ", ack_msg);
+		TL_Send(connection, &ack_msg, 1);
+		printf("finished wiritng");
+		UA_ByteString_delete(ack_msg);
 	} else {
 		DBG_ERR(printf("TL_process - wrong connection state \n"));
 		retval = UA_ERROR_MULTIPLE_HEL;
@@ -70,7 +72,7 @@ static UA_Int32 TL_handleHello(TL_Connection* connection, UA_ByteString* msg, UA
 }
 
 static UA_Int32 TL_handleOpen(TL_Connection* connection, UA_ByteString* msg, UA_Int32* pos) {
-	if (connection->connectionState == connectionState_ESTABLISHED) {
+	if (connection->connectionState == CONNECTIONSTATE_ESTABLISHED) {
 		return SL_Channel_new(connection,msg,pos); // create new secure channel and associate with this TL connection
 	}
 	return UA_ERR_INVALID_VALUE;
@@ -83,7 +85,7 @@ static UA_Int32 TL_handleMsg(TL_Connection* connection, UA_ByteString* msg, UA_I
 
 static UA_Int32 TL_handleClo(TL_Connection* connection, UA_ByteString* msg, UA_Int32* pos) {
 	SL_Channel* slc = connection->secureChannel;
-	connection->connectionState = connectionState_CLOSE;
+	connection->connectionState = CONNECTIONSTATE_CLOSE;
 	return SL_process(slc,msg,pos);
 }
 
@@ -130,9 +132,9 @@ UA_Int32 TL_Process(TL_Connection* connection, UA_ByteString* msg) {
 UA_Int32 TL_Send(TL_Connection* connection, UA_ByteString** gather_buf, UA_UInt32 gather_len) {
 	UA_Int32 retval = UA_SUCCESS;
 	DBG_VERBOSE(printf("TL_send - entered \n"));
-
 	//	if (TL_check(connection,msg,TL_CHECK_REMOTE) == UA_SUCCESS) {
 	retval = connection->writerCallback(connection, gather_buf, gather_len);
+	DBG_VERBOSE(printf("TL_send - exited \n"));
 		//}
 	/* else */
 	/* { */

+ 2 - 2
src/ua_transport_binary_secure.c

@@ -150,7 +150,7 @@ UA_Int32 SL_Channel_init(SL_Channel *channel) {
 	UA_alloc((void**)&(channel->localNonce.data), sizeof(UA_Byte));
 	channel->localNonce.length = 1;
 
-	channel->connectionState = connectionState_CLOSED;
+	channel->connectionState = CONNECTIONSTATE_CLOSED;
 
 	channel->sequenceHeader.requestId = 0;
 	channel->sequenceHeader.sequenceNumber = 1;
@@ -223,7 +223,7 @@ UA_Int32 SL_process(SL_Channel* connection, UA_ByteString* msg, UA_Int32* pos) {
 	DBG_VERBOSE(printf("SL_process - entered \n"));
 	UA_UInt32 secureChannelId;
 
-	if (connection->connectionState == connectionState_ESTABLISHED) {
+	if (connection->connectionState == CONNECTIONSTATE_ESTABLISHED) {
 		UA_UInt32_decodeBinary(msg,pos,&secureChannelId);
 
 		//FIXME: we assume SAS, need to check if AAS or SAS