summaryrefslogtreecommitdiff
path: root/res
diff options
context:
space:
mode:
authorDavid M. Lee <dlee@digium.com>2013-08-30 13:40:27 +0000
committerDavid M. Lee <dlee@digium.com>2013-08-30 13:40:27 +0000
commit9bed50db41690339ca64bdcc6d6bc1c6edfebe4a (patch)
tree7316204731ab8415ce3ada9a51589c65dacdff58 /res
parent7f547872e41ee53ad85a5b0820a8c0ff7c7f1c04 (diff)
optional_api: Fix linking problems between modules that export global symbols
With the new work in Asterisk 12, there are some uses of the optional_api that are prone to failure. The details are rather involved, and captured on [the wiki][1]. This patch addresses the issue by removing almost all of the magic from the optional API implementation. Instead of relying on weak symbol resolution, a new optional_api.c module was added to Asterisk core. For modules providing an optional API, the pointer to the implementation function is registered with the core. For modules that use an optional API, a pointer to a stub function, along with a optional_ref function pointer are registered with the core. The optional_ref function pointers is set to the implementation function when it's provided, or the stub function when it's now. Since the implementation no longer relies on magic, it is now supported on all platforms. In the spirit of choice, an OPTIONAL_API flag was added, so we can disable the optional_api if needed (maybe it's buggy on some bizarre platform I haven't tested on) The AST_OPTIONAL_API*() macros themselves remained unchanged, so existing code could remain unchanged. But to help with debugging the optional_api, the patch limits the #include of optional API's to just the modules using the API. This also reduces resource waste maintaining optional_ref pointers that aren't used. Other changes made as a part of this patch: * The stubs for http_websocket that wrap system calls set errno to ENOSYS. * res_http_websocket now properly increments module use count. * In loader.c, the while() wrappers around dlclose() were removed. The while(!dlclose()) is actually an anti-pattern, which can lead to infinite loops if the module you're attempting to unload exports a symbol that was directly linked to. * The special handling of nonoptreq on systems without weak symbol support was removed, since we no longer rely on weak symbols for optional_api. [1]: https://wiki.asterisk.org/wiki/x/wACUAQ (closes issue ASTERISK-22296) Reported by: Matt Jordan Review: https://reviewboard.asterisk.org/r/2797/ ........ Merged revisions 397989 from http://svn.asterisk.org/svn/asterisk/branches/12 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@397990 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res')
-rw-r--r--res/ari/ari_websockets.c16
-rw-r--r--res/ari/internal.h22
-rw-r--r--res/res_ari.c11
-rw-r--r--res/res_ari_events.c1
-rw-r--r--res/res_http_websocket.c55
5 files changed, 88 insertions, 17 deletions
diff --git a/res/ari/ari_websockets.c b/res/ari/ari_websockets.c
index a34e0f691..20639e12e 100644
--- a/res/ari/ari_websockets.c
+++ b/res/ari/ari_websockets.c
@@ -20,8 +20,10 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-#include "asterisk/astobj2.h"
#include "asterisk/ari.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/http_websocket.h"
+#include "internal.h"
/*! \file
*
@@ -163,3 +165,15 @@ int ast_ari_websocket_session_write(struct ast_ari_websocket_session *session,
return ast_websocket_write(session->ws_session,
AST_WEBSOCKET_OPCODE_TEXT, str, strlen(str));
}
+
+void ari_handle_websocket(struct ast_websocket_server *ws_server,
+ struct ast_tcptls_session_instance *ser, const char *uri,
+ enum ast_http_method method, struct ast_variable *get_params,
+ struct ast_variable *headers)
+{
+ struct ast_http_uri fake_urih = {
+ .data = ws_server,
+ };
+ ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
+ headers);
+}
diff --git a/res/ari/internal.h b/res/ari/internal.h
index ffacd4939..8453747f1 100644
--- a/res/ari/internal.h
+++ b/res/ari/internal.h
@@ -25,7 +25,9 @@
* \author David M. Lee, II <dlee@digium.com>
*/
+#include "asterisk/http.h"
#include "asterisk/json.h"
+#include "asterisk/stringfields.h"
/*! @{ */
@@ -139,5 +141,25 @@ struct ast_ari_conf_user *ast_ari_config_validate_user(const char *username,
/*! @} */
+/* Forward-declare websocket structs. This avoids including http_websocket.h,
+ * which causes optional_api stuff to happen, which makes optional_api more
+ * difficult to debug. */
+
+struct ast_websocket_server;
+
+/*!
+ * \brief Wrapper for invoking the websocket code for an incoming connection.
+ *
+ * \param ws_server WebSocket server to invoke.
+ * \param ser HTTP session.
+ * \param uri Requested URI.
+ * \param method Requested HTTP method.
+ * \param get_params Parsed query parameters.
+ * \param headers Parsed HTTP headers.
+ */
+void ari_handle_websocket(struct ast_websocket_server *ws_server,
+ struct ast_tcptls_session_instance *ser, const char *uri,
+ enum ast_http_method method, struct ast_variable *get_params,
+ struct ast_variable *headers);
#endif /* ARI_INTERNAL_H_ */
diff --git a/res/res_ari.c b/res/res_ari.c
index 4777bb7c2..5475efce9 100644
--- a/res/res_ari.c
+++ b/res/res_ari.c
@@ -124,11 +124,11 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+#include "ari/internal.h"
+#include "asterisk/ari.h"
#include "asterisk/astobj2.h"
#include "asterisk/module.h"
#include "asterisk/paths.h"
-#include "asterisk/ari.h"
-#include "ari/internal.h"
#include <string.h>
#include <sys/stat.h>
@@ -522,11 +522,8 @@ void ast_ari_invoke(struct ast_tcptls_session_instance *ser,
if (handler->ws_server && method == AST_HTTP_GET) {
/* WebSocket! */
- struct ast_http_uri fake_urih = {
- .data = handler->ws_server,
- };
- ast_websocket_uri_cb(ser, &fake_urih, uri, method, get_params,
- headers);
+ ari_handle_websocket(handler->ws_server, ser, uri, method,
+ get_params, headers);
/* Since the WebSocket code handles the connection, we shouldn't
* do anything else; setting no_response */
response->no_response = 1;
diff --git a/res/res_ari_events.c b/res/res_ari_events.c
index 567167f14..17337bbc8 100644
--- a/res/res_ari_events.c
+++ b/res/res_ari_events.c
@@ -48,6 +48,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#if defined(AST_DEVMODE)
#include "ari/ari_model_validators.h"
#endif
+#include "asterisk/http_websocket.h"
#define MAX_VALS 128
diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c
index 129e92c19..b03745e6f 100644
--- a/res/res_http_websocket.c
+++ b/res/res_http_websocket.c
@@ -107,18 +107,24 @@ struct ast_websocket_server {
struct ao2_container *protocols; /*!< Container for registered protocols */
};
-static void websocket_server_dtor(void *obj)
+static void websocket_server_internal_dtor(void *obj)
{
struct ast_websocket_server *server = obj;
ao2_cleanup(server->protocols);
server->protocols = NULL;
}
-struct ast_websocket_server *ast_websocket_server_create(void)
+static void websocket_server_dtor(void *obj)
+{
+ websocket_server_internal_dtor(obj);
+ ast_module_unref(ast_module_info->self);
+}
+
+static struct ast_websocket_server *websocket_server_create_impl(void (*dtor)(void *))
{
RAII_VAR(struct ast_websocket_server *, server, NULL, ao2_cleanup);
- server = ao2_alloc(sizeof(*server), websocket_server_dtor);
+ server = ao2_alloc(sizeof(*server), dtor);
if (!server) {
return NULL;
}
@@ -132,6 +138,17 @@ struct ast_websocket_server *ast_websocket_server_create(void)
return server;
}
+static struct ast_websocket_server *websocket_server_internal_create(void)
+{
+ return websocket_server_create_impl(websocket_server_internal_dtor);
+}
+
+struct ast_websocket_server *AST_OPTIONAL_API_NAME(ast_websocket_server_create)(void)
+{
+ ast_module_ref(ast_module_info->self);
+ return websocket_server_create_impl(websocket_server_dtor);
+}
+
/*! \brief Destructor function for sessions */
static void session_destroy_fn(void *obj)
{
@@ -512,7 +529,7 @@ static struct websocket_protocol *one_protocol(
return ao2_callback(server->protocols, OBJ_NOLOCK, NULL, NULL);
}
-int ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
+int AST_OPTIONAL_API_NAME(ast_websocket_uri_cb)(struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_vars, struct ast_variable *headers)
{
struct ast_variable *v;
char *upgrade = NULL, *key = NULL, *key1 = NULL, *key2 = NULL, *protos = NULL, *requested_protocols = NULL, *protocol = NULL;
@@ -521,6 +538,8 @@ int ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct a
struct ast_websocket *session;
struct ast_websocket_server *server;
+ SCOPED_MODULE_USE(ast_module_info->self);
+
/* Upgrade requests are only permitted on GET methods */
if (method != AST_HTTP_GET) {
ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
@@ -674,7 +693,7 @@ int ast_websocket_uri_cb(struct ast_tcptls_session_instance *ser, const struct a
}
static struct ast_http_uri websocketuri = {
- .callback = ast_websocket_uri_cb,
+ .callback = AST_OPTIONAL_API_NAME(ast_websocket_uri_cb),
.description = "Asterisk HTTP WebSocket",
.uri = "ws",
.has_subtree = 0,
@@ -719,7 +738,7 @@ end:
ast_websocket_unref(session);
}
-int AST_OPTIONAL_API_NAME(ast_websocket_add_protocol)(const char *name, ast_websocket_callback callback)
+static int websocket_add_protocol_internal(const char *name, ast_websocket_callback callback)
{
struct ast_websocket_server *ws_server = websocketuri.data;
if (!ws_server) {
@@ -728,7 +747,16 @@ int AST_OPTIONAL_API_NAME(ast_websocket_add_protocol)(const char *name, ast_webs
return ast_websocket_server_add_protocol(ws_server, name, callback);
}
-int AST_OPTIONAL_API_NAME(ast_websocket_remove_protocol)(const char *name, ast_websocket_callback callback)
+int AST_OPTIONAL_API_NAME(ast_websocket_add_protocol)(const char *name, ast_websocket_callback callback)
+{
+ int res = websocket_add_protocol_internal(name, callback);
+ if (res == 0) {
+ ast_module_ref(ast_module_info->self);
+ }
+ return res;
+}
+
+static int websocket_remove_protocol_internal(const char *name, ast_websocket_callback callback)
{
struct ast_websocket_server *ws_server = websocketuri.data;
if (!ws_server) {
@@ -737,14 +765,23 @@ int AST_OPTIONAL_API_NAME(ast_websocket_remove_protocol)(const char *name, ast_w
return ast_websocket_server_remove_protocol(ws_server, name, callback);
}
+int AST_OPTIONAL_API_NAME(ast_websocket_remove_protocol)(const char *name, ast_websocket_callback callback)
+{
+ int res = websocket_remove_protocol_internal(name, callback);
+ if (res == 0) {
+ ast_module_unref(ast_module_info->self);
+ }
+ return res;
+}
+
static int load_module(void)
{
- websocketuri.data = ast_websocket_server_create();
+ websocketuri.data = websocket_server_internal_create();
if (!websocketuri.data) {
return AST_MODULE_LOAD_FAILURE;
}
ast_http_uri_link(&websocketuri);
- ast_websocket_add_protocol("echo", websocket_echo_callback);
+ websocket_add_protocol_internal("echo", websocket_echo_callback);
return 0;
}