opcuaClient.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. C ECHO client example using sockets
  3. */
  4. #include <stdio.h> //printf
  5. #include <string.h> //strlen
  6. #include <sys/socket.h> //socket
  7. #include <arpa/inet.h> //inet_addr
  8. #include <unistd.h> // for close
  9. #include <stdlib.h> // pulls in declaration of malloc, free
  10. #include "ua_transport_generated.h"
  11. #include "ua_namespace_0.h"
  12. UA_Int32 sendHello(UA_Int32 sock,UA_String *endpointURL)
  13. {
  14. UA_ByteString *message;
  15. UA_ByteString_new(&message);
  16. UA_ByteString_newMembers(message,1000);
  17. UA_UInt32 offset = 0;
  18. UA_String endpointUrl;
  19. UA_String_copy(endpointURL,&endpointUrl);
  20. UA_TcpMessageHeader messageHeader;
  21. UA_TcpHelloMessage hello;
  22. messageHeader.isFinal = 'F';
  23. messageHeader.messageType = UA_MESSAGETYPE_HEL;
  24. hello.endpointUrl = endpointUrl;
  25. hello.maxChunkCount = 1;
  26. hello.maxMessageSize = 16777216;
  27. hello.protocolVersion = 0;
  28. hello.receiveBufferSize = 65536;
  29. hello.sendBufferSize = 65536;
  30. messageHeader.messageSize = UA_TcpHelloMessage_calcSizeBinary((UA_TcpHelloMessage const*) &hello) +
  31. UA_TcpMessageHeader_calcSizeBinary((UA_TcpMessageHeader const*)&messageHeader);
  32. UA_TcpMessageHeader_encodeBinary((UA_TcpMessageHeader const*) &messageHeader, message, &offset);
  33. UA_TcpHelloMessage_encodeBinary((UA_TcpHelloMessage const*) &hello, message, &offset);
  34. UA_Int32 sendret = send(sock , message->data, offset , 0);
  35. UA_ByteString_delete(message);
  36. free(endpointUrl.data);
  37. if(sendret <0)
  38. {
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. int sendOpenSecureChannel(UA_Int32 sock)
  44. {
  45. UA_ByteString *message;
  46. UA_ByteString_new(&message);
  47. UA_ByteString_newMembers(message,1000);
  48. UA_UInt32 offset = 0;
  49. UA_TcpMessageHeader msghdr;
  50. msghdr.isFinal = 'F';
  51. msghdr.messageType = UA_MESSAGETYPE_OPN;
  52. msghdr.messageSize = 133;
  53. UA_TcpMessageHeader_encodeBinary(&msghdr,message,&offset);
  54. UA_UInt32 secureChannelId = 0;
  55. UA_UInt32_encodeBinary(&secureChannelId,message,&offset);
  56. UA_String securityPolicy;
  57. UA_String_copycstring("http://opcfoundation.org/UA/SecurityPolicy#None",&securityPolicy);
  58. UA_String_encodeBinary(&securityPolicy,message,&offset);
  59. UA_String senderCert;
  60. senderCert.data = UA_NULL;
  61. senderCert.length = -1;
  62. UA_String_encodeBinary(&senderCert,message,&offset);
  63. UA_String receiverCertThumb;
  64. receiverCertThumb.data = UA_NULL;
  65. receiverCertThumb.length = -1;
  66. UA_String_encodeBinary(&receiverCertThumb,message,&offset);
  67. UA_UInt32 sequenceNumber = 51;
  68. UA_UInt32_encodeBinary(&sequenceNumber,message,&offset);
  69. UA_UInt32 requestId = 1;
  70. UA_UInt32_encodeBinary(&requestId,message,&offset);
  71. UA_NodeId type;
  72. type.identifier.numeric = 446;
  73. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  74. type.namespaceIndex = 0;
  75. UA_NodeId_encodeBinary(&type,message,&offset);
  76. UA_OpenSecureChannelRequest opnSecRq;
  77. UA_OpenSecureChannelRequest_init(&opnSecRq);
  78. opnSecRq.requestHeader.timestamp = UA_DateTime_now();
  79. UA_ByteString_newMembers(&opnSecRq.clientNonce,1);
  80. opnSecRq.clientNonce.data[0] = 0;
  81. opnSecRq.clientProtocolVersion = 0;
  82. opnSecRq.requestedLifetime = 30000;
  83. opnSecRq.securityMode = UA_SECURITYMODE_NONE;
  84. opnSecRq.requestType = UA_SECURITYTOKENREQUESTTYPE_ISSUE;
  85. UA_OpenSecureChannelRequest_encodeBinary(&opnSecRq,message,&offset);
  86. UA_Int32 sendret = send(sock , message->data, offset , 0);
  87. UA_ByteString_delete(message);
  88. free(securityPolicy.data);
  89. if(sendret<0)
  90. {
  91. printf("send opensecurechannel failed");
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. UA_Int32 sendCreateSession(UA_Int32 sock,UA_UInt32 channelId,UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId,UA_String *endpointUrl)
  97. {
  98. UA_ByteString *message;
  99. UA_ByteString_new(&message);
  100. UA_ByteString_newMembers(message,65536);
  101. UA_UInt32 tmpChannelId = channelId;
  102. UA_UInt32 offset = 0;
  103. UA_TcpMessageHeader msghdr;
  104. msghdr.isFinal = 'F';
  105. msghdr.messageType = UA_MESSAGETYPE_MSG;
  106. msghdr.messageSize = 162;
  107. UA_TcpMessageHeader_encodeBinary(&msghdr,message,&offset);
  108. UA_UInt32_encodeBinary(&tmpChannelId,message,&offset);
  109. UA_UInt32_encodeBinary(&tokenId,message,&offset);
  110. UA_UInt32_encodeBinary(&sequenceNumber,message,&offset);
  111. UA_UInt32_encodeBinary(&requestId,message,&offset);
  112. UA_NodeId type;
  113. type.identifier.numeric = 461;
  114. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  115. type.namespaceIndex = 0;
  116. UA_NodeId_encodeBinary(&type, message, &offset);
  117. UA_CreateSessionRequest rq;
  118. UA_RequestHeader_init(&rq.requestHeader);
  119. rq.requestHeader.authenticationToken.identifier.numeric = 0;
  120. rq.requestHeader.authenticationToken.identifierType = UA_NODEIDTYPE_NUMERIC;
  121. rq.requestHeader.authenticationToken.namespaceIndex = 0;
  122. rq.requestHeader.requestHandle = 1;
  123. rq.requestHeader.timestamp = UA_DateTime_now();
  124. rq.requestHeader.timeoutHint = 10000;
  125. rq.requestHeader.auditEntryId.length = -1;
  126. UA_String_copy(endpointUrl,&rq.endpointUrl);
  127. rq.clientDescription.applicationName.locale.length = -1;
  128. rq.clientDescription.applicationName.text.length = -1;
  129. rq.clientDescription.applicationUri.length = -1;
  130. rq.clientDescription.discoveryProfileUri.length = -1;
  131. rq.clientDescription.discoveryUrls = UA_NULL;
  132. rq.clientDescription.discoveryUrlsSize = -1;
  133. rq.clientDescription.gatewayServerUri.length = -1;
  134. rq.clientDescription.productUri.length = -1;
  135. UA_String_copycstring("mysession",&rq.sessionName);
  136. UA_String_copycstring("abcd",&rq.clientCertificate);
  137. UA_ByteString_newMembers(&rq.clientNonce,1);
  138. rq.clientNonce.data[0] = 0;
  139. rq.requestedSessionTimeout = 1200000;
  140. rq.maxResponseMessageSize = UA_INT32_MAX;
  141. UA_CreateSessionRequest_encodeBinary(&rq,message, &offset);
  142. UA_Int32 sendret = send(sock , message->data, offset , 0);
  143. UA_ByteString_delete(message);
  144. free(rq.sessionName.data);
  145. free(rq.clientCertificate.data);
  146. if(sendret<0)
  147. {
  148. printf("send opensecurechannel failed");
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. UA_Int32 sendActivateSession(UA_Int32 sock,UA_UInt32 channelId,UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId)
  154. {
  155. UA_ByteString *message;
  156. UA_ByteString_new(&message);
  157. UA_ByteString_newMembers(message,65536);
  158. UA_UInt32 tmpChannelId = channelId;
  159. UA_UInt32 offset = 0;
  160. UA_TcpMessageHeader msghdr;
  161. msghdr.isFinal = 'F';
  162. msghdr.messageType = UA_MESSAGETYPE_MSG;
  163. msghdr.messageSize = 84;
  164. UA_TcpMessageHeader_encodeBinary(&msghdr,message,&offset);
  165. UA_UInt32_encodeBinary(&tmpChannelId,message,&offset);
  166. UA_UInt32_encodeBinary(&tokenId,message,&offset);
  167. UA_UInt32_encodeBinary(&sequenceNumber,message,&offset);
  168. UA_UInt32_encodeBinary(&requestId,message,&offset);
  169. UA_NodeId type;
  170. type.identifier.numeric = 467;
  171. type.identifierType = UA_NODEIDTYPE_NUMERIC;
  172. type.namespaceIndex = 0;
  173. UA_NodeId_encodeBinary(&type, message, &offset);
  174. UA_ActivateSessionRequest rq;
  175. UA_ActivateSessionRequest_init(&rq);
  176. UA_ActivateSessionRequest_encodeBinary(&rq, message, &offset);
  177. UA_Int32 sendret = send(sock , message->data, offset , 0);
  178. UA_ByteString_delete(message);
  179. if(sendret<0)
  180. {
  181. printf("send opensecurechannel failed");
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. int main(int argc , char *argv[])
  187. {
  188. int sock;
  189. struct sockaddr_in server;
  190. UA_ByteString *reply;
  191. UA_ByteString_new(&reply);
  192. UA_ByteString_newMembers(reply, 65536);
  193. //Create socket
  194. sock = socket(AF_INET , SOCK_STREAM , 0);
  195. if (sock == -1)
  196. {
  197. printf("Could not create socket");
  198. }
  199. server.sin_addr.s_addr = inet_addr("134.130.125.48");
  200. server.sin_family = AF_INET;
  201. server.sin_port = htons( 16663 );
  202. //Connect to remote server
  203. if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
  204. {
  205. perror("connect failed. Error");
  206. return 1;
  207. }
  208. UA_String *endpointUrl;
  209. UA_String_new(&endpointUrl);
  210. UA_String_copycstring("opc.tcp://134.130.125.48:16663",endpointUrl);
  211. sendHello(sock,endpointUrl);
  212. int received = recv(sock ,reply->data , reply->length , 0);
  213. sendOpenSecureChannel(sock);
  214. received = recv(sock , reply->data, reply->length , 0);
  215. UA_UInt32 recvOffset = 0;
  216. UA_TcpMessageHeader msghdr;
  217. UA_TcpMessageHeader_decodeBinary(reply,&recvOffset,&msghdr);
  218. UA_UInt32 secureChannelId;
  219. UA_UInt32_decodeBinary(reply,&recvOffset,&secureChannelId);
  220. sendCreateSession(sock,secureChannelId,1,52,2,endpointUrl);
  221. received = recv(sock , reply->data, reply->length , 0);
  222. sendActivateSession(sock,secureChannelId,1,53,3);
  223. received = recv(sock , reply->data, reply->length , 0);
  224. /*
  225. UA_TcpMessageHeader reqTcpHeader;
  226. UA_UInt32 reqSecureChannelId = 0;
  227. UA_UInt32 reqTokenId = 0;
  228. UA_SequenceHeader reqSequenceHeader;
  229. UA_NodeId reqRequestType;
  230. UA_ReadRequest req;
  231. UA_RequestHeader reqHeader;
  232. UA_NodeId reqHeaderAuthToken;
  233. UA_ExtensionObject reqHeaderAdditionalHeader;
  234. UA_NodeId_init(&reqRequestType);
  235. reqRequestType.identifierType = UA_NODEIDTYPE_NUMERIC;
  236. reqRequestType.identifier.numeric = 631; //read request
  237. UA_ReadRequest_init(&req);
  238. req.requestHeader = reqHeader;
  239. UA_RequestHeader_init(&(req.requestHeader));
  240. req.requestHeader.authenticationToken = reqHeaderAuthToken;
  241. UA_NodeId_init(&(req.requestHeader.authenticationToken));
  242. req.requestHeader.additionalHeader = reqHeaderAdditionalHeader;
  243. UA_ExtensionObject_init(&(req.requestHeader.additionalHeader));
  244. UA_Array_new((void **)&req.nodesToRead, 1, &UA_.types[UA_READVALUEID]);
  245. req.nodesToReadSize = 1;
  246. UA_ReadValueId_init(&(req.nodesToRead[0]));
  247. req.nodesToRead[0].attributeId = 13; //UA_ATTRIBUTEID_VALUE
  248. UA_NodeId_init(&(req.nodesToRead[0].nodeId));
  249. req.nodesToRead[0].nodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  250. req.nodesToRead[0].nodeId.identifier.numeric = 2255;
  251. UA_QualifiedName_init(&(req.nodesToRead[0].dataEncoding));
  252. messageEncodedLength = UA_TcpMessageHeader_calcSizeBinary(&reqTcpHeader) +
  253. UA_UInt32_calcSizeBinary(&reqSecureChannelId)+
  254. UA_UInt32_calcSizeBinary(&reqTokenId)+
  255. UA_SequenceHeader_calcSizeBinary(&reqSequenceHeader)+
  256. UA_NodeId_calcSizeBinary(&reqRequestType) +
  257. UA_ReadRequest_calcSizeBinary(&req);
  258. UA_TcpMessageHeader_init(&reqTcpHeader);
  259. reqTcpHeader.messageType = UA_MESSAGETYPE_MSG;
  260. reqTcpHeader.messageSize = messageEncodedLength;
  261. reqTcpHeader.isFinal = 'F';
  262. UA_TcpMessageHeader_encodeBinary(&reqTcpHeader, &message, &messagepos);
  263. UA_UInt32_encodeBinary(&reqSecureChannelId, &message, &messagepos);
  264. UA_UInt32_encodeBinary(&reqTokenId, &message, &messagepos);
  265. UA_SequenceHeader_encodeBinary(&reqSequenceHeader, &message, &messagepos);
  266. UA_NodeId_encodeBinary(&reqRequestType, &message, &messagepos);
  267. UA_ReadRequest_encodeBinary(&req, &message, &messagepos);
  268. */
  269. //Send some data
  270. //Receive a reply from the server
  271. received = recv(sock ,reply->data , 2000 , 0);
  272. if(received < 0)
  273. {
  274. puts("recv failed");
  275. return 1;
  276. }
  277. for(int i=0;i<received;i++){
  278. //show only printable ascii
  279. if(reply->data[i] >= 32 && reply->data[i]<= 126)
  280. printf("%c",reply->data[i]);
  281. }
  282. printf("\n");
  283. close(sock);
  284. return 0;
  285. }