• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(LWS_WITH_PLUGINS)
59 static int
protocol_plugin_cb(struct lws_plugin * pin,void * each_user)60 protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
61 {
62 	struct lws_context *context = (struct lws_context *)each_user;
63 	const lws_plugin_protocol_t *plpr =
64 			(const lws_plugin_protocol_t *)pin->hdr;
65 
66 	context->plugin_protocol_count += plpr->count_protocols;
67 	context->plugin_extension_count += plpr->count_extensions;
68 
69 	return 0;
70 }
71 #endif
72 
73 int
lws_plat_init(struct lws_context * context,const struct lws_context_creation_info * info)74 lws_plat_init(struct lws_context *context,
75 	      const struct lws_context_creation_info *info)
76 {
77 	struct lws_context_per_thread *pt = &context->pt[0];
78 	int i, n = context->count_threads;
79 
80 #if defined(LWS_WITH_MBEDTLS)
81 	{
82 		int n;
83 
84 		/* initialize platform random through mbedtls */
85 		mbedtls_entropy_init(&context->mec);
86 		mbedtls_ctr_drbg_init(&context->mcdc);
87 
88 		n = mbedtls_ctr_drbg_seed(&context->mcdc, mbedtls_entropy_func,
89 					  &context->mec, NULL, 0);
90 		if (n)
91 			lwsl_err("%s: mbedtls_ctr_drbg_seed() returned 0x%x\n",
92 				 __func__, n);
93 #if 0
94 		else {
95 			uint8_t rtest[16];
96 			lwsl_notice("%s: started drbg\n", __func__);
97 			if (mbedtls_ctr_drbg_random(&context->mcdc, rtest,
98 							sizeof(rtest)))
99 				lwsl_err("%s: get random failed\n", __func__);
100 			else
101 				lwsl_hexdump_notice(rtest, sizeof(rtest));
102 		}
103 #endif
104 	}
105 #endif
106 
107 	for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
108 		context->fd_hashtable[i].wsi =
109 			lws_zalloc(sizeof(struct lws*) * context->max_fds,
110 				   "win hashtable");
111 
112 		if (!context->fd_hashtable[i].wsi)
113 			return -1;
114 	}
115 
116 	while (n--) {
117 		pt->fds_count = 0;
118 
119 		pt++;
120 	}
121 
122 	context->fd_random = 0;
123 
124 #if defined(LWS_WITH_PLUGINS)
125 	if (info->plugin_dirs)
126 		lws_plat_plugins_init(&context->plugin_list, info->plugin_dirs,
127 				      "lws_protocol_plugin",
128 				      protocol_plugin_cb, context);
129 #endif
130 
131 	return 0;
132 }
133 
134 void
lws_plat_context_early_destroy(struct lws_context * context)135 lws_plat_context_early_destroy(struct lws_context *context)
136 {
137 
138 }
139 
140 void
lws_plat_context_late_destroy(struct lws_context * context)141 lws_plat_context_late_destroy(struct lws_context *context)
142 {
143 	int n;
144 
145 #ifdef LWS_WITH_PLUGINS
146 	if (context->plugin_list)
147 		lws_plugins_destroy(&context->plugin_list, NULL, NULL);
148 #endif
149 
150 	for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
151 		if (context->fd_hashtable[n].wsi)
152 			lws_free(context->fd_hashtable[n].wsi);
153 	}
154 
155 	WSACleanup();
156 }
157