opcuaServer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <stdio.h>
  6. #ifndef WIN32
  7. #include <sys/mman.h>
  8. #include <sys/wait.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11. #else
  12. #include "winsock2.h"
  13. #endif
  14. #include <sys/types.h>
  15. #include <fcntl.h>
  16. #include <signal.h>
  17. #include <errno.h> // errno, EINTR
  18. #include "server/ua_server.h"
  19. #include "logger_stdout.h"
  20. #include "networklayer_tcp.h"
  21. UA_Boolean running = UA_TRUE;
  22. void stopHandler(int sign) {
  23. running = UA_FALSE;
  24. }
  25. void serverCallback(UA_Server *server) {
  26. printf("does whatever servers do\n");
  27. }
  28. UA_ByteString loadCertificate() {
  29. UA_ByteString certificate = UA_STRING_NULL;
  30. FILE *fp = UA_NULL;
  31. //FIXME: a potiential bug of locating the certificate, we need to get the path from the server's config
  32. fp=fopen("localhost.der", "rb");
  33. if(!fp) {
  34. errno = 0; // otherwise we think sth went wrong on the tcp socket level
  35. return certificate;
  36. }
  37. fseek(fp, 0, SEEK_END);
  38. certificate.length = ftell(fp);
  39. UA_alloc((void**)&certificate.data, certificate.length*sizeof(UA_Byte));
  40. fseek(fp, 0, SEEK_SET);
  41. if(fread(certificate.data, sizeof(UA_Byte), certificate.length, fp) < (size_t)certificate.length)
  42. UA_ByteString_deleteMembers(&certificate); // error reading the cert
  43. fclose(fp);
  44. return certificate;
  45. }
  46. int main(int argc, char** argv) {
  47. signal(SIGINT, stopHandler); /* catches ctrl-c */
  48. UA_Server server;
  49. UA_String endpointUrl;
  50. UA_String_copycstring("no endpoint url",&endpointUrl);
  51. UA_Server_init(&server, &endpointUrl);
  52. Logger_Stdout_init(&server.logger);
  53. server.serverCertificate = loadCertificate();
  54. NetworklayerTCP* nl;
  55. NetworklayerTCP_new(&nl, UA_ConnectionConfig_standard, 16664);
  56. struct timeval callback_interval = {1, 0}; // 1 second
  57. UA_Int32 retval = NetworkLayerTCP_run(nl, &server, callback_interval,
  58. serverCallback, &running);
  59. NetworklayerTCP_delete(nl);
  60. UA_Server_deleteMembers(&server);
  61. return retval == UA_SUCCESS ? 0 : retval;
  62. }