gloox  1.0
socks5bytestreamserver.h
1 /*
2  Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #ifndef SOCKS5BYTESTREAMSERVER_H__
15 #define SOCKS5BYTESTREAMSERVER_H__
16 
17 #include "macros.h"
18 #include "connectionhandler.h"
19 #include "logsink.h"
20 #include "mutex.h"
21 
22 namespace gloox
23 {
24 
25  class ConnectionTCPServer;
26 
38  {
39 
40  friend class SOCKS5BytestreamManager;
41 
42  public:
49  SOCKS5BytestreamServer( const LogSink& logInstance, int port, const std::string& ip = EmptyString );
50 
55 
61  ConnectionError listen();
62 
69  ConnectionError recv( int timeout );
70 
74  void stop();
75 
81  int localPort() const;
82 
88  const std::string localInterface() const;
89 
90  // reimplemented from ConnectionHandler
91  virtual void handleIncomingConnection( ConnectionBase* server, ConnectionBase* connection );
92 
93  // reimplemented from ConnectionDataHandler
94  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
95 
96  // reimplemented from ConnectionDataHandler
97  virtual void handleConnect( const ConnectionBase* connection );
98 
99  // reimplemented from ConnectionDataHandler
100  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
101 
102  private:
103  SOCKS5BytestreamServer& operator=( const SOCKS5BytestreamServer& );
104  void registerHash( const std::string& hash );
105  void removeHash( const std::string& hash );
106  ConnectionBase* getConnection( const std::string& hash );
107 
108  enum NegotiationState
109  {
111  StateUnnegotiated,
112  StateAuthmethodAccepted,
113  StateAuthAccepted,
114  StateDestinationAccepted,
115  StateActive
116  };
117 
118  struct ConnectionInfo
119  {
120  NegotiationState state;
121  std::string hash;
122  };
123 
124  typedef std::map<ConnectionBase*, ConnectionInfo> ConnectionMap;
125  ConnectionMap m_connections;
126 
127  typedef std::list<const ConnectionBase*> ConnectionList;
128  ConnectionList m_oldConnections;
129 
130  typedef std::list<std::string> HashMap;
131  HashMap m_hashes;
132 
133  ConnectionTCPServer* m_tcpServer;
134 
135  util::Mutex m_mutex;
136  const LogSink& m_logInstance;
137  std::string m_ip;
138  int m_port;
139 
140  };
141 
142 }
143 
144 #endif // SOCKS5BYTESTREAMSERVER_H__