ua_server.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014-2017 (c) Florian Palm
  7. * Copyright 2015-2016 (c) Sten Grüner
  8. * Copyright 2015-2016 (c) Chris Iatrou
  9. * Copyright 2015 (c) LEvertz
  10. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  11. * Copyright 2016 (c) Julian Grothoff
  12. * Copyright 2016-2017 (c) Stefan Profanter, fortiss GmbH
  13. * Copyright 2016 (c) Lorenz Haas
  14. * Copyright 2017 (c) frax2222
  15. * Copyright 2017 (c) Mark Giraud, Fraunhofer IOSB
  16. * Copyright 2018 (c) Hilscher Gesellschaft für Systemautomation mbH (Author: Martin Lang)
  17. */
  18. #include "ua_server_internal.h"
  19. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  20. #include "ua_pubsub_ns0.h"
  21. #endif
  22. #ifdef UA_ENABLE_SUBSCRIPTIONS
  23. #include "ua_subscription.h"
  24. #endif
  25. #ifdef UA_ENABLE_VALGRIND_INTERACTIVE
  26. #include <valgrind/memcheck.h>
  27. #endif
  28. /**********************/
  29. /* Namespace Handling */
  30. /**********************/
  31. void setupNs1Uri(UA_Server *server) {
  32. if (!server->namespaces[1].data) {
  33. UA_String_copy(&server->config.applicationDescription.applicationUri, &server->namespaces[1]);
  34. }
  35. }
  36. UA_UInt16 addNamespace(UA_Server *server, const UA_String name) {
  37. /* ensure that the uri for ns1 is set up from the app description */
  38. setupNs1Uri(server);
  39. /* Check if the namespace already exists in the server's namespace array */
  40. for(UA_UInt16 i = 0; i < server->namespacesSize; ++i) {
  41. if(UA_String_equal(&name, &server->namespaces[i]))
  42. return i;
  43. }
  44. /* Make the array bigger */
  45. UA_String *newNS = (UA_String*)UA_realloc(server->namespaces,
  46. sizeof(UA_String) * (server->namespacesSize + 1));
  47. if(!newNS)
  48. return 0;
  49. server->namespaces = newNS;
  50. /* Copy the namespace string */
  51. UA_StatusCode retval = UA_String_copy(&name, &server->namespaces[server->namespacesSize]);
  52. if(retval != UA_STATUSCODE_GOOD)
  53. return 0;
  54. /* Announce the change (otherwise, the array appears unchanged) */
  55. ++server->namespacesSize;
  56. return (UA_UInt16)(server->namespacesSize - 1);
  57. }
  58. UA_UInt16 UA_Server_addNamespace(UA_Server *server, const char* name) {
  59. /* Override const attribute to get string (dirty hack) */
  60. UA_String nameString;
  61. nameString.length = strlen(name);
  62. nameString.data = (UA_Byte*)(uintptr_t)name;
  63. return addNamespace(server, nameString);
  64. }
  65. UA_ServerConfig*
  66. UA_Server_getConfig(UA_Server *server)
  67. {
  68. if(!server)
  69. return NULL;
  70. return &server->config;
  71. }
  72. UA_StatusCode
  73. UA_Server_getNamespaceByName(UA_Server *server, const UA_String namespaceUri,
  74. size_t* foundIndex) {
  75. /* ensure that the uri for ns1 is set up from the app description */
  76. setupNs1Uri(server);
  77. for(size_t idx = 0; idx < server->namespacesSize; idx++) {
  78. if(!UA_String_equal(&server->namespaces[idx], &namespaceUri))
  79. continue;
  80. (*foundIndex) = idx;
  81. return UA_STATUSCODE_GOOD;
  82. }
  83. return UA_STATUSCODE_BADNOTFOUND;
  84. }
  85. UA_StatusCode
  86. UA_Server_forEachChildNodeCall(UA_Server *server, UA_NodeId parentNodeId,
  87. UA_NodeIteratorCallback callback, void *handle) {
  88. const UA_Node *parent = UA_Nodestore_getNode(server->nsCtx, &parentNodeId);
  89. if(!parent)
  90. return UA_STATUSCODE_BADNODEIDINVALID;
  91. /* TODO: We need to do an ugly copy of the references array since users may
  92. * delete references from within the callback. In single-threaded mode this
  93. * changes the same node we point at here. In multi-threaded mode, this
  94. * creates a new copy as nodes are truly immutable.
  95. * The callback could remove a node via the regular public API.
  96. * This can remove a member of the nodes-array we iterate over...
  97. * */
  98. UA_Node *parentCopy = UA_Node_copy_alloc(parent);
  99. if(!parentCopy) {
  100. UA_Nodestore_releaseNode(server->nsCtx, parent);
  101. return UA_STATUSCODE_BADUNEXPECTEDERROR;
  102. }
  103. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  104. for(size_t i = parentCopy->referencesSize; i > 0; --i) {
  105. UA_NodeReferenceKind *ref = &parentCopy->references[i - 1];
  106. for(size_t j = 0; j<ref->targetIdsSize; j++) {
  107. retval = callback(ref->targetIds[j].nodeId, ref->isInverse,
  108. ref->referenceTypeId, handle);
  109. if(retval != UA_STATUSCODE_GOOD)
  110. goto cleanup;
  111. }
  112. }
  113. cleanup:
  114. UA_Node_deleteMembers(parentCopy);
  115. UA_free(parentCopy);
  116. UA_Nodestore_releaseNode(server->nsCtx, parent);
  117. return retval;
  118. }
  119. /********************/
  120. /* Server Lifecycle */
  121. /********************/
  122. /* The server needs to be stopped before it can be deleted */
  123. void UA_Server_delete(UA_Server *server) {
  124. /* Delete all internal data */
  125. UA_SecureChannelManager_deleteMembers(&server->secureChannelManager);
  126. UA_SessionManager_deleteMembers(&server->sessionManager);
  127. UA_Array_delete(server->namespaces, server->namespacesSize, &UA_TYPES[UA_TYPES_STRING]);
  128. #ifdef UA_ENABLE_SUBSCRIPTIONS
  129. UA_MonitoredItem *mon, *mon_tmp;
  130. LIST_FOREACH_SAFE(mon, &server->localMonitoredItems, listEntry, mon_tmp) {
  131. LIST_REMOVE(mon, listEntry);
  132. UA_MonitoredItem_delete(server, mon);
  133. }
  134. #endif
  135. #ifdef UA_ENABLE_PUBSUB
  136. UA_PubSubManager_delete(server, &server->pubSubManager);
  137. #endif
  138. #ifdef UA_ENABLE_DISCOVERY
  139. UA_DiscoveryManager_deleteMembers(&server->discoveryManager, server);
  140. #endif
  141. /* Clean up the Admin Session */
  142. UA_Session_deleteMembersCleanup(&server->adminSession, server);
  143. /* Clean up the work queue */
  144. UA_WorkQueue_cleanup(&server->workQueue);
  145. /* Delete the timed work */
  146. UA_Timer_deleteMembers(&server->timer);
  147. /* Clean up the nodestore */
  148. UA_Nodestore_delete(server->nsCtx);
  149. /* Clean up the config */
  150. UA_ServerConfig_clean(&server->config);
  151. /* Delete the server itself */
  152. UA_free(server);
  153. }
  154. /* Recurring cleanup. Removing unused and timed-out channels and sessions */
  155. static void
  156. UA_Server_cleanup(UA_Server *server, void *_) {
  157. UA_DateTime nowMonotonic = UA_DateTime_nowMonotonic();
  158. UA_SessionManager_cleanupTimedOut(&server->sessionManager, nowMonotonic);
  159. UA_SecureChannelManager_cleanupTimedOut(&server->secureChannelManager, nowMonotonic);
  160. #ifdef UA_ENABLE_DISCOVERY
  161. UA_Discovery_cleanupTimedOut(server, nowMonotonic);
  162. #endif
  163. }
  164. /********************/
  165. /* Server Lifecycle */
  166. /********************/
  167. UA_Server *
  168. UA_Server_new() {
  169. /* Allocate the server */
  170. UA_Server *server = (UA_Server *)UA_calloc(1, sizeof(UA_Server));
  171. if(!server)
  172. return NULL;
  173. /* Init start time to zero, the actual start time will be sampled in
  174. * UA_Server_run_startup() */
  175. server->startTime = 0;
  176. /* Set a seed for non-cyptographic randomness */
  177. #ifndef UA_ENABLE_DETERMINISTIC_RNG
  178. UA_random_seed((UA_UInt64)UA_DateTime_now());
  179. #endif
  180. /* Initialize the handling of repeated callbacks */
  181. UA_Timer_init(&server->timer);
  182. UA_WorkQueue_init(&server->workQueue);
  183. /* Initialize the adminSession */
  184. UA_Session_init(&server->adminSession);
  185. server->adminSession.sessionId.identifierType = UA_NODEIDTYPE_GUID;
  186. server->adminSession.sessionId.identifier.guid.data1 = 1;
  187. server->adminSession.validTill = UA_INT64_MAX;
  188. /* Create Namespaces 0 and 1
  189. * Ns1 will be filled later with the uri from the app description */
  190. server->namespaces = (UA_String *)UA_Array_new(2, &UA_TYPES[UA_TYPES_STRING]);
  191. if(!server->namespaces) {
  192. UA_Server_delete(server);
  193. return NULL;
  194. }
  195. server->namespaces[0] = UA_STRING_ALLOC("http://opcfoundation.org/UA/");
  196. server->namespaces[1] = UA_STRING_NULL;
  197. server->namespacesSize = 2;
  198. /* Initialized SecureChannel and Session managers */
  199. UA_SecureChannelManager_init(&server->secureChannelManager, server);
  200. UA_SessionManager_init(&server->sessionManager, server);
  201. /* Add a regular callback for cleanup and maintenance. With a 10s interval. */
  202. UA_Server_addRepeatedCallback(server, (UA_ServerCallback)UA_Server_cleanup, NULL,
  203. 10000.0, NULL);
  204. /* Initialize namespace 0*/
  205. UA_StatusCode retVal = UA_Nodestore_new(&server->nsCtx);
  206. if(retVal != UA_STATUSCODE_GOOD)
  207. goto cleanup;
  208. retVal = UA_Server_initNS0(server);
  209. if(retVal != UA_STATUSCODE_GOOD)
  210. goto cleanup;
  211. /* Build PubSub information model */
  212. #ifdef UA_ENABLE_PUBSUB_INFORMATIONMODEL
  213. UA_Server_initPubSubNS0(server);
  214. #endif
  215. return server;
  216. cleanup:
  217. UA_Server_delete(server);
  218. return NULL;
  219. }
  220. /*******************/
  221. /* Timed Callbacks */
  222. /*******************/
  223. UA_StatusCode
  224. UA_Server_addTimedCallback(UA_Server *server, UA_ServerCallback callback,
  225. void *data, UA_DateTime date, UA_UInt64 *callbackId) {
  226. return UA_Timer_addTimedCallback(&server->timer,
  227. (UA_ApplicationCallback)callback,
  228. server, data, date, callbackId);
  229. }
  230. UA_StatusCode
  231. UA_Server_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  232. void *data, UA_Double interval_ms,
  233. UA_UInt64 *callbackId) {
  234. return UA_Timer_addRepeatedCallback(&server->timer,
  235. (UA_ApplicationCallback)callback,
  236. server, data, interval_ms, callbackId);
  237. }
  238. UA_StatusCode
  239. UA_Server_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  240. UA_Double interval_ms) {
  241. return UA_Timer_changeRepeatedCallbackInterval(&server->timer, callbackId,
  242. interval_ms);
  243. }
  244. void
  245. UA_Server_removeCallback(UA_Server *server, UA_UInt64 callbackId) {
  246. UA_Timer_removeCallback(&server->timer, callbackId);
  247. }
  248. UA_StatusCode UA_EXPORT
  249. UA_Server_updateCertificate(UA_Server *server,
  250. const UA_ByteString *oldCertificate,
  251. const UA_ByteString *newCertificate,
  252. const UA_ByteString *newPrivateKey,
  253. UA_Boolean closeSessions,
  254. UA_Boolean closeSecureChannels) {
  255. if (server == NULL || oldCertificate == NULL
  256. || newCertificate == NULL || newPrivateKey == NULL) {
  257. return UA_STATUSCODE_BADINTERNALERROR;
  258. }
  259. if (closeSessions) {
  260. UA_SessionManager *sm = &server->sessionManager;
  261. session_list_entry *current;
  262. LIST_FOREACH(current, &sm->sessions, pointers) {
  263. if (UA_ByteString_equal(oldCertificate,
  264. &current->session.header.channel->securityPolicy->localCertificate)) {
  265. UA_SessionManager_removeSession(sm, &current->session.header.authenticationToken);
  266. }
  267. }
  268. }
  269. if (closeSecureChannels) {
  270. UA_SecureChannelManager *cm = &server->secureChannelManager;
  271. channel_entry *entry;
  272. TAILQ_FOREACH(entry, &cm->channels, pointers) {
  273. if(UA_ByteString_equal(&entry->channel.securityPolicy->localCertificate, oldCertificate)){
  274. UA_SecureChannelManager_close(cm, entry->channel.securityToken.channelId);
  275. }
  276. }
  277. }
  278. size_t i = 0;
  279. while (i < server->config.endpointsSize) {
  280. UA_EndpointDescription *ed = &server->config.endpoints[i];
  281. if (UA_ByteString_equal(&ed->serverCertificate, oldCertificate)) {
  282. UA_String_deleteMembers(&ed->serverCertificate);
  283. UA_String_copy(newCertificate, &ed->serverCertificate);
  284. UA_SecurityPolicy *sp = UA_SecurityPolicy_getSecurityPolicyByUri(server, &server->config.endpoints[i].securityPolicyUri);
  285. if(!sp)
  286. return UA_STATUSCODE_BADINTERNALERROR;
  287. sp->updateCertificateAndPrivateKey(sp, *newCertificate, *newPrivateKey);
  288. }
  289. i++;
  290. }
  291. return UA_STATUSCODE_GOOD;
  292. }
  293. /***************************/
  294. /* Server lookup functions */
  295. /***************************/
  296. UA_SecurityPolicy *
  297. UA_SecurityPolicy_getSecurityPolicyByUri(const UA_Server *server,
  298. UA_ByteString *securityPolicyUri)
  299. {
  300. for(size_t i = 0; i < server->config.securityPoliciesSize; i++) {
  301. UA_SecurityPolicy *securityPolicyCandidate = &server->config.securityPolicies[i];
  302. if(UA_ByteString_equal(securityPolicyUri,
  303. &securityPolicyCandidate->policyUri))
  304. return securityPolicyCandidate;
  305. }
  306. return NULL;
  307. }
  308. #ifdef UA_ENABLE_ENCRYPTION
  309. /* The local ApplicationURI has to match the certificates of the
  310. * SecurityPolicies */
  311. static void
  312. verifyServerApplicationURI(const UA_Server *server) {
  313. #if UA_LOGLEVEL <= 400
  314. for(size_t i = 0; i < server->config.securityPoliciesSize; i++) {
  315. UA_SecurityPolicy *sp = &server->config.securityPolicies[i];
  316. if(!sp->certificateVerification)
  317. continue;
  318. UA_StatusCode retval =
  319. sp->certificateVerification->
  320. verifyApplicationURI(sp->certificateVerification->context,
  321. &sp->localCertificate,
  322. &server->config.applicationDescription.applicationUri);
  323. if(retval != UA_STATUSCODE_GOOD) {
  324. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  325. "The configured ApplicationURI does not match the URI "
  326. "specified in the certificate for the SecurityPolicy %.*s",
  327. (int)sp->policyUri.length, sp->policyUri.data);
  328. }
  329. }
  330. #endif
  331. }
  332. #endif
  333. /********************/
  334. /* Main Server Loop */
  335. /********************/
  336. #define UA_MAXTIMEOUT 50 /* Max timeout in ms between main-loop iterations */
  337. /* Start: Spin up the workers and the network layer and sample the server's
  338. * start time.
  339. * Iterate: Process repeated callbacks and events in the network layer. This
  340. * part can be driven from an external main-loop in an event-driven
  341. * single-threaded architecture.
  342. * Stop: Stop workers, finish all callbacks, stop the network layer, clean up */
  343. UA_StatusCode
  344. UA_Server_run_startup(UA_Server *server) {
  345. if(server->state > UA_SERVERLIFECYCLE_FRESH)
  346. return UA_STATUSCODE_GOOD;
  347. /* At least one endpoint has to be configured */
  348. if(server->config.endpointsSize == 0) {
  349. UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_SERVER,
  350. "There has to be at least one endpoint.");
  351. }
  352. /* Initialized discovery */
  353. #ifdef UA_ENABLE_DISCOVERY
  354. UA_DiscoveryManager_init(&server->discoveryManager, server);
  355. #endif
  356. /* Does the ApplicationURI match the local certificates? */
  357. #ifdef UA_ENABLE_ENCRYPTION
  358. verifyServerApplicationURI(server);
  359. #endif
  360. /* Sample the start time and set it to the Server object */
  361. server->startTime = UA_DateTime_now();
  362. UA_Variant var;
  363. UA_Variant_init(&var);
  364. UA_Variant_setScalar(&var, &server->startTime, &UA_TYPES[UA_TYPES_DATETIME]);
  365. UA_Server_writeValue(server,
  366. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STARTTIME),
  367. var);
  368. /* Start the networklayers */
  369. UA_StatusCode result = UA_STATUSCODE_GOOD;
  370. for(size_t i = 0; i < server->config.networkLayersSize; ++i) {
  371. UA_ServerNetworkLayer *nl = &server->config.networkLayers[i];
  372. result |= nl->start(nl, &server->config.customHostname);
  373. }
  374. /* Spin up the worker threads */
  375. #ifdef UA_ENABLE_MULTITHREADING
  376. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER,
  377. "Spinning up %u worker thread(s)", server->config.nThreads);
  378. UA_WorkQueue_start(&server->workQueue, server->config.nThreads);
  379. #endif
  380. /* Start the multicast discovery server */
  381. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  382. if(server->config.discovery.mdnsEnable)
  383. startMulticastDiscoveryServer(server);
  384. #endif
  385. server->state = UA_SERVERLIFECYCLE_FRESH;
  386. return result;
  387. }
  388. static void
  389. serverExecuteRepeatedCallback(UA_Server *server, UA_ApplicationCallback cb,
  390. void *callbackApplication, void *data) {
  391. #ifndef UA_ENABLE_MULTITHREADING
  392. cb(callbackApplication, data);
  393. #else
  394. UA_WorkQueue_enqueue(&server->workQueue, cb, callbackApplication, data);
  395. #endif
  396. }
  397. UA_UInt16
  398. UA_Server_run_iterate(UA_Server *server, UA_Boolean waitInternal) {
  399. /* Process repeated work */
  400. UA_DateTime now = UA_DateTime_nowMonotonic();
  401. UA_DateTime nextRepeated = UA_Timer_process(&server->timer, now,
  402. (UA_TimerExecutionCallback)serverExecuteRepeatedCallback, server);
  403. UA_DateTime latest = now + (UA_MAXTIMEOUT * UA_DATETIME_MSEC);
  404. if(nextRepeated > latest)
  405. nextRepeated = latest;
  406. UA_UInt16 timeout = 0;
  407. /* round always to upper value to avoid timeout to be set to 0
  408. * if(nextRepeated - now) < (UA_DATETIME_MSEC/2) */
  409. if(waitInternal)
  410. timeout = (UA_UInt16)(((nextRepeated - now) + (UA_DATETIME_MSEC - 1)) / UA_DATETIME_MSEC);
  411. /* Listen on the networklayer */
  412. for(size_t i = 0; i < server->config.networkLayersSize; ++i) {
  413. UA_ServerNetworkLayer *nl = &server->config.networkLayers[i];
  414. nl->listen(nl, server, timeout);
  415. }
  416. #if defined(UA_ENABLE_DISCOVERY_MULTICAST) && !defined(UA_ENABLE_MULTITHREADING)
  417. if(server->config.discovery.mdnsEnable) {
  418. // TODO multicastNextRepeat does not consider new input data (requests)
  419. // on the socket. It will be handled on the next call. if needed, we
  420. // need to use select with timeout on the multicast socket
  421. // server->mdnsSocket (see example in mdnsd library) on higher level.
  422. UA_DateTime multicastNextRepeat = 0;
  423. UA_StatusCode hasNext =
  424. iterateMulticastDiscoveryServer(server, &multicastNextRepeat, true);
  425. if(hasNext == UA_STATUSCODE_GOOD && multicastNextRepeat < nextRepeated)
  426. nextRepeated = multicastNextRepeat;
  427. }
  428. #endif
  429. #ifndef UA_ENABLE_MULTITHREADING
  430. UA_WorkQueue_manuallyProcessDelayed(&server->workQueue);
  431. #endif
  432. now = UA_DateTime_nowMonotonic();
  433. timeout = 0;
  434. if(nextRepeated > now)
  435. timeout = (UA_UInt16)((nextRepeated - now) / UA_DATETIME_MSEC);
  436. return timeout;
  437. }
  438. UA_StatusCode
  439. UA_Server_run_shutdown(UA_Server *server) {
  440. /* Stop the netowrk layer */
  441. for(size_t i = 0; i < server->config.networkLayersSize; ++i) {
  442. UA_ServerNetworkLayer *nl = &server->config.networkLayers[i];
  443. nl->stop(nl, server);
  444. }
  445. #ifdef UA_ENABLE_MULTITHREADING
  446. /* Shut down the workers */
  447. UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER,
  448. "Shutting down %u worker thread(s)",
  449. (UA_UInt32)server->workQueue.workersSize);
  450. UA_WorkQueue_stop(&server->workQueue);
  451. #endif
  452. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  453. /* Stop multicast discovery */
  454. if(server->config.discovery.mdnsEnable)
  455. stopMulticastDiscoveryServer(server);
  456. #endif
  457. /* Execute all delayed callbacks */
  458. UA_WorkQueue_cleanup(&server->workQueue);
  459. return UA_STATUSCODE_GOOD;
  460. }
  461. UA_StatusCode
  462. UA_Server_run(UA_Server *server, const volatile UA_Boolean *running) {
  463. UA_StatusCode retval = UA_Server_run_startup(server);
  464. if(retval != UA_STATUSCODE_GOOD)
  465. return retval;
  466. #ifdef UA_ENABLE_VALGRIND_INTERACTIVE
  467. size_t loopCount = 0;
  468. #endif
  469. while(*running) {
  470. #ifdef UA_ENABLE_VALGRIND_INTERACTIVE
  471. if(loopCount == 0) {
  472. VALGRIND_DO_LEAK_CHECK;
  473. }
  474. ++loopCount;
  475. loopCount %= UA_VALGRIND_INTERACTIVE_INTERVAL;
  476. #endif
  477. UA_Server_run_iterate(server, true);
  478. }
  479. return UA_Server_run_shutdown(server);
  480. }
  481. #ifdef UA_ENABLE_HISTORIZING
  482. /* Allow insert of historical data */
  483. UA_Boolean
  484. UA_Server_AccessControl_allowHistoryUpdateUpdateData(UA_Server *server,
  485. const UA_NodeId *sessionId, void *sessionContext,
  486. const UA_NodeId *nodeId,
  487. UA_PerformUpdateType performInsertReplace,
  488. const UA_DataValue *value) {
  489. if(server->config.accessControl.allowHistoryUpdateUpdateData &&
  490. !server->config.accessControl.allowHistoryUpdateUpdateData(server, &server->config.accessControl,
  491. sessionId, sessionContext, nodeId,
  492. performInsertReplace, value)) {
  493. return false;
  494. }
  495. return true;
  496. }
  497. /* Allow delete of historical data */
  498. UA_Boolean
  499. UA_Server_AccessControl_allowHistoryUpdateDeleteRawModified(UA_Server *server,
  500. const UA_NodeId *sessionId, void *sessionContext,
  501. const UA_NodeId *nodeId,
  502. UA_DateTime startTimestamp,
  503. UA_DateTime endTimestamp,
  504. bool isDeleteModified) {
  505. if(server->config.accessControl.allowHistoryUpdateDeleteRawModified &&
  506. !server->config.accessControl.allowHistoryUpdateDeleteRawModified(server, &server->config.accessControl,
  507. sessionId, sessionContext, nodeId,
  508. startTimestamp, endTimestamp,
  509. isDeleteModified)) {
  510. return false;
  511. }
  512. return true;
  513. }
  514. #endif /* UA_ENABLE_HISTORIZING */