common.c
Go to the documentation of this file.
00001 /*
00002  * wpa_supplicant/hostapd / common helper functions, etc.
00003  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 
00017 #include "common.h"
00018 
00019 
00020 static int hex2num(char c)
00021 {
00022         if (c >= '0' && c <= '9')
00023                 return c - '0';
00024         if (c >= 'a' && c <= 'f')
00025                 return c - 'a' + 10;
00026         if (c >= 'A' && c <= 'F')
00027                 return c - 'A' + 10;
00028         return -1;
00029 }
00030 
00031 
00032 static int hex2byte(const char *hex)
00033 {
00034         int a, b;
00035         a = hex2num(*hex++);
00036         if (a < 0)
00037                 return -1;
00038         b = hex2num(*hex++);
00039         if (b < 0)
00040                 return -1;
00041         return (a << 4) | b;
00042 }
00043 
00044 
00051 int hwaddr_aton(const char *txt, u8 *addr)
00052 {
00053         int i;
00054 
00055         for (i = 0; i < 6; i++) {
00056                 int a, b;
00057 
00058                 a = hex2num(*txt++);
00059                 if (a < 0)
00060                         return -1;
00061                 b = hex2num(*txt++);
00062                 if (b < 0)
00063                         return -1;
00064                 *addr++ = (a << 4) | b;
00065                 if (i < 5 && *txt++ != ':')
00066                         return -1;
00067         }
00068 
00069         return 0;
00070 }
00071 
00072 
00079 int hwaddr_aton2(const char *txt, u8 *addr)
00080 {
00081         int i;
00082         const char *pos = txt;
00083 
00084         for (i = 0; i < 6; i++) {
00085                 int a, b;
00086 
00087                 while (*pos == ':' || *pos == '.' || *pos == '-')
00088                         pos++;
00089 
00090                 a = hex2num(*pos++);
00091                 if (a < 0)
00092                         return -1;
00093                 b = hex2num(*pos++);
00094                 if (b < 0)
00095                         return -1;
00096                 *addr++ = (a << 4) | b;
00097         }
00098 
00099         return pos - txt;
00100 }
00101 
00102 
00111 int hexstr2bin(const char *hex, u8 *buf, size_t len)
00112 {
00113         size_t i;
00114         int a;
00115         const char *ipos = hex;
00116         u8 *opos = buf;
00117 
00118         for (i = 0; i < len; i++) {
00119                 a = hex2byte(ipos);
00120                 if (a < 0)
00121                         return -1;
00122                 *opos++ = a;
00123                 ipos += 2;
00124         }
00125         return 0;
00126 }
00127 
00128 
00138 void inc_byte_array(u8 *counter, size_t len)
00139 {
00140         int pos = len - 1;
00141         while (pos >= 0) {
00142                 counter[pos]++;
00143                 if (counter[pos] != 0)
00144                         break;
00145                 pos--;
00146         }
00147 }
00148 
00149 
00150 void wpa_get_ntp_timestamp(u8 *buf)
00151 {
00152         struct os_time now;
00153         u32 sec, usec;
00154         be32 tmp;
00155 
00156         /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
00157         os_get_time(&now);
00158         sec = now.sec + 2208988800U; /* Epoch to 1900 */
00159         /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
00160         usec = now.usec;
00161         usec = 4295 * usec - (usec >> 5) - (usec >> 9);
00162         tmp = host_to_be32(sec);
00163         os_memcpy(buf, (u8 *) &tmp, 4);
00164         tmp = host_to_be32(usec);
00165         os_memcpy(buf + 4, (u8 *) &tmp, 4);
00166 }
00167 
00168 
00169 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
00170                                     size_t len, int uppercase)
00171 {
00172         size_t i;
00173         char *pos = buf, *end = buf + buf_size;
00174         int ret;
00175         if (buf_size == 0)
00176                 return 0;
00177         for (i = 0; i < len; i++) {
00178                 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
00179                                   data[i]);
00180                 if (ret < 0 || ret >= end - pos) {
00181                         end[-1] = '\0';
00182                         return pos - buf;
00183                 }
00184                 pos += ret;
00185         }
00186         end[-1] = '\0';
00187         return pos - buf;
00188 }
00189 
00198 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
00199 {
00200         return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
00201 }
00202 
00203 
00212 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
00213                                size_t len)
00214 {
00215         return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
00216 }
00217 
00218 
00219 #ifdef CONFIG_ANSI_C_EXTRA
00220 
00221 #ifdef _WIN32_WCE
00222 void perror(const char *s)
00223 {
00224         wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
00225                    s, (int) GetLastError());
00226 }
00227 #endif /* _WIN32_WCE */
00228 
00229 
00230 int optind = 1;
00231 int optopt;
00232 char *optarg;
00233 
00234 int getopt(int argc, char *const argv[], const char *optstring)
00235 {
00236         static int optchr = 1;
00237         char *cp;
00238 
00239         if (optchr == 1) {
00240                 if (optind >= argc) {
00241                         /* all arguments processed */
00242                         return EOF;
00243                 }
00244 
00245                 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
00246                         /* no option characters */
00247                         return EOF;
00248                 }
00249         }
00250 
00251         if (os_strcmp(argv[optind], "--") == 0) {
00252                 /* no more options */
00253                 optind++;
00254                 return EOF;
00255         }
00256 
00257         optopt = argv[optind][optchr];
00258         cp = os_strchr(optstring, optopt);
00259         if (cp == NULL || optopt == ':') {
00260                 if (argv[optind][++optchr] == '\0') {
00261                         optchr = 1;
00262                         optind++;
00263                 }
00264                 return '?';
00265         }
00266 
00267         if (cp[1] == ':') {
00268                 /* Argument required */
00269                 optchr = 1;
00270                 if (argv[optind][optchr + 1]) {
00271                         /* No space between option and argument */
00272                         optarg = &argv[optind++][optchr + 1];
00273                 } else if (++optind >= argc) {
00274                         /* option requires an argument */
00275                         return '?';
00276                 } else {
00277                         /* Argument in the next argv */
00278                         optarg = argv[optind++];
00279                 }
00280         } else {
00281                 /* No argument */
00282                 if (argv[optind][++optchr] == '\0') {
00283                         optchr = 1;
00284                         optind++;
00285                 }
00286                 optarg = NULL;
00287         }
00288         return *cp;
00289 }
00290 #endif /* CONFIG_ANSI_C_EXTRA */
00291 
00292 
00293 #ifdef CONFIG_NATIVE_WINDOWS
00294 
00302 void wpa_unicode2ascii_inplace(TCHAR *str)
00303 {
00304 #ifdef UNICODE
00305         char *dst = (char *) str;
00306         while (*str)
00307                 *dst++ = (char) *str++;
00308         *dst = '\0';
00309 #endif /* UNICODE */
00310 }
00311 
00312 
00313 TCHAR * wpa_strdup_tchar(const char *str)
00314 {
00315 #ifdef UNICODE
00316         TCHAR *buf;
00317         buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
00318         if (buf == NULL)
00319                 return NULL;
00320         wsprintf(buf, L"%S", str);
00321         return buf;
00322 #else /* UNICODE */
00323         return os_strdup(str);
00324 #endif /* UNICODE */
00325 }
00326 #endif /* CONFIG_NATIVE_WINDOWS */
00327 
00328 
00343 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
00344 {
00345         static char ssid_txt[33];
00346         char *pos;
00347 
00348         if (ssid_len > 32)
00349                 ssid_len = 32;
00350         os_memcpy(ssid_txt, ssid, ssid_len);
00351         ssid_txt[ssid_len] = '\0';
00352         for (pos = ssid_txt; *pos != '\0'; pos++) {
00353                 if ((u8) *pos < 32 || (u8) *pos >= 127)
00354                         *pos = '_';
00355         }
00356         return ssid_txt;
00357 }
00358 
00359 
00360 void * __hide_aliasing_typecast(void *foo)
00361 {
00362         return foo;
00363 }


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:33:19