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
9#include "includes.h"
10#include <time.h>
11#include <winsock2.h>
12#include <wincrypt.h>
13
14#include "os.h"
15#include "common.h"
16
17void os_sleep(os_time_t sec, os_time_t usec)
18{
19	if (sec)
20		Sleep(sec * 1000);
21	if (usec)
22		Sleep(usec / 1000);
23}
24
25
26int os_get_time(struct os_time *t)
27{
28#define EPOCHFILETIME (116444736000000000ULL)
29	FILETIME ft;
30	LARGE_INTEGER li;
31	ULONGLONG tt;
32
33#ifdef _WIN32_WCE
34	SYSTEMTIME st;
35
36	GetSystemTime(&st);
37	SystemTimeToFileTime(&st, &ft);
38#else /* _WIN32_WCE */
39	GetSystemTimeAsFileTime(&ft);
40#endif /* _WIN32_WCE */
41	li.LowPart = ft.dwLowDateTime;
42	li.HighPart = ft.dwHighDateTime;
43	tt = (li.QuadPart - EPOCHFILETIME) / 10;
44	t->sec = (os_time_t) (tt / 1000000);
45	t->usec = (os_time_t) (tt % 1000000);
46
47	return 0;
48}
49
50
51int os_get_reltime(struct os_reltime *t)
52{
53	/* consider using performance counters or so instead */
54	struct os_time now;
55	int res = os_get_time(&now);
56	t->sec = now.sec;
57	t->usec = now.usec;
58	return res;
59}
60
61
62int os_mktime(int year, int month, int day, int hour, int min, int sec,
63	      os_time_t *t)
64{
65	struct tm tm, *tm1;
66	time_t t_local, t1, t2;
67	os_time_t tz_offset;
68
69	if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
70	    hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
71	    sec > 60)
72		return -1;
73
74	memset(&tm, 0, sizeof(tm));
75	tm.tm_year = year - 1900;
76	tm.tm_mon = month - 1;
77	tm.tm_mday = day;
78	tm.tm_hour = hour;
79	tm.tm_min = min;
80	tm.tm_sec = sec;
81
82	t_local = mktime(&tm);
83
84	/* figure out offset to UTC */
85	tm1 = localtime(&t_local);
86	if (tm1) {
87		t1 = mktime(tm1);
88		tm1 = gmtime(&t_local);
89		if (tm1) {
90			t2 = mktime(tm1);
91			tz_offset = t2 - t1;
92		} else
93			tz_offset = 0;
94	} else
95		tz_offset = 0;
96
97	*t = (os_time_t) t_local - tz_offset;
98	return 0;
99}
100
101
102int os_gmtime(os_time_t t, struct os_tm *tm)
103{
104	struct tm *tm2;
105	time_t t2 = t;
106
107	tm2 = gmtime(&t2);
108	if (tm2 == NULL)
109		return -1;
110	tm->sec = tm2->tm_sec;
111	tm->min = tm2->tm_min;
112	tm->hour = tm2->tm_hour;
113	tm->day = tm2->tm_mday;
114	tm->month = tm2->tm_mon + 1;
115	tm->year = tm2->tm_year + 1900;
116	return 0;
117}
118
119
120int os_daemonize(const char *pid_file)
121{
122	/* TODO */
123	return -1;
124}
125
126
127void os_daemonize_terminate(const char *pid_file)
128{
129}
130
131
132int os_get_random(unsigned char *buf, size_t len)
133{
134	HCRYPTPROV prov;
135	BOOL ret;
136
137	if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
138				 CRYPT_VERIFYCONTEXT))
139		return -1;
140
141	ret = CryptGenRandom(prov, len, buf);
142	CryptReleaseContext(prov, 0);
143
144	return ret ? 0 : -1;
145}
146
147
148unsigned long os_random(void)
149{
150	return rand();
151}
152
153
154char * os_rel2abs_path(const char *rel_path)
155{
156	return _strdup(rel_path);
157}
158
159
160int os_program_init(void)
161{
162#ifdef CONFIG_NATIVE_WINDOWS
163	WSADATA wsaData;
164	if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
165		printf("Could not find a usable WinSock.dll\n");
166		return -1;
167	}
168#endif /* CONFIG_NATIVE_WINDOWS */
169	return 0;
170}
171
172
173void os_program_deinit(void)
174{
175#ifdef CONFIG_NATIVE_WINDOWS
176	WSACleanup();
177#endif /* CONFIG_NATIVE_WINDOWS */
178}
179
180
181int os_setenv(const char *name, const char *value, int overwrite)
182{
183	return -1;
184}
185
186
187int os_unsetenv(const char *name)
188{
189	return -1;
190}
191
192
193char * os_readfile(const char *name, size_t *len)
194{
195	FILE *f;
196	char *buf;
197
198	f = fopen(name, "rb");
199	if (f == NULL)
200		return NULL;
201
202	fseek(f, 0, SEEK_END);
203	*len = ftell(f);
204	fseek(f, 0, SEEK_SET);
205
206	buf = malloc(*len);
207	if (buf == NULL) {
208		fclose(f);
209		return NULL;
210	}
211
212	fread(buf, 1, *len, f);
213	fclose(f);
214
215	return buf;
216}
217
218
219int os_fdatasync(FILE *stream)
220{
221	HANDLE h;
222
223	if (stream == NULL)
224		return -1;
225
226	h = (HANDLE) _get_osfhandle(_fileno(stream));
227	if (h == INVALID_HANDLE_VALUE)
228		return -1;
229
230	if (!FlushFileBuffers(h))
231		return -1;
232
233	return 0;
234}
235
236
237void * os_zalloc(size_t size)
238{
239	return calloc(1, size);
240}
241
242
243size_t os_strlcpy(char *dest, const char *src, size_t siz)
244{
245	const char *s = src;
246	size_t left = siz;
247
248	if (left) {
249		/* Copy string up to the maximum size of the dest buffer */
250		while (--left != 0) {
251			if ((*dest++ = *s++) == '\0')
252				break;
253		}
254	}
255
256	if (left == 0) {
257		/* Not enough room for the string; force NUL-termination */
258		if (siz != 0)
259			*dest = '\0';
260		while (*s++)
261			; /* determine total src string length */
262	}
263
264	return s - src - 1;
265}
266
267
268int os_memcmp_const(const void *a, const void *b, size_t len)
269{
270	const u8 *aa = a;
271	const u8 *bb = b;
272	size_t i;
273	u8 res;
274
275	for (res = 0, i = 0; i < len; i++)
276		res |= aa[i] ^ bb[i];
277
278	return res;
279}
280
281
282int os_exec(const char *program, const char *arg, int wait_completion)
283{
284	return -1;
285}
286