1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef REMOTING_HOST_GNUBBY_AUTH_HANDLER_POSIX_H_ 6 #define REMOTING_HOST_GNUBBY_AUTH_HANDLER_POSIX_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/memory/scoped_ptr.h" 12 #include "base/threading/non_thread_safe.h" 13 #include "net/socket/stream_listen_socket.h" 14 #include "remoting/host/gnubby_auth_handler.h" 15 16 namespace base { 17 class DictionaryValue; 18 } // namespace base 19 20 namespace remoting { 21 22 namespace protocol { 23 class ClientStub; 24 } // namespace protocol 25 26 class GnubbySocket; 27 28 class GnubbyAuthHandlerPosix : public GnubbyAuthHandler, 29 public base::NonThreadSafe, 30 public net::StreamListenSocket::Delegate { 31 public: 32 explicit GnubbyAuthHandlerPosix(protocol::ClientStub* client_stub); 33 virtual ~GnubbyAuthHandlerPosix(); 34 35 bool HasActiveSocketForTesting(net::StreamListenSocket* socket) const; 36 int GetConnectionIdForTesting(net::StreamListenSocket* socket) const; 37 GnubbySocket* GetGnubbySocketForTesting( 38 net::StreamListenSocket* socket) const; 39 40 private: 41 typedef std::map<int, GnubbySocket*> ActiveSockets; 42 43 // GnubbyAuthHandler interface. 44 virtual void DeliverClientMessage(const std::string& message) OVERRIDE; 45 virtual void DeliverHostDataMessage(int connection_id, 46 const std::string& data) const OVERRIDE; 47 48 // StreamListenSocket::Delegate interface. 49 virtual void DidAccept(net::StreamListenSocket* server, 50 scoped_ptr<net::StreamListenSocket> socket) OVERRIDE; 51 virtual void DidRead(net::StreamListenSocket* socket, 52 const char* data, 53 int len) OVERRIDE; 54 virtual void DidClose(net::StreamListenSocket* socket) OVERRIDE; 55 56 // Create socket for authorization. 57 void CreateAuthorizationSocket(); 58 59 // Process a gnubby request. 60 void ProcessGnubbyRequest(int connection_id, const std::string& request_data); 61 62 // Gets an active socket iterator for the connection id in |message|. 63 ActiveSockets::iterator GetSocketForMessage(base::DictionaryValue* message); 64 65 // Send an error and close an active socket. 66 void SendErrorAndCloseActiveSocket(const ActiveSockets::iterator& iter); 67 68 // A request timed out. 69 void RequestTimedOut(int connection_id); 70 71 // Interface through which communication with the client occurs. 72 protocol::ClientStub* client_stub_; 73 74 // Socket used to listen for authorization requests. 75 scoped_ptr<net::StreamListenSocket> auth_socket_; 76 77 // The last assigned gnubby connection id. 78 int last_connection_id_; 79 80 // Sockets by connection id used to process gnubbyd requests. 81 ActiveSockets active_sockets_; 82 83 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerPosix); 84 }; 85 86 } // namespace remoting 87 88 #endif // REMOTING_HOST_GNUBBY_AUTH_HANDLER_POSIX_H_ 89