Imported Upstream version 3.2.0
[platform/upstream/libwebsockets.git] / lib / plat / windows / windows-misc.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2018 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
23 #define _WINSOCK_DEPRECATED_NO_WARNINGS
24 #endif
25 #include "core/private.h"
26
27
28 lws_usec_t
29 lws_now_usecs(void)
30 {
31 #ifndef DELTA_EPOCH_IN_MICROSECS
32 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
33 #endif
34         FILETIME filetime;
35         ULARGE_INTEGER datetime;
36
37 #ifdef _WIN32_WCE
38         GetCurrentFT(&filetime);
39 #else
40         GetSystemTimeAsFileTime(&filetime);
41 #endif
42
43         /*
44          * As per Windows documentation for FILETIME, copy the resulting
45          * FILETIME structure to a ULARGE_INTEGER structure using memcpy
46          * (using memcpy instead of direct assignment can prevent alignment
47          * faults on 64-bit Windows).
48          */
49         memcpy(&datetime, &filetime, sizeof(datetime));
50
51         /* Windows file times are in 100s of nanoseconds. */
52         return (datetime.QuadPart / 10) - DELTA_EPOCH_IN_MICROSECS;
53 }
54
55
56 #ifdef _WIN32_WCE
57 time_t time(time_t *t)
58 {
59         time_t ret = lws_now_usecs() / 1000000;
60
61         if(t != NULL)
62                 *t = ret;
63
64         return ret;
65 }
66 #endif
67
68 LWS_VISIBLE int
69 lws_get_random(struct lws_context *context, void *buf, int len)
70 {
71         int n;
72         char *p = (char *)buf;
73
74         for (n = 0; n < len; n++)
75                 p[n] = (unsigned char)rand();
76
77         return n;
78 }
79
80
81 LWS_VISIBLE void
82 lwsl_emit_syslog(int level, const char *line)
83 {
84         lwsl_emit_stderr(level, line);
85 }
86
87
88 int kill(int pid, int sig)
89 {
90         lwsl_err("Sorry Windows doesn't support kill().");
91         exit(0);
92 }
93
94 int fork(void)
95 {
96         lwsl_err("Sorry Windows doesn't support fork().");
97         exit(0);
98 }
99
100
101 int
102 lws_plat_recommended_rsa_bits(void)
103 {
104         return 4096;
105 }
106
107
108