os_win32.c
Go to the documentation of this file.
00001 /*
00002  * wpa_supplicant/hostapd / OS specific functions for Win32 systems
00003  * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 #include <winsock2.h>
00017 #include <wincrypt.h>
00018 
00019 #include "os.h"
00020 
00021 void os_sleep(os_time_t sec, os_time_t usec)
00022 {
00023         if (sec)
00024                 Sleep(sec * 1000);
00025         if (usec)
00026                 Sleep(usec / 1000);
00027 }
00028 
00029 
00030 int os_get_time(struct os_time *t)
00031 {
00032 #define EPOCHFILETIME (116444736000000000ULL)
00033         FILETIME ft;
00034         LARGE_INTEGER li;
00035         ULONGLONG tt;
00036 
00037 #ifdef _WIN32_WCE
00038         SYSTEMTIME st;
00039 
00040         GetSystemTime(&st);
00041         SystemTimeToFileTime(&st, &ft);
00042 #else /* _WIN32_WCE */
00043         GetSystemTimeAsFileTime(&ft);
00044 #endif /* _WIN32_WCE */
00045         li.LowPart = ft.dwLowDateTime;
00046         li.HighPart = ft.dwHighDateTime;
00047         tt = (li.QuadPart - EPOCHFILETIME) / 10;
00048         t->sec = (os_time_t) (tt / 1000000);
00049         t->usec = (os_time_t) (tt % 1000000);
00050 
00051         return 0;
00052 }
00053 
00054 
00055 int os_mktime(int year, int month, int day, int hour, int min, int sec,
00056               os_time_t *t)
00057 {
00058         struct tm tm, *tm1;
00059         time_t t_local, t1, t2;
00060         os_time_t tz_offset;
00061 
00062         if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
00063             hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
00064             sec > 60)
00065                 return -1;
00066 
00067         memset(&tm, 0, sizeof(tm));
00068         tm.tm_year = year - 1900;
00069         tm.tm_mon = month - 1;
00070         tm.tm_mday = day;
00071         tm.tm_hour = hour;
00072         tm.tm_min = min;
00073         tm.tm_sec = sec;
00074 
00075         t_local = mktime(&tm);
00076 
00077         /* figure out offset to UTC */
00078         tm1 = localtime(&t_local);
00079         if (tm1) {
00080                 t1 = mktime(tm1);
00081                 tm1 = gmtime(&t_local);
00082                 if (tm1) {
00083                         t2 = mktime(tm1);
00084                         tz_offset = t2 - t1;
00085                 } else
00086                         tz_offset = 0;
00087         } else
00088                 tz_offset = 0;
00089 
00090         *t = (os_time_t) t_local - tz_offset;
00091         return 0;
00092 }
00093 
00094 
00095 int os_daemonize(const char *pid_file)
00096 {
00097         /* TODO */
00098         return -1;
00099 }
00100 
00101 
00102 void os_daemonize_terminate(const char *pid_file)
00103 {
00104 }
00105 
00106 
00107 int os_get_random(unsigned char *buf, size_t len)
00108 {
00109         HCRYPTPROV prov;
00110         BOOL ret;
00111 
00112         if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
00113                                  CRYPT_VERIFYCONTEXT))
00114                 return -1;
00115 
00116         ret = CryptGenRandom(prov, len, buf);
00117         CryptReleaseContext(prov, 0);
00118 
00119         return ret ? 0 : -1;
00120 }
00121 
00122 
00123 unsigned long os_random(void)
00124 {
00125         return rand();
00126 }
00127 
00128 
00129 char * os_rel2abs_path(const char *rel_path)
00130 {
00131         return _strdup(rel_path);
00132 }
00133 
00134 
00135 int os_program_init(void)
00136 {
00137 #ifdef CONFIG_NATIVE_WINDOWS
00138         WSADATA wsaData;
00139         if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
00140                 printf("Could not find a usable WinSock.dll\n");
00141                 return -1;
00142         }
00143 #endif /* CONFIG_NATIVE_WINDOWS */
00144         return 0;
00145 }
00146 
00147 
00148 void os_program_deinit(void)
00149 {
00150 #ifdef CONFIG_NATIVE_WINDOWS
00151         WSACleanup();
00152 #endif /* CONFIG_NATIVE_WINDOWS */
00153 }
00154 
00155 
00156 int os_setenv(const char *name, const char *value, int overwrite)
00157 {
00158         return -1;
00159 }
00160 
00161 
00162 int os_unsetenv(const char *name)
00163 {
00164         return -1;
00165 }
00166 
00167 
00168 char * os_readfile(const char *name, size_t *len)
00169 {
00170         FILE *f;
00171         char *buf;
00172 
00173         f = fopen(name, "rb");
00174         if (f == NULL)
00175                 return NULL;
00176 
00177         fseek(f, 0, SEEK_END);
00178         *len = ftell(f);
00179         fseek(f, 0, SEEK_SET);
00180 
00181         buf = malloc(*len);
00182         if (buf == NULL) {
00183                 fclose(f);
00184                 return NULL;
00185         }
00186 
00187         fread(buf, 1, *len, f);
00188         fclose(f);
00189 
00190         return buf;
00191 }
00192 
00193 
00194 void * os_zalloc(size_t size)
00195 {
00196         return calloc(1, size);
00197 }
00198 
00199 
00200 size_t os_strlcpy(char *dest, const char *src, size_t siz)
00201 {
00202         const char *s = src;
00203         size_t left = siz;
00204 
00205         if (left) {
00206                 /* Copy string up to the maximum size of the dest buffer */
00207                 while (--left != 0) {
00208                         if ((*dest++ = *s++) == '\0')
00209                                 break;
00210                 }
00211         }
00212 
00213         if (left == 0) {
00214                 /* Not enough room for the string; force NUL-termination */
00215                 if (siz != 0)
00216                         *dest = '\0';
00217                 while (*s++)
00218                         ; /* determine total src string length */
00219         }
00220 
00221         return s - src - 1;
00222 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:26:38