forked from OPCFoundation/UA-LDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindservers.c
236 lines (202 loc) · 8.23 KB
/
findservers.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
/* Copyright (c) 1996-2018, 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_memory.h>
#include <opcua_string.h>
#include <opcua_core.h>
#include <opcua_datetime.h>
/* local includes */
#include "config.h"
#include "ualds.h"
#include "settings.h"
/* local platform includes */
#include <platform.h>
#include <log.h>
/** FindServer 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_findservers(
OpcUa_Endpoint hEndpoint,
OpcUa_Handle hContext,
OpcUa_Void **ppRequest,
OpcUa_EncodeableType *pRequestType)
{
OpcUa_FindServersRequest *pRequest;
OpcUa_FindServersResponse *pResponse;
OpcUa_EncodeableType *pResponseType = 0;
OpcUa_StatusCode uStatus = OpcUa_Good;
char **szUriArray = 0;
char szTmpUrl[UALDS_CONF_MAX_URI_LENGTH];
int i, j;
char szHostname[50];
int numServers = 0;
int numServerNames = 0;
int numDiscoveryUrls = 0;
int tmpInt;
UALDS_UNUSED(pRequestType);
OpcUa_ReturnErrorIfArgumentNull(ppRequest);
pRequest = *ppRequest;
ualds_platform_getfqhostname(szHostname, 50);
uStatus = OpcUa_Endpoint_BeginSendResponse(
hEndpoint,
hContext,
(OpcUa_Void**)&pResponse,
&pResponseType);
OpcUa_ReturnErrorIfBad(uStatus);
if ( pResponse )
{
OpcUa_Mutex_Lock(g_mutex);
ualds_expirationcheck();
ualds_settings_begingroup("RegisteredServers");
ualds_settings_beginreadarray("Servers", &numServers);
if (numServers > 0)
{
szUriArray = OpcUa_Alloc(numServers * sizeof(char*));
if (szUriArray)
{
OpcUa_MemSet(szUriArray, 0, numServers * sizeof(char*));
tmpInt = 0;
for (i=0; i<numServers; i++)
{
OpcUa_Boolean bFilteredOut = OpcUa_False;
ualds_settings_setarrayindex(i);
ualds_settings_readstring("ServerUri", szTmpUrl, UALDS_CONF_MAX_URI_LENGTH);
/* filter by ServerUri */
if (pRequest->NoOfServerUris > 0)
{
bFilteredOut = OpcUa_True;
for (j = 0; j < pRequest->NoOfServerUris; j++)
{
if (OpcUa_String_StrnCmp(&pRequest->ServerUris[j], OpcUa_String_FromCString(szTmpUrl), OPCUA_STRING_LENDONTCARE, OpcUa_False) == 0)
{
bFilteredOut = OpcUa_False;
break;
}
}
}
if (bFilteredOut == OpcUa_False)
{
szUriArray[tmpInt] = OpcUa_Alloc(UALDS_CONF_MAX_URI_LENGTH);
if (szUriArray[tmpInt])
{
strlcpy(szUriArray[tmpInt], szTmpUrl, UALDS_CONF_MAX_URI_LENGTH);
}
tmpInt++;
}
}
numServers = tmpInt;
}
else
{
UALDS_BUILDRESPONSEHEADER;
pResponse->ResponseHeader.ServiceResult = OpcUa_BadOutOfMemory;
/* Send response */
OpcUa_Endpoint_EndSendResponse(
hEndpoint,
&hContext,
OpcUa_Good,
pResponse,
pResponseType);
ualds_settings_endarray();
ualds_settings_endgroup();
OpcUa_Mutex_Unlock(g_mutex);
return OpcUa_Good;
}
}
ualds_settings_endarray();
ualds_settings_endgroup();
pResponse->NoOfServers = numServers;
pResponse->Servers = OpcUa_Alloc(sizeof(OpcUa_ApplicationDescription) * pResponse->NoOfServers);
if (pResponse->Servers)
{
for (i=0; i<pResponse->NoOfServers; i++)
{
OpcUa_ApplicationDescription_Initialize(&pResponse->Servers[i]);
if (szUriArray[i] == 0) continue;
ualds_settings_begingroup(szUriArray[i]);
ualds_settings_readuastring("ProductUri", &pResponse->Servers[i].ProductUri);
ualds_settings_beginreadarray("ServerNames", &numServerNames);
if (numServerNames > 0)
{
ualds_settings_setarrayindex(0);
ualds_settings_readuastring("Locale", &pResponse->Servers[i].ApplicationName.Locale);
ualds_settings_readuastring("Text", &pResponse->Servers[i].ApplicationName.Text);
}
ualds_settings_endarray();
ualds_settings_readint("ServerType", &tmpInt);
pResponse->Servers[i].ApplicationType = (OpcUa_ApplicationType)tmpInt;
ualds_settings_readuastring("GatewayServerUri", &pResponse->Servers[i].GatewayServerUri);
ualds_settings_beginreadarray("DiscoveryUrls", &numDiscoveryUrls);
if (numDiscoveryUrls > 0)
{
pResponse->Servers[i].NoOfDiscoveryUrls = numDiscoveryUrls;
pResponse->Servers[i].DiscoveryUrls = OpcUa_Alloc(sizeof(OpcUa_String) * numDiscoveryUrls);
if (pResponse->Servers[i].DiscoveryUrls)
{
for (j=0; j<numDiscoveryUrls; j++)
{
ualds_settings_setarrayindex(j);
ualds_settings_readstring("Url", szTmpUrl, UALDS_CONF_MAX_URI_LENGTH);
replace_string(szTmpUrl, UALDS_CONF_MAX_URI_LENGTH, "[gethostname]", szHostname);
OpcUa_String_Initialize(&pResponse->Servers[i].DiscoveryUrls[j]);
OpcUa_String_AttachCopy(&pResponse->Servers[i].DiscoveryUrls[j], szTmpUrl);
}
}
}
ualds_settings_endarray();
ualds_settings_endgroup();
/* finally replace [gethostname] in ServerUri and copy to ApplicationUri */
replace_string(szUriArray[i], UALDS_CONF_MAX_URI_LENGTH, "[gethostname]", szHostname);
OpcUa_String_AttachCopy(&pResponse->Servers[i].ApplicationUri, szUriArray[i]);
}
}
else
{
uStatus = OpcUa_BadOutOfMemory;
}
OpcUa_Mutex_Unlock(g_mutex);
UALDS_BUILDRESPONSEHEADER;
/* Send response */
OpcUa_Endpoint_EndSendResponse(
hEndpoint,
&hContext,
uStatus,
pResponse,
pResponseType);
/* free response */
OpcUa_FindServersResponse_Clear(pResponse);
OpcUa_Free(pResponse);
/* cleanup */
if (szUriArray)
{
for (i=0; i<numServers; i++)
{
if (szUriArray[i]) OpcUa_Free(szUriArray[i]);
}
OpcUa_Free(szUriArray);
}
/* 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;
}
/** @} */