2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "private-lib-core.h"
27 #if !defined(LWS_WITH_NETWORK)
28 #include <crypto/crypto.h>
33 #if !defined(LWS_WITH_NETWORK)
35 strcpy(char *dest, const char *src)
47 char *strncpy(char *dest, const char *src, size_t limit)
51 while (*src && limit--)
62 int lws_plat_apply_FD_CLOEXEC(int n)
67 void TEE_GenerateRandom(void *randomBuffer, uint32_t randomBufferLen);
68 #if defined(LWS_WITH_NETWORK)
72 return ((unsigned long long)time(NULL)) * 1000000;
77 lws_get_random(struct lws_context *context, void *buf, int len)
79 #if defined(LWS_WITH_NETWORK)
80 TEE_GenerateRandom(buf, len);
82 crypto_rng_read(buf, len);
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 */
103 void lwsl_emit_optee(int level, const char *line)
105 char buf[50], linecp[512];
106 int n, m = LWS_ARRAY_SIZE(colours) - 1;
108 lwsl_timestamp(level, buf, sizeof(buf));
110 n = 1 << (LWS_ARRAY_SIZE(colours) - 1);
118 if ((unsigned int)n > sizeof(linecp) - 1)
119 n = sizeof(linecp) - 1;
121 memcpy(linecp, line, n - 1);
122 linecp[n - 1] = '\0';
125 EMSG("%c%s%s%s%c[0m", 27, colours[m], buf, linecp, 27);
129 lws_plat_set_nonblocking(int fd)
135 lws_plat_drop_app_privileges(struct lws_context *context, int actually_init)
141 lws_plat_context_early_init(void)
147 lws_plat_context_early_destroy(struct lws_context *context)
152 lws_plat_context_late_destroy(struct lws_context *context)
154 #if defined(LWS_WITH_NETWORK)
155 if (context->lws_lookup)
156 lws_free(context->lws_lookup);
161 _lws_plat_file_open(const struct lws_plat_file_ops *fops,
162 const char *filename, const char *vpath, lws_fop_flags_t *flags)
168 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
174 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
180 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
181 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)
197 lws_plat_init(struct lws_context *context,
198 const struct lws_context_creation_info *info)
200 #if defined(LWS_WITH_NETWORK)
201 /* master 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",
210 lwsl_notice(" mem: platform fd map: %5lu bytes\n",
211 (long)sizeof(struct lws *) * context->max_fds);
213 #ifdef LWS_WITH_PLUGINS
214 if (info->plugin_dirs)
215 lws_plat_plugins_init(context, info->plugin_dirs);
222 lws_plat_write_file(const char *filename, void *buf, int len)
228 lws_plat_read_file(const char *filename, void *buf, int len)
234 lws_plat_recommended_rsa_bits(void)