From a71b7930b9898ca3e95963cec65bf445712442a2 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Tue, 11 Jul 2023 10:05:21 +0000 Subject: [PATCH] pre-commit: Include re-styled examples files to checks --- .pre-commit-config.yaml | 2 +- examples/coap-client.c | 502 +++++++++++---------- examples/coap-rd.c | 200 ++++----- examples/coap-server.c | 740 +++++++++++++++---------------- examples/etsi_iot_01.c | 62 +-- examples/getopt.c | 75 ++-- examples/oscore-interop-server.c | 165 +++---- examples/tiny.c | 46 +- 8 files changed, 885 insertions(+), 907 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8a4af084..3b962bdc 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/riot|examples/lwip|examples/contiki|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_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/examples/coap-client.c b/examples/coap-client.c index 5d26fd56..f22cfb83 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -27,10 +27,10 @@ #if !defined(S_ISDIR) #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) #endif -char * strndup(const char* s1, size_t n); +char *strndup(const char *s1, size_t n); char * -strndup(const char* s1, size_t n) { - char* copy = (char*)malloc(n + 1); +strndup(const char *s1, size_t n) { + char *copy = (char *)malloc(n + 1); if (copy) { memcpy(copy, s1, n); copy[n] = 0; @@ -118,7 +118,7 @@ static size_t ca_mem_len = 0; static int verify_peer_cert = 1; /* PKI granularity - by default set */ typedef struct ih_def_t { - char* hint_match; + char *hint_match; coap_bin_const_t *new_identity; coap_bin_const_t *new_key; } ih_def_t; @@ -180,7 +180,7 @@ append_to_output(const uint8_t *data, size_t len) { written = fwrite(data, 1, len, file); len -= written; data += written; - } while ( written && len ); + } while (written && len); fflush(file); return 0; @@ -208,7 +208,7 @@ free_xmit_data(coap_session_t *session COAP_UNUSED, void *app_ptr) { static void track_new_token(size_t tokenlen, uint8_t *token) { track_token *new_list = realloc(tracked_tokens, - (tracked_tokens_count + 1) * sizeof(tracked_tokens[0])); + (tracked_tokens_count + 1) * sizeof(tracked_tokens[0])); if (!new_list) { coap_log_info("Unable to track new token\n"); return; @@ -244,9 +244,9 @@ track_flush_token(coap_bin_const_t *token, int force) { /* Only remove if not Observing */ coap_delete_binary(tracked_tokens[i].token); if (tracked_tokens_count-i > 1) { - memmove (&tracked_tokens[i], - &tracked_tokens[i+1], - (tracked_tokens_count-i-1) * sizeof (tracked_tokens[0])); + memmove(&tracked_tokens[i], + &tracked_tokens[i+1], + (tracked_tokens_count-i-1) * sizeof(tracked_tokens[0])); } tracked_tokens_count--; } @@ -282,8 +282,7 @@ coap_new_request(coap_context_t *ctx, coap_session_new_token(session, &tokenlen, token); /* Update the last part 8 bytes of the large token */ memcpy(&the_token.s[the_token.length - tokenlen], token, tokenlen); - } - else { + } else { coap_session_new_token(session, &the_token.length, the_token.s); } track_new_token(the_token.length, the_token.s); @@ -307,7 +306,7 @@ static int event_handler(coap_session_t *session COAP_UNUSED, const coap_event_t event) { - switch(event) { + switch (event) { case COAP_EVENT_DTLS_CLOSED: case COAP_EVENT_TCP_CLOSED: case COAP_EVENT_SESSION_CLOSED: @@ -352,7 +351,7 @@ nack_handler(coap_session_t *session COAP_UNUSED, coap_log_err("nack_handler: Unexpected token\n"); } - switch(reason) { + switch (reason) { case COAP_NACK_TOO_MANY_RETRIES: case COAP_NACK_NOT_DELIVERABLE: case COAP_NACK_RST: @@ -391,7 +390,7 @@ message_handler(coap_session_t *session COAP_UNUSED, coap_bin_const_t token = coap_pdu_get_token(received); coap_log_debug("** process incoming %d.%02d response:\n", - COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F); + COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F); if (coap_get_log_level() < COAP_LOG_DEBUG) coap_show_pdu(COAP_LOG_INFO, received); @@ -417,9 +416,8 @@ message_handler(coap_session_t *session COAP_UNUSED, /* set obs timer if we have successfully subscribed a resource */ if (doing_observe && !obs_started && coap_check_option(received, COAP_OPTION_OBSERVE, &opt_iter)) { - coap_log_debug( - "observation relationship established, set timeout to %d\n", - obs_seconds); + coap_log_debug("observation relationship established, set timeout to %d\n", + obs_seconds); obs_started = 1; obs_ms = obs_seconds * 1000; obs_ms_reset = 1; @@ -428,7 +426,7 @@ message_handler(coap_session_t *session COAP_UNUSED, if (coap_get_data_large(received, &len, &databuf, &offset, &total)) { append_to_output(databuf, len); if ((len + offset == total) && add_nl) - append_to_output((const uint8_t*)"\n", 1); + append_to_output((const uint8_t *)"\n", 1); } /* Check if Block2 option is set */ @@ -439,9 +437,9 @@ message_handler(coap_session_t *session COAP_UNUSED, if (coap_opt_block_num(block_opt) == 0) { /* See if observe is set in first response */ ready = doing_observe ? coap_check_option(received, - COAP_OPTION_OBSERVE, &opt_iter) == NULL : 1; + COAP_OPTION_OBSERVE, &opt_iter) == NULL : 1; } - if(COAP_OPT_BLOCK_MORE(block_opt)) { + if (COAP_OPT_BLOCK_MORE(block_opt)) { doing_getting_block = 1; } else { doing_getting_block = 0; @@ -457,7 +455,7 @@ message_handler(coap_session_t *session COAP_UNUSED, rcv_code & 0x1F); if (coap_get_data_large(received, &len, &databuf, &offset, &total)) { fprintf(stderr, " "); - while(len--) { + while (len--) { fprintf(stderr, "%c", isprint(*databuf) ? *databuf : '.'); databuf++; } @@ -472,7 +470,7 @@ message_handler(coap_session_t *session COAP_UNUSED, /* our job is done, we can exit at any time */ ready = doing_observe ? coap_check_option(received, - COAP_OPTION_OBSERVE, &opt_iter) == NULL : 1; + COAP_OPTION_OBSERVE, &opt_iter) == NULL : 1; return COAP_RESPONSE_OK; } @@ -482,153 +480,153 @@ usage(const char *program, const char *version) { char buffer[120]; const char *lib_build = coap_package_build(); - p = strrchr( program, '/' ); - if ( p ) + p = strrchr(program, '/'); + if (p) program = ++p; - fprintf( stderr, "%s v%s -- a small CoAP implementation\n" - "Copyright (C) 2010-2023 Olaf Bergmann and others\n\n" - "Build: %s\n" - "%s\n" - , program, version, lib_build, - coap_string_tls_version(buffer, sizeof(buffer))); + fprintf(stderr, "%s v%s -- a small CoAP implementation\n" + "Copyright (C) 2010-2023 Olaf Bergmann and others\n\n" + "Build: %s\n" + "%s\n" + , program, version, lib_build, + coap_string_tls_version(buffer, sizeof(buffer))); fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer))); fprintf(stderr, "\n" - "Usage: %s [-a addr] [-b [num,]size] [-e text] [-f file] [-l loss]\n" - "\t\t[-m method] [-o file] [-p port] [-r] [-s duration] [-t type]\n" - "\t\t[-v num] [-w] [-A type] [-B seconds]\n" - "\t\t[-E oscore_conf_file[,seq_file]] [-G count] [-H hoplimit]\n" - "\t\t[-K interval] [-N] [-O num,text] [-P scheme://address[:port]\n" - "\t\t[-T token] [-U] [-V num] [-X size]\n" - "\t\t[[-h match_hint_file] [-k key] [-u user]]\n" - "\t\t[[-c certfile] [-j keyfile] [-n] [-C cafile]\n" - "\t\t[-J pkcs11_pin] [-M raw_pk] [-R trust_casfile]\n" - "\t\t[-S match_pki_sni_file]] URI\n" - "\tURI can be an absolute URI or a URI prefixed with scheme and host\n\n" - "General Options\n" - "\t-a addr\t\tThe local interface address to use\n" - "\t-b [num,]size\tBlock size to be used in GET/PUT/POST requests\n" - "\t \t\t(value must be a multiple of 16 not larger than 1024)\n" - "\t \t\tIf num is present, the request chain will start at\n" - "\t \t\tblock num\n" - "\t-e text\t\tInclude text as payload (use percent-encoding for\n" - "\t \t\tnon-ASCII characters)\n" - "\t-f file\t\tFile to send with PUT/POST (use '-' for STDIN)\n" - "\t-l list\t\tFail to send some datagrams specified by a comma\n" - "\t \t\tseparated list of numbers or number ranges\n" - "\t \t\t(for debugging only)\n" - "\t-l loss%%\tRandomly fail to send datagrams with the specified\n" - "\t \t\tprobability - 100%% all datagrams, 0%% no datagrams\n" - "\t-m method\tRequest method (get|put|post|delete|fetch|patch|ipatch),\n" - "\t \t\tdefault is 'get'\n" - "\t-o file\t\tOutput received data to this file (use '-' for STDOUT)\n" - "\t-p port\t\tSend from the specified port\n" - "\t-r \t\tUse reliable protocol (TCP or TLS); requires TCP support\n" - "\t-s duration\tSubscribe to / Observe resource for given duration\n" - "\t \t\tin seconds\n" - "\t-t type\t\tContent format for given resource for PUT/POST\n" - "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" - "\t \t\tCoAP logging\n" - "\t-w \t\tAppend a newline to received data\n" - "\t-A type\t\tAccepted media type\n" - "\t-B seconds\tBreak operation after waiting given seconds\n" - "\t \t\t(default is %d)\n" - "\t-E oscore_conf_file[,seq_file]\n" - "\t \t\toscore_conf_file contains OSCORE configuration. See\n" - "\t \t\tcoap-oscore-conf(5) for definitions.\n" - "\t \t\tOptional seq_file is used to save the current transmit\n" - "\t \t\tsequence number, so on restart sequence numbers continue\n" - "\t-G count\tRepeat the Request 'count' times with a second delay\n" - "\t \t\tbetween each one. Must have a value between 1 and 255\n" - "\t \t\tinclusive. Default is '1'\n" - "\t-H hoplimit\tSet the Hop Limit count to hoplimit for proxies. Must\n" - "\t \t\thave a value between 1 and 255 inclusive.\n" - "\t \t\tDefault is '16'\n" - "\t-K interval\tSend a ping after interval seconds of inactivity\n" - "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" - "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" - "\t \t\t(Sum of one or more of 1,2 and 16)\n" - "\t-N \t\tSend NON-confirmable message\n" - "\t-O num,text\tAdd option num with contents text to request. If the\n" - "\t \t\ttext begins with 0x, then the hex text (two [0-9a-f] per\n" - "\t \t\tbyte) is converted to binary data\n" - "\t-P scheme://address[:port]\n" - "\t \t\tScheme, address and optional port to define how to\n" - "\t \t\tconnect to a CoAP proxy (automatically adds Proxy-Uri\n" - "\t \t\toption to request) to forward the request to.\n" - "\t \t\tScheme is one of coap, coaps, coap+tcp and coaps+tcp\n" - "\t-T token\tDefine the initial starting token (up to 24 characters)\n" - "\t-U \t\tNever include Uri-Host or Uri-Port options\n" - "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" - "\t \t\tlibrary logging\n" - "\t-X size\t\tMaximum message size to use for TCP based connections\n" - "\t \t\t(default is 8388864). Maximum value of 2^32 -1\n" - ,program, wait_seconds); - fprintf( stderr, - "PSK Options (if supported by underlying (D)TLS library)\n" - "\t-h match_hint_file\n" - "\t \t\tThis is a file that contains one or more lines of\n" - "\t \t\treceived Identity Hints to match to use different\n" - "\t \t\tuser identity and associated pre-shared key (PSK) (comma\n" - "\t \t\tseparated) instead of the '-k key' and '-u user'\n" - "\t \t\toptions. E.g., per line\n" - "\t \t\t hint_to_match,use_user,with_key\n" - "\t \t\tNote: -k and -u still need to be defined for the default\n" - "\t \t\tin case there is no match\n" - "\t-k key \t\tPre-shared key for the specified user identity\n" - "\t-u user\t\tUser identity to send for pre-shared key mode\n" - "PKI Options (if supported by underlying (D)TLS library)\n" - "\tNote: If any one of '-c certfile', '-j keyfile' or '-C cafile' is in\n" - "\tPKCS11 URI naming format (pkcs11: prefix), then any remaining non\n" - "\tPKCS11 URI file definitions have to be in DER, not PEM, format.\n" - "\tOtherwise all of '-c certfile', '-j keyfile' or '-C cafile' are in\n" - "\tPEM format.\n\n" - "\t-c certfile\tPEM file or PKCS11 URI for the certificate. The private\n" - "\t \t\tkey can also be in the PEM file, or has the same PKCS11\n" - "\t \t\tURI. If not, the private key is defined by '-j keyfile'\n" - "\t-j keyfile\tPEM file or PKCS11 URI for the private key for the\n" - "\t \t\tcertificate in '-c certfile' if the parameter is\n" - "\t \t\tdifferent from certfile in '-c certfile'\n" - "\t-n \t\tDisable remote peer certificate checking\n" - "\t-C cafile\tPEM file or PKCS11 URI for the CA certificate that was\n" - "\t \t\tused to sign the server certfile. Ideally the client\n" - "\t \t\tcertificate should be signed by the same CA so that\n" - "\t \t\tmutual authentication can take place. The contents of\n" - "\t \t\tcafile are added to the trusted store of root CAs.\n" - "\t \t\tUsing the -C or -R options will trigger the\n" - "\t \t\tvalidation of the server certificate unless overridden\n" - "\t \t\tby the -n option\n" - "\t-J pkcs11_pin\tThe user pin to unlock access to the PKCS11 token\n" - "\t-M rpk_file\tRaw Public Key (RPK) PEM file or PKCS11 URI that\n" - "\t \t\tcontains both PUBLIC KEY and PRIVATE KEY or just\n" - "\t \t\tEC PRIVATE KEY. (GnuTLS and TinyDTLS(PEM) support only).\n" - "\t \t\t'-C cafile' or '-R trust_casfile' are not required\n" - "\t-R trust_casfile\n" - "\t \t\tPEM file containing the set of trusted root CAs\n" - "\t \t\tthat are to be used to validate the server certificate.\n" - "\t \t\tAlternatively, this can point to a directory containing\n" - "\t \t\ta set of CA PEM files.\n" - "\t \t\tUsing '-R trust_casfile' disables common CA mutual\n" - "\t \t\tauthentication which can only be done by using\n" - "\t \t\t'-C cafile'.\n" - "\t \t\tUsing the -C or -R options will will trigger the\n" - "\t \t\tvalidation of the server certificate unless overridden\n" - "\t \t\tby the -n option\n" - ); - fprintf( stderr, - "Examples:\n" - "\tcoap-client -m get coap://[::1]/\n" - "\tcoap-client -m get coap://[::1]/.well-known/core\n" - "\tcoap-client -m get coap+tcp://[::1]/.well-known/core\n" - "\tcoap-client -m get coap://%%2Funix%%2Fdomain%%2Fpath%%2Fdgram/.well-known/core\n" - "\tcoap-client -m get coap+tcp://%%2Funix%%2Fdomain%%2Fpath%%2Fstream/.well-known/core\n" - "\tcoap-client -m get coaps://[::1]/.well-known/core\n" - "\tcoap-client -m get coaps+tcp://[::1]/.well-known/core\n" - "\tcoap-client -m get coaps://%%2Funix%%2Fdomain%%2Fpath%%2Fdtls/.well-known/core\n" - "\tcoap-client -m get coaps+tcp://%%2Funix%%2Fdomain%%2Fpath%%2Ftls/.well-known/core\n" - "\tcoap-client -m get -T cafe coap://[::1]/time\n" - "\techo -n 1000 | coap-client -m put -T cafe coap://[::1]/time -f -\n" - ); + "Usage: %s [-a addr] [-b [num,]size] [-e text] [-f file] [-l loss]\n" + "\t\t[-m method] [-o file] [-p port] [-r] [-s duration] [-t type]\n" + "\t\t[-v num] [-w] [-A type] [-B seconds]\n" + "\t\t[-E oscore_conf_file[,seq_file]] [-G count] [-H hoplimit]\n" + "\t\t[-K interval] [-N] [-O num,text] [-P scheme://address[:port]\n" + "\t\t[-T token] [-U] [-V num] [-X size]\n" + "\t\t[[-h match_hint_file] [-k key] [-u user]]\n" + "\t\t[[-c certfile] [-j keyfile] [-n] [-C cafile]\n" + "\t\t[-J pkcs11_pin] [-M raw_pk] [-R trust_casfile]\n" + "\t\t[-S match_pki_sni_file]] URI\n" + "\tURI can be an absolute URI or a URI prefixed with scheme and host\n\n" + "General Options\n" + "\t-a addr\t\tThe local interface address to use\n" + "\t-b [num,]size\tBlock size to be used in GET/PUT/POST requests\n" + "\t \t\t(value must be a multiple of 16 not larger than 1024)\n" + "\t \t\tIf num is present, the request chain will start at\n" + "\t \t\tblock num\n" + "\t-e text\t\tInclude text as payload (use percent-encoding for\n" + "\t \t\tnon-ASCII characters)\n" + "\t-f file\t\tFile to send with PUT/POST (use '-' for STDIN)\n" + "\t-l list\t\tFail to send some datagrams specified by a comma\n" + "\t \t\tseparated list of numbers or number ranges\n" + "\t \t\t(for debugging only)\n" + "\t-l loss%%\tRandomly fail to send datagrams with the specified\n" + "\t \t\tprobability - 100%% all datagrams, 0%% no datagrams\n" + "\t-m method\tRequest method (get|put|post|delete|fetch|patch|ipatch),\n" + "\t \t\tdefault is 'get'\n" + "\t-o file\t\tOutput received data to this file (use '-' for STDOUT)\n" + "\t-p port\t\tSend from the specified port\n" + "\t-r \t\tUse reliable protocol (TCP or TLS); requires TCP support\n" + "\t-s duration\tSubscribe to / Observe resource for given duration\n" + "\t \t\tin seconds\n" + "\t-t type\t\tContent format for given resource for PUT/POST\n" + "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" + "\t \t\tCoAP logging\n" + "\t-w \t\tAppend a newline to received data\n" + "\t-A type\t\tAccepted media type\n" + "\t-B seconds\tBreak operation after waiting given seconds\n" + "\t \t\t(default is %d)\n" + "\t-E oscore_conf_file[,seq_file]\n" + "\t \t\toscore_conf_file contains OSCORE configuration. See\n" + "\t \t\tcoap-oscore-conf(5) for definitions.\n" + "\t \t\tOptional seq_file is used to save the current transmit\n" + "\t \t\tsequence number, so on restart sequence numbers continue\n" + "\t-G count\tRepeat the Request 'count' times with a second delay\n" + "\t \t\tbetween each one. Must have a value between 1 and 255\n" + "\t \t\tinclusive. Default is '1'\n" + "\t-H hoplimit\tSet the Hop Limit count to hoplimit for proxies. Must\n" + "\t \t\thave a value between 1 and 255 inclusive.\n" + "\t \t\tDefault is '16'\n" + "\t-K interval\tSend a ping after interval seconds of inactivity\n" + "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" + "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" + "\t \t\t(Sum of one or more of 1,2 and 16)\n" + "\t-N \t\tSend NON-confirmable message\n" + "\t-O num,text\tAdd option num with contents text to request. If the\n" + "\t \t\ttext begins with 0x, then the hex text (two [0-9a-f] per\n" + "\t \t\tbyte) is converted to binary data\n" + "\t-P scheme://address[:port]\n" + "\t \t\tScheme, address and optional port to define how to\n" + "\t \t\tconnect to a CoAP proxy (automatically adds Proxy-Uri\n" + "\t \t\toption to request) to forward the request to.\n" + "\t \t\tScheme is one of coap, coaps, coap+tcp and coaps+tcp\n" + "\t-T token\tDefine the initial starting token (up to 24 characters)\n" + "\t-U \t\tNever include Uri-Host or Uri-Port options\n" + "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" + "\t \t\tlibrary logging\n" + "\t-X size\t\tMaximum message size to use for TCP based connections\n" + "\t \t\t(default is 8388864). Maximum value of 2^32 -1\n" + ,program, wait_seconds); + fprintf(stderr, + "PSK Options (if supported by underlying (D)TLS library)\n" + "\t-h match_hint_file\n" + "\t \t\tThis is a file that contains one or more lines of\n" + "\t \t\treceived Identity Hints to match to use different\n" + "\t \t\tuser identity and associated pre-shared key (PSK) (comma\n" + "\t \t\tseparated) instead of the '-k key' and '-u user'\n" + "\t \t\toptions. E.g., per line\n" + "\t \t\t hint_to_match,use_user,with_key\n" + "\t \t\tNote: -k and -u still need to be defined for the default\n" + "\t \t\tin case there is no match\n" + "\t-k key \t\tPre-shared key for the specified user identity\n" + "\t-u user\t\tUser identity to send for pre-shared key mode\n" + "PKI Options (if supported by underlying (D)TLS library)\n" + "\tNote: If any one of '-c certfile', '-j keyfile' or '-C cafile' is in\n" + "\tPKCS11 URI naming format (pkcs11: prefix), then any remaining non\n" + "\tPKCS11 URI file definitions have to be in DER, not PEM, format.\n" + "\tOtherwise all of '-c certfile', '-j keyfile' or '-C cafile' are in\n" + "\tPEM format.\n\n" + "\t-c certfile\tPEM file or PKCS11 URI for the certificate. The private\n" + "\t \t\tkey can also be in the PEM file, or has the same PKCS11\n" + "\t \t\tURI. If not, the private key is defined by '-j keyfile'\n" + "\t-j keyfile\tPEM file or PKCS11 URI for the private key for the\n" + "\t \t\tcertificate in '-c certfile' if the parameter is\n" + "\t \t\tdifferent from certfile in '-c certfile'\n" + "\t-n \t\tDisable remote peer certificate checking\n" + "\t-C cafile\tPEM file or PKCS11 URI for the CA certificate that was\n" + "\t \t\tused to sign the server certfile. Ideally the client\n" + "\t \t\tcertificate should be signed by the same CA so that\n" + "\t \t\tmutual authentication can take place. The contents of\n" + "\t \t\tcafile are added to the trusted store of root CAs.\n" + "\t \t\tUsing the -C or -R options will trigger the\n" + "\t \t\tvalidation of the server certificate unless overridden\n" + "\t \t\tby the -n option\n" + "\t-J pkcs11_pin\tThe user pin to unlock access to the PKCS11 token\n" + "\t-M rpk_file\tRaw Public Key (RPK) PEM file or PKCS11 URI that\n" + "\t \t\tcontains both PUBLIC KEY and PRIVATE KEY or just\n" + "\t \t\tEC PRIVATE KEY. (GnuTLS and TinyDTLS(PEM) support only).\n" + "\t \t\t'-C cafile' or '-R trust_casfile' are not required\n" + "\t-R trust_casfile\n" + "\t \t\tPEM file containing the set of trusted root CAs\n" + "\t \t\tthat are to be used to validate the server certificate.\n" + "\t \t\tAlternatively, this can point to a directory containing\n" + "\t \t\ta set of CA PEM files.\n" + "\t \t\tUsing '-R trust_casfile' disables common CA mutual\n" + "\t \t\tauthentication which can only be done by using\n" + "\t \t\t'-C cafile'.\n" + "\t \t\tUsing the -C or -R options will will trigger the\n" + "\t \t\tvalidation of the server certificate unless overridden\n" + "\t \t\tby the -n option\n" + ); + fprintf(stderr, + "Examples:\n" + "\tcoap-client -m get coap://[::1]/\n" + "\tcoap-client -m get coap://[::1]/.well-known/core\n" + "\tcoap-client -m get coap+tcp://[::1]/.well-known/core\n" + "\tcoap-client -m get coap://%%2Funix%%2Fdomain%%2Fpath%%2Fdgram/.well-known/core\n" + "\tcoap-client -m get coap+tcp://%%2Funix%%2Fdomain%%2Fpath%%2Fstream/.well-known/core\n" + "\tcoap-client -m get coaps://[::1]/.well-known/core\n" + "\tcoap-client -m get coaps+tcp://[::1]/.well-known/core\n" + "\tcoap-client -m get coaps://%%2Funix%%2Fdomain%%2Fpath%%2Fdtls/.well-known/core\n" + "\tcoap-client -m get coaps+tcp://%%2Funix%%2Fdomain%%2Fpath%%2Ftls/.well-known/core\n" + "\tcoap-client -m get -T cafe coap://[::1]/time\n" + "\techo -n 1000 | coap-client -m put -T cafe coap://[::1]/time -f -\n" + ); } typedef struct { @@ -667,7 +665,7 @@ cmdline_content_type(char *arg, uint16_t key) { } else { for (i=0; content_types[i].media_type && - strncmp(arg, content_types[i].media_type, strlen(arg)) != 0 ; + strncmp(arg, content_types[i].media_type, strlen(arg)) != 0 ; ++i) ; @@ -703,7 +701,7 @@ cmdline_hop_limit(char *arg) { } static uint8_t * -read_file_mem(const char* filename, size_t *length) { +read_file_mem(const char *filename, size_t *length) { FILE *f; uint8_t *buf; struct stat statbuf; @@ -735,8 +733,8 @@ read_file_mem(const char* filename, size_t *length) { } static FILE *oscore_seq_num_fp = NULL; -static const char* oscore_conf_file = NULL; -static const char* oscore_seq_save_file = NULL; +static const char *oscore_conf_file = NULL; +static const char *oscore_seq_save_file = NULL; static int oscore_save_seq_num(uint64_t sender_seq_num, void *param COAP_UNUSED) { @@ -825,15 +823,14 @@ cmdline_uri(char *arg) { /* create Proxy-Uri from argument */ size_t len = strlen(arg); if (len > 1034) { - coap_log_err( - "Absolute URI length must be <= 1034 bytes for a proxy\n"); + coap_log_err("Absolute URI length must be <= 1034 bytes for a proxy\n"); return -1; } coap_insert_optlist(&optlist, - coap_new_optlist(COAP_OPTION_PROXY_URI, - len, - (unsigned char *)arg)); + coap_new_optlist(COAP_OPTION_PROXY_URI, + len, + (unsigned char *)arg)); } else { /* split arg into Uri-* options */ if (coap_split_uri((unsigned char *)arg, strlen(arg), &uri) < 0) { @@ -844,8 +841,7 @@ cmdline_uri(char *arg) { /* Need to special case use of reliable */ if (uri.scheme == COAP_URI_SCHEME_COAPS && reliable) { if (!coap_tls_is_supported()) { - coap_log_emerg( - "coaps+tcp URI scheme not supported in this version of libcoap\n"); + coap_log_emerg("coaps+tcp URI scheme not supported in this version of libcoap\n"); return -1; } else { uri.scheme = COAP_URI_SCHEME_COAPS_TCP; @@ -854,8 +850,7 @@ cmdline_uri(char *arg) { if (uri.scheme == COAP_URI_SCHEME_COAP && reliable) { if (!coap_tcp_is_supported()) { - coap_log_emerg( - "coap+tcp URI scheme not supported in this version of libcoap\n"); + coap_log_emerg("coap+tcp URI scheme not supported in this version of libcoap\n"); return -1; } else { uri.scheme = COAP_URI_SCHEME_COAP_TCP; @@ -869,9 +864,9 @@ static int cmdline_blocksize(char *arg) { uint16_t size; - again: +again: size = 0; - while(*arg && *arg != ',') + while (*arg && *arg != ',') size = size * 10 + (*arg++ - '0'); if (*arg == ',') { @@ -890,7 +885,7 @@ cmdline_blocksize(char *arg) { } else if (size > 1024) { coap_log_warn("Maximum block size is 1024\n"); return 0; - } else if ((size % 16) != 0 ){ + } else if ((size % 16) != 0) { coap_log_warn("Block size %u is not a multiple of 16\n", size); return 0; } @@ -923,10 +918,10 @@ set_blocksize(void) { } block.m = (opt == COAP_OPTION_BLOCK1 || opt == COAP_OPTION_Q_BLOCK1) && - ((1ull << (block.szx + 4)) < payload.length); + ((1ull << (block.szx + 4)) < payload.length); opt_length = coap_encode_var_safe(buf, sizeof(buf), - (block.num << 4 | block.m << 3 | block.szx)); + (block.num << 4 | block.m << 3 | block.szx)); coap_insert_optlist(&optlist, coap_new_optlist(opt, opt_length, buf)); } @@ -939,9 +934,9 @@ cmdline_subscribe(char *arg) { obs_seconds = atoi(arg); coap_insert_optlist(&optlist, coap_new_optlist(COAP_OPTION_OBSERVE, - coap_encode_var_safe(buf, sizeof(buf), - COAP_OBSERVE_ESTABLISH), buf) - ); + coap_encode_var_safe(buf, sizeof(buf), + COAP_OBSERVE_ESTABLISH), buf) + ); doing_observe = 1; } @@ -1017,7 +1012,7 @@ cmdline_option(char *arg) { if (*arg == ',') ++arg; - /* read hex string when arg starts with "0x" */ + /* read hex string when arg starts with "0x" */ if (arg[0] == '0' && arg[1] == 'x') { /* As the command line option is part of our environment we can do * the conversion in place. */ @@ -1035,16 +1030,13 @@ cmdline_option(char *arg) { if (strcasecmp(arg, "coaps+tcp") == 0) { proxy.scheme = COAP_URI_SCHEME_COAPS_TCP; proxy.port = COAPS_DEFAULT_PORT; - } - else if (strcasecmp(arg, "coap+tcp") == 0) { + } else if (strcasecmp(arg, "coap+tcp") == 0) { proxy.scheme = COAP_URI_SCHEME_COAP_TCP; proxy.port = COAP_DEFAULT_PORT; - } - else if (strcasecmp(arg, "coaps") == 0) { + } else if (strcasecmp(arg, "coaps") == 0) { proxy.scheme = COAP_URI_SCHEME_COAPS; proxy.port = COAPS_DEFAULT_PORT; - } - else if (strcasecmp(arg, "coap") == 0) { + } else if (strcasecmp(arg, "coap") == 0) { proxy.scheme = COAP_URI_SCHEME_COAP; proxy.port = COAP_DEFAULT_PORT; } else { @@ -1085,12 +1077,14 @@ decode_segment(const uint8_t *seg, size_t length, unsigned char *buf) { if (*seg == '%') { *buf = (hexchar_to_dec(seg[1]) << 4) + hexchar_to_dec(seg[2]); - seg += 2; length -= 2; + seg += 2; + length -= 2; } else { *buf = *seg; } - ++buf; ++seg; + ++buf; + ++seg; } } @@ -1113,7 +1107,9 @@ check_segment(const uint8_t *s, size_t length) { length -= 2; } - ++s; ++n; --length; + ++s; + ++n; + --length; } return n; @@ -1156,7 +1152,7 @@ cmdline_input_from_file(char *filename, coap_string_t *buf) { } else { /* read from specified input file */ inputfile = fopen(filename, "r"); - if ( !inputfile ) { + if (!inputfile) { perror("cmdline_input_from_file: fopen"); return 0; } @@ -1198,7 +1194,7 @@ cmdline_input_from_file(char *filename, coap_string_t *buf) { static method_t cmdline_method(char *arg) { static const char *methods[] = - { 0, "get", "post", "put", "delete", "fetch", "patch", "ipatch", 0}; + { 0, "get", "post", "put", "delete", "fetch", "patch", "ipatch", 0}; unsigned char i; for (i=1; methods[i] && strcasecmp(arg,methods[i]) != 0 ; ++i) @@ -1227,11 +1223,12 @@ cmdline_read_key(char *arg, unsigned char **buf, size_t maxlen) { return len; } /* Need at least one byte for the pre-shared key */ - coap_log_crit("Invalid Pre-Shared Key specified\n" ); + coap_log_crit("Invalid Pre-Shared Key specified\n"); return -1; } -static int cmdline_read_hint_check(const char *arg) { +static int +cmdline_read_hint_check(const char *arg) { FILE *fp = fopen(arg, "r"); static char tmpbuf[256]; if (fp == NULL) { @@ -1251,7 +1248,7 @@ static int cmdline_read_hint_check(const char *arg) { if (tcp) { ih_def_t *new_ih_list; new_ih_list = realloc(valid_ihs.ih_list, - (valid_ihs.count + 1)*sizeof (valid_ihs.ih_list[0])); + (valid_ihs.count + 1)*sizeof(valid_ihs.ih_list[0])); if (new_ih_list == NULL) { break; } @@ -1261,10 +1258,10 @@ static int cmdline_read_hint_check(const char *arg) { tcp = strchr(cp, ','); if (tcp) { valid_ihs.ih_list[valid_ihs.count].new_identity = - coap_new_bin_const((const uint8_t *)cp, tcp-cp); + coap_new_bin_const((const uint8_t *)cp, tcp-cp); cp = tcp+1; valid_ihs.ih_list[valid_ihs.count].new_key = - coap_new_bin_const((const uint8_t *)cp, strlen(cp)); + coap_new_bin_const((const uint8_t *)cp, strlen(cp)); valid_ihs.count++; } else { /* Badly formatted */ @@ -1285,7 +1282,7 @@ verify_cn_callback(const char *cn, int validated COAP_UNUSED, void *arg COAP_UNUSED) { coap_log_info("CN '%s' presented by server (%s)\n", - cn, depth ? "CA" : "Certificate"); + cn, depth ? "CA" : "Certificate"); return 1; } @@ -1313,7 +1310,7 @@ verify_ih_callback(coap_str_const_t *hint, psk_identity_info.identity = *valid_ihs.ih_list[i].new_identity; } coap_log_info("Switching to using '%s' identity + '%s' key\n", - psk_identity_info.identity.s, psk_identity_info.key.s); + psk_identity_info.identity.s, psk_identity_info.key.s); return &psk_identity_info; } } @@ -1337,7 +1334,7 @@ setup_pki(coap_context_t *ctx) { } memset(client_sni, 0, sizeof(client_sni)); - memset (&dtls_pki, 0, sizeof(dtls_pki)); + memset(&dtls_pki, 0, sizeof(dtls_pki)); dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION; if (ca_file || root_ca_file) { /* @@ -1358,8 +1355,7 @@ setup_pki(coap_context_t *ctx) { dtls_pki.check_cert_revocation = 1; dtls_pki.allow_no_crl = 1; dtls_pki.allow_expired_crl = 1; - } - else if (is_rpk_not_cert) { + } else if (is_rpk_not_cert) { dtls_pki.verify_peer_cert = verify_peer_cert; } dtls_pki.is_rpk_not_cert = is_rpk_not_cert; @@ -1371,17 +1367,16 @@ setup_pki(coap_context_t *ctx) { memcpy(client_sni, "localhost", 9); dtls_pki.client_sni = client_sni; - if ((key_file && strncasecmp (key_file, "pkcs11:", 7) == 0) || - (cert_file && strncasecmp (cert_file, "pkcs11:", 7) == 0) || - (ca_file && strncasecmp (ca_file, "pkcs11:", 7) == 0)) { + if ((key_file && strncasecmp(key_file, "pkcs11:", 7) == 0) || + (cert_file && strncasecmp(cert_file, "pkcs11:", 7) == 0) || + (ca_file && strncasecmp(ca_file, "pkcs11:", 7) == 0)) { dtls_pki.pki_key.key_type = COAP_PKI_KEY_PKCS11; dtls_pki.pki_key.key.pkcs11.public_cert = cert_file; dtls_pki.pki_key.key.pkcs11.private_key = key_file ? - key_file : cert_file; + key_file : cert_file; dtls_pki.pki_key.key.pkcs11.ca = ca_file; dtls_pki.pki_key.key.pkcs11.user_pin = pkcs11_pin; - } - else if (!is_rpk_not_cert) { + } else if (!is_rpk_not_cert) { dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM; dtls_pki.pki_key.key.pem.public_cert = cert_file; dtls_pki.pki_key.key.pem.private_key = key_file ? key_file : cert_file; @@ -1400,7 +1395,7 @@ setup_pki(coap_context_t *ctx) { dtls_pki.pki_key.key.pem_buf.ca_cert_len = ca_mem_len; dtls_pki.pki_key.key.pem_buf.public_cert_len = cert_mem_len; dtls_pki.pki_key.key.pem_buf.private_key_len = key_mem ? - key_mem_len : cert_mem_len; + key_mem_len : cert_mem_len; } return &dtls_pki; } @@ -1414,7 +1409,7 @@ setup_psk(const uint8_t *identity, static char client_sni[256]; memset(client_sni, 0, sizeof(client_sni)); - memset (&dtls_psk, 0, sizeof(dtls_psk)); + memset(&dtls_psk, 0, sizeof(dtls_psk)); dtls_psk.version = COAP_DTLS_CPSK_SETUP_VERSION; dtls_psk.validate_ih_call_back = verify_ih_callback; dtls_psk.ih_call_back_arg = &dtls_psk.psk_info; @@ -1431,7 +1426,7 @@ setup_psk(const uint8_t *identity, return &dtls_psk; } -static coap_session_t* +static coap_session_t * open_session(coap_context_t *ctx, coap_proto_t proto, coap_address_t *bind_addr, @@ -1458,14 +1453,14 @@ open_session(coap_context_t *ctx, } else if (identity || key) { /* Setup PSK session */ coap_dtls_cpsk_t *dtls_psk = setup_psk(identity, identity_len, - key, key_len); + key, key_len); if (doing_oscore) { session = coap_new_client_session_oscore_psk(ctx, bind_addr, dst, proto, dtls_psk, oscore_conf); } else session = coap_new_client_session_psk2(ctx, bind_addr, dst, proto, - dtls_psk); + dtls_psk); } else { /* No PKI or PSK defined, as encrypted, use PKI */ coap_dtls_pki_t *dtls_pki = setup_pki(ctx); @@ -1512,7 +1507,7 @@ get_session(coap_context_t *ctx, if (local_addr) { if (!coap_address_set_unix_domain(&bind_addr, (const uint8_t *)local_addr, - strlen(local_addr))) { + strlen(local_addr))) { fprintf(stderr, "coap_address_set_unix_domain: %s: failed\n", local_addr); return NULL; @@ -1540,11 +1535,11 @@ get_session(coap_context_t *ctx, coap_str_const_t local; uint16_t port = local_port ? atoi(local_port) : 0; - local.s = (const uint8_t*)local_addr; + local.s = (const uint8_t *)local_addr; local.length = strlen(local_addr); /* resolve local address where data should be sent from */ info_list = coap_resolve_address_info(&local, port, port, - AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL, + AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL, 1 << scheme, COAP_RESOLVE_TYPE_LOCAL); if (!info_list) { @@ -1553,7 +1548,7 @@ get_session(coap_context_t *ctx, } /* iterate through results until success */ - for (info = info_list; info != NULL; info = info->next ) { + for (info = info_list; info != NULL; info = info->next) { session = open_session(ctx, proto, &info->addr, dst, identity, identity_len, key, key_len); if (session) @@ -1569,10 +1564,10 @@ get_session(coap_context_t *ctx, bind_addr.addr.sa.sa_family = dst->addr.sa.sa_family; coap_address_set_port(&bind_addr, atoi(local_port)); session = open_session(ctx, proto, &bind_addr, dst, - identity, identity_len, key, key_len); + identity, identity_len, key, key_len); } else { session = open_session(ctx, proto, NULL, dst, - identity, identity_len, key, key_len); + identity, identity_len, key, key_len); } return session; } @@ -1608,7 +1603,8 @@ main(int argc, char **argv) { struct sigaction sa; #endif - while ((opt = getopt(argc, argv, "a:b:c:e:f:h:j:k:l:m:no:p:rs:t:u:v:wA:B:C:E:G:H:J:K:L:M:NO:P:R:T:UV:X:")) != -1) { + while ((opt = getopt(argc, argv, + "a:b:c:e:f:h:j:k:l:m:no:p:rs:t:u:v:wA:B:C:E:G:H:J:K:L:M:NO:P:R:T:UV:X:")) != -1) { switch (opt) { case 'a': strncpy(node_str, optarg, NI_MAXHOST - 1); @@ -1757,7 +1753,7 @@ main(int argc, char **argv) { } break; default: - usage( argv[0], LIBCOAP_PACKAGE_VERSION ); + usage(argv[0], LIBCOAP_PACKAGE_VERSION); goto failed; } } @@ -1765,15 +1761,15 @@ main(int argc, char **argv) { #ifdef _WIN32 signal(SIGINT, handle_sigint); #else - memset (&sa, 0, sizeof(sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handle_sigint; sa.sa_flags = 0; - sigaction (SIGINT, &sa, NULL); - sigaction (SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); /* So we do not exit on a SIGPIPE */ sa.sa_handler = SIG_IGN; - sigaction (SIGPIPE, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); #endif coap_startup(); @@ -1785,12 +1781,12 @@ main(int argc, char **argv) { goto failed; } } else { - usage( argv[0], LIBCOAP_PACKAGE_VERSION ); + usage(argv[0], LIBCOAP_PACKAGE_VERSION); goto failed; } if (key_length < 0) { - coap_log_crit("Invalid pre-shared key specified\n" ); + coap_log_crit("Invalid pre-shared key specified\n"); goto failed; } @@ -1818,9 +1814,9 @@ main(int argc, char **argv) { memcpy(&dst, &info_list->addr, sizeof(dst)); coap_free_address_info(info_list); - ctx = coap_new_context( NULL ); - if ( !ctx ) { - coap_log_emerg("cannot create context\n" ); + ctx = coap_new_context(NULL); + if (!ctx) { + coap_log_emerg("cannot create context\n"); goto failed; } @@ -1849,10 +1845,10 @@ main(int argc, char **argv) { user_length >= 0 ? user_length : 0, key_length > 0 ? key : NULL, key_length > 0 ? key_length : 0 - ); + ); - if ( !session ) { - coap_log_err("cannot create client session\n" ); + if (!session) { + coap_log_err("cannot create client session\n"); goto failed; } /* @@ -1865,11 +1861,11 @@ main(int argc, char **argv) { /* Convert provided uri into CoAP options */ if (coap_uri_into_options(&uri, !uri_host_option && !proxy.host.length ? - &dst : NULL, + &dst : NULL, &optlist, create_uri_opts, buf, sizeof(buf)) < 0) { coap_log_err("Failed to create options for URI\n"); - goto failed; + goto failed; } /* set block option if requested at commandline */ @@ -1886,7 +1882,7 @@ main(int argc, char **argv) { data_len = payload.length; } if (!(pdu = coap_new_request(ctx, session, method, &optlist, data, - data_len))) { + data_len))) { goto failed; } @@ -1907,11 +1903,11 @@ main(int argc, char **argv) { } repeat_count--; - while(!quit && /* immediate quit not required .. and .. */ - (tracked_tokens_count || /* token not responded to or still observe */ - is_mcast || /* mcast active */ - repeat_count || /* more repeat transmissions to go */ - coap_io_pending(ctx))) { /* i/o not yet complete */ + while (!quit && /* immediate quit not required .. and .. */ + (tracked_tokens_count || /* token not responded to or still observe */ + is_mcast || /* mcast active */ + repeat_count || /* more repeat transmissions to go */ + coap_io_pending(ctx))) { /* i/o not yet complete */ uint32_t timeout_ms; /* * 3 factors determine how long to wait in coap_io_process() @@ -1930,18 +1926,18 @@ main(int argc, char **argv) { result = coap_io_process(ctx, timeout_ms); - if ( result >= 0 ) { + if (result >= 0) { if (wait_ms > 0) { - if ( (unsigned)result >= wait_ms ) { + if ((unsigned)result >= wait_ms) { coap_log_info("timeout\n"); break; } else { wait_ms -= result; } } - if ( obs_ms > 0 && !obs_ms_reset ) { - if ( (unsigned)result >= obs_ms ) { - coap_log_debug("clear observation relationship\n" ); + if (obs_ms > 0 && !obs_ms_reset) { + if ((unsigned)result >= obs_ms) { + coap_log_debug("clear observation relationship\n"); for (i = 0; i < tracked_tokens_count; i++) { if (tracked_tokens[i].observe) { coap_cancel_observe(session, tracked_tokens[i].token, msgtype); @@ -1995,7 +1991,7 @@ main(int argc, char **argv) { exit_code = 0; - finish: +finish: /* Clean up library usage */ coap_session_release(session); diff --git a/examples/coap-rd.c b/examples/coap-rd.c index ad98a58c..fae2c976 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -149,7 +149,7 @@ hnd_get_resource(coap_resource_t *resource, COAP_OPTION_CONTENT_TYPE, coap_encode_var_safe(buf, sizeof(buf), COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), - buf); + buf); if (rd && rd->etag_len) coap_add_option(response, COAP_OPTION_ETAG, rd->etag_len, rd->etag); @@ -172,7 +172,7 @@ hnd_put_resource(coap_resource_t *resource COAP_UNUSED, coap_pdu_t *response; size_t size = sizeof(coap_hdr_t); int type = (request->hdr->type == COAP_MESSAGE_CON) - ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON; + ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON; rd_t *rd = NULL; unsigned char code; /* result code */ const uint8_t *data; @@ -189,8 +189,7 @@ hnd_put_resource(coap_resource_t *resource COAP_UNUSED, tmp.s = (unsigned char *)coap_malloc(tmp.length); if (!tmp.s) { - coap_log_debug( - "hnd_put_rd: cannot allocate storage for new rd\n"); + coap_log_debug("hnd_put_rd: cannot allocate storage for new rd\n"); code = COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE; goto finish; } @@ -210,18 +209,18 @@ hnd_put_resource(coap_resource_t *resource COAP_UNUSED, code = COAP_RESPONSE_CODE_CHANGED; /* FIXME: update lifetime */ - } else { + } else { code = COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE; } - finish: +finish: /* FIXME: do not create a new response but use the old one instead */ response = coap_pdu_init(type, code, request->hdr->id, size); if (!response) { coap_log_debug("cannot create response for mid=0x%x\n", - request->hdr->id); + request->hdr->id); return; } @@ -230,7 +229,7 @@ hnd_put_resource(coap_resource_t *resource COAP_UNUSED, if (coap_send(ctx, peer, response) == COAP_INVALID_MID) { coap_log_debug("hnd_get_rd: cannot send response for mid=0x%x\n", - request->hdr->id); + request->hdr->id); } #endif } @@ -267,7 +266,7 @@ hnd_get_rd(coap_resource_t *resource COAP_UNUSED, COAP_OPTION_CONTENT_TYPE, coap_encode_var_safe(buf, sizeof(buf), COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), - buf); + buf); coap_add_option(response, COAP_OPTION_MAXAGE, @@ -297,7 +296,8 @@ parse_param(const uint8_t *search, /* key is only valid if we are at end of string or delimiter follows */ if (!data_len || *data == '=' || *data == '&') { while (data_len && *data != '=') { - ++data; --data_len; + ++data; + --data_len; } if (data_len > 1 && result) { @@ -305,7 +305,8 @@ parse_param(const uint8_t *search, result->s = ++data; while (--data_len && *data != '&') { - ++data; result->length++; + ++data; + result->length++; } } @@ -420,21 +421,21 @@ hnd_post_rd(coap_resource_t *resource COAP_UNUSED, if (ins.length && loc_size > 1) { loc[loc_size++] = '-'; memcpy((char *)(loc + loc_size), - ins.s, min(ins.length, LOCSIZE - loc_size - 1)); + ins.s, min(ins.length, LOCSIZE - loc_size - 1)); loc_size += min(ins.length, LOCSIZE - loc_size - 1); } } else { /* generate node identifier */ loc_size += - snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, - "%x", coap_pdu_get_mid(request)); + snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, + "%x", coap_pdu_get_mid(request)); if (loc_size > 1) { if (ins.length) { loc[loc_size++] = '-'; memcpy((char *)(loc + loc_size), - ins.s, - min(ins.length, LOCSIZE - loc_size - 1)); + ins.s, + min(ins.length, LOCSIZE - loc_size - 1)); loc_size += min(ins.length, LOCSIZE - loc_size - 1); } else { coap_tick_t now; @@ -512,7 +513,8 @@ hnd_post_rd(coap_resource_t *resource COAP_UNUSED, coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED); - { /* split path into segments and add Location-Path options */ + { + /* split path into segments and add Location-Path options */ unsigned char _b[LOCSIZE]; unsigned char *b = _b; size_t buflen = sizeof(_b); @@ -553,86 +555,85 @@ usage(const char *program, const char *version) { char buffer[120]; const char *lib_build = coap_package_build(); - p = strrchr( program, '/' ); - if ( p ) + p = strrchr(program, '/'); + if (p) program = ++p; - fprintf( stderr, "%s v%s -- CoRE Resource Directory implementation\n" - "(c) 2011-2012,2019-2023 Olaf Bergmann and others\n\n" - "Build: %s\n" - "%s\n" - , program, version, lib_build, - coap_string_tls_version(buffer, sizeof(buffer))); + fprintf(stderr, "%s v%s -- CoRE Resource Directory implementation\n" + "(c) 2011-2012,2019-2023 Olaf Bergmann and others\n\n" + "Build: %s\n" + "%s\n" + , program, version, lib_build, + coap_string_tls_version(buffer, sizeof(buffer))); fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer))); fprintf(stderr, "\n" - "Usage: %s [-g group] [-G group_if] [-p port] [-v num] [-A address]\n" - "\t [-w [port][,secure_port]] [-T max_token_size] [-V num]\n" - "\t [[-h hint] [-k key]]\n" - "\t [[-c certfile] [-C cafile] [-n] [-R trust_casfile]]\n" - "General Options\n" - "\t-g group\tJoin the given multicast group.\n" - "\t \t\tNote: DTLS over multicast is not currently supported\n" - "\t-G group_if\tUse this interface for listening for the multicast\n" - "\t \t\tgroup. This can be different from the implied interface\n" - "\t \t\tif the -A option is used\n" - "\t-p port\t\tListen on specified port\n" - "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" - "\t \t\tCoAP logging\n" - "\t-w [port][,secure_port]\n" - "\t \t\tEnable WebSockets support on port (WS) and/or secure_port\n" - "\t \t\t(WSS), comma separated\n" - "\t-A address\tInterface address to bind to\n" - "\t-T max_token_length\tSet the maximum token length (8-65804)\n" - "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" - "\t \t\tlibrary logging\n" - "PSK Options (if supported by underlying (D)TLS library)\n" - "\t-h hint\t\tIdentity Hint. Default is CoAP. Zero length is no hint\n" - "\t-k key \t\tPre-Shared Key. This argument requires (D)TLS with PSK\n" - "\t \t\tto be available. This cannot be empty if defined.\n" - "\t \t\tNote that both -c and -k need to be defined\n" - "\t \t\tfor both PSK and PKI to be concurrently supported\n" - "PKI Options (if supported by underlying (D)TLS library)\n" - "\t-c certfile\tPEM file containing both CERTIFICATE and PRIVATE KEY\n" - "\t \t\tThis argument requires (D)TLS with PKI to be available\n" - "\t-n \t\tDisable remote peer certificate checking. This gives\n" - "\t \t\tclients the ability to use PKI, but without any defined\n" - "\t \t\tcertificates\n" - "\t-C cafile\tPEM file that contains a list of one or\n" - "\t \t\tmore CAs that are to be passed to the client for the\n" - "\t \t\tclient to determine what client certificate to use.\n" - "\t \t\tNormally, this list of CAs would be the root CA and and\n" - "\t \t\tany intermediate CAs. Ideally the server certificate\n" - "\t \t\tshould be signed by the same CA so that mutual\n" - "\t \t\tauthentication can take place. The contents of cafile\n" - "\t \t\tare added to the trusted store of root CAs.\n" - "\t \t\tUsing the -C or -R options will will trigger the\n" - "\t \t\tvalidation of the client certificate unless overridden\n" - "\t \t\tby the -n option\n" - "\t-R trust_casfile\tPEM file containing the set of trusted root CAs\n" - "\t \t\tthat are to be used to validate the client certificate.\n" - "\t \t\tAlternatively, this can point to a directory containing\n" - "\t \t\ta set of CA PEM files.\n" - "\t \t\tUsing '-R trust_casfile' disables common CA mutual\n" - "\t \t\tauthentication which can only be done by using\n" - "\t \t\t'-C cafile'.\n" - "\t \t\tUsing the -C or -R options will will trigger the\n" - "\t \t\tvalidation of the client certificate unless overridden\n" - "\t \t\tby the -n option\n" - , - program); + "Usage: %s [-g group] [-G group_if] [-p port] [-v num] [-A address]\n" + "\t [-w [port][,secure_port]] [-T max_token_size] [-V num]\n" + "\t [[-h hint] [-k key]]\n" + "\t [[-c certfile] [-C cafile] [-n] [-R trust_casfile]]\n" + "General Options\n" + "\t-g group\tJoin the given multicast group.\n" + "\t \t\tNote: DTLS over multicast is not currently supported\n" + "\t-G group_if\tUse this interface for listening for the multicast\n" + "\t \t\tgroup. This can be different from the implied interface\n" + "\t \t\tif the -A option is used\n" + "\t-p port\t\tListen on specified port\n" + "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" + "\t \t\tCoAP logging\n" + "\t-w [port][,secure_port]\n" + "\t \t\tEnable WebSockets support on port (WS) and/or secure_port\n" + "\t \t\t(WSS), comma separated\n" + "\t-A address\tInterface address to bind to\n" + "\t-T max_token_length\tSet the maximum token length (8-65804)\n" + "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" + "\t \t\tlibrary logging\n" + "PSK Options (if supported by underlying (D)TLS library)\n" + "\t-h hint\t\tIdentity Hint. Default is CoAP. Zero length is no hint\n" + "\t-k key \t\tPre-Shared Key. This argument requires (D)TLS with PSK\n" + "\t \t\tto be available. This cannot be empty if defined.\n" + "\t \t\tNote that both -c and -k need to be defined\n" + "\t \t\tfor both PSK and PKI to be concurrently supported\n" + "PKI Options (if supported by underlying (D)TLS library)\n" + "\t-c certfile\tPEM file containing both CERTIFICATE and PRIVATE KEY\n" + "\t \t\tThis argument requires (D)TLS with PKI to be available\n" + "\t-n \t\tDisable remote peer certificate checking. This gives\n" + "\t \t\tclients the ability to use PKI, but without any defined\n" + "\t \t\tcertificates\n" + "\t-C cafile\tPEM file that contains a list of one or\n" + "\t \t\tmore CAs that are to be passed to the client for the\n" + "\t \t\tclient to determine what client certificate to use.\n" + "\t \t\tNormally, this list of CAs would be the root CA and and\n" + "\t \t\tany intermediate CAs. Ideally the server certificate\n" + "\t \t\tshould be signed by the same CA so that mutual\n" + "\t \t\tauthentication can take place. The contents of cafile\n" + "\t \t\tare added to the trusted store of root CAs.\n" + "\t \t\tUsing the -C or -R options will will trigger the\n" + "\t \t\tvalidation of the client certificate unless overridden\n" + "\t \t\tby the -n option\n" + "\t-R trust_casfile\tPEM file containing the set of trusted root CAs\n" + "\t \t\tthat are to be used to validate the client certificate.\n" + "\t \t\tAlternatively, this can point to a directory containing\n" + "\t \t\ta set of CA PEM files.\n" + "\t \t\tUsing '-R trust_casfile' disables common CA mutual\n" + "\t \t\tauthentication which can only be done by using\n" + "\t \t\t'-C cafile'.\n" + "\t \t\tUsing the -C or -R options will will trigger the\n" + "\t \t\tvalidation of the client certificate unless overridden\n" + "\t \t\tby the -n option\n" + , + program); } static void fill_keystore(coap_context_t *ctx) { if (cert_file == NULL && key_defined == 0) { if (coap_dtls_is_supported() || coap_tls_is_supported()) { - coap_log_debug( - "(D)TLS not enabled as neither -k or -c options specified\n"); + coap_log_debug("(D)TLS not enabled as neither -k or -c options specified\n"); } } if (cert_file) { coap_dtls_pki_t dtls_pki; - memset (&dtls_pki, 0, sizeof(dtls_pki)); + memset(&dtls_pki, 0, sizeof(dtls_pki)); dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION; if (ca_file || root_ca_file) { /* @@ -671,7 +672,7 @@ fill_keystore(coap_context_t *ctx) { } if (key_defined) { coap_dtls_spsk_t dtls_psk; - memset (&dtls_psk, 0, sizeof(dtls_psk)); + memset(&dtls_psk, 0, sizeof(dtls_psk)); dtls_psk.version = COAP_DTLS_SPSK_SETUP_VERSION; dtls_psk.validate_id_call_back = NULL; dtls_psk.validate_sni_call_back = NULL; @@ -712,8 +713,8 @@ get_context(const char *node, const char *port) { s_port = u_s_port + 1; } scheme_hint_bits = - coap_get_available_scheme_hint_bits(cert_file != NULL || key_defined != 0, - enable_ws, COAP_PROTO_NONE); + coap_get_available_scheme_hint_bits(cert_file != NULL || key_defined != 0, + enable_ws, COAP_PROTO_NONE); info_list = coap_resolve_address_info(node ? &local : NULL, u_s_port, s_port, AI_PASSIVE | AI_NUMERICHOST, scheme_hint_bits, @@ -724,7 +725,7 @@ get_context(const char *node, const char *port) { ep = coap_new_endpoint(ctx, &info->addr, info->proto); if (!ep) { coap_log_warn("cannot create endpoint for proto %u\n", - info->proto); + info->proto); } else { have_ep = 1; } @@ -744,8 +745,7 @@ cmdline_read_extended_token_size(char *arg) { if (extended_token_size < COAP_TOKEN_DEFAULT_MAX) { coap_log_err("Extended Token Length must be 8 or greater\n"); return 0; - } - else if (extended_token_size > COAP_TOKEN_EXT_MAX) { + } else if (extended_token_size > COAP_TOKEN_EXT_MAX) { coap_log_err("Extended Token Length must be 65804 or less\n"); return 0; } @@ -795,7 +795,7 @@ main(int argc, char **argv) { case 'k' : key_length = cmdline_read_key(optarg, key, MAX_KEY); if (key_length < 0) { - coap_log_crit("Invalid Pre-Shared Key specified\n" ); + coap_log_crit("Invalid Pre-Shared Key specified\n"); break; } key_defined = 1; @@ -829,8 +829,8 @@ main(int argc, char **argv) { enable_ws = 1; break; default: - usage( argv[0], LIBCOAP_PACKAGE_VERSION ); - exit( 1 ); + usage(argv[0], LIBCOAP_PACKAGE_VERSION); + exit(1); } } @@ -852,25 +852,25 @@ main(int argc, char **argv) { #ifdef _WIN32 signal(SIGINT, handle_sigint); #else - memset (&sa, 0, sizeof(sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handle_sigint; sa.sa_flags = 0; - sigaction (SIGINT, &sa, NULL); - sigaction (SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); /* So we do not exit on a SIGPIPE */ sa.sa_handler = SIG_IGN; - sigaction (SIGPIPE, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); #endif - while ( !quit ) { - result = coap_io_process( ctx, COAP_RESOURCE_CHECK_TIME * 1000 ); - if ( result >= 0 ) { + while (!quit) { + result = coap_io_process(ctx, COAP_RESOURCE_CHECK_TIME * 1000); + if (result >= 0) { /* coap_check_resource_list( ctx ); */ } } - coap_free_context( ctx ); + coap_free_context(ctx); coap_cleanup(); return 0; diff --git a/examples/coap-server.c b/examples/coap-server.c index e8654674..192f227b 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -30,10 +30,10 @@ #ifndef R_OK #define R_OK 4 #endif -char * strndup(const char* s1, size_t n); +char *strndup(const char *s1, size_t n); char * -strndup(const char* s1, size_t n) { - char* copy = (char*)malloc(n + 1); +strndup(const char *s1, size_t n) { + char *copy = (char *)malloc(n + 1); if (copy) { memcpy(copy, s1, n); copy[n] = 0; @@ -132,11 +132,10 @@ static int enable_ws = 0; static int ws_port = 80; static int wss_port = 443; -static coap_dtls_pki_t * -setup_pki(coap_context_t *ctx, coap_dtls_role_t role, char *sni); +static coap_dtls_pki_t *setup_pki(coap_context_t *ctx, coap_dtls_role_t role, char *sni); typedef struct psk_sni_def_t { - char* sni_match; + char *sni_match; coap_bin_const_t *new_key; coap_bin_const_t *new_hint; } psk_sni_def_t; @@ -161,7 +160,7 @@ typedef struct valid_ids_t { static valid_ids_t valid_ids = {0, NULL}; typedef struct pki_sni_def_t { - char* sni_match; + char *sni_match; char *new_cert; char *new_ca; } pki_sni_def_t; @@ -259,7 +258,7 @@ reference_resource_data(transient_value_t *entry) { } #define INDEX "This is a test server made with libcoap (see https://libcoap.net)\n" \ - "Copyright (C) 2010--2023 Olaf Bergmann and others\n\n" + "Copyright (C) 2010--2023 Olaf Bergmann and others\n\n" static void hnd_get_index(coap_resource_t *resource, @@ -304,9 +303,8 @@ hnd_get_fetch_time(coap_resource_t *resource, coap_string_equal(query, ticks)) { /* parameter is in query, output ticks */ len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now); - } - else if (code == COAP_REQUEST_CODE_FETCH && size == ticks->length && - memcmp(data, ticks->s, ticks->length) == 0) { + } else if (code == COAP_REQUEST_CODE_FETCH && size == ticks->length && + memcmp(data, ticks->s, ticks->length) == 0) { /* parameter is in data, output ticks */ len = snprintf((char *)buf, sizeof(buf), "%u", (unsigned int)now); } else { /* output human-readable time */ @@ -348,7 +346,7 @@ hnd_put_time(coap_resource_t *resource, /* if my_clock_base was deleted, we pretend to have no such resource */ coap_pdu_set_code(response, my_clock_base ? COAP_RESPONSE_CODE_CHANGED : - COAP_RESPONSE_CODE_CREATED); + COAP_RESPONSE_CODE_CREATED); coap_resource_notify_observers(resource, NULL); @@ -360,7 +358,7 @@ hnd_put_time(coap_resource_t *resource, } else { my_clock_base = 0; coap_ticks(&t); - while(size--) + while (size--) my_clock_base = my_clock_base * 10 + *data++; my_clock_base -= t / COAP_TICKS_PER_SECOND; @@ -371,8 +369,8 @@ hnd_put_time(coap_resource_t *resource, coap_add_option(response, COAP_OPTION_CONTENT_FORMAT, coap_encode_var_safe(buf, sizeof(buf), - COAP_MEDIATYPE_TEXT_PLAIN), buf); - coap_add_data(response, 22, (const uint8_t*)"Invalid set time value"); + COAP_MEDIATYPE_TEXT_PLAIN), buf); + coap_add_data(response, 22, (const uint8_t *)"Invalid set time value"); /* re-init as value is bad */ my_clock_base = clock_offset; } @@ -425,8 +423,7 @@ hnd_get_async(coap_resource_t *resource, delay = delay * 10 + (*p - '0'); } } else { - coap_log_debug( - "async: query is just a number of seconds to alter delay\n"); + coap_log_debug("async: query is just a number of seconds to alter delay\n"); } if (delay == 0) { coap_log_info("async: delay of 0 not supported\n"); @@ -438,7 +435,7 @@ hnd_get_async(coap_resource_t *resource, request, COAP_TICKS_PER_SECOND * delay); if (async == NULL) { - coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); + coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); return; } /* @@ -467,10 +464,10 @@ hnd_get_async(coap_resource_t *resource, #endif static void hnd_get_example_data(coap_resource_t *resource, - coap_session_t *session, - const coap_pdu_t *request, - const coap_string_t *query, - coap_pdu_t *response) { + coap_session_t *session, + const coap_pdu_t *request, + const coap_string_t *query, + coap_pdu_t *response) { coap_binary_t body; if (!example_data_value) { /* Initialise for the first time */ @@ -498,7 +495,7 @@ hnd_get_example_data(coap_resource_t *resource, static void cache_free_app_data(void *data) { - coap_binary_t *bdata = (coap_binary_t*)data; + coap_binary_t *bdata = (coap_binary_t *)data; coap_delete_binary(bdata); } @@ -508,10 +505,10 @@ cache_free_app_data(void *data) { static void hnd_put_example_data(coap_resource_t *resource, - coap_session_t *session, - const coap_pdu_t *request, - const coap_string_t *query COAP_UNUSED, - coap_pdu_t *response) { + coap_session_t *session, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, + coap_pdu_t *response) { size_t size; const uint8_t *data; coap_opt_iterator_t opt_iter; @@ -521,7 +518,7 @@ hnd_put_example_data(coap_resource_t *resource, coap_binary_t *data_so_far; if (coap_get_data_large(request, &size, &data, &offset, &total) && - size != total) { + size != total) { /* * A part of the data has been received (COAP_BLOCK_SINGLE_BODY not set). * However, total unfortunately is only an indication, so it is not safe to @@ -536,7 +533,7 @@ hnd_put_example_data(coap_resource_t *resource, */ coap_cache_entry_t *cache_entry = coap_cache_get_by_pdu(session, request, - COAP_CACHE_IS_SESSION_BASED); + COAP_CACHE_IS_SESSION_BASED); if (offset == 0) { if (!cache_entry) { @@ -546,8 +543,8 @@ hnd_put_example_data(coap_resource_t *resource, * the session is deleted as session_based is set here. */ cache_entry = coap_new_cache_entry(session, request, - COAP_CACHE_NOT_RECORD_PDU, - COAP_CACHE_IS_SESSION_BASED, 0); + COAP_CACHE_NOT_RECORD_PDU, + COAP_CACHE_IS_SESSION_BASED, 0); } else { data_so_far = coap_cache_get_app_data(cache_entry); if (data_so_far) { @@ -561,8 +558,7 @@ hnd_put_example_data(coap_resource_t *resource, if (offset == 0) { coap_log_warn("Unable to create a new cache entry\n"); } else { - coap_log_warn( - "No cache entry available for the non-first BLOCK\n"); + coap_log_warn("No cache entry available for the non-first BLOCK\n"); } coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR); return; @@ -598,8 +594,7 @@ hnd_put_example_data(coap_resource_t *resource, coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED); /* Need to de-reference as value may be in use elsewhere */ release_resource_data(session, example_data_value); - } - else + } else /* just generated response */ coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED); @@ -611,8 +606,8 @@ hnd_put_example_data(coap_resource_t *resource, if ((option = coap_check_option(request, COAP_OPTION_CONTENT_FORMAT, &opt_iter)) != NULL) { example_data_media_type = - coap_decode_var_bytes (coap_opt_value (option), - coap_opt_length (option)); + coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option)); } else { example_data_media_type = COAP_MEDIATYPE_TEXT_PLAIN; } @@ -661,7 +656,7 @@ get_uri_proxy_scheme_info(const coap_pdu_t *request, coap_string_t **uri_path, coap_string_t **uri_query) { - const char *opt_val = (const char*)coap_opt_value(opt); + const char *opt_val = (const char *)coap_opt_value(opt); int opt_len = coap_opt_length(opt); coap_opt_iterator_t opt_iter; @@ -669,24 +664,21 @@ get_uri_proxy_scheme_info(const coap_pdu_t *request, strncasecmp(opt_val, "coaps+tcp", 9) == 0) { uri->scheme = COAP_URI_SCHEME_COAPS_TCP; uri->port = COAPS_DEFAULT_PORT; - } - else if (opt_len == 8 && - strncasecmp(opt_val, "coap+tcp", 8) == 0) { + } else if (opt_len == 8 && + strncasecmp(opt_val, "coap+tcp", 8) == 0) { uri->scheme = COAP_URI_SCHEME_COAP_TCP; uri->port = COAP_DEFAULT_PORT; - } - else if (opt_len == 5 && - strncasecmp(opt_val, "coaps", 5) == 0) { + } else if (opt_len == 5 && + strncasecmp(opt_val, "coaps", 5) == 0) { uri->scheme = COAP_URI_SCHEME_COAPS; uri->port = COAPS_DEFAULT_PORT; - } - else if (opt_len == 4 && - strncasecmp(opt_val, "coap", 4) == 0) { + } else if (opt_len == 4 && + strncasecmp(opt_val, "coap", 4) == 0) { uri->scheme = COAP_URI_SCHEME_COAP; uri->port = COAP_DEFAULT_PORT; } else { coap_log_warn("Unsupported Proxy Scheme '%*.*s'\n", - opt_len, opt_len, opt_val); + opt_len, opt_len, opt_val); return 0; } @@ -701,8 +693,8 @@ get_uri_proxy_scheme_info(const coap_pdu_t *request, opt = coap_check_option(request, COAP_OPTION_URI_PORT, &opt_iter); if (opt) { uri->port = - coap_decode_var_bytes (coap_opt_value (opt), - coap_opt_length (opt)); + coap_decode_var_bytes(coap_opt_value(opt), + coap_opt_length(opt)); } *uri_path = coap_get_uri_path(request); if (*uri_path) { @@ -724,49 +716,43 @@ verify_proxy_scheme_supported(coap_uri_scheme_t scheme) { switch (scheme) { case COAP_URI_SCHEME_HTTP: case COAP_URI_SCHEME_HTTPS: - coap_log_warn("Proxy URI http or https not supported\n"); + coap_log_warn("Proxy URI http or https not supported\n"); return 0; case COAP_URI_SCHEME_COAP: break; case COAP_URI_SCHEME_COAPS: if (!coap_dtls_is_supported()) { - coap_log_warn( - "coaps URI scheme not supported for proxy\n"); + coap_log_warn("coaps URI scheme not supported for proxy\n"); return 0; } break; case COAP_URI_SCHEME_COAP_TCP: if (!coap_tcp_is_supported()) { - coap_log_warn( - "coap+tcp URI scheme not supported for proxy\n"); + coap_log_warn("coap+tcp URI scheme not supported for proxy\n"); return 0; } break; case COAP_URI_SCHEME_COAPS_TCP: if (!coap_tls_is_supported()) { - coap_log_warn( - "coaps+tcp URI scheme not supported for proxy\n"); + coap_log_warn("coaps+tcp URI scheme not supported for proxy\n"); return 0; } break; case COAP_URI_SCHEME_COAP_WS: if (!coap_ws_is_supported()) { - coap_log_warn( - "coap+ws URI scheme not supported for proxy\n"); + coap_log_warn("coap+ws URI scheme not supported for proxy\n"); return 0; } break; case COAP_URI_SCHEME_COAPS_WS: if (!coap_wss_is_supported()) { - coap_log_warn( - "coaps+ws URI scheme not supported for proxy\n"); + coap_log_warn("coaps+ws URI scheme not supported for proxy\n"); return 0; } break; case COAP_URI_SCHEME_LAST: default: - coap_log_warn( - "%d URI scheme not supported\n", scheme); + coap_log_warn("%d URI scheme not supported\n", scheme); break; } return 1; @@ -776,7 +762,7 @@ static coap_dtls_cpsk_t * setup_cpsk(char *client_sni) { static coap_dtls_cpsk_t dtls_cpsk; - memset (&dtls_cpsk, 0, sizeof(dtls_cpsk)); + memset(&dtls_cpsk, 0, sizeof(dtls_cpsk)); dtls_cpsk.version = COAP_DTLS_CPSK_SETUP_VERSION; dtls_cpsk.client_sni = client_sni; dtls_cpsk.psk_info.identity.s = user; @@ -817,8 +803,7 @@ get_proxy_session(coap_session_t *session, coap_pdu_t *response, return NULL; } memcpy(proxy_list[i].token->s, token->s, token->length); - } - else + } else proxy_list[i].token = NULL; if (query) { @@ -828,8 +813,7 @@ get_proxy_session(coap_session_t *session, coap_pdu_t *response, return NULL; } memcpy(proxy_list[i].query->s, query->s, query->length); - } - else + } else proxy_list[i].query = NULL; proxy_list[i].ongoing = NULL; @@ -858,7 +842,7 @@ remove_proxy_association(coap_session_t *session, int send_failure) { response = coap_pdu_init(proxy_list[i].req_type, COAP_RESPONSE_CODE_BAD_GATEWAY, coap_new_message_id(proxy_list[i].incoming), - coap_session_max_pdu_size(proxy_list[i].incoming)); + coap_session_max_pdu_size(proxy_list[i].incoming)); if (!response) { coap_log_info("PDU creation issue\n"); return; @@ -867,12 +851,11 @@ remove_proxy_association(coap_session_t *session, int send_failure) { if (proxy_list[i].token && !coap_add_token(response, proxy_list[i].token->length, proxy_list[i].token->s)) { - coap_log_debug( - "Cannot add token to incoming proxy response PDU\n"); + coap_log_debug("Cannot add token to incoming proxy response PDU\n"); } if (coap_send(proxy_list[i].incoming, response) == - COAP_INVALID_MID) { + COAP_INVALID_MID) { coap_log_info("Failed to send PDU with 5.02 gateway issue\n"); } break; @@ -882,9 +865,9 @@ remove_proxy_association(coap_session_t *session, int send_failure) { coap_delete_binary(proxy_list[i].token); coap_delete_string(proxy_list[i].query); if (proxy_list_count-i > 1) { - memmove (&proxy_list[i], - &proxy_list[i+1], - (proxy_list_count-i-1) * sizeof (proxy_list[0])); + memmove(&proxy_list[i], + &proxy_list[i+1], + (proxy_list_count-i-1) * sizeof(proxy_list[0])); } proxy_list_count--; } @@ -945,7 +928,7 @@ get_ongoing_proxy_session(coap_session_t *session, case COAP_URI_SCHEME_COAP_TCP: case COAP_URI_SCHEME_COAP_WS: new_proxy_list->ongoing = - coap_new_client_session(context, NULL, &dst, proto); + coap_new_client_session(context, NULL, &dst, proto); break; case COAP_URI_SCHEME_COAPS: case COAP_URI_SCHEME_COAPS_TCP: @@ -962,13 +945,13 @@ get_ongoing_proxy_session(coap_session_t *session, coap_dtls_pki_t *dtls_pki = setup_pki(context, COAP_DTLS_ROLE_CLIENT, client_sni); new_proxy_list->ongoing = - coap_new_client_session_pki(context, NULL, &dst, proto, dtls_pki); + coap_new_client_session_pki(context, NULL, &dst, proto, dtls_pki); } else { /* Use our defined PSK */ coap_dtls_cpsk_t *dtls_cpsk = setup_cpsk(client_sni); new_proxy_list->ongoing = - coap_new_client_session_psk2(context, NULL, &dst, proto, dtls_cpsk); + coap_new_client_session_psk2(context, NULL, &dst, proto, dtls_cpsk); } break; case COAP_URI_SCHEME_HTTP: @@ -994,10 +977,10 @@ release_proxy_body_data(coap_session_t *session COAP_UNUSED, static void hnd_proxy_uri(coap_resource_t *resource COAP_UNUSED, - coap_session_t *session, - const coap_pdu_t *request, - const coap_string_t *query, - coap_pdu_t *response) { + coap_session_t *session, + const coap_pdu_t *request, + const coap_string_t *query, + coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *opt; coap_opt_t *proxy_uri; @@ -1028,7 +1011,7 @@ hnd_proxy_uri(coap_resource_t *resource COAP_UNUSED, if (!get_uri_proxy_scheme_info(request, opt, &uri, &uri_path, &uri_query)) { coap_pdu_set_code(response, - COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); + COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); goto cleanup; } proxy_scheme_option = 1; @@ -1039,15 +1022,15 @@ hnd_proxy_uri(coap_resource_t *resource COAP_UNUSED, proxy_uri = coap_check_option(request, COAP_OPTION_PROXY_URI, &opt_iter); if (proxy_uri) { coap_log_info("Proxy URI '%.*s'\n", - coap_opt_length(proxy_uri), - (const char*)coap_opt_value(proxy_uri)); + coap_opt_length(proxy_uri), + (const char *)coap_opt_value(proxy_uri)); if (coap_split_proxy_uri(coap_opt_value(proxy_uri), coap_opt_length(proxy_uri), &uri) < 0) { /* Need to return a 5.05 RFC7252 Section 5.7.2 */ coap_log_warn("Proxy URI not decodable\n"); coap_pdu_set_code(response, - COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); + COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED); goto cleanup; } } @@ -1156,9 +1139,9 @@ hnd_proxy_uri(coap_resource_t *resource COAP_UNUSED, default: add_in: coap_insert_optlist(&optlist, - coap_new_optlist(opt_iter.number, - coap_opt_length(option), - coap_opt_value(option))); + coap_new_optlist(opt_iter.number, + coap_opt_length(option), + coap_opt_value(option))); break; } } @@ -1235,9 +1218,9 @@ hnd_delete(coap_resource_t *resource, release_resource_data(session, dynamic_entry[i].value); coap_delete_string(dynamic_entry[i].uri_path); if (dynamic_count-i > 1) { - memmove (&dynamic_entry[i], - &dynamic_entry[i+1], - (dynamic_count-i-1) * sizeof (dynamic_entry[0])); + memmove(&dynamic_entry[i], + &dynamic_entry[i+1], + (dynamic_count-i-1) * sizeof(dynamic_entry[0])); } dynamic_count--; break; @@ -1305,10 +1288,10 @@ hnd_get(coap_resource_t *resource, static void hnd_put_post(coap_resource_t *resource, - coap_session_t *session, - const coap_pdu_t *request, - const coap_string_t *query COAP_UNUSED, - coap_pdu_t *response) { + coap_session_t *session, + const coap_pdu_t *request, + const coap_string_t *query COAP_UNUSED, + coap_pdu_t *response) { coap_string_t *uri_path; int i; size_t size; @@ -1345,8 +1328,8 @@ hnd_put_post(coap_resource_t *resource, return; } dynamic_count++; - dynamic_entry = realloc (dynamic_entry, - dynamic_count * sizeof(dynamic_entry[0])); + dynamic_entry = realloc(dynamic_entry, + dynamic_count * sizeof(dynamic_entry[0])); if (dynamic_entry) { dynamic_entry[i].uri_path = uri_path; dynamic_entry[i].value = NULL; @@ -1355,8 +1338,8 @@ hnd_put_post(coap_resource_t *resource, if ((option = coap_check_option(request, COAP_OPTION_CONTENT_FORMAT, &opt_iter)) != NULL) { dynamic_entry[i].media_type = - coap_decode_var_bytes (coap_opt_value (option), - coap_opt_length (option)); + coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option)); } else { dynamic_entry[i].media_type = COAP_MEDIATYPE_TEXT_PLAIN; } @@ -1369,7 +1352,7 @@ hnd_put_post(coap_resource_t *resource, buf[sizeof(buf) - 1] = '\0'; coap_add_attr(resource, coap_make_str_const("ct"), - coap_make_str_const((char*)buf), + coap_make_str_const((char *)buf), 0); } else { dynamic_count--; @@ -1385,7 +1368,7 @@ hnd_put_post(coap_resource_t *resource, resource_entry = &dynamic_entry[i]; if (coap_get_data_large(request, &size, &data, &offset, &total) && - size != total) { + size != total) { /* * A part of the data has been received (COAP_BLOCK_SINGLE_BODY not set). * However, total unfortunately is only an indication, so it is not safe to @@ -1400,13 +1383,13 @@ hnd_put_post(coap_resource_t *resource, */ coap_cache_entry_t *cache_entry = coap_cache_get_by_pdu(session, request, - COAP_CACHE_IS_SESSION_BASED); + COAP_CACHE_IS_SESSION_BASED); if (offset == 0) { if (!cache_entry) { cache_entry = coap_new_cache_entry(session, request, - COAP_CACHE_NOT_RECORD_PDU, - COAP_CACHE_IS_SESSION_BASED, 0); + COAP_CACHE_NOT_RECORD_PDU, + COAP_CACHE_IS_SESSION_BASED, 0); } else { data_so_far = coap_cache_get_app_data(cache_entry); if (data_so_far) { @@ -1420,8 +1403,7 @@ hnd_put_post(coap_resource_t *resource, if (offset == 0) { coap_log_warn("Unable to create a new cache entry\n"); } else { - coap_log_warn( - "No cache entry available for the non-first BLOCK\n"); + coap_log_warn("No cache entry available for the non-first BLOCK\n"); } coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR); return; @@ -1455,7 +1437,7 @@ hnd_put_post(coap_resource_t *resource, data_so_far = coap_cache_get_app_data(cache_entry); coap_cache_set_app_data(cache_entry, NULL, NULL); } else { - coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE); + coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE); return; } } else { @@ -1530,10 +1512,10 @@ fail: static void hnd_put_post_unknown(coap_resource_t *resource COAP_UNUSED, - coap_session_t *session, - const coap_pdu_t *request, - const coap_string_t *query, - coap_pdu_t *response) { + coap_session_t *session, + const coap_pdu_t *request, + const coap_string_t *query, + coap_pdu_t *response) { coap_resource_t *r; coap_string_t *uri_path; @@ -1554,8 +1536,8 @@ hnd_put_post_unknown(coap_resource_t *resource COAP_UNUSED, * Create a resource to handle the new URI * uri_path will get deleted when the resource is removed */ - r = coap_resource_init((coap_str_const_t*)uri_path, - COAP_RESOURCE_FLAGS_RELEASE_URI | resource_flags); + r = coap_resource_init((coap_str_const_t *)uri_path, + COAP_RESOURCE_FLAGS_RELEASE_URI | resource_flags); coap_add_attr(r, coap_make_str_const("title"), coap_make_str_const("\"Dynamic\""), 0); coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_post); coap_register_request_handler(r, COAP_REQUEST_POST, hnd_put_post); @@ -1573,9 +1555,9 @@ hnd_put_post_unknown(coap_resource_t *resource COAP_UNUSED, #if SERVER_CAN_PROXY static int proxy_event_handler(coap_session_t *session, - coap_event_t event) { + coap_event_t event) { - switch(event) { + switch (event) { case COAP_EVENT_DTLS_CLOSED: case COAP_EVENT_TCP_CLOSED: case COAP_EVENT_SESSION_CLOSED: @@ -1613,9 +1595,9 @@ proxy_event_handler(coap_session_t *session, static coap_response_t proxy_response_handler(coap_session_t *session, - const coap_pdu_t *sent COAP_UNUSED, - const coap_pdu_t *received, - const coap_mid_t id COAP_UNUSED) { + const coap_pdu_t *sent COAP_UNUSED, + const coap_pdu_t *received, + const coap_mid_t id COAP_UNUSED) { coap_pdu_t *pdu = NULL; coap_session_t *incoming = NULL; @@ -1648,7 +1630,7 @@ proxy_response_handler(coap_session_t *session, } coap_log_debug("** process upstream incoming %d.%02d response:\n", - COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F); + COAP_RESPONSE_CLASS(rcv_code), rcv_code & 0x1F); if (coap_get_log_level() < COAP_LOG_DEBUG) coap_show_pdu(COAP_LOG_INFO, received); @@ -1688,16 +1670,16 @@ proxy_response_handler(coap_session_t *session, while ((option = coap_option_next(&opt_iter))) { switch (opt_iter.number) { case COAP_OPTION_CONTENT_FORMAT: - media_type = coap_decode_var_bytes(coap_opt_value (option), - coap_opt_length (option)); + media_type = coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option)); break; case COAP_OPTION_MAXAGE: - maxage = coap_decode_var_bytes(coap_opt_value (option), - coap_opt_length (option)); + maxage = coap_decode_var_bytes(coap_opt_value(option), + coap_opt_length(option)); break; case COAP_OPTION_ETAG: - etag = coap_decode_var_bytes8(coap_opt_value (option), - coap_opt_length (option)); + etag = coap_decode_var_bytes8(coap_opt_value(option), + coap_opt_length(option)); break; case COAP_OPTION_BLOCK2: case COAP_OPTION_Q_BLOCK2: @@ -1705,9 +1687,9 @@ proxy_response_handler(coap_session_t *session, break; default: coap_insert_optlist(&optlist, - coap_new_optlist(opt_iter.number, - coap_opt_length(option), - coap_opt_value(option))); + coap_new_optlist(opt_iter.number, + coap_opt_length(option), + coap_opt_value(option))); break; } } @@ -1736,11 +1718,11 @@ proxy_response_handler(coap_session_t *session, static void proxy_nack_handler(coap_session_t *session, - const coap_pdu_t *sent COAP_UNUSED, - const coap_nack_reason_t reason, - const coap_mid_t id COAP_UNUSED) { + const coap_pdu_t *sent COAP_UNUSED, + const coap_nack_reason_t reason, + const coap_mid_t id COAP_UNUSED) { - switch(reason) { + switch (reason) { case COAP_NACK_TOO_MANY_RETRIES: case COAP_NACK_NOT_DELIVERABLE: case COAP_NACK_RST: @@ -1801,8 +1783,8 @@ init_resources(coap_context_t *ctx) { if (coap_async_is_supported()) { r = coap_resource_init(coap_make_str_const("async"), resource_flags | - COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT | - COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_DELAYS); + COAP_RESOURCE_FLAGS_HAS_MCAST_SUPPORT | + COAP_RESOURCE_FLAGS_LIB_DIS_MCAST_DELAYS); coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_async); coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0); @@ -1846,13 +1828,13 @@ verify_cn_callback(const char *cn, } role = { .v = arg }; coap_log_info("CN '%s' presented by %s (%s)\n", - cn, role.r == COAP_DTLS_ROLE_SERVER ? "client" : "server", - depth ? "CA" : "Certificate"); + cn, role.r == COAP_DTLS_ROLE_SERVER ? "client" : "server", + depth ? "CA" : "Certificate"); return 1; } static uint8_t * -read_file_mem(const char* file, size_t *length) { +read_file_mem(const char *file, size_t *length) { FILE *f; uint8_t *buf; struct stat statbuf; @@ -1886,17 +1868,16 @@ read_file_mem(const char* file, size_t *length) { static void update_pki_key(coap_dtls_key_t *dtls_key, const char *key_name, const char *cert_name, const char *ca_name) { - memset (dtls_key, 0, sizeof(*dtls_key)); - if ((key_name && strncasecmp (key_name, "pkcs11:", 7) == 0) || - (cert_name && strncasecmp (cert_name, "pkcs11:", 7) == 0) || - (ca_name && strncasecmp (ca_name, "pkcs11:", 7) == 0)) { + memset(dtls_key, 0, sizeof(*dtls_key)); + if ((key_name && strncasecmp(key_name, "pkcs11:", 7) == 0) || + (cert_name && strncasecmp(cert_name, "pkcs11:", 7) == 0) || + (ca_name && strncasecmp(ca_name, "pkcs11:", 7) == 0)) { dtls_key->key_type = COAP_PKI_KEY_PKCS11; dtls_key->key.pkcs11.public_cert = cert_name; dtls_key->key.pkcs11.private_key = key_name ? key_name : cert_name; dtls_key->key.pkcs11.ca = ca_name; dtls_key->key.pkcs11.user_pin = pkcs11_pin; - } - else if (!use_pem_buf && !is_rpk_not_cert) { + } else if (!use_pem_buf && !is_rpk_not_cert) { dtls_key->key_type = COAP_PKI_KEY_PEM; dtls_key->key.pem.public_cert = cert_name; dtls_key->key.pem.private_key = key_name ? key_name : cert_name; @@ -1917,13 +1898,13 @@ update_pki_key(coap_dtls_key_t *dtls_key, const char *key_name, dtls_key->key.pem_buf.ca_cert_len = ca_mem_len; dtls_key->key.pem_buf.public_cert_len = cert_mem_len; dtls_key->key.pem_buf.private_key_len = key_mem ? - key_mem_len : cert_mem_len; + key_mem_len : cert_mem_len; } } static coap_dtls_key_t * verify_pki_sni_callback(const char *sni, - void *arg COAP_UNUSED) { + void *arg COAP_UNUSED) { static coap_dtls_key_t dtls_key; update_pki_key(&dtls_key, key_file, cert_file, ca_file); @@ -1935,8 +1916,8 @@ verify_pki_sni_callback(const char *sni, /* Test for SNI to change cert + ca */ if (strcasecmp(sni, valid_pki_snis.pki_sni_list[i].sni_match) == 0) { coap_log_info("Switching to using cert '%s' + ca '%s'\n", - valid_pki_snis.pki_sni_list[i].new_cert, - valid_pki_snis.pki_sni_list[i].new_ca); + valid_pki_snis.pki_sni_list[i].new_cert, + valid_pki_snis.pki_sni_list[i].new_ca); update_pki_key(&dtls_key, valid_pki_snis.pki_sni_list[i].new_cert, valid_pki_snis.pki_sni_list[i].new_cert, valid_pki_snis.pki_sni_list[i].new_ca); @@ -1951,12 +1932,12 @@ verify_pki_sni_callback(const char *sni, static const coap_dtls_spsk_info_t * verify_psk_sni_callback(const char *sni, - coap_session_t *c_session COAP_UNUSED, - void *arg COAP_UNUSED) { + coap_session_t *c_session COAP_UNUSED, + void *arg COAP_UNUSED) { static coap_dtls_spsk_info_t psk_info; /* Preset with the defined keys */ - memset (&psk_info, 0, sizeof(psk_info)); + memset(&psk_info, 0, sizeof(psk_info)); psk_info.hint.s = (const uint8_t *)hint; psk_info.hint.length = hint ? strlen(hint) : 0; psk_info.key.s = key; @@ -1967,12 +1948,12 @@ verify_psk_sni_callback(const char *sni, for (i = 0; i < valid_psk_snis.count; i++) { /* Test for identity match to change key */ if (strcasecmp(sni, - valid_psk_snis.psk_sni_list[i].sni_match) == 0) { + valid_psk_snis.psk_sni_list[i].sni_match) == 0) { coap_log_info("Switching to using '%.*s' hint + '%.*s' key\n", - (int)valid_psk_snis.psk_sni_list[i].new_hint->length, - valid_psk_snis.psk_sni_list[i].new_hint->s, - (int)valid_psk_snis.psk_sni_list[i].new_key->length, - valid_psk_snis.psk_sni_list[i].new_key->s); + (int)valid_psk_snis.psk_sni_list[i].new_hint->length, + valid_psk_snis.psk_sni_list[i].new_hint->s, + (int)valid_psk_snis.psk_sni_list[i].new_key->length, + valid_psk_snis.psk_sni_list[i].new_key->s); psk_info.hint = *valid_psk_snis.psk_sni_list[i].new_hint; psk_info.key = *valid_psk_snis.psk_sni_list[i].new_key; break; @@ -1994,9 +1975,9 @@ verify_id_callback(coap_bin_const_t *identity, size_t i; coap_log_info("Identity '%.*s' requested, current hint '%.*s'\n", (int)identity->length, - identity->s, - s_psk_hint ? (int)s_psk_hint->length : 0, - s_psk_hint ? (const char *)s_psk_hint->s : ""); + identity->s, + s_psk_hint ? (int)s_psk_hint->length : 0, + s_psk_hint ? (const char *)s_psk_hint->s : ""); for (i = 0; i < valid_ids.count; i++) { /* Check for hint match */ @@ -2008,8 +1989,8 @@ verify_id_callback(coap_bin_const_t *identity, /* Test for identity match to change key */ if (coap_binary_equal(identity, valid_ids.id_list[i].identity_match)) { coap_log_info("Switching to using '%.*s' key\n", - (int)valid_ids.id_list[i].new_key->length, - valid_ids.id_list[i].new_key->s); + (int)valid_ids.id_list[i].new_key->length, + valid_ids.id_list[i].new_key->s); return valid_ids.id_list[i].new_key; } } @@ -2041,7 +2022,7 @@ setup_pki(coap_context_t *ctx, coap_dtls_role_t role, char *client_sni) { } } - memset (&dtls_pki, 0, sizeof(dtls_pki)); + memset(&dtls_pki, 0, sizeof(dtls_pki)); dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION; if (ca_file || root_ca_file) { /* @@ -2062,13 +2043,12 @@ setup_pki(coap_context_t *ctx, coap_dtls_role_t role, char *client_sni) { dtls_pki.check_cert_revocation = 1; dtls_pki.allow_no_crl = 1; dtls_pki.allow_expired_crl = 1; - } - else if (is_rpk_not_cert) { + } else if (is_rpk_not_cert) { dtls_pki.verify_peer_cert = verify_peer_cert; } dtls_pki.is_rpk_not_cert = is_rpk_not_cert; dtls_pki.validate_cn_call_back = verify_cn_callback; - dtls_pki.cn_call_back_arg = (void*)role; + dtls_pki.cn_call_back_arg = (void *)role; dtls_pki.validate_sni_call_back = role == COAP_DTLS_ROLE_SERVER ? verify_pki_sni_callback : NULL; dtls_pki.sni_call_back_arg = NULL; @@ -2092,7 +2072,7 @@ static coap_dtls_spsk_t * setup_spsk(void) { static coap_dtls_spsk_t dtls_spsk; - memset (&dtls_spsk, 0, sizeof(dtls_spsk)); + memset(&dtls_spsk, 0, sizeof(dtls_spsk)); dtls_spsk.version = COAP_DTLS_SPSK_SETUP_VERSION; dtls_spsk.validate_id_call_back = valid_ids.count ? verify_id_callback : NULL; @@ -2110,8 +2090,7 @@ fill_keystore(coap_context_t *ctx) { if (cert_file == NULL && key_defined == 0) { if (coap_dtls_is_supported() || coap_tls_is_supported()) { - coap_log_debug( - "(D)TLS not enabled as none of -k, -c or -M options specified\n"); + coap_log_debug("(D)TLS not enabled as none of -k, -c or -M options specified\n"); } return; } @@ -2120,7 +2099,7 @@ fill_keystore(coap_context_t *ctx) { COAP_DTLS_ROLE_SERVER, NULL); if (!coap_context_set_pki(ctx, dtls_pki)) { coap_log_info("Unable to set up %s keys\n", - is_rpk_not_cert ? "RPK" : "PKI"); + is_rpk_not_cert ? "RPK" : "PKI"); /* So we do not set up DTLS */ cert_file = NULL; } @@ -2137,183 +2116,183 @@ fill_keystore(coap_context_t *ctx) { } static void -usage( const char *program, const char *version) { +usage(const char *program, const char *version) { const char *p; char buffer[120]; const char *lib_build = coap_package_build(); - p = strrchr( program, '/' ); - if ( p ) + p = strrchr(program, '/'); + if (p) program = ++p; - fprintf( stderr, "%s v%s -- a small CoAP implementation\n" - "(c) 2010,2011,2015-2023 Olaf Bergmann and others\n\n" - "Build: %s\n" - "%s\n" - , program, version, lib_build, - coap_string_tls_version(buffer, sizeof(buffer))); + fprintf(stderr, "%s v%s -- a small CoAP implementation\n" + "(c) 2010,2011,2015-2023 Olaf Bergmann and others\n\n" + "Build: %s\n" + "%s\n" + , program, version, lib_build, + coap_string_tls_version(buffer, sizeof(buffer))); fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer))); fprintf(stderr, "\n" - "Usage: %s [-d max] [-e] [-g group] [-l loss] [-p port] [-r] [-v num]\n" - "\t\t[-w [port][,secure_port]] [-A address]\n" - "\t\t[-E oscore_conf_file[,seq_file]] [-G group_if] [-L value] [-N]\n" - "\t\t[-P scheme://address[:port],[name1[,name2..]]]\n" - "\t\t[-T max_token_size] [-U type] [-V num] [-X size]\n" - "\t\t[[-h hint] [-i match_identity_file] [-k key]\n" - "\t\t[-s match_psk_sni_file] [-u user]]\n" - "\t\t[[-c certfile] [-j keyfile] [-m] [-n] [-C cafile]\n" - "\t\t[-J pkcs11_pin] [-M rpk_file] [-R trust_casfile]\n" - "\t\t[-S match_pki_sni_file]]\n" - "General Options\n" - "\t-d max \t\tAllow dynamic creation of up to a total of max\n" - "\t \t\tresources. If max is reached, a 4.06 code is returned\n" - "\t \t\tuntil one of the dynamic resources has been deleted\n" - "\t-e \t\tEcho back the data sent with a PUT\n" - "\t-g group\tJoin the given multicast group\n" - "\t \t\tNote: DTLS over multicast is not currently supported\n" - "\t-l list\t\tFail to send some datagrams specified by a comma\n" - "\t \t\tseparated list of numbers or number ranges\n" - "\t \t\t(for debugging only)\n" - "\t-l loss%%\tRandomly fail to send datagrams with the specified\n" - "\t \t\tprobability - 100%% all datagrams, 0%% no datagrams\n" - "\t \t\t(for debugging only)\n" - "\t-p port\t\tListen on specified port for UDP and TCP. If (D)TLS is\n" - "\t \t\tenabled, then the coap-server will also listen on\n" - "\t \t\t'port'+1 for DTLS and TLS. The default port is 5683\n" - "\t-r \t\tEnable multicast per resource support. If enabled,\n" - "\t \t\tonly '/', '/async' and '/.well-known/core' are enabled\n" - "\t \t\tfor multicast requests support, otherwise all\n" - "\t \t\tresources are enabled\n" - "\t-t \t\tTrack resource's observe values so observe\n" - "\t \t\tsubscriptions can be maintained over a server restart.\n" - "\t \t\tNote: Use 'kill SIGUSR2 ' for controlled shutdown\n" - "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" - "\t \t\tCoAP logging\n" - "\t-w [port][,secure_port]\n" - "\t \t\tEnable WebSockets support on port (WS) and/or secure_port\n" - "\t \t\t(WSS), comma separated\n" - "\t-A address\tInterface address to bind to\n" - "\t-E oscore_conf_file[,seq_file]\n" - "\t \t\toscore_conf_file contains OSCORE configuration. See\n" - "\t \t\tcoap-oscore-conf(5) for definitions.\n" - "\t \t\tOptional seq_file is used to save the current transmit\n" - "\t \t\tsequence number, so on restart sequence numbers continue\n" - "\t-G group_if\tUse this interface for listening for the multicast\n" - "\t \t\tgroup. This can be different from the implied interface\n" - "\t \t\tif the -A option is used\n" - "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" - "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" - "\t \t\t(Sum of one or more of 1,2 and 4)\n" - "\t-N \t\tMake \"observe\" responses NON-confirmable. Even if set\n" - "\t \t\tevery fifth response will still be sent as a confirmable\n" - "\t \t\tresponse (RFC 7641 requirement)\n" - , program); - fprintf( stderr, - "\t-P scheme://address[:port],[name1[,name2[,name3..]]]\n" - "\t \t\tScheme, address, optional port of how to connect to the\n" - "\t \t\tnext proxy server and zero or more names (comma\n" - "\t \t\tseparated) that this proxy server is known by. The\n" - "\t \t\t, (comma) is required. If there is no name1 or if the\n" - "\t \t\thostname of the incoming proxy request matches one of\n" - "\t \t\tthese names, then this server is considered to be the\n" - "\t \t\tfinal endpoint. If scheme://address[:port] is not\n" - "\t \t\tdefined before the leading , (comma) of the first name,\n" - "\t \t\tthen the ongoing connection will be a direct connection.\n" - "\t \t\tScheme is one of coap, coaps, coap+tcp and coaps+tcp\n" - "\t-T max_token_length\tSet the maximum token length (8-65804)\n" - "\t-U type\t\tTreat address defined by -A as a Unix socket address.\n" - "\t \t\ttype is 'coap', 'coaps', 'coap+tcp' or 'coaps+tcp'\n" - "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" - "\t \t\tlibrary logging\n" - "\t-X size\t\tMaximum message size to use for TCP based connections\n" - "\t \t\t(default is 8388864). Maximum value of 2^32 -1\n" - "PSK Options (if supported by underlying (D)TLS library)\n" - "\t-h hint\t\tIdentity Hint to send. Default is CoAP. Zero length is\n" - "\t \t\tno hint\n" - "\t-i match_identity_file\n" - "\t \t\tThis is a file that contains one or more lines of\n" - "\t \t\tIdentity Hints and (user) Identities to match for\n" - "\t \t\ta different new Pre-Shared Key (PSK) (comma separated)\n" - "\t \t\tto be used. E.g., per line\n" - "\t \t\t hint_to_match,identity_to_match,use_key\n" - "\t \t\tNote: -k still needs to be defined for the default case\n" - "\t \t\tNote: A match using the -s option may mean that the\n" - "\t \t\tcurrent Identity Hint is different to that defined by -h\n" - "\t-k key \t\tPre-Shared Key. This argument requires (D)TLS with PSK\n" - "\t \t\tto be available. This cannot be empty if defined.\n" - "\t \t\tNote that both -c and -k need to be defined for both\n" - "\t \t\tPSK and PKI to be concurrently supported\n" - "\t-s match_psk_sni_file\n" - "\t \t\tThis is a file that contains one or more lines of\n" - "\t \t\treceived Subject Name Identifier (SNI) to match to use\n" - "\t \t\ta different Identity Hint and associated Pre-Shared Key\n" - "\t \t\t(PSK) (comma separated) instead of the '-h hint' and\n" - "\t \t\t'-k key' options. E.g., per line\n" - "\t \t\t sni_to_match,use_hint,with_key\n" - "\t \t\tNote: -k still needs to be defined for the default case\n" - "\t \t\tif there is not a match\n" - "\t \t\tNote: The associated Pre-Shared Key will get updated if\n" - "\t \t\tthere is also a -i match. The update checking order is\n" - "\t \t\t-s followed by -i\n" - "\t-u user\t\tUser identity for pre-shared key mode (only used if\n" - "\t \t\toption -P is set)\n" - ); + "Usage: %s [-d max] [-e] [-g group] [-l loss] [-p port] [-r] [-v num]\n" + "\t\t[-w [port][,secure_port]] [-A address]\n" + "\t\t[-E oscore_conf_file[,seq_file]] [-G group_if] [-L value] [-N]\n" + "\t\t[-P scheme://address[:port],[name1[,name2..]]]\n" + "\t\t[-T max_token_size] [-U type] [-V num] [-X size]\n" + "\t\t[[-h hint] [-i match_identity_file] [-k key]\n" + "\t\t[-s match_psk_sni_file] [-u user]]\n" + "\t\t[[-c certfile] [-j keyfile] [-m] [-n] [-C cafile]\n" + "\t\t[-J pkcs11_pin] [-M rpk_file] [-R trust_casfile]\n" + "\t\t[-S match_pki_sni_file]]\n" + "General Options\n" + "\t-d max \t\tAllow dynamic creation of up to a total of max\n" + "\t \t\tresources. If max is reached, a 4.06 code is returned\n" + "\t \t\tuntil one of the dynamic resources has been deleted\n" + "\t-e \t\tEcho back the data sent with a PUT\n" + "\t-g group\tJoin the given multicast group\n" + "\t \t\tNote: DTLS over multicast is not currently supported\n" + "\t-l list\t\tFail to send some datagrams specified by a comma\n" + "\t \t\tseparated list of numbers or number ranges\n" + "\t \t\t(for debugging only)\n" + "\t-l loss%%\tRandomly fail to send datagrams with the specified\n" + "\t \t\tprobability - 100%% all datagrams, 0%% no datagrams\n" + "\t \t\t(for debugging only)\n" + "\t-p port\t\tListen on specified port for UDP and TCP. If (D)TLS is\n" + "\t \t\tenabled, then the coap-server will also listen on\n" + "\t \t\t'port'+1 for DTLS and TLS. The default port is 5683\n" + "\t-r \t\tEnable multicast per resource support. If enabled,\n" + "\t \t\tonly '/', '/async' and '/.well-known/core' are enabled\n" + "\t \t\tfor multicast requests support, otherwise all\n" + "\t \t\tresources are enabled\n" + "\t-t \t\tTrack resource's observe values so observe\n" + "\t \t\tsubscriptions can be maintained over a server restart.\n" + "\t \t\tNote: Use 'kill SIGUSR2 ' for controlled shutdown\n" + "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" + "\t \t\tCoAP logging\n" + "\t-w [port][,secure_port]\n" + "\t \t\tEnable WebSockets support on port (WS) and/or secure_port\n" + "\t \t\t(WSS), comma separated\n" + "\t-A address\tInterface address to bind to\n" + "\t-E oscore_conf_file[,seq_file]\n" + "\t \t\toscore_conf_file contains OSCORE configuration. See\n" + "\t \t\tcoap-oscore-conf(5) for definitions.\n" + "\t \t\tOptional seq_file is used to save the current transmit\n" + "\t \t\tsequence number, so on restart sequence numbers continue\n" + "\t-G group_if\tUse this interface for listening for the multicast\n" + "\t \t\tgroup. This can be different from the implied interface\n" + "\t \t\tif the -A option is used\n" + "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" + "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" + "\t \t\t(Sum of one or more of 1,2 and 4)\n" + "\t-N \t\tMake \"observe\" responses NON-confirmable. Even if set\n" + "\t \t\tevery fifth response will still be sent as a confirmable\n" + "\t \t\tresponse (RFC 7641 requirement)\n" + , program); fprintf(stderr, - "PKI Options (if supported by underlying (D)TLS library)\n" - "\tNote: If any one of '-c certfile', '-j keyfile' or '-C cafile' is in\n" - "\tPKCS11 URI naming format (pkcs11: prefix), then any remaining non\n" - "\tPKCS11 URI file definitions have to be in DER, not PEM, format.\n" - "\tOtherwise all of '-c certfile', '-j keyfile' or '-C cafile' are in\n" - "\tPEM format.\n\n" - "\t-c certfile\tPEM file or PKCS11 URI for the certificate. The private\n" - "\t \t\tkey can also be in the PEM file, or has the same PKCS11\n" - "\t \t\tURI. If not, the private key is defined by '-j keyfile'.\n" - "\t \t\tNote that both -c and -k need to be defined for both\n" - "\t \t\tPSK and PKI to be concurrently supported\n" - "\t-j keyfile\tPEM file or PKCS11 URI for the private key for the\n" - "\t \t\tcertificate in '-c certfile' if the parameter is\n" - "\t \t\tdifferent from certfile in '-c certfile'\n" - "\t-m \t\tUse COAP_PKI_KEY_PEM_BUF instead of COAP_PKI_KEY_PEM i/f\n" - "\t \t\tby reading into memory the Cert / CA file (for testing)\n" - "\t-n \t\tDisable remote peer certificate checking. This gives\n" - "\t \t\tclients the ability to use PKI, but without any defined\n" - "\t \t\tcertificates\n" - "\t-C cafile\tPEM file or PKCS11 URI that contains a list of one or\n" - "\t \t\tmore CAs that are to be passed to the client for the\n" - "\t \t\tclient to determine what client certificate to use.\n" - "\t \t\tNormally, this list of CAs would be the root CA and and\n" - "\t \t\tany intermediate CAs. Ideally the server certificate\n" - "\t \t\tshould be signed by the same CA so that mutual\n" - "\t \t\tauthentication can take place. The contents of cafile\n" - "\t \t\tare added to the trusted store of root CAs.\n" - "\t \t\tUsing the -C or -R options will will trigger the\n" - "\t \t\tvalidation of the client certificate unless overridden\n" - "\t \t\tby the -n option\n" - "\t-J pkcs11_pin\tThe user pin to unlock access to the PKCS11 token\n" - "\t-M rpk_file\tRaw Public Key (RPK) PEM file or PKCS11 URI that\n" - "\t \t\tcontains both PUBLIC KEY and PRIVATE KEY or just\n" - "\t \t\tEC PRIVATE KEY. (GnuTLS and TinyDTLS(PEM) support only).\n" - "\t \t\t'-C cafile' or '-R trust_casfile' are not required\n" - "\t-R trust_casfile\n" - "\t \t\tPEM file containing the set of trusted root CAs\n" - "\t \t\tthat are to be used to validate the client certificate.\n" - "\t \t\tAlternatively, this can point to a directory containing\n" - "\t \t\ta set of CA PEM files.\n" - "\t \t\tUsing '-R trust_casfile' disables common CA mutual\n" - "\t \t\tauthentication which can only be done by using\n" - "\t \t\t'-C cafile'.\n" - "\t \t\tUsing the -C or -R options will will trigger the\n" - "\t \t\tvalidation of the client certificate unless overridden\n" - "\t \t\tby the -n option\n" - "\t-S match_pki_sni_file\n" - "\t \t\tThis option denotes a file that contains one or more\n" - "\t \t\tlines of Subject Name Identifier (SNI) to match for new\n" - "\t \t\tCert file and new CA file (comma separated) to be used.\n" - "\t \t\tE.g., per line\n" - "\t \t\t sni_to_match,new_cert_file,new_ca_file\n" - "\t \t\tNote: -c and -C still need to be defined for the default\n" - "\t \t\tcase\n" - ); + "\t-P scheme://address[:port],[name1[,name2[,name3..]]]\n" + "\t \t\tScheme, address, optional port of how to connect to the\n" + "\t \t\tnext proxy server and zero or more names (comma\n" + "\t \t\tseparated) that this proxy server is known by. The\n" + "\t \t\t, (comma) is required. If there is no name1 or if the\n" + "\t \t\thostname of the incoming proxy request matches one of\n" + "\t \t\tthese names, then this server is considered to be the\n" + "\t \t\tfinal endpoint. If scheme://address[:port] is not\n" + "\t \t\tdefined before the leading , (comma) of the first name,\n" + "\t \t\tthen the ongoing connection will be a direct connection.\n" + "\t \t\tScheme is one of coap, coaps, coap+tcp and coaps+tcp\n" + "\t-T max_token_length\tSet the maximum token length (8-65804)\n" + "\t-U type\t\tTreat address defined by -A as a Unix socket address.\n" + "\t \t\ttype is 'coap', 'coaps', 'coap+tcp' or 'coaps+tcp'\n" + "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" + "\t \t\tlibrary logging\n" + "\t-X size\t\tMaximum message size to use for TCP based connections\n" + "\t \t\t(default is 8388864). Maximum value of 2^32 -1\n" + "PSK Options (if supported by underlying (D)TLS library)\n" + "\t-h hint\t\tIdentity Hint to send. Default is CoAP. Zero length is\n" + "\t \t\tno hint\n" + "\t-i match_identity_file\n" + "\t \t\tThis is a file that contains one or more lines of\n" + "\t \t\tIdentity Hints and (user) Identities to match for\n" + "\t \t\ta different new Pre-Shared Key (PSK) (comma separated)\n" + "\t \t\tto be used. E.g., per line\n" + "\t \t\t hint_to_match,identity_to_match,use_key\n" + "\t \t\tNote: -k still needs to be defined for the default case\n" + "\t \t\tNote: A match using the -s option may mean that the\n" + "\t \t\tcurrent Identity Hint is different to that defined by -h\n" + "\t-k key \t\tPre-Shared Key. This argument requires (D)TLS with PSK\n" + "\t \t\tto be available. This cannot be empty if defined.\n" + "\t \t\tNote that both -c and -k need to be defined for both\n" + "\t \t\tPSK and PKI to be concurrently supported\n" + "\t-s match_psk_sni_file\n" + "\t \t\tThis is a file that contains one or more lines of\n" + "\t \t\treceived Subject Name Identifier (SNI) to match to use\n" + "\t \t\ta different Identity Hint and associated Pre-Shared Key\n" + "\t \t\t(PSK) (comma separated) instead of the '-h hint' and\n" + "\t \t\t'-k key' options. E.g., per line\n" + "\t \t\t sni_to_match,use_hint,with_key\n" + "\t \t\tNote: -k still needs to be defined for the default case\n" + "\t \t\tif there is not a match\n" + "\t \t\tNote: The associated Pre-Shared Key will get updated if\n" + "\t \t\tthere is also a -i match. The update checking order is\n" + "\t \t\t-s followed by -i\n" + "\t-u user\t\tUser identity for pre-shared key mode (only used if\n" + "\t \t\toption -P is set)\n" + ); + fprintf(stderr, + "PKI Options (if supported by underlying (D)TLS library)\n" + "\tNote: If any one of '-c certfile', '-j keyfile' or '-C cafile' is in\n" + "\tPKCS11 URI naming format (pkcs11: prefix), then any remaining non\n" + "\tPKCS11 URI file definitions have to be in DER, not PEM, format.\n" + "\tOtherwise all of '-c certfile', '-j keyfile' or '-C cafile' are in\n" + "\tPEM format.\n\n" + "\t-c certfile\tPEM file or PKCS11 URI for the certificate. The private\n" + "\t \t\tkey can also be in the PEM file, or has the same PKCS11\n" + "\t \t\tURI. If not, the private key is defined by '-j keyfile'.\n" + "\t \t\tNote that both -c and -k need to be defined for both\n" + "\t \t\tPSK and PKI to be concurrently supported\n" + "\t-j keyfile\tPEM file or PKCS11 URI for the private key for the\n" + "\t \t\tcertificate in '-c certfile' if the parameter is\n" + "\t \t\tdifferent from certfile in '-c certfile'\n" + "\t-m \t\tUse COAP_PKI_KEY_PEM_BUF instead of COAP_PKI_KEY_PEM i/f\n" + "\t \t\tby reading into memory the Cert / CA file (for testing)\n" + "\t-n \t\tDisable remote peer certificate checking. This gives\n" + "\t \t\tclients the ability to use PKI, but without any defined\n" + "\t \t\tcertificates\n" + "\t-C cafile\tPEM file or PKCS11 URI that contains a list of one or\n" + "\t \t\tmore CAs that are to be passed to the client for the\n" + "\t \t\tclient to determine what client certificate to use.\n" + "\t \t\tNormally, this list of CAs would be the root CA and and\n" + "\t \t\tany intermediate CAs. Ideally the server certificate\n" + "\t \t\tshould be signed by the same CA so that mutual\n" + "\t \t\tauthentication can take place. The contents of cafile\n" + "\t \t\tare added to the trusted store of root CAs.\n" + "\t \t\tUsing the -C or -R options will will trigger the\n" + "\t \t\tvalidation of the client certificate unless overridden\n" + "\t \t\tby the -n option\n" + "\t-J pkcs11_pin\tThe user pin to unlock access to the PKCS11 token\n" + "\t-M rpk_file\tRaw Public Key (RPK) PEM file or PKCS11 URI that\n" + "\t \t\tcontains both PUBLIC KEY and PRIVATE KEY or just\n" + "\t \t\tEC PRIVATE KEY. (GnuTLS and TinyDTLS(PEM) support only).\n" + "\t \t\t'-C cafile' or '-R trust_casfile' are not required\n" + "\t-R trust_casfile\n" + "\t \t\tPEM file containing the set of trusted root CAs\n" + "\t \t\tthat are to be used to validate the client certificate.\n" + "\t \t\tAlternatively, this can point to a directory containing\n" + "\t \t\ta set of CA PEM files.\n" + "\t \t\tUsing '-R trust_casfile' disables common CA mutual\n" + "\t \t\tauthentication which can only be done by using\n" + "\t \t\t'-C cafile'.\n" + "\t \t\tUsing the -C or -R options will will trigger the\n" + "\t \t\tvalidation of the client certificate unless overridden\n" + "\t \t\tby the -n option\n" + "\t-S match_pki_sni_file\n" + "\t \t\tThis option denotes a file that contains one or more\n" + "\t \t\tlines of Subject Name Identifier (SNI) to match for new\n" + "\t \t\tCert file and new CA file (comma separated) to be used.\n" + "\t \t\tE.g., per line\n" + "\t \t\t sni_to_match,new_cert_file,new_ca_file\n" + "\t \t\tNote: -c and -C still need to be defined for the default\n" + "\t \t\tcase\n" + ); } static coap_context_t * @@ -2345,8 +2324,8 @@ get_context(const char *node, const char *port) { s_port = u_s_port + 1; } scheme_hint_bits = - coap_get_available_scheme_hint_bits(cert_file != NULL || key_defined != 0, - enable_ws, use_unix_proto); + coap_get_available_scheme_hint_bits(cert_file != NULL || key_defined != 0, + enable_ws, use_unix_proto); info_list = coap_resolve_address_info(node ? &local : NULL, u_s_port, s_port, AI_PASSIVE | AI_NUMERICHOST, scheme_hint_bits, @@ -2357,7 +2336,7 @@ get_context(const char *node, const char *port) { ep = coap_new_endpoint(ctx, &info->addr, info->proto); if (!ep) { coap_log_warn("cannot create endpoint for proto %u\n", - info->proto); + info->proto); } else { have_ep = 1; } @@ -2397,7 +2376,7 @@ cmdline_proxy(char *arg) { proxy_host_name_count++; next_name = strchr(next_name+1, ','); } - proxy_host_name_list = coap_malloc(proxy_host_name_count * sizeof(char*)); + proxy_host_name_list = coap_malloc(proxy_host_name_count * sizeof(char *)); next_name = host_start; ofs = 0; while (next_name) { @@ -2423,8 +2402,8 @@ cmdline_read_user(char *arg, unsigned char **buf, size_t maxlen) { #endif /* SERVER_CAN_PROXY */ static FILE *oscore_seq_num_fp = NULL; -static const char* oscore_conf_file = NULL; -static const char* oscore_seq_save_file = NULL; +static const char *oscore_conf_file = NULL; +static const char *oscore_seq_save_file = NULL; static int oscore_save_seq_num(uint64_t sender_seq_num, void *param COAP_UNUSED) { @@ -2506,11 +2485,12 @@ cmdline_read_key(char *arg, unsigned char **buf, size_t maxlen) { return len; } /* Need at least one byte for the pre-shared key */ - coap_log_crit("Invalid Pre-Shared Key specified\n" ); + coap_log_crit("Invalid Pre-Shared Key specified\n"); return -1; } -static int cmdline_read_psk_sni_check(char *arg) { +static int +cmdline_read_psk_sni_check(char *arg) { FILE *fp = fopen(arg, "r"); static char tmpbuf[256]; if (fp == NULL) { @@ -2530,7 +2510,7 @@ static int cmdline_read_psk_sni_check(char *arg) { if (tcp) { psk_sni_def_t *new_psk_sni_list; new_psk_sni_list = realloc(valid_psk_snis.psk_sni_list, - (valid_psk_snis.count + 1)*sizeof (valid_psk_snis.psk_sni_list[0])); + (valid_psk_snis.count + 1)*sizeof(valid_psk_snis.psk_sni_list[0])); if (new_psk_sni_list == NULL) { break; } @@ -2540,10 +2520,10 @@ static int cmdline_read_psk_sni_check(char *arg) { tcp = strchr(cp, ','); if (tcp) { valid_psk_snis.psk_sni_list[valid_psk_snis.count].new_hint = - coap_new_bin_const((const uint8_t *)cp, tcp-cp); + coap_new_bin_const((const uint8_t *)cp, tcp-cp); cp = tcp+1; valid_psk_snis.psk_sni_list[valid_psk_snis.count].new_key = - coap_new_bin_const((const uint8_t *)cp, strlen(cp)); + coap_new_bin_const((const uint8_t *)cp, strlen(cp)); valid_psk_snis.count++; } else { free(valid_psk_snis.psk_sni_list[valid_psk_snis.count].sni_match); @@ -2575,7 +2555,7 @@ cmdline_read_identity_check(char *arg) { if (tcp) { id_def_t *new_id_list; new_id_list = realloc(valid_ids.id_list, - (valid_ids.count + 1)*sizeof (valid_ids.id_list[0])); + (valid_ids.count + 1)*sizeof(valid_ids.id_list[0])); if (new_id_list == NULL) { break; } @@ -2585,10 +2565,10 @@ cmdline_read_identity_check(char *arg) { tcp = strchr(cp, ','); if (tcp) { valid_ids.id_list[valid_ids.count].identity_match = - coap_new_bin_const((const uint8_t *)cp, tcp-cp); + coap_new_bin_const((const uint8_t *)cp, tcp-cp); cp = tcp+1; valid_ids.id_list[valid_ids.count].new_key = - coap_new_bin_const((const uint8_t *)cp, strlen(cp)); + coap_new_bin_const((const uint8_t *)cp, strlen(cp)); valid_ids.count++; } else { free(valid_ids.id_list[valid_ids.count].hint_match); @@ -2666,32 +2646,32 @@ cmdline_read_pki_sni_check(char *arg) { if (tcp) { pki_sni_def_t *new_pki_sni_list; new_pki_sni_list = realloc(valid_pki_snis.pki_sni_list, - (valid_pki_snis.count + 1)*sizeof (valid_pki_snis.pki_sni_list[0])); + (valid_pki_snis.count + 1)*sizeof(valid_pki_snis.pki_sni_list[0])); if (new_pki_sni_list == NULL) { break; } valid_pki_snis.pki_sni_list = new_pki_sni_list; valid_pki_snis.pki_sni_list[valid_pki_snis.count].sni_match = - strndup(cp, tcp-cp); + strndup(cp, tcp-cp); cp = tcp+1; tcp = strchr(cp, ','); if (tcp) { int fail = 0; valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_cert = - strndup(cp, tcp-cp); + strndup(cp, tcp-cp); cp = tcp+1; valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_ca = - strndup(cp, strlen(cp)); + strndup(cp, strlen(cp)); if (access(valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_cert, - R_OK)) { + R_OK)) { coap_log_err("SNI file: Cert File: %s: Unable to access\n", - valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_cert); + valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_cert); fail = 1; } if (access(valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_ca, - R_OK)) { + R_OK)) { coap_log_err("SNI file: CA File: %s: Unable to access\n", - valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_ca); + valid_pki_snis.pki_sni_list[valid_pki_snis.count].new_ca); fail = 1; } if (fail) { @@ -2702,8 +2682,7 @@ cmdline_read_pki_sni_check(char *arg) { valid_pki_snis.count++; } } else { - coap_log_err( - "SNI file: SNI_match,Use_Cert_file,Use_CA_file not defined\n"); + coap_log_err("SNI file: SNI_match,Use_Cert_file,Use_CA_file not defined\n"); free(valid_pki_snis.pki_sni_list[valid_pki_snis.count].sni_match); } } @@ -2718,8 +2697,7 @@ cmdline_read_extended_token_size(char *arg) { if (extended_token_size < COAP_TOKEN_DEFAULT_MAX) { coap_log_err("Extended Token Length must be 8 or greater\n"); return 0; - } - else if (extended_token_size > COAP_TOKEN_EXT_MAX) { + } else if (extended_token_size > COAP_TOKEN_EXT_MAX) { coap_log_err("Extended Token Length must be 65804 or less\n"); return 0; } @@ -2747,17 +2725,19 @@ main(int argc, char **argv) { int exit_code = 0; uint16_t cache_ignore_options[] = { COAP_OPTION_BLOCK1, COAP_OPTION_BLOCK2, - /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ + /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ COAP_OPTION_MAXAGE, - /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ - COAP_OPTION_IF_NONE_MATCH }; + /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ + COAP_OPTION_IF_NONE_MATCH + }; #ifndef _WIN32 struct sigaction sa; #endif clock_offset = time(NULL); - while ((opt = getopt(argc, argv, "c:d:eg:G:h:i:j:J:k:l:mnp:rs:tu:v:w:A:C:E:L:M:NP:R:S:T:U:V:X:")) != -1) { + while ((opt = getopt(argc, argv, + "c:d:eg:G:h:i:j:J:k:l:mnp:rs:tu:v:w:A:C:E:L:M:NP:R:S:T:U:V:X:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -2909,7 +2889,7 @@ main(int argc, char **argv) { csm_max_message_size = strtol(optarg, NULL, 10); break; default: - usage( argv[0], LIBCOAP_PACKAGE_VERSION ); + usage(argv[0], LIBCOAP_PACKAGE_VERSION); goto failed; } } @@ -2917,17 +2897,17 @@ main(int argc, char **argv) { #ifdef _WIN32 signal(SIGINT, handle_sigint); #else - memset (&sa, 0, sizeof(sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handle_sigint; sa.sa_flags = 0; - sigaction (SIGINT, &sa, NULL); - sigaction (SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); sa.sa_handler = handle_sigusr2; - sigaction (SIGUSR2, &sa, NULL); + sigaction(SIGUSR2, &sa, NULL); /* So we do not exit on a SIGPIPE */ sa.sa_handler = SIG_IGN; - sigaction (SIGPIPE, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); #endif coap_startup(); @@ -2953,7 +2933,7 @@ main(int argc, char **argv) { /* Define the options to ignore when setting up cache-keys */ coap_cache_ignore_options(ctx, cache_ignore_options, - sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0])); + sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0])); /* join multicast group if requested at command line */ if (group) coap_join_mcast_group_intf(ctx, group, group_if); @@ -2982,7 +2962,7 @@ main(int argc, char **argv) { wait_ms = COAP_RESOURCE_CHECK_TIME * 1000; - while ( !quit ) { + while (!quit) { int result; if (coap_fd != -1) { @@ -3001,7 +2981,7 @@ main(int argc, char **argv) { tv.tv_sec = wait_ms / 1000; tv.tv_usec = (wait_ms % 1000) * 1000; /* Wait until any i/o takes place or timeout */ - result = select (nfds, &readfds, NULL, NULL, &tv); + result = select(nfds, &readfds, NULL, NULL, &tv); if (result == -1) { if (errno != EAGAIN) { coap_log_debug("select: %s (%d)\n", coap_socket_strerror(), errno); @@ -3024,11 +3004,11 @@ main(int argc, char **argv) { * * result is time spent in coap_io_process() */ - result = coap_io_process( ctx, wait_ms ); + result = coap_io_process(ctx, wait_ms); } - if ( result < 0 ) { + if (result < 0) { break; - } else if ( result && (unsigned)result < wait_ms ) { + } else if (result && (unsigned)result < wait_ms) { /* decrement if there is a result wait time returned */ wait_ms -= result; } else { @@ -3053,7 +3033,7 @@ main(int argc, char **argv) { } /* need to wait until next second starts if wait_ms is too large */ next_sec_ms = 1000 - (now % COAP_TICKS_PER_SECOND) * - 1000 / COAP_TICKS_PER_SECOND; + 1000 / COAP_TICKS_PER_SECOND; if (next_sec_ms && next_sec_ms < wait_ms) wait_ms = next_sec_ms; } diff --git a/examples/etsi_iot_01.c b/examples/etsi_iot_01.c index aec7442a..e4029036 100644 --- a/examples/etsi_iot_01.c +++ b/examples/etsi_iot_01.c @@ -53,7 +53,7 @@ handle_sigint(int signum COAP_UNUSED) { } #define INDEX "libcoap server for ETSI CoAP Plugtest, March 2012, Paris\n" \ - "Copyright (C) 2012 Olaf Bergmann \n\n" + "Copyright (C) 2012 Olaf Bergmann \n\n" static coap_payload_t * coap_new_payload(size_t size) { @@ -73,7 +73,7 @@ coap_find_payload(coap_resource_t *resource) { } static void -coap_add_payload(coap_resource_t *resource, coap_payload_t *payload){ +coap_add_payload(coap_resource_t *resource, coap_payload_t *payload) { assert(payload); coap_resource_set_userdata(resource, payload); @@ -107,7 +107,7 @@ hnd_get_index(coap_resource_t *resource COAP_UNUSED, buf); coap_add_option(response, COAP_OPTION_MAXAGE, - coap_encode_var_safe(buf, sizeof(buf), 0x2ffff), buf); + coap_encode_var_safe(buf, sizeof(buf), 0x2ffff), buf); coap_add_data(response, strlen(INDEX), (const uint8_t *)INDEX); } @@ -201,7 +201,7 @@ hnd_post_test(coap_resource_t *resource COAP_UNUSED, option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter); if (option) { test_payload->media_type = - coap_decode_var_bytes(coap_opt_value(option), coap_opt_length(option)); + coap_decode_var_bytes(coap_opt_value(option), coap_opt_length(option)); } coap_add_resource(coap_session_get_context(session), r); @@ -260,7 +260,7 @@ hnd_put_test(coap_resource_t *resource, if (option) { /* set media type given in request */ payload->media_type = - coap_decode_var_bytes(coap_opt_value(option), coap_opt_length(option)); + coap_decode_var_bytes(coap_opt_value(option), coap_opt_length(option)); } else { /* set default value */ payload->media_type = COAP_MEDIATYPE_TEXT_PLAIN; @@ -270,7 +270,7 @@ hnd_put_test(coap_resource_t *resource, */ return; - error: +error: coap_log_warn("cannot modify resource\n"); coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR); } @@ -371,8 +371,8 @@ hnd_get_separate(coap_resource_t *resource COAP_UNUSED, /* don't allow delay to be less than COAP_RESOURCE_CHECK_TIME*/ delay = d < COAP_RESOURCE_CHECK_TIME_SEC - ? COAP_RESOURCE_CHECK_TIME_SEC - : d; + ? COAP_RESOURCE_CHECK_TIME_SEC + : d; coap_log_debug("set delay to %lu\n", delay); break; } @@ -417,7 +417,7 @@ make_large(const char *filename) { return NULL; inputfile = fopen(filename, "r"); - if ( !inputfile ) { + if (!inputfile) { coap_log_warn("cannot read file %s\n", filename); coap_free(payload); return NULL; @@ -514,20 +514,20 @@ init_resources(coap_context_t *ctx) { } static void -usage( const char *program, const char *version) { +usage(const char *program, const char *version) { const char *p; - p = strrchr( program, '/' ); - if ( p ) + p = strrchr(program, '/'); + if (p) program = ++p; - fprintf( stderr, "%s v%s -- ETSI CoAP plugtest server\n" - "(c) 2012 Olaf Bergmann \n\n" - "usage: %s [-A address] [-p port]\n\n" - "\t-A address\tinterface address to bind to\n" - "\t-p port\t\tlisten on specified port\n" - "\t-v num\t\tverbosity level (default: 3)\n", - program, version, program ); + fprintf(stderr, "%s v%s -- ETSI CoAP plugtest server\n" + "(c) 2012 Olaf Bergmann \n\n" + "usage: %s [-A address] [-p port]\n\n" + "\t-A address\tinterface address to bind to\n" + "\t-p port\t\tlisten on specified port\n" + "\t-v num\t\tverbosity level (default: 3)\n", + program, version, program); } static coap_context_t * @@ -543,7 +543,7 @@ get_context(const char *node, const char *port) { hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; s = getaddrinfo(node, port, &hints, &result); - if ( s != 0 ) { + if (s != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); return NULL; } @@ -567,7 +567,7 @@ get_context(const char *node, const char *port) { fprintf(stderr, "no context available for interface '%s'\n", node); - finish: +finish: freeaddrinfo(result); return ctx; } @@ -596,8 +596,8 @@ main(int argc, char **argv) { log_level = strtol(optarg, NULL, 10); break; default: - usage( argv[0], LIBCOAP_PACKAGE_VERSION ); - exit( 1 ); + usage(argv[0], LIBCOAP_PACKAGE_VERSION); + exit(1); } } @@ -609,24 +609,24 @@ main(int argc, char **argv) { init_resources(ctx); - memset (&sa, 0, sizeof(sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handle_sigint; sa.sa_flags = 0; - sigaction (SIGINT, &sa, NULL); - sigaction (SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); /* So we do not exit on a SIGPIPE */ sa.sa_handler = SIG_IGN; - sigaction (SIGPIPE, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); - while ( !quit ) { - result = coap_io_process( ctx, COAP_RESOURCE_CHECK_TIME * 1000 ); - if ( result >= 0 ) { + while (!quit) { + result = coap_io_process(ctx, COAP_RESOURCE_CHECK_TIME * 1000); + if (result >= 0) { /* coap_check_resource_list( ctx ); */ } } - coap_free_context( ctx ); + coap_free_context(ctx); coap_cleanup(); return 0; diff --git a/examples/getopt.c b/examples/getopt.c index b3865d63..59719e12 100644 --- a/examples/getopt.c +++ b/examples/getopt.c @@ -20,47 +20,46 @@ static int optopt; static char *optarg; static int -getopt(int argc, char *argv[], const char *opts) -{ - static int sp = 1; - int c; - char *cp; +getopt(int argc, char *argv[], const char *opts) { + static int sp = 1; + int c; + char *cp; - if (sp == 1) { - if (optind >= argc || - argv[optind][0] != '-' || argv[optind][1] == '\0') - return EOF; - else if (!strcmp(argv[optind], "--")) { - optind++; - return EOF; - } + if (sp == 1) { + if (optind >= argc || + argv[optind][0] != '-' || argv[optind][1] == '\0') + return EOF; + else if (!strcmp(argv[optind], "--")) { + optind++; + return EOF; } - optopt = c = argv[optind][sp]; - if (c == ':' || !(cp = strchr(opts, c))) { - fprintf(stderr, ": illegal option -- %c\n", c); - if (argv[optind][++sp] == '\0') { - optind++; - sp = 1; - } - return '?'; + } + optopt = c = argv[optind][sp]; + if (c == ':' || !(cp = strchr(opts, c))) { + fprintf(stderr, ": illegal option -- %c\n", c); + if (argv[optind][++sp] == '\0') { + optind++; + sp = 1; } - if (*++cp == ':') { - if (argv[optind][sp+1] != '\0') - optarg = &argv[optind++][sp+1]; - else if(++optind >= argc) { - fprintf(stderr, ": option requires an argument -- %c\n", c); - sp = 1; - return '?'; - } else - optarg = argv[optind++]; - sp = 1; - } else { - if (argv[optind][++sp] == '\0') { - sp = 1; - optind++; - } - optarg = NULL; + return '?'; + } + if (*++cp == ':') { + if (argv[optind][sp+1] != '\0') + optarg = &argv[optind++][sp+1]; + else if (++optind >= argc) { + fprintf(stderr, ": option requires an argument -- %c\n", c); + sp = 1; + return '?'; + } else + optarg = argv[optind++]; + sp = 1; + } else { + if (argv[optind][++sp] == '\0') { + sp = 1; + optind++; } + optarg = NULL; + } - return c; + return c; } diff --git a/examples/oscore-interop-server.c b/examples/oscore-interop-server.c index 2fb900c8..b73b92d2 100644 --- a/examples/oscore-interop-server.c +++ b/examples/oscore-interop-server.c @@ -32,9 +32,9 @@ #ifndef R_OK #define R_OK 4 #endif -static char* -strndup(const char* s1, size_t n) { - char* copy = (char*)malloc(n + 1); +static char * +strndup(const char *s1, size_t n) { + char *copy = (char *)malloc(n + 1); if (copy) { memcpy(copy, s1, n); copy[n] = 0; @@ -81,9 +81,9 @@ handle_sigint(int signum COAP_UNUSED) { } #define INDEX "This is a OSCORE test server made with libcoap " \ - "(see https://libcoap.net)\n" \ - "Copyright (C) 2023 Olaf Bergmann " \ - "and others\n\n" + "(see https://libcoap.net)\n" \ + "Copyright (C) 2023 Olaf Bergmann " \ + "and others\n\n" static void hnd_get_index(coap_resource_t *resource, @@ -185,8 +185,8 @@ hnd_put_hello_7(coap_resource_t *resource, if ((option = coap_check_option(request, COAP_OPTION_IF_MATCH, &opt_iter)) != NULL) { - etag = coap_decode_var_bytes8 (coap_opt_value (option), - coap_opt_length (option)); + etag = coap_decode_var_bytes8(coap_opt_value(option), + coap_opt_length(option)); if (etag != 0x7b) { coap_pdu_set_code(response, COAP_RESPONSE_CODE_PRECONDITION_FAILED); return; @@ -207,7 +207,7 @@ hnd_get_observe1(coap_resource_t *resource, const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { - static int count = 0; + static int count = 0; count++; switch (count) { @@ -242,7 +242,7 @@ hnd_get_observe2(coap_resource_t *resource, const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { - static int count = 0; + static int count = 0; count++; switch (count) { @@ -337,7 +337,7 @@ init_resources(coap_context_t *ctx) { } static uint8_t * -read_file_mem(const char* file, size_t *length) { +read_file_mem(const char *file, size_t *length) { FILE *f; uint8_t *buf; struct stat statbuf; @@ -369,65 +369,65 @@ read_file_mem(const char* file, size_t *length) { } static void -usage( const char *program, const char *version) { +usage(const char *program, const char *version) { const char *p; char buffer[120]; const char *lib_build = coap_package_build(); - p = strrchr( program, '/' ); - if ( p ) + p = strrchr(program, '/'); + if (p) program = ++p; - fprintf( stderr, "%s v%s -- OSCORE interop implementation\n" - "(c) 2023 Olaf Bergmann and others\n\n" - "Build: %s\n" - "%s\n" - , program, version, lib_build, - coap_string_tls_version(buffer, sizeof(buffer))); + fprintf(stderr, "%s v%s -- OSCORE interop implementation\n" + "(c) 2023 Olaf Bergmann and others\n\n" + "Build: %s\n" + "%s\n" + , program, version, lib_build, + coap_string_tls_version(buffer, sizeof(buffer))); fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer))); fprintf(stderr, "\n" - "Usage: %s [-d max] [-g group] [-l loss] [-p port] [-r] [-v num]\n" - "\t\t[-A address] [-E oscore_conf_file[,seq_file]] [-G group_if]\n" - "\t\t[-L value] [-N] [-P scheme://address[:port],[name1[,name2..]]]\n" - "\t\t[-X size]\n" - "General Options\n" - "\t-d max \t\tAllow dynamic creation of up to a total of max\n" - "\t \t\tresources. If max is reached, a 4.06 code is returned\n" - "\t \t\tuntil one of the dynamic resources has been deleted\n" - "\t-g group\tJoin the given multicast group\n" - "\t \t\tNote: DTLS over multicast is not currently supported\n" - "\t-l list\t\tFail to send some datagrams specified by a comma\n" - "\t \t\tseparated list of numbers or number ranges\n" - "\t \t\t(for debugging only)\n" - "\t-l loss%%\tRandomly fail to send datagrams with the specified\n" - "\t \t\tprobability - 100%% all datagrams, 0%% no datagrams\n" - "\t \t\t(for debugging only)\n" - "\t-p port\t\tListen on specified port for UDP and TCP. If (D)TLS is\n" - "\t \t\tenabled, then the coap-server will also listen on\n" - "\t \t\t 'port'+1 for DTLS and TLS. The default port is 5683\n" - "\t-r \t\tEnable multicast per resource support. If enabled,\n" - "\t \t\tonly '/', '/async' and '/.well-known/core' are enabled\n" - "\t \t\tfor multicast requests support, otherwise all\n" - "\t \t\tresources are enabled\n" - "\t-v num \t\tVerbosity level (default 3, maximum is 9). Above 7,\n" - "\t \t\tthere is increased verbosity in GnuTLS and OpenSSL\n" - "\t \t\tlogging\n" - "\t-A address\tInterface address to bind to\n" - "\t-E oscore_conf_file[,seq_file]\n" - "\t \t\toscore_conf_file contains OSCORE configuration. See\n" - "\t \t\tcoap-oscore-conf(5) for definitions.\n" - "\t \t\tOptional seq_file is used to save the current transmit\n" - "\t \t\tsequence number, so on restart sequence numbers continue\n" - "\t-G group_if\tUse this interface for listening for the multicast\n" - "\t \t\tgroup. This can be different from the implied interface\n" - "\t \t\tif the -A option is used\n" - "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" - "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" - "\t \t\t(Sum of one or more of 1,2 and 4)\n" - "\t-N \t\tMake \"observe\" responses NON-confirmable. Even if set\n" - "\t \t\tevery fifth response will still be sent as a confirmable\n" - "\t \t\tresponse (RFC 7641 requirement)\n" - , program); + "Usage: %s [-d max] [-g group] [-l loss] [-p port] [-r] [-v num]\n" + "\t\t[-A address] [-E oscore_conf_file[,seq_file]] [-G group_if]\n" + "\t\t[-L value] [-N] [-P scheme://address[:port],[name1[,name2..]]]\n" + "\t\t[-X size]\n" + "General Options\n" + "\t-d max \t\tAllow dynamic creation of up to a total of max\n" + "\t \t\tresources. If max is reached, a 4.06 code is returned\n" + "\t \t\tuntil one of the dynamic resources has been deleted\n" + "\t-g group\tJoin the given multicast group\n" + "\t \t\tNote: DTLS over multicast is not currently supported\n" + "\t-l list\t\tFail to send some datagrams specified by a comma\n" + "\t \t\tseparated list of numbers or number ranges\n" + "\t \t\t(for debugging only)\n" + "\t-l loss%%\tRandomly fail to send datagrams with the specified\n" + "\t \t\tprobability - 100%% all datagrams, 0%% no datagrams\n" + "\t \t\t(for debugging only)\n" + "\t-p port\t\tListen on specified port for UDP and TCP. If (D)TLS is\n" + "\t \t\tenabled, then the coap-server will also listen on\n" + "\t \t\t 'port'+1 for DTLS and TLS. The default port is 5683\n" + "\t-r \t\tEnable multicast per resource support. If enabled,\n" + "\t \t\tonly '/', '/async' and '/.well-known/core' are enabled\n" + "\t \t\tfor multicast requests support, otherwise all\n" + "\t \t\tresources are enabled\n" + "\t-v num \t\tVerbosity level (default 3, maximum is 9). Above 7,\n" + "\t \t\tthere is increased verbosity in GnuTLS and OpenSSL\n" + "\t \t\tlogging\n" + "\t-A address\tInterface address to bind to\n" + "\t-E oscore_conf_file[,seq_file]\n" + "\t \t\toscore_conf_file contains OSCORE configuration. See\n" + "\t \t\tcoap-oscore-conf(5) for definitions.\n" + "\t \t\tOptional seq_file is used to save the current transmit\n" + "\t \t\tsequence number, so on restart sequence numbers continue\n" + "\t-G group_if\tUse this interface for listening for the multicast\n" + "\t \t\tgroup. This can be different from the implied interface\n" + "\t \t\tif the -A option is used\n" + "\t-L value\tSum of one or more COAP_BLOCK_* flag valuess for block\n" + "\t \t\thandling methods. Default is 1 (COAP_BLOCK_USE_LIBCOAP)\n" + "\t \t\t(Sum of one or more of 1,2 and 4)\n" + "\t-N \t\tMake \"observe\" responses NON-confirmable. Even if set\n" + "\t \t\tevery fifth response will still be sent as a confirmable\n" + "\t \t\tresponse (RFC 7641 requirement)\n" + , program); } static coap_context_t * @@ -448,7 +448,7 @@ get_context(const char *node, const char *port) { hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; s = getaddrinfo(node, port, &hints, &result); - if ( s != 0 ) { + if (s != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); coap_free_context(ctx); return NULL; @@ -491,8 +491,8 @@ finish: } static FILE *oscore_seq_num_fp = NULL; -static const char* oscore_conf_file = NULL; -static const char* oscore_seq_save_file = NULL; +static const char *oscore_conf_file = NULL; +static const char *oscore_seq_save_file = NULL; static int oscore_save_seq_num(uint64_t sender_seq_num, void *param COAP_UNUSED) { @@ -584,10 +584,11 @@ main(int argc, char **argv) { int nfds = 0; uint16_t cache_ignore_options[] = { COAP_OPTION_BLOCK1, COAP_OPTION_BLOCK2, - /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ + /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ COAP_OPTION_MAXAGE, - /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ - COAP_OPTION_IF_NONE_MATCH }; + /* See https://rfc-editor.org/rfc/rfc7959#section-2.10 */ + COAP_OPTION_IF_NONE_MATCH + }; #ifndef _WIN32 struct sigaction sa; #endif @@ -639,23 +640,23 @@ main(int argc, char **argv) { csm_max_message_size = strtol(optarg, NULL, 10); break; default: - usage( argv[0], LIBCOAP_PACKAGE_VERSION ); - exit( 1 ); + usage(argv[0], LIBCOAP_PACKAGE_VERSION); + exit(1); } } #ifdef _WIN32 signal(SIGINT, handle_sigint); #else - memset (&sa, 0, sizeof(sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handle_sigint; sa.sa_flags = 0; - sigaction (SIGINT, &sa, NULL); - sigaction (SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); /* So we do not exit on a SIGPIPE */ sa.sa_handler = SIG_IGN; - sigaction (SIGPIPE, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); #endif coap_startup(); @@ -679,7 +680,7 @@ main(int argc, char **argv) { /* Define the options to ignore when setting up cache-keys */ coap_cache_ignore_options(ctx, cache_ignore_options, - sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0])); + sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0])); /* join multicast group if requested at command line */ if (group) coap_join_mcast_group_intf(ctx, group, group_if); @@ -694,7 +695,7 @@ main(int argc, char **argv) { wait_ms = COAP_RESOURCE_CHECK_TIME * 1000; - while ( !quit ) { + while (!quit) { int result; if (coap_fd != -1) { @@ -713,11 +714,11 @@ main(int argc, char **argv) { tv.tv_sec = wait_ms / 1000; tv.tv_usec = (wait_ms % 1000) * 1000; /* Wait until any i/o takes place or timeout */ - result = select (nfds, &readfds, NULL, NULL, &tv); + result = select(nfds, &readfds, NULL, NULL, &tv); if (result == -1) { if (errno != EAGAIN) { coap_log_debug("select: %s (%d)\n", coap_socket_strerror(), - errno); + errno); break; } } @@ -737,11 +738,11 @@ main(int argc, char **argv) { * * result is time spent in coap_io_process() */ - result = coap_io_process( ctx, wait_ms ); + result = coap_io_process(ctx, wait_ms); } - if ( result < 0 ) { + if (result < 0) { break; - } else if ( result && (unsigned)result < wait_ms ) { + } else if (result && (unsigned)result < wait_ms) { /* decrement if there is a result wait time returned */ wait_ms -= result; } else { @@ -769,7 +770,7 @@ main(int argc, char **argv) { } /* need to wait until next second starts if wait_ms is too large */ next_sec_ms = 1000 - (now % COAP_TICKS_PER_SECOND) * - 1000 / COAP_TICKS_PER_SECOND; + 1000 / COAP_TICKS_PER_SECOND; if (next_sec_ms && next_sec_ms < wait_ms) wait_ms = next_sec_ms; } diff --git a/examples/tiny.c b/examples/tiny.c index ad48aae5..c5554d33 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -36,7 +36,8 @@ /* #define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (v >> ls) & MMASK) + ls) */ COAP_STATIC_INLINE unsigned char COAP_PSEUDOFP_ENCODE_8_4_DOWN(unsigned int v, int *ls) { - if (v < HIBIT) return v; + if (v < HIBIT) + return v; *ls = coap_fls(v) - Nn; return ((v >> *ls) & MMASK) + *ls; } @@ -64,7 +65,7 @@ make_pdu(coap_session_t *session, unsigned int value) { enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(value, &ls); len = sprintf((char *)buf, "%c%u", enc, COAP_PSEUDOFP_DECODE_8_4(enc)); - coap_add_data( pdu, len, buf ); + coap_add_data(pdu, len, buf); return pdu; } @@ -73,15 +74,15 @@ static void usage(const char *program) { const char *p; - p = strrchr( program, '/' ); - if ( p ) + p = strrchr(program, '/'); + if (p) program = ++p; - fprintf( stderr, "%s -- tiny fake sensor\n" - "(c) 2010 Olaf Bergmann \n\n" - "usage: %s [group address]\n" - "\n\nSends some fake sensor values to specified multicast group\n", - program, program ); + fprintf(stderr, "%s -- tiny fake sensor\n" + "(c) 2010 Olaf Bergmann \n\n" + "usage: %s [group address]\n" + "\n\nSends some fake sensor values to specified multicast group\n", + program, program); } static coap_session_t * @@ -101,7 +102,7 @@ get_session(coap_context_t *ctx, const char *group) { hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL; s = getaddrinfo(group, NULL, &hints, &result); - if ( s != 0 ) { + if (s != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); return NULL; } @@ -140,9 +141,9 @@ main(int argc, char **argv) { struct sigaction sa; coap_context_t *ctx; - if ( argc > 1 && strncmp(argv[1], "-h", 2) == 0 ) { - usage( argv[0] ); - exit( 1 ); + if (argc > 1 && strncmp(argv[1], "-h", 2) == 0) { + usage(argv[0]); + exit(1); } ctx = coap_new_context(NULL); @@ -151,29 +152,30 @@ main(int argc, char **argv) { session = get_session(ctx, argc > 1 ? argv[1] : "::1"); - if ( !session ) + if (!session) return -1; - memset (&sa, 0, sizeof(sa)); + memset(&sa, 0, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_handler = handle_sigint; sa.sa_flags = 0; - sigaction (SIGINT, &sa, NULL); - sigaction (SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + sigaction(SIGTERM, &sa, NULL); /* So we do not exit on a SIGPIPE */ sa.sa_handler = SIG_IGN; - sigaction (SIGPIPE, &sa, NULL); + sigaction(SIGPIPE, &sa, NULL); - while ( !quit ) { + while (!quit) { - if (! (pdu = make_pdu( session, rand() & 0xfff ) ) ) + if (!(pdu = make_pdu(session, rand() & 0xfff))) break; coap_send(session, pdu); - tv.tv_sec = 5; tv.tv_usec = 0; + tv.tv_sec = 5; + tv.tv_usec = 0; - select( 0, 0, 0, 0, &tv ); + select(0, 0, 0, 0, &tv); }