new cspot/bell

This commit is contained in:
philippe44
2023-05-06 23:50:26 +02:00
parent e0e7e718ba
commit 8bad480112
163 changed files with 6611 additions and 6739 deletions

View File

@@ -1,9 +1,10 @@
#include <vector>
#include <unistd.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <cstring>
#include <vector>
#if __has_include("avahi-client/client.h")
#include <avahi-client/client.h>
@@ -14,40 +15,42 @@
#define BELL_DISABLE_AVAHI
#endif
#include "mdnssvc.h"
#include "BellLogger.h"
#include "MDNSService.h"
#include "mdnssvc.h"
using namespace bell;
#ifndef BELL_DISABLE_AVAHI
static void groupHandler(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) { }
static void groupHandler(AvahiEntryGroup* g, AvahiEntryGroupState state,
AVAHI_GCC_UNUSED void* userdata) {}
#endif
class implMDNSService : public MDNSService {
private:
private:
#ifndef BELL_DISABLE_AVAHI
AvahiEntryGroup *avahiGroup;
AvahiEntryGroup* avahiGroup;
#endif
struct mdns_service* service;
struct mdns_service* service;
public:
public:
#ifndef BELL_DISABLE_AVAHI
static AvahiClient *avahiClient;
static AvahiSimplePoll *avahiPoll;
static AvahiClient* avahiClient;
static AvahiSimplePoll* avahiPoll;
#endif
static struct mdnsd* mdnsServer;
static in_addr_t host;
static struct mdnsd* mdnsServer;
static in_addr_t host;
implMDNSService(struct mdns_service* service) : service(service) { };
#ifndef BELL_DISABLE_AVAHI
implMDNSService(AvahiEntryGroup *avahiGroup) : avahiGroup(avahiGroup) { };
#endif
void unregisterService();
implMDNSService(struct mdns_service* service) : service(service){};
#ifndef BELL_DISABLE_AVAHI
implMDNSService(AvahiEntryGroup* avahiGroup) : avahiGroup(avahiGroup){};
#endif
void unregisterService();
};
struct mdnsd* implMDNSService::mdnsServer = NULL;
in_addr_t implMDNSService::host = INADDR_ANY;
static std::mutex registerMutex;
#ifndef BELL_DISABLE_AVAHI
AvahiClient* implMDNSService::avahiClient = NULL;
AvahiSimplePoll* implMDNSService::avahiPoll = NULL;
@@ -60,130 +63,135 @@ AvahiSimplePoll* implMDNSService::avahiPoll = NULL;
void implMDNSService::unregisterService() {
#ifndef BELL_DISABLE_AVAHI
if (avahiGroup) {
avahi_entry_group_free(avahiGroup);
} else
if (avahiGroup) {
avahi_entry_group_free(avahiGroup);
} else
#endif
{
mdns_service_remove(implMDNSService::mdnsServer, service);
}
{
mdns_service_remove(implMDNSService::mdnsServer, service);
}
}
std::unique_ptr<MDNSService> MDNSService::registerService(
const std::string& serviceName,
const std::string& serviceType,
const std::string& serviceProto,
const std::string& serviceHost,
int servicePort,
const std::map<std::string, std::string> txtData
) {
const std::string& serviceName, const std::string& serviceType,
const std::string& serviceProto, const std::string& serviceHost,
int servicePort, const std::map<std::string, std::string> txtData) {
std::lock_guard lock(registerMutex);
#ifndef BELL_DISABLE_AVAHI
// try avahi first if available
if (!implMDNSService::avahiPoll) {
implMDNSService::avahiPoll = avahi_simple_poll_new();
// try avahi first if available
if (!implMDNSService::avahiPoll) {
implMDNSService::avahiPoll = avahi_simple_poll_new();
}
if (implMDNSService::avahiPoll && !implMDNSService::avahiClient) {
implMDNSService::avahiClient =
avahi_client_new(avahi_simple_poll_get(implMDNSService::avahiPoll),
AvahiClientFlags(0), NULL, NULL, NULL);
}
AvahiEntryGroup* avahiGroup = NULL;
if (implMDNSService::avahiClient &&
(avahiGroup = avahi_entry_group_new(implMDNSService::avahiClient,
groupHandler, NULL)) == NULL) {
BELL_LOG(error, "MDNS", "cannot create service %s", serviceName.c_str());
}
if (avahiGroup != NULL) {
AvahiStringList* avahiTxt = NULL;
for (auto& [key, value] : txtData) {
avahiTxt =
avahi_string_list_add_pair(avahiTxt, key.c_str(), value.c_str());
}
if (implMDNSService::avahiPoll && !implMDNSService::avahiClient) {
implMDNSService::avahiClient = avahi_client_new(avahi_simple_poll_get(implMDNSService::avahiPoll),
AvahiClientFlags(0), NULL, NULL, NULL);
std::string type(serviceType + "." + serviceProto);
int ret = avahi_entry_group_add_service_strlst(
avahiGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0,
serviceName.c_str(), type.c_str(), NULL, NULL, servicePort, avahiTxt);
avahi_string_list_free(avahiTxt);
if (ret >= 0) {
ret = avahi_entry_group_commit(avahiGroup);
}
AvahiEntryGroup *avahiGroup;
if (implMDNSService::avahiClient &&
(avahiGroup = avahi_entry_group_new(implMDNSService::avahiClient, groupHandler, NULL)) == NULL) {
BELL_LOG(error, "MDNS", "cannot create service %s", serviceName.c_str());
}
if (avahiGroup) {
AvahiStringList* avahiTxt = NULL;
for (auto& [key, value] : txtData) {
avahiTxt = avahi_string_list_add_pair(avahiTxt, key.c_str(), value.c_str());
}
std::string type(serviceType + "." + serviceProto);
int ret = avahi_entry_group_add_service_strlst(avahiGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags) 0,
serviceName.c_str(), type.c_str(), NULL, NULL, servicePort, avahiTxt);
avahi_string_list_free(avahiTxt);
if (ret >= 0) {
ret = avahi_entry_group_commit(avahiGroup);
}
if (ret < 0) {
BELL_LOG(error, "MDNS", "cannot run service %s", serviceName.c_str());
avahi_entry_group_free(avahiGroup);
} else {
BELL_LOG(info, "MDNS", "using avahi for %s", serviceName.c_str());
return std::make_unique<implMDNSService>(avahiGroup);
}
if (ret < 0) {
BELL_LOG(error, "MDNS", "cannot run service %s", serviceName.c_str());
avahi_entry_group_free(avahiGroup);
} else {
BELL_LOG(info, "MDNS", "using avahi for %s", serviceName.c_str());
return std::make_unique<implMDNSService>(avahiGroup);
}
}
#endif
// avahi failed, use build-in server
struct ifaddrs* ifaddr;
// avahi failed, use build-in server
struct ifaddrs* ifaddr;
// get the host address first
if (serviceHost.size()) {
struct hostent *h = gethostbyname(serviceHost.c_str());
if (h) {
memcpy(&implMDNSService::host, h->h_addr_list[0], 4);
}
// get the host address first
if (serviceHost.size()) {
struct hostent* h = gethostbyname(serviceHost.c_str());
if (h) {
memcpy(&implMDNSService::host, h->h_addr_list[0], 4);
}
}
// try go guess ifaddr if we have nothing as listening to INADDR_ANY usually does not work
if (implMDNSService::host == INADDR_ANY && getifaddrs(&ifaddr) != -1) {
for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET ||
!(ifa->ifa_flags & IFF_UP) || !(ifa->ifa_flags & IFF_MULTICAST) ||
(ifa->ifa_flags & IFF_LOOPBACK)) continue;
// try go guess ifaddr if we have nothing as listening to INADDR_ANY usually does not work
if (implMDNSService::host == INADDR_ANY && getifaddrs(&ifaddr) != -1) {
for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET ||
!(ifa->ifa_flags & IFF_UP) || !(ifa->ifa_flags & IFF_MULTICAST) ||
(ifa->ifa_flags & IFF_LOOPBACK))
continue;
implMDNSService::host = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
break;
}
freeifaddrs(ifaddr);
}
if (!implMDNSService::mdnsServer) {
char hostname[256];
struct in_addr addr;
// it's the same, but who knows..
addr.s_addr = implMDNSService::host;
gethostname(hostname, sizeof(hostname));
implMDNSService::mdnsServer = mdnsd_start(addr, false);
if (implMDNSService::mdnsServer) {
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, addr);
}
implMDNSService::host =
((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
break;
}
freeifaddrs(ifaddr);
}
if (!implMDNSService::mdnsServer) {
char hostname[256];
struct in_addr addr;
// it's the same, but who knows..
addr.s_addr = implMDNSService::host;
gethostname(hostname, sizeof(hostname));
implMDNSService::mdnsServer = mdnsd_start(addr, false);
if (implMDNSService::mdnsServer) {
std::vector<const char*> txt;
std::vector<std::unique_ptr<std::string>> txtStr;
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, addr);
}
}
for (auto& [key, value] : txtData) {
auto str = make_unique<std::string>(key + "=" + value);
txtStr.push_back(std::move(str));
txt.push_back(txtStr.back()->c_str());
}
if (implMDNSService::mdnsServer) {
std::vector<const char*> txt;
std::vector<std::unique_ptr<std::string>> txtStr;
txt.push_back(NULL);
std::string type(serviceType + "." + serviceProto + ".local");
BELL_LOG(info, "MDNS", "using built-in mDNS for %s", serviceName.c_str());
struct mdns_service* mdnsService = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
if (mdnsService) {
auto service = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
return std::make_unique<implMDNSService>(service);
}
for (auto& [key, value] : txtData) {
auto str = make_unique<std::string>(key + "=" + value);
txtStr.push_back(std::move(str));
txt.push_back(txtStr.back()->c_str());
}
BELL_LOG(error, "MDNS", "cannot start any mDNS listener for %s", serviceName.c_str());
return NULL;
txt.push_back(NULL);
std::string type(serviceType + "." + serviceProto + ".local");
BELL_LOG(info, "MDNS", "using built-in mDNS for %s", serviceName.c_str());
struct mdns_service* mdnsService =
mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
if (mdnsService) {
auto service =
mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
type.c_str(), servicePort, NULL, txt.data());
return std::make_unique<implMDNSService>(service);
}
}
BELL_LOG(error, "MDNS", "cannot start any mDNS listener for %s",
serviceName.c_str());
return NULL;
}

View File

@@ -1,33 +1,33 @@
#include "WrappedSemaphore.h"
#include <sys/time.h>
using namespace bell;
WrappedSemaphore::WrappedSemaphore(int count)
{
sem_init(&this->semaphoreHandle, 0, 0); // eek pointer
WrappedSemaphore::WrappedSemaphore(int count) {
sem_init(&this->semaphoreHandle, 0, 0); // eek pointer
}
WrappedSemaphore::~WrappedSemaphore()
{
sem_destroy(&this->semaphoreHandle);
WrappedSemaphore::~WrappedSemaphore() {
sem_destroy(&this->semaphoreHandle);
}
int WrappedSemaphore::wait()
{
sem_wait(&this->semaphoreHandle);
return 0;
int WrappedSemaphore::wait() {
sem_wait(&this->semaphoreHandle);
return 0;
}
int WrappedSemaphore::twait(long milliseconds)
{
// wait on semaphore with timeout
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec += (milliseconds % 1000) * 1000000;
return sem_timedwait(&this->semaphoreHandle, &ts);
int WrappedSemaphore::twait(long milliseconds) {
// wait on semaphore with timeout
struct timespec ts;
struct timeval tv;
gettimeofday(&tv, 0);
ts.tv_sec = tv.tv_sec + milliseconds / 1000;
ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000;
return sem_timedwait(&this->semaphoreHandle, &ts);
}
void WrappedSemaphore::give()
{
sem_post(&this->semaphoreHandle);
void WrappedSemaphore::give() {
sem_post(&this->semaphoreHandle);
}