ua_client.c 24 KB

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