OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 // TODO(ajwong): Unfork with | |
6 // chrome/common/net/notifier/communicator/ssl_socket_adapter.h | |
7 | |
8 #ifndef REMOTING_JINGLE_GLUE_SSL_SOCKET_ADAPTER_H_ | |
9 #define REMOTING_JINGLE_GLUE_SSL_SOCKET_ADAPTER_H_ | |
10 | |
11 #include "base/scoped_ptr.h" | |
12 #include "net/base/completion_callback.h" | |
13 #include "net/base/io_buffer.h" | |
14 #include "net/base/net_errors.h" | |
15 #include "net/base/net_log.h" | |
16 #include "net/socket/client_socket.h" | |
17 #include "net/socket/ssl_client_socket.h" | |
18 #include "talk/base/asyncsocket.h" | |
19 #include "talk/base/ssladapter.h" | |
20 | |
21 namespace remoting { | |
22 | |
23 class SSLSocketAdapter; | |
24 | |
25 // TODO(sergeyu): Write unittests for this code! | |
26 | |
27 // This class provides a wrapper to libjingle's talk_base::AsyncSocket that | |
28 // implements Chromium's net::ClientSocket interface. It's used by | |
29 // SSLSocketAdapter to enable Chromium's SSL implementation to work over | |
30 // libjingle's socket class. | |
31 class TransportSocket : public net::ClientSocket, public sigslot::has_slots<> { | |
32 public: | |
33 TransportSocket(talk_base::AsyncSocket* socket, | |
34 SSLSocketAdapter *ssl_adapter); | |
35 | |
36 void set_addr(const talk_base::SocketAddress& addr) { | |
37 addr_ = addr; | |
38 } | |
39 | |
40 // net::ClientSocket implementation | |
41 | |
42 virtual int Connect(net::CompletionCallback* callback); | |
43 virtual void Disconnect(); | |
44 virtual bool IsConnected() const; | |
45 virtual bool IsConnectedAndIdle() const; | |
46 virtual int GetPeerAddress(net::AddressList* address) const; | |
47 virtual const net::BoundNetLog& NetLog() const { return net_log_; } | |
48 | |
49 // net::Socket implementation | |
50 | |
51 virtual int Read(net::IOBuffer* buf, int buf_len, | |
52 net::CompletionCallback* callback); | |
53 virtual int Write(net::IOBuffer* buf, int buf_len, | |
54 net::CompletionCallback* callback); | |
55 virtual bool SetReceiveBufferSize(int32 size); | |
56 virtual bool SetSendBufferSize(int32 size); | |
57 | |
58 private: | |
59 friend class SSLSocketAdapter; | |
60 | |
61 void OnReadEvent(talk_base::AsyncSocket* socket); | |
62 void OnWriteEvent(talk_base::AsyncSocket* socket); | |
63 | |
64 net::CompletionCallback* read_callback_; | |
65 net::CompletionCallback* write_callback_; | |
66 | |
67 scoped_refptr<net::IOBuffer> read_buffer_; | |
68 int read_buffer_len_; | |
69 scoped_refptr<net::IOBuffer> write_buffer_; | |
70 int write_buffer_len_; | |
71 | |
72 net::BoundNetLog net_log_; | |
73 | |
74 talk_base::AsyncSocket *socket_; | |
75 talk_base::SocketAddress addr_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(TransportSocket); | |
78 }; | |
79 | |
80 // This provides a talk_base::AsyncSocketAdapter interface around Chromium's | |
81 // net::SSLClientSocket class. This allows notifier to use Chromium's SSL | |
82 // implementation instead of OpenSSL. | |
83 class SSLSocketAdapter : public talk_base::SSLAdapter { | |
84 public: | |
85 explicit SSLSocketAdapter(talk_base::AsyncSocket* socket); | |
86 | |
87 // StartSSL returns 0 if successful, or non-zero on failure. | |
88 // If StartSSL is called while the socket is closed or connecting, the SSL | |
89 // negotiation will begin as soon as the socket connects. | |
90 // | |
91 // restartable is not implemented, and must be set to false. | |
92 virtual int StartSSL(const char* hostname, bool restartable); | |
93 | |
94 // Create the default SSL adapter for this platform. | |
95 static SSLSocketAdapter* Create(AsyncSocket* socket); | |
96 | |
97 virtual int Send(const void* pv, size_t cb); | |
98 virtual int Recv(void* pv, size_t cb); | |
99 | |
100 private: | |
101 friend class TransportSocket; | |
102 | |
103 enum SSLState { | |
104 SSLSTATE_NONE, | |
105 SSLSTATE_WAIT, | |
106 SSLSTATE_CONNECTED, | |
107 }; | |
108 | |
109 enum IOState { | |
110 IOSTATE_NONE, | |
111 IOSTATE_PENDING, | |
112 IOSTATE_COMPLETE, | |
113 }; | |
114 | |
115 void OnConnected(int result); | |
116 void OnRead(int result); | |
117 void OnWrite(int result); | |
118 | |
119 virtual void OnConnectEvent(talk_base::AsyncSocket* socket); | |
120 | |
121 int BeginSSL(); | |
122 | |
123 bool ignore_bad_cert_; | |
124 std::string hostname_; | |
125 TransportSocket* transport_socket_; | |
126 scoped_ptr<net::SSLClientSocket> ssl_socket_; | |
127 net::CompletionCallbackImpl<SSLSocketAdapter> connected_callback_; | |
128 net::CompletionCallbackImpl<SSLSocketAdapter> read_callback_; | |
129 net::CompletionCallbackImpl<SSLSocketAdapter> write_callback_; | |
130 SSLState ssl_state_; | |
131 IOState read_state_; | |
132 IOState write_state_; | |
133 scoped_refptr<net::IOBuffer> transport_buf_; | |
134 int data_transferred_; | |
135 | |
136 DISALLOW_COPY_AND_ASSIGN(SSLSocketAdapter); | |
137 }; | |
138 | |
139 } // namespace remoting | |
140 | |
141 #endif // REMOTING_JINGLE_GLUE_SSL_SOCKET_ADAPTER_H_ | |
OLD | NEW |