aboutsummaryrefslogtreecommitdiffstats
path: root/src/server_register.c
blob: e1defd04707d91c332a0bd0fa6f899e9637622a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * @file  server_register.c
 * @brief OPC UA Server that registers with a Local Discovery Server.
 *
 * This program runs an OPC UA server configured with security and periodically
 * registers itself with a remote LDS using the RegisterServer2 service. It
 * uses separate certificate pairs for the server and for the client connection
 * to the LDS. On shutdown, it deregisters from the LDS.
 */

#include "common.h"

#include <open62541/client.h>
#include <open62541/client_config_default.h>
#include <open62541/plugin/log_stdout.h>
#include <open62541/server.h>
#include <open62541/server_config_default.h>

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

UA_Boolean running = true;

static void
stopHandler (int sign)
{
  UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  running = false;
}

/* ========================================================================
 * Main
 * ======================================================================== */

int
main (int argc, char **argv)
{
  signal (SIGINT, stopHandler);
  signal (SIGTERM, stopHandler);

  if (argc < 11)
    {
      UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
                    "Usage: %s\n"
                    "  <port> <applicationUri>\n"
                    "  <server-certificate.der> <server-private-key.der>\n"
                    "  <client-certificate.der> <client-private-key.der>\n"
                    "  <discovery-server-endpoint>\n"
                    "  <register-interval-seconds>\n"
                    "  <security-mode> <security-policy>\n"
                    "  [<trustlist1.der>, ...]\n"
                    "\n"
                    "Security modes : None, Sign, SignAndEncrypt\n"
                    "Security policies: None, Basic256Sha256, "
                    "Aes256_Sha256_RsaPss,\n"
                    "                   Aes128_Sha256_RsaOaep, ECC_nistP256",
                    argv[0]);
      return EXIT_FAILURE;
    }

  UA_UInt16 port = (UA_UInt16)atoi (argv[1]);
  const char *applicationUri = argv[2];
  const char *clientCertPath = argv[5];
  const char *clientKeyPath = argv[6];
  const char *discoveryEndpoint = argv[7];
  int registerInterval = atoi (argv[8]);

  UA_MessageSecurityMode securityMode = parseSecurityMode (argv[9]);
  if (securityMode == UA_MESSAGESECURITYMODE_INVALID)
    {
      UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
                    "Unknown security mode: %s", argv[9]);
      return EXIT_FAILURE;
    }

  const char *securityPolicyUri = resolveSecurityPolicyUri (argv[10]);
  if (!securityPolicyUri)
    {
      UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
                    "Unknown security policy: %s", argv[10]);
      return EXIT_FAILURE;
    }

  size_t trustSize = (argc > 11) ? (size_t)argc - 11 : 0;

  UA_StatusCode retval;
  UA_Server *server = createSecureServer (
      port, applicationUri, argv[3], argv[4], argv + 11, trustSize, &retval);
  if (!server)
    return EXIT_FAILURE;

  UA_ServerConfig *serverConfig = UA_Server_getConfig (server);

  serverConfig->applicationDescription.applicationType
      = UA_APPLICATIONTYPE_SERVER;

  UA_Server_run_startup (server);

  /* UA_Server_registerDiscovery consumes (clears) the client config,
     so a fresh zero-initialized config is needed for every call. */
  UA_ClientConfig clientConfig;
  memset (&clientConfig, 0, sizeof (UA_ClientConfig));
  retval = createSecureClientConfig (
      &clientConfig, applicationUri, clientCertPath, clientKeyPath, argv + 11,
      trustSize, securityMode, securityPolicyUri);
  if (retval != UA_STATUSCODE_GOOD)
    {
      UA_Server_run_shutdown (server);
      UA_Server_delete (server);
      return EXIT_FAILURE;
    }

  UA_String discoveryUrl = UA_STRING_ALLOC (discoveryEndpoint);
  retval = UA_Server_registerDiscovery (server, &clientConfig, discoveryUrl,
                                        UA_STRING_NULL);
  UA_String_clear (&discoveryUrl);
  if (retval != UA_STATUSCODE_GOOD)
    UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                    "Initial register failed: %s",
                    UA_StatusCode_name (retval));

  time_t lastRegister = time (NULL);

  while (running)
    {
      UA_Server_run_iterate (server, true);

      time_t now = time (NULL);
      if (now - lastRegister >= registerInterval)
        {
          memset (&clientConfig, 0, sizeof (UA_ClientConfig));
          retval = createSecureClientConfig (
              &clientConfig, applicationUri, clientCertPath, clientKeyPath,
              argv + 11, trustSize, securityMode, securityPolicyUri);
          if (retval == UA_STATUSCODE_GOOD)
            {
              UA_String reregUrl = UA_STRING_ALLOC (discoveryEndpoint);
              retval = UA_Server_registerDiscovery (server, &clientConfig,
                                                    reregUrl, UA_STRING_NULL);
              UA_String_clear (&reregUrl);
              if (retval != UA_STATUSCODE_GOOD)
                UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                                "Re-register failed: %s",
                                UA_StatusCode_name (retval));
            }
          lastRegister = now;
        }
    }

  memset (&clientConfig, 0, sizeof (UA_ClientConfig));
  retval = createSecureClientConfig (
      &clientConfig, applicationUri, clientCertPath, clientKeyPath, argv + 11,
      trustSize, securityMode, securityPolicyUri);
  if (retval == UA_STATUSCODE_GOOD)
    {
      UA_String deregUrl = UA_STRING_ALLOC (discoveryEndpoint);
      retval = UA_Server_deregisterDiscovery (server, &clientConfig, deregUrl);
      UA_String_clear (&deregUrl);
      if (retval != UA_STATUSCODE_GOOD)
        UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                      "Could not unregister from discovery server: %s",
                      UA_StatusCode_name (retval));
    }

  UA_Server_run_shutdown (server);
  UA_Server_delete (server);
  return EXIT_SUCCESS;
}