2013-01-16 14:56:31 +01:00
|
|
|
#include "zeroconf.h"
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
#include "avahi.h"
|
|
|
|
#endif
|
|
|
|
|
2013-01-16 15:26:35 +01:00
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
#include "bonjour.h"
|
|
|
|
#endif
|
|
|
|
|
2013-01-17 14:11:15 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
#include "tinysvcmdns.h"
|
|
|
|
#endif
|
|
|
|
|
2013-02-22 14:39:29 +01:00
|
|
|
#include <QTextCodec>
|
|
|
|
|
2014-02-06 16:49:49 +01:00
|
|
|
Zeroconf* Zeroconf::sInstance = nullptr;
|
2013-01-16 14:56:31 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
Zeroconf::~Zeroconf() {}
|
2013-01-16 14:56:31 +01:00
|
|
|
|
|
|
|
Zeroconf* Zeroconf::GetZeroconf() {
|
|
|
|
if (!sInstance) {
|
2014-02-07 16:34:20 +01:00
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
sInstance = new Avahi;
|
|
|
|
#endif // HAVE_DBUS
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
sInstance = new Bonjour;
|
|
|
|
#endif
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
sInstance = new TinySVCMDNS;
|
|
|
|
#endif
|
2013-01-16 14:56:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
}
|
2013-02-22 14:39:29 +01:00
|
|
|
|
|
|
|
QByteArray Zeroconf::TruncateName(const QString& name) {
|
|
|
|
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
|
|
|
|
QByteArray truncated_utf8;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QChar c : name) {
|
2014-02-06 16:49:49 +01:00
|
|
|
QByteArray rendered = codec->fromUnicode(&c, 1, nullptr);
|
2013-02-22 14:39:29 +01:00
|
|
|
if (truncated_utf8.size() + rendered.size() >= 63) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
truncated_utf8 += rendered;
|
|
|
|
}
|
|
|
|
// NULL-terminate the string.
|
|
|
|
truncated_utf8.append('\0');
|
|
|
|
return truncated_utf8;
|
|
|
|
}
|
2013-02-22 15:29:00 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void Zeroconf::Publish(const QString& domain, const QString& type,
|
|
|
|
const QString& name, quint16 port) {
|
2013-02-22 15:29:00 +01:00
|
|
|
QByteArray truncated_name = TruncateName(name);
|
2014-02-07 16:34:20 +01:00
|
|
|
PublishInternal(domain, type, truncated_name, port);
|
2013-02-22 15:29:00 +01:00
|
|
|
}
|