blob: d0933f254f58ea1b1875d59560b324c80f707fc0 [file] [log] [blame]
Sergey Ulanov7de85802018-01-19 19:50:341// Copyright 2017 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 NET_TEST_TCP_SOCKET_PROXY_H_
6#define NET_TEST_TCP_SOCKET_PROXY_H_
7
8#include <stdint.h>
9
10#include <memory>
11
12#include "base/memory/ref_counted.h"
13#include "base/threading/thread_checker.h"
14
15namespace base {
16class SingleThreadTaskRunner;
17} // namespace base
18
19namespace net {
20
21class IPEndPoint;
22
23// TcpSocketProxy proxies TCP connection from localhost to a remote IP address.
24class TcpSocketProxy {
25 public:
26 explicit TcpSocketProxy(
27 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
28 ~TcpSocketProxy();
29
30 // Initializes local socket for the proxy. If |local_port| is not 0 then the
31 // proxy will listen on that port. Otherwise the socket will be bound to an
32 // available port and local_port() should be used to get the port number.
33 // Returns false if initialization fails.
34 bool Initialize(int local_port = 0);
35
36 // Local port number for the proxy or 0 if the proxy is not initialized.
37 uint16_t local_port() const { return local_port_; }
38
39 // Starts the proxy for the specified |remote_endpoint|. Must be called after
40 // a successful Initialize() call and before any incoming connection on
41 // local_port() are initiated. Port number in |remote_endpoint| may be
42 // different from local_port().
43 void Start(const IPEndPoint& remote_endpoint);
44
45 private:
46 class Core;
47
48 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
49
50 // Core implements the proxy functionality. It runs on |io_task_runner_|.
51 std::unique_ptr<Core> core_;
52
53 uint16_t local_port_ = 0;
54
55 THREAD_CHECKER(thread_checker_);
56
57 DISALLOW_COPY_AND_ASSIGN(TcpSocketProxy);
58};
59
60} // namespace net
61
62#endif // NET_TEST_TCP_SOCKET_PROXY_H_