common.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * wpa_supplicant/hostapd / common helper functions, etc.
  3. * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. static int hex2num(char c)
  17. {
  18. if (c >= '0' && c <= '9')
  19. return c - '0';
  20. if (c >= 'a' && c <= 'f')
  21. return c - 'a' + 10;
  22. if (c >= 'A' && c <= 'F')
  23. return c - 'A' + 10;
  24. return -1;
  25. }
  26. static int hex2byte(const char *hex)
  27. {
  28. int a, b;
  29. a = hex2num(*hex++);
  30. if (a < 0)
  31. return -1;
  32. b = hex2num(*hex++);
  33. if (b < 0)
  34. return -1;
  35. return (a << 4) | b;
  36. }
  37. /**
  38. * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
  39. * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
  40. * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
  41. * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
  42. */
  43. int hwaddr_aton(const char *txt, u8 *addr)
  44. {
  45. int i;
  46. for (i = 0; i < 6; i++) {
  47. int a, b;
  48. a = hex2num(*txt++);
  49. if (a < 0)
  50. return -1;
  51. b = hex2num(*txt++);
  52. if (b < 0)
  53. return -1;
  54. *addr++ = (a << 4) | b;
  55. if (i < 5 && *txt++ != ':')
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. /**
  61. * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
  62. * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
  63. * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
  64. * Returns: Characters used (> 0) on success, -1 on failure
  65. */
  66. int hwaddr_aton2(const char *txt, u8 *addr)
  67. {
  68. int i;
  69. const char *pos = txt;
  70. for (i = 0; i < 6; i++) {
  71. int a, b;
  72. while (*pos == ':' || *pos == '.' || *pos == '-')
  73. pos++;
  74. a = hex2num(*pos++);
  75. if (a < 0)
  76. return -1;
  77. b = hex2num(*pos++);
  78. if (b < 0)
  79. return -1;
  80. *addr++ = (a << 4) | b;
  81. }
  82. return pos - txt;
  83. }
  84. /**
  85. * hexstr2bin - Convert ASCII hex string into binary data
  86. * @hex: ASCII hex string (e.g., "01ab")
  87. * @buf: Buffer for the binary data
  88. * @len: Length of the text to convert in bytes (of buf); hex will be double
  89. * this size
  90. * Returns: 0 on success, -1 on failure (invalid hex string)
  91. */
  92. int hexstr2bin(const char *hex, u8 *buf, size_t len)
  93. {
  94. size_t i;
  95. int a;
  96. const char *ipos = hex;
  97. u8 *opos = buf;
  98. for (i = 0; i < len; i++) {
  99. a = hex2byte(ipos);
  100. if (a < 0)
  101. return -1;
  102. *opos++ = a;
  103. ipos += 2;
  104. }
  105. return 0;
  106. }
  107. /**
  108. * inc_byte_array - Increment arbitrary length byte array by one
  109. * @counter: Pointer to byte array
  110. * @len: Length of the counter in bytes
  111. *
  112. * This function increments the last byte of the counter by one and continues
  113. * rolling over to more significant bytes if the byte was incremented from
  114. * 0xff to 0x00.
  115. */
  116. void inc_byte_array(u8 *counter, size_t len)
  117. {
  118. int pos = len - 1;
  119. while (pos >= 0) {
  120. counter[pos]++;
  121. if (counter[pos] != 0)
  122. break;
  123. pos--;
  124. }
  125. }
  126. void wpa_get_ntp_timestamp(u8 *buf)
  127. {
  128. struct os_time now;
  129. u32 sec, usec;
  130. be32 tmp;
  131. /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
  132. os_get_time(&now);
  133. sec = now.sec + 2208988800U; /* Epoch to 1900 */
  134. /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
  135. usec = now.usec;
  136. usec = 4295 * usec - (usec >> 5) - (usec >> 9);
  137. tmp = host_to_be32(sec);
  138. os_memcpy(buf, (u8 *) &tmp, 4);
  139. tmp = host_to_be32(usec);
  140. os_memcpy(buf + 4, (u8 *) &tmp, 4);
  141. }
  142. static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
  143. size_t len, int uppercase)
  144. {
  145. size_t i;
  146. char *pos = buf, *end = buf + buf_size;
  147. int ret;
  148. if (buf_size == 0)
  149. return 0;
  150. for (i = 0; i < len; i++) {
  151. ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
  152. data[i]);
  153. if (ret < 0 || ret >= end - pos) {
  154. end[-1] = '\0';
  155. return pos - buf;
  156. }
  157. pos += ret;
  158. }
  159. end[-1] = '\0';
  160. return pos - buf;
  161. }
  162. /**
  163. * wpa_snprintf_hex - Print data as a hex string into a buffer
  164. * @buf: Memory area to use as the output buffer
  165. * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
  166. * @data: Data to be printed
  167. * @len: Length of data in bytes
  168. * Returns: Number of bytes written
  169. */
  170. int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
  171. {
  172. return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
  173. }
  174. /**
  175. * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
  176. * @buf: Memory area to use as the output buffer
  177. * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
  178. * @data: Data to be printed
  179. * @len: Length of data in bytes
  180. * Returns: Number of bytes written
  181. */
  182. int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
  183. size_t len)
  184. {
  185. return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
  186. }
  187. #ifdef CONFIG_ANSI_C_EXTRA
  188. #ifdef _WIN32_WCE
  189. void perror(const char *s)
  190. {
  191. wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
  192. s, (int) GetLastError());
  193. }
  194. #endif /* _WIN32_WCE */
  195. int optind = 1;
  196. int optopt;
  197. char *optarg;
  198. int getopt(int argc, char *const argv[], const char *optstring)
  199. {
  200. static int optchr = 1;
  201. char *cp;
  202. if (optchr == 1) {
  203. if (optind >= argc) {
  204. /* all arguments processed */
  205. return EOF;
  206. }
  207. if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
  208. /* no option characters */
  209. return EOF;
  210. }
  211. }
  212. if (os_strcmp(argv[optind], "--") == 0) {
  213. /* no more options */
  214. optind++;
  215. return EOF;
  216. }
  217. optopt = argv[optind][optchr];
  218. cp = os_strchr(optstring, optopt);
  219. if (cp == NULL || optopt == ':') {
  220. if (argv[optind][++optchr] == '\0') {
  221. optchr = 1;
  222. optind++;
  223. }
  224. return '?';
  225. }
  226. if (cp[1] == ':') {
  227. /* Argument required */
  228. optchr = 1;
  229. if (argv[optind][optchr + 1]) {
  230. /* No space between option and argument */
  231. optarg = &argv[optind++][optchr + 1];
  232. } else if (++optind >= argc) {
  233. /* option requires an argument */
  234. return '?';
  235. } else {
  236. /* Argument in the next argv */
  237. optarg = argv[optind++];
  238. }
  239. } else {
  240. /* No argument */
  241. if (argv[optind][++optchr] == '\0') {
  242. optchr = 1;
  243. optind++;
  244. }
  245. optarg = NULL;
  246. }
  247. return *cp;
  248. }
  249. #endif /* CONFIG_ANSI_C_EXTRA */
  250. #ifdef CONFIG_NATIVE_WINDOWS
  251. /**
  252. * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
  253. * @str: Pointer to string to convert
  254. *
  255. * This function converts a unicode string to ASCII using the same
  256. * buffer for output. If UNICODE is not set, the buffer is not
  257. * modified.
  258. */
  259. void wpa_unicode2ascii_inplace(TCHAR *str)
  260. {
  261. #ifdef UNICODE
  262. char *dst = (char *) str;
  263. while (*str)
  264. *dst++ = (char) *str++;
  265. *dst = '\0';
  266. #endif /* UNICODE */
  267. }
  268. TCHAR * wpa_strdup_tchar(const char *str)
  269. {
  270. #ifdef UNICODE
  271. TCHAR *buf;
  272. buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
  273. if (buf == NULL)
  274. return NULL;
  275. wsprintf(buf, L"%S", str);
  276. return buf;
  277. #else /* UNICODE */
  278. return os_strdup(str);
  279. #endif /* UNICODE */
  280. }
  281. #endif /* CONFIG_NATIVE_WINDOWS */
  282. /**
  283. * wpa_ssid_txt - Convert SSID to a printable string
  284. * @ssid: SSID (32-octet string)
  285. * @ssid_len: Length of ssid in octets
  286. * Returns: Pointer to a printable string
  287. *
  288. * This function can be used to convert SSIDs into printable form. In most
  289. * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
  290. * does not limit the used character set, so anything could be used in an SSID.
  291. *
  292. * This function uses a static buffer, so only one call can be used at the
  293. * time, i.e., this is not re-entrant and the returned buffer must be used
  294. * before calling this again.
  295. */
  296. const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
  297. {
  298. static char ssid_txt[33];
  299. char *pos;
  300. if (ssid_len > 32)
  301. ssid_len = 32;
  302. os_memcpy(ssid_txt, ssid, ssid_len);
  303. ssid_txt[ssid_len] = '\0';
  304. for (pos = ssid_txt; *pos != '\0'; pos++) {
  305. if ((u8) *pos < 32 || (u8) *pos >= 127)
  306. *pos = '_';
  307. }
  308. return ssid_txt;
  309. }
  310. void * __hide_aliasing_typecast(void *foo)
  311. {
  312. return foo;
  313. }