curl-fuzzer C++ reference
Loading...
Searching...
No Matches
tftp_mock_server.h
Go to the documentation of this file.
1/*
2 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3 *
4 * SPDX-License-Identifier: curl
5 */
6
9
10#ifndef PROTO_FUZZER_TFTP_MOCK_SERVER_H_
11#define PROTO_FUZZER_TFTP_MOCK_SERVER_H_
12
13#include <netinet/in.h>
14
15#include <array>
16#include <cstddef>
17#include <cstdint>
18#include <string>
19#include <vector>
20
21#include "curl_fuzzer.pb.h"
24
25namespace proto_fuzzer {
26
34
44
60class TftpMockServer final : public MockServerBase {
61 public:
63 ~TftpMockServer() override;
64
65 void ObserveState() override;
66
67 TftpMockServer(const TftpMockServer&) = delete;
68 TftpMockServer& operator=(const TftpMockServer&) = delete;
69
70 const std::vector<TftpReceivedDatagram>& received_datagrams() const;
71
72 std::uint16_t request_port() const;
73
74 std::uint16_t transfer_port() const;
75
76 protected:
77 curl_socket_t HandleOpenSocket(curlsocktype purpose, struct curl_sockaddr* address) override;
78
79 SocketSetupDisposition GetSocketSetupDisposition(curl_socket_t curlfd, curlsocktype purpose) const override;
80
81 void RunLoop(CURLM* multi, CURL* easy, const curl::fuzzer::proto::Scenario& scenario) override;
82
83 private:
87 static constexpr std::size_t kMaxCapturedDatagrams = scenario_limits::kMaxResponseChunks + 2;
88
89 void PrepareScript(const curl::fuzzer::proto::Connection& connection);
90
91 void ResetPeer();
92
93 static bool ConfigureSocket(int fd);
94
95 static int OpenLoopbackSocket(struct sockaddr_in* bound_address);
96
97 static bool RewriteDestination(struct curl_sockaddr* address, const struct sockaddr_in& destination);
98
99 std::size_t DrainSocket(int fd, TftpSocketRole role);
100
101 std::size_t DrainClientDatagrams();
102
103 void RememberClientAddress(const struct sockaddr_in& address, socklen_t length);
104
105 bool SendNextDatagram();
106
107 std::array<const std::string*, scenario_limits::kMaxResponseChunks + 1> response_datagrams_;
108 std::size_t response_datagram_count_;
109 std::size_t next_response_datagram_;
110
111 int request_fd_;
112 int transfer_fd_;
113 struct sockaddr_in client_address_;
114 socklen_t client_address_length_;
115 bool has_client_address_;
116 bool socket_opened_;
117 std::uint16_t request_port_;
118 std::uint16_t transfer_port_;
119 std::vector<TftpReceivedDatagram> received_datagrams_;
120};
121
122} // namespace proto_fuzzer
123
124#endif // PROTO_FUZZER_TFTP_MOCK_SERVER_H_
~TftpMockServer() override
Close the two server endpoints after the base drive has removed curl's separately-owned client descri...
void RunLoop(CURLM *multi, CURL *easy, const curl::fuzzer::proto::Scenario &scenario) override
Give curl one nonblocking state-machine turn, retain all client packets that turn produced,...
SocketSetupDisposition GetSocketSetupDisposition(curl_socket_t curlfd, curlsocktype purpose) const override
TFTP returns an unconnected IPv4 datagram descriptor, so curl must still bind/configure it before the...
std::uint16_t request_port() const
Expose the kernel-selected request endpoint for transfer-ID assertions, not for routing.
void ObserveState() override
Retain protocol outcomes in coverage and introspector builds.
curl_socket_t HandleOpenSocket(curlsocktype purpose, struct curl_sockaddr *address) override
Create both server transfer IDs before returning curl's client socket.
std::uint16_t transfer_port() const
Expose the kernel-selected response endpoint for transfer-ID assertions, not for routing.
TftpMockServer()
Start without live descriptors or borrowed scenario storage.
const std::vector< TftpReceivedDatagram > & received_datagrams() const
Return bounded observations from the most recent run rather than parsing them in the peer.
MockServerBase — common plumbing shared by every protocol-specific mock server.
SocketSetupDisposition
Tell curl whether an application-provided socket still needs its normal connect/options setup or alre...
Shared complexity limits for structured scenarios.
constexpr std::size_t kMaxResponseChunks
Enough response boundaries to exercise incremental parsers while keeping per-perform delivery work fi...
One bounded observation of a datagram curl sent to the in-process peer.
TftpSocketRole received_on
Server endpoint that received the packet.
std::uint16_t source_port
Curl's UDP transfer ID in host byte order.
std::string bytes
Complete UDP payload, including TFTP header.
TftpSocketRole
Identifies which stable server transfer ID received a client datagram.
@ kRequest
Endpoint placed in curl's rewritten destination address.
@ kTransfer
Endpoint used for replies and the rest of the exchange.