diff --git a/examples/coap-server.c b/examples/coap-server.c index 5a615cac..f5c38675 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -2170,7 +2170,7 @@ usage( const char *program, const char *version) { fprintf(stderr, "\n" "Usage: %s [-d max] [-e] [-g group] [-G group_if] [-l loss] [-p port]\n" "\t\t[-v num] [-A address] [-L value] [-N]\n" - "\t\t[-P scheme://address[:port],name1[,name2..]]\n" + "\t\t[-P scheme://address[:port],[name1[,name2..]]]\n" "\t\t[[-h hint] [-i match_identity_file] [-k key]\n" "\t\t[-s match_psk_sni_file] [-u user]]\n" "\t\t[[-c certfile] [-j keyfile] [-m] [-n] [-C cafile]\n" @@ -2207,15 +2207,16 @@ usage( const char *program, const char *version) { "\t \t\tresponse (RFC 7641 requirement)\n" , program); fprintf( stderr, - "\t-P scheme://address[:port],name1[,name2[,name3..]]\tScheme, address,\n" - "\t \t\toptional port of how to connect to the next proxy server\n" - "\t \t\tand one or more names (comma separated) that this proxy\n" - "\t \t\tserver is known by. If the hostname of the incoming proxy\n" - "\t \t\trequest matches one of these names, then this server is\n" - "\t \t\tconsidered to be the final endpoint. If\n" - "\t \t\tscheme://address[:port] is not defined before the leading\n" - "\t \t\t, (comma) of the first name, then the ongoing connection\n" - "\t \t\twill be a direct connection.\n" + "\t-P scheme://address[:port],[name1[,name2[,name3..]]]\n" + "\t \t\tScheme, address, optional port of how to connect to the\n" + "\t \t\tnext proxy server and zero or more names (comma\n" + "\t \t\tseparated) that this proxy server is known by. The\n" + "\t \t\t, (comma) is required. If there is no name1 or if the\n" + "\t \t\thostname of the incoming proxy request matches one of\n" + "\t \t\tthese names, then this server is considered to be the\n" + "\t \t\tfinal endpoint. If scheme://address[:port] is not\n" + "\t \t\tdefined before the leading , (comma) of the first name,\n" + "\t \t\tthen the ongoing connection will be a direct connection.\n" "\t \t\tScheme is one of coap, coaps, coap+tcp and coaps+tcp\n" "PSK Options (if supported by underlying (D)TLS library)\n" "\t-h hint\t\tIdentity Hint to send. Default is CoAP. Zero length is\n" @@ -2398,7 +2399,7 @@ cmdline_proxy(char *arg) { size_t ofs; if (!host_start) { - coap_log(LOG_WARNING, "One or more proxy host names not defined\n"); + coap_log(LOG_WARNING, "Zero or more proxy host names not defined\n"); return 0; } *host_start = '\000'; diff --git a/man/coap-server.txt.in b/man/coap-server.txt.in index 10bdb871..9848cba4 100644 --- a/man/coap-server.txt.in +++ b/man/coap-server.txt.in @@ -21,7 +21,7 @@ SYNOPSIS -------- *coap-server* [*-d* max] [*-e*] [*-g* group] [*-G* group_if] [*-l* loss] [*-p* port] [*-v* num] [*-A* address] [*-L* value] [*-N*] - [*-P* scheme://addr[:port],name1[,name2..]] + [*-P* scheme://addr[:port],[name1[,name2..]]] [[*-h* hint] [*-i* match_identity_file] [*-k* key] [*-s* match_psk_sni_file] [*-u* user]] [[*-c* certfile] [*-j* keyfile] [*-n*] [*-C* cafile] @@ -90,10 +90,11 @@ OPTIONS - General fifth response will still be sent as a confirmable response (RFC 7641 requirement). -*-P* scheme://address[:port],name1[,name2[,name3..]] :: +*-P* scheme://address[:port],[name1[,name2[,name3..]]] :: Scheme, address, optional port of how to connect to the next proxy server - and one or more names (comma separated) that this proxy server is known by. - If the hostname of the incoming proxy request matches one of these names, + and zero or more names (comma separated) that this proxy server is known by. + The , (comma) is required. If there is no name1 or + if the hostname of the incoming proxy request matches one of these names, then this server is considered to be the final endpoint. If scheme://address[:port] is not defined before the leading , (comma) of the first name, then the ongoing connection will be a direct connection. diff --git a/src/net.c b/src/net.c index 8406d2e4..69a3aba1 100644 --- a/src/net.c +++ b/src/net.c @@ -2589,14 +2589,24 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu if (opt) { uri.host.length = coap_opt_length(opt); uri.host.s = coap_opt_value(opt); - } + } else + uri.host.length = 0; } + resource = context->proxy_uri_resource; - if (uri.host.length && resource->proxy_name_count && resource->proxy_name_list) { + if (uri.host.length && resource->proxy_name_count && + resource->proxy_name_list) { size_t i; - for (i = 0; i < resource->proxy_name_count; i++) { - if (coap_string_equal(&uri.host, resource->proxy_name_list[i])) { - break; + + if (resource->proxy_name_count == 1 && + resource->proxy_name_list[0]->length == 0) { + /* If proxy_name_list[0] is zero length, then this is the endpoint */ + i = 0; + } else { + for (i = 0; i < resource->proxy_name_count; i++) { + if (coap_string_equal(&uri.host, resource->proxy_name_list[i])) { + break; + } } } if (i != resource->proxy_name_count) { @@ -2653,11 +2663,11 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu * unknown resource handler is defined, a PUT or optionally other methods, * if configured, for the unknown handler. * - * if well-known URI generate a default response - * - * else if a PROXY URI/Scheme request and proxy URI handler defined, call the + * if a PROXY URI/Scheme request and proxy URI handler defined, call the * proxy URI handler * + * else if well-known URI generate a default response + * * else if unknown URI handler defined, call the unknown * URI handler (to allow for potential generation of resource * [RFC7272 5.8.3]) if the appropriate method is defined. @@ -2666,7 +2676,9 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu * * else return 4.04 */ - if (coap_string_equal(uri_path, &coap_default_uri_wellknown)) { + if (is_proxy_uri || is_proxy_scheme) { + resource = context->proxy_uri_resource; + } else if (coap_string_equal(uri_path, &coap_default_uri_wellknown)) { /* request for .well-known/core */ if (pdu->code == COAP_REQUEST_CODE_GET) { /* GET */ coap_log(LOG_INFO, "create default response for %s\n", @@ -2677,8 +2689,6 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(405), &opt_filter); } - } else if (is_proxy_uri || is_proxy_scheme) { - resource = context->proxy_uri_resource; } else if ((context->unknown_resource != NULL) && ((size_t)pdu->code - 1 < (sizeof(resource->handler) / sizeof(coap_method_handler_t))) && @@ -2740,6 +2750,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu if (response) { /* Need to delete unused response - it will get re-created further on */ coap_delete_pdu(response); + response = NULL; } } } diff --git a/src/uri.c b/src/uri.c index b7bef0f6..d66038c0 100644 --- a/src/uri.c +++ b/src/uri.c @@ -616,6 +616,21 @@ coap_string_t *coap_get_uri_path(const coap_pdu_t *request) { size_t length = 0; static const uint8_t hex[] = "0123456789ABCDEF"; + q = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter); + if (q) { + coap_uri_t uri; + + if (coap_split_proxy_uri(coap_opt_value(q), + coap_opt_length(q), &uri) < 0) { + return NULL; + } + uri_path = coap_new_string(uri.path.length); + if (uri_path) { + memcpy(uri_path->s, uri.path.s, uri.path.length); + } + return uri_path; + } + coap_option_filter_clear(&f); coap_option_filter_set(&f, COAP_OPTION_URI_PATH); coap_option_iterator_init(request, &opt_iter, &f);