Skip to content

Commit

Permalink
refactor(server): Keep server backpointer in the DiscoveryManager
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfr committed Oct 3, 2023
1 parent abd717b commit 70f2635
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/server/ua_discovery_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ UA_DiscoveryManager_start(UA_Server *server,
if(res != UA_STATUSCODE_GOOD)
return res;

dm->logging = &server->config.logger;
dm->serverConfig = &server->config;
dm->server = server;

#ifdef UA_ENABLE_DISCOVERY_MULTICAST
if(server->config.mdnsEnabled)
Expand Down
4 changes: 1 addition & 3 deletions src/server/ua_discovery_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ struct UA_DiscoveryManager {

UA_UInt64 discoveryCallbackId;

/* Taken from the server config during startup */
UA_Logger *logging;
const UA_ServerConfig *serverConfig;
UA_Server *server; /* backpointer */

LIST_HEAD(, periodicServerRegisterCallback_entry) periodicServerRegisterCallbacks;
LIST_HEAD(, registeredServer_list_entry) registeredServers;
Expand Down
34 changes: 17 additions & 17 deletions src/server/ua_server_discovery_mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ UA_DiscoveryManager_addEntryToServersOnNetwork(UA_DiscoveryManager *dm,
return UA_STATUSCODE_BADALREADYEXISTS;
}

UA_LOG_DEBUG(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_DEBUG(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Multicast DNS: Add entry to ServersOnNetwork: %s (%*.s)",
fqdnMdnsRecord, (int)serverNameLen, serverName);

Expand All @@ -106,7 +106,7 @@ UA_DiscoveryManager_addEntryToServersOnNetwork(UA_DiscoveryManager *dm,
dm->serverOnNetworkRecordIdCounter++;
if(dm->serverOnNetworkRecordIdCounter == 0)
dm->serverOnNetworkRecordIdLastReset = UA_DateTime_now();
UA_EventLoop *el = server->config.eventLoop;
UA_EventLoop *el = dm->server->config.eventLoop;
listEntry->lastSeen = el->dateTime_nowMonotonic(el);

/* add to hash */
Expand Down Expand Up @@ -144,7 +144,7 @@ getInterfaces(UA_DiscoveryManager *dm) {
/* todo: malloc may fail: return a statuscode */
adapter_addresses = (IP_ADAPTER_ADDRESSES*)UA_malloc(adapter_addresses_buffer_size);
if(!adapter_addresses) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"GetAdaptersAddresses out of memory");
adapter_addresses = NULL;
break;
Expand All @@ -166,7 +166,7 @@ getInterfaces(UA_DiscoveryManager *dm) {
}

/* Unexpected error */
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"GetAdaptersAddresses returned an unexpected error. "
"Not setting mDNS A records.");
UA_free(adapter_addresses);
Expand All @@ -183,7 +183,7 @@ UA_DiscoveryManager_removeEntryFromServersOnNetwork(UA_DiscoveryManager *dm,
const char *fqdnMdnsRecord,
const char *serverName,
size_t serverNameLen) {
UA_LOG_DEBUG(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_DEBUG(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Multicast DNS: Remove entry from ServersOnNetwork: %s (%*.s)",
fqdnMdnsRecord, (int)serverNameLen, serverName);

Expand Down Expand Up @@ -260,7 +260,7 @@ setTxt(UA_DiscoveryManager *dm, const struct resource *r,
if (!entry->pathTmp) {
entry->pathTmp = (char*)UA_malloc(pathLen+1);
if (!entry->pathTmp) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Cannot alloc memory for mDNS srv path");
return;
}
Expand Down Expand Up @@ -320,7 +320,7 @@ setSrv(UA_DiscoveryManager *dm, const struct resource *r,
/* opc.tcp://[servername]:[port][path] */
char *newUrl = (char*)UA_malloc(10 + srvNameLen + 8 + 1);
if (!newUrl) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Cannot allocate char for discovery url. Out of memory.");
return;
}
Expand All @@ -329,7 +329,7 @@ setSrv(UA_DiscoveryManager *dm, const struct resource *r,
r->known.srv.name, r->known.srv.port);

entry->serverOnNetwork.discoveryUrl = UA_String_fromChars(newUrl);
UA_LOG_INFO(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_INFO(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Multicast DNS: found server: %.*s",
(int)entry->serverOnNetwork.discoveryUrl.length,
(char*)entry->serverOnNetwork.discoveryUrl.data);
Expand Down Expand Up @@ -379,7 +379,7 @@ mdns_record_received(const struct resource *r, void *data) {

/* Check that the ttl is positive */
if(r->ttl == 0) {
UA_LOG_INFO(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_INFO(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Multicast DNS: remove server (TTL=0): %.*s",
(int)entry->serverOnNetwork.discoveryUrl.length,
entry->serverOnNetwork.discoveryUrl.data);
Expand All @@ -389,7 +389,7 @@ mdns_record_received(const struct resource *r, void *data) {
}

/* Update lastSeen */
UA_EventLoop *el = server->config.eventLoop;
UA_EventLoop *el = dm->server->config.eventLoop;
entry->lastSeen = el->dateTime_nowMonotonic(el);

/* TXT and SRV are already set */
Expand Down Expand Up @@ -430,7 +430,7 @@ mdns_create_txt(UA_DiscoveryManager *dm, const char *fullServiceDomain, const ch
if(path[0] == '/') {
allocPath = (char*)UA_malloc(pathLen+1);
if(!allocPath) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Cannot alloc memory for txt path");
return;
}
Expand All @@ -439,7 +439,7 @@ mdns_create_txt(UA_DiscoveryManager *dm, const char *fullServiceDomain, const ch
} else {
allocPath = (char*)UA_malloc(pathLen + 2);
if(!allocPath) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"Cannot alloc memory for txt path");
return;
}
Expand Down Expand Up @@ -583,7 +583,7 @@ mdns_set_address_record(UA_DiscoveryManager *dm, const char *fullServiceDomain,
struct ifaddrs *ifaddr;
struct ifaddrs *ifa;
if(getifaddrs(&ifaddr) == -1) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_SERVER,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"getifaddrs returned an unexpected error. Not setting mDNS A records.");
return;
}
Expand Down Expand Up @@ -617,15 +617,15 @@ mdns_set_address_record(UA_DiscoveryManager *dm, const char *fullServiceDomain,
void
mdns_set_address_record(UA_DiscoveryManager *dm, const char *fullServiceDomain,
const char *localDomain) {
if(dm->serverConfig->mdnsIpAddressListSize == 0) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_SERVER,
if(dm->server->config.mdnsIpAddressListSize == 0) {
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_SERVER,
"If UA_HAS_GETIFADDR is false, config.mdnsIpAddressList must be set");
return;
}

for(size_t i=0; i< dm->serverConfig->mdnsIpAddressListSize; i++) {
for(size_t i=0; i< dm->server->config.mdnsIpAddressListSize; i++) {
mdns_set_address_record_if(dm, fullServiceDomain, localDomain,
(char*)&dm->serverConfig->mdnsIpAddressList[i], 4);
(char*)&dm->server->config.mdnsIpAddressList[i], 4);
}
}

Expand Down
36 changes: 18 additions & 18 deletions src/server/ua_services_discovery_multicast.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,19 @@ addMdnsRecordForNetworkLayer(UA_DiscoveryManager *dm, const UA_String *appName,
UA_StatusCode retval =
UA_parseEndpointUrl(discoveryUrl, &hostname, &port, &path);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Server url is invalid: %.*s",
(int)discoveryUrl->length, discoveryUrl->data);
return retval;
}

retval = UA_Discovery_addRecord(dm, appName, &hostname, port,
&path, UA_DISCOVERY_TCP, true,
dm->serverConfig->mdnsConfig.serverCapabilities,
dm->serverConfig->mdnsConfig.serverCapabilitiesSize,
dm->server->config.mdnsConfig.serverCapabilities,
dm->server->config.mdnsConfig.serverCapabilitiesSize,
true);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Cannot add mDNS Record: %s", UA_StatusCode_name(retval));
return retval;
}
Expand Down Expand Up @@ -256,7 +256,7 @@ discovery_createMulticastSocket(UA_Server* server, UA_DiscoveryManager *dm) {
}

if(!dm->cm) {
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"No UDP communication supported");
return;
}
Expand Down Expand Up @@ -296,7 +296,7 @@ discovery_createMulticastSocket(UA_Server* server, UA_DiscoveryManager *dm) {
res = dm->cm->openConnection(dm->cm, &kvm, server, dm,
MulticastDiscoveryRecvCallback);
if(res != UA_STATUSCODE_GOOD)
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Could not create the mdns UDP multicast listen connection");
}

Expand All @@ -306,7 +306,7 @@ discovery_createMulticastSocket(UA_Server* server, UA_DiscoveryManager *dm) {
res = dm->cm->openConnection(dm->cm, &kvm, server, dm,
MulticastDiscoverySendCallback);
if(res != UA_STATUSCODE_GOOD)
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Could not create the mdns UDP multicast send connection");
}
}
Expand Down Expand Up @@ -498,7 +498,7 @@ UA_Discovery_updateMdnsForDiscoveryUrl(UA_DiscoveryManager *dm, const UA_String
UA_StatusCode retval =
UA_parseEndpointUrl(discoveryUrl, &hostname, &port, &path);
if(retval != UA_STATUSCODE_GOOD) {
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Server url invalid: %.*s",
(int)discoveryUrl->length, discoveryUrl->data);
return;
Expand All @@ -509,7 +509,7 @@ UA_Discovery_updateMdnsForDiscoveryUrl(UA_DiscoveryManager *dm, const UA_String
UA_Discovery_removeRecord(dm, serverName, &hostname,
port, updateTxt);
if(removeRetval != UA_STATUSCODE_GOOD)
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Could not remove mDNS record for hostname %.*s.",
(int)serverName->length, serverName->data);
return;
Expand All @@ -528,7 +528,7 @@ UA_Discovery_updateMdnsForDiscoveryUrl(UA_DiscoveryManager *dm, const UA_String
capabilities, capabilitiesSize,
false);
if(addRetval != UA_STATUSCODE_GOOD)
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Could not add mDNS record for hostname %.*s.",
(int)serverName->length, serverName->data);
}
Expand All @@ -554,7 +554,7 @@ UA_Discovery_multicastConflict(char *name, int type, void *arg) {
(void)type;

UA_DiscoveryManager *dm = (UA_DiscoveryManager*) arg;
UA_LOG_ERROR(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_ERROR(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS name conflict detected: "
"'%s' for type %d", name, type);
}
Expand Down Expand Up @@ -668,11 +668,11 @@ UA_Discovery_addRecord(UA_DiscoveryManager *dm, const UA_String *servername,
/* Use a limit for the hostname length to make sure full string fits into 63
* chars (limited by DNS spec) */
if(hostnameLen+servernameLen + 1 > 63) { /* include dash between servername-hostname */
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS: Combination of hostname+servername exceeds "
"maximum of 62 chars. It will be truncated.");
} else if(hostnameLen > 63) {
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS: Hostname length exceeds maximum of 63 chars. "
"It will be truncated.");
}
Expand All @@ -694,7 +694,7 @@ UA_Discovery_addRecord(UA_DiscoveryManager *dm, const UA_String *servername,
if(exists == true)
return UA_STATUSCODE_GOOD;

UA_LOG_INFO(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_INFO(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS: add record for domain: %s", fullServiceDomain);

if(isSelf && dm->selfFqdnMdnsRecord.length == 0) {
Expand Down Expand Up @@ -802,7 +802,7 @@ UA_Discovery_removeRecord(UA_DiscoveryManager *dm, const UA_String *servername,
return UA_STATUSCODE_BADOUTOFRANGE;

if(hostnameLen+servernameLen+1 > 63) { /* include dash between servername-hostname */
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS: Combination of hostname+servername exceeds "
"maximum of 62 chars. It will be truncated.");
}
Expand All @@ -811,7 +811,7 @@ UA_Discovery_removeRecord(UA_DiscoveryManager *dm, const UA_String *servername,
char fullServiceDomain[63 + 24];
createFullServiceDomain(fullServiceDomain, 63+24, servername, hostname);

UA_LOG_INFO(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_INFO(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS: remove record for domain: %s",
fullServiceDomain);

Expand All @@ -826,7 +826,7 @@ UA_Discovery_removeRecord(UA_DiscoveryManager *dm, const UA_String *servername,
mdns_find_record(dm->mdnsDaemon, QTYPE_PTR,
"_opcua-tcp._tcp.local.", fullServiceDomain);
if(!r) {
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS: could not remove record. "
"PTR Record not found for domain: %s", fullServiceDomain);
return UA_STATUSCODE_BADNOTHINGTODO;
Expand All @@ -840,7 +840,7 @@ UA_Discovery_removeRecord(UA_DiscoveryManager *dm, const UA_String *servername,
mdns_record_t *r2 =
mdnsd_get_published(dm->mdnsDaemon, fullServiceDomain);
if(!r2) {
UA_LOG_WARNING(dm->logging, UA_LOGCATEGORY_DISCOVERY,
UA_LOG_WARNING(dm->server->config.logging, UA_LOGCATEGORY_DISCOVERY,
"Multicast DNS: could not remove record. Record not "
"found for domain: %s", fullServiceDomain);
return UA_STATUSCODE_BADNOTHINGTODO;
Expand Down

0 comments on commit 70f2635

Please sign in to comment.