common.c

Go to the documentation of this file.
00001 
00016 #include <stdlib.h>
00017 #include <stdio.h>
00018 #include <string.h>
00019 #include <unistd.h>
00020 #include <errno.h>
00021 #include <stdarg.h>
00022 #include <ctype.h>
00023 #include <time.h>
00024 #include <sys/time.h>
00025 #ifdef CONFIG_NATIVE_WINDOWS
00026 #include <winsock2.h>
00027 #include <wincrypt.h>
00028 #endif /* CONFIG_NATIVE_WINDOWS */
00029 
00030 #include "common.h"
00031 
00032 
00033 int wpa_debug_level = MSG_INFO;
00034 int wpa_debug_show_keys = 0;
00035 int wpa_debug_timestamp = 0;
00036 
00037 
00038 int hostapd_get_rand(u8 *buf, size_t len)
00039 {
00040 #ifdef CONFIG_NATIVE_WINDOWS
00041         HCRYPTPROV prov;
00042         BOOL ret;
00043 
00044         if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
00045                                  CRYPT_VERIFYCONTEXT))
00046                 return -1;
00047 
00048         ret = CryptGenRandom(prov, len, buf);
00049         CryptReleaseContext(prov, 0);
00050 
00051         return ret ? 0 : -1;
00052 #else /* CONFIG_NATIVE_WINDOWS */
00053         FILE *f;
00054         size_t rc;
00055 
00056         f = fopen("/dev/urandom", "r");
00057         if (f == NULL) {
00058                 printf("Could not open /dev/urandom.\n");
00059                 return -1;
00060         }
00061 
00062         rc = fread(buf, 1, len, f);
00063         fclose(f);
00064 
00065         return rc != len ? -1 : 0;
00066 #endif /* CONFIG_NATIVE_WINDOWS */
00067 }
00068 
00069 
00070 void hostapd_hexdump(const char *title, const u8 *buf, size_t len)
00071 {
00072         size_t i;
00073         printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
00074         for (i = 0; i < len; i++)
00075                 printf(" %02x", buf[i]);
00076         printf("\n");
00077 }
00078 
00079 
00080 static int hex2num(char c)
00081 {
00082         if (c >= '0' && c <= '9')
00083                 return c - '0';
00084         if (c >= 'a' && c <= 'f')
00085                 return c - 'a' + 10;
00086         if (c >= 'A' && c <= 'F')
00087                 return c - 'A' + 10;
00088         return -1;
00089 }
00090 
00091 
00092 static int hex2byte(const char *hex)
00093 {
00094         int a, b;
00095         a = hex2num(*hex++);
00096         if (a < 0)
00097                 return -1;
00098         b = hex2num(*hex++);
00099         if (b < 0)
00100                 return -1;
00101         return (a << 4) | b;
00102 }
00103 
00104 
00112 int hwaddr_aton(const char *txt, u8 *addr)
00113 {
00114         int i;
00115 
00116         for (i = 0; i < 6; i++) {
00117                 int a, b;
00118 
00119                 a = hex2num(*txt++);
00120                 if (a < 0)
00121                         return -1;
00122                 b = hex2num(*txt++);
00123                 if (b < 0)
00124                         return -1;
00125                 *addr++ = (a << 4) | b;
00126                 if (i < 5 && *txt++ != ':')
00127                         return -1;
00128         }
00129 
00130         return 0;
00131 }
00132 
00133 
00143 int hexstr2bin(const char *hex, u8 *buf, size_t len)
00144 {
00145         int i, a;
00146         const char *ipos = hex;
00147         u8 *opos = buf;
00148 
00149         for (i = 0; i < len; i++) {
00150                 a = hex2byte(ipos);
00151                 if (a < 0)
00152                         return -1;
00153                 *opos++ = a;
00154                 ipos += 2;
00155         }
00156         return 0;
00157 }
00158 
00159 
00160 char * rel2abs_path(const char *rel_path)
00161 {
00162         char *buf = NULL, *cwd, *ret;
00163         size_t len = 128, cwd_len, rel_len, ret_len;
00164 
00165         if (rel_path[0] == '/')
00166                 return strdup(rel_path);
00167 
00168         for (;;) {
00169                 buf = malloc(len);
00170                 if (buf == NULL)
00171                         return NULL;
00172                 cwd = getcwd(buf, len);
00173                 if (cwd == NULL) {
00174                         free(buf);
00175                         if (errno != ERANGE) {
00176                                 return NULL;
00177                         }
00178                         len *= 2;
00179                 } else {
00180                         break;
00181                 }
00182         }
00183 
00184         cwd_len = strlen(cwd);
00185         rel_len = strlen(rel_path);
00186         ret_len = cwd_len + 1 + rel_len + 1;
00187         ret = malloc(ret_len);
00188         if (ret) {
00189                 memcpy(ret, cwd, cwd_len);
00190                 ret[cwd_len] = '/';
00191                 memcpy(ret + cwd_len + 1, rel_path, rel_len);
00192                 ret[ret_len - 1] = '\0';
00193         }
00194         free(buf);
00195         return ret;
00196 }
00197 
00198 
00209 void inc_byte_array(u8 *counter, size_t len)
00210 {
00211         int pos = len - 1;
00212         while (pos >= 0) {
00213                 counter[pos]++;
00214                 if (counter[pos] != 0)
00215                         break;
00216                 pos--;
00217         }
00218 }
00219 
00220 
00221 void print_char(char c)
00222 {
00223         if (c >= 32 && c < 127)
00224                 printf("%c", c);
00225         else
00226                 printf("<%02x>", c);
00227 }
00228 
00229 
00230 void fprint_char(FILE *f, char c)
00231 {
00232         if (c >= 32 && c < 127)
00233                 fprintf(f, "%c", c);
00234         else
00235                 fprintf(f, "<%02x>", c);
00236 }
00237 
00238 
00239 #ifndef CONFIG_NO_STDOUT_DEBUG
00240 
00241 void wpa_debug_print_timestamp(void)
00242 {
00243         struct timeval tv;
00244         char buf[16];
00245 
00246         if (!wpa_debug_timestamp)
00247                 return;
00248 
00249         gettimeofday(&tv, NULL);
00250         if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S",
00251                      localtime((const time_t *) &tv.tv_sec)) <= 0) {
00252                 snprintf(buf, sizeof(buf), "%u", (int) tv.tv_sec);
00253         }
00254         printf("%s.%06u: ", buf, (unsigned int) tv.tv_usec);
00255 }
00256 
00257 
00270 void wpa_printf(int level, char *fmt, ...)
00271 {
00272         va_list ap;
00273 
00274         va_start(ap, fmt);
00275         if (level >= wpa_debug_level) {
00276                 wpa_debug_print_timestamp();
00277                 vprintf(fmt, ap);
00278                 printf("\n");
00279         }
00280         va_end(ap);
00281 }
00282 
00283 
00284 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
00285                          size_t len, int show)
00286 {
00287         size_t i;
00288         if (level < wpa_debug_level)
00289                 return;
00290         wpa_debug_print_timestamp();
00291         printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
00292         if (buf == NULL) {
00293                 printf(" [NULL]");
00294         } else if (show) {
00295                 for (i = 0; i < len; i++)
00296                         printf(" %02x", buf[i]);
00297         } else {
00298                 printf(" [REMOVED]");
00299         }
00300         printf("\n");
00301 }
00302 
00303 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
00304 {
00305         _wpa_hexdump(level, title, buf, len, 1);
00306 }
00307 
00308 
00309 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
00310 {
00311         _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
00312 }
00313 
00314 
00315 static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
00316                                size_t len, int show)
00317 {
00318         int i, llen;
00319         const u8 *pos = buf;
00320         const int line_len = 16;
00321 
00322         if (level < wpa_debug_level)
00323                 return;
00324         wpa_debug_print_timestamp();
00325         if (!show) {
00326                 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
00327                        title, (unsigned long) len);
00328                 return;
00329         }
00330         if (buf == NULL) {
00331                 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
00332                        title, (unsigned long) len);
00333                 return;
00334         }
00335         printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
00336         while (len) {
00337                 llen = len > line_len ? line_len : len;
00338                 printf("    ");
00339                 for (i = 0; i < llen; i++)
00340                         printf(" %02x", pos[i]);
00341                 for (i = llen; i < line_len; i++)
00342                         printf("   ");
00343                 printf("   ");
00344                 for (i = 0; i < llen; i++) {
00345                         if (isprint(pos[i]))
00346                                 printf("%c", pos[i]);
00347                         else
00348                                 printf("_");
00349                 }
00350                 for (i = llen; i < line_len; i++)
00351                         printf(" ");
00352                 printf("\n");
00353                 pos += llen;
00354                 len -= llen;
00355         }
00356 }
00357 
00358 
00359 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
00360 {
00361         _wpa_hexdump_ascii(level, title, buf, len, 1);
00362 }
00363 
00364 
00365 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
00366                            size_t len)
00367 {
00368         _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
00369 }
00370 
00371 #endif /* CONFIG_NO_STDOUT_DEBUG */
00372 
00373 
00374 #ifdef CONFIG_NATIVE_WINDOWS
00375 
00376 #define EPOCHFILETIME (116444736000000000ULL)
00377 
00378 int gettimeofday(struct timeval *tv, struct timezone *tz)
00379 {
00380         FILETIME ft;
00381         LARGE_INTEGER li;
00382         ULONGLONG t;
00383 
00384         GetSystemTimeAsFileTime(&ft);
00385         li.LowPart = ft.dwLowDateTime;
00386         li.HighPart = ft.dwHighDateTime;
00387         t = (li.QuadPart - EPOCHFILETIME) / 10;
00388         tv->tv_sec = (long) (t / 1000000);
00389         tv->tv_usec = (long) (t % 1000000);
00390 
00391         return 0;
00392 }
00393 #endif /* CONFIG_NATIVE_WINDOWS */
00394 

Generated on Sat May 6 21:13:30 2006 for wpa_supplicant by  doxygen 1.4.2