-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update comms logs with new logging API #1169
Changes from 1 commit
21d419c
31737b7
50a290d
da14848
18f1490
d42d8d9
21ae9d3
22fcadd
6fd81fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,17 +17,19 @@ | |
****************************************************************************** | ||
*/ | ||
|
||
#include "logging.h" | ||
LOG_SOURCE_CATEGORY("message_channel.dtls") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was hoping to not have to hard code the parent category, but I do like the idea of knowing the logs are a sub category of the communication library. Is it fair to say all files within the communication/ folder will have a default parent category of "comm" ? That's probably a better way of keeping log messages filterable by "comm" as well ;-) I'll make this change in a couple places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if category name is not defined explicitly for a log statement's scope, upper level category name is used, up to category specified at module level (which is "comm" for communication/ directory). I didn't find a way to make categories easy to define and override, and also maintain nesting automatically at compile time. |
||
|
||
#include "dtls_message_channel.h" | ||
#include "protocol.h" | ||
#include "service_debug.h" | ||
#include "rng_hal.h" | ||
#include "mbedtls/error.h" | ||
#include "mbedtls/ssl_internal.h" | ||
#include "timer_hal.h" | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "dtls_session_persist.h" | ||
#include "service_debug.h" | ||
|
||
|
||
#if HAL_PLATFORM_CLOUD_UDP | ||
|
||
|
@@ -86,7 +88,7 @@ auto SessionPersist::restore(mbedtls_ssl_context* context, bool renegotiate, uin | |
} | ||
|
||
if (!is_valid() || keys_checksum!=this->keys_checksum) { | ||
DEBUG("discarding session: valid %d, keys_sum: %d/%d", is_valid(), keys_checksum, this->keys_checksum); | ||
LOG(WARN,"discarding session: valid %d, keys_sum: %d/%d", is_valid(), keys_checksum, this->keys_checksum); | ||
return NO_SESSION; | ||
} | ||
|
||
|
@@ -113,14 +115,14 @@ auto SessionPersist::restore(mbedtls_ssl_context* context, bool renegotiate, uin | |
context->transform_negotiate->ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite); | ||
if (!context->transform_negotiate->ciphersuite_info) | ||
{ | ||
DEBUG("unknown ciphersuite with id %d", ciphersuite); | ||
LOG(ERROR,"unknown ciphersuite with id %d", ciphersuite); | ||
return ERROR; | ||
} | ||
|
||
int err = mbedtls_ssl_derive_keys(context); | ||
if (err) | ||
{ | ||
DEBUG("derive keys failed with %d", err); | ||
LOG(ERROR,"derive keys failed with %d", err); | ||
return ERROR; | ||
} | ||
|
||
|
@@ -159,11 +161,11 @@ SessionPersist sessionPersist; | |
|
||
#define EXIT_ERROR(x, msg) \ | ||
if (x) { \ | ||
WARN("DTLS initialization failure: " #msg ": %c%04X",(x<0)?'-':' ',(x<0)?-x:x);\ | ||
LOG(WARN,"DTLS initialization failure: " #msg ": %c%04X",(x<0)?'-':' ',(x<0)?-x:x);\ | ||
return UNKNOWN; \ | ||
} | ||
|
||
static void my_debug( void *ctx, int level, | ||
static void my_debug(void *ctx, int level, | ||
const char *file, int line, | ||
const char *str ) | ||
{ | ||
|
@@ -327,15 +329,15 @@ ProtocolError DTLSMessageChannel::setup_context() | |
|
||
if ((ssl_context.session_negotiate->peer_cert = (mbedtls_x509_crt*)calloc(1, sizeof(mbedtls_x509_crt))) == NULL) | ||
{ | ||
ERROR("unable to allocate certificate storage"); | ||
LOG(ERROR,"unable to allocate certificate storage"); | ||
return INSUFFICIENT_STORAGE; | ||
} | ||
|
||
mbedtls_x509_crt_init(ssl_context.session_negotiate->peer_cert); | ||
ret = mbedtls_pk_parse_public_key(&ssl_context.session_negotiate->peer_cert->pk, server_public, server_public_len); | ||
if (ret) { | ||
WARN("unable to parse negotiated public key: -%x", -ret); | ||
return IO_ERROR; | ||
LOG(WARN,"unable to parse negotiated public key: -%x", -ret); | ||
return IO_ERROR_PARSING_SERVER_PUBLIC_KEY; | ||
} | ||
|
||
return NO_ERROR; | ||
|
@@ -344,30 +346,30 @@ ProtocolError DTLSMessageChannel::setup_context() | |
ProtocolError DTLSMessageChannel::establish(uint32_t& flags, uint32_t app_state_crc) | ||
{ | ||
int ret = 0; | ||
INFO("establish"); | ||
LOG(INFO,"setup context"); | ||
ProtocolError error = setup_context(); | ||
if (error) { | ||
INFO("setup_contex error %x", error); | ||
LOG(ERROR,"setup_contex error %x", error); | ||
return error; | ||
} | ||
bool renegotiate = false; | ||
|
||
SessionPersist::RestoreStatus restoreStatus = sessionPersist.restore(&ssl_context, renegotiate, keys_checksum, coap_state, callbacks.restore); | ||
DEBUG("restoreStatus = %d", restoreStatus); | ||
LOG(INFO,"(CMPL,RENEG,NO_SESS,ERR) restoreStatus=%d", restoreStatus); | ||
if (restoreStatus==SessionPersist::COMPLETE) | ||
{ | ||
DEBUG("out_ctr %d,%d,%d,%d,%d,%d,%d,%d, next_coap_id=%d", sessionPersist.out_ctr[0], | ||
LOG(INFO,"out_ctr %d,%d,%d,%d,%d,%d,%d,%d, next_coap_id=%x", sessionPersist.out_ctr[0], | ||
sessionPersist.out_ctr[1],sessionPersist.out_ctr[2],sessionPersist.out_ctr[3], | ||
sessionPersist.out_ctr[4],sessionPersist.out_ctr[5],sessionPersist.out_ctr[6], | ||
sessionPersist.out_ctr[7], sessionPersist.next_coap_id); | ||
sessionPersist.make_persistent(); | ||
uint32_t actual = sessionPersist.application_state_checksum(this->callbacks.calculate_crc); | ||
DEBUG("application state checksum: %x, expected: %x", actual, app_state_crc); | ||
LOG(INFO,"application state checksum: %x, expected: %x", actual, app_state_crc); | ||
if (actual==app_state_crc) { | ||
DEBUG("skipping sending hello message"); | ||
LOG(WARN,"skipping sending hello message"); | ||
flags |= Protocol::SKIP_SESSION_RESUME_HELLO; | ||
} | ||
DEBUG("restored session from persisted session data. next_msg_id=%d", *coap_state); | ||
LOG(INFO,"restored session from persisted session data. next_msg_id=%d", *coap_state); | ||
return SESSION_RESUMED; | ||
} | ||
else if (restoreStatus==SessionPersist::RENEGOTIATE) | ||
|
@@ -405,14 +407,14 @@ ProtocolError DTLSMessageChannel::establish(uint32_t& flags, uint32_t app_state_ | |
|
||
if (ret) | ||
{ | ||
DEBUG("handshake failed -%x", -ret); | ||
LOG(ERROR,"handshake failed -%x", -ret); | ||
reset_session(); | ||
} | ||
else | ||
{ | ||
sessionPersist.prepare_save(random, keys_checksum, &ssl_context, 0); | ||
} | ||
return ret==0 ? NO_ERROR : IO_ERROR; | ||
return ret==0 ? NO_ERROR : IO_ERROR_GENERIC_ESTABLISH; | ||
} | ||
|
||
ProtocolError DTLSMessageChannel::notify_established() | ||
|
@@ -445,7 +447,7 @@ ProtocolError DTLSMessageChannel::receive(Message& message) | |
break; | ||
default: | ||
reset_session(); | ||
return IO_ERROR; | ||
return IO_ERROR_GENERIC_RECEIVE; | ||
} | ||
} | ||
message.set_length(ret); | ||
|
@@ -461,7 +463,7 @@ ProtocolError DTLSMessageChannel::receive(Message& message) | |
sprintf(buf, "%02x", c); | ||
LOG_PRINT(TRACE, buf); | ||
} | ||
LOG_PRINT(TRACE, "\n"); | ||
LOG_PRINT(TRACE, "\r\n"); | ||
} | ||
#endif | ||
} | ||
|
@@ -477,7 +479,7 @@ ProtocolError DTLSMessageChannel::send(Message& message) | |
{ | ||
// send unencrypted | ||
int bytes = this->send(message.buf(), message.length()); | ||
return bytes < 0 ? IO_ERROR : NO_ERROR; | ||
return bytes < 0 ? IO_ERROR_GENERIC_SEND : NO_ERROR; | ||
} | ||
|
||
#ifdef DEBUG_BUILD | ||
|
@@ -489,14 +491,14 @@ ProtocolError DTLSMessageChannel::send(Message& message) | |
sprintf(buf, "%02x", c); | ||
LOG_PRINT(TRACE, buf); | ||
} | ||
LOG_PRINT(TRACE, "\n"); | ||
LOG_PRINT(TRACE, "\r\n"); | ||
#endif | ||
|
||
int ret = mbedtls_ssl_write(&ssl_context, message.buf(), message.length()); | ||
if (ret < 0 && ret != MBEDTLS_ERR_SSL_WANT_WRITE) | ||
{ | ||
reset_session(); | ||
return IO_ERROR; | ||
return IO_ERROR_GENERIC_MBEDTLS_SSL_WRITE; | ||
} | ||
sessionPersist.update(&ssl_context, callbacks.save, coap_state ? *coap_state : 0); | ||
return NO_ERROR; | ||
|
@@ -509,7 +511,7 @@ bool DTLSMessageChannel::is_unreliable() | |
|
||
ProtocolError DTLSMessageChannel::command(Command command, void* arg) | ||
{ | ||
DEBUG("session command: %d", command); | ||
LOG(INFO,"session command (CLS,DIS,MOV,LOD,SAV): %d", command); | ||
switch (command) | ||
{ | ||
case CLOSE: | ||
|
@@ -518,7 +520,7 @@ ProtocolError DTLSMessageChannel::command(Command command, void* arg) | |
|
||
case DISCARD_SESSION: | ||
reset_session(); | ||
return IO_ERROR; //force re-establish | ||
return IO_ERROR_DISCARD_SESSION; //force re-establish | ||
|
||
case MOVE_SESSION: | ||
move_session = true; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just add single "\r\n" sequence to
my_debug
callback which all mbedtls debugging output is being forwarded to?As I side note, I think we should avoid making non-critical changes in our third party dependencies whenever possible, since it complicates merging. Ideally, when we know that we didn't make any changes in some library, we should be able to just replace its sources with newer version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did originally do that via
debug_send_line
, but there are some cases where it's not appropriate, like a buffer dump. So the higher level API needed modification instead. The above change to line 73 I believe was residual testing that should have been removed (MBEDTLS_THREADING_C is not currently defined). If we add\r\n
tomy_debug
most of the log messages would end with\n\r\n
adding a bunch of space to the log output. I agree we should try to minimize changes to 3rd party libraries, but we also need to have effective logs. I think these changes should merge with upstream MBEDTLS library changes without conflict. I'll revert the change on line 73.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, new line sequence should be specific to a log handler, as "\r\n", for example, makes sense only for serial logging. We can make the logging framework perform new line conversions on the fly, but, surely, that's not something that needs to be discussed in the scope of this PR.