summaryrefslogtreecommitdiffstats
path: root/content/renderer/p2p/ipc_socket_factory.cc
blob: 565e692db5b8f41549d88ec2002ad59eb41ce71f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/p2p/ipc_socket_factory.h"

#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "content/renderer/p2p/socket_client.h"
#include "content/renderer/p2p/socket_dispatcher.h"
#include "jingle/glue/utils.h"
#include "third_party/libjingle/source/talk/base/asyncpacketsocket.h"

namespace {

// IpcPacketSocket implements talk_base::AsyncPacketSocket interface
// using P2PSocketClient that works over IPC-channel. It must be used
// on the thread it was created.
class IpcPacketSocket : public talk_base::AsyncPacketSocket,
                        public P2PSocketClient::Delegate {
 public:
  IpcPacketSocket();
  virtual ~IpcPacketSocket();

  bool Init(P2PSocketType type, P2PSocketClient* client,
            const talk_base::SocketAddress& address);

  // talk_base::AsyncPacketSocket interface.
  virtual talk_base::SocketAddress GetLocalAddress(bool* allocated) const;
  virtual talk_base::SocketAddress GetRemoteAddress() const;
  virtual int Send(const void *pv, size_t cb);
  virtual int SendTo(const void *pv, size_t cb,
                     const talk_base::SocketAddress& addr);
  virtual int Close();
  virtual talk_base::Socket::ConnState GetState() const;
  virtual int GetOption(talk_base::Socket::Option opt, int* value);
  virtual int SetOption(talk_base::Socket::Option opt, int value);
  virtual int GetError() const;
  virtual void SetError(int error);

  // P2PSocketClient::Delegate
  virtual void OnOpen(const net::IPEndPoint& address);
  virtual void OnError();
  virtual void OnDataReceived(const net::IPEndPoint& address,
                              const std::vector<char>& data);

 private:
  enum State {
    STATE_UNINITIALIZED,
    STATE_OPENING,
    STATE_OPEN,
    STATE_CLOSED,
    STATE_ERROR,
  };

  // Message loop on which this socket was created and being used.
  MessageLoop* message_loop_;

  // Corresponding P2P socket client.
  scoped_refptr<P2PSocketClient> client_;

  // Local address is allocated by the browser process, and the
  // renderer side doesn't know the address until it receives OnOpen()
  // event from the browser.
  talk_base::SocketAddress local_address_;
  bool address_initialized_;

  // Remote address for client TCP connections.
  talk_base::SocketAddress remote_address_;

  // Current state of the object.
  State state_;

  // Current error code. Valid when state_ == STATE_ERROR.
  int error_;

  DISALLOW_COPY_AND_ASSIGN(IpcPacketSocket);
};

IpcPacketSocket::IpcPacketSocket()
    : message_loop_(MessageLoop::current()),
      address_initialized_(false),
      state_(STATE_UNINITIALIZED), error_(0) {
}

IpcPacketSocket::~IpcPacketSocket() {
  if (state_ == STATE_OPENING || state_ == STATE_OPEN ||
      state_ == STATE_ERROR) {
    Close();
  }
}

bool IpcPacketSocket::Init(P2PSocketType type, P2PSocketClient* client,
                           const talk_base::SocketAddress& address) {
  DCHECK_EQ(MessageLoop::current(), message_loop_);
  DCHECK_EQ(state_, STATE_UNINITIALIZED);

  client_ = client;
  remote_address_ = address;
  state_ = STATE_OPENING;

  net::IPEndPoint address_chrome;
  if (!jingle_glue::SocketAddressToIPEndPoint(address, &address_chrome)) {
    return false;
  }

  client_->Init(type, address_chrome, this,
                base::MessageLoopProxy::CreateForCurrentThread());

  return true;
}

// talk_base::AsyncPacketSocket interface.
talk_base::SocketAddress IpcPacketSocket::GetLocalAddress(
     bool* allocated) const {
  DCHECK_EQ(MessageLoop::current(), message_loop_);

  *allocated = address_initialized_;
  return local_address_;
}

talk_base::SocketAddress IpcPacketSocket::GetRemoteAddress() const {
  DCHECK_EQ(MessageLoop::current(), message_loop_);

  return remote_address_;
}

int IpcPacketSocket::Send(const void *data, size_t data_size) {
  DCHECK_EQ(MessageLoop::current(), message_loop_);
  return SendTo(data, data_size, remote_address_);
}

int IpcPacketSocket::SendTo(const void *data, size_t data_size,
                            const talk_base::SocketAddress& address) {
  DCHECK_EQ(MessageLoop::current(), message_loop_);

  switch (state_) {
    case STATE_UNINITIALIZED:
      NOTREACHED();
      return EWOULDBLOCK;
    case STATE_OPENING:
      return EWOULDBLOCK;
    case STATE_CLOSED:
      return ENOTCONN;
    case STATE_ERROR:
      return error_;
    case STATE_OPEN:
      // Continue sending the packet.
      break;
  }

  const char* data_char = reinterpret_cast<const char*>(data);
  std::vector<char> data_vector(data_char, data_char + data_size);

  net::IPEndPoint address_chrome;
  if (!jingle_glue::SocketAddressToIPEndPoint(address, &address_chrome)) {
    // Just drop the packet if we failed to convert the address.
    return 0;
  }

  client_->Send(address_chrome, data_vector);

  // Fake successful send. The caller ignores result anyway.
  return data_size;
}

int IpcPacketSocket::Close() {
  DCHECK_EQ(MessageLoop::current(), message_loop_);

  client_->Close();
  state_ = STATE_CLOSED;

  return 0;
}

talk_base::Socket::ConnState IpcPacketSocket::GetState() const {
  DCHECK_EQ(MessageLoop::current(), message_loop_);

  switch (state_) {
    case STATE_UNINITIALIZED:
      NOTREACHED();
      return talk_base::Socket::CS_CONNECTING;

    case STATE_OPENING:
      return talk_base::Socket::CS_CONNECTING;

    case STATE_OPEN:
      return talk_base::Socket::CS_CONNECTED;

    case STATE_CLOSED:
    case STATE_ERROR:
      return talk_base::Socket::CS_CLOSED;
  }

  NOTREACHED();
  return talk_base::Socket::CS_CLOSED;
}

int IpcPacketSocket::GetOption(talk_base::Socket::Option opt, int* value) {
  // We don't support socket options for IPC sockets.
  return -1;
}

int IpcPacketSocket::SetOption(talk_base::Socket::Option opt, int value) {
  // We don't support socket options for IPC sockets.
  //
  // TODO(sergeyu): Make sure we set proper socket options on the
  // browser side.
  return -1;
}

int IpcPacketSocket::GetError() const {
  DCHECK_EQ(MessageLoop::current(), message_loop_);
  return error_;
}

void IpcPacketSocket::SetError(int error) {
  DCHECK_EQ(MessageLoop::current(), message_loop_);
  error_ = error;
}

void IpcPacketSocket::OnOpen(const net::IPEndPoint& address) {
  DCHECK_EQ(MessageLoop::current(), message_loop_);

  if (!jingle_glue::IPEndPointToSocketAddress(address, &local_address_)) {
    // Always expect correct IPv4 address to be allocated.
    NOTREACHED();
  }
  SignalAddressReady(this, local_address_);
  address_initialized_ = true;
  state_ = STATE_OPEN;
}

void IpcPacketSocket::OnError() {
  DCHECK_EQ(MessageLoop::current(), message_loop_);
  state_ = STATE_ERROR;
  error_ = ECONNABORTED;
}

void IpcPacketSocket::OnDataReceived(const net::IPEndPoint& address,
                                     const std::vector<char>& data) {
  DCHECK_EQ(MessageLoop::current(), message_loop_);

  talk_base::SocketAddress address_lj;
  if (!jingle_glue::IPEndPointToSocketAddress(address, &address_lj)) {
    // We should always be able to convert address here because we
    // don't expect IPv6 address on IPv4 connections.
    NOTREACHED();
    return;
  }

  SignalReadPacket(this, &data[0], data.size(), address_lj);
}

}  // namespace

IpcPacketSocketFactory::IpcPacketSocketFactory(
    P2PSocketDispatcher* socket_dispatcher)
    : socket_dispatcher_(socket_dispatcher) {
}

IpcPacketSocketFactory::~IpcPacketSocketFactory() {
}

talk_base::AsyncPacketSocket* IpcPacketSocketFactory::CreateUdpSocket(
    const talk_base::SocketAddress& local_address, int min_port, int max_port) {
  talk_base::SocketAddress crome_address;
  P2PSocketClient* socket_client = new P2PSocketClient(socket_dispatcher_);
  scoped_ptr<IpcPacketSocket> socket(new IpcPacketSocket());
  // TODO(sergeyu): Respect local_address and port limits here (need
  // to pass them over IPC channel to the browser).
  if (!socket->Init(P2P_SOCKET_UDP, socket_client,
                    talk_base::SocketAddress())) {
    return NULL;
  }

  // Socket increments reference count if Init() was successful.
  return socket.release();
}

talk_base::AsyncPacketSocket* IpcPacketSocketFactory::CreateServerTcpSocket(
    const talk_base::SocketAddress& local_address, int min_port, int max_port,
    bool listen, bool ssl) {
  // TODO(sergeyu): Implement this.
  NOTIMPLEMENTED();
  return NULL;
}

talk_base::AsyncPacketSocket* IpcPacketSocketFactory::CreateClientTcpSocket(
    const talk_base::SocketAddress& local_address,
    const talk_base::SocketAddress& remote_address,
    const talk_base::ProxyInfo& proxy_info,
    const std::string& user_agent, bool ssl) {
  // TODO(sergeyu): Implement this;
  NOTIMPLEMENTED();
  return NULL;
}