8 #include <satcat5/coap_writer.h>
21 #ifndef SATCAT5_COAP_PROXY_SILENT
22 #define SATCAT5_COAP_PROXY_SILENT 1
26 constexpr u32 TOKEN_MASK = 0xFFFFFFFEu;
27 constexpr u32 token_client(u32 x)
28 {
return (x & TOKEN_MASK) + 0; }
29 constexpr u32 token_server(u32 x)
30 {
return (x & TOKEN_MASK) + 1; }
32 ProxyResource::ProxyResource(
34 const char* local_uri,
40 , m_fwd_addr(fwd_addr)
41 , m_fwd_port(fwd_port)
42 , m_fwd_uri(normalize_uri(fwd_uri ? fwd_uri : local_uri))
59 || server->
connect(m_fwd_addr, m_fwd_port, udp::PORT_NONE,
true);
88 return SATCAT5_COAP_PROXY_SILENT || client->
open_separate(msg);
93 , m_msgid(satcat5::util::prng.next())
94 , m_token(satcat5::util::prng.next())
95 , m_extra_connection(this, udp)
103 if (m_aux_ep) tmp = m_aux_ep->
find_token(token_client(token));
104 if (!tmp) tmp =
find_token(token_client(token));
120 return (
m_token += 2) & TOKEN_MASK;
125 u32 rcvd_token = token_server(msg->
token());
171 if (client && client != obj)
CoAP request/response handling for a single client-server connection.
bool is_separate() const
< Awaiting separate response?
bool open_separate(const satcat5::coap::ReadHeader *msg)
If able, send the first half of a separated response.
satcat5::io::Writeable * open_response_auto()
Automatically call open_response() or continue_separate().
bool error_response(satcat5::coap::Code code, const char *why=0)
If able, return an error in response to an incoming request from a remote client.
satcat5::io::Writeable * open_request()
If able, send a request to the current remote server.
void set_proxy_token(u32 token)
Set identifier for matching incoming proxy responses.
u32 get_proxy_token() const
Query the most recent proxy token.
Variant of coap::Connection for UDP connections.
bool is_match_addr(const satcat5::udp::Addr &dstaddr, const satcat5::udp::Port &dstport) const
Is this object connected to the designated address?
bool connect(const satcat5::udp::Addr &dstaddr, const satcat5::udp::Port &dstport=satcat5::udp::PORT_COAP, const satcat5::udp::Port &srcport=satcat5::udp::PORT_NONE, bool allow_reuse=false)
Set remote endpoint for later calls to open_request().
satcat5::coap::Connection * get_idle_connection() const
Get the first idle connection, or null if all are busy.
Connection * find_token(u32 token) const
Scan connections for a matching proxy-ID.
Define a reverse-proxy CoAP resource.
bool request_any(Connection *obj, Reader *msg)
Event handler for all incoming requests.
CoAP server with a mix of local and reverse-proxy resources.
void coap_response(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
u16 m_msgid
Counter for outgoing message-IDs.
void proxy_response(Connection *client, Reader *msg)
Internal handler for proxy responses.
u32 next_token()
Unique transaction tokens match client and server.
Connection * find_client(u32 token) const
Given a token, find associated client connection.
void coap_reqwait(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
u32 m_token
Counter for client/server tokens.
ProxyServer(satcat5::udp::Dispatch *udp, satcat5::udp::Port port=satcat5::udp::PORT_COAP)
Constructor. By default, bind this server to port 5683.
virtual void local_response(Connection *obj, Reader *msg)
Event handler for non-proxy responses.
ConnectionUdp * find_server(u32 token) const
Given a token, find associated server connection.
void coap_error(Connection *obj) override
The Child class overrides these event-handlers:
void coap_separate(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
u16 next_msgid()
Outgoing messages are numbered sequentially.
Base-class for parsing CoAP message headers and options.
satcat5::util::optional< u64 > size1() const
Accessors for parsed options.
satcat5::util::optional< const char * > uri_path() const
Accessors for parsed options.
satcat5::util::optional< u64 > block2()
Accessors for parsed options.
satcat5::util::optional< u64 > block1() const
Accessors for parsed options.
satcat5::util::optional< u16 > format() const
Accessors for parsed options.
Define a single CoAP resource.
Manager for several Resource objects available on an Endpoint.
Message formatting for the Constrained Applications Protocol (CoAP).
bool write_uri(u16 id, const char *str)
Convert a URI to consecutive Uri-Path options.
bool write_option(u16 id, unsigned len, const void *data)
Write option(s) one at a time, in various formats.
satcat5::io::Writeable * write_data()
After the last option, start writing message data.
bool write_header(u8 type, Code code, u16 msg_id, u64 token=0, u8 tkl=0)
Always start by writing the header, with optional token.
Abstract API for reading byte-streams and packets.
bool copy_and_finalize(satcat5::io::Writeable *dst, satcat5::io::CopyMode mode=CopyMode::PACKET)
Copy data to a Writeable object, then finalize.
Abstract API for writing byte-streams and packets.
Dispatcher sorts incoming UDP messages by port index.
constexpr u16 OPTION_BLOCK2
Option: Block transfer (RFC7959)
constexpr Code CODE_GATE_TIMEOUT(5, 4)
< 5.03 Service Unavailable
constexpr u16 OPTION_SIZE1
Option: Size1.
constexpr Code CODE_SERVER_ERROR(5, 0)
< 4.15 Unsupported Content-Format
constexpr u16 OPTION_BLOCK1
Option: Block transfer (RFC7959)
constexpr u16 OPTION_URI_PATH
Option: Uri-Path.
constexpr u16 OPTION_FORMAT
Option: Content-Format.
CoAP reverse-proxy for specific resources.
Message parsing for the Constrained Applications Protocol (CoAP)
IPv4 address is a 32-bit unsigned integer.
UDP and TCP ports are both 16-bit unsigned integers.
T value() const
Fetch the inner value.
Miscellaneous mathematical utility functions.