1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
26 #define _WINSOCK_DEPRECATED_NO_WARNINGS
27 #endif
28 #include "private-lib-core.h"
29 
30 /*
31  * Normally you don't want this, use lws_sul instead inside the event loop.
32  * But sometimes for drivers it makes sense, so there's an internal-only
33  * crossplatform api for it.
34  */
35 
36 void
lws_msleep(unsigned int ms)37 lws_msleep(unsigned int ms)
38 {
39         Sleep(ms);
40 }
41 
42 lws_usec_t
lws_now_usecs(void)43 lws_now_usecs(void)
44 {
45 #ifndef DELTA_EPOCH_IN_MICROSECS
46 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
47 #endif
48 	FILETIME filetime;
49 	ULARGE_INTEGER datetime;
50 
51 #ifdef _WIN32_WCE
52 	GetCurrentFT(&filetime);
53 #else
54 	GetSystemTimeAsFileTime(&filetime);
55 #endif
56 
57 	/*
58 	 * As per Windows documentation for FILETIME, copy the resulting
59 	 * FILETIME structure to a ULARGE_INTEGER structure using memcpy
60 	 * (using memcpy instead of direct assignment can prevent alignment
61 	 * faults on 64-bit Windows).
62 	 */
63 	memcpy(&datetime, &filetime, sizeof(datetime));
64 
65 	/* Windows file times are in 100s of nanoseconds. */
66 	return (datetime.QuadPart / 10) - DELTA_EPOCH_IN_MICROSECS;
67 }
68 
69 
70 #ifdef _WIN32_WCE
time(time_t * t)71 time_t time(time_t *t)
72 {
73 	time_t ret = lws_now_usecs() / 1000000;
74 
75 	if(t != NULL)
76 		*t = ret;
77 
78 	return ret;
79 }
80 #endif
81 
82 size_t
lws_get_random(struct lws_context * context,void * buf,size_t len)83 lws_get_random(struct lws_context *context, void *buf, size_t len)
84 {
85 	size_t n;
86 	char *p = (char *)buf;
87 
88 	for (n = 0; n < len; n++)
89 		p[n] = (unsigned char)rand();
90 
91 	return n;
92 }
93 
94 
95 void
lwsl_emit_syslog(int level,const char * line)96 lwsl_emit_syslog(int level, const char *line)
97 {
98 	lwsl_emit_stderr(level, line);
99 }
100 
101 
kill(int pid,int sig)102 int kill(int pid, int sig)
103 {
104 	lwsl_err("Sorry Windows doesn't support kill().");
105 	exit(0);
106 }
107 
fork(void)108 int fork(void)
109 {
110 	lwsl_err("Sorry Windows doesn't support fork().");
111 	exit(0);
112 }
113 
114 
115 int
lws_plat_recommended_rsa_bits(void)116 lws_plat_recommended_rsa_bits(void)
117 {
118 	return 4096;
119 }
120 
121 
122 
123