publicservice.cpp
/* This file is part of the KDE project
*
* Copyright (C) 2004, 2005 Jakub Stachowski <qbast@go2.pl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "publicservice.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <netinet/in.h>
#include <sys/socket.h>
#include <qapplication.h>
#include <network/ksocketaddress.h>
#include <kurl.h>
#include <unistd.h>
#include "sdevent.h"
#include "responder.h"
#include "servicebrowser.h"
#include "settings.h"
namespace DNSSD
{
static unsigned long publicIP();
#ifdef HAVE_DNSSD
void publish_callback (DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name,
const char*, const char*, void *context);
#endif
class PublicServicePrivate : public Responder
{
public:
PublicServicePrivate() : m_published(false)
{}
bool m_published;
};
PublicService::PublicService(const QString& name, const QString& type, unsigned int port,
const QString& domain)
: QObject(), ServiceBase(name, type, QString::null, domain, port)
{
d = new PublicServicePrivate;
if (domain.isNull())
if (Configuration::publishType()==Configuration::EnumPublishType::LAN) m_domain="local.";
else m_domain=Configuration::publishDomain();
}
PublicService::~PublicService()
{
stop();
delete d;
}
void PublicService::setServiceName(const QString& serviceName)
{
m_serviceName = serviceName;
if (d->isRunning()) {
stop();
publishAsync();
}
}
void PublicService::setDomain(const QString& domain)
{
m_domain = domain;
if (d->isRunning()) {
stop();
publishAsync();
}
}
void PublicService::setType(const QString& type)
{
m_type = type;
if (d->isRunning()) {
stop();
publishAsync();
}
}
void PublicService::setPort(unsigned short port)
{
m_port = port;
if (d->isRunning()) {
stop();
publishAsync();
}
}
bool PublicService::isPublished() const
{
return d->m_published;
}
void PublicService::setTextData(const QMap<QString,QString>& textData)
{
m_textData = textData;
if (d->isRunning()) {
stop();
publishAsync();
}
}
bool PublicService::publish()
{
publishAsync();
while (d->isRunning() && !d->m_published) d->process();
return d->m_published;
}
void PublicService::stop()
{
d->stop();
d->m_published = false;
}
void PublicService::publishAsync()
{
if (d->isRunning()) stop();
#ifdef HAVE_DNSSD
if (ServiceBrowser::isAvailable()==ServiceBrowser::Working) {
TXTRecordRef txt;
TXTRecordCreate(&txt,0,0);
QMap<QString,QString>::ConstIterator itEnd = m_textData.end();
for (QMap<QString,QString>::ConstIterator it = m_textData.begin(); it!=itEnd ; ++it) {
QCString value = it.data().utf8();
if (TXTRecordSetValue(&txt,it.key().utf8(),value.length(),value)!=kDNSServiceErr_NoError) {
TXTRecordDeallocate(&txt);
emit published(false);
return;
}
}
DNSServiceRef ref;
if (DNSServiceRegister(&ref,0,0,m_serviceName.utf8(),m_type.ascii(),domainToDNS(m_domain),NULL,
htons(m_port),TXTRecordGetLength(&txt),TXTRecordGetBytesPtr(&txt),publish_callback,
reinterpret_cast<void*>(this)) == kDNSServiceErr_NoError) d->setRef(ref);
TXTRecordDeallocate(&txt);
}
#endif
if (!d->isRunning()) emit published(false);
}
#ifdef HAVE_DNSSD
void publish_callback (DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name,
const char*, const char*, void *context)
{
QObject *obj = reinterpret_cast<QObject*>(context);
if (errorCode != kDNSServiceErr_NoError) {
ErrorEvent err;
QApplication::sendEvent(obj, &err);
} else {
PublishEvent pev(QString::fromUtf8(name));
QApplication::sendEvent(obj, &pev);
}
}
#endif
const KURL PublicService::toInvitation(const QString& host)
{
KURL url;
url.setProtocol("invitation");
if (host.isEmpty()) { // select best address
unsigned long s_address = publicIP();
if (!s_address) return KURL();
KNetwork::KIpAddress addr(s_address);
url.setHost(addr.toString());
} else url.setHost(host);
//FIXME: if there is no public interface, select any non-loopback
url.setPort(m_port);
url.setPath("/"+m_type+"/"+KURL::encode_string(m_serviceName));
QString query;
QMap<QString,QString>::ConstIterator itEnd = m_textData.end();
for (QMap<QString,QString>::ConstIterator it = m_textData.begin(); it!=itEnd ; ++it)
url.addQueryItem(it.key(),it.data());;
return url;
}
void PublicService::customEvent(QCustomEvent* event)
{
if (event->type()==QEvent::User+SD_ERROR) {
stop();
emit published(false);
}
if (event->type()==QEvent::User+SD_PUBLISH) {
d->m_published=true;
emit published(true);
m_serviceName = static_cast<PublishEvent*>(event)->m_name;
}
}
void PublicService::virtual_hook(int, void*)
{
}
static unsigned long publicIP()
{
struct sockaddr_in addr;
socklen_t len = sizeof(addr);
int sock = socket(AF_INET,SOCK_DGRAM,0);
if (sock == -1) return 0;
addr.sin_family = AF_INET;
addr.sin_port = 1; // Not important, any port and public address will do
addr.sin_addr.s_addr = 0x11111111;
if ((connect(sock,(const struct sockaddr*)&addr,sizeof(addr))) == -1) { close(sock); return 0; }
if ((getsockname(sock,(struct sockaddr*)&addr, &len)) == -1) { close(sock); return 0; }
::close(sock);
return addr.sin_addr.s_addr;
}
}
#include "publicservice.moc"
DNSSD::PublicService::stop
void stop()
Stops publishing or abort incomplete publish request.
Definition: publicservice.cpp:128
DNSSD::ServiceBrowser::isAvailable
static const State isAvailable()
Checks availability of DNS-SD services (this also covers publishing).
Definition: servicebrowser.cpp:86
DNSSD::PublicService::publishAsync
void publishAsync()
Asynchronous version of publish().
Definition: publicservice.cpp:134
DNSSD::PublicService::publish
bool publish()
Synchrounous publish.
Definition: publicservice.cpp:121
DNSSD::PublicService::setType
void setType(const QString &type)
Sets type of service.
Definition: publicservice.cpp:89
DNSSD::ServiceBase::type
const QString & type() const
Returns type of service.
Definition: servicebase.cpp:74
DNSSD::ServiceBase
This class is used to carry information about service.
Definition: servicebase.h:40
DNSSD::PublicService::setServiceName
void setServiceName(const QString &serviceName)
Sets name of the service.
Definition: publicservice.cpp:70
DNSSD::PublicService::isPublished
bool isPublished() const
Returns true is currently published.
Definition: publicservice.cpp:107
DNSSD::ServiceBase::port
unsigned short port() const
Returns port number.
Definition: servicebase.cpp:89
DNSSD::ServiceBase::m_textData
QMap< QString, QString > m_textData
Map of TXT properties.
Definition: servicebase.h:99
DNSSD::PublicService::toInvitation
const KURL toInvitation(const QString &host=QString::null)
Translates service into URL that can be sent to another user.
Definition: publicservice.cpp:175
DNSSD::PublicService::setPort
void setPort(unsigned short port)
Sets port.
Definition: publicservice.cpp:98
DNSSD::ServiceBase::textData
const QMap< QString, QString > & textData() const
Returns read only map of text properties.
Definition: servicebase.cpp:93
DNSSD::PublicService::setDomain
void setDomain(const QString &domain)
Sets domain where service is published.
Definition: publicservice.cpp:79
DNSSD::PublicService::setTextData
void setTextData(const QMap< QString, QString > &textData)
Sets new text properties.
Definition: publicservice.cpp:112
DNSSD::PublicService::PublicService
PublicService(const QString &name=QString::null, const QString &type=QString::null, unsigned int port=0, const QString &domain=QString::null)
Definition: publicservice.cpp:53
DNSSD::ServiceBase::serviceName
const QString & serviceName() const
Returns name of service.
Definition: servicebase.cpp:69
DNSSD::ServiceBase::domain
const QString & domain() const
Returns domain that given service belongs to.
Definition: servicebase.cpp:79
DNSSD::PublicService::published
void published(bool)
Emitted when publishing is complete - parameter is set to true if it was successfull.

KDE Home | KDE Accessibility Home | Description of Access Keys