diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3b962bdc71..fc4a586d54 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_oscore|examples|tests).*\.(c|cpp|cxx|h|h.in|h.riot|h.riot.in|h.windows|h.windows.in|h.contiki|hpp|inc)$' + files: '^(coap_config|man|include|src/oscore|src/coap_[a-g]|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)$' exclude: '^.*/(coap_uthash_internal.h|coap_utlist_internal.h)$' args: ['--style=google', '--align-pointer=name', diff --git a/src/coap_address.c b/src/coap_address.c index 45c036f817..39c5be36e9 100644 --- a/src/coap_address.c +++ b/src/coap_address.c @@ -39,10 +39,12 @@ coap_address_get_port(const coap_address_t *addr) { assert(addr != NULL); switch (addr->addr.sa.sa_family) { #if COAP_IPV4_SUPPORT - case AF_INET: return ntohs(addr->addr.sin.sin_port); + case AF_INET: + return ntohs(addr->addr.sin.sin_port); #endif /* COAP_IPV4_SUPPORT */ #if COAP_IPV6_SUPPORT - case AF_INET6: return ntohs(addr->addr.sin6.sin6_port); + case AF_INET6: + return ntohs(addr->addr.sin6.sin6_port); #endif /* COAP_IPV6_SUPPORT */ default: /* undefined */ ; @@ -71,30 +73,30 @@ coap_address_set_port(coap_address_t *addr, uint16_t port) { int coap_address_equals(const coap_address_t *a, const coap_address_t *b) { - assert(a); assert(b); + assert(a); + assert(b); if (a->size != b->size || a->addr.sa.sa_family != b->addr.sa.sa_family) return 0; /* need to compare only relevant parts of sockaddr_in6 */ - switch (a->addr.sa.sa_family) { + switch (a->addr.sa.sa_family) { #if COAP_IPV4_SUPPORT - case AF_INET: - return - a->addr.sin.sin_port == b->addr.sin.sin_port && - memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr, - sizeof(struct in_addr)) == 0; + case AF_INET: + return a->addr.sin.sin_port == b->addr.sin.sin_port && + memcmp(&a->addr.sin.sin_addr, &b->addr.sin.sin_addr, + sizeof(struct in_addr)) == 0; #endif /* COAP_IPV4_SUPPORT */ #if COAP_IPV6_SUPPORT - case AF_INET6: - return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port && - memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr, - sizeof(struct in6_addr)) == 0; + case AF_INET6: + return a->addr.sin6.sin6_port == b->addr.sin6.sin6_port && + memcmp(&a->addr.sin6.sin6_addr, &b->addr.sin6.sin6_addr, + sizeof(struct in6_addr)) == 0; #endif /* COAP_IPV6_SUPPORT */ - default: /* fall through and signal error */ - ; - } - return 0; + default: /* fall through and signal error */ + ; + } + return 0; } int @@ -115,7 +117,7 @@ coap_is_mcast(const coap_address_t *a) { case AF_INET6: #if COAP_IPV4_SUPPORT return IN6_IS_ADDR_MULTICAST(&a->addr.sin6.sin6_addr) || - (IN6_IS_ADDR_V4MAPPED(&a->addr.sin6.sin6_addr) && + (IN6_IS_ADDR_V4MAPPED(&a->addr.sin6.sin6_addr) && IN_MULTICAST(ntohl(a->addr.sin6.sin6_addr.s6_addr[12]))); #else /* ! COAP_IPV4_SUPPORT */ return a->addr.sin6.sin6_addr.s6_addr[0] == 0xff; @@ -226,7 +228,8 @@ coap_is_bcast(const coap_address_t *a) { #endif /* !defined(WITH_CONTIKI) && !defined(WITH_LWIP) */ -void coap_address_init(coap_address_t *addr) { +void +coap_address_init(coap_address_t *addr) { assert(addr); memset(addr, 0, sizeof(coap_address_t)); #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) @@ -237,7 +240,7 @@ void coap_address_init(coap_address_t *addr) { int coap_address_set_unix_domain(coap_address_t *addr, - const uint8_t *host, size_t host_len) { + const uint8_t *host, size_t host_len) { #if COAP_AF_UNIX_SUPPORT size_t i; size_t ofs = 0; @@ -330,12 +333,24 @@ coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, switch (use_unix_proto) { /* For AF_UNIX, can only listen on a single endpoint */ - case COAP_PROTO_UDP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; break; - case COAP_PROTO_TCP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP; break; - case COAP_PROTO_DTLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS; break; - case COAP_PROTO_TLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP; break; - case COAP_PROTO_WS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_WS; break; - case COAP_PROTO_WSS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS; break; + case COAP_PROTO_UDP: + scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; + break; + case COAP_PROTO_TCP: + scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP; + break; + case COAP_PROTO_DTLS: + scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS; + break; + case COAP_PROTO_TLS: + scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP; + break; + case COAP_PROTO_WS: + scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_WS; + break; + case COAP_PROTO_WSS: + scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS; + break; case COAP_PROTO_NONE: /* If use_unix_proto was not defined */ case COAP_PROTO_LAST: default: @@ -445,7 +460,7 @@ coap_resolve_address_info(const coap_str_const_t *address, else memcpy(addrstr, "localhost", 9); - memset ((char *)&hints, 0, sizeof(hints)); + memset((char *)&hints, 0, sizeof(hints)); hints.ai_socktype = 0; hints.ai_family = AF_UNSPEC; hints.ai_flags = ai_hints_flags; @@ -782,12 +797,12 @@ coap_free_address_info(coap_addr_info_t *info) { void coap_address_copy(coap_address_t *dst, const coap_address_t *src) { #if defined(WITH_LWIP) || defined(WITH_CONTIKI) - memcpy( dst, src, sizeof( coap_address_t ) ); + memcpy(dst, src, sizeof(coap_address_t)); #else - memset( dst, 0, sizeof( coap_address_t ) ); + memset(dst, 0, sizeof(coap_address_t)); dst->size = src->size; #if COAP_IPV6_SUPPORT - if ( src->addr.sa.sa_family == AF_INET6 ) { + if (src->addr.sa.sa_family == AF_INET6) { dst->addr.sin6.sin6_family = src->addr.sin6.sin6_family; dst->addr.sin6.sin6_addr = src->addr.sin6.sin6_addr; dst->addr.sin6.sin6_port = src->addr.sin6.sin6_port; @@ -798,13 +813,13 @@ coap_address_copy(coap_address_t *dst, const coap_address_t *src) { else #endif /* COAP_IPV4_SUPPORT && COAP_IPV6_SUPPORT */ #if COAP_IPV4_SUPPORT - if ( src->addr.sa.sa_family == AF_INET ) { - dst->addr.sin = src->addr.sin; - } + if (src->addr.sa.sa_family == AF_INET) { + dst->addr.sin = src->addr.sin; + } #endif /* COAP_IPV4_SUPPORT */ - else { - memcpy( &dst->addr, &src->addr, src->size ); - } + else { + memcpy(&dst->addr, &src->addr, src->size); + } #endif } diff --git a/src/coap_asn1.c b/src/coap_asn1.c index e54fa4944a..2dc16abc1a 100644 --- a/src/coap_asn1.c +++ b/src/coap_asn1.c @@ -16,8 +16,7 @@ #include "coap3/coap_internal.h" size_t -asn1_len(const uint8_t **ptr) -{ +asn1_len(const uint8_t **ptr) { size_t len = 0; if ((**ptr) & 0x80) { @@ -28,8 +27,7 @@ asn1_len(const uint8_t **ptr) (*ptr)++; octets--; } - } - else { + } else { len = (**ptr) & 0x7f; (*ptr)++; } @@ -37,8 +35,7 @@ asn1_len(const uint8_t **ptr) } coap_asn1_tag_t -asn1_tag_c(const uint8_t **ptr, int *constructed, int *cls) -{ +asn1_tag_c(const uint8_t **ptr, int *constructed, int *cls) { coap_asn1_tag_t tag = 0; uint8_t byte; @@ -66,8 +63,7 @@ asn1_tag_c(const uint8_t **ptr, int *constructed, int *cls) /* caller must free off returned coap_binary_t* */ coap_binary_t * get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, - asn1_validate validate) -{ + asn1_validate validate) { int constructed; int class; const uint8_t *acp = ptr; diff --git a/src/coap_async.c b/src/coap_async.c index d21894e29b..ed12547ba2 100644 --- a/src/coap_async.c +++ b/src/coap_async.c @@ -28,7 +28,7 @@ if ((out)->field1 == (val1) && (out)->field2 == (val2) && \ ((val2) == 0 || memcmp((out)->field3, (val3), (val2)) == 0)) break; \ } \ -} while(0) + } while(0) int coap_async_is_supported(void) { @@ -60,10 +60,9 @@ coap_register_async(coap_session_t *session, /* Output maybe truncated */ outbuflen = strlen(outbuf); snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, - "%02x", request->token[i]); + "%02x", request->token[i]); } - coap_log_debug( - "asynchronous state for token '%s' already registered\n", outbuf); + coap_log_debug("asynchronous state for token '%s' already registered\n", outbuf); return NULL; } @@ -90,7 +89,7 @@ coap_register_async(coap_session_t *session, coap_add_data(s->pdu, len, data); } - s->session = coap_session_reference( session ); + s->session = coap_session_reference(session); coap_async_set_delay(s, delay); @@ -103,7 +102,7 @@ coap_async_trigger(coap_async_t *async) { coap_ticks(&async->delay); coap_log_debug(" %s: Async request triggered\n", - coap_session_str(async->session)); + coap_session_str(async->session)); #ifdef COAP_EPOLL_SUPPORT coap_update_epoll_timer(async->session->context, 0); #endif /* COAP_EPOLL_SUPPORT */ @@ -123,15 +122,14 @@ coap_async_set_delay(coap_async_t *async, coap_tick_t delay) { coap_update_epoll_timer(async->session->context, delay); #endif /* COAP_EPOLL_SUPPORT */ coap_log_debug(" %s: Async request delayed for %u.%03u secs\n", - coap_session_str(async->session), - (unsigned int)(delay / COAP_TICKS_PER_SECOND), - (unsigned int)((delay % COAP_TICKS_PER_SECOND) * - 1000 / COAP_TICKS_PER_SECOND)); - } - else { + coap_session_str(async->session), + (unsigned int)(delay / COAP_TICKS_PER_SECOND), + (unsigned int)((delay % COAP_TICKS_PER_SECOND) * + 1000 / COAP_TICKS_PER_SECOND)); + } else { async->delay = 0; coap_log_debug(" %s: Async request indefinately delayed\n", - coap_session_str(async->session)); + coap_session_str(async->session)); } } diff --git a/src/coap_block.c b/src/coap_block.c index f09df89e10..4223f6dd60 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -75,7 +75,7 @@ coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, if (session == NULL || COAP_PROTO_NOT_RELIABLE(session->proto) || !(session->csm_bert_rem_support && session->csm_bert_loc_support)) - /* No BERT support */ + /* No BERT support */ return 0; block->szx = 6; /* BERT is 1024 block chunks */ @@ -151,13 +151,11 @@ setup_block_b(coap_session_t *session, coap_pdu_t *pdu, coap_block_b_t *block, int new_blk_size; if (avail < 16) { /* bad luck, this is the smallest block size */ - coap_log_debug( - "not enough space, even the smallest block does not fit (1)\n"); + coap_log_debug("not enough space, even the smallest block does not fit (1)\n"); return 0; } new_blk_size = coap_flsll((long long)avail) - 5; - coap_log_debug( - "decrease block size for %zu to %d\n", avail, new_blk_size); + coap_log_debug("decrease block size for %zu to %d\n", avail, new_blk_size); szx = block->szx; block->szx = new_blk_size; block->num <<= szx - block->szx; @@ -194,9 +192,9 @@ coap_write_block_opt(coap_block_t *block, coap_option_num_t number, /* to re-encode the block option */ coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf), ((block_b.num << 4) | - (block_b.m << 3) | - block_b.szx)), - buf); + (block_b.m << 3) | + block_b.szx)), + buf); return 1; } @@ -219,15 +217,15 @@ coap_write_block_b_opt(coap_session_t *session, coap_block_b_t *block, assert(pdu->max_size > 0); if (!setup_block_b(session, pdu, block, block->num, - block->szx, data_length)) + block->szx, data_length)) return -3; /* to re-encode the block option */ coap_update_option(pdu, number, coap_encode_var_safe(buf, sizeof(buf), ((block->num << 4) | - (block->m << 3) | - block->aszx)), - buf); + (block->m << 3) | + block->aszx)), + buf); return 1; } @@ -273,12 +271,12 @@ coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data, */ void coap_add_data_blocked_response(const coap_pdu_t *request, - coap_pdu_t *response, - uint16_t media_type, - int maxage, - size_t length, - const uint8_t* data -) { + coap_pdu_t *response, + uint16_t media_type, + int maxage, + size_t length, + const uint8_t *data + ) { coap_key_t etag; unsigned char buf[4]; coap_block_t block2; @@ -294,8 +292,8 @@ coap_add_data_blocked_response(const coap_pdu_t *request, block2_requested = 1; if (block2.num != 0 && length <= (block2.num << (block2.szx + 4))) { coap_log_debug("Illegal block requested (%d > last = %zu)\n", - block2.num, - length >> (block2.szx + 4)); + block2.num, + length >> (block2.szx + 4)); response->code = COAP_RESPONSE_CODE(400); goto error; } @@ -309,14 +307,14 @@ coap_add_data_blocked_response(const coap_pdu_t *request, coap_insert_option(response, COAP_OPTION_ETAG, sizeof(etag), etag); coap_insert_option(response, COAP_OPTION_CONTENT_FORMAT, - coap_encode_var_safe(buf, sizeof(buf), - media_type), - buf); + coap_encode_var_safe(buf, sizeof(buf), + media_type), + buf); if (maxage >= 0) { coap_insert_option(response, - COAP_OPTION_MAXAGE, - coap_encode_var_safe(buf, sizeof(buf), maxage), buf); + COAP_OPTION_MAXAGE, + coap_encode_var_safe(buf, sizeof(buf), maxage), buf); } if (block2_requested) { @@ -326,16 +324,16 @@ coap_add_data_blocked_response(const coap_pdu_t *request, switch (res) { case -2: /* illegal block (caught above) */ - response->code = COAP_RESPONSE_CODE(400); - goto error; + response->code = COAP_RESPONSE_CODE(400); + goto error; case -1: /* should really not happen */ - assert(0); - /* fall through if assert is a no-op */ + assert(0); + /* fall through if assert is a no-op */ case -3: /* cannot handle request */ - response->code = COAP_RESPONSE_CODE(500); - goto error; + response->code = COAP_RESPONSE_CODE(500); + goto error; default: /* everything is good */ - ; + ; } coap_add_option_internal(response, @@ -379,7 +377,7 @@ coap_add_data_blocked_response(const coap_pdu_t *request, void coap_context_set_block_mode(coap_context_t *context, - uint8_t block_mode) { + uint8_t block_mode) { context->block_mode = (block_mode & (COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY | #if COAP_Q_BLOCK_SUPPORT @@ -397,7 +395,7 @@ coap_context_set_block_mode(coap_context_t *context, COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, - const uint8_t *b, size_t blen) { + const uint8_t *b, size_t blen) { return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0); } @@ -413,16 +411,15 @@ coap_cancel_observe(coap_session_t *session, coap_binary_t *token, return 0; if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) { - coap_log_debug( - "** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n", - coap_session_str(session)); + coap_log_debug("** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n", + coap_session_str(session)); return 0; } LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) { if (lg_crcv->observe_set) { if ((!token && !lg_crcv->app_token->length) || (token && - coap_binary_equal(token, lg_crcv->app_token))) { + coap_binary_equal(token, lg_crcv->app_token))) { uint8_t buf[8]; coap_mid_t mid; size_t size; @@ -433,15 +430,15 @@ coap_cancel_observe(coap_session_t *session, coap_binary_t *token, COAP_OPTION_Q_BLOCK1, &block); #endif /* COAP_Q_BLOCK_SUPPORT */ coap_bin_const_t *otoken = lg_crcv->obs_token ? - lg_crcv->obs_token[0] ? - lg_crcv->obs_token[0] : - (coap_bin_const_t *)lg_crcv->app_token : - (coap_bin_const_t *)lg_crcv->app_token; - coap_pdu_t * pdu = coap_pdu_duplicate(&lg_crcv->pdu, - session, - otoken->length, - otoken->s, - NULL); + lg_crcv->obs_token[0] ? + lg_crcv->obs_token[0] : + (coap_bin_const_t *)lg_crcv->app_token : + (coap_bin_const_t *)lg_crcv->app_token; + coap_pdu_t *pdu = coap_pdu_duplicate(&lg_crcv->pdu, + session, + otoken->length, + otoken->s, + NULL); lg_crcv->observe_set = 0; if (pdu == NULL) @@ -477,8 +474,7 @@ coap_cancel_observe(coap_session_t *session, coap_binary_t *token, /* See if large xmit using Q-Block1 (but not testing Q-Block1) */ if (using_q_block1) { mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU); - } - else { + } else { mid = coap_send_internal(session, pdu); } #else /* ! COAP_Q_BLOCK_SUPPORT */ @@ -497,8 +493,7 @@ coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_mid_t coap_retransmit_oscore_pdu(coap_session_t *session, coap_pdu_t *pdu, - coap_opt_t* echo) -{ + coap_opt_t *echo) { coap_lg_crcv_t *lg_crcv; uint64_t token_match = STATE_TOKEN_BASE(coap_decode_var_bytes8(pdu->actual_token.s, @@ -513,7 +508,7 @@ coap_retransmit_oscore_pdu(coap_session_t *session, LL_FOREACH(session->lg_crcv, lg_crcv) { if (token_match != STATE_TOKEN_BASE(lg_crcv->state_token) && - !coap_binary_equal(&pdu->actual_token, lg_crcv->app_token)) { + !coap_binary_equal(&pdu->actual_token, lg_crcv->app_token)) { /* try out the next one */ continue; } @@ -558,8 +553,7 @@ coap_lg_xmit_t * coap_find_lg_xmit_response(const coap_session_t *session, const coap_pdu_t *request, const coap_resource_t *resource, - const coap_string_t *query) -{ + const coap_string_t *query) { coap_lg_xmit_t *lg_xmit; coap_opt_iterator_t opt_iter; coap_opt_t *rtag_opt = coap_check_option(request, @@ -576,7 +570,7 @@ coap_find_lg_xmit_response(const coap_session_t *session, request->code != lg_xmit->b.b2.request_method || !coap_string_equal(query ? query : &empty, lg_xmit->b.b2.query ? - lg_xmit->b.b2.query : &empty)) { + lg_xmit->b.b2.query : &empty)) { /* try out the next one */ continue; } @@ -635,9 +629,8 @@ coap_add_data_large_internal(coap_session_t *session, } if (!(session->block_mode & COAP_BLOCK_USE_LIBCOAP)) { - coap_log_debug( - "** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n", - coap_session_str(session)); + coap_log_debug("** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n", + coap_session_str(session)); goto add_data; } @@ -653,8 +646,7 @@ coap_add_data_large_internal(coap_session_t *session, #define MAX_BLK_LEN (((1 << 20) - 1) * (1 << (6 + 4))) if (length > MAX_BLK_LEN) { - coap_log_warn( - "Size of large buffer restricted to 0x%x bytes\n", MAX_BLK_LEN); + coap_log_warn("Size of large buffer restricted to 0x%x bytes\n", MAX_BLK_LEN); length = MAX_BLK_LEN; } @@ -666,14 +658,13 @@ coap_add_data_large_internal(coap_session_t *session, if (session->block_mode & (COAP_BLOCK_HAS_Q_BLOCK|COAP_BLOCK_TRY_Q_BLOCK)) { option = COAP_OPTION_Q_BLOCK1; alt_option = COAP_OPTION_BLOCK1; - } - else { + } else { option = COAP_OPTION_BLOCK1; alt_option = COAP_OPTION_Q_BLOCK1; } #else /* ! COAP_Q_BLOCK_SUPPORT */ if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK1, &block)) { - coap_remove_option(pdu, COAP_OPTION_Q_BLOCK1); + coap_remove_option(pdu, COAP_OPTION_Q_BLOCK1); } option = COAP_OPTION_BLOCK1; #endif /* ! COAP_Q_BLOCK_SUPPORT */ @@ -689,22 +680,20 @@ coap_add_data_large_internal(coap_session_t *session, break; } } - } - else { + } else { /* Have to assume that it is a response even if code is 0.00 */ assert(resource); #if COAP_Q_BLOCK_SUPPORT if (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) { option = COAP_OPTION_Q_BLOCK2; alt_option = COAP_OPTION_BLOCK2; - } - else { + } else { option = COAP_OPTION_BLOCK2; alt_option = COAP_OPTION_Q_BLOCK2; } #else /* ! COAP_Q_BLOCK_SUPPORT */ if (coap_get_block_b(session, pdu, COAP_OPTION_Q_BLOCK2, &block)) { - coap_remove_option(pdu, COAP_OPTION_Q_BLOCK2); + coap_remove_option(pdu, COAP_OPTION_Q_BLOCK2); } option = COAP_OPTION_BLOCK2; #endif /* ! COAP_Q_BLOCK_SUPPORT */ @@ -755,11 +744,9 @@ coap_add_data_large_internal(coap_session_t *session, if (coap_get_block_b(session, pdu, alt_option, &alt_block)) { if (have_block_defined) { /* Cannot have both options set */ - coap_log_warn( - "Both BlockX and Q-BlockX cannot be set at the same time\n"); + coap_log_warn("Both BlockX and Q-BlockX cannot be set at the same time\n"); coap_remove_option(pdu, alt_option); - } - else { + } else { block = alt_block; if (block.szx < blk_size) blk_size = block.szx; @@ -771,8 +758,7 @@ coap_add_data_large_internal(coap_session_t *session, if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) { /* bad luck, this is the smallest block size */ - coap_log_debug( - "not enough space, even the smallest block does not fit (2)\n"); + coap_log_debug("not enough space, even the smallest block does not fit (2)\n"); goto fail; } @@ -791,8 +777,7 @@ coap_add_data_large_internal(coap_session_t *session, } if (release_func) release_func(session, app_ptr); - } - else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) { + } else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) { /* Only add in lg_xmit if more than one block needs to be handled */ size_t rem; @@ -809,7 +794,7 @@ coap_add_data_large_internal(coap_session_t *session, pdu->body_length = 0; coap_log_debug("** %s: lg_xmit %p initialized\n", - coap_session_str(session), (void*)lg_xmit); + coap_session_str(session), (void *)lg_xmit); /* Update lg_xmit with large data information */ memset(lg_xmit, 0, sizeof(coap_lg_xmit_t)); lg_xmit->blk_size = blk_size; @@ -818,7 +803,7 @@ coap_add_data_large_internal(coap_session_t *session, lg_xmit->length = length; #if COAP_Q_BLOCK_SUPPORT lg_xmit->non_timeout_random_ticks = - coap_get_non_timeout_random_ticks(session); + coap_get_non_timeout_random_ticks(session); #endif /* COAP_Q_BLOCK_SUPPORT */ lg_xmit->release_func = release_func; lg_xmit->app_ptr = app_ptr; @@ -874,8 +859,7 @@ coap_add_data_large_internal(coap_session_t *session, sizeof(lg_xmit->b.b2.rtag)); memcpy(lg_xmit->b.b2.rtag, coap_opt_value(opt), coap_opt_length(opt)); lg_xmit->b.b2.rtag_set = 1; - } - else { + } else { lg_xmit->b.b2.rtag_set = 0; } lg_xmit->b.b2.etag = etag; @@ -885,8 +869,7 @@ coap_add_data_large_internal(coap_session_t *session, coap_ticks(&now); lg_xmit->b.b2.maxage_expire = coap_ticks_to_rt(now) + maxage; - } - else { + } else { lg_xmit->b.b2.maxage_expire = 0; } coap_update_option(pdu, @@ -913,13 +896,13 @@ coap_add_data_large_internal(coap_session_t *session, coap_update_option(pdu, lg_xmit->option, coap_encode_var_safe(buf, sizeof(buf), - (block.num << 4) | (block.m << 3) | block.aszx), + (block.num << 4) | (block.m << 3) | block.aszx), buf); /* Set up skeletal PDU to use as a basis for all the subsequent blocks */ memcpy(&lg_xmit->pdu, pdu, sizeof(lg_xmit->pdu)); lg_xmit->pdu.token = coap_malloc_type(COAP_PDU_BUF, - lg_xmit->pdu.used_size + lg_xmit->pdu.max_hdr_size); + lg_xmit->pdu.used_size + lg_xmit->pdu.max_hdr_size); if (!lg_xmit->pdu.token) goto fail; @@ -929,7 +912,7 @@ coap_add_data_large_internal(coap_session_t *session, if (pdu->data) lg_xmit->pdu.data = lg_xmit->pdu.token + (pdu->data - pdu->token); lg_xmit->pdu.actual_token.s = lg_xmit->pdu.token + pdu->e_token_length - - pdu->actual_token.length; + pdu->actual_token.length; lg_xmit->pdu.actual_token.length = pdu->actual_token.length; /* Check we still have space after adding in some options */ @@ -945,8 +928,7 @@ coap_add_data_large_internal(coap_session_t *session, if (avail < (ssize_t)chunk) { /* chunk size change down */ if (avail < 16) { - coap_log_warn( - "not enough space, even the smallest block does not fit (3)\n"); + coap_log_warn("not enough space, even the smallest block does not fit (3)\n"); goto fail; } blk_size = coap_flsll((long long)avail) - 4 - 1; @@ -956,10 +938,10 @@ coap_add_data_large_internal(coap_session_t *session, block.chunk_size = (uint32_t)chunk; block.bert = 0; coap_update_option(pdu, - lg_xmit->option, - coap_encode_var_safe(buf, sizeof(buf), - (block.num << 4) | (block.m << 3) | lg_xmit->blk_size), - buf); + lg_xmit->option, + coap_encode_var_safe(buf, sizeof(buf), + (block.num << 4) | (block.m << 3) | lg_xmit->blk_size), + buf); } rem = block.chunk_size; @@ -975,8 +957,7 @@ coap_add_data_large_internal(coap_session_t *session, /* Link the new lg_xmit in */ LL_PREPEND(session->lg_xmit,lg_xmit); - } - else { + } else { /* No need to use blocks */ if (etag) { coap_update_option(pdu, @@ -986,9 +967,9 @@ coap_add_data_large_internal(coap_session_t *session, } if (have_block_defined) { coap_update_option(pdu, - option, - coap_encode_var_safe(buf, sizeof(buf), - (0 << 4) | (0 << 3) | blk_size), buf); + option, + coap_encode_var_safe(buf, sizeof(buf), + (0 << 4) | (0 << 3) | blk_size), buf); } add_data: if (!coap_add_data(pdu, length, data)) @@ -1044,14 +1025,14 @@ coap_add_data_large_response(coap_resource_t *resource, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr -) { + ) { unsigned char buf[4]; coap_block_b_t block; int block_requested = 0; int single_request = 0; #if COAP_Q_BLOCK_SUPPORT uint16_t block_opt = (session->block_mode & COAP_BLOCK_HAS_Q_BLOCK) ? - COAP_OPTION_Q_BLOCK2 : COAP_OPTION_BLOCK2; + COAP_OPTION_Q_BLOCK2 : COAP_OPTION_BLOCK2; #else /* ! COAP_Q_BLOCK_SUPPORT */ uint16_t block_opt = COAP_OPTION_BLOCK2; #endif /* ! COAP_Q_BLOCK_SUPPORT */ @@ -1066,8 +1047,8 @@ coap_add_data_large_response(coap_resource_t *resource, block_requested = 1; if (block.num != 0 && length <= (block.num << (block.szx + 4))) { coap_log_debug("Illegal block requested (%d > last = %zu)\n", - block.num, - length >> (block.szx + 4)); + block.num, + length >> (block.szx + 4)); response->code = COAP_RESPONSE_CODE(400); goto error; } @@ -1077,8 +1058,8 @@ coap_add_data_large_response(coap_resource_t *resource, block_requested = 1; if (block.num != 0 && length <= (block.num << (block.szx + 4))) { coap_log_debug("Illegal block requested (%d > last = %zu)\n", - block.num, - length >> (block.szx + 4)); + block.num, + length >> (block.szx + 4)); response->code = COAP_RESPONSE_CODE(400); goto error; } @@ -1111,16 +1092,16 @@ coap_add_data_large_response(coap_resource_t *resource, switch (res) { case -2: /* illegal block (caught above) */ - response->code = COAP_RESPONSE_CODE(400); - goto error; + response->code = COAP_RESPONSE_CODE(400); + goto error; case -1: /* should really not happen */ - assert(0); - /* fall through if assert is a no-op */ + assert(0); + /* fall through if assert is a no-op */ case -3: /* cannot handle request */ - response->code = COAP_RESPONSE_CODE(500); - goto error; + response->code = COAP_RESPONSE_CODE(500); + goto error; default: /* everything is good */ - ; + ; } } @@ -1174,23 +1155,20 @@ coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, /* Expire this entry */ LL_DELETE(session->lg_xmit, p); coap_block_delete_lg_xmit(session, p); - } - else { + } else { /* Delay until the lg_xmit needs to expire */ if (*tim_rem > p->last_all_sent + idle_timeout - now) { *tim_rem = p->last_all_sent + idle_timeout - now; ret = 1; } } - } - else if (p->last_sent) { + } else if (p->last_sent) { if (p->last_sent + partial_timeout <= now) { /* Expire this entry */ LL_DELETE(session->lg_xmit, p); coap_block_delete_lg_xmit(session, p); coap_handle_event(session->context, COAP_EVENT_XMIT_BLOCK_FAIL, session); - } - else { + } else { /* Delay until the lg_xmit needs to expire */ if (*tim_rem > p->last_sent + partial_timeout - now) { *tim_rem = p->last_sent + partial_timeout - now; @@ -1258,7 +1236,7 @@ coap_request_missing_q_block2(coap_session_t *session, coap_lg_crcv_t *lg_crcv) } coap_insert_option(pdu, COAP_OPTION_Q_BLOCK2, coap_encode_var_safe(buf, sizeof(buf), - (block << 4) | (1 << 3) | lg_crcv->szx), + (block << 4) | (1 << 3) | lg_crcv->szx), buf); block_payload_set = block / COAP_MAX_PAYLOADS(session); goto send_it; @@ -1279,10 +1257,10 @@ coap_request_missing_q_block2(coap_session_t *session, coap_lg_crcv_t *lg_crcv) if (block_payload_set == -1) block_payload_set = block / COAP_MAX_PAYLOADS(session); for (; block < (int)lg_crcv->rec_blocks.range[i].begin && - block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) { + block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) { coap_insert_option(pdu, COAP_OPTION_Q_BLOCK2, coap_encode_var_safe(buf, sizeof(buf), - (block << 4) | (0 << 3) | lg_crcv->szx), + (block << 4) | (0 << 3) | lg_crcv->szx), buf); } } @@ -1304,10 +1282,10 @@ coap_request_missing_q_block2(coap_session_t *session, coap_lg_crcv_t *lg_crcv) if (block_payload_set == -1) block_payload_set = block / COAP_MAX_PAYLOADS(session); for (; block < (ssize_t)sofar && - block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) { + block_payload_set == (block / COAP_MAX_PAYLOADS(session)); block++) { coap_insert_option(pdu, COAP_OPTION_Q_BLOCK2, coap_encode_var_safe(buf, sizeof(buf), - (block << 4) | (0 << 3) | lg_crcv->szx), + (block << 4) | (0 << 3) | lg_crcv->szx), buf); } } @@ -1351,7 +1329,7 @@ coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now, #if COAP_Q_BLOCK_SUPPORT if (p->block_option == COAP_OPTION_Q_BLOCK2 && p->rec_blocks.used) { size_t scaled_timeout = receive_timeout * - ((size_t)1 << p->rec_blocks.retry); + ((size_t)1 << p->rec_blocks.retry); if (p->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) { /* Done NON_MAX_RETRANSMIT retries */ @@ -1363,8 +1341,7 @@ coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now, } if (p->rec_blocks.last_seen + scaled_timeout <= now) { coap_request_missing_q_block2(session, p); - } - else { + } else { if (*tim_rem > p->rec_blocks.last_seen + scaled_timeout - now) { *tim_rem = p->rec_blocks.last_seen + scaled_timeout - now; ret = 1; @@ -1382,8 +1359,7 @@ coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now, /* Expire this entry */ LL_DELETE(session->lg_crcv, p); coap_block_delete_lg_crcv(session, p); - } - else if (!p->observe_set && p->last_used) { + } else if (!p->observe_set && p->last_used) { /* Delay until the lg_crcv needs to expire */ if (*tim_rem > p->last_used + partial_timeout - now) { *tim_rem = p->last_used + partial_timeout - now; @@ -1409,10 +1385,10 @@ pdu_408_build(coap_session_t *session, coap_lg_srcv_t *p) { if (!pdu) return NULL; if (p->last_token) - coap_add_token(pdu, p->last_token->length, p->last_token->s); + coap_add_token(pdu, p->last_token->length, p->last_token->s); coap_add_option_internal(pdu, COAP_OPTION_CONTENT_TYPE, coap_encode_var_safe(buf, sizeof(buf), - COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ), + COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ), buf); pdu->token[pdu->used_size++] = COAP_PAYLOAD_START; pdu->data = pdu->token + pdu->used_size; @@ -1428,23 +1404,19 @@ add_408_block(coap_pdu_t *pdu, int block) { if (block < 0 || block >= (1 << 20)) { return 0; - } - else if (block < 24) { + } else if (block < 24) { len = 1; val[0] = block; - } - else if (block < 0x100) { + } else if (block < 0x100) { len = 2; val[0] = 24; val[1] = block; - } - else if (block < 0x10000) { + } else if (block < 0x10000) { len = 3; val[0] = 25; val[1] = block >> 8; val[2] = block & 0xff; - } - else { /* Largest block number is 2^^20 - 1 */ + } else { /* Largest block number is 2^^20 - 1 */ len = 4; val[0] = 26; val[1] = block >> 16; @@ -1499,7 +1471,7 @@ check_all_blocks_in_for_payload_set(coap_session_t *session, coap_rblock_t *rec_blocks) { if (rec_blocks->used && (rec_blocks->range[0].end + 1) / COAP_MAX_PAYLOADS(session) > - rec_blocks->processing_payload_set) + rec_blocks->processing_payload_set) return 1; return 0; } @@ -1509,7 +1481,7 @@ check_any_blocks_next_payload_set(coap_session_t *session, coap_rblock_t *rec_blocks) { if (rec_blocks->used > 1 && rec_blocks->range[1].begin / COAP_MAX_PAYLOADS(session) == - rec_blocks->processing_payload_set) + rec_blocks->processing_payload_set) return 1; return 0; } @@ -1547,7 +1519,7 @@ coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now, #if COAP_Q_BLOCK_SUPPORT if (p->block_option == COAP_OPTION_Q_BLOCK1 && p->rec_blocks.used) { size_t scaled_timeout = receive_timeout * - ((size_t)1 << p->rec_blocks.retry); + ((size_t)1 << p->rec_blocks.retry); if (p->rec_blocks.retry >= COAP_NON_MAX_RETRANSMIT(session)) { /* Done NON_MAX_RETRANSMIT retries */ @@ -1646,8 +1618,7 @@ coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now, /* Expire this entry */ LL_DELETE(session->lg_srcv, p); coap_block_delete_lg_srcv(session, p); - } - else if (p->last_used) { + } else if (p->last_used) { /* Delay until the lg_srcv needs to expire */ if (*tim_rem > p->last_used + partial_timeout - now) { *tim_rem = p->last_used + partial_timeout - now; @@ -1686,7 +1657,7 @@ coap_send_q_blocks(coap_session_t *session, coap_queue_t *delayqueue; delayqueue_cnt = session->con_active + - (send_pdu == COAP_SEND_INC_PDU ? 1 : 0); + (send_pdu == COAP_SEND_INC_PDU ? 1 : 0); LL_FOREACH(session->delayqueue, delayqueue) { delayqueue_cnt++; } @@ -1695,18 +1666,17 @@ coap_send_q_blocks(coap_session_t *session, if (block.m && ((pdu->type == COAP_MESSAGE_NON && ((block.num + 1) % COAP_MAX_PAYLOADS(session)) + 1 != - COAP_MAX_PAYLOADS(session)) || + COAP_MAX_PAYLOADS(session)) || (pdu->type == COAP_MESSAGE_ACK && - lg_xmit->option == COAP_OPTION_Q_BLOCK2) || + lg_xmit->option == COAP_OPTION_Q_BLOCK2) || (pdu->type == COAP_MESSAGE_CON && - delayqueue_cnt < COAP_NSTART(session)) || + delayqueue_cnt < COAP_NSTART(session)) || COAP_PROTO_RELIABLE(session->proto))) { /* Allocate next pdu if there is headroom */ if (COAP_PDU_IS_RESPONSE(pdu)) { ptoken = pdu->actual_token.s; ltoken_length = pdu->actual_token.length; - } - else { + } else { token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count); ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token); ptoken = ltoken; @@ -1738,8 +1708,8 @@ coap_send_q_blocks(coap_session_t *session, lg_xmit->offset = block.num * chunk; block.m = lg_xmit->offset + chunk < lg_xmit->length; if (block.m && ((block_pdu->type == COAP_MESSAGE_NON && - (block.num % COAP_MAX_PAYLOADS(session)) + 1 != - COAP_MAX_PAYLOADS(session)) || + (block.num % COAP_MAX_PAYLOADS(session)) + 1 != + COAP_MAX_PAYLOADS(session)) || (block_pdu->type == COAP_MESSAGE_CON && delayqueue_cnt + 1 < COAP_NSTART(session)) || COAP_PROTO_RELIABLE(session->proto))) { @@ -1752,8 +1722,7 @@ coap_send_q_blocks(coap_session_t *session, if (COAP_PDU_IS_RESPONSE(block_pdu)) { ptoken = block_pdu->actual_token.s; ltoken_length = block_pdu->actual_token.length; - } - else { + } else { token = STATE_TOKEN_FULL(lg_xmit->b.b1.state_token,++lg_xmit->b.b1.count); ltoken_length = coap_encode_var_safe8(ltoken, sizeof(token), token); ptoken = ltoken; @@ -1762,12 +1731,12 @@ coap_send_q_blocks(coap_session_t *session, ltoken_length, ptoken, &drop_options); } if (!coap_update_option(block_pdu, lg_xmit->option, - coap_encode_var_safe(buf, - sizeof(buf), - ((block.num) << 4) | - (block.m << 3) | - block.szx), - buf)) { + coap_encode_var_safe(buf, + sizeof(buf), + ((block.num) << 4) | + (block.m << 3) | + block.szx), + buf)) { coap_log_warn("Internal update issue option\n"); coap_delete_pdu(block_pdu); coap_delete_pdu(t_pdu); @@ -1798,8 +1767,7 @@ coap_send_q_blocks(coap_session_t *session, if (!block.m) { lg_xmit->last_payload = 0; coap_ticks(&lg_xmit->last_all_sent); - } - else + } else coap_ticks(&lg_xmit->last_payload); return mid; } @@ -1832,21 +1800,18 @@ coap_block_check_q_block1_xmit(coap_session_t *session, coap_tick_t now) { coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU); if (tim_rem > non_timeout) tim_rem = non_timeout; - } - else { + } else { /* Delay until the next MAX_PAYLOAD needs to be sent off */ if (tim_rem > lg_xmit->last_payload - timed_out) tim_rem = lg_xmit->last_payload - timed_out; } - } - else if (lg_xmit->last_all_sent) { + } else if (lg_xmit->last_all_sent) { non_timeout = COAP_NON_TIMEOUT_TICKS(session); if (lg_xmit->last_all_sent + 4 * non_timeout <= now) { /* Expire this entry */ LL_DELETE(session->lg_xmit, lg_xmit); coap_block_delete_lg_xmit(session, lg_xmit); - } - else { + } else { /* Delay until the lg_xmit needs to expire */ if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now; @@ -1886,21 +1851,18 @@ coap_block_check_q_block2_xmit(coap_session_t *session, coap_tick_t now) { coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->pdu, COAP_SEND_SKIP_PDU); if (tim_rem > non_timeout) tim_rem = non_timeout; - } - else { + } else { /* Delay until the next MAX_PAYLOAD needs to be sent off */ if (tim_rem > lg_xmit->last_payload - timed_out) tim_rem = lg_xmit->last_payload - timed_out; } - } - else if (lg_xmit->last_all_sent) { + } else if (lg_xmit->last_all_sent) { non_timeout = COAP_NON_TIMEOUT_TICKS(session); if (lg_xmit->last_all_sent + 4 * non_timeout <= now) { /* Expire this entry */ - LL_DELETE( session->lg_xmit, lg_xmit); + LL_DELETE(session->lg_xmit, lg_xmit); coap_block_delete_lg_xmit(session, lg_xmit); - } - else { + } else { /* Delay until the lg_xmit needs to expire */ if (tim_rem > lg_xmit->last_all_sent + 4 * non_timeout - now) tim_rem = lg_xmit->last_all_sent + 4 * non_timeout - now; @@ -1968,15 +1930,15 @@ coap_send_q_block1(coap_session_t *session, /* Need to send up to MAX_PAYLOAD blocks if this is a Q_BLOCK1 */ coap_lg_xmit_t *lg_xmit; uint64_t token_match = - STATE_TOKEN_BASE(coap_decode_var_bytes8(request->actual_token.s, - request->actual_token.length)); + STATE_TOKEN_BASE(coap_decode_var_bytes8(request->actual_token.s, + request->actual_token.length)); LL_FOREACH(session->lg_xmit, lg_xmit) { if (lg_xmit->option == COAP_OPTION_Q_BLOCK1 && (token_match == STATE_TOKEN_BASE(lg_xmit->b.b1.state_token) || token_match == - STATE_TOKEN_BASE(coap_decode_var_bytes8(lg_xmit->b.b1.app_token->s, - lg_xmit->b.b1.app_token->length)))) + STATE_TOKEN_BASE(coap_decode_var_bytes8(lg_xmit->b.b1.app_token->s, + lg_xmit->b.b1.app_token->length)))) break; /* try out the next one */ } @@ -2060,20 +2022,19 @@ coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, coap_lg_crcv_t *lg_crcv; uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1); size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : - pdu->used_size; + pdu->used_size; size_t data_len = lg_xmit ? lg_xmit->length : - pdu->data ? - pdu->used_size - (pdu->data - pdu->token) : 0; + pdu->data ? + pdu->used_size - (pdu->data - pdu->token) : 0; lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t)); if (lg_crcv == NULL) return NULL; - coap_log_debug( - "** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n", - coap_session_str(session), (void*)lg_crcv, - STATE_TOKEN_BASE(state_token)); + coap_log_debug("** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n", + coap_session_str(session), (void *)lg_crcv, + STATE_TOKEN_BASE(state_token)); memset(lg_crcv, 0, sizeof(coap_lg_crcv_t)); lg_crcv->initial = 1; coap_ticks(&lg_crcv->last_used); @@ -2083,7 +2044,7 @@ coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, lg_crcv->pdu.max_size = token_options + data_len + 9; lg_crcv->pdu.used_size = token_options + data_len; lg_crcv->pdu.token = coap_malloc_type(COAP_PDU_BUF, - token_options + data_len + lg_crcv->pdu.max_hdr_size); + token_options + data_len + lg_crcv->pdu.max_hdr_size); if (!lg_crcv->pdu.token) { coap_block_delete_lg_crcv(session, lg_crcv); return NULL; @@ -2124,7 +2085,7 @@ coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, void coap_block_delete_lg_crcv(coap_session_t *session, - coap_lg_crcv_t *lg_crcv) { + coap_lg_crcv_t *lg_crcv) { size_t i; #if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG) @@ -2137,7 +2098,7 @@ coap_block_delete_lg_crcv(coap_session_t *session, coap_free_type(COAP_PDU_BUF, lg_crcv->pdu.token - lg_crcv->pdu.max_hdr_size); coap_free_type(COAP_STRING, lg_crcv->body_data); coap_log_debug("** %s: lg_crcv %p released\n", - coap_session_str(session), (void*)lg_crcv); + coap_session_str(session), (void *)lg_crcv); coap_delete_binary(lg_crcv->app_token); for (i = 0; i < lg_crcv->obs_token_cnt; i++) { coap_delete_bin_const(lg_crcv->obs_token[i]); @@ -2150,7 +2111,7 @@ coap_block_delete_lg_crcv(coap_session_t *session, #if COAP_SERVER_SUPPORT void coap_block_delete_lg_srcv(coap_session_t *session, - coap_lg_srcv_t *lg_srcv) { + coap_lg_srcv_t *lg_srcv) { #if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG) (void)session; #endif @@ -2163,14 +2124,14 @@ coap_block_delete_lg_srcv(coap_session_t *session, #endif /* COAP_Q_BLOCK_SUPPORT */ coap_free_type(COAP_STRING, lg_srcv->body_data); coap_log_debug("** %s: lg_srcv %p released\n", - coap_session_str(session), (void*)lg_srcv); + coap_session_str(session), (void *)lg_srcv); coap_free_type(COAP_LG_SRCV, lg_srcv); } #endif /* COAP_SERVER_SUPPORT */ void coap_block_delete_lg_xmit(coap_session_t *session, - coap_lg_xmit_t *lg_xmit) { + coap_lg_xmit_t *lg_xmit) { if (lg_xmit == NULL) return; @@ -2186,7 +2147,7 @@ coap_block_delete_lg_xmit(coap_session_t *session, coap_delete_string(lg_xmit->b.b2.query); coap_log_debug("** %s: lg_xmit %p released\n", - coap_session_str(session), (void*)lg_xmit); + coap_session_str(session), (void *)lg_xmit); coap_free_type(COAP_LG_XMIT, lg_xmit); } @@ -2270,8 +2231,7 @@ coap_handle_request_send_block(coap_session_t *session, #endif /* COAP_Q_BLOCK_SUPPORT */ if (coap_get_block_b(session, pdu, COAP_OPTION_BLOCK2, &alt_block)) { if (block_opt) { - coap_log_warn( - "Block2 and Q-Block2 cannot be in the same request\n"); + coap_log_warn("Block2 and Q-Block2 cannot be in the same request\n"); coap_add_data(response, sizeof("Both Block2 and Q-Block2 invalid")-1, (const uint8_t *)"Both Block2 and Q-Block2 invalid"); response->code = COAP_RESPONSE_CODE(400); @@ -2304,7 +2264,7 @@ coap_handle_request_send_block(coap_session_t *session, etag_opt = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter); if (etag_opt) { uint64_t etag = coap_decode_var_bytes8(coap_opt_value(etag_opt), - coap_opt_length(etag_opt)); + coap_opt_length(etag_opt)); if (etag != p->b.b2.etag) { /* Not a match - pass up to a higher level */ return 0; @@ -2312,8 +2272,7 @@ coap_handle_request_send_block(coap_session_t *session, out_pdu->code = COAP_RESPONSE_CODE(203); coap_ticks(&p->last_sent); goto skip_app_handler; - } - else { + } else { out_pdu->code = p->pdu.code; } coap_ticks(&p->last_obs); @@ -2322,13 +2281,11 @@ coap_handle_request_send_block(coap_session_t *session, chunk = (size_t)1 << (p->blk_size + 4); if (block_opt) { if (block.bert) { - coap_log_debug( - "found Block option, block is BERT, block nr. %u, M %d\n", - block.num, block.m); + coap_log_debug("found Block option, block is BERT, block nr. %u, M %d\n", + block.num, block.m); } else { - coap_log_debug( - "found Block option, block size is %u, block nr. %u, M %d\n", - 1 << (block.szx + 4), block.num, block.m); + coap_log_debug("found Block option, block size is %u, block nr. %u, M %d\n", + 1 << (block.szx + 4), block.num, block.m); } if (block.bert == 0 && block.szx != p->blk_size) { if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) { @@ -2340,17 +2297,15 @@ coap_handle_request_send_block(coap_session_t *session, p->blk_size = block.szx; chunk = (size_t)1 << (p->blk_size + 4); p->offset = block.num * chunk; - coap_log_debug( - "new Block size is %u, block number %u completed\n", - 1 << (block.szx + 4), block.num); + coap_log_debug("new Block size is %u, block number %u completed\n", + 1 << (block.szx + 4), block.num); } else { - coap_log_debug( - "ignoring request to increase Block size, " - "next block is not aligned on requested block size " - "boundary. (%zu x %u mod %u = %zu (which is not 0)\n", - p->offset/chunk + 1, (1 << (p->blk_size + 4)), - (1 << (block.szx + 4)), - (p->offset + chunk) % ((size_t)1 << (block.szx + 4))); + coap_log_debug("ignoring request to increase Block size, " + "next block is not aligned on requested block size " + "boundary. (%zu x %u mod %u = %zu (which is not 0)\n", + p->offset/chunk + 1, (1 << (p->blk_size + 4)), + (1 << (block.szx + 4)), + (p->offset + chunk) % ((size_t)1 << (block.szx + 4))); } } } @@ -2375,7 +2330,7 @@ coap_handle_request_send_block(coap_session_t *session, if (block.aszx != COAP_OPT_BLOCK_SZX(option)) { coap_add_data(response, sizeof("Changing blocksize during request invalid")-1, - (const uint8_t *)"Changing blocksize during request invalid"); + (const uint8_t *)"Changing blocksize during request invalid"); response->code = COAP_RESPONSE_CODE(400); goto skip_app_handler; } @@ -2388,23 +2343,21 @@ coap_handle_request_send_block(coap_session_t *session, } /* 'Continue' request */ for (i = 0; i < COAP_MAX_PAYLOADS(session) && - num + i < max_block; i++) { + num + i < max_block; i++) { add_block_send(num + i, 1, out_blocks, &request_cnt, COAP_MAX_PAYLOADS(session)); p->last_block = num + i; } - } - else { + } else { /* Requesting remaining payloads in this MAX_PAYLOADS */ for (i = 0; i < COAP_MAX_PAYLOADS(session) - - num % COAP_MAX_PAYLOADS(session) && - num + i < max_block; i++) { + num % COAP_MAX_PAYLOADS(session) && + num + i < max_block; i++) { add_block_send(num + i, 0, out_blocks, &request_cnt, COAP_MAX_PAYLOADS(session)); } } - } - else + } else add_block_send(num, 0, out_blocks, &request_cnt, COAP_MAX_PAYLOADS(session)); #else /* ! COAP_Q_BLOCK_SUPPORT */ @@ -2437,16 +2390,14 @@ coap_handle_request_send_block(coap_session_t *session, if (out_blocks[i].is_continue) { out_pdu = coap_pdu_duplicate(&p->pdu, session, p->pdu.actual_token.length, p->pdu.actual_token.s, &drop_options); - } - else { + } else { out_pdu = coap_pdu_duplicate(&p->pdu, session, pdu->actual_token.length, pdu->actual_token.s, &drop_options); } if (!out_pdu) { goto internal_issue; } - } - else { + } else { if (out_blocks[i].is_continue) coap_update_token(response, p->pdu.actual_token.length, p->pdu.actual_token.s); @@ -2472,17 +2423,17 @@ coap_handle_request_send_block(coap_session_t *session, if (block.bert) { size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size; block.m = (p->length - p->offset) > - ((out_pdu->max_size - token_options) /1024) * 1024; + ((out_pdu->max_size - token_options) /1024) * 1024; } else { block.m = (p->offset + chunk) < p->length; } if (!coap_update_option(out_pdu, p->option, - coap_encode_var_safe(buf, - sizeof(buf), - (block.num << 4) | - (block.m << 3) | - block.aszx), - buf)) { + coap_encode_var_safe(buf, + sizeof(buf), + (block.num << 4) | + (block.m << 3) | + block.aszx), + buf)) { goto internal_issue; } if (!(p->offset + chunk < p->length)) { @@ -2507,8 +2458,7 @@ coap_handle_request_send_block(coap_session_t *session, rem = coap_ticks_to_rt(now); if (p->b.b2.maxage_expire > rem) { rem = p->b.b2.maxage_expire - rem; - } - else { + } else { rem = 0; /* Entry needs to be expired */ coap_ticks(&p->last_all_sent); @@ -2572,14 +2522,13 @@ update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num) { if (block_num < rec_blocks->range[i].begin) { if (block_num + 1 == rec_blocks->range[i].begin) { rec_blocks->range[i].begin = block_num; - } - else { + } else { /* Need to insert a new range */ if (rec_blocks->used == COAP_RBLOCK_CNT -1) /* Too many losses */ return 0; memmove(&rec_blocks->range[i+1], &rec_blocks->range[i], - (rec_blocks->used - i) * sizeof (rec_blocks->range[0])); + (rec_blocks->used - i) * sizeof(rec_blocks->range[0])); rec_blocks->range[i].begin = rec_blocks->range[i].end = block_num; rec_blocks->used++; } @@ -2592,8 +2541,8 @@ update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num) { /* Merge the 2 ranges */ rec_blocks->range[i].end = rec_blocks->range[i+1].end; if (i+2 < rec_blocks->used) { - memmove (&rec_blocks->range[i+1], &rec_blocks->range[i+2], - (rec_blocks->used - (i+2)) * sizeof (rec_blocks->range[0])); + memmove(&rec_blocks->range[i+1], &rec_blocks->range[i+2], + (rec_blocks->used - (i+2)) * sizeof(rec_blocks->range[0])); } rec_blocks->used--; } @@ -2672,8 +2621,8 @@ coap_handle_request_put_block(coap_context_t *context, COAP_OPTION_CONTENT_FORMAT, &opt_iter); uint16_t fmt = fmt_opt ? coap_decode_var_bytes(coap_opt_value(fmt_opt), - coap_opt_length(fmt_opt)) : - COAP_MEDIATYPE_TEXT_PLAIN; + coap_opt_length(fmt_opt)) : + COAP_MEDIATYPE_TEXT_PLAIN; coap_opt_t *rtag_opt = coap_check_option(pdu, COAP_OPTION_RTAG, &opt_iter); @@ -2686,7 +2635,7 @@ coap_handle_request_put_block(coap_context_t *context, length = block.chunk_size; } total = size_opt ? coap_decode_var_bytes(coap_opt_value(size_opt), - coap_opt_length(size_opt)) : 0; + coap_opt_length(size_opt)) : 0; offset = block.num << (block.szx + 4); LL_FOREACH(session->lg_srcv, p) { @@ -2722,7 +2671,7 @@ coap_handle_request_put_block(coap_context_t *context, goto skip_app_handler; } coap_log_debug("** %s: lg_srcv %p initialized\n", - coap_session_str(session), (void*)p); + coap_session_str(session), (void *)p); memset(p, 0, sizeof(coap_lg_srcv_t)); coap_ticks(&p->last_used); p->resource = resource; @@ -2773,9 +2722,9 @@ coap_handle_request_put_block(coap_context_t *context, #endif /* COAP_Q_BLOCK_SUPPORT */ if (session->block_mode & #if COAP_Q_BLOCK_SUPPORT - (COAP_BLOCK_SINGLE_BODY|COAP_BLOCK_HAS_Q_BLOCK) || + (COAP_BLOCK_SINGLE_BODY|COAP_BLOCK_HAS_Q_BLOCK) || #else /* ! COAP_Q_BLOCK_SUPPORT */ - (COAP_BLOCK_SINGLE_BODY) || + (COAP_BLOCK_SINGLE_BODY) || #endif /* ! COAP_Q_BLOCK_SUPPORT */ block.bert) { size_t chunk = (size_t)1 << (block.szx + 4); @@ -2802,7 +2751,7 @@ coap_handle_request_put_block(coap_context_t *context, if (update_data) { #if COAP_Q_BLOCK_SUPPORT p->rec_blocks.processing_payload_set = - block.num / COAP_MAX_PAYLOADS(session); + block.num / COAP_MAX_PAYLOADS(session); #endif /* COAP_Q_BLOCK_SUPPORT */ /* Update saved data */ if (p->total_len < saved_offset + length) { @@ -2816,7 +2765,7 @@ coap_handle_request_put_block(coap_context_t *context, } if (block.m || !check_all_blocks_in(&p->rec_blocks, - (uint32_t)(p->total_len + chunk -1)/chunk)) { + (uint32_t)(p->total_len + chunk -1)/chunk)) { /* Not all the payloads of the body have arrived */ if (block.m) { uint8_t buf[4]; @@ -2824,16 +2773,15 @@ coap_handle_request_put_block(coap_context_t *context, #if COAP_Q_BLOCK_SUPPORT if (block_option == COAP_OPTION_Q_BLOCK1) { if (check_all_blocks_in(&p->rec_blocks, - (uint32_t)(p->total_len + chunk -1)/chunk)) { + (uint32_t)(p->total_len + chunk -1)/chunk)) { goto give_app_data; } if (p->rec_blocks.used == 1 && (p->rec_blocks.range[0].end % COAP_MAX_PAYLOADS(session)) + 1 - == COAP_MAX_PAYLOADS(session)) { + == COAP_MAX_PAYLOADS(session)) { /* Blocks could arrive in wrong order */ block.num = p->rec_blocks.range[0].end; - } - else { + } else { /* The remote end will be sending the next one unless this is a MAX_PAYLOADS and all previous have been received */ goto skip_app_handler; @@ -2845,11 +2793,11 @@ coap_handle_request_put_block(coap_context_t *context, #endif /* COAP_Q_BLOCK_SUPPORT */ /* Ask for the next block */ coap_insert_option(response, block_option, - coap_encode_var_safe(buf, sizeof(buf), - (saved_num << 4) | - (block.m << 3) | - block.aszx), - buf); + coap_encode_var_safe(buf, sizeof(buf), + (saved_num << 4) | + (block.m << 3) | + block.aszx), + buf); response->code = COAP_RESPONSE_CODE(231); goto skip_app_handler; } @@ -2877,8 +2825,7 @@ coap_handle_request_put_block(coap_context_t *context, coap_show_pdu(COAP_LOG_DEBUG, pdu); *pfree_lg_srcv = p; goto call_app_handler; - } - else { + } else { /* No need to update body_data and body_length as a single PDU */ pdu->body_offset = offset; /* Exact match if last block */ @@ -2891,15 +2838,14 @@ coap_handle_request_put_block(coap_context_t *context, pdu->body_total = offset + length + block.m; coap_insert_option(response, block_option, - coap_encode_var_safe(buf, sizeof(buf), - (block.num << 4) | - (block.m << 3) | - block.aszx), - buf); + coap_encode_var_safe(buf, sizeof(buf), + (block.num << 4) | + (block.m << 3) | + block.aszx), + buf); *added_block = 1; goto call_app_handler; - } - else { + } else { pdu->body_total = offset + length + block.m; } } @@ -2932,15 +2878,13 @@ derive_cbor_value(const uint8_t **bp, size_t rem_len) { (*bp)++; if (value < 24) { return value; - } - else if (value == 24) { + } else if (value == 24) { if (rem_len < 2) return (uint32_t)-1; value = **bp; (*bp)++; return value; - } - else if (value == 25) { + } else if (value == 25) { if (rem_len < 3) return (uint32_t)-1; value = **bp << 8; @@ -2965,8 +2909,7 @@ derive_cbor_value(const uint8_t **bp, size_t rem_len) { static int check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent, - coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) -{ + coap_lg_xmit_t *lg_xmit, coap_lg_crcv_t *lg_crcv) { /* Check for Echo option for freshness */ coap_opt_iterator_t opt_iter; coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter); @@ -2994,9 +2937,9 @@ check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent, have_data = 1; data = &lg_xmit->data[offset]; data_len = (lg_xmit->length - offset) > blk_size ? blk_size : - lg_xmit->length - offset; + lg_xmit->length - offset; } - } else /* lg_crcv */ { + } else { /* lg_crcv */ sent = &lg_crcv->pdu; if (coap_get_data(sent, &data_len, &data)) have_data = 1; @@ -3048,15 +2991,14 @@ check_freshness(coap_session_t *session, coap_pdu_t *rcvd, coap_pdu_t *sent, } static void -track_echo(coap_session_t *session, coap_pdu_t *rcvd) -{ +track_echo(coap_session_t *session, coap_pdu_t *rcvd) { coap_opt_iterator_t opt_iter; coap_opt_t *opt = coap_check_option(rcvd, COAP_OPTION_ECHO, &opt_iter); if (opt) { coap_delete_bin_const(session->echo); session->echo = coap_new_bin_const(coap_opt_value(opt), - coap_opt_length(opt)); + coap_opt_length(opt)); } } @@ -3080,15 +3022,15 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, coap_lg_xmit_t *p; coap_lg_xmit_t *q; uint64_t token_match = - STATE_TOKEN_BASE(coap_decode_var_bytes8(rcvd->actual_token.s, - rcvd->actual_token.length)); + STATE_TOKEN_BASE(coap_decode_var_bytes8(rcvd->actual_token.s, + rcvd->actual_token.length)); coap_lg_crcv_t *lg_crcv = NULL; LL_FOREACH_SAFE(session->lg_xmit, p, q) { if (!COAP_PDU_IS_REQUEST(&p->pdu) || (token_match != STATE_TOKEN_BASE(p->b.b1.state_token) && token_match != - STATE_TOKEN_BASE(coap_decode_var_bytes8(p->b.b1.app_token->s, + STATE_TOKEN_BASE(coap_decode_var_bytes8(p->b.b1.app_token->s, p->b.b1.app_token->length)))) { /* try out the next one */ continue; @@ -3101,13 +3043,11 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, coap_get_block_b(session, rcvd, p->option, &block)) { if (block.bert) { - coap_log_debug( - "found Block option, block is BERT, block nr. %u (%zu)\n", - block.num, p->b.b1.bert_size); + coap_log_debug("found Block option, block is BERT, block nr. %u (%zu)\n", + block.num, p->b.b1.bert_size); } else { - coap_log_debug( - "found Block option, block size is %u, block nr. %u\n", - 1 << (block.szx + 4), block.num); + coap_log_debug("found Block option, block size is %u, block nr. %u\n", + 1 << (block.szx + 4), block.num); } if (block.szx != p->blk_size) { if ((p->offset + chunk) % ((size_t)1 << (block.szx + 4)) == 0) { @@ -3119,18 +3059,17 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, p->blk_size = block.szx; chunk = (size_t)1 << (p->blk_size + 4); p->offset = block.num * chunk; - coap_log_debug( - "new Block size is %u, block number %u completed\n", - 1 << (block.szx + 4), block.num); + coap_log_debug("new Block size is %u, block number %u completed\n", + 1 << (block.szx + 4), block.num); block.bert = 0; block.aszx = block.szx; } else { coap_log_debug("ignoring request to increase Block size, " - "next block is not aligned on requested block size boundary. " - "(%zu x %u mod %u = %zu != 0)\n", - p->offset/chunk + 1, (1 << (p->blk_size + 4)), - (1 << (block.szx + 4)), - (p->offset + chunk) % ((size_t)1 << (block.szx + 4))); + "next block is not aligned on requested block size boundary. " + "(%zu x %u mod %u = %zu != 0)\n", + p->offset/chunk + 1, (1 << (p->blk_size + 4)), + (1 << (block.szx + 4)), + (p->offset + chunk) % ((size_t)1 << (block.szx + 4))); } } track_echo(session, rcvd); @@ -3185,17 +3124,17 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, block.num++; if (block.bert) { size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : - pdu->used_size; + pdu->used_size; block.m = (p->length - p->offset) > - ((pdu->max_size - token_options) /1024) * 1024; + ((pdu->max_size - token_options) /1024) * 1024; } else { block.m = (p->offset + chunk) < p->length; } coap_update_option(pdu, p->option, coap_encode_var_safe(buf, sizeof(buf), - (block.num << 4) | - (block.m << 3) | - block.aszx), + (block.num << 4) | + (block.m << 3) | + block.aszx), buf); if (!coap_add_block_b_data(pdu, @@ -3212,8 +3151,7 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, COAP_SEND_INC_PDU) == COAP_INVALID_MID) goto fail_body; return 1; - } - else if (coap_send_internal(session, pdu) == COAP_INVALID_MID) + } else if (coap_send_internal(session, pdu) == COAP_INVALID_MID) goto fail_body; #else /* ! COAP_Q_BLOCK_SUPPORT */ if (coap_send_internal(session, pdu) == COAP_INVALID_MID) @@ -3231,7 +3169,7 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK1, &block)) return 1; } else if (rcvd->code == COAP_RESPONSE_CODE(408) && - p->option == COAP_OPTION_Q_BLOCK1) { + p->option == COAP_OPTION_Q_BLOCK1) { size_t length; const uint8_t *data; coap_opt_iterator_t opt_iter; @@ -3239,9 +3177,9 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, COAP_OPTION_CONTENT_FORMAT, &opt_iter); uint16_t fmt = fmt_opt ? - coap_decode_var_bytes(coap_opt_value(fmt_opt), - coap_opt_length(fmt_opt)) : - COAP_MEDIATYPE_TEXT_PLAIN; + coap_decode_var_bytes(coap_opt_value(fmt_opt), + coap_opt_length(fmt_opt)) : + COAP_MEDIATYPE_TEXT_PLAIN; if (fmt != COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ) goto fail_body; @@ -3264,7 +3202,7 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, size_t ltoken_length; for (i = 0; (bp < data + length) && - i < COAP_MAX_PAYLOADS(session); i++) { + i < COAP_MAX_PAYLOADS(session); i++) { if ((*bp & 0xc0) != 0x00) /* uint(value) */ goto fail_cbor; block.num = derive_cbor_value(&bp, data + length - bp); @@ -3284,9 +3222,9 @@ coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, coap_update_option(pdu, p->option, coap_encode_var_safe(buf, sizeof(buf), - (block.num << 4) | - (block.m << 3) | - block.szx), + (block.num << 4) | + (block.m << 3) | + block.szx), buf); if (!coap_add_block(pdu, @@ -3355,8 +3293,7 @@ coap_block_build_body(coap_binary_t *body_data, size_t length, /* Update saved data */ if (offset + length <= total && body_data->length >= total) { memcpy(&body_data->s[offset], data, length); - } - else { + } else { /* * total may be inaccurate as per * https://rfc-editor.org/rfc/rfc7959#section-4 @@ -3372,8 +3309,7 @@ coap_block_build_body(coap_binary_t *body_data, size_t length, if (new) { body_data = new; memcpy(&body_data->s[offset], data, length); - } - else { + } else { coap_delete_binary(body_data); return NULL; } @@ -3409,8 +3345,8 @@ coap_handle_response_get_block(coap_context_t *context, size_t offset; int ack_rst_sent = 0; uint64_t token_match = - STATE_TOKEN_BASE(coap_decode_var_bytes8(rcvd->actual_token.s, - rcvd->actual_token.length)); + STATE_TOKEN_BASE(coap_decode_var_bytes8(rcvd->actual_token.s, + rcvd->actual_token.length)); memset(&block, 0, sizeof(block)); #if COAP_Q_BLOCK_SUPPORT @@ -3422,7 +3358,7 @@ coap_handle_response_get_block(coap_context_t *context, coap_opt_iterator_t opt_iter; if (token_match != STATE_TOKEN_BASE(p->state_token) && - !coap_binary_equal(&rcvd->actual_token, p->app_token)) { + !coap_binary_equal(&rcvd->actual_token, p->app_token)) { /* try out the next one */ continue; } @@ -3435,8 +3371,8 @@ coap_handle_response_get_block(coap_context_t *context, coap_opt_t *size_opt = coap_check_option(rcvd, COAP_OPTION_SIZE2, &opt_iter); size_t size2 = size_opt ? - coap_decode_var_bytes(coap_opt_value(size_opt), - coap_opt_length(size_opt)) : 0; + coap_decode_var_bytes(coap_opt_value(size_opt), + coap_opt_length(size_opt)) : 0; /* length and data are cleared on error */ (void)coap_get_data(rcvd, &length, &data); @@ -3449,8 +3385,7 @@ coap_handle_response_get_block(coap_context_t *context, #if COAP_Q_BLOCK_SUPPORT if (coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &qblock)) { if (have_block) { - coap_log_warn( - "Both Block1 and Q-Block1 not supported in a response\n"); + coap_log_warn("Both Block1 and Q-Block1 not supported in a response\n"); } have_block = 1; block_opt = COAP_OPTION_Q_BLOCK2; @@ -3464,12 +3399,12 @@ coap_handle_response_get_block(coap_context_t *context, track_echo(session, rcvd); if (have_block && (block.m || length)) { coap_opt_t *fmt_opt = coap_check_option(rcvd, - COAP_OPTION_CONTENT_FORMAT, - &opt_iter); + COAP_OPTION_CONTENT_FORMAT, + &opt_iter); uint16_t fmt = fmt_opt ? - coap_decode_var_bytes(coap_opt_value(fmt_opt), - coap_opt_length(fmt_opt)) : - COAP_MEDIATYPE_TEXT_PLAIN; + coap_decode_var_bytes(coap_opt_value(fmt_opt), + coap_opt_length(fmt_opt)) : + COAP_MEDIATYPE_TEXT_PLAIN; coap_opt_t *etag_opt = coap_check_option(rcvd, COAP_OPTION_ETAG, &opt_iter); @@ -3505,8 +3440,7 @@ coap_handle_response_get_block(coap_context_t *context, p->etag_length = coap_opt_length(etag_opt); memcpy(p->etag, coap_opt_value(etag_opt), p->etag_length); p->etag_set = 1; - } - else { + } else { p->etag_set = 0; } p->total_len = size2; @@ -3524,8 +3458,8 @@ coap_handle_response_get_block(coap_context_t *context, if (etag_opt) { if (!full_match(coap_opt_value(etag_opt), - coap_opt_length(etag_opt), - p->etag, p->etag_length)) { + coap_opt_length(etag_opt), + p->etag, p->etag_length)) { /* body of data has changed - need to restart request */ coap_pdu_t *pdu; uint64_t token = STATE_TOKEN_FULL(p->state_token, @@ -3538,8 +3472,7 @@ coap_handle_response_get_block(coap_context_t *context, goto reinit; #endif /* COAP_Q_BLOCK_SUPPORT */ - coap_log_warn( - "Data body updated during receipt - new request started\n"); + coap_log_warn("Data body updated during receipt - new request started\n"); if (!(session->block_mode & COAP_BLOCK_SINGLE_BODY)) coap_handle_event(context, COAP_EVENT_PARTIAL_BLOCK, session); @@ -3556,7 +3489,7 @@ coap_handle_response_get_block(coap_context_t *context, coap_update_option(pdu, block_opt, coap_encode_var_safe(buf, sizeof(buf), - (0 << 4) | (0 << 3) | block.aszx), + (0 << 4) | (0 << 3) | block.aszx), buf); if (coap_send_internal(session, pdu) == COAP_INVALID_MID) @@ -3564,8 +3497,7 @@ coap_handle_response_get_block(coap_context_t *context, goto skip_app_handler; } - } - else if (p->etag_set) { + } else if (p->etag_set) { /* Cannot handle this change in ETag to not being there */ coap_log_warn("Not all blocks have ETag option\n"); goto fail_resp; @@ -3589,8 +3521,7 @@ coap_handle_response_get_block(coap_context_t *context, p->observe_length = min(coap_opt_length(obs_opt), 3); memcpy(p->observe, coap_opt_value(obs_opt), p->observe_length); p->observe_set = 1; - } - else { + } else { p->observe_set = 0; } } @@ -3601,9 +3532,8 @@ coap_handle_response_get_block(coap_context_t *context, uint32_t this_payload_set = block.num / COAP_MAX_PAYLOADS(session); #endif /* COAP_Q_BLOCK_SUPPORT */ - coap_log_debug( - "found Block option, block size is %u, block nr. %u\n", - 1 << (block.szx + 4), block.num); + coap_log_debug("found Block option, block size is %u, block nr. %u\n", + 1 << (block.szx + 4), block.num); #if COAP_Q_BLOCK_SUPPORT if (block_opt == COAP_OPTION_Q_BLOCK2 && p->rec_blocks.used && this_payload_set > p->rec_blocks.processing_payload_set && @@ -3657,15 +3587,14 @@ coap_handle_response_get_block(coap_context_t *context, block.num = p->rec_blocks.range[0].end; /* Now requesting next payload */ p->rec_blocks.processing_payload_set = - block.num / COAP_MAX_PAYLOADS(session) + 1; + block.num / COAP_MAX_PAYLOADS(session) + 1; if (check_any_blocks_next_payload_set(session, &p->rec_blocks)) { /* Need to ask for them individually */ coap_request_missing_q_block2(session, p); goto skip_app_handler; } - } - else { + } else { /* The remote end will be sending the next one unless this is a MAX_PAYLOADS and all previous have been received */ goto skip_app_handler; @@ -3674,8 +3603,7 @@ coap_handle_response_get_block(coap_context_t *context, rcvd->type != COAP_MESSAGE_NON) goto skip_app_handler; - } - else + } else #endif /* COAP_Q_BLOCK_SUPPORT */ block.m = 0; @@ -3694,8 +3622,8 @@ coap_handle_response_get_block(coap_context_t *context, coap_update_option(pdu, block_opt, coap_encode_var_safe(buf, sizeof(buf), - ((block.num + 1) << 4) | - (block.m << 3) | block.aszx), + ((block.num + 1) << 4) | + (block.m << 3) | block.aszx), buf); if (coap_send_internal(session, pdu) == COAP_INVALID_MID) @@ -3709,7 +3637,7 @@ coap_handle_response_get_block(coap_context_t *context, rcvd->body_offset = saved_offset; #if COAP_Q_BLOCK_SUPPORT rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ? - p->total_len : size2; + p->total_len : size2; #else /* ! COAP_Q_BLOCK_SUPPORT */ rcvd->body_total = size2; #endif /* ! COAP_Q_BLOCK_SUPPORT */ @@ -3730,18 +3658,17 @@ coap_handle_response_get_block(coap_context_t *context, rcvd->body_data = p->body_data->s; #if COAP_Q_BLOCK_SUPPORT rcvd->body_length = block_opt == COAP_OPTION_Q_BLOCK2 ? - p->total_len : saved_offset + length; + p->total_len : saved_offset + length; #else /* ! COAP_Q_BLOCK_SUPPORT */ rcvd->body_length = saved_offset + length; #endif /* ! COAP_Q_BLOCK_SUPPORT */ rcvd->body_offset = 0; rcvd->body_total = rcvd->body_length; - } - else { + } else { rcvd->body_offset = saved_offset; #if COAP_Q_BLOCK_SUPPORT rcvd->body_total = block_opt == COAP_OPTION_Q_BLOCK2 ? - p->total_len : size2; + p->total_len : size2; #else /* ! COAP_Q_BLOCK_SUPPORT */ rcvd->body_total = size2; #endif /* ! COAP_Q_BLOCK_SUPPORT */ @@ -3758,8 +3685,7 @@ coap_handle_response_get_block(coap_context_t *context, coap_send_rst(session, rcvd); else coap_send_ack(session, rcvd); - } - else { + } else { coap_send_ack(session, rcvd); } ack_rst_sent = 1; @@ -3834,12 +3760,12 @@ coap_handle_response_get_block(coap_context_t *context, if (!sent) { if (coap_get_block_b(session, rcvd, COAP_OPTION_BLOCK2, &block) #if COAP_Q_BLOCK_SUPPORT - || + || coap_get_block_b(session, rcvd, COAP_OPTION_Q_BLOCK2, &block) #endif /* COAP_Q_BLOCK_SUPPORT */ - ) { + ) { coap_log_debug("** %s: large body receive internal issue\n", - coap_session_str(session)); + coap_session_str(session)); goto skip_app_handler; } } else if (COAP_RESPONSE_CLASS(rcvd->code) == 2) { @@ -3931,7 +3857,7 @@ coap_check_code_lg_xmit(const coap_session_t *session, lg_xmit = coap_find_lg_xmit_response(session, request, resource, query); if (lg_xmit && lg_xmit->pdu.code == 0) { lg_xmit->pdu.code = response->code; - return; + return; } } #endif /* COAP_SERVER_SUPPORT */ diff --git a/src/coap_cache.c b/src/coap_cache.c index eba27b7e57..1698bb21fd 100644 --- a/src/coap_cache.c +++ b/src/coap_cache.c @@ -57,13 +57,11 @@ coap_cache_ignore_options(coap_context_t *ctx, if (ctx->cache_ignore_options) { memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0])); ctx->cache_ignore_count = count; - } - else { + } else { coap_log_warn("Unable to create cache_ignore_options\n"); return 0; } - } - else { + } else { ctx->cache_ignore_options = NULL; ctx->cache_ignore_count = count; } @@ -92,7 +90,7 @@ coap_cache_derive_key_w_ignore(const coap_session_t *session, if (session_based == COAP_CACHE_IS_SESSION_BASED) { /* Include the session ptr */ - if (!coap_digest_update(dctx, (const uint8_t*)&session, sizeof(session))) { + if (!coap_digest_update(dctx, (const uint8_t *)&session, sizeof(session))) { goto update_fail; } } @@ -152,9 +150,9 @@ coap_delete_cache_key(coap_cache_key_t *cache_key) { coap_cache_entry_t * coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, - coap_cache_record_pdu_t record_pdu, - coap_cache_session_based_t session_based, - unsigned int idle_timeout) { + coap_cache_record_pdu_t record_pdu, + coap_cache_session_based_t session_based, + unsigned int idle_timeout) { coap_cache_entry_t *entry = coap_malloc_type(COAP_CACHE_ENTRY, sizeof(coap_cache_entry_t)); if (!entry) { @@ -247,7 +245,7 @@ coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry) { const coap_pdu_t * coap_cache_get_pdu(const coap_cache_entry_t *cache_entry) { - return cache_entry->pdu; + return cache_entry->pdu; } void diff --git a/src/coap_debug.c b/src/coap_debug.c index 546dc1e07c..c38e3627ba 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -48,30 +48,33 @@ # ifndef LOG_CONF_LEVEL_COAP # define LOG_CONF_LEVEL_COAP 2 /* = LOG_LEVEL_WARN */ # endif -static coap_log_t maxlog = LOG_CONF_LEVEL_COAP == 0 /* = LOG_LEVEL_NONE */ - ? COAP_LOG_EMERG - : (LOG_CONF_LEVEL_COAP == 1 /* = LOG_LEVEL_ERR */ - ? COAP_LOG_ERR - : (LOG_CONF_LEVEL_COAP == 2 /* = LOG_LEVEL_WARN */ - ? COAP_LOG_WARN - : (LOG_CONF_LEVEL_COAP == 3 /* = LOG_LEVEL_INFO */ - ? COAP_LOG_INFO - : COAP_LOG_DEBUG))); +static coap_log_t maxlog = LOG_CONF_LEVEL_COAP == 0 ? /* = LOG_LEVEL_NONE */ + COAP_LOG_EMERG : + (LOG_CONF_LEVEL_COAP == 1 ? /* = LOG_LEVEL_ERR */ + COAP_LOG_ERR : + (LOG_CONF_LEVEL_COAP == 2 ? /* = LOG_LEVEL_WARN */ + COAP_LOG_WARN : + (LOG_CONF_LEVEL_COAP == 3 ? /* = LOG_LEVEL_INFO */ + COAP_LOG_INFO : + COAP_LOG_DEBUG))); #else /* WITH_CONTIKI */ static coap_log_t maxlog = COAP_LOG_WARN; /* default maximum CoAP log level */ #endif /* WITH_CONTIKI */ static int use_fprintf_for_show_pdu = 1; /* non zero to output with fprintf */ -const char *coap_package_name(void) { +const char * +coap_package_name(void) { return PACKAGE_NAME; } -const char *coap_package_version(void) { +const char * +coap_package_version(void) { return PACKAGE_STRING; } -const char *coap_package_build(void) { +const char * +coap_package_build(void) { #ifdef LIBCOAP_PACKAGE_BUILD return LIBCOAP_PACKAGE_BUILD; #else /* !LIBCOAP_PACKAGE_BUILD */ @@ -107,8 +110,7 @@ static const char *loglevels[] = { #ifdef WITH_CONTIKI void -coap_print_contiki_prefix(coap_log_t level) -{ +coap_print_contiki_prefix(coap_log_t level) { printf("[%s: COAP ] ", loglevels[level]); } #endif /* WITH_CONTIKI */ @@ -124,7 +126,7 @@ print_timestamp(char *s, size_t len, coap_tick_t t) { lensofar = strftime(s, len, "%b %d %H:%M:%S", tmp); if (len > lensofar + 4) { lensofar += snprintf(&s[lensofar], len-lensofar, ".%03u", - (unsigned int)((coap_ticks_to_rt_us(t) % 1000000)/1000)); + (unsigned int)((coap_ticks_to_rt_us(t) % 1000000)/1000)); } return lensofar; } @@ -157,15 +159,15 @@ print_timestamp(char *s, size_t len, coap_tick_t t) { static inline size_t strnlen(const char *s, size_t maxlen) { size_t n = 0; - while(*s++ && n < maxlen) + while (*s++ && n < maxlen) ++n; return n; } #endif /* HAVE_STRNLEN && !__MINGW32__ */ static size_t -print_readable( const uint8_t *data, size_t len, - unsigned char *result, size_t buflen, int encode_always ) { +print_readable(const uint8_t *data, size_t len, + unsigned char *result, size_t buflen, int encode_always) { const uint8_t hex[] = "0123456789ABCDEF"; size_t cnt = 0; assert(data || len == 0); @@ -177,8 +179,8 @@ print_readable( const uint8_t *data, size_t len, while (len) { if (!encode_always && isprint(*data)) { if (cnt+1 < buflen) { /* keep one byte for terminating zero */ - *result++ = *data; - ++cnt; + *result++ = *data; + ++cnt; } else { break; } @@ -193,7 +195,8 @@ print_readable( const uint8_t *data, size_t len, break; } - ++data; --len; + ++data; + --len; } *result = '\0'; /* add a terminating zero */ @@ -488,9 +491,11 @@ msg_type_string(uint16_t t) { static const char * msg_code_string(uint16_t c) { static const char *methods[] = { "0.00", "GET", "POST", "PUT", "DELETE", - "FETCH", "PATCH", "iPATCH" }; + "FETCH", "PATCH", "iPATCH" + }; static const char *signals[] = { "7.00", "CSM", "Ping", "Pong", "Release", - "Abort" }; + "Abort" + }; static char buf[5]; if (c < sizeof(methods)/sizeof(const char *)) { @@ -669,14 +674,14 @@ is_binary(int content_format) { } #define COAP_DO_SHOW_OUTPUT_LINE \ - do { \ - if (use_fprintf_for_show_pdu) { \ - fprintf(COAP_DEBUG_FD, "%s", outbuf); \ - } \ - else { \ - coap_log(level, "%s", outbuf); \ - } \ - } while (0) + do { \ + if (use_fprintf_for_show_pdu) { \ + fprintf(COAP_DEBUG_FD, "%s", outbuf); \ + } \ + else { \ + coap_log(level, "%s", outbuf); \ + } \ + } while (0) /* * It is possible to override the output debug buffer size and hence control @@ -715,7 +720,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { size_t data_len; const uint8_t *data; uint32_t opt_len; - const uint8_t* opt_val; + const uint8_t *opt_val; size_t outbuflen = 0; int is_oscore_payload = 0; @@ -747,7 +752,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { for (i = 0; i < pdu->actual_token.length; i++) { outbuflen = strlen(outbuf); snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, - "%02x", pdu->actual_token.s[i]); + "%02x", pdu->actual_token.s[i]); } outbuflen = strlen(outbuf); snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "}"); @@ -766,193 +771,200 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ","); } - if (pdu->code == COAP_SIGNALING_CODE_CSM) switch(opt_iter.number) { - case COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH: - case COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE: - buf_len = snprintf((char *)buf, sizeof(buf), "%u", - coap_decode_var_bytes(coap_opt_value(option), - coap_opt_length(option))); - break; - default: - buf_len = 0; - break; - } else if (pdu->code == COAP_SIGNALING_CODE_PING - || pdu->code == COAP_SIGNALING_CODE_PONG) { - buf_len = 0; - } else if (pdu->code == COAP_SIGNALING_CODE_RELEASE) switch(opt_iter.number) { - case COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS: - buf_len = print_readable(coap_opt_value(option), - coap_opt_length(option), - buf, sizeof(buf), 0); - break; - case COAP_SIGNALING_OPTION_HOLD_OFF: - buf_len = snprintf((char *)buf, sizeof(buf), "%u", - coap_decode_var_bytes(coap_opt_value(option), - coap_opt_length(option))); - break; - default: - buf_len = 0; - break; - } else if (pdu->code == COAP_SIGNALING_CODE_ABORT) switch(opt_iter.number) { - case COAP_SIGNALING_OPTION_BAD_CSM_OPTION: - buf_len = snprintf((char *)buf, sizeof(buf), "%u", - coap_decode_var_bytes(coap_opt_value(option), - coap_opt_length(option))); - break; - default: + if (pdu->code == COAP_SIGNALING_CODE_CSM) { + switch (opt_iter.number) { + case COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH: + case COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE: + buf_len = snprintf((char *)buf, sizeof(buf), "%u", + coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option))); + break; + default: + buf_len = 0; + break; + } + } else if (pdu->code == COAP_SIGNALING_CODE_PING || + pdu->code == COAP_SIGNALING_CODE_PONG) { buf_len = 0; - break; - } else switch (opt_iter.number) { - case COAP_OPTION_CONTENT_FORMAT: - case COAP_OPTION_ACCEPT: - content_format = (int)coap_decode_var_bytes(coap_opt_value(option), - coap_opt_length(option)); + } else if (pdu->code == COAP_SIGNALING_CODE_RELEASE) { + switch (opt_iter.number) { + case COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS: + buf_len = print_readable(coap_opt_value(option), + coap_opt_length(option), + buf, sizeof(buf), 0); + break; + case COAP_SIGNALING_OPTION_HOLD_OFF: + buf_len = snprintf((char *)buf, sizeof(buf), "%u", + coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option))); + break; + default: + buf_len = 0; + break; + } + } else if (pdu->code == COAP_SIGNALING_CODE_ABORT) { + switch (opt_iter.number) { + case COAP_SIGNALING_OPTION_BAD_CSM_OPTION: + buf_len = snprintf((char *)buf, sizeof(buf), "%u", + coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option))); + break; + default: + buf_len = 0; + break; + } + } else { + switch (opt_iter.number) { + case COAP_OPTION_CONTENT_FORMAT: + case COAP_OPTION_ACCEPT: + content_format = (int)coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option)); - buf_len = print_content_format(content_format, buf, sizeof(buf)); - break; + buf_len = print_content_format(content_format, buf, sizeof(buf)); + break; - case COAP_OPTION_BLOCK1: - case COAP_OPTION_BLOCK2: - case COAP_OPTION_Q_BLOCK1: - case COAP_OPTION_Q_BLOCK2: - /* split block option into number/more/size where more is the - * letter M if set, the _ otherwise */ - if (COAP_OPT_BLOCK_SZX(option) == 7) { - if (coap_get_data(pdu, &data_len, &data)) - buf_len = snprintf((char *)buf, sizeof(buf), "%u/%c/BERT(%zu)", + case COAP_OPTION_BLOCK1: + case COAP_OPTION_BLOCK2: + case COAP_OPTION_Q_BLOCK1: + case COAP_OPTION_Q_BLOCK2: + /* split block option into number/more/size where more is the + * letter M if set, the _ otherwise */ + if (COAP_OPT_BLOCK_SZX(option) == 7) { + if (coap_get_data(pdu, &data_len, &data)) + buf_len = snprintf((char *)buf, sizeof(buf), "%u/%c/BERT(%zu)", + coap_opt_block_num(option), /* block number */ + COAP_OPT_BLOCK_MORE(option) ? 'M' : '_', /* M bit */ + data_len); + else + buf_len = snprintf((char *)buf, sizeof(buf), "%u/%c/BERT", + coap_opt_block_num(option), /* block number */ + COAP_OPT_BLOCK_MORE(option) ? 'M' : '_'); /* M bit */ + } else { + buf_len = snprintf((char *)buf, sizeof(buf), "%u/%c/%u", coap_opt_block_num(option), /* block number */ COAP_OPT_BLOCK_MORE(option) ? 'M' : '_', /* M bit */ - data_len); - else - buf_len = snprintf((char *)buf, sizeof(buf), "%u/%c/BERT", - coap_opt_block_num(option), /* block number */ - COAP_OPT_BLOCK_MORE(option) ? 'M' : '_'); /* M bit */ - } else { - buf_len = snprintf((char *)buf, sizeof(buf), "%u/%c/%u", - coap_opt_block_num(option), /* block number */ - COAP_OPT_BLOCK_MORE(option) ? 'M' : '_', /* M bit */ - (1 << (COAP_OPT_BLOCK_SZX(option) + 4))); /* block size */ - } - - break; + (1 << (COAP_OPT_BLOCK_SZX(option) + 4))); /* block size */ + } - case COAP_OPTION_OSCORE: - opt_len = coap_opt_length(option); - buf[0] = '\000'; - if (opt_len) { - size_t ofs = 1; - size_t cnt; + break; - opt_val = coap_opt_value(option); - if (opt_val[0] & 0x20) { - /* Group Flag */ - snprintf((char *)buf, sizeof(buf), "grp"); - } - if (opt_val[0] & 0x07) { - /* Partial IV */ - cnt = opt_val[0] & 0x07; - if (cnt > opt_len - ofs) - goto no_more; - buf_len = strlen((char *)buf); - snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, "%spIV=0x", - buf_len ? "," : ""); - for (i = 0; (uint32_t)i < cnt; i++) { + case COAP_OPTION_OSCORE: + opt_len = coap_opt_length(option); + buf[0] = '\000'; + if (opt_len) { + size_t ofs = 1; + size_t cnt; + + opt_val = coap_opt_value(option); + if (opt_val[0] & 0x20) { + /* Group Flag */ + snprintf((char *)buf, sizeof(buf), "grp"); + } + if (opt_val[0] & 0x07) { + /* Partial IV */ + cnt = opt_val[0] & 0x07; + if (cnt > opt_len - ofs) + goto no_more; buf_len = strlen((char *)buf); - snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, - "%02x", opt_val[ofs + i]); + snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, "%spIV=0x", + buf_len ? "," : ""); + for (i = 0; (uint32_t)i < cnt; i++) { + buf_len = strlen((char *)buf); + snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, + "%02x", opt_val[ofs + i]); + } + ofs += cnt; } - ofs += cnt; - } - if (opt_val[0] & 0x10) { - /* kid context */ - if (ofs >= opt_len) - goto no_more; - cnt = opt_val[ofs]; - if (cnt > opt_len - ofs - 1) - goto no_more; - ofs++; - buf_len = strlen((char *)buf); - snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, "%skc=0x", - buf_len ? "," : ""); - for (i = 0; (uint32_t)i < cnt; i++) { + if (opt_val[0] & 0x10) { + /* kid context */ + if (ofs >= opt_len) + goto no_more; + cnt = opt_val[ofs]; + if (cnt > opt_len - ofs - 1) + goto no_more; + ofs++; buf_len = strlen((char *)buf); - snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, - "%02x", opt_val[ofs + i]); + snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, "%skc=0x", + buf_len ? "," : ""); + for (i = 0; (uint32_t)i < cnt; i++) { + buf_len = strlen((char *)buf); + snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, + "%02x", opt_val[ofs + i]); + } + ofs += cnt; } - ofs += cnt; - } - if (opt_val[0] & 0x08) { - /* kid */ - if (ofs >= opt_len) - goto no_more; - cnt = opt_len - ofs; - buf_len = strlen((char *)buf); - snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, "%skid=0x", - buf_len ? "," : ""); - for (i = 0; (uint32_t)i < cnt; i++) { + if (opt_val[0] & 0x08) { + /* kid */ + if (ofs >= opt_len) + goto no_more; + cnt = opt_len - ofs; buf_len = strlen((char *)buf); - snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, - "%02x", opt_val[ofs + i]); + snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, "%skid=0x", + buf_len ? "," : ""); + for (i = 0; (uint32_t)i < cnt; i++) { + buf_len = strlen((char *)buf); + snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, + "%02x", opt_val[ofs + i]); + } } } - } no_more: - buf_len = strlen((char *)buf); - is_oscore_payload = 1; - break; + buf_len = strlen((char *)buf); + is_oscore_payload = 1; + break; - case COAP_OPTION_URI_PORT: - case COAP_OPTION_MAXAGE: - case COAP_OPTION_OBSERVE: - case COAP_OPTION_SIZE1: - case COAP_OPTION_SIZE2: - case COAP_OPTION_HOP_LIMIT: - if (coap_opt_length(option)) { - /* show values as unsigned decimal value */ - buf_len = snprintf((char *)buf, sizeof(buf), "%u", - coap_decode_var_bytes(coap_opt_value(option), - coap_opt_length(option))); - } - break; + case COAP_OPTION_URI_PORT: + case COAP_OPTION_MAXAGE: + case COAP_OPTION_OBSERVE: + case COAP_OPTION_SIZE1: + case COAP_OPTION_SIZE2: + case COAP_OPTION_HOP_LIMIT: + if (coap_opt_length(option)) { + /* show values as unsigned decimal value */ + buf_len = snprintf((char *)buf, sizeof(buf), "%u", + coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option))); + } + break; - case COAP_OPTION_IF_MATCH: - case COAP_OPTION_ETAG: - case COAP_OPTION_ECHO: - case COAP_OPTION_NORESPONSE: - case COAP_OPTION_RTAG: - opt_len = coap_opt_length(option); - opt_val = coap_opt_value(option); - snprintf((char *)buf, sizeof(buf), "0x"); - for (i = 0; (uint32_t)i < opt_len; i++) { + case COAP_OPTION_IF_MATCH: + case COAP_OPTION_ETAG: + case COAP_OPTION_ECHO: + case COAP_OPTION_NORESPONSE: + case COAP_OPTION_RTAG: + opt_len = coap_opt_length(option); + opt_val = coap_opt_value(option); + snprintf((char *)buf, sizeof(buf), "0x"); + for (i = 0; (uint32_t)i < opt_len; i++) { + buf_len = strlen((char *)buf); + snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, + "%02x", opt_val[i]); + } buf_len = strlen((char *)buf); - snprintf((char *)&buf[buf_len], sizeof(buf)-buf_len, - "%02x", opt_val[i]); - } - buf_len = strlen((char *)buf); - break; - default: - /* generic output function for all other option types */ - if (opt_iter.number == COAP_OPTION_URI_PATH || - opt_iter.number == COAP_OPTION_PROXY_URI || - opt_iter.number == COAP_OPTION_URI_HOST || - opt_iter.number == COAP_OPTION_LOCATION_PATH || - opt_iter.number == COAP_OPTION_LOCATION_QUERY || - opt_iter.number == COAP_OPTION_PROXY_SCHEME || - opt_iter.number == COAP_OPTION_URI_QUERY) { - encode = 0; - } else { - encode = 1; + break; + default: + /* generic output function for all other option types */ + if (opt_iter.number == COAP_OPTION_URI_PATH || + opt_iter.number == COAP_OPTION_PROXY_URI || + opt_iter.number == COAP_OPTION_URI_HOST || + opt_iter.number == COAP_OPTION_LOCATION_PATH || + opt_iter.number == COAP_OPTION_LOCATION_QUERY || + opt_iter.number == COAP_OPTION_PROXY_SCHEME || + opt_iter.number == COAP_OPTION_URI_QUERY) { + encode = 0; + } else { + encode = 1; + } } - - buf_len = print_readable(coap_opt_value(option), - coap_opt_length(option), - buf, sizeof(buf), encode); } + buf_len = print_readable(coap_opt_value(option), + coap_opt_length(option), + buf, sizeof(buf), encode); outbuflen = strlen(outbuf); snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, - " %s:%.*s", msg_option_string(pdu->code, opt_iter.number), - (int)buf_len, buf); + " %s:%.*s", msg_option_string(pdu->code, opt_iter.number), + (int)buf_len, buf); } outbuflen = strlen(outbuf); @@ -986,7 +998,8 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { data_len = keep_data_len; data = keep_data; outbuflen = strlen(outbuf); - if (outbuflen == sizeof(outbuf)-1) outbuflen--; + if (outbuflen == sizeof(outbuf)-1) + outbuflen--; snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n"); COAP_DO_SHOW_OUTPUT_LINE; /* @@ -998,7 +1011,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { while (data_len--) { outbuflen = strlen(outbuf); snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, - "%c ", isprint (*data) ? *data : '.'); + "%c ", isprint(*data) ? *data : '.'); data++; } outbuflen = strlen(outbuf); @@ -1015,7 +1028,7 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { } if (max_length > 1) { outbuflen += print_readable(data, data_len, - (unsigned char*)&outbuf[outbuflen], + (unsigned char *)&outbuf[outbuflen], max_length, 0); } /* print_readable may be handling unprintables - hence headroom of 4 */ @@ -1027,7 +1040,8 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { } outbuflen = strlen(outbuf); - if (outbuflen == sizeof(outbuf)-1) outbuflen--; + if (outbuflen == sizeof(outbuf)-1) + outbuflen--; snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n"); COAP_DO_SHOW_OUTPUT_LINE; @@ -1036,15 +1050,15 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) { #endif /* COAP_CONSTRAINED_STACK */ } -void coap_show_tls_version(coap_log_t level) -{ +void +coap_show_tls_version(coap_log_t level) { char buffer[128]; coap_string_tls_version(buffer, sizeof(buffer)); coap_log(level, "%s\n", buffer); } -char *coap_string_tls_version(char *buffer, size_t bufsize) -{ +char * +coap_string_tls_version(char *buffer, size_t bufsize) { coap_tls_version_t *tls_version = coap_get_tls_library_version(); char beta[8]; char sub[2]; @@ -1080,7 +1094,7 @@ char *coap_string_tls_version(char *buffer, size_t bufsize) break; } sub[0] = ((tls_version->version >> 4) & 0xff) ? - ((tls_version->version >> 4) & 0xff) + 'a' -1 : '\000'; + ((tls_version->version >> 4) & 0xff) + 'a' -1 : '\000'; sub[1] = '\000'; switch (tls_version->built_version &0xf) { case 0: @@ -1135,8 +1149,8 @@ char *coap_string_tls_version(char *buffer, size_t bufsize) return buffer; } -char *coap_string_tls_support(char *buffer, size_t bufsize) -{ +char * +coap_string_tls_support(char *buffer, size_t bufsize) { const int have_tls = coap_tls_is_supported(); const int have_dtls = coap_dtls_is_supported(); const int have_psk = coap_dtls_psk_is_supported(); @@ -1165,7 +1179,8 @@ char *coap_string_tls_support(char *buffer, size_t bufsize) static coap_log_handler_t log_handler = NULL; -void coap_set_log_handler(coap_log_handler_t handler) { +void +coap_set_log_handler(coap_log_handler_t handler) { log_handler = handler; } @@ -1185,7 +1200,7 @@ coap_log_impl(coap_log_t level, const char *format, ...) { coap_mutex_lock(&static_log_mutex); #endif /* COAP_CONSTRAINED_STACK */ - vsnprintf( message, sizeof(message), format, ap); + vsnprintf(message, sizeof(message), format, ap); va_end(ap); log_handler(level, message); #if COAP_CONSTRAINED_STACK @@ -1226,7 +1241,8 @@ static int num_packet_loss_intervals = 0; static int packet_loss_level = 0; static int send_packet_count = 0; -int coap_debug_set_packet_loss(const char *loss_level) { +int +coap_debug_set_packet_loss(const char *loss_level) { const char *p = loss_level; char *end = NULL; int n = (int)strtol(p, &end, 10), i = 0; @@ -1266,22 +1282,23 @@ int coap_debug_set_packet_loss(const char *loss_level) { return 1; } -int coap_debug_send_packet(void) { +int +coap_debug_send_packet(void) { ++send_packet_count; if (num_packet_loss_intervals > 0) { int i; for (i = 0; i < num_packet_loss_intervals; i++) { - if (send_packet_count >= packet_loss_intervals[i].start - && send_packet_count <= packet_loss_intervals[i].end) { + if (send_packet_count >= packet_loss_intervals[i].start && + send_packet_count <= packet_loss_intervals[i].end) { coap_log_debug("Packet %u dropped\n", send_packet_count); return 0; } } } - if ( packet_loss_level > 0 ) { + if (packet_loss_level > 0) { uint16_t r = 0; - coap_prng( (uint8_t*)&r, 2 ); - if ( r < packet_loss_level ) { + coap_prng((uint8_t *)&r, 2); + if (r < packet_loss_level) { coap_log_debug("Packet %u dropped\n", send_packet_count); return 0; } diff --git a/src/coap_encode.c b/src/coap_encode.c index 4c07b1c670..e63349b97f 100644 --- a/src/coap_encode.c +++ b/src/coap_encode.c @@ -17,14 +17,15 @@ /* Carsten suggested this when fls() is not available: */ #ifndef HAVE_FLS -int coap_fls(unsigned int i) { +int +coap_fls(unsigned int i) { return coap_flsll(i); } #endif #ifndef HAVE_FLSLL -int coap_flsll(long long j) -{ +int +coap_flsll(long long j) { unsigned long long i = (unsigned long long)j; int n; for (n = 0; i; n++) @@ -50,7 +51,7 @@ coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val) { i >>= 8; if (n > length) { - assert (n <= length); + assert(n <= length); return 0; } i = n; @@ -81,7 +82,7 @@ coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val) { tval >>= 8; if (n > length) { - assert (n <= length); + assert(n <= length); return 0; } i = n; diff --git a/src/coap_gnutls.c b/src/coap_gnutls.c index 4c69fced8b..df646be936 100644 --- a/src/coap_gnutls.c +++ b/src/coap_gnutls.c @@ -64,11 +64,11 @@ #include #if (GNUTLS_VERSION_NUMBER >= 0x030606) #define COAP_GNUTLS_KEY_RPK GNUTLS_KEY_DIGITAL_SIGNATURE | \ - GNUTLS_KEY_NON_REPUDIATION | \ - GNUTLS_KEY_KEY_ENCIPHERMENT | \ - GNUTLS_KEY_DATA_ENCIPHERMENT | \ - GNUTLS_KEY_KEY_AGREEMENT | \ - GNUTLS_KEY_KEY_CERT_SIGN + GNUTLS_KEY_NON_REPUDIATION | \ + GNUTLS_KEY_KEY_ENCIPHERMENT | \ + GNUTLS_KEY_DATA_ENCIPHERMENT | \ + GNUTLS_KEY_KEY_AGREEMENT | \ + GNUTLS_KEY_KEY_CERT_SIGN #endif /* GNUTLS_VERSION_NUMBER >= 0x030606 */ #ifndef GNUTLS_CRT_RAW @@ -148,20 +148,20 @@ typedef enum coap_free_bye_t { #define VARIANTS_NO_TLS13_3_6_4 VARIANTS_3_5_5 ":-VERS-TLS1.3" #define G_ACTION(xx) do { \ - ret = (xx); \ -} while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED) + ret = (xx); \ + } while (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED) #define G_CHECK(xx,func) do { \ - if ((ret = (xx)) < 0) { \ - coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \ - goto fail; \ - } \ -} while (0) + if ((ret = (xx)) < 0) { \ + coap_log_warn("%s: '%s'\n", func, gnutls_strerror(ret)); \ + goto fail; \ + } \ + } while (0) #define G_ACTION_CHECK(xx,func) do { \ - G_ACTION(xx); \ - G_CHECK(xx, func); \ -} while 0 + G_ACTION(xx); \ + G_CHECK(xx, func); \ + } while 0 static coap_log_t dtls_log_level = 0; @@ -252,7 +252,7 @@ coap_get_tls_library_version(void) { if (vers) { int p1, p2, p3; - sscanf (vers, "%d.%d.%d", &p1, &p2, &p3); + sscanf(vers, "%d.%d.%d", &p1, &p2, &p3); version.version = (p1 << 16) | (p2 << 8) | p3; } version.built_version = GNUTLS_VERSION_NUMBER; @@ -261,21 +261,19 @@ coap_get_tls_library_version(void) { } static void -coap_gnutls_audit_log_func(gnutls_session_t g_session, const char* text) -{ +coap_gnutls_audit_log_func(gnutls_session_t g_session, const char *text) { if (g_session) { coap_session_t *c_session = - (coap_session_t *)gnutls_transport_get_ptr(g_session); + (coap_session_t *)gnutls_transport_get_ptr(g_session); coap_log_warn("** %s: %s", - coap_session_str(c_session), text); + coap_session_str(c_session), text); } else { coap_log_warn("** (null): %s", text); } } static void -coap_gnutls_log_func(int level, const char* text) -{ +coap_gnutls_log_func(int level, const char *text) { /* Things get noisy, even at level 1 */ if (level > 0) level += COAP_LOG_WARN; @@ -290,11 +288,10 @@ coap_gnutls_log_func(int level, const char* text) */ int coap_dtls_context_set_pki(coap_context_t *c_context, - const coap_dtls_pki_t* setup_data, - const coap_dtls_role_t role COAP_UNUSED) -{ + const coap_dtls_pki_t *setup_data, + const coap_dtls_role_t role COAP_UNUSED) { coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_context->dtls_context); + ((coap_gnutls_context_t *)c_context->dtls_context); if (!g_context || !setup_data) return 0; @@ -314,8 +311,7 @@ coap_dtls_context_set_pki(coap_context_t *c_context, g_context->setup_data.allow_expired_crl = 0; g_context->setup_data.allow_bad_md_hash = 0; g_context->setup_data.allow_short_rsa_length = 0; - } - else { + } else { /* Allow all of these but warn if issue */ g_context->setup_data.allow_self_signed = 1; g_context->setup_data.allow_expired_certs = 1; @@ -339,21 +335,18 @@ coap_dtls_context_set_pki(coap_context_t *c_context, int coap_dtls_context_set_pki_root_cas(coap_context_t *c_context, const char *ca_file, - const char *ca_path) -{ + const char *ca_path) { coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_context->dtls_context); + ((coap_gnutls_context_t *)c_context->dtls_context); if (!g_context) { - coap_log_warn( - "coap_context_set_pki_root_cas: (D)TLS environment " - "not set up\n"); + coap_log_warn("coap_context_set_pki_root_cas: (D)TLS environment " + "not set up\n"); return 0; } if (ca_file == NULL && ca_path == NULL) { - coap_log_warn( - "coap_context_set_pki_root_cas: ca_file and/or ca_path " - "not defined\n"); + coap_log_warn("coap_context_set_pki_root_cas: ca_file and/or ca_path " + "not defined\n"); return 0; } if (g_context->root_ca_file) { @@ -384,10 +377,10 @@ coap_dtls_context_set_pki_root_cas(coap_context_t *c_context, */ int coap_dtls_context_set_spsk(coap_context_t *c_context, - coap_dtls_spsk_t *setup_data -) { + coap_dtls_spsk_t *setup_data + ) { coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_context->dtls_context); + ((coap_gnutls_context_t *)c_context->dtls_context); if (!g_context || !setup_data) return 0; @@ -404,10 +397,10 @@ coap_dtls_context_set_spsk(coap_context_t *c_context, */ int coap_dtls_context_set_cpsk(coap_context_t *c_context, - coap_dtls_cpsk_t *setup_data -) { + coap_dtls_cpsk_t *setup_data + ) { coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_context->dtls_context); + ((coap_gnutls_context_t *)c_context->dtls_context); if (!g_context || !setup_data) return 0; @@ -422,19 +415,20 @@ coap_dtls_context_set_cpsk(coap_context_t *c_context, * 1 passed */ int -coap_dtls_context_check_keys_enabled(coap_context_t *c_context) -{ +coap_dtls_context_check_keys_enabled(coap_context_t *c_context) { coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_context->dtls_context); + ((coap_gnutls_context_t *)c_context->dtls_context); return g_context->psk_pki_enabled ? 1 : 0; } -void coap_dtls_startup(void) { +void +coap_dtls_startup(void) { gnutls_global_set_audit_log_function(coap_gnutls_audit_log_func); gnutls_global_set_log_function(coap_gnutls_log_func); } -void coap_dtls_shutdown(void) { +void +coap_dtls_shutdown(void) { } void * @@ -473,11 +467,11 @@ coap_dtls_new_context(coap_context_t *c_context COAP_UNUSED) { const char *err; int ret; coap_gnutls_context_t *g_context = - (coap_gnutls_context_t *) - gnutls_malloc(sizeof(coap_gnutls_context_t)); + (coap_gnutls_context_t *) + gnutls_malloc(sizeof(coap_gnutls_context_t)); if (g_context) { - coap_tls_version_t* tls_version = coap_get_tls_library_version(); + coap_tls_version_t *tls_version = coap_get_tls_library_version(); const char *priority; G_CHECK(gnutls_global_init(), "gnutls_global_init"); @@ -490,21 +484,17 @@ coap_dtls_new_context(coap_context_t *c_context COAP_UNUSED) { if (tls_version->version >= 0x030606) { priority = VARIANTS_3_6_6; - } - else if (tls_version->version >= 0x030505) { + } else if (tls_version->version >= 0x030505) { priority = VARIANTS_3_5_5; - } - else { + } else { priority = VARIANTS_BASE; } ret = gnutls_priority_init(&g_context->priority_cache, priority, &err); if (ret != GNUTLS_E_SUCCESS) { if (ret == GNUTLS_E_INVALID_REQUEST) - coap_log_warn( - "gnutls_priority_init: Syntax error at: %s\n", err); + coap_log_warn("gnutls_priority_init: Syntax error at: %s\n", err); else - coap_log_warn( - "gnutls_priority_init: %s\n", gnutls_strerror(ret)); + coap_log_warn("gnutls_priority_init: %s\n", gnutls_strerror(ret)); goto fail; } } @@ -536,7 +526,7 @@ coap_dtls_free_context(void *handle) { gnutls_free(g_context->psk_sni_entry_list[i].sni); /* YUK - A memory leak in 3.3.0 (fixed by 3.3.26) of hint */ gnutls_psk_free_server_credentials( - g_context->psk_sni_entry_list[i].psk_credentials); + g_context->psk_sni_entry_list[i].psk_credentials); } if (g_context->psk_sni_entry_list) gnutls_free(g_context->psk_sni_entry_list); @@ -559,7 +549,7 @@ static int psk_client_callback(gnutls_session_t g_session, char **username, gnutls_datum_t *key) { coap_session_t *c_session = - (coap_session_t *)gnutls_transport_get_ptr(g_session); + (coap_session_t *)gnutls_transport_get_ptr(g_session); coap_gnutls_context_t *g_context; coap_dtls_cpsk_t *setup_data; const char *hint = gnutls_psk_client_get_hint(g_session); @@ -581,12 +571,12 @@ psk_client_callback(gnutls_session_t g_session, setup_data = &c_session->cpsk_setup_data; - temp.s = hint ? (const uint8_t*)hint : (const uint8_t*)""; - temp.length = strlen((const char*)temp.s); + temp.s = hint ? (const uint8_t *)hint : (const uint8_t *)""; + temp.length = strlen((const char *)temp.s); coap_session_refresh_psk_hint(c_session, &temp); coap_log_debug("got psk_identity_hint: '%.*s'\n", (int)temp.length, - (const char *)temp.s); + (const char *)temp.s); if (setup_data->validate_ih_call_back) { coap_str_const_t lhint; @@ -594,9 +584,9 @@ psk_client_callback(gnutls_session_t g_session, lhint.length = temp.length; lhint.s = temp.s; cpsk_info = - setup_data->validate_ih_call_back(&lhint, - c_session, - setup_data->ih_call_back_arg); + setup_data->validate_ih_call_back(&lhint, + c_session, + setup_data->ih_call_back_arg); if (cpsk_info == NULL) return -1; @@ -605,8 +595,7 @@ psk_client_callback(gnutls_session_t g_session, coap_session_refresh_psk_key(c_session, &cpsk_info->key); psk_identity = &cpsk_info->identity; psk_key = &cpsk_info->key; - } - else { + } else { psk_identity = coap_get_session_client_psk_identity(c_session); psk_key = coap_get_session_client_psk_key(c_session); } @@ -646,9 +635,9 @@ typedef struct { * return Type of certificate and SAN or CN if appropriate derived from * certificate. GNUTLS_CRT_UNKNOWN if failure. */ -static gnutls_certificate_type_t get_san_or_cn(gnutls_session_t g_session, - coap_gnutls_certificate_info_t *cert_info) -{ +static gnutls_certificate_type_t +get_san_or_cn(gnutls_session_t g_session, + coap_gnutls_certificate_info_t *cert_info) { gnutls_x509_crt_t cert; char dn[256]; size_t size; @@ -658,7 +647,7 @@ static gnutls_certificate_type_t get_san_or_cn(gnutls_session_t g_session, #if (GNUTLS_VERSION_NUMBER >= 0x030606) cert_info->certificate_type = gnutls_certificate_type_get2(g_session, - GNUTLS_CTYPE_PEERS); + GNUTLS_CTYPE_PEERS); #else /* < 3.6.6 */ cert_info->certificate_type = gnutls_certificate_type_get(g_session); #endif /* < 3.6.6 */ @@ -666,7 +655,7 @@ static gnutls_certificate_type_t get_san_or_cn(gnutls_session_t g_session, cert_info->san_or_cn = NULL; cert_info->cert_list = gnutls_certificate_get_peers(g_session, - &cert_info->cert_list_size); + &cert_info->cert_list_size); if (cert_info->cert_list_size == 0) { return GNUTLS_CRT_UNKNOWN; } @@ -678,7 +667,7 @@ static gnutls_certificate_type_t get_san_or_cn(gnutls_session_t g_session, /* Interested only in first cert in chain */ G_CHECK(gnutls_x509_crt_import(cert, &cert_info->cert_list[0], - GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import"); + GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import"); cert_info->self_signed = gnutls_x509_crt_check_issuer(cert, cert); @@ -755,12 +744,12 @@ check_rpk_cert(coap_gnutls_context_t *g_context, "gnutls_pubkey_export"); gnutls_pcert_deinit(&pcert); if (!g_context->setup_data.validate_cn_call_back(COAP_DTLS_RPK_CERT_CN, - der, - size, - c_session, - 0, - 1, - g_context->setup_data.cn_call_back_arg)) { + der, + size, + c_session, + 0, + 1, + g_context->setup_data.cn_call_back_arg)) { return 0; } } @@ -774,14 +763,14 @@ check_rpk_cert(coap_gnutls_context_t *g_context, * return 0 failed * 1 passed */ -static int cert_verify_gnutls(gnutls_session_t g_session) -{ +static int +cert_verify_gnutls(gnutls_session_t g_session) { unsigned int status = 0; unsigned int fail = 0; coap_session_t *c_session = - (coap_session_t *)gnutls_transport_get_ptr(g_session); + (coap_session_t *)gnutls_transport_get_ptr(g_session); coap_gnutls_context_t *g_context = - (coap_gnutls_context_t *)c_session->context->dtls_context; + (coap_gnutls_context_t *)c_session->context->dtls_context; coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; int alert = GNUTLS_A_BAD_CERTIFICATE; int ret; @@ -811,19 +800,16 @@ static int cert_verify_gnutls(gnutls_session_t g_session) if (status & (GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED)) { status &= ~(GNUTLS_CERT_NOT_ACTIVATED|GNUTLS_CERT_EXPIRED); if (g_context->setup_data.allow_expired_certs) { - coap_log_info( - " %s: %s: overridden: '%s'\n", - coap_session_str(c_session), - "The certificate has an invalid usage date", - OUTPUT_CERT_NAME); - } - else { + coap_log_info(" %s: %s: overridden: '%s'\n", + coap_session_str(c_session), + "The certificate has an invalid usage date", + OUTPUT_CERT_NAME); + } else { fail = 1; - coap_log_warn( - " %s: %s: '%s'\n", - coap_session_str(c_session), - "The certificate has an invalid usage date", - OUTPUT_CERT_NAME); + coap_log_warn(" %s: %s: '%s'\n", + coap_session_str(c_session), + "The certificate has an invalid usage date", + OUTPUT_CERT_NAME); } } if (status & (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED| @@ -831,19 +817,16 @@ static int cert_verify_gnutls(gnutls_session_t g_session) status &= ~(GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED| GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE); if (g_context->setup_data.allow_expired_crl) { - coap_log_info( - " %s: %s: overridden: '%s'\n", - coap_session_str(c_session), - "The certificate's CRL entry has an invalid usage date", - OUTPUT_CERT_NAME); - } - else { + coap_log_info(" %s: %s: overridden: '%s'\n", + coap_session_str(c_session), + "The certificate's CRL entry has an invalid usage date", + OUTPUT_CERT_NAME); + } else { fail = 1; - coap_log_warn( - " %s: %s: '%s'\n", - coap_session_str(c_session), - "The certificate's CRL entry has an invalid usage date", - OUTPUT_CERT_NAME); + coap_log_warn(" %s: %s: '%s'\n", + coap_session_str(c_session), + "The certificate's CRL entry has an invalid usage date", + OUTPUT_CERT_NAME); } } if (status & (GNUTLS_CERT_SIGNER_NOT_FOUND)) { @@ -851,48 +834,40 @@ static int cert_verify_gnutls(gnutls_session_t g_session) if (cert_info.self_signed) { if (g_context->setup_data.allow_self_signed && !g_context->setup_data.check_common_ca) { - coap_log_info( - " %s: %s: overridden: '%s'\n", - coap_session_str(c_session), - "Self-signed", - OUTPUT_CERT_NAME); - } - else { + coap_log_info(" %s: %s: overridden: '%s'\n", + coap_session_str(c_session), + "Self-signed", + OUTPUT_CERT_NAME); + } else { fail = 1; alert = GNUTLS_A_UNKNOWN_CA; - coap_log_warn( - " %s: %s: '%s'\n", - coap_session_str(c_session), - "Self-signed", - OUTPUT_CERT_NAME); + coap_log_warn(" %s: %s: '%s'\n", + coap_session_str(c_session), + "Self-signed", + OUTPUT_CERT_NAME); } - } - else { + } else { if (!g_context->setup_data.verify_peer_cert) { - coap_log_info( - " %s: %s: overridden: '%s'\n", - coap_session_str(c_session), - "The peer certificate's CA is unknown", - OUTPUT_CERT_NAME); - } - else { + coap_log_info(" %s: %s: overridden: '%s'\n", + coap_session_str(c_session), + "The peer certificate's CA is unknown", + OUTPUT_CERT_NAME); + } else { fail = 1; alert = GNUTLS_A_UNKNOWN_CA; - coap_log_warn( - " %s: %s: '%s'\n", - coap_session_str(c_session), - "The peer certificate's CA is unknown", - OUTPUT_CERT_NAME); + coap_log_warn(" %s: %s: '%s'\n", + coap_session_str(c_session), + "The peer certificate's CA is unknown", + OUTPUT_CERT_NAME); } } } if (status) { - fail = 1; - coap_log_warn( - " %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n", - coap_session_str(c_session), - status, OUTPUT_CERT_NAME); + fail = 1; + coap_log_warn(" %s: gnutls_certificate_verify_peers() status 0x%x: '%s'\n", + coap_session_str(c_session), + status, OUTPUT_CERT_NAME); } } @@ -911,19 +886,19 @@ static int cert_verify_gnutls(gnutls_session_t g_session) /* Interested only in first cert in chain */ G_CHECK(gnutls_x509_crt_import(cert, &cert_info.cert_list[0], - GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import"); + GNUTLS_X509_FMT_DER), "gnutls_x509_crt_import"); size = sizeof(der); G_CHECK(gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, der, &size), "gnutls_x509_crt_export"); gnutls_x509_crt_deinit(cert); if (!g_context->setup_data.validate_cn_call_back(OUTPUT_CERT_NAME, - der, - size, - c_session, - 0, - cert_is_trusted, - g_context->setup_data.cn_call_back_arg)) { + der, + size, + c_session, + 0, + cert_is_trusted, + g_context->setup_data.cn_call_back_arg)) { alert = GNUTLS_A_ACCESS_DENIED; goto fail; } @@ -932,7 +907,7 @@ static int cert_verify_gnutls(gnutls_session_t g_session) if (g_context->setup_data.additional_tls_setup_call_back) { /* Additional application setup wanted */ if (!g_context->setup_data.additional_tls_setup_call_back(g_session, - &g_context->setup_data)) { + &g_context->setup_data)) { goto fail; } } @@ -962,8 +937,8 @@ static int cert_verify_gnutls(gnutls_session_t g_session) * return -1 failed * 0 passed */ -static int cert_verify_callback_gnutls(gnutls_session_t g_session) -{ +static int +cert_verify_callback_gnutls(gnutls_session_t g_session) { if (gnutls_auth_get_type(g_session) == GNUTLS_CRD_CERTIFICATE) { if (cert_verify_gnutls(g_session) == 0) { return -1; @@ -982,8 +957,7 @@ pin_callback(void *user_data, int attempt, const char *token_label COAP_UNUSED, unsigned int flags COAP_UNUSED, char *pin, - size_t pin_max) -{ + size_t pin_max) { coap_dtls_pki_t *setup_data = (coap_dtls_pki_t *)user_data; /* Only do this on first attempt to prevent token lockout */ @@ -999,23 +973,22 @@ pin_callback(void *user_data, int attempt, /* first part of Raw public key, this is the start of the Subject Public Key */ static const unsigned char cert_asn1_header1[] = { 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, + 0x30, 0x13, /* SEQUENCE, length 19 bytes */ + 0x06, 0x07, /* OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1) */ + 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, }; /* PrimeX will get inserted */ #if 0 - 0x06, 0x08, /* OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7) */ - 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, +0x06, 0x08, /* OBJECT IDENTIFIER prime256v1 (1 2 840 10045 3 1 7) */ + 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, #endif static const unsigned char cert_asn1_header2[] = { - 0x03, 0x42, /* BIT STRING, length 66 bytes */ -/* Note: 0 bits (0x00) and no compression (0x04) are already in the certificate */ + 0x03, 0x42, /* BIT STRING, length 66 bytes */ + /* Note: 0 bits (0x00) and no compression (0x04) are already in the certificate */ }; static gnutls_datum_t * -get_asn1_spki(const uint8_t *data, size_t size) -{ +get_asn1_spki(const uint8_t *data, size_t size) { coap_binary_t *pub_key = get_asn1_tag(COAP_ASN1_BITSTRING, data, size, NULL); coap_binary_t *prime = get_asn1_tag(COAP_ASN1_IDENTIFIER, data, size, NULL); gnutls_datum_t *spki = NULL; @@ -1043,8 +1016,10 @@ get_asn1_spki(const uint8_t *data, size_t size) spki->size = header_size + pub_key->length; } } - if (pub_key) coap_delete_binary(pub_key); - if (prime) coap_delete_binary(prime); + if (pub_key) + coap_delete_binary(pub_key); + if (prime) + coap_delete_binary(prime); return spki; } #endif /* GNUTLS_VERSION_NUMBER >= 0x030606 */ @@ -1057,8 +1032,7 @@ static int setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, gnutls_session_t g_session, coap_gnutls_context_t *g_context, - coap_dtls_pki_t *setup_data, coap_dtls_role_t role) -{ + coap_dtls_pki_t *setup_data, coap_dtls_role_t role) { int ret; G_CHECK(gnutls_certificate_allocate_credentials(pki_credentials), @@ -1071,38 +1045,32 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, setup_data->pki_key.key.pem.private_key && setup_data->pki_key.key.pem.private_key[0]) { if (setup_data->is_rpk_not_cert) { - coap_log_warn( - "RPK keys cannot be in COAP_PKI_KEY_PEM format\n"); + coap_log_warn("RPK keys cannot be in COAP_PKI_KEY_PEM format\n"); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; - } - else { + } else { G_CHECK(gnutls_certificate_set_x509_key_file(*pki_credentials, - setup_data->pki_key.key.pem.public_cert, - setup_data->pki_key.key.pem.private_key, - GNUTLS_X509_FMT_PEM), - "gnutls_certificate_set_x509_key_file"); + setup_data->pki_key.key.pem.public_cert, + setup_data->pki_key.key.pem.private_key, + GNUTLS_X509_FMT_PEM), + "gnutls_certificate_set_x509_key_file"); } - } - else if (role == COAP_DTLS_ROLE_SERVER) { - coap_log_err( - "***setup_pki: (D)TLS: No %s Certificate + Private " - "Key defined\n", - role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client"); + } else if (role == COAP_DTLS_ROLE_SERVER) { + coap_log_err("***setup_pki: (D)TLS: No %s Certificate + Private " + "Key defined\n", + role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client"); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; } if (setup_data->pki_key.key.pem.ca_file && setup_data->pki_key.key.pem.ca_file[0]) { ret = gnutls_certificate_set_x509_trust_file(*pki_credentials, - setup_data->pki_key.key.pem.ca_file, - GNUTLS_X509_FMT_PEM); + setup_data->pki_key.key.pem.ca_file, + GNUTLS_X509_FMT_PEM); if (ret == 0) { - coap_log_warn( - "gnutls_certificate_set_x509_trust_file: No certificates found\n"); - } - else if (ret < 0) { + coap_log_warn("gnutls_certificate_set_x509_trust_file: No certificates found\n"); + } else if (ret < 0) { coap_log_warn("%s: '%s'\n", - "gnutls_certificate_set_x509_trust_file", - gnutls_strerror(ret)); + "gnutls_certificate_set_x509_trust_file", + gnutls_strerror(ret)); goto fail; } } @@ -1131,11 +1099,10 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, cert.size); cert.data[cert.size] = '\000'; cert.size++; - } - else { + } else { /* To get around const issue */ memcpy(&cert.data, - &setup_data->pki_key.key.pem_buf.public_cert, sizeof(cert.data)); + &setup_data->pki_key.key.pem_buf.public_cert, sizeof(cert.data)); } key.size = setup_data->pki_key.key.pem_buf.private_key_len; if (setup_data->pki_key.key.pem_buf.private_key[key.size-1] != '\000') { @@ -1144,14 +1111,14 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, key.data = gnutls_malloc(key.size + 1); if (!key.data) { coap_log_err("gnutls_malloc failure\n"); - if (alloced_cert_memory) gnutls_free(cert.data); + if (alloced_cert_memory) + gnutls_free(cert.data); return GNUTLS_E_MEMORY_ERROR; } memcpy(key.data, setup_data->pki_key.key.pem_buf.private_key, key.size); key.data[key.size] = '\000'; key.size++; - } - else { + } else { /* To get around const issue */ memcpy(&key.data, &setup_data->pki_key.key.pem_buf.private_key, sizeof(key.data)); @@ -1159,21 +1126,21 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, if (setup_data->is_rpk_not_cert) { #if (GNUTLS_VERSION_NUMBER >= 0x030606) int have_done_key = 0; - if (strstr ((char*)key.data, "-----BEGIN EC PRIVATE KEY-----")) { + if (strstr((char *)key.data, "-----BEGIN EC PRIVATE KEY-----")) { gnutls_datum_t der_private; if (gnutls_pem_base64_decode2("EC PRIVATE KEY", &key, &der_private) == 0) { gnutls_datum_t *spki = get_asn1_spki(der_private.data, - der_private.size); + der_private.size); if (spki) { ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials, - spki, - &der_private, - GNUTLS_X509_FMT_DER, NULL, - COAP_GNUTLS_KEY_RPK, - NULL, 0, 0); + spki, + &der_private, + GNUTLS_X509_FMT_DER, NULL, + COAP_GNUTLS_KEY_RPK, + NULL, 0, 0); if (ret >= 0) { have_done_key = 1; } @@ -1184,35 +1151,35 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, } if (!have_done_key) { G_CHECK(gnutls_certificate_set_rawpk_key_mem(*pki_credentials, - &cert, - &key, - GNUTLS_X509_FMT_PEM, NULL, - COAP_GNUTLS_KEY_RPK, - NULL, 0, 0), - "gnutls_certificate_set_rawpk_key_mem"); + &cert, + &key, + GNUTLS_X509_FMT_PEM, NULL, + COAP_GNUTLS_KEY_RPK, + NULL, 0, 0), + "gnutls_certificate_set_rawpk_key_mem"); } #else /* GNUTLS_VERSION_NUMBER < 0x030606 */ - coap_log_err( - "RPK Support not available (needs gnutls 3.6.6 or later)\n"); - if (alloced_cert_memory) gnutls_free(cert.data); - if (alloced_key_memory) gnutls_free(key.data); + coap_log_err("RPK Support not available (needs gnutls 3.6.6 or later)\n"); + if (alloced_cert_memory) + gnutls_free(cert.data); + if (alloced_key_memory) + gnutls_free(key.data); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; #endif /* GNUTLS_VERSION_NUMBER < 0x030606 */ - } - else { + } else { G_CHECK(gnutls_certificate_set_x509_key_mem(*pki_credentials, - &cert, - &key, - GNUTLS_X509_FMT_PEM), - "gnutls_certificate_set_x509_key_mem"); + &cert, + &key, + GNUTLS_X509_FMT_PEM), + "gnutls_certificate_set_x509_key_mem"); } - if (alloced_cert_memory) gnutls_free(cert.data); - if (alloced_key_memory) gnutls_free(key.data); - } - else if (role == COAP_DTLS_ROLE_SERVER) { - coap_log_err( - "***setup_pki: (D)TLS: No Server Certificate + Private " - "Key defined\n"); + if (alloced_cert_memory) + gnutls_free(cert.data); + if (alloced_key_memory) + gnutls_free(key.data); + } else if (role == COAP_DTLS_ROLE_SERVER) { + coap_log_err("***setup_pki: (D)TLS: No Server Certificate + Private " + "Key defined\n"); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; } if (setup_data->pki_key.key.pem_buf.ca_cert && @@ -1232,27 +1199,26 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, memcpy(ca.data, setup_data->pki_key.key.pem_buf.ca_cert, ca.size); ca.data[ca.size] = '\000'; ca.size++; - } - else { + } else { /* To get around const issue */ memcpy(&ca.data, &setup_data->pki_key.key.pem_buf.ca_cert, sizeof(ca.data)); } ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials, - &ca, - GNUTLS_X509_FMT_PEM); + &ca, + GNUTLS_X509_FMT_PEM); if (ret == 0) { - coap_log_warn( - "gnutls_certificate_set_x509_trust_mem: No certificates found\n"); - } - else if (ret < 0) { + coap_log_warn("gnutls_certificate_set_x509_trust_mem: No certificates found\n"); + } else if (ret < 0) { coap_log_warn("%s: '%s'\n", - "gnutls_certificate_set_x509_trust_mem", - gnutls_strerror(ret)); - if (alloced_ca_memory) gnutls_free(ca.data); + "gnutls_certificate_set_x509_trust_mem", + gnutls_strerror(ret)); + if (alloced_ca_memory) + gnutls_free(ca.data); goto fail; } - if (alloced_ca_memory) gnutls_free(ca.data); + if (alloced_ca_memory) + gnutls_free(ca.data); } break; @@ -1266,26 +1232,26 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, /* Kludge to get around const parameters */ memcpy(&cert.data, &setup_data->pki_key.key.asn1.public_cert, - sizeof(cert.data)); + sizeof(cert.data)); cert.size = setup_data->pki_key.key.asn1.public_cert_len; memcpy(&key.data, &setup_data->pki_key.key.asn1.private_key, - sizeof(key.data)); + sizeof(key.data)); key.size = setup_data->pki_key.key.asn1.private_key_len; if (setup_data->is_rpk_not_cert) { #if (GNUTLS_VERSION_NUMBER >= 0x030606) int have_done_key = 0; if (setup_data->pki_key.key.asn1.private_key_type == - COAP_ASN1_PKEY_EC) { + COAP_ASN1_PKEY_EC) { gnutls_datum_t *spki = get_asn1_spki(key.data, key.size); if (spki) { ret = gnutls_certificate_set_rawpk_key_mem(*pki_credentials, - spki, - &key, - GNUTLS_X509_FMT_DER, NULL, - COAP_GNUTLS_KEY_RPK, - NULL, 0, 0); + spki, + &key, + GNUTLS_X509_FMT_DER, NULL, + COAP_GNUTLS_KEY_RPK, + NULL, 0, 0); if (ret >= 0) { have_done_key = 1; } @@ -1294,32 +1260,28 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, } if (!have_done_key) { G_CHECK(gnutls_certificate_set_rawpk_key_mem(*pki_credentials, - &cert, - &key, - GNUTLS_X509_FMT_DER, NULL, - COAP_GNUTLS_KEY_RPK, - NULL, 0, 0), - "gnutls_certificate_set_rawpk_key_mem"); + &cert, + &key, + GNUTLS_X509_FMT_DER, NULL, + COAP_GNUTLS_KEY_RPK, + NULL, 0, 0), + "gnutls_certificate_set_rawpk_key_mem"); } #else /* GNUTLS_VERSION_NUMBER < 0x030606 */ - coap_log_err( - "RPK Support not available (needs gnutls 3.6.6 or later)\n"); - return GNUTLS_E_INSUFFICIENT_CREDENTIALS; + coap_log_err("RPK Support not available (needs gnutls 3.6.6 or later)\n"); + return GNUTLS_E_INSUFFICIENT_CREDENTIALS; #endif /* GNUTLS_VERSION_NUMBER < 0x030606 */ - } - else { + } else { G_CHECK(gnutls_certificate_set_x509_key_mem(*pki_credentials, - &cert, - &key, - GNUTLS_X509_FMT_DER), - "gnutls_certificate_set_x509_key_mem"); + &cert, + &key, + GNUTLS_X509_FMT_DER), + "gnutls_certificate_set_x509_key_mem"); } - } - else if (role == COAP_DTLS_ROLE_SERVER) { - coap_log_err( - "***setup_pki: (D)TLS: No %s Certificate + Private " - "Key defined\n", - role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client"); + } else if (role == COAP_DTLS_ROLE_SERVER) { + coap_log_err("***setup_pki: (D)TLS: No %s Certificate + Private " + "Key defined\n", + role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client"); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; } if (setup_data->pki_key.key.asn1.ca_cert && @@ -1328,19 +1290,17 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, /* Kludge to get around const parameters */ memcpy(&ca_cert.data, &setup_data->pki_key.key.asn1.ca_cert, - sizeof(ca_cert.data)); + sizeof(ca_cert.data)); ca_cert.size = setup_data->pki_key.key.asn1.ca_cert_len; ret = gnutls_certificate_set_x509_trust_mem(*pki_credentials, - &ca_cert, - GNUTLS_X509_FMT_DER); + &ca_cert, + GNUTLS_X509_FMT_DER); if (ret == 0) { - coap_log_warn( - "gnutls_certificate_set_x509_trust_mem: No certificates found\n"); - } - else if (ret < 0) { + coap_log_warn("gnutls_certificate_set_x509_trust_mem: No certificates found\n"); + } else if (ret < 0) { coap_log_warn("%s: '%s'\n", - "gnutls_certificate_set_x509_trust_mem", - gnutls_strerror(ret)); + "gnutls_certificate_set_x509_trust_mem", + gnutls_strerror(ret)); goto fail; } } @@ -1356,77 +1316,69 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, if (setup_data->is_rpk_not_cert) { #if (GNUTLS_VERSION_NUMBER >= 0x030606) G_CHECK(gnutls_certificate_set_rawpk_key_file(*pki_credentials, - setup_data->pki_key.key.pkcs11.public_cert, - setup_data->pki_key.key.pkcs11.private_key, - GNUTLS_X509_FMT_PEM, NULL, - COAP_GNUTLS_KEY_RPK, - NULL, 0, GNUTLS_PKCS_PLAIN, 0), - "gnutls_certificate_set_rawpk_key_file"); + setup_data->pki_key.key.pkcs11.public_cert, + setup_data->pki_key.key.pkcs11.private_key, + GNUTLS_X509_FMT_PEM, NULL, + COAP_GNUTLS_KEY_RPK, + NULL, 0, GNUTLS_PKCS_PLAIN, 0), + "gnutls_certificate_set_rawpk_key_file"); #else /* GNUTLS_VERSION_NUMBER < 0x030606 */ - coap_log_err( - "RPK Support not available (needs gnutls 3.6.6 or later)\n"); - return GNUTLS_E_INSUFFICIENT_CREDENTIALS; + coap_log_err("RPK Support not available (needs gnutls 3.6.6 or later)\n"); + return GNUTLS_E_INSUFFICIENT_CREDENTIALS; #endif /* GNUTLS_VERSION_NUMBER < 0x030606 */ - } - else { + } else { G_CHECK(gnutls_certificate_set_x509_key_file(*pki_credentials, - setup_data->pki_key.key.pkcs11.public_cert, - setup_data->pki_key.key.pkcs11.private_key, - GNUTLS_X509_FMT_DER), - "gnutls_certificate_set_x509_key_file"); + setup_data->pki_key.key.pkcs11.public_cert, + setup_data->pki_key.key.pkcs11.private_key, + GNUTLS_X509_FMT_DER), + "gnutls_certificate_set_x509_key_file"); } - } - else if (role == COAP_DTLS_ROLE_SERVER) { - coap_log_err( - "***setup_pki: (D)TLS: No %s Certificate + Private " - "Key defined\n", - role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client"); + } else if (role == COAP_DTLS_ROLE_SERVER) { + coap_log_err("***setup_pki: (D)TLS: No %s Certificate + Private " + "Key defined\n", + role == COAP_DTLS_ROLE_SERVER ? "Server" : "Client"); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; } if (setup_data->pki_key.key.pkcs11.ca && setup_data->pki_key.key.pkcs11.ca[0]) { ret = gnutls_certificate_set_x509_trust_file(*pki_credentials, - setup_data->pki_key.key.pkcs11.ca, - GNUTLS_X509_FMT_DER); + setup_data->pki_key.key.pkcs11.ca, + GNUTLS_X509_FMT_DER); if (ret == 0) { - coap_log_warn( - "gnutls_certificate_set_x509_trust_file: No certificates found\n"); - } - else if (ret < 0) { + coap_log_warn("gnutls_certificate_set_x509_trust_file: No certificates found\n"); + } else if (ret < 0) { coap_log_warn("%s: '%s'\n", - "gnutls_certificate_set_x509_trust_file", - gnutls_strerror(ret)); + "gnutls_certificate_set_x509_trust_file", + gnutls_strerror(ret)); goto fail; } } break; default: - coap_log_err( - "***setup_pki: (D)TLS: Unknown key type %d\n", - setup_data->pki_key.key_type); + coap_log_err("***setup_pki: (D)TLS: Unknown key type %d\n", + setup_data->pki_key.key_type); return GNUTLS_E_INSUFFICIENT_CREDENTIALS; } if (g_context->root_ca_file) { ret = gnutls_certificate_set_x509_trust_file(*pki_credentials, - g_context->root_ca_file, - GNUTLS_X509_FMT_PEM); + g_context->root_ca_file, + GNUTLS_X509_FMT_PEM); if (ret == 0) { - coap_log_warn( - "gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n"); + coap_log_warn("gnutls_certificate_set_x509_trust_file: Root CA: No certificates found\n"); } } if (g_context->root_ca_path) { #if (GNUTLS_VERSION_NUMBER >= 0x030306) G_CHECK(gnutls_certificate_set_x509_trust_dir(*pki_credentials, - g_context->root_ca_path, - GNUTLS_X509_FMT_PEM), + g_context->root_ca_path, + GNUTLS_X509_FMT_PEM), "gnutls_certificate_set_x509_trust_dir"); #endif } gnutls_certificate_send_x509_rdn_sequence(g_session, - setup_data->check_common_ca ? 0 : 1); + setup_data->check_common_ca ? 0 : 1); if (!(g_context->psk_pki_enabled & IS_PKI)) { /* No PKI defined at all - still need a trust set up for 3.6.0 or later */ G_CHECK(gnutls_certificate_set_x509_system_trust(*pki_credentials), @@ -1441,7 +1393,7 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, if (setup_data->cert_chain_validation) { gnutls_certificate_set_verify_limits(*pki_credentials, 0, - setup_data->cert_chain_verify_depth + 2); + setup_data->cert_chain_verify_depth + 2); } /* @@ -1449,9 +1401,9 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, * CRL checking (can raise GNUTLS_CERT_MISSING_OCSP_STATUS) */ gnutls_certificate_set_verify_flags(*pki_credentials, - (setup_data->check_cert_revocation == 0 ? - GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0) - ); + (setup_data->check_cert_revocation == 0 ? + GNUTLS_VERIFY_DISABLE_CRL_CHECKS : 0) + ); return GNUTLS_E_SUCCESS; @@ -1467,20 +1419,19 @@ setup_pki_credentials(gnutls_certificate_credentials_t *pki_credentials, static int setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials, coap_gnutls_context_t *g_context COAP_UNUSED, - coap_dtls_spsk_t *setup_data) -{ + coap_dtls_spsk_t *setup_data) { int ret; char hint[COAP_DTLS_HINT_LENGTH]; G_CHECK(gnutls_psk_allocate_server_credentials(psk_credentials), "gnutls_psk_allocate_server_credentials"); gnutls_psk_set_server_credentials_function(*psk_credentials, - psk_server_callback); + psk_server_callback); if (setup_data->psk_info.hint.s) { snprintf(hint, sizeof(hint), "%.*s", (int)setup_data->psk_info.hint.length, setup_data->psk_info.hint.s); G_CHECK(gnutls_psk_set_server_credentials_hint(*psk_credentials, hint), - "gnutls_psk_set_server_credentials_hint"); + "gnutls_psk_set_server_credentials_hint"); } return GNUTLS_E_SUCCESS; @@ -1494,12 +1445,11 @@ setup_psk_credentials(gnutls_psk_server_credentials_t *psk_credentials, * neg GNUTLS_E_* error code */ static int -post_client_hello_gnutls_psk(gnutls_session_t g_session) -{ +post_client_hello_gnutls_psk(gnutls_session_t g_session) { coap_session_t *c_session = - (coap_session_t *)gnutls_transport_get_ptr(g_session); + (coap_session_t *)gnutls_transport_get_ptr(g_session); coap_gnutls_context_t *g_context = - (coap_gnutls_context_t *)c_session->context->dtls_context; + (coap_gnutls_context_t *)c_session->context->dtls_context; coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; int ret = GNUTLS_E_SUCCESS; char *name = NULL; @@ -1515,7 +1465,7 @@ post_client_hello_gnutls_psk(gnutls_session_t g_session) if (name == NULL) return GNUTLS_E_MEMORY_ERROR; - for (i=0; ; ) { + for (i=0; ;) { ret = gnutls_server_name_get(g_session, name, &len, &type, i); if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) { char *new_name; @@ -1556,9 +1506,9 @@ post_client_hello_gnutls_psk(gnutls_session_t g_session) * New SNI request */ const coap_dtls_spsk_info_t *new_entry = - c_session->context->spsk_setup_data.validate_sni_call_back(name, - c_session, - c_session->context->spsk_setup_data.sni_call_back_arg); + c_session->context->spsk_setup_data.validate_sni_call_back(name, + c_session, + c_session->context->spsk_setup_data.sni_call_back_arg); if (!new_entry) { G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, GNUTLS_A_UNRECOGNIZED_NAME)); @@ -1568,16 +1518,16 @@ post_client_hello_gnutls_psk(gnutls_session_t g_session) } g_context->psk_sni_entry_list = - gnutls_realloc(g_context->psk_sni_entry_list, - (i+1)*sizeof(psk_sni_entry)); + gnutls_realloc(g_context->psk_sni_entry_list, + (i+1)*sizeof(psk_sni_entry)); g_context->psk_sni_entry_list[i].sni = gnutls_strdup(name); g_context->psk_sni_entry_list[i].psk_info = *new_entry; sni_setup_data = c_session->context->spsk_setup_data; sni_setup_data.psk_info = *new_entry; if ((ret = setup_psk_credentials( - &g_context->psk_sni_entry_list[i].psk_credentials, - g_context, - &sni_setup_data)) < 0) { + &g_context->psk_sni_entry_list[i].psk_credentials, + g_context, + &sni_setup_data)) < 0) { int keep_ret = ret; G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, GNUTLS_A_BAD_CERTIFICATE)); @@ -1588,10 +1538,10 @@ post_client_hello_gnutls_psk(gnutls_session_t g_session) g_context->psk_sni_count++; } G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK, - g_context->psk_sni_entry_list[i].psk_credentials), + g_context->psk_sni_entry_list[i].psk_credentials), "gnutls_credentials_set"); coap_session_refresh_psk_hint(c_session, - &g_context->psk_sni_entry_list[i].psk_info.hint); + &g_context->psk_sni_entry_list[i].psk_info.hint); coap_session_refresh_psk_key(c_session, &g_context->psk_sni_entry_list[i].psk_info.key); } @@ -1609,12 +1559,11 @@ post_client_hello_gnutls_psk(gnutls_session_t g_session) * neg GNUTLS_E_* error code */ static int -post_client_hello_gnutls_pki(gnutls_session_t g_session) -{ +post_client_hello_gnutls_pki(gnutls_session_t g_session) { coap_session_t *c_session = - (coap_session_t *)gnutls_transport_get_ptr(g_session); + (coap_session_t *)gnutls_transport_get_ptr(g_session); coap_gnutls_context_t *g_context = - (coap_gnutls_context_t *)c_session->context->dtls_context; + (coap_gnutls_context_t *)c_session->context->dtls_context; coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; int ret = GNUTLS_E_SUCCESS; char *name = NULL; @@ -1630,7 +1579,7 @@ post_client_hello_gnutls_pki(gnutls_session_t g_session) if (name == NULL) return GNUTLS_E_MEMORY_ERROR; - for (i=0; ; ) { + for (i=0; ;) { ret = gnutls_server_name_get(g_session, name, &len, &type, i); if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) { char *new_name; @@ -1671,8 +1620,8 @@ post_client_hello_gnutls_pki(gnutls_session_t g_session) * New SNI request */ coap_dtls_key_t *new_entry = - g_context->setup_data.validate_sni_call_back(name, - g_context->setup_data.sni_call_back_arg); + g_context->setup_data.validate_sni_call_back(name, + g_context->setup_data.sni_call_back_arg); if (!new_entry) { G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, GNUTLS_A_UNRECOGNIZED_NAME)); @@ -1682,17 +1631,17 @@ post_client_hello_gnutls_pki(gnutls_session_t g_session) } g_context->pki_sni_entry_list = gnutls_realloc( - g_context->pki_sni_entry_list, - (i+1)*sizeof(pki_sni_entry)); + g_context->pki_sni_entry_list, + (i+1)*sizeof(pki_sni_entry)); g_context->pki_sni_entry_list[i].sni = gnutls_strdup(name); g_context->pki_sni_entry_list[i].pki_key = *new_entry; sni_setup_data = g_context->setup_data; sni_setup_data.pki_key = *new_entry; if ((ret = setup_pki_credentials( - &g_context->pki_sni_entry_list[i].pki_credentials, - g_session, - g_context, - &sni_setup_data, COAP_DTLS_ROLE_SERVER)) < 0) { + &g_context->pki_sni_entry_list[i].pki_credentials, + g_session, + g_context, + &sni_setup_data, COAP_DTLS_ROLE_SERVER)) < 0) { int keep_ret = ret; G_ACTION(gnutls_alert_send(g_session, GNUTLS_AL_FATAL, GNUTLS_A_BAD_CERTIFICATE)); @@ -1703,7 +1652,7 @@ post_client_hello_gnutls_pki(gnutls_session_t g_session) g_context->pki_sni_count++; } G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE, - g_context->pki_sni_entry_list[i].pki_credentials), + g_context->pki_sni_entry_list[i].pki_credentials), "gnutls_credentials_set"); } @@ -1722,10 +1671,9 @@ post_client_hello_gnutls_pki(gnutls_session_t g_session) * neg GNUTLS_E_* error code */ static int -setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) -{ +setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) { coap_gnutls_context_t *g_context = - (coap_gnutls_context_t *)c_session->context->dtls_context; + (coap_gnutls_context_t *)c_session->context->dtls_context; int ret; g_context->psk_pki_enabled |= IS_CLIENT; @@ -1747,7 +1695,7 @@ setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) } if (setup_data->validate_ih_call_back) { const char *err; - coap_tls_version_t* tls_version = coap_get_tls_library_version(); + coap_tls_version_t *tls_version = coap_get_tls_library_version(); if (tls_version->version >= 0x030604) { /* Disable TLS1.3 if Identity Hint Callback set */ @@ -1755,19 +1703,16 @@ setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) if (tls_version->version >= 0x030606) { priority = VARIANTS_NO_TLS13_3_6_6; - } - else { + } else { priority = VARIANTS_NO_TLS13_3_6_4; } ret = gnutls_priority_set_direct(g_env->g_session, - priority, &err); + priority, &err); if (ret < 0) { if (ret == GNUTLS_E_INVALID_REQUEST) - coap_log_warn( - "gnutls_priority_set_direct: Syntax error at: %s\n", err); + coap_log_warn("gnutls_priority_set_direct: Syntax error at: %s\n", err); else - coap_log_warn( - "gnutls_priority_set_direct: %s\n", gnutls_strerror(ret)); + coap_log_warn("gnutls_priority_set_direct: %s\n", gnutls_strerror(ret)); goto fail; } } @@ -1821,10 +1766,9 @@ setup_client_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) static int psk_server_callback(gnutls_session_t g_session, const char *identity, - gnutls_datum_t *key) -{ + gnutls_datum_t *key) { coap_session_t *c_session = - (coap_session_t *)gnutls_transport_get_ptr(g_session); + (coap_session_t *)gnutls_transport_get_ptr(g_session); coap_gnutls_context_t *g_context; coap_dtls_spsk_t *setup_data; coap_bin_const_t lidentity; @@ -1840,12 +1784,12 @@ psk_server_callback(gnutls_session_t g_session, /* Track the Identity being used */ - lidentity.s = identity ? (const uint8_t*)identity : (const uint8_t*)""; - lidentity.length = strlen((const char*)lidentity.s); + lidentity.s = identity ? (const uint8_t *)identity : (const uint8_t *)""; + lidentity.length = strlen((const char *)lidentity.s); coap_session_refresh_psk_identity(c_session, &lidentity); coap_log_debug("got psk_identity: '%.*s'\n", - (int)lidentity.length, (const char *)lidentity.s); + (int)lidentity.length, (const char *)lidentity.s); if (setup_data->validate_id_call_back) { psk_key = setup_data->validate_id_call_back(&lidentity, @@ -1853,8 +1797,7 @@ psk_server_callback(gnutls_session_t g_session, setup_data->id_call_back_arg); coap_session_refresh_psk_key(c_session, psk_key); - } - else { + } else { psk_key = coap_get_session_server_psk_key(c_session); } @@ -1874,25 +1817,24 @@ psk_server_callback(gnutls_session_t g_session, * neg GNUTLS_E_* error code */ static int -setup_server_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) -{ +setup_server_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) { coap_gnutls_context_t *g_context = - (coap_gnutls_context_t *)c_session->context->dtls_context; + (coap_gnutls_context_t *)c_session->context->dtls_context; int ret = GNUTLS_E_SUCCESS; g_context->psk_pki_enabled |= IS_SERVER; if (g_context->psk_pki_enabled & IS_PSK) { G_CHECK(setup_psk_credentials( - &g_env->psk_sv_credentials, - g_context, - &c_session->context->spsk_setup_data), + &g_env->psk_sv_credentials, + g_context, + &c_session->context->spsk_setup_data), "setup_psk_credentials\n"); G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_PSK, g_env->psk_sv_credentials), "gnutls_credentials_set\n"); gnutls_handshake_set_post_client_hello_function(g_env->g_session, - post_client_hello_gnutls_psk); + post_client_hello_gnutls_psk); } if (g_context->psk_pki_enabled & IS_PKI) { @@ -1905,18 +1847,16 @@ setup_server_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) if (setup_data->verify_peer_cert) { gnutls_certificate_server_set_request(g_env->g_session, GNUTLS_CERT_REQUIRE); - } - else if (setup_data->is_rpk_not_cert) { + } else if (setup_data->is_rpk_not_cert) { gnutls_certificate_server_set_request(g_env->g_session, GNUTLS_CERT_REQUEST); - } - else { + } else { gnutls_certificate_server_set_request(g_env->g_session, GNUTLS_CERT_IGNORE); } gnutls_handshake_set_post_client_hello_function(g_env->g_session, - post_client_hello_gnutls_pki); + post_client_hello_gnutls_pki); G_CHECK(gnutls_credentials_set(g_env->g_session, GNUTLS_CRD_CERTIFICATE, g_env->pki_credentials), @@ -1935,8 +1875,7 @@ setup_server_ssl_session(coap_session_t *c_session, coap_gnutls_env_t *g_env) * -1 error (error in errno) */ static ssize_t -coap_dgram_read(gnutls_transport_ptr_t context, void *out, size_t outl) -{ +coap_dgram_read(gnutls_transport_ptr_t context, void *out, size_t outl) { ssize_t ret = 0; coap_session_t *c_session = (coap_session_t *)context; coap_ssl_t *data; @@ -1964,8 +1903,7 @@ coap_dgram_read(gnutls_transport_ptr_t context, void *out, size_t outl) data->pdu = NULL; } } - } - else { + } else { errno = EAGAIN; ret = -1; } @@ -1981,7 +1919,7 @@ coap_dgram_read(gnutls_transport_ptr_t context, void *out, size_t outl) /* callback function given to gnutls for sending data over socket */ static ssize_t coap_dgram_write(gnutls_transport_ptr_t context, const void *send_buffer, - size_t send_buffer_length) { + size_t send_buffer_length) { ssize_t result = -1; coap_session_t *c_session = (coap_session_t *)context; @@ -1990,13 +1928,13 @@ coap_dgram_write(gnutls_transport_ptr_t context, const void *send_buffer, #if COAP_SERVER_SUPPORT && c_session->endpoint == NULL #endif /* COAP_SERVER_SUPPORT */ - ) { + ) { /* socket was closed on client due to error */ errno = ECONNRESET; return -1; } result = c_session->sock.lfunc[COAP_LAYER_TLS].write(c_session, - send_buffer, send_buffer_length); + send_buffer, send_buffer_length); if (result != (int)send_buffer_length) { coap_log_warn("coap_netif_dgrm_write failed (%zd != %zu)\n", result, send_buffer_length); @@ -2032,10 +1970,10 @@ receive_timeout(gnutls_transport_ptr_t context, unsigned int ms COAP_UNUSED) { FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds); - FD_SET (c_session->sock.fd, &readfds); + FD_SET(c_session->sock.fd, &readfds); if (!(g_env && g_env->doing_dtls_timeout)) { - FD_SET (c_session->sock.fd, &writefds); - FD_SET (c_session->sock.fd, &exceptfds); + FD_SET(c_session->sock.fd, &writefds); + FD_SET(c_session->sock.fd, &exceptfds); } /* Polling */ tv.tv_sec = 0; @@ -2047,10 +1985,9 @@ receive_timeout(gnutls_transport_ptr_t context, unsigned int ms COAP_UNUSED) { } static coap_gnutls_env_t * -coap_dtls_new_gnutls_env(coap_session_t *c_session, int type) -{ +coap_dtls_new_gnutls_env(coap_session_t *c_session, int type) { coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_session->context->dtls_context); + ((coap_gnutls_context_t *)c_session->context->dtls_context); coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; #if (GNUTLS_VERSION_NUMBER >= 0x030606) int flags = type | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK; @@ -2086,8 +2023,7 @@ coap_dtls_new_gnutls_env(coap_session_t *c_session, int type) #else /* ! COAP_SERVER_SUPPORT */ goto fail; #endif /* ! COAP_SERVER_SUPPORT */ - } - else { + } else { #if COAP_CLIENT_SUPPORT G_CHECK(setup_client_ssl_session(c_session, g_env), "setup_client_ssl_session"); @@ -2099,7 +2035,7 @@ coap_dtls_new_gnutls_env(coap_session_t *c_session, int type) gnutls_handshake_set_timeout(g_env->g_session, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT); gnutls_dtls_set_timeouts(g_env->g_session, COAP_DTLS_RETRANSMIT_MS, - COAP_DTLS_RETRANSMIT_TOTAL_MS); + COAP_DTLS_RETRANSMIT_TOTAL_MS); return g_env; @@ -2112,8 +2048,7 @@ coap_dtls_new_gnutls_env(coap_session_t *c_session, int type) static void coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context, coap_gnutls_env_t *g_env, - coap_free_bye_t free_bye) -{ + coap_free_bye_t free_bye) { if (g_env) { /* It is suggested not to use GNUTLS_SHUT_RDWR in DTLS * connections because the peer's closure message might @@ -2121,7 +2056,7 @@ coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context, if (free_bye != COAP_FREE_BYE_NONE && !g_env->sent_alert) { /* Only do this if appropriate */ gnutls_bye(g_env->g_session, free_bye == COAP_FREE_BYE_AS_UDP ? - GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR); + GNUTLS_SHUT_WR : GNUTLS_SHUT_RDWR); } gnutls_deinit(g_env->g_session); g_env->g_session = NULL; @@ -2130,8 +2065,7 @@ coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context, g_env->psk_cl_credentials != NULL) { gnutls_psk_free_client_credentials(g_env->psk_cl_credentials); g_env->psk_cl_credentials = NULL; - } - else { + } else { /* YUK - A memory leak in 3.3.0 (fixed by 3.3.26) of hint */ if (g_env->psk_sv_credentials != NULL) gnutls_psk_free_server_credentials(g_env->psk_sv_credentials); @@ -2153,7 +2087,7 @@ coap_dtls_free_gnutls_env(coap_gnutls_context_t *g_context, void * coap_dtls_new_server_session(coap_session_t *c_session) { coap_gnutls_env_t *g_env = - (coap_gnutls_env_t *)c_session->tls; + (coap_gnutls_env_t *)c_session->tls; gnutls_transport_set_ptr(g_env->g_session, c_session); @@ -2161,18 +2095,19 @@ coap_dtls_new_server_session(coap_session_t *c_session) { } #endif /* COAP_SERVER_SUPPORT */ -static void log_last_alert(coap_session_t *c_session, - gnutls_session_t g_session) { +static void +log_last_alert(coap_session_t *c_session, + gnutls_session_t g_session) { int last_alert = gnutls_alert_get(g_session); if (last_alert == GNUTLS_A_CLOSE_NOTIFY) coap_log_debug("***%s: Alert '%d': %s\n", - coap_session_str(c_session), - last_alert, gnutls_alert_get_name(last_alert)); + coap_session_str(c_session), + last_alert, gnutls_alert_get_name(last_alert)); else coap_log_warn("***%s: Alert '%d': %s\n", - coap_session_str(c_session), - last_alert, gnutls_alert_get_name(last_alert)); + coap_session_str(c_session), + last_alert, gnutls_alert_get_name(last_alert)); } /* @@ -2189,7 +2124,7 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) { case GNUTLS_E_SUCCESS: g_env->established = 1; coap_log_debug("* %s: GnuTLS established\n", - coap_session_str(c_session)); + coap_session_str(c_session)); ret = 1; break; case GNUTLS_E_INTERRUPTED: @@ -2201,15 +2136,14 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) { ret = 0; break; case GNUTLS_E_INSUFFICIENT_CREDENTIALS: - coap_log_warn( - "Insufficient credentials provided.\n"); + coap_log_warn("Insufficient credentials provided.\n"); ret = -1; break; case GNUTLS_E_FATAL_ALERT_RECEIVED: /* Stop the sending of an alert on closedown */ g_env->sent_alert = 1; log_last_alert(c_session, g_env->g_session); - /* Fall through */ + /* Fall through */ case GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET: case GNUTLS_E_UNEXPECTED_PACKET: c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; @@ -2224,22 +2158,20 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) { #if (GNUTLS_VERSION_NUMBER > 0x030606) case GNUTLS_E_CERTIFICATE_REQUIRED: #endif /* GNUTLS_VERSION_NUMBER > 0x030606 */ - coap_log_warn( - "Client Certificate requested and required, but not provided\n" - ); + coap_log_warn("Client Certificate requested and required, but not provided\n" + ); G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL, - GNUTLS_A_BAD_CERTIFICATE)); + GNUTLS_A_BAD_CERTIFICATE)); g_env->sent_alert = 1; c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; ret = -1; break; case GNUTLS_E_DECRYPTION_FAILED: - coap_log_warn( - "do_gnutls_handshake: session establish " - "returned '%s'\n", - gnutls_strerror(ret)); + coap_log_warn("do_gnutls_handshake: session establish " + "returned '%s'\n", + gnutls_strerror(ret)); G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL, - GNUTLS_A_DECRYPT_ERROR)); + GNUTLS_A_DECRYPT_ERROR)); g_env->sent_alert = 1; c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; ret = -1; @@ -2250,17 +2182,16 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) { ret = -1; break; } - /* Fall through */ + /* Fall through */ case GNUTLS_E_UNKNOWN_CIPHER_SUITE: case GNUTLS_E_NO_CIPHER_SUITES: case GNUTLS_E_INVALID_SESSION: - coap_log_warn( - "do_gnutls_handshake: session establish " - "returned '%s'\n", - gnutls_strerror(ret)); + coap_log_warn("do_gnutls_handshake: session establish " + "returned '%s'\n", + gnutls_strerror(ret)); if (!g_env->sent_alert) { G_ACTION(gnutls_alert_send(g_env->g_session, GNUTLS_AL_FATAL, - GNUTLS_A_HANDSHAKE_FAILURE)); + GNUTLS_A_HANDSHAKE_FAILURE)); g_env->sent_alert = 1; } c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; @@ -2275,10 +2206,9 @@ do_gnutls_handshake(coap_session_t *c_session, coap_gnutls_env_t *g_env) { ret = -1; break; default: - coap_log_warn( - "do_gnutls_handshake: session establish " - "returned %d: '%s'\n", - ret, gnutls_strerror(ret)); + coap_log_warn("do_gnutls_handshake: session establish " + "returned %d: '%s'\n", + ret, gnutls_strerror(ret)); ret = -1; break; } @@ -2297,7 +2227,7 @@ coap_dtls_new_client_session(coap_session_t *c_session) { coap_dtls_free_gnutls_env(c_session->context->dtls_context, g_env, COAP_PROTO_NOT_RELIABLE(c_session->proto) ? - COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP); + COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP); return NULL; } } @@ -2309,15 +2239,16 @@ void coap_dtls_free_session(coap_session_t *c_session) { if (c_session && c_session->context && c_session->tls) { coap_dtls_free_gnutls_env(c_session->context->dtls_context, - c_session->tls, - COAP_PROTO_NOT_RELIABLE(c_session->proto) ? - COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP); + c_session->tls, + COAP_PROTO_NOT_RELIABLE(c_session->proto) ? + COAP_FREE_BYE_AS_UDP : COAP_FREE_BYE_AS_TCP); c_session->tls = NULL; coap_handle_event(c_session->context, COAP_EVENT_DTLS_CLOSED, c_session); } } -void coap_dtls_session_update_mtu(coap_session_t *c_session) { +void +coap_dtls_session_update_mtu(coap_session_t *c_session) { coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; int ret; @@ -2359,10 +2290,9 @@ coap_dtls_send(coap_session_t *c_session, ret = -1; break; default: - coap_log_debug( - "coap_dtls_send: gnutls_record_send " - "returned %d: '%s'\n", - ret, gnutls_strerror(ret)); + coap_log_debug("coap_dtls_send: gnutls_record_send " + "returned %d: '%s'\n", + ret, gnutls_strerror(ret)); ret = -1; break; } @@ -2370,8 +2300,7 @@ coap_dtls_send(coap_session_t *c_session, coap_log_warn("coap_dtls_send: cannot send PDU\n"); } } - } - else { + } else { ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { /* Just connected, so send the data */ @@ -2400,15 +2329,18 @@ coap_dtls_send(coap_session_t *c_session, return ret; } -int coap_dtls_is_context_timeout(void) { +int +coap_dtls_is_context_timeout(void) { return 0; } -coap_tick_t coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) { +coap_tick_t +coap_dtls_get_context_timeout(void *dtls_context COAP_UNUSED) { return 0; } -coap_tick_t coap_dtls_get_timeout(coap_session_t *c_session, coap_tick_t now) { +coap_tick_t +coap_dtls_get_timeout(coap_session_t *c_session, coap_tick_t now) { coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; assert(c_session->state == COAP_SESSION_STATE_HANDSHAKE); @@ -2449,8 +2381,7 @@ coap_dtls_handle_timeout(coap_session_t *c_session) { g_env->doing_dtls_timeout = 0; coap_session_disconnected(c_session, COAP_NACK_TLS_FAILED); return 1; - } - else { + } else { g_env->doing_dtls_timeout = 0; return 0; } @@ -2474,7 +2405,7 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data, if (ssl_data->pdu_len) coap_log_err("** %s: Previous data not read %u bytes\n", - coap_session_str(c_session), ssl_data->pdu_len); + coap_session_str(c_session), ssl_data->pdu_len); ssl_data->pdu = data; ssl_data->pdu_len = data_len; @@ -2489,11 +2420,9 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data, ret = gnutls_record_recv(g_env->g_session, pdu, (int)sizeof(pdu)); if (ret > 0) { return coap_handle_dgram(c_session->context, c_session, pdu, (size_t)ret); - } - else if (ret == 0) { + } else if (ret == 0) { c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; - } - else { + } else { switch (ret) { case GNUTLS_E_FATAL_ALERT_RECEIVED: /* Stop the sending of an alert on closedown */ @@ -2508,26 +2437,23 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data, ret = 0; break; default: - coap_log_warn( - "coap_dtls_receive: gnutls_record_recv returned %d\n", ret); + coap_log_warn("coap_dtls_receive: gnutls_record_recv returned %d\n", ret); ret = -1; break; } } - } - else { + } else { ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { coap_session_connected(c_session); - } - else { + } else { ret = -1; if (ssl_data->pdu_len && !g_env->sent_alert) { /* Do the handshake again incase of internal timeout */ ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { /* Just connected, so send the data */ - coap_session_connected(c_session); + coap_session_connected(c_session); } } } @@ -2565,9 +2491,9 @@ coap_dtls_receive(coap_session_t *c_session, const uint8_t *data, */ int coap_dtls_hello(coap_session_t *c_session, - const uint8_t *data, - size_t data_len -) { + const uint8_t *data, + size_t data_len + ) { coap_gnutls_env_t *g_env = (coap_gnutls_env_t *)c_session->tls; coap_ssl_t *ssl_data; int ret; @@ -2578,8 +2504,7 @@ coap_dtls_hello(coap_session_t *c_session, c_session->tls = g_env; gnutls_key_generate(&g_env->coap_ssl_data.cookie_key, GNUTLS_COOKIE_KEY_SIZE); - } - else { + } else { /* error should have already been reported */ return -1; } @@ -2590,12 +2515,12 @@ coap_dtls_hello(coap_session_t *c_session, memset(&prestate, 0, sizeof(prestate)); /* 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)); ret = gnutls_dtls_cookie_verify(&g_env->coap_ssl_data.cookie_key, - &c_session->addr_info, - sizeof(c_session->addr_info), - data_rw, data_len, - &prestate); + &c_session->addr_info, + sizeof(c_session->addr_info), + data_rw, data_len, + &prestate); if (ret < 0) { /* cookie not valid */ coap_log_debug("Invalid Cookie - sending Hello Verify\n"); gnutls_dtls_cookie_send(&g_env->coap_ssl_data.cookie_key, @@ -2619,14 +2544,12 @@ coap_dtls_hello(coap_session_t *c_session, * as the above failed, need to remove g_env to clean up any * pollution of the information */ - coap_dtls_free_gnutls_env( - ((coap_gnutls_context_t *)c_session->context->dtls_context), - g_env, COAP_FREE_BYE_NONE); + coap_dtls_free_gnutls_env(((coap_gnutls_context_t *)c_session->context->dtls_context), + g_env, COAP_FREE_BYE_NONE); c_session->tls = NULL; ssl_data = NULL; ret = -1; - } - else { + } else { /* Client Hello has been seen */ ret = 1; } @@ -2634,14 +2557,15 @@ coap_dtls_hello(coap_session_t *c_session, if (ssl_data && ssl_data->pdu_len) { /* pdu data is held on stack which will not stay there */ coap_log_debug("coap_dtls_hello: ret %d: remaining data %u\n", ret, ssl_data->pdu_len); - ssl_data->pdu_len = 0; - ssl_data->pdu = NULL; + ssl_data->pdu_len = 0; + ssl_data->pdu = NULL; } return ret; } #endif /* COAP_SERVER_SUPPORT */ -unsigned int coap_dtls_get_overhead(coap_session_t *c_session COAP_UNUSED) { +unsigned int +coap_dtls_get_overhead(coap_session_t *c_session COAP_UNUSED) { return 37; } @@ -2697,11 +2621,10 @@ coap_sock_write(gnutls_transport_ptr_t context, const void *in, size_t inl) { * alert can be read (which effectively is what happens with DTLS). */ ret = inl; - } - else { - coap_log_debug( "* %s: failed to send %zd bytes (%s) state %d\n", - coap_session_str(c_session), inl, coap_socket_strerror(), - c_session->state); + } else { + coap_log_debug("* %s: failed to send %zd bytes (%s) state %d\n", + coap_session_str(c_session), inl, coap_socket_strerror(), + c_session->state); } } if (ret == 0) { @@ -2716,7 +2639,7 @@ void * coap_tls_new_client_session(coap_session_t *c_session) { coap_gnutls_env_t *g_env = gnutls_malloc(sizeof(coap_gnutls_env_t)); coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_session->context->dtls_context); + ((coap_gnutls_context_t *)c_session->context->dtls_context); #if (GNUTLS_VERSION_NUMBER >= 0x030606) int flags = GNUTLS_CLIENT | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK; #else /* < 3.6.6 */ @@ -2762,7 +2685,7 @@ void * coap_tls_new_server_session(coap_session_t *c_session) { coap_gnutls_env_t *g_env = gnutls_malloc(sizeof(coap_gnutls_env_t)); coap_gnutls_context_t *g_context = - ((coap_gnutls_context_t *)c_session->context->dtls_context); + ((coap_gnutls_context_t *)c_session->context->dtls_context); #if (GNUTLS_VERSION_NUMBER >= 0x030606) int flags = GNUTLS_SERVER | GNUTLS_NONBLOCK | GNUTLS_ENABLE_RAWPK; #else /* < 3.6.6 */ @@ -2786,7 +2709,7 @@ coap_tls_new_server_session(coap_session_t *c_session) { gnutls_priority_set(g_env->g_session, g_context->priority_cache); gnutls_handshake_set_timeout(g_env->g_session, - GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT); + GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT); c_session->tls = g_env; ret = do_gnutls_handshake(c_session, g_env); @@ -2841,10 +2764,9 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, c_session->dtls_event = COAP_EVENT_DTLS_CLOSED; break; default: - coap_log_warn( - "coap_tls_write: gnutls_record_send " - "returned %d: '%s'\n", - ret, gnutls_strerror(ret)); + coap_log_warn("coap_tls_write: gnutls_record_send " + "returned %d: '%s'\n", + ret, gnutls_strerror(ret)); ret = -1; break; } @@ -2852,8 +2774,7 @@ coap_tls_write(coap_session_t *c_session, const uint8_t *data, coap_log_info("coap_tls_write: cannot send PDU\n"); } } - } - else { + } else { ret = do_gnutls_handshake(c_session, g_env); if (ret == 1) { coap_handle_event(c_session->context, COAP_EVENT_DTLS_CONNECTED, @@ -2937,10 +2858,9 @@ coap_tls_read(coap_session_t *c_session, uint8_t *data, size_t data_len) { c_session->dtls_event = COAP_EVENT_DTLS_ERROR; break; default: - coap_log_warn( - "coap_tls_read: gnutls_record_recv " - "returned %d: '%s'\n", - ret, gnutls_strerror(ret)); + coap_log_warn("coap_tls_read: gnutls_record_recv " + "returned %d: '%s'\n", + ret, gnutls_strerror(ret)); ret = -1; break; } @@ -2993,7 +2913,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) { - gnutls_hash_output(digest_ctx, (uint8_t*)digest_buffer); + gnutls_hash_output(digest_ctx, (uint8_t *)digest_buffer); coap_digest_free(digest_ctx); return 1; @@ -3010,9 +2930,9 @@ static struct hash_algs { gnutls_digest_algorithm_t dig_type; size_t dig_size; } hashs[] = { - {COSE_ALGORITHM_SHA_1, GNUTLS_DIG_SHA1, 20}, - {COSE_ALGORITHM_SHA_256_256, GNUTLS_DIG_SHA256, 32}, - {COSE_ALGORITHM_SHA_512, GNUTLS_DIG_SHA512, 64}, + {COSE_ALGORITHM_SHA_1, GNUTLS_DIG_SHA1, 20}, + {COSE_ALGORITHM_SHA_256_256, GNUTLS_DIG_SHA256, 32}, + {COSE_ALGORITHM_SHA_512, GNUTLS_DIG_SHA512, 64}, }; static gnutls_digest_algorithm_t @@ -3082,7 +3002,8 @@ static struct cipher_algs { cose_alg_t alg; gnutls_cipher_algorithm_t cipher_type; } ciphers[] = {{COSE_ALGORITHM_AES_CCM_16_64_128, GNUTLS_CIPHER_AES_128_CCM_8}, - {COSE_ALGORITHM_AES_CCM_16_64_256, GNUTLS_CIPHER_AES_256_CCM_8}}; + {COSE_ALGORITHM_AES_CCM_16_64_256, GNUTLS_CIPHER_AES_256_CCM_8} +}; static gnutls_cipher_algorithm_t get_cipher_alg(cose_alg_t alg) { @@ -3105,8 +3026,8 @@ static struct hmac_algs { cose_hmac_alg_t hmac_alg; gnutls_mac_algorithm_t hmac_type; } hmacs[] = { - {COSE_HMAC_ALG_HMAC256_256, GNUTLS_MAC_SHA256}, - {COSE_HMAC_ALG_HMAC512_512, GNUTLS_MAC_SHA512}, + {COSE_HMAC_ALG_HMAC256_256, GNUTLS_MAC_SHA256}, + {COSE_HMAC_ALG_HMAC512_512, GNUTLS_MAC_SHA512}, }; static gnutls_mac_algorithm_t @@ -3159,9 +3080,8 @@ coap_crypto_aead_encrypt(const coap_crypto_param_t *params, return 0; } if ((algo = 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; } tag_size = gnutls_cipher_get_tag_size(algo); @@ -3224,9 +3144,8 @@ coap_crypto_aead_decrypt(const coap_crypto_param_t *params, return 0; } if ((algo = 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; } tag_size = gnutls_cipher_get_tag_size(algo); @@ -3312,7 +3231,8 @@ coap_crypto_hmac(cose_hmac_alg_t hmac_alg, */ #pragma GCC diagnostic ignored "-Wunused-function" #endif -static inline void dummy(void) { +static inline void +dummy(void) { } #endif /* !COAP_WITH_LIBGNUTLS */