diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9e5a955d7c..362bb0ecd4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ repos: rev: v0.9.0 hooks: - id: astyle_py - files: '^(coap_config|man|include|src/oscore|src/coap_[a-s]|src/coap_oscore|examples|tests).*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$' + files: '^.*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$' exclude: '^.*/(coap_uthash_internal.h|coap_utlist_internal.h)$' args: ['--style=google', '--align-pointer=name', diff --git a/src/coap_tcp.c b/src/coap_tcp.c index 678c6af70d..124a927a07 100644 --- a/src/coap_tcp.c +++ b/src/coap_tcp.c @@ -23,7 +23,7 @@ # define OPTVAL_GT(t) (t) #endif #ifdef HAVE_SYS_IOCTL_H - #include +#include #endif #ifdef HAVE_WS2TCPIP_H #include @@ -54,15 +54,14 @@ coap_socket_connect_tcp1(coap_socket_t *sock, u_long u_on = 1; #endif coap_address_t connect_addr; - coap_address_copy( &connect_addr, server ); + coap_address_copy(&connect_addr, server); sock->flags &= ~COAP_SOCKET_CONNECTED; sock->fd = socket(server->addr.sa.sa_family, SOCK_STREAM, 0); if (sock->fd == COAP_INVALID_SOCKET) { - coap_log_warn( - "coap_socket_connect_tcp1: socket: %s\n", - coap_socket_strerror()); + coap_log_warn("coap_socket_connect_tcp1: socket: %s\n", + coap_socket_strerror()); goto error; } @@ -72,9 +71,8 @@ coap_socket_connect_tcp1(coap_socket_t *sock, #else if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) { #endif - coap_log_warn( - "coap_socket_connect_tcp1: ioctl FIONBIO: %s\n", - coap_socket_strerror()); + coap_log_warn("coap_socket_connect_tcp1: ioctl FIONBIO: %s\n", + coap_socket_strerror()); } #endif /* RIOT_VERSION */ @@ -91,10 +89,10 @@ coap_socket_connect_tcp1(coap_socket_t *sock, connect_addr.addr.sin6.sin6_port = htons(default_port); #ifndef RIOT_VERSION /* Configure the socket as dual-stacked */ - if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR) - coap_log_warn( - "coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n", - coap_socket_strerror()); + if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), + sizeof(off)) == COAP_SOCKET_ERROR) + coap_log_warn("coap_socket_connect_tcp1: setsockopt IPV6_V6ONLY: %s\n", + coap_socket_strerror()); #endif /* RIOT_VERSION */ break; #endif /* COAP_IPV6_SUPPORT */ @@ -110,17 +108,16 @@ coap_socket_connect_tcp1(coap_socket_t *sock, if (local_if && local_if->addr.sa.sa_family) { coap_address_copy(local_addr, local_if); if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR) - coap_log_warn( - "coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n", - coap_socket_strerror()); + coap_log_warn("coap_socket_connect_tcp1: setsockopt SO_REUSEADDR: %s\n", + coap_socket_strerror()); if (bind(sock->fd, &local_if->addr.sa, #if COAP_IPV4_SUPPORT local_if->addr.sa.sa_family == AF_INET ? - (socklen_t)sizeof(struct sockaddr_in) : + (socklen_t)sizeof(struct sockaddr_in) : #endif /* COAP_IPV4_SUPPORT */ - (socklen_t)local_if->size) == COAP_SOCKET_ERROR) { + (socklen_t)local_if->size) == COAP_SOCKET_ERROR) { coap_log_warn("coap_socket_connect_tcp1: bind: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); goto error; } } else { @@ -142,18 +139,18 @@ coap_socket_connect_tcp1(coap_socket_t *sock, return 1; } coap_log_warn("coap_socket_connect_tcp1: connect: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); goto error; } if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) { coap_log_warn("coap_socket_connect_tcp1: getsockname: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); } if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) { coap_log_warn("coap_socket_connect_tcp1: getpeername: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); } sock->flags |= COAP_SOCKET_CONNECTED; @@ -170,35 +167,34 @@ coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *remote_addr) { int error = 0; #ifdef _WIN32 - int optlen = (int)sizeof( error ); + int optlen = (int)sizeof(error); #else - socklen_t optlen = (socklen_t)sizeof( error ); + socklen_t optlen = (socklen_t)sizeof(error); #endif sock->flags &= ~(COAP_SOCKET_WANT_CONNECT | COAP_SOCKET_CAN_CONNECT); if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR, OPTVAL_GT(&error), - &optlen) == COAP_SOCKET_ERROR) { + &optlen) == COAP_SOCKET_ERROR) { coap_log_warn("coap_socket_finish_connect_tcp: getsockopt: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); } if (error) { - coap_log_warn( - "coap_socket_finish_connect_tcp: connect failed: %s\n", - coap_socket_format_errno(error)); + coap_log_warn("coap_socket_finish_connect_tcp: connect failed: %s\n", + coap_socket_format_errno(error)); coap_socket_close(sock); return 0; } if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) { coap_log_warn("coap_socket_connect_tcp: getsockname: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); } if (getpeername(sock->fd, &remote_addr->addr.sa, &remote_addr->size) == COAP_SOCKET_ERROR) { coap_log_warn("coap_socket_connect_tcp: getpeername: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); } return 1; @@ -220,7 +216,7 @@ coap_socket_bind_tcp(coap_socket_t *sock, if (sock->fd == COAP_INVALID_SOCKET) { coap_log_warn("coap_socket_bind_tcp: socket: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); goto error; } @@ -231,20 +227,18 @@ coap_socket_bind_tcp(coap_socket_t *sock, if (ioctl(sock->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) { #endif coap_log_warn("coap_socket_bind_tcp: ioctl FIONBIO: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); } #endif /* RIOT_VERSION */ - if (setsockopt (sock->fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on), - sizeof (on)) == COAP_SOCKET_ERROR) - coap_log_warn( - "coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n", - coap_socket_strerror()); + if (setsockopt(sock->fd, SOL_SOCKET, SO_KEEPALIVE, OPTVAL_T(&on), + sizeof(on)) == COAP_SOCKET_ERROR) + coap_log_warn("coap_socket_bind_tcp: setsockopt SO_KEEPALIVE: %s\n", + coap_socket_strerror()); if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, OPTVAL_T(&on), sizeof(on)) == COAP_SOCKET_ERROR) - coap_log_warn( - "coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n", - coap_socket_strerror()); + coap_log_warn("coap_socket_bind_tcp: setsockopt SO_REUSEADDR: %s\n", + coap_socket_strerror()); switch (listen_addr->addr.sa.sa_family) { #if COAP_IPV4_SUPPORT @@ -255,10 +249,10 @@ coap_socket_bind_tcp(coap_socket_t *sock, case AF_INET6: #ifndef RIOT_VERSION /* Configure the socket as dual-stacked */ - if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), sizeof(off)) == COAP_SOCKET_ERROR) - coap_log_alert( - "coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n", - coap_socket_strerror()); + if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY, OPTVAL_T(&off), + sizeof(off)) == COAP_SOCKET_ERROR) + coap_log_alert("coap_socket_bind_tcp: setsockopt IPV6_V6ONLY: %s\n", + coap_socket_strerror()); #endif /* RIOT_VERSION */ break; #endif /* COAP_IPV6_SUPPORT */ @@ -273,24 +267,24 @@ coap_socket_bind_tcp(coap_socket_t *sock, if (bind(sock->fd, &listen_addr->addr.sa, #if COAP_IPV4_SUPPORT listen_addr->addr.sa.sa_family == AF_INET ? - (socklen_t)sizeof(struct sockaddr_in) : + (socklen_t)sizeof(struct sockaddr_in) : #endif /* COAP_IPV4_SUPPORT */ - (socklen_t)listen_addr->size) == COAP_SOCKET_ERROR) { + (socklen_t)listen_addr->size) == COAP_SOCKET_ERROR) { coap_log_alert("coap_socket_bind_tcp: bind: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); goto error; } bound_addr->size = (socklen_t)sizeof(*bound_addr); if (getsockname(sock->fd, &bound_addr->addr.sa, &bound_addr->size) < 0) { coap_log_warn("coap_socket_bind_tcp: getsockname: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); goto error; } if (listen(sock->fd, 5) == COAP_SOCKET_ERROR) { coap_log_alert("coap_socket_bind_tcp: listen: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); goto error; } @@ -320,22 +314,22 @@ coap_socket_accept_tcp(coap_socket_t *server, &remote_addr->size); if (new_client->fd == COAP_INVALID_SOCKET) { coap_log_warn("coap_socket_accept_tcp: accept: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); return 0; } - if (getsockname( new_client->fd, &local_addr->addr.sa, &local_addr->size) < 0) + if (getsockname(new_client->fd, &local_addr->addr.sa, &local_addr->size) < 0) coap_log_warn("coap_socket_accept_tcp: getsockname: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); #ifndef RIOT_VERSION - #ifdef _WIN32 +#ifdef _WIN32 if (ioctlsocket(new_client->fd, FIONBIO, &u_on) == COAP_SOCKET_ERROR) { #else if (ioctl(new_client->fd, FIONBIO, &on) == COAP_SOCKET_ERROR) { #endif coap_log_warn("coap_socket_accept_tcp: ioctl FIONBIO: %s\n", - coap_socket_strerror()); + coap_socket_strerror()); } #endif /* RIOT_VERSION */ return 1; diff --git a/src/coap_time.c b/src/coap_time.c index f967bcbc03..f84a044ca5 100644 --- a/src/coap_time.c +++ b/src/coap_time.c @@ -31,9 +31,9 @@ static coap_tick_t coap_clock_offset = 0; #if _POSIX_TIMERS && !defined(__APPLE__) - /* _POSIX_TIMERS is > 0 when clock_gettime() is available */ +/* _POSIX_TIMERS is > 0 when clock_gettime() is available */ - /* Use real-time clock for correct timestamps in coap_log(). */ +/* Use real-time clock for correct timestamps in coap_log(). */ #define COAP_CLOCK CLOCK_REALTIME #endif @@ -47,10 +47,10 @@ gettimeofday(struct timeval *tp, TIME_ZONE_INFORMATION *tzp) { ULARGE_INTEGER time; uint64_t tUsSinceUnicEpoch; - GetSystemTimeAsFileTime( &file_time ); + GetSystemTimeAsFileTime(&file_time); time.LowPart = file_time.dwLowDateTime; time.HighPart = file_time.dwHighDateTime; - tUsSinceUnicEpoch = ( time.QuadPart - s_tUnixEpoch ) / 10; + tUsSinceUnicEpoch = (time.QuadPart - s_tUnixEpoch) / 10; tp->tv_sec = (long)(tUsSinceUnicEpoch / 1000000); tp->tv_usec = (long)(tUsSinceUnicEpoch % 1000000); @@ -118,11 +118,13 @@ coap_ticks_to_rt(coap_tick_t t) { return coap_clock_offset + (t / COAP_TICKS_PER_SECOND); } -uint64_t coap_ticks_to_rt_us(coap_tick_t t) { +uint64_t +coap_ticks_to_rt_us(coap_tick_t t) { return (uint64_t)coap_clock_offset * 1000000 + (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND; } -coap_tick_t coap_ticks_from_rt_us(uint64_t t) { +coap_tick_t +coap_ticks_from_rt_us(uint64_t t) { return (coap_tick_t)((t - (uint64_t)coap_clock_offset * 1000000) * COAP_TICKS_PER_SECOND / 1000000); } @@ -133,8 +135,8 @@ coap_tick_t coap_ticks_from_rt_us(uint64_t t) { #else /* HAVE_TIME_H */ /* make compilers happy that do not like empty modules */ -COAP_STATIC_INLINE void dummy(void) -{ +COAP_STATIC_INLINE void +dummy(void) { } #endif /* not HAVE_TIME_H */ diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index abc3817007..9357b1cf96 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -144,7 +144,8 @@ dtls_logging(log_t d_level, const char *message) { } #endif /* HAVE_DTLS_SET_LOG_HANDLER */ -void coap_dtls_startup(void) { +void +coap_dtls_startup(void) { dtls_init(); dtls_ticks(&dtls_tick_0); coap_ticks(&coap_tick_0); @@ -154,7 +155,8 @@ void coap_dtls_startup(void) { #endif /* HAVE_DTLS_SET_LOG_HANDLER */ } -void coap_dtls_shutdown(void) { +void +coap_dtls_shutdown(void) { } void * @@ -164,7 +166,7 @@ coap_dtls_get_tls(const coap_session_t *c_session, *tls_lib = COAP_TLS_LIBRARY_TINYDTLS; if (c_session && c_session->context && c_session->context->dtls_context) { const coap_tiny_context_t *t_context = - (const coap_tiny_context_t *)c_session->context->dtls_context; + (const coap_tiny_context_t *)c_session->context->dtls_context; return t_context->dtls_context; } @@ -284,9 +286,9 @@ put_session_addr(const coap_address_t *a, session_t *s) { static int dtls_send_to_peer(struct dtls_context_t *dtls_context, - session_t *dtls_session, uint8 *data, size_t len) { + session_t *dtls_session, uint8 *data, size_t len) { coap_tiny_context_t *t_context = - (coap_tiny_context_t *)dtls_get_app_data(dtls_context); + (coap_tiny_context_t *)dtls_get_app_data(dtls_context); coap_context_t *coap_context = t_context ? t_context->coap_context : NULL; coap_session_t *coap_session; coap_address_t remote_addr; @@ -304,9 +306,9 @@ dtls_send_to_peer(struct dtls_context_t *dtls_context, static int dtls_application_data(struct dtls_context_t *dtls_context, - session_t *dtls_session, uint8 *data, size_t len) { + session_t *dtls_session, uint8 *data, size_t len) { coap_tiny_context_t *t_context = - (coap_tiny_context_t *)dtls_get_app_data(dtls_context); + (coap_tiny_context_t *)dtls_get_app_data(dtls_context); coap_context_t *coap_context = t_context ? t_context->coap_context : NULL; coap_session_t *coap_session; coap_address_t remote_addr; @@ -315,8 +317,7 @@ dtls_application_data(struct dtls_context_t *dtls_context, get_session_addr(dtls_session, &remote_addr); coap_session = coap_session_get_by_peer(coap_context, &remote_addr, dtls_session->ifindex); if (!coap_session) { - coap_log_debug( - "dropped message that was received on invalid interface\n"); + coap_log_debug("dropped message that was received on invalid interface\n"); return -1; } @@ -327,9 +328,9 @@ static int coap_event_dtls = 0; static int dtls_event(struct dtls_context_t *dtls_context, - session_t *dtls_session, - dtls_alert_level_t level, - uint16_t code) { + session_t *dtls_session, + dtls_alert_level_t level, + uint16_t code) { (void)dtls_context; (void)dtls_session; @@ -338,19 +339,16 @@ dtls_event(struct dtls_context_t *dtls_context, /* handle DTLS events */ switch (code) { - case DTLS_ALERT_CLOSE_NOTIFY: - { + case DTLS_ALERT_CLOSE_NOTIFY: { coap_event_dtls = COAP_EVENT_DTLS_CLOSED; break; } - case DTLS_EVENT_CONNECTED: - { + case DTLS_EVENT_CONNECTED: { coap_event_dtls = COAP_EVENT_DTLS_CONNECTED; break; } #ifdef DTLS_EVENT_RENEGOTIATE - case DTLS_EVENT_RENEGOTIATE: - { + case DTLS_EVENT_RENEGOTIATE: { coap_event_dtls = COAP_EVENT_DTLS_RENEGOTIATE; break; } @@ -368,13 +366,13 @@ dtls_event(struct dtls_context_t *dtls_context, * session. */ static int get_psk_info(struct dtls_context_t *dtls_context, - const session_t *dtls_session, - dtls_credentials_type_t type, - const uint8_t *id, size_t id_len, - unsigned char *result, size_t result_length) { + const session_t *dtls_session, + dtls_credentials_type_t type, + const uint8_t *id, size_t id_len, + unsigned char *result, size_t result_length) { coap_tiny_context_t *t_context = - (coap_tiny_context_t *)dtls_get_app_data(dtls_context); + (coap_tiny_context_t *)dtls_get_app_data(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; @@ -413,7 +411,7 @@ get_psk_info(struct dtls_context_t *dtls_context, coap_session_refresh_psk_hint(coap_session, &temp); coap_log_debug("got psk_identity_hint: '%.*s'\n", (int)id_len, - id ? (const char*)id : ""); + id ? (const char *)id : ""); if (setup_cdata->validate_ih_call_back) { coap_str_const_t lhint; @@ -421,19 +419,17 @@ get_psk_info(struct dtls_context_t *dtls_context, lhint.length = id_len; lhint.s = id; cpsk_info = - setup_cdata->validate_ih_call_back(&lhint, - coap_session, - setup_cdata->ih_call_back_arg); + setup_cdata->validate_ih_call_back(&lhint, + coap_session, + setup_cdata->ih_call_back_arg); if (cpsk_info) { psk_identity = &cpsk_info->identity; coap_session_refresh_psk_identity(coap_session, &cpsk_info->identity); coap_session_refresh_psk_key(coap_session, &cpsk_info->key); - } - else { + } else { psk_identity = NULL; } - } - else { + } else { psk_identity = coap_get_session_client_psk_identity(coap_session); } if (psk_identity == NULL) { @@ -442,11 +438,9 @@ get_psk_info(struct dtls_context_t *dtls_context, goto error; } if (psk_identity->length > result_length) { - coap_log_warn( - "psk_identity too large, truncated to %zd bytes\n", - result_length); - } - else { + coap_log_warn("psk_identity too large, truncated to %zd bytes\n", + result_length); + } else { /* Reduce to match */ result_length = psk_identity->length; } @@ -466,11 +460,9 @@ get_psk_info(struct dtls_context_t *dtls_context, goto error; } if (psk_key->length > result_length) { - coap_log_warn( - "psk_key too large, truncated to %zd bytes\n", - result_length); - } - else { + coap_log_warn("psk_key too large, truncated to %zd bytes\n", + result_length); + } else { /* Reduce to match */ result_length = psk_key->length; } @@ -483,22 +475,21 @@ get_psk_info(struct dtls_context_t *dtls_context, coap_bin_const_t lidentity; lidentity.length = id ? id_len : 0; - lidentity.s = id ? (const uint8_t*)id : (const uint8_t *)""; + lidentity.s = id ? (const uint8_t *)id : (const uint8_t *)""; setup_sdata = &coap_session->context->spsk_setup_data; /* Track the Identity being used */ coap_session_refresh_psk_identity(coap_session, &lidentity); coap_log_debug("got psk_identity: '%.*s'\n", - (int)lidentity.length, lidentity.s); + (int)lidentity.length, lidentity.s); if (setup_sdata->validate_id_call_back) { psk_key = - setup_sdata->validate_id_call_back(&lidentity, - coap_session, - setup_sdata->id_call_back_arg); - } - else { + setup_sdata->validate_id_call_back(&lidentity, + coap_session, + setup_sdata->id_call_back_arg); + } else { psk_key = coap_get_session_server_psk_key(coap_session); } @@ -509,11 +500,9 @@ get_psk_info(struct dtls_context_t *dtls_context, if (setup_sdata->validate_id_call_back) coap_session_refresh_psk_key(coap_session, psk_key); if (psk_key->length > result_length) { - coap_log_warn( - "psk_key too large, truncated to %zd bytes\n", - result_length); - } - else { + coap_log_warn("psk_key too large, truncated to %zd bytes\n", + result_length); + } else { /* Reduce to match */ result_length = psk_key->length; } @@ -529,11 +518,9 @@ get_psk_info(struct dtls_context_t *dtls_context, if (psk_hint == NULL) return 0; if (psk_hint->length > result_length) { - coap_log_warn( - "psk_hint too large, truncated to %zd bytes\n", - result_length); - } - else { + coap_log_warn("psk_hint too large, truncated to %zd bytes\n", + result_length); + } else { /* Reduce to match */ result_length = psk_hint->length; } @@ -559,7 +546,7 @@ get_ecdsa_key(struct dtls_context_t *dtls_context, const dtls_ecdsa_key_t **result) { static dtls_ecdsa_key_t ecdsa_key; coap_tiny_context_t *t_context = - (coap_tiny_context_t *)dtls_get_app_data(dtls_context); + (coap_tiny_context_t *)dtls_get_app_data(dtls_context); ecdsa_key.curve = DTLS_ECDH_CURVE_SECP256R1; ecdsa_key.priv_key = t_context->priv_key->s; @@ -573,13 +560,13 @@ get_ecdsa_key(struct dtls_context_t *dtls_context, /* first part of Raw public key, the is the start of the Subject Public Key */ static const unsigned char cert_asn1_header[] = { 0x30, 0x59, /* SEQUENCE, length 89 bytes */ - 0x30, 0x13, /* SEQUENCE, length 19 bytes */ - 0x06, 0x07, /* OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1) */ - 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, - 0x06, 0x08, /* OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7) */ - 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, - 0x03, 0x42, 0x00, /* BIT STRING, length 66 bytes, 0 bits unused */ - 0x04 /* uncompressed, followed by the r and s values of the public key */ + 0x30, 0x13, /* SEQUENCE, length 19 bytes */ + 0x06, 0x07, /* OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1) */ + 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, + 0x06, 0x08, /* OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7) */ + 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, + 0x03, 0x42, 0x00, /* BIT STRING, length 66 bytes, 0 bits unused */ + 0x04 /* uncompressed, followed by the r and s values of the public key */ }; #define DTLS_CE_LENGTH (sizeof(cert_asn1_header) + key_size + key_size) @@ -590,7 +577,7 @@ verify_ecdsa_key(struct dtls_context_t *dtls_context COAP_UNUSED, const uint8_t *other_pub_y, size_t key_size) { coap_tiny_context_t *t_context = - (coap_tiny_context_t *)dtls_get_app_data(dtls_context); + (coap_tiny_context_t *)dtls_get_app_data(dtls_context); if (t_context && t_context->setup_data.validate_cn_call_back) { /* Need to build asn.1 certificate - code taken from tinydtls */ uint8 *p; @@ -620,7 +607,7 @@ verify_ecdsa_key(struct dtls_context_t *dtls_context COAP_UNUSED, if (!c_session) return -3; if (!t_context->setup_data.validate_cn_call_back(COAP_DTLS_RPK_CERT_CN, - buf, p-buf, c_session, 0, 1, t_context->setup_data.cn_call_back_arg)) { + buf, p-buf, c_session, 0, 1, t_context->setup_data.cn_call_back_arg)) { return -1; } } @@ -707,13 +694,15 @@ coap_dtls_new_session(coap_session_t *session) { } #if COAP_SERVER_SUPPORT -void *coap_dtls_new_server_session(coap_session_t *session) { +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) { +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; dtls_context_t *dtls_context = t_context ? t_context->dtls_context : NULL; @@ -722,7 +711,7 @@ void *coap_dtls_new_client_session(coap_session_t *session) { if (!dtls_session) return NULL; peer = - dtls_get_peer(dtls_context, dtls_session); + dtls_get_peer(dtls_context, dtls_session); if (!peer) { /* The peer connection does not yet exist. */ @@ -730,7 +719,7 @@ void *coap_dtls_new_client_session(coap_session_t *session) { * connection attempt is made, 0 for session reuse. */ if (dtls_connect(dtls_context, dtls_session) >= 0) { peer = - dtls_get_peer(dtls_context, dtls_session); + dtls_get_peer(dtls_context, dtls_session); } } @@ -752,14 +741,14 @@ coap_dtls_session_update_mtu(coap_session_t *session) { void coap_dtls_free_session(coap_session_t *coap_session) { coap_tiny_context_t *t_context = - (coap_tiny_context_t *)coap_session->context->dtls_context; + (coap_tiny_context_t *)coap_session->context->dtls_context; dtls_context_t *dtls_context = t_context ? t_context->dtls_context : NULL; if (dtls_context == NULL) return; if (coap_session->tls && dtls_context) { dtls_peer_t *peer = dtls_get_peer(dtls_context, (session_t *)coap_session->tls); - if ( peer ) + if (peer) dtls_reset_peer(dtls_context, peer); else dtls_close(dtls_context, (session_t *)coap_session->tls); @@ -783,9 +772,9 @@ coap_dtls_send(coap_session_t *session, coap_event_dtls = -1; /* Need to do this to not get a compiler warning about const parameters */ - memcpy (&data_rw, &data, sizeof(data_rw)); + memcpy(&data_rw, &data, sizeof(data_rw)); res = dtls_write(dtls_context, - (session_t *)session->tls, data_rw, data_len); + (session_t *)session->tls, data_rw, data_len); if (res < 0) coap_log_warn("coap_dtls_send: cannot send PDU\n"); @@ -811,22 +800,26 @@ coap_dtls_send(coap_session_t *session, return res; } -int coap_dtls_is_context_timeout(void) { +int +coap_dtls_is_context_timeout(void) { return 1; } -coap_tick_t coap_dtls_get_context_timeout(void *tiny_context) { +coap_tick_t +coap_dtls_get_context_timeout(void *tiny_context) { clock_time_t next = 0; coap_tiny_context_t *t_context = (coap_tiny_context_t *)tiny_context; dtls_context_t *dtls_context = t_context ? t_context->dtls_context : NULL; if (tiny_context) dtls_check_retransmit(dtls_context, &next); if (next > 0) - return ((coap_tick_t)(next - dtls_tick_0)) * COAP_TICKS_PER_SECOND / DTLS_TICKS_PER_SECOND + coap_tick_0; + return ((coap_tick_t)(next - dtls_tick_0)) * COAP_TICKS_PER_SECOND / DTLS_TICKS_PER_SECOND + + coap_tick_0; return 0; } -coap_tick_t coap_dtls_get_timeout(coap_session_t *session, coap_tick_t now) { +coap_tick_t +coap_dtls_get_timeout(coap_session_t *session, coap_tick_t now) { (void)session; (void)now; return 0; @@ -844,9 +837,9 @@ coap_dtls_handle_timeout(coap_session_t *session) { int coap_dtls_receive(coap_session_t *session, - const uint8_t *data, - size_t data_len -) { + const uint8_t *data, + size_t data_len + ) { session_t *dtls_session = (session_t *)session->tls; int err; uint8_t *data_rw; @@ -856,7 +849,7 @@ coap_dtls_receive(coap_session_t *session, assert(dtls_context); coap_event_dtls = -1; /* Need to do this to not get a compiler warning about const parameters */ - memcpy (&data_rw, &data, sizeof(data_rw)); + memcpy(&data_rw, &data, sizeof(data_rw)); err = dtls_handle_message(dtls_context, dtls_session, data_rw, (int)data_len); if (err) { @@ -879,9 +872,9 @@ coap_dtls_receive(coap_session_t *session, #if COAP_SERVER_SUPPORT int coap_dtls_hello(coap_session_t *session, - const uint8_t *data, - size_t data_len -) { + const uint8_t *data, + size_t data_len + ) { session_t dtls_session; coap_tiny_context_t *t_context = (coap_tiny_context_t *)session->context->dtls_context; dtls_context_t *dtls_context = t_context ? t_context->dtls_context : NULL; @@ -892,9 +885,9 @@ coap_dtls_hello(coap_session_t *session, put_session_addr(&session->addr_info.remote, &dtls_session); dtls_session.ifindex = session->ifindex; /* Need to do this to not get a compiler warning about const parameters */ - memcpy (&data_rw, &data, sizeof(data_rw)); + memcpy(&data_rw, &data, sizeof(data_rw)); int res = dtls_handle_message(dtls_context, &dtls_session, - data_rw, (int)data_len); + data_rw, (int)data_len); if (res >= 0) { if (dtls_get_peer(dtls_context, &dtls_session)) res = 1; @@ -905,12 +898,14 @@ coap_dtls_hello(coap_session_t *session, } #endif /* COAP_SERVER_SUPPORT */ -unsigned int coap_dtls_get_overhead(coap_session_t *session) { +unsigned int +coap_dtls_get_overhead(coap_session_t *session) { (void)session; return 13 + 8 + 8; } -int coap_tls_is_supported(void) { +int +coap_tls_is_supported(void) { return 0; } @@ -922,7 +917,7 @@ coap_get_tls_library_version(void) { version.version = 0; if (vers) { long int p1, p2 = 0, p3 = 0; - char* endptr; + char *endptr; p1 = strtol(vers, &endptr, 10); if (*endptr == '.') { @@ -939,21 +934,20 @@ coap_get_tls_library_version(void) { } #ifdef DTLS_ECC -static const uint8_t b64_6[256] = - { +static const uint8_t b64_6[256] = { 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, -/* + / */ + /* + / */ 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, -/* 0 1 2 3 4 5 6 7 8 9 = */ + /* 0 1 2 3 4 5 6 7 8 9 = */ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, -/* A B C D E F G H I J K L M N O */ + /* A B C D E F G H I J K L M N O */ 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, -/* P Q R S T U V W X Y Z */ + /* P Q R S T U V W X Y Z */ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, -/* a b c d e f g h i j k l m n o */ + /* a b c d e f g h i j k l m n o */ 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, -/* p q r s t u v w x y z */ + /* p q r s t u v w x y z */ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, @@ -963,12 +957,11 @@ static const uint8_t b64_6[256] = 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 - }; +}; /* caller must free off returned coap_binary_t* */ static coap_binary_t * -pem_base64_decode (const uint8_t *data, size_t size) -{ +pem_base64_decode(const uint8_t *data, size_t size) { uint8_t *tbuf = coap_malloc_type(COAP_STRING, size); size_t nbytesdecoded; size_t i; @@ -1025,16 +1018,15 @@ pem_base64_decode (const uint8_t *data, size_t size) return decoded; } -typedef coap_binary_t * (*asn1_callback)(const uint8_t *data, size_t size); +typedef coap_binary_t *(*asn1_callback)(const uint8_t *data, size_t size); static int -asn1_verify_privkey(const uint8_t *data, size_t size) -{ +asn1_verify_privkey(const uint8_t *data, size_t size) { /* Check if we have the private key (with optional leading 0x00) */ /* skip leading 0x00 */ if (size - 1 == DTLS_EC_KEY_SIZE && *data == '\000') { - --size; - ++data; + --size; + ++data; } /* Check if we have the private key */ @@ -1045,8 +1037,7 @@ asn1_verify_privkey(const uint8_t *data, size_t size) } static int -asn1_verify_pubkey(const uint8_t *data, size_t size) -{ +asn1_verify_pubkey(const uint8_t *data, size_t size) { (void)data; /* We have the public key @@ -1058,11 +1049,10 @@ asn1_verify_pubkey(const uint8_t *data, size_t size) } static int -asn1_verify_curve(const uint8_t *data, size_t size) -{ +asn1_verify_curve(const uint8_t *data, size_t size) { static uint8_t prime256v1_oid[] = - /* OID 1.2.840.10045.3.1.7 */ - { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 }; + /* OID 1.2.840.10045.3.1.7 */ + { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 }; /* Check that we have the correct EC (only one supported) */ if (size != sizeof(prime256v1_oid) || @@ -1073,8 +1063,7 @@ asn1_verify_curve(const uint8_t *data, size_t size) } static int -asn1_verify_pkcs8_version(const uint8_t *data, size_t size) -{ +asn1_verify_pkcs8_version(const uint8_t *data, size_t size) { /* Check that we have the version */ if (size != 1 || *data != 0) return 0; @@ -1083,11 +1072,10 @@ asn1_verify_pkcs8_version(const uint8_t *data, size_t size) } static int -asn1_verify_ec_identifier(const uint8_t *data, size_t size) -{ +asn1_verify_ec_identifier(const uint8_t *data, size_t size) { static uint8_t ec_public_key_oid[] = - /* OID 1.2.840.10045.2.1 */ - { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01 }; + /* OID 1.2.840.10045.2.1 */ + { 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01 }; /* Check that we have the correct ecPublicKey */ if (size != sizeof(ec_public_key_oid) || @@ -1098,8 +1086,7 @@ asn1_verify_ec_identifier(const uint8_t *data, size_t size) } static int -asn1_verify_ec_key(const uint8_t *data, size_t size) -{ +asn1_verify_ec_key(const uint8_t *data, size_t size) { (void)data; if (size == 0) @@ -1112,8 +1099,7 @@ static int asn1_derive_keys(coap_tiny_context_t *t_context, const uint8_t *priv_data, size_t priv_len, const uint8_t *pub_data, size_t pub_len, - int is_pkcs8) -{ + int is_pkcs8) { coap_binary_t *test; t_context->priv_key = get_asn1_tag(COAP_ASN1_OCTETSTRING, priv_data, @@ -1125,14 +1111,14 @@ asn1_derive_keys(coap_tiny_context_t *t_context, /* skip leading 0x00 */ if (t_context->priv_key->length - 1 == DTLS_EC_KEY_SIZE && t_context->priv_key->s[0] == '\000') { - t_context->priv_key->length--; - t_context->priv_key->s++; + t_context->priv_key->length--; + t_context->priv_key->s++; } if (!is_pkcs8) { /* pkcs8 abstraction tested for valid eliptic curve */ test = get_asn1_tag(COAP_ASN1_IDENTIFIER, priv_data, priv_len, - asn1_verify_curve); + asn1_verify_curve); if (!test) { coap_log_info("EC Private Key (RPK) invalid elliptic curve\n"); coap_delete_binary(t_context->priv_key); @@ -1143,7 +1129,7 @@ asn1_derive_keys(coap_tiny_context_t *t_context, } t_context->pub_key = get_asn1_tag(COAP_ASN1_BITSTRING, pub_data, pub_len, - asn1_verify_pubkey); + asn1_verify_pubkey); if (!t_context->pub_key) { coap_log_info("EC Public Key (RPK) invalid\n"); coap_delete_binary(t_context->priv_key); @@ -1158,25 +1144,24 @@ asn1_derive_keys(coap_tiny_context_t *t_context, } static coap_binary_t * -ec_abstract_pkcs8_asn1(const uint8_t *asn1_ptr, size_t asn1_length) -{ +ec_abstract_pkcs8_asn1(const uint8_t *asn1_ptr, size_t asn1_length) { coap_binary_t *test; test = get_asn1_tag(COAP_ASN1_INTEGER, asn1_ptr, asn1_length, - asn1_verify_pkcs8_version); + asn1_verify_pkcs8_version); if (!test) return 0; coap_delete_binary(test); test = get_asn1_tag(COAP_ASN1_IDENTIFIER, asn1_ptr, asn1_length, - asn1_verify_ec_identifier); + asn1_verify_ec_identifier); if (!test) return 0; coap_delete_binary(test); test = get_asn1_tag(COAP_ASN1_IDENTIFIER, asn1_ptr, asn1_length, - asn1_verify_curve); + asn1_verify_curve); if (!test) { coap_log_info("EC Private Key (RPK) invalid elliptic curve\n"); return 0; @@ -1184,19 +1169,18 @@ ec_abstract_pkcs8_asn1(const uint8_t *asn1_ptr, size_t asn1_length) coap_delete_binary(test); test = get_asn1_tag(COAP_ASN1_OCTETSTRING, asn1_ptr, asn1_length, - asn1_verify_ec_key); + asn1_verify_ec_key); return test; } static coap_binary_t * -pem_decode_mem_asn1(const char *begstr, const uint8_t *str) -{ - char *bcp = str ? strstr((const char*)str, begstr) : NULL; +pem_decode_mem_asn1(const char *begstr, const uint8_t *str) { + char *bcp = str ? strstr((const char *)str, begstr) : NULL; char *tcp = bcp ? strstr(bcp, "-----END ") : NULL; if (bcp && tcp) { bcp += strlen(begstr); - return pem_base64_decode ((const uint8_t *)bcp, tcp - bcp); + return pem_base64_decode((const uint8_t *)bcp, tcp - bcp); } return NULL; } @@ -1205,7 +1189,7 @@ pem_decode_mem_asn1(const char *begstr, const uint8_t *str) int coap_dtls_context_set_pki(coap_context_t *ctx, - const coap_dtls_pki_t* setup_data, + const coap_dtls_pki_t *setup_data, const coap_dtls_role_t role COAP_UNUSED) { #ifdef DTLS_ECC coap_tiny_context_t *t_context; @@ -1249,10 +1233,10 @@ coap_dtls_context_set_pki(coap_context_t *ctx, setup_data->pki_key.key.pem_buf.private_key[0]) { /* Need to take PEM memory information and convert to binary */ asn1_priv = pem_decode_mem_asn1("-----BEGIN EC PRIVATE KEY-----", - setup_data->pki_key.key.pem_buf.private_key); + setup_data->pki_key.key.pem_buf.private_key); if (!asn1_priv) { asn1_priv = pem_decode_mem_asn1("-----BEGIN PRIVATE KEY-----", - setup_data->pki_key.key.pem_buf.private_key); + setup_data->pki_key.key.pem_buf.private_key); if (!asn1_priv) { coap_log_info("Private Key (RPK) invalid\n"); return 0; @@ -1268,14 +1252,14 @@ coap_dtls_context_set_pki(coap_context_t *ctx, is_pkcs8 = 1; } asn1_pub = pem_decode_mem_asn1( - "-----BEGIN PUBLIC KEY-----", - setup_data->pki_key.key.pem_buf.public_cert); + "-----BEGIN PUBLIC KEY-----", + setup_data->pki_key.key.pem_buf.public_cert); if (!asn1_pub) { asn1_pub = pem_decode_mem_asn1("-----BEGIN EC PRIVATE KEY-----", - setup_data->pki_key.key.pem_buf.private_key); + setup_data->pki_key.key.pem_buf.private_key); if (!asn1_pub) { asn1_pub = pem_decode_mem_asn1("-----BEGIN PRIVATE KEY-----", - setup_data->pki_key.key.pem_buf.private_key); + setup_data->pki_key.key.pem_buf.private_key); if (!asn1_pub) { coap_log_info("Public Key (RPK) invalid\n"); coap_delete_binary(asn1_priv); @@ -1308,14 +1292,14 @@ coap_dtls_context_set_pki(coap_context_t *ctx, case COAP_PKI_KEY_ASN1: if (setup_data->pki_key.key.asn1.private_key && setup_data->pki_key.key.asn1.private_key_len && - setup_data->pki_key.key.asn1.private_key_type == COAP_ASN1_PKEY_EC) { - const uint8_t* private_key = setup_data->pki_key.key.asn1.private_key; + setup_data->pki_key.key.asn1.private_key_type == COAP_ASN1_PKEY_EC) { + const uint8_t *private_key = setup_data->pki_key.key.asn1.private_key; size_t private_key_len = setup_data->pki_key.key.asn1.private_key_len; /* Check to see whether this is in pkcs8 format or not */ asn1_temp = ec_abstract_pkcs8_asn1( - setup_data->pki_key.key.asn1.private_key, - setup_data->pki_key.key.asn1.private_key_len); + setup_data->pki_key.key.asn1.private_key, + setup_data->pki_key.key.asn1.private_key_len); if (asn1_temp) { private_key = asn1_temp->s; private_key_len = asn1_temp->length; @@ -1331,11 +1315,11 @@ coap_dtls_context_set_pki(coap_context_t *ctx, setup_data->pki_key.key.asn1.public_cert_len, is_pkcs8)) { coap_log_info("Unable to derive Public/Private Keys\n"); - if (asn1_temp) coap_delete_binary(asn1_temp); + if (asn1_temp) + coap_delete_binary(asn1_temp); return 0; } - } - else { + } else { if (!asn1_derive_keys(t_context, private_key, private_key_len, @@ -1343,7 +1327,8 @@ coap_dtls_context_set_pki(coap_context_t *ctx, private_key_len, is_pkcs8)) { coap_log_info("Unable to derive Public/Private Keys\n"); - if (asn1_temp) coap_delete_binary(asn1_temp); + if (asn1_temp) + coap_delete_binary(asn1_temp); return 0; } } @@ -1366,9 +1351,9 @@ coap_dtls_context_set_pki(coap_context_t *ctx, int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, - const char *ca_file COAP_UNUSED, - const char *ca_path COAP_UNUSED -) { + const char *ca_file COAP_UNUSED, + const char *ca_path COAP_UNUSED + ) { coap_log_warn("Root CAs PKI not supported\n"); return 0; } @@ -1376,8 +1361,8 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, #if COAP_CLIENT_SUPPORT int coap_dtls_context_set_cpsk(coap_context_t *coap_context COAP_UNUSED, - coap_dtls_cpsk_t *setup_data -) { + coap_dtls_cpsk_t *setup_data + ) { if (!setup_data) return 0; @@ -1393,15 +1378,14 @@ coap_dtls_context_set_cpsk(coap_context_t *coap_context COAP_UNUSED, #if COAP_SERVER_SUPPORT int coap_dtls_context_set_spsk(coap_context_t *coap_context COAP_UNUSED, - coap_dtls_spsk_t *setup_data -) { + coap_dtls_spsk_t *setup_data + ) { if (!setup_data) return 0; #ifdef DTLS_PSK if (setup_data->validate_sni_call_back) { - coap_log_warn( - "CoAP Server with TinyDTLS does not support SNI selection\n"); + coap_log_warn("CoAP Server with TinyDTLS does not support SNI selection\n"); } return 1; @@ -1413,8 +1397,7 @@ coap_dtls_context_set_spsk(coap_context_t *coap_context COAP_UNUSED, #endif /* COAP_SERVER_SUPPORT */ int -coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) -{ +coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED) { return 1; } @@ -1446,7 +1429,7 @@ ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED -) { + ) { return -1; } @@ -1493,7 +1476,7 @@ coap_digest_update(coap_digest_ctx_t *digest_ctx, int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer) { - dtls_sha256_final((uint8_t*)digest_buffer, digest_ctx); + dtls_sha256_final((uint8_t *)digest_buffer, digest_ctx); coap_digest_free(digest_ctx); return 1; @@ -1552,7 +1535,7 @@ static struct hmac_algs { cose_hmac_alg_t hmac_alg; u_int hmac_type; } hmacs[] = { - {COSE_HMAC_ALG_HMAC256_256, 1}, + {COSE_HMAC_ALG_HMAC256_256, 1}, }; static u_int @@ -1597,16 +1580,14 @@ coap_crypto_aead_encrypt(const coap_crypto_param_t *params, assert(params); if (get_cipher_alg(params->alg) == 0) { - coap_log_debug( - "coap_crypto_encrypt: algorithm %d not supported\n", - params->alg); + coap_log_debug("coap_crypto_encrypt: algorithm %d not supported\n", + params->alg); return 0; } ccm = ¶ms->params.aes; if (*max_result_len < (data->length + ccm->tag_len)) { - coap_log_warn( - "coap_encrypt: result buffer too small\n"); + coap_log_warn("coap_encrypt: result buffer too small\n"); return 0; } @@ -1649,17 +1630,15 @@ coap_crypto_aead_decrypt(const coap_crypto_param_t *params, assert(params); if (get_cipher_alg(params->alg) == 0) { - coap_log_debug( - "coap_crypto_decrypt: algorithm %d not supported\n", - params->alg); + coap_log_debug("coap_crypto_decrypt: algorithm %d not supported\n", + params->alg); return 0; } ccm = ¶ms->params.aes; if ((*max_result_len + ccm->tag_len) < data->length) { - coap_log_warn( - "coap_decrypt: result buffer too small\n"); + coap_log_warn("coap_decrypt: result buffer too small\n"); return 0; } @@ -1688,8 +1667,7 @@ coap_crypto_aead_decrypt(const coap_crypto_param_t *params, int coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, - coap_bin_const_t *data, coap_bin_const_t **hmac) -{ + coap_bin_const_t *data, coap_bin_const_t **hmac) { dtls_hmac_context_t hmac_context; int num_bytes; coap_binary_t *dummy; @@ -1698,8 +1676,7 @@ coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, return 0; if (get_hmac_alg(hmac_alg) == 0) { - coap_log_debug( - "coap_crypto_hmac: algorithm %d not supported\n", hmac_alg); + coap_log_debug("coap_crypto_hmac: algorithm %d not supported\n", hmac_alg); return 0; } @@ -1729,7 +1706,8 @@ coap_crypto_hmac(cose_hmac_alg_t hmac_alg, coap_bin_const_t *key, */ #pragma GCC diagnostic ignored "-Wunused-function" #endif -static inline void dummy(void) { +static inline void +dummy(void) { } #endif /* COAP_WITH_LIBTINYDTLS */ diff --git a/src/coap_uri.c b/src/coap_uri.c index 4babec0ff4..4b4f8f358e 100644 --- a/src/coap_uri.c +++ b/src/coap_uri.c @@ -121,36 +121,31 @@ coap_split_uri_sub(const uint8_t *str_var, break; case COAP_URI_SCHEME_COAPS: if (!coap_dtls_is_supported()) { - coap_log_err( - "coaps URI scheme not supported in this version of libcoap\n"); + coap_log_err("coaps URI scheme not supported in this version of libcoap\n"); return -1; } break; case COAP_URI_SCHEME_COAP_TCP: if (!coap_tcp_is_supported()) { - coap_log_err( - "coap+tcp URI scheme not supported in this version of libcoap\n"); + coap_log_err("coap+tcp URI scheme not supported in this version of libcoap\n"); return -1; } break; case COAP_URI_SCHEME_COAPS_TCP: if (!coap_tcp_is_supported()) { - coap_log_err( - "coaps+tcp URI scheme not supported in this version of libcoap\n"); + coap_log_err("coaps+tcp URI scheme not supported in this version of libcoap\n"); return -1; } break; case COAP_URI_SCHEME_COAP_WS: if (!coap_ws_is_supported()) { - coap_log_err( - "coap+ws URI scheme not supported in this version of libcoap\n"); + coap_log_err("coap+ws URI scheme not supported in this version of libcoap\n"); return -1; } break; case COAP_URI_SCHEME_COAPS_WS: if (!coap_wss_is_supported()) { - coap_log_err( - "coaps+ws URI scheme not supported in this version of libcoap\n"); + coap_log_err("coaps+ws URI scheme not supported in this version of libcoap\n"); return -1; } break; @@ -172,7 +167,8 @@ coap_split_uri_sub(const uint8_t *str_var, ++p; while (len && *q != ']') { - ++q; --len; + ++q; + --len; } if (!len || *q != ']' || p == q) { @@ -181,7 +177,8 @@ coap_split_uri_sub(const uint8_t *str_var, } COAP_SET_STR(&uri->host, q - p, p); - ++q; --len; + ++q; + --len; } else { /* IPv4 address, FQDN or Unix domain socket */ if (len >= 3 && p[0] == '%' && p[1] == '2' && @@ -298,7 +295,7 @@ coap_uri_into_options(const coap_uri_t *uri, const coap_address_t *dst, /* Add in UriHost if not match (need to strip off &iface) */ size_t uri_host_len = uri->host.length; - uint8_t* cp = (uint8_t*)strchr((const char*)uri->host.s, '%'); + uint8_t *cp = (uint8_t *)strchr((const char *)uri->host.s, '%'); if (cp && (size_t)(cp - uri->host.s) < uri_host_len) /* %iface specified in host name */ @@ -307,11 +304,11 @@ coap_uri_into_options(const coap_uri_t *uri, const coap_address_t *dst, if (coap_print_ip_addr(dst, addr, sizeof(addr)) && (strlen(addr) != uri_host_len || memcmp(addr, uri->host.s, uri_host_len) != 0)) { - /* add Uri-Host */ - coap_insert_optlist(optlist_chain, - coap_new_optlist(COAP_OPTION_URI_HOST, - uri->host.length, - uri->host.s)); + /* add Uri-Host */ + coap_insert_optlist(optlist_chain, + coap_new_optlist(COAP_OPTION_URI_HOST, + uri->host.length, + uri->host.s)); } } /* Add in UriPort if not default */ @@ -328,7 +325,7 @@ coap_uri_into_options(const coap_uri_t *uri, const coap_address_t *dst, break; default: if (uri->port != (coap_uri_scheme_is_secure(uri) ? COAPS_DEFAULT_PORT : - COAP_DEFAULT_PORT)) + COAP_DEFAULT_PORT)) add_option = 1; break; } @@ -336,23 +333,23 @@ coap_uri_into_options(const coap_uri_t *uri, const coap_address_t *dst, coap_insert_optlist(optlist_chain, coap_new_optlist(COAP_OPTION_URI_PORT, coap_encode_var_safe(buf, 4, - (uri->port & 0xffff)), - buf)); + (uri->port & 0xffff)), + buf)); } if (uri->path.length) { if (uri->path.length > buflen) coap_log_warn("URI path will be truncated (max buffer %zu)\n", - buflen); + buflen); res = coap_split_path(uri->path.s, uri->path.length, buf, &buflen); if (res < 0) return -1; while (res--) { coap_insert_optlist(optlist_chain, - coap_new_optlist(COAP_OPTION_URI_PATH, - coap_opt_length(buf), - coap_opt_value(buf))); + coap_new_optlist(COAP_OPTION_URI_PATH, + coap_opt_length(buf), + coap_opt_value(buf))); buf += coap_opt_size(buf); } @@ -363,16 +360,16 @@ coap_uri_into_options(const coap_uri_t *uri, const coap_address_t *dst, buf = _buf; if (uri->query.length > buflen) coap_log_warn("URI query will be truncated (max buffer %zu)\n", - buflen); + buflen); res = coap_split_query(uri->query.s, uri->query.length, buf, &buflen); if (res < 0) return -1; while (res--) { coap_insert_optlist(optlist_chain, - coap_new_optlist(COAP_OPTION_URI_QUERY, - coap_opt_length(buf), - coap_opt_value(buf))); + coap_new_optlist(COAP_OPTION_URI_QUERY, + coap_opt_length(buf), + coap_opt_value(buf))); buf += coap_opt_size(buf); } @@ -421,12 +418,14 @@ decode_segment(const uint8_t *seg, size_t length, unsigned char *buf) { if (*seg == '%') { *buf = (hexchar_to_dec(seg[1]) << 4) + hexchar_to_dec(seg[2]); - seg += 2; length -= 2; + seg += 2; + length -= 2; } else { *buf = *seg; } - ++buf; ++seg; + ++buf; + ++seg; } } @@ -442,13 +441,15 @@ check_segment(const uint8_t *s, size_t length, size_t *segment_size) { while (length) { if (*s == '%') { if (length < 2 || !(isxdigit(s[1]) && isxdigit(s[2]))) - return -1; + return -1; s += 2; length -= 2; } - ++s; ++n; --length; + ++s; + ++n; + --length; } *segment_size = n; @@ -478,7 +479,7 @@ check_segment(const uint8_t *s, size_t length, size_t *segment_size) { */ static int make_decoded_option(const uint8_t *s, size_t length, - unsigned char *buf, size_t buflen, size_t* optionsize) { + unsigned char *buf, size_t buflen, size_t *optionsize) { int res; size_t segmentlen; size_t written; @@ -604,7 +605,7 @@ coap_split_path(const uint8_t *s, size_t length, int coap_split_query(const uint8_t *s, size_t length, - unsigned char *buf, size_t *buflen) { + unsigned char *buf, size_t *buflen) { struct cnt_str tmp = { { *buflen, buf }, 0 }; const uint8_t *p; @@ -632,7 +633,7 @@ coap_uri_t * coap_new_uri(const uint8_t *uri, unsigned int length) { unsigned char *result; - result = (unsigned char*)coap_malloc_type(COAP_STRING, length + 1 + sizeof(coap_uri_t)); + result = (unsigned char *)coap_malloc_type(COAP_STRING, length + 1 + sizeof(coap_uri_t)); if (!result) return NULL; @@ -652,38 +653,38 @@ coap_clone_uri(const coap_uri_t *uri) { coap_uri_t *result; uint8_t *p; - if ( !uri ) + if (!uri) return NULL; result = (coap_uri_t *)coap_malloc_type(COAP_STRING, uri->query.length + uri->host.length + - uri->path.length + sizeof(coap_uri_t) + 1); + uri->path.length + sizeof(coap_uri_t) + 1); - if ( !result ) + if (!result) return NULL; - memset( result, 0, sizeof(coap_uri_t) ); + memset(result, 0, sizeof(coap_uri_t)); result->port = uri->port; - if ( uri->host.length ) { + if (uri->host.length) { result->host.s = p = URI_DATA(result); result->host.length = uri->host.length; memcpy(p, uri->host.s, uri->host.length); } - if ( uri->path.length ) { + if (uri->path.length) { result->path.s = p = URI_DATA(result) + uri->host.length; result->path.length = uri->path.length; memcpy(p, uri->path.s, uri->path.length); } - if ( uri->query.length ) { + if (uri->query.length) { result->query.s = p = URI_DATA(result) + uri->host.length + uri->path.length; result->query.length = uri->query.length; - memcpy (p, uri->query.s, uri->query.length); + memcpy(p, uri->query.s, uri->query.length); } return result; @@ -696,11 +697,11 @@ coap_delete_uri(coap_uri_t *uri) { COAP_STATIC_INLINE int is_unescaped_in_path(const uint8_t c) { - return ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' ) - || ( c >= '0' && c <= '9' ) || c == '-' || c == '.' || c == '_' - || c == '~' || c == '!' || c == '$' || c == '\'' || c == '(' - || c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || c=='=' - || c==':' || c=='@' || c == '&'; + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || + (c >= '0' && c <= '9') || c == '-' || c == '.' || c == '_' || + c == '~' || c == '!' || c == '$' || c == '\'' || c == '(' || + c == ')' || c == '*' || c == '+' || c == ',' || c == ';' || + c=='=' || c==':' || c=='@' || c == '&'; } COAP_STATIC_INLINE int @@ -708,7 +709,8 @@ is_unescaped_in_query(const uint8_t c) { return is_unescaped_in_path(c) || c=='/' || c=='?'; } -coap_string_t *coap_get_query(const coap_pdu_t *request) { +coap_string_t * +coap_get_query(const coap_pdu_t *request) { coap_opt_iterator_t opt_iter; coap_opt_filter_t f; coap_opt_t *q; @@ -758,7 +760,8 @@ coap_string_t *coap_get_query(const coap_pdu_t *request) { return query; } -coap_string_t *coap_get_uri_path(const coap_pdu_t *request) { +coap_string_t * +coap_get_uri_path(const coap_pdu_t *request) { coap_opt_iterator_t opt_iter; coap_opt_filter_t f; coap_opt_t *q; diff --git a/src/coap_ws.c b/src/coap_ws.c index c5eba83ba3..d42e33f4f7 100644 --- a/src/coap_ws.c +++ b/src/coap_ws.c @@ -27,12 +27,12 @@ #endif #define COAP_WS_RESPONSE \ -"HTTP/1.1 101 Switching Protocols\r\n" \ -"Upgrade: websocket\r\n" \ -"Connection: Upgrade\r\n" \ -"Sec-WebSocket-Accept: %s\r\n" \ -"Sec-WebSocket-Protocol: coap\r\n" \ -"\r\n" + "HTTP/1.1 101 Switching Protocols\r\n" \ + "Upgrade: websocket\r\n" \ + "Connection: Upgrade\r\n" \ + "Sec-WebSocket-Accept: %s\r\n" \ + "Sec-WebSocket-Protocol: coap\r\n" \ + "\r\n" int coap_ws_is_supported(void) { @@ -67,20 +67,20 @@ coap_base64_encode_buffer(const uint8_t *string, size_t len, char *encoded, for (i = 0; i < len - 2; i += 3) { *p++ = basis_64[(string[i] >> 2) & 0x3F]; *p++ = basis_64[((string[i] & 0x3) << 4) | - ((int) (string[i + 1] & 0xF0) >> 4)]; + ((int)(string[i + 1] & 0xF0) >> 4)]; *p++ = basis_64[((string[i + 1] & 0xF) << 2) | - ((int) (string[i + 2] & 0xC0) >> 6)]; + ((int)(string[i + 2] & 0xC0) >> 6)]; *p++ = basis_64[string[i + 2] & 0x3F]; } if (i < len) { *p++ = basis_64[(string[i] >> 2) & 0x3F]; if (i == (len - 1)) { - *p++ = basis_64[((string[i] & 0x3) << 4)]; - *p++ = '='; + *p++ = basis_64[((string[i] & 0x3) << 4)]; + *p++ = '='; } else { - *p++ = basis_64[((string[i] & 0x3) << 4) | - ((int) (string[i + 1] & 0xF0) >> 4)]; - *p++ = basis_64[((string[i + 1] & 0xF) << 2)]; + *p++ = basis_64[((string[i] & 0x3) << 4) | + ((int)(string[i + 1] & 0xF0) >> 4)]; + *p++ = basis_64[((string[i + 1] & 0xF) << 2)]; } *p++ = '='; } @@ -128,24 +128,24 @@ coap_base64_decode_buffer(const char *bufcoded, size_t *len, uint8_t *bufplain, while (nprbytes > 4) { *(bufout++) = - (uint8_t) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); + (uint8_t)(pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); *(bufout++) = - (uint8_t) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); + (uint8_t)(pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); *(bufout++) = - (uint8_t) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); + (uint8_t)(pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); bufin += 4; nprbytes -= 4; } /* Note: (nprbytes == 1) would be an error, so just ignore that case */ if (nprbytes > 1) { - *(bufout++) = (uint8_t) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); + *(bufout++) = (uint8_t)(pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); } if (nprbytes > 2) { - *(bufout++) = (uint8_t) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); + *(bufout++) = (uint8_t)(pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); } if (nprbytes > 3) { - *(bufout++) = (uint8_t) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); + *(bufout++) = (uint8_t)(pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); } if (len) @@ -287,17 +287,18 @@ coap_ws_write(coap_session_t *session, const uint8_t *data, size_t datalen) { static char * coap_ws_split_rd_header(coap_session_t *session) { - char *cp = strchr((char*)session->ws->http_hdr, ' '); + char *cp = strchr((char *)session->ws->http_hdr, ' '); if (!cp) - cp = strchr((char*)session->ws->http_hdr, '\t'); + cp = strchr((char *)session->ws->http_hdr, '\t'); if (!cp) return NULL; *cp = '\000'; cp++; - while (isblank(*cp)) cp++; + while (isblank(*cp)) + cp++; return cp; } @@ -307,9 +308,9 @@ coap_ws_rd_http_header_server(coap_session_t *session) { char *value; if (!ws->seen_first) { - if (strcasecmp((char*)ws->http_hdr, - "GET /.well-known/coap HTTP/1.1") != 0) { - coap_log_info("WS: Invalid GET request %s\n", (char*)ws->http_hdr); + if (strcasecmp((char *)ws->http_hdr, + "GET /.well-known/coap HTTP/1.1") != 0) { + coap_log_info("WS: Invalid GET request %s\n", (char *)ws->http_hdr); return 0; } ws->seen_first = 1; @@ -320,14 +321,13 @@ coap_ws_rd_http_header_server(coap_session_t *session) { if (!value) return 0; - if (strcasecmp((char*)ws->http_hdr, "Host:") == 0) { + if (strcasecmp((char *)ws->http_hdr, "Host:") == 0) { if (ws->seen_host) { coap_log_debug("WS: Duplicate Host: header\n"); return 0; } ws->seen_host = 1; - } - else if (strcasecmp((char*)ws->http_hdr, "Upgrade:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Upgrade:") == 0) { if (ws->seen_upg) { coap_log_debug("WS: Duplicate Upgrade: header\n"); return 0; @@ -337,8 +337,7 @@ coap_ws_rd_http_header_server(coap_session_t *session) { return 0; } ws->seen_upg = 1; - } - else if (strcasecmp((char*)ws->http_hdr, "Connection:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Connection:") == 0) { if (ws->seen_conn) { coap_log_debug("WS: Duplicate Connection: header\n"); return 0; @@ -348,7 +347,7 @@ coap_ws_rd_http_header_server(coap_session_t *session) { return 0; } ws->seen_conn = 1; - } else if (strcasecmp((char*)ws->http_hdr, "Sec-WebSocket-Key:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Key:") == 0) { size_t len; if (ws->seen_key) { @@ -363,7 +362,7 @@ coap_ws_rd_http_header_server(coap_session_t *session) { } coap_ws_log_key(session); ws->seen_key = 1; - } else if (strcasecmp((char*)ws->http_hdr, "Sec-WebSocket-Protocol:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Protocol:") == 0) { if (ws->seen_proto) { coap_log_debug("WS: Duplicate Sec-WebSocket-Protocol: header\n"); return 0; @@ -373,7 +372,7 @@ coap_ws_rd_http_header_server(coap_session_t *session) { return 0; } ws->seen_proto = 1; - } else if (strcasecmp((char*)ws->http_hdr, "Sec-WebSocket-Version:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Version:") == 0) { if (ws->seen_ver) { coap_log_debug("WS: Duplicate Sec-WebSocket-Version: header\n"); return 0; @@ -425,9 +424,9 @@ coap_ws_rd_http_header_client(coap_session_t *session) { if (!ws->seen_first) { value = coap_ws_split_rd_header(session); - if (strcmp((char*)ws->http_hdr, "HTTP/1.1") != 0 || + if (strcmp((char *)ws->http_hdr, "HTTP/1.1") != 0 || atoi(value) != 101) { - coap_log_info("WS: Invalid GET response %s\n", (char*)ws->http_hdr); + coap_log_info("WS: Invalid GET response %s\n", (char *)ws->http_hdr); return 0; } ws->seen_first = 1; @@ -438,7 +437,7 @@ coap_ws_rd_http_header_client(coap_session_t *session) { if (!value) return 0; - if (strcasecmp((char*)ws->http_hdr, "Upgrade:") == 0) { + if (strcasecmp((char *)ws->http_hdr, "Upgrade:") == 0) { if (ws->seen_upg) { coap_log_debug("WS: Duplicate Upgrade: header\n"); return 0; @@ -448,8 +447,7 @@ coap_ws_rd_http_header_client(coap_session_t *session) { return 0; } ws->seen_upg = 1; - } - else if (strcasecmp((char*)ws->http_hdr, "Connection:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Connection:") == 0) { if (ws->seen_conn) { coap_log_debug("WS: Duplicate Connection: header\n"); return 0; @@ -459,7 +457,7 @@ coap_ws_rd_http_header_client(coap_session_t *session) { return 0; } ws->seen_conn = 1; - } else if (strcasecmp((char*)ws->http_hdr, "Sec-WebSocket-Accept:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Accept:") == 0) { char hash[30]; if (ws->seen_key) { @@ -473,7 +471,7 @@ coap_ws_rd_http_header_client(coap_session_t *session) { return 0; } ws->seen_key = 1; - } else if (strcasecmp((char*)ws->http_hdr, "Sec-WebSocket-Protocol:") == 0) { + } else if (strcasecmp((char *)ws->http_hdr, "Sec-WebSocket-Protocol:") == 0) { if (ws->seen_proto) { coap_log_debug("WS: Duplicate Sec-WebSocket-Protocol: header\n"); return 0; @@ -506,7 +504,7 @@ coap_ws_rd_http_header(coap_session_t *session) { * some frame info that needs to be subsequently processed */ rem = ws->http_ofs > (sizeof(ws->http_hdr) - 1 - COAP_MAX_FS) ? - sizeof(ws->http_hdr) - ws->http_ofs : COAP_MAX_FS; + sizeof(ws->http_hdr) - ws->http_ofs : COAP_MAX_FS; bytes = session->sock.lfunc[COAP_LAYER_WS].read(session, &ws->http_hdr[ws->http_ofs], rem); @@ -542,7 +540,7 @@ coap_ws_rd_http_header(coap_session_t *session) { } } - rem = ws->http_ofs - ((uint8_t*)cp + 1 - ws->http_hdr); + rem = ws->http_ofs - ((uint8_t *)cp + 1 - ws->http_hdr); if (ws->http_hdr[0] == '\000') { /* Found trailing empty header line */ if (ws->state == COAP_SESSION_TYPE_SERVER) { @@ -601,7 +599,7 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { snprintf(buf, sizeof(buf), "HTTP/1.1 400 Invalid request\r\n\r\n"); coap_log_debug("WS: Response (Fail)\n%s", buf); if (coap_netif_available(session)) { - session->sock.lfunc[COAP_LAYER_WS].write(session, (uint8_t*)buf, + session->sock.lfunc[COAP_LAYER_WS].write(session, (uint8_t *)buf, strlen(buf)); } coap_session_disconnected(session, COAP_NACK_WS_LAYER_FAILED); @@ -619,8 +617,8 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { } snprintf(buf, sizeof(buf), COAP_WS_RESPONSE, hash); coap_log_debug("WS: Response\n%s", buf); - session->sock.lfunc[COAP_LAYER_WS].write(session, (uint8_t*)buf, - strlen(buf)); + session->sock.lfunc[COAP_LAYER_WS].write(session, (uint8_t *)buf, + strlen(buf)); coap_handle_event(session->context, COAP_EVENT_WS_CONNECTED, session); coap_log_debug("WS: established\n"); @@ -637,8 +635,8 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { /* Get WebSockets frame if not already completely in */ if (!session->ws->all_hdr_in) { ret = session->sock.lfunc[COAP_LAYER_WS].read(session, - &session->ws->rd_header[session->ws->hdr_ofs], - sizeof(session->ws->rd_header) - session->ws->hdr_ofs); + &session->ws->rd_header[session->ws->hdr_ofs], + sizeof(session->ws->rd_header) - session->ws->hdr_ofs); if (ret < 0) return ret; session->ws->hdr_ofs += (int)ret; @@ -648,10 +646,10 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { if (session->ws->state == COAP_SESSION_TYPE_SERVER && !(session->ws->rd_header[1] & WS_B1_MASK_BIT)) { - /* Client has failed to mask the data */ - session->ws->close_reason = 1002; - coap_ws_close(session); - return 0; + /* Client has failed to mask the data */ + session->ws->close_reason = 1002; + coap_ws_close(session); + return 0; } bytes_size = session->ws->rd_header[1] & WS_B1_LEN_MASK; @@ -672,7 +670,7 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { op_code = session->ws->rd_header[0] & WS_B0_OP_MASK; if (op_code != WS_OP_BINARY && op_code != WS_OP_CLOSE) { /* Remote has failed to use correct opcode */ - session->ws->close_reason = 1003; + session->ws->close_reason = 1003; coap_ws_close(session); return 0; if (op_code == WS_OP_CLOSE) { @@ -758,8 +756,8 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { /* Get in (remaining) data */ ret = session->sock.lfunc[COAP_LAYER_WS].read(session, - &data[session->ws->data_ofs], - session->ws->data_size - session->ws->data_ofs); + &data[session->ws->data_ofs], + session->ws->data_size - session->ws->data_ofs); if (ret <= 0) return ret; session->ws->data_ofs += ret; @@ -782,14 +780,14 @@ coap_ws_read(coap_session_t *session, uint8_t *data, size_t datalen) { } #define COAP_WS_REQUEST \ -"GET /.well-known/coap HTTP/1.1\r\n" \ -"Host: %s\r\n" \ -"Upgrade: websocket\r\n" \ -"Connection: Upgrade\r\n" \ -"Sec-WebSocket-Key: %s\r\n" \ -"Sec-WebSocket-Protocol: coap\r\n" \ -"Sec-WebSocket-Version: 13\r\n" \ -"\r\n" + "GET /.well-known/coap HTTP/1.1\r\n" \ + "Host: %s\r\n" \ + "Upgrade: websocket\r\n" \ + "Connection: Upgrade\r\n" \ + "Sec-WebSocket-Key: %s\r\n" \ + "Sec-WebSocket-Protocol: coap\r\n" \ + "Sec-WebSocket-Version: 13\r\n" \ + "\r\n" void coap_ws_establish(coap_session_t *session) { @@ -825,7 +823,7 @@ coap_ws_establish(coap_session_t *session) { coap_address_get_port(&session->addr_info.remote) != 443) { port = coap_address_get_port(&session->addr_info.remote); } - if (strchr((const char*)session->ws_host->s, ':')) { + if (strchr((const char *)session->ws_host->s, ':')) { if (port) { snprintf(host, sizeof(host), "[%s]:%d", session->ws_host->s, port); } else { @@ -840,8 +838,8 @@ coap_ws_establish(coap_session_t *session) { } snprintf(buf, sizeof(buf), COAP_WS_REQUEST, host, base64); coap_log_debug("WS Request\n%s", buf); - session->sock.lfunc[COAP_LAYER_WS].write(session, (uint8_t*)buf, - strlen(buf)); + session->sock.lfunc[COAP_LAYER_WS].write(session, (uint8_t *)buf, + strlen(buf)); } else { session->ws->state = COAP_SESSION_TYPE_SERVER; }