fuzzed_datagram_client_socket.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2016 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. #ifndef NET_SOCKET_FUZZED_DATAGRAM_CLIENT_SOCKET_H_
  5. #define NET_SOCKET_FUZZED_DATAGRAM_CLIENT_SOCKET_H_
  6. #include "net/socket/datagram_client_socket.h"
  7. #include <stdint.h>
  8. #include "base/memory/weak_ptr.h"
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/base/ip_endpoint.h"
  11. #include "net/base/network_handle.h"
  12. #include "net/log/net_log_with_source.h"
  13. #include "net/traffic_annotation/network_traffic_annotation.h"
  14. class FuzzedDataProvider;
  15. namespace net {
  16. class IOBuffer;
  17. // Datagram ClientSocket implementation for use with fuzzers. Can fail to
  18. // connect, reads and writes can succeed or fail synchronously or
  19. // asynchronously. Successful reads return random data.
  20. class FuzzedDatagramClientSocket : public DatagramClientSocket {
  21. public:
  22. // |data_provider| must outlive the created socket.
  23. explicit FuzzedDatagramClientSocket(FuzzedDataProvider* data_provider);
  24. FuzzedDatagramClientSocket(const FuzzedDatagramClientSocket&) = delete;
  25. FuzzedDatagramClientSocket& operator=(const FuzzedDatagramClientSocket&) =
  26. delete;
  27. ~FuzzedDatagramClientSocket() override;
  28. // DatagramClientSocket implementation:
  29. int Connect(const IPEndPoint& address) override;
  30. int ConnectUsingNetwork(handles::NetworkHandle network,
  31. const IPEndPoint& address) override;
  32. int ConnectUsingDefaultNetwork(const IPEndPoint& address) override;
  33. handles::NetworkHandle GetBoundNetwork() const override;
  34. void ApplySocketTag(const SocketTag& tag) override;
  35. // DatagramSocket implementation:
  36. void Close() override;
  37. int GetPeerAddress(IPEndPoint* address) const override;
  38. int GetLocalAddress(IPEndPoint* address) const override;
  39. void UseNonBlockingIO() override;
  40. int SetMulticastInterface(uint32_t interface_index) override;
  41. const NetLogWithSource& NetLog() const override;
  42. // Socket implementation:
  43. int Read(IOBuffer* buf,
  44. int buf_len,
  45. CompletionOnceCallback callback) override;
  46. int Write(IOBuffer* buf,
  47. int buf_len,
  48. CompletionOnceCallback callback,
  49. const NetworkTrafficAnnotationTag& traffic_annotation) override;
  50. int SetReceiveBufferSize(int32_t size) override;
  51. int SetSendBufferSize(int32_t size) override;
  52. int SetDoNotFragment() override;
  53. void SetMsgConfirm(bool confirm) override {}
  54. private:
  55. void OnReadComplete(net::CompletionOnceCallback callback, int result);
  56. void OnWriteComplete(net::CompletionOnceCallback callback, int result);
  57. FuzzedDataProvider* data_provider_;
  58. bool connected_ = false;
  59. bool read_pending_ = false;
  60. bool write_pending_ = false;
  61. NetLogWithSource net_log_;
  62. IPEndPoint remote_address_;
  63. base::WeakPtrFactory<FuzzedDatagramClientSocket> weak_factory_{this};
  64. };
  65. } // namespace net
  66. #endif // NET_SOCKET_FUZZED_DATAGRAM_CLIENT_SOCKET_H_