From 95fbe705031e3dd6e4b903c1d4b2c77c76264690 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Thu, 9 Feb 2006 16:28:49 +0000 Subject: [PATCH] Initial framework for directory interface modules (ldap etc) git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@575 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 54 ++++++++++++++++++++++++++ src/include/switch_loadable_module.h | 9 +++++ src/include/switch_module_interfaces.h | 33 ++++++++++++++++ src/include/switch_types.h | 15 +++++++ src/switch_core.c | 46 ++++++++++++++++++++++ src/switch_loadable_module.c | 16 ++++++++ 6 files changed, 173 insertions(+) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index e6fa2240ee..3098f7bf2c 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -818,6 +818,60 @@ SWITCH_DECLARE(switch_status) switch_core_speech_feed_tts(switch_speech_handle * SWITCH_DECLARE(switch_status) switch_core_speech_close(switch_speech_handle *sh, unsigned int *flags); ///\} + +///\defgroup dir Directory Service Functions +///\ingroup core1 +///\{ +/*! + \brief Open a directory handle + \param dh a direcotry handle to use + \param module_name the directory module to use + \param source the source of the db (ip, hostname, path etc) + \param dsn the username or designation of the lookup + \param passwd the password + \param pool the pool to use (NULL for new pool) + \return SWITCH_STATUS_SUCCESS if the handle is opened +*/ +SWITCH_DECLARE(switch_status) switch_core_directory_open(switch_directory_handle *dh, + char *module_name, + char *source, + char *dsn, + char *passwd, + switch_memory_pool *pool); + +/*! + \brief Query a directory handle + \param dh a direcotry handle to use + \param query a string of filters or query data + \return SWITCH_STATUS_SUCCESS if the query is successful +*/ +SWITCH_DECLARE(switch_status) switch_core_directory_query(switch_directory_handle *dh, char *query); + +/*! + \brief Obtain the next record in a lookup + \param dh a direcotry handle to use + \return SWITCH_STATUS_SUCCESS if another record exists +*/ +SWITCH_DECLARE(switch_status) switch_core_directory_next(switch_directory_handle *dh); + +/*! + \brief Obtain the next name/value pair in the current record + \param dh a direcotry handle to use + \param var a pointer to pointer of the name to fill in + \param val a pointer to poinbter of the value to fill in + \return SWITCH_STATUS_SUCCESS if an item exists +*/ +SWITCH_DECLARE(switch_status) switch_core_directory_next_pair(switch_directory_handle *dh, char **var, char **val); + +/*! + \brief Close an open directory handle + \param dh a direcotry handle to close + \return SWITCH_STATUS_SUCCESS if handle was closed +*/ +SWITCH_DECLARE(switch_status) switch_core_directory_close(switch_directory_handle *dh); +///\} + + ///\defgroup misc Misc ///\ingroup core1 ///\{ diff --git a/src/include/switch_loadable_module.h b/src/include/switch_loadable_module.h index abc6ab1f4a..5f39f473e3 100644 --- a/src/include/switch_loadable_module.h +++ b/src/include/switch_loadable_module.h @@ -73,6 +73,8 @@ struct switch_loadable_module_interface { const switch_file_interface *file_interface; /*! the table of speech interfaces the module has implmented */ const switch_speech_interface *speech_interface; + /*! the table of directory interfaces the module has implmented */ + const switch_directory_interface *directory_interface; }; /*! @@ -142,6 +144,13 @@ SWITCH_DECLARE(switch_file_interface *) switch_loadable_module_get_file_interfac */ SWITCH_DECLARE(switch_speech_interface *) switch_loadable_module_get_speech_interface(char *name); +/*! + \brief Retrieve the directory interface by it's registered name + \param name the name of the directory interface + \return the desired directory interface + */ +SWITCH_DECLARE(switch_directory_interface *) switch_loadable_module_get_directory_interface(char *name); + /*! \brief Retrieve the list of loaded codecs into an array diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h index 30dd8a9f43..098a789c2c 100644 --- a/src/include/switch_module_interfaces.h +++ b/src/include/switch_module_interfaces.h @@ -320,6 +320,39 @@ struct switch_speech_handle { }; + +/*! \brief Abstract interface to a directory module */ +struct switch_directory_interface { + /*! the name of the interface */ + const char *interface_name; + /*! function to open the directory interface */ + switch_status (*directory_open)(switch_directory_handle *dh, char *source, char *dsn, char *passwd); + /*! function to close the directory interface */ + switch_status (*directory_close)(switch_directory_handle *dh); + /*! function to query the directory interface */ + switch_status (*directory_query)(switch_directory_handle *dh, char *query); + /*! function to advance to the next record */ + switch_status (*directory_next)(switch_directory_handle *dh); + /*! function to advance to the next name/value pair in the current record */ + switch_status (*directory_next_pair)(switch_directory_handle *dh, char **var, char **val); + + const struct switch_directory_interface *next; +}; + +/*! an abstract representation of a directory interface. */ +struct switch_directory_handle { + /*! the interface of the module that implemented the current directory interface */ + const struct switch_directory_interface *directory_interface; + /*! flags to control behaviour */ + unsigned int flags; + + /*! the handle's memory pool */ + switch_memory_pool *memory_pool; + /*! private data for the format module to store handle specific info */ + void *private; +}; + + /* nobody has more setting than speex so we will let them set the standard */ /*! \brief Various codec settings (currently only relevant to speex) */ struct switch_codec_settings { diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 7b549c39c5..3777248c58 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -241,6 +241,19 @@ typedef enum { } switch_speech_flag; + +/*! + \enum switch_directory_flag + \brief Directory Handle related flags +
+SWITCH_DIRECTORY_FLAG_FREE_POOL =		(1 <<  0) - Free interface's pool on destruction.
+
+*/ +typedef enum { + SWITCH_DIRECTORY_FLAG_FREE_POOL = (1 << 0), + +} switch_directory_flag; + /*! \enum switch_codec_type \brief Codec types @@ -380,6 +393,8 @@ typedef struct switch_codec_settings switch_codec_settings; typedef struct switch_config switch_config; typedef struct switch_speech_handle switch_speech_handle; typedef struct switch_speech_interface switch_speech_interface; +typedef struct switch_directory_handle switch_directory_handle; +typedef struct switch_directory_interface switch_directory_interface; typedef void (*switch_application_function)(switch_core_session *, char *); typedef void (*switch_event_callback_t)(switch_event *); typedef switch_caller_extension *(*switch_dialplan_hunt_function)(switch_core_session *); diff --git a/src/switch_core.c b/src/switch_core.c index 911d261b0a..d67ba3689e 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -444,6 +444,52 @@ SWITCH_DECLARE(switch_status) switch_core_file_close(switch_file_handle *fh) return fh->file_interface->file_close(fh); } +SWITCH_DECLARE(switch_status) switch_core_directory_open(switch_directory_handle *dh, + char *module_name, + char *source, + char *dsn, + char *passwd, + switch_memory_pool *pool) +{ + switch_status status; + + if (!(dh->directory_interface = switch_loadable_module_get_directory_interface(module_name))) { + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "invalid directory module [%s]!\n", module_name); + return SWITCH_STATUS_GENERR; + } + + if (pool) { + dh->memory_pool = pool; + } else { + if ((status = switch_core_new_memory_pool(&dh->memory_pool)) != SWITCH_STATUS_SUCCESS) { + return status; + } + switch_set_flag(dh, SWITCH_DIRECTORY_FLAG_FREE_POOL); + } + + return dh->directory_interface->directory_open(dh, source, dsn, passwd); +} + +SWITCH_DECLARE(switch_status) switch_core_directory_query(switch_directory_handle *dh, char *query) +{ + return dh->directory_interface->directory_query(dh, query); +} + +SWITCH_DECLARE(switch_status) switch_core_directory_next(switch_directory_handle *dh) +{ + return dh->directory_interface->directory_next(dh); +} + +SWITCH_DECLARE(switch_status) switch_core_directory_next_pair(switch_directory_handle *dh, char **var, char **val) +{ + return dh->directory_interface->directory_next_pair(dh, var, val); +} + +SWITCH_DECLARE(switch_status) switch_core_directory_close(switch_directory_handle *dh) +{ + return dh->directory_interface->directory_close(dh); +} + SWITCH_DECLARE(switch_status) switch_core_speech_open(switch_speech_handle *sh, char *module_name, unsigned int flags, diff --git a/src/switch_loadable_module.c b/src/switch_loadable_module.c index 62ac57868e..c2c283960a 100644 --- a/src/switch_loadable_module.c +++ b/src/switch_loadable_module.c @@ -63,6 +63,7 @@ struct switch_loadable_module_container { switch_hash *api_hash; switch_hash *file_hash; switch_hash *speech_hash; + switch_hash *directory_hash; switch_memory_pool *pool; }; @@ -220,6 +221,7 @@ SWITCH_DECLARE(switch_status) switch_loadable_module_init() switch_core_hash_init(&loadable_modules.api_hash, loadable_modules.pool); switch_core_hash_init(&loadable_modules.file_hash, loadable_modules.pool); switch_core_hash_init(&loadable_modules.speech_hash, loadable_modules.pool); + switch_core_hash_init(&loadable_modules.directory_hash, loadable_modules.pool); switch_core_hash_init(&loadable_modules.dialplan_hash, loadable_modules.pool); while (apr_dir_read(&finfo, finfo_flags, module_dir_handle) == APR_SUCCESS) { @@ -331,6 +333,15 @@ SWITCH_DECLARE(switch_status) switch_loadable_module_init() switch_core_hash_insert(loadable_modules.speech_hash, (char *) ptr->interface_name, (void *) ptr); } } + + if (new_module->interface->directory_interface) { + const switch_directory_interface *ptr; + + for (ptr = new_module->interface->directory_interface; ptr; ptr = ptr->next) { + switch_console_printf(SWITCH_CHANNEL_CONSOLE, "Adding Directory interface '%s'\n", ptr->interface_name); + switch_core_hash_insert(loadable_modules.directory_hash, (char *) ptr->interface_name, (void *) ptr); + } + } } @@ -400,6 +411,11 @@ SWITCH_DECLARE(switch_speech_interface *) switch_loadable_module_get_speech_inte return switch_core_hash_find(loadable_modules.speech_hash, name); } +SWITCH_DECLARE(switch_directory_interface *) switch_loadable_module_get_directory_interface(char *name) +{ + return switch_core_hash_find(loadable_modules.directory_hash, name); +} + SWITCH_DECLARE(int) switch_loadable_module_get_codecs(switch_memory_pool *pool, switch_codec_interface **array, int arraylen) {