123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include "ua_util.h"
- #include "ua_connection.h"
- #include "ua_types_encoding_binary.h"
- #include "ua_types_generated_encoding_binary.h"
- #include "ua_securechannel.h"
- const UA_ConnectionConfig UA_ConnectionConfig_standard =
- {.protocolVersion = 0, .sendBufferSize = 65536, .recvBufferSize = 65536,
- .maxMessageSize = 65536, .maxChunkCount = 1};
- void UA_Connection_init(UA_Connection *connection) {
- connection->state = UA_CONNECTION_CLOSED;
- connection->localConf = UA_ConnectionConfig_standard;
- connection->remoteConf = UA_ConnectionConfig_standard;
- connection->channel = NULL;
- connection->sockfd = 0;
- connection->handle = NULL;
- UA_ByteString_init(&connection->incompleteMessage);
- connection->send = NULL;
- connection->close = NULL;
- connection->recv = NULL;
- connection->getSendBuffer = NULL;
- connection->releaseSendBuffer = NULL;
- connection->releaseRecvBuffer = NULL;
- }
- void UA_Connection_deleteMembers(UA_Connection *connection) {
- UA_ByteString_deleteMembers(&connection->incompleteMessage);
- }
- UA_StatusCode
- UA_Connection_completeMessages(UA_Connection *connection, UA_ByteString * UA_RESTRICT message,
- UA_Boolean * UA_RESTRICT realloced) {
- UA_ByteString *current = message;
- *realloced = UA_FALSE;
- if(connection->incompleteMessage.length > 0) {
-
- UA_Byte *data = UA_realloc(connection->incompleteMessage.data,
- connection->incompleteMessage.length + message->length);
- if(!data) {
-
- UA_ByteString_deleteMembers(&connection->incompleteMessage);
- connection->releaseRecvBuffer(connection, message);
- return UA_STATUSCODE_BADOUTOFMEMORY;
- }
- memcpy(&data[connection->incompleteMessage.length], message->data, message->length);
- connection->incompleteMessage.data = data;
- connection->incompleteMessage.length += message->length;
- connection->releaseRecvBuffer(connection, message);
- current = &connection->incompleteMessage;
- *realloced = UA_TRUE;
- }
-
- size_t pos = 0;
- size_t delete_at = current->length-1;
- while(current->length - pos >= 16) {
- UA_UInt32 msgtype = current->data[pos] + (current->data[pos+1] << 8) + (current->data[pos+2] << 16);
- if(msgtype != ('M' + ('S' << 8) + ('G' << 16)) &&
- msgtype != ('O' + ('P' << 8) + ('N' << 16)) &&
- msgtype != ('H' + ('E' << 8) + ('L' << 16)) &&
- msgtype != ('A' + ('C' << 8) + ('K' << 16)) &&
- msgtype != ('C' + ('L' << 8) + ('O' << 16))) {
-
- delete_at = pos;
- break;
- }
- UA_Int32 length = 0;
- size_t length_pos = pos + 4;
- UA_StatusCode retval = UA_Int32_decodeBinary(current, &length_pos, &length);
- if(retval != UA_STATUSCODE_GOOD || length < 16 || length > (UA_Int32)connection->localConf.maxMessageSize) {
-
- delete_at = pos;
- break;
- }
- if(length + pos > current->length)
- break;
- pos += length;
- }
-
- if(delete_at == 0) {
- if(!*realloced) {
- connection->releaseRecvBuffer(connection, message);
- *realloced = UA_TRUE;
- } else
- UA_ByteString_deleteMembers(current);
- return UA_STATUSCODE_GOOD;
- }
-
- if(pos == 0) {
- if(!*realloced) {
-
- UA_ByteString_copy(current, &connection->incompleteMessage);
- connection->releaseRecvBuffer(connection, message);
- *realloced = UA_TRUE;
- }
- return UA_STATUSCODE_GOOD;
- }
-
- if(current->length != pos) {
- UA_Byte *data = UA_malloc(current->length - pos);
- if(!data) {
- UA_ByteString_deleteMembers(&connection->incompleteMessage);
- if(!*realloced) {
- connection->releaseRecvBuffer(connection, message);
- *realloced = UA_TRUE;
- }
- return UA_STATUSCODE_BADOUTOFMEMORY;
- }
- size_t newlength = current->length - pos;
- memcpy(data, ¤t->data[pos], newlength);
- current->length = pos;
- if(*realloced)
- *message = *current;
- connection->incompleteMessage.data = data;
- connection->incompleteMessage.length = newlength;
- return UA_STATUSCODE_GOOD;
- }
- if(current == &connection->incompleteMessage) {
- *message = *current;
- connection->incompleteMessage = UA_BYTESTRING_NULL;
- }
- return UA_STATUSCODE_GOOD;
- }
- void UA_Connection_detachSecureChannel(UA_Connection *connection) {
- #ifdef UA_MULTITHREADING
- UA_SecureChannel *channel = connection->channel;
- if(channel)
- uatomic_cmpxchg(&channel->connection, connection, NULL);
- uatomic_set(&connection->channel, NULL);
- #else
- if(connection->channel)
- connection->channel->connection = NULL;
- connection->channel = NULL;
- #endif
- }
- void UA_Connection_attachSecureChannel(UA_Connection *connection, UA_SecureChannel *channel) {
- #ifdef UA_MULTITHREADING
- if(uatomic_cmpxchg(&channel->connection, NULL, connection) == NULL)
- uatomic_set(&connection->channel, channel);
- #else
- if(channel->connection != NULL)
- return;
- channel->connection = connection;
- connection->channel = channel;
- #endif
- }
|