Update libraries and make it build on windows.

Still gets some warnings because compilers have changed. But should work.
This commit is contained in:
Sandu Liviu Catalin committed 2025-06-25 22:34:23 +03:00
1 parent 520a5eacc5
commit f2b7499f85
3038 files changed
+251668 -273857

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
set(LIBNAME "DNSSDAvahi")
set(POCO_LIBNAME "Poco${LIBNAME}")
# Sources
file(GLOB SRCS_G "src/*.cpp")
POCO_SOURCES_AUTO( Avahi_SRCS ${SRCS_G})
# Headers
file(GLOB_RECURSE HDRS_G "include/*.h" )
POCO_HEADERS_AUTO( Avahi_SRCS ${HDRS_G})
add_definitions( ${Avahi_CFLAGS} -DTHREADSAFE)
add_library( "${LIBNAME}" ${LIB_MODE} ${Avahi_SRCS} )
add_library( "${POCO_LIBNAME}" ALIAS "${LIBNAME}")
set_target_properties( "${LIBNAME}"
PROPERTIES
VERSION ${SHARED_LIBRARY_VERSION} SOVERSION ${SHARED_LIBRARY_VERSION}
OUTPUT_NAME ${POCO_LIBNAME}
DEFINE_SYMBOL Avahi_EXPORTS
)
target_link_libraries( "${LIBNAME}" Foundation Net DNSSD ${AVAHI_LIBRARIES})
target_include_directories( "${LIBNAME}"
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_definitions("${LIBNAME}" PUBLIC ${LIB_MODE_DEFINITIONS})
POCO_INSTALL("${LIBNAME}")
POCO_GENERATE_PACKAGE("${LIBNAME}")
+21
View File
@@ -0,0 +1,21 @@
#
# Makefile
#
# $Id: //poco/1.7/DNSSD/Avahi/Makefile#1 $
#
# Makefile for Poco DNSSD Avahi
#
include $(POCO_BASE)/build/rules/global
SYSLIBS += -lavahi-common -lavahi-client
objects = \
AvahiResponderImpl AvahiBrowserImpl
target = PocoDNSSDAvahi
target_version = 1
target_libs = PocoNet PocoDNSSD PocoFoundation
include $(POCO_BASE)/build/rules/lib
@@ -0,0 +1,5 @@
include(CMakeFindDependencyMacro)
find_dependency(PocoFoundation)
find_dependency(PocoNet)
find_dependency(PocoDNSSD)
include("${CMAKE_CURRENT_LIST_DIR}/PocoDNSSDAvahiTargets.cmake")
+3
View File
@@ -0,0 +1,3 @@
Foundation
Net
DNSSD
@@ -0,0 +1,76 @@
//
// Avahi.h
//
// $Id: //poco/1.7/DNSSD/Avahi/include/Poco/DNSSD/Avahi/Avahi.h#1 $
//
// Library: DNSSD/Avahi
// Package: Implementation
// Module: Avahi
//
// Basic definitions for the Poco DNSSD Avahi library.
// This file must be the first file included by every other DNSSD Avahi
// header file.
//
// Copyright (c) 2006-2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef DNSSD_Avahi_Avahi_INCLUDED
#define DNSSD_Avahi_Avahi_INCLUDED
#include "Poco/DNSSD/DNSSD.h"
//
// The following block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the Avahi_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// DNSSD_Avahi_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(Avahi_EXPORTS)
#define DNSSD_Avahi_API __declspec(dllexport)
#else
#define DNSSD_Avahi_API __declspec(dllimport)
#endif
#endif
#if !defined(DNSSD_Avahi_API)
#if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
#define DNSSD_Avahi_API __attribute__ ((visibility ("default")))
#else
#define DNSSD_Avahi_API
#endif
#endif
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(Avahi_EXPORTS)
#pragma comment(lib, "PocoDNSSDAvahi" POCO_LIB_SUFFIX)
#endif
#endif
namespace Poco {
namespace DNSSD {
void DNSSD_Avahi_API initializeDNSSD();
/// Initialize the DNSSD subsystem.
void DNSSD_Avahi_API uninitializeDNSSD();
/// Uninitialize the DNSSD subsystem.
} } // namespace Poco::DNSSD
#endif // DNSSD_Avahi_Avahi_INCLUDED
@@ -0,0 +1,86 @@
//
// AvahiBrowserImpl.h
//
// $Id: //poco/1.7/DNSSD/Avahi/include/Poco/DNSSD/Avahi/AvahiBrowserImpl.h#1 $
//
// Library: DNSSD/Avahi
// Package: Implementation
// Module: AvahiBrowserImpl
//
// Definition of the AvahiBrowserImpl class.
//
// Copyright (c) 2006-2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef DNSSD_Avahi_AvahiBrowserImpl_INCLUDED
#define DNSSD_Avahi_AvahiBrowserImpl_INCLUDED
#include "Poco/DNSSD/Avahi/Avahi.h"
#include "Poco/DNSSD/DNSSDBrowser.h"
#include <avahi-client/lookup.h>
#include <map>
namespace Poco {
namespace DNSSD {
namespace Avahi {
class AvahiResponderImpl;
class DNSSD_Avahi_API AvahiBrowserImpl: public DNSSDBrowser
/// The DNSSDBrowser implementation for Avahi.
{
public:
AvahiBrowserImpl(AvahiResponderImpl& responder);
/// Creates the AvahiBrowserImpl.
~AvahiBrowserImpl();
/// Destroys the AvahiBrowserImpl.
// DNSSDBrowser
BrowseHandle browse(const std::string& regType, const std::string& domain, int options, Poco::Int32 networkInterface);
BrowseHandle resolve(const Service& service, int options);
BrowseHandle enumerateBrowseDomains(Poco::Int32 networkInterface);
BrowseHandle enumerateRegistrationDomains(Poco::Int32 networkInterface);
BrowseHandle queryRecord(const std::string& name, Poco::UInt16 type, Poco::UInt16 clazz, int options, Poco::Int32 networkInterface);
BrowseHandle resolveHost(const std::string& host, int options, Poco::Int32 networkInterface);
void cancel(BrowseHandle& browseHandle);
// Implementation
void onBrowseReply(AvahiServiceBrowser* browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char* name, const char* type, const char* domain, AvahiLookupResultFlags flags);
void onResolveReply(AvahiServiceResolver* resolver, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char* name, const char* type, const char* domain, const char* host, const AvahiAddress* a, uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags flags);
void onEnumerateBrowseDomainsReply(AvahiDomainBrowser* browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char* domain, AvahiLookupResultFlags flags, bool isDefault);
void onEnumerateRegistrationDomainsReply(AvahiDomainBrowser* browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char* domain, AvahiLookupResultFlags flags, bool isDefault);
void onQueryRecordReply(AvahiRecordBrowser* browser, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char* name, uint16_t clazz, uint16_t type, const void* data, std::size_t size, AvahiLookupResultFlags flags);
protected:
enum HandleTypes
{
DOMAIN_BROWSER_HANDLE,
SERVICE_BROWSER_HANDLE,
SERVICE_RESOLVER_HANDLE,
RECORD_BROWSER_HANDLE
};
static void parseTXTRecord(AvahiStringList* strList, Service::Properties& properties);
static void escape(const char* str, std::string& escaped);
private:
typedef std::map<std::string, Poco::Net::IPAddress> AddressMap;
AvahiResponderImpl& _responder;
AddressMap _addressMap;
};
} } } // namespace Poco::DNSSD::Avahi
#endif // DNSSD_Avahi_AvahiBrowserImpl_INCLUDED
@@ -0,0 +1,134 @@
//
// AvahiResponderImpl.h
//
// $Id: //poco/1.7/DNSSD/Avahi/include/Poco/DNSSD/Avahi/AvahiResponderImpl.h#1 $
//
// Library: DNSSD/Avahi
// Package: Implementation
// Module: AvahiResponderImpl
//
// Definition of the AvahiResponderImpl class.
//
// Copyright (c) 2006-2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef DNSSD_Avahi_AvahiResponderImpl_INCLUDED
#define DNSSD_Avahi_AvahiResponderImpl_INCLUDED
#include "Poco/DNSSD/Avahi/Avahi.h"
#include "Poco/DNSSD/Avahi/AvahiBrowserImpl.h"
#include "Poco/DNSSD/DNSSDResponderImpl.h"
#include "Poco/DNSSD/DNSSDResponder.h"
#include "Poco/ScopedLock.h"
#include "Poco/Mutex.h"
#include "Poco/Event.h"
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include <map>
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/simple-watch.h>
namespace Poco {
namespace DNSSD {
namespace Avahi {
class AvahiBrowserImpl;
class DNSSD_Avahi_API AvahiResponderImpl: public Poco::DNSSD::DNSSDResponderImpl, public Poco::Runnable
/// The DNSSDResponderImpl implementation for Avahi.
{
public:
typedef Poco::ScopedLock<AvahiResponderImpl> ScopedLock;
AvahiResponderImpl(Poco::DNSSD::DNSSDResponder& owner);
/// Creates the AvahiResponder, using the given owner.
~AvahiResponderImpl();
/// Destroys the AvahiResponderImpl.
// DNSSDResponderImpl
DNSSDBrowser& browser();
ServiceHandle registerService(const Service& service, int options);
void unregisterService(ServiceHandle& serviceHandle);
RecordHandle addRecord(ServiceHandle serviceHandle, const Record& record);
void updateRecord(ServiceHandle serviceHandle, RecordHandle recordHandle, const Record& record);
void removeRecord(ServiceHandle serviceHandle, RecordHandle& recordHandle);
void start();
void stop();
// Runnable
void run();
// Implementation
static const char* describeError(int code);
/// Returns a human-readable string describing the error.
void onClientStateChange(AvahiClientState state);
void onGroupStateChange(AvahiEntryGroup* avahiGroup, AvahiEntryGroupState state);
void lock();
void unlock();
protected:
enum
{
START_TIMEOUT = 5000 // milliseconds
};
struct RecordInfo
{
Record record;
int id;
};
typedef std::vector<RecordInfo> RecordVec;
struct ServiceInfo
{
Service service;
RecordVec records;
int options;
};
typedef std::map<ServiceHandle, ServiceInfo> ServiceMap;
void reregisterServices();
void setupEntryGroup(AvahiEntryGroup* avahiGroup, const Service& service, const RecordVec& records, int options, bool rename);
static AvahiStringList* createTXTRecord(const Service::Properties& properties);
private:
Poco::DNSSD::DNSSDResponder& _owner;
AvahiBrowserImpl _browser;
AvahiSimplePoll* _avahiPoll;
AvahiClient* _avahiClient;
Poco::Event _avahiClientReady;
ServiceMap _services;
bool _running;
int _nextRecordId;
Poco::Mutex _mutex;
Poco::Thread _pollThread;
friend class AvahiBrowserImpl;
};
class DNSSD_Avahi_API AvahiResponderImplFactory: public Poco::DNSSD::DNSSDResponderImplFactory
/// A factory for AvahiResponderImplFactory objects.
{
public:
DNSSDResponderImpl* createResponderImpl(Poco::DNSSD::DNSSDResponder& owner)
{
return new AvahiResponderImpl(owner);
}
};
} } } // namespace Poco::DNSSD::Avahi
#endif // DNSSD_Avahi_AvahiResponderImpl_INCLUDED
+582
View File
@@ -0,0 +1,582 @@
//
// AvahiBrowserImpl.cpp
//
// $Id: //poco/1.7/DNSSD/Avahi/src/AvahiBrowserImpl.cpp#1 $
//
// Library: DNSSD/Avahi
// Package: Implementation
// Module: AvahiBrowserImpl
//
// Copyright (c) 2006-2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/DNSSD/Avahi/AvahiBrowserImpl.h"
#include "Poco/DNSSD/Avahi/AvahiResponderImpl.h"
#include "Poco/DNSSD/DNSSDException.h"
#include "Poco/NumberFormatter.h"
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include <avahi-common/address.h>
#include <iostream>
namespace Poco {
namespace DNSSD {
namespace Avahi {
extern "C" void onBrowseReply(
AvahiServiceBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* name,
const char* type,
const char* domain,
AvahiLookupResultFlags flags,
void* context)
{
try
{
AvahiBrowserImpl* pBrowser = reinterpret_cast<AvahiBrowserImpl*>(context);
pBrowser->onBrowseReply(browser, interface, protocol, event, name, type, domain, flags);
}
catch (...)
{
}
}
extern "C" void onResolveReply(
AvahiServiceResolver* resolver,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiResolverEvent event,
const char* name,
const char* type,
const char* domain,
const char* host,
const AvahiAddress* a,
uint16_t port,
AvahiStringList* txt,
AvahiLookupResultFlags flags,
void* context)
{
try
{
AvahiBrowserImpl* pBrowser = reinterpret_cast<AvahiBrowserImpl*>(context);
pBrowser->onResolveReply(resolver, interface, protocol, event, name, type, domain, host, a, port, txt, flags);
}
catch (...)
{
}
}
extern "C" void onEnumerateBrowseDomainsReply(
AvahiDomainBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* domain,
AvahiLookupResultFlags flags,
void* context)
{
try
{
AvahiBrowserImpl* pBrowser = reinterpret_cast<AvahiBrowserImpl*>(context);
pBrowser->onEnumerateBrowseDomainsReply(browser, interface, protocol, event, domain, flags, false);
}
catch (...)
{
}
}
extern "C" void onEnumerateDefaultBrowseDomainsReply(
AvahiDomainBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* domain,
AvahiLookupResultFlags flags,
void* context)
{
try
{
AvahiBrowserImpl* pBrowser = reinterpret_cast<AvahiBrowserImpl*>(context);
pBrowser->onEnumerateBrowseDomainsReply(browser, interface, protocol, event, domain, flags, true);
}
catch (...)
{
}
if (event == AVAHI_BROWSER_ALL_FOR_NOW || event == AVAHI_BROWSER_FAILURE)
{
avahi_domain_browser_free(browser);
}
}
extern "C" void onEnumerateRegistrationDomainsReply(
AvahiDomainBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* domain,
AvahiLookupResultFlags flags,
void* context)
{
try
{
AvahiBrowserImpl* pBrowser = reinterpret_cast<AvahiBrowserImpl*>(context);
pBrowser->onEnumerateRegistrationDomainsReply(browser, interface, protocol, event, domain, flags, false);
}
catch (...)
{
}
}
extern "C" void onEnumerateDefaultRegistrationDomainsReply(
AvahiDomainBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* domain,
AvahiLookupResultFlags flags,
void* context)
{
try
{
AvahiBrowserImpl* pBrowser = reinterpret_cast<AvahiBrowserImpl*>(context);
pBrowser->onEnumerateRegistrationDomainsReply(browser, interface, protocol, event, domain, flags, true);
}
catch (...)
{
}
if (event == AVAHI_BROWSER_ALL_FOR_NOW || event == AVAHI_BROWSER_FAILURE)
{
avahi_domain_browser_free(browser);
}
}
extern "C" void onQueryRecordReply(
AvahiRecordBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* name,
uint16_t clazz,
uint16_t type,
const void* data,
std::size_t size,
AvahiLookupResultFlags flags,
void* context)
{
try
{
AvahiBrowserImpl* pBrowser = reinterpret_cast<AvahiBrowserImpl*>(context);
pBrowser->onQueryRecordReply(browser, interface, protocol, event, name, clazz, type, data, size, flags);
}
catch (...)
{
}
}
AvahiBrowserImpl::AvahiBrowserImpl(AvahiResponderImpl& responder):
_responder(responder)
{
}
AvahiBrowserImpl::~AvahiBrowserImpl()
{
}
BrowseHandle AvahiBrowserImpl::browse(const std::string& regType, const std::string& domain, int options, Poco::Int32 networkInterface)
{
AvahiResponderImpl::ScopedLock lock(_responder);
AvahiIfIndex ifIndex = networkInterface == 0 ? AVAHI_IF_UNSPEC : networkInterface;
AvahiServiceBrowser* browser = avahi_service_browser_new(_responder._avahiClient, ifIndex, AVAHI_PROTO_UNSPEC, regType.c_str(), domain.empty() ? NULL : domain.c_str(), (AvahiLookupFlags) 0, Poco::DNSSD::Avahi::onBrowseReply, this);
if (!browser)
{
int error = avahi_client_errno(_responder._avahiClient);
throw Poco::DNSSD::DNSSDException("Failed to browse for " + regType, AvahiResponderImpl::describeError(error), error);
}
return BrowseHandle(browser, SERVICE_BROWSER_HANDLE);
}
BrowseHandle AvahiBrowserImpl::resolve(const Service& service, int options)
{
AvahiResponderImpl::ScopedLock lock(_responder);
AvahiIfIndex ifIndex = service.networkInterface();
AvahiServiceResolver* resolver = avahi_service_resolver_new(_responder._avahiClient, ifIndex, AVAHI_PROTO_UNSPEC, service.name().c_str(), service.type().c_str(), service.domain().c_str(), AVAHI_PROTO_UNSPEC, (AvahiLookupFlags) 0, Poco::DNSSD::Avahi::onResolveReply, this);
if (!resolver)
{
int error = avahi_client_errno(_responder._avahiClient);
throw Poco::DNSSD::DNSSDException("Failed to resolve service " + service.name(), AvahiResponderImpl::describeError(error), error);
}
return BrowseHandle(resolver, SERVICE_RESOLVER_HANDLE);
}
BrowseHandle AvahiBrowserImpl::enumerateBrowseDomains(Poco::Int32 networkInterface)
{
AvahiResponderImpl::ScopedLock lock(_responder);
AvahiIfIndex ifIndex = networkInterface == 0 ? AVAHI_IF_UNSPEC : networkInterface;
AvahiDomainBrowser* browser = avahi_domain_browser_new(_responder._avahiClient, ifIndex, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE, (AvahiLookupFlags) 0, Poco::DNSSD::Avahi::onEnumerateBrowseDomainsReply, this);
if (!browser)
{
int error = avahi_client_errno(_responder._avahiClient);
throw Poco::DNSSD::DNSSDException("Failed to enumerate browse domains", AvahiResponderImpl::describeError(error), error);
}
AvahiDomainBrowser* defaultBrowser = avahi_domain_browser_new(_responder._avahiClient, ifIndex, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_BROWSE_DEFAULT, (AvahiLookupFlags) 0, Poco::DNSSD::Avahi::onEnumerateDefaultBrowseDomainsReply, this);
if (!defaultBrowser)
{
int error = avahi_client_errno(_responder._avahiClient);
avahi_domain_browser_free(browser);
throw Poco::DNSSD::DNSSDException("Failed to enumerate default browse domains", AvahiResponderImpl::describeError(error), error);
}
return BrowseHandle(browser, DOMAIN_BROWSER_HANDLE);
}
BrowseHandle AvahiBrowserImpl::enumerateRegistrationDomains(Poco::Int32 networkInterface)
{
AvahiResponderImpl::ScopedLock lock(_responder);
AvahiIfIndex ifIndex = networkInterface == 0 ? AVAHI_IF_UNSPEC : networkInterface;
AvahiDomainBrowser* browser = avahi_domain_browser_new(_responder._avahiClient, ifIndex, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_REGISTER, (AvahiLookupFlags) 0, Poco::DNSSD::Avahi::onEnumerateRegistrationDomainsReply, this);
if (!browser)
{
int error = avahi_client_errno(_responder._avahiClient);
throw Poco::DNSSD::DNSSDException("Failed to enumerate registration domains", AvahiResponderImpl::describeError(error), error);
}
AvahiDomainBrowser* defaultBrowser = avahi_domain_browser_new(_responder._avahiClient, ifIndex, AVAHI_PROTO_UNSPEC, NULL, AVAHI_DOMAIN_BROWSER_REGISTER_DEFAULT, (AvahiLookupFlags) 0, Poco::DNSSD::Avahi::onEnumerateDefaultRegistrationDomainsReply, this);
if (!defaultBrowser)
{
int error = avahi_client_errno(_responder._avahiClient);
avahi_domain_browser_free(browser);
throw Poco::DNSSD::DNSSDException("Failed to enumerate default registration domains", AvahiResponderImpl::describeError(error), error);
}
return BrowseHandle(browser, DOMAIN_BROWSER_HANDLE);
}
BrowseHandle AvahiBrowserImpl::queryRecord(const std::string& name, Poco::UInt16 type, Poco::UInt16 clazz, int options, Poco::Int32 networkInterface)
{
AvahiResponderImpl::ScopedLock lock(_responder);
AvahiIfIndex ifIndex = networkInterface == 0 ? AVAHI_IF_UNSPEC : networkInterface;
AvahiRecordBrowser* browser = avahi_record_browser_new(_responder._avahiClient, ifIndex, AVAHI_PROTO_UNSPEC, name.c_str(), clazz, type, (AvahiLookupFlags) 0, Poco::DNSSD::Avahi::onQueryRecordReply, this);
if (!browser)
{
int error = avahi_client_errno(_responder._avahiClient);
throw Poco::DNSSD::DNSSDException("Failed to query for record", AvahiResponderImpl::describeError(error), error);
}
return BrowseHandle(browser, RECORD_BROWSER_HANDLE);
}
BrowseHandle AvahiBrowserImpl::resolveHost(const std::string& host, int options, Poco::Int32 networkInterface)
{
AvahiResponderImpl::ScopedLock lock(_responder);
AddressMap::iterator it = _addressMap.find(host);
if (it != _addressMap.end())
{
ResolveHostEventArgs args(BrowseHandle(), 0, host, it->second, networkInterface, 0);
hostResolved(this, args);
}
else
{
int err = AVAHI_ERR_DNS_NXDOMAIN;
Error error(networkInterface, err, AvahiResponderImpl::describeError(err));
ErrorEventArgs args(BrowseHandle(), 0, error);
hostResolveError(this, args);
}
return BrowseHandle();
}
void AvahiBrowserImpl::cancel(BrowseHandle& browseHandle)
{
if (!browseHandle.isValid()) return;
AvahiResponderImpl::ScopedLock lock(_responder);
switch (browseHandle.subtype())
{
case SERVICE_BROWSER_HANDLE:
avahi_service_browser_free(browseHandle.cast<AvahiServiceBrowser*>());
break;
case SERVICE_RESOLVER_HANDLE:
avahi_service_resolver_free(browseHandle.cast<AvahiServiceResolver*>());
break;
case DOMAIN_BROWSER_HANDLE:
avahi_domain_browser_free(browseHandle.cast<AvahiDomainBrowser*>());
break;
case RECORD_BROWSER_HANDLE:
avahi_record_browser_free(browseHandle.cast<AvahiRecordBrowser*>());
break;
}
browseHandle.reset();
}
void AvahiBrowserImpl::onBrowseReply(
AvahiServiceBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* name,
const char* type,
const char* domain,
AvahiLookupResultFlags flags)
{
AvahiResponderImpl::ScopedLock lock(_responder);
if (event == AVAHI_BROWSER_NEW || event == AVAHI_BROWSER_REMOVE)
{
int eventFlags(0);
Service service(interface, name, type, domain);
ServiceEventArgs args(BrowseHandle(browser), eventFlags, service);
if (event == AVAHI_BROWSER_NEW)
{
serviceFound(this, args);
}
else
{
serviceRemoved(this, args);
}
}
else if (event == AVAHI_BROWSER_FAILURE)
{
int err = avahi_client_errno(_responder._avahiClient);
Error error(interface, err, AvahiResponderImpl::describeError(err));
ErrorEventArgs args(BrowseHandle(browser), 0, error);
browseError(this, args);
}
}
void AvahiBrowserImpl::onResolveReply(
AvahiServiceResolver* resolver,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiResolverEvent event,
const char* name,
const char* type,
const char* domain,
const char* host,
const AvahiAddress* a,
uint16_t port,
AvahiStringList* txt,
AvahiLookupResultFlags flags)
{
AvahiResponderImpl::ScopedLock lock(_responder);
if (event == AVAHI_RESOLVER_FOUND)
{
Poco::Net::IPAddress addr;
switch (a->proto)
{
case AVAHI_PROTO_INET:
addr = Poco::Net::IPAddress(&a->data.ipv4.address, sizeof(a->data.ipv4.address));
break;
#if defined(POCO_HAVE_IPv6)
case AVAHI_PROTO_INET6:
addr = Poco::Net::IPAddress(&a->data.ipv6.address, sizeof(a->data.ipv6.address));
break;
#endif
}
int eventFlags(0);
std::string fullName;
escape(name, fullName);
fullName += '.';
fullName += type;
fullName += '.';
fullName += domain;
fullName += '.';
Service service(interface, name, fullName, type, domain, host, port);
_addressMap[service.host()] = addr;
parseTXTRecord(txt, service.properties());
ServiceEventArgs args(BrowseHandle(resolver), eventFlags, service);
serviceResolved(this, args);
}
else if (event == AVAHI_RESOLVER_FAILURE)
{
int err = avahi_client_errno(_responder._avahiClient);
Error error(interface, err, AvahiResponderImpl::describeError(err));
ErrorEventArgs args(BrowseHandle(resolver), 0, error);
resolveError(this, args);
}
}
void AvahiBrowserImpl::onEnumerateBrowseDomainsReply(
AvahiDomainBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* domain,
AvahiLookupResultFlags flags,
bool isDefault)
{
AvahiResponderImpl::ScopedLock lock(_responder);
if (event == AVAHI_BROWSER_NEW || event == AVAHI_BROWSER_REMOVE)
{
int eventFlags(0);
Poco::Int32 ifIndex = interface == AVAHI_IF_UNSPEC ? 0 : interface;
Domain dom(ifIndex, domain, isDefault);
DomainEventArgs args(BrowseHandle(browser), eventFlags, dom);
if (event == AVAHI_BROWSER_NEW)
{
browseDomainFound(this, args);
}
else
{
browseDomainRemoved(this, args);
}
}
else if (event == AVAHI_BROWSER_FAILURE)
{
int err = avahi_client_errno(_responder._avahiClient);
Error error(interface, err, AvahiResponderImpl::describeError(err));
ErrorEventArgs args(BrowseHandle(browser), 0, error);
browseDomainError(this, args);
}
}
void AvahiBrowserImpl::onEnumerateRegistrationDomainsReply(
AvahiDomainBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* domain,
AvahiLookupResultFlags flags,
bool isDefault)
{
AvahiResponderImpl::ScopedLock lock(_responder);
if (event == AVAHI_BROWSER_NEW || event == AVAHI_BROWSER_REMOVE)
{
int eventFlags(0);
Poco::Int32 ifIndex = interface == AVAHI_IF_UNSPEC ? 0 : interface;
Domain dom(ifIndex, domain, isDefault);
DomainEventArgs args(BrowseHandle(browser), eventFlags, dom);
if (event == AVAHI_BROWSER_NEW)
{
registrationDomainFound(this, args);
}
else
{
registrationDomainRemoved(this, args);
}
}
else if (event == AVAHI_BROWSER_FAILURE)
{
int err = avahi_client_errno(_responder._avahiClient);
Error error(interface, err, AvahiResponderImpl::describeError(err));
ErrorEventArgs args(BrowseHandle(browser), 0, error);
registrationDomainError(this, args);
}
}
void AvahiBrowserImpl::onQueryRecordReply(
AvahiRecordBrowser* browser,
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiBrowserEvent event,
const char* name,
uint16_t clazz,
uint16_t type,
const void* data,
std::size_t size,
AvahiLookupResultFlags flags)
{
AvahiResponderImpl::ScopedLock lock(_responder);
if (event == AVAHI_BROWSER_NEW || event == AVAHI_BROWSER_REMOVE)
{
int eventFlags(0);
Poco::Int32 ifIndex = interface == AVAHI_IF_UNSPEC ? 0 : interface;
Record record(ifIndex, name, type, clazz, size, data, 0);
RecordEventArgs args(BrowseHandle(browser), eventFlags, record);
if (event == AVAHI_BROWSER_NEW)
{
recordFound(this, args);
}
else
{
recordRemoved(this, args);
}
}
else if (event == AVAHI_BROWSER_FAILURE)
{
int err = avahi_client_errno(_responder._avahiClient);
Error error(interface, err, AvahiResponderImpl::describeError(err));
ErrorEventArgs args(BrowseHandle(browser), 0, error);
recordError(this, args);
}
}
void AvahiBrowserImpl::parseTXTRecord(AvahiStringList* strList, Service::Properties& properties)
{
while (strList)
{
char* key;
char* value;
std::size_t size;
avahi_string_list_get_pair(strList, &key, &value, &size);
properties.set(key, std::string(value ? value : "", size));
avahi_free(key);
avahi_free(value);
strList = avahi_string_list_get_next(strList);
}
}
void AvahiBrowserImpl::escape(const char* str, std::string& escaped)
{
while (*str)
{
if (*str <= ' ' || *str >= 127)
{
escaped += '\\';
Poco::NumberFormatter::append0(escaped, static_cast<unsigned>(static_cast<unsigned char>(*str)), 3);
}
else if (*str == '\\')
{
escaped += "\\\\";
}
else if (*str == '.')
{
escaped += "\\.";
}
else
{
escaped += *str;
}
++str;
}
}
} } } // namespace Poco::DNSSD::Avahi
+514
View File
@@ -0,0 +1,514 @@
//
// AvahiResponderImpl.cpp
//
// $Id: //poco/1.7/DNSSD/Avahi/src/AvahiResponderImpl.cpp#1 $
//
// Library: DNSSD/Avahi
// Package: Implementation
// Module: AvahiResponderImpl
//
// Copyright (c) 2006-2024, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/DNSSD/Avahi/AvahiResponderImpl.h"
#include "Poco/DNSSD/DNSSDException.h"
#include "Poco/StringTokenizer.h"
#include <avahi-common/alternative.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
namespace Poco {
namespace DNSSD {
namespace Avahi {
extern "C" int avahiPollFunc(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata)
{
Poco::Mutex* pMutex = reinterpret_cast<Poco::Mutex*>(userdata);
pMutex->unlock();
int ret = poll(ufds, nfds, timeout);
pMutex->lock();
return ret;
}
extern "C" void onClientStateChange(AvahiClient* avahiClient, AvahiClientState state, void* userdata)
{
try
{
AvahiResponderImpl* pResponder = reinterpret_cast<AvahiResponderImpl*>(userdata);
pResponder->onClientStateChange(state);
}
catch (...)
{
}
}
extern "C" void onGroupStateChange(AvahiEntryGroup* avahiGroup, AvahiEntryGroupState state, void* userdata)
{
try
{
AvahiResponderImpl* pResponder = reinterpret_cast<AvahiResponderImpl*>(userdata);
pResponder->onGroupStateChange(avahiGroup, state);
}
catch (...)
{
}
}
AvahiResponderImpl::AvahiResponderImpl(Poco::DNSSD::DNSSDResponder& owner):
_owner(owner),
_browser(*this),
_avahiPoll(0),
_avahiClient(0),
_running(false),
_nextRecordId(1)
{
_avahiPoll = avahi_simple_poll_new();
if (!_avahiPoll) throw DNSSDException("Cannot create Avahi simple poll object");
avahi_simple_poll_set_func(_avahiPoll, Poco::DNSSD::Avahi::avahiPollFunc, &_mutex);
int error;
_avahiClient = avahi_client_new(avahi_simple_poll_get(_avahiPoll), AVAHI_CLIENT_NO_FAIL, Poco::DNSSD::Avahi::onClientStateChange, this, &error);
if (!_avahiClient)
{
avahi_simple_poll_free(_avahiPoll);
throw DNSSDException("Cannot create Avahi client", describeError(error), error);
}
}
AvahiResponderImpl::~AvahiResponderImpl()
{
try
{
stop();
avahi_client_free(_avahiClient);
avahi_simple_poll_free(_avahiPoll);
}
catch (...)
{
poco_unexpected();
}
}
DNSSDBrowser& AvahiResponderImpl::browser()
{
return _browser;
}
ServiceHandle AvahiResponderImpl::registerService(const Service& service, int options)
{
ScopedLock lock(*this);
AvahiEntryGroup* avahiGroup = avahi_entry_group_new(_avahiClient, Poco::DNSSD::Avahi::onGroupStateChange, this);
if (!avahiGroup) throw DNSSDException("Cannot create Avahi Entry Group");
try
{
ServiceHandle serviceHandle(avahiGroup);
RecordVec records;
setupEntryGroup(avahiGroup, service, records, options, false);
_services[serviceHandle].options = options;
return serviceHandle;
}
catch (...)
{
avahi_entry_group_free(avahiGroup);
throw;
}
}
void AvahiResponderImpl::unregisterService(ServiceHandle& serviceHandle)
{
ScopedLock lock(*this);
ServiceMap::iterator it = _services.find(serviceHandle);
if (it != _services.end())
{
_services.erase(it);
}
AvahiEntryGroup* avahiGroup = serviceHandle.cast<AvahiEntryGroup*>();
avahi_entry_group_free(avahiGroup);
}
RecordHandle AvahiResponderImpl::addRecord(ServiceHandle serviceHandle, const Record& record)
{
ScopedLock lock(*this);
ServiceMap::iterator it = _services.find(serviceHandle);
if (it != _services.end())
{
RecordInfo recordInfo;
recordInfo.record = record;
recordInfo.id = _nextRecordId++;
it->second.records.push_back(recordInfo);
setupEntryGroup(serviceHandle.cast<AvahiEntryGroup*>(), it->second.service, it->second.records, it->second.options, false);
return RecordHandle(reinterpret_cast<void*>(recordInfo.id));
}
else throw Poco::InvalidArgumentException("Unknown ServiceHandle");
}
void AvahiResponderImpl::updateRecord(ServiceHandle serviceHandle, RecordHandle recordHandle, const Record& record)
{
ScopedLock lock(*this);
ServiceMap::iterator it = _services.find(serviceHandle);
if (it != _services.end())
{
bool found = false;
for (RecordVec::iterator itRec = it->second.records.begin(); itRec != it->second.records.end(); ++itRec)
{
if (itRec->id == static_cast<int>(reinterpret_cast<Poco::IntPtr>(recordHandle.cast<void*>())))
{
itRec->record = record;
found = true;
break;
}
}
if (!found) throw Poco::NotFoundException("Record not found", record.name());
setupEntryGroup(serviceHandle.cast<AvahiEntryGroup*>(), it->second.service, it->second.records, it->second.options, false);
}
else throw Poco::InvalidArgumentException("Unknown ServiceHandle");
}
void AvahiResponderImpl::removeRecord(ServiceHandle serviceHandle, RecordHandle& recordHandle)
{
ScopedLock lock(*this);
ServiceMap::iterator it = _services.find(serviceHandle);
if (it != _services.end())
{
bool found = false;
for (RecordVec::iterator itRec = it->second.records.begin(); itRec != it->second.records.end(); ++itRec)
{
if (itRec->id == static_cast<int>(reinterpret_cast<Poco::IntPtr>(recordHandle.cast<void*>())))
{
it->second.records.erase(itRec);
recordHandle.reset();
found = true;
break;
}
}
if (!found) throw Poco::NotFoundException("Record not found");
setupEntryGroup(serviceHandle.cast<AvahiEntryGroup*>(), it->second.service, it->second.records, it->second.options, false);
}
else throw Poco::InvalidArgumentException("Unknown ServiceHandle");
}
void AvahiResponderImpl::start()
{
if (!_running)
{
if (!_pollThread.isRunning())
{
_pollThread.start(*this);
}
if (!_avahiClientReady.tryWait(START_TIMEOUT))
{
std::string state;
switch (avahi_client_get_state(_avahiClient))
{
case AVAHI_CLIENT_S_REGISTERING:
state = "registering";
break;
case AVAHI_CLIENT_S_RUNNING:
state = "running";
break;
case AVAHI_CLIENT_S_COLLISION:
state = "collision";
break;
case AVAHI_CLIENT_FAILURE:
state = "failure";
break;
case AVAHI_CLIENT_CONNECTING:
state = "connecting";
};
throw DNSSDException("Avahi client not ready; current state", state);
}
_running = true;
}
}
void AvahiResponderImpl::stop()
{
if (_running)
{
{
ScopedLock lock(*this);
avahi_simple_poll_quit(_avahiPoll);
}
_pollThread.join();
_running = false;
}
}
const char* AvahiResponderImpl::describeError(int code)
{
return avahi_strerror(code);
}
void AvahiResponderImpl::lock()
{
_mutex.lock();
}
void AvahiResponderImpl::unlock()
{
_mutex.unlock();
}
void AvahiResponderImpl::run()
{
ScopedLock lock(*this);
avahi_simple_poll_loop(_avahiPoll);
}
void AvahiResponderImpl::setupEntryGroup(AvahiEntryGroup* avahiGroup, const Service& service, const RecordVec& records, int options, bool rename)
{
avahi_entry_group_reset(avahiGroup);
AvahiStringList* txtList = createTXTRecord(service.properties());
try
{
int ifIndex = service.networkInterface() == 0 ? AVAHI_IF_UNSPEC : service.networkInterface();
std::string type = service.type();
std::string subtypes;
std::string::size_type pos = type.find(',');
if (pos != std::string::npos)
{
subtypes.assign(type, pos + 1, type.size() - pos);
type.resize(pos);
}
std::string name(service.name());
if (name.empty()) name = avahi_client_get_host_name(_avahiClient);
const char* domain = service.domain().empty() ? 0 : service.domain().c_str();
const char* host = service.host().empty() ? 0 : service.host().c_str();
int error = rename ? AVAHI_ERR_COLLISION : avahi_entry_group_add_service_strlst(
avahiGroup,
ifIndex,
AVAHI_PROTO_UNSPEC,
(AvahiPublishFlags) 0,
name.c_str(),
type.c_str(),
domain,
host,
service.port(),
txtList
);
while (error == AVAHI_ERR_COLLISION)
{
if (options & DNSSDResponder::REG_NO_AUTORENAME) throw DNSSDException("Cannot register service: " + name, describeError(error), error);
const char* newName = avahi_alternative_service_name(name.c_str());
name = newName;
avahi_free(const_cast<char*>(newName));
error = avahi_entry_group_add_service_strlst(
avahiGroup,
ifIndex,
AVAHI_PROTO_UNSPEC,
(AvahiPublishFlags) 0,
name.c_str(),
type.c_str(),
domain,
host,
service.port(),
txtList
);
}
if (error) throw DNSSDException("Cannot add service to Avahi Entry Group: " + name, describeError(error), error);
if (!subtypes.empty())
{
Poco::StringTokenizer tok(subtypes, ",", Poco::StringTokenizer::TOK_IGNORE_EMPTY | Poco::StringTokenizer::TOK_TRIM);
for (Poco::StringTokenizer::Iterator it = tok.begin(); it != tok.end(); ++it)
{
error = avahi_entry_group_add_service_subtype(
avahiGroup,
service.networkInterface(),
AVAHI_PROTO_UNSPEC,
(AvahiPublishFlags) 0,
name.c_str(),
type.c_str(),
domain,
it->c_str()
);
if (error) throw DNSSDException("Cannot add service subtype to Avahi Entry Group", describeError(error), error);
}
}
for (RecordVec::const_iterator it = records.begin(); it != records.end(); ++it)
{
error = avahi_entry_group_add_record(
avahiGroup,
ifIndex,
AVAHI_PROTO_UNSPEC,
(AvahiPublishFlags) 0,
it->record.name().c_str(),
it->record.clazz(),
it->record.type(),
it->record.ttl(),
it->record.data(),
it->record.length()
);
if (error) throw DNSSDException("Cannot add record to Avahi Entry Group: " + it->record.name(), describeError(error), error);
}
error = avahi_entry_group_commit(avahiGroup);
if (error) throw DNSSDException("Cannot commit Avahi Entry Group", describeError(error), error);
ServiceHandle serviceHandle(avahiGroup);
_services[serviceHandle].service = Service(service.networkInterface(), name, "", service.type(), service.domain(), service.host(), service.port(), service.properties());
avahi_string_list_free(txtList);
}
catch (...)
{
avahi_entry_group_reset(avahiGroup);
avahi_string_list_free(txtList);
throw;
}
}
AvahiStringList* AvahiResponderImpl::createTXTRecord(const Service::Properties& properties)
{
AvahiStringList* avahiList = 0;
Service::Properties::ConstIterator itVers = properties.find("txtvers");
Service::Properties::ConstIterator itEnd = properties.end();
std::string entry;
if (itVers != itEnd)
{
std::string entry(itVers->first);
entry += '=';
entry += itVers->second;
avahiList = avahi_string_list_new(entry.c_str(), NULL);
}
Service::Properties::ConstIterator it = properties.begin();
for (; it != itEnd; ++it)
{
if (it != itVers)
{
if (avahiList)
{
avahiList = avahi_string_list_add_pair_arbitrary(avahiList, it->first.c_str(), reinterpret_cast<const uint8_t*>(it->second.empty() ? NULL : it->second.c_str()), it->second.size());
}
else
{
std::string entry(it->first);
if (!it->second.empty())
{
entry += '=';
entry += it->second;
}
avahiList = avahi_string_list_new(entry.c_str(), NULL);
}
}
}
return avahiList;
}
void AvahiResponderImpl::onClientStateChange(AvahiClientState state)
{
ScopedLock lock(*this);
if (state == AVAHI_CLIENT_S_RUNNING)
{
_avahiClientReady.set();
reregisterServices();
}
}
void AvahiResponderImpl::onGroupStateChange(AvahiEntryGroup* avahiGroup, AvahiEntryGroupState state)
{
ScopedLock lock(*this);
ServiceHandle serviceHandle(avahiGroup);
ServiceMap::iterator it = _services.find(serviceHandle);
if (it != _services.end())
{
if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
{
DNSSDResponder::ServiceEventArgs args(serviceHandle, it->second.service);
_owner.serviceRegistered(this, args);
}
else if (state == AVAHI_ENTRY_GROUP_COLLISION)
{
if (it->second.options & DNSSDResponder::REG_NO_AUTORENAME)
{
int error = AVAHI_ERR_COLLISION;
DNSSDResponder::ErrorEventArgs args(serviceHandle, it->second.service, Error(it->second.service.networkInterface(), error, describeError(error)));
_owner.serviceRegistrationFailed(this, args);
}
else
{
setupEntryGroup(avahiGroup, it->second.service, it->second.records, it->second.options, true);
}
}
else if (state == AVAHI_ENTRY_GROUP_FAILURE)
{
int error = avahi_client_errno(_avahiClient);
DNSSDResponder::ErrorEventArgs args(serviceHandle, it->second.service, Error(it->second.service.networkInterface(), error, describeError(error)));
_owner.serviceRegistrationFailed(this, args);
}
}
}
void AvahiResponderImpl::reregisterServices()
{
for (ServiceMap::iterator it = _services.begin(); it != _services.end(); ++it)
{
setupEntryGroup(it->first.cast<AvahiEntryGroup*>(), it->second.service, it->second.records, it->second.options, false);
}
}
} } } // namespace Poco::DNSSD::Avahi
namespace Poco {
namespace DNSSD {
namespace
{
Poco::DNSSD::Avahi::AvahiResponderImplFactory implFactory;
}
void initializeDNSSD()
{
Poco::DNSSD::DNSSDResponder::registerImplFactory(implFactory);
}
void uninitializeDNSSD()
{
Poco::DNSSD::DNSSDResponder::unregisterImplFactory();
}
} } // namespace Poco::DNSSD