os_win32.c revision 1f69aa52ea2e0a73ac502565df8c666ee49cab6a
1/* 2 * wpa_supplicant/hostapd / OS specific functions for Win32 systems 3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15#include "includes.h" 16#include <time.h> 17#include <winsock2.h> 18#include <wincrypt.h> 19 20#include "os.h" 21 22void os_sleep(os_time_t sec, os_time_t usec) 23{ 24 if (sec) 25 Sleep(sec * 1000); 26 if (usec) 27 Sleep(usec / 1000); 28} 29 30 31int os_get_time(struct os_time *t) 32{ 33#define EPOCHFILETIME (116444736000000000ULL) 34 FILETIME ft; 35 LARGE_INTEGER li; 36 ULONGLONG tt; 37 38#ifdef _WIN32_WCE 39 SYSTEMTIME st; 40 41 GetSystemTime(&st); 42 SystemTimeToFileTime(&st, &ft); 43#else /* _WIN32_WCE */ 44 GetSystemTimeAsFileTime(&ft); 45#endif /* _WIN32_WCE */ 46 li.LowPart = ft.dwLowDateTime; 47 li.HighPart = ft.dwHighDateTime; 48 tt = (li.QuadPart - EPOCHFILETIME) / 10; 49 t->sec = (os_time_t) (tt / 1000000); 50 t->usec = (os_time_t) (tt % 1000000); 51 52 return 0; 53} 54 55 56int os_mktime(int year, int month, int day, int hour, int min, int sec, 57 os_time_t *t) 58{ 59 struct tm tm, *tm1; 60 time_t t_local, t1, t2; 61 os_time_t tz_offset; 62 63 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 || 64 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || 65 sec > 60) 66 return -1; 67 68 memset(&tm, 0, sizeof(tm)); 69 tm.tm_year = year - 1900; 70 tm.tm_mon = month - 1; 71 tm.tm_mday = day; 72 tm.tm_hour = hour; 73 tm.tm_min = min; 74 tm.tm_sec = sec; 75 76 t_local = mktime(&tm); 77 78 /* figure out offset to UTC */ 79 tm1 = localtime(&t_local); 80 if (tm1) { 81 t1 = mktime(tm1); 82 tm1 = gmtime(&t_local); 83 if (tm1) { 84 t2 = mktime(tm1); 85 tz_offset = t2 - t1; 86 } else 87 tz_offset = 0; 88 } else 89 tz_offset = 0; 90 91 *t = (os_time_t) t_local - tz_offset; 92 return 0; 93} 94 95 96int os_gmtime(os_time_t t, struct os_tm *tm) 97{ 98 struct tm *tm2; 99 time_t t2 = t; 100 101 tm2 = gmtime(&t2); 102 if (tm2 == NULL) 103 return -1; 104 tm->sec = tm2->tm_sec; 105 tm->min = tm2->tm_min; 106 tm->hour = tm2->tm_hour; 107 tm->day = tm2->tm_mday; 108 tm->month = tm2->tm_mon + 1; 109 tm->year = tm2->tm_year + 1900; 110 return 0; 111} 112 113 114int os_daemonize(const char *pid_file) 115{ 116 /* TODO */ 117 return -1; 118} 119 120 121void os_daemonize_terminate(const char *pid_file) 122{ 123} 124 125 126int os_get_random(unsigned char *buf, size_t len) 127{ 128 HCRYPTPROV prov; 129 BOOL ret; 130 131 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 132 CRYPT_VERIFYCONTEXT)) 133 return -1; 134 135 ret = CryptGenRandom(prov, len, buf); 136 CryptReleaseContext(prov, 0); 137 138 return ret ? 0 : -1; 139} 140 141 142unsigned long os_random(void) 143{ 144 return rand(); 145} 146 147 148char * os_rel2abs_path(const char *rel_path) 149{ 150 return _strdup(rel_path); 151} 152 153 154int os_program_init(void) 155{ 156#ifdef CONFIG_NATIVE_WINDOWS 157 WSADATA wsaData; 158 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) { 159 printf("Could not find a usable WinSock.dll\n"); 160 return -1; 161 } 162#endif /* CONFIG_NATIVE_WINDOWS */ 163 return 0; 164} 165 166 167void os_program_deinit(void) 168{ 169#ifdef CONFIG_NATIVE_WINDOWS 170 WSACleanup(); 171#endif /* CONFIG_NATIVE_WINDOWS */ 172} 173 174 175int os_setenv(const char *name, const char *value, int overwrite) 176{ 177 return -1; 178} 179 180 181int os_unsetenv(const char *name) 182{ 183 return -1; 184} 185 186 187char * os_readfile(const char *name, size_t *len) 188{ 189 FILE *f; 190 char *buf; 191 192 f = fopen(name, "rb"); 193 if (f == NULL) 194 return NULL; 195 196 fseek(f, 0, SEEK_END); 197 *len = ftell(f); 198 fseek(f, 0, SEEK_SET); 199 200 buf = malloc(*len); 201 if (buf == NULL) { 202 fclose(f); 203 return NULL; 204 } 205 206 fread(buf, 1, *len, f); 207 fclose(f); 208 209 return buf; 210} 211 212 213void * os_zalloc(size_t size) 214{ 215 return calloc(1, size); 216} 217 218 219size_t os_strlcpy(char *dest, const char *src, size_t siz) 220{ 221 const char *s = src; 222 size_t left = siz; 223 224 if (left) { 225 /* Copy string up to the maximum size of the dest buffer */ 226 while (--left != 0) { 227 if ((*dest++ = *s++) == '\0') 228 break; 229 } 230 } 231 232 if (left == 0) { 233 /* Not enough room for the string; force NUL-termination */ 234 if (siz != 0) 235 *dest = '\0'; 236 while (*s++) 237 ; /* determine total src string length */ 238 } 239 240 return s - src - 1; 241} 242