common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * wpa_supplicant/hostapd / common helper functions, etc.
  3. * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. static int hex2num(char c)
  11. {
  12. if (c >= '0' && c <= '9')
  13. return c - '0';
  14. if (c >= 'a' && c <= 'f')
  15. return c - 'a' + 10;
  16. if (c >= 'A' && c <= 'F')
  17. return c - 'A' + 10;
  18. return -1;
  19. }
  20. int hex2byte(const char *hex)
  21. {
  22. int a, b;
  23. a = hex2num(*hex++);
  24. if (a < 0)
  25. return -1;
  26. b = hex2num(*hex++);
  27. if (b < 0)
  28. return -1;
  29. return (a << 4) | b;
  30. }
  31. /**
  32. * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
  33. * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
  34. * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
  35. * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
  36. */
  37. int hwaddr_aton(const char *txt, u8 *addr)
  38. {
  39. int i;
  40. for (i = 0; i < 6; i++) {
  41. int a, b;
  42. a = hex2num(*txt++);
  43. if (a < 0)
  44. return -1;
  45. b = hex2num(*txt++);
  46. if (b < 0)
  47. return -1;
  48. *addr++ = (a << 4) | b;
  49. if (i < 5 && *txt++ != ':')
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. /**
  55. * hwaddr_compact_aton - Convert ASCII string to MAC address (no colon delimitors format)
  56. * @txt: MAC address as a string (e.g., "001122334455")
  57. * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
  58. * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
  59. */
  60. int hwaddr_compact_aton(const char *txt, u8 *addr)
  61. {
  62. int i;
  63. for (i = 0; i < 6; i++) {
  64. int a, b;
  65. a = hex2num(*txt++);
  66. if (a < 0)
  67. return -1;
  68. b = hex2num(*txt++);
  69. if (b < 0)
  70. return -1;
  71. *addr++ = (a << 4) | b;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
  77. * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
  78. * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
  79. * Returns: Characters used (> 0) on success, -1 on failure
  80. */
  81. int hwaddr_aton2(const char *txt, u8 *addr)
  82. {
  83. int i;
  84. const char *pos = txt;
  85. for (i = 0; i < 6; i++) {
  86. int a, b;
  87. while (*pos == ':' || *pos == '.' || *pos == '-')
  88. pos++;
  89. a = hex2num(*pos++);
  90. if (a < 0)
  91. return -1;
  92. b = hex2num(*pos++);
  93. if (b < 0)
  94. return -1;
  95. *addr++ = (a << 4) | b;
  96. }
  97. return pos - txt;
  98. }
  99. /**
  100. * hexstr2bin - Convert ASCII hex string into binary data
  101. * @hex: ASCII hex string (e.g., "01ab")
  102. * @buf: Buffer for the binary data
  103. * @len: Length of the text to convert in bytes (of buf); hex will be double
  104. * this size
  105. * Returns: 0 on success, -1 on failure (invalid hex string)
  106. */
  107. int hexstr2bin(const char *hex, u8 *buf, size_t len)
  108. {
  109. size_t i;
  110. int a;
  111. const char *ipos = hex;
  112. u8 *opos = buf;
  113. for (i = 0; i < len; i++) {
  114. a = hex2byte(ipos);
  115. if (a < 0)
  116. return -1;
  117. *opos++ = a;
  118. ipos += 2;
  119. }
  120. return 0;
  121. }
  122. /**
  123. * inc_byte_array - Increment arbitrary length byte array by one
  124. * @counter: Pointer to byte array
  125. * @len: Length of the counter in bytes
  126. *
  127. * This function increments the last byte of the counter by one and continues
  128. * rolling over to more significant bytes if the byte was incremented from
  129. * 0xff to 0x00.
  130. */
  131. void inc_byte_array(u8 *counter, size_t len)
  132. {
  133. int pos = len - 1;
  134. while (pos >= 0) {
  135. counter[pos]++;
  136. if (counter[pos] != 0)
  137. break;
  138. pos--;
  139. }
  140. }
  141. void wpa_get_ntp_timestamp(u8 *buf)
  142. {
  143. struct os_time now;
  144. u32 sec, usec;
  145. be32 tmp;
  146. /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
  147. os_get_time(&now);
  148. sec = now.sec + 2208988800U; /* Epoch to 1900 */
  149. /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
  150. usec = now.usec;
  151. usec = 4295 * usec - (usec >> 5) - (usec >> 9);
  152. tmp = host_to_be32(sec);
  153. os_memcpy(buf, (u8 *) &tmp, 4);
  154. tmp = host_to_be32(usec);
  155. os_memcpy(buf + 4, (u8 *) &tmp, 4);
  156. }
  157. static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
  158. size_t len, int uppercase)
  159. {
  160. size_t i;
  161. char *pos = buf, *end = buf + buf_size;
  162. int ret;
  163. if (buf_size == 0)
  164. return 0;
  165. for (i = 0; i < len; i++) {
  166. ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
  167. data[i]);
  168. if (ret < 0 || ret >= end - pos) {
  169. end[-1] = '\0';
  170. return pos - buf;
  171. }
  172. pos += ret;
  173. }
  174. end[-1] = '\0';
  175. return pos - buf;
  176. }
  177. /**
  178. * wpa_snprintf_hex - Print data as a hex string into a buffer
  179. * @buf: Memory area to use as the output buffer
  180. * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
  181. * @data: Data to be printed
  182. * @len: Length of data in bytes
  183. * Returns: Number of bytes written
  184. */
  185. int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
  186. {
  187. return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
  188. }
  189. /**
  190. * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
  191. * @buf: Memory area to use as the output buffer
  192. * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
  193. * @data: Data to be printed
  194. * @len: Length of data in bytes
  195. * Returns: Number of bytes written
  196. */
  197. int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
  198. size_t len)
  199. {
  200. return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
  201. }
  202. #ifdef CONFIG_ANSI_C_EXTRA
  203. #ifdef _WIN32_WCE
  204. void perror(const char *s)
  205. {
  206. wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
  207. s, (int) GetLastError());
  208. }
  209. #endif /* _WIN32_WCE */
  210. int optind = 1;
  211. int optopt;
  212. char *optarg;
  213. int getopt(int argc, char *const argv[], const char *optstring)
  214. {
  215. static int optchr = 1;
  216. char *cp;
  217. if (optchr == 1) {
  218. if (optind >= argc) {
  219. /* all arguments processed */
  220. return EOF;
  221. }
  222. if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
  223. /* no option characters */
  224. return EOF;
  225. }
  226. }
  227. if (os_strcmp(argv[optind], "--") == 0) {
  228. /* no more options */
  229. optind++;
  230. return EOF;
  231. }
  232. optopt = argv[optind][optchr];
  233. cp = os_strchr(optstring, optopt);
  234. if (cp == NULL || optopt == ':') {
  235. if (argv[optind][++optchr] == '\0') {
  236. optchr = 1;
  237. optind++;
  238. }
  239. return '?';
  240. }
  241. if (cp[1] == ':') {
  242. /* Argument required */
  243. optchr = 1;
  244. if (argv[optind][optchr + 1]) {
  245. /* No space between option and argument */
  246. optarg = &argv[optind++][optchr + 1];
  247. } else if (++optind >= argc) {
  248. /* option requires an argument */
  249. return '?';
  250. } else {
  251. /* Argument in the next argv */
  252. optarg = argv[optind++];
  253. }
  254. } else {
  255. /* No argument */
  256. if (argv[optind][++optchr] == '\0') {
  257. optchr = 1;
  258. optind++;
  259. }
  260. optarg = NULL;
  261. }
  262. return *cp;
  263. }
  264. #endif /* CONFIG_ANSI_C_EXTRA */
  265. #ifdef CONFIG_NATIVE_WINDOWS
  266. /**
  267. * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
  268. * @str: Pointer to string to convert
  269. *
  270. * This function converts a unicode string to ASCII using the same
  271. * buffer for output. If UNICODE is not set, the buffer is not
  272. * modified.
  273. */
  274. void wpa_unicode2ascii_inplace(TCHAR *str)
  275. {
  276. #ifdef UNICODE
  277. char *dst = (char *) str;
  278. while (*str)
  279. *dst++ = (char) *str++;
  280. *dst = '\0';
  281. #endif /* UNICODE */
  282. }
  283. TCHAR * wpa_strdup_tchar(const char *str)
  284. {
  285. #ifdef UNICODE
  286. TCHAR *buf;
  287. buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
  288. if (buf == NULL)
  289. return NULL;
  290. wsprintf(buf, L"%S", str);
  291. return buf;
  292. #else /* UNICODE */
  293. return os_strdup(str);
  294. #endif /* UNICODE */
  295. }
  296. #endif /* CONFIG_NATIVE_WINDOWS */
  297. void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
  298. {
  299. char *end = txt + maxlen;
  300. size_t i;
  301. for (i = 0; i < len; i++) {
  302. if (txt + 4 > end)
  303. break;
  304. switch (data[i]) {
  305. case '\"':
  306. *txt++ = '\\';
  307. *txt++ = '\"';
  308. break;
  309. case '\\':
  310. *txt++ = '\\';
  311. *txt++ = '\\';
  312. break;
  313. case '\e':
  314. *txt++ = '\\';
  315. *txt++ = 'e';
  316. break;
  317. case '\n':
  318. *txt++ = '\\';
  319. *txt++ = 'n';
  320. break;
  321. case '\r':
  322. *txt++ = '\\';
  323. *txt++ = 'r';
  324. break;
  325. case '\t':
  326. *txt++ = '\\';
  327. *txt++ = 't';
  328. break;
  329. default:
  330. if (data[i] >= 32 && data[i] <= 127) {
  331. *txt++ = data[i];
  332. } else {
  333. txt += os_snprintf(txt, end - txt, "\\x%02x",
  334. data[i]);
  335. }
  336. break;
  337. }
  338. }
  339. *txt = '\0';
  340. }
  341. size_t printf_decode(u8 *buf, size_t maxlen, const char *str)
  342. {
  343. const char *pos = str;
  344. size_t len = 0;
  345. int val;
  346. while (*pos) {
  347. if (len == maxlen)
  348. break;
  349. switch (*pos) {
  350. case '\\':
  351. pos++;
  352. switch (*pos) {
  353. case '\\':
  354. buf[len++] = '\\';
  355. pos++;
  356. break;
  357. case '"':
  358. buf[len++] = '"';
  359. pos++;
  360. break;
  361. case 'n':
  362. buf[len++] = '\n';
  363. pos++;
  364. break;
  365. case 'r':
  366. buf[len++] = '\r';
  367. pos++;
  368. break;
  369. case 't':
  370. buf[len++] = '\t';
  371. pos++;
  372. break;
  373. case 'e':
  374. buf[len++] = '\e';
  375. pos++;
  376. break;
  377. case 'x':
  378. pos++;
  379. val = hex2byte(pos);
  380. if (val < 0) {
  381. val = hex2num(*pos);
  382. if (val < 0)
  383. break;
  384. buf[len++] = val;
  385. pos++;
  386. } else {
  387. buf[len++] = val;
  388. pos += 2;
  389. }
  390. break;
  391. case '0':
  392. case '1':
  393. case '2':
  394. case '3':
  395. case '4':
  396. case '5':
  397. case '6':
  398. case '7':
  399. val = *pos++ - '0';
  400. if (*pos >= '0' && *pos <= '7')
  401. val = val * 8 + (*pos++ - '0');
  402. if (*pos >= '0' && *pos <= '7')
  403. val = val * 8 + (*pos++ - '0');
  404. buf[len++] = val;
  405. break;
  406. default:
  407. break;
  408. }
  409. break;
  410. default:
  411. buf[len++] = *pos++;
  412. break;
  413. }
  414. }
  415. return len;
  416. }
  417. /**
  418. * wpa_ssid_txt - Convert SSID to a printable string
  419. * @ssid: SSID (32-octet string)
  420. * @ssid_len: Length of ssid in octets
  421. * Returns: Pointer to a printable string
  422. *
  423. * This function can be used to convert SSIDs into printable form. In most
  424. * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
  425. * does not limit the used character set, so anything could be used in an SSID.
  426. *
  427. * This function uses a static buffer, so only one call can be used at the
  428. * time, i.e., this is not re-entrant and the returned buffer must be used
  429. * before calling this again.
  430. */
  431. const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
  432. {
  433. static char ssid_txt[32 * 4 + 1];
  434. if (ssid == NULL) {
  435. ssid_txt[0] = '\0';
  436. return ssid_txt;
  437. }
  438. printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
  439. return ssid_txt;
  440. }
  441. void * __hide_aliasing_typecast(void *foo)
  442. {
  443. return foo;
  444. }
  445. char * wpa_config_parse_string(const char *value, size_t *len)
  446. {
  447. if (*value == '"') {
  448. const char *pos;
  449. char *str;
  450. value++;
  451. pos = os_strrchr(value, '"');
  452. if (pos == NULL || pos[1] != '\0')
  453. return NULL;
  454. *len = pos - value;
  455. str = os_malloc(*len + 1);
  456. if (str == NULL)
  457. return NULL;
  458. os_memcpy(str, value, *len);
  459. str[*len] = '\0';
  460. return str;
  461. } else if (*value == 'P' && value[1] == '"') {
  462. const char *pos;
  463. char *tstr, *str;
  464. size_t tlen;
  465. value += 2;
  466. pos = os_strrchr(value, '"');
  467. if (pos == NULL || pos[1] != '\0')
  468. return NULL;
  469. tlen = pos - value;
  470. tstr = os_malloc(tlen + 1);
  471. if (tstr == NULL)
  472. return NULL;
  473. os_memcpy(tstr, value, tlen);
  474. tstr[tlen] = '\0';
  475. str = os_malloc(tlen + 1);
  476. if (str == NULL) {
  477. os_free(tstr);
  478. return NULL;
  479. }
  480. *len = printf_decode((u8 *) str, tlen + 1, tstr);
  481. os_free(tstr);
  482. return str;
  483. } else {
  484. u8 *str;
  485. size_t tlen, hlen = os_strlen(value);
  486. if (hlen & 1)
  487. return NULL;
  488. tlen = hlen / 2;
  489. str = os_malloc(tlen + 1);
  490. if (str == NULL)
  491. return NULL;
  492. if (hexstr2bin(value, str, tlen)) {
  493. os_free(str);
  494. return NULL;
  495. }
  496. str[tlen] = '\0';
  497. *len = tlen;
  498. return (char *) str;
  499. }
  500. }
  501. int is_hex(const u8 *data, size_t len)
  502. {
  503. size_t i;
  504. for (i = 0; i < len; i++) {
  505. if (data[i] < 32 || data[i] >= 127)
  506. return 1;
  507. }
  508. return 0;
  509. }
  510. size_t merge_byte_arrays(u8 *res, size_t res_len,
  511. const u8 *src1, size_t src1_len,
  512. const u8 *src2, size_t src2_len)
  513. {
  514. size_t len = 0;
  515. os_memset(res, 0, res_len);
  516. if (src1) {
  517. if (src1_len >= res_len) {
  518. os_memcpy(res, src1, res_len);
  519. return res_len;
  520. }
  521. os_memcpy(res, src1, src1_len);
  522. len += src1_len;
  523. }
  524. if (src2) {
  525. if (len + src2_len >= res_len) {
  526. os_memcpy(res + len, src2, res_len - len);
  527. return res_len;
  528. }
  529. os_memcpy(res + len, src2, src2_len);
  530. len += src2_len;
  531. }
  532. return len;
  533. }