SatCat5
udp_tftp.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
19 
20 #pragma once
21 
22 #include <satcat5/udp_socket.h>
23 
24 namespace satcat5 {
25  namespace udp {
29  : public satcat5::net::Protocol
30  , public satcat5::poll::Timer
31  {
32  public:
34  explicit TftpTransfer(satcat5::udp::Dispatch* iface);
35  ~TftpTransfer();
36 
38  inline bool active() const
39  {return m_flags > 0;}
40 
42  inline u32 progress_blocks() const
43  {return m_block_id;}
45  inline u32 progress_bytes() const
46  {return m_xfer_bytes;}
47 
49  void reset(const char* msg);
50 
52  void request(
53  const satcat5::ip::Addr& dstaddr,
54  u16 opcode, const char* filename);
55 
57  bool is_duplicate_request();
58 
61  void accept();
62 
66  void file_send(satcat5::io::Readable* src, bool now);
67  void file_recv(satcat5::io::Writeable* dst, bool now);
69 
71  void send_error(u16 errcode);
72 
73  protected:
74  friend satcat5::test::TftpClient;
75  friend satcat5::test::TftpServer;
76 
77  // Inherited event handlers:
78  void frame_rcvd(satcat5::io::LimitedRead& src) override;
79  void timer_event() override;
80 
81  // Internal event handlers:
82  void read_data(u16 block_id, satcat5::io::LimitedRead& src);
83  void read_error(satcat5::io::LimitedRead& src);
84  void send_ack(u16 block_id);
85  void send_data(u16 block_id);
86  void send_packet(unsigned len, u16 retry);
87 
88  // Interface objects.
89  satcat5::udp::Address m_addr;
90  satcat5::io::Readable* m_src;
92 
93  // Transfer state uses soft-matching against an extended
94  // 32-bit Block-ID to allow files larger than 32 MiB.
95  u32 m_xfer_bytes;
96  u32 m_block_id;
97  u16 m_flags;
98 
99  // Internal buffer allows retransmission of lost packets.
100  // (Max 4-byte header + 512 bytes data = 516 bytes.)
101  u16 m_retry_count;
102  u16 m_retry_len;
103  u8 m_retry_buff[516];
104  };
105 
111  {
112  public:
114  explicit TftpClient(satcat5::udp::Dispatch* iface);
115  ~TftpClient() {}
116 
118  void begin_download(
120  const satcat5::ip::Addr& server,
121  const char* filename);
122 
124  void begin_upload(
126  const satcat5::ip::Addr& server,
127  const char* filename);
128 
130  inline bool active() const
131  {return m_xfer.active();}
133  inline u32 progress_blocks() const
134  {return m_xfer.progress_blocks();}
136  inline u32 progress_bytes() const
137  {return m_xfer.progress_bytes();}
138 
139  protected:
140  friend satcat5::test::TftpClient;
141 
142  // Upload or download state.
144  };
145 
150  {
151  public:
153  inline bool active() const
154  {return m_xfer.active();}
155 
156  protected:
159  explicit TftpServerCore(satcat5::udp::Dispatch* iface);
160  ~TftpServerCore();
161 
163  virtual satcat5::io::Readable* read(const char* filename) = 0;
164  virtual satcat5::io::Writeable* write(const char* filename) = 0;
165 
166  // Inherited event handler for handling incoming packets.
167  void frame_rcvd(satcat5::io::LimitedRead& src) override;
168 
169  // Connection to client (one at a time).
170  satcat5::udp::Dispatch* const m_iface;
172  };
173 
181  public:
185  satcat5::udp::Dispatch* iface,
188  ~TftpServerSimple() {}
189 
190  protected:
191  friend satcat5::test::TftpServer;
192 
193  // Required overrides from TftpServer.
194  satcat5::io::Readable* read(const char* filename) override;
195  satcat5::io::Writeable* write(const char* filename) override;
196 
197  // Interface objects.
198  satcat5::io::Readable* const m_src;
199  satcat5::io::Writeable* const m_dst;
200  };
201  }
202 }
Limited read of next N bytes.
Definition: io_readable.h:255
Abstract API for reading byte-streams and packets.
Definition: io_readable.h:68
Abstract API for writing byte-streams and packets.
Definition: io_writeable.h:24
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Implementation of "net::Address" for UDP Dispatch.
Definition: udp_core.h:72
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
A TFTP client makes request(s) to a remote server.
Definition: udp_tftp.h:111
void begin_upload(satcat5::io::Readable *src, const satcat5::ip::Addr &server, const char *filename)
Upload data from a Readable stream to the server.
Definition: udp_tftp.cc:431
u32 progress_bytes() const
Transfer progress, measured in bytes.
Definition: udp_tftp.h:136
u32 progress_blocks() const
Transfer progress, measured in 512-byte blocks.
Definition: udp_tftp.h:133
TftpClient(satcat5::udp::Dispatch *iface)
Attach this client to a network interface.
Definition: udp_tftp.cc:415
bool active() const
Is there an active transfer?
Definition: udp_tftp.h:130
void begin_download(satcat5::io::Writeable *dst, const satcat5::ip::Addr &server, const char *filename)
Download a file from server to a Writeable stream.
Definition: udp_tftp.cc:421
ServerCore is the base class that handles TFTP network functions.
Definition: udp_tftp.h:150
bool active() const
Is there an active transfer?
Definition: udp_tftp.h:153
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: udp_tftp.cc:454
virtual satcat5::io::Readable * read(const char *filename)=0
Child class MUST override these methods.
TftpServerCore(satcat5::udp::Dispatch *iface)
Users cannot instantiate this class directly.
Definition: udp_tftp.cc:441
TFTP server with a simple streaming source and sink.
Definition: udp_tftp.h:180
satcat5::io::Readable * read(const char *filename) override
Child class MUST override these methods.
Definition: udp_tftp.cc:500
TftpServerSimple(satcat5::udp::Dispatch *iface, satcat5::io::Readable *src, satcat5::io::Writeable *dst)
Attach this server to a network interface, and to a source and sink for transfered data.
Definition: udp_tftp.cc:489
Transfer objects used by both TftpClient and TftpServer.
Definition: udp_tftp.h:31
void file_send(satcat5::io::Readable *src, bool now)
Begin transfer of a single file (DATA-ACK-DATA-ACK).
Definition: udp_tftp.cc:170
u32 progress_blocks() const
Transfer progress, measured in 512-byte blocks.
Definition: udp_tftp.h:42
void request(const satcat5::ip::Addr &dstaddr, u16 opcode, const char *filename)
Issue a write-request or read-request.
Definition: udp_tftp.cc:112
bool is_duplicate_request()
Before calling accept, test if this is a duplicate request.
Definition: udp_tftp.cc:137
void timer_event() override
Child class MUST override this method.
Definition: udp_tftp.cc:253
void accept()
Accept remote connection and note reply address.
Definition: udp_tftp.cc:149
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: udp_tftp.cc:209
void file_recv(satcat5::io::Writeable *dst, bool now)
Begin transfer of a single file (DATA-ACK-DATA-ACK).
Definition: udp_tftp.cc:190
void send_error(u16 errcode)
Send an error message.
Definition: udp_tftp.cc:363
TftpTransfer(satcat5::udp::Dispatch *iface)
Create an idle connection object.
Definition: udp_tftp.cc:62
u32 progress_bytes() const
Transfer progress, measured in bytes.
Definition: udp_tftp.h:45
bool active() const
Is there a transfer in progress?
Definition: udp_tftp.h:38
void reset(const char *msg)
Immediately revert to the idle state.
Definition: udp_tftp.cc:82
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15