diff --git a/configure.ac b/configure.ac index 18dcbf03..391ea901 100644 --- a/configure.ac +++ b/configure.ac @@ -527,6 +527,8 @@ if test "x$build_dtls" = "xyes"; then if test "x$tinydtls_libs_overridden" != "xyes"; then TinyDTLS_LIBS="$auto_TinyDTLS_LIBS" fi + # 'git submodule update' may be required + AC_DEFINE(HAVE_DTLS_SET_LOG_HANDLER, [1], [Define if TinyDTLS has dtls_set_log_handler]) else AC_MSG_ERROR([==> You want to build libcoap with DTLS support using the TinyDTLS submodule library but no suitable version could be found! Check whether you have updated the TinyDTLS git submodule, use the system-provided version if available (set '--with-submodule-tinydtls=no'), @@ -538,9 +540,7 @@ if test "x$build_dtls" = "xyes"; then AC_MSG_NOTICE([Using system-provided TinyDTLS]) tinydtls_version=`$PKG_CONFIG --modversion tinydtls` AX_CHECK_TINYDTLS_VERSION - have_gnutls="no" # don't confuse AC_MSG_RESULT at the end of the script - have_mbedtls="no" # don't confuse AC_MSG_RESULT at the end of the script - have_openssl="no" # don't confuse AC_MSG_RESULT at the end of the script + AC_CHECK_LIB(tinydtls,dtls_set_log_handler,AC_DEFINE(HAVE_DTLS_SET_LOG_HANDLER, [1], [Define if TinyDTLS has dtls_set_log_handler])) else AC_MSG_ERROR([==> You want to build libcoap with DTLS support using the TinyDTLS library but no suitable version could be found! Use the submodule TinyDTLS version (set '--with-submodule-tinydtls=yes' and update the git submodule), diff --git a/examples/coap-client.c b/examples/coap-client.c index b119c93e..80eea191 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -512,7 +512,7 @@ usage(const char *program, const char *version) { "\t\t[-m method] [-o file] [-p port] [-r] [-s duration] [-t type]\n" "\t\t[-v num] [-w] [-A type] [-B seconds] [G count] [-H hoplimit]\n" "\t\t[-K interval] [-N] [-O num,text] [-P scheme://address[:port]]\n" - "\t\t[-T token] [-U] [-X size]\n" + "\t\t[-T token] [-U] [-V num] [-X size]\n" "\t\t[[-h match_hint_file] [-k key] [-u user]]\n" "\t\t[[-c certfile] [-j keyfile] [-n] [-C cafile]\n" "\t\t[-J pkcs11_pin] [-M raw_pk] [-R trust_casfile]\n" @@ -540,8 +540,8 @@ usage(const char *program, const char *version) { "\t-s duration\tSubscribe to / Observe resource for given duration\n" "\t \t\tin seconds\n" "\t-t type\t\tContent format for given resource for PUT/POST\n" - "\t-v num \t\tVerbosity level (default 3, maximum is 9). Above 7,\n" - "\t \t\tthere is increased verbosity in GnuTLS logging\n" + "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" + "\t \t\tCoAP logging\n" "\t-w \t\tAppend a newline to received data\n" "\t-A type\t\tAccepted media type\n" "\t-B seconds\tBreak operation after waiting given seconds\n" @@ -566,6 +566,8 @@ usage(const char *program, const char *version) { "\t \t\tScheme is one of coap, coaps, coap+tcp and coaps+tcp\n" "\t-T token\tDefine the initial starting token\n" "\t-U \t\tNever include Uri-Host or Uri-Port options\n" + "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" + "\t \t\tlibrary logging\n" "\t-X size\t\tMaximum message size to use for TCP based connections\n" "\t \t\t(default is 8388864). Maximum value of 2^32 -1\n" ,program, wait_seconds); @@ -1468,7 +1470,8 @@ main(int argc, char **argv) { char port_str[NI_MAXSERV] = "0"; char node_str[NI_MAXHOST] = ""; int opt, res; - coap_log_t log_level = LOG_WARNING; + coap_log_t log_level = COAP_LOG_WARN; + coap_log_t dtls_log_level = COAP_LOG_ERR; unsigned char *user = NULL, *key = NULL; ssize_t user_length = -1, key_length = 0; int create_uri_opts = 1; @@ -1481,7 +1484,7 @@ main(int argc, char **argv) { struct sigaction sa; #endif - while ((opt = getopt(argc, argv, "a:b:c:e:f:h:j:k:l:m:no:p:rs:t:u:v:wA:B:C:G:H:J:K:L:M:NO:P:R:T:UX:")) != -1) { + while ((opt = getopt(argc, argv, "a:b:c:e:f:h:j:k:l:m:no:p:rs:t:u:v:wA:B:C:G:H:J:K:L:M:NO:P:R:T:UV:X:")) != -1) { switch (opt) { case 'a': strncpy(node_str, optarg, NI_MAXHOST - 1); @@ -1585,6 +1588,9 @@ main(int argc, char **argv) { case 'v': log_level = strtol(optarg, NULL, 10); break; + case 'V': + dtls_log_level = strtol(optarg, NULL, 10); + break; case 'l': if (!coap_debug_set_packet_loss(optarg)) { usage(argv[0], LIBCOAP_PACKAGE_VERSION); @@ -1641,8 +1647,8 @@ main(int argc, char **argv) { #endif coap_startup(); - coap_dtls_set_log_level(log_level); coap_set_log_level(log_level); + coap_dtls_set_log_level(dtls_log_level); if (optind < argc) { if (cmdline_uri(argv[optind], create_uri_opts) < 0) { diff --git a/examples/coap-rd.c b/examples/coap-rd.c index 61a50cd8..04c073dc 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -583,6 +583,7 @@ usage(const char *program, const char *version) { fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer))); fprintf(stderr, "\n" "Usage: %s [-g group] [-G group_if] [-p port] [-v num] [-A address]\n" + "\t [-V num]\n" "\t [[-h hint] [-k key]]\n" "\t [[-c certfile] [-C cafile] [-n] [-R trust_casfile]]\n" "General Options\n" @@ -592,9 +593,11 @@ usage(const char *program, const char *version) { "\t \t\tgroup. This can be different from the implied interface\n" "\t \t\tif the -A option is used\n" "\t-p port\t\tListen on specified port\n" - "\t-v num \t\tVerbosity level (default 3, maximum is 9). Above 7,\n" - "\t \t\tthere is increased verbosity in GnuTLS and OpenSSL logging\n" + "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" + "\t \t\tCoAP logging\n" "\t-A address\tInterface address to bind to\n" + "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" + "\t \t\tlibrary logging\n" "PSK Options (if supported by underlying (D)TLS library)\n" "\t-h hint\t\tIdentity Hint. Default is CoAP. Zero length is no hint\n" "\t-k key \t\tPre-Shared Key. This argument requires (D)TLS with PSK\n" @@ -781,12 +784,13 @@ main(int argc, char **argv) { char *group = NULL; char *group_if = NULL; int opt; - coap_log_t log_level = LOG_WARNING; + coap_log_t log_level = COAP_LOG_WARN; + coap_log_t dtls_log_level = COAP_LOG_ERR; #ifndef _WIN32 struct sigaction sa; #endif - while ((opt = getopt(argc, argv, "A:c:C:g:G:h:k:n:R:p:v:")) != -1) { + while ((opt = getopt(argc, argv, "A:c:C:g:G:h:k:n:R:p:v:V:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -832,6 +836,9 @@ main(int argc, char **argv) { case 'v' : log_level = strtol(optarg, NULL, 10); break; + case 'V': + dtls_log_level = strtol(optarg, NULL, 10); + break; default: usage( argv[0], LIBCOAP_PACKAGE_VERSION ); exit( 1 ); @@ -839,8 +846,8 @@ main(int argc, char **argv) { } coap_startup(); - coap_dtls_set_log_level(log_level); coap_set_log_level(log_level); + coap_dtls_set_log_level(dtls_log_level); ctx = get_context(addr_str, port_str); if (!ctx) diff --git a/examples/coap-server.c b/examples/coap-server.c index 894bbaa0..13a9cb06 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -2128,7 +2128,7 @@ usage( const char *program, const char *version) { fprintf(stderr, "\n" "Usage: %s [-d max] [-e] [-g group] [-G group_if] [-l loss] [-p port]\n" "\t\t[-r] [-v num] [-A address] [-L value] [-N]\n" - "\t\t[-P scheme://address[:port],[name1[,name2..]]] [-X size]\n" + "\t\t[-P scheme://address[:port],[name1[,name2..]]] [-V num] [-X size]\n" "\t\t[[-h hint] [-i match_identity_file] [-k key]\n" "\t\t[-s match_psk_sni_file] [-u user]]\n" "\t\t[[-c certfile] [-j keyfile] [-m] [-n] [-C cafile]\n" @@ -2157,9 +2157,8 @@ usage( const char *program, const char *version) { "\t \t\tonly '/', '/async' and '/.well-known/core' are enabled\n" "\t \t\tfor multicast requests support, otherwise all\n" "\t \t\tresources are enabled\n" - "\t-v num \t\tVerbosity level (default 3, maximum is 9). Above 7,\n" - "\t \t\tthere is increased verbosity in GnuTLS and OpenSSL\n" - "\t \t\tlogging\n" + "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" + "\t \t\tCoAP logging\n" "\t-A address\tInterface address to bind to\n" "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" @@ -2180,6 +2179,8 @@ usage( const char *program, const char *version) { "\t \t\tdefined before the leading , (comma) of the first name,\n" "\t \t\tthen the ongoing connection will be a direct connection.\n" "\t \t\tScheme is one of coap, coaps, coap+tcp and coaps+tcp\n" + "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" + "\t \t\tlibrary logging\n" "\t-X size\t\tMaximum message size to use for TCP based connections\n" "\t \t\t(default is 8388864). Maximum value of 2^32 -1\n" "PSK Options (if supported by underlying (D)TLS library)\n" @@ -2584,7 +2585,8 @@ main(int argc, char **argv) { char port_str[NI_MAXSERV] = "5683"; int opt; int mcast_per_resource = 0; - coap_log_t log_level = LOG_WARNING; + coap_log_t log_level = COAP_LOG_WARN; + coap_log_t dtls_log_level = COAP_LOG_ERR; unsigned wait_ms; coap_time_t t_last = 0; int coap_fd; @@ -2603,7 +2605,7 @@ main(int argc, char **argv) { clock_offset = time(NULL); - while ((opt = getopt(argc, argv, "c:d:eg:G:h:i:j:J:k:l:mnp:rs:u:v:A:C:L:M:NP:R:S:X:")) != -1) { + while ((opt = getopt(argc, argv, "c:d:eg:G:h:i:j:J:k:l:mnp:rs:u:v:A:C:L:M:NP:R:S:V:X:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -2724,6 +2726,9 @@ main(int argc, char **argv) { case 'v' : log_level = strtol(optarg, NULL, 10); break; + case 'V': + dtls_log_level = strtol(optarg, NULL, 10); + break; case 'X': csm_max_message_size = strtol(optarg, NULL, 10); break; @@ -2748,8 +2753,8 @@ main(int argc, char **argv) { #endif coap_startup(); - coap_dtls_set_log_level(log_level); coap_set_log_level(log_level); + coap_dtls_set_log_level(dtls_log_level); ctx = get_context(addr_str, port_str); if (!ctx) diff --git a/include/coap3/coap_debug.h b/include/coap3/coap_debug.h index 4901722d..be6cf5e5 100644 --- a/include/coap3/coap_debug.h +++ b/include/coap3/coap_debug.h @@ -26,22 +26,46 @@ #ifndef COAP_DEBUG_FD /** - * Used for output for @c LOG_DEBUG to @c LOG_ERR. + * Used for output for @c COAP_LOG_OSCORE to @c COAP_LOG_ERR. */ #define COAP_DEBUG_FD stdout #endif #ifndef COAP_ERR_FD /** - * Used for output for @c LOG_CRIT to @c LOG_EMERG. + * Used for output for @c COAP_LOG_CRIT to @c COAP_LOG_EMERG. */ #define COAP_ERR_FD stderr #endif +#define coap_log_emerg(...) coap_log(COAP_LOG_EMERG, __VA_ARGS__) +#define coap_log_alert(...) coap_log(COAP_LOG_ALERT, __VA_ARGS__) +#define coap_log_crit(...) coap_log(COAP_LOG_CRIT, __VA_ARGS__) +#define coap_log_err(...) coap_log(COAP_LOG_ERR, __VA_ARGS__) +#define coap_log_warn(...) coap_log(COAP_LOG_WARN, __VA_ARGS__) +#define coap_log_info(...) coap_log(COAP_LOG_INFO, __VA_ARGS__) +#define coap_log_notice(...) coap_log(COAP_LOG_NOTICE, __VA_ARGS__) +#define coap_log_debug(...) coap_log(COAP_LOG_DEBUG, __VA_ARGS__) +#define coap_log_oscore(...) coap_log(COAP_LOG_OSCORE, __VA_ARGS__) + /** - * Logging type. One of LOG_* from @b syslog. + * Logging type. These should be used where possible in the code instead + * of the syslog definitions, or alternatively use the coap_log_*() functions + * to reduce line length. */ -typedef int coap_log_t; +typedef enum { + COAP_LOG_EMERG = 0, + COAP_LOG_ALERT, + COAP_LOG_CRIT, + COAP_LOG_ERR, + COAP_LOG_WARN, + COAP_LOG_NOTICE, + COAP_LOG_INFO, + COAP_LOG_DEBUG, + COAP_LOG_OSCORE, + COAP_LOG_DTLS_BASE, +#define COAP_LOG_CIPHERS COAP_LOG_DTLS_BASE /* For backward compatability */ +} coap_log_t; #ifdef HAVE_SYSLOG_H #include @@ -57,53 +81,63 @@ typedef int coap_log_t; #endif /* HAVE_SYSLOG_H */ #ifndef LOG_EMERG -# define LOG_EMERG 0 +# define LOG_EMERG COAP_LOG_EMERG #endif #ifndef LOG_ALERT -# define LOG_ALERT 1 +# define LOG_ALERT COAP_LOG_ALERT #endif #ifndef LOG_CRIT -# define LOG_CRIT 2 +# define LOG_CRIT COAP_LOG_CRIT #endif #ifndef LOG_ERR -# define LOG_ERR 3 +# define LOG_ERR COAP_LOG_ERR #endif #ifndef LOG_WARNING -# define LOG_WARNING 4 +# define LOG_WARNING COAP_LOG_WARN #endif #ifndef LOG_NOTICE -# define LOG_NOTICE 5 +# define LOG_NOTICE COAP_LOG_NOTICE #endif #ifndef LOG_INFO -# define LOG_INFO 6 +# define LOG_INFO COAP_LOG_INFO #endif #ifndef LOG_DEBUG -# define LOG_DEBUG 7 +# define LOG_DEBUG COAP_LOG_DEBUG #endif -/* - LOG_DEBUG+2 gives ciphers in GnuTLS - Use COAP_LOG_CIPHERS to output Cipher Info in OpenSSL etc. - */ -#define COAP_LOG_CIPHERS (LOG_DEBUG+2) /** * Get the current logging level. * - * @return One of the LOG_* values. + * @return One of the COAP_LOG_* values. */ coap_log_t coap_get_log_level(void); /** * Sets the log level to the specified value. * - * @param level One of the LOG_* values. + * @param level One of the COAP_LOG_* values. */ void coap_set_log_level(coap_log_t level); +/** + * Sets the (D)TLS logging level to the specified @p level. + * + * @param level One of the COAP_LOG_* values. + */ +void coap_dtls_set_log_level(coap_log_t level); + +/** + * Get the current (D)TLS logging. + * + * @return One of the COAP_LOG_* values. + */ +coap_log_t coap_dtls_get_log_level(void); + /** * Logging callback handler definition. * - * @param level One of the LOG_* values. + * @param level One of the COAP_LOG_* values, or if used for (D)TLS logging, + * COAP_LOG_DTLS_BASE + one of the COAP_LOG_* values. * @param message Zero-terminated string message to log. */ typedef void (*coap_log_handler_t) (coap_log_t level, const char *message); @@ -112,6 +146,7 @@ typedef void (*coap_log_handler_t) (coap_log_t level, const char *message); * Add a custom log callback handler. * * @param handler The logging handler to use or @p NULL to use default handler. + * This handler will be used for both CoAP and (D)TLS logging. */ void coap_set_log_handler(coap_log_handler_t handler); @@ -137,13 +172,14 @@ const char *coap_package_version(void); const char *coap_package_build(void); /** - * Writes the given text to @c COAP_ERR_FD (for @p level <= @c LOG_CRIT) or @c - * COAP_DEBUG_FD (for @p level >= @c LOG_ERR). The text is output only when - * @p level is below or equal to the log level that set by coap_set_log_level(). + * Writes the given text to @c COAP_ERR_FD (for @p level <= @c COAP_LOG_CRIT) or + * @c COAP_DEBUG_FD (for @p level >= @c COAP_LOG_ERR). The text is output only + * when @p level is below or equal to the log level that set by + * coap_set_log_level(). * * Internal function. * - * @param level One of the LOG_* values. + * @param level One of the COAP_LOG_* values. & @param format The format string to use. */ #if (defined(__GNUC__)) @@ -156,11 +192,11 @@ void coap_log_impl(coap_log_t level, const char *format, ...); #ifndef coap_log /** * Logging function. - * Writes the given text to @c COAP_ERR_FD (for @p level <= @c LOG_CRIT) or @c - * COAP_DEBUG_FD (for @p level >= @c LOG_ERR). The text is output only when + * Writes the given text to @c COAP_ERR_FD (for @p level <= @c COAP_LOG_CRIT) or @c + * COAP_DEBUG_FD (for @p level >= @c COAP_LOG_ERR). The text is output only when * @p level is below or equal to the log level that set by coap_set_log_level(). * - * @param level One of the LOG_* values. + * @param level One of the COAP_LOG_* values. */ #define coap_log(level, ...) do { \ if ((int)((level))<=(int)coap_get_log_level()) \ @@ -168,6 +204,21 @@ void coap_log_impl(coap_log_t level, const char *format, ...); } while(0) #endif +#ifndef coap_dtls_log +/** + * Logging function. + * Writes the given text to @c COAP_ERR_FD (for @p level <= @c COAP_LOG_CRIT) or @c + * COAP_DEBUG_FD (for @p level >= @c COAP_LOG_ERR). The text is output only when + * @p level is below or equal to the log level that set by coap_dtls_set_log_level(). + * + * @param level One of the COAP_LOG_* values. + */ +#define coap_dtls_log(level, ...) do { \ + if ((int)((level))<=(int)coap_dtls_get_log_level()) \ + coap_log_impl((level)+COAP_LOG_DTLS_BASE, __VA_ARGS__); \ +} while(0) +#endif + #include "pdu.h" /** diff --git a/include/coap3/coap_dtls.h b/include/coap3/coap_dtls.h index 9f95018a..2e1b8605 100644 --- a/include/coap3/coap_dtls.h +++ b/include/coap3/coap_dtls.h @@ -498,22 +498,4 @@ typedef struct coap_dtls_spsk_t { /** @} */ -/** - * @ingroup logging - * Sets the (D)TLS logging level to the specified @p level. - * Note: coap_log_level() will influence output if at a specified level. - * - * @param level The logging level to use - LOG_* - */ -void coap_dtls_set_log_level(int level); - -/** - * @ingroup logging - * Get the current (D)TLS logging. - * - * @return The current log level (one of LOG_*). - */ -int coap_dtls_get_log_level(void); - - #endif /* COAP_DTLS_H */ diff --git a/man/Makefile.am b/man/Makefile.am index ecfa5f40..24fb5ac2 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -101,6 +101,20 @@ install-man: install-man3 install-man5 install-man7 @echo ".so man3/coap_context.3" > coap_context_get_session_timeout.3 @echo ".so man3/coap_context.3" > coap_context_set_csm_timeout.3 @echo ".so man3/coap_context.3" > coap_context_get_csm_timeout.3 + @echo ".so man3/coap_logging.3" > coap_log_info.3 + @echo ".so man3/coap_logging.3" > coap_log_debug.3 + @echo ".so man3/coap_logging.3" > coap_log_oscore.3 + @echo ".so man3/coap_logging.3" > coap_log_dtls.3 + @echo ".so man3/coap_logging.3" > coap_get_log_level.3 + @echo ".so man3/coap_logging.3" > coap_set_log_level.3 + @echo ".so man3/coap_logging.3" > coap_set_log_handler.3 + @echo ".so man3/coap_logging.3" > coap_dtls_log.3 + @echo ".so man3/coap_logging.3" > coap_dtls_get_log_level.3 + @echo ".so man3/coap_logging.3" > coap_dtls_set_log_level.3 + @echo ".so man3/coap_logging.3" > coap_package_name.3 + @echo ".so man3/coap_logging.3" > coap_package_version.3 + @echo ".so man3/coap_logging.3" > coap_package_build.3 + @echo ".so man3/coap_logging.3" > coap_set_show_pdu_output.3 @echo ".so man3/coap_logging.3" > coap_show_pdu.3 @echo ".so man3/coap_logging.3" > coap_endpoint_str.3 @echo ".so man3/coap_logging.3" > coap_session_str.3 diff --git a/man/coap-client.txt.in b/man/coap-client.txt.in index a031b854..b2e6a1f7 100644 --- a/man/coap-client.txt.in +++ b/man/coap-client.txt.in @@ -24,7 +24,7 @@ SYNOPSIS [*-t* type] [*-v* num] [*-w*] [*-A* type] [*-B* seconds] [*-G* count] [*-H* hoplimit] [*-K* interval] [*-L* value] [*-N*] [*-O* num,text] [*-P* scheme://addr[:port]] [*-T* token] [*-U*] - [*-X* size] + [*-V* num] [*-X* size] [[*-h* match_hint_file] [*-k* key] [*-u* user]] [[*-c* certfile] [*-j* keyfile] [-n] [*-C* cafile] [*-J* pkcs11_pin] [*-M* rpk_file] [*-R* trust_casfile]] URI @@ -113,8 +113,8 @@ OPTIONS - General application/cbor (cbor) *-v* num:: - The verbosity level to use (default 3, maximum is 9). Above 7, there is - increased verbosity in GnuTLS and OpenSSL logging. + The verbosity level to use (default 4, maximum is 8) for general + CoAP logging. *-w*:: Append a newline to received data. @@ -166,6 +166,10 @@ OPTIONS - General *-U* :: Never include Uri-Host or Uri-Port options. +*-V* num:: + The verbosity level to use (default 3, maximum is 7) for (D)TLS + library logging. + *-X* size:: Maximum message size to use for TCP based connections (default is 8388864). Maximum value of 2^32 -1. diff --git a/man/coap-rd.txt.in b/man/coap-rd.txt.in index 14dc1515..0726ba28 100644 --- a/man/coap-rd.txt.in +++ b/man/coap-rd.txt.in @@ -20,6 +20,7 @@ coap-rd-notls SYNOPSIS -------- *coap-rd* [*-g* group] [*-G* group_if] [*-p* port] [*-v* num] [*-A* address] + [*-V* num] [[*-h* hint] [*-k* key]] [[*-c* certfile] [*-n*] [*-C* cafile] [*-R* trusted_casfile]] @@ -50,12 +51,16 @@ OPTIONS The default port is 5683 if not given any other value. *-v* num:: - The verbosity level to use (default: 3, maximum is 9). Above 7, there is - increased verbosity in GnuTLS and OpenSSL logging. + The verbosity level to use (default 4, maximum is 8) for general + CoAP logging. *-A* address:: The local address of the interface which the server has to listen on. +*-V* num:: + The verbosity level to use (default 3, maximum is 7) for (D)TLS + library logging. + OPTIONS - PSK ------------- (If supported by underlying (D)TLS library) diff --git a/man/coap-server.txt.in b/man/coap-server.txt.in index 27726690..692fa552 100644 --- a/man/coap-server.txt.in +++ b/man/coap-server.txt.in @@ -21,7 +21,7 @@ SYNOPSIS -------- *coap-server* [*-d* max] [*-e*] [*-g* group] [*-G* group_if] [*-l* loss] [*-p* port] [-r] [*-v* num] [*-A* address] [*-L* value] [*-N*] - [*-P* scheme://addr[:port],[name1[,name2..]]] [*-X* size] + [*-P* scheme://addr[:port],[name1[,name2..]]] [*-V* num] [*-X* size] [[*-h* hint] [*-i* match_identity_file] [*-k* key] [*-s* match_psk_sni_file] [*-u* user]] [[*-c* certfile] [*-j* keyfile] [*-n*] [*-C* cafile] @@ -76,8 +76,8 @@ OPTIONS - General otherwise all resources are enabled. *-v* num:: - The verbosity level to use (default 3, maximum is 9). Above 7, there is - increased verbosity in GnuTLS and OpenSSL logging. + The verbosity level to use (default 4, maximum is 8) for general + CoAP logging. *-A* address:: The local address of the interface which the server has to listen on. @@ -105,6 +105,10 @@ OPTIONS - General first name, then the ongoing connection will be a direct connection. Scheme is one of coap, coaps, coap+tcp and coaps+tcp. +*-V* num:: + The verbosity level to use (default 3, maximum is 7) for (D)TLS + library logging. + *-X* size:: Maximum message size to use for TCP based connections (default is 8388864). Maximum value of 2^32 -1. diff --git a/man/coap_logging.txt.in b/man/coap_logging.txt.in index 5a10d5e9..e0aa60f0 100644 --- a/man/coap_logging.txt.in +++ b/man/coap_logging.txt.in @@ -12,8 +12,20 @@ NAME ---- coap_logging, coap_log, +coap_log_emerg, +coap_log_alert, +coap_log_crit, +coap_logi_err, +coap_log_warn, +coap_log_info, +coap_log_notice, +coap_log_debug, +coap_log_oscore, coap_get_log_level, coap_set_log_level, +coap_dtls_log, +coap_dtls_get_log_level, +coap_dtls_set_log_level, coap_set_log_handler, coap_package_name, coap_package_version, @@ -28,15 +40,35 @@ SYNOPSIS -------- *#include * -*void coap_log(coap_log_t _level_, const char *_format_, ...);* +*void coap_log(coap_log_t _level_, const char *_format_, _..._);* + +*void coap_log_emerg(const char *_format_, _..._);* + +*void coap_log_alert(const char *_format_, _..._);* + +*void coap_log_crit(const char *_format_, _..._);* + +*void coap_log_err(const char *_format_, _..._);* + +*void coap_log_warn(const char *_format_, _..._);* + +*void coap_log_info(const char *_format_, _..._);* + +*void coap_log_notice(const char *_format_, _..._);* + +*void coap_log_debug(const char *_format_, _..._);* + +*void coap_log_oscore(const char *_format_, _..._);* *void coap_set_log_level(coap_log_t _level_);* *coap_log_t coap_get_log_level(void);* -*void coap_dtls_set_log_level(int _level_);* +*void coap_dtls_log(coap_log_t _level_, const char *_format_, _..._);* -*int coap_dtls_get_log_level(void);* +*void coap_dtls_set_log_level(coap_log_t _level_);* + +*coap_log_t coap_dtls_get_log_level(void);* *void coap_set_log_handler(coap_log_handler_t _handler_);* @@ -69,54 +101,133 @@ Logging by default is to stderr or stdout depending on the logging level of the log entry. It ia possible to send the logging information to an application logging callback handler for processing by the application. +Logging levels (*coap_log_t*) are defined by (initially the same as for +*syslog*()), which are numerically incrementing in value: + +*COAP_LOG_EMERG*:: +Emergency level (0). + +*COAP_LOG_ALERT*:: +Alert level (1). + +*COAP_LOG_CRIT*:: +Critical level (2). + +*COAP_LOG_ERR*:: +Error level (3). + +*COAP_LOG_WARN*:: +Warning level (the default) (4). + +*COAP_LOG_NOTICE*:: +Notice level (5). + +*COAP_LOG_INFO*:: +Information level (6). + +*COAP_LOG_DEBUG*:: +Debug level (7). + +With an additional level: + +*COAP_LOG_OSCORE*:: +Debug OSCORE information (8). + +FUNCTIONS +--------- + +*Function: coap_log()* + The *coap_log*() function is used to log information at the appropriate _level_. The rest of the parameters follow the standard *printf*() function format. -Logging levels (*coap_log_t*) are defined by (the same as for *syslog*()), which -are numerically incrementing in value: +*Function: coap_log_emerg()* -*LOG_EMERG*:: -Emergency level (0). +The *coap_log_emerg*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_EMERG. +The parameters follow the standard *printf*() function format. -*LOG_ALERT*:: -Alert level (1). +*Function: coap_log_alert()* -*LOG_CRIT*:: -Critical level (2). +The *coap_log_alert*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_ALERT. +The parameters follow the standard *printf*() function format. -*LOG_ERR*:: -Error level (3). +*Function: coap_log_crit()* -*LOG_WARNING*:: -Warning level (the default) (4). +The *coap_log_crit*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_CRIT. +The parameters follow the standard *printf*() function format. -*LOG_NOTICE*:: -Notice level (5). +*Function: coap_log_err()* -*LOG_INFO*:: -Information level (6). +The *coap_log_err*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_ERR. +The parameters follow the standard *printf*() function format. -*LOG_DEBUG*:: -Debug level (7). +*Function: coap_log_warn()* + +The *coap_log_warn*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_WARN. +The parameters follow the standard *printf*() function format. + +*Function: coap_log_notice()* + +The *coap_log_notice*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_NOTICE. +The parameters follow the standard *printf*() function format. + +*Function: coap_log_info()* + +The *coap_log_info*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_INFO. +The parameters follow the standard *printf*() function format. + +*Function: coap_log_debug()* + +The *coap_log_debug*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_DEBUG. +The parameters follow the standard *printf*() function format. + +*Function: coap_log_oscore()* + +The *coap_log_oscore*() function provides a wrapper to the *coap_log*() function +with _level_ set to COAP_LOG_OSCORE. +The parameters follow the standard *printf*() function format. + +*Function: coap_set_log_level()* The *coap_set_log_level*() function is used to set the current logging _level_ for output by any subsequent *coap_log*() calls. Output is only logged if the *coap_log*() _level_ definition is smaller than or equal to the current logging _level_. +*Function: coap_get_log_level()* + The *coap_get_log_level*() function is used to get the current logging level. -The *coap_dtls_set_log_level*() function is used to set the logging _level_ -for output by the DTLS library for specific DTLS information. Usually, both -*coap_set_log_level*() and *coap_dtls_set_log_level*() would be set to the -same _level_ value. If the logging level is set to greater than LOG_DEBUG, -the underlying TLS library may get more verbose in its output. +*Function: coap_dtls_log()* + +The *coap_dtls_log*() function is used to log (D)TLS library information at the +appropriate _level_. The rest of the parameters follow the standard *printf*() +function format. + +*Function: coap_dtls_set_log_level()* + +The *coap_dtls_set_log_level*() function is used to set the current logging +_level_ for output by any subsequent *coap_dtls_log*() calls. Output is only +logged if the *coap_dtls_log*() _level_ definition is smaller than or equal to +the current DTLS logging _level_. + +*Function: coap_dtls_get_log_level()* The *coap_dtls_get_log_level*() function is used to get the current logging level for the DTLS library. +*Function: coap_set_log_handler()* + The *coap_set_log_handler*()* function can be used to define an alternative -logging handler for processing the logging messages. The logging handler +logging handler for processing any logging messages. The logging handler prototype is defined as: [source, c] @@ -124,26 +235,40 @@ prototype is defined as: typedef void (*coap_log_handler_t) (coap_log_t level, const char *message); ---- +*Function: coap_package_name()* + The *coap_package_name*() function returns the name of this library. +*Function: coap_package_version()* + The *coap_package_version*() function returns the version of this library. +*Function: coap_package_build()* + The *coap_package_build*() function returns the git information (as in "git describe --tags") for the build of this library or version of this library if git is not used. +*Function: coap_set_show_pdu_output()* + The *coap_set_show_pdu_output*() function defines whether the output from *coap_show_pdu*() is to be either sent to stdout/stderr, or output using *coap_log*(). _use_fprintf_ is set to 1 for stdout/stderr (the default), and _use_fprintf_ is set to 0 for *coap_log*(). +*Function: coap_show_pdu()* + The *coap_show_pdu*() function is used to decode the _pdu_, outputting as appropriate for logging _level_. Where the output goes is dependent on *coap_set_show_pdu_output*(). +*Function: coap_endpoint_str()* + The *coap_endpoint_str*() function returns a description string of the _endpoint_. +*Function: coap_session_str()* + The *coap_session_str*() function is used to get a string containing the information about the _session_. @@ -171,8 +296,11 @@ SEE ALSO FURTHER INFORMATION ------------------- -See "RFC7252: The Constrained Application Protocol (CoAP)" for further -information. +See + +"https://tools.ietf.org/html/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" + +for further information. BUGS ---- diff --git a/man/examples-code-check.c b/man/examples-code-check.c index 6a55d589..7d005673 100644 --- a/man/examples-code-check.c +++ b/man/examples-code-check.c @@ -59,6 +59,16 @@ const char *define_list[] = { "coap_string_equal(", "coap_binary_equal(", "coap_log(", + "coap_log_emerg(", + "coap_log_alert(", + "coap_log_crit(", + "coap_log_err(", + "coap_log_warn(", + "coap_log_info(", + "coap_log_notice(", + "coap_log_debug(", + "coap_log_oscore(", + "coap_dtls_log(", }; const char *struct_list[] = { diff --git a/src/coap_debug.c b/src/coap_debug.c index 47633acd..1b799ec0 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -47,7 +47,7 @@ #include "net/ip/uip-debug.h" #endif -static coap_log_t maxlog = LOG_WARNING; /* default maximum log level */ +static coap_log_t maxlog = COAP_LOG_WARN; /* default maximum CoAP log level */ static int use_fprintf_for_show_pdu = 1; /* non zero to output with fprintf */ @@ -82,9 +82,13 @@ coap_set_log_level(coap_log_t level) { maxlog = level; } -/* this array has the same order as the type log_t */ +/* this array has the same order as the type coap_log_t with the (D)TLS + entries added to the list with a COAP_LOG_DTLS_BASE offset */ static const char *loglevels[] = { - "EMRG", "ALRT", "CRIT", "ERR ", "WARN", "NOTE", "INFO", "DEBG", "????", "CIPH" + /* General logging */ + "EMRG", "ALRT", "CRIT", "ERR ", "WARN", "NOTE", "INFO", "DEBG", "OSC ", + /* (D)TLS logging */ + "Emrg", "Alrt", "Crit", "Err ", "Warn", "Note", "Info", "Debg" }; #ifdef HAVE_TIME_H @@ -505,6 +509,7 @@ print_content_format(unsigned int format_type, { COAP_MEDIATYPE_APPLICATION_JSON, "application/json" }, { COAP_MEDIATYPE_APPLICATION_CBOR, "application/cbor" }, { COAP_MEDIATYPE_APPLICATION_CWT, "application/cwt" }, + { COAP_MEDIATYPE_APPLICATION_COAP_GROUP_JSON, "application/coap-group+json" }, { COAP_MEDIATYPE_APPLICATION_COSE_SIGN, "application/cose; cose-type=\"cose-sign\"" }, { COAP_MEDIATYPE_APPLICATION_COSE_SIGN1, "application/cose; cose-type=\"cose-sign1\"" }, { COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT, "application/cose; cose-type=\"cose-encrypt\"" }, @@ -521,8 +526,8 @@ print_content_format(unsigned int format_type, { COAP_MEDIATYPE_APPLICATION_SENSML_EXI, "application/sensml-exi" }, { COAP_MEDIATYPE_APPLICATION_SENML_XML, "application/senml+xml" }, { COAP_MEDIATYPE_APPLICATION_SENSML_XML, "application/sensml+xml" }, - { COAP_MEDIATYPE_APPLICATION_COAP_GROUP_JSON, "application/coap-group+json" }, { COAP_MEDIATYPE_APPLICATION_DOTS_CBOR, "application/dots+cbor" }, + { COAP_MEDIATYPE_APPLICATION_ACE_CBOR, "application/ace+cbor" }, { 75, "application/dcaf+cbor" } }; @@ -967,9 +972,6 @@ void coap_set_log_handler(coap_log_handler_t handler) { void coap_log_impl(coap_log_t level, const char *format, ...) { - if (maxlog < level) - return; - if (log_handler) { #if COAP_CONSTRAINED_STACK static coap_mutex_t static_log_mutex = COAP_MUTEX_INITIALIZER; @@ -980,7 +982,7 @@ coap_log_impl(coap_log_t level, const char *format, ...) { va_list ap; va_start(ap, format); #if COAP_CONSTRAINED_STACK - coap_mutex_lock(&static_log_mutex); + coap_mutex_lock(&static_log_mutex); #endif /* COAP_CONSTRAINED_STACK */ vsnprintf( message, sizeof(message), format, ap); @@ -1003,8 +1005,11 @@ coap_log_impl(coap_log_t level, const char *format, ...) { if (len) fprintf(log_fd, "%.*s ", (int)len, timebuf); - if (level <= COAP_LOG_CIPHERS) + if (level > sizeof(loglevels)/sizeof(loglevels[0])) { + fprintf(log_fd, "%4d ", level); + } else { fprintf(log_fd, "%s ", loglevels[level]); + } va_start(ap, format); vfprintf(log_fd, format, ap); diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index ef7a4440..6fb52800 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -165,7 +165,7 @@ typedef enum coap_free_bye_t { G_CHECK(xx, func); \ } while 0 -static int dtls_log_level = 0; +static coap_log_t dtls_log_level = 0; #if COAP_SERVER_SUPPORT static int post_client_hello_gnutls_pki(gnutls_session_t g_session); @@ -278,10 +278,12 @@ coap_gnutls_audit_log_func(gnutls_session_t g_session, const char* text) static void coap_gnutls_log_func(int level, const char* text) { - /* debug logging in gnutls starts at 2 */ - if (level > 2) - level = 2; - coap_log(LOG_DEBUG + level - 2, "%s", text); + /* Things get noisy, even at level 1 */ + if (level > 0) + level += COAP_LOG_WARN; + if (level > COAP_LOG_DEBUG) + level = COAP_LOG_DEBUG; + coap_dtls_log(level, "%s", text); } /* @@ -451,21 +453,15 @@ coap_dtls_get_tls(const coap_session_t *c_session, } void -coap_dtls_set_log_level(int level) { +coap_dtls_set_log_level(coap_log_t level) { dtls_log_level = level; - if (level - LOG_DEBUG >= -2) { - /* debug logging in gnutls starts at 2 */ - gnutls_global_set_log_level(2 + level - LOG_DEBUG); - } - else { - gnutls_global_set_log_level(0); - } + gnutls_global_set_log_level(dtls_log_level); } /* * return current logging level */ -int +coap_log_t coap_dtls_get_log_level(void) { return dtls_log_level; } diff --git a/src/coap_mbedtls.c b/src/coap_mbedtls.c index 9819543f..5c570f5f 100644 --- a/src/coap_mbedtls.c +++ b/src/coap_mbedtls.c @@ -1362,8 +1362,10 @@ reset: static void mbedtls_debug_out(void *ctx COAP_UNUSED, int level, - const char *file, int line, const char *str) { - int log_level; + const char *file COAP_UNUSED, + int line COAP_UNUSED, const char *str) { + + coap_log_t coap_level = COAP_LOG_DEBUG; /* * 0 No debug * 1 Error @@ -1372,20 +1374,24 @@ mbedtls_debug_out(void *ctx COAP_UNUSED, int level, * 4 Verbose */ switch (level) { - case 4: - case 3: - case 2: - log_level = COAP_LOG_CIPHERS; + case 0: + coap_level = COAP_LOG_EMERG; break; case 1: - log_level = LOG_ERR; + coap_level = COAP_LOG_WARN; break; - case 0: + case 2: + coap_level = COAP_LOG_NOTICE; + break; + case 3: + coap_level = COAP_LOG_INFO; + break; + case 4: default: - log_level = 0; + coap_level = COAP_LOG_DEBUG; break; } - coap_log(log_level, "%s:%04d: %s", file, line, str); + coap_dtls_log(coap_level, "%s", str); } #if !COAP_DISABLE_TCP @@ -1429,7 +1435,7 @@ coap_sock_read(void *ctx, unsigned char *out, size_t outl) { ret = MBEDTLS_ERR_NET_CONN_RESET; } #else - if (errno == EAGAIN) { + if (errno == EAGAIN || errno == EINTR) { ret = MBEDTLS_ERR_SSL_WANT_READ; } else if (errno == EPIPE || errno == ECONNRESET) { @@ -1660,8 +1666,8 @@ coap_dtls_rpk_is_supported(void) { return 0; } -void *coap_dtls_new_context(coap_context_t *c_context) -{ +void * +coap_dtls_new_context(coap_context_t *c_context) { coap_mbedtls_context_t *m_context; (void)c_context; @@ -2479,10 +2485,10 @@ coap_dtls_get_tls(const coap_session_t *c_session, return NULL; } -static int keep_log_level = 0; +static coap_log_t keep_log_level = COAP_LOG_EMERG; -void coap_dtls_set_log_level(int level) -{ +void +coap_dtls_set_log_level(coap_log_t level) { #if !defined(ESPIDF_VERSION) int use_level; /* @@ -2493,20 +2499,34 @@ void coap_dtls_set_log_level(int level) * 3 Informational * 4 Verbose */ - - if (level <= LOG_ERR) { + switch ((int)level) { + case COAP_LOG_EMERG: + use_level = 0; + break; + case COAP_LOG_ALERT: + case COAP_LOG_CRIT: + case COAP_LOG_ERR: + case COAP_LOG_WARN: use_level = 1; - } - else { - use_level = (level >= LOG_DEBUG) ? level - LOG_DEBUG + 2 : 0; + break; + case COAP_LOG_NOTICE: + use_level = 2; + break; + case COAP_LOG_INFO: + use_level = 3; + break; + case COAP_LOG_DEBUG: + default: + use_level = 4; + break; } mbedtls_debug_set_threshold(use_level); #endif /* !ESPIDF_VERSION) */ keep_log_level = level; } -int coap_dtls_get_log_level(void) -{ +coap_log_t +coap_dtls_get_log_level(void) { return keep_log_level; } diff --git a/src/coap_notls.c b/src/coap_notls.c index 1a6cf0e7..f0242356 100644 --- a/src/coap_notls.c +++ b/src/coap_notls.c @@ -113,7 +113,7 @@ coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) return 0; } -static int dtls_log_level = 0; +static coap_log_t dtls_log_level = COAP_LOG_EMERG; void coap_dtls_startup(void) { } @@ -130,11 +130,11 @@ void coap_dtls_shutdown(void) { } void -coap_dtls_set_log_level(int level) { +coap_dtls_set_log_level(coap_log_t level) { dtls_log_level = level; } -int +coap_log_t coap_dtls_get_log_level(void) { return dtls_log_level; } diff --git a/src/coap_openssl.c b/src/coap_openssl.c index cd1ea4e8..7761f6e1 100644 --- a/src/coap_openssl.c +++ b/src/coap_openssl.c @@ -280,13 +280,17 @@ coap_dtls_get_tls(const coap_session_t *c_session, return NULL; } -static int dtls_log_level = 0; +/* + * Logging levels use the standard CoAP logging levels + */ +static coap_log_t dtls_log_level = COAP_LOG_EMERG; -void coap_dtls_set_log_level(int level) { +void coap_dtls_set_log_level(coap_log_t level) { dtls_log_level = level; } -int coap_dtls_get_log_level(void) { +coap_log_t +coap_dtls_get_log_level(void) { return dtls_log_level; } @@ -635,45 +639,44 @@ static void coap_dtls_info_callback(const SSL *ssl, int where, int ret) { pstr = "undefined"; if (where & SSL_CB_LOOP) { - if (dtls_log_level >= LOG_DEBUG) - coap_log(LOG_DEBUG, "* %s: %s:%s\n", + coap_dtls_log(COAP_LOG_DEBUG, "* %s: %s:%s\n", coap_session_str(session), pstr, SSL_state_string_long(ssl)); } else if (where & SSL_CB_ALERT) { - int log_level = LOG_INFO; + coap_log_t log_level = COAP_LOG_INFO; pstr = (where & SSL_CB_READ) ? "read" : "write"; if ((where & (SSL_CB_WRITE|SSL_CB_READ)) && (ret >> 8) == SSL3_AL_FATAL) { session->dtls_event = COAP_EVENT_DTLS_ERROR; if ((ret & 0xff) != SSL3_AD_CLOSE_NOTIFY) - log_level = LOG_WARNING; + log_level = COAP_LOG_WARN; } - if (dtls_log_level >= log_level) - coap_log(log_level, "* %s: SSL3 alert %s:%s:%s\n", - coap_session_str(session), - pstr, - SSL_alert_type_string_long(ret), - SSL_alert_desc_string_long(ret)); + /* Need to let CoAP logging know why this session is dying */ + coap_log(log_level, "* %s: SSL3 alert %s:%s:%s\n", + coap_session_str(session), + pstr, + SSL_alert_type_string_long(ret), + SSL_alert_desc_string_long(ret)); } else if (where & SSL_CB_EXIT) { if (ret == 0) { - if (dtls_log_level >= LOG_WARNING) { + if (dtls_log_level >= COAP_LOG_WARN) { unsigned long e; - coap_log(LOG_WARNING, "* %s: %s:failed in %s\n", + coap_dtls_log(COAP_LOG_WARN, "* %s: %s:failed in %s\n", coap_session_str(session), pstr, SSL_state_string_long(ssl)); while ((e = ERR_get_error())) - coap_log(LOG_WARNING, "* %s: %s%s\n", + coap_dtls_log(COAP_LOG_WARN, "* %s: %s%s\n", coap_session_str(session), ERR_reason_error_string(e), ssl_function_definition(e)); } } else if (ret < 0) { - if (dtls_log_level >= LOG_WARNING) { + if (dtls_log_level >= COAP_LOG_WARN) { int err = SSL_get_error(ssl, ret); if (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE && err != SSL_ERROR_WANT_CONNECT && err != SSL_ERROR_WANT_ACCEPT && err != SSL_ERROR_WANT_X509_LOOKUP) { long e; - coap_log(LOG_WARNING, "* %s: %s:error in %s\n", + coap_dtls_log(COAP_LOG_WARN, "* %s: %s:error in %s\n", coap_session_str(session), pstr, SSL_state_string_long(ssl)); while ((e = ERR_get_error())) - coap_log(LOG_WARNING, "* %s: %s%s\n", + coap_dtls_log(COAP_LOG_WARN, "* %s: %s%s\n", coap_session_str(session), ERR_reason_error_string(e), ssl_function_definition(e)); } @@ -812,8 +815,7 @@ void *coap_dtls_new_context(coap_context_t *coap_context) { coap_set_user_prefs(context->dtls.ctx); memset(cookie_secret, 0, sizeof(cookie_secret)); if (!RAND_bytes(cookie_secret, (int)sizeof(cookie_secret))) { - if (dtls_log_level >= LOG_WARNING) - coap_log(LOG_WARNING, + coap_dtls_log(COAP_LOG_WARN, "Insufficient entropy for random cookie generation"); coap_prng(cookie_secret, sizeof(cookie_secret)); } @@ -2058,7 +2060,7 @@ tls_secret_call_back(SSL *ssl, for (ii = 0; ii < sk_SSL_CIPHER_num (peer_ciphers); ii++) { const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii); - coap_log(COAP_LOG_CIPHERS, "Client cipher: %s\n", + coap_dtls_log(COAP_LOG_INFO, "Client cipher: %s\n", SSL_CIPHER_get_name(peer_cipher)); if (strstr (SSL_CIPHER_get_name (peer_cipher), "PSK")) { psk_requested = 1; @@ -2380,9 +2382,10 @@ tls_client_hello_call_back(SSL *ssl, for (ii = 0; ii < sk_SSL_CIPHER_num (peer_ciphers); ii++) { const SSL_CIPHER *peer_cipher = sk_SSL_CIPHER_value(peer_ciphers, ii); - coap_log(COAP_LOG_CIPHERS, "Client cipher: %s (%04x)\n", - SSL_CIPHER_get_name(peer_cipher), - SSL_CIPHER_get_protocol_id(peer_cipher)); + coap_dtls_log(COAP_LOG_INFO, + "Client cipher: %s (%04x)\n", + SSL_CIPHER_get_name(peer_cipher), + SSL_CIPHER_get_protocol_id(peer_cipher)); if (strstr (SSL_CIPHER_get_name (peer_cipher), "PSK")) { psk_requested = 1; break; @@ -3184,7 +3187,7 @@ int coap_dtls_receive(coap_session_t *session, int err = SSL_get_error(ssl, r); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { if (in_init && SSL_is_init_finished(ssl)) { - coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_connected(session); @@ -3430,7 +3433,7 @@ ssize_t coap_tls_write(coap_session_t *session, int err = SSL_get_error(ssl, r); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { if (in_init && SSL_is_init_finished(ssl)) { - coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); @@ -3458,7 +3461,7 @@ ssize_t coap_tls_write(coap_session_t *session, r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { - coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); @@ -3495,7 +3498,7 @@ ssize_t coap_tls_read(coap_session_t *session, int err = SSL_get_error(ssl, r); if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) { if (in_init && SSL_is_init_finished(ssl)) { - coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); @@ -3521,7 +3524,7 @@ ssize_t coap_tls_read(coap_session_t *session, r = -1; } } else if (in_init && SSL_is_init_finished(ssl)) { - coap_log(COAP_LOG_CIPHERS, "* %s: Using cipher: %s\n", + coap_dtls_log(COAP_LOG_INFO, "* %s: Using cipher: %s\n", coap_session_str(session), SSL_get_cipher_name(ssl)); coap_handle_event(session->context, COAP_EVENT_DTLS_CONNECTED, session); coap_session_send_csm(session); diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 3c8de98d..340645a7 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -97,11 +97,53 @@ coap_dtls_rpk_is_supported(void) { #endif /* ! DTLS_ECC */ } +static coap_log_t +dtls_map_logging(log_t d_level) { + /* DTLS_LOG_ERR is missing, so account for the gap */ + switch (d_level) { + case DTLS_LOG_EMERG: + return COAP_LOG_EMERG; + break; + case DTLS_LOG_ALERT: + return COAP_LOG_ALERT; + break; + case DTLS_LOG_CRIT: + return COAP_LOG_CRIT; + break; + case DTLS_LOG_WARN: + return COAP_LOG_WARN; + break; + case DTLS_LOG_NOTICE: + return COAP_LOG_NOTICE; + break; + case DTLS_LOG_INFO: + return COAP_LOG_INFO; + break; + case DTLS_LOG_DEBUG: + default: + return COAP_LOG_DEBUG; + break; + } + return COAP_LOG_DEBUG; +} +#ifdef HAVE_DTLS_SET_LOG_HANDLER +/* Valid after TinyDTLS submodule has been updated */ +static void +dtls_logging(log_t d_level, const char *message) { + coap_log_t c_level = dtls_map_logging(d_level); + + coap_dtls_log(c_level, "%s", message); +} +#endif /* HAVE_DTLS_SET_LOG_HANDLER */ void coap_dtls_startup(void) { dtls_init(); dtls_ticks(&dtls_tick_0); coap_ticks(&coap_tick_0); +#ifdef HAVE_DTLS_SET_LOG_HANDLER + /* Valid after TinyDTLS submodule has been updated */ + dtls_set_log_handler(dtls_logging); +#endif /* HAVE_DTLS_SET_LOG_HANDLER */ } void coap_dtls_shutdown(void) { @@ -122,16 +164,49 @@ coap_dtls_get_tls(const coap_session_t *c_session, } void -coap_dtls_set_log_level(int level) { - dtls_set_log_level(level); +coap_dtls_set_log_level(coap_log_t c_level) { + log_t d_level; + + /* DTLS_LOG_ERR is missing, so account for the gap */ + switch (c_level) { + case COAP_LOG_EMERG: + d_level = DTLS_LOG_EMERG; + break; + case COAP_LOG_ALERT: + d_level = DTLS_LOG_ALERT; + break; + case COAP_LOG_CRIT: + d_level = DTLS_LOG_CRIT; + break; + case COAP_LOG_ERR: + case COAP_LOG_WARN: + d_level = DTLS_LOG_WARN; + break; + case COAP_LOG_NOTICE: + d_level = DTLS_LOG_NOTICE; + break; + case COAP_LOG_INFO: + d_level = DTLS_LOG_INFO; + break; + case COAP_LOG_DEBUG: + case COAP_LOG_OSCORE: + case COAP_LOG_DTLS_BASE: + default: + d_level = DTLS_LOG_DEBUG; + break; + } + dtls_set_log_level(d_level); } -int +coap_log_t coap_dtls_get_log_level(void) { - return dtls_get_log_level(); + log_t d_level = dtls_get_log_level(); + + return dtls_map_logging(d_level); } -static void get_session_addr(const session_t *s, coap_address_t *a) { +static void +get_session_addr(const session_t *s, coap_address_t *a) { #if defined(WITH_CONTIKI) || defined(WITH_LWIP) a->addr = s->addr; a->port = s->port; @@ -149,7 +224,8 @@ static void get_session_addr(const session_t *s, coap_address_t *a) { #endif /* ! WITH_CONTIKI && ! WITH_LWIP */ } -static void put_session_addr(const coap_address_t *a, session_t *s) { +static void +put_session_addr(const coap_address_t *a, session_t *s) { #if defined(WITH_CONTIKI) || defined(WITH_LWIP) s->size = (unsigned char)sizeof(s->addr); s->addr = a->addr;