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>
|
|
|
|
|
2013-01-16 14:56:31 +01:00
|
|
|
Zeroconf* Zeroconf::sInstance = NULL;
|
|
|
|
|
|
|
|
Zeroconf::~Zeroconf() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Zeroconf* Zeroconf::GetZeroconf() {
|
|
|
|
if (!sInstance) {
|
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
sInstance = new Avahi;
|
|
|
|
#endif // HAVE_DBUS
|
2013-01-16 15:26:35 +01:00
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
sInstance = new Bonjour;
|
|
|
|
#endif
|
2013-01-17 14:11:15 +01:00
|
|
|
#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;
|
|
|
|
foreach (QChar c, name) {
|
|
|
|
QByteArray rendered = codec->fromUnicode(&c, 1, NULL);
|
|
|
|
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
|
|
|
|
|
|
|
void Zeroconf::Publish(
|
|
|
|
const QString& domain,
|
|
|
|
const QString& type,
|
|
|
|
const QString& name,
|
|
|
|
quint16 port) {
|
|
|
|
QByteArray truncated_name = TruncateName(name);
|
|
|
|
PublishInternal(
|
|
|
|
domain,
|
|
|
|
type,
|
|
|
|
truncated_name,
|
|
|
|
port);
|
|
|
|
}
|