21#if COAP_CLIENT_SUPPORT == 0
22#error For Proxy support, COAP_CLIENT_SUPPORT must be set
24#if COAP_SERVER_SUPPORT == 0
25#error For Proxy support, COAP_SERVER_SUPPORT must be set
29#define strcasecmp _stricmp
30#define strncasecmp _strnicmp
43 for (i = 0; i < context->proxy_list_count; i++) {
44 for (j = 0; j < context->proxy_list[i].req_count; j++) {
64 for (i = 0; i < context->proxy_list_count; i++) {
84coap_get_uri_proxy_scheme_info(
const coap_pdu_t *request,
95 strncasecmp(opt_val,
"coaps+tcp", 9) == 0) {
98 }
else if (opt_len == 8 &&
99 strncasecmp(opt_val,
"coap+tcp", 8) == 0) {
102 }
else if (opt_len == 5 &&
103 strncasecmp(opt_val,
"coaps", 5) == 0) {
106 }
else if (opt_len == 4 &&
107 strncasecmp(opt_val,
"coap", 4) == 0) {
110 }
else if (opt_len == 7 &&
111 strncasecmp(opt_val,
"coap+ws", 7) == 0) {
114 }
else if (opt_len == 8 &&
115 strncasecmp(opt_val,
"coaps+ws", 8) == 0) {
120 opt_len, opt_len, opt_val);
140 uri->
path.
s = (*uri_path)->s;
148 uri->
query.
s = (*uri_query)->s;
176 coap_log_warn(
"coap+tcp URI scheme not supported for proxy\n");
182 coap_log_warn(
"coaps+tcp URI scheme not supported for proxy\n");
188 coap_log_warn(
"coap+ws URI scheme not supported for proxy\n");
194 coap_log_warn(
"coaps+ws URI scheme not supported for proxy\n");
214 size_t proxy_list_count = session->
context->proxy_list_count;
227 memcpy(server_use, &server_list->
entry[server_list->
next_entry],
sizeof(*server_use));
229 switch (server_list->
type) {
244 if (!coap_get_uri_proxy_scheme_info(request, proxy_scheme, &server_use->
uri, &uri_path,
260 &server_use->
uri) < 0) {
268 if (!(proxy_scheme || proxy_uri)) {
290 for (i = 0; i < proxy_list_count; i++) {
292 proxy_list[i].uri.port == server_use->
uri.
port &&
293 proxy_list[i].uri.scheme == server_use->
uri.
scheme) {
296 return &proxy_list[i];
298 if (proxy_list[i].incoming == session) {
300 return &proxy_list[i];
309 if (new_proxy_list == NULL) {
313 session->
context->proxy_list = proxy_list = new_proxy_list;
314 memset(&proxy_list[i], 0,
sizeof(proxy_list[i]));
316 proxy_list[i].
uri = server_use->
uri;
320 session->
context->proxy_list_count++;
323 return &proxy_list[i];
332 size_t proxy_list_count = session->
context->proxy_list_count;
334 for (i = 0; i < proxy_list_count; i++) {
336 for (j = 0; j < proxy_list[i].
req_count; j++) {
337 if (proxy_list[i].req_list[j].incoming == session) {
341 if (proxy_list[i].req_count-j > 1) {
342 memmove(&proxy_list[i].req_list[j], &proxy_list[i].req_list[j+1],
343 (proxy_list[i].req_count-j-1) *
sizeof(proxy_list[i].req_list[0]));
349 if (proxy_list[i].incoming == session) {
356 if (proxy_list[i].ongoing == session) {
359 for (j = 0; j < proxy_list[i].
req_count; j++) {
365 response =
coap_pdu_init(proxy_list[i].req_list[j].pdu->type,
377 coap_log_debug(
"Cannot add token to incoming proxy response PDU\n");
381 coap_log_info(
"Failed to send PDU with 5.02 gateway issue\n");
389 ongoing = proxy_list[i].
ongoing;
391 if (proxy_list_count-i > 1) {
392 memmove(&proxy_list[i],
394 (proxy_list_count-i-1) *
sizeof(proxy_list[0]));
396 session->
context->proxy_list_count--;
415 static char client_sni[256];
417 proxy_entry = coap_proxy_get_session(session, request, response, server_list, server_use);
436 if (info_list == NULL) {
441 proto = info_list->
proto;
442 memcpy(&dst, &info_list->
addr,
sizeof(dst));
445 snprintf(client_sni,
sizeof(client_sni),
"%*.*s", (
int)server_use->
uri.
host.
length,
452#if COAP_OSCORE_SUPPORT
461#if COAP_OSCORE_SUPPORT
468#if COAP_OSCORE_SUPPORT
481 coap_log_warn(
"Proxy: (D)TLS not configured for secure session\n");
497 coap_log_warn(
"Proxy: (D)TLS not configured for secure session\n");
499#if COAP_OSCORE_SUPPORT
510 if (proxy_entry->
ongoing == NULL) {
571 proxy_entry = coap_proxy_get_ongoing_session(session, request, response,
572 server_list, &server_use);
581 if (new_req_list == NULL) {
584 proxy_entry->
req_list = new_req_list;
588 if (new_req_list[proxy_entry->
req_count].token_used == NULL) {
592 r_token.
length, r_token.
s, NULL);
593 if (new_req_list[proxy_entry->
req_count].pdu == NULL) {
603 switch (server_list->
type) {
636 switch (opt_iter.
number) {
677 assert(size == total);
687 memcpy(body_data->
s, data, size);
689 coap_proxy_release_body_data, body_data)) {
753 size_t proxy_list_count = session->
context->proxy_list_count;
757 for (i = 0; i < proxy_list_count; i++) {
758 proxy_entry = &proxy_list[i];
759 for (j = 0; j < proxy_entry->
req_count; j++) {
761 proxy_req = &proxy_entry->
req_list[j];
769 if (i == proxy_list_count) {
770 coap_log_warn(
"Unknown proxy ongoing session response received\n");
774 req_pdu = proxy_req->
pdu;
779 coap_log_debug(
"** process upstream incoming %d.%02d response:\n",
784 assert(size == total);
790 memcpy(body_data->
s, data, size);
807 coap_log_debug(
"cannot add token to ongoing proxy response PDU\n");
816 switch (opt_iter.
number) {
849 media_type, maxage, etag, size, data,
850 coap_proxy_release_body_data,
864 if (option == NULL && proxy_entry->
req_count) {
void coap_free_address_info(coap_addr_info_t *info)
Free off the one or more linked sets of coap_addr_info_t returned from coap_resolve_address_info().
coap_addr_info_t * coap_resolve_address_info(const coap_str_const_t *address, uint16_t port, uint16_t secure_port, uint16_t ws_port, uint16_t ws_secure_port, int ai_hints_flags, int scheme_hint_bits, coap_resolve_type_t type)
Resolve the specified address into a set of coap_address_t that can be used to bind() (local) or conn...
@ COAP_RESOLVE_TYPE_REMOTE
remote side of session
Library specific build wrapper for coap_internal.h.
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
coap_uri_scheme_t
The scheme specifiers.
@ COAP_URI_SCHEME_COAPS_WS
@ COAP_URI_SCHEME_COAPS_TCP
@ COAP_URI_SCHEME_COAP_TCP
@ COAP_URI_SCHEME_COAP_WS
int coap_proxy_check_timeouts(coap_context_t *context, coap_tick_t now, coap_tick_t *tim_rem)
Idle timeout inactive proxy sessions as well as return in tim_rem the time to remaining to timeout th...
coap_response_t coap_proxy_forward_response_lkd(coap_session_t *session, const coap_pdu_t *received, coap_cache_key_t **cache_key)
Forward the returning response back to the appropriate client.
void coap_proxy_cleanup(coap_context_t *context)
Close down proxy tracking, releasing any memory used.
void coap_proxy_remove_association(coap_session_t *session, int send_failure)
int coap_proxy_forward_request_lkd(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, coap_resource_t *resource, coap_cache_key_t *cache_key, coap_proxy_server_list_t *server_list)
Forward incoming request upstream to the next proxy/server.
coap_mid_t coap_send_lkd(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
int coap_add_data_large_response_lkd(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
int coap_add_data_large_request_lkd(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_OK
Response is fine.
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
#define coap_lock_unlock(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
coap_optlist_t * coap_new_optlist(uint16_t number, size_t length, const uint8_t *data)
Create a new optlist entry.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
void coap_delete_optlist(coap_optlist_t *queue)
Removes all entries from the optlist_chain, freeing off their memory usage.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
int coap_add_optlist_pdu(coap_pdu_t *pdu, coap_optlist_t **options)
The current optlist of optlist_chain is first sorted (as per RFC7272 ordering requirements) and then ...
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
int coap_insert_optlist(coap_optlist_t **head, coap_optlist_t *node)
Adds optlist to the given optlist_chain.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
coap_session_t * coap_new_client_session_oscore_psk_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *psk_data, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server with PSK credentials as well as protecting the ...
coap_session_t * coap_new_client_session_oscore_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server, protecting the data using OSCORE.
coap_session_t * coap_new_client_session_oscore_pki_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *pki_data, coap_oscore_conf_t *oscore_conf)
Creates a new client session to the designated server with PKI credentials as well as protecting the ...
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
#define COAP_OPTION_URI_HOST
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
#define COAP_OPTION_BLOCK2
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
#define COAP_OPTION_Q_BLOCK1
#define COAP_OPTION_PROXY_SCHEME
#define COAP_DEFAULT_PORT
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
coap_proto_t
CoAP protocol types.
coap_pdu_code_t
Set of codes available for a PDU.
#define COAP_MEDIATYPE_TEXT_PLAIN
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
#define COAP_OPTION_Q_BLOCK2
#define COAPS_DEFAULT_PORT
#define COAP_OPTION_URI_PORT
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
#define COAP_INVALID_MID
Indicates an invalid message id.
#define COAP_OPTION_MAXAGE
#define COAP_OPTION_PROXY_URI
#define COAP_OPTION_OBSERVE
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
COAP_API coap_response_t coap_proxy_forward_response(coap_session_t *session, const coap_pdu_t *received, coap_cache_key_t **cache_key)
Forward the returning response back to the appropriate client.
int coap_verify_proxy_scheme_supported(coap_uri_scheme_t scheme)
Verify that the CoAP Scheme is supported for an ongoing proxy connection.
COAP_API int coap_proxy_forward_request(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, coap_resource_t *resource, coap_cache_key_t *cache_key, coap_proxy_server_list_t *server_list)
Forward incoming request upstream to the next proxy/server.
@ COAP_PROXY_DIRECT_STRIP
Act as a direct proxy, strip out proxy options.
@ COAP_PROXY_REVERSE_STRIP
Act as a reverse proxy, strip out proxy options.
@ COAP_PROXY_REVERSE
Act as a reverse proxy.
@ COAP_PROXY_DIRECT
Act as a direct proxy.
@ COAP_PROXY_FORWARD_STRIP
Act as a forward proxy, strip out proxy options.
@ COAP_PROXY_FORWARD
Act as a forward proxy.
coap_session_t * coap_new_client_session_psk2_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
coap_session_t * coap_new_client_session_pki_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
coap_session_t * coap_new_client_session_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
int coap_tcp_is_supported(void)
Check whether TCP is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_ws_is_supported(void)
Check whether WebSockets is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_proxy_is_supported(void)
Check whether Proxy code is available.
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
int coap_uri_into_optlist(const coap_uri_t *uri, const coap_address_t *dst, coap_optlist_t **optlist_chain, int create_port_host_opt)
Takes a coap_uri_t and then adds CoAP options into the optlist_chain.
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Resolved addresses information.
coap_proto_t proto
CoAP protocol to use.
coap_address_t addr
The address to connect / bind to.
coap_address_t remote
remote address and port
Multi-purpose address abstraction.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
The CoAP stack's global state is stored in a coap_context_t object.
char * client_sni
If not NULL, SNI to use in client TLS setup.
char * client_sni
If not NULL, SNI to use in client TLS setup.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
Representation of chained list of CoAP options to install.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_pdu_type_t type
message type
coap_tick_t idle_timeout_ticks
Idle timeout (0 == no timeout)
coap_session_t * incoming
Incoming session (used if client tracking(.
coap_proxy_req_t * req_list
Incoming list of request info.
coap_tick_t last_used
Last time entry was used.
coap_uri_t uri
URI info for connection.
coap_session_t * ongoing
Ongoing session.
size_t req_count
Count of incoming request info.
coap_bin_const_t * token_used
coap_cache_key_t * cache_key
coap_session_t * incoming
coap_resource_t * resource
int track_client_session
If 1, track individual connections to upstream server, else 0.
coap_proxy_server_t * entry
Set of servers to connect to.
coap_proxy_t type
The proxy type.
unsigned int idle_timeout_secs
Proxy session idle timeout (0 is no timeout)
size_t next_entry
Next server to us (% entry_count)
size_t entry_count
The number of servers.
coap_dtls_pki_t * dtls_pki
PKI configuration to use if not NULL.
coap_oscore_conf_t * oscore_conf
OSCORE configuration if not NULL.
coap_uri_t uri
host and port define the server, scheme method
coap_dtls_cpsk_t * dtls_cpsk
PSK configuration to use if not NULL.
Abstraction of resource that can be attached to coap_context_t.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
remote/local address info
coap_context_t * context
session's context
const uint8_t * s
read-only string data
size_t length
length of string
CoAP string data definition.
Representation of parsed URI.
enum coap_uri_scheme_t scheme
The parsed scheme specifier.
coap_str_const_t path
The complete path if present or {0, NULL}.
uint16_t port
The port in host byte order.
coap_str_const_t query
The complete query if present or {0, NULL}.
coap_str_const_t host
The host part of the URI.