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'>";
An abstract base class for a connection.
ConnectionBase * m_connection
An implementation of a link-local client.
virtual void handleStartNode(const Tag *start)
The namespace for the gloox library.
virtual ConnectionError recv(int timeout=-1)
const std::string & findAttribute(const std::string &name) const
bool connect(bool block=true)
virtual void handleConnect(const ConnectionBase *connection)
const std::string & full() const
void setConnectionImpl(ConnectionBase *cb)
ConnectionState state() const
virtual ConnectionError recv(int timeout=-1)
const std::string EmptyString
This is an abstraction of an XML element.