PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libtomahawk/network/PortFwdThread.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 144 lines | 94 code | 29 blank | 21 comment | 10 complexity | 02f34f17dbad4455c7cd29eda29313b2 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. *
  5. * Tomahawk is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Tomahawk is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "PortFwdThread.h"
  19. #include "portfwd/portfwd.h"
  20. #include "utils/Logger.h"
  21. #include "Source.h"
  22. #include <QCoreApplication>
  23. #include <QNetworkInterface>
  24. #include <QStringList>
  25. #include <QTime>
  26. #include <QTimer>
  27. PortFwdThread::PortFwdThread( unsigned int port )
  28. : QThread()
  29. , m_port( port )
  30. {
  31. }
  32. PortFwdThread::~PortFwdThread()
  33. {
  34. }
  35. void
  36. PortFwdThread::run()
  37. {
  38. m_worker = QPointer< PortFwdWorker >( new PortFwdWorker( m_port ) );
  39. Q_ASSERT( m_worker );
  40. connect( m_worker.data(), SIGNAL( externalAddressDetected( QHostAddress, unsigned int ) ), this, SIGNAL( externalAddressDetected( QHostAddress, unsigned int ) ) );
  41. QTimer::singleShot( 0, m_worker.data(), SLOT( work() ) );
  42. exec();
  43. if ( m_worker.data()->externalPort() )
  44. {
  45. qDebug() << "Unregistering port fwd";
  46. m_worker.data()->unregister();
  47. }
  48. if ( m_worker )
  49. delete m_worker.data();
  50. }
  51. QPointer< PortFwdWorker >
  52. PortFwdThread::worker() const
  53. {
  54. return m_worker;
  55. }
  56. PortFwdWorker::PortFwdWorker( unsigned int port )
  57. : QObject()
  58. , m_externalPort( 0 )
  59. , m_port( port )
  60. {
  61. }
  62. PortFwdWorker::~PortFwdWorker()
  63. {
  64. delete m_portfwd;
  65. }
  66. void
  67. PortFwdWorker::unregister()
  68. {
  69. m_portfwd->remove( m_externalPort );
  70. }
  71. void
  72. PortFwdWorker::work()
  73. {
  74. qsrand( QTime( 0, 0, 0 ).secsTo( QTime::currentTime() ) );
  75. m_portfwd = new Portfwd();
  76. foreach ( QHostAddress ha, QNetworkInterface::allAddresses() )
  77. {
  78. if( ha.toString() == "127.0.0.1" ) continue;
  79. if( ha.toString().contains( ":" ) ) continue; //ipv6
  80. m_portfwd->addBlockedDevice( ha.toString().toStdString() );
  81. }
  82. // try and pick an available port:
  83. if ( m_portfwd->init( 2000 ) )
  84. {
  85. int tryport = m_port;
  86. // last.fm office firewall policy hack
  87. // (corp. firewall allows outgoing connections to this port,
  88. // so listen on this if you want lastfmers to connect to you)
  89. if ( qApp->arguments().contains( "--porthack" ) )
  90. {
  91. tryport = 3389;
  92. m_portfwd->remove( tryport );
  93. }
  94. for ( int r = 0; r < 3; ++r )
  95. {
  96. qDebug() << "Trying to setup portfwd on" << tryport;
  97. if ( m_portfwd->add( tryport, m_port ) )
  98. {
  99. QString pubip = QString( m_portfwd->external_ip().c_str() ).trimmed();
  100. m_externalAddress = QHostAddress( pubip );
  101. m_externalPort = tryport;
  102. tDebug() << "External servent address detected as" << pubip << ":" << m_externalPort;
  103. qDebug() << "Max upstream " << m_portfwd->max_upstream_bps() << "bps";
  104. qDebug() << "Max downstream" << m_portfwd->max_downstream_bps() << "bps";
  105. break;
  106. }
  107. tryport = qAbs( 10000 + 50000 * (float)qrand() / RAND_MAX );
  108. }
  109. }
  110. else
  111. tDebug() << "No UPNP Gateway device found?";
  112. if ( !m_externalPort )
  113. tDebug() << "Could not setup fwd for port:" << m_port;
  114. emit externalAddressDetected( m_externalAddress, m_externalPort );
  115. }