UA_Int32 UA_OPCUATcpAcknowledgeMessage_encodeBinary(UA_OPCUATcpAcknowledgeMessage const * src, UA_Int32* pos, UA_ByteString* dst) { UA_Int32 retval = UA_SUCCESS; retval |= UA_UInt32_encodeBinary(&(src->protocolVersion),pos,dst); retval |= UA_UInt32_encodeBinary(&(src->receiveBufferSize),pos,dst); retval |= UA_UInt32_encodeBinary(&(src->sendBufferSize),pos,dst); retval |= UA_UInt32_encodeBinary(&(src->maxMessageSize),pos,dst); retval |= UA_UInt32_encodeBinary(&(src->maxChunkCount),pos,dst); return retval; }
static UA_Int64 sendReadRequest(ConnectionInfo *connectionInfo, UA_Int32 nodeIds_size,UA_NodeId* nodeIds){ /*UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId, UA_NodeId authenticationToken, UA_Int32 nodeIds_size,UA_NodeId* nodeIds) { */ UA_ByteString *message = UA_ByteString_new(); UA_ByteString_newMembers(message, 65536); UA_UInt32 tmpChannelId = connectionInfo->channelId; size_t offset = 0; UA_TcpMessageHeader msghdr; msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF; UA_NodeId type; type.identifier.numeric = 631; type.identifierType = UA_NODEIDTYPE_NUMERIC; type.namespaceIndex = 0; UA_ReadRequest rq; UA_ReadRequest_init(&rq); rq.maxAge = 0; rq.nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size); rq.nodesToReadSize = 1; for(UA_Int32 i=0;i<nodeIds_size;i++) { UA_ReadValueId_init(&(rq.nodesToRead[i])); rq.nodesToRead[i].attributeId = 6; //WriteMask UA_NodeId_init(&(rq.nodesToRead[i].nodeId)); rq.nodesToRead[i].nodeId = nodeIds[i]; UA_QualifiedName_init(&(rq.nodesToRead[0].dataEncoding)); } rq.requestHeader.timeoutHint = 10000; rq.requestHeader.timestamp = UA_DateTime_now(); rq.requestHeader.authenticationToken = connectionInfo->authenticationToken; rq.timestampsToReturn = 0x03; rq.requestHeader.requestHandle = 1 + connectionInfo->sequenceHdr.requestId; msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) + UA_ReadRequest_calcSizeBinary(&rq); UA_TcpMessageHeader_encodeBinary(&msghdr,message,&offset); UA_UInt32_encodeBinary(&tmpChannelId, message, &offset); UA_UInt32_encodeBinary(&connectionInfo->tokenId, message, &offset); UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.sequenceNumber, message, &offset); UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.requestId, message, &offset); UA_NodeId_encodeBinary(&type,message,&offset); UA_ReadRequest_encodeBinary(&rq, message, &offset); UA_DateTime tic = UA_DateTime_now(); UA_Int32 sendret = send(connectionInfo->socket, message->data, offset, 0); UA_Array_delete(rq.nodesToRead, &UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size); UA_ByteString_delete(message); if (sendret < 0) { printf("send readrequest failed"); return 1; } return tic; }
static UA_Int32 sendCreateSession(UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId, UA_String *endpointUrl) { UA_ByteString message; UA_ByteString_newMembers(&message, 65536); UA_UInt32 tmpChannelId = channelId; size_t offset = 0; UA_TcpMessageHeader msghdr; msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF; UA_NodeId type; type.identifier.numeric = 461; type.identifierType = UA_NODEIDTYPE_NUMERIC; type.namespaceIndex = 0; UA_CreateSessionRequest rq; UA_CreateSessionRequest_init(&rq); rq.requestHeader.requestHandle = 1; rq.requestHeader.timestamp = UA_DateTime_now(); rq.requestHeader.timeoutHint = 10000; rq.requestHeader.authenticationToken.identifier.numeric = 10; rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC; rq.requestHeader.authenticationToken.namespaceIndex = 10; UA_String_copy(endpointUrl, &rq.endpointUrl); UA_String_copycstring("mysession", &rq.sessionName); UA_String_copycstring("abcd", &rq.clientCertificate); UA_ByteString_newMembers(&rq.clientNonce, 1); rq.clientNonce.data[0] = 0; rq.requestedSessionTimeout = 1200000; rq.maxResponseMessageSize = UA_INT32_MAX; msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) + UA_CreateSessionRequest_calcSizeBinary(&rq); UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset); UA_UInt32_encodeBinary(&tmpChannelId, &message, &offset); UA_UInt32_encodeBinary(&tokenId, &message, &offset); UA_UInt32_encodeBinary(&sequenceNumber, &message, &offset); UA_UInt32_encodeBinary(&requestId, &message, &offset); UA_NodeId_encodeBinary(&type, &message, &offset); UA_CreateSessionRequest_encodeBinary(&rq, &message, &offset); UA_Int32 sendret = send(sock, message.data, offset, 0); UA_ByteString_deleteMembers(&message); UA_CreateSessionRequest_deleteMembers(&rq); if (sendret < 0) { printf("send opensecurechannel failed"); return 1; } return 0; }
UA_Int32 UA_OPCUATcpMessageHeader_encodeBinary(UA_OPCUATcpMessageHeader const * src, UA_Int32* pos, UA_ByteString* dst) { UA_Int32 retval = UA_SUCCESS; retval |= UA_MessageType_encodeBinary(&(src->messageType),pos,dst); retval |= UA_Byte_encodeBinary(&(src->isFinal),pos,dst); retval |= UA_UInt32_encodeBinary(&(src->messageSize),pos,dst); return retval; }
static UA_Int32 closeSession(ConnectionInfo *connectionInfo) { size_t offset = 0; UA_ByteString message; UA_ByteString_newMembers(&message, 65536); UA_CloseSessionRequest rq; UA_CloseSessionRequest_init(&rq); rq.requestHeader.requestHandle = 1; rq.requestHeader.timestamp = UA_DateTime_now(); rq.requestHeader.timeoutHint = 10000; rq.requestHeader.authenticationToken.identifier.numeric = 10; rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC; rq.requestHeader.authenticationToken.namespaceIndex = 10; rq.deleteSubscriptions = UA_TRUE; UA_TcpMessageHeader msghdr; msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF; UA_NodeId type; type.identifier.numeric = 473; type.identifierType = UA_NODEIDTYPE_NUMERIC; type.namespaceIndex = 0; msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) + UA_CloseSessionRequest_calcSizeBinary(&rq); UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset); UA_UInt32_encodeBinary(&connectionInfo->channelId, &message, &offset); UA_UInt32_encodeBinary(&connectionInfo->tokenId, &message, &offset); UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.sequenceNumber, &message, &offset); UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.requestId, &message, &offset); UA_NodeId_encodeBinary(&type, &message, &offset); UA_CloseSessionRequest_encodeBinary(&rq, &message, &offset); UA_Int32 sendret = send(connectionInfo->socket, message.data, offset, 0); UA_ByteString_deleteMembers(&message); UA_CloseSessionRequest_deleteMembers(&rq); if(sendret < 0) { printf("send closesessionrequest failed"); return 1; } return 0; }
static UA_Int32 sendActivateSession(UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId, UA_NodeId authenticationToken) { UA_ByteString *message = UA_ByteString_new(); UA_ByteString_newMembers(message, 65536); UA_UInt32 tmpChannelId = channelId; size_t offset = 0; UA_TcpMessageHeader msghdr; msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF; msghdr.messageSize = 86; UA_NodeId type; type.identifier.numeric = 467; type.identifierType = UA_NODEIDTYPE_NUMERIC; type.namespaceIndex = 0; UA_ActivateSessionRequest rq; UA_ActivateSessionRequest_init(&rq); rq.requestHeader.requestHandle = 2; rq.requestHeader.authenticationToken = authenticationToken; rq.requestHeader.timestamp = UA_DateTime_now(); rq.requestHeader.timeoutHint = 10000; msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) + UA_ActivateSessionRequest_calcSizeBinary(&rq); UA_TcpMessageHeader_encodeBinary(&msghdr, message, &offset); UA_UInt32_encodeBinary(&tmpChannelId, message, &offset); UA_UInt32_encodeBinary(&tokenId, message, &offset); UA_UInt32_encodeBinary(&sequenceNumber, message, &offset); UA_UInt32_encodeBinary(&requestId, message, &offset); UA_NodeId_encodeBinary(&type, message, &offset); UA_ActivateSessionRequest_encodeBinary(&rq, message, &offset); UA_Int32 sendret = send(sock, message->data, offset, 0); UA_ByteString_delete(message); if (sendret < 0) { printf("send opensecurechannel failed"); return 1; } return 0; }
static UA_Int32 closeSecureChannel(ConnectionInfo *connectionInfo) { size_t offset = 0; UA_ByteString message; UA_ByteString_newMembers(&message, 65536); UA_CloseSecureChannelRequest rq; UA_CloseSecureChannelRequest_init(&rq); rq.requestHeader.requestHandle = 1; rq.requestHeader.timestamp = UA_DateTime_now(); rq.requestHeader.timeoutHint = 10000; rq.requestHeader.authenticationToken.identifier.numeric = 10; rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC; rq.requestHeader.authenticationToken.namespaceIndex = 10; UA_TcpMessageHeader msghdr; msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_CLOF; msghdr.messageSize = 4 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_CloseSecureChannelRequest_calcSizeBinary(&rq); UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset); UA_UInt32_encodeBinary(&connectionInfo->channelId, &message, &offset); UA_CloseSecureChannelRequest_encodeBinary(&rq, &message, &offset); UA_Int32 sendret = send(connectionInfo->socket, message.data, offset, 0); UA_ByteString_deleteMembers(&message); UA_CloseSecureChannelRequest_deleteMembers(&rq); if(sendret < 0) { printf("send CloseSecureChannelRequest failed"); return 1; } return 0; }
static int sendOpenSecureChannel(UA_Int32 sock) { UA_TcpMessageHeader msghdr; msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_OPNF; UA_UInt32 secureChannelId = 0; UA_String securityPolicy; UA_String_copycstring("http://opcfoundation.org/UA/SecurityPolicy#None", &securityPolicy); UA_String senderCert; senderCert.data = UA_NULL; senderCert.length = -1; UA_String receiverCertThumb; receiverCertThumb.data = UA_NULL; receiverCertThumb.length = -1; UA_UInt32 sequenceNumber = 51; UA_UInt32 requestId = 1; UA_NodeId type; type.identifier.numeric = 446; // id of opensecurechannelrequest type.identifierType = UA_NODEIDTYPE_NUMERIC; type.namespaceIndex = 0; UA_OpenSecureChannelRequest opnSecRq; UA_OpenSecureChannelRequest_init(&opnSecRq); opnSecRq.requestHeader.timestamp = UA_DateTime_now(); UA_ByteString_newMembers(&opnSecRq.clientNonce, 1); opnSecRq.clientNonce.data[0] = 0; opnSecRq.clientProtocolVersion = 0; opnSecRq.requestedLifetime = 30000; opnSecRq.securityMode = UA_MESSAGESECURITYMODE_NONE; opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_ISSUE; opnSecRq.requestHeader.authenticationToken.identifier.numeric = 10; opnSecRq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC; opnSecRq.requestHeader.authenticationToken.namespaceIndex = 10; msghdr.messageSize = 135; // todo: compute the message size from the actual content UA_ByteString message; UA_ByteString_newMembers(&message, 1000); size_t offset = 0; UA_TcpMessageHeader_encodeBinary(&msghdr, &message, &offset); UA_UInt32_encodeBinary(&secureChannelId, &message, &offset); UA_String_encodeBinary(&securityPolicy, &message, &offset); UA_String_encodeBinary(&senderCert, &message, &offset); UA_String_encodeBinary(&receiverCertThumb, &message, &offset); UA_UInt32_encodeBinary(&sequenceNumber, &message, &offset); UA_UInt32_encodeBinary(&requestId, &message, &offset); UA_NodeId_encodeBinary(&type, &message, &offset); UA_OpenSecureChannelRequest_encodeBinary(&opnSecRq, &message, &offset); UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq); UA_String_deleteMembers(&securityPolicy); UA_Int32 sendret = send(sock, message.data, offset, 0); UA_ByteString_deleteMembers(&message); if (sendret < 0) { printf("send opensecurechannel failed"); return 1; } return 0; }
int main(int argc , char *argv[]) { int sock; struct sockaddr_in server; UA_ByteString message; message.data = (UA_Byte*)malloc(1000*sizeof(UA_Byte)); message.length = 1000; //UA_UInt32 messageEncodedLength = 0; UA_Byte server_reply[2000]; unsigned int messagepos = 0; //Create socket #ifdef UA_ENABLE_NONSTANDARD_UDP sock = socket(AF_INET , SOCK_DGRAM , 0); #else sock = socket(AF_INET , SOCK_STREAM , 0); #endif if(sock == -1) { printf("Could not create socket"); } server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = AF_INET; server.sin_port = htons( 16664 ); //Connect to remote server if(connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { perror("connect failed. Error"); return 1; } UA_TcpMessageHeader reqTcpHeader; UA_UInt32 reqSecureChannelId = 0; UA_UInt32 reqTokenId = 0; UA_SequenceHeader reqSequenceHeader; UA_NodeId reqRequestType; UA_ReadRequest req; UA_RequestHeader reqHeader; UA_NodeId reqHeaderAuthToken; UA_ExtensionObject reqHeaderAdditionalHeader; UA_NodeId_init(&reqRequestType); reqRequestType.identifierType = UA_NODEIDTYPE_NUMERIC; reqRequestType.identifier.numeric = 631; //read request UA_SequenceHeader_init(&reqSequenceHeader); reqSequenceHeader.sequenceNumber = 42; UA_ReadRequest_init(&req); req.requestHeader = reqHeader; UA_RequestHeader_init(&(req.requestHeader)); req.requestHeader.authenticationToken = reqHeaderAuthToken; UA_NodeId_init(&(req.requestHeader.authenticationToken)); req.requestHeader.additionalHeader = reqHeaderAdditionalHeader; UA_ExtensionObject_init(&(req.requestHeader.additionalHeader)); req.nodesToRead= UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], 1); req.nodesToReadSize = 1; UA_ReadValueId_init(&(req.nodesToRead[0])); req.nodesToRead[0].attributeId = 13; //UA_ATTRIBUTEID_VALUE UA_NodeId_init(&(req.nodesToRead[0].nodeId)); req.nodesToRead[0].nodeId.identifierType = UA_NODEIDTYPE_NUMERIC; req.nodesToRead[0].nodeId.identifier.numeric = 2255; UA_QualifiedName_init(&(req.nodesToRead[0].dataEncoding)); /**messageEncodedLength = UA_TcpMessageHeader_calcSizeBinary(&reqTcpHeader) + UA_UInt32_calcSizeBinary(&reqSecureChannelId)+ UA_UInt32_calcSizeBinary(&reqTokenId)+ UA_SequenceHeader_calcSizeBinary(&reqSequenceHeader)+ UA_NodeId_calcSizeBinary(&reqRequestType) + UA_ReadRequest_calcSizeBinary(&req);**/ UA_TcpMessageHeader_init(&reqTcpHeader); reqTcpHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF; UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos); UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos); UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos); UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos); UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos); UA_ReadRequest_encodeBinary(&req, &message, &messagepos); reqTcpHeader.messageSize = messagepos; messagepos=0; UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos); UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos); UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos); UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos); UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos); UA_ReadRequest_encodeBinary(&req, &message, &messagepos); //Send some data if(send(sock , message.data, messagepos , 0) < 0) { puts("Send failed"); return 1; } //Receive a reply from the server int received = recv(sock , server_reply , 2000 , 0); if(received < 0) { puts("recv failed"); return 1; } for(int i=0;i<received;i++) { //show only printable ascii if(server_reply[i] >= 32 && server_reply[i]<= 126) printf("%c",server_reply[i]); } printf("\n"); close(sock); return 0; }
UA_Int32 UA_SecureConversationMessageAbortBody_encodeBinary(UA_SecureConversationMessageAbortBody const * src, UA_Int32* pos, UA_ByteString* dst) { UA_Int32 retval = UA_SUCCESS; retval |= UA_UInt32_encodeBinary(&(src->error),pos,dst); retval |= UA_String_encodeBinary(&(src->reason),pos,dst); return retval; }
UA_Int32 UA_SequenceHeader_encodeBinary(UA_SequenceHeader const * src, UA_Int32* pos, UA_ByteString* dst) { UA_Int32 retval = UA_SUCCESS; retval |= UA_UInt32_encodeBinary(&(src->sequenceNumber),pos,dst); retval |= UA_UInt32_encodeBinary(&(src->requestId),pos,dst); return retval; }
UA_Int32 UA_SecureConversationMessageHeader_encodeBinary(UA_SecureConversationMessageHeader const * src, UA_Int32* pos, UA_ByteString* dst) { UA_Int32 retval = UA_SUCCESS; // retval |= UA_OPCUATcpMessageHeader_encode(src->tcpMessageHeader,pos,dst); retval |= UA_UInt32_encodeBinary(&(src->secureChannelId),pos,dst); return retval; }