gloox  1.1-svn
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 "connectiontcpserver.h"
20 #include "logsink.h"
21 #include "mutex.h"
22 
23 namespace gloox
24 {
25 
37  {
38 
39  friend class SOCKS5BytestreamManager;
40 
41  public:
48  SOCKS5BytestreamServer( const LogSink& logInstance, int port, const std::string& ip = EmptyString );
49 
54 
60  void setServerImpl( ConnectionBase *server );
61 
65  void removeServerImpl();
66 
72  ConnectionError listen();
73 
80  ConnectionError recv( int timeout );
81 
85  void stop();
86 
92  int localPort() const;
93 
99  const std::string localInterface() const;
100 
105  ConnectionBase* server() const { return m_server; }
106 
107  // reimplemented from ConnectionHandler
108  virtual void handleIncomingConnection( ConnectionBase* server, ConnectionBase* connection );
109 
110  // reimplemented from ConnectionDataHandler
111  virtual void handleReceivedData( const ConnectionBase* connection, const std::string& data );
112 
113  // reimplemented from ConnectionDataHandler
114  virtual void handleConnect( const ConnectionBase* connection );
115 
116  // reimplemented from ConnectionDataHandler
117  virtual void handleDisconnect( const ConnectionBase* connection, ConnectionError reason );
118 
119  private:
120  SOCKS5BytestreamServer& operator=( const SOCKS5BytestreamServer& );
121  void registerHash( const std::string& hash );
122  void removeHash( const std::string& hash );
123  ConnectionBase* getConnection( const std::string& hash );
124 
125  enum NegotiationState
126  {
128  StateUnnegotiated,
129  StateAuthmethodAccepted,
130  StateAuthAccepted,
131  StateDestinationAccepted,
132  StateActive
133  };
134 
135  struct ConnectionInfo
136  {
137  NegotiationState state;
138  std::string hash;
139  };
140 
141  typedef std::map<ConnectionBase*, ConnectionInfo> ConnectionMap;
142  ConnectionMap m_connections;
143 
144  typedef std::list<const ConnectionBase*> ConnectionList;
145  ConnectionList m_oldConnections;
146 
147  typedef std::list<std::string> HashMap;
148  HashMap m_hashes;
149 
150  ConnectionBase* m_server;
151 
152  util::Mutex m_mutex;
153  const LogSink& m_logInstance;
154  std::string m_ip;
155  int m_port;
156 
157  };
158 
159 }
160 
161 #endif // SOCKS5BYTESTREAMSERVER_H__