~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to src/utils/os_win32.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 <winsock2.h>
17
 
#include <wincrypt.h>
18
 
 
19
 
#include "os.h"
20
 
 
21
 
void os_sleep(os_time_t sec, os_time_t usec)
22
 
{
23
 
        if (sec)
24
 
                Sleep(sec * 1000);
25
 
        if (usec)
26
 
                Sleep(usec / 1000);
27
 
}
28
 
 
29
 
 
30
 
int os_get_time(struct os_time *t)
31
 
{
32
 
#define EPOCHFILETIME (116444736000000000ULL)
33
 
        FILETIME ft;
34
 
        LARGE_INTEGER li;
35
 
        ULONGLONG tt;
36
 
 
37
 
#ifdef _WIN32_WCE
38
 
        SYSTEMTIME st;
39
 
 
40
 
        GetSystemTime(&st);
41
 
        SystemTimeToFileTime(&st, &ft);
42
 
#else /* _WIN32_WCE */
43
 
        GetSystemTimeAsFileTime(&ft);
44
 
#endif /* _WIN32_WCE */
45
 
        li.LowPart = ft.dwLowDateTime;
46
 
        li.HighPart = ft.dwHighDateTime;
47
 
        tt = (li.QuadPart - EPOCHFILETIME) / 10;
48
 
        t->sec = (os_time_t) (tt / 1000000);
49
 
        t->usec = (os_time_t) (tt % 1000000);
50
 
 
51
 
        return 0;
52
 
}
53
 
 
54
 
 
55
 
int os_mktime(int year, int month, int day, int hour, int min, int sec,
56
 
              os_time_t *t)
57
 
{
58
 
        struct tm tm;
59
 
 
60
 
        if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
61
 
            hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
62
 
            sec > 60)
63
 
                return -1;
64
 
 
65
 
        memset(&tm, 0, sizeof(tm));
66
 
        tm.tm_year = year - 1900;
67
 
        tm.tm_mon = month - 1;
68
 
        tm.tm_mday = day;
69
 
        tm.tm_hour = hour;
70
 
        tm.tm_min = min;
71
 
        tm.tm_sec = sec;
72
 
 
73
 
        *t = (os_time_t) mktime(&tm);
74
 
        return 0;
75
 
}
76
 
 
77
 
 
78
 
int os_daemonize(const char *pid_file)
79
 
{
80
 
        /* TODO */
81
 
        return -1;
82
 
}
83
 
 
84
 
 
85
 
void os_daemonize_terminate(const char *pid_file)
86
 
{
87
 
}
88
 
 
89
 
 
90
 
int os_get_random(unsigned char *buf, size_t len)
91
 
{
92
 
        HCRYPTPROV prov;
93
 
        BOOL ret;
94
 
 
95
 
        if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
96
 
                                 CRYPT_VERIFYCONTEXT))
97
 
                return -1;
98
 
 
99
 
        ret = CryptGenRandom(prov, len, buf);
100
 
        CryptReleaseContext(prov, 0);
101
 
 
102
 
        return ret ? 0 : -1;
103
 
}
104
 
 
105
 
 
106
 
unsigned long os_random(void)
107
 
{
108
 
        return rand();
109
 
}
110
 
 
111
 
 
112
 
char * os_rel2abs_path(const char *rel_path)
113
 
{
114
 
        return _strdup(rel_path);
115
 
}
116
 
 
117
 
 
118
 
int os_program_init(void)
119
 
{
120
 
#ifdef CONFIG_NATIVE_WINDOWS
121
 
        WSADATA wsaData;
122
 
        if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
123
 
                printf("Could not find a usable WinSock.dll\n");
124
 
                return -1;
125
 
        }
126
 
#endif /* CONFIG_NATIVE_WINDOWS */
127
 
        return 0;
128
 
}
129
 
 
130
 
 
131
 
void os_program_deinit(void)
132
 
{
133
 
#ifdef CONFIG_NATIVE_WINDOWS
134
 
        WSACleanup();
135
 
#endif /* CONFIG_NATIVE_WINDOWS */
136
 
}
137
 
 
138
 
 
139
 
int os_setenv(const char *name, const char *value, int overwrite)
140
 
{
141
 
        return -1;
142
 
}
143
 
 
144
 
 
145
 
int os_unsetenv(const char *name)
146
 
{
147
 
        return -1;
148
 
}
149
 
 
150
 
 
151
 
char * os_readfile(const char *name, size_t *len)
152
 
{
153
 
        FILE *f;
154
 
        char *buf;
155
 
 
156
 
        f = fopen(name, "rb");
157
 
        if (f == NULL)
158
 
                return NULL;
159
 
 
160
 
        fseek(f, 0, SEEK_END);
161
 
        *len = ftell(f);
162
 
        fseek(f, 0, SEEK_SET);
163
 
 
164
 
        buf = malloc(*len);
165
 
        if (buf == NULL) {
166
 
                fclose(f);
167
 
                return NULL;
168
 
        }
169
 
 
170
 
        fread(buf, 1, *len, f);
171
 
        fclose(f);
172
 
 
173
 
        return buf;
174
 
}
175
 
 
176
 
 
177
 
void * os_zalloc(size_t size)
178
 
{
179
 
        return calloc(1, size);
180
 
}
181
 
 
182
 
 
183
 
size_t os_strlcpy(char *dest, const char *src, size_t siz)
184
 
{
185
 
        const char *s = src;
186
 
        size_t left = siz;
187
 
 
188
 
        if (left) {
189
 
                /* Copy string up to the maximum size of the dest buffer */
190
 
                while (--left != 0) {
191
 
                        if ((*dest++ = *s++) == '\0')
192
 
                                break;
193
 
                }
194
 
        }
195
 
 
196
 
        if (left == 0) {
197
 
                /* Not enough room for the string; force NUL-termination */
198
 
                if (siz != 0)
199
 
                        *dest = '\0';
200
 
                while (*s++)
201
 
                        ; /* determine total src string length */
202
 
        }
203
 
 
204
 
        return s - src - 1;
205
 
}