11 #if defined(HAVE_STRNLEN) && defined(__GNUC__) && !defined(_GNU_SOURCE) 20 #ifdef HAVE_ARPA_INET_H 21 #include <arpa/inet.h> 23 #ifdef HAVE_WS2TCPIP_H 32 # define fprintf(fd, ...) LWIP_PLATFORM_DIAG((__VA_ARGS__)) 38 # define DEBUG DEBUG_PRINT 40 #include "net/ip/uip-debug.h" 52 return PACKAGE_STRING;
72 "EMRG",
"ALRT",
"CRIT",
"ERR ",
"WARN",
"NOTE",
"INFO",
"DEBG",
"????",
"CIPH" 81 tmp = localtime(&now);
82 return strftime(s, len,
"%b %d %H:%M:%S", tmp);
90 return snprintf(s, len,
"%u.%03u",
113 while(*s++ && n < maxlen)
121 unsigned char *result,
size_t buflen,
int encode_always ) {
122 const uint8_t hex[] =
"0123456789ABCDEF";
124 assert(data || len == 0);
131 if (!encode_always && isprint(*data)) {
132 if (cnt+1 < buflen) {
139 if (cnt+4 < buflen) {
142 *result++ = hex[(*data & 0xf0) >> 4];
143 *result++ = hex[*data & 0x0f];
157 #define min(a,b) ((a) < (b) ? (a) : (b)) 162 #if defined( HAVE_ARPA_INET_H ) || defined( HAVE_WS2TCPIP_H ) 163 const void *addrptr = NULL;
165 unsigned char *p = buf;
168 switch (addr->
addr.
sa.sa_family) {
170 addrptr = &addr->
addr.
sin.sin_addr;
171 port = ntohs(addr->
addr.
sin.sin_port);
172 need_buf = INET_ADDRSTRLEN;
180 addrptr = &addr->
addr.
sin6.sin6_addr;
181 port = ntohs(addr->
addr.
sin6.sin6_port);
186 memcpy(buf,
"(unknown address type)",
min(22, len));
191 if (inet_ntop(addr->
addr.
sa.sa_family, addrptr, (
char *)p,
192 min(len, need_buf)) == 0) {
193 perror(
"coap_print_addr");
199 if (addr->
addr.
sa.sa_family == AF_INET6) {
206 p += snprintf((
char *)p, buf + len - p + 1,
":%d", port);
208 return buf + len - p;
211 unsigned char *p = buf;
213 # if NETSTACK_CONF_WITH_IPV6 214 const uint8_t hex[] =
"0123456789ABCDEF";
221 for (i=0; i < 16; i += 2) {
225 *p++ = hex[(addr->
addr.u8[i] & 0xf0) >> 4];
226 *p++ = hex[(addr->
addr.u8[i] & 0x0f)];
227 *p++ = hex[(addr->
addr.u8[i+1] & 0xf0) >> 4];
228 *p++ = hex[(addr->
addr.u8[i+1] & 0x0f)];
232 # warning "IPv4 network addresses will not be included in debug output" 237 if (buf + len - p < 6)
241 p += snprintf((
char *)p, buf + len - p + 1,
":%d", uip_htons(addr->port));
249 # warning "inet_ntop() not available, network addresses will not be included in debug output" 256 # define fprintf(fd, ...) { (void)fd; PRINTF(__VA_ARGS__); } 260 # define vfprintf(fd, ...) { (void)fd; vprintf(__VA_ARGS__); } 262 # define vfprintf(fd, ...) { (void)fd; PRINTF(__VA_ARGS__); } 269 static const char *types[] = {
"CON",
"NON",
"ACK",
"RST",
"???" };
271 return types[
min(t,
sizeof(types)/
sizeof(
char *) - 1)];
277 static const char *methods[] = {
"0.00",
"GET",
"POST",
"PUT",
"DELETE",
278 "FETCH",
"PATCH",
"iPATCH" };
279 static const char *signals[] = {
"7.00",
"CSM",
"Ping",
"Pong",
"Release",
283 if (c <
sizeof(methods)/
sizeof(
const char *)) {
285 }
else if (c >= 224 && c - 224 < (
int)(
sizeof(signals)/
sizeof(
const char *))) {
286 return signals[c-224];
288 snprintf(buf,
sizeof(buf),
"%u.%02u", (c >> 5) & 0x7, c & 0x1f);
296 struct option_desc_t {
301 static struct option_desc_t options[] = {
324 static struct option_desc_t options_csm[] = {
329 static struct option_desc_t options_pingpong[] = {
333 static struct option_desc_t options_release[] = {
338 static struct option_desc_t options_abort[] = {
346 for (i = 0; i <
sizeof(options_csm)/
sizeof(
struct option_desc_t); i++) {
347 if (option_type == options_csm[i].type) {
348 return options_csm[i].name;
352 for (i = 0; i <
sizeof(options_pingpong)/
sizeof(
struct option_desc_t); i++) {
353 if (option_type == options_pingpong[i].type) {
354 return options_pingpong[i].name;
358 for (i = 0; i <
sizeof(options_release)/
sizeof(
struct option_desc_t); i++) {
359 if (option_type == options_release[i].type) {
360 return options_release[i].name;
364 for (i = 0; i <
sizeof(options_abort)/
sizeof(
struct option_desc_t); i++) {
365 if (option_type == options_abort[i].type) {
366 return options_abort[i].name;
371 for (i = 0; i <
sizeof(options)/
sizeof(
struct option_desc_t); i++) {
372 if (option_type == options[i].type) {
373 return options[i].name;
378 snprintf(buf,
sizeof(buf),
"%u", option_type);
384 unsigned char *result,
unsigned int buflen) {
390 static struct desc_t formats[] = {
414 { 75,
"application/dcaf+cbor" }
420 for (i = 0; i <
sizeof(formats)/
sizeof(
struct desc_t); i++) {
421 if (format_type == formats[i].type) {
422 return snprintf((
char *)result, buflen,
"%s", formats[i].name);
427 return snprintf((
char *)result, buflen,
"%d", format_type);
437 return !(content_format == -1 ||
444 #define COAP_DO_SHOW_OUTPUT_LINE \ 446 if (use_fprintf_for_show_pdu) { \ 447 fprintf(COAP_DEBUG_FD, "%s", outbuf); \ 450 coap_log(level, "%s", outbuf); \ 456 #if COAP_CONSTRAINED_STACK 457 static coap_mutex_t static_show_pdu_mutex = COAP_MUTEX_INITIALIZER;
465 int encode = 0, have_options = 0, i;
468 int content_format = -1;
477 #if COAP_CONSTRAINED_STACK 478 coap_mutex_lock(&static_show_pdu_mutex);
481 snprintf(outbuf,
sizeof(outbuf),
"v:%d t:%s c:%s i:%04x {",
486 outbuflen = strlen(outbuf);
487 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
488 "%02x", pdu->
token[i]);
490 outbuflen = strlen(outbuf);
491 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
"}");
496 outbuflen = strlen(outbuf);
497 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
" [");
502 outbuflen = strlen(outbuf);
503 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
",");
508 buf_len = snprintf((
char *)buf,
sizeof(buf),
"%u",
522 buf,
sizeof(buf), 0);
525 buf_len = snprintf((
char *)buf,
sizeof(buf),
"%u",
534 buf_len = snprintf((
char *)buf,
sizeof(buf),
"%u",
541 }
else switch (opt_iter.
type) {
553 buf_len = snprintf((
char *)buf,
sizeof(buf),
"%u/%c/%u",
566 buf_len = snprintf((
char *)buf,
sizeof(buf),
"%u",
586 buf,
sizeof(buf), encode);
589 outbuflen = strlen(outbuf);
590 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
595 outbuflen = strlen(outbuf);
596 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
" ]");
600 outbuflen = strlen(outbuf);
601 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
" :: ");
604 int keep_data_len = data_len;
607 outbuflen = strlen(outbuf);
608 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
609 "binary data length %zu\n", data_len);
615 snprintf(outbuf,
sizeof(outbuf),
"<<");
617 outbuflen = strlen(outbuf);
618 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
621 outbuflen = strlen(outbuf);
622 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
">>");
623 data_len = keep_data_len;
625 outbuflen = strlen(outbuf);
626 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
"\n");
633 snprintf(outbuf,
sizeof(outbuf),
"<<");
635 outbuflen = strlen(outbuf);
636 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
637 "%c ", isprint (*data) ? *data :
'.');
640 outbuflen = strlen(outbuf);
641 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
">>");
645 outbuflen = strlen(outbuf);
646 max_length =
sizeof(outbuf)-outbuflen;
647 if (snprintf(&outbuf[outbuflen], max_length,
"'%s'", buf) >= (int)max_length)
648 outbuf[
sizeof(outbuf)-1] =
'\000';
653 outbuflen = strlen(outbuf);
654 snprintf(&outbuf[outbuflen],
sizeof(outbuf)-outbuflen,
"\n");
657 #if COAP_CONSTRAINED_STACK 658 coap_mutex_unlock(&static_show_pdu_mutex);
677 switch (tls_version->
type) {
679 snprintf(buffer, bufsize,
"TLS Library: None");
682 snprintf(buffer, bufsize,
"TLS Library: TinyDTLS - runtime %lu.%lu.%lu, " 683 "libcoap built for %lu.%lu.%lu",
684 (
unsigned long)(tls_version->
version >> 16),
685 (
unsigned long)((tls_version->
version >> 8) & 0xff),
686 (
unsigned long)(tls_version->
version & 0xff),
692 switch (tls_version->
version &0xf) {
694 strcpy(beta,
"-dev");
700 strcpy(beta,
"-beta");
701 beta[5] = (tls_version->
version &0xf) +
'0';
705 sub[0] = ((tls_version->
version >> 4) & 0xff) ?
706 ((tls_version->
version >> 4) & 0xff) +
'a' -1 :
'\000';
710 strcpy(b_beta,
"-dev");
716 strcpy(b_beta,
"-beta");
722 ((tls_version->
built_version >> 4) & 0xff) +
'a' -1 :
'\000';
724 snprintf(buffer, bufsize,
"TLS Library: OpenSSL - runtime " 725 "%lu.%lu.%lu%s%s, libcoap built for %lu.%lu.%lu%s%s",
726 (
unsigned long)(tls_version->
version >> 28),
727 (
unsigned long)((tls_version->
version >> 20) & 0xff),
728 (
unsigned long)((tls_version->
version >> 12) & 0xff), sub, beta,
735 snprintf(buffer, bufsize,
"TLS Library: GnuTLS - runtime %lu.%lu.%lu, " 736 "libcoap built for %lu.%lu.%lu",
737 (
unsigned long)(tls_version->
version >> 16),
738 (
unsigned long)((tls_version->
version >> 8) & 0xff),
739 (
unsigned long)(tls_version->
version & 0xff),
745 snprintf(buffer, bufsize,
"Library type %d unknown", tls_version->
type);
764 #if COAP_CONSTRAINED_STACK 765 static coap_mutex_t static_log_mutex = COAP_MUTEX_INITIALIZER;
771 va_start(ap, format);
772 #if COAP_CONSTRAINED_STACK 773 coap_mutex_lock(&static_log_mutex);
776 vsnprintf( message,
sizeof(message), format, ap);
779 #if COAP_CONSTRAINED_STACK 780 coap_mutex_unlock(&static_log_mutex);
792 fprintf(log_fd,
"%s ", timebuf);
795 fprintf(log_fd,
"%s ",
loglevels[level]);
797 va_start(ap, format);
798 vfprintf(log_fd, format, ap);
813 const char *p = loss_level;
815 int n = (int)strtol(p, &end, 10), i = 0;
816 if (end == p || n < 0)
830 n = (int)strtol(p, &end, 10);
831 if (end == p || n <= 0)
840 n = (int)strtol(p, &end, 10);
841 if (end == p || n <= 0)
uint8_t code
request method (value 1–10) or response code (value 40-255)
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
#define COAP_OPTION_IF_MATCH
#define COAP_MEDIATYPE_APPLICATION_SENSML_JSON
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
Returns the value of field num in the given block option block_opt.
#define COAP_SIGNALING_PING
COAP_STATIC_INLINE int is_binary(int content_format)
Returns 1 if the given content_format is either unknown or known to carry binary data.
#define COAP_SIGNALING_CSM
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC0
#define COAP_OPTION_PROXY_URI
#define COAP_OPTION_CONTENT_FORMAT
static const char * msg_code_string(uint16_t c)
Returns a textual description of the method or response code.
#define COAP_OPTION_NORESPONSE
multi-purpose address abstraction
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
const char * coap_package_name(void)
Get the library package name.
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
#define COAP_OPTION_OBSERVE
#define COAP_DEBUG_BUF_SIZE
#define COAP_MEDIATYPE_APPLICATION_JSON
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_OPTION_BLOCK1
int coap_debug_set_packet_loss(const char *loss_level)
Set the packet loss level for testing.
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
#define COAP_OPTION_MAXAGE
uint64_t version
(D)TLS runtime Library Version
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
#define COAP_MEDIATYPE_APPLICATION_SENSML_EXI
#define COAP_DO_SHOW_OUTPUT_LINE
#define COAP_OPTION_LOCATION_QUERY
#define COAP_OPTION_PROXY_SCHEME
uint16_t type
decoded option type
#define COAP_MEDIATYPE_APPLICATION_SENSML_XML
#define COAP_MEDIATYPE_APPLICATION_SENML_CBOR
#define COAP_MEDIATYPE_APPLICATION_SENML_JSON
#define COAP_MEDIATYPE_APPLICATION_EXI
static coap_log_handler_t log_handler
char * coap_string_tls_version(char *buffer, size_t bufsize)
Build a string containing the current (D)TLS library linked with and built for version.
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
void coap_set_log_level(coap_log_t level)
Sets the log level to the specified value.
#define COAP_DEFAULT_VERSION
void coap_log_impl(coap_log_t level, const char *format,...)
Writes the given text to COAP_ERR_FD (for level <= LOG_CRIT) or COAP_DEBUG_FD (for level >= LOG_ERR)...
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
coap_tls_library_t type
Library type.
#define COAP_DEBUG_FD
Used for output for LOG_DEBUG to LOG_ERR.
void(* coap_log_handler_t)(coap_log_t level, const char *message)
Logging call-back handler definition.
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
#define COAP_SIGNALING_PONG
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint16_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
#define COAP_SIGNALING_OPTION_CUSTODY
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN
#define COAP_OPTION_URI_PORT
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_ERR_FD
Used for output for LOG_CRIT to LOG_EMERG.
#define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR
static int num_packet_loss_intervals
uint8_t * token
first byte of token, if any, or options
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
The structure used for returning the underlying (D)TLS library information.
static size_t print_readable(const uint8_t *data, size_t len, unsigned char *result, size_t buflen, int encode_always)
#define COAP_OPTION_IF_NONE_MATCH
Iterator to run through PDU options.
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
int coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data)
Retrieves the length and data pointer of specified PDU.
#define COAP_OPTION_SIZE1
static unsigned int print_content_format(unsigned int format_type, unsigned char *result, unsigned int buflen)
static int send_packet_count
void coap_set_show_pdu_output(int use_fprintf)
Defines the output mode for the coap_show_pdu() function.
#define COAP_OPTION_SIZE2
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0
#define COAP_STATIC_INLINE
void coap_show_tls_version(coap_log_t level)
Display the current (D)TLS library linked with and built for version.
#define COAP_MEDIATYPE_APPLICATION_XML
#define COAP_OPTION_BLOCK2
#define COAP_OPTION_LOCATION_PATH
unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int len)
Decodes multiple-length byte sequences.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
static const char * loglevels[]
#define COAP_MEDIATYPE_APPLICATION_SENML_EXI
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
coap_log_t
Pre-defined log levels akin to what is used in syslog with LOG_CIPHERS added.
#define COAP_OPTION_URI_PATH
const char * coap_package_version(void)
Get the library package version.
#define COAP_MEDIATYPE_TEXT_PLAIN
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1
coap_log_t coap_get_log_level(void)
Get the current logging level.
#define COAP_OPTION_URI_QUERY
#define coap_log(level,...)
Logging function.
static size_t strnlen(const char *s, size_t maxlen)
A length-safe strlen() fake.
union coap_address_t::@0 addr
#define COAP_OPTION_ACCEPT
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS
#define COAP_SIGNALING_RELEASE
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION
void coap_set_log_handler(coap_log_handler_t handler)
Add a custom log callback handler.
#define COAP_MEDIATYPE_APPLICATION_CBOR
#define COAP_SIGNALING_OPTION_HOLD_OFF
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
#define COAP_MEDIATYPE_APPLICATION_SENML_XML
#define prng(Buf, Length)
Fills Buf with Length bytes of random data.
uint8_t token_length
length of Token
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list...
uint64_t built_version
(D)TLS Built against Library Version
#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM
#define COAP_OPTION_URI_HOST
#define COAP_SIGNALING_ABORT
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET
static int use_fprintf_for_show_pdu
uint16_t tid
transaction id, if any, in regular host byte order
static int packet_loss_level
static struct packet_num_interval packet_loss_intervals[10]
Pulls together all the internal only header files.
COAP_STATIC_INLINE size_t print_timestamp(char *s, size_t len, coap_tick_t t)
static const char * msg_type_string(uint16_t t)
Returns a textual description of the message type t.
static const char * msg_option_string(uint8_t code, uint16_t option_type)
Returns a textual description of the option name.