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 #include "private-lib-core.h"
26
27 #if !defined(LWS_WITH_NETWORK)
28 #include <crypto/crypto.h>
29 #endif
30
31 int errno;
32
33 #if !defined(LWS_WITH_NETWORK)
34 char *
strcpy(char * dest,const char * src)35 strcpy(char *dest, const char *src)
36 {
37 char *desto = dest;
38
39 while (*src)
40 *(dest++) = *(src++);
41
42 *(dest++) = '\0';
43
44 return desto;
45 }
46
strncpy(char * dest,const char * src,size_t limit)47 char *strncpy(char *dest, const char *src, size_t limit)
48 {
49 char *desto = dest;
50
51 while (*src && limit--)
52 *(dest++) = *(src++);
53
54 if (limit)
55 *(dest++) = '\0';
56
57 return desto;
58 }
59
60 #endif
61
lws_plat_apply_FD_CLOEXEC(int n)62 int lws_plat_apply_FD_CLOEXEC(int n)
63 {
64 return 0;
65 }
66
67 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen);
68 #if defined(LWS_WITH_NETWORK)
69 uint64_t
lws_now_usecs(void)70 lws_now_usecs(void)
71 {
72 return ((unsigned long long)time(NULL)) * 1000000;
73 }
74 #endif
75
76 size_t
lws_get_random(struct lws_context * context,void * buf,size_t len)77 lws_get_random(struct lws_context *context, void *buf, size_t len)
78 {
79 #if defined(LWS_WITH_NETWORK)
80 TEE_GenerateRandom(buf, len);
81 #else
82 crypto_rng_read(buf, len);
83 #endif
84
85 return len;
86 }
87
88
89 static const char * const colours[] = {
90 "[31;1m", /* LLL_ERR */
91 "[36;1m", /* LLL_WARN */
92 "[35;1m", /* LLL_NOTICE */
93 "[32;1m", /* LLL_INFO */
94 "[34;1m", /* LLL_DEBUG */
95 "[33;1m", /* LLL_PARSER */
96 "[33;1m", /* LLL_HEADER */
97 "[33;1m", /* LLL_EXT */
98 "[33;1m", /* LLL_CLIENT */
99 "[33;1m", /* LLL_LATENCY */
100 "[30;1m", /* LLL_USER */
101 };
102
lwsl_emit_optee(int level,const char * line)103 void lwsl_emit_optee(int level, const char *line)
104 {
105 char buf[50], linecp[512];
106 int n, m = LWS_ARRAY_SIZE(colours) - 1;
107
108 lwsl_timestamp(level, buf, sizeof(buf));
109
110 n = 1 << (LWS_ARRAY_SIZE(colours) - 1);
111 while (n) {
112 if (level & n)
113 break;
114 m--;
115 n >>= 1;
116 }
117 n = strlen(line);
118 if ((unsigned int)n > sizeof(linecp) - 1)
119 n = sizeof(linecp) - 1;
120 if (n) {
121 memcpy(linecp, line, n - 1);
122 linecp[n - 1] = '\0';
123 } else
124 linecp[0] = '\0';
125 EMSG("%c%s%s%s%c[0m", 27, colours[m], buf, linecp, 27);
126 }
127
128 int
lws_plat_set_nonblocking(lws_sockfd_type fd)129 lws_plat_set_nonblocking(lws_sockfd_type fd)
130 {
131 return 0;
132 }
133
134 int
lws_plat_drop_app_privileges(struct lws_context * context,int actually_init)135 lws_plat_drop_app_privileges(struct lws_context *context, int actually_init)
136 {
137 return 0;
138 }
139
140 int
lws_plat_context_early_init(void)141 lws_plat_context_early_init(void)
142 {
143 return 0;
144 }
145
146 void
lws_plat_context_early_destroy(struct lws_context * context)147 lws_plat_context_early_destroy(struct lws_context *context)
148 {
149 }
150
151 void
lws_plat_context_late_destroy(struct lws_context * context)152 lws_plat_context_late_destroy(struct lws_context *context)
153 {
154 #if defined(LWS_WITH_NETWORK)
155 if (context->lws_lookup)
156 lws_free(context->lws_lookup);
157 #endif
158 }
159
160 lws_fop_fd_t
_lws_plat_file_open(const struct lws_plat_file_ops * fops,const char * filename,const char * vpath,lws_fop_flags_t * flags)161 _lws_plat_file_open(const struct lws_plat_file_ops *fops,
162 const char *filename, const char *vpath, lws_fop_flags_t *flags)
163 {
164 return NULL;
165 }
166
167 int
_lws_plat_file_close(lws_fop_fd_t * fop_fd)168 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
169 {
170 return 0;
171 }
172
173 lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd,lws_fileofs_t offset)174 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
175 {
176 return 0;
177 }
178
179 int
_lws_plat_file_read(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)180 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
181 uint8_t *buf, lws_filepos_t len)
182 {
183
184 return 0;
185 }
186
187 int
_lws_plat_file_write(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)188 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
189 uint8_t *buf, lws_filepos_t len)
190 {
191
192 return 0;
193 }
194
195
196 int
lws_plat_init(struct lws_context * context,const struct lws_context_creation_info * info)197 lws_plat_init(struct lws_context *context,
198 const struct lws_context_creation_info *info)
199 {
200 #if defined(LWS_WITH_NETWORK)
201 /* context has the global fd lookup array */
202 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
203 context->max_fds, "lws_lookup");
204 if (context->lws_lookup == NULL) {
205 lwsl_err("OOM on lws_lookup array for %d connections\n",
206 context->max_fds);
207 return 1;
208 }
209
210 lwsl_notice(" mem: platform fd map: %5lu bytes\n",
211 (long)sizeof(struct lws *) * context->max_fds);
212 #endif
213 #ifdef LWS_WITH_PLUGINS
214 if (info->plugin_dirs)
215 lws_plat_plugins_init(context, info->plugin_dirs);
216 #endif
217
218 return 0;
219 }
220
221 int
lws_plat_write_file(const char * filename,void * buf,size_t len)222 lws_plat_write_file(const char *filename, void *buf, size_t len)
223 {
224 return 1;
225 }
226
227 int
lws_plat_read_file(const char * filename,void * buf,int len)228 lws_plat_read_file(const char *filename, void *buf, int len)
229 {
230 return -1;
231 }
232
233 int
lws_plat_recommended_rsa_bits(void)234 lws_plat_recommended_rsa_bits(void)
235 {
236 return 4096;
237 }
238
239 int
lws_plat_ntpclient_config(struct lws_context * context)240 lws_plat_ntpclient_config(struct lws_context *context)
241 {
242 #if 0
243 char *ntpsrv = getenv("LWS_NTP_SERVER");
244
245 if (ntpsrv && strlen(ntpsrv) < 64) {
246 lws_system_blob_heap_append(lws_system_get_blob(context,
247 LWS_SYSBLOB_TYPE_NTP_SERVER, 0),
248 (const uint8_t *)ntpsrv,
249 strlen(ntpsrv));
250 return 1;
251 }
252 #endif
253 return 0;
254 }
255
256 void
lws_msleep(unsigned int ms)257 lws_msleep(unsigned int ms)
258 {
259 }
260
261
262