-
Notifications
You must be signed in to change notification settings - Fork 48
/
registerserver.c
280 lines (251 loc) · 9.95 KB
/
registerserver.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Copyright (c) 1996-2024, OPC Foundation. All rights reserved.
The source code in this file is covered under a dual - license scenario :
- RCL: for OPC Foundation members in good - standing
- GPL V2: everybody else
RCL license terms accompanied with this source code.See http ://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code.See http ://opcfoundation.org/License/GPLv2
This source code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/* system includes */
#include <time.h>
/* uastack includes */
#include <opcua_serverstub.h>
#include <opcua_string.h>
#include <opcua_core.h>
#include <opcua_datetime.h>
#include <opcua_memory.h>
#include <opcua_securechannel_types.h>
/* local includes */
#include "config.h"
#include "settings.h"
#include "ualds.h"
#ifdef HAVE_HDS
#include "zeroconf.h"
#include "findserversonnetwork.h"
#endif
/* local platform includes */
#include <platform.h>
#include <log.h>
/** RegisterServer Service implementation.
* @param hEndpoint OPC UA Endpoint handle.
* @param hContext Service context.
* @param ppRequest Pointer to decoded service request structure.
* @param pRequestType The service type.
*
* @return OpcUa_StatusCode
*/
OpcUa_StatusCode ualds_registerserver(
OpcUa_Endpoint hEndpoint,
OpcUa_Handle hContext,
OpcUa_Void **ppRequest,
OpcUa_EncodeableType *pRequestType)
{
OpcUa_Endpoint_SecurityPolicyConfiguration policy;
memset(&policy, 0, sizeof(OpcUa_Endpoint_SecurityPolicyConfiguration));
if (OpcUa_IsGood(OpcUa_Endpoint_GetMessageSecureChannelSecurityPolicy(hEndpoint, hContext, &policy)) &&
policy.uMessageSecurityModes == OPCUA_SECURECHANNEL_MESSAGESECURITYMODE_NONE)
{
return OpcUa_BadServiceUnsupported;
}
OpcUa_RegisterServerRequest *pRequest;
OpcUa_RegisterServerResponse *pResponse;
OpcUa_EncodeableType *pResponseType = 0;
OpcUa_StatusCode uStatus = OpcUa_Good;
int i;
int numServers = 0;
int bExists = 0;
char szServerUri[UALDS_CONF_MAX_URI_LENGTH];
char *pszServerUri;
UALDS_UNUSED(pRequestType);
OpcUa_ReturnErrorIfArgumentNull(ppRequest);
pRequest = *ppRequest;
uStatus = OpcUa_Endpoint_BeginSendResponse(
hEndpoint,
hContext,
(OpcUa_Void**)&pResponse,
&pResponseType);
OpcUa_ReturnErrorIfBad(uStatus);
if (pResponse)
{
OpcUa_Mutex_Lock(g_mutex);
OpcUa_Boolean bIsOnline = OpcUa_False;
if (OpcUa_IsGood(uStatus))
{
/* check for valid parameters */
if (OpcUa_String_StrLen(&pRequest->Server.ServerUri) == 0 ||
( OpcUa_String_StrnCmp(&pRequest->Server.ServerUri, OpcUa_String_FromCString("urn:"), 4, OpcUa_False) != 0 &&
OpcUa_String_StrnCmp(&pRequest->Server.ServerUri, OpcUa_String_FromCString("http://"), 7, OpcUa_False) != 0))
{
ualds_log(UALDS_LOG_ERR, "ServerUri '%s' is invalid (needs to start with 'urn:' or 'http://').", OpcUa_String_GetRawString(&pRequest->Server.ServerUri));
uStatus = OpcUa_BadServerUriInvalid;
}
else if (pRequest->Server.NoOfServerNames <= 0 ||
OpcUa_String_StrLen(&pRequest->Server.ServerNames[0].Text) == 0)
{
ualds_log(UALDS_LOG_ERR, "No ServerNames have been provided.");
uStatus = OpcUa_BadServerNameMissing;
}
else if (pRequest->Server.NoOfDiscoveryUrls <= 0 ||
OpcUa_String_StrLen(&pRequest->Server.DiscoveryUrls[0]) == 0)
{
ualds_log(UALDS_LOG_ERR, "No DiscoveryUrls have been provided.");
uStatus = OpcUa_BadDiscoveryUrlMissing;
}
}
if (OpcUa_IsGood(uStatus))
{
switch (pRequest->Server.ServerType)
{
case OpcUa_ApplicationType_Server:
case OpcUa_ApplicationType_ClientAndServer:
case OpcUa_ApplicationType_DiscoveryServer:
break;
default:
ualds_log(UALDS_LOG_ERR, "Invalid ServerType %i.", pRequest->Server.ServerType);
uStatus = OpcUa_BadInvalidArgument;
break;
}
}
if (OpcUa_IsGood(uStatus))
{
/* get pointer to the request's server uri, this makes coding easier */
pszServerUri = OpcUa_String_GetRawString(&pRequest->Server.ServerUri);
/* ignore IsOnline if SemaphoreFilePath is set */
if (OpcUa_String_StrLen(&pRequest->Server.SemaphoreFilePath) > 0)
{
if (ualds_platform_fileexists(OpcUa_String_GetRawString(&pRequest->Server.SemaphoreFilePath)))
{
bIsOnline = OpcUa_True;
}
else
{
uStatus = OpcUa_BadSempahoreFileMissing;
}
}
else
{
bIsOnline = pRequest->Server.IsOnline;
}
if (bIsOnline != OpcUa_False)
{
ualds_log(UALDS_LOG_INFO, "Registering server %s.", pszServerUri);
ualds_settings_begingroup("RegisteredServers");
ualds_settings_beginreadarray("Servers", &numServers);
/* check if requested server is already registered */
for (i=0; i<numServers; i++)
{
ualds_settings_setarrayindex(i);
ualds_settings_readstring("ServerUri", szServerUri, sizeof(szServerUri));
if (strcmp(szServerUri, pszServerUri) == 0)
{
bExists = 1;
break;
}
}
ualds_settings_endarray();
if (bExists == 0)
{
/* add new server */
ualds_settings_beginwritearray("Servers", numServers+1);
ualds_settings_setarrayindex(numServers);
ualds_settings_writestring("ServerUri", pszServerUri);
ualds_settings_endarray();
}
ualds_settings_endgroup();
/* update server info */
ualds_settings_begingroup(pszServerUri);
ualds_settings_writestring("ProductUri", OpcUa_String_GetRawString(&pRequest->Server.ProductUri));
ualds_settings_beginwritearray("ServerNames", pRequest->Server.NoOfServerNames);
for (i=0; i<pRequest->Server.NoOfServerNames; i++)
{
ualds_settings_setarrayindex(i);
ualds_settings_writestring("Locale", OpcUa_String_GetRawString(&pRequest->Server.ServerNames[i].Locale));
ualds_settings_writestring("Text", OpcUa_String_GetRawString(&pRequest->Server.ServerNames[i].Text));
}
ualds_settings_endarray();
ualds_settings_writeint("ServerType", pRequest->Server.ServerType);
ualds_settings_writestring("GatewayServerUri", OpcUa_String_GetRawString(&pRequest->Server.GatewayServerUri));
ualds_settings_beginwritearray("DiscoveryUrls", pRequest->Server.NoOfDiscoveryUrls);
for (i=0; i<pRequest->Server.NoOfDiscoveryUrls; i++)
{
ualds_settings_setarrayindex(i);
ualds_settings_writestring("Url", OpcUa_String_GetRawString(&pRequest->Server.DiscoveryUrls[i]));
}
ualds_settings_endarray();
ualds_settings_writestring("SemaphoreFilePath", OpcUa_String_GetRawString(&pRequest->Server.SemaphoreFilePath));
ualds_settings_writetime_t("UpdateTime", time(0));
ualds_settings_endgroup();
#ifdef HAVE_HDS
if (bExists == 0)
{
if (g_bEnableZeroconf == 0)
{
ualds_zeroconf_register_offline(pszServerUri);
}
}
#endif
ualds_settings_flush();
}
else
{
/* unregister */
ualds_log(UALDS_LOG_INFO, "Unregistering server %s.", pszServerUri);
#ifdef HAVE_HDS
if (g_bEnableZeroconf == 0)
{
ualds_zeroconf_unregister_offline(pszServerUri);
}
#endif
ualds_settings_removegroup(pszServerUri);
ualds_settings_flush();
ualds_expirationcheck();
}
}
OpcUa_Mutex_Unlock(g_mutex);
// update on network
if (OpcUa_IsGood(uStatus))
{
if (bIsOnline != OpcUa_False)
{
#ifdef HAVE_HDS
if (bExists == 0)
{
if (g_bEnableZeroconf)
{
ualds_zeroconf_addRegistration(pszServerUri);
}
}
#endif
}
else
{
#ifdef HAVE_HDS
if (g_bEnableZeroconf)
{
ualds_zeroconf_removeRegistration(pszServerUri);
}
#endif
}
}
UALDS_BUILDRESPONSEHEADER;
/* Send response */
OpcUa_Endpoint_EndSendResponse(
hEndpoint,
&hContext,
uStatus,
pResponse,
pResponseType);
/* free response */
OpcUa_RegisterServerResponse_Clear(pResponse);
OpcUa_Free(pResponse);
/* we have send a response, either with good or bad status, this does not matter.
* we must return Good now, otherwise the stack will send an error message.
*/
uStatus = OpcUa_Good;
}
return uStatus;
}
/** @} */