13 #include "linklocalclient.h"
20 #include "connectiontcpclient.h"
24 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
25 # include <arpa/inet.h>
28 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
30 #elif defined( _WIN32_WCE )
31 # include <winsock2.h>
42 m_interface( 0 ), m_port( 0 ), m_streamSent( false )
56 const std::string& domain,
int iface )
58 m_interface = interface;
59 return resolve( service, type, domain );
77 FD_SET( DNSServiceRefSockFD( m_currentRef ), &fds );
79 tv.tv_sec = timeout / 1000000;
80 tv.tv_usec = timeout % 1000000;
82 if( select( FD_SETSIZE, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
84 if( FD_ISSET( DNSServiceRefSockFD( m_currentRef ), &fds ) != 0 )
85 DNSServiceProcessResult( m_currentRef );
93 bool Client::resolve(
const std::string& service,
const std::string& type,
94 const std::string& domain )
98 DNSServiceErrorType e = DNSServiceResolve( &m_rRef, 0, m_interface, service.c_str(), type.c_str(),
99 domain.c_str(), (DNSServiceResolveReply)&handleResolveReply,
this );
100 if( e != kDNSServiceErr_NoError )
102 DNSServiceRefDeallocate( m_rRef );
106 m_currentRef = m_rRef;
111 void Client::handleResolveReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
112 DNSServiceErrorType errorCode,
const char* fullname,
const char* hosttarget,
113 uint16_t port, uint16_t txtLen,
const unsigned char* txtRecord,
void* context )
115 if( !context || errorCode != kDNSServiceErr_NoError )
120 static_cast<Client*
>( context )->query( hosttarget, ntohs(
port ) );
123 bool Client::query(
const std::string& hostname,
int port )
127 DNSServiceErrorType e = DNSServiceQueryRecord( &m_qRef, 0, m_interface, hostname.c_str(), kDNSServiceType_A,
128 kDNSServiceClass_IN, (DNSServiceQueryRecordReply)&handleQueryReply,
this );
129 if( e != kDNSServiceErr_NoError )
132 DNSServiceRefDeallocate( m_qRef );
136 m_currentRef = m_qRef;
141 void Client::handleQueryReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
142 DNSServiceErrorType errorCode,
const char *fullname, uint16_t rrtype,
143 uint16_t rrclass, uint16_t rdlen,
const void *rdata, uint32_t ttl,
148 if( !context || errorCode != kDNSServiceErr_NoError )
151 const unsigned char* rd =
static_cast<const unsigned char*
>( rdata );
152 std::string addr = util::int2string( rd[0] );
154 addr += util::int2string( rd[1] );
156 addr += util::int2string( rd[2] );
158 addr += util::int2string( rd[3] );
160 static_cast<Client*
>( context )->handleQuery( addr );
163 void Client::handleQuery(
const std::string& addr )
167 DNSServiceRefDeallocate( m_rRef );
171 ConnectionTCPClient* connection =
new ConnectionTCPClient(
this,
logInstance(), addr, m_port );
185 DNSServiceRefDeallocate( m_qRef );
200 if( start && !m_streamSent )
204 void Client::sendStart(
const std::string& to )
207 std::string s =
"<?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' to='";
211 s +=
"' version='1.0'>";
virtual ConnectionError recv(int timeout=-1)
bool connect(bool block=true)
void setConnectionImpl(ConnectionBase *cb)
ConnectionBase * m_connection
An abstract base class for a connection.
ConnectionState state() const
const std::string & full() const
An implementation of a link-local client.
virtual ConnectionError recv(int timeout=-1)
virtual void handleConnect(const ConnectionBase *connection)
virtual void handleStartNode(const Tag *start)
This is an abstraction of an XML element.
const std::string & findAttribute(const std::string &name) const
The namespace for the gloox library.
const std::string EmptyString