13157ba21SRui Paulo /*
23157ba21SRui Paulo * wpa_supplicant/hostapd / OS specific functions for Win32 systems
33157ba21SRui Paulo * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
43157ba21SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
73157ba21SRui Paulo */
83157ba21SRui Paulo
93157ba21SRui Paulo #include "includes.h"
10f05cddf9SRui Paulo #include <time.h>
113157ba21SRui Paulo #include <winsock2.h>
123157ba21SRui Paulo #include <wincrypt.h>
133157ba21SRui Paulo
143157ba21SRui Paulo #include "os.h"
155b9c547cSRui Paulo #include "common.h"
163157ba21SRui Paulo
os_sleep(os_time_t sec,os_time_t usec)173157ba21SRui Paulo void os_sleep(os_time_t sec, os_time_t usec)
183157ba21SRui Paulo {
193157ba21SRui Paulo if (sec)
203157ba21SRui Paulo Sleep(sec * 1000);
213157ba21SRui Paulo if (usec)
223157ba21SRui Paulo Sleep(usec / 1000);
233157ba21SRui Paulo }
243157ba21SRui Paulo
253157ba21SRui Paulo
os_get_time(struct os_time * t)263157ba21SRui Paulo int os_get_time(struct os_time *t)
273157ba21SRui Paulo {
283157ba21SRui Paulo #define EPOCHFILETIME (116444736000000000ULL)
293157ba21SRui Paulo FILETIME ft;
303157ba21SRui Paulo LARGE_INTEGER li;
313157ba21SRui Paulo ULONGLONG tt;
323157ba21SRui Paulo
333157ba21SRui Paulo #ifdef _WIN32_WCE
343157ba21SRui Paulo SYSTEMTIME st;
353157ba21SRui Paulo
363157ba21SRui Paulo GetSystemTime(&st);
373157ba21SRui Paulo SystemTimeToFileTime(&st, &ft);
383157ba21SRui Paulo #else /* _WIN32_WCE */
393157ba21SRui Paulo GetSystemTimeAsFileTime(&ft);
403157ba21SRui Paulo #endif /* _WIN32_WCE */
413157ba21SRui Paulo li.LowPart = ft.dwLowDateTime;
423157ba21SRui Paulo li.HighPart = ft.dwHighDateTime;
433157ba21SRui Paulo tt = (li.QuadPart - EPOCHFILETIME) / 10;
443157ba21SRui Paulo t->sec = (os_time_t) (tt / 1000000);
453157ba21SRui Paulo t->usec = (os_time_t) (tt % 1000000);
463157ba21SRui Paulo
473157ba21SRui Paulo return 0;
483157ba21SRui Paulo }
493157ba21SRui Paulo
503157ba21SRui Paulo
os_get_reltime(struct os_reltime * t)515b9c547cSRui Paulo int os_get_reltime(struct os_reltime *t)
525b9c547cSRui Paulo {
535b9c547cSRui Paulo /* consider using performance counters or so instead */
545b9c547cSRui Paulo struct os_time now;
555b9c547cSRui Paulo int res = os_get_time(&now);
565b9c547cSRui Paulo t->sec = now.sec;
575b9c547cSRui Paulo t->usec = now.usec;
585b9c547cSRui Paulo return res;
595b9c547cSRui Paulo }
605b9c547cSRui Paulo
615b9c547cSRui Paulo
os_mktime(int year,int month,int day,int hour,int min,int sec,os_time_t * t)623157ba21SRui Paulo int os_mktime(int year, int month, int day, int hour, int min, int sec,
633157ba21SRui Paulo os_time_t *t)
643157ba21SRui Paulo {
653157ba21SRui Paulo struct tm tm, *tm1;
663157ba21SRui Paulo time_t t_local, t1, t2;
673157ba21SRui Paulo os_time_t tz_offset;
683157ba21SRui Paulo
693157ba21SRui Paulo if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
703157ba21SRui Paulo hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
713157ba21SRui Paulo sec > 60)
723157ba21SRui Paulo return -1;
733157ba21SRui Paulo
743157ba21SRui Paulo memset(&tm, 0, sizeof(tm));
753157ba21SRui Paulo tm.tm_year = year - 1900;
763157ba21SRui Paulo tm.tm_mon = month - 1;
773157ba21SRui Paulo tm.tm_mday = day;
783157ba21SRui Paulo tm.tm_hour = hour;
793157ba21SRui Paulo tm.tm_min = min;
803157ba21SRui Paulo tm.tm_sec = sec;
813157ba21SRui Paulo
823157ba21SRui Paulo t_local = mktime(&tm);
833157ba21SRui Paulo
843157ba21SRui Paulo /* figure out offset to UTC */
853157ba21SRui Paulo tm1 = localtime(&t_local);
863157ba21SRui Paulo if (tm1) {
873157ba21SRui Paulo t1 = mktime(tm1);
883157ba21SRui Paulo tm1 = gmtime(&t_local);
893157ba21SRui Paulo if (tm1) {
903157ba21SRui Paulo t2 = mktime(tm1);
913157ba21SRui Paulo tz_offset = t2 - t1;
923157ba21SRui Paulo } else
933157ba21SRui Paulo tz_offset = 0;
943157ba21SRui Paulo } else
953157ba21SRui Paulo tz_offset = 0;
963157ba21SRui Paulo
973157ba21SRui Paulo *t = (os_time_t) t_local - tz_offset;
983157ba21SRui Paulo return 0;
993157ba21SRui Paulo }
1003157ba21SRui Paulo
1013157ba21SRui Paulo
os_gmtime(os_time_t t,struct os_tm * tm)102f05cddf9SRui Paulo int os_gmtime(os_time_t t, struct os_tm *tm)
103f05cddf9SRui Paulo {
104f05cddf9SRui Paulo struct tm *tm2;
105f05cddf9SRui Paulo time_t t2 = t;
106f05cddf9SRui Paulo
107f05cddf9SRui Paulo tm2 = gmtime(&t2);
108f05cddf9SRui Paulo if (tm2 == NULL)
109f05cddf9SRui Paulo return -1;
110f05cddf9SRui Paulo tm->sec = tm2->tm_sec;
111f05cddf9SRui Paulo tm->min = tm2->tm_min;
112f05cddf9SRui Paulo tm->hour = tm2->tm_hour;
113f05cddf9SRui Paulo tm->day = tm2->tm_mday;
114f05cddf9SRui Paulo tm->month = tm2->tm_mon + 1;
115f05cddf9SRui Paulo tm->year = tm2->tm_year + 1900;
116f05cddf9SRui Paulo return 0;
117f05cddf9SRui Paulo }
118f05cddf9SRui Paulo
119f05cddf9SRui Paulo
os_daemonize(const char * pid_file)1203157ba21SRui Paulo int os_daemonize(const char *pid_file)
1213157ba21SRui Paulo {
1223157ba21SRui Paulo /* TODO */
1233157ba21SRui Paulo return -1;
1243157ba21SRui Paulo }
1253157ba21SRui Paulo
1263157ba21SRui Paulo
os_daemonize_terminate(const char * pid_file)1273157ba21SRui Paulo void os_daemonize_terminate(const char *pid_file)
1283157ba21SRui Paulo {
1293157ba21SRui Paulo }
1303157ba21SRui Paulo
1313157ba21SRui Paulo
os_get_random(unsigned char * buf,size_t len)1323157ba21SRui Paulo int os_get_random(unsigned char *buf, size_t len)
1333157ba21SRui Paulo {
1343157ba21SRui Paulo HCRYPTPROV prov;
1353157ba21SRui Paulo BOOL ret;
1363157ba21SRui Paulo
1373157ba21SRui Paulo if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
1383157ba21SRui Paulo CRYPT_VERIFYCONTEXT))
1393157ba21SRui Paulo return -1;
1403157ba21SRui Paulo
1413157ba21SRui Paulo ret = CryptGenRandom(prov, len, buf);
1423157ba21SRui Paulo CryptReleaseContext(prov, 0);
1433157ba21SRui Paulo
1443157ba21SRui Paulo return ret ? 0 : -1;
1453157ba21SRui Paulo }
1463157ba21SRui Paulo
1473157ba21SRui Paulo
os_random(void)1483157ba21SRui Paulo unsigned long os_random(void)
1493157ba21SRui Paulo {
1503157ba21SRui Paulo return rand();
1513157ba21SRui Paulo }
1523157ba21SRui Paulo
1533157ba21SRui Paulo
os_rel2abs_path(const char * rel_path)1543157ba21SRui Paulo char * os_rel2abs_path(const char *rel_path)
1553157ba21SRui Paulo {
1563157ba21SRui Paulo return _strdup(rel_path);
1573157ba21SRui Paulo }
1583157ba21SRui Paulo
1593157ba21SRui Paulo
os_program_init(void)1603157ba21SRui Paulo int os_program_init(void)
1613157ba21SRui Paulo {
1623157ba21SRui Paulo #ifdef CONFIG_NATIVE_WINDOWS
1633157ba21SRui Paulo WSADATA wsaData;
1643157ba21SRui Paulo if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
1653157ba21SRui Paulo printf("Could not find a usable WinSock.dll\n");
1663157ba21SRui Paulo return -1;
1673157ba21SRui Paulo }
1683157ba21SRui Paulo #endif /* CONFIG_NATIVE_WINDOWS */
1693157ba21SRui Paulo return 0;
1703157ba21SRui Paulo }
1713157ba21SRui Paulo
1723157ba21SRui Paulo
os_program_deinit(void)1733157ba21SRui Paulo void os_program_deinit(void)
1743157ba21SRui Paulo {
1753157ba21SRui Paulo #ifdef CONFIG_NATIVE_WINDOWS
1763157ba21SRui Paulo WSACleanup();
1773157ba21SRui Paulo #endif /* CONFIG_NATIVE_WINDOWS */
1783157ba21SRui Paulo }
1793157ba21SRui Paulo
1803157ba21SRui Paulo
os_setenv(const char * name,const char * value,int overwrite)1813157ba21SRui Paulo int os_setenv(const char *name, const char *value, int overwrite)
1823157ba21SRui Paulo {
1833157ba21SRui Paulo return -1;
1843157ba21SRui Paulo }
1853157ba21SRui Paulo
1863157ba21SRui Paulo
os_unsetenv(const char * name)1873157ba21SRui Paulo int os_unsetenv(const char *name)
1883157ba21SRui Paulo {
1893157ba21SRui Paulo return -1;
1903157ba21SRui Paulo }
1913157ba21SRui Paulo
1923157ba21SRui Paulo
os_readfile(const char * name,size_t * len)1933157ba21SRui Paulo char * os_readfile(const char *name, size_t *len)
1943157ba21SRui Paulo {
1953157ba21SRui Paulo FILE *f;
1963157ba21SRui Paulo char *buf;
1973157ba21SRui Paulo
1983157ba21SRui Paulo f = fopen(name, "rb");
1993157ba21SRui Paulo if (f == NULL)
2003157ba21SRui Paulo return NULL;
2013157ba21SRui Paulo
2023157ba21SRui Paulo fseek(f, 0, SEEK_END);
2033157ba21SRui Paulo *len = ftell(f);
2043157ba21SRui Paulo fseek(f, 0, SEEK_SET);
2053157ba21SRui Paulo
2063157ba21SRui Paulo buf = malloc(*len);
2073157ba21SRui Paulo if (buf == NULL) {
2083157ba21SRui Paulo fclose(f);
2093157ba21SRui Paulo return NULL;
2103157ba21SRui Paulo }
2113157ba21SRui Paulo
2123157ba21SRui Paulo fread(buf, 1, *len, f);
2133157ba21SRui Paulo fclose(f);
2143157ba21SRui Paulo
2153157ba21SRui Paulo return buf;
2163157ba21SRui Paulo }
2173157ba21SRui Paulo
2183157ba21SRui Paulo
os_fdatasync(FILE * stream)219325151a3SRui Paulo int os_fdatasync(FILE *stream)
220325151a3SRui Paulo {
221325151a3SRui Paulo HANDLE h;
222325151a3SRui Paulo
223325151a3SRui Paulo if (stream == NULL)
224325151a3SRui Paulo return -1;
225325151a3SRui Paulo
226325151a3SRui Paulo h = (HANDLE) _get_osfhandle(_fileno(stream));
227325151a3SRui Paulo if (h == INVALID_HANDLE_VALUE)
228325151a3SRui Paulo return -1;
229325151a3SRui Paulo
230325151a3SRui Paulo if (!FlushFileBuffers(h))
231325151a3SRui Paulo return -1;
232325151a3SRui Paulo
233325151a3SRui Paulo return 0;
234325151a3SRui Paulo }
235325151a3SRui Paulo
236325151a3SRui Paulo
os_zalloc(size_t size)2373157ba21SRui Paulo void * os_zalloc(size_t size)
2383157ba21SRui Paulo {
2393157ba21SRui Paulo return calloc(1, size);
2403157ba21SRui Paulo }
2413157ba21SRui Paulo
2423157ba21SRui Paulo
os_strlcpy(char * dest,const char * src,size_t siz)2433157ba21SRui Paulo size_t os_strlcpy(char *dest, const char *src, size_t siz)
2443157ba21SRui Paulo {
2453157ba21SRui Paulo const char *s = src;
2463157ba21SRui Paulo size_t left = siz;
2473157ba21SRui Paulo
2483157ba21SRui Paulo if (left) {
2493157ba21SRui Paulo /* Copy string up to the maximum size of the dest buffer */
2503157ba21SRui Paulo while (--left != 0) {
2513157ba21SRui Paulo if ((*dest++ = *s++) == '\0')
2523157ba21SRui Paulo break;
2533157ba21SRui Paulo }
2543157ba21SRui Paulo }
2553157ba21SRui Paulo
2563157ba21SRui Paulo if (left == 0) {
2573157ba21SRui Paulo /* Not enough room for the string; force NUL-termination */
2583157ba21SRui Paulo if (siz != 0)
2593157ba21SRui Paulo *dest = '\0';
2603157ba21SRui Paulo while (*s++)
2613157ba21SRui Paulo ; /* determine total src string length */
2623157ba21SRui Paulo }
2633157ba21SRui Paulo
2643157ba21SRui Paulo return s - src - 1;
2653157ba21SRui Paulo }
2665b9c547cSRui Paulo
2675b9c547cSRui Paulo
os_memcmp_const(const void * a,const void * b,size_t len)2685b9c547cSRui Paulo int os_memcmp_const(const void *a, const void *b, size_t len)
2695b9c547cSRui Paulo {
2705b9c547cSRui Paulo const u8 *aa = a;
2715b9c547cSRui Paulo const u8 *bb = b;
2725b9c547cSRui Paulo size_t i;
2735b9c547cSRui Paulo u8 res;
2745b9c547cSRui Paulo
2755b9c547cSRui Paulo for (res = 0, i = 0; i < len; i++)
2765b9c547cSRui Paulo res |= aa[i] ^ bb[i];
2775b9c547cSRui Paulo
2785b9c547cSRui Paulo return res;
2795b9c547cSRui Paulo }
2805b9c547cSRui Paulo
2815b9c547cSRui Paulo
os_exec(const char * program,const char * arg,int wait_completion)2825b9c547cSRui Paulo int os_exec(const char *program, const char *arg, int wait_completion)
2835b9c547cSRui Paulo {
2845b9c547cSRui Paulo return -1;
2855b9c547cSRui Paulo }
286*85732ac8SCy Schubert
287*85732ac8SCy Schubert
os_memdup(const void * src,size_t len)288*85732ac8SCy Schubert void * os_memdup(const void *src, size_t len)
289*85732ac8SCy Schubert {
290*85732ac8SCy Schubert void *r = os_malloc(len);
291*85732ac8SCy Schubert
292*85732ac8SCy Schubert if (r)
293*85732ac8SCy Schubert os_memcpy(r, src, len);
294*85732ac8SCy Schubert return r;
295*85732ac8SCy Schubert }
296