Skip to content

Commit

Permalink
Proxy: Update documentation, streamline the code
Browse files Browse the repository at this point in the history
Tidy up naming to reduce confusion between supported proxy types.

Provides simpler proxy examples.

Fixes issue with COAP_PROXY_REVERSE_STRIP.

Support upstream server using AF_UNIX.
  • Loading branch information
mrdeep1 committed Feb 20, 2025
1 parent 4289cb0 commit c4d933a
Show file tree
Hide file tree
Showing 10 changed files with 416 additions and 232 deletions.
26 changes: 15 additions & 11 deletions examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ static ssize_t user_length = -1;

static size_t proxy_host_name_count = 0;
static const char **proxy_host_name_list = NULL;
static coap_proxy_server_list_t forward_proxy = { NULL, 0, 0, COAP_PROXY_FORWARD, 0, 300};
static coap_proxy_server_list_t forward_proxy = { NULL, 0, 0, COAP_PROXY_FORWARD_STATIC, 0, 300};
static coap_proxy_server_list_t reverse_proxy = { NULL, 0, 0, COAP_PROXY_REVERSE_STRIP, 0, 10};

static coap_dtls_cpsk_t *
Expand All @@ -646,12 +646,12 @@ setup_cpsk(char *client_sni) {

static void
hnd_forward_proxy_uri(coap_resource_t *resource,
coap_session_t *session,
coap_session_t *req_session,
const coap_pdu_t *request,
const coap_string_t *query COAP_UNUSED,
coap_pdu_t *response) {

if (!coap_proxy_forward_request(session, request, response, resource,
if (!coap_proxy_forward_request(req_session, request, response, resource,
NULL, &forward_proxy)) {
coap_log_debug("hnd_forward_proxy_uri: Failed to forward PDU\n");
/* Non ACK response code set on error detection */
Expand All @@ -662,12 +662,12 @@ hnd_forward_proxy_uri(coap_resource_t *resource,

static void
hnd_reverse_proxy_uri(coap_resource_t *resource,
coap_session_t *session,
coap_session_t *rsp_session,
const coap_pdu_t *request,
const coap_string_t *query COAP_UNUSED,
coap_pdu_t *response) {

if (!coap_proxy_forward_request(session, request, response, resource,
if (!coap_proxy_forward_request(rsp_session, request, response, resource,
NULL, &reverse_proxy)) {
coap_log_debug("hnd_reverse_proxy_uri: Failed to forward PDU\n");
/* Non ACK response code set on error detection */
Expand Down Expand Up @@ -1085,11 +1085,11 @@ proxy_event_handler(coap_session_t *session COAP_UNUSED,
}

static coap_response_t
reverse_response_handler(coap_session_t *session,
reverse_response_handler(coap_session_t *rsp_session,
const coap_pdu_t *sent COAP_UNUSED,
const coap_pdu_t *received,
const coap_mid_t id COAP_UNUSED) {
return coap_proxy_forward_response(session, received, NULL);
return coap_proxy_forward_response(rsp_session, received, NULL);
}

static void
Expand Down Expand Up @@ -1517,7 +1517,9 @@ proxy_dtls_setup(coap_context_t *ctx, coap_proxy_server_list_t *proxy_info) {
for (i = 0; i < proxy_info->entry_count; i++) {
coap_proxy_server_t *proxy_server = &proxy_info->entry[i];

if (proxy_info->type == COAP_PROXY_DIRECT || proxy_info->type == COAP_PROXY_DIRECT_STRIP) {
if (proxy_info->type == COAP_PROXY_FORWARD_DYNAMIC ||
proxy_info->type == COAP_PROXY_FORWARD_DYNAMIC_STRIP) {
/* This will get filled in by the libcoap proxy logic */
memset(client_sni, 0, sizeof(client_sni));
} else {
snprintf(client_sni, sizeof(client_sni), "%*.*s", (int)proxy_server->uri.host.length,
Expand Down Expand Up @@ -1581,7 +1583,7 @@ usage(const char *program, const char *version) {
"\t \t\tuntil one of the dynamic resources has been deleted\n"
"\t-e \t\tEcho back the data sent with a PUT\n"
"\t-f scheme://address[:port]\n"
"\t \t\tAct as a reverse proxy where scheme, address, optional\n"
"\t \t\tAct as a reverse proxy where scheme, address and optional\n"
"\t \t\tport define how to connect to the internal server.\n"
"\t \t\tScheme is one of coap, coaps, coap+tcp, coaps+tcp,\n"
"\t \t\tcoap+ws, and coaps+ws. http(s) is not currently supported.\n"
Expand Down Expand Up @@ -1826,10 +1828,12 @@ cmdline_proxy(char *arg) {
coap_log_err("Unsupported CoAP Proxy protocol\n");
return 0;
}
forward_proxy.type = COAP_PROXY_FORWARD;
forward_proxy.type = COAP_PROXY_FORWARD_STATIC;
forward_proxy.idle_timeout_secs = 300;
} else {
memset(&uri, 0, sizeof(uri));
forward_proxy.type = COAP_PROXY_DIRECT_STRIP;
forward_proxy.type = COAP_PROXY_FORWARD_DYNAMIC_STRIP;
forward_proxy.idle_timeout_secs = 10;
}

new_entry = realloc(forward_proxy.entry,
Expand Down
42 changes: 27 additions & 15 deletions include/coap3/coap_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,23 @@
*/

typedef enum {
COAP_PROXY_REVERSE, /**< Act as a reverse proxy */
COAP_PROXY_REVERSE_STRIP, /**< Act as a reverse proxy, strip out proxy options */
COAP_PROXY_FORWARD, /**< Act as a forward proxy */
COAP_PROXY_FORWARD_STRIP, /**< Act as a forward proxy, strip out proxy options */
COAP_PROXY_DIRECT, /**< Act as a direct proxy */
COAP_PROXY_DIRECT_STRIP, /**< Act as a direct proxy, strip out proxy options */
COAP_PROXY_REVERSE, /**< Act as a reverse proxy */
COAP_PROXY_REVERSE_STRIP, /**< Act as a reverse proxy,
strip out proxy options */
COAP_PROXY_FORWARD_STATIC, /**< Act as a forward-static proxy */
COAP_PROXY_FORWARD_STATIC_STRIP, /**< Act as a forward-static proxy,
strip out proxy options */
COAP_PROXY_FORWARD_DYNAMIC, /**< Act as a forward-dynamic proxy
using the request's Proxy-Uri or
Proxy-Scheme options to determine
server */
COAP_PROXY_FORWARD_DYNAMIC_STRIP, /**< Act as a forward-dynamic proxy,
strip out proxy options */
/* For backward compatability */
COAP_PROXY_FORWARD = COAP_PROXY_FORWARD_STATIC,
COAP_PROXY_FORWARD_STRIP = COAP_PROXY_FORWARD_STATIC_STRIP,
COAP_PROXY_DIRECT = COAP_PROXY_FORWARD_DYNAMIC,
COAP_PROXY_DIRECT_STRIP = COAP_PROXY_FORWARD_DYNAMIC_STRIP,
} coap_proxy_t;

typedef struct coap_proxy_server_t {
Expand All @@ -42,13 +53,14 @@ typedef struct coap_proxy_server_t {

typedef struct coap_proxy_server_list_t {
coap_proxy_server_t *entry; /**< Set of servers to connect to */
size_t entry_count; /**< The number of servers */
size_t entry_count; /**< The number of servers in entry list */
size_t next_entry; /**< Next server to use (% entry_count) */
coap_proxy_t type; /**< The proxy type */
int track_client_session; /**< If 1, track individual connections to upstream
server, else 0 for all clients to share the same
ongoing session */
unsigned int idle_timeout_secs; /**< Proxy session idle timeout (0 is no timeout) */
unsigned int idle_timeout_secs; /**< Proxy upstream session idle timeout
(0 is no timeout) */
} coap_proxy_server_list_t;

/**
Expand All @@ -64,16 +76,16 @@ int coap_verify_proxy_scheme_supported(coap_uri_scheme_t scheme);
* Forward incoming request upstream to the next proxy/server.
*
* Possible scenarios:
* Acting as a reverse proxy - connect to internal server
* Acting as a reverse proxy - connect to defined internal server
* (possibly round robin load balancing over multiple servers).
* Acting as a forward proxy - connect to host defined in Proxy-Uri
* Acting as a forward-dynamic proxy - connect to host defined in Proxy-Uri
* or Proxy-Scheme with Uri-Host (and maybe Uri-Port).
* Acting as a relay proxy - connect to defined upstream server
* Acting as a forward-static proxy - connect to defined upstream server
* (possibly round robin load balancing over multiple servers).
*
* A request that should go direct to this server is not supported here.
*
* @param session The client session.
* @param req_session The client session.
* @param request The client's request PDU.
* @param response The response PDU that will get sent back to the client.
* @param resource The resource associated with this request.
Expand All @@ -83,7 +95,7 @@ int coap_verify_proxy_scheme_supported(coap_uri_scheme_t scheme);
* @return @c 1 if success, or @c 0 if failure (@p response code set to
* appropriate value).
*/
int COAP_API coap_proxy_forward_request(coap_session_t *session,
int COAP_API coap_proxy_forward_request(coap_session_t *req_session,
const coap_pdu_t *request,
coap_pdu_t *response,
coap_resource_t *resource,
Expand All @@ -93,7 +105,7 @@ int COAP_API coap_proxy_forward_request(coap_session_t *session,
/**
* Forward the returning response back to the appropriate client.
*
* @param session The session handling the response.
* @param rsp_session The upstream session receiving the response.
* @param received The received PDU.
* @param cache_key Updated with the cache key pointer provided to
* coap_proxy_forward_request(). The caller should
Expand All @@ -102,7 +114,7 @@ int COAP_API coap_proxy_forward_request(coap_session_t *session,
*
* @return One of COAP_RESPONSE_FAIL or COAP_RESPONSE_OK.
*/
coap_response_t COAP_API coap_proxy_forward_response(coap_session_t *session,
coap_response_t COAP_API coap_proxy_forward_response(coap_session_t *rsp_session,
const coap_pdu_t *received,
coap_cache_key_t **cache_key);

Expand Down
2 changes: 1 addition & 1 deletion include/coap3/coap_proxy_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void coap_proxy_remove_association(coap_session_t *session, int send_failure);
* Forward incoming request upstream to the next proxy/server.
*
* Possible scenarios:
* Acting as a reverse proxy - connect to internal server
* Acting as a reverse proxy - connect to defined internal server
* (possibly round robin load balancing over multiple servers).
* Acting as a forward proxy - connect to host defined in Proxy-Uri
* or Proxy-Scheme with Uri-Host (and maybe Uri-Port).
Expand Down
1 change: 1 addition & 0 deletions include/coap3/coap_resource_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct coap_resource_t {
unsigned int cacheable:1; /**< can be cached */
unsigned int is_unknown:1; /**< resource created for unknown handler */
unsigned int is_proxy_uri:1; /**< resource created for proxy URI handler */
unsigned int is_reverse_proxy:1; /**< resource created for reverse proxy URI handler */

/**
* Used to store handlers for the seven coap methods @c GET, @c POST, @c PUT,
Expand Down
2 changes: 1 addition & 1 deletion man/coap-server.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ OPTIONS - General
Echo back the data sent with a PUT.

*-f* scheme://address[:port]::
Act as a reverse proxy where scheme, address, optional
Act as a reverse proxy where scheme, address and optional
port define how to connect to the internal server.
Scheme is one of coap, coaps, coap+tcp, coaps+tcp,
coap+ws, and coaps+ws. http(s) is not currently supported.
Expand Down
Loading

0 comments on commit c4d933a

Please sign in to comment.