os_win32.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * wpa_supplicant/hostapd / OS specific functions for Win32 systems
  3. * Copyright (c) 2005-2006, 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 <time.h>
  10. #include <winsock2.h>
  11. #include <wincrypt.h>
  12. #include "os.h"
  13. void os_sleep(os_time_t sec, os_time_t usec)
  14. {
  15. if (sec)
  16. Sleep(sec * 1000);
  17. if (usec)
  18. Sleep(usec / 1000);
  19. }
  20. int os_get_time(struct os_time *t)
  21. {
  22. #define EPOCHFILETIME (116444736000000000ULL)
  23. FILETIME ft;
  24. LARGE_INTEGER li;
  25. ULONGLONG tt;
  26. #ifdef _WIN32_WCE
  27. SYSTEMTIME st;
  28. GetSystemTime(&st);
  29. SystemTimeToFileTime(&st, &ft);
  30. #else /* _WIN32_WCE */
  31. GetSystemTimeAsFileTime(&ft);
  32. #endif /* _WIN32_WCE */
  33. li.LowPart = ft.dwLowDateTime;
  34. li.HighPart = ft.dwHighDateTime;
  35. tt = (li.QuadPart - EPOCHFILETIME) / 10;
  36. t->sec = (os_time_t) (tt / 1000000);
  37. t->usec = (os_time_t) (tt % 1000000);
  38. return 0;
  39. }
  40. int os_get_reltime(struct os_reltime *t)
  41. {
  42. /* consider using performance counters or so instead */
  43. struct os_time now;
  44. int res = os_get_time(&now);
  45. t->sec = now.sec;
  46. t->usec = now.usec;
  47. return res;
  48. }
  49. int os_mktime(int year, int month, int day, int hour, int min, int sec,
  50. os_time_t *t)
  51. {
  52. struct tm tm, *tm1;
  53. time_t t_local, t1, t2;
  54. os_time_t tz_offset;
  55. if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
  56. hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
  57. sec > 60)
  58. return -1;
  59. memset(&tm, 0, sizeof(tm));
  60. tm.tm_year = year - 1900;
  61. tm.tm_mon = month - 1;
  62. tm.tm_mday = day;
  63. tm.tm_hour = hour;
  64. tm.tm_min = min;
  65. tm.tm_sec = sec;
  66. t_local = mktime(&tm);
  67. /* figure out offset to UTC */
  68. tm1 = localtime(&t_local);
  69. if (tm1) {
  70. t1 = mktime(tm1);
  71. tm1 = gmtime(&t_local);
  72. if (tm1) {
  73. t2 = mktime(tm1);
  74. tz_offset = t2 - t1;
  75. } else
  76. tz_offset = 0;
  77. } else
  78. tz_offset = 0;
  79. *t = (os_time_t) t_local - tz_offset;
  80. return 0;
  81. }
  82. int os_gmtime(os_time_t t, struct os_tm *tm)
  83. {
  84. struct tm *tm2;
  85. time_t t2 = t;
  86. tm2 = gmtime(&t2);
  87. if (tm2 == NULL)
  88. return -1;
  89. tm->sec = tm2->tm_sec;
  90. tm->min = tm2->tm_min;
  91. tm->hour = tm2->tm_hour;
  92. tm->day = tm2->tm_mday;
  93. tm->month = tm2->tm_mon + 1;
  94. tm->year = tm2->tm_year + 1900;
  95. return 0;
  96. }
  97. int os_daemonize(const char *pid_file)
  98. {
  99. /* TODO */
  100. return -1;
  101. }
  102. void os_daemonize_terminate(const char *pid_file)
  103. {
  104. }
  105. int os_get_random(unsigned char *buf, size_t len)
  106. {
  107. HCRYPTPROV prov;
  108. BOOL ret;
  109. if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
  110. CRYPT_VERIFYCONTEXT))
  111. return -1;
  112. ret = CryptGenRandom(prov, len, buf);
  113. CryptReleaseContext(prov, 0);
  114. return ret ? 0 : -1;
  115. }
  116. unsigned long os_random(void)
  117. {
  118. return rand();
  119. }
  120. char * os_rel2abs_path(const char *rel_path)
  121. {
  122. return _strdup(rel_path);
  123. }
  124. int os_program_init(void)
  125. {
  126. #ifdef CONFIG_NATIVE_WINDOWS
  127. WSADATA wsaData;
  128. if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
  129. printf("Could not find a usable WinSock.dll\n");
  130. return -1;
  131. }
  132. #endif /* CONFIG_NATIVE_WINDOWS */
  133. return 0;
  134. }
  135. void os_program_deinit(void)
  136. {
  137. #ifdef CONFIG_NATIVE_WINDOWS
  138. WSACleanup();
  139. #endif /* CONFIG_NATIVE_WINDOWS */
  140. }
  141. int os_setenv(const char *name, const char *value, int overwrite)
  142. {
  143. return -1;
  144. }
  145. int os_unsetenv(const char *name)
  146. {
  147. return -1;
  148. }
  149. char * os_readfile(const char *name, size_t *len)
  150. {
  151. FILE *f;
  152. char *buf;
  153. f = fopen(name, "rb");
  154. if (f == NULL)
  155. return NULL;
  156. fseek(f, 0, SEEK_END);
  157. *len = ftell(f);
  158. fseek(f, 0, SEEK_SET);
  159. buf = malloc(*len);
  160. if (buf == NULL) {
  161. fclose(f);
  162. return NULL;
  163. }
  164. fread(buf, 1, *len, f);
  165. fclose(f);
  166. return buf;
  167. }
  168. void * os_zalloc(size_t size)
  169. {
  170. return calloc(1, size);
  171. }
  172. size_t os_strlcpy(char *dest, const char *src, size_t siz)
  173. {
  174. const char *s = src;
  175. size_t left = siz;
  176. if (left) {
  177. /* Copy string up to the maximum size of the dest buffer */
  178. while (--left != 0) {
  179. if ((*dest++ = *s++) == '\0')
  180. break;
  181. }
  182. }
  183. if (left == 0) {
  184. /* Not enough room for the string; force NUL-termination */
  185. if (siz != 0)
  186. *dest = '\0';
  187. while (*s++)
  188. ; /* determine total src string length */
  189. }
  190. return s - src - 1;
  191. }