common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 '\033':
  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 + 1 >= 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++] = '\033';
  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. if (maxlen > len)
  416. buf[len] = '\0';
  417. return len;
  418. }
  419. /**
  420. * wpa_ssid_txt - Convert SSID to a printable string
  421. * @ssid: SSID (32-octet string)
  422. * @ssid_len: Length of ssid in octets
  423. * Returns: Pointer to a printable string
  424. *
  425. * This function can be used to convert SSIDs into printable form. In most
  426. * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
  427. * does not limit the used character set, so anything could be used in an SSID.
  428. *
  429. * This function uses a static buffer, so only one call can be used at the
  430. * time, i.e., this is not re-entrant and the returned buffer must be used
  431. * before calling this again.
  432. */
  433. const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
  434. {
  435. static char ssid_txt[32 * 4 + 1];
  436. if (ssid == NULL) {
  437. ssid_txt[0] = '\0';
  438. return ssid_txt;
  439. }
  440. printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
  441. return ssid_txt;
  442. }
  443. void * __hide_aliasing_typecast(void *foo)
  444. {
  445. return foo;
  446. }
  447. char * wpa_config_parse_string(const char *value, size_t *len)
  448. {
  449. if (*value == '"') {
  450. const char *pos;
  451. char *str;
  452. value++;
  453. pos = os_strrchr(value, '"');
  454. if (pos == NULL || pos[1] != '\0')
  455. return NULL;
  456. *len = pos - value;
  457. str = dup_binstr(value, *len);
  458. if (str == NULL)
  459. return NULL;
  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 = dup_binstr(value, tlen);
  471. if (tstr == NULL)
  472. return NULL;
  473. str = os_malloc(tlen + 1);
  474. if (str == NULL) {
  475. os_free(tstr);
  476. return NULL;
  477. }
  478. *len = printf_decode((u8 *) str, tlen + 1, tstr);
  479. os_free(tstr);
  480. return str;
  481. } else {
  482. u8 *str;
  483. size_t tlen, hlen = os_strlen(value);
  484. if (hlen & 1)
  485. return NULL;
  486. tlen = hlen / 2;
  487. str = os_malloc(tlen + 1);
  488. if (str == NULL)
  489. return NULL;
  490. if (hexstr2bin(value, str, tlen)) {
  491. os_free(str);
  492. return NULL;
  493. }
  494. str[tlen] = '\0';
  495. *len = tlen;
  496. return (char *) str;
  497. }
  498. }
  499. int is_hex(const u8 *data, size_t len)
  500. {
  501. size_t i;
  502. for (i = 0; i < len; i++) {
  503. if (data[i] < 32 || data[i] >= 127)
  504. return 1;
  505. }
  506. return 0;
  507. }
  508. int find_first_bit(u32 value)
  509. {
  510. int pos = 0;
  511. while (value) {
  512. if (value & 0x1)
  513. return pos;
  514. value >>= 1;
  515. pos++;
  516. }
  517. return -1;
  518. }
  519. size_t merge_byte_arrays(u8 *res, size_t res_len,
  520. const u8 *src1, size_t src1_len,
  521. const u8 *src2, size_t src2_len)
  522. {
  523. size_t len = 0;
  524. os_memset(res, 0, res_len);
  525. if (src1) {
  526. if (src1_len >= res_len) {
  527. os_memcpy(res, src1, res_len);
  528. return res_len;
  529. }
  530. os_memcpy(res, src1, src1_len);
  531. len += src1_len;
  532. }
  533. if (src2) {
  534. if (len + src2_len >= res_len) {
  535. os_memcpy(res + len, src2, res_len - len);
  536. return res_len;
  537. }
  538. os_memcpy(res + len, src2, src2_len);
  539. len += src2_len;
  540. }
  541. return len;
  542. }
  543. char * dup_binstr(const void *src, size_t len)
  544. {
  545. char *res;
  546. if (src == NULL)
  547. return NULL;
  548. res = os_malloc(len + 1);
  549. if (res == NULL)
  550. return NULL;
  551. os_memcpy(res, src, len);
  552. res[len] = '\0';
  553. return res;
  554. }
  555. int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value)
  556. {
  557. struct wpa_freq_range *freq = NULL, *n;
  558. unsigned int count = 0;
  559. const char *pos, *pos2, *pos3;
  560. /*
  561. * Comma separated list of frequency ranges.
  562. * For example: 2412-2432,2462,5000-6000
  563. */
  564. pos = value;
  565. while (pos && pos[0]) {
  566. n = os_realloc_array(freq, count + 1,
  567. sizeof(struct wpa_freq_range));
  568. if (n == NULL) {
  569. os_free(freq);
  570. return -1;
  571. }
  572. freq = n;
  573. freq[count].min = atoi(pos);
  574. pos2 = os_strchr(pos, '-');
  575. pos3 = os_strchr(pos, ',');
  576. if (pos2 && (!pos3 || pos2 < pos3)) {
  577. pos2++;
  578. freq[count].max = atoi(pos2);
  579. } else
  580. freq[count].max = freq[count].min;
  581. pos = pos3;
  582. if (pos)
  583. pos++;
  584. count++;
  585. }
  586. os_free(res->range);
  587. res->range = freq;
  588. res->num = count;
  589. return 0;
  590. }
  591. int freq_range_list_includes(const struct wpa_freq_range_list *list,
  592. unsigned int freq)
  593. {
  594. unsigned int i;
  595. if (list == NULL)
  596. return 0;
  597. for (i = 0; i < list->num; i++) {
  598. if (freq >= list->range[i].min && freq <= list->range[i].max)
  599. return 1;
  600. }
  601. return 0;
  602. }
  603. char * freq_range_list_str(const struct wpa_freq_range_list *list)
  604. {
  605. char *buf, *pos, *end;
  606. size_t maxlen;
  607. unsigned int i;
  608. int res;
  609. if (list->num == 0)
  610. return NULL;
  611. maxlen = list->num * 30;
  612. buf = os_malloc(maxlen);
  613. if (buf == NULL)
  614. return NULL;
  615. pos = buf;
  616. end = buf + maxlen;
  617. for (i = 0; i < list->num; i++) {
  618. struct wpa_freq_range *range = &list->range[i];
  619. if (range->min == range->max)
  620. res = os_snprintf(pos, end - pos, "%s%u",
  621. i == 0 ? "" : ",", range->min);
  622. else
  623. res = os_snprintf(pos, end - pos, "%s%u-%u",
  624. i == 0 ? "" : ",",
  625. range->min, range->max);
  626. if (res < 0 || res > end - pos) {
  627. os_free(buf);
  628. return NULL;
  629. }
  630. pos += res;
  631. }
  632. return buf;
  633. }
  634. int int_array_len(const int *a)
  635. {
  636. int i;
  637. for (i = 0; a && a[i]; i++)
  638. ;
  639. return i;
  640. }
  641. void int_array_concat(int **res, const int *a)
  642. {
  643. int reslen, alen, i;
  644. int *n;
  645. reslen = int_array_len(*res);
  646. alen = int_array_len(a);
  647. n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
  648. if (n == NULL) {
  649. os_free(*res);
  650. *res = NULL;
  651. return;
  652. }
  653. for (i = 0; i <= alen; i++)
  654. n[reslen + i] = a[i];
  655. *res = n;
  656. }
  657. static int freq_cmp(const void *a, const void *b)
  658. {
  659. int _a = *(int *) a;
  660. int _b = *(int *) b;
  661. if (_a == 0)
  662. return 1;
  663. if (_b == 0)
  664. return -1;
  665. return _a - _b;
  666. }
  667. void int_array_sort_unique(int *a)
  668. {
  669. int alen;
  670. int i, j;
  671. if (a == NULL)
  672. return;
  673. alen = int_array_len(a);
  674. qsort(a, alen, sizeof(int), freq_cmp);
  675. i = 0;
  676. j = 1;
  677. while (a[i] && a[j]) {
  678. if (a[i] == a[j]) {
  679. j++;
  680. continue;
  681. }
  682. a[++i] = a[j++];
  683. }
  684. if (a[i])
  685. i++;
  686. a[i] = 0;
  687. }
  688. void int_array_add_unique(int **res, int a)
  689. {
  690. int reslen;
  691. int *n;
  692. for (reslen = 0; *res && (*res)[reslen]; reslen++) {
  693. if ((*res)[reslen] == a)
  694. return; /* already in the list */
  695. }
  696. n = os_realloc_array(*res, reslen + 2, sizeof(int));
  697. if (n == NULL) {
  698. os_free(*res);
  699. *res = NULL;
  700. return;
  701. }
  702. n[reslen] = a;
  703. n[reslen + 1] = 0;
  704. *res = n;
  705. }
  706. void str_clear_free(char *str)
  707. {
  708. if (str) {
  709. size_t len = os_strlen(str);
  710. os_memset(str, 0, len);
  711. os_free(str);
  712. }
  713. }
  714. void bin_clear_free(void *bin, size_t len)
  715. {
  716. if (bin) {
  717. os_memset(bin, 0, len);
  718. os_free(bin);
  719. }
  720. }
  721. int random_mac_addr(u8 *addr)
  722. {
  723. if (os_get_random(addr, ETH_ALEN) < 0)
  724. return -1;
  725. addr[0] &= 0xfe; /* unicast */
  726. addr[0] |= 0x02; /* locally administered */
  727. return 0;
  728. }
  729. int random_mac_addr_keep_oui(u8 *addr)
  730. {
  731. if (os_get_random(addr + 3, 3) < 0)
  732. return -1;
  733. addr[0] &= 0xfe; /* unicast */
  734. addr[0] |= 0x02; /* locally administered */
  735. return 0;
  736. }
  737. /**
  738. * str_token - Get next token from a string
  739. * @buf: String to tokenize. Note that the string might be modified.
  740. * @delim: String of delimiters
  741. * @context: Pointer to save our context. Should be initialized with
  742. * NULL on the first call, and passed for any further call.
  743. * Returns: The next token, NULL if there are no more valid tokens.
  744. */
  745. char * str_token(char *str, const char *delim, char **context)
  746. {
  747. char *end, *pos = str;
  748. if (*context)
  749. pos = *context;
  750. while (*pos && os_strchr(delim, *pos))
  751. pos++;
  752. if (!*pos)
  753. return NULL;
  754. end = pos + 1;
  755. while (*end && !os_strchr(delim, *end))
  756. end++;
  757. if (*end)
  758. *end++ = '\0';
  759. *context = end;
  760. return pos;
  761. }