windows-plugins.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
  25. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  26. #endif
  27. #include "private-lib-core.h"
  28. /*
  29. * ie, if the plugins api needed at all
  30. */
  31. #if defined(LWS_WITH_PLUGINS_API) && (UV_VERSION_MAJOR > 0)
  32. const lws_plugin_header_t *
  33. lws_plat_dlopen(struct lws_plugin **pplugin, const char *libpath,
  34. const char *sofilename, const char *_class,
  35. each_plugin_cb_t each, void *each_user)
  36. {
  37. const lws_plugin_header_t *hdr;
  38. struct lws_plugin *pin;
  39. char sym[96], *dot;
  40. uv_lib_t lib;
  41. void *v;
  42. int m;
  43. lib.errmsg = NULL;
  44. lib.handle = NULL;
  45. if (uv_dlopen(libpath, &lib)) {
  46. uv_dlerror(&lib);
  47. lwsl_err("Error loading DSO: %s\n", lib.errmsg);
  48. uv_dlclose(&lib);
  49. return NULL;
  50. }
  51. /* we could open it... can we get his export struct? */
  52. m = lws_snprintf(sym, sizeof(sym) - 1, "%s", sofilename);
  53. if (m < 4)
  54. goto bail;
  55. dot = strchr(sym, '.');
  56. if (dot)
  57. *dot = '\0'; /* snip the .so or .lib or what-have-you*/
  58. if (uv_dlsym(&lib, sym, &v)) {
  59. uv_dlerror(&lib);
  60. lwsl_err("%s: Failed to get '%s' on %s: %s\n",
  61. __func__, path, dent.name, lib.errmsg);
  62. goto bail;
  63. }
  64. hdr = (const lws_plugin_header_t *)v;
  65. if (hdr->api_magic != LWS_PLUGIN_API_MAGIC) {
  66. lwsl_err("%s: plugin %s has outdated api %d (vs %d)\n",
  67. __func__, libpath, hdr->api_magic,
  68. LWS_PLUGIN_API_MAGIC);
  69. goto bail;
  70. }
  71. if (strcmp(hdr->_class, _class))
  72. goto bail;
  73. pin = lws_malloc(sizeof(*pin), __func__);
  74. if (!pin)
  75. goto bail;
  76. pin->list = *pplugin;
  77. *pplugin = pin;
  78. pin->u.lib = lib;
  79. pin->hdr = hdr;
  80. if (each)
  81. each(pin, each_user);
  82. return hdr;
  83. bail:
  84. uv_dlclose(&lib);
  85. return NULL;
  86. }
  87. int
  88. lws_plat_destroy_dl(struct lws_plugin *p)
  89. {
  90. return uv_dlclose(&p->u.lib);
  91. }
  92. #endif
  93. /*
  94. * Specifically for protocol plugins support
  95. */
  96. #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
  97. static int
  98. protocol_plugin_cb(struct lws_plugin *pin, void *each_user)
  99. {
  100. struct lws_context *context = (struct lws_context *)each_user;
  101. const lws_plugin_protocol_t *plpr =
  102. (const lws_plugin_protocol_t *)pin->hdr;
  103. context->plugin_protocol_count += plpr->count_protocols;
  104. context->plugin_extension_count += plpr->count_extensions;
  105. return 0;
  106. }
  107. #endif
  108. int
  109. lws_plat_plugins_init(struct lws_context *context, const char * const *d)
  110. {
  111. #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
  112. if (info->plugin_dirs) {
  113. uv_loop_init(&context->uv.loop);
  114. lws_plugins_init(&context->plugin_list, info->plugin_dirs,
  115. "lws_protocol_plugin", NULL,
  116. protocol_plugin_cb, context);
  117. }
  118. #endif
  119. return 0;
  120. }
  121. int
  122. lws_plat_plugins_destroy(struct lws_context * context)
  123. {
  124. #if defined(LWS_WITH_PLUGINS) && (UV_VERSION_MAJOR > 0)
  125. if (lws_check_opt(context->options, LWS_SERVER_OPTION_LIBUV) &&
  126. context->plugin_list) {
  127. lws_plugins_destroy(&context->plugin_list, NULL, NULL);
  128. while (uv_loop_close(&context->uv.loop))
  129. ;
  130. }
  131. #endif
  132. return 0;
  133. }