Skip to content

Commit

Permalink
Merge branch '1.4' into ctt_session_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfr authored Oct 25, 2023
2 parents 8cd8351 + 461c45f commit e802857
Show file tree
Hide file tree
Showing 18 changed files with 1,419 additions and 1,529 deletions.
10 changes: 4 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ set(exported_headers ${PROJECT_BINARY_DIR}/src_generated/open62541/config.h
${PROJECT_SOURCE_DIR}/include/open62541/plugin/nodestore.h
${PROJECT_SOURCE_DIR}/include/open62541/plugin/historydatabase.h
${PROJECT_SOURCE_DIR}/include/open62541/server_pubsub.h
${PROJECT_SOURCE_DIR}/include/open62541/server.h
${PROJECT_SOURCE_DIR}/include/open62541/client.h
${PROJECT_SOURCE_DIR}/include/open62541/server.h
${PROJECT_SOURCE_DIR}/include/open62541/client_highlevel.h
${PROJECT_SOURCE_DIR}/include/open62541/client_subscriptions.h
${PROJECT_SOURCE_DIR}/include/open62541/client_highlevel_async.h)
Expand Down Expand Up @@ -822,7 +822,6 @@ set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
${PROJECT_SOURCE_DIR}/src/server/ua_server_config.c
${PROJECT_SOURCE_DIR}/src/server/ua_server_binary.c
${PROJECT_SOURCE_DIR}/src/server/ua_server_utils.c
${PROJECT_SOURCE_DIR}/src/server/ua_server_discovery.c
${PROJECT_SOURCE_DIR}/src/server/ua_server_async.c
${PROJECT_SOURCE_DIR}/src/server/ua_services_view.c
${PROJECT_SOURCE_DIR}/src/server/ua_services_method.c
Expand All @@ -833,7 +832,6 @@ set(lib_sources ${PROJECT_SOURCE_DIR}/src/ua_types.c
${PROJECT_SOURCE_DIR}/src/server/ua_services_monitoreditem.c
${PROJECT_SOURCE_DIR}/src/server/ua_services_securechannel.c
${PROJECT_SOURCE_DIR}/src/server/ua_services_nodemanagement.c
${PROJECT_SOURCE_DIR}/src/server/ua_services_discovery_multicast.c
# pubsub
${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_networkmessage.c
${PROJECT_SOURCE_DIR}/src/pubsub/ua_pubsub_eventloop.c
Expand Down Expand Up @@ -936,7 +934,7 @@ if(UA_ENABLE_DISCOVERY_MULTICAST)
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.h
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.h)
list(APPEND lib_sources
${PROJECT_SOURCE_DIR}/src/server/ua_server_discovery_mdns.c
${PROJECT_SOURCE_DIR}/src/server/ua_discovery_mdns.c
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.c
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.c
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/sdtxt.c
Expand Down Expand Up @@ -1073,8 +1071,8 @@ list(INSERT plugin_sources 0
endif()

if(UA_ENABLE_DISCOVERY)
list(APPEND lib_headers ${PROJECT_SOURCE_DIR}/src/server/ua_discovery_manager.h)
list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_discovery_manager.c)
list(APPEND lib_headers ${PROJECT_SOURCE_DIR}/src/server/ua_discovery.h)
list(APPEND lib_sources ${PROJECT_SOURCE_DIR}/src/server/ua_discovery.c)
endif()

if(UA_ENABLE_NODESETLOADER)
Expand Down
59 changes: 26 additions & 33 deletions examples/discovery/server_multicast.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ serverOnNetworkCallback(const UA_ServerOnNetwork *serverOnNetwork, UA_Boolean is
* @param discoveryServerUrl The discovery url from the remote server
* @return The endpoint description (which needs to be freed) or NULL
*/
static
UA_EndpointDescription *getRegisterEndpointFromServer(const char *discoveryServerUrl) {
static UA_EndpointDescription *
getRegisterEndpointFromServer(const char *discoveryServerUrl) {
UA_Client *client = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(client));
UA_EndpointDescription *endpointArray = NULL;
Expand Down Expand Up @@ -161,19 +161,19 @@ static UA_ByteString loadFile(const char *const path) {
* @param argv from the main method
* @return NULL or the initialized non-connected client
*/
static
UA_Client *getRegisterClient(UA_EndpointDescription *endpointRegister, int argc, char **argv) {
static UA_StatusCode
getRegisterClient(UA_ClientConfig *cc, UA_EndpointDescription *endpointRegister, int argc, char **argv) {
UA_ClientConfig_setDefault(cc);
if(endpointRegister->securityMode == UA_MESSAGESECURITYMODE_NONE) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Using LDS endpoint with security None");
UA_Client *client = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(client));
return client;
return UA_STATUSCODE_GOOD;
}

#ifdef UA_ENABLE_ENCRYPTION
if(endpointRegister->securityMode == UA_MESSAGESECURITYMODE_SIGN) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"LDS endpoint which only supports Sign is currently not supported");
return NULL;
return UA_STATUSCODE_BADINTERNALERROR;
}

UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
Expand All @@ -185,7 +185,7 @@ UA_Client *getRegisterClient(UA_EndpointDescription *endpointRegister, int argc,
"The required arguments are "
"<client-certificate.der> <client-private-key.der> "
"[<trustlist1.crl>, ...]");
return NULL;
return UA_STATUSCODE_BADINTERNALERROR;
}

/* Load certificate and key */
Expand All @@ -203,8 +203,6 @@ UA_Client *getRegisterClient(UA_EndpointDescription *endpointRegister, int argc,
trustList[trustListCount] = loadFile(argv[trustListCount + 3]);

/* Secure client initialization */
UA_Client *clientRegister = UA_Client_new();
UA_ClientConfig *cc = UA_Client_getConfig(clientRegister);
UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
trustList, trustListSize,
revocationList, revocationListSize);
Expand All @@ -214,11 +212,9 @@ UA_Client *getRegisterClient(UA_EndpointDescription *endpointRegister, int argc,
UA_ByteString_clear(&privateKey);
for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++)
UA_ByteString_clear(&trustList[deleteCount]);

return clientRegister;
#else
return NULL;
#endif

return UA_STATUSCODE_GOOD;
}

int main(int argc, char **argv) {
Expand Down Expand Up @@ -300,49 +296,46 @@ int main(int argc, char **argv) {
return EXIT_FAILURE;
}

UA_Client *clientRegister = getRegisterClient(endpointRegister, argc, argv);
if(!clientRegister) {
UA_ClientConfig cc;
memset(&cc, 0, sizeof(UA_ClientConfig));
retval = getRegisterClient(&cc, endpointRegister, argc, argv);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
"Could not create the client for remote registering");
UA_ClientConfig_clear(&cc);
UA_Server_run_shutdown(server);
UA_Server_delete(server);
UA_EndpointDescription_delete(endpointRegister);
return EXIT_FAILURE;
}

/* Connect the client */
char *endpointUrl = (char*)UA_malloc(endpointRegister->endpointUrl.length + 1);
memcpy(endpointUrl, endpointRegister->endpointUrl.data, endpointRegister->endpointUrl.length);
endpointUrl[endpointRegister->endpointUrl.length] = 0;
UA_EndpointDescription_delete(endpointRegister);
retval = UA_Server_addPeriodicServerRegisterCallback(server, clientRegister, endpointUrl,
10 * 60 * 1000, 500, NULL);
/* Register the server */
retval = UA_Server_registerDiscovery(server, &cc, endpointRegister->endpointUrl, UA_STRING_NULL);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Could not create periodic job for server register. StatusCode %s",
UA_StatusCode_name(retval));
UA_free(endpointUrl);
UA_Client_disconnect(clientRegister);
UA_Client_delete(clientRegister);
UA_Server_run_shutdown(server);
UA_Server_delete(server);
UA_EndpointDescription_delete(endpointRegister);
return EXIT_FAILURE;
}

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

UA_Server_run_shutdown(server);

// UNregister the server from the discovery server.
retval = UA_Server_unregister_discovery(server, clientRegister);
/* Deregister the server from the discovery server */
memset(&cc, 0, sizeof(UA_ClientConfig));
retval = getRegisterClient(&cc, endpointRegister, argc, argv);
retval |= UA_Server_deregisterDiscovery(server, &cc, endpointRegister->endpointUrl);
if(retval != UA_STATUSCODE_GOOD)
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Could not unregister server from discovery server. "
"StatusCode %s", UA_StatusCode_name(retval));

UA_free(endpointUrl);
UA_Client_disconnect(clientRegister);
UA_Client_delete(clientRegister);
UA_Server_delete(server);
UA_EndpointDescription_delete(endpointRegister);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
41 changes: 15 additions & 26 deletions examples/discovery/server_register.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,50 +97,39 @@ int main(int argc, char **argv) {
myIntegerName, UA_NODEID_NULL, attr, dateDataSource,
&myInteger, NULL);

UA_Client *clientRegister = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(clientRegister));
UA_Server_run_startup(server);

// register server
UA_ClientConfig cc;
memset(&cc, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefault(&cc);

// periodic server register after 10 Minutes, delay first register for 500ms
UA_UInt64 callbackId;
UA_StatusCode retval =
UA_Server_addPeriodicServerRegisterCallback(server, clientRegister, DISCOVERY_SERVER_ENDPOINT,
10 * 60 * 1000, 500, &callbackId);
// UA_StatusCode retval = UA_Server_addPeriodicServerRegisterJob(server,
// "opc.tcp://localhost:4840", 10*60*1000, 500, NULL);
UA_Server_registerDiscovery(server, &cc,
UA_STRING(DISCOVERY_SERVER_ENDPOINT), UA_STRING_NULL);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Could not create periodic job for server register. StatusCode %s",
UA_StatusCode_name(retval));
UA_Client_disconnect(clientRegister);
UA_Client_delete(clientRegister);
UA_Server_delete(server);
return EXIT_FAILURE;
}

retval = UA_Server_run(server, &running);

if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Could not start the server. StatusCode %s",
UA_StatusCode_name(retval));
UA_Client_disconnect(clientRegister);
UA_Client_delete(clientRegister);
UA_Server_delete(server);
return EXIT_FAILURE;
}
while(running)
UA_Server_run_iterate(server, true);

// Unregister the server from the discovery server.
retval = UA_Server_unregister_discovery(server, clientRegister);
memset(&cc, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefault(&cc);
retval = UA_Server_deregisterDiscovery(server, &cc,
UA_STRING(DISCOVERY_SERVER_ENDPOINT));
//retval = UA_Server_unregister_discovery(server, "opc.tcp://localhost:4840" );
if(retval != UA_STATUSCODE_GOOD)
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Could not unregister server from discovery server. StatusCode %s",
UA_StatusCode_name(retval));

UA_Server_removeCallback(server, callbackId);

UA_Client_disconnect(clientRegister);
UA_Client_delete(clientRegister);
UA_Server_run_shutdown(server);
UA_Server_delete(server);
return EXIT_SUCCESS;
}
54 changes: 25 additions & 29 deletions examples/server_ctt.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ static UA_UsernamePasswordLogin usernamePasswords[2] = {

static const UA_NodeId baseDataVariableType = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_BASEDATAVARIABLETYPE}};
static const UA_NodeId accessDenied = {1, UA_NODEIDTYPE_NUMERIC, {1337}};
static UA_Client *ldsClientRegister;
static UA_UInt64 ldsCallbackId;

/* Custom AccessControl policy that disallows access to one specific node */
static UA_Byte
Expand Down Expand Up @@ -853,24 +851,30 @@ setInformationModel(UA_Server *server) {
#endif
}

static UA_Boolean hasRegistered = false;

static void
notifyState(UA_Server *server, UA_LifecycleState state) {
#ifdef UA_ENABLE_DISCOVERY
static void configureLdsRegistration(UA_Server *server){
ldsClientRegister = UA_Client_new();
UA_ClientConfig_setDefault(UA_Client_getConfig(ldsClientRegister));

// periodic server register after 1 Minutes, delay first register for 500ms
UA_StatusCode retval =
UA_Server_addPeriodicServerRegisterCallback(server, ldsClientRegister, "opc.tcp://localhost:4840",
60 * 1000, 500, &ldsCallbackId);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
"Could not create periodic job for server register. StatusCode %s",
UA_StatusCode_name(retval));
UA_Client_disconnect(ldsClientRegister);
UA_Client_delete(ldsClientRegister);
if(state == UA_LIFECYCLESTATE_STARTED && !hasRegistered) {
UA_ClientConfig cc;
memset(&cc, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefault(&cc);
UA_Server_registerDiscovery(server, &cc,
UA_STRING("opc.tcp://localhost:4840"),
UA_STRING_NULL);
hasRegistered = true;
}
if(state != UA_LIFECYCLESTATE_STARTED && hasRegistered) {
UA_ClientConfig cc;
memset(&cc, 0, sizeof(UA_ClientConfig));
UA_ClientConfig_setDefault(&cc);
UA_Server_deregisterDiscovery(server, &cc,
UA_STRING("opc.tcp://localhost:4840"));
hasRegistered = false;
}
}
#endif
}

#ifdef UA_ENABLE_ENCRYPTION
static void
Expand Down Expand Up @@ -1356,8 +1360,8 @@ int main(int argc, char **argv) {
#endif /* UA_ENABLE_ENCRYPTION */

/* Limit the number of SecureChannels and Sessions */
config->maxSecureChannels = 40;
config->maxSessions = 10;
config->maxSecureChannels = 60;
config->maxSessions = 50;
config->maxSessionTimeout = 10 * 60 * 10000; /* 10 minutes */

/* Revolve the SecureChannel token every 300 seconds */
Expand Down Expand Up @@ -1398,24 +1402,16 @@ int main(int argc, char **argv) {
config->applicationDescription.applicationUri =
UA_String_fromChars("urn:open62541.server.application");

/* Lifecycle config */
config->shutdownDelay = 5000.0; /* 5s */
config->notifyLifecycleState = notifyState;

setInformationModel(server);

#ifdef UA_ENABLE_DISCOVERY

configureLdsRegistration(server);

#endif

/* run server */
res = UA_Server_runUntilInterrupt(server);

cleanup:
UA_Server_removeCallback(server, ldsCallbackId);
UA_Server_unregister_discovery(server, ldsClientRegister);
UA_Client_disconnect(ldsClientRegister);
UA_Client_delete(ldsClientRegister);
UA_Server_delete(server);

UA_ByteString_clear(&certificate);
Expand Down
Loading

0 comments on commit e802857

Please sign in to comment.