os_win32.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_mktime(int year, int month, int day, int hour, int min, int sec,
  41. os_time_t *t)
  42. {
  43. struct tm tm, *tm1;
  44. time_t t_local, t1, t2;
  45. os_time_t tz_offset;
  46. if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
  47. hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
  48. sec > 60)
  49. return -1;
  50. memset(&tm, 0, sizeof(tm));
  51. tm.tm_year = year - 1900;
  52. tm.tm_mon = month - 1;
  53. tm.tm_mday = day;
  54. tm.tm_hour = hour;
  55. tm.tm_min = min;
  56. tm.tm_sec = sec;
  57. t_local = mktime(&tm);
  58. /* figure out offset to UTC */
  59. tm1 = localtime(&t_local);
  60. if (tm1) {
  61. t1 = mktime(tm1);
  62. tm1 = gmtime(&t_local);
  63. if (tm1) {
  64. t2 = mktime(tm1);
  65. tz_offset = t2 - t1;
  66. } else
  67. tz_offset = 0;
  68. } else
  69. tz_offset = 0;
  70. *t = (os_time_t) t_local - tz_offset;
  71. return 0;
  72. }
  73. int os_gmtime(os_time_t t, struct os_tm *tm)
  74. {
  75. struct tm *tm2;
  76. time_t t2 = t;
  77. tm2 = gmtime(&t2);
  78. if (tm2 == NULL)
  79. return -1;
  80. tm->sec = tm2->tm_sec;
  81. tm->min = tm2->tm_min;
  82. tm->hour = tm2->tm_hour;
  83. tm->day = tm2->tm_mday;
  84. tm->month = tm2->tm_mon + 1;
  85. tm->year = tm2->tm_year + 1900;
  86. return 0;
  87. }
  88. int os_daemonize(const char *pid_file)
  89. {
  90. /* TODO */
  91. return -1;
  92. }
  93. void os_daemonize_terminate(const char *pid_file)
  94. {
  95. }
  96. int os_get_random(unsigned char *buf, size_t len)
  97. {
  98. HCRYPTPROV prov;
  99. BOOL ret;
  100. if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
  101. CRYPT_VERIFYCONTEXT))
  102. return -1;
  103. ret = CryptGenRandom(prov, len, buf);
  104. CryptReleaseContext(prov, 0);
  105. return ret ? 0 : -1;
  106. }
  107. unsigned long os_random(void)
  108. {
  109. return rand();
  110. }
  111. char * os_rel2abs_path(const char *rel_path)
  112. {
  113. return _strdup(rel_path);
  114. }
  115. int os_program_init(void)
  116. {
  117. #ifdef CONFIG_NATIVE_WINDOWS
  118. WSADATA wsaData;
  119. if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
  120. printf("Could not find a usable WinSock.dll\n");
  121. return -1;
  122. }
  123. #endif /* CONFIG_NATIVE_WINDOWS */
  124. return 0;
  125. }
  126. void os_program_deinit(void)
  127. {
  128. #ifdef CONFIG_NATIVE_WINDOWS
  129. WSACleanup();
  130. #endif /* CONFIG_NATIVE_WINDOWS */
  131. }
  132. int os_setenv(const char *name, const char *value, int overwrite)
  133. {
  134. return -1;
  135. }
  136. int os_unsetenv(const char *name)
  137. {
  138. return -1;
  139. }
  140. char * os_readfile(const char *name, size_t *len)
  141. {
  142. FILE *f;
  143. char *buf;
  144. f = fopen(name, "rb");
  145. if (f == NULL)
  146. return NULL;
  147. fseek(f, 0, SEEK_END);
  148. *len = ftell(f);
  149. fseek(f, 0, SEEK_SET);
  150. buf = malloc(*len);
  151. if (buf == NULL) {
  152. fclose(f);
  153. return NULL;
  154. }
  155. fread(buf, 1, *len, f);
  156. fclose(f);
  157. return buf;
  158. }
  159. void * os_zalloc(size_t size)
  160. {
  161. return calloc(1, size);
  162. }
  163. size_t os_strlcpy(char *dest, const char *src, size_t siz)
  164. {
  165. const char *s = src;
  166. size_t left = siz;
  167. if (left) {
  168. /* Copy string up to the maximum size of the dest buffer */
  169. while (--left != 0) {
  170. if ((*dest++ = *s++) == '\0')
  171. break;
  172. }
  173. }
  174. if (left == 0) {
  175. /* Not enough room for the string; force NUL-termination */
  176. if (siz != 0)
  177. *dest = '\0';
  178. while (*s++)
  179. ; /* determine total src string length */
  180. }
  181. return s - src - 1;
  182. }