/src/libtomahawk/network/Servent.h

http://github.com/tomahawk-player/tomahawk · C Header · 203 lines · 94 code · 44 blank · 65 comment · 0 complexity · 4cab369fe958b1793e887f2584525e3d MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
  5. * Copyright 2013, Teo Mrnjavac <teo@kde.org>
  6. * Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
  7. *
  8. * Tomahawk is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Tomahawk is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef SERVENT_H
  22. #define SERVENT_H
  23. // time before new connection terminate if it could not be established
  24. #define CONNECT_TIMEOUT 10000
  25. #include "network/Enums.h"
  26. #include "DllMacro.h"
  27. #include "Typedefs.h"
  28. #include <QHostAddress>
  29. #include <QTcpServer>
  30. #include <functional>
  31. class Connection;
  32. class Connector;
  33. class ControlConnection;
  34. class NetworkReply;
  35. class PeerInfo;
  36. class PortFwdThread;
  37. class ProxyConnection;
  38. class QTcpSocketExtra;
  39. class RemoteCollectionConnection;
  40. class SipInfo;
  41. class StreamConnection;
  42. class ServentPrivate;
  43. class DLLEXPORT Servent : public QTcpServer
  44. {
  45. Q_OBJECT
  46. public:
  47. static Servent* instance();
  48. static bool isValidExternalIP( const QHostAddress& addr );
  49. static SipInfo getSipInfoForOldVersions( const QList<SipInfo> &sipInfos );
  50. explicit Servent( QObject* parent = 0 );
  51. virtual ~Servent();
  52. /**
  53. * Start listening for connections.
  54. *
  55. * @param ha The address to listen on, pass QHostAddress::Any or QHostAddress::AnyIPv6 depending on your Qt version to listen on all interfaces.
  56. * @param upnp If true, try to create a port forward on the next router through UPnP.
  57. * @param port The port we should listen on, if not possible fallback to defaultPort.
  58. * @param mode How the external IP address should be determined.
  59. * @param defaultPort If we cannot listen on the port the user configured try this port as fallback.
  60. * @param autoDetectExternalIp If true, try to automatically detect the external port by querying a remote server.
  61. * @param externalHost manually supplied external hostname (only with mode == Tomahawk::Network::ExternalAddress::Static)
  62. * @param externalPort manually supplied external port (only with mode == Tomahawk::Network::ExternalAddress::Static)
  63. * @return True if we could listen on any of the supplied ports.
  64. */
  65. bool startListening( QHostAddress ha, bool upnp, int port,
  66. Tomahawk::Network::ExternalAddress::Mode mode,
  67. int defaultPort, bool autoDetectExternalIp = false,
  68. const QString& externalHost = QString(),
  69. int externalPort = -1 );
  70. // creates new token that allows a controlconnection to be set up
  71. QString createConnectionKey( const QString& name = "", const QString &nodeid = "", const QString &key = "", bool onceOnly = true );
  72. void registerOffer( const QString& key, Connection* conn );
  73. void registerLazyOffer( const QString& key, const Tomahawk::peerinfo_ptr& peerInfo, const QString &nodeid , const int timeout );
  74. void registerControlConnection( ControlConnection* conn );
  75. void unregisterControlConnection( ControlConnection* conn );
  76. ControlConnection* lookupControlConnection( const SipInfo& sipInfo );
  77. ControlConnection* lookupControlConnection( const QString& nodeid );
  78. void remoteIODeviceFactory( const Tomahawk::result_ptr& result, const QString& url,
  79. std::function< void ( const QString&, QSharedPointer< QIODevice >& ) > callback );
  80. // you may call this method as often as you like for the same peerInfo, dupe checking is done inside
  81. void registerPeer( const Tomahawk::peerinfo_ptr& peerInfo );
  82. void handleSipInfo( const Tomahawk::peerinfo_ptr& peerInfo );
  83. void initiateConnection( const SipInfo& sipInfo, Connection* conn );
  84. void reverseOfferRequest( ControlConnection* orig_conn, const QString &theirdbid, const QString& key, const QString& theirkey );
  85. bool visibleExternally() const;
  86. /**
  87. * Is the probality that this host supports IPv6 high?
  88. *
  89. * Though we cannot fully test for IPv6 connectivity, some guesses based on non-localhost addresses are done.
  90. */
  91. bool ipv6ConnectivityLikely() const;
  92. /**
  93. * The port this Peer listens directly (per default)
  94. */
  95. int port() const;
  96. /**
  97. * The IP addresses this Peer listens directly (per default)
  98. */
  99. QList< QHostAddress > addresses() const;
  100. /**
  101. * An additional address this peer listens to, e.g. via UPnP.
  102. */
  103. QString additionalAddress() const;
  104. /**
  105. * An additional port this peer listens to, e.g. via UPnP (only in combination with additionalAddress.
  106. */
  107. int additionalPort() const;
  108. static bool isIPWhitelisted( QHostAddress ip );
  109. bool connectedToSession( const QString& session );
  110. unsigned int numConnectedPeers() const;
  111. QList< StreamConnection* > streams() const;
  112. bool isReady() const;
  113. QList<SipInfo> getLocalSipInfos(const QString& nodeid, const QString &key);
  114. void queueForAclResult( const QString& username, const QSet<Tomahawk::peerinfo_ptr>& peerInfos );
  115. signals:
  116. void dbSyncTriggered();
  117. /**
  118. * @brief ipDetectionFailed Emitted when the automatic external IP detection failed.
  119. * @param error If the failure was caused by a network error, this is its error code.
  120. * If the error wasn't network related, QNetworkReply::NoError will be returned.
  121. * @param errorString A string explaining the error.
  122. */
  123. void ipDetectionFailed( QNetworkReply::NetworkError error, QString errorString );
  124. void streamStarted( StreamConnection* );
  125. void streamFinished( StreamConnection* );
  126. void ready();
  127. protected:
  128. void incomingConnection( qintptr sd ) Q_DECL_OVERRIDE;
  129. public slots:
  130. void setExternalAddress( QHostAddress ha, unsigned int port );
  131. void createParallelConnection( Connection* orig_conn, Connection* new_conn, const QString& key );
  132. void registerStreamConnection( StreamConnection* );
  133. void onStreamFinished( StreamConnection* sc );
  134. void socketConnected();
  135. void triggerDBSync();
  136. void onSipInfoChanged();
  137. private slots:
  138. void deleteLazyOffer( const QString& key );
  139. void readyRead();
  140. void socketError( QAbstractSocket::SocketError e );
  141. void checkACLResult( const QString &nodeid, const QString &username, Tomahawk::ACLStatus::Type peerStatus );
  142. void ipDetected();
  143. Connection* claimOffer( ControlConnection* cc, const QString &nodeid, const QString &key, const QHostAddress peer = QHostAddress::Any );
  144. private:
  145. Q_DECLARE_PRIVATE( Servent )
  146. ServentPrivate* d_ptr;
  147. void handoverSocket( Connection* conn, QTcpSocketExtra* sock );
  148. void cleanupSocket( QTcpSocketExtra* sock );
  149. void printCurrentTransfers();
  150. /**
  151. * Remove addresses from the list that we shall not use in Tomahawk (e.g.
  152. * for sending to other peers).
  153. *
  154. * @param addresses The list that shall be cleanded.
  155. */
  156. void cleanAddresses( QList<QHostAddress>& addresses ) const;
  157. static Servent* s_instance;
  158. };
  159. #endif // SERVENT_H