File: linklocalmanager.cpp

package info (click to toggle)
gloox 1.0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,732 kB
  • ctags: 6,556
  • sloc: cpp: 45,479; sh: 11,344; makefile: 1,018
file content (255 lines) | stat: -rw-r--r-- 8,314 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
  Copyright (c) 2012-2016 by Jakob Schröter <js@camaya.net>
  This file is part of the gloox library. http://camaya.net/gloox

  This software is distributed under a license. The full license
  agreement can be found in the file LICENSE in this distribution.
  This software may not be copied, modified, sold or distributed
  other than expressed in the named license agreement.

  This software is distributed without any warranty.
*/

#include "linklocalmanager.h"

#ifdef HAVE_MDNS

#include "linklocalhandler.h"
#include "connectiontcpclient.h"
#include "jid.h"
#include "util.h"

#include <cstdio>

#if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
# include <unistd.h>
# include <arpa/inet.h>
#endif

#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
# include <winsock.h>
#elif defined( _WIN32_WCE )
# include <winsock2.h>
#endif

#define LINKLOCAL_SERVICE_PORT 5562

const std::string LINKLOCAL_SERVICE_TYPE = "_presence._tcp";

namespace gloox
{

  namespace LinkLocal
  {

    Manager::Manager( const std::string& user, ConnectionHandler* connHandler, const LogSink &logInstance )
      : m_publishRef( 0 ), m_browseRef( 0 ), m_user( user ), m_interface( 0 ), m_port( 0 ),
        m_logInstance( logInstance ), m_browseFd( 0 ), m_server( connHandler, m_logInstance, EmptyString, LINKLOCAL_SERVICE_PORT ),
        m_linkLocalHandler( 0 ), m_connectionHandler( connHandler )
    {

      setPort( LINKLOCAL_SERVICE_PORT ); // does more than just setting m_port
      addTXTData( "node", GLOOX_CAPS_NODE );
    }

    Manager::~Manager()
    {
      deregisterService();
      stopBrowsing();
    }

    void Manager::addTXTData( const std::string& key, const std::string& value )
    {
      if( value.empty() || key.empty() || key == "txtvers" )
        return;

      m_txtData[key] = value;
    }

    void Manager::removeTXTData( const std::string& key )
    {
      m_txtData.erase( key );
    }

    void Manager::registerService()
    {
      if( m_publishRef )
        deregisterService();

      m_server.connect();

      std::string txtRecord;
      txtRecord += (char)9; // length of mandatory txtvers=1
      txtRecord += "txtvers=1"; // this is here because it SHOULD be the first entry
      StringMap::const_iterator it = m_txtData.begin();
      for( ; it != m_txtData.end(); ++it )
      {
        txtRecord += (char)( (*it).first.length() + (*it).second.length() + 1 );
        txtRecord += (*it).first;
        txtRecord += '=';
        txtRecord += (*it).second;
      }

      std::string service = m_user + "@";
      if( m_host.empty() )
      {
        char host[65];
        gethostname( host, 65 );
        service += host;
      }
      else
        service += m_host;

      /*DNSServiceErrorType e =*/ DNSServiceRegister( &m_publishRef,
                                                  0,                                      // flags
                                                  m_interface,                            // interface, 0 = any, -1 = local only
                                                  service.c_str(),                        // service name, 0 = local computer name
                                                  LINKLOCAL_SERVICE_TYPE.c_str(),         // service type
                                                  m_domain.c_str(),                       // domain, 0 = default domain(s)
                                                  m_host.c_str(),                         // host, 0 = default host name(s)
                                                  htons( m_port ),                        // port
                                                  (short unsigned int)txtRecord.length(), // TXT record length
                                                  (const void*)txtRecord.c_str(),         // TXT record
                                                  0,                                      // callback
                                                  0 );                                    // context
    }

    void Manager::deregisterService()
    {
      if( m_publishRef )
      {
        DNSServiceRefDeallocate( m_publishRef );
        m_publishRef = 0;
      }
    }

    bool Manager::startBrowsing()
    {
      if( !m_linkLocalHandler )
        return false;

      if( m_browseRef )
        stopBrowsing();

      DNSServiceErrorType e = DNSServiceBrowse( &m_browseRef,
                                                0,                              // flags, currently ignored
                                                m_interface,                    // interface, 0 = any, -1 = local only
                                                LINKLOCAL_SERVICE_TYPE.c_str(), // service type
                                                m_domain.c_str(),               // domain, 0 = default domain(s)
                                                &handleBrowseReply,             // callback
                                                this );                         // context
      if ( e != kDNSServiceErr_NoError )
        return false;

      return true;
    }

    void Manager::stopBrowsing()
    {
      if( m_browseRef )
      {
        DNSServiceRefDeallocate( m_browseRef );
        m_browseRef = 0;
      }
    }

    void Manager::recv( int timeout )
    {
      if( !m_browseRef )
        return;

      struct timeval tv;

      fd_set fds;
      FD_ZERO( &fds );
      // the following causes a C4127 warning in VC++ Express 2008 and possibly other versions.
      // however, the reason for the warning can't be fixed in gloox.
      FD_SET( DNSServiceRefSockFD( m_browseRef ), &fds );

      tv.tv_sec = timeout / 1000000;
      tv.tv_usec = timeout % 1000000;

      if( select( FD_SETSIZE, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
      {
        if( FD_ISSET( DNSServiceRefSockFD( m_browseRef ), &fds ) != 0 )
          DNSServiceProcessResult( m_browseRef );
      }

      m_server.recv( timeout );
    }


    void Manager::handleBrowseReply( DNSServiceRef /*sdRef*/, DNSServiceFlags flags, uint32_t interfaceIndex,
                                     DNSServiceErrorType errorCode, const char* serviceName, const char* regtype,
                                     const char* replyDomain, void* context )
    {
      if( !context || errorCode != kDNSServiceErr_NoError )
        return;

      Flag f = ( flags & kDNSServiceFlagsAdd ) == kDNSServiceFlagsAdd
                                              ? AddService
                                              : RemoveService;

      Manager* m = static_cast<Manager*>( context );
      m->handleBrowse( f, serviceName, regtype, replyDomain, interfaceIndex, ( flags & kDNSServiceFlagsMoreComing ) == kDNSServiceFlagsMoreComing );

    }

    void Manager::handleBrowse( Flag flag, const std::string& service, const std::string& regtype, const std::string& domain, int iface, bool moreComing )
    {
      Service s( flag, service, regtype, domain, interface );
      m_tmpServices.push_back( s );

//       switch( flag )
//       {
//         case AddService:
//         {
//           m_services.push_back( s );
//           break;
//         }
//         case RemoveService:
//         {
//           ServiceList::iterator it = m_services.begin();
//           for( ; it != m_services.end(); ++it )
//           {
//             if( (*it)->service == service && (*it)->regtype == regtype && (*it)->domain == domain )
//             {
//               m_services.erase( it );
//             }
//           }
//           break;
//         }
//       }

      if( !moreComing )
      {
        m_linkLocalHandler->handleBrowseReply( m_tmpServices );
        m_tmpServices.clear();
      }
    }


//     const StringMap Manager::decodeTXT( const std::string& txt )
//     {
//       StringMap result;
//       if( txt.empty() )
//         return result;
//
//       std::string::const_iterator it = txt.begin();
//       while( it < txt.end() )
//       {
//         int len = (int)(*it);
//         std::string tmp( ++it, it + len + 1 );
//         it += len;
//         size_t pos = tmp.find( '=' );
//         result.insert( std::make_pair( tmp.substr( 0, pos ), tmp.substr( pos + 1 ) ) );
//       }
//
//       return result;
//     }

  }

}

#endif // HAVE_MDNS