ua_client.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include "ua_client.h"
  2. #include "ua_nodeids.h"
  3. #include "ua_types.h"
  4. #include "ua_types_encoding_binary.h"
  5. #include "ua_transport_generated.h"
  6. struct UA_Client {
  7. UA_ClientNetworkLayer networkLayer;
  8. UA_String endpointUrl;
  9. UA_Connection connection;
  10. UA_UInt32 sequenceNumber;
  11. UA_UInt32 requestId;
  12. /* Secure Channel */
  13. UA_ChannelSecurityToken securityToken;
  14. UA_ByteString clientNonce;
  15. UA_ByteString serverNonce;
  16. /* UA_SequenceHeader sequenceHdr; */
  17. /* UA_NodeId authenticationToken; */
  18. /* Session */
  19. UA_NodeId sessionId;
  20. UA_NodeId authenticationToken;
  21. };
  22. UA_Client * UA_Client_new(void) {
  23. UA_Client *client = UA_malloc(sizeof(UA_Client));
  24. if(!client)
  25. return UA_NULL;
  26. UA_String_init(&client->endpointUrl);
  27. client->connection.state = UA_CONNECTION_OPENING;
  28. client->sequenceNumber = 0;
  29. client->requestId = 0;
  30. /* Secure Channel */
  31. UA_ChannelSecurityToken_deleteMembers(&client->securityToken);
  32. UA_ByteString_init(&client->clientNonce);
  33. UA_ByteString_init(&client->serverNonce);
  34. return client;
  35. }
  36. void UA_Client_delete(UA_Client* client){
  37. client->networkLayer.delete(client->networkLayer.nlHandle);
  38. UA_String_deleteMembers(&client->endpointUrl);
  39. // client->connection
  40. /* Secure Channel */
  41. UA_ByteString_deleteMembers(&client->clientNonce);
  42. UA_ByteString_deleteMembers(&client->serverNonce);
  43. free(client);
  44. }
  45. static UA_StatusCode HelAckHandshake(UA_Client *client);
  46. static UA_StatusCode SecureChannelHandshake(UA_Client *client);
  47. static UA_StatusCode SessionHandshake(UA_Client *client);
  48. static UA_StatusCode CloseSession(UA_Client *client);
  49. static UA_StatusCode CloseSecureChannel(UA_Client *client);
  50. UA_StatusCode UA_Client_connect(UA_Client *client, UA_ConnectionConfig conf,
  51. UA_ClientNetworkLayer networkLayer, char *endpointUrl)
  52. {
  53. UA_StatusCode retval = UA_String_copycstring(endpointUrl, &client->endpointUrl);
  54. if(retval != UA_STATUSCODE_GOOD)
  55. return UA_STATUSCODE_BADOUTOFMEMORY;
  56. client->networkLayer = networkLayer;
  57. client->connection.localConf = conf;
  58. retval = networkLayer.connect(client->endpointUrl, client->networkLayer.nlHandle);
  59. if(retval != UA_STATUSCODE_GOOD)
  60. return retval;
  61. retval = HelAckHandshake(client);
  62. if(retval == UA_STATUSCODE_GOOD)
  63. retval = SecureChannelHandshake(client);
  64. if(retval == UA_STATUSCODE_GOOD)
  65. retval = SessionHandshake(client);
  66. printf("Connection returned %i\n", retval);
  67. return retval;
  68. }
  69. UA_StatusCode UA_EXPORT UA_Client_disconnect(UA_Client *client) {
  70. return UA_STATUSCODE_GOOD;
  71. }
  72. #define SETREQUESTHEADER(HEADER, CLIENT) \
  73. UA_NodeId_copy(&CLIENT->authenticationToken, &HEADER.authenticationToken); \
  74. HEADER.timestamp = UA_DateTime_now(); \
  75. HEADER.timeoutHint = 10000; \
  76. /* extend this */
  77. static UA_StatusCode SecureChannelHandshake(UA_Client *client) {
  78. UA_ByteString_deleteMembers(&client->clientNonce); // if the handshake is repeated
  79. UA_ByteString_newMembers(&client->clientNonce, 1);
  80. client->clientNonce.data[0] = 0;
  81. UA_SecureConversationMessageHeader messageHeader;
  82. messageHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_OPNF;
  83. messageHeader.secureChannelId = 0;
  84. UA_SequenceHeader seqHeader;
  85. seqHeader.sequenceNumber = ++client->sequenceNumber;
  86. seqHeader.requestId = ++client->requestId;
  87. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  88. UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
  89. UA_String_copycstring("http://opcfoundation.org/UA/SecurityPolicy#None", &asymHeader.securityPolicyUri);
  90. /* id of opensecurechannelrequest */
  91. UA_NodeId requestType = UA_NODEID_STATIC(0, UA_NS0ID_OPENSECURECHANNELREQUEST +
  92. UA_ENCODINGOFFSET_BINARY);
  93. UA_OpenSecureChannelRequest opnSecRq;
  94. UA_OpenSecureChannelRequest_init(&opnSecRq);
  95. opnSecRq.requestHeader.timestamp = UA_DateTime_now();
  96. UA_ByteString_copy(&client->clientNonce, &opnSecRq.clientNonce);
  97. opnSecRq.clientProtocolVersion = 0;
  98. opnSecRq.requestedLifetime = 30000;
  99. opnSecRq.securityMode = UA_MESSAGESECURITYMODE_NONE;
  100. opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_ISSUE;
  101. opnSecRq.requestHeader.authenticationToken.identifier.numeric = 10;
  102. opnSecRq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC;
  103. opnSecRq.requestHeader.authenticationToken.namespaceIndex = 10;
  104. messageHeader.messageHeader.messageSize =
  105. UA_SecureConversationMessageHeader_calcSizeBinary(&messageHeader) +
  106. UA_AsymmetricAlgorithmSecurityHeader_calcSizeBinary(&asymHeader) +
  107. UA_SequenceHeader_calcSizeBinary(&seqHeader) +
  108. UA_NodeId_calcSizeBinary(&requestType) +
  109. UA_OpenSecureChannelRequest_calcSizeBinary(&opnSecRq);
  110. UA_ByteString message;
  111. message.data = UA_alloca(messageHeader.messageHeader.messageSize);
  112. message.length = messageHeader.messageHeader.messageSize;
  113. size_t offset = 0;
  114. UA_SecureConversationMessageHeader_encodeBinary(&messageHeader, &message, &offset);
  115. UA_AsymmetricAlgorithmSecurityHeader_encodeBinary(&asymHeader, &message, &offset);
  116. UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
  117. UA_NodeId_encodeBinary(&requestType, &message, &offset);
  118. UA_OpenSecureChannelRequest_encodeBinary(&opnSecRq, &message, &offset);
  119. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  120. UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
  121. UA_ByteStringArray buf = {.stringsSize = 1, .strings = &message};
  122. UA_StatusCode retval = client->networkLayer.send(client->networkLayer.nlHandle, buf);
  123. if(retval)
  124. return retval;
  125. // parse the response
  126. UA_ByteString reply;
  127. UA_ByteString_newMembers(&reply, client->connection.localConf.recvBufferSize);
  128. retval = client->networkLayer.awaitResponse(client->networkLayer.nlHandle, &reply, 1000);
  129. if(retval) {
  130. UA_ByteString_deleteMembers(&reply);
  131. return retval;
  132. }
  133. offset = 0;
  134. UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
  135. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &asymHeader);
  136. UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
  137. UA_NodeId_decodeBinary(&reply, &offset, &requestType);
  138. if(!UA_NodeId_equal(&requestType, &UA_NODEID_STATIC(0, UA_NS0ID_OPENSECURECHANNELRESPONSE +
  139. UA_ENCODINGOFFSET_BINARY))) {
  140. UA_ByteString_deleteMembers(&reply);
  141. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  142. return UA_STATUSCODE_BADINTERNALERROR;
  143. }
  144. UA_OpenSecureChannelResponse response;
  145. UA_OpenSecureChannelResponse_decodeBinary(&reply, &offset, &response);
  146. UA_ByteString_deleteMembers(&reply);
  147. retval = response.responseHeader.serviceResult;
  148. if(retval == UA_STATUSCODE_GOOD) {
  149. UA_ChannelSecurityToken_copy(&response.securityToken, &client->securityToken);
  150. UA_ByteString_deleteMembers(&client->serverNonce); // if the handshake is repeated
  151. UA_ByteString_copy(&response.serverNonce, &client->serverNonce);
  152. }
  153. UA_OpenSecureChannelResponse_deleteMembers(&response);
  154. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  155. return retval;
  156. }
  157. // The tcp connection is established. Now do the handshake
  158. static UA_StatusCode HelAckHandshake(UA_Client *c) {
  159. UA_TcpMessageHeader messageHeader;
  160. messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_HELF;
  161. UA_TcpHelloMessage hello;
  162. UA_String_copy(&c->endpointUrl, &hello.endpointUrl);
  163. UA_Connection *conn = &c->connection;
  164. hello.maxChunkCount = conn->localConf.maxChunkCount;
  165. hello.maxMessageSize = conn->localConf.maxMessageSize;
  166. hello.protocolVersion = conn->localConf.protocolVersion;
  167. hello.receiveBufferSize = conn->localConf.recvBufferSize;
  168. hello.sendBufferSize = conn->localConf.sendBufferSize;
  169. messageHeader.messageSize = UA_TcpHelloMessage_calcSizeBinary((UA_TcpHelloMessage const*) &hello) +
  170. UA_TcpMessageHeader_calcSizeBinary((UA_TcpMessageHeader const*) &messageHeader);
  171. UA_ByteString message;
  172. message.data = UA_alloca(messageHeader.messageSize);
  173. message.length = messageHeader.messageSize;
  174. size_t offset = 0;
  175. UA_TcpMessageHeader_encodeBinary(&messageHeader, &message, &offset);
  176. UA_TcpHelloMessage_encodeBinary(&hello, &message, &offset);
  177. UA_TcpHelloMessage_deleteMembers(&hello);
  178. UA_ByteStringArray buf = {.stringsSize = 1, .strings = &message};
  179. UA_StatusCode retval = c->networkLayer.send(c->networkLayer.nlHandle, buf);
  180. if(retval)
  181. return retval;
  182. UA_Byte replybuf[1024];
  183. UA_ByteString reply = {.data = replybuf, .length = 1024};
  184. retval = c->networkLayer.awaitResponse(c->networkLayer.nlHandle, &reply, 0);
  185. if (retval)
  186. return retval;
  187. offset = 0;
  188. UA_TcpMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
  189. UA_TcpAcknowledgeMessage ackMessage;
  190. retval = UA_TcpAcknowledgeMessage_decodeBinary(&reply, &offset, &ackMessage);
  191. if(retval != UA_STATUSCODE_GOOD) {
  192. UA_TcpAcknowledgeMessage_deleteMembers(&ackMessage);
  193. return retval;
  194. }
  195. conn->remoteConf.maxChunkCount = ackMessage.maxChunkCount;
  196. conn->remoteConf.maxMessageSize = ackMessage.maxMessageSize;
  197. conn->remoteConf.protocolVersion = ackMessage.protocolVersion;
  198. conn->remoteConf.recvBufferSize = ackMessage.receiveBufferSize;
  199. conn->remoteConf.sendBufferSize = ackMessage.sendBufferSize;
  200. conn->state = UA_CONNECTION_ESTABLISHED;
  201. UA_TcpAcknowledgeMessage_deleteMembers(&ackMessage);
  202. return UA_STATUSCODE_GOOD;
  203. }
  204. /** If the request fails, then the response is cast to UA_ResponseHeader (at the beginning of every
  205. response) and filled with the appropriate error code */
  206. static void synchronousRequest(const void *request, const UA_DataType *requestType,
  207. void *response, const UA_DataType *responseType, UA_Client *client)
  208. {
  209. UA_ResponseHeader *respHeader = (UA_ResponseHeader*)response;
  210. UA_SecureConversationMessageHeader msgHeader;
  211. msgHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF;
  212. msgHeader.secureChannelId = client->securityToken.channelId;
  213. UA_SymmetricAlgorithmSecurityHeader symHeader;
  214. symHeader.tokenId = client->securityToken.tokenId;
  215. UA_SequenceHeader seqHeader;
  216. seqHeader.sequenceNumber = ++client->sequenceNumber;
  217. seqHeader.requestId = ++client->requestId;
  218. UA_NodeId requestId = UA_NODEID_STATIC(0, requestType->typeId.identifier.numeric +
  219. UA_ENCODINGOFFSET_BINARY);
  220. msgHeader.messageHeader.messageSize =
  221. UA_SecureConversationMessageHeader_calcSizeBinary(&msgHeader) +
  222. UA_SymmetricAlgorithmSecurityHeader_calcSizeBinary(&symHeader) +
  223. UA_SequenceHeader_calcSizeBinary(&seqHeader) +
  224. UA_NodeId_calcSizeBinary(&requestId) +
  225. UA_calcSizeBinary(request, requestType);
  226. UA_ByteString message;
  227. UA_StatusCode retval = UA_ByteString_newMembers(&message, msgHeader.messageHeader.messageSize);
  228. if(retval != UA_STATUSCODE_GOOD) {
  229. }
  230. size_t offset = 0;
  231. retval |= UA_SecureConversationMessageHeader_encodeBinary(&msgHeader, &message, &offset);
  232. retval |= UA_SymmetricAlgorithmSecurityHeader_encodeBinary(&symHeader, &message, &offset);
  233. retval |= UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
  234. retval |= UA_NodeId_encodeBinary(&requestId, &message, &offset);
  235. retval |= UA_encodeBinary(request, requestType, &message, &offset);
  236. UA_ByteStringArray buf = {.stringsSize = 1, .strings = &message};
  237. retval = client->networkLayer.send(client->networkLayer.nlHandle, buf);
  238. if(retval != UA_STATUSCODE_GOOD) {
  239. respHeader->serviceResult = retval;
  240. return;
  241. }
  242. /* Response */
  243. UA_ByteString reply;
  244. UA_ByteString_newMembers(&reply, client->connection.localConf.recvBufferSize);
  245. retval = client->networkLayer.awaitResponse(client->networkLayer.nlHandle, &reply, 1000);
  246. if(retval != UA_STATUSCODE_GOOD) {
  247. UA_ByteString_deleteMembers(&reply);
  248. respHeader->serviceResult = retval;
  249. return;
  250. }
  251. offset = 0;
  252. retval |= UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &msgHeader);
  253. retval |= UA_SymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &symHeader);
  254. retval |= UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
  255. UA_NodeId responseId;
  256. retval |= UA_NodeId_decodeBinary(&reply, &offset, &responseId);
  257. if(!UA_NodeId_equal(&responseId, &UA_NODEID_STATIC(0, responseType->typeId.identifier.numeric +
  258. UA_ENCODINGOFFSET_BINARY))) {
  259. UA_ByteString_deleteMembers(&reply);
  260. UA_SymmetricAlgorithmSecurityHeader_deleteMembers(&symHeader);
  261. respHeader->serviceResult = retval;
  262. return;
  263. }
  264. retval = UA_decodeBinary(&reply, &offset, response, responseType);
  265. UA_ByteString_deleteMembers(&reply);
  266. if(retval != UA_STATUSCODE_GOOD)
  267. respHeader->serviceResult = retval;
  268. }
  269. static UA_StatusCode SessionHandshake(UA_Client *client) {
  270. UA_CreateSessionRequest request;
  271. UA_CreateSessionRequest_init(&request);
  272. SETREQUESTHEADER(request.requestHeader, client);
  273. /* UA_String_copy(endpointUrl, &rq.endpointUrl); */
  274. /* UA_String_copycstring("mysession", &rq.sessionName); */
  275. /* UA_String_copycstring("abcd", &rq.clientCertificate); */
  276. UA_ByteString_copy(&client->clientNonce, &request.clientNonce);
  277. request.requestedSessionTimeout = 1200000;
  278. request.maxResponseMessageSize = UA_INT32_MAX;
  279. UA_CreateSessionResponse response;
  280. synchronousRequest(&request, &UA_TYPES[UA_TYPES_CREATESESSIONREQUEST],
  281. &response, &UA_TYPES[UA_TYPES_CREATESESSIONRESPONSE],
  282. client);
  283. UA_CreateSessionResponse_deleteMembers(&response);
  284. return response.responseHeader.serviceResult; // not deleted
  285. }
  286. /*************************/
  287. /* User-Facing Functions */
  288. /*************************/
  289. UA_ReadResponse UA_Client_read(UA_Client *client, const UA_ReadRequest *request) {
  290. UA_ReadResponse response;
  291. synchronousRequest(request, &UA_TYPES[UA_TYPES_READREQUEST], &response,
  292. &UA_TYPES[UA_TYPES_READRESPONSE], client);
  293. return response;
  294. }
  295. UA_WriteResponse UA_Client_write(UA_Client *client, const UA_WriteRequest *request) {
  296. UA_WriteResponse response;
  297. synchronousRequest(request, &UA_TYPES[UA_TYPES_WRITEREQUEST], &response,
  298. &UA_TYPES[UA_TYPES_WRITERESPONSE], client);
  299. return response;
  300. }
  301. UA_BrowseResponse UA_Client_browse(UA_Client *client, const UA_BrowseRequest *request) {
  302. UA_BrowseResponse response;
  303. synchronousRequest(request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  304. &UA_TYPES[UA_TYPES_BROWSERESPONSE], client);
  305. return response;
  306. }
  307. UA_TranslateBrowsePathsToNodeIdsResponse
  308. UA_Client_translateBrowsePathsToNodeIds(UA_Client *client,
  309. const UA_TranslateBrowsePathsToNodeIdsRequest *request) {
  310. UA_TranslateBrowsePathsToNodeIdsResponse response;
  311. synchronousRequest(request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  312. &UA_TYPES[UA_TYPES_BROWSERESPONSE], client);
  313. return response;
  314. }
  315. UA_AddNodesResponse UA_Client_addNodes(UA_Client *client, const UA_AddNodesRequest *request) {
  316. UA_AddNodesResponse response;
  317. synchronousRequest(request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  318. &UA_TYPES[UA_TYPES_BROWSERESPONSE], client);
  319. return response;
  320. }
  321. UA_AddReferencesResponse UA_Client_addReferences(UA_Client *client, const UA_AddReferencesRequest *request) {
  322. UA_AddReferencesResponse response;
  323. synchronousRequest(request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  324. &UA_TYPES[UA_TYPES_BROWSERESPONSE], client);
  325. return response;
  326. }
  327. UA_DeleteNodesResponse UA_Client_deleteNodes(UA_Client *client, const UA_DeleteNodesRequest *request) {
  328. UA_DeleteNodesResponse response;
  329. synchronousRequest(request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  330. &UA_TYPES[UA_TYPES_BROWSERESPONSE], client);
  331. return response;
  332. }
  333. UA_DeleteReferencesResponse UA_Client_deleteReferences(UA_Client *client,
  334. const UA_DeleteReferencesRequest *request) {
  335. UA_DeleteReferencesResponse response;
  336. synchronousRequest(request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  337. &UA_TYPES[UA_TYPES_BROWSERESPONSE], client);
  338. return response;
  339. }