gloox 1.0
socks5bytestreammanager.h
00001 /*
00002   Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net>
00003   This file is part of the gloox library. http://camaya.net/gloox
00004 
00005   This software is distributed under a license. The full license
00006   agreement can be found in the file LICENSE in this distribution.
00007   This software may not be copied, modified, sold or distributed
00008   other than expressed in the named license agreement.
00009 
00010   This software is distributed without any warranty.
00011 */
00012 
00013 
00014 #ifndef SOCKS5BYTESTREAMMANAGER_H__
00015 #define SOCKS5BYTESTREAMMANAGER_H__
00016 
00017 #include "iqhandler.h"
00018 #include "jid.h"
00019 #include "stanzaextension.h"
00020 
00021 namespace gloox
00022 {
00023 
00024   class BytestreamHandler;
00025   class SOCKS5BytestreamServer;
00026   class SOCKS5Bytestream;
00027   class ClientBase;
00028 
00032   struct StreamHost
00033   {
00034     JID jid;                    
00035     std::string host;           
00036     int port;                   
00037 //         std::string zeroconf;       /**< A zeroconf identifier. */
00038   };
00039 
00043   typedef std::list<StreamHost> StreamHostList;
00044 
00051   class GLOOX_API SOCKS5BytestreamManager : public IqHandler
00052   {
00053 
00054     friend class SOCKS5Bytestream;
00055 
00056     public:
00057 
00061       enum S5BMode
00062       {
00063         S5BTCP,                     
00064         S5BUDP,                     
00065         S5BInvalid                  
00066       };
00067 
00074       SOCKS5BytestreamManager( ClientBase* parent, BytestreamHandler* s5bh );
00075 
00079       virtual ~SOCKS5BytestreamManager();
00080 
00086       void setStreamHosts( StreamHostList hosts ) { m_hosts = hosts; }
00087 
00094       void addStreamHost( const JID& jid, const std::string& host, int port );
00095 
00111       bool requestSOCKS5Bytestream( const JID& to, S5BMode mode, const std::string& sid = EmptyString,
00112                                     const JID& from = JID() );
00113 
00120       bool dispose( SOCKS5Bytestream* s5b );
00121 
00126       void acceptSOCKS5Bytestream( const std::string& sid );
00127 
00133       void rejectSOCKS5Bytestream( const std::string& sid, StanzaError reason = StanzaErrorNotAcceptable );
00134 
00141       void registerBytestreamHandler( BytestreamHandler* s5bh )
00142         { m_socks5BytestreamHandler = s5bh; }
00143 
00147       void removeBytestreamHandler()
00148         { m_socks5BytestreamHandler = 0; }
00149 
00155       void registerSOCKS5BytestreamServer( SOCKS5BytestreamServer* server ) { m_server = server; }
00156 
00160       void removeSOCKS5BytestreamServer() { m_server = 0; }
00161 
00162       // reimplemented from IqHandler.
00163       virtual bool handleIq( const IQ& iq );
00164 
00165       // reimplemented from IqHandler.
00166       virtual void handleIqID( const IQ& iq, int context );
00167 
00168     private:
00169 #ifdef SOCKS5BYTESTREAMMANAGER_TEST
00170     public:
00171 #endif
00172 
00173       class Query : public StanzaExtension
00174       {
00175         public:
00179           Query();
00180 
00187           Query( const std::string& sid, S5BMode mode,
00188                  const StreamHostList& hosts );
00189 
00197           Query( const JID& jid, const std::string& sid, bool activate );
00198 
00203           Query( const Tag* tag );
00204 
00208           virtual ~Query();
00209 
00214           const std::string& sid() const { return m_sid; }
00215 
00220           const JID& jid() const { return m_jid; }
00221 
00226           S5BMode mode() const { return m_mode; }
00227 
00232           const StreamHostList& hosts() const { return m_hosts; }
00233 
00234           // reimplemented from StanzaExtension
00235           virtual const std::string& filterString() const;
00236 
00237           // reimplemented from StanzaExtension
00238           virtual StanzaExtension* newInstance( const Tag* tag ) const
00239           {
00240             return new Query( tag );
00241           }
00242 
00243           // reimplemented from StanzaExtension
00244           virtual Tag* tag() const;
00245 
00246           // reimplemented from StanzaExtension
00247           virtual StanzaExtension* clone() const
00248           {
00249             return new Query( *this );
00250           }
00251 
00252         private:
00253           enum QueryType
00254           {
00255             TypeSH,
00256             TypeSHU,
00257             TypeA,
00258             TypeInvalid
00259           };
00260 
00261           std::string m_sid;
00262           JID m_jid;
00263           SOCKS5BytestreamManager::S5BMode m_mode;
00264           StreamHostList m_hosts;
00265           QueryType m_type;
00266 
00267       };
00268 
00269       SOCKS5BytestreamManager& operator=( const SOCKS5BytestreamManager&);
00270       void rejectSOCKS5Bytestream( const JID& from, const std::string& id, StanzaError reason = StanzaErrorNotAcceptable );
00271       bool haveStream( const JID& from );
00272       const StreamHost* findProxy( const JID& from, const std::string& hostjid, const std::string& sid );
00273 
00274       void acknowledgeStreamHost( bool success, const JID& jid, const std::string& sid );
00275 
00276       enum IBBActionType
00277       {
00278         S5BOpenStream,
00279         S5BCloseStream,
00280         S5BActivateStream
00281       };
00282 
00283       typedef std::map<std::string, SOCKS5Bytestream*> S5BMap;
00284       S5BMap m_s5bMap;
00285 
00286       struct AsyncS5BItem
00287       {
00288         JID from;
00289         JID to;
00290         std::string id;
00291         StreamHostList sHosts;
00292         bool incoming;
00293       };
00294       typedef std::map<std::string, AsyncS5BItem> AsyncTrackMap;
00295       AsyncTrackMap m_asyncTrackMap;
00296 
00297       ClientBase* m_parent;
00298       BytestreamHandler* m_socks5BytestreamHandler;
00299       SOCKS5BytestreamServer* m_server;
00300       StreamHostList m_hosts;
00301       StringMap m_trackMap;
00302 
00303   };
00304 
00305 }
00306 
00307 #endif // SOCKS5BYTESTREAMMANAGER_H__