Skip to content

Commit

Permalink
refactor(server): Only use logging-pointer instead of inline structure
Browse files Browse the repository at this point in the history
We could not adjust all pointers when moving the config "into" the
server.
  • Loading branch information
jpfr committed Nov 10, 2023
1 parent 6f5a557 commit 1076104
Show file tree
Hide file tree
Showing 43 changed files with 656 additions and 680 deletions.
6 changes: 3 additions & 3 deletions examples/server_ctt.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ enableNoneSecurityPolicy(UA_ServerConfig *config) {
UA_StatusCode retval = UA_ServerConfig_addEndpoint(config, UA_SECURITY_POLICY_NONE_URI,
UA_MESSAGESECURITYMODE_NONE);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Failed to add SecurityPolicy#None to the endpoint list.");
}
config->securityPolicyNoneDiscoveryOnly = false;
Expand All @@ -928,7 +928,7 @@ static void
enableBasic128SecurityPolicy(UA_ServerConfig *config) {
UA_StatusCode retval = UA_ServerConfig_addSecurityPolicyBasic128Rsa15(config, &certificate, &privateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic128Rsa15 with error code %s",
UA_StatusCode_name(retval));
return;
Expand All @@ -943,7 +943,7 @@ static void
enableBasic256SecurityPolicy(UA_ServerConfig *config) {
UA_StatusCode retval = UA_ServerConfig_addSecurityPolicyBasic256(config, &certificate, &privateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic256 with error code %s",
UA_StatusCode_name(retval));
return;
Expand Down
4 changes: 3 additions & 1 deletion examples/server_loglevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ int main(int argc, char **argv) {
}
#endif

UA_Logger logger = UA_Log_Stdout_withLevel( log_level );

UA_Server *server = UA_Server_new();
UA_ServerConfig *config = UA_Server_getConfig(server);
UA_ServerConfig_setDefault(config);
config->logger = UA_Log_Stdout_withLevel( log_level );
config->logging = &logger;

/* Some data */
UA_StatusCode retval;
Expand Down
4 changes: 1 addition & 3 deletions include/open62541/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ typedef struct {
* The :ref:`tutorials` provide a good starting point for this. */

struct UA_ServerConfig {
UA_Logger logger; /* logger is deprecated but still supported at this time.
Use logging pointer instead. */
UA_Logger *logging; /* If NULL and "logger" is set, make this point to "logger" */
const UA_Logger *logging; /* Plugin for log output */
void *context; /* Used to attach custom data to a server config. This can
* then be retrieved e.g. in a callback that forwards a
* pointer to the server. */
Expand Down
12 changes: 6 additions & 6 deletions plugins/ua_accesscontrol_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ UA_AccessControl_default(UA_ServerConfig *config,
const UA_ByteString *userTokenPolicyUri,
size_t usernamePasswordLoginSize,
const UA_UsernamePasswordLogin *usernamePasswordLogin) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_SERVER,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_SERVER,
"AccessControl: Unconfigured AccessControl. Users have all permissions.");
UA_AccessControl *ac = &config->accessControl;

Expand Down Expand Up @@ -336,7 +336,7 @@ UA_AccessControl_default(UA_ServerConfig *config,
/* Allow anonymous? */
context->allowAnonymous = allowAnonymous;
if(allowAnonymous) {
UA_LOG_INFO(&config->logger, UA_LOGCATEGORY_SERVER,
UA_LOG_INFO(config->logging, UA_LOGCATEGORY_SERVER,
"AccessControl: Anonymous login is enabled");
}

Expand All @@ -360,7 +360,7 @@ UA_AccessControl_default(UA_ServerConfig *config,
if(config->securityPoliciesSize > 0)
numOfPolcies = config->securityPoliciesSize;
else {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_SERVER,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_SERVER,
"No security policies defined for the secure channel.");
return UA_STATUSCODE_BADINTERNALERROR;
}
Expand All @@ -382,7 +382,7 @@ UA_AccessControl_default(UA_ServerConfig *config,
ac->userTokenPoliciesSize = policies * numOfPolcies;

if(policies == 0) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_SERVER,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_SERVER,
"No allowed policies set.");
return UA_STATUSCODE_GOOD;
}
Expand All @@ -408,7 +408,7 @@ UA_AccessControl_default(UA_ServerConfig *config,
ac->userTokenPolicies[policies].policyId = UA_STRING_ALLOC(CERTIFICATE_POLICY);
#if UA_LOGLEVEL <= 400
if(UA_ByteString_equal(utpUri, &UA_SECURITY_POLICY_NONE_URI)) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_SERVER,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_SERVER,
"x509 Certificate Authentication configured, "
"but no encrypting SecurityPolicy. "
"This can leak credentials on the network.");
Expand All @@ -424,7 +424,7 @@ UA_AccessControl_default(UA_ServerConfig *config,
ac->userTokenPolicies[policies].policyId = UA_STRING_ALLOC(USERNAME_POLICY);
#if UA_LOGLEVEL <= 400
if(UA_ByteString_equal(utpUri, &UA_SECURITY_POLICY_NONE_URI)) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_SERVER,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_SERVER,
"Username/Password Authentication configured, "
"but no encrypting SecurityPolicy. "
"This can leak credentials on the network.");
Expand Down
52 changes: 25 additions & 27 deletions plugins/ua_config_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void
shutdownServer(UA_Server *server, void *context) {
struct InterruptContext *ic = (struct InterruptContext*)context;
UA_ServerConfig *config = UA_Server_getConfig(ic->server);
UA_LOG_INFO(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_INFO(config->logging, UA_LOGCATEGORY_USERLAND,
"Stopping the server");
ic->running = false;
}
Expand All @@ -81,13 +81,13 @@ interruptServer(UA_InterruptManager *im, uintptr_t interruptHandle,
UA_ServerConfig *config = UA_Server_getConfig(ic->server);

if(config->shutdownDelay <= 0.0) {
UA_LOG_INFO(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_INFO(config->logging, UA_LOGCATEGORY_USERLAND,
"Received SIGINT interrupt. Stopping the server.");
ic->running = false;
return;
}

UA_LOG_INFO(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_INFO(config->logging, UA_LOGCATEGORY_USERLAND,
"Received SIGINT interrupt. Stopping the server in %.2fs.",
config->shutdownDelay / 1000.0);

Expand Down Expand Up @@ -122,7 +122,7 @@ UA_Server_runUntilInterrupt(UA_Server *server) {
es = es->next;
}
if(!es) {
UA_LOG_ERROR(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_ERROR(config->logging, UA_LOGCATEGORY_USERLAND,
"No Interrupt EventSource configured");
return UA_STATUSCODE_BADINTERNALERROR;
}
Expand All @@ -136,7 +136,7 @@ UA_Server_runUntilInterrupt(UA_Server *server) {
im->registerInterrupt(im, SIGINT, &UA_KEYVALUEMAP_NULL,
interruptServer, &ic);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_ERROR(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_ERROR(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not register the interrupt with status code %s",
UA_StatusCode_name(retval));
return retval;
Expand Down Expand Up @@ -267,14 +267,12 @@ setDefaultConfig(UA_ServerConfig *conf, UA_UInt16 portNumber) {
UA_Nodestore_HashMap(&conf->nodestore);

/* Logging */
if(!conf->logger.log)
conf->logger = UA_Log_Stdout_withLevel(UA_LOGLEVEL_INFO);
if(conf->logging == NULL)
conf->logging = &conf->logger;
conf->logging = UA_Log_Stdout;

/* EventLoop */
if(conf->eventLoop == NULL) {
conf->eventLoop = UA_EventLoop_new_POSIX(&conf->logger);
conf->eventLoop = UA_EventLoop_new_POSIX(conf->logging);
if(conf->eventLoop == NULL)
return UA_STATUSCODE_BADOUTOFMEMORY;

Expand Down Expand Up @@ -305,7 +303,7 @@ setDefaultConfig(UA_ServerConfig *conf, UA_UInt16 portNumber) {
if(im) {
conf->eventLoop->registerEventSource(conf->eventLoop, &im->eventSource);
} else {
UA_LOG_WARNING(&conf->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(conf->logging, UA_LOGCATEGORY_USERLAND,
"Cannot create the Interrupt Manager (only relevant if used)");
}
#ifdef UA_ENABLE_MQTT
Expand Down Expand Up @@ -377,11 +375,11 @@ setDefaultConfig(UA_ServerConfig *conf, UA_UInt16 portNumber) {
char serverUrlBuffer[2][512];

if(portNumber == 0) {
UA_LOG_WARNING(&conf->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(conf->logging, UA_LOGCATEGORY_USERLAND,
"Cannot set the ServerUrl with a zero port");
} else {
if(conf->serverUrlsSize > 0) {
UA_LOG_WARNING(&conf->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(conf->logging, UA_LOGCATEGORY_USERLAND,
"ServerUrls already set. Overriding.");
UA_Array_delete(conf->serverUrls, conf->serverUrlsSize,
&UA_TYPES[UA_TYPES_STRING]);
Expand Down Expand Up @@ -540,7 +538,7 @@ UA_ServerConfig_addSecurityPolicyNone(UA_ServerConfig *config,
localCertificate = *certificate;
UA_StatusCode retval =
UA_SecurityPolicy_None(&config->securityPolicies[config->securityPoliciesSize],
localCertificate, &config->logger);
localCertificate, config->logging);
if(retval != UA_STATUSCODE_GOOD) {
if(config->securityPoliciesSize == 0) {
UA_free(config->securityPolicies);
Expand Down Expand Up @@ -697,7 +695,7 @@ UA_ServerConfig_addSecurityPolicyBasic128Rsa15(UA_ServerConfig *config,
localPrivateKey = *privateKey;
UA_StatusCode retval =
UA_SecurityPolicy_Basic128Rsa15(&config->securityPolicies[config->securityPoliciesSize],
localCertificate, localPrivateKey, &config->logger);
localCertificate, localPrivateKey, config->logging);
if(retval != UA_STATUSCODE_GOOD) {
if(config->securityPoliciesSize == 0) {
UA_free(config->securityPolicies);
Expand Down Expand Up @@ -731,7 +729,7 @@ UA_ServerConfig_addSecurityPolicyBasic256(UA_ServerConfig *config,
localPrivateKey = *privateKey;
UA_StatusCode retval =
UA_SecurityPolicy_Basic256(&config->securityPolicies[config->securityPoliciesSize],
localCertificate, localPrivateKey, &config->logger);
localCertificate, localPrivateKey, config->logging);
if(retval != UA_STATUSCODE_GOOD) {
if(config->securityPoliciesSize == 0) {
UA_free(config->securityPolicies);
Expand Down Expand Up @@ -765,7 +763,7 @@ UA_ServerConfig_addSecurityPolicyBasic256Sha256(UA_ServerConfig *config,
localPrivateKey = *privateKey;
UA_StatusCode retval =
UA_SecurityPolicy_Basic256Sha256(&config->securityPolicies[config->securityPoliciesSize],
localCertificate, localPrivateKey, &config->logger);
localCertificate, localPrivateKey, config->logging);
if(retval != UA_STATUSCODE_GOOD) {
if(config->securityPoliciesSize == 0) {
UA_free(config->securityPolicies);
Expand Down Expand Up @@ -799,7 +797,7 @@ UA_ServerConfig_addSecurityPolicyAes128Sha256RsaOaep(UA_ServerConfig *config,
localPrivateKey = *privateKey;
UA_StatusCode retval =
UA_SecurityPolicy_Aes128Sha256RsaOaep(&config->securityPolicies[config->securityPoliciesSize],
localCertificate, localPrivateKey, &config->logger);
localCertificate, localPrivateKey, config->logging);
if(retval != UA_STATUSCODE_GOOD) {
if(config->securityPoliciesSize == 0) {
UA_free(config->securityPolicies);
Expand Down Expand Up @@ -833,7 +831,7 @@ UA_ServerConfig_addSecurityPolicyAes256Sha256RsaPss(UA_ServerConfig *config,
localPrivateKey = *privateKey;
UA_StatusCode retval =
UA_SecurityPolicy_Aes256Sha256RsaPss(&config->securityPolicies[config->securityPoliciesSize],
localCertificate, localPrivateKey, &config->logger);
localCertificate, localPrivateKey, config->logging);
if(retval != UA_STATUSCODE_GOOD) {
if(config->securityPoliciesSize == 0) {
UA_free(config->securityPolicies);
Expand Down Expand Up @@ -861,42 +859,42 @@ UA_ServerConfig_addAllSecurityPolicies(UA_ServerConfig *config,

UA_StatusCode retval = UA_ServerConfig_addSecurityPolicyNone(config, &localCertificate);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#None with error code %s",
UA_StatusCode_name(retval));
}

retval = UA_ServerConfig_addSecurityPolicyBasic128Rsa15(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic128Rsa15 with error code %s",
UA_StatusCode_name(retval));
}

retval = UA_ServerConfig_addSecurityPolicyBasic256(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic256 with error code %s",
UA_StatusCode_name(retval));
}

retval = UA_ServerConfig_addSecurityPolicyAes256Sha256RsaPss(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Aes256Sha256RsaPss with error code %s",
UA_StatusCode_name(retval));
}

retval = UA_ServerConfig_addSecurityPolicyBasic256Sha256(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic256Sha256 with error code %s",
UA_StatusCode_name(retval));
}

retval = UA_ServerConfig_addSecurityPolicyAes128Sha256RsaOaep(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Aes128Sha256RsaOaep with error code %s",
UA_StatusCode_name(retval));
}
Expand All @@ -919,21 +917,21 @@ UA_ServerConfig_addAllSecureSecurityPolicies(UA_ServerConfig *config,

UA_StatusCode retval = UA_ServerConfig_addSecurityPolicyBasic256Sha256(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic256Sha256 with error code %s",
UA_StatusCode_name(retval));
}

retval = UA_ServerConfig_addSecurityPolicyAes128Sha256RsaOaep(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Aes128Sha256RsaOaep with error code %s",
UA_StatusCode_name(retval));
}

retval = UA_ServerConfig_addSecurityPolicyAes256Sha256RsaPss(config, &localCertificate, &localPrivateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Aes256Sha256RsaPss with error code %s",
UA_StatusCode_name(retval));
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/ua_config_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ PARSE_JSON(SecurityPolciesField) {
}

if(certificate.length == 0 || privateKey.length == 0) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Certificate and PrivateKey must be set for every policy.");
if(policy.length > 0)
UA_String_clear(&policy);
Expand All @@ -714,33 +714,33 @@ PARSE_JSON(SecurityPolciesField) {
} else if(UA_String_equal(&policy, &basic128Rsa15uri)) {
retval = UA_ServerConfig_addSecurityPolicyBasic128Rsa15(config, &certificate, &privateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic128Rsa15 with error code %s",
UA_StatusCode_name(retval));
}
} else if(UA_String_equal(&policy, &basic256uri)) {
retval = UA_ServerConfig_addSecurityPolicyBasic256(config, &certificate, &privateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic256 with error code %s",
UA_StatusCode_name(retval));
}
} else if(UA_String_equal(&policy, &basic256Sha256uri)) {
retval = UA_ServerConfig_addSecurityPolicyBasic256Sha256(config, &certificate, &privateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Basic256Sha256 with error code %s",
UA_StatusCode_name(retval));
}
} else if(UA_String_equal(&policy, &aes128sha256rsaoaepuri)) {
retval = UA_ServerConfig_addSecurityPolicyAes128Sha256RsaOaep(config, &certificate, &privateKey);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND,
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND,
"Could not add SecurityPolicy#Aes128Sha256RsaOaep with error code %s",
UA_StatusCode_name(retval));
}
} else {
UA_LOG_WARNING(&config->logger, UA_LOGCATEGORY_USERLAND, "Unknown Security Policy.");
UA_LOG_WARNING(config->logging, UA_LOGCATEGORY_USERLAND, "Unknown Security Policy.");
}

/* Add all Endpoints */
Expand Down
Loading

0 comments on commit 1076104

Please sign in to comment.