1 /* debug.c -- debug utilities
3 * Copyright (C) 2011--2012 Olaf Bergmann <bergmann@tzi.org>
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include "dtls_config.h"
29 #if defined(HAVE_ASSERT_H) && !defined(assert)
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
47 static int maxlog = DTLS_LOG_WARN; /* default maximum log level */
49 const char *dtls_package_name() {
53 const char *dtls_package_version() {
54 return PACKAGE_VERSION;
58 dtls_get_log_level() {
63 dtls_set_log_level(log_t level) {
67 /* this array has the same order as the type log_t */
68 static char *loglevels[] = {
69 "EMRG", "ALRT", "CRIT", "WARN", "NOTE", "INFO", "DEBG"
75 print_timestamp(char *s, size_t len, time_t t) {
78 return strftime(s, len, "%b %d %H:%M:%S", tmp);
81 #else /* alternative implementation: just print the timestamp */
84 print_timestamp(char *s, size_t len, clock_time_t t) {
86 return snprintf(s, len, "%u.%03u",
87 (unsigned int)(t / CLOCK_SECOND),
88 (unsigned int)(t % CLOCK_SECOND));
89 #else /* HAVE_SNPRINTF */
90 /* @todo do manual conversion of timestamp */
92 #endif /* HAVE_SNPRINTF */
95 #endif /* HAVE_TIME_H */
98 * A length-safe strlen() fake.
100 * @param s The string to count characters != 0.
101 * @param maxlen The maximum length of @p s.
103 * @return The length of @p s.
106 dtls_strnlen(const char *s, size_t maxlen) {
108 while(*s++ && n < maxlen)
114 #define min(a,b) ((a) < (b) ? (a) : (b))
118 dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
119 #ifdef HAVE_ARPA_INET_H
120 const void *addrptr = NULL;
124 switch (addr->addr.sa.sa_family) {
126 if (len < INET_ADDRSTRLEN)
129 addrptr = &addr->addr.sin.sin_addr;
130 port = ntohs(addr->addr.sin.sin_port);
133 if (len < INET6_ADDRSTRLEN + 2)
138 addrptr = &addr->addr.sin6.sin6_addr;
139 port = ntohs(addr->addr.sin6.sin6_port);
143 memcpy(buf, "(unknown address type)", min(22, len));
147 if (inet_ntop(addr->addr.sa.sa_family, addrptr, p, len) == 0) {
148 perror("dsrv_print_addr");
152 p += dtls_strnlen(p, len);
154 if (addr->addr.sa.sa_family == AF_INET6) {
161 p += snprintf(p, buf + len - p + 1, ":%d", port);
164 #else /* HAVE_ARPA_INET_H */
167 # ifdef UIP_CONF_IPV6
169 const char hex[] = "0123456789ABCDEF";
176 for (i=0; i < 16; i += 2) {
180 *p++ = hex[(addr->addr.u8[i] & 0xf0) >> 4];
181 *p++ = hex[(addr->addr.u8[i] & 0x0f)];
182 *p++ = hex[(addr->addr.u8[i+1] & 0xf0) >> 4];
183 *p++ = hex[(addr->addr.u8[i+1] & 0x0f)];
186 # else /* UIP_CONF_IPV6 */
187 # warning "IPv4 network addresses will not be included in debug output"
191 # endif /* UIP_CONF_IPV6 */
192 if (buf + len - p < 6)
195 p += sprintf(p, ":%d", uip_htons(addr->port));
198 # else /* WITH_CONTIKI */
199 /* TODO: output addresses manually */
200 # warning "inet_ntop() not available, network addresses will not be included in debug output"
201 # endif /* WITH_CONTIKI */
208 dsrv_log(log_t level, char *format, ...) {
209 static char timebuf[32];
216 log_fd = level <= DTLS_LOG_CRIT ? stderr : stdout;
218 if (print_timestamp(timebuf,sizeof(timebuf), time(NULL)))
219 fprintf(log_fd, "%s ", timebuf);
221 if (level <= DTLS_LOG_DEBUG)
222 fprintf(log_fd, "%s ", loglevels[level]);
224 va_start(ap, format);
225 vfprintf(log_fd, format, ap);
229 #elif defined (HAVE_VPRINTF) /* WITH_CONTIKI */
231 dsrv_log(log_t level, char *format, ...) {
232 static char timebuf[32];
238 if (print_timestamp(timebuf,sizeof(timebuf), clock_time()))
239 PRINTF("%s ", timebuf);
241 if (level <= DTLS_LOG_DEBUG)
242 PRINTF("%s ", loglevels[level]);
244 va_start(ap, format);
248 #endif /* WITH_CONTIKI */
251 /** dumps packets in usual hexdump format */
252 void hexdump(const unsigned char *packet, int length) {
259 printf("%02X ", *packet++);
271 /** dump as narrow string of hex digits */
272 void dump(unsigned char *buf, size_t len) {
274 printf("%02x", *buf++);
277 void dtls_dsrv_log_addr(log_t level, const char *name, const session_t *addr)
282 len = dsrv_print_addr(addr, addrbuf, sizeof(addrbuf));
285 dsrv_log(level, "%s: %s\n", name, addrbuf);
290 dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, size_t length, int extend) {
291 static char timebuf[32];
298 log_fd = level <= DTLS_LOG_CRIT ? stderr : stdout;
300 if (print_timestamp(timebuf, sizeof(timebuf), time(NULL)))
301 fprintf(log_fd, "%s ", timebuf);
303 if (level <= DTLS_LOG_DEBUG)
304 fprintf(log_fd, "%s ", loglevels[level]);
307 fprintf(log_fd, "%s: (%zu bytes):\n", name, length);
311 fprintf(log_fd, "%08X ", n);
313 fprintf(log_fd, "%02X ", *buf++);
318 fprintf(log_fd, "\n");
320 fprintf(log_fd, " ");
324 fprintf(log_fd, "%s: (%zu bytes): ", name, length);
326 fprintf(log_fd, "%02X", *buf++);
328 fprintf(log_fd, "\n");
332 #else /* WITH_CONTIKI */
334 dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, size_t length, int extend) {
335 static char timebuf[32];
341 if (print_timestamp(timebuf,sizeof(timebuf), clock_time()))
342 PRINTF("%s ", timebuf);
344 if (level >= 0 && level <= DTLS_LOG_DEBUG)
345 PRINTF("%s ", loglevels[level]);
348 PRINTF("%s: (%zu bytes):\n", name, length);
354 PRINTF("%02X ", *buf++);
365 PRINTF("%s: (%zu bytes): ", name, length);
367 PRINTF("%02X", *buf++);
371 #endif /* WITH_CONTIKI */