mbed port of tinydtls

Committer:
ashleymills
Date:
Fri Oct 11 08:46:21 2013 +0000
Revision:
1:bc8a649bad13
Parent:
0:04990d454f45
Cleaned up all the debug stuff I added finding the hash table bug.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:04990d454f45 1 /* debug.c -- debug utilities
ashleymills 0:04990d454f45 2 *
ashleymills 0:04990d454f45 3 * Copyright (C) 2011--2012 Olaf Bergmann <bergmann@tzi.org>
ashleymills 0:04990d454f45 4 *
ashleymills 0:04990d454f45 5 * Permission is hereby granted, free of charge, to any person
ashleymills 0:04990d454f45 6 * obtaining a copy of this software and associated documentation
ashleymills 0:04990d454f45 7 * files (the "Software"), to deal in the Software without
ashleymills 0:04990d454f45 8 * restriction, including without limitation the rights to use, copy,
ashleymills 0:04990d454f45 9 * modify, merge, publish, distribute, sublicense, and/or sell copies
ashleymills 0:04990d454f45 10 * of the Software, and to permit persons to whom the Software is
ashleymills 0:04990d454f45 11 * furnished to do so, subject to the following conditions:
ashleymills 0:04990d454f45 12 *
ashleymills 0:04990d454f45 13 * The above copyright notice and this permission notice shall be
ashleymills 0:04990d454f45 14 * included in all copies or substantial portions of the Software.
ashleymills 0:04990d454f45 15 *
ashleymills 0:04990d454f45 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
ashleymills 0:04990d454f45 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
ashleymills 0:04990d454f45 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ashleymills 0:04990d454f45 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
ashleymills 0:04990d454f45 20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ashleymills 0:04990d454f45 21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
ashleymills 0:04990d454f45 22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
ashleymills 0:04990d454f45 23 * SOFTWARE.
ashleymills 0:04990d454f45 24 */
ashleymills 0:04990d454f45 25
ashleymills 0:04990d454f45 26 #include "config.h"
ashleymills 0:04990d454f45 27
ashleymills 0:04990d454f45 28 #if defined(HAVE_ASSERT_H) && !defined(assert)
ashleymills 0:04990d454f45 29 #include <assert.h>
ashleymills 0:04990d454f45 30 #endif
ashleymills 0:04990d454f45 31
ashleymills 0:04990d454f45 32 #include <stdarg.h>
ashleymills 0:04990d454f45 33 #include <string.h>
ashleymills 0:04990d454f45 34 #include <stdio.h>
ashleymills 0:04990d454f45 35
ashleymills 0:04990d454f45 36 #ifdef HAVE_ARPA_INET_H
ashleymills 0:04990d454f45 37 #include <arpa/inet.h>
ashleymills 0:04990d454f45 38 #endif
ashleymills 0:04990d454f45 39
ashleymills 0:04990d454f45 40 #ifdef HAVE_TIME_H
ashleymills 0:04990d454f45 41 #include <time.h>
ashleymills 0:04990d454f45 42 #endif
ashleymills 0:04990d454f45 43
ashleymills 0:04990d454f45 44 #include "global.h"
ashleymills 0:04990d454f45 45 #include "debug.h"
ashleymills 0:04990d454f45 46
ashleymills 0:04990d454f45 47 #ifdef WITH_CONTIKI
ashleymills 0:04990d454f45 48 # ifndef DEBUG
ashleymills 0:04990d454f45 49 # define DEBUG DEBUG_PRINT
ashleymills 0:04990d454f45 50 # endif /* DEBUG */
ashleymills 0:04990d454f45 51 #include "net/uip-debug.h"
ashleymills 0:04990d454f45 52 #else
ashleymills 0:04990d454f45 53 #define PRINTF(...) printf(...)
ashleymills 0:04990d454f45 54 #endif
ashleymills 0:04990d454f45 55
ashleymills 0:04990d454f45 56 static int maxlog = LOG_WARN; /* default maximum log level */
ashleymills 0:04990d454f45 57
ashleymills 0:04990d454f45 58 log_t
ashleymills 0:04990d454f45 59 dtls_get_log_level() {
ashleymills 0:04990d454f45 60 return maxlog;
ashleymills 0:04990d454f45 61 }
ashleymills 0:04990d454f45 62
ashleymills 0:04990d454f45 63 void
ashleymills 0:04990d454f45 64 dtls_set_log_level(log_t level) {
ashleymills 0:04990d454f45 65 maxlog = level;
ashleymills 0:04990d454f45 66 }
ashleymills 0:04990d454f45 67
ashleymills 0:04990d454f45 68 /* this array has the same order as the type log_t */
ashleymills 0:04990d454f45 69 static char *loglevels[] = {
ashleymills 0:04990d454f45 70 "EMRG", "ALRT", "CRIT", "WARN", "NOTE", "INFO", "DEBG"
ashleymills 0:04990d454f45 71 };
ashleymills 0:04990d454f45 72
ashleymills 0:04990d454f45 73 #ifdef HAVE_TIME_H
ashleymills 0:04990d454f45 74
ashleymills 0:04990d454f45 75 static inline size_t
ashleymills 0:04990d454f45 76 print_timestamp(char *s, size_t len, time_t t) {
ashleymills 0:04990d454f45 77 struct tm *tmp;
ashleymills 0:04990d454f45 78 tmp = localtime(&t);
ashleymills 0:04990d454f45 79 return strftime(s, len, "%b %d %H:%M:%S", tmp);
ashleymills 0:04990d454f45 80 }
ashleymills 0:04990d454f45 81
ashleymills 0:04990d454f45 82 #else /* alternative implementation: just print the timestamp */
ashleymills 0:04990d454f45 83
ashleymills 0:04990d454f45 84 static inline size_t
ashleymills 0:04990d454f45 85 print_timestamp(char *s, size_t len, clock_time_t t) {
ashleymills 0:04990d454f45 86 #ifdef HAVE_SNPRINTF
ashleymills 0:04990d454f45 87 return snprintf(s, len, "%u.%03u",
ashleymills 0:04990d454f45 88 (unsigned int)(t / CLOCK_SECOND),
ashleymills 0:04990d454f45 89 (unsigned int)(t % CLOCK_SECOND));
ashleymills 0:04990d454f45 90 #else /* HAVE_SNPRINTF */
ashleymills 0:04990d454f45 91 /* @todo do manual conversion of timestamp */
ashleymills 0:04990d454f45 92 return 0;
ashleymills 0:04990d454f45 93 #endif /* HAVE_SNPRINTF */
ashleymills 0:04990d454f45 94 }
ashleymills 0:04990d454f45 95
ashleymills 0:04990d454f45 96 #endif /* HAVE_TIME_H */
ashleymills 0:04990d454f45 97
ashleymills 0:04990d454f45 98 #ifndef HAVE_STRNLEN
ashleymills 0:04990d454f45 99 /**
ashleymills 0:04990d454f45 100 * A length-safe strlen() fake.
ashleymills 0:04990d454f45 101 *
ashleymills 0:04990d454f45 102 * @param s The string to count characters != 0.
ashleymills 0:04990d454f45 103 * @param maxlen The maximum length of @p s.
ashleymills 0:04990d454f45 104 *
ashleymills 0:04990d454f45 105 * @return The length of @p s.
ashleymills 0:04990d454f45 106 */
ashleymills 0:04990d454f45 107 static inline size_t
ashleymills 0:04990d454f45 108 strnlen(const char *s, size_t maxlen) {
ashleymills 0:04990d454f45 109 size_t n = 0;
ashleymills 0:04990d454f45 110 while(*s++ && n < maxlen)
ashleymills 0:04990d454f45 111 ++n;
ashleymills 0:04990d454f45 112 return n;
ashleymills 0:04990d454f45 113 }
ashleymills 0:04990d454f45 114 #endif /* HAVE_STRNLEN */
ashleymills 0:04990d454f45 115
ashleymills 0:04990d454f45 116 #ifndef min
ashleymills 0:04990d454f45 117 #define min(a,b) ((a) < (b) ? (a) : (b))
ashleymills 0:04990d454f45 118 #endif
ashleymills 0:04990d454f45 119
ashleymills 0:04990d454f45 120 size_t
ashleymills 0:04990d454f45 121 dsrv_print_addr(const session_t *addr, unsigned char *buf, size_t len) {
ashleymills 0:04990d454f45 122 snprintf(buf,len,"%s:%d",inet_ntoa(addr->addr.sin.sin_addr),ntohs(addr->addr.sin.sin_port));
ashleymills 0:04990d454f45 123
ashleymills 0:04990d454f45 124 // just print the address in the buffer
ashleymills 0:04990d454f45 125
ashleymills 0:04990d454f45 126 /*
ashleymills 0:04990d454f45 127 #define DUMMY 1
ashleymills 0:04990d454f45 128 #ifdef DUMMY //HAVE_ARPA_INET_H
ashleymills 0:04990d454f45 129 const void *addrptr = NULL;
ashleymills 0:04990d454f45 130 int port;
ashleymills 0:04990d454f45 131 unsigned char *p = buf;
ashleymills 0:04990d454f45 132
ashleymills 0:04990d454f45 133 switch (addr->addr.sa.sa_family) {
ashleymills 0:04990d454f45 134 case AF_INET:
ashleymills 0:04990d454f45 135 addrptr = &addr->addr.sin.sin_addr;
ashleymills 0:04990d454f45 136 port = ntohs(addr->addr.sin.sin_port);
ashleymills 0:04990d454f45 137 break;
ashleymills 0:04990d454f45 138
ashleymills 0:04990d454f45 139 case AF_INET6:
ashleymills 0:04990d454f45 140 if (len < 7) // do not proceed if buffer is even too short for [::]:0 //
ashleymills 0:04990d454f45 141 return 0;
ashleymills 0:04990d454f45 142
ashleymills 0:04990d454f45 143 *p++ = '[';
ashleymills 0:04990d454f45 144
ashleymills 0:04990d454f45 145 addrptr = &addr->addr.sin6.sin6_addr;
ashleymills 0:04990d454f45 146 port = ntohs(addr->addr.sin6.sin6_port);
ashleymills 0:04990d454f45 147
ashleymills 0:04990d454f45 148 break;
ashleymills 0:04990d454f45 149
ashleymills 0:04990d454f45 150 default:
ashleymills 0:04990d454f45 151 memcpy(buf, "(unknown address type)", min(22, len));
ashleymills 0:04990d454f45 152 return min(22, len);
ashleymills 0:04990d454f45 153 }
ashleymills 0:04990d454f45 154
ashleymills 0:04990d454f45 155 if (inet_ntop(addr->addr.sa.sa_family, addrptr, (char *)p, len) == 0) {
ashleymills 0:04990d454f45 156 perror("dsrv_print_addr");
ashleymills 0:04990d454f45 157 return 0;
ashleymills 0:04990d454f45 158 }
ashleymills 0:04990d454f45 159
ashleymills 0:04990d454f45 160 p += strnlen((char *)p, len);
ashleymills 0:04990d454f45 161
ashleymills 0:04990d454f45 162
ashleymills 0:04990d454f45 163 if (addr->addr.sa.sa_family == AF_INET6) {
ashleymills 0:04990d454f45 164 if (p < buf + len) {
ashleymills 0:04990d454f45 165 *p++ = ']';
ashleymills 0:04990d454f45 166 } else
ashleymills 0:04990d454f45 167 return 0;
ashleymills 0:04990d454f45 168 }
ashleymills 0:04990d454f45 169
ashleymills 0:04990d454f45 170
ashleymills 0:04990d454f45 171 p += snprintf((char *)p, buf + len - p + 1, ":%d", port);
ashleymills 0:04990d454f45 172
ashleymills 0:04990d454f45 173 return buf + len - p;
ashleymills 0:04990d454f45 174 #else // HAVE_ARPA_INET_H
ashleymills 0:04990d454f45 175 # if WITH_CONTIKI
ashleymills 0:04990d454f45 176 unsigned char *p = buf;
ashleymills 0:04990d454f45 177 uint8_t i;
ashleymills 0:04990d454f45 178 # if WITH_UIP6
ashleymills 0:04990d454f45 179 const unsigned char hex[] = "0123456789ABCDEF";
ashleymills 0:04990d454f45 180
ashleymills 0:04990d454f45 181 if (len < 41)
ashleymills 0:04990d454f45 182 return 0;
ashleymills 0:04990d454f45 183
ashleymills 0:04990d454f45 184 *p++ = '[';
ashleymills 0:04990d454f45 185
ashleymills 0:04990d454f45 186 for (i=0; i < 8; i += 4) {
ashleymills 0:04990d454f45 187 *p++ = hex[(addr->addr.u16[i] & 0xf000) >> 24];
ashleymills 0:04990d454f45 188 *p++ = hex[(addr->addr.u16[i] & 0x0f00) >> 16];
ashleymills 0:04990d454f45 189 *p++ = hex[(addr->addr.u16[i] & 0x00f0) >> 8];
ashleymills 0:04990d454f45 190 *p++ = hex[(addr->addr.u16[i] & 0x000f)];
ashleymills 0:04990d454f45 191 *p++ = ':';
ashleymills 0:04990d454f45 192 }
ashleymills 0:04990d454f45 193 *(p-1) = ']';
ashleymills 0:04990d454f45 194 # else // WITH_UIP6
ashleymills 0:04990d454f45 195 # warning "IPv4 network addresses will not be included in debug output"
ashleymills 0:04990d454f45 196
ashleymills 0:04990d454f45 197 if (len < 21)
ashleymills 0:04990d454f45 198 return 0;
ashleymills 0:04990d454f45 199 # endif // WITH_UIP6
ashleymills 0:04990d454f45 200 if (buf + len - p < 6)
ashleymills 0:04990d454f45 201 return 0;
ashleymills 0:04990d454f45 202
ashleymills 0:04990d454f45 203 #ifdef HAVE_SNPRINTF
ashleymills 0:04990d454f45 204 p += snprintf((char *)p, buf + len - p + 1, ":%d", uip_htons(addr->port));
ashleymills 0:04990d454f45 205 #else // HAVE_SNPRINTF
ashleymills 0:04990d454f45 206 // @todo manual conversion of port number
ashleymills 0:04990d454f45 207 #endif // HAVE_SNPRINTF
ashleymills 0:04990d454f45 208
ashleymills 0:04990d454f45 209 return buf + len - p;
ashleymills 0:04990d454f45 210 # else // WITH_CONTIKI
ashleymills 0:04990d454f45 211 // TODO: output addresses manually
ashleymills 0:04990d454f45 212 # warning "inet_ntop() not available, network addresses will not be included in debug output"
ashleymills 0:04990d454f45 213 # endif // WITH_CONTIKI
ashleymills 0:04990d454f45 214 return 0;
ashleymills 0:04990d454f45 215 #endif
ashleymills 0:04990d454f45 216 */
ashleymills 0:04990d454f45 217 }
ashleymills 0:04990d454f45 218
ashleymills 0:04990d454f45 219
ashleymills 0:04990d454f45 220 #ifndef WITH_CONTIKI
ashleymills 0:04990d454f45 221 void
ashleymills 0:04990d454f45 222 dsrv_log(log_t level, char *format, ...) {
ashleymills 0:04990d454f45 223 static char timebuf[32];
ashleymills 0:04990d454f45 224 va_list ap;
ashleymills 0:04990d454f45 225 FILE *log_fd;
ashleymills 0:04990d454f45 226
ashleymills 0:04990d454f45 227 if (maxlog < level)
ashleymills 0:04990d454f45 228 return;
ashleymills 0:04990d454f45 229
ashleymills 0:04990d454f45 230 log_fd = stdout;//level <= LOG_CRIT ? stderr : stdout;
ashleymills 0:04990d454f45 231
ashleymills 0:04990d454f45 232 //if (print_timestamp(timebuf,sizeof(timebuf), time(NULL)))
ashleymills 0:04990d454f45 233 // fprintf(log_fd, "%s ", timebuf);
ashleymills 0:04990d454f45 234
ashleymills 0:04990d454f45 235 if (level >= 0 && level <= LOG_DEBUG)
ashleymills 0:04990d454f45 236 printf("%s ", loglevels[level]);
ashleymills 0:04990d454f45 237
ashleymills 0:04990d454f45 238 va_start(ap, format);
ashleymills 0:04990d454f45 239 vprintf(format, ap);
ashleymills 0:04990d454f45 240 va_end(ap);
ashleymills 0:04990d454f45 241 fflush(stdout);
ashleymills 0:04990d454f45 242 }
ashleymills 0:04990d454f45 243 #else /* WITH_CONTIKI */
ashleymills 0:04990d454f45 244 void
ashleymills 0:04990d454f45 245 dsrv_log(log_t level, char *format, ...) {
ashleymills 0:04990d454f45 246 static char timebuf[32];
ashleymills 0:04990d454f45 247 va_list ap;
ashleymills 0:04990d454f45 248
ashleymills 0:04990d454f45 249 if (maxlog < level)
ashleymills 0:04990d454f45 250 return;
ashleymills 0:04990d454f45 251
ashleymills 0:04990d454f45 252 if (print_timestamp(timebuf,sizeof(timebuf), clock_time()))
ashleymills 0:04990d454f45 253 PRINTF("%s ", timebuf);
ashleymills 0:04990d454f45 254
ashleymills 0:04990d454f45 255 if (level >= 0 && level <= LOG_DEBUG)
ashleymills 0:04990d454f45 256 PRINTF("%s ", loglevels[level]);
ashleymills 0:04990d454f45 257
ashleymills 0:04990d454f45 258 va_start(ap, format);
ashleymills 0:04990d454f45 259 #ifdef HAVE_VPRINTF
ashleymills 0:04990d454f45 260 vprintf(format, ap);
ashleymills 0:04990d454f45 261 #else
ashleymills 0:04990d454f45 262 PRINTF(format, ap);
ashleymills 0:04990d454f45 263 #endif
ashleymills 0:04990d454f45 264 va_end(ap);
ashleymills 0:04990d454f45 265 }
ashleymills 0:04990d454f45 266 #endif /* WITH_CONTIKI */