ua_client.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. #include "ua_types_generated.h"
  2. #include "ua_client.h"
  3. #include "ua_nodeids.h"
  4. #include "ua_securechannel.h"
  5. #include "ua_types_encoding_binary.h"
  6. #include "ua_transport_generated.h"
  7. struct UA_Client {
  8. /* Connection */
  9. UA_Connection connection;
  10. UA_SecureChannel channel;
  11. UA_String endpointUrl;
  12. UA_UInt32 requestId;
  13. /* Session */
  14. UA_UserTokenPolicy token;
  15. UA_NodeId sessionId;
  16. UA_NodeId authenticationToken;
  17. /* Config */
  18. UA_Logger logger;
  19. UA_ClientConfig config;
  20. UA_DateTime scExpiresAt;
  21. };
  22. const UA_EXPORT UA_ClientConfig UA_ClientConfig_standard =
  23. { 5 /* ms receive timout */, 30000, 2000,
  24. {.protocolVersion = 0, .sendBufferSize = 65536, .recvBufferSize = 65536,
  25. .maxMessageSize = 65536, .maxChunkCount = 1}};
  26. UA_Client * UA_Client_new(UA_ClientConfig config, UA_Logger logger) {
  27. UA_Client *client = UA_calloc(1, sizeof(UA_Client));
  28. if(!client)
  29. return UA_NULL;
  30. UA_Connection_init(&client->connection);
  31. UA_SecureChannel_init(&client->channel);
  32. client->channel.connection = &client->connection;
  33. UA_String_init(&client->endpointUrl);
  34. client->requestId = 0;
  35. UA_NodeId_init(&client->authenticationToken);
  36. client->logger = logger;
  37. client->config = config;
  38. client->scExpiresAt = 0;
  39. return client;
  40. }
  41. void UA_Client_delete(UA_Client* client){
  42. UA_Connection_deleteMembers(&client->connection);
  43. UA_SecureChannel_deleteMembersCleanup(&client->channel);
  44. UA_String_deleteMembers(&client->endpointUrl);
  45. UA_UserTokenPolicy_deleteMembers(&client->token);
  46. free(client);
  47. }
  48. static UA_StatusCode HelAckHandshake(UA_Client *c) {
  49. UA_TcpMessageHeader messageHeader;
  50. messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_HELF;
  51. UA_TcpHelloMessage hello;
  52. UA_String_copy(&c->endpointUrl, &hello.endpointUrl);
  53. UA_Connection *conn = &c->connection;
  54. hello.maxChunkCount = conn->localConf.maxChunkCount;
  55. hello.maxMessageSize = conn->localConf.maxMessageSize;
  56. hello.protocolVersion = conn->localConf.protocolVersion;
  57. hello.receiveBufferSize = conn->localConf.recvBufferSize;
  58. hello.sendBufferSize = conn->localConf.sendBufferSize;
  59. messageHeader.messageSize = UA_TcpHelloMessage_calcSizeBinary((UA_TcpHelloMessage const*)&hello) +
  60. UA_TcpMessageHeader_calcSizeBinary((UA_TcpMessageHeader const*)&messageHeader);
  61. UA_ByteString message;
  62. UA_StatusCode retval = c->connection.getBuffer(&c->connection, &message);
  63. if(retval != UA_STATUSCODE_GOOD)
  64. return retval;
  65. size_t offset = 0;
  66. UA_TcpMessageHeader_encodeBinary(&messageHeader, &message, &offset);
  67. UA_TcpHelloMessage_encodeBinary(&hello, &message, &offset);
  68. UA_TcpHelloMessage_deleteMembers(&hello);
  69. retval = c->connection.write(&c->connection, &message, messageHeader.messageSize);
  70. if(retval != UA_STATUSCODE_GOOD)
  71. c->connection.releaseBuffer(&c->connection, &message);
  72. if(retval)
  73. return retval;
  74. UA_ByteString reply;
  75. UA_ByteString_init(&reply);
  76. do {
  77. retval = c->connection.recv(&c->connection, &reply, c->config.timeout);
  78. if(retval == UA_STATUSCODE_BADCONNECTIONCLOSED)
  79. return retval;
  80. } while(retval != UA_STATUSCODE_GOOD);
  81. offset = 0;
  82. UA_TcpMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
  83. UA_TcpAcknowledgeMessage ackMessage;
  84. retval = UA_TcpAcknowledgeMessage_decodeBinary(&reply, &offset, &ackMessage);
  85. c->connection.releaseBuffer(&c->connection, &reply);
  86. if(retval != UA_STATUSCODE_GOOD)
  87. return retval;
  88. conn->remoteConf.maxChunkCount = ackMessage.maxChunkCount;
  89. conn->remoteConf.maxMessageSize = ackMessage.maxMessageSize;
  90. conn->remoteConf.protocolVersion = ackMessage.protocolVersion;
  91. conn->remoteConf.recvBufferSize = ackMessage.receiveBufferSize;
  92. conn->remoteConf.sendBufferSize = ackMessage.sendBufferSize;
  93. conn->state = UA_CONNECTION_ESTABLISHED;
  94. return UA_STATUSCODE_GOOD;
  95. }
  96. static UA_StatusCode SecureChannelHandshake(UA_Client *client, UA_Boolean renew) {
  97. UA_SecureConversationMessageHeader messageHeader;
  98. messageHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_OPNF;
  99. messageHeader.secureChannelId = 0;
  100. UA_SequenceHeader seqHeader;
  101. seqHeader.sequenceNumber = ++client->channel.sequenceNumber;
  102. seqHeader.requestId = ++client->requestId;
  103. UA_AsymmetricAlgorithmSecurityHeader asymHeader;
  104. UA_AsymmetricAlgorithmSecurityHeader_init(&asymHeader);
  105. asymHeader.securityPolicyUri = UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
  106. /* id of opensecurechannelrequest */
  107. UA_NodeId requestType = UA_NODEID_NUMERIC(0, UA_NS0ID_OPENSECURECHANNELREQUEST + UA_ENCODINGOFFSET_BINARY);
  108. UA_OpenSecureChannelRequest opnSecRq;
  109. UA_OpenSecureChannelRequest_init(&opnSecRq);
  110. opnSecRq.requestHeader.timestamp = UA_DateTime_now();
  111. opnSecRq.requestHeader.authenticationToken = client->authenticationToken;
  112. opnSecRq.requestedLifetime = client->config.secureChannelLifeTime;
  113. if(renew) {
  114. opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_RENEW;
  115. } else {
  116. opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_ISSUE;
  117. UA_SecureChannel_generateNonce(&client->channel.clientNonce);
  118. UA_ByteString_copy(&client->channel.clientNonce, &opnSecRq.clientNonce);
  119. opnSecRq.securityMode = UA_MESSAGESECURITYMODE_NONE;
  120. }
  121. messageHeader.messageHeader.messageSize =
  122. UA_SecureConversationMessageHeader_calcSizeBinary(&messageHeader) +
  123. UA_AsymmetricAlgorithmSecurityHeader_calcSizeBinary(&asymHeader) +
  124. UA_SequenceHeader_calcSizeBinary(&seqHeader) +
  125. UA_NodeId_calcSizeBinary(&requestType) +
  126. UA_OpenSecureChannelRequest_calcSizeBinary(&opnSecRq);
  127. UA_ByteString message;
  128. UA_StatusCode retval = client->connection.getBuffer(&client->connection, &message);
  129. if(retval != UA_STATUSCODE_GOOD) {
  130. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  131. UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
  132. return retval;
  133. }
  134. size_t offset = 0;
  135. UA_SecureConversationMessageHeader_encodeBinary(&messageHeader, &message, &offset);
  136. UA_AsymmetricAlgorithmSecurityHeader_encodeBinary(&asymHeader, &message, &offset);
  137. UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
  138. UA_NodeId_encodeBinary(&requestType, &message, &offset);
  139. UA_OpenSecureChannelRequest_encodeBinary(&opnSecRq, &message, &offset);
  140. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  141. UA_OpenSecureChannelRequest_deleteMembers(&opnSecRq);
  142. retval = client->connection.write(&client->connection, &message,
  143. messageHeader.messageHeader.messageSize);
  144. if(retval != UA_STATUSCODE_GOOD)
  145. client->connection.releaseBuffer(&client->connection, &message);
  146. if(retval)
  147. return retval;
  148. // parse the response
  149. UA_ByteString reply;
  150. UA_ByteString_init(&reply);
  151. do {
  152. retval = client->connection.recv(&client->connection, &reply, client->config.timeout);
  153. if(retval == UA_STATUSCODE_BADCONNECTIONCLOSED)
  154. return retval;
  155. } while(retval != UA_STATUSCODE_GOOD);
  156. offset = 0;
  157. UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &messageHeader);
  158. UA_AsymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &asymHeader);
  159. UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
  160. UA_NodeId_decodeBinary(&reply, &offset, &requestType);
  161. UA_NodeId expectedRequest = UA_NODEID_NUMERIC(0, UA_NS0ID_OPENSECURECHANNELRESPONSE +
  162. UA_ENCODINGOFFSET_BINARY);
  163. if(!UA_NodeId_equal(&requestType, &expectedRequest)) {
  164. client->connection.releaseBuffer(&client->connection, &reply);
  165. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  166. UA_NodeId_deleteMembers(&requestType);
  167. return UA_STATUSCODE_BADINTERNALERROR;
  168. }
  169. UA_OpenSecureChannelResponse response;
  170. UA_OpenSecureChannelResponse_decodeBinary(&reply, &offset, &response);
  171. client->scExpiresAt = UA_DateTime_now() + response.securityToken.revisedLifetime * 10000;
  172. client->connection.releaseBuffer(&client->connection, &reply);
  173. retval = response.responseHeader.serviceResult;
  174. if(!renew && retval == UA_STATUSCODE_GOOD) {
  175. UA_ChannelSecurityToken_copy(&response.securityToken, &client->channel.securityToken);
  176. UA_ByteString_deleteMembers(&client->channel.serverNonce); // if the handshake is repeated
  177. UA_ByteString_copy(&response.serverNonce, &client->channel.serverNonce);
  178. }
  179. UA_OpenSecureChannelResponse_deleteMembers(&response);
  180. UA_AsymmetricAlgorithmSecurityHeader_deleteMembers(&asymHeader);
  181. return retval;
  182. }
  183. /** If the request fails, then the response is cast to UA_ResponseHeader (at the beginning of every
  184. response) and filled with the appropriate error code */
  185. static void synchronousRequest(UA_Client *client, void *request, const UA_DataType *requestType,
  186. void *response, const UA_DataType *responseType) {
  187. /* Check if sc needs to be renewed */
  188. if(client->scExpiresAt - UA_DateTime_now() <= client->config.timeToRenewSecureChannel * 10000 )
  189. UA_Client_renewSecureChannel(client);
  190. /* Copy authenticationToken token to request header */
  191. typedef struct {
  192. UA_RequestHeader requestHeader;
  193. } headerOnlyRequest;
  194. /* The cast is valid, since all requests start with a requestHeader */
  195. UA_NodeId_copy(&client->authenticationToken, &((headerOnlyRequest*)request)->requestHeader.authenticationToken);
  196. if(!response)
  197. return;
  198. UA_init(response, responseType);
  199. /* Send the request */
  200. UA_UInt32 requestId = ++client->requestId;
  201. UA_StatusCode retval = UA_SecureChannel_sendBinaryMessage(&client->channel, requestId,
  202. request, requestType);
  203. UA_ResponseHeader *respHeader = (UA_ResponseHeader*)response;
  204. if(retval) {
  205. respHeader->serviceResult = retval;
  206. return;
  207. }
  208. /* Retrieve the response */
  209. // Todo: push this into the generic securechannel implementation for client and server
  210. UA_ByteString reply;
  211. UA_ByteString_init(&reply);
  212. do {
  213. retval = client->connection.recv(&client->connection, &reply, client->config.timeout);
  214. if(retval == UA_STATUSCODE_BADCONNECTIONCLOSED) {
  215. respHeader->serviceResult = retval;
  216. return;
  217. }
  218. } while(retval != UA_STATUSCODE_GOOD);
  219. size_t offset = 0;
  220. UA_SecureConversationMessageHeader msgHeader;
  221. retval |= UA_SecureConversationMessageHeader_decodeBinary(&reply, &offset, &msgHeader);
  222. UA_SymmetricAlgorithmSecurityHeader symHeader;
  223. retval |= UA_SymmetricAlgorithmSecurityHeader_decodeBinary(&reply, &offset, &symHeader);
  224. UA_SequenceHeader seqHeader;
  225. retval |= UA_SequenceHeader_decodeBinary(&reply, &offset, &seqHeader);
  226. UA_NodeId responseId;
  227. retval |= UA_NodeId_decodeBinary(&reply, &offset, &responseId);
  228. UA_NodeId expectedNodeId = UA_NODEID_NUMERIC(0, responseType->typeId.identifier.numeric +
  229. UA_ENCODINGOFFSET_BINARY);
  230. if(!UA_NodeId_equal(&responseId, &expectedNodeId) || seqHeader.requestId != requestId) {
  231. // Todo: we need to demux responses since a publish responses may come at any time
  232. client->connection.releaseBuffer(&client->connection, &reply);
  233. UA_SymmetricAlgorithmSecurityHeader_deleteMembers(&symHeader);
  234. respHeader->serviceResult = UA_STATUSCODE_BADINTERNALERROR;
  235. return;
  236. }
  237. retval = UA_decodeBinary(&reply, &offset, response, responseType);
  238. client->connection.releaseBuffer(&client->connection, &reply);
  239. if(retval != UA_STATUSCODE_GOOD)
  240. respHeader->serviceResult = retval;
  241. }
  242. static UA_StatusCode ActivateSession(UA_Client *client) {
  243. UA_ActivateSessionRequest request;
  244. UA_ActivateSessionRequest_init(&request);
  245. request.requestHeader.requestHandle = 2; //TODO: is it a magic number?
  246. request.requestHeader.authenticationToken = client->authenticationToken;
  247. request.requestHeader.timestamp = UA_DateTime_now();
  248. request.requestHeader.timeoutHint = 10000;
  249. UA_AnonymousIdentityToken identityToken;
  250. UA_AnonymousIdentityToken_init(&identityToken);
  251. UA_String_copy(&client->token.policyId, &identityToken.policyId);
  252. //manual ExtensionObject encoding of the identityToken
  253. request.userIdentityToken.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  254. request.userIdentityToken.typeId = UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN].typeId;
  255. request.userIdentityToken.typeId.identifier.numeric+=UA_ENCODINGOFFSET_BINARY;
  256. UA_ByteString_newMembers(&request.userIdentityToken.body, identityToken.policyId.length+4);
  257. size_t offset = 0;
  258. UA_ByteString_encodeBinary(&identityToken.policyId,&request.userIdentityToken.body,&offset);
  259. UA_ActivateSessionResponse response;
  260. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_ACTIVATESESSIONREQUEST],
  261. &response, &UA_TYPES[UA_TYPES_ACTIVATESESSIONRESPONSE]);
  262. UA_AnonymousIdentityToken_deleteMembers(&identityToken);
  263. UA_ActivateSessionRequest_deleteMembers(&request);
  264. UA_ActivateSessionResponse_deleteMembers(&response);
  265. return response.responseHeader.serviceResult; // not deleted
  266. }
  267. static UA_StatusCode EndpointsHandshake(UA_Client *client) {
  268. UA_GetEndpointsRequest request;
  269. UA_GetEndpointsRequest_init(&request);
  270. UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
  271. request.requestHeader.timestamp = UA_DateTime_now();
  272. request.requestHeader.timeoutHint = 10000;
  273. UA_String_copy(&client->endpointUrl, &request.endpointUrl);
  274. request.profileUrisSize = 1;
  275. request.profileUris = UA_Array_new(&UA_TYPES[UA_TYPES_STRING], request.profileUrisSize);
  276. *request.profileUris = UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
  277. UA_GetEndpointsResponse response;
  278. UA_GetEndpointsResponse_init(&response);
  279. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST],
  280. &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]);
  281. UA_Boolean endpointFound = UA_FALSE;
  282. UA_Boolean tokenFound = UA_FALSE;
  283. for(UA_Int32 i=0; i<response.endpointsSize; ++i){
  284. UA_EndpointDescription* endpoint = &response.endpoints[i];
  285. /* look out for an endpoint without security */
  286. if(!UA_String_equal(&endpoint->securityPolicyUri,
  287. &UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#None")))
  288. continue;
  289. endpointFound = UA_TRUE;
  290. /* endpoint with no security found */
  291. /* look for a user token policy with an anonymous token */
  292. for(UA_Int32 j=0; j<endpoint->userIdentityTokensSize; ++j) {
  293. UA_UserTokenPolicy* userToken = &endpoint->userIdentityTokens[j];
  294. if(userToken->tokenType != UA_USERTOKENTYPE_ANONYMOUS)
  295. continue;
  296. tokenFound = UA_TRUE;
  297. UA_UserTokenPolicy_copy(userToken, &client->token);
  298. break;
  299. }
  300. }
  301. UA_GetEndpointsRequest_deleteMembers(&request);
  302. UA_GetEndpointsResponse_deleteMembers(&response);
  303. if(!endpointFound){
  304. UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT, "No suitable endpoint found");
  305. return UA_STATUSCODE_BADINTERNALERROR;
  306. }
  307. if(!tokenFound){
  308. UA_LOG_ERROR(client->logger, UA_LOGCATEGORY_CLIENT, "No anonymous token found");
  309. return UA_STATUSCODE_BADINTERNALERROR;
  310. }
  311. return response.responseHeader.serviceResult;
  312. }
  313. static UA_StatusCode SessionHandshake(UA_Client *client) {
  314. UA_CreateSessionRequest request;
  315. UA_CreateSessionRequest_init(&request);
  316. // todo: is this needed for all requests?
  317. UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
  318. request.requestHeader.timestamp = UA_DateTime_now();
  319. request.requestHeader.timeoutHint = 10000;
  320. UA_ByteString_copy(&client->channel.clientNonce, &request.clientNonce);
  321. request.requestedSessionTimeout = 1200000;
  322. request.maxResponseMessageSize = UA_INT32_MAX;
  323. UA_CreateSessionResponse response;
  324. UA_CreateSessionResponse_init(&response);
  325. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_CREATESESSIONREQUEST],
  326. &response, &UA_TYPES[UA_TYPES_CREATESESSIONRESPONSE]);
  327. UA_NodeId_copy(&response.authenticationToken, &client->authenticationToken);
  328. UA_CreateSessionRequest_deleteMembers(&request);
  329. UA_CreateSessionResponse_deleteMembers(&response);
  330. return response.responseHeader.serviceResult; // not deleted
  331. }
  332. static UA_StatusCode CloseSession(UA_Client *client) {
  333. UA_CloseSessionRequest request;
  334. UA_CloseSessionRequest_init(&request);
  335. request.requestHeader.timestamp = UA_DateTime_now();
  336. request.requestHeader.timeoutHint = 10000;
  337. request.deleteSubscriptions = UA_TRUE;
  338. UA_NodeId_copy(&client->authenticationToken, &request.requestHeader.authenticationToken);
  339. UA_CreateSessionResponse response;
  340. synchronousRequest(client, &request, &UA_TYPES[UA_TYPES_CLOSESESSIONREQUEST],
  341. &response, &UA_TYPES[UA_TYPES_CLOSESESSIONRESPONSE]);
  342. UA_CloseSessionRequest_deleteMembers(&request);
  343. UA_CloseSessionResponse_deleteMembers(&response);
  344. return response.responseHeader.serviceResult; // not deleted
  345. }
  346. static UA_StatusCode CloseSecureChannel(UA_Client *client) {
  347. UA_SecureChannel *channel = &client->channel;
  348. UA_CloseSecureChannelRequest request;
  349. UA_CloseSecureChannelRequest_init(&request);
  350. request.requestHeader.requestHandle = 1; //TODO: magic number?
  351. request.requestHeader.timestamp = UA_DateTime_now();
  352. request.requestHeader.timeoutHint = 10000;
  353. request.requestHeader.authenticationToken = client->authenticationToken;
  354. UA_SecureConversationMessageHeader msgHeader;
  355. msgHeader.messageHeader.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_CLOF;
  356. msgHeader.secureChannelId = client->channel.securityToken.channelId;
  357. UA_SymmetricAlgorithmSecurityHeader symHeader;
  358. symHeader.tokenId = channel->securityToken.tokenId;
  359. UA_SequenceHeader seqHeader;
  360. seqHeader.sequenceNumber = ++channel->sequenceNumber;
  361. seqHeader.requestId = ++client->requestId;
  362. UA_NodeId typeId = UA_NODEID_NUMERIC(0, UA_NS0ID_CLOSESECURECHANNELREQUEST + UA_ENCODINGOFFSET_BINARY);
  363. msgHeader.messageHeader.messageSize =
  364. UA_SecureConversationMessageHeader_calcSizeBinary(&msgHeader) +
  365. UA_SymmetricAlgorithmSecurityHeader_calcSizeBinary(&symHeader) +
  366. UA_SequenceHeader_calcSizeBinary(&seqHeader) +
  367. UA_NodeId_calcSizeBinary(&typeId) +
  368. UA_calcSizeBinary(&request, &UA_TYPES[UA_TYPES_CLOSESECURECHANNELREQUEST]);
  369. UA_ByteString message;
  370. UA_StatusCode retval = client->connection.getBuffer(&client->connection, &message);
  371. if(retval != UA_STATUSCODE_GOOD)
  372. return retval;
  373. size_t offset = 0;
  374. retval |= UA_SecureConversationMessageHeader_encodeBinary(&msgHeader, &message, &offset);
  375. retval |= UA_SymmetricAlgorithmSecurityHeader_encodeBinary(&symHeader, &message, &offset);
  376. retval |= UA_SequenceHeader_encodeBinary(&seqHeader, &message, &offset);
  377. retval |= UA_NodeId_encodeBinary(&typeId, &message, &offset);
  378. retval |= UA_encodeBinary(&request, &UA_TYPES[UA_TYPES_CLOSESECURECHANNELREQUEST], &message, &offset);
  379. if(retval == UA_STATUSCODE_GOOD)
  380. retval = client->connection.write(&client->connection, &message, msgHeader.messageHeader.messageSize);
  381. client->connection.releaseBuffer(&client->connection, &message);
  382. return retval;
  383. }
  384. /*************************/
  385. /* User-Facing Functions */
  386. /*************************/
  387. UA_StatusCode UA_Client_connect(UA_Client *client, UA_ConnectClientConnection connectFunc, char *endpointUrl) {
  388. client->connection = connectFunc(UA_ConnectionConfig_standard, endpointUrl, &client->logger);
  389. if(client->connection.state != UA_CONNECTION_OPENING)
  390. return UA_STATUSCODE_BADCONNECTIONCLOSED;
  391. client->endpointUrl = UA_STRING_ALLOC(endpointUrl);
  392. if(client->endpointUrl.length < 0)
  393. return UA_STATUSCODE_BADOUTOFMEMORY;
  394. client->connection.localConf = client->config.localConnectionConfig;
  395. UA_StatusCode retval = HelAckHandshake(client);
  396. if(retval == UA_STATUSCODE_GOOD)
  397. retval = SecureChannelHandshake(client, UA_FALSE);
  398. if(retval == UA_STATUSCODE_GOOD)
  399. retval = EndpointsHandshake(client);
  400. if(retval == UA_STATUSCODE_GOOD)
  401. retval = SessionHandshake(client);
  402. if(retval == UA_STATUSCODE_GOOD)
  403. retval = ActivateSession(client);
  404. return retval;
  405. }
  406. UA_StatusCode UA_Client_disconnect(UA_Client *client) {
  407. UA_StatusCode retval;
  408. retval = CloseSession(client);
  409. if(retval == UA_STATUSCODE_GOOD)
  410. retval = CloseSecureChannel(client);
  411. return retval;
  412. }
  413. UA_StatusCode UA_Client_renewSecureChannel(UA_Client *client) {
  414. return SecureChannelHandshake(client, UA_TRUE);
  415. }
  416. UA_ReadResponse UA_Client_read(UA_Client *client, UA_ReadRequest *request) {
  417. UA_ReadResponse response;
  418. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_READREQUEST], &response,
  419. &UA_TYPES[UA_TYPES_READRESPONSE]);
  420. return response;
  421. }
  422. UA_WriteResponse UA_Client_write(UA_Client *client, UA_WriteRequest *request) {
  423. UA_WriteResponse response;
  424. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_WRITEREQUEST], &response,
  425. &UA_TYPES[UA_TYPES_WRITERESPONSE]);
  426. return response;
  427. }
  428. UA_BrowseResponse UA_Client_browse(UA_Client *client, UA_BrowseRequest *request) {
  429. UA_BrowseResponse response;
  430. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_BROWSEREQUEST], &response,
  431. &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
  432. return response;
  433. }
  434. UA_BrowseNextResponse UA_Client_browseNext(UA_Client *client, UA_BrowseNextRequest *request) {
  435. UA_BrowseNextResponse response;
  436. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST], &response,
  437. &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
  438. return response;
  439. }
  440. UA_TranslateBrowsePathsToNodeIdsResponse
  441. UA_Client_translateTranslateBrowsePathsToNodeIds(UA_Client *client,
  442. UA_TranslateBrowsePathsToNodeIdsRequest *request) {
  443. UA_TranslateBrowsePathsToNodeIdsResponse response;
  444. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
  445. &response, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
  446. return response;
  447. }
  448. UA_AddNodesResponse UA_Client_addNodes(UA_Client *client, UA_AddNodesRequest *request) {
  449. UA_AddNodesResponse response;
  450. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  451. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  452. return response;
  453. }
  454. UA_AddReferencesResponse UA_Client_addReferences(UA_Client *client, UA_AddReferencesRequest *request) {
  455. UA_AddReferencesResponse response;
  456. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_ADDREFERENCESREQUEST],
  457. &response, &UA_TYPES[UA_TYPES_ADDREFERENCESRESPONSE]);
  458. return response;
  459. }
  460. UA_DeleteNodesResponse UA_Client_deleteNodes(UA_Client *client, UA_DeleteNodesRequest *request) {
  461. UA_DeleteNodesResponse response;
  462. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  463. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  464. return response;
  465. }
  466. UA_DeleteReferencesResponse UA_Client_deleteReferences(UA_Client *client, UA_DeleteReferencesRequest *request) {
  467. UA_DeleteReferencesResponse response;
  468. synchronousRequest(client, request, &UA_TYPES[UA_TYPES_DELETEREFERENCESREQUEST],
  469. &response, &UA_TYPES[UA_TYPES_DELETEREFERENCESRESPONSE]);
  470. return response;
  471. }