1 // Copyright 2018 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 SERVICES_NETWORK_TCP_BOUND_SOCKET_H_
6 #define SERVICES_NETWORK_TCP_BOUND_SOCKET_H_
7 
8 #include <memory>
9 
10 #include "base/component_export.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "mojo/public/cpp/bindings/pending_receiver.h"
15 #include "mojo/public/cpp/bindings/pending_remote.h"
16 #include "mojo/public/cpp/bindings/receiver_set.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/socket/tcp_socket.h"
19 #include "net/traffic_annotation/network_traffic_annotation.h"
20 #include "services/network/public/mojom/tcp_socket.mojom.h"
21 #include "services/network/tcp_server_socket.h"
22 
23 namespace net {
24 class IPEndPoint;
25 class NetLog;
26 }  // namespace net
27 
28 namespace network {
29 class SocketFactory;
30 
31 // A socket bound to an address. Can be converted into either a TCPServerSocket
32 // or a TCPConnectedSocket.
COMPONENT_EXPORT(NETWORK_SERVICE)33 class COMPONENT_EXPORT(NETWORK_SERVICE) TCPBoundSocket
34     : public mojom::TCPBoundSocket {
35  public:
36   // Constructs a TCPBoundSocket. |socket_factory| must outlive |this|. When a
37   // new connection is accepted, |socket_factory| will be notified to take
38   // ownership of the connection.
39   TCPBoundSocket(SocketFactory* socket_factory,
40                  net::NetLog* net_log,
41                  const net::NetworkTrafficAnnotationTag& traffic_annotation);
42   ~TCPBoundSocket() override;
43 
44   // Attempts to bind a socket to the specified address. Returns net::OK on
45   // success, setting |local_addr_out| to the bound address. Returns a network
46   // error code on failure. Must be called before Listen() or Connect().
47   int Bind(const net::IPEndPoint& local_addr, net::IPEndPoint* local_addr_out);
48 
49   // Sets the id used to remove the socket from the owning ReceiverSet. Must be
50   // called before Listen() or Connect().
51   void set_id(mojo::ReceiverId receiver_id) { receiver_id_ = receiver_id; }
52 
53   // mojom::TCPBoundSocket implementation.
54   void Listen(uint32_t backlog,
55               mojo::PendingReceiver<mojom::TCPServerSocket> receiver,
56               ListenCallback callback) override;
57   void Connect(const net::AddressList& remote_addr,
58                mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
59                mojo::PendingReceiver<mojom::TCPConnectedSocket> receiver,
60                mojo::PendingRemote<mojom::SocketObserver> observer,
61                ConnectCallback callback) override;
62 
63  private:
64   void OnConnectComplete(int result,
65                          const base::Optional<net::IPEndPoint>& local_addr,
66                          const base::Optional<net::IPEndPoint>& peer_addr,
67                          mojo::ScopedDataPipeConsumerHandle receive_stream,
68                          mojo::ScopedDataPipeProducerHandle send_stream);
69 
70   virtual int ListenInternal(int backlog);
71 
72   net::IPEndPoint bind_address_;
73 
74   mojo::ReceiverId receiver_id_ = -1;
75   SocketFactory* const socket_factory_;
76   std::unique_ptr<net::TCPSocket> socket_;
77   const net::NetworkTrafficAnnotationTag traffic_annotation_;
78 
79   mojo::PendingReceiver<mojom::TCPConnectedSocket> connected_socket_receiver_;
80   ConnectCallback connect_callback_;
81 
82   // Takes ownership of |socket_| if Connect() is called.
83   std::unique_ptr<TCPConnectedSocket> connecting_socket_;
84 
85   base::WeakPtrFactory<TCPBoundSocket> weak_factory_{this};
86 
87   DISALLOW_COPY_AND_ASSIGN(TCPBoundSocket);
88 };
89 
90 }  // namespace network
91 
92 #endif  // SERVICES_NETWORK_TCP_BOUND_SOCKET_H_
93