12 #include <string_view>
13 #include <unordered_map>
23 enum Type : std::uint16_t {
83 : Type_(
Type), Filename(Filename), Mode(Mode) {
88 : Type_(
Type), Filename(std::move(Filename)), Mode(std::move(Mode)) {
93 const std::vector<std::string> &OptionsNames,
const std::vector<std::string> &OptionsValues)
95 this->OptionsNames = OptionsNames;
96 this->OptionsValues = OptionsValues;
100 std::vector<std::string> &&OptionsValues) noexcept
101 : Type_(
Type), Filename(std::move(Filename)), Mode(std::move(Mode)), OptionsNames(std::move(OptionsNames)),
102 OptionsValues(std::move(OptionsValues)) {
109 template <
class OutputIterator> std::size_t
serialize(OutputIterator It)
const noexcept {
110 assert(OptionsNames.size() == OptionsValues.size());
112 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 0);
113 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 8);
115 for (
auto Byte : Filename) {
116 *(It++) =
static_cast<std::uint8_t
>(Byte);
120 for (
auto Byte : Mode) {
121 *(It++) =
static_cast<std::uint8_t
>(Byte);
125 std::size_t OptionsSize = 0;
126 for (std::size_t Idx = 0; Idx != OptionsNames.size(); ++Idx) {
127 for (
auto Byte : OptionsNames[Idx]) {
128 *(It++) =
static_cast<std::uint8_t
>(Byte);
131 for (
auto Byte : OptionsValues[Idx]) {
132 *(It++) =
static_cast<std::uint8_t
>(Byte);
135 OptionsSize += OptionsNames[Idx].size() + OptionsValues[Idx].size() + 2;
138 return sizeof(Type_) + Filename.size() + Mode.size() + OptionsSize + 2;
141 std::uint16_t
getType() const noexcept {
return Type_; }
143 std::string_view
getFilename() const noexcept {
return std::string_view(Filename.data(), Filename.size()); }
145 std::string_view
getMode() const noexcept {
return std::string_view(Mode.data(), Mode.size()); }
148 return std::string_view(OptionsNames[Idx].data(), OptionsNames[Idx].size());
152 return std::string_view(OptionsValues[Idx].data(), OptionsValues[Idx].size());
159 std::string Filename;
161 std::vector<std::string> OptionsNames;
162 std::vector<std::string> OptionsValues;
172 Data(std::uint16_t Block,
const std::vector<std::uint8_t> &Buffer)
173 : Block(Block), DataBuffer(Buffer.begin(), Buffer.end()) {
177 assert(Buffer.size() >= 0 && Buffer.size() <= 512);
181 Data(std::uint16_t Block, std::vector<std::uint8_t> &&Buffer) noexcept : Block(Block) {
185 assert(Buffer.size() >= 0 && Buffer.size() <= 512);
186 this->DataBuffer = std::move(Buffer);
192 template <
class OutputIterator> std::size_t
serialize(OutputIterator It)
const noexcept {
193 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 0);
194 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 8);
195 *(It++) =
static_cast<std::uint8_t
>(htons(Block) >> 0);
196 *(It++) =
static_cast<std::uint8_t
>(htons(Block) >> 8);
197 for (
auto Byte : DataBuffer) {
201 return sizeof(Type_) +
sizeof(Block) + DataBuffer.size();
204 std::uint16_t
getType() const noexcept {
return Type_; }
206 std::uint16_t
getBlock() const noexcept {
return Block; }
208 const std::vector<std::uint8_t> &
getData() const noexcept {
return DataBuffer; }
215 std::vector<std::uint8_t> DataBuffer;
229 std::uint16_t
getType() const noexcept {
return Type_; }
231 std::uint16_t
getBlock() const noexcept {
return Block; }
236 template <
class OutputIterator> std::size_t
serialize(OutputIterator It)
const noexcept {
237 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 0);
238 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 8);
239 *(It++) =
static_cast<std::uint8_t
>(htons(Block) >> 0);
240 *(It++) =
static_cast<std::uint8_t
>(htons(Block) >> 8);
242 return sizeof(Type_) +
sizeof(Block);
258 Error(std::uint16_t ErrorCode, std::string_view ErrorMessage) : ErrorCode(ErrorCode), ErrorMessage(ErrorMessage) {
259 assert(ErrorCode >= 0 && ErrorCode <= 8);
262 Error(std::uint16_t ErrorCode, std::string &&ErrorMessage)
263 : ErrorCode(ErrorCode), ErrorMessage(std::move(ErrorMessage)) {
264 assert(ErrorCode >= 0 && ErrorCode <= 8);
267 std::uint16_t
getType() const noexcept {
return Type_; }
272 return std::string_view(ErrorMessage.data(), ErrorMessage.size());
278 template <
class OutputIterator> std::size_t
serialize(OutputIterator It)
const noexcept {
279 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 0);
280 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 8);
281 *(It++) =
static_cast<std::uint8_t
>(htons(ErrorCode) >> 0);
282 *(It++) =
static_cast<std::uint8_t
>(htons(ErrorCode) >> 8);
284 for (
auto Byte : ErrorMessage) {
289 return sizeof(Type_) +
sizeof(ErrorCode) + ErrorMessage.size() + 1;
296 std::uint16_t ErrorCode;
297 std::string ErrorMessage;
310 template <
class OutputIterator> std::size_t
serialize(OutputIterator It)
const noexcept {
311 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 0);
312 *(It++) =
static_cast<std::uint8_t
>(htons(Type_) >> 8);
314 std::size_t OptionsSize = 0;
315 for (
const auto &[Key, Value] : Options) {
316 for (
auto Byte : Key) {
317 *(It++) =
static_cast<std::uint8_t
>(Byte);
320 for (
auto Byte : Value) {
321 *(It++) =
static_cast<std::uint8_t
>(Byte);
324 OptionsSize += Key.size() + Value.size() + 2;
327 return sizeof(Type_) + OptionsSize;
330 std::uint16_t
getType() const noexcept {
return Type_; }
333 auto begin() noexcept {
return Options.begin(); }
336 auto begin() const noexcept {
return Options.begin(); }
339 auto cbegin() const noexcept {
return Options.cbegin(); }
342 auto end() noexcept {
return Options.end(); }
345 auto end() const noexcept {
return Options.end(); }
348 auto cend() const noexcept {
return Options.cend(); }
352 std::string_view
getOptionValue(
const std::string &OptionName)
const noexcept {
return Options.at(OptionName); }
359 std::unordered_map<std::string, std::string> Options;
Acknowledgment Trivial File Transfer Protocol packet.
Definition: packets.hpp:219
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Acknowledgment &Packet)
Definition: parsers.hpp:153
std::uint16_t getType() const noexcept
Definition: packets.hpp:229
std::size_t serialize(OutputIterator It) const noexcept
Definition: packets.hpp:236
Acknowledgment()=default
Use with parsing functions only.
Acknowledgment(std::uint16_t Block) noexcept
Definition: packets.hpp:224
std::uint16_t getBlock() const noexcept
Definition: packets.hpp:231
Data Trivial File Transfer Protocol packet.
Definition: packets.hpp:166
Data(std::uint16_t Block, const std::vector< std::uint8_t > &Buffer)
Definition: packets.hpp:172
Data()=default
Use with parsing functions only.
Data(std::uint16_t Block, std::vector< std::uint8_t > &&Buffer) noexcept
Definition: packets.hpp:181
std::size_t serialize(OutputIterator It) const noexcept
Definition: packets.hpp:192
std::uint16_t getType() const noexcept
Definition: packets.hpp:204
const std::vector< std::uint8_t > & getData() const noexcept
Definition: packets.hpp:208
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Data &Packet)
Definition: parsers.hpp:98
std::uint16_t getBlock() const noexcept
Definition: packets.hpp:206
Error Trivial File Transfer Protocol packet.
Definition: packets.hpp:253
Error(std::uint16_t ErrorCode, std::string &&ErrorMessage)
Definition: packets.hpp:262
std::string_view getErrorMessage() const noexcept
Definition: packets.hpp:271
Error()=default
Use with parsing functions only.
std::uint16_t getErrorCode() const noexcept
Definition: packets.hpp:269
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Error &Packet)
Definition: parsers.hpp:198
std::uint16_t getType() const noexcept
Definition: packets.hpp:267
std::size_t serialize(OutputIterator It) const noexcept
Definition: packets.hpp:278
Error(std::uint16_t ErrorCode, std::string_view ErrorMessage)
Definition: packets.hpp:258
Option Acknowledgment Trivial File Transfer Protocol packet.
Definition: packets.hpp:301
auto end() noexcept
Definition: packets.hpp:342
std::uint16_t getType() const noexcept
Definition: packets.hpp:330
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, OptionAcknowledgment &Packet)
Definition: parsers.hpp:252
OptionAcknowledgment(std::unordered_map< std::string, std::string > Options)
Definition: packets.hpp:305
auto begin() const noexcept
Definition: packets.hpp:336
OptionAcknowledgment()=default
Use with parsing functions only.
std::size_t serialize(OutputIterator It) const noexcept
Definition: packets.hpp:310
auto cbegin() const noexcept
Definition: packets.hpp:339
auto end() const noexcept
Definition: packets.hpp:345
std::string_view getOptionValue(const std::string &OptionName) const noexcept
Definition: packets.hpp:352
auto begin() noexcept
Definition: packets.hpp:333
auto cend() const noexcept
Definition: packets.hpp:348
Read/Write Request (RRQ/WRQ) Trivial File Transfer Protocol packet.
Definition: packets.hpp:77
Request(types::Type Type, std::string &&Filename, std::string &&Mode) noexcept
Definition: packets.hpp:87
std::size_t serialize(OutputIterator It) const noexcept
Definition: packets.hpp:109
std::string_view getOptionName(std::size_t Idx) const noexcept
Definition: packets.hpp:147
Request(types::Type Type, std::string_view Filename, std::string_view Mode, const std::vector< std::string > &OptionsNames, const std::vector< std::string > &OptionsValues)
Definition: packets.hpp:92
std::string_view getOptionValue(std::size_t Idx) const noexcept
Definition: packets.hpp:151
Request(types::Type Type, std::string_view Filename, std::string_view Mode)
Definition: packets.hpp:82
Request()=default
Use with parsing functions only.
std::string_view getFilename() const noexcept
Definition: packets.hpp:143
friend ParseResult parse(const std::uint8_t *Buffer, std::size_t Len, Request &Packet)
Definition: parsers.hpp:19
std::string_view getMode() const noexcept
Definition: packets.hpp:145
Request(types::Type Type, std::string &&Filename, std::string &&Mode, std::vector< std::string > &&OptionsNames, std::vector< std::string > &&OptionsValues) noexcept
Definition: packets.hpp:99
std::uint16_t getType() const noexcept
Definition: packets.hpp:141
Error
Trivial File Transfer Protocol error code.
Definition: packets.hpp:43
@ UnknownTransferID
Unknown transfer ID error code.
Definition: packets.hpp:55
@ FileNotFound
File not found error code.
Definition: packets.hpp:47
@ NoSuchUser
No such user error code.
Definition: packets.hpp:59
@ NotDefined
Not defined, see error message (if any) error code.
Definition: packets.hpp:45
@ IllegalOperation
Illegal TFTP operation error code.
Definition: packets.hpp:53
@ AccessViolation
Access violation error code.
Definition: packets.hpp:49
@ FileAlreadyExists
File already exists error code.
Definition: packets.hpp:57
@ DiskFull
Disk full or allocation exceeded error code.
Definition: packets.hpp:51
TransferMode
Trivial File Transfer Protocol transfer mode.
Definition: packets.hpp:67
@ NetAscii
netascii transfer mode
Definition: packets.hpp:69
@ Octet
octet (binary) transfer mode
Definition: packets.hpp:71
Type
Trivial File Transfer Protocol packet type.
Definition: packets.hpp:23
@ AcknowledgmentPacket
Acknowledgment (ACK) operation code.
Definition: packets.hpp:31
@ ReadRequest
Read request (RRQ) operation code.
Definition: packets.hpp:25
@ OptionAcknowledgmentPacket
Option Acknowledgment (OACK) operation code.
Definition: packets.hpp:35
@ DataPacket
Data (DATA) operation code.
Definition: packets.hpp:29
@ WriteRequest
Write request (WRQ) operation code.
Definition: packets.hpp:27
@ ErrorPacket
Error (ERROR) operation code.
Definition: packets.hpp:33
Definition: packets.hpp:16
The result of parsing a single packet.
Definition: parsers.hpp:8