1 // Copyright 2017 The Chromium Authors 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_SOCKET_FUZZED_SERVER_SOCKET_H_ 6 #define NET_SOCKET_FUZZED_SERVER_SOCKET_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 12 #include "base/memory/raw_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "net/base/completion_once_callback.h" 15 #include "net/base/ip_endpoint.h" 16 #include "net/socket/server_socket.h" 17 18 class FuzzedDataProvider; 19 20 namespace net { 21 22 class NetLog; 23 class StreamSocket; 24 25 // A ServerSocket that uses a FuzzedDataProvider to generate the input the 26 // server receives. It succeeds in Accept()ing, asynchronously, a single 27 // connection with that input; later calls to Accept will just return 28 // ERR_IO_PENDING but will not invoke the callback. 29 class FuzzedServerSocket : public ServerSocket { 30 public: 31 // |data_provider| is used as to determine behavior of the socket. It 32 // must remain valid until after both this object and the StreamSocket 33 // produced by Accept are destroyed. 34 FuzzedServerSocket(FuzzedDataProvider* data_provider, net::NetLog* net_log); 35 36 FuzzedServerSocket(const FuzzedServerSocket&) = delete; 37 FuzzedServerSocket& operator=(const FuzzedServerSocket&) = delete; 38 39 ~FuzzedServerSocket() override; 40 41 int Listen(const IPEndPoint& address, 42 int backlog, 43 std::optional<bool> ipv6_only) override; 44 int GetLocalAddress(IPEndPoint* address) const override; 45 46 int Accept(std::unique_ptr<StreamSocket>* socket, 47 CompletionOnceCallback callback) override; 48 49 private: 50 void DispatchAccept(std::unique_ptr<StreamSocket>* socket, 51 CompletionOnceCallback callback); 52 53 raw_ptr<FuzzedDataProvider> data_provider_; 54 raw_ptr<net::NetLog> net_log_; 55 56 IPEndPoint listening_on_; 57 bool first_accept_ = true; 58 bool listen_called_ = false; 59 60 base::WeakPtrFactory<FuzzedServerSocket> weak_factory_{this}; 61 }; 62 63 } // namespace net 64 65 #endif // NET_SOCKET_FUZZED_SERVER_SOCKET_H_ 66