gloox  0.9.9.12
socks5bytestreamserver.h
1 /*
2  Copyright (c) 2007-2008 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 = "" );
50 
55 
61  ConnectionError listen();
62 
69  ConnectionError recv( int timeout );
70 
74  void stop();
75 
76  // re-implemented from ConnectionHandler
77  virtual void handleIncomingConnection( ConnectionBase* connection );
78 
79  // re-implemented from ConnectionDataHandler
80  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
81 
82  // re-implemented from ConnectionDataHandler
83  virtual void handleConnect( const ConnectionBase* connection );
84 
85  // re-implemented from ConnectionDataHandler
86  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
87 
88  private:
90  void registerHash( const std::string& hash );
91  void removeHash( const std::string& hash );
92  ConnectionBase* getConnection( const std::string& hash );
93 
94  enum NegotiationState
95  {
97  StateUnnegotiated,
98  StateAuthmethodAccepted,
99  StateAuthAccepted,
100  StateDestinationAccepted,
101  StateActive
102  };
103 
104  struct ConnectionInfo
105  {
106  NegotiationState state;
107  std::string hash;
108  };
109 
110  typedef std::map<ConnectionBase*, ConnectionInfo> ConnectionMap;
111  ConnectionMap m_connections;
112 
113  typedef std::list<const ConnectionBase*> ConnectionList;
114  ConnectionList m_oldConnections;
115 
116  typedef std::list<std::string> HashMap;
117  HashMap m_hashes;
118 
119  ConnectionTCPServer* m_tcpServer;
120 
121  Mutex m_mutex;
122  const LogSink& m_logInstance;
123  std::string m_ip;
124  int m_port;
125 
126  };
127 
128 }
129 
130 #endif // SOCKS5BYTESTREAMSERVER_H__