lws-plat-optee.c 5.4 KB

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