diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b792c8e..dfb3fac8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,14 @@ option( USE_VENDORED_TINYDTLS "compile with the tinydtls project in the submodule if on, otherwise try to find the compiled lib with find_package" ON) +option( + ENABLE_CLIENT_MODE + "compile with support for client mode code" + ON) +option( + ENABLE_SERVER_MODE + "compile with support for server mode code" + ON) option( WITH_EPOLL "compile with epoll support" @@ -164,6 +172,20 @@ else() HAVE_STRUCT_CMSGHDR) endif() +if(${ENABLE_CLIENT_MODE}) + set(COAP_CLIENT_SUPPORT "1") + message(STATUS "compiling with client support") +else() + message(STATUS "compiling without client support") +endif() + +if(${ENABLE_SERVER_MODE}) + set(COAP_SERVER_SUPPORT "1") + message(STATUS "compiling with server support") +else() + message(STATUS "compiling without server support") +endif() + if(${WITH_EPOLL} AND ${HAVE_EPOLL_H} AND ${HAVE_TIMERFD_H}) @@ -359,6 +381,8 @@ endif() message(STATUS "ENABLE_DTLS:.....................${ENABLE_DTLS}") message(STATUS "ENABLE_TCP:......................${ENABLE_TCP}") +message(STATUS "ENABLE_CLIENT_MODE:..............${ENABLE_CLIENT_MODE}") +message(STATUS "ENABLE_SERVER_MODE:..............${ENABLE_SERVER_MODE}") message(STATUS "ENABLE_DOCS:.....................${ENABLE_DOCS}") message(STATUS "ENABLE_EXAMPLES:.................${ENABLE_EXAMPLES}") message(STATUS "DTLS_BACKEND:....................${DTLS_BACKEND}") @@ -370,7 +394,7 @@ message(STATUS "HAVE_LIBTINYDTLS:................${HAVE_LIBTINYDTLS}") message(STATUS "HAVE_LIBGNUTLS:..................${HAVE_LIBGNUTLS}") message(STATUS "HAVE_OPENSSL:....................${HAVE_OPENSSL}") message(STATUS "HAVE_MBEDTLS:....................${HAVE_MBEDTLS}") -message(STATUS "COAP_EPOLL_SUPPORT:..............${COAP_EPOLL_SUPPORT}") +message(STATUS "WITH_EPOLL:......................${WITH_EPOLL}") message(STATUS "CMAKE_C_COMPILER:................${CMAKE_C_COMPILER}") message(STATUS "BUILD_SHARED_LIBS:...............${BUILD_SHARED_LIBS}") message(STATUS "CMAKE_BUILD_TYPE:................${CMAKE_BUILD_TYPE}") diff --git a/cmake_coap_config.h.in b/cmake_coap_config.h.in index 773d429c..b7c2ed0b 100644 --- a/cmake_coap_config.h.in +++ b/cmake_coap_config.h.in @@ -21,6 +21,12 @@ /* Define to 1 if you have header file. */ #cmakedefine HAVE_WINSOCK2_H "@HAVE_WINSOCK2_H@" +/* Define if the library has client support */ +#cmakedefine COAP_CLIENT_SUPPORT @COAP_CLIENT_SUPPORT@ + +/* Define if the library has server support */ +#cmakedefine COAP_SERVER_SUPPORT @COAP_SERVER_SUPPORT@ + /* Define if the system has epoll support */ #cmakedefine COAP_EPOLL_SUPPORT "@COAP_EPOLL_SUPPORT@" diff --git a/configure.ac b/configure.ac index b57cbb94..11fdc4be 100644 --- a/configure.ac +++ b/configure.ac @@ -766,6 +766,31 @@ if test "x$enable_small_stack" = "xyes"; then AC_DEFINE(COAP_CONSTRAINED_STACK, 1, [Define if the system has small stack size]) fi +AC_ARG_ENABLE([server-mode], + [AS_HELP_STRING([--enable-server-mode], + [Enable CoAP server mode supporting code [default=yes]])], + [enable_server_mode="$enableval"], + [enable_server_mode="yes"]) + +if test "x$enable_server_mode" = "xyes"; then + AC_DEFINE(COAP_SERVER_SUPPORT, 1, [Define if libcoap supports server mode code]) +fi +AM_CONDITIONAL(HAVE_SERVER_SUPPORT, [test "x$enable_server_mode" = "xyes"]) + +AC_ARG_ENABLE([client-mode], + [AS_HELP_STRING([--enable-client-mode], + [Enable CoAP client mode supporting code [default=yes]])], + [enable_client_mode="$enableval"], + [enable_client_mode="yes"]) + +if test "x$enable_client_mode" = "xyes"; then + AC_DEFINE(COAP_CLIENT_SUPPORT, 1, [Define if libcoap supports client mode code]) +fi +AM_CONDITIONAL(HAVE_CLIENT_SUPPORT, [test "x$enable_client_mode" = "xyes"]) +if test "x$enable_server_mode" != "xyes" -a "x$enable_client_mode" != "xyes" ; then + AC_MSG_ERROR([==> One or both of '--enable-server-mode' and '--enable-client-mode' need to be set!]) +fi + # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T AC_TYPE_SSIZE_T @@ -904,6 +929,16 @@ libcoap configuration summary: libcoap API version : "$LIBCOAP_API_VERSION" libcoap DTLS lib extn : "$LIBCOAP_DTLS_LIB_EXTENSION_NAME" host system : "$host"]); +if test "x$enable_server_mode" = "xyes"; then + AC_MSG_RESULT([ build with server support: "yes"]) +else + AC_MSG_RESULT([ build with server support: "no"]) +fi +if test "x$enable_client_mode" = "xyes"; then + AC_MSG_RESULT([ build with client support: "yes"]) +else + AC_MSG_RESULT([ build with client support: "no"]) +fi if test "x$build_tcp" != "xno"; then AC_MSG_RESULT([ build with TCP support : "yes"]) else diff --git a/examples/Makefile.am b/examples/Makefile.am index e32a41f3..f1d9751e 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -19,14 +19,37 @@ AM_CFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include \ # -bin_PROGRAMS = coap-client@LIBCOAP_DTLS_LIB_EXTENSION_NAME@ \ - coap-server@LIBCOAP_DTLS_LIB_EXTENSION_NAME@ \ - coap-rd@LIBCOAP_DTLS_LIB_EXTENSION_NAME@ +bin_PROGRAMS = +noinst_PROGRAMS = +check_PROGRAMS = -check_PROGRAMS = coap-etsi_iot_01 coap-tiny +if HAVE_CLIENT_SUPPORT + +bin_PROGRAMS += coap-client@LIBCOAP_DTLS_LIB_EXTENSION_NAME@ +check_PROGRAMS += coap-tiny if BUILD_ADD_DEFAULT_NAMES -noinst_PROGRAMS = coap-client coap-server coap-rd +noinst_PROGRAMS += coap-client +endif # BUILD_ADD_DEFAULT_NAMES + +endif # HAVE_CLIENT_SUPPORT + +if HAVE_SERVER_SUPPORT + +bin_PROGRAMS += coap-server@LIBCOAP_DTLS_LIB_EXTENSION_NAME@ \ + coap-rd@LIBCOAP_DTLS_LIB_EXTENSION_NAME@ +check_PROGRAMS += coap-etsi_iot_01 + +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 coap_client_LDADD = $(DTLS_LIBS) \ @@ -39,7 +62,6 @@ coap_server_LDADD = $(DTLS_LIBS) \ coap_rd_SOURCES = coap-rd.c coap_rd_LDADD = $(DTLS_LIBS) \ $(top_builddir)/.libs/libcoap-$(LIBCOAP_NAME_SUFFIX).la -endif # BUILD_ADD_DEFAULT_NAMES coap_client@LIBCOAP_DTLS_LIB_EXTENSION_NAME@_SOURCES = coap-client.c coap_client@LIBCOAP_DTLS_LIB_EXTENSION_NAME@_LDADD = $(DTLS_LIBS) \ diff --git a/examples/coap-server.c b/examples/coap-server.c index 5687932f..959d1491 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -51,6 +51,10 @@ static char* strndup(const char* s1, size_t n) #include #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 @@ -1796,7 +1800,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); -#ifdef SERVER_CAN_PROXY +#if SERVER_CAN_PROXY if (proxy_host_name_count) { r = coap_resource_proxy_uri_init(hnd_proxy_uri, proxy_host_name_count, proxy_host_name_list); diff --git a/include/coap3/coap_asn1_internal.h b/include/coap3/coap_asn1_internal.h index 5bb4e2a8..b07ef30c 100644 --- a/include/coap3/coap_asn1_internal.h +++ b/include/coap3/coap_asn1_internal.h @@ -17,6 +17,7 @@ #ifndef COAP_ASN1_INTERNAL_H_ #define COAP_ASN1_INTERNAL_H_ +#include "coap_internal.h" /** * @defgroup asn1 ASN.1 Support (Internal) diff --git a/include/coap3/coap_async_internal.h b/include/coap3/coap_async_internal.h index a23b1c8e..36dd6b47 100644 --- a/include/coap3/coap_async_internal.h +++ b/include/coap3/coap_async_internal.h @@ -17,8 +17,10 @@ #ifndef COAP_ASYNC_INTERNAL_H_ #define COAP_ASYNC_INTERNAL_H_ -#include "coap3/net.h" +#include "coap_internal.h" +#include "net.h" +/* Note that if COAP_SERVER_SUPPORT is not set, then WITHOUT_ASYNC undefined */ #ifndef WITHOUT_ASYNC /** diff --git a/include/coap3/coap_block_internal.h b/include/coap3/coap_block_internal.h index b7ad0a55..df800ed1 100644 --- a/include/coap3/coap_block_internal.h +++ b/include/coap3/coap_block_internal.h @@ -19,6 +19,7 @@ #ifndef COAP_BLOCK_INTERNAL_H_ #define COAP_BLOCK_INTERNAL_H_ +#include "coap_internal.h" #include "coap_pdu_internal.h" #include "resource.h" @@ -93,6 +94,7 @@ struct coap_lg_xmit_t { void *app_ptr; /**< applicaton provided ptr for de-alloc function */ }; +#if COAP_CLIENT_SUPPORT /** * Structure to hold large body (many blocks) client receive information */ @@ -120,7 +122,9 @@ struct coap_lg_crcv_t { coap_tick_t last_used; /**< Last time all data sent or 0 */ uint16_t block_option; /**< Block option in use */ }; +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT /** * Structure to hold large body (many blocks) server receive information */ @@ -147,7 +151,9 @@ struct coap_lg_srcv_t { coap_tick_t last_used; /**< Last time data sent or 0 */ uint16_t block_option; /**< Block option in use */ }; +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu); @@ -156,7 +162,9 @@ void coap_block_delete_lg_crcv(coap_session_t *session, coap_tick_t coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now); +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv); @@ -179,7 +187,9 @@ int coap_handle_request_put_block(coap_context_t *context, coap_string_t *query, coap_method_handler_t h, int *added_block); +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *rcvd); int coap_handle_response_get_block(coap_context_t *context, @@ -187,6 +197,7 @@ int coap_handle_response_get_block(coap_context_t *context, coap_pdu_t *sent, coap_pdu_t *rcvd, coap_recurse_t recursive); +#endif /* COAP_CLIENT_SUPPORT */ void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit); diff --git a/include/coap3/coap_cache_internal.h b/include/coap3/coap_cache_internal.h index 29c07568..b39e5bdd 100644 --- a/include/coap3/coap_cache_internal.h +++ b/include/coap3/coap_cache_internal.h @@ -17,8 +17,10 @@ #ifndef COAP_CACHE_INTERNAL_H_ #define COAP_CACHE_INTERNAL_H_ +#include "coap_internal.h" #include "coap_io.h" +#if COAP_SERVER_SUPPORT /** * @defgroup cache_internal Cache Support (Internal) * CoAP Cache Structures, Enums and Functions that are not exposed to @@ -108,4 +110,6 @@ int coap_digest_final(coap_digest_ctx_t *digest_ctx, /** @} */ +#endif /* COAP_SERVER_SUPPORT */ + #endif /* COAP_CACHE_INTERNAL_H_ */ diff --git a/include/coap3/coap_dtls_internal.h b/include/coap3/coap_dtls_internal.h index 8ea09a4e..91af8921 100644 --- a/include/coap3/coap_dtls_internal.h +++ b/include/coap3/coap_dtls_internal.h @@ -13,6 +13,8 @@ #ifndef COAP_DTLS_INTERNAL_H_ #define COAP_DTLS_INTERNAL_H_ +#include "coap_internal.h" + /** * @defgroup dtls_internal DTLS Support (Internal) * CoAP DTLS Structures, Enums and Functions that are not exposed to @@ -41,6 +43,7 @@ void * coap_dtls_new_context(coap_context_t *coap_context); +#if COAP_SERVER_SUPPORT /** * Set the DTLS context's default server PSK information. * This does the PSK specifics following coap_dtls_new_context(). @@ -55,7 +58,9 @@ coap_dtls_new_context(coap_context_t *coap_context); int coap_dtls_context_set_spsk(coap_context_t *coap_context, coap_dtls_spsk_t *setup_data); +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT /** * Set the DTLS context's default client PSK information. * This does the PSK specifics following coap_dtls_new_context(). @@ -70,6 +75,7 @@ coap_dtls_context_set_spsk(coap_context_t *coap_context, int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data); +#endif /* COAP_CLIENT_SUPPORT */ /** * Set the DTLS context's default server PKI information. @@ -128,6 +134,7 @@ int coap_dtls_context_check_keys_enabled(coap_context_t *coap_context); */ void coap_dtls_free_context(void *dtls_context); +#if COAP_CLIENT_SUPPORT /** * Create a new client-side session. This should send a HELLO to the server. * @@ -137,7 +144,9 @@ void coap_dtls_free_context(void *dtls_context); * parameters for the session. */ void *coap_dtls_new_client_session(coap_session_t *coap_session); +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT /** * Create a new DTLS server-side session. * Called after coap_dtls_hello() has returned @c 1, signalling that a validated @@ -150,6 +159,7 @@ void *coap_dtls_new_client_session(coap_session_t *coap_session); * parameters for the DTLS session. */ void *coap_dtls_new_server_session(coap_session_t *coap_session); +#endif /* COAP_SERVER_SUPPORT */ /** * Terminates the DTLS session (may send an ALERT if necessary) then frees the @@ -230,6 +240,7 @@ int coap_dtls_receive(coap_session_t *coap_session, const uint8_t *data, size_t data_len); +#if COAP_SERVER_SUPPORT /** * Handling client HELLO messages from a new candiate peer. * Note that session->tls is empty. @@ -245,6 +256,7 @@ int coap_dtls_receive(coap_session_t *coap_session, int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len); +#endif /* COAP_SERVER_SUPPORT */ /** * Get DTLS overhead over cleartext PDUs. @@ -255,6 +267,7 @@ int coap_dtls_hello(coap_session_t *coap_session, */ unsigned int coap_dtls_get_overhead(coap_session_t *coap_session); +#if COAP_CLIENT_SUPPORT /** * Create a new TLS client-side session. * @@ -266,7 +279,9 @@ unsigned int coap_dtls_get_overhead(coap_session_t *coap_session); * parameters for the session. */ void *coap_tls_new_client_session(coap_session_t *coap_session, int *connected); +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT /** * Create a TLS new server-side session. * @@ -278,6 +293,7 @@ void *coap_tls_new_client_session(coap_session_t *coap_session, int *connected); * parameters for the session. */ void *coap_tls_new_server_session(coap_session_t *coap_session, int *connected); +#endif /* COAP_SERVER_SUPPORT */ /** * Terminates the TLS session (may send an ALERT if necessary) then frees the diff --git a/include/coap3/coap_internal.h b/include/coap3/coap_internal.h index 5eee5c12..2951f731 100644 --- a/include/coap3/coap_internal.h +++ b/include/coap3/coap_internal.h @@ -35,6 +35,21 @@ # include #endif +/* By default without either configured, these need to be set */ +#ifndef COAP_SERVER_SUPPORT +#ifndef COAP_CLIENT_SUPPORT +#define COAP_SERVER_SUPPORT 1 +#define COAP_CLIENT_SUPPORT 1 +#endif /* COAP_CLIENT_SUPPORT */ +#endif /* COAP_SERVER_SUPPORT */ + +#if ! COAP_SERVER_SUPPORT +#ifndef WITHOUT_ASYNC +/* ASYNC is only there for Server code */ +#define WITHOUT_ASYNC +#endif /* WITHOUT_ASYNC */ +#endif /* COAP_SERVER_SUPPORT */ + #include "coap3/coap.h" /* diff --git a/include/coap3/coap_io_internal.h b/include/coap3/coap_io_internal.h index 241ef7f9..dfe41406 100644 --- a/include/coap3/coap_io_internal.h +++ b/include/coap3/coap_io_internal.h @@ -12,6 +12,7 @@ #ifndef COAP_IO_INTERNAL_H_ #define COAP_IO_INTERNAL_H_ +#include "coap_internal.h" #include #include "address.h" @@ -54,11 +55,14 @@ struct coap_socket_t { #define COAP_SOCKET_CAN_CONNECT 0x0800 /**< non blocking client socket can now connect without blocking */ #define COAP_SOCKET_MULTICAST 0x1000 /**< socket is used for multicast communication */ +#if COAP_SERVER_SUPPORT coap_endpoint_t *coap_malloc_endpoint( void ); void coap_mfree_endpoint( coap_endpoint_t *ep ); +#endif /* COAP_SERVER_SUPPORT */ const char *coap_socket_format_errno(int error); +#if COAP_CLIENT_SUPPORT int coap_socket_connect_udp(coap_socket_t *sock, const coap_address_t *local_if, @@ -66,6 +70,7 @@ coap_socket_connect_udp(coap_socket_t *sock, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr); +#endif /* COAP_CLIENT_SUPPORT */ int coap_socket_bind_udp(coap_socket_t *sock, diff --git a/include/coap3/coap_net_internal.h b/include/coap3/coap_net_internal.h index bf243f3d..7d1d3f43 100644 --- a/include/coap3/coap_net_internal.h +++ b/include/coap3/coap_net_internal.h @@ -18,6 +18,8 @@ #ifndef COAP_NET_INTERNAL_H_ #define COAP_NET_INTERNAL_H_ +#include "coap_internal.h" + /** * @defgroup context_internal Context Handling (Internal) * CoAP Context Structures, Enums and Functions that are not exposed to @@ -44,6 +46,7 @@ struct coap_queue_t { */ struct coap_context_t { coap_opt_filter_t known_options; +#if COAP_SERVER_SUPPORT coap_resource_t *resources; /**< hash table or list of known resources */ coap_resource_t *unknown_resource; /**< can be used for handling @@ -53,6 +56,7 @@ struct coap_context_t { coap_resource_release_userdata_handler_t release_userdata; /**< function to release user_data when resource is deleted */ +#endif /* COAP_SERVER_SUPPORT */ #ifndef WITHOUT_ASYNC /** @@ -65,8 +69,12 @@ struct coap_context_t { * to sendqueue_basetime. */ coap_tick_t sendqueue_basetime; coap_queue_t *sendqueue; +#if COAP_SERVER_SUPPORT coap_endpoint_t *endpoint; /**< the endpoints used for listening */ +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT coap_session_t *sessions; /**< client sessions */ +#endif /* COAP_CLIENT_SUPPORT */ #ifdef WITH_CONTIKI struct uip_udp_conn *conn; /**< uIP connection object */ @@ -81,7 +89,9 @@ struct coap_context_t { * context, otherwise 0. */ #endif /* WITH_LWIP */ +#if COAP_CLIENT_SUPPORT coap_response_handler_t response_handler; +#endif /* COAP_CLIENT_SUPPORT */ coap_nack_handler_t nack_handler; coap_ping_handler_t ping_handler; coap_pong_handler_t pong_handler; @@ -97,20 +107,26 @@ struct coap_context_t { ssize_t (*network_read)(coap_socket_t *sock, coap_packet_t *packet); +#if COAP_CLIENT_SUPPORT size_t(*get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len); +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT size_t(*get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len); size_t(*get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len); +#endif /* COAP_SERVER_SUPPORT */ void *dtls_context; +#if COAP_SERVER_SUPPORT coap_dtls_spsk_t spsk_setup_data; /**< Contains the initial PSK server setup data */ +#endif /* COAP_SERVER_SUPPORT */ unsigned int session_timeout; /**< Number of seconds of inactivity after which an unused session will be closed. @@ -126,15 +142,19 @@ struct coap_context_t { disabled. */ unsigned int csm_timeout; /**< Timeout for waiting for a CSM from the remote side. 0 means disabled. */ +#if COAP_SERVER_SUPPORT uint8_t observe_pending; /**< Observe response pending */ +#endif /* COAP_SERVER_SUPPORT */ uint8_t block_mode; /**< Zero or more COAP_BLOCK_ or'd options */ uint64_t etag; /**< Next ETag to use */ +#if COAP_SERVER_SUPPORT coap_cache_entry_t *cache; /**< CoAP cache-entry cache */ uint16_t *cache_ignore_options; /**< CoAP options to ignore when creating a cache-key */ size_t cache_ignore_count; /**< The number of CoAP options to ignore when creating a cache-key */ +#endif /* COAP_SERVER_SUPPORT */ void *app; /**< application-specific data */ #ifdef COAP_EPOLL_SUPPORT int epfd; /**< External FD for epoll */ diff --git a/include/coap3/coap_pdu_internal.h b/include/coap3/coap_pdu_internal.h index 0d8446c0..d7c4b9ff 100644 --- a/include/coap3/coap_pdu_internal.h +++ b/include/coap3/coap_pdu_internal.h @@ -17,6 +17,8 @@ #ifndef COAP_COAP_PDU_INTERNAL_H_ #define COAP_COAP_PDU_INTERNAL_H_ +#include "coap_internal.h" + #ifdef WITH_LWIP #include #endif diff --git a/include/coap3/coap_resource_internal.h b/include/coap3/coap_resource_internal.h index cbcc36a6..4db17229 100644 --- a/include/coap3/coap_resource_internal.h +++ b/include/coap3/coap_resource_internal.h @@ -17,8 +17,10 @@ #ifndef COAP_RESOURCE_INTERNAL_H_ #define COAP_RESOURCE_INTERNAL_H_ +#include "coap_internal.h" #include "uthash.h" +#if COAP_SERVER_SUPPORT /** * @defgroup coap_resource_internal Resources (Internal) * Structures, Enums and Functions that are not exposed to applications @@ -135,7 +137,8 @@ coap_print_status_t coap_print_wellknown(coap_context_t *, size_t *, size_t, coap_opt_t *); - /** @} */ +#endif /* COAP_SERVER_SUPPORT */ + #endif /* COAP_RESOURCE_INTERNAL_H_ */ diff --git a/include/coap3/coap_session_internal.h b/include/coap3/coap_session_internal.h index 1fd2de9e..260f76cd 100644 --- a/include/coap3/coap_session_internal.h +++ b/include/coap3/coap_session_internal.h @@ -18,6 +18,7 @@ #ifndef COAP_SESSION_INTERNAL_H_ #define COAP_SESSION_INTERNAL_H_ +#include "coap_internal.h" #include "coap_io_internal.h" #define COAP_DEFAULT_SESSION_TIMEOUT 300 @@ -59,7 +60,9 @@ struct coap_session_t { int ifindex; /**< interface index */ coap_socket_t sock; /**< socket object for the session, if any */ +#if COAP_SERVER_SUPPORT coap_endpoint_t *endpoint; /**< session's endpoint */ +#endif /* COAP_SERVER_SUPPORT */ coap_context_t *context; /**< session's context */ void *tls; /**< security parameters */ uint16_t tx_mid; /**< the last message id that was used in @@ -71,8 +74,12 @@ struct coap_session_t { coap_queue_t *delayqueue; /**< list of delayed messages waiting to be sent */ coap_lg_xmit_t *lg_xmit; /**< list of large transmissions */ +#if COAP_CLIENT_SUPPORT coap_lg_crcv_t *lg_crcv; /**< Client list of expected large receives */ +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT coap_lg_srcv_t *lg_srcv; /**< Server list of expected large receives */ +#endif /* COAP_SERVER_SUPPORT */ size_t partial_write; /**< if > 0 indicates number of bytes already written from the pdu at the head of sendqueue */ @@ -129,6 +136,7 @@ struct coap_session_t { uint64_t tx_token; /**< Next token number to use */ }; +#if COAP_SERVER_SUPPORT /** * Abstraction of virtual endpoint that can be attached to coap_context_t. The * keys (port, bind_addr) must uniquely identify this endpoint. @@ -143,6 +151,7 @@ struct coap_endpoint_t { coap_address_t bind_addr; /**< local interface address */ coap_session_t *sessions; /**< hash table or list of active sessions */ }; +#endif /* COAP_SERVER_SUPPORT */ /** * Notify session transport has just connected and CSM exchange can now start. @@ -184,6 +193,7 @@ int coap_session_refresh_psk_hint(coap_session_t *session, int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key); +#if COAP_SERVER_SUPPORT /** * Creates a new server session for the specified endpoint. * @param ctx The CoAP context. @@ -196,6 +206,7 @@ coap_session_t *coap_new_server_session( coap_context_t *ctx, coap_endpoint_t *ep ); +#endif /* COAP_SERVER_SUPPORT */ /** * Function interface for datagram data transmission. This function returns @@ -245,6 +256,7 @@ ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node); +#if COAP_SERVER_SUPPORT /** * Lookup the server session for the packet received on an endpoint, or create * a new one. @@ -256,6 +268,7 @@ coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, */ coap_session_t *coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now); +#endif /* COAP_SERVER_SUPPORT */ /** * Create a new DTLS session for the @p session. diff --git a/include/coap3/coap_subscribe_internal.h b/include/coap3/coap_subscribe_internal.h index b7702362..18c98cd6 100644 --- a/include/coap3/coap_subscribe_internal.h +++ b/include/coap3/coap_subscribe_internal.h @@ -18,6 +18,10 @@ #ifndef COAP_SUBSCRIBE_INTERNAL_H_ #define COAP_SUBSCRIBE_INTERNAL_H_ +#include "coap_internal.h" + +#if COAP_SERVER_SUPPORT + /** * @defgroup subscribe_internal Observe Subscription (Internal) * CoAP Observe Subscription Structures, Enums and Functions that are not @@ -148,4 +152,5 @@ void coap_delete_observers(coap_context_t *context, coap_session_t *session); /** @} */ +#endif /* COAP_SERVER_SUPPORT */ #endif /* COAP_SUBSCRIBE_INTERNAL_H_ */ diff --git a/include/coap3/coap_tcp_internal.h b/include/coap3/coap_tcp_internal.h index 4fd31b38..bb67bdce 100644 --- a/include/coap3/coap_tcp_internal.h +++ b/include/coap3/coap_tcp_internal.h @@ -17,6 +17,7 @@ #ifndef COAP_TCP_INTERNAL_H_ #define COAP_TCP_INTERNAL_H_ +#include "coap_internal.h" #include "coap_io.h" /** diff --git a/src/async.c b/src/async.c index 3a647b38..45d4f8ae 100644 --- a/src/async.c +++ b/src/async.c @@ -154,7 +154,7 @@ coap_async_get_app_data(const coap_async_t *async) { return async->appdata; } -#else +#else /* WITHOUT_ASYNC */ int coap_async_is_supported(void) { diff --git a/src/block.c b/src/block.c index 46689039..afa539ad 100644 --- a/src/block.c +++ b/src/block.c @@ -275,6 +275,7 @@ full_match(const uint8_t *a, size_t alen, return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0); } +#if COAP_CLIENT_SUPPORT int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t type) { @@ -328,6 +329,7 @@ coap_cancel_observe(coap_session_t *session, coap_binary_t *token, } return 0; } +#endif /* COAP_CLIENT_SUPPORT */ int coap_add_data_large_internal(coap_session_t *session, @@ -623,6 +625,7 @@ fail: return 0; } +#if COAP_CLIENT_SUPPORT int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, @@ -633,7 +636,9 @@ coap_add_data_large_request(coap_session_t *session, return coap_add_data_large_internal(session, pdu, NULL, NULL, -1, 0, length, data, release_func, app_ptr); } +#endif /* ! COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, @@ -729,6 +734,7 @@ error: (const unsigned char *)coap_response_phrase(response->code)); return 0; } +#endif /* ! COAP_SERVER_SUPPORT */ coap_tick_t coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now) { @@ -755,6 +761,7 @@ coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now) { return tim_rem; } +#if COAP_CLIENT_SUPPORT coap_tick_t coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now) { coap_lg_crcv_t *p; @@ -777,6 +784,7 @@ coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now) { } return tim_rem; } +#endif /* COAP_CLIENT_SUPPORT */ static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num) { @@ -809,6 +817,7 @@ check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks) { return 1; } +#if COAP_SERVER_SUPPORT coap_tick_t coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now) { coap_lg_srcv_t *p; @@ -830,7 +839,9 @@ coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now) { } return tim_rem; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu) { coap_lg_crcv_t *lg_crcv; @@ -897,7 +908,9 @@ coap_block_delete_lg_crcv(coap_session_t *session, coap_delete_binary(lg_crcv->app_token); coap_free_type(COAP_LG_CRCV, lg_crcv); } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv) { @@ -910,6 +923,7 @@ coap_block_delete_lg_srcv(coap_session_t *session, coap_session_str(session), (void*)lg_srcv); coap_free_type(COAP_LG_SRCV, lg_srcv); } +#endif /* COAP_SERVER_SUPPORT */ void coap_block_delete_lg_xmit(coap_session_t *session, @@ -933,6 +947,7 @@ coap_block_delete_lg_xmit(coap_session_t *session, coap_free_type(COAP_LG_XMIT, lg_xmit); } +#if COAP_SERVER_SUPPORT static int add_block_send(uint32_t num, uint32_t *out_blocks, uint32_t *count, uint32_t max_count) { @@ -1179,6 +1194,7 @@ internal_issue: skip_app_handler: return 1; } +#endif /* COAP_SERVER_SUPPORT */ static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num) { @@ -1235,6 +1251,7 @@ update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num) { return 1; } +#if COAP_SERVER_SUPPORT /* * Need to check if this is a large PUT / POST using multiple blocks * @@ -1453,7 +1470,9 @@ call_app_handler: skip_app_handler: return 1; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT /* * Need to see if this is a response to a large body request transfer. If so, * need to initiate the request containing the next block and not trouble the @@ -1576,6 +1595,7 @@ fail_body: } /* end of LL_FOREACH_SAFE */ return 0; } +#endif /* COAP_CLIENT_SUPPORT */ /* * Re-assemble payloads into a body @@ -1621,6 +1641,7 @@ coap_block_build_body(coap_binary_t *body_data, size_t length, return body_data; } +#if COAP_CLIENT_SUPPORT /* * Need to see if this is a large body response to a request. If so, * need to initiate the request for the next block and not trouble the @@ -1967,6 +1988,7 @@ call_app_handler: skip_app_handler: return 1; } +#endif /* COAP_CLIENT_SUPPORT */ /* Check if lg_xmit generated and update PDU code if so */ void diff --git a/src/coap_cache.c b/src/coap_cache.c index 21d75f60..a60c5fbe 100644 --- a/src/coap_cache.c +++ b/src/coap_cache.c @@ -10,6 +10,7 @@ #include "coap3/coap_internal.h" +#if COAP_SERVER_SUPPORT /* Determines if the given option_type denotes an option type that can * be used as CacheKey. Options that can be cache keys are not Unsafe * and not marked explicitly as NoCacheKey. */ @@ -267,3 +268,4 @@ coap_expire_cache_entries(coap_context_t *ctx) { } } +#endif /* ! COAP_SERVER_SUPPORT */ diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index fae4ff07..ca6928f6 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -162,11 +162,13 @@ typedef enum coap_free_bye_t { static int dtls_log_level = 0; +#if COAP_SERVER_SUPPORT static int post_client_hello_gnutls_pki(gnutls_session_t g_session); static int post_client_hello_gnutls_psk(gnutls_session_t g_session); static int psk_server_callback(gnutls_session_t g_session, const char *identity, gnutls_datum_t *key); +#endif /* COAP_SERVER_SUPPORT */ /* * return 0 failed @@ -330,6 +332,7 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *c_context, return 1; } +#if COAP_SERVER_SUPPORT /* * return 0 failed * 1 passed @@ -347,7 +350,9 @@ coap_dtls_context_set_spsk(coap_context_t *c_context, g_context->psk_pki_enabled |= IS_PSK; return 1; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT /* * return 0 failed * 1 passed @@ -365,6 +370,7 @@ coap_dtls_context_set_cpsk(coap_context_t *c_context, g_context->psk_pki_enabled |= IS_PSK; return 1; } +#endif /* COAP_CLIENT_SUPPORT */ /* * return 0 failed @@ -502,6 +508,7 @@ coap_dtls_free_context(void *handle) { gnutls_free(g_context); } +#if COAP_CLIENT_SUPPORT /* * gnutls_psk_client_credentials_function return values * (see gnutls_psk_set_client_credentials_function()) @@ -607,6 +614,7 @@ psk_client_callback(gnutls_session_t g_session, return 0; } +#endif /* COAP_CLIENT_SUPPORT */ typedef struct { gnutls_certificate_type_t certificate_type; @@ -1433,6 +1441,7 @@ fail: return ret; } +#if COAP_SERVER_SUPPORT /* * return 0 Success (GNUTLS_E_SUCCESS) * neg GNUTLS_E_* error code @@ -1462,7 +1471,6 @@ fail: return ret; } - /* * return 0 Success (GNUTLS_E_SUCCESS) * neg GNUTLS_E_* error code @@ -1688,7 +1696,9 @@ end: fail: return ret; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT /* * return 0 Success (GNUTLS_E_SUCCESS) * neg GNUTLS_E_* error code @@ -1780,7 +1790,9 @@ setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) fail: return ret; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT /* * gnutls_psk_server_credentials_function return values * (see gnutls_psk_set_server_credentials_function()) @@ -1912,6 +1924,7 @@ setup_server_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) fail: return ret; } +#endif /* COAP_SERVER_SUPPORT */ /* * return +ve data amount @@ -2053,12 +2066,20 @@ coap_dtls_new_gnutls_env(coap_session_t *c_session, int type) "gnutls_priority_set"); if (type == GNUTLS_SERVER) { +#if COAP_SERVER_SUPPORT G_CHECK(setup_server_ssl_session(c_session, g_env), "setup_server_ssl_session"); +#else /* ! COAP_SERVER_SUPPORT */ + goto fail; +#endif /* ! COAP_SERVER_SUPPORT */ } else { +#if COAP_CLIENT_SUPPORT G_CHECK(setup_client_ssl_session(c_session, g_env), "setup_client_ssl_session"); +#else /* COAP_CLIENT_SUPPORT */ + goto fail; +#endif /* COAP_CLIENT_SUPPORT */ } gnutls_handshake_set_timeout(g_env->g_session, @@ -2114,6 +2135,7 @@ coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context, } } +#if COAP_SERVER_SUPPORT void *coap_dtls_new_server_session(coap_session_t *c_session) { coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; @@ -2122,6 +2144,7 @@ void *coap_dtls_new_server_session(coap_session_t *c_session) { return g_env; } +#endif /* COAP_SERVER_SUPPORT */ static void log_last_alert(coap_session_t *c_session, gnutls_session_t g_session) { @@ -2245,6 +2268,7 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) { return ret; } +#if COAP_CLIENT_SUPPORT void *coap_dtls_new_client_session(coap_session_t *c_session) { coap_gnutls_env_t *g_env = coap_dtls_new_gnutls_env(c_session, GNUTLS_CLIENT); int ret; @@ -2261,6 +2285,7 @@ void *coap_dtls_new_client_session(coap_session_t *c_session) { } return g_env; } +#endif /* COAP_CLIENT_SUPPORT */ void coap_dtls_free_session(coap_session_t *c_session) { if (c_session && c_session->context && c_session->tls) { @@ -2495,6 +2520,7 @@ coap_dtls_receive(coap_session_t *c_session, return ret; } +#if COAP_SERVER_SUPPORT /* * return -1 failure * 0 not completed @@ -2576,6 +2602,7 @@ coap_dtls_hello(coap_session_t *c_session, } return ret; } +#endif /* COAP_SERVER_SUPPORT */ unsigned int coap_dtls_get_overhead(coap_session_t *c_session COAP_UNUSED) { return 37; @@ -2662,6 +2689,7 @@ coap_sock_write(gnutls_transport_ptr_t context, const void *in, size_t inl) { return ret; } +#if COAP_CLIENT_SUPPORT void *coap_tls_new_client_session(coap_session_t *c_session, int *connected) { coap_gnutls_env_t *g_env = gnutls_malloc(sizeof(coap_gnutls_env_t)); coap_gnutls_context_t *g_context = @@ -2706,7 +2734,9 @@ fail: gnutls_free(g_env); return NULL; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void *coap_tls_new_server_session(coap_session_t *c_session, int *connected) { coap_gnutls_env_t *g_env = gnutls_malloc(sizeof(coap_gnutls_env_t)); coap_gnutls_context_t *g_context = @@ -2747,6 +2777,7 @@ void *coap_tls_new_server_session(coap_session_t *c_session, int *connected) { fail: return NULL; } +#endif /* COAP_SERVER_SUPPORT */ void coap_tls_free_session(coap_session_t *c_session) { coap_dtls_free_session(c_session); @@ -2903,6 +2934,7 @@ ssize_t coap_tls_read(coap_session_t *c_session, } #endif /* !COAP_DISABLE_TCP */ +#if COAP_SERVER_SUPPORT coap_digest_ctx_t * coap_digest_setup(void) { gnutls_hash_hd_t digest_ctx; @@ -2935,6 +2967,7 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_free(digest_ctx); return 1; } +#endif /* COAP_SERVER_SUPPORT */ #else /* !HAVE_LIBGNUTLS */ diff --git a/src/coap_io.c b/src/coap_io.c index a1b2258a..84fe18e0 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -140,6 +140,7 @@ void coap_socket_close(coap_socket_t *sock) { #else +#if COAP_SERVER_SUPPORT coap_endpoint_t * coap_malloc_endpoint(void) { return (coap_endpoint_t *)coap_malloc_type(COAP_ENDPOINT, sizeof(coap_endpoint_t)); @@ -149,6 +150,7 @@ void coap_mfree_endpoint(coap_endpoint_t *ep) { coap_free_type(COAP_ENDPOINT, ep); } +#endif /* COAP_SERVER_SUPPORT */ int coap_socket_bind_udp(coap_socket_t *sock, @@ -238,6 +240,7 @@ error: return 0; } +#if COAP_CLIENT_SUPPORT int coap_socket_connect_udp(coap_socket_t *sock, const coap_address_t *local_if, @@ -364,12 +367,17 @@ error: coap_socket_close(sock); return 0; } +#endif /* COAP_CLIENT_SUPPORT */ void coap_socket_close(coap_socket_t *sock) { if (sock->fd != COAP_INVALID_SOCKET) { #ifdef COAP_EPOLL_SUPPORT +#if COAP_SERVER_SUPPORT coap_context_t *context = sock->session ? sock->session->context : sock->endpoint ? sock->endpoint->context : NULL; +#else /* COAP_SERVER_SUPPORT */ + coap_context_t *context = sock->session ? sock->session->context : NULL; +#endif /* COAP_SERVER_SUPPORT */ if (context != NULL) { int ret; struct epoll_event event; @@ -405,8 +413,12 @@ coap_epoll_ctl_mod(coap_socket_t *sock, if (sock == NULL) return; +#if COAP_SERVER_SUPPORT context = sock->session ? sock->session->context : sock->endpoint ? sock->endpoint->context : NULL; +#else /* COAP_SERVER_SUPPORT */ + context = sock->session ? sock->session->context : NULL; +#endif /* COAP_SERVER_SUPPORT */ if (context == NULL) return; @@ -1063,9 +1075,7 @@ coap_io_prepare_io(coap_context_t *ctx, coap_tick_t now) { coap_queue_t *nextpdu; - coap_endpoint_t *ep; coap_session_t *s, *rtmp; - coap_tick_t session_timeout; coap_tick_t timeout = 0; coap_tick_t s_timeout; #ifdef COAP_EPOLL_SUPPORT @@ -1075,19 +1085,25 @@ coap_io_prepare_io(coap_context_t *ctx, *num_sockets = 0; +#if COAP_SERVER_SUPPORT /* Check to see if we need to send off any Observe requests */ coap_check_notify(ctx); - - if (ctx->session_timeout > 0) - session_timeout = ctx->session_timeout * COAP_TICKS_PER_SECOND; - else - session_timeout = COAP_DEFAULT_SESSION_TIMEOUT * COAP_TICKS_PER_SECOND; +#endif /* COAP_SERVER_SUPPORT */ #ifndef WITHOUT_ASYNC /* Check to see if we need to send off any Async requests */ timeout = coap_check_async(ctx, now); #endif /* WITHOUT_ASYNC */ +#if COAP_SERVER_SUPPORT + coap_endpoint_t *ep; + coap_tick_t session_timeout; + + if (ctx->session_timeout > 0) + session_timeout = ctx->session_timeout * COAP_TICKS_PER_SECOND; + else + session_timeout = COAP_DEFAULT_SESSION_TIMEOUT * COAP_TICKS_PER_SECOND; + LL_FOREACH(ctx->endpoint, ep) { #ifndef COAP_EPOLL_SUPPORT if (ep->sock.flags & (COAP_SOCKET_WANT_READ | COAP_SOCKET_WANT_WRITE | COAP_SOCKET_WANT_ACCEPT)) { @@ -1128,6 +1144,8 @@ coap_io_prepare_io(coap_context_t *ctx, } } } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) { if (!COAP_DISABLE_TCP && s->type == COAP_SESSION_TYPE_CLIENT @@ -1186,6 +1204,7 @@ coap_io_prepare_io(coap_context_t *ctx, } #endif /* ! COAP_EPOLL_SUPPORT */ } +#endif /* COAP_CLIENT_SUPPORT */ nextpdu = coap_peek_next(ctx); @@ -1209,6 +1228,7 @@ coap_io_prepare_io(coap_context_t *ctx, timeout = tls_timeout - now; } } else { +#if COAP_SERVER_SUPPORT LL_FOREACH(ctx->endpoint, ep) { if (ep->proto == COAP_PROTO_DTLS) { SESSIONS_ITER(ep->sessions, s, rtmp) { @@ -1235,6 +1255,8 @@ coap_io_prepare_io(coap_context_t *ctx, } } } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT SESSIONS_ITER(ctx->sessions, s, rtmp) { if (s->state == COAP_SESSION_STATE_HANDSHAKE && s->proto == COAP_PROTO_DTLS && s->tls) { @@ -1256,6 +1278,7 @@ coap_io_prepare_io(coap_context_t *ctx, timeout = tls_timeout - now; } } +#endif /* COAP_CLIENT_SUPPORT */ } } @@ -1449,7 +1472,9 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, } while (nfds == COAP_MAX_EPOLL_EVENTS); #endif /* COAP_EPOLL_SUPPORT */ +#if COAP_SERVER_SUPPORT coap_expire_cache_entries(ctx); +#endif /* COAP_SERVER_SUPPORT */ coap_ticks(&now); #ifndef WITHOUT_ASYNC /* Check to see if we need to send off any Async requests as delay might diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index f3da526a..6fb7ce4f 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -68,6 +68,13 @@ #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ #endif /* ! MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */ +#if ! COAP_SERVER_SUPPORT +#undef MBEDTLS_SSL_SRV_C +#endif /* ! COAP_SERVER_SUPPORT */ +#if ! COAP_CLIENT_SUPPORT +#undef MBEDTLS_SSL_CLI_C +#endif /* ! COAP_CLIENT_SUPPORT */ + #ifdef _WIN32 #define strcasecmp _stricmp #endif @@ -955,6 +962,7 @@ fail: } #endif /* MBEDTLS_SSL_SRV_C */ +#if COAP_CLIENT_SUPPORT static int *psk_ciphers = NULL; static int *pki_ciphers = NULL; static int processed_ciphers = 0; @@ -1134,6 +1142,7 @@ static int setup_client_ssl_session(coap_session_t *c_session, fail: return ret; } +#endif /* COAP_CLIENT_SUPPORT */ static void mbedtls_cleanup(coap_mbedtls_env_t *m_env) { @@ -1432,15 +1441,21 @@ static coap_mbedtls_env_t *coap_dtls_new_mbedtls_env(coap_session_t *c_session, } if (role == COAP_DTLS_ROLE_CLIENT) { +#if COAP_CLIENT_SUPPORT if (setup_client_ssl_session(c_session, m_env) != 0) { goto fail; } -#if defined(MBEDTLS_SSL_SRV_C) +#else /* !COAP_CLIENT_SUPPORT */ + goto fail; +#endif /* !COAP_CLIENT_SUPPORT */ } else if (role == COAP_DTLS_ROLE_SERVER) { +#if defined(MBEDTLS_SSL_SRV_C) if (setup_server_ssl_session(c_session, m_env) != 0) { goto fail; } -#endif /* MBEDTLS_SSL_SRV_C */ +#else /* ! MBEDTLS_SSL_SRV_C */ + goto fail; +#endif /* ! MBEDTLS_SSL_SRV_C */ } else { goto fail; } @@ -1512,6 +1527,7 @@ void *coap_dtls_new_context(coap_context_t *c_context) return m_context; } +#if COAP_SERVER_SUPPORT /* * return 0 failed * 1 passed @@ -1535,7 +1551,9 @@ coap_dtls_context_set_spsk(coap_context_t *c_context, m_context->psk_pki_enabled |= IS_PSK; return 1; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT /* * return 0 failed * 1 passed @@ -1545,11 +1563,11 @@ coap_dtls_context_set_cpsk(coap_context_t *c_context, coap_dtls_cpsk_t *setup_data ) { #if !defined(MBEDTLS_SSL_CLI_C) - coap_log(LOG_EMERG, "coap_context_set_spsk:" + coap_log(LOG_EMERG, "coap_context_set_cpsk:" " libcoap not compiled for Client Mode for Mbed TLS" " - update Mbed TLS to include Client Mode\n"); return 0; -#endif /* !MBEDTLS_SSL_CLI_C */ +#else /* MBEDTLS_SSL_CLI_C */ coap_mbedtls_context_t *m_context = ((coap_mbedtls_context_t *)c_context->dtls_context); @@ -1563,6 +1581,8 @@ coap_dtls_context_set_cpsk(coap_context_t *c_context, m_context->psk_pki_enabled |= IS_PSK; return 1; } +#endif /* MBEDTLS_SSL_CLI_C */ +#endif /* COAP_CLIENT_SUPPORT */ int coap_dtls_context_set_pki(coap_context_t *c_context, const coap_dtls_pki_t *setup_data, @@ -1668,6 +1688,7 @@ void coap_dtls_free_context(void *dtls_context) mbedtls_free(m_context); } +#if COAP_CLIENT_SUPPORT void *coap_dtls_new_client_session(coap_session_t *c_session) { #if !defined(MBEDTLS_SSL_CLI_C) @@ -1695,7 +1716,9 @@ void *coap_dtls_new_client_session(coap_session_t *c_session) return m_env; #endif /* MBEDTLS_SSL_CLI_C */ } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void *coap_dtls_new_server_session(coap_session_t *c_session) { #if !defined(MBEDTLS_SSL_SRV_C) @@ -1717,6 +1740,7 @@ void *coap_dtls_new_server_session(coap_session_t *c_session) return m_env; #endif /* MBEDTLS_SSL_SRV_C */ } +#endif /* COAP_SERVER_SUPPORT */ void coap_dtls_free_session(coap_session_t *c_session) { @@ -1976,6 +2000,7 @@ finish: return ret; } +#if COAP_SERVER_SUPPORT /* * return -1 failure * 0 not completed @@ -2050,6 +2075,7 @@ int coap_dtls_hello(coap_session_t *c_session, return ret; #endif /* MBEDTLS_SSL_PROTO_DTLS && MBEDTLS_SSL_SRV_C */ } +#endif /* COAP_SERVER_SUPPORT */ unsigned int coap_dtls_get_overhead(coap_session_t *c_session) { @@ -2063,11 +2089,13 @@ unsigned int coap_dtls_get_overhead(coap_session_t *c_session) } #if !COAP_DISABLE_TCP +#if COAP_CLIENT_SUPPORT void *coap_tls_new_client_session(coap_session_t *c_session, int *connected) { #if !defined(MBEDTLS_SSL_CLI_C) (void)c_session; + *connected = 0; coap_log(LOG_EMERG, "coap_tls_new_client_session:" " libcoap not compiled for Client Mode for Mbed TLS" " - update Mbed TLS to include Client Mode\n"); @@ -2095,7 +2123,9 @@ void *coap_tls_new_client_session(coap_session_t *c_session, return m_env; #endif /* MBEDTLS_SSL_CLI_C */ } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void *coap_tls_new_server_session(coap_session_t *c_session COAP_UNUSED, int *connected COAP_UNUSED) { @@ -2119,6 +2149,7 @@ void *coap_tls_new_server_session(coap_session_t *c_session COAP_UNUSED, return m_env; #endif /* MBEDTLS_SSL_SRV_C */ } +#endif /* COAP_SERVER_SUPPORT */ void coap_tls_free_session(coap_session_t *c_session) { @@ -2329,6 +2360,7 @@ coap_tls_version_t * coap_get_tls_library_version(void) return &version; } +#if COAP_SERVER_SUPPORT coap_digest_ctx_t * coap_digest_setup(void) { mbedtls_sha256_context *digest_ctx = mbedtls_malloc(sizeof(mbedtls_sha256_context)); @@ -2363,6 +2395,7 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_free(digest_ctx); return ret == 0; } +#endif /* COAP_SERVER_SUPPORT */ #else /* !HAVE_MBEDTLS */ diff --git a/src/coap_notls.c b/src/coap_notls.c index 374d7036..54cf4db9 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -47,19 +47,23 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, return 0; } +#if COAP_CLIENT_SUPPORT int coap_dtls_context_set_cpsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_cpsk_t* setup_data COAP_UNUSED ) { return 0; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT int coap_dtls_context_set_spsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_spsk_t* setup_data COAP_UNUSED ) { return 0; } +#endif /* COAP_SERVER_SUPPORT */ int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) @@ -102,13 +106,17 @@ void coap_dtls_free_context(void *handle COAP_UNUSED) { } +#if COAP_SERVER_SUPPORT void *coap_dtls_new_server_session(coap_session_t *session COAP_UNUSED) { return NULL; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT void *coap_dtls_new_client_session(coap_session_t *session COAP_UNUSED) { return NULL; } +#endif /* COAP_CLIENT_SUPPORT */ void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED) { } @@ -148,6 +156,7 @@ coap_dtls_receive(coap_session_t *session COAP_UNUSED, return -1; } +#if COAP_SERVER_SUPPORT int coap_dtls_hello(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, @@ -155,18 +164,23 @@ coap_dtls_hello(coap_session_t *session COAP_UNUSED, ) { return 0; } +#endif /* COAP_SERVER_SUPPORT */ unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED) { return 0; } +#if COAP_CLIENT_SUPPORT void *coap_tls_new_client_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED) { return NULL; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void *coap_tls_new_server_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED) { return NULL; } +#endif /* COAP_SERVER_SUPPORT */ void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED) { } @@ -185,6 +199,7 @@ ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, return -1; } +#if COAP_SERVER_SUPPORT typedef struct coap_local_hash_t { size_t ofs; coap_key_t key[8]; /* 32 bytes in total */ @@ -228,6 +243,7 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_free(digest_ctx); return 1; } +#endif /* COAP_SERVER_SUPPORT */ #else /* !HAVE_LIBTINYDTLS && !HAVE_OPENSSL && !HAVE_LIBGNUTLS */ diff --git a/src/coap_openssl.c b/src/coap_openssl.c index 8026e7a6..5e7046e8 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -145,11 +145,13 @@ typedef struct coap_openssl_context_t { psk_sni_entry *psk_sni_entry_list; } coap_openssl_context_t; +#if COAP_SERVER_SUPPORT #if OPENSSL_VERSION_NUMBER < 0x10101000L static int psk_tls_server_name_call_back(SSL *ssl, int *sd, void *arg); #else /* OPENSSL_VERSION_NUMBER >= 0x10101000L */ static int psk_tls_client_hello_call_back(SSL *ssl, int *al, void *arg); #endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L */ +#endif /* COAP_SERVER_SUPPORT */ int coap_dtls_is_supported(void) { if (SSLeay() < 0x10100000L) { @@ -299,11 +301,13 @@ static int coap_dgram_write(BIO *a, const char *in, int inl) { coap_ssl_data *data = (coap_ssl_data *)BIO_get_data(a); if (data->session) { +#if COAP_SERVER_SUPPORT if (data->session->sock.flags == COAP_SOCKET_EMPTY && data->session->endpoint == NULL) { /* socket was closed on client due to error */ BIO_clear_retry_flags(a); return -1; } +#endif /* COAP_SERVER_SUPPORT */ ret = (int)coap_session_send(data->session, (const uint8_t *)in, (size_t)inl); BIO_clear_retry_flags(a); if (ret <= 0) @@ -410,6 +414,7 @@ coap_dtls_verify_cookie(SSL *ssl, return 0; } +#if COAP_CLIENT_SUPPORT static unsigned int coap_dtls_psk_client_callback( SSL *ssl, @@ -490,7 +495,9 @@ coap_dtls_psk_client_callback( identity[identity_len] = 0; return (unsigned)psk_len; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT static unsigned int coap_dtls_psk_server_callback( SSL *ssl, @@ -547,6 +554,7 @@ coap_dtls_psk_server_callback( (uint8_t*)psk, max_psk_len); } +#endif /* COAP_SERVER_SUPPORT */ static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) { coap_session_t *session = (coap_session_t*)SSL_get_app_data(ssl); @@ -789,6 +797,7 @@ error: return NULL; } +#if COAP_SERVER_SUPPORT int coap_dtls_context_set_spsk(coap_context_t *c_context, coap_dtls_spsk_t *setup_data @@ -858,7 +867,9 @@ coap_dtls_context_set_spsk(coap_context_t *c_context, o_context->psk_pki_enabled |= IS_PSK; return 1; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT int coap_dtls_context_set_cpsk(coap_context_t *c_context, coap_dtls_cpsk_t *setup_data @@ -889,6 +900,7 @@ coap_dtls_context_set_cpsk(coap_context_t *c_context, o_context->psk_pki_enabled |= IS_PSK; return 1; } +#endif /* COAP_CLIENT_SUPPORT */ static int map_key_type(int asn1_private_key_type @@ -920,6 +932,7 @@ map_key_type(int asn1_private_key_type #if !COAP_DISABLE_TCP static uint8_t coap_alpn[] = { 4, 'c', 'o', 'a', 'p' }; +#if COAP_SERVER_SUPPORT static int server_alpn_callback (SSL *ssl COAP_UNUSED, const unsigned char **out, @@ -941,6 +954,7 @@ server_alpn_callback (SSL *ssl COAP_UNUSED, *out = tout; return (ret != OPENSSL_NPN_NEGOTIATED) ? SSL_TLSEXT_ERR_NOACK : SSL_TLSEXT_ERR_OK; } +#endif /* COAP_SERVER_SUPPORT */ #endif /* !COAP_DISABLE_TCP */ static void @@ -984,7 +998,7 @@ missing_ENGINE_load_cert (const char *cert_id) return params.cert; } -#if OPENSSL_VERSION_NUMBER < 0x10101000L +#if OPENSSL_VERSION_NUMBER < 0x10101000L && COAP_SERVER_SUPPORT static int setup_pki_server(SSL_CTX *ctx, const coap_dtls_pki_t* setup_data @@ -1365,6 +1379,7 @@ setup_pki_server(SSL_CTX *ctx, } #endif /* OPENSSL_VERSION_NUMBER < 0x10101000L */ +#if OPENSSL_VERSION_NUMBER >= 0x10101000L || COAP_CLIENT_SUPPORT static int setup_pki_ssl(SSL *ssl, coap_dtls_pki_t* setup_data, coap_dtls_role_t role @@ -1780,6 +1795,7 @@ setup_pki_ssl(SSL *ssl, } return 1; } +#endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L || COAP_CLIENT_SUPPORT */ static char* get_san_or_cn_from_cert(X509* x509) { @@ -1936,6 +1952,7 @@ tls_verify_call_back(int preverify_ok, X509_STORE_CTX *ctx) { return preverify_ok; } +#if COAP_SERVER_SUPPORT #if OPENSSL_VERSION_NUMBER < 0x10101000L /* * During the SSL/TLS initial negotiations, tls_secret_call_back() is called so @@ -2405,10 +2422,10 @@ is_x509: } sni_setup_data = *setup_data; sni_setup_data.pki_key = context->sni_entry_list[i].pki_key; - setup_pki_ssl(ssl, &sni_setup_data, 1); + setup_pki_ssl(ssl, &sni_setup_data, COAP_DTLS_ROLE_SERVER); } else { - setup_pki_ssl(ssl, setup_data, 1); + setup_pki_ssl(ssl, setup_data, COAP_DTLS_ROLE_SERVER); } coap_log(LOG_DEBUG, " %s: Using PKI ciphers\n", @@ -2562,6 +2579,7 @@ int_err: return SSL_CLIENT_HELLO_ERROR; } #endif /* OPENSSL_VERSION_NUMBER >= 0x10101000L */ +#endif /* COAP_SERVER_SUPPORT */ int coap_dtls_context_set_pki(coap_context_t *ctx, @@ -2588,6 +2606,7 @@ coap_dtls_context_set_pki(coap_context_t *ctx, context->setup_data.allow_bad_md_hash = 1; context->setup_data.allow_short_rsa_length = 1; } +#if COAP_SERVER_SUPPORT if (role == COAP_DTLS_ROLE_SERVER) { if (context->dtls.ctx) { /* SERVER DTLS */ @@ -2650,6 +2669,9 @@ coap_dtls_context_set_pki(coap_context_t *ctx, } #endif /* !COAP_DISABLE_TCP */ } +#else /* ! COAP_SERVER_SUPPORT */ + (void)role; +#endif /* ! COAP_SERVER_SUPPORT */ if (!context->dtls.ssl) { /* This is set up to handle new incoming sessions to a server */ @@ -2745,6 +2767,7 @@ void coap_dtls_free_context(void *handle) { coap_free(context); } +#if COAP_SERVER_SUPPORT void * coap_dtls_new_server_session(coap_session_t *session) { BIO *nbio = NULL; SSL *nssl = NULL, *ssl = NULL; @@ -2800,7 +2823,9 @@ error: SSL_free(nssl); return NULL; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT static int setup_client_ssl_session(coap_session_t *session, SSL *ssl ) { @@ -2817,7 +2842,9 @@ setup_client_ssl_session(coap_session_t *session, SSL *ssl setup_data->client_sni); } SSL_set_psk_client_callback(ssl, coap_dtls_psk_client_callback); +#if COAP_SERVER_SUPPORT SSL_set_psk_server_callback(ssl, coap_dtls_psk_server_callback); +#endif /* COAP_SERVER_SUPPORT */ SSL_set_cipher_list(ssl, COAP_OPENSSL_PSK_CIPHERS); if (setup_data->validate_ih_call_back) { if (session->proto == COAP_PROTO_DTLS) { @@ -2834,7 +2861,7 @@ setup_client_ssl_session(coap_session_t *session, SSL *ssl } if (context->psk_pki_enabled & IS_PKI) { coap_dtls_pki_t *setup_data = &context->setup_data; - if (!setup_pki_ssl(ssl, setup_data, 0)) + if (!setup_pki_ssl(ssl, setup_data, COAP_DTLS_ROLE_CLIENT)) return 0; /* libcoap is managing (D)TLS connection based on setup_data options */ #if !COAP_DISABLE_TCP @@ -2925,6 +2952,7 @@ void coap_dtls_session_update_mtu(coap_session_t *session) { if (ssl) SSL_set_mtu(ssl, (long)session->mtu); } +#endif /* COAP_CLIENT_SUPPORT */ void coap_dtls_free_session(coap_session_t *session) { SSL *ssl = (SSL *)session->tls; @@ -3007,6 +3035,7 @@ void coap_dtls_handle_timeout(coap_session_t *session) { } } +#if COAP_SERVER_SUPPORT int coap_dtls_hello(coap_session_t *session, const uint8_t *data, size_t data_len) { coap_dtls_context_t *dtls = &((coap_openssl_context_t *)session->context->dtls_context)->dtls; @@ -3042,6 +3071,7 @@ int coap_dtls_hello(coap_session_t *session, */ return r; } +#endif /* COAP_SERVER_SUPPORT */ int coap_dtls_receive(coap_session_t *session, const uint8_t *data, size_t data_len) { @@ -3165,6 +3195,7 @@ unsigned int coap_dtls_get_overhead(coap_session_t *session) { } #if !COAP_DISABLE_TCP +#if COAP_CLIENT_SUPPORT void *coap_tls_new_client_session(coap_session_t *session, int *connected) { BIO *bio = NULL; SSL *ssl = NULL; @@ -3217,7 +3248,9 @@ error: SSL_free(ssl); return NULL; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void *coap_tls_new_server_session(coap_session_t *session, int *connected) { BIO *bio = NULL; SSL *ssl = NULL; @@ -3275,6 +3308,7 @@ error: SSL_free(ssl); return NULL; } +#endif /* COAP_SERVER_SUPPORT */ void coap_tls_free_session(coap_session_t *session) { SSL *ssl = (SSL *)session->tls; @@ -3420,6 +3454,7 @@ ssize_t coap_tls_read(coap_session_t *session, } #endif /* !COAP_DISABLE_TCP */ +#if COAP_SERVER_SUPPORT coap_digest_ctx_t * coap_digest_setup(void) { SHA256_CTX *digest_ctx = OPENSSL_malloc(sizeof(SHA256_CTX)); @@ -3450,6 +3485,7 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_free(digest_ctx); return ret; } +#endif /* COAP_SERVER_SUPPORT */ #else /* !HAVE_OPENSSL */ diff --git a/src/coap_session.c b/src/coap_session.c index 72c361b3..bae39260 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -1,4 +1,4 @@ -/* session.c -- Session management for libcoap +/* coap_session.c -- Session management for libcoap * * Copyright (C) 2017 Jean-Claue Michelou * @@ -112,6 +112,9 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_address_t *remote_addr, int ifindex, coap_context_t *context, coap_endpoint_t *endpoint) { coap_session_t *session = (coap_session_t*)coap_malloc_type(COAP_SESSION, sizeof(coap_session_t)); +#if ! COAP_SERVER_SUPPORT + (void)endpoint; +#endif /* ! COAP_SERVER_SUPPORT */ if (!session) return NULL; memset(session, 0, sizeof(*session)); @@ -131,12 +134,14 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, coap_address_init(&session->addr_info.remote); session->ifindex = ifindex; session->context = context; +#if COAP_SERVER_SUPPORT session->endpoint = endpoint; - session->block_mode = context->block_mode; if (endpoint) session->mtu = endpoint->default_mtu; else +#endif /* COAP_SERVER_SUPPORT */ session->mtu = COAP_DEFAULT_MTU; + session->block_mode = context->block_mode; if (proto == COAP_PROTO_DTLS) { session->tls_overhead = 29; if (session->tls_overhead >= session->mtu) { @@ -158,10 +163,10 @@ coap_make_session(coap_proto_t proto, coap_session_type_t type, void coap_session_mfree(coap_session_t *session) { coap_queue_t *q, *tmp; - coap_cache_entry_t *cp, *ctmp; coap_lg_xmit_t *lq, *ltmp; + +#if COAP_CLIENT_SUPPORT coap_lg_crcv_t *cq, *etmp; - coap_lg_srcv_t *sq, *stmp; /* Need to do this before (D)TLS and socket is closed down */ LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) { @@ -183,6 +188,7 @@ void coap_session_mfree(coap_session_t *session) { LL_DELETE(session->lg_crcv, cq); coap_block_delete_lg_crcv(session, cq); } +#endif /* COAP_CLIENT_SUPPORT */ if (session->partial_pdu) coap_delete_pdu(session->partial_pdu); @@ -201,12 +207,15 @@ void coap_session_mfree(coap_session_t *session) { if (session->psk_hint) coap_free(session->psk_hint); +#if COAP_SERVER_SUPPORT + coap_cache_entry_t *cp, *ctmp; HASH_ITER(hh, session->context->cache, cp, ctmp) { /* cp->session is NULL if not session based */ if (cp->session == session) { coap_delete_cache_entry(session->context, cp); } } +#endif /* COAP_SERVER_SUPPORT */ LL_FOREACH_SAFE(session->delayqueue, q, tmp) { if (q->pdu->type==COAP_MESSAGE_CON && session->context && session->context->nack_handler) session->context->nack_handler(session, q->pdu, session->proto == COAP_PROTO_DTLS ? COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, q->id); @@ -216,10 +225,14 @@ void coap_session_mfree(coap_session_t *session) { LL_DELETE(session->lg_xmit, lq); coap_block_delete_lg_xmit(session, lq); } +#if COAP_SERVER_SUPPORT + coap_lg_srcv_t *sq, *stmp; + LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) { LL_DELETE(session->lg_srcv, sq); coap_block_delete_lg_srcv(session, sq); } +#endif /* COAP_SERVER_SUPPORT */ } void coap_session_free(coap_session_t *session) { @@ -229,13 +242,18 @@ void coap_session_free(coap_session_t *session) { if (session->ref) return; coap_session_mfree(session); +#if COAP_SERVER_SUPPORT if (session->endpoint) { if (session->endpoint->sessions) SESSIONS_DELETE(session->endpoint->sessions, session); - } else if (session->context) { + } else +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT + if (session->context) { if (session->context->sessions) SESSIONS_DELETE(session->context->sessions, session); } +#endif /* COAP_CLIENT_SUPPORT */ coap_log(LOG_DEBUG, "***%s: session closed\n", coap_session_str(session)); coap_free_type(COAP_SESSION, session); @@ -278,10 +296,12 @@ ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t d ssize_t bytes_written; coap_socket_t *sock = &session->sock; +#if COAP_SERVER_SUPPORT if (sock->flags == COAP_SOCKET_EMPTY) { assert(session->endpoint != NULL); sock = &session->endpoint->sock; } +#endif /* COAP_SERVER_SUPPORT */ bytes_written = coap_socket_send(sock, session, data, datalen); if (bytes_written == (ssize_t)datalen) { @@ -460,7 +480,9 @@ void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reaso coap_log(LOG_DEBUG, "***%s: session disconnected (reason %d)\n", coap_session_str(session), reason); +#if COAP_SERVER_SUPPORT coap_delete_observers( session->context, session ); +#endif /* COAP_SERVER_SUPPORT */ if ( session->tls) { if (session->proto == COAP_PROTO_DTLS) @@ -541,6 +563,7 @@ void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reaso #endif /* !COAP_DISABLE_TCP */ } +#if COAP_SERVER_SUPPORT static void coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto, const coap_addr_tuple_t *addr_info) { @@ -703,6 +726,7 @@ coap_session_new_dtls_session(coap_session_t *session, } return session; } +#endif /* COAP_SERVER_SUPPORT */ #ifdef COAP_EPOLL_SUPPORT static void @@ -717,8 +741,12 @@ coap_epoll_ctl_add(coap_socket_t *sock, if (sock == NULL) return; +#if COAP_SERVER_SUPPORT context = sock->session ? sock->session->context : sock->endpoint ? sock->endpoint->context : NULL; +#else /* ! COAP_SERVER_SUPPORT */ + context = sock->session ? sock->session->context : NULL; +#endif /* ! COAP_SERVER_SUPPORT */ if (context == NULL) return; @@ -737,6 +765,7 @@ coap_epoll_ctl_add(coap_socket_t *sock, } #endif /* COAP_EPOLL_SUPPORT */ +#if COAP_CLIENT_SUPPORT static coap_session_t * coap_session_create_client( coap_context_t *ctx, @@ -864,7 +893,9 @@ coap_session_connect(coap_session_t *session) { coap_ticks(&session->last_rx_tx); return session; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT static coap_session_t * coap_session_accept(coap_session_t *session) { #if !COAP_DISABLE_TCP @@ -894,7 +925,9 @@ coap_session_accept(coap_session_t *session) { #endif /* COAP_DISABLE_TCP */ return session; } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT coap_session_t *coap_new_client_session( coap_context_t *ctx, const coap_address_t *local_if, @@ -992,7 +1025,9 @@ coap_session_t *coap_new_client_session_psk2( coap_session_str(session)); return coap_session_connect(session); } +#endif /* ! COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint ) { @@ -1021,6 +1056,7 @@ int coap_session_refresh_psk_hint(coap_session_t *session, return 1; } +#endif /* COAP_SERVER_SUPPORT */ int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key @@ -1064,6 +1100,7 @@ coap_session_get_psk_key(const coap_session_t *session) { return NULL; } +#if COAP_CLIENT_SUPPORT coap_session_t *coap_new_client_session_pki( coap_context_t *ctx, const coap_address_t *local_if, @@ -1102,7 +1139,9 @@ coap_session_t *coap_new_client_session_pki( coap_session_str(session)); return coap_session_connect(session); } +#endif /* ! COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT coap_session_t *coap_new_server_session( coap_context_t *ctx, coap_endpoint_t *ep @@ -1141,6 +1180,7 @@ error: coap_session_free(session); return NULL; } +#endif /* COAP_SERVER_SUPPORT */ void coap_session_init_token(coap_session_t *session, size_t len, @@ -1194,15 +1234,21 @@ coap_session_get_type(const coap_session_t *session) { return 0; } +#if COAP_CLIENT_SUPPORT int coap_session_set_type_client(coap_session_t *session) { +#if COAP_SERVER_SUPPORT if (session && session->type == COAP_SESSION_TYPE_SERVER) { coap_session_reference(session); session->type = COAP_SESSION_TYPE_CLIENT; return 1; } +#else /* ! COAP_SERVER_SUPPORT */ + (void)session; +#endif /* ! COAP_SERVER_SUPPORT */ return 0; } +#endif /* COAP_CLIENT_SUPPORT */ coap_session_state_t coap_session_get_state(const coap_session_t *session) { @@ -1225,6 +1271,7 @@ void *coap_session_get_tls(const coap_session_t *session, } #ifndef WITH_LWIP +#if COAP_SERVER_SUPPORT coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) { coap_endpoint_t *ep = NULL; @@ -1354,6 +1401,7 @@ coap_free_endpoint(coap_endpoint_t *ep) { coap_mfree_endpoint(ep); } } +#endif /* COAP_SERVER_SUPPORT */ #endif /* WITH_LWIP */ coap_session_t * @@ -1361,12 +1409,16 @@ coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex) { coap_session_t *s, *rtmp; - coap_endpoint_t *ep; +#if COAP_CLIENT_SUPPORT SESSIONS_ITER(ctx->sessions, s, rtmp) { if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote, remote_addr)) return s; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT + coap_endpoint_t *ep; + LL_FOREACH(ctx->endpoint, ep) { SESSIONS_ITER(ep->sessions, s, rtmp) { if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote, @@ -1374,6 +1426,7 @@ coap_session_get_by_peer(const coap_context_t *ctx, return s; } } +#endif /* COAP_SERVER_SUPPORT */ return NULL; } @@ -1416,6 +1469,7 @@ const char *coap_session_str(const coap_session_t *session) { return szSession; } +#if COAP_SERVER_SUPPORT const char *coap_endpoint_str(const coap_endpoint_t *endpoint) { static char szEndpoint[128]; char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint); @@ -1436,5 +1490,5 @@ const char *coap_endpoint_str(const coap_endpoint_t *endpoint) { return szEndpoint; } - +#endif /* ! COAP_SERVER_SUPPORT */ #endif /* COAP_SESSION_C_ */ diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 2baf03a0..637d7e25 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -206,13 +206,13 @@ get_psk_info(struct dtls_context_t *dtls_context, coap_context_t *coap_context = t_context ? t_context->coap_context : NULL; coap_session_t *coap_session; int fatal_error = DTLS_ALERT_INTERNAL_ERROR; +#if COAP_CLIENT_SUPPORT size_t identity_length; uint8_t psk[COAP_DTLS_MAX_PSK]; size_t psk_len = 0; - coap_address_t remote_addr; coap_dtls_cpsk_t *setup_cdata; - coap_dtls_spsk_t *setup_sdata; - coap_bin_const_t temp; +#endif /* COAP_CLIENT_SUPPORT */ + coap_address_t remote_addr; assert(coap_context); get_session_addr(dtls_session, &remote_addr); @@ -225,15 +225,19 @@ get_psk_info(struct dtls_context_t *dtls_context, switch (type) { case DTLS_PSK_IDENTITY: +#if COAP_CLIENT_SUPPORT if (!coap_context || !coap_context->get_client_psk || coap_session->type != COAP_SESSION_TYPE_CLIENT) goto error; setup_cdata = &coap_session->cpsk_setup_data; +#if COAP_SERVER_SUPPORT + coap_bin_const_t temp; temp.s = id; temp.length = id_len; coap_session_refresh_psk_hint(coap_session, &temp); +#endif /* COAP_SERVER_SUPPORT */ coap_log(LOG_DEBUG, "got psk_identity_hint: '%.*s'\n", (int)id_len, id ? (const char*)id : ""); @@ -275,8 +279,12 @@ get_psk_info(struct dtls_context_t *dtls_context, goto error; } return (int)identity_length; +#else /* ! COAP_CLIENT_SUPPORT */ + return 0; +#endif /* ! COAP_CLIENT_SUPPORT */ case DTLS_PSK_KEY: +#if COAP_CLIENT_SUPPORT if (coap_session->type == COAP_SESSION_TYPE_CLIENT) { if (!coap_context || !coap_context->get_client_psk) goto error; @@ -290,7 +298,10 @@ get_psk_info(struct dtls_context_t *dtls_context, } return (int)psk_len; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT if (coap_context->get_server_psk) { + coap_dtls_spsk_t *setup_sdata; setup_sdata = &coap_session->context->spsk_setup_data; if (!id) @@ -324,11 +335,14 @@ get_psk_info(struct dtls_context_t *dtls_context, return (int)coap_context->get_server_psk(coap_session, (const uint8_t*)id, id_len, (uint8_t*)result, result_length); } +#endif /* COAP_SERVER_SUPPORT */ return 0; case DTLS_PSK_HINT: +#if COAP_SERVER_SUPPORT if (coap_context->get_server_hint) return (int)coap_context->get_server_hint(coap_session, (uint8_t *)result, result_length); +#endif /* COAP_SERVER_SUPPORT */ return 0; default: @@ -489,10 +503,13 @@ coap_dtls_new_session(coap_session_t *session) { return dtls_session; } +#if COAP_SERVER_SUPPORT void *coap_dtls_new_server_session(coap_session_t *session) { return coap_dtls_new_session(session); } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT void *coap_dtls_new_client_session(coap_session_t *session) { dtls_peer_t *peer; coap_tiny_context_t *t_context = (coap_tiny_context_t *)session->context->dtls_context; @@ -522,6 +539,7 @@ void *coap_dtls_new_client_session(coap_session_t *session) { return dtls_session; } +#endif /* COAP_CLIENT_SUPPORT */ void coap_dtls_session_update_mtu(coap_session_t *session) { @@ -644,6 +662,7 @@ coap_dtls_receive(coap_session_t *session, return err; } +#if COAP_SERVER_SUPPORT int coap_dtls_hello(coap_session_t *session, const uint8_t *data, @@ -670,6 +689,7 @@ coap_dtls_hello(coap_session_t *session, } return res; } +#endif /* COAP_SERVER_SUPPORT */ unsigned int coap_dtls_get_overhead(coap_session_t *session) { (void)session; @@ -1139,6 +1159,7 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, return 0; } +#if COAP_CLIENT_SUPPORT int coap_dtls_context_set_cpsk(coap_context_t *coap_context COAP_UNUSED, coap_dtls_cpsk_t *setup_data @@ -1148,7 +1169,9 @@ coap_dtls_context_set_cpsk(coap_context_t *coap_context COAP_UNUSED, return 1; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT int coap_dtls_context_set_spsk(coap_context_t *coap_context COAP_UNUSED, coap_dtls_spsk_t *setup_data @@ -1163,6 +1186,7 @@ coap_dtls_context_set_spsk(coap_context_t *coap_context COAP_UNUSED, return 1; } +#endif /* COAP_SERVER_SUPPORT */ int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) @@ -1171,13 +1195,17 @@ coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) } #if !COAP_DISABLE_TCP +#if COAP_CLIENT_SUPPORT void *coap_tls_new_client_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED) { return NULL; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT void *coap_tls_new_server_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED) { return NULL; } +#endif /* COAP_SERVER_SUPPORT */ void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED) { } @@ -1197,6 +1225,7 @@ ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, } #endif /* !COAP_DISABLE_TCP */ +#if COAP_SERVER_SUPPORT coap_digest_ctx_t * coap_digest_setup(void) { dtls_sha256_ctx *digest_ctx = coap_malloc(sizeof(dtls_sha256_ctx)); @@ -1230,6 +1259,7 @@ coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_free(digest_ctx); return 1; } +#endif /* COAP_SERVER_SUPPORT */ #else /* !HAVE_LIBTINYDTLS */ diff --git a/src/net.c b/src/net.c index bc88254b..4cddfca9 100644 --- a/src/net.c +++ b/src/net.c @@ -294,6 +294,7 @@ coap_pop_next(coap_context_t *context) { return next; } +#if COAP_CLIENT_SUPPORT static size_t coap_get_session_client_psk( const coap_session_t *session, @@ -329,7 +330,9 @@ coap_get_session_client_psk( *identity_len = 0; return 0; } +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT static size_t coap_get_context_server_psk( const coap_session_t *session, @@ -433,6 +436,7 @@ int coap_context_set_pki(coap_context_t *ctx, } return 0; } +#endif /* ! COAP_SERVER_SUPPORT */ int coap_context_set_pki_root_cas(coap_context_t *ctx, const char *ca_file, @@ -506,6 +510,10 @@ coap_new_context( const coap_address_t *listen_addr) { coap_context_t *c; +#if ! COAP_SERVER_SUPPORT + (void)listen_addr; +#endif /* COAP_SERVER_SUPPORT */ + #ifdef WITH_CONTIKI if (initialized) return NULL; @@ -515,9 +523,6 @@ coap_new_context( #ifndef WITH_CONTIKI c = coap_malloc_type(COAP_CONTEXT, sizeof(coap_context_t)); -#endif /* not WITH_CONTIKI */ - -#ifndef WITH_CONTIKI if (!c) { coap_log(LOG_EMERG, "coap_init: malloc: failed\n"); return NULL; @@ -582,21 +587,27 @@ coap_new_context( /* set default CSM timeout */ c->csm_timeout = 30; +#if COAP_SERVER_SUPPORT if (listen_addr) { coap_endpoint_t *endpoint = coap_new_endpoint(c, listen_addr, COAP_PROTO_UDP); if (endpoint == NULL) { goto onerror; } } +#endif /* COAP_SERVER_SUPPORT */ #if !defined(WITH_LWIP) c->network_send = coap_network_send; c->network_read = coap_network_read; #endif +#if COAP_CLIENT_SUPPORT c->get_client_psk = coap_get_session_client_psk; +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT c->get_server_psk = coap_get_context_server_psk; c->get_server_hint = coap_get_context_server_hint; +#endif /* COAP_SERVER_SUPPORT */ #ifdef WITH_CONTIKI process_start(&coap_retransmit_process, (char *)c); @@ -610,9 +621,11 @@ coap_new_context( return c; +#if defined(COAP_EPOLL_SUPPORT) || COAP_SERVER_SUPPORT onerror: coap_free_type(COAP_CONTEXT, c); return NULL; +#endif /* COAP_EPOLL_SUPPORT || COAP_SERVER_SUPPORT */ } void @@ -629,15 +642,13 @@ coap_get_app_data(const coap_context_t *ctx) { void coap_free_context(coap_context_t *context) { - coap_endpoint_t *ep, *tmp; - coap_session_t *sp, *rtmp; - coap_cache_entry_t *cp, *ctmp; - if (!context) return; +#if COAP_SERVER_SUPPORT /* Removing a resource may cause a CON observe to be sent */ coap_delete_all_resources(context); +#endif /* COAP_SERVER_SUPPORT */ coap_delete_all(context->sendqueue); @@ -649,6 +660,9 @@ coap_free_context(coap_context_t *context) { #ifndef WITHOUT_ASYNC coap_delete_all_async(context); #endif /* WITHOUT_ASYNC */ +#if COAP_SERVER_SUPPORT + coap_cache_entry_t *cp, *ctmp; + HASH_ITER(hh, context->cache, cp, ctmp) { coap_delete_cache_entry(context, cp); } @@ -656,13 +670,20 @@ coap_free_context(coap_context_t *context) { coap_free(context->cache_ignore_options); } + coap_endpoint_t *ep, *tmp; + LL_FOREACH_SAFE(context->endpoint, ep, tmp) { coap_free_endpoint(ep); } +#endif /* COAP_SERVER_SUPPORT */ + +#if COAP_CLIENT_SUPPORT + coap_session_t *sp, *rtmp; SESSIONS_ITER_SAFE(context->sessions, sp, rtmp) { coap_session_release(sp); } +#endif /* COAP_CLIENT_SUPPORT */ if (context->dtls_context) coap_dtls_free_context(context->dtls_context); @@ -820,6 +841,9 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { #else if (session->state == COAP_SESSION_STATE_NONE) { +#if ! COAP_CLIENT_SUPPORT + return -1; +#else /* COAP_CLIENT_SUPPORT */ if (session->proto == COAP_PROTO_DTLS && !session->tls) { session->tls = coap_dtls_new_client_session(session); if (session->tls) { @@ -869,6 +893,7 @@ coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node) { } else { return -1; } +#endif /* COAP_CLIENT_SUPPORT */ } if (pdu->type == COAP_MESSAGE_CON && @@ -1056,14 +1081,16 @@ token_match(const uint8_t *a, size_t alen, coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu) { coap_mid_t mid = COAP_INVALID_MID; + + assert(pdu); + +#if COAP_CLIENT_SUPPORT coap_lg_crcv_t *lg_crcv = NULL; coap_opt_iterator_t opt_iter; int observe_action = -1; int have_block1 = 0; coap_opt_t *opt; - assert(pdu); - if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) { return coap_send_internal(session, pdu); } @@ -1138,7 +1165,9 @@ coap_send(coap_session_t *session, coap_pdu_t *pdu) { } send_it: +#endif /* COAP_CLIENT_SUPPORT */ mid = coap_send_internal(session, pdu); +#if COAP_CLIENT_SUPPORT if (lg_crcv) { if (mid != COAP_INVALID_MID) { LL_PREPEND(session->lg_crcv, lg_crcv); @@ -1147,6 +1176,7 @@ send_it: coap_block_delete_lg_crcv(session, lg_crcv); } } +#endif /* COAP_CLIENT_SUPPORT */ return mid; } @@ -1384,6 +1414,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { coap_session_str(node->session), node->id, node->retransmit_cnt); #endif +#if COAP_SERVER_SUPPORT /* Check if subscriptions exist that should be canceled after COAP_MAX_NOTIFY_FAILURES */ if (node->pdu->code >= 64) { @@ -1394,6 +1425,7 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { coap_handle_failed_notify(context, node->session, &token); } +#endif /* COAP_SERVER_SUPPORT */ if (node->session->con_active) { node->session->con_active--; if (node->session->state == COAP_SESSION_STATE_ESTABLISHED) { @@ -1433,9 +1465,12 @@ coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_p coap_packet_get_memmapped(packet, &data, &data_len); if (session->proto == COAP_PROTO_DTLS) { +#if COAP_SERVER_SUPPORT if (session->type == COAP_SESSION_TYPE_HELLO) result = coap_dtls_hello(session, data, data_len); - else if (session->tls) + else +#endif /* COAP_SERVER_SUPPORT */ + if (session->tls) result = coap_dtls_receive(session, data, data_len); } else if (session->proto == COAP_PROTO_UDP) { result = coap_handle_dgram(ctx, session, data, data_len); @@ -1443,6 +1478,7 @@ coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_p return result; } +#if COAP_CLIENT_SUPPORT static void coap_connect_session(coap_context_t *ctx, coap_session_t *session, @@ -1479,6 +1515,7 @@ coap_connect_session(coap_context_t *ctx, } #endif /* !COAP_DISABLE_TCP */ } +#endif /* COAP_CLIENT_SUPPORT */ static void coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) { @@ -1674,6 +1711,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) #endif /* COAP_CONSTRAINED_STACK */ } +#if COAP_SERVER_SUPPORT static int coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now) { ssize_t bytes_read = -1; @@ -1733,6 +1771,7 @@ coap_accept_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, session->last_rx_tx = now; return session != NULL; } +#endif /* COAP_SERVER_SUPPORT */ void coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { @@ -1742,9 +1781,10 @@ coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { coap_log(LOG_EMERG, "coap_io_do_io() requires libcoap not compiled for using epoll\n"); #else /* ! COAP_EPOLL_SUPPORT */ - coap_endpoint_t *ep, *tmp; coap_session_t *s, *rtmp; +#if COAP_SERVER_SUPPORT + coap_endpoint_t *ep, *tmp; LL_FOREACH_SAFE(ctx->endpoint, ep, tmp) { if ((ep->sock.flags & COAP_SOCKET_CAN_READ) != 0) coap_read_endpoint(ctx, ep, now); @@ -1764,7 +1804,9 @@ coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { coap_session_release(s); } } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) { /* Make sure the session object is not deleted in one of the callbacks */ coap_session_reference(s); @@ -1779,6 +1821,7 @@ coap_io_do_io(coap_context_t *ctx, coap_tick_t now) { } coap_session_release( s ); } +#endif /* COAP_CLIENT_SUPPORT */ #endif /* ! COAP_EPOLL_SUPPORT */ } @@ -1804,6 +1847,7 @@ coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents /* Ignore 'timer trigger' ptr which is NULL */ if (sock) { +#if COAP_SERVER_SUPPORT if (sock->endpoint) { coap_endpoint_t *endpoint = sock->endpoint; if ((sock->flags & COAP_SOCKET_WANT_READ) && @@ -1830,12 +1874,15 @@ coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents } } - else if (sock->session) { + else +#endif /* COAP_SERVER_SUPPORT */ + if (sock->session) { coap_session_t *session = sock->session; /* Make sure the session object is not deleted in one of the callbacks */ coap_session_reference(session); +#if COAP_CLIENT_SUPPORT if ((sock->flags & COAP_SOCKET_WANT_CONNECT) && (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) { sock->flags |= COAP_SOCKET_CAN_CONNECT; @@ -1844,6 +1891,7 @@ coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents coap_epoll_ctl_mod(sock, EPOLLIN, __func__); } } +#endif /* COAP_CLIENT_SUPPORT */ if ((sock->flags & COAP_SOCKET_WANT_READ) && (events[j].events & (EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) { @@ -2153,6 +2201,7 @@ coap_new_error_response(const coap_pdu_t *request, coap_pdu_code_t code, return response; } +#if COAP_SERVER_SUPPORT /** * Quick hack to determine the size of the resource description for * .well-known/core. @@ -2317,6 +2366,7 @@ error: resp->used_size = resp->token_length; return resp; } +#endif /* COAP_SERVER_SUPPORT */ /** * This function cancels outstanding messages for the session and @@ -2333,20 +2383,24 @@ coap_cancel(coap_context_t *context, const coap_queue_t *sent) { coap_binary_t token = { 0, NULL }; int num_cancelled = 0; /* the number of observers cancelled */ + (void)context; /* remove observer for this resource, if any * get token from sent and try to find a matching resource. Uh! */ COAP_SET_STR(&token, sent->pdu->token_length, sent->pdu->token); +#if COAP_SERVER_SUPPORT RESOURCES_ITER(context->resources, r) { coap_cancel_all_messages(context, sent->session, token.s, token.length); num_cancelled += coap_delete_observer(r, sent->session, &token); } +#endif /* COAP_SERVER_SUPPORT */ return num_cancelled; } +#if COAP_SERVER_SUPPORT /** * Internal flags to control the treatment of responses (specifically * in presence of the No-Response option). @@ -2888,7 +2942,9 @@ fail_response: coap_log(LOG_WARNING, "cannot send response for mid=0x%x\n", mid); } } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT static void handle_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd) { @@ -2928,6 +2984,7 @@ handle_response(coap_context_t *context, coap_session_t *session, coap_send_ack(session, rcvd); } } +#endif /* COAP_CLIENT_SUPPORT */ #if !COAP_DISABLE_TCP static void @@ -3007,6 +3064,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, if (coap_option_check_critical(context, pdu, &opt_filter) == 0) goto cleanup; +#if COAP_SERVER_SUPPORT /* if sent code was >= 64 the message might have been a * notification. Then, we must flag the observer to be alive * by setting obs->fail_cnt = 0. */ @@ -3015,6 +3073,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, { sent->pdu->token_length, sent->pdu->token }; coap_touch_observer(context, sent->session, &token); } +#endif /* COAP_SERVER_SUPPORT */ if (pdu->code == 0) { /* an empty ACK needs no further handling */ @@ -3062,6 +3121,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, session->last_ping_mid = COAP_INVALID_MID; } } +#if COAP_SERVER_SUPPORT else { /* Need to check is there is a subscription active and delete it */ RESOURCES_ITER(context->resources, r) { @@ -3076,6 +3136,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, } } } +#endif /* COAP_SERVER_SUPPORT */ goto cleanup; case COAP_MESSAGE_NON: @@ -3119,11 +3180,17 @@ coap_dispatch(coap_context_t *context, coap_session_t *session, handle_signaling(context, session, pdu); else #endif /* !COAP_DISABLE_TCP */ +#if COAP_SERVER_SUPPORT if (COAP_PDU_IS_REQUEST(pdu)) handle_request(context, session, pdu); - else if (COAP_PDU_IS_RESPONSE(pdu)) + else +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT + if (COAP_PDU_IS_RESPONSE(pdu)) handle_response(context, session, sent ? sent->pdu : NULL, pdu); - else { + else +#endif /* COAP_CLIENT_SUPPORT */ + { if (COAP_PDU_IS_EMPTY(pdu)) { if (context->ping_handler) { context->ping_handler(session, pdu, pdu->mid); @@ -3167,12 +3234,14 @@ coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *s int coap_can_exit(coap_context_t *context) { - coap_endpoint_t *ep; coap_session_t *s, *rtmp; if (!context) return 1; if (context->sendqueue) return 0; +#if COAP_SERVER_SUPPORT + coap_endpoint_t *ep; + LL_FOREACH(context->endpoint, ep) { SESSIONS_ITER(ep->sessions, s, rtmp) { if (s->delayqueue) @@ -3181,12 +3250,15 @@ coap_can_exit(coap_context_t *context) { return 0; } } +#endif /* COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT SESSIONS_ITER(context->sessions, s, rtmp) { if (s->delayqueue) return 0; if (s->lg_xmit) return 0; } +#endif /* COAP_CLIENT_SUPPORT */ return 1; } #ifndef WITHOUT_ASYNC @@ -3244,7 +3316,12 @@ void coap_cleanup(void) { void coap_register_response_handler(coap_context_t *context, coap_response_handler_t handler) { +#if COAP_CLIENT_SUPPORT context->response_handler = handler; +#else /* ! COAP_CLIENT_SUPPORT */ + (void)context; + (void)handler; +#endif /* COAP_CLIENT_SUPPORT */ } void @@ -3271,6 +3348,7 @@ coap_register_option(coap_context_t *ctx, uint16_t type) { } #if ! defined WITH_CONTIKI && ! defined WITH_LWIP && ! defined RIOT_VERSION +#if COAP_SERVER_SUPPORT int coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, const char *ifname) { @@ -3493,7 +3571,9 @@ coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, return result; } +#endif /* ! COAP_SERVER_SUPPORT */ +#if COAP_CLIENT_SUPPORT int coap_mcast_set_hops(coap_session_t *session, size_t hops) { if (session && coap_is_mcast(&session->addr_info.remote)) { @@ -3520,6 +3600,8 @@ coap_mcast_set_hops(coap_session_t *session, size_t hops) { } return 0; } +#endif /* COAP_CLIENT_SUPPORT */ + #else /* defined WITH_CONTIKI || defined WITH_LWIP */ int coap_join_mcast_group_intf(coap_context_t *ctx COAP_UNUSED, diff --git a/src/resource.c b/src/resource.c index e172a24b..91635311 100644 --- a/src/resource.c +++ b/src/resource.c @@ -10,6 +10,7 @@ #include "coap3/coap_internal.h" +#if COAP_SERVER_SUPPORT #include #include @@ -1141,3 +1142,5 @@ coap_handle_failed_notify(coap_context_t *context, coap_remove_failed_observers(context, r, session, token); } } + +#endif /* ! COAP_SERVER_SUPPORT */ diff --git a/src/subscribe.c b/src/subscribe.c index e497a880..fb043a7e 100644 --- a/src/subscribe.c +++ b/src/subscribe.c @@ -11,8 +11,10 @@ #include "coap3/coap_internal.h" +#if COAP_SERVER_SUPPORT void coap_subscription_init(coap_subscription_t *s) { assert(s); memset(s, 0, sizeof(coap_subscription_t)); } +#endif /* COAP_SERVER_SUPPORT */ diff --git a/tests/test_sendqueue.c b/tests/test_sendqueue.c index 78c75da5..31554d19 100644 --- a/tests/test_sendqueue.c +++ b/tests/test_sendqueue.c @@ -13,6 +13,7 @@ #include +#if COAP_CLIENT_SUPPORT static coap_context_t *ctx; /* Holds the coap context for most tests */ static coap_session_t *session; /* Holds a reference-counted session object */ @@ -362,4 +363,5 @@ t_init_sendqueue_tests(void) { return suite; } +#endif /* COAP_CLIENT_SUPPORT */ diff --git a/tests/test_session.c b/tests/test_session.c index 09353a98..9d58e981 100644 --- a/tests/test_session.c +++ b/tests/test_session.c @@ -11,6 +11,7 @@ #include "test_common.h" #include "test_session.h" +#if COAP_CLIENT_SUPPORT #include /* The error threshold for timeout calculations. The precision of @@ -222,3 +223,4 @@ t_init_session_tests(void) { return suite; } +#endif /* COAP_CLIENT_SUPPORT */ diff --git a/tests/test_wellknown.c b/tests/test_wellknown.c index fe27b7c6..401400f7 100644 --- a/tests/test_wellknown.c +++ b/tests/test_wellknown.c @@ -11,6 +11,8 @@ #include "test_common.h" #include "test_wellknown.h" +#if COAP_SERVER_SUPPORT +#if COAP_CLIENT_SUPPORT #include #ifdef HAVE_NETINET_IN_H #include @@ -337,4 +339,6 @@ t_init_wellknown_tests(void) { return suite; } +#endif /* COAP_CLIENT_SUPPORT */ +#endif /* COAP_SERVER_SUPPORT */ diff --git a/tests/testdriver.c b/tests/testdriver.c index 24cdc5e5..3ead6154 100644 --- a/tests/testdriver.c +++ b/tests/testdriver.c @@ -40,9 +40,13 @@ main(int argc COAP_UNUSED, char **argv COAP_UNUSED) { t_init_option_tests(); t_init_pdu_tests(); t_init_error_response_tests(); +#if COAP_CLIENT_SUPPORT t_init_session_tests(); t_init_sendqueue_tests(); +#endif /* COAP_CLIENT_SUPPORT */ +#if COAP_SERVER_SUPPORT && COAP_CLIENT_SUPPORT t_init_wellknown_tests(); +#endif /* COAP_SERVER_SUPPORT && COAP_CLIENT_SUPPORT */ t_init_tls_tests(); CU_basic_set_mode(run_mode);