13 #include "linklocalmanager.h"
17 #include "linklocalhandler.h"
18 #include "connectiontcpclient.h"
25 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
26 # include <arpa/inet.h>
29 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
31 #elif defined( _WIN32_WCE )
32 # include <winsock2.h>
35 #define LINKLOCAL_SERVICE_PORT 5562
37 const std::string LINKLOCAL_SERVICE_TYPE =
"_presence._tcp";
46 : m_publishRef( 0 ), m_browseRef( 0 ), m_user( user ), m_interface( 0 ), m_port( 0 ),
47 m_logInstance( logInstance ), m_browseFd( 0 ), m_server( connHandler, m_logInstance,
EmptyString, LINKLOCAL_SERVICE_PORT ),
48 m_linkLocalHandler( 0 ), m_connectionHandler( connHandler )
51 setPort( LINKLOCAL_SERVICE_PORT );
63 if( value.empty() || key.empty() || key ==
"txtvers" )
66 m_txtData[key] = value;
71 m_txtData.erase( key );
81 std::string txtRecord;
83 txtRecord +=
"txtvers=1";
84 StringMap::const_iterator it = m_txtData.begin();
85 for( ; it != m_txtData.end(); ++it )
87 txtRecord += (char)( (*it).first.length() + (*it).second.length() + 1 );
88 txtRecord += (*it).first;
90 txtRecord += (*it).second;
93 std::string service = m_user +
"@";
97 gethostname( host, 65 );
103 DNSServiceRegister( &m_publishRef,
107 LINKLOCAL_SERVICE_TYPE.c_str(),
111 (
short unsigned int)txtRecord.length(),
112 (
const void*)txtRecord.c_str(),
121 DNSServiceRefDeallocate( m_publishRef );
128 if( !m_linkLocalHandler )
134 DNSServiceErrorType e = DNSServiceBrowse( &m_browseRef,
137 LINKLOCAL_SERVICE_TYPE.c_str(),
141 if ( e != kDNSServiceErr_NoError )
151 DNSServiceRefDeallocate( m_browseRef );
167 FD_SET( DNSServiceRefSockFD( m_browseRef ), &fds );
169 tv.tv_sec = timeout / 1000000;
170 tv.tv_usec = timeout % 1000000;
172 if( select( FD_SETSIZE, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
174 if( FD_ISSET( DNSServiceRefSockFD( m_browseRef ), &fds ) != 0 )
175 DNSServiceProcessResult( m_browseRef );
178 m_server.
recv( timeout );
182 void Manager::handleBrowseReply( DNSServiceRef , DNSServiceFlags flags, uint32_t interfaceIndex,
183 DNSServiceErrorType errorCode,
const char* serviceName,
const char* regtype,
184 const char* replyDomain,
void* context )
186 if( !context || errorCode != kDNSServiceErr_NoError )
189 Flag f = ( flags & kDNSServiceFlagsAdd ) == kDNSServiceFlagsAdd
194 m->handleBrowse( f, serviceName, regtype, replyDomain, interfaceIndex, ( flags & kDNSServiceFlagsMoreComing ) == kDNSServiceFlagsMoreComing );
198 void Manager::handleBrowse(
Flag flag,
const std::string& service,
const std::string& regtype,
const std::string& domain,
int interface,
bool moreComing )
200 Service s( flag, service, regtype, domain, interface );
201 m_tmpServices.push_back( s );
227 m_tmpServices.clear();