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 int
lws_plat_drop_app_privileges(struct lws_context * context,int actually_set)31 lws_plat_drop_app_privileges(struct lws_context *context, int actually_set)
32 {
33 return 0;
34 }
35
36 int
lws_plat_context_early_init(void)37 lws_plat_context_early_init(void)
38 {
39 WORD wVersionRequested;
40 WSADATA wsaData;
41 int err;
42
43 /* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
44 wVersionRequested = MAKEWORD(2, 2);
45
46 err = WSAStartup(wVersionRequested, &wsaData);
47 if (!err)
48 return 0;
49 /*
50 * Tell the user that we could not find a usable
51 * Winsock DLL
52 */
53 lwsl_err("WSAStartup failed with error: %d\n", err);
54
55 return 1;
56 }
57
58 int
lws_plat_init(struct lws_context * context,const struct lws_context_creation_info * info)59 lws_plat_init(struct lws_context *context,
60 const struct lws_context_creation_info *info)
61 {
62 struct lws_context_per_thread *pt = &context->pt[0];
63 int i, n = context->count_threads;
64
65 for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
66 context->fd_hashtable[i].wsi =
67 lws_zalloc(sizeof(struct lws*) * context->max_fds,
68 "win hashtable");
69
70 if (!context->fd_hashtable[i].wsi)
71 return -1;
72 }
73
74 while (n--) {
75 pt->fds_count = 0;
76 pt->events = WSACreateEvent(); /* the cancel event */
77 InitializeCriticalSection(&pt->interrupt_lock);
78
79 pt++;
80 }
81
82 context->fd_random = 0;
83
84 #ifdef LWS_WITH_PLUGINS
85 if (info->plugin_dirs)
86 lws_plat_plugins_init(context, info->plugin_dirs);
87 #endif
88
89 return 0;
90 }
91
92 void
lws_plat_context_early_destroy(struct lws_context * context)93 lws_plat_context_early_destroy(struct lws_context *context)
94 {
95 struct lws_context_per_thread *pt = &context->pt[0];
96 int n = context->count_threads;
97
98 while (n--) {
99 WSACloseEvent(pt->events);
100 DeleteCriticalSection(&pt->interrupt_lock);
101 pt++;
102 }
103 }
104
105 void
lws_plat_context_late_destroy(struct lws_context * context)106 lws_plat_context_late_destroy(struct lws_context *context)
107 {
108 int n;
109
110 for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
111 if (context->fd_hashtable[n].wsi)
112 lws_free(context->fd_hashtable[n].wsi);
113 }
114
115 WSACleanup();
116 }
117