Skip to content

Commit

Permalink
WIP: remove mdnsd
Browse files Browse the repository at this point in the history
Signed-off-by: Vasilij Strassheim <[email protected]>
  • Loading branch information
V-Strassheim committed Oct 31, 2024
1 parent 31cb119 commit c87ad5f
Show file tree
Hide file tree
Showing 7 changed files with 1,150 additions and 663 deletions.
36 changes: 6 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ if(BUILD_SHARED_LIBS)
set(UA_DYNAMIC_LINKING ON)
if(UA_ENABLE_DISCOVERY_MULTICAST)
set(MDNSD_DYNAMIC_LINKING ON)
find_package(PkgConfig REQUIRED)
pkg_check_modules(AVAHI REQUIRED avahi-client avahi-common)
include_directories(${AVAHI_INCLUDE_DIRS})
list(APPEND open62541_LIBRARIES "${AVAHI_LIBRARIES}")
endif()
endif()

Expand Down Expand Up @@ -696,25 +700,6 @@ file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/src_generated")
# Generate the config.h
configure_file(include/open62541/config.h.in ${PROJECT_BINARY_DIR}/src_generated/open62541/config.h)

if(UA_ENABLE_DISCOVERY_MULTICAST)
include(GenerateExportHeader)
set(MDNSD_LOGLEVEL 300 CACHE STRING "Level at which logs shall be reported" FORCE)

# create a "fake" empty library to generate the export header macros
if (APPLE)
add_library(libmdnsd INTERFACE ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.h)
else()
add_library(libmdnsd ${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.h)
endif()

set_property(TARGET libmdnsd PROPERTY LINKER_LANGUAGE C)
set_property(TARGET libmdnsd PROPERTY DEFINE_SYMBOL "MDNSD_DYNAMIC_LINKING_EXPORT")
configure_file("deps/mdnsd/libmdnsd/mdnsd_config_extra.in"
"${PROJECT_BINARY_DIR}/src_generated/mdnsd_config_extra")
file(READ "${PROJECT_BINARY_DIR}/src_generated/mdnsd_config_extra" MDNSD_CONFIG_EXTRA)
generate_export_header(libmdnsd EXPORT_FILE_NAME "${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h"
BASE_NAME MDNSD DEFINE_NO_DEPRECATED CUSTOM_CONTENT_FROM_VARIABLE MDNSD_CONFIG_EXTRA)
endif()

# Exported headers
set(exported_headers ${PROJECT_BINARY_DIR}/src_generated/open62541/config.h
Expand Down Expand Up @@ -875,18 +860,9 @@ endif()

if(UA_ENABLE_DISCOVERY_MULTICAST)
# prepend in list, otherwise it complains that winsock2.h has to be included before windows.h
list(APPEND lib_headers
${PROJECT_BINARY_DIR}/src_generated/mdnsd_config.h
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/1035.h
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/xht.h
${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_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
${PROJECT_SOURCE_DIR}/deps/mdnsd/libmdnsd/mdnsd.c)
${PROJECT_SOURCE_DIR}/src/server/ua_discovery_mdns.c
)
endif()

if(UA_BUILD_FUZZING OR UA_BUILD_OSS_FUZZ)
Expand Down
2 changes: 1 addition & 1 deletion examples/discovery/server_multicast.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ int main(int argc, char **argv) {
config->mdnsConfig.mdnsServerName = UA_String_fromChars("Sample-Multicast-Server");

//setting custom outbound interface
config->mdnsInterfaceIP = UA_String_fromChars("0.0.0.0");
config->mdnsInterfaceIP = UA_String_fromChars("192.168.0.42");

// See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
// For a LDS server, you should only indicate the LDS capability.
Expand Down
18 changes: 1 addition & 17 deletions src/server/ua_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

#include <open62541/client.h>
#include "ua_discovery.h"
#include "ua_discovery_avahi.h"
#include "ua_server_internal.h"

#ifdef UA_ENABLE_DISCOVERY
Expand All @@ -26,11 +26,6 @@ UA_DiscoveryManager_setState(UA_DiscoveryManager *dm,
if(state == UA_LIFECYCLESTATE_STOPPING ||
state == UA_LIFECYCLESTATE_STOPPED) {
state = UA_LIFECYCLESTATE_STOPPED;
#ifdef UA_ENABLE_DISCOVERY_MULTICAST
if(dm->mdnsRecvConnectionsSize != 0 || dm->mdnsSendConnection != 0)
state = UA_LIFECYCLESTATE_STOPPING;
#endif

for(size_t i = 0; i < UA_MAXREGISTERREQUESTS; i++) {
if(dm->registerRequests[i].client != NULL)
state = UA_LIFECYCLESTATE_STOPPING;
Expand Down Expand Up @@ -86,12 +81,6 @@ UA_DiscoveryManager_clear(struct UA_ServerComponent *sc) {
}
}

/* Clean up mdns daemon */
if(dm->mdnsDaemon) {
mdnsd_shutdown(dm->mdnsDaemon);
mdnsd_free(dm->mdnsDaemon);
dm->mdnsDaemon = NULL;
}
# endif /* UA_ENABLE_DISCOVERY_MULTICAST */

return UA_STATUSCODE_GOOD;
Expand Down Expand Up @@ -154,11 +143,6 @@ UA_DiscoveryManager_cleanupTimedOut(UA_Server *server, void *data) {
dm->registeredServersSize--;
}
}

#ifdef UA_ENABLE_DISCOVERY_MULTICAST
/* Send out multicast */
UA_DiscoveryManager_sendMulticastMessages(dm);
#endif
}

static UA_StatusCode
Expand Down
15 changes: 2 additions & 13 deletions src/server/ua_discovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ typedef struct {

#ifdef UA_ENABLE_DISCOVERY_MULTICAST

#include "mdnsd/libmdnsd/mdnsd.h"
#define UA_MAXMDNSRECVSOCKETS 8

/**
* TXT record:
Expand Down Expand Up @@ -93,11 +91,7 @@ struct UA_DiscoveryManager {
void* registerServerCallbackData;

# ifdef UA_ENABLE_DISCOVERY_MULTICAST
mdns_daemon_t *mdnsDaemon;
UA_ConnectionManager *cm;
uintptr_t mdnsSendConnection;
uintptr_t mdnsRecvConnections[UA_MAXMDNSRECVSOCKETS];
size_t mdnsRecvConnectionsSize;
// mdns_daemon_t *mdnsDaemon;
UA_Boolean mdnsMainSrvAdded;

/* Full Domain Name of server itself. Used to detect if received mDNS
Expand Down Expand Up @@ -141,7 +135,6 @@ UA_Discovery_updateMdnsForDiscoveryUrl(UA_DiscoveryManager *dm, const UA_String

void UA_DiscoveryManager_startMulticast(UA_DiscoveryManager *dm);
void UA_DiscoveryManager_stopMulticast(UA_DiscoveryManager *dm);
void UA_DiscoveryManager_sendMulticastMessages(UA_DiscoveryManager *dm);

UA_StatusCode
UA_DiscoveryManager_addEntryToServersOnNetwork(UA_DiscoveryManager *dm,
Expand All @@ -154,7 +147,7 @@ UA_DiscoveryManager_removeEntryFromServersOnNetwork(UA_DiscoveryManager *dm,
const char *fqdnMdnsRecord,
UA_String serverName);

void mdns_record_received(const struct resource *r, void *data);
// void mdns_record_received(const struct resource *r, void *data);

void mdns_create_txt(UA_DiscoveryManager *dm, const char *fullServiceDomain,
const char *path, const UA_String *capabilites,
Expand All @@ -164,10 +157,6 @@ void mdns_create_txt(UA_DiscoveryManager *dm, const char *fullServiceDomain,
void mdns_set_address_record(UA_DiscoveryManager *dm, const char *fullServiceDomain,
const char *localDomain);

mdns_record_t *
mdns_find_record(mdns_daemon_t *mdnsDaemon, unsigned short type,
const char *host, const char *rdname);

#endif /* UA_ENABLE_DISCOVERY_MULTICAST */

#endif /* UA_ENABLE_DISCOVERY */
Expand Down
198 changes: 198 additions & 0 deletions src/server/ua_discovery_avahi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2014-2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
* Copyright 2014, 2017 (c) Florian Palm
* Copyright 2015-2016 (c) Sten Grüner
* Copyright 2015 (c) Chris Iatrou
* Copyright 2015-2016 (c) Oleksiy Vasylyev
* Copyright 2016-2017 (c) Stefan Profanter, fortiss GmbH
* Copyright 2017 (c) Julian Grothoff
*/

#ifndef SRC_SERVER_UA_DISCOVERY_AVAHI_INCLUDED
#define SRC_SERVER_UA_DISCOVERY_AVAHI_INCLUDED

#ifndef UA_DISCOVERY_MANAGER_H_
#define UA_DISCOVERY_MANAGER_H_

#include "ua_server_internal.h"

_UA_BEGIN_DECLS

#ifdef UA_ENABLE_DISCOVERY

#ifdef UA_ENABLE_DISCOVERY
struct UA_DiscoveryManager;
typedef struct UA_DiscoveryManager UA_DiscoveryManager;
#endif

typedef struct registeredServer {
LIST_ENTRY(registeredServer) pointers;
UA_RegisteredServer registeredServer;
UA_DateTime lastSeen;
} registeredServer;

/* Store async register service calls. So we can cancel outstanding requests
* during shutdown. */
typedef struct {
UA_DelayedCallback cleanupCallback; /* delayed cleanup */
UA_Server *server;
UA_DiscoveryManager *dm;
UA_Client *client;
UA_String semaphoreFilePath;
UA_Boolean unregister;

UA_Boolean register2;
UA_Boolean shutdown;
UA_Boolean connectSuccess;
} asyncRegisterRequest;
#define UA_MAXREGISTERREQUESTS 4

#ifdef UA_ENABLE_DISCOVERY_MULTICAST

#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-client/publish.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/strlst.h>
#include <avahi-common/error.h>

typedef struct {
AvahiSimplePoll *simple_poll;
AvahiClient *client;
AvahiEntryGroup *group;
AvahiServiceBrowser *browser;
} AvahiServiceContext;

/**
* TXT record:
* [servername]-[hostname]._opcua-tcp._tcp.local. TXT path=/ caps=NA,DA,...
*
* A/AAAA record for all ip addresses:
* [servername]-[hostname]._opcua-tcp._tcp.local. A [ip].
* [hostname]. A [ip].
*/

typedef struct serverOnNetwork {
LIST_ENTRY(serverOnNetwork) pointers;
UA_ServerOnNetwork serverOnNetwork;
UA_DateTime created;
UA_DateTime lastSeen;
UA_Boolean txtSet;
UA_Boolean srvSet;
char* pathTmp;
} serverOnNetwork;

#define SERVER_ON_NETWORK_HASH_SIZE 1000
typedef struct serverOnNetwork_hash_entry {
serverOnNetwork *entry;
struct serverOnNetwork_hash_entry* next;
} serverOnNetwork_hash_entry;

#endif

struct UA_DiscoveryManager {
UA_ServerComponent sc;

UA_UInt64 discoveryCallbackId;

/* Outstanding requests. So they can be cancelled during shutdown. */
asyncRegisterRequest registerRequests[UA_MAXREGISTERREQUESTS];

LIST_HEAD(, registeredServer) registeredServers;
size_t registeredServersSize;
UA_Server_registerServerCallback registerServerCallback;
void* registerServerCallbackData;

# ifdef UA_ENABLE_DISCOVERY_MULTICAST
AvahiServiceContext *ctx;
UA_Boolean mdnsMainSrvAdded;

/* Full Domain Name of server itself. Used to detect if received mDNS
* message was from itself */
UA_String selfFqdnMdnsRecord;

LIST_HEAD(, serverOnNetwork) serverOnNetwork;

UA_UInt32 serverOnNetworkRecordIdCounter;
UA_DateTime serverOnNetworkRecordIdLastReset;

/* hash mapping domain name to serverOnNetwork list entry */
struct serverOnNetwork_hash_entry* serverOnNetworkHash[SERVER_ON_NETWORK_HASH_SIZE];

UA_Server_serverOnNetworkCallback serverOnNetworkCallback;
void *serverOnNetworkCallbackData;

UA_UInt64 mdnsCallbackId;
# endif /* UA_ENABLE_DISCOVERY_MULTICAST */
};

void
UA_DiscoveryManager_setState(UA_DiscoveryManager *dm,
UA_LifecycleState state);

#ifdef UA_ENABLE_DISCOVERY_MULTICAST


/** note: ua_discovery "internals" */
void UA_DiscoveryManager_clearMulticast(UA_DiscoveryManager *dm);
void UA_DiscoveryManager_startMulticast(UA_DiscoveryManager *dm);
void UA_DiscoveryManager_stopMulticast(UA_DiscoveryManager *dm);


/** note: ua_services_discovery :*/

/* Sends out a new mDNS package for the given server data. This Method is
* normally called when another server calls the RegisterServer Service on this
* server. Then this server is responsible to send out a new mDNS package to
* announce it.
*
* Additionally this method also adds the given server to the internal
* serversOnNetwork list so that a client finds it when calling
* FindServersOnNetwork. */
void
UA_Discovery_updateMdnsForDiscoveryUrl(UA_DiscoveryManager *dm, const UA_String serverName,
const UA_MdnsDiscoveryConfiguration *mdnsConfig,
const UA_String discoveryUrl, UA_Boolean isOnline,
UA_Boolean updateTxt);





// UA_StatusCode
// UA_DiscoveryManager_addEntryToServersOnNetwork(UA_DiscoveryManager *dm,
// const char *fqdnMdnsRecord,
// UA_String serverName,
// struct serverOnNetwork **addedEntry);

// UA_StatusCode
// UA_DiscoveryManager_removeEntryFromServersOnNetwork(UA_DiscoveryManager *dm,
// const char *fqdnMdnsRecord,
// UA_String serverName);

// void mdns_record_received(const struct resource *r, void *data);

// void mdns_create_txt(UA_DiscoveryManager *dm, const char *fullServiceDomain,
// const char *path, const UA_String *capabilites,
// const size_t capabilitiesSize,
// void (*conflict)(char *host, int type, void *arg));

// void mdns_set_address_record(UA_DiscoveryManager *dm, const char *fullServiceDomain,
// const char *localDomain);

// mdns_record_t *
// mdns_find_record(mdns_daemon_t *mdnsDaemon, unsigned short type,
// const char *host, const char *rdname);

#endif /* UA_ENABLE_DISCOVERY_MULTICAST */

#endif /* UA_ENABLE_DISCOVERY */

_UA_END_DECLS

#endif /* UA_DISCOVERY_MANAGER_H_ */

#endif /* SRC_SERVER_UA_DISCOVERY_AVAHI_INCLUDED */
Loading

0 comments on commit c87ad5f

Please sign in to comment.