MythTV  master
ssdp.cpp
Go to the documentation of this file.
1 // Program Name: ssdp.cpp
3 // Created : Oct. 1, 2005
4 //
5 // Purpose : SSDP Discovery Service Implmenetation
6 //
7 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 #include <algorithm>
14 #include <cerrno>
15 #include <chrono> // for milliseconds
16 #include <cstdlib>
17 #include <thread> // for sleep_for
18 
19 #include "upnp.h"
20 #include "mythlogging.h"
21 
22 #include "upnptasksearch.h"
23 #include "upnptaskcache.h"
24 
25 #include "mmulticastsocketdevice.h"
26 #include "mbroadcastsocketdevice.h"
27 
28 #include <QRegExp>
29 #include <QStringList>
30 
31 #ifdef ANDROID
32 #include <sys/select.h>
33 #endif
34 
35 using namespace std;
36 
39 //
40 // SSDP Class Implementation
41 //
44 
45 // We're creating this class immediately so it will always be available.
46 
47 static QMutex g_pSSDPCreationLock;
48 SSDP* SSDP::g_pSSDP = nullptr;
49 
51 //
53 
55 {
56  QMutexLocker locker(&g_pSSDPCreationLock);
57  return g_pSSDP ? g_pSSDP : (g_pSSDP = new SSDP());
58 }
59 
61 //
63 
65 {
66  QMutexLocker locker(&g_pSSDPCreationLock);
67  delete g_pSSDP;
68  g_pSSDP = nullptr;
69 }
70 
72 //
74 
76  MThread("SSDP")
77 {
78  LOG(VB_UPNP, LOG_NOTICE, "Starting up SSDP Thread..." );
79 
81 
82  m_nPort = pConfig->GetValue("UPnP/SSDP/Port" , SSDP_PORT );
83  m_nSearchPort = pConfig->GetValue("UPnP/SSDP/SearchPort", SSDP_SEARCHPORT);
84 
90  new MBroadcastSocketDevice("255.255.255.255", m_nPort);
91 
92  m_Sockets[ SocketIdx_Search ]->setBlocking( false );
93  m_Sockets[ SocketIdx_Multicast ]->setBlocking( false );
94  m_Sockets[ SocketIdx_Broadcast ]->setBlocking( false );
95 
96  // Setup SearchSocket
97  QHostAddress ip4addr( QHostAddress::Any );
98 
99  m_Sockets[ SocketIdx_Search ]->bind( ip4addr , m_nSearchPort );
100  m_Sockets[ SocketIdx_Search ]->bind( QHostAddress::Any, m_nSearchPort );
101 
102  // ----------------------------------------------------------------------
103  // Create the SSDP (Upnp Discovery) Thread.
104  // ----------------------------------------------------------------------
105 
106  start();
107 
108  LOG(VB_UPNP, LOG_INFO, "SSDP Thread Starting soon" );
109 }
110 
112 //
114 
116 {
117  LOG(VB_UPNP, LOG_NOTICE, "Shutting Down SSDP Thread..." );
118 
120 
121  m_bTermRequested = true;
122  wait();
123 
124  if (m_pNotifyTask != nullptr)
125  {
127  m_pNotifyTask = nullptr;
128  }
129 
130  for (int nIdx = 0; nIdx < kNumberOfSockets; nIdx++ )
131  {
132  if (m_Sockets[ nIdx ] != nullptr )
133  {
134  delete m_Sockets[ nIdx ];
135  }
136  }
137 
138  LOG(VB_UPNP, LOG_INFO, "SSDP Thread Terminated." );
139 }
140 
142 {
143  m_bTermRequested = true;
144 }
145 
147 //
149 
150 void SSDP::EnableNotifications( int nServicePort )
151 {
152  if ( m_pNotifyTask == nullptr )
153  {
154  m_nServicePort = nServicePort;
155 
156  LOG(VB_UPNP, LOG_INFO,
157  "SSDP::EnableNotifications() - creating new task");
159 
160  // ------------------------------------------------------------------
161  // First Send out Notification that we are leaving the network.
162  // ------------------------------------------------------------------
163 
164  LOG(VB_UPNP, LOG_INFO,
165  "SSDP::EnableNotifications() - sending NTS_byebye");
167  m_pNotifyTask->Execute( nullptr );
168 
170  }
171 
172  // ------------------------------------------------------------------
173  // Add Announcement Task to the Queue
174  // ------------------------------------------------------------------
175 
176  LOG(VB_UPNP, LOG_INFO, "SSDP::EnableNotifications() - sending NTS_alive");
177 
179 
181 
182  LOG(VB_UPNP, LOG_INFO,
183  "SSDP::EnableNotifications() - Task added to UPnP queue");
184 }
185 
187 //
189 
191 {
192  m_bAnnouncementsEnabled = false;
193 
194  if (m_pNotifyTask != nullptr)
195  {
196  // Send Announcement that we are leaving.
197 
199  m_pNotifyTask->Execute( nullptr );
200  }
201 }
202 
204 //
206 void SSDP::PerformSearch(const QString &sST, uint timeout_secs)
207 {
208  timeout_secs = std::max(std::min(timeout_secs, 5U), 1U);
209  QString rRequest = QString("M-SEARCH * HTTP/1.1\r\n"
210  "HOST: 239.255.255.250:1900\r\n"
211  "MAN: \"ssdp:discover\"\r\n"
212  "MX: %1\r\n"
213  "ST: %2\r\n"
214  "\r\n")
215  .arg(timeout_secs).arg(sST);
216 
217  LOG(VB_UPNP, LOG_DEBUG, QString("\n\n%1\n").arg(rRequest));
218 
219  QByteArray sRequest = rRequest.toUtf8();
220 
221  MSocketDevice *pSocket = m_Sockets[ SocketIdx_Search ];
222  if ( !pSocket->isValid() )
223  {
224  pSocket->setProtocol(MSocketDevice::IPv4);
225  pSocket->setSocket(pSocket->createNewSocket(), MSocketDevice::Datagram);
226  }
227 
228  QHostAddress address;
229  address.setAddress( SSDP_GROUP );
230 
231  int nSize = sRequest.size();
232 
233  if ( pSocket->writeBlock( sRequest.data(),
234  sRequest.size(), address, SSDP_PORT ) != nSize)
235  LOG(VB_GENERAL, LOG_INFO,
236  "SSDP::PerformSearch - did not write entire buffer.");
237 
238  std::this_thread::sleep_for(std::chrono::milliseconds(random() % 250));
239 
240  if ( pSocket->writeBlock( sRequest.data(),
241  sRequest.size(), address, SSDP_PORT ) != nSize)
242  LOG(VB_GENERAL, LOG_INFO,
243  "SSDP::PerformSearch - did not write entire buffer.");
244 }
245 
247 //
249 
250 void SSDP::run()
251 {
252  RunProlog();
253 
254  fd_set read_set;
255  struct timeval timeout {};
256 
257  LOG(VB_UPNP, LOG_INFO, "SSDP::Run - SSDP Thread Started." );
258 
259  // ----------------------------------------------------------------------
260  // Listen for new Requests
261  // ----------------------------------------------------------------------
262 
263  while ( ! m_bTermRequested )
264  {
265  int nMaxSocket = 0;
266 
267  FD_ZERO( &read_set );
268 
269  for (size_t nIdx = 0; nIdx < kNumberOfSockets; nIdx++ )
270  {
271  if (m_Sockets[nIdx] != nullptr && m_Sockets[nIdx]->socket() >= 0)
272  {
273  FD_SET( m_Sockets[ nIdx ]->socket(), &read_set );
274  nMaxSocket = max( m_Sockets[ nIdx ]->socket(), nMaxSocket );
275 
276 #if 0
277  if (m_Sockets[ nIdx ]->bytesAvailable() > 0)
278  {
279  LOG(VB_GENERAL, LOG_DEBUG,
280  QString("Found Extra data before select: %1")
281  .arg(nIdx));
282  ProcessData( m_Sockets[ nIdx ] );
283  }
284 #endif
285  }
286  }
287 
288  timeout.tv_sec = 1;
289  timeout.tv_usec = 0;
290 
291  int count = select(nMaxSocket + 1, &read_set, nullptr, nullptr, &timeout);
292 
293  for (int nIdx = 0; count && nIdx < kNumberOfSockets; nIdx++ )
294  {
295  bool cond1 = m_Sockets[nIdx] != nullptr;
296  bool cond2 = cond1 && m_Sockets[nIdx]->socket() >= 0;
297  bool cond3 = cond2 && FD_ISSET(m_Sockets[nIdx]->socket(), &read_set);
298 
299  if (cond3)
300  {
301 #if 0
302  LOG(VB_GENERAL, LOG_DEBUG, QString("FD_ISSET( %1 )").arg(nIdx));
303 #endif
304 
305  ProcessData(m_Sockets[nIdx]);
306  count--;
307  }
308  }
309  }
310 
311  RunEpilog();
312 }
313 
315 //
317 
318 void SSDP::ProcessData( MSocketDevice *pSocket )
319 {
320  QByteArray buffer;
321  long nBytes = pSocket->bytesAvailable();
322  int retries = 0;
323  // Note: this function MUST do a read even if someone sends a zero byte UDP message
324  // Otherwise the select() will continue to signal data ready, so to prevent using 100%
325  // CPU, we need to call a recv function to make select() block again
326  bool didDoRead = false;
327 
328  // UDP message of zero length? OK, "recv" it and move on
329  if (nBytes == 0)
330  {
331  LOG(VB_UPNP, LOG_WARNING, QString("SSDP: Received 0 byte UDP message"));
332  }
333 
334  while ((nBytes = pSocket->bytesAvailable()) > 0 || (nBytes == 0 && !didDoRead))
335  {
336  buffer.resize(nBytes);
337 
338  long nRead = 0;
339  do
340  {
341  long ret = pSocket->readBlock( buffer.data() + nRead, nBytes - nRead );
342  didDoRead = true;
343  if (ret < 0)
344  {
345  if (errno == EAGAIN
346 #if EAGAIN != EWOULDBLOCK
347  || errno == EWOULDBLOCK
348 #endif
349  )
350  {
351  if (retries == 3)
352  {
353  nBytes = nRead;
354  buffer.resize(nBytes);
355  break;
356  }
357  retries++;
358  std::this_thread::sleep_for(std::chrono::milliseconds(10));
359  continue;
360  }
361  LOG(VB_GENERAL, LOG_ERR, QString("Socket readBlock error %1")
362  .arg(pSocket->error()));
363  buffer.clear();
364  break;
365  }
366  retries = 0;
367 
368  nRead += ret;
369 
370  if (0 == ret && nBytes != 0)
371  {
372  LOG(VB_SOCKET, LOG_WARNING,
373  QString("%1 bytes reported available, "
374  "but only %2 bytes read.")
375  .arg(nBytes).arg(nRead));
376  nBytes = nRead;
377  buffer.resize(nBytes);
378  break;
379  }
380  }
381  while (nRead < nBytes);
382 
383  if (buffer.isEmpty())
384  continue;
385 
386  QHostAddress peerAddress = pSocket->peerAddress();
387  quint16 peerPort = pSocket->peerPort ();
388 
389  // ------------------------------------------------------------------
390  QString str = QString(buffer.constData());
391  QStringList lines = str.split("\r\n", QString::SkipEmptyParts);
392  QString sRequestLine = !lines.empty() ? lines[0] : "";
393 
394  if (!lines.isEmpty())
395  lines.pop_front();
396 
397  // ------------------------------------------------------------------
398  // Parse request Type
399  // ------------------------------------------------------------------
400 
401  LOG(VB_UPNP, LOG_DEBUG, QString("SSDP::ProcessData - requestLine: %1")
402  .arg(sRequestLine));
403 
404  SSDPRequestType eType = ProcessRequestLine( sRequestLine );
405 
406  // ------------------------------------------------------------------
407  // Read Headers into map
408  // ------------------------------------------------------------------
409 
410  QStringMap headers;
411 
412  for ( QStringList::Iterator it = lines.begin();
413  it != lines.end(); ++it )
414  {
415  QString sLine = *it;
416  QString sName = sLine.section( ':', 0, 0 ).trimmed();
417  QString sValue = sLine.section( ':', 1 );
418 
419  sValue.truncate( sValue.length() ); //-2
420 
421  if ((sName.length() != 0) && (sValue.length() !=0))
422  headers.insert( sName.toLower(), sValue.trimmed() );
423  }
424 
425 #if 0
426  pSocket->SetDestAddress( peerAddress, peerPort );
427 #endif
428 
429  // --------------------------------------------------------------
430  // See if this is a valid request
431  // --------------------------------------------------------------
432 
433  switch( eType )
434  {
435  case SSDP_MSearch:
436  {
437  // ----------------------------------------------------------
438  // If we haven't enabled notifications yet, then we don't
439  // want to answer search requests.
440  // ----------------------------------------------------------
441 
442  if (m_pNotifyTask != nullptr)
443  ProcessSearchRequest( headers, peerAddress, peerPort );
444 
445  break;
446  }
447 
448  case SSDP_MSearchResp:
449  ProcessSearchResponse( headers);
450  break;
451 
452  case SSDP_Notify:
453  ProcessNotify( headers );
454  break;
455 
456  case SSDP_Unknown:
457  default:
458  LOG(VB_UPNP, LOG_ERR,
459  "SSPD::ProcessData - Unknown request Type.");
460  break;
461  }
462  }
463 }
464 
466 //
468 
470 {
471  QStringList tokens = sLine.split(m_procReqLineExp, QString::SkipEmptyParts);
472 
473  // ----------------------------------------------------------------------
474  // if this is actually a response, then sLine's format will be:
475  // HTTP/m.n <response code> <response text>
476  // otherwise:
477  // <method> <Resource URI> HTTP/m.n
478  // ----------------------------------------------------------------------
479 
480  if ( sLine.startsWith( QString("HTTP/") ))
481  return SSDP_MSearchResp;
482 
483  if (tokens.count() > 0)
484  {
485  if (tokens[0] == "M-SEARCH" ) return SSDP_MSearch;
486  if (tokens[0] == "NOTIFY" ) return SSDP_Notify;
487  }
488 
489  return SSDP_Unknown;
490 }
491 
493 //
495 
496 QString SSDP::GetHeaderValue( const QStringMap &headers,
497  const QString &sKey, const QString &sDefault )
498 {
499  QStringMap::const_iterator it = headers.find( sKey.toLower() );
500 
501  if ( it == headers.end())
502  return( sDefault );
503 
504  return *it;
505 }
506 
508 //
510 
511 bool SSDP::ProcessSearchRequest( const QStringMap &sHeaders,
512  const QHostAddress& peerAddress,
513  quint16 peerPort )
514 {
515  QString sMAN = GetHeaderValue( sHeaders, "MAN", "" );
516  QString sST = GetHeaderValue( sHeaders, "ST" , "" );
517  QString sMX = GetHeaderValue( sHeaders, "MX" , "" );
518  int nMX = 0;
519 
520  LOG(VB_UPNP, LOG_DEBUG, QString("SSDP::ProcessSearchrequest : [%1] MX=%2")
521  .arg(sST).arg(sMX));
522 
523  // ----------------------------------------------------------------------
524  // Validate Header Values...
525  // ----------------------------------------------------------------------
526 
527 #if 0
528  if ( pRequest->m_sMethod != "*" ) return false;
529  if ( pRequest->m_sProtocol != "HTTP" ) return false;
530  if ( pRequest->m_nMajor != 1 ) return false;
531 #endif
532  if ( sMAN != "\"ssdp:discover\"" ) return false;
533  if ( sST.length() == 0 ) return false;
534  if ( sMX.length() == 0 ) return false;
535  if ((nMX = sMX.toInt()) == 0 ) return false;
536  if ( nMX < 0 ) return false;
537 
538  // ----------------------------------------------------------------------
539  // Adjust timeout to be a random interval between 0 and MX (max of 120)
540  // ----------------------------------------------------------------------
541 
542  nMX = (nMX > 120) ? 120 : nMX;
543 
544  int nNewMX = (0 + ((unsigned short)random() % nMX)) * 1000;
545 
546  // ----------------------------------------------------------------------
547  // See what they are looking for...
548  // ----------------------------------------------------------------------
549 
550  if ((sST == "ssdp:all") || (sST == "upnp:rootdevice"))
551  {
552  auto *pTask = new UPnpSearchTask( m_nServicePort,
553  peerAddress, peerPort, sST,
554  UPnp::g_UPnpDeviceDesc.m_rootDevice.GetUDN());
555 
556 #if 0
557  // Excute task now for fastest response, queue for time-delayed response
558  // -=>TODO: To be trully uPnp compliant, this Execute should be removed.
559  pTask->Execute( nullptr );
560 #endif
561 
562  TaskQueue::Instance()->AddTask( nNewMX, pTask );
563 
564  pTask->DecrRef();
565 
566  return true;
567  }
568 
569  // ----------------------------------------------------------------------
570  // Look for a specific device/service
571  // ----------------------------------------------------------------------
572 
573  QString sUDN = UPnp::g_UPnpDeviceDesc.FindDeviceUDN(
574  &(UPnp::g_UPnpDeviceDesc.m_rootDevice), sST );
575 
576  if (sUDN.length() > 0)
577  {
578  auto *pTask = new UPnpSearchTask( m_nServicePort, peerAddress,
579  peerPort, sST, sUDN );
580 
581  // Excute task now for fastest response, queue for time-delayed response
582  // -=>TODO: To be trully uPnp compliant, this Execute should be removed.
583  pTask->Execute( nullptr );
584 
585  TaskQueue::Instance()->AddTask( nNewMX, pTask );
586 
587  pTask->DecrRef();
588 
589  return true;
590  }
591 
592  return false;
593 }
594 
596 //
598 
600 {
601  QString sDescURL = GetHeaderValue( headers, "LOCATION" , "" );
602  QString sST = GetHeaderValue( headers, "ST" , "" );
603  QString sUSN = GetHeaderValue( headers, "USN" , "" );
604  QString sCache = GetHeaderValue( headers, "CACHE-CONTROL" , "" );
605 
606  LOG(VB_UPNP, LOG_DEBUG,
607  QString( "SSDP::ProcessSearchResponse ...\n"
608  "DescURL=%1\n"
609  "ST =%2\n"
610  "USN =%3\n"
611  "Cache =%4")
612  .arg(sDescURL).arg(sST).arg(sUSN).arg(sCache));
613 
614  int nPos = sCache.indexOf("max-age", 0, Qt::CaseInsensitive);
615 
616  if (nPos < 0)
617  return false;
618 
619  if ((nPos = sCache.indexOf("=", nPos)) < 0)
620  return false;
621 
622  int nSecs = sCache.mid( nPos+1 ).toInt();
623 
624  SSDPCache::Instance()->Add( sST, sUSN, sDescURL, nSecs );
625 
626  return true;
627 }
628 
630 //
632 
633 bool SSDP::ProcessNotify( const QStringMap &headers )
634 {
635  QString sDescURL = GetHeaderValue( headers, "LOCATION" , "" );
636  QString sNTS = GetHeaderValue( headers, "NTS" , "" );
637  QString sNT = GetHeaderValue( headers, "NT" , "" );
638  QString sUSN = GetHeaderValue( headers, "USN" , "" );
639  QString sCache = GetHeaderValue( headers, "CACHE-CONTROL" , "" );
640 
641  LOG(VB_UPNP, LOG_DEBUG,
642  QString( "SSDP::ProcessNotify ...\n"
643  "DescURL=%1\n"
644  "NTS =%2\n"
645  "NT =%3\n"
646  "USN =%4\n"
647  "Cache =%5" )
648  .arg(sDescURL).arg(sNTS).arg(sNT).arg(sUSN).arg(sCache));
649 
650  if (sNTS.contains( "ssdp:alive"))
651  {
652  int nPos = sCache.indexOf("max-age", 0, Qt::CaseInsensitive);
653 
654  if (nPos < 0)
655  return false;
656 
657  if ((nPos = sCache.indexOf("=", nPos)) < 0)
658  return false;
659 
660  int nSecs = sCache.mid( nPos+1 ).toInt();
661 
662  SSDPCache::Instance()->Add( sNT, sUSN, sDescURL, nSecs );
663 
664  return true;
665  }
666 
667 
668  if ( sNTS.contains( "ssdp:byebye" ) )
669  {
670  SSDPCache::Instance()->Remove( sNT, sUSN );
671 
672  return true;
673  }
674 
675  return false;
676 }
677 
680 //
681 // SSDPExtension Implementation
682 //
685 
687 //
689 
690 SSDPExtension::SSDPExtension( int nServicePort , const QString &sSharePath)
691  : HttpServerExtension( "SSDP" , sSharePath),
692  m_nServicePort(nServicePort)
693 {
695  m_sUPnpDescPath = UPnp::GetConfiguration()->GetValue( "UPnP/DescXmlPath",
696  m_sSharePath );
697 }
698 
700 //
702 
703 SSDPMethod SSDPExtension::GetMethod( const QString &sURI )
704 {
705  if (sURI == "getDeviceDesc" ) return( SSDPM_GetDeviceDesc );
706  if (sURI == "getDeviceList" ) return( SSDPM_GetDeviceList );
707 
708  return( SSDPM_Unknown );
709 }
710 
712 //
714 
716 {
717  // -=>TODO: This is very inefficient... should look into making
718  // it a unique path.
719 
720  return QStringList( "/" );
721 }
722 
724 //
726 
728 {
729  if (pRequest)
730  {
731  if ( pRequest->m_sBaseUrl != "/")
732  return( false );
733 
734  switch( GetMethod( pRequest->m_sMethod ))
735  {
736  case SSDPM_GetDeviceDesc: GetDeviceDesc( pRequest ); return( true );
737  case SSDPM_GetDeviceList: GetDeviceList( pRequest ); return( true );
738 
739  default: break;
740  }
741  }
742 
743  return( false );
744 }
745 
747 //
749 
751 {
752  pRequest->m_eResponseType = ResponseTypeXML;
753 
754  QString sUserAgent = pRequest->GetRequestHeader( "User-Agent", "" );
755 
756  LOG(VB_UPNP, LOG_DEBUG, "SSDPExtension::GetDeviceDesc - " +
757  QString( "Host=%1 Port=%2 UserAgent=%3" )
758  .arg(pRequest->GetHostAddress()) .arg(m_nServicePort)
759  .arg(sUserAgent));
760 
761  QTextStream stream( &(pRequest->m_response) );
762 
765  stream,
766  sUserAgent );
767 }
768 
770 //
772 
773 void SSDPExtension::GetFile( HTTPRequest *pRequest, const QString& sFileName )
774 {
775  pRequest->m_eResponseType = ResponseTypeHTML;
776 
777  pRequest->m_sFileName = m_sUPnpDescPath + sFileName;
778 
779  if (QFile::exists( pRequest->m_sFileName ))
780  {
781  LOG(VB_UPNP, LOG_DEBUG,
782  QString("SSDPExtension::GetFile( %1 ) - Exists")
783  .arg(pRequest->m_sFileName));
784 
785  pRequest->m_eResponseType = ResponseTypeFile;
786  pRequest->m_nResponseStatus = 200;
787  pRequest->m_mapRespHeaders[ "Cache-Control" ]
788  = "no-cache=\"Ext\", max-age = 7200"; // 2 hours
789  }
790  else
791  {
792  pRequest->m_nResponseStatus = 404;
793  pRequest->m_response.write( pRequest->GetResponsePage() );
794  LOG(VB_UPNP, LOG_ERR,
795  QString("SSDPExtension::GetFile( %1 ) - Not Found")
796  .arg(pRequest->m_sFileName));
797  }
798 
799 }
800 
802 //
804 
806 {
807  LOG(VB_UPNP, LOG_DEBUG, "SSDPExtension::GetDeviceList");
808 
809  QString sXML;
810  QTextStream os(&sXML, QIODevice::WriteOnly);
811 
812  uint nDevCount = 0, nEntryCount = 0;
813  SSDPCache::Instance()->OutputXML(os, &nDevCount, &nEntryCount);
814 
815  NameValues list;
816  list.push_back(
817  NameValue("DeviceCount", (int)nDevCount));
818  list.push_back(
819  NameValue("DevicesAllocated", SSDPCacheEntries::g_nAllocated));
820  list.push_back(
821  NameValue("CacheEntriesFound", (int)nEntryCount));
822  list.push_back(
823  NameValue("CacheEntriesAllocated", DeviceLocation::g_nAllocated));
824  list.push_back(
825  NameValue("DeviceList", sXML));
826 
827  pRequest->FormatActionResponse(list);
828 
829  pRequest->m_eResponseType = ResponseTypeXML;
830  pRequest->m_nResponseStatus = 200;
831 }
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
SSDPRequestType
Definition: ssdp.h:37
void start(QThread::Priority=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:294
static SSDPCache * Instance()
Definition: ssdpcache.cpp:240
QMap< QString, QString > QStringMap
Definition: upnputil.h:40
virtual int GetValue(const QString &sSetting, int Default)=0
void Remove(const QString &sURI, const QString &sUSN)
Definition: ssdpcache.cpp:413
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
SSDPMethod
Definition: ssdp.h:30
virtual QString GetHostAddress()=0
#define SocketIdx_Broadcast
Definition: ssdp.h:55
SSDPExtension(int nServicePort, const QString &sSharePath)
Definition: ssdp.cpp:690
void GetFile(HTTPRequest *pRequest, const QString &sFileName)
Definition: ssdp.cpp:773
QString m_sBaseUrl
Definition: httprequest.h:121
static TaskQueue * Instance()
Definition: taskqueue.cpp:58
void GetDeviceDesc(HTTPRequest *pRequest)
Definition: ssdp.cpp:750
QString m_sSharePath
Definition: httpserver.h:78
static QMutex g_pSSDPCreationLock
Definition: ssdp.cpp:47
SSDPRequestType ProcessRequestLine(const QString &sLine)
Definition: ssdp.cpp:469
bool m_bAnnouncementsEnabled
Definition: ssdp.h:72
#define SSDP_GROUP
Definition: ssdp.h:26
static void GetDeviceList(HTTPRequest *pRequest)
Definition: ssdp.cpp:805
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
QString m_sMethod
Definition: httprequest.h:123
QStringMap m_mapRespHeaders
Definition: httprequest.h:147
QString GetRequestHeader(const QString &sKey, QString sDefault)
static constexpr int kNumberOfSockets
Definition: ssdp.h:64
static int g_nAllocated
Definition: ssdpcache.h:62
void EnableNotifications(int nServicePort)
Definition: ssdp.cpp:150
long m_nResponseStatus
Definition: httprequest.h:146
Definition: ssdp.h:57
void DisableNotifications()
Definition: ssdp.cpp:190
void Execute(TaskQueue *) override
QByteArray GetResponsePage(void)
#define SocketIdx_Multicast
Definition: ssdp.h:54
static QString GetHeaderValue(const QStringMap &headers, const QString &sKey, const QString &sDefault)
Definition: ssdp.cpp:496
void run() override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: ssdp.cpp:250
void PerformSearch(const QString &sST, uint timeout_secs=2)
Definition: ssdp.cpp:206
int m_nServicePort
Definition: ssdp.h:146
QRegExp m_procReqLineExp
Definition: ssdp.h:63
QString FindDeviceUDN(UPnpDevice *pDevice, QString sST)
Definition: upnpdevice.cpp:544
QStringList GetBasePaths() override
Definition: ssdp.cpp:715
MSocketDevice * m_Sockets[kNumberOfSockets]
Definition: ssdp.h:65
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
void RequestTerminate(void)
Definition: ssdp.cpp:141
void FormatActionResponse(Serializer *ser)
UPnpNotifyTask * m_pNotifyTask
Definition: ssdp.h:71
unsigned int uint
Definition: compat.h:140
QString m_sFileName
Definition: httprequest.h:149
void ProcessData(MSocketDevice *pSocket)
Definition: ssdp.cpp:318
static Configuration * GetConfiguration()
Definition: upnp.cpp:71
void AddTask(long msec, Task *pTask)
Definition: taskqueue.cpp:170
void SetNTS(UPnpNotifyNTS nts)
void Execute(TaskQueue *) override
static int g_nAllocated
Definition: upnpdevice.h:207
static SSDP * g_pSSDP
Definition: ssdp.h:61
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static SSDP * Instance()
Definition: ssdp.cpp:54
#define SocketIdx_Search
Definition: ssdp.h:53
QString m_sUPnpDescPath
Definition: ssdp.h:145
static bool ProcessSearchResponse(const QStringMap &sHeaders)
Definition: ssdp.cpp:599
bool m_bTermRequested
Definition: ssdp.h:74
int m_nPort
Definition: ssdp.h:67
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
QBuffer m_response
Definition: httprequest.h:151
int m_nServicePort
Definition: ssdp.h:69
QTextStream & OutputXML(QTextStream &os, uint *pnDevCount=nullptr, uint *pnEntryCount=nullptr) const
Outputs the XML for this device.
Definition: ssdpcache.cpp:551
HttpResponseType m_eResponseType
Definition: httprequest.h:143
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: ssdp.cpp:727
#define SSDP_PORT
Definition: ssdp.h:27
static bool ProcessNotify(const QStringMap &sHeaders)
Definition: ssdp.cpp:633
static long int random(void)
Definition: compat.h:149
static SSDPMethod GetMethod(const QString &sURI)
Definition: ssdp.cpp:703
#define SSDP_SEARCHPORT
Definition: ssdp.h:28
void Add(const QString &sURI, const QString &sUSN, const QString &sLocation, long sExpiresInSecs)
Definition: ssdpcache.cpp:328
int m_nSearchPort
Definition: ssdp.h:68
bool ProcessSearchRequest(const QStringMap &sHeaders, const QHostAddress &peerAddress, quint16 peerPort)
Definition: ssdp.cpp:511
void GetValidXML(const QString &sBaseAddress, int nPort, QTextStream &os, const QString &sUserAgent="")
Definition: upnpdevice.cpp:316
SSDP()
Definition: ssdp.cpp:75
static void Shutdown()
Definition: ssdp.cpp:64
~SSDP()
Definition: ssdp.cpp:115
static UPnpDeviceDesc g_UPnpDeviceDesc
Definition: upnp.h:106