opcuaServerMini.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**************************************************************
  2. * opcuaServerMini is a pathologically unstructured bare bone
  3. * implementation of the 62541 communications w/o any structures
  4. * or comfort. Furthermore, the implementation assumes a
  5. * little endian target and consequently ignores all the rules
  6. * of 62541 towards security.
  7. *
  8. * The nice thing about this approach is that you can easily see
  9. * that the memory footprint of a simple server can be held
  10. * quite small and memory allocation capabilities is not needed
  11. * as long as features are consequently minimized.
  12. ****************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <memory.h>
  16. #include <errno.h> // errno, EINTR
  17. #include "ua_statuscodes.h"
  18. #include "ua_transport.h"
  19. #include "ua_transport_binary.h"
  20. #include "networklayer.h"
  21. #ifdef LINUX
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <sys/socketvar.h>
  26. #include <unistd.h>
  27. void server_run();
  28. #endif
  29. #define PORT 16664
  30. #define MAXMSG 512
  31. #define BUFFER_SIZE 8192
  32. int main(void) {
  33. #ifdef LINUX
  34. printf("Starting open62541 demo server on port %d\n", PORT);
  35. server_run();
  36. #endif
  37. return EXIT_SUCCESS;
  38. }
  39. #ifdef LINUX
  40. UA_Byte ack_msg_buf[] = { 0x41, 0x43, /* AC */
  41. 0x4b, 0x46, 0x1c, 0x00, 0x00, 0x00, 0xff, 0xff, /* KF...... */
  42. 0xff, 0xff, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, /* ... ... */
  43. 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x01, 0x00, /* ...@.... */
  44. 0x00, 0x00 /* .. */
  45. };
  46. UA_ByteString ack_msg = { sizeof(ack_msg_buf), ack_msg_buf };
  47. UA_ByteString* ack_msg_gb[] = { &ack_msg };
  48. UA_Byte opn_msg_buf[] = { 0x4f, 0x50, /* OP */
  49. 0x4e, 0x46, 0x88, 0x00, 0x00, 0x00, 0x19, 0x00, /* NF...... */
  50. 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x68, 0x74, /* ../...ht */
  51. 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6f, 0x70, 0x63, /* tp://opc */
  52. 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74, 0x69, /* foundati */
  53. 0x6f, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x55, /* on.org/U */
  54. 0x41, 0x2f, 0x53, 0x65, 0x63, 0x75, 0x72, 0x69, /* A/Securi */
  55. 0x74, 0x79, 0x50, 0x6f, 0x6c, 0x69, 0x63, 0x79, /* tyPolicy */
  56. 0x23, 0x4e, 0x6f, 0x6e, 0x65, 0xff, 0xff, 0xff, /* #None... */
  57. 0xff, 0xff, 0xff, 0xff, 0xff, 0x33, 0x00, 0x00, /* .....3.. */
  58. 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0xc1, /* ........ */
  59. 0x01, 0x10, 0x56, 0x73, 0x9e, 0xdf, 0x63, 0xcf, /* ..Vs..c. */
  60. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
  62. 0x00, 0xff, 0xff, 0xff, 0xff, 0x19, 0x00, 0x00, /* ........ */
  63. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
  65. 0x00, 0x01, 0x00, 0x00, 0x00, 0x48 /* .....H */
  66. };
  67. UA_ByteString opn_msg = { sizeof(opn_msg_buf), opn_msg_buf };
  68. UA_ByteString* opn_msg_gb[] = { &opn_msg };
  69. UA_Byte scm_msg_buf[] = { 0x4d, 0x53, /* MS */
  70. 0x47, 0x46, 0x8b, 0x01, 0x00, 0x00, 0x19, 0x00, /* GF<XX>.. */ // <XX> = msglen. TODO: should be set or checked by writer?
  71. 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x34, 0x00, /* ......4. */
  72. 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 /* ...... */
  73. };
  74. UA_Byte rsp_msg_buf[] = { 0x01, 0x00, /* ........ */
  75. 0xaf, 0x01, 0x2c, 0xc1, 0x73, 0x9e, 0xdf, 0x63, /* <><XXXXX */ // <> = response nS0-ID
  76. 0xcf, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, /* X>....<A */ // <X> = timestamp,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* A>...... */ // <A> = statuscode
  78. 0x00, 0x00 /* .. */
  79. };
  80. UA_Byte gep_msg_buf[] = {
  81. 0x01, 0x00, 0x00, 0x00, 0x1b, 0x00, /* ...... */
  82. 0x00, 0x00, 0x6f, 0x70, 0x63, 0x2e, 0x74, 0x63, /* ..opc.tc */
  83. 0x70, 0x3a, 0x2f, 0x2f, 0x31, 0x30, 0x2e, 0x30, /* p://10.0 */
  84. 0x2e, 0x35, 0x33, 0x2e, 0x31, 0x39, 0x38, 0x3a, /* .53.198: */
  85. 0x31, 0x36, 0x36, 0x36, 0x34, 0x27, 0x00, 0x00, /* 16664'.. */
  86. 0x00, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, /* .http:// */
  87. 0x6f, 0x70, 0x65, 0x6e, 0x36, 0x32, 0x35, 0x34, /* open6254 */
  88. 0x31, 0x2e, 0x69, 0x6e, 0x66, 0x6f, 0x2f, 0x61, /* 1.info/a */
  89. 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, /* pplicati */
  90. 0x6f, 0x6e, 0x73, 0x2f, 0x34, 0x37, 0x31, 0x31, /* ons/4711 */
  91. 0x25, 0x00, 0x00, 0x00, 0x68, 0x74, 0x74, 0x70, /* %...http */
  92. 0x3a, 0x2f, 0x2f, 0x6f, 0x70, 0x65, 0x6e, 0x36, /* ://open6 */
  93. 0x32, 0x35, 0x34, 0x31, 0x2e, 0x69, 0x6e, 0x66, /* 2541.inf */
  94. 0x6f, 0x2f, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, /* o/produc */
  95. 0x74, 0x2f, 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, /* t/releas */
  96. 0x65, 0x03, 0x02, 0x00, 0x00, 0x00, 0x45, 0x4e, /* e.....EN */
  97. 0x19, 0x00, 0x00, 0x00, 0x54, 0x68, 0x65, 0x20, /* ....The */
  98. 0x6f, 0x70, 0x65, 0x6e, 0x36, 0x32, 0x35, 0x34, /* open6254 */
  99. 0x31, 0x20, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, /* 1 applic */
  100. 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, /* ation... */
  101. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* ........ */
  102. 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, /* ........ */
  103. 0xff, 0x01, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, /* ...../.. */
  104. 0x00, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, /* .http:// */
  105. 0x6f, 0x70, 0x63, 0x66, 0x6f, 0x75, 0x6e, 0x64, /* opcfound */
  106. 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x6f, 0x72, /* ation.or */
  107. 0x67, 0x2f, 0x55, 0x41, 0x2f, 0x53, 0x65, 0x63, /* g/UA/Sec */
  108. 0x75, 0x72, 0x69, 0x74, 0x79, 0x50, 0x6f, 0x6c, /* urityPol */
  109. 0x69, 0x63, 0x79, 0x23, 0x4e, 0x6f, 0x6e, 0x65, /* icy#None */
  110. 0x01, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, /* ........ */
  111. 0x6d, 0x79, 0x2d, 0x61, 0x6e, 0x6f, 0x6e, 0x79, /* my-anony */
  112. 0x6d, 0x6f, 0x75, 0x73, 0x2d, 0x70, 0x6f, 0x6c, /* mous-pol */
  113. 0x69, 0x63, 0x79, 0x00, 0x00, 0x00, 0x00, 0xff, /* icy..... */
  114. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* ........ */
  115. 0xff, 0xff, 0xff, 0x41, 0x00, 0x00, 0x00, 0x68, /* ...A...h */
  116. 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6f, 0x70, /* ttp://op */
  117. 0x63, 0x66, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x74, /* cfoundat */
  118. 0x69, 0x6f, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, /* ion.org/ */
  119. 0x55, 0x41, 0x2d, 0x50, 0x72, 0x6f, 0x66, 0x69, /* UA-Profi */
  120. 0x6c, 0x65, 0x2f, 0x54, 0x72, 0x61, 0x6e, 0x73, /* le/Trans */
  121. 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x75, 0x61, 0x74, /* port/uat */
  122. 0x63, 0x70, 0x2d, 0x75, 0x61, 0x73, 0x63, 0x2d, /* cp-uasc- */
  123. 0x75, 0x61, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, /* uabinary */
  124. 0x00 /* . */
  125. };
  126. UA_Byte csr_msg_buf[] = {
  127. 0x01, 0x01, 0x9a, 0x02, 0x00, 0x00, /* <XX>.. */ // <X> = sessionID
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
  129. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* ........ */
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
  131. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* ........ */
  132. 0x00, 0x00, 0x00, 0x00 /* .... */
  133. };
  134. UA_Byte asr_msg_buf[] = {
  135. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, /* ...... */
  136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 /* ...... */
  137. };
  138. UA_ByteString scm_msg = { sizeof(scm_msg_buf), scm_msg_buf };
  139. UA_ByteString rsp_msg = { sizeof(rsp_msg_buf), rsp_msg_buf };
  140. UA_ByteString* sf_msg_gb[] = { &scm_msg, &rsp_msg }; // servicefault
  141. UA_ByteString gep_msg = { sizeof(gep_msg_buf), gep_msg_buf };
  142. UA_ByteString* gep_msg_gb[] = { &scm_msg, &rsp_msg, &gep_msg }; //getendpoint response
  143. UA_ByteString csr_msg = { sizeof(csr_msg_buf), csr_msg_buf };
  144. UA_ByteString* csr_msg_gb[] = { &scm_msg, &rsp_msg, &csr_msg }; // create session response
  145. UA_ByteString asr_msg = { sizeof(asr_msg_buf), asr_msg_buf };
  146. UA_ByteString* asr_msg_gb[] = { &scm_msg, &rsp_msg, &asr_msg }; // activate session response
  147. UA_Int32 myProcess(TL_Connection* connection, const UA_ByteString* msg) {
  148. switch (*((UA_Int32*) &(msg->data[0]))) { // compare first four bytes
  149. case 0x464c4548: // HELF
  150. connection->writerCallback(connection, (const UA_ByteString**) &ack_msg_gb, 1);
  151. break;
  152. case 0x464e504f: // OPNF
  153. connection->writerCallback(connection, (const UA_ByteString**) &opn_msg_gb, 1);
  154. break;
  155. case 0x464f4c43: // CLOF
  156. connection->connectionState = CONNECTIONSTATE_CLOSE;
  157. break;
  158. case 0x4647534d: // MSGF
  159. switch (*((UA_Int16*) &(msg->data[26]))) {
  160. case 428: // GetEndpointsRequest
  161. #pragma GCC diagnostic push
  162. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  163. printf("server_run - GetEndpointsRequest\n");
  164. *(UA_Int32*) (&scm_msg_buf[4]) = sizeof(scm_msg_buf) + sizeof(rsp_msg_buf) + sizeof(gep_msg_buf);
  165. *(UA_Int16*) (&rsp_msg_buf[2]) = *(UA_Int16*) (&msg->data[26]) + 3;
  166. *(UA_Int32*) (&rsp_msg_buf[16]) = UA_STATUSCODE_GOOD;
  167. connection->writerCallback(connection, (const UA_ByteString**) &gep_msg_gb, 3);
  168. break;
  169. case 461: // CreateSessionRequest
  170. printf("server_run - CreateSessionRequest\n");
  171. *(UA_Int32*) (&scm_msg_buf[4]) = sizeof(scm_msg_buf) + sizeof(rsp_msg_buf) + sizeof(csr_msg_buf);
  172. *(UA_Int16*) (&rsp_msg_buf[2]) = *(UA_Int16*) (&msg->data[26]) + 3;
  173. *(UA_Int32*) (&rsp_msg_buf[16]) = UA_STATUSCODE_GOOD;
  174. connection->writerCallback(connection, (const UA_ByteString**) &csr_msg_gb, 3);
  175. break;
  176. case 467: // FIXME: ActivateSessionRequest
  177. printf("server_run - ActivateSessionRequest\n");
  178. *(UA_Int32*) (&scm_msg_buf[4]) = sizeof(scm_msg_buf) + sizeof(rsp_msg_buf) + sizeof(asr_msg_buf);
  179. *(UA_Int16*) (&rsp_msg_buf[2]) = *(UA_Int16*) (&msg->data[26]) + 3;
  180. *(UA_Int32*) (&rsp_msg_buf[16]) = UA_STATUSCODE_GOOD;
  181. connection->writerCallback(connection, (const UA_ByteString**) &asr_msg_gb, 3);
  182. break;
  183. default: // unknown service - send a simple response header of type UA_SERVICEFAULT
  184. *(UA_Int32*) (&scm_msg_buf[4]) = sizeof(scm_msg_buf) + sizeof(rsp_msg_buf);
  185. *(UA_Int16*) (&rsp_msg_buf[2]) = UA_SERVICEFAULT_NS0 + 2; // encodingBinary
  186. *(UA_Int32*) (&rsp_msg_buf[16]) = UA_STATUSCODE_BADNOTIMPLEMENTED;
  187. #pragma GCC diagnostic pop
  188. printf("server_run - unknown request %d\n", *((UA_Int16*) &(msg->data[26])));
  189. connection->writerCallback(connection, (const UA_ByteString**) &sf_msg_gb, 2);
  190. break;
  191. }
  192. break;
  193. }
  194. return UA_SUCCESS;
  195. }
  196. /** write message provided in the gather buffers to a tcp transport layer connection */
  197. UA_Int32 myWriter(struct TL_Connection const * c, UA_ByteString const * const * gather_buf, UA_UInt32 gather_len) {
  198. struct iovec iov[gather_len];
  199. UA_UInt32 total_len = 0;
  200. for(UA_UInt32 i=0;i<gather_len;i++) {
  201. iov[i].iov_base = gather_buf[i]->data;
  202. iov[i].iov_len = gather_buf[i]->length;
  203. total_len += gather_buf[i]->length;
  204. }
  205. struct msghdr message;
  206. message.msg_name = UA_NULL;
  207. message.msg_namelen = 0;
  208. message.msg_iov = iov;
  209. message.msg_iovlen = gather_len;
  210. message.msg_control = UA_NULL;
  211. message.msg_controllen = 0;
  212. message.msg_flags = 0;
  213. UA_UInt32 nWritten = 0;
  214. while (nWritten < total_len) {
  215. int n=0;
  216. do {
  217. DBG_VERBOSE(printf("myWriter - enter write with %d bytes to write\n",total_len));
  218. n = sendmsg(c->connectionHandle, &message, 0);
  219. DBG_VERBOSE(printf("myWriter - leave write with n=%d,errno={%d,%s}\n",n,(n>0)?0:errno,(n>0)?"":strerror(errno)));
  220. } while (n == -1L && errno == EINTR);
  221. if (n >= 0) {
  222. nWritten += n;
  223. break;
  224. // TODO: handle incompletely send messages
  225. } else {
  226. break;
  227. // TODO: error handling
  228. }
  229. }
  230. return UA_SUCCESS;
  231. }
  232. void server_run() {
  233. TL_Connection connection;
  234. connection.connectionState = CONNECTIONSTATE_CLOSED;
  235. connection.writerCallback = (TL_Writer) myWriter;
  236. connection.localConf.maxChunkCount = 1;
  237. connection.localConf.maxMessageSize = BUFFER_SIZE;
  238. connection.localConf.protocolVersion = 0;
  239. connection.localConf.recvBufferSize = BUFFER_SIZE;
  240. connection.localConf.recvBufferSize = BUFFER_SIZE;
  241. UA_ByteString slMessage = { -1, UA_NULL };
  242. int optval = 1;
  243. int sockfd, newsockfd, portno, clilen;
  244. char buffer[BUFFER_SIZE];
  245. struct sockaddr_in serv_addr, cli_addr;
  246. int n;
  247. /* First call to socket() function */
  248. sockfd = socket(PF_INET, SOCK_STREAM, 0);
  249. if (sockfd < 0) {
  250. perror("ERROR opening socket");
  251. exit(1);
  252. }
  253. /* Initialize socket structure */
  254. memset((void *) &serv_addr, 0, sizeof(serv_addr));
  255. portno = PORT;
  256. serv_addr.sin_family = AF_INET;
  257. serv_addr.sin_addr.s_addr = INADDR_ANY;
  258. serv_addr.sin_port = htons(portno);
  259. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval) == -1) {
  260. perror("setsockopt");
  261. exit(1);
  262. }
  263. /* Now bind the host address using bind() call.*/
  264. if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
  265. perror("ERROR on binding");
  266. exit(1);
  267. }
  268. /* Now start listening for the clients, here process will
  269. * go in sleep mode and will wait for the incoming connection
  270. */
  271. while (listen(sockfd, 5) != -1) {
  272. clilen = sizeof(cli_addr);
  273. /* Accept actual connection from the client */
  274. newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen);
  275. if (newsockfd < 0) {
  276. perror("ERROR on accept");
  277. exit(1);
  278. }
  279. printf("server_run - connection accepted: %i, state: %i\n", newsockfd, connection.connectionState);
  280. connection.connectionHandle = newsockfd;
  281. do {
  282. memset(buffer, 0, BUFFER_SIZE);
  283. n = read(newsockfd, buffer, BUFFER_SIZE);
  284. if (n > 0) {
  285. slMessage.data = (UA_Byte*) buffer;
  286. slMessage.length = n;
  287. DBG(printf("server_run - received=%d\n",n));
  288. myProcess(&connection, &slMessage);
  289. } else if (n <= 0) {
  290. perror("ERROR reading from socket1");
  291. connection.connectionState = CONNECTIONSTATE_CLOSE;
  292. }
  293. } while(connection.connectionState != CONNECTIONSTATE_CLOSE);
  294. shutdown(newsockfd,2);
  295. close(newsockfd);
  296. connection.connectionState = CONNECTIONSTATE_CLOSED;
  297. }
  298. shutdown(sockfd,2);
  299. close(sockfd);
  300. }
  301. #endif