Skip to content

Commit

Permalink
coap-server.c: Replace SERVER_CAN_PROXY with COAP_PROXY_SUPPORT
Browse files Browse the repository at this point in the history
Make use of the new coap_defines.h file.
  • Loading branch information
mrdeep1 committed Aug 7, 2024
1 parent 92198b0 commit 3fb4783
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 44 deletions.
3 changes: 3 additions & 0 deletions coap_config.h.contiki
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
/* Define to 1 if libcoap supports server mode code. */
#define COAP_SERVER_SUPPORT 1

/* Define to 1 if libcoap supports proxy code. */
#define COAP_PROXY_SUPPORT 1

/* Define to 1 if the system has small stack size. */
#define COAP_CONSTRAINED_STACK 1

Expand Down
7 changes: 7 additions & 0 deletions coap_config.h.riot
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@
#endif /* COAP_SERVER_SUPPORT */
#endif /* CONFIG_LIBCOAP_SERVER_SUPPORT */

#ifdef CONFIG_LIBCOAP_PROXY_SUPPORT
#ifndef COAP_PROXY_SUPPORT
/* Define to 1 if the library has proxy code support. */
#define COAP_PROXY_SUPPORT 1
#endif /* COAP_PROXY_SUPPORT */
#endif /* CONFIG_LIBCOAP_PROXY_SUPPORT */

#ifdef CONFIG_LIBCOAP_ASYNC_SUPPORT
#ifndef COAP_ASYNC_SUPPORT
/* Define to 1 to build with support for async separate responses. */
Expand Down
5 changes: 5 additions & 0 deletions coap_config.h.windows
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
#define COAP_SERVER_SUPPORT 1
#endif

#ifndef COAP_PROXY_SUPPORT
/* Define if libcoap supports proxy code. */
#define COAP_PROXY_SUPPORT 1
#endif

#ifndef COAP_WITH_OBSERVE_PERSIST
/* Define to build support for persisting observes. */
#define COAP_WITH_OBSERVE_PERSIST 0
Expand Down
5 changes: 0 additions & 5 deletions examples/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ if BUILD_ADD_DEFAULT_NAMES
noinst_PROGRAMS += coap-server coap-rd
endif # BUILD_ADD_DEFAULT_NAMES

if ! HAVE_CLIENT_SUPPORT
coap_server_CPPFLAGS=-DSERVER_CAN_PROXY=0
coap_server@LIBCOAP_DTLS_LIB_EXTENSION_NAME@_CPPFLAGS=-DSERVER_CAN_PROXY=0
endif # ! HAVE_CLIENT_SUPPORT

endif # HAVE_SERVER_SUPPORT

coap_client_SOURCES = coap-client.c
Expand Down
53 changes: 23 additions & 30 deletions examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,11 @@ strndup(const char *s1, size_t n) {
#include <syslog.h>
#endif

/*
* SERVER_CAN_PROXY=0 can be set by build system if
* "./configure --disable-client-mode" is used.
*/
#ifndef SERVER_CAN_PROXY
#define SERVER_CAN_PROXY 1
#endif

/* Need to refresh time once per sec */
#define COAP_RESOURCE_CHECK_TIME 1

#include <coap3/coap.h>
#include <coap3/coap_defines.h>

#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
Expand Down Expand Up @@ -624,7 +617,7 @@ hnd_put_example_data(coap_resource_t *resource,
}
}

#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT

#define MAX_USER 128 /* Maximum length of a user name (i.e., PSK
* identity) in bytes. */
Expand Down Expand Up @@ -682,7 +675,7 @@ hnd_reverse_proxy_uri(coap_resource_t *resource,
/* Leave response code as is */
}

#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */

typedef struct dynamic_resource_t {
coap_string_t *uri_path;
Expand Down Expand Up @@ -1052,7 +1045,7 @@ hnd_put_post_unknown(coap_resource_t *resource COAP_UNUSED,
hnd_put_post(r, session, request, query, response);
}

#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
static int
proxy_event_handler(coap_session_t *session COAP_UNUSED,
coap_event_t event) {
Expand Down Expand Up @@ -1120,13 +1113,13 @@ proxy_nack_handler(coap_session_t *session COAP_UNUSED,
return;
}

#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */

static void
init_resources(coap_context_t *ctx) {
coap_resource_t *r;

#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
if (reverse_proxy.entry_count) {
/* Create a reverse proxy resource to handle PUTs */
r = coap_resource_unknown_init2(hnd_reverse_proxy_uri,
Expand All @@ -1143,7 +1136,7 @@ init_resources(coap_context_t *ctx) {
coap_register_response_handler(ctx, reverse_response_handler);
coap_register_nack_handler(ctx, proxy_nack_handler);
} else {
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */
r = coap_resource_init(NULL, COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT);
coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_index);

Expand Down Expand Up @@ -1198,7 +1191,7 @@ init_resources(coap_context_t *ctx) {
coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"Example Data\""), 0);
coap_add_resource(ctx, r);

#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
}
if (proxy_host_name_count) {
r = coap_resource_proxy_uri_init2(hnd_forward_proxy_uri, proxy_host_name_count,
Expand All @@ -1208,7 +1201,7 @@ init_resources(coap_context_t *ctx) {
coap_register_response_handler(ctx, reverse_response_handler);
coap_register_nack_handler(ctx, proxy_nack_handler);
}
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */
}

static int
Expand Down Expand Up @@ -1522,7 +1515,7 @@ fill_keystore(coap_context_t *ctx) {
}
}

#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
static void
proxy_dtls_setup(coap_context_t *ctx, coap_proxy_server_list_t *proxy_info) {
size_t i;
Expand Down Expand Up @@ -1553,7 +1546,7 @@ proxy_dtls_setup(coap_context_t *ctx, coap_proxy_server_list_t *proxy_info) {
*/
}
}
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */


static void
Expand Down Expand Up @@ -1813,7 +1806,7 @@ get_context(const char *node, const char *port) {
return ctx;
}

#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
static int
cmdline_proxy(char *arg) {
char *host_start = strchr(arg, ',');
Expand Down Expand Up @@ -1912,7 +1905,7 @@ cmdline_read_user(char *arg, unsigned char **buf, size_t maxlen) {
/* 0 length Identity is valid */
return len;
}
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */

static FILE *oscore_seq_num_fp = NULL;
static const char *oscore_conf_file = NULL;
Expand Down Expand Up @@ -2386,13 +2379,13 @@ main(int argc, char **argv) {
fprintf(stderr, "Reverse Proxy support not available as libcoap proxy code not enabled\n");
goto failed;
}
#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
if (!cmdline_reverse_proxy(optarg)) {
fprintf(stderr, "Reverse Proxy error specifying upstream address\n");
goto failed;
}
block_mode |= COAP_BLOCK_SINGLE_BODY;
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */
break;
case 'g' :
group = optarg;
Expand Down Expand Up @@ -2460,13 +2453,13 @@ main(int argc, char **argv) {
fprintf(stderr, "Proxy support not available as libcoap proxy code not enabled\n");
goto failed;
}
#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
if (!cmdline_proxy(optarg)) {
fprintf(stderr, "error specifying proxy address or host names\n");
goto failed;
}
block_mode |= COAP_BLOCK_SINGLE_BODY;
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */
break;
case 'q':
tls_engine_conf = optarg;
Expand Down Expand Up @@ -2501,9 +2494,9 @@ main(int argc, char **argv) {
fprintf(stderr, "Proxy support not available as libcoap proxy code not enabled\n");
goto failed;
}
#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
user_length = cmdline_read_user(optarg, &user, MAX_USER);
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */
break;
case 'U':
if (!cmdline_unix(optarg)) {
Expand Down Expand Up @@ -2581,14 +2574,14 @@ main(int argc, char **argv) {
if (get_oscore_conf(ctx) == NULL)
goto failed;
}
#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
if (reverse_proxy.entry_count) {
proxy_dtls_setup(ctx, &reverse_proxy);
}
if (forward_proxy.entry_count) {
proxy_dtls_setup(ctx, &forward_proxy);
}
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */
if (extended_token_size > COAP_TOKEN_DEFAULT_MAX)
coap_context_set_max_token_size(ctx, extended_token_size);

Expand Down Expand Up @@ -2742,14 +2735,14 @@ main(int argc, char **argv) {
}
free(dynamic_entry);
release_resource_data(NULL, example_data_value);
#if SERVER_CAN_PROXY
#if COAP_PROXY_SUPPORT
free(reverse_proxy.entry);
free(forward_proxy.entry);
#if defined(_WIN32) && !defined(__MINGW32__)
#pragma warning( disable : 4090 )
#endif
coap_free(proxy_host_name_list);
#endif /* SERVER_CAN_PROXY */
#endif /* COAP_PROXY_SUPPORT */
if (oscore_seq_num_fp)
fclose(oscore_seq_num_fp);

Expand Down
12 changes: 12 additions & 0 deletions examples/riot/pkg_libcoap/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ config LIBCOAP_MAX_LG_SRCVS

endif # LIBCOAP_SERVER_SUPPORT

config LIBCOAP_PROXY_SUPPORT
bool "Enable Proxy functionality within CoAP"
depends on LIBCOAP_CLIENT_SUPPORT && LIBCOAP_SERVER_SUPPORT
default n
help
Enable Proxy functionality (ability to receive requests, pass
them to an upstream server and send back responses to client)
for CoAP.

If this option is disabled, redundant CoAP proxy only code is
removed.

config LIBCOAP_MAX_LG_XMITS
int "Max number of large transmits supported"
default 2 if LIBCOAP_SERVER_SUPPORT
Expand Down
27 changes: 18 additions & 9 deletions win32/libcoap.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ TYPE %(FullPath) &gt;&gt; "$(IntDir)$(TargetName).def"</Command>
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
</PreBuildEvent>
<PreBuildEvent>
<Message>Copying config files for windows</Message>
Expand Down Expand Up @@ -431,7 +432,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
</PreBuildEvent>
<PreBuildEvent>
<Message>Copying config files for windows</Message>
Expand Down Expand Up @@ -471,7 +473,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
<Message>Copying config files for windows</Message>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand All @@ -496,7 +499,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
<Message>Copying config files for windows</Message>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand All @@ -519,7 +523,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
</PreBuildEvent>
<PreBuildEvent>
<Message>Copying config files for windows</Message>
Expand Down Expand Up @@ -564,7 +569,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
</PreBuildEvent>
<PreBuildEvent>
<Message>Copying config files for windows</Message>
Expand Down Expand Up @@ -606,7 +612,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
<Message>Copying config files for windows</Message>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand Down Expand Up @@ -634,7 +641,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
<Message>Copying config files for windows</Message>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand All @@ -658,7 +666,8 @@ copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</C
</Link>
<PreBuildEvent>
<Command>copy /Y ..\coap_config.h.windows ..\coap_config.h
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h</Command>
copy /Y ..\$(LibCoAPIncludeDir)\coap.h.windows ..\$(LibCoAPIncludeDir)\coap.h
copy /Y ..\coap_config.h.windows ..\$(LibCoAPIncludeDir)\coap_defines.h</Command>
<Message>Copying config files for windows</Message>
</PreBuildEvent>
</ItemDefinitionGroup>
Expand Down

0 comments on commit 3fb4783

Please sign in to comment.