00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #include <qeventloop.h>
00024 #include <qapplication.h>
00025 #include <kurl.h>
00026 #ifdef HAVE_SYS_TYPES_H
00027 #include <sys/types.h>
00028 #endif
00029 #include <netinet/in.h>
00030 #include <avahi-client/client.h>
00031 #include <avahi-common/strlst.h>
00032 #ifdef AVAHI_API_0_6
00033 #include <avahi-client/lookup.h>
00034 #endif
00035 #include "remoteservice.h"
00036 #include "responder.h"
00037 #include "sdevent.h"
00038
00039 namespace DNSSD
00040 {
00041 #ifdef AVAHI_API_0_6
00042 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
00043 const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
00044 uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context);
00045 #else
00046 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
00047 const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
00048 uint16_t port, AvahiStringList* txt, void* context);
00049 #endif
00050
00051 class RemoteServicePrivate : public Responder
00052 {
00053 public:
00054 RemoteServicePrivate() : m_resolved(false), m_running(false), m_resolver(0) {}
00055 bool m_resolved;
00056 bool m_running;
00057 AvahiServiceResolver* m_resolver;
00058 void stop() {
00059 m_running = false;
00060 if (m_resolver) avahi_service_resolver_free(m_resolver);
00061 m_resolver=0;
00062 }
00063 };
00064
00065 RemoteService::RemoteService(const QString& label)
00066 {
00067 decode(label);
00068 d = new RemoteServicePrivate();
00069 }
00070 RemoteService::RemoteService(const QString& name,const QString& type,const QString& domain)
00071 : ServiceBase(name, type, domain)
00072 {
00073 d = new RemoteServicePrivate();
00074 }
00075
00076 RemoteService::RemoteService(const KURL& url)
00077 {
00078 d = new RemoteServicePrivate();
00079 if (!url.isValid()) return;
00080 if (url.protocol()!="invitation") return;
00081 if (!url.hasPath()) return;
00082 m_hostName = url.host();
00083 m_port = url.port();
00084 m_type = url.path().section('/',1,1);
00085 m_serviceName = url.path().section('/',2);
00086 m_textData = url.queryItems();
00087 d->m_resolved=true;
00088 }
00089
00090 RemoteService::~RemoteService()
00091 {
00092 if (d->m_resolver) avahi_service_resolver_free(d->m_resolver);
00093 delete d;
00094 }
00095
00096 bool RemoteService::resolve()
00097 {
00098 resolveAsync();
00099 while (d->m_running && !d->m_resolved) Responder::self().process();
00100 d->stop();
00101 return d->m_resolved;
00102 }
00103
00104 void RemoteService::resolveAsync()
00105 {
00106 if (d->m_running) return;
00107 d->m_resolved = false;
00108
00109 #ifdef AVAHI_API_0_6
00110 d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00111 m_serviceName.utf8(), m_type.ascii(), domainToDNS(m_domain), AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_ADDRESS,
00112 resolve_callback, this);
00113 #else
00114 d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00115 m_serviceName.utf8(), m_type.ascii(), m_domain.utf8(), AVAHI_PROTO_UNSPEC, resolve_callback, this);
00116 #endif
00117 if (d->m_resolver) d->m_running=true;
00118 else emit resolved(false);
00119 }
00120
00121 bool RemoteService::isResolved() const
00122 {
00123 return d->m_resolved;
00124 }
00125
00126 void RemoteService::customEvent(QCustomEvent* event)
00127 {
00128 if (event->type() == QEvent::User+SD_ERROR) {
00129 d->stop();
00130 d->m_resolved=false;
00131 emit resolved(false);
00132 }
00133 if (event->type() == QEvent::User+SD_RESOLVE) {
00134 ResolveEvent* rev = static_cast<ResolveEvent*>(event);
00135 m_hostName = rev->m_hostname;
00136 m_port = rev->m_port;
00137 m_textData = rev->m_txtdata;
00138 d->m_resolved = true;
00139 emit resolved(true);
00140 }
00141 }
00142
00143 void RemoteService::virtual_hook(int, void*)
00144 {
00145
00146 }
00147
00148 QDataStream & operator<< (QDataStream & s, const RemoteService & a)
00149 {
00150 s << (static_cast<ServiceBase>(a));
00151 Q_INT8 resolved = a.d->m_resolved ? 1:0;
00152 s << resolved;
00153 return s;
00154 }
00155
00156 QDataStream & operator>> (QDataStream & s, RemoteService & a)
00157 {
00158
00159 a.d->stop();
00160 Q_INT8 resolved;
00161 operator>>(s,(static_cast<ServiceBase&>(a)));
00162 s >> resolved;
00163 a.d->m_resolved = (resolved == 1);
00164 return s;
00165 }
00166
00167 #ifdef AVAHI_API_0_6
00168 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
00169 const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
00170 uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context)
00171 #else
00172 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
00173 const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
00174 uint16_t port, AvahiStringList* txt, void* context)
00175 #endif
00176 {
00177 QObject *obj = reinterpret_cast<QObject*>(context);
00178 if (e != AVAHI_RESOLVER_FOUND) {
00179 ErrorEvent err;
00180 QApplication::sendEvent(obj, &err);
00181 return;
00182 }
00183 QMap<QString,QString> map;
00184 while (txt) {
00185 char *key, *value;
00186 size_t size;
00187 if (avahi_string_list_get_pair(txt,&key,&value,&size)) break;
00188 map[QString::fromUtf8(key)]=(value) ? QString::fromUtf8(value) : QString::null;
00189 txt = txt->next;
00190 }
00191 ResolveEvent rev(DNSToDomain(hostname),port,map);
00192 QApplication::sendEvent(obj, &rev);
00193 }
00194
00195
00196 }
00197
00198 #include "remoteservice.moc"