curl-fuzzer C++ reference
Loading...
Searching...
No Matches
proto_fuzzer::MockConnection Class Reference

Owns one half of a socketpair used to feed canned responses to libcurl. More...

Public Member Functions

 MockConnection ()
 Construct a non-blocking AF_UNIX/SOCK_STREAM socketpair.
virtual ~MockConnection ()
 Close the server-side fd (and the client-side fd if it was never handed off via take_client_fd()).
virtual bool ok () const
curl_socket_t take_client_fd ()
 Hand the client-side fd to libcurl.
int server_fd () const
std::size_t client_send_buffer_size () const
 Query the capacity curl's endpoint reports for queued client writes while this object still owns that endpoint.
bool EnsureClientSendBufferSize (std::size_t minimum)
 Ensure curl's endpoint reports at least minimum bytes of send buffer, requesting a larger SO_SNDBUF when the platform default is smaller.
bool FillClientSendBuffer ()
 Saturate curl's endpoint before ownership is transferred.
virtual bool WriteAll (const unsigned char *data, std::size_t size)
 Write 'size' bytes from 'data' to the server fd, looping until the whole buffer is sent or a short/failed write occurs.
virtual std::size_t DrainIncoming ()
 Advance incoming transport work according to the configured per-call limit.
void SetIncomingDataObserver (IncomingDataObserver *observer)
 Register a non-owning sink for bytes consumed from curl.
void ReadAvailable (std::string *out)
 Non-blocking read: append whatever bytes are currently available on the server fd to 'out'.
virtual void ShutdownWrite ()
 Signal end-of-response to libcurl by half-closing the write side.
void ApplyBackpressure (int recv_buf_bytes, std::size_t drain_limit)
 Apply deterministic backpressure knobs before traffic begins.

Protected Member Functions

 MockConnection (int adopted_server_fd)
 Adopt one connected descriptor as this peer's server endpoint.
void NotifyIncomingData (const unsigned char *data, std::size_t size)
 Forward one application-data fragment to the optional observer.

Detailed Description

Owns one half of a socketpair used to feed canned responses to libcurl.

The destructor closes the server-side fd; the client-side fd is handed to libcurl via CURLOPT_OPENSOCKETFUNCTION and becomes curl's to close.

Definition at line 29 of file mock_server.h.

Constructor & Destructor Documentation

◆ MockConnection() [1/2]

proto_fuzzer::MockConnection::MockConnection ( )

Construct a non-blocking AF_UNIX/SOCK_STREAM socketpair.

Both fds are validated to fit inside FD_SETSIZE; on any failure ok() returns false and the instance is unusable.

Definition at line 58 of file mock_server.cc.

◆ MockConnection() [2/2]

proto_fuzzer::MockConnection::MockConnection ( int adopted_server_fd)
explicitprotected

Adopt one connected descriptor as this peer's server endpoint.

This path lets real loopback listeners reuse the bounded transport machinery without manufacturing a socketpair whose client endpoint curl cannot inherit.

Parameters
adopted_server_fdConnected descriptor whose ownership transfers.

Definition at line 89 of file mock_server.cc.

Member Function Documentation

◆ ApplyBackpressure()

void proto_fuzzer::MockConnection::ApplyBackpressure ( int recv_buf_bytes,
std::size_t drain_limit )

Apply deterministic backpressure knobs before traffic begins.

A positive recv_buf_bytes sets SO_RCVBUF on the server fd and SO_SNDBUF on the client fd, which this object still owns, to make curl short-write or see EAGAIN. Linux socketpairs effectively use max(SNDBUF, RCVBUF*2) as pipe capacity, so both settings are needed to produce short writes reliably. Cap each DrainIncoming call at drain_limit bytes; zero leaves the drain unlimited.

Parameters
recv_buf_bytesSO_RCVBUF size in bytes, or 0 to leave default.
drain_limitMax bytes drained per DrainIncoming call, 0 for unlimited.

Definition at line 272 of file mock_server.cc.

◆ client_send_buffer_size()

std::size_t proto_fuzzer::MockConnection::client_send_buffer_size ( ) const

Query the capacity curl's endpoint reports for queued client writes while this object still owns that endpoint.

Synchronous protocol mocks use this before transferring fd ownership because they cannot rely on the outer driver to make room. A zero result covers both an invalid fd and a platform query failure; callers need a verified capacity before proceeding.

Returns
SO_SNDBUF in bytes, or zero when it cannot be queried.

Definition at line 123 of file mock_server.cc.

◆ DrainIncoming()

std::size_t proto_fuzzer::MockConnection::DrainIncoming ( )
virtual

Advance incoming transport work according to the configured per-call limit.

This plaintext implementation reports bytes consumed; layered transports may also report handshake state changes so the bounded outer loop does not mistake useful protocol progress for an idle connection. When a backpressure drain limit has been applied (see ApplyBackpressure), stop after drain_limit_ bytes so the kernel recv buffer stays near-full and curl keeps seeing short writes. Otherwise drain until read() returns 0/EAGAIN, matching legacy behaviour.

Returns
number of bytes consumed during this call.

Definition at line 225 of file mock_server.cc.

◆ EnsureClientSendBufferSize()

bool proto_fuzzer::MockConnection::EnsureClientSendBufferSize ( std::size_t minimum)

Ensure curl's endpoint reports at least minimum bytes of send buffer, requesting a larger SO_SNDBUF when the platform default is smaller.

Linux may transform socket-buffer requests, so the post-set query establishes the contract rather than assuming setsockopt accepted the exact value.

Parameters
minimumSmallest acceptable reported capacity in bytes.
Returns
true when the queried postcondition holds.

Definition at line 141 of file mock_server.cc.

◆ FillClientSendBuffer()

bool proto_fuzzer::MockConnection::FillClientSendBuffer ( )

Saturate curl's endpoint before ownership is transferred.

The temporary non-blocking flag makes the stopping condition explicit and is restored so curl receives the same descriptor state as every other stream peer.

Returns
true only when the endpoint reached EAGAIN/EWOULDBLOCK.

Definition at line 159 of file mock_server.cc.

◆ NotifyIncomingData()

void proto_fuzzer::MockConnection::NotifyIncomingData ( const unsigned char * data,
std::size_t size )
protected

Forward one application-data fragment to the optional observer.

Parameters
dataFirst byte of the fragment.
sizeNumber of bytes available at data.

Definition at line 258 of file mock_server.cc.

◆ ok()

bool proto_fuzzer::MockConnection::ok ( ) const
virtual
Returns
true if the underlying socketpair was set up successfully.

Definition at line 112 of file mock_server.cc.

◆ ReadAvailable()

void proto_fuzzer::MockConnection::ReadAvailable ( std::string * out)

Non-blocking read: append whatever bytes are currently available on the server fd to 'out'.

Used by the WS handshake path to collect curl's HTTP Upgrade request without losing any bytes.

Parameters
outDestination buffer; unchanged if no bytes are pending.

Definition at line 288 of file mock_server.cc.

◆ server_fd()

int proto_fuzzer::MockConnection::server_fd ( ) const
Returns
the server-side fd (still owned by this MockConnection).

Definition at line 115 of file mock_server.cc.

◆ SetIncomingDataObserver()

void proto_fuzzer::MockConnection::SetIncomingDataObserver ( IncomingDataObserver * observer)

Register a non-owning sink for bytes consumed from curl.

TLS transports notify it after decryption, while plaintext transports report socket bytes directly. The observer must outlive this connection.

Parameters
observerObserver to notify, or nullptr to disable observation.

Definition at line 253 of file mock_server.cc.

◆ take_client_fd()

curl_socket_t proto_fuzzer::MockConnection::take_client_fd ( )

Hand the client-side fd to libcurl.

After this call the caller owns the fd and the MockConnection will not close it on destruction.

Returns
the client-side socket fd as a curl_socket_t.

Definition at line 187 of file mock_server.cc.

◆ WriteAll()

bool proto_fuzzer::MockConnection::WriteAll ( const unsigned char * data,
std::size_t size )
virtual

Write 'size' bytes from 'data' to the server fd, looping until the whole buffer is sent or a short/failed write occurs.

MSG_NOSIGNAL is a harness invariant rather than a curl behavior choice: a response race may leave the peer closed, and that must look like an ordinary failed mock write instead of terminating the fuzz process with SIGPIPE.

Parameters
dataBuffer to send.
sizeNumber of bytes in 'data'.
Returns
false on short or failed write (treat the connection as lost).

Definition at line 201 of file mock_server.cc.


The documentation for this class was generated from the following files: