libcoap  4.2.0
coap_debug.c
Go to the documentation of this file.
1 /* debug.c -- debug utilities
2  *
3  * Copyright (C) 2010--2012,2014--2019 Olaf Bergmann <bergmann@tzi.org> and others
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #include "coap_config.h"
10 
11 #if defined(HAVE_STRNLEN) && defined(__GNUC__) && !defined(_GNU_SOURCE)
12 #define _GNU_SOURCE 1
13 #endif
14 
15 #if defined(HAVE_ASSERT_H) && !defined(assert)
16 # include <assert.h>
17 #endif
18 
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <ctype.h>
23 
24 #ifdef HAVE_ARPA_INET_H
25 #include <arpa/inet.h>
26 #endif
27 #ifdef HAVE_WS2TCPIP_H
28 #include <ws2tcpip.h>
29 #endif
30 
31 #ifdef HAVE_TIME_H
32 #include <time.h>
33 #endif
34 
35 #include "libcoap.h"
36 #include "block.h"
37 #include "coap_debug.h"
38 #include "encode.h"
39 #include "net.h"
40 
41 #ifdef WITH_LWIP
42 # define fprintf(fd, ...) LWIP_PLATFORM_DIAG((__VA_ARGS__))
43 # define fflush(...)
44 #endif
45 
46 #ifdef WITH_CONTIKI
47 # ifndef DEBUG
48 # define DEBUG DEBUG_PRINT
49 # endif /* DEBUG */
50 #include "net/ip/uip-debug.h"
51 #endif
52 
53 static coap_log_t maxlog = LOG_WARNING; /* default maximum log level */
54 
55 static int use_fprintf_for_show_pdu = 1; /* non zero to output with fprintf */
56 
57 const char *coap_package_name(void) {
58  return PACKAGE_NAME;
59 }
60 
61 const char *coap_package_version(void) {
62  return PACKAGE_STRING;
63 }
64 
65 void
66 coap_set_show_pdu_output(int use_fprintf) {
67  use_fprintf_for_show_pdu = use_fprintf;
68 }
69 
72  return maxlog;
73 }
74 
75 void
77  maxlog = level;
78 }
79 
80 /* this array has the same order as the type log_t */
81 static const char *loglevels[] = {
82  "EMRG", "ALRT", "CRIT", "ERR ", "WARN", "NOTE", "INFO", "DEBG"
83 };
84 
85 #ifdef HAVE_TIME_H
86 
87 COAP_STATIC_INLINE size_t
88 print_timestamp(char *s, size_t len, coap_tick_t t) {
89  struct tm *tmp;
90  time_t now = coap_ticks_to_rt(t);
91  tmp = localtime(&now);
92  return strftime(s, len, "%b %d %H:%M:%S", tmp);
93 }
94 
95 #else /* alternative implementation: just print the timestamp */
96 
97 COAP_STATIC_INLINE size_t
98 print_timestamp(char *s, size_t len, coap_tick_t t) {
99 #ifdef HAVE_SNPRINTF
100  return snprintf(s, len, "%u.%03u",
101  (unsigned int)coap_ticks_to_rt(t),
102  (unsigned int)(t % COAP_TICKS_PER_SECOND));
103 #else /* HAVE_SNPRINTF */
104  /* @todo do manual conversion of timestamp */
105  return 0;
106 #endif /* HAVE_SNPRINTF */
107 }
108 
109 #endif /* HAVE_TIME_H */
110 
111 #ifndef HAVE_STRNLEN
112 
120 static inline size_t
121 strnlen(const char *s, size_t maxlen) {
122  size_t n = 0;
123  while(*s++ && n < maxlen)
124  ++n;
125  return n;
126 }
127 #endif /* HAVE_STRNLEN */
128 
129 static size_t
130 print_readable( const uint8_t *data, size_t len,
131  unsigned char *result, size_t buflen, int encode_always ) {
132  const uint8_t hex[] = "0123456789ABCDEF";
133  size_t cnt = 0;
134  assert(data || len == 0);
135 
136  if (buflen == 0) { /* there is nothing we can do here but return */
137  return 0;
138  }
139 
140  while (len) {
141  if (!encode_always && isprint(*data)) {
142  if (cnt+1 < buflen) { /* keep one byte for terminating zero */
143  *result++ = *data;
144  ++cnt;
145  } else {
146  break;
147  }
148  } else {
149  if (cnt+4 < buflen) { /* keep one byte for terminating zero */
150  *result++ = '\\';
151  *result++ = 'x';
152  *result++ = hex[(*data & 0xf0) >> 4];
153  *result++ = hex[*data & 0x0f];
154  cnt += 4;
155  } else
156  break;
157  }
158 
159  ++data; --len;
160  }
161 
162  *result = '\0'; /* add a terminating zero */
163  return cnt;
164 }
165 
166 #ifndef min
167 #define min(a,b) ((a) < (b) ? (a) : (b))
168 #endif
169 
170 size_t
171 coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len) {
172 #if defined( HAVE_ARPA_INET_H ) || defined( HAVE_WS2TCPIP_H )
173  const void *addrptr = NULL;
174  in_port_t port;
175  unsigned char *p = buf;
176 
177  switch (addr->addr.sa.sa_family) {
178  case AF_INET:
179  addrptr = &addr->addr.sin.sin_addr;
180  port = ntohs(addr->addr.sin.sin_port);
181  break;
182  case AF_INET6:
183  if (len < 7) /* do not proceed if buffer is even too short for [::]:0 */
184  return 0;
185 
186  *p++ = '[';
187 
188  addrptr = &addr->addr.sin6.sin6_addr;
189  port = ntohs(addr->addr.sin6.sin6_port);
190 
191  break;
192  default:
193  memcpy(buf, "(unknown address type)", min(22, len));
194  return min(22, len);
195  }
196 
197  /* Cast needed for Windows, since it doesn't have the correct API signature. */
198  if (inet_ntop(addr->addr.sa.sa_family, addrptr, (char *)p, len) == 0) {
199  perror("coap_print_addr");
200  return 0;
201  }
202 
203  p += strnlen((char *)p, len);
204 
205  if (addr->addr.sa.sa_family == AF_INET6) {
206  if (p < buf + len) {
207  *p++ = ']';
208  } else
209  return 0;
210  }
211 
212  p += snprintf((char *)p, buf + len - p + 1, ":%d", port);
213 
214  return buf + len - p;
215 #else /* HAVE_ARPA_INET_H */
216 # if WITH_CONTIKI
217  unsigned char *p = buf;
218  uint8_t i;
219 # if NETSTACK_CONF_WITH_IPV6
220  const uint8_t hex[] = "0123456789ABCDEF";
221 
222  if (len < 41)
223  return 0;
224 
225  *p++ = '[';
226 
227  for (i=0; i < 16; i += 2) {
228  if (i) {
229  *p++ = ':';
230  }
231  *p++ = hex[(addr->addr.u8[i] & 0xf0) >> 4];
232  *p++ = hex[(addr->addr.u8[i] & 0x0f)];
233  *p++ = hex[(addr->addr.u8[i+1] & 0xf0) >> 4];
234  *p++ = hex[(addr->addr.u8[i+1] & 0x0f)];
235  }
236  *p++ = ']';
237 # else /* WITH_UIP6 */
238 # warning "IPv4 network addresses will not be included in debug output"
239 
240  if (len < 21)
241  return 0;
242 # endif /* WITH_UIP6 */
243  if (buf + len - p < 6)
244  return 0;
245 
246 #ifdef HAVE_SNPRINTF
247  p += snprintf((char *)p, buf + len - p + 1, ":%d", uip_htons(addr->port));
248 #else /* HAVE_SNPRINTF */
249  /* @todo manual conversion of port number */
250 #endif /* HAVE_SNPRINTF */
251 
252  return p - buf;
253 # else /* WITH_CONTIKI */
254  /* TODO: output addresses manually */
255 # warning "inet_ntop() not available, network addresses will not be included in debug output"
256 # endif /* WITH_CONTIKI */
257  return 0;
258 #endif
259 }
260 
261 #ifdef WITH_CONTIKI
262 # define fprintf(fd, ...) PRINTF(__VA_ARGS__)
263 # define fflush(...)
264 
265 # ifdef HAVE_VPRINTF
266 # define vfprintf(fd, ...) vprintf(__VA_ARGS__)
267 # else /* HAVE_VPRINTF */
268 # define vfprintf(fd, ...) PRINTF(__VA_ARGS__)
269 # endif /* HAVE_VPRINTF */
270 #endif /* WITH_CONTIKI */
271 
273 static const char *
274 msg_type_string(uint16_t t) {
275  static const char *types[] = { "CON", "NON", "ACK", "RST", "???" };
276 
277  return types[min(t, sizeof(types)/sizeof(char *) - 1)];
278 }
279 
281 static const char *
282 msg_code_string(uint16_t c) {
283  static const char *methods[] = { "0.00", "GET", "POST", "PUT", "DELETE",
284  "FETCH", "PATCH", "iPATCH" };
285  static const char *signals[] = { "7.00", "CSM", "Ping", "Pong", "Release",
286  "Abort" };
287  static char buf[5];
288 
289  if (c < sizeof(methods)/sizeof(const char *)) {
290  return methods[c];
291  } else if (c >= 224 && c - 224 < (int)(sizeof(signals)/sizeof(const char *))) {
292  return signals[c-224];
293  } else {
294  snprintf(buf, sizeof(buf), "%u.%02u", c >> 5, c & 0x1f);
295  return buf;
296  }
297 }
298 
300 static const char *
301 msg_option_string(uint8_t code, uint16_t option_type) {
302  struct option_desc_t {
303  uint16_t type;
304  const char *name;
305  };
306 
307  static struct option_desc_t options[] = {
308  { COAP_OPTION_IF_MATCH, "If-Match" },
309  { COAP_OPTION_URI_HOST, "Uri-Host" },
310  { COAP_OPTION_ETAG, "ETag" },
311  { COAP_OPTION_IF_NONE_MATCH, "If-None-Match" },
312  { COAP_OPTION_OBSERVE, "Observe" },
313  { COAP_OPTION_URI_PORT, "Uri-Port" },
314  { COAP_OPTION_LOCATION_PATH, "Location-Path" },
315  { COAP_OPTION_URI_PATH, "Uri-Path" },
316  { COAP_OPTION_CONTENT_FORMAT, "Content-Format" },
317  { COAP_OPTION_MAXAGE, "Max-Age" },
318  { COAP_OPTION_URI_QUERY, "Uri-Query" },
319  { COAP_OPTION_ACCEPT, "Accept" },
320  { COAP_OPTION_LOCATION_QUERY, "Location-Query" },
321  { COAP_OPTION_BLOCK2, "Block2" },
322  { COAP_OPTION_BLOCK1, "Block1" },
323  { COAP_OPTION_PROXY_URI, "Proxy-Uri" },
324  { COAP_OPTION_PROXY_SCHEME, "Proxy-Scheme" },
325  { COAP_OPTION_SIZE1, "Size1" },
326  { COAP_OPTION_SIZE2, "Size2" },
327  { COAP_OPTION_NORESPONSE, "No-Response" }
328  };
329 
330  static struct option_desc_t options_csm[] = {
331  { COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE, "Max-Message-Size" },
332  { COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER, "Block-wise-Transfer" }
333  };
334 
335  static struct option_desc_t options_pingpong[] = {
336  { COAP_SIGNALING_OPTION_CUSTODY, "Custody" }
337  };
338 
339  static struct option_desc_t options_release[] = {
340  { COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS, "Alternative-Address" },
341  { COAP_SIGNALING_OPTION_HOLD_OFF, "Hold-Off" }
342  };
343 
344  static struct option_desc_t options_abort[] = {
345  { COAP_SIGNALING_OPTION_BAD_CSM_OPTION, "Bad-CSM-Option" }
346  };
347 
348  static char buf[6];
349  size_t i;
350 
351  if (code == COAP_SIGNALING_CSM) {
352  for (i = 0; i < sizeof(options_csm)/sizeof(struct option_desc_t); i++) {
353  if (option_type == options_csm[i].type) {
354  return options_csm[i].name;
355  }
356  }
357  } else if (code == COAP_SIGNALING_PING || code == COAP_SIGNALING_PONG) {
358  for (i = 0; i < sizeof(options_pingpong)/sizeof(struct option_desc_t); i++) {
359  if (option_type == options_pingpong[i].type) {
360  return options_pingpong[i].name;
361  }
362  }
363  } else if (code == COAP_SIGNALING_RELEASE) {
364  for (i = 0; i < sizeof(options_release)/sizeof(struct option_desc_t); i++) {
365  if (option_type == options_release[i].type) {
366  return options_release[i].name;
367  }
368  }
369  } else if (code == COAP_SIGNALING_ABORT) {
370  for (i = 0; i < sizeof(options_abort)/sizeof(struct option_desc_t); i++) {
371  if (option_type == options_abort[i].type) {
372  return options_abort[i].name;
373  }
374  }
375  } else {
376  /* search option_type in list of known options */
377  for (i = 0; i < sizeof(options)/sizeof(struct option_desc_t); i++) {
378  if (option_type == options[i].type) {
379  return options[i].name;
380  }
381  }
382  }
383  /* unknown option type, just print to buf */
384  snprintf(buf, sizeof(buf), "%u", option_type);
385  return buf;
386 }
387 
388 static unsigned int
389 print_content_format(unsigned int format_type,
390  unsigned char *result, unsigned int buflen) {
391  struct desc_t {
392  unsigned int type;
393  const char *name;
394  };
395 
396  static struct desc_t formats[] = {
397  { COAP_MEDIATYPE_TEXT_PLAIN, "text/plain" },
398  { COAP_MEDIATYPE_APPLICATION_LINK_FORMAT, "application/link-format" },
399  { COAP_MEDIATYPE_APPLICATION_XML, "application/xml" },
400  { COAP_MEDIATYPE_APPLICATION_OCTET_STREAM, "application/octet-stream" },
401  { COAP_MEDIATYPE_APPLICATION_EXI, "application/exi" },
402  { COAP_MEDIATYPE_APPLICATION_JSON, "application/json" },
403  { COAP_MEDIATYPE_APPLICATION_CBOR, "application/cbor" },
404  { COAP_MEDIATYPE_APPLICATION_COSE_SIGN, "application/cose; cose-type=\"cose-sign\"" },
405  { COAP_MEDIATYPE_APPLICATION_COSE_SIGN1, "application/cose; cose-type=\"cose-sign1\"" },
406  { COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT, "application/cose; cose-type=\"cose-encrypt\"" },
407  { COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0, "application/cose; cose-type=\"cose-encrypt0\"" },
408  { COAP_MEDIATYPE_APPLICATION_COSE_MAC, "application/cose; cose-type=\"cose-mac\"" },
409  { COAP_MEDIATYPE_APPLICATION_COSE_MAC0, "application/cose; cose-type=\"cose-mac0\"" },
410  { COAP_MEDIATYPE_APPLICATION_COSE_KEY, "application/cose-key" },
411  { COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET, "application/cose-key-set" },
412  { COAP_MEDIATYPE_APPLICATION_SENML_JSON, "application/senml+json" },
413  { COAP_MEDIATYPE_APPLICATION_SENSML_JSON, "application/sensml+json" },
414  { COAP_MEDIATYPE_APPLICATION_SENML_CBOR, "application/senml+cbor" },
415  { COAP_MEDIATYPE_APPLICATION_SENSML_CBOR, "application/sensml+cbor" },
416  { COAP_MEDIATYPE_APPLICATION_SENML_EXI, "application/senml-exi" },
417  { COAP_MEDIATYPE_APPLICATION_SENSML_EXI, "application/sensml-exi" },
418  { COAP_MEDIATYPE_APPLICATION_SENML_XML, "application/senml+xml" },
419  { COAP_MEDIATYPE_APPLICATION_SENSML_XML, "application/sensml+xml" },
420  { 75, "application/dcaf+cbor" }
421  };
422 
423  size_t i;
424 
425  /* search format_type in list of known content formats */
426  for (i = 0; i < sizeof(formats)/sizeof(struct desc_t); i++) {
427  if (format_type == formats[i].type) {
428  return snprintf((char *)result, buflen, "%s", formats[i].name);
429  }
430  }
431 
432  /* unknown content format, just print numeric value to buf */
433  return snprintf((char *)result, buflen, "%d", format_type);
434 }
435 
442 is_binary(int content_format) {
443  return !(content_format == -1 ||
444  content_format == COAP_MEDIATYPE_TEXT_PLAIN ||
445  content_format == COAP_MEDIATYPE_APPLICATION_LINK_FORMAT ||
446  content_format == COAP_MEDIATYPE_APPLICATION_XML ||
447  content_format == COAP_MEDIATYPE_APPLICATION_JSON);
448 }
449 
450 #define COAP_DO_SHOW_OUTPUT_LINE \
451  do { \
452  if (use_fprintf_for_show_pdu) { \
453  fprintf(COAP_DEBUG_FD, "%s", outbuf); \
454  } \
455  else { \
456  coap_log(level, "%s", outbuf); \
457  } \
458  } while (0)
459 
460 void
461 coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
462  unsigned char buf[1024]; /* need some space for output creation */
463  size_t buf_len = 0; /* takes the number of bytes written to buf */
464  int encode = 0, have_options = 0, i;
465  coap_opt_iterator_t opt_iter;
466  coap_opt_t *option;
467  int content_format = -1;
468  size_t data_len;
469  unsigned char *data;
470  char outbuf[COAP_DEBUG_BUF_SIZE];
471  int outbuflen = 0;
472 
473  /* Save time if not needed */
474  if (level > coap_get_log_level())
475  return;
476 
477  snprintf(outbuf, sizeof(outbuf), "v:%d t:%s c:%s i:%04x {",
479  msg_code_string(pdu->code), pdu->tid);
480 
481  for (i = 0; i < pdu->token_length; i++) {
482  outbuflen = strlen(outbuf);
483  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
484  "%02x", pdu->token[i]);
485  }
486  outbuflen = strlen(outbuf);
487  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "}");
488 
489  /* show options, if any */
490  coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
491 
492  outbuflen = strlen(outbuf);
493  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " [");
494  while ((option = coap_option_next(&opt_iter))) {
495  if (!have_options) {
496  have_options = 1;
497  } else {
498  outbuflen = strlen(outbuf);
499  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ",");
500  }
501 
502  if (pdu->code == COAP_SIGNALING_CSM) switch(opt_iter.type) {
504  buf_len = snprintf((char *)buf, sizeof(buf), "%u",
506  coap_opt_length(option)));
507  break;
508  default:
509  buf_len = 0;
510  break;
511  } else if (pdu->code == COAP_SIGNALING_PING
512  || pdu->code == COAP_SIGNALING_PONG) {
513  buf_len = 0;
514  } else if (pdu->code == COAP_SIGNALING_RELEASE) switch(opt_iter.type) {
516  buf_len = print_readable(coap_opt_value(option),
517  coap_opt_length(option),
518  buf, sizeof(buf), 0);
519  break;
521  buf_len = snprintf((char *)buf, sizeof(buf), "%u",
523  coap_opt_length(option)));
524  break;
525  default:
526  buf_len = 0;
527  break;
528  } else if (pdu->code == COAP_SIGNALING_ABORT) switch(opt_iter.type) {
530  buf_len = snprintf((char *)buf, sizeof(buf), "%u",
532  coap_opt_length(option)));
533  break;
534  default:
535  buf_len = 0;
536  break;
537  } else switch (opt_iter.type) {
539  content_format = (int)coap_decode_var_bytes(coap_opt_value(option),
540  coap_opt_length(option));
541 
542  buf_len = print_content_format(content_format, buf, sizeof(buf));
543  break;
544 
545  case COAP_OPTION_BLOCK1:
546  case COAP_OPTION_BLOCK2:
547  /* split block option into number/more/size where more is the
548  * letter M if set, the _ otherwise */
549  buf_len = snprintf((char *)buf, sizeof(buf), "%u/%c/%u",
550  coap_opt_block_num(option), /* block number */
551  COAP_OPT_BLOCK_MORE(option) ? 'M' : '_', /* M bit */
552  (1 << (COAP_OPT_BLOCK_SZX(option) + 4))); /* block size */
553 
554  break;
555 
557  case COAP_OPTION_MAXAGE:
558  case COAP_OPTION_OBSERVE:
559  case COAP_OPTION_SIZE1:
560  case COAP_OPTION_SIZE2:
561  /* show values as unsigned decimal value */
562  buf_len = snprintf((char *)buf, sizeof(buf), "%u",
564  coap_opt_length(option)));
565  break;
566 
567  default:
568  /* generic output function for all other option types */
569  if (opt_iter.type == COAP_OPTION_URI_PATH ||
570  opt_iter.type == COAP_OPTION_PROXY_URI ||
571  opt_iter.type == COAP_OPTION_URI_HOST ||
572  opt_iter.type == COAP_OPTION_LOCATION_PATH ||
573  opt_iter.type == COAP_OPTION_LOCATION_QUERY ||
574  opt_iter.type == COAP_OPTION_URI_QUERY) {
575  encode = 0;
576  } else {
577  encode = 1;
578  }
579 
580  buf_len = print_readable(coap_opt_value(option),
581  coap_opt_length(option),
582  buf, sizeof(buf), encode);
583  }
584 
585  outbuflen = strlen(outbuf);
586  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
587  " %s:%.*s", msg_option_string(pdu->code, opt_iter.type),
588  (int)buf_len, buf);
589  }
590 
591  outbuflen = strlen(outbuf);
592  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " ]");
593 
594  if (coap_get_data(pdu, &data_len, &data)) {
595 
596  outbuflen = strlen(outbuf);
597  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " :: ");
598 
599  if (is_binary(content_format)) {
600  int keep_data_len = data_len;
601  uint8_t *keep_data = data;
602 
603  outbuflen = strlen(outbuf);
604  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
605  "binary data length %zu\n", data_len);
607  /*
608  * Output hex dump of binary data as a continuous entry
609  */
610  outbuf[0] = '\000';
611  snprintf(outbuf, sizeof(outbuf), "<<");
612  while (data_len--) {
613  outbuflen = strlen(outbuf);
614  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
615  "%02x", *data++);
616  }
617  outbuflen = strlen(outbuf);
618  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ">>");
619  data_len = keep_data_len;
620  data = keep_data;
621  outbuflen = strlen(outbuf);
622  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n");
624  /*
625  * Output ascii readable (if possible), immediately under the
626  * hex value of the character output above to help binary debugging
627  */
628  outbuf[0] = '\000';
629  snprintf(outbuf, sizeof(outbuf), "<<");
630  while (data_len--) {
631  outbuflen = strlen(outbuf);
632  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
633  "%c ", isprint (*data) ? *data : '.');
634  data++;
635  }
636  outbuflen = strlen(outbuf);
637  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ">>");
638  } else {
639  if (print_readable(data, data_len, buf, sizeof(buf), 0)) {
640  outbuflen = strlen(outbuf);
641  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "'%s'", buf);
642  }
643  }
644  }
645 
646  outbuflen = strlen(outbuf);
647  snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n");
649 }
650 
652 {
653  char buffer[64];
654  coap_string_tls_version(buffer, sizeof(buffer));
655  coap_log(level, "%s\n", buffer);
656 }
657 
658 char *coap_string_tls_version(char *buffer, size_t bufsize)
659 {
661  char beta[8];
662  char sub[2];
663  char b_beta[8];
664  char b_sub[2];
665 
666  switch (tls_version->type) {
668  snprintf(buffer, bufsize, "TLS Library: None");
669  break;
671  snprintf(buffer, bufsize, "TLS Library: TinyDTLS - runtime %lu.%lu.%lu, "
672  "libcoap built for %lu.%lu.%lu",
673  (unsigned long)(tls_version->version >> 16),
674  (unsigned long)((tls_version->version >> 8) & 0xff),
675  (unsigned long)(tls_version->version & 0xff),
676  (unsigned long)(tls_version->built_version >> 16),
677  (unsigned long)((tls_version->built_version >> 8) & 0xff),
678  (unsigned long)(tls_version->built_version & 0xff));
679  break;
681  switch (tls_version->version &0xf) {
682  case 0:
683  strcpy(beta, "-dev");
684  break;
685  case 0xf:
686  strcpy(beta, "");
687  break;
688  default:
689  strcpy(beta, "-beta");
690  beta[5] = (tls_version->version &0xf) + '0';
691  beta[6] = '\000';
692  break;
693  }
694  sub[0] = ((tls_version->version >> 4) & 0xff) ?
695  ((tls_version->version >> 4) & 0xff) + 'a' -1 : '\000';
696  sub[1] = '\000';
697  switch (tls_version->built_version &0xf) {
698  case 0:
699  strcpy(b_beta, "-dev");
700  break;
701  case 0xf:
702  strcpy(b_beta, "");
703  break;
704  default:
705  strcpy(b_beta, "-beta");
706  b_beta[5] = (tls_version->built_version &0xf) + '0';
707  b_beta[6] = '\000';
708  break;
709  }
710  b_sub[0] = ((tls_version->built_version >> 4) & 0xff) ?
711  ((tls_version->built_version >> 4) & 0xff) + 'a' -1 : '\000';
712  b_sub[1] = '\000';
713  snprintf(buffer, bufsize, "TLS Library: OpenSSL - runtime "
714  "%lu.%lu.%lu%s%s, libcoap built for %lu.%lu.%lu%s%s",
715  (unsigned long)(tls_version->version >> 28),
716  (unsigned long)((tls_version->version >> 20) & 0xff),
717  (unsigned long)((tls_version->version >> 12) & 0xff), sub, beta,
718  (unsigned long)(tls_version->built_version >> 28),
719  (unsigned long)((tls_version->built_version >> 20) & 0xff),
720  (unsigned long)((tls_version->built_version >> 12) & 0xff),
721  b_sub, b_beta);
722  break;
724  snprintf(buffer, bufsize, "TLS Library: GnuTLS - runtime %lu.%lu.%lu, "
725  "libcoap built for %lu.%lu.%lu",
726  (unsigned long)(tls_version->version >> 16),
727  (unsigned long)((tls_version->version >> 8) & 0xff),
728  (unsigned long)(tls_version->version & 0xff),
729  (unsigned long)(tls_version->built_version >> 16),
730  (unsigned long)((tls_version->built_version >> 8) & 0xff),
731  (unsigned long)(tls_version->built_version & 0xff));
732  break;
733  default:
734  snprintf(buffer, bufsize, "Library type %d unknown", tls_version->type);
735  break;
736  }
737  return buffer;
738 }
739 
741 
743  log_handler = handler;
744 }
745 
746 void
747 coap_log_impl(coap_log_t level, const char *format, ...) {
748 
749  if (maxlog < level)
750  return;
751 
752  if (log_handler) {
753 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
754  char message[128];
755 #else
756  char message[8 + 1024 * 2]; /* O/H + Max packet payload size * 2 */
757 #endif
758  va_list ap;
759  va_start(ap, format);
760  vsnprintf( message, sizeof(message), format, ap);
761  va_end(ap);
762  log_handler(level, message);
763  } else {
764  char timebuf[32];
765  coap_tick_t now;
766  va_list ap;
767  FILE *log_fd;
768 
769  log_fd = level <= LOG_CRIT ? COAP_ERR_FD : COAP_DEBUG_FD;
770 
771  coap_ticks(&now);
772  if (print_timestamp(timebuf,sizeof(timebuf), now))
773  fprintf(log_fd, "%s ", timebuf);
774 
775  if (level <= LOG_DEBUG)
776  fprintf(log_fd, "%s ", loglevels[level]);
777 
778  va_start(ap, format);
779  vfprintf(log_fd, format, ap);
780  va_end(ap);
781  fflush(log_fd);
782  }
783 }
784 
785 static struct packet_num_interval {
786  int start;
787  int end;
790 static int packet_loss_level = 0;
791 static int send_packet_count = 0;
792 
793 int coap_debug_set_packet_loss(const char *loss_level) {
794  const char *p = loss_level;
795  char *end = NULL;
796  int n = (int)strtol(p, &end, 10), i = 0;
797  if (end == p || n < 0)
798  return 0;
799  if (*end == '%') {
800  if (n > 100)
801  n = 100;
802  packet_loss_level = n * 65536 / 100;
803  coap_log(LOG_DEBUG, "packet loss level set to %d%%\n", n);
804  } else {
805  if (n <= 0)
806  return 0;
807  while (i < 10) {
809  if (*end == '-') {
810  p = end + 1;
811  n = (int)strtol(p, &end, 10);
812  if (end == p || n <= 0)
813  return 0;
814  }
815  packet_loss_intervals[i++].end = n;
816  if (*end == 0)
817  break;
818  if (*end != ',')
819  return 0;
820  p = end + 1;
821  n = (int)strtol(p, &end, 10);
822  if (end == p || n <= 0)
823  return 0;
824  }
825  if (i == 10)
826  return 0;
828  }
829  send_packet_count = 0;
830  return 1;
831 }
832 
835  if (num_packet_loss_intervals > 0) {
836  int i;
837  for (i = 0; i < num_packet_loss_intervals; i++) {
840  return 0;
841  }
842  }
843  if ( packet_loss_level > 0 ) {
844  uint16_t r = 0;
845  prng( (uint8_t*)&r, 2 );
846  if ( r < packet_loss_level )
847  return 0;
848  }
849  return 1;
850 }
uint8_t type
message type
Definition: pdu.h:288
uint8_t code
request method (value 1–10) or response code (value 40-255)
Definition: pdu.h:289
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition: coap_debug.c:461
#define COAP_OPTION_IF_MATCH
Definition: pdu.h:92
#define COAP_MEDIATYPE_APPLICATION_SENSML_JSON
Definition: pdu.h:221
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:25
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.
Definition: block.c:27
#define COAP_SIGNALING_PING
Definition: pdu.h:181
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.
Definition: coap_debug.c:442
#define COAP_SIGNALING_CSM
Definition: pdu.h:180
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC0
Definition: pdu.h:214
#define COAP_OPTION_PROXY_URI
Definition: pdu.h:106
#define COAP_OPTION_CONTENT_FORMAT
Definition: pdu.h:99
struct sockaddr_in6 sin6
Definition: address.h:67
struct sockaddr_in sin
Definition: address.h:66
static const char * msg_code_string(uint16_t c)
Returns a textual description of the method or response code.
Definition: coap_debug.c:282
#define COAP_OPTION_NORESPONSE
Definition: pdu.h:122
multi-purpose address abstraction
Definition: address.h:62
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: option.h:122
#define COAP_TLS_LIBRARY_OPENSSL
Using OpenSSL library.
Definition: coap_dtls.h:40
const char * coap_package_name(void)
Get the library package name.
Definition: coap_debug.c:57
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition: coap_debug.c:171
#define COAP_OPTION_OBSERVE
Definition: pdu.h:112
#define COAP_OPTION_ETAG
Definition: pdu.h:94
#define COAP_DEBUG_BUF_SIZE
Definition: pdu.h:60
#define COAP_MEDIATYPE_APPLICATION_JSON
Definition: pdu.h:205
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_OPTION_BLOCK1
Definition: pdu.h:118
int coap_debug_set_packet_loss(const char *loss_level)
Set the packet loss level for testing.
Definition: coap_debug.c:793
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition: pdu.h:188
#define COAP_OPTION_MAXAGE
Definition: pdu.h:101
Debug.
Definition: coap_debug.h:49
uint64_t version
(D)TLS runtime Library Version
Definition: coap_dtls.h:48
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:157
#define COAP_MEDIATYPE_APPLICATION_SENSML_EXI
Definition: pdu.h:225
#define COAP_DO_SHOW_OUTPUT_LINE
Definition: coap_debug.c:450
#define COAP_OPTION_LOCATION_QUERY
Definition: pdu.h:104
#define COAP_OPTION_PROXY_SCHEME
Definition: pdu.h:107
#define min(a, b)
Definition: coap_debug.c:167
uint16_t type
decoded option type
Definition: option.h:239
#define COAP_MEDIATYPE_APPLICATION_SENSML_XML
Definition: pdu.h:227
#define COAP_MEDIATYPE_APPLICATION_SENML_CBOR
Definition: pdu.h:222
#define COAP_MEDIATYPE_APPLICATION_SENML_JSON
Definition: pdu.h:220
#define COAP_MEDIATYPE_APPLICATION_EXI
Definition: pdu.h:204
static coap_log_handler_t log_handler
Definition: coap_debug.c:740
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.
Definition: coap_debug.c:658
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
Definition: coap_debug.c:833
void coap_set_log_level(coap_log_t level)
Sets the log level to the specified value.
Definition: coap_debug.c:76
static coap_log_t maxlog
Definition: coap_debug.c:53
#define COAP_DEFAULT_VERSION
Definition: pdu.h:64
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)...
Definition: coap_debug.c:747
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:100
#define COAP_DEBUG_FD
Used for output for LOG_DEBUG to LOG_ERR.
Definition: coap_debug.h:23
void(* coap_log_handler_t)(coap_log_t level, const char *message)
Logging call-back handler definition.
Definition: coap_debug.h:73
coap_tls_version_t * coap_get_tls_library_version(void)
Determine the type and version of the underlying (D)TLS library.
Definition: coap_notls.c:33
#define COAP_SIGNALING_PONG
Definition: pdu.h:182
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
int type
Library type.
Definition: coap_dtls.h:49
uint16_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:249
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition: pdu.h:187
#define COAP_SIGNALING_OPTION_CUSTODY
Definition: pdu.h:190
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN
Definition: pdu.h:209
#define COAP_OPTION_URI_PORT
Definition: pdu.h:96
Warning.
Definition: coap_debug.h:46
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:85
#define assert(...)
Definition: mem.c:18
#define COAP_ERR_FD
Used for output for LOG_CRIT to LOG_EMERG.
Definition: coap_debug.h:30
#define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR
Definition: pdu.h:223
static int num_packet_loss_intervals
Definition: coap_debug.c:789
#define COAP_TLS_LIBRARY_TINYDTLS
Using TinyDTLS library.
Definition: coap_dtls.h:39
uint8_t * token
first byte of token, if any, or options
Definition: pdu.h:298
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.
Definition: coap_dtls.h:47
static size_t print_readable(const uint8_t *data, size_t len, unsigned char *result, size_t buflen, int encode_always)
Definition: coap_debug.c:130
#define COAP_OPTION_IF_NONE_MATCH
Definition: pdu.h:95
Iterator to run through PDU options.
Definition: option.h:237
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
Definition: pdu.h:200
int coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition: pdu.c:332
#define COAP_OPTION_SIZE1
Definition: pdu.h:108
static unsigned int print_content_format(unsigned int format_type, unsigned char *result, unsigned int buflen)
Definition: coap_debug.c:389
static int send_packet_count
Definition: coap_debug.c:791
void coap_set_show_pdu_output(int use_fprintf)
Defines the output mode for the coap_show_pdu() function.
Definition: coap_debug.c:66
#define COAP_OPTION_SIZE2
Definition: pdu.h:105
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0
Definition: pdu.h:212
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
void coap_show_tls_version(coap_log_t level)
Display the current (D)TLS library linked with and built for version.
Definition: coap_debug.c:651
#define COAP_TLS_LIBRARY_GNUTLS
Using GnuTLS library.
Definition: coap_dtls.h:41
#define COAP_MEDIATYPE_APPLICATION_XML
Definition: pdu.h:201
#define COAP_OPTION_BLOCK2
Definition: pdu.h:117
#define COAP_OPTION_LOCATION_PATH
Definition: pdu.h:97
unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int len)
Decodes multiple-length byte sequences.
Definition: encode.c:36
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:286
static const char * loglevels[]
Definition: coap_debug.c:81
#define COAP_MEDIATYPE_APPLICATION_SENML_EXI
Definition: pdu.h:224
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
Definition: block.h:55
coap_log_t
Pre-defined log levels akin to what is used in syslog.
Definition: coap_debug.h:41
#define COAP_OPTION_URI_PATH
Definition: pdu.h:98
const char * coap_package_version(void)
Get the library package version.
Definition: coap_debug.c:61
#define COAP_TLS_LIBRARY_NOTLS
No DTLS library.
Definition: coap_dtls.h:38
#define COAP_MEDIATYPE_TEXT_PLAIN
Definition: pdu.h:199
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1
Definition: pdu.h:210
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:71
#define COAP_OPTION_URI_QUERY
Definition: pdu.h:102
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:122
static size_t strnlen(const char *s, size_t maxlen)
A length-safe strlen() fake.
Definition: coap_debug.c:121
union coap_address_t::@0 addr
#define COAP_OPTION_ACCEPT
Definition: pdu.h:103
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS
Definition: pdu.h:192
#define COAP_SIGNALING_RELEASE
Definition: pdu.h:183
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION
Definition: pdu.h:195
void coap_set_log_handler(coap_log_handler_t handler)
Add a custom log callback handler.
Definition: coap_debug.c:742
#define COAP_MEDIATYPE_APPLICATION_CBOR
Definition: pdu.h:206
unsigned char uint8_t
Definition: uthash.h:79
#define COAP_SIGNALING_OPTION_HOLD_OFF
Definition: pdu.h:193
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY
Definition: pdu.h:216
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
Definition: block.h:51
#define COAP_MEDIATYPE_APPLICATION_SENML_XML
Definition: pdu.h:226
#define prng(Buf, Length)
Fills Buf with Length bytes of random data.
Definition: prng.h:112
uint8_t token_length
length of Token
Definition: pdu.h:292
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT
Definition: pdu.h:211
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC
Definition: pdu.h:213
Critical.
Definition: coap_debug.h:44
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&#39;s option list...
Definition: option.c:121
uint64_t built_version
(D)TLS Built against Library Version
Definition: coap_dtls.h:50
#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM
Definition: pdu.h:202
#define COAP_OPTION_URI_HOST
Definition: pdu.h:93
#define COAP_SIGNALING_ABORT
Definition: pdu.h:184
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET
Definition: pdu.h:217
static int use_fprintf_for_show_pdu
Definition: coap_debug.c:55
struct sockaddr sa
Definition: address.h:65
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:293
static int packet_loss_level
Definition: coap_debug.c:790
static struct packet_num_interval packet_loss_intervals[10]
COAP_STATIC_INLINE size_t print_timestamp(char *s, size_t len, coap_tick_t t)
Definition: coap_debug.c:98
static const char * msg_type_string(uint16_t t)
Returns a textual description of the message type t.
Definition: coap_debug.c:274
static const char * msg_option_string(uint8_t code, uint16_t option_type)
Returns a textual description of the option name.
Definition: coap_debug.c:301