SatCat5
coap_proxy.h
Go to the documentation of this file.
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
40 
41 #pragma once
42 
43 #include <satcat5/coap_connection.h>
44 #include <satcat5/coap_resource.h>
45 #include <satcat5/utils.h>
46 
47 namespace satcat5 {
48  namespace coap {
51  class ProxyResource final : public satcat5::coap::Resource {
52  public:
61  const char* local_uri,
62  const satcat5::ip::Addr& fwd_addr,
63  const satcat5::udp::Port& fwd_port,
64  const char* fwd_uri = nullptr);
65 
69  bool request_get(Connection* obj, Reader* msg) override
70  { return request_any(obj, msg); }
71  bool request_post(Connection* obj, Reader* msg) override
72  { return request_any(obj, msg); }
73  bool request_put(Connection* obj, Reader* msg) override
74  { return request_any(obj, msg); }
75  bool request_delete(Connection* obj, Reader* msg) override
76  { return request_any(obj, msg); }
78 
79  protected:
82  bool request_any(Connection* obj, Reader* msg);
83 
84  satcat5::coap::ProxyServer* const m_pool;
85  const satcat5::ip::Addr m_fwd_addr;
86  const satcat5::udp::Port m_fwd_port;
87  const char* const m_fwd_uri;
88  };
89 
98  public:
101  satcat5::udp::Port port = satcat5::udp::PORT_COAP);
102 
105  Connection* find_client(u32 token) const;
106 
109  ConnectionUdp* find_server(u32 token) const;
110 
112  u16 next_msgid();
113 
115  u32 next_token();
116 
117  protected:
122  virtual void local_response(Connection* obj, Reader* msg) {} // GCOVR_EXCL_LINE
123 
125  void proxy_response(Connection* client, Reader* msg);
126 
127  // Override all proxy-related coap::Endpoint callbacks.
128  // (The coap_request callback is handled by the parent class.)
129  void coap_response(Connection* obj, Reader* msg) override;
130  void coap_reqwait(Connection* obj, Reader* msg) override;
131  void coap_separate(Connection* obj, Reader* msg) override;
132  void coap_error(Connection* obj) override;
133 
134  // Internal state
135  u16 m_msgid;
136  u32 m_token;
137 
142  };
143  }
144 }
CoAP request/response handling for a single client-server connection.
Variant of coap::Connection for UDP connections.
satcat5::udp::Dispatch * udp() const
Pointer to the parent's network interface.
Define a reverse-proxy CoAP resource.
Definition: coap_proxy.h:51
bool request_get(Connection *obj, Reader *msg) override
Forward GET, POST, PUT, and DELETE requests.
Definition: coap_proxy.h:69
ProxyResource(satcat5::coap::ProxyServer *server, const char *local_uri, const satcat5::ip::Addr &fwd_addr, const satcat5::udp::Port &fwd_port, const char *fwd_uri=nullptr)
Constructor sets the URI path for the resource.
Definition: coap_proxy.cc:32
bool request_any(Connection *obj, Reader *msg)
Event handler for all incoming requests.
Definition: coap_proxy.cc:47
bool request_delete(Connection *obj, Reader *msg) override
Forward GET, POST, PUT, and DELETE requests.
Definition: coap_proxy.h:75
bool request_put(Connection *obj, Reader *msg) override
Forward GET, POST, PUT, and DELETE requests.
Definition: coap_proxy.h:73
bool request_post(Connection *obj, Reader *msg) override
Forward GET, POST, PUT, and DELETE requests.
Definition: coap_proxy.h:71
CoAP server with a mix of local and reverse-proxy resources.
Definition: coap_proxy.h:97
void coap_response(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
Definition: coap_proxy.cc:123
u16 m_msgid
Counter for outgoing message-IDs.
Definition: coap_proxy.h:135
void proxy_response(Connection *client, Reader *msg)
Internal handler for proxy responses.
Definition: coap_proxy.cc:138
u32 next_token()
Unique transaction tokens match client and server.
Definition: coap_proxy.cc:118
Connection * find_client(u32 token) const
Given a token, find associated client connection.
Definition: coap_proxy.cc:100
void coap_reqwait(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
Definition: coap_proxy.cc:156
u32 m_token
Counter for client/server tokens.
Definition: coap_proxy.h:136
ProxyServer(satcat5::udp::Dispatch *udp, satcat5::udp::Port port=satcat5::udp::PORT_COAP)
Constructor. By default, bind this server to port 5683.
Definition: coap_proxy.cc:91
virtual void local_response(Connection *obj, Reader *msg)
Event handler for non-proxy responses.
Definition: coap_proxy.h:122
ConnectionUdp * find_server(u32 token) const
Given a token, find associated server connection.
Definition: coap_proxy.cc:108
satcat5::coap::ConnectionUdp m_extra_connection
Proxy operation requires at least two ConnectionUdp objects.
Definition: coap_proxy.h:141
void coap_error(Connection *obj) override
The Child class overrides these event-handlers:
Definition: coap_proxy.cc:168
void coap_separate(Connection *obj, Reader *msg) override
The Child class overrides these event-handlers:
Definition: coap_proxy.cc:162
u16 next_msgid()
Outgoing messages are numbered sequentially.
Definition: coap_proxy.cc:113
Base-class for parsing CoAP message headers and options.
Definition: coap_reader.h:162
Define a single CoAP resource.
Definition: coap_resource.h:47
Manager for several Resource objects available on an Endpoint.
Dispatcher sorts incoming UDP messages by port index.
Definition: udp_dispatch.h:20
CoAP resource handler definition and resource server creation.
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15
UDP and TCP ports are both 16-bit unsigned integers.
Definition: ip_core.h:119
Miscellaneous mathematical utility functions.