diff options
Diffstat (limited to 'res')
-rw-r--r-- | res/res_config_pgsql.c | 50 | ||||
-rw-r--r-- | res/res_config_sqlite3.c | 219 | ||||
-rw-r--r-- | res/res_pjsip/pjsip_cli.c | 2 | ||||
-rw-r--r-- | res/res_sdp_translator_pjmedia.c | 577 |
4 files changed, 808 insertions, 40 deletions
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c index 25a482705..f0859617d 100644 --- a/res/res_config_pgsql.c +++ b/res/res_config_pgsql.c @@ -382,6 +382,9 @@ static struct columns *find_column(struct tables *t, const char *colname) return NULL; } +#define IS_SQL_LIKE_CLAUSE(x) ((x) && ast_ends_with(x, " LIKE")) +static char *ESCAPE_CLAUSE = " ESCAPE '\\'"; + static struct ast_variable *realtime_pgsql(const char *database, const char *tablename, const struct ast_variable *fields) { RAII_VAR(PGresult *, result, NULL, PQclear); @@ -391,6 +394,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab char *stringp; char *chunk; char *op; + char *escape = ""; const struct ast_variable *field = fields; struct ast_variable *var = NULL, *prev = NULL; @@ -418,7 +422,14 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab /* Create the first part of the query using the first parameter/value pairs we just extracted If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */ - op = strchr(field->name, ' ') ? "" : " ="; + if (!strchr(field->name, ' ')) { + op = " ="; + } else { + op = ""; + if (IS_SQL_LIKE_CLAUSE(field->name)) { + escape = ESCAPE_CLAUSE; + } + } ESCAPE_STRING(escapebuf, field->value); if (pgresult) { @@ -426,12 +437,17 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab return NULL; } - ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'", tablename, field->name, op, ast_str_buffer(escapebuf)); + ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'%s", tablename, field->name, op, ast_str_buffer(escapebuf), escape); while ((field = field->next)) { - if (!strchr(field->name, ' ')) + escape = ""; + if (!strchr(field->name, ' ')) { op = " ="; - else + } else { op = ""; + if (IS_SQL_LIKE_CLAUSE(field->name)) { + escape = ESCAPE_CLAUSE; + } + } ESCAPE_STRING(escapebuf, field->value); if (pgresult) { @@ -439,7 +455,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab return NULL; } - ast_str_append(&sql, 0, " AND %s%s '%s'", field->name, op, ast_str_buffer(escapebuf)); + ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(escapebuf), escape); } /* We now have our complete statement; Lets connect to the server and execute it. */ @@ -505,6 +521,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char char *stringp; char *chunk; char *op; + char *escape = ""; struct ast_variable *var = NULL; struct ast_config *cfg = NULL; struct ast_category *cat = NULL; @@ -543,10 +560,15 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char /* Create the first part of the query using the first parameter/value pairs we just extracted If there is only 1 set, then we have our query. Otherwise, loop thru the list and concat */ - if (!strchr(field->name, ' ')) + if (!strchr(field->name, ' ')) { op = " ="; - else + escape = ""; + } else { op = ""; + if (IS_SQL_LIKE_CLAUSE(field->name)) { + escape = ESCAPE_CLAUSE; + } + } ESCAPE_STRING(escapebuf, field->value); if (pgresult) { @@ -555,12 +577,18 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char return NULL; } - ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'", table, field->name, op, ast_str_buffer(escapebuf)); + ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'%s", table, field->name, op, ast_str_buffer(escapebuf), escape); while ((field = field->next)) { - if (!strchr(field->name, ' ')) + escape = ""; + if (!strchr(field->name, ' ')) { op = " ="; - else + escape = ""; + } else { op = ""; + if (IS_SQL_LIKE_CLAUSE(field->name)) { + escape = ESCAPE_CLAUSE; + } + } ESCAPE_STRING(escapebuf, field->value); if (pgresult) { @@ -569,7 +597,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char return NULL; } - ast_str_append(&sql, 0, " AND %s%s '%s'", field->name, op, ast_str_buffer(escapebuf)); + ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(escapebuf), escape); } if (initfield) { diff --git a/res/res_config_sqlite3.c b/res/res_config_sqlite3.c index f2a6b00db..8c514b07c 100644 --- a/res/res_config_sqlite3.c +++ b/res/res_config_sqlite3.c @@ -58,6 +58,8 @@ /*** DOCUMENTATION ***/ +static int has_explicit_like_escaping; + static struct ast_config *realtime_sqlite3_load(const char *database, const char *table, const char *configfile, struct ast_config *config, struct ast_flags flags, const char *suggested_include_file, const char *who_asked); static struct ast_variable *realtime_sqlite3(const char *database, const char *table, const struct ast_variable *fields); static struct ast_config *realtime_sqlite3_multi(const char *database, const char *table, const struct ast_variable *fields); @@ -113,7 +115,13 @@ AST_THREADSTORAGE(escape_table_buf); AST_THREADSTORAGE(escape_column_buf); AST_THREADSTORAGE(escape_value_buf); -static int realtime_sqlite3_execute_handle(struct realtime_sqlite3_db *db, const char *sql, int (*callback)(void*, int, char **, char **), void *arg, int sync); +typedef int (*callback_t)(void*, int, char **, char **); + +static int realtime_sqlite3_exec_query_with_handle(struct realtime_sqlite3_db *, const char *, callback_t, void *); +static int realtime_sqlite3_exec_query(const char *, const char *, callback_t, void *); +static int realtime_sqlite3_exec_update_with_handle(struct realtime_sqlite3_db *, const char *); +static int realtime_sqlite3_exec_update(const char *, const char *); + void db_start_batch(struct realtime_sqlite3_db *db); void db_stop_batch(struct realtime_sqlite3_db *db); @@ -301,20 +309,20 @@ static void *db_sync_thread(void *data) { struct realtime_sqlite3_db *db = data; ao2_lock(db); - realtime_sqlite3_execute_handle(db, "BEGIN TRANSACTION", NULL, NULL, 0); + realtime_sqlite3_exec_query_with_handle(db, "BEGIN TRANSACTION", NULL, NULL); for (;;) { if (!db->wakeup) { ast_cond_wait(&db->cond, ao2_object_get_lockaddr(db)); } db->wakeup = 0; - if (realtime_sqlite3_execute_handle(db, "COMMIT", NULL, NULL, 0) < 0) { - realtime_sqlite3_execute_handle(db, "ROLLBACK", NULL, NULL, 0); + if (realtime_sqlite3_exec_query_with_handle(db, "COMMIT", NULL, NULL) < 0) { + realtime_sqlite3_exec_query_with_handle(db, "ROLLBACK", NULL, NULL); } if (db->exiting) { ao2_unlock(db); break; } - realtime_sqlite3_execute_handle(db, "BEGIN TRANSACTION", NULL, NULL, 0); + realtime_sqlite3_exec_query_with_handle(db, "BEGIN TRANSACTION", NULL, NULL); ao2_unlock(db); usleep(1000 * db->batch); ao2_lock(db); @@ -525,18 +533,125 @@ struct cfg_entry_args { const char *who_asked; }; -/*! Exeute an SQL statement given the database object +/*! + * Structure passed to row counting SQLite callback. + */ +struct row_counter_args { + callback_t wrapped_callback; + void *wrapped_arg; + int row_count; +}; + +/*! + * \internal + * \brief SQLite3 callback that counts rows of a result set. + * + * \details + * This is used to decorate existing callbacks so that we can count the number + * of rows returned from a SELECT statement and still process each row + * independently. + * + * \param data user data pointer passed in via sqlite3_exec() + * \param num_columns number of columns in the result + * \param values array of pointers to column values + * \param columns array of pointers of to column names + * + * \return the return value of the wrapped callback, or 0 if no wrapped callback + * is provided. + */ +static int row_counter_wrapper(void *arg, int num_columns, char **values, char **columns) +{ + struct row_counter_args *wrapped = arg; + wrapped->row_count++; + if (wrapped->wrapped_callback) { + return wrapped->wrapped_callback(wrapped->wrapped_arg, num_columns, values, columns); + } + return 0; +} + +/*! + * \internal + * \brief Execute a SQL SELECT statement using a database handle + * + * \param db the database handle to use for the query + * \param sql the SQL statement to execute + * \param callback a user defined callback that will be called for each row of + * the result set + * \param arg data to be passed to the user defined callback + * + * \return if successful, the number of rows returned from the provided SELECT + * statement. -1 on failure. + */ +static int realtime_sqlite3_exec_query_with_handle(struct realtime_sqlite3_db *db, const char *sql, callback_t callback, void *arg) +{ + int res = 0; + char *errmsg; + struct row_counter_args wrapper = { + .wrapped_callback = callback, + .wrapped_arg = arg, + .row_count = 0, + }; + + ao2_lock(db); + if (sqlite3_exec(db->handle, sql, row_counter_wrapper, &wrapper, &errmsg) != SQLITE_OK) { + ast_log(LOG_WARNING, "Could not execute '%s': %s\n", sql, errmsg); + sqlite3_free(errmsg); + res = -1; + } + ao2_unlock(db); + + return res == 0 ? wrapper.row_count : res; +} + +/*! + * \internal + * \brief Execute a SQL SELECT statement on the specified database + * + * \param database the name of the database to query + * \param sql the SQL statement to execute + * \param callback a user defined callback that will be called for each row of + * the result set + * \param arg data to be passed to the user defined callback + * + * \return if successful, the number of rows returned from the provided SELECT + * statement. -1 on failure. + */ +static int realtime_sqlite3_exec_query(const char *database, const char *sql, callback_t callback, void *arg) +{ + struct realtime_sqlite3_db *db; + int res; + + if (!(db = find_database(database))) { + ast_log(LOG_WARNING, "Could not find database: %s\n", database); + return -1; + } + + res = realtime_sqlite3_exec_query_with_handle(db, sql, callback, arg); + ao2_ref(db, -1); + + return res; +} + +/*! + * \internal + * \brief Execute a SQL INSERT/UPDATE/DELETE statement using a database handle + * + * \note A database sync operation is always performed after a statement + * is executed. + * + * \param db the database handle to use for the query + * \param sql the SQL statement to execute * - * \retval -1 ERROR - * \retval > -1 Number of rows changed + * \return if successful, the number of rows modified by the provided SQL + * statement. -1 on failure. */ -static int realtime_sqlite3_execute_handle(struct realtime_sqlite3_db *db, const char *sql, int (*callback)(void*, int, char **, char **), void *arg, int sync) +static int realtime_sqlite3_exec_update_with_handle(struct realtime_sqlite3_db *db, const char *sql) { int res = 0; char *errmsg; ao2_lock(db); - if (sqlite3_exec(db->handle, sql, callback, arg, &errmsg) != SQLITE_OK) { + if (sqlite3_exec(db->handle, sql, NULL, NULL, &errmsg) != SQLITE_OK) { ast_log(LOG_WARNING, "Could not execute '%s': %s\n", sql, errmsg); sqlite3_free(errmsg); res = -1; @@ -545,19 +660,25 @@ static int realtime_sqlite3_execute_handle(struct realtime_sqlite3_db *db, const } ao2_unlock(db); - if (sync) { - db_sync(db); - } + db_sync(db); return res; } -/*! Exeute an SQL statement give the database name +/*! + * \internal + * \brief Execute a SQL INSERT/UPDATE/DELETE statement using a database handle + * + * \note A database sync operation is always performed after a statement + * is executed. * - * \retval -1 ERROR - * \retval > -1 Number of rows changed + * \param database the name of the database to query + * \param sql the SQL statement to execute + * + * \return if successful, the number of rows modified by the provided SQL + * statement. -1 on failure. */ -static int realtime_sqlite3_execute(const char *database, const char *sql, int (*callback)(void*, int, char **, char **), void *arg, int sync) +static int realtime_sqlite3_exec_update(const char *database, const char *sql) { struct realtime_sqlite3_db *db; int res; @@ -567,7 +688,7 @@ static int realtime_sqlite3_execute(const char *database, const char *sql, int ( return -1; } - res = realtime_sqlite3_execute_handle(db, sql, callback, arg, sync); + res = realtime_sqlite3_exec_update_with_handle(db, sql); ao2_ref(db, -1); return res; @@ -651,13 +772,15 @@ static struct ast_config *realtime_sqlite3_load(const char *database, const char args.flags = flags; args.who_asked = who_asked; - realtime_sqlite3_execute(database, sql, static_realtime_cb, &args, 0); + realtime_sqlite3_exec_query(database, sql, static_realtime_cb, &args); sqlite3_free(sql); return config; } +#define IS_SQL_LIKE_CLAUSE(x) ((x) && ast_ends_with(x, " LIKE")) + /*! \brief Helper function for single and multi-row realtime load functions */ static int realtime_sqlite3_helper(const char *database, const char *table, const struct ast_variable *fields, int is_multi, void *arg) { @@ -683,13 +806,22 @@ static int realtime_sqlite3_helper(const char *database, const char *table, cons ast_str_append(&sql, 0, " AND %s %s", sqlite3_escape_column_op(field->name), sqlite3_escape_value(field->value)); } + + if (has_explicit_like_escaping && IS_SQL_LIKE_CLAUSE(field->name)) { + /* + * The realtime framework is going to pre-escape these + * for us with a backslash. We just need to make sure + * to tell SQLite about it + */ + ast_str_append(&sql, 0, " ESCAPE '\\'"); + } } if (!is_multi) { ast_str_append(&sql, 0, "%s", " LIMIT 1"); } - if (realtime_sqlite3_execute(database, ast_str_buffer(sql), is_multi ? append_row_to_cfg : row_to_varlist, arg, 0) < 0) { + if (realtime_sqlite3_exec_query(database, ast_str_buffer(sql), is_multi ? append_row_to_cfg : row_to_varlist, arg) < 0) { ast_free(sql); return -1; } @@ -760,7 +892,7 @@ static int realtime_sqlite3_update(const char *database, const char *table, cons ast_str_append(&sql, 0, " WHERE %s %s", sqlite3_escape_column_op(keyfield), sqlite3_escape_value(entity)); - res = realtime_sqlite3_execute(database, ast_str_buffer(sql), NULL, NULL, 1); + res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); ast_free(sql); return res; @@ -811,7 +943,7 @@ static int realtime_sqlite3_update2(const char *database, const char *table, con ast_str_append(&sql, 0, "%s", ast_str_buffer(where_clause)); - res = realtime_sqlite3_execute(database, ast_str_buffer(sql), NULL, NULL, 1); + res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); ast_free(sql); ast_free(where_clause); @@ -855,7 +987,7 @@ static int realtime_sqlite3_store(const char *database, const char *table, const ast_str_append(&sql, 0, "%s)", ast_str_buffer(values)); - res = realtime_sqlite3_execute(database, ast_str_buffer(sql), NULL, NULL, 1); + res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); ast_free(sql); ast_free(values); @@ -891,7 +1023,7 @@ static int realtime_sqlite3_destroy(const char *database, const char *table, con } } - res = realtime_sqlite3_execute(database, ast_str_buffer(sql), NULL, NULL, 1); + res = realtime_sqlite3_exec_update(database, ast_str_buffer(sql)); ast_free(sql); @@ -944,7 +1076,9 @@ static int handle_missing_table(struct realtime_sqlite3_db *db, const char *tabl return -1; } - while ((column = va_arg(ap, typeof(column))) && (type = va_arg(ap, typeof(type))) && (sz = va_arg(ap, typeof(sz)))) { + while ((column = va_arg(ap, typeof(column)))) { + type = va_arg(ap, typeof(type)); + sz = va_arg(ap, typeof(sz)); if (first) { ast_str_set(&sql, 0, "CREATE TABLE IF NOT EXISTS %s (%s %s", sqlite3_escape_table(table), sqlite3_escape_column(column), get_sqlite_column_type(type)); @@ -956,7 +1090,7 @@ static int handle_missing_table(struct realtime_sqlite3_db *db, const char *tabl ast_str_append(&sql, 0, ")"); - res = realtime_sqlite3_execute_handle(db, ast_str_buffer(sql), NULL, NULL, 1) < 0 ? -1 : 0; + res = realtime_sqlite3_exec_update_with_handle(db, ast_str_buffer(sql)) < 0 ? -1 : 0; ast_free(sql); return res; @@ -981,7 +1115,7 @@ static int handle_missing_column(struct realtime_sqlite3_db *db, const char *tab return -1; } - if (!(res = (realtime_sqlite3_execute_handle(db, sql, NULL, NULL, 1) < 0 ? -1 : 0))) { + if (!(res = (realtime_sqlite3_exec_update_with_handle(db, sql) < 0 ? -1 : 0))) { ast_log(LOG_NOTICE, "Creating column '%s' type %s for table %s\n", column, sqltype, table); } @@ -1059,7 +1193,7 @@ static int realtime_sqlite3_require(const char *database, const char *table, va_ return -1; } - if ((res = realtime_sqlite3_execute_handle(db, sql, add_column_name, columns, 0)) < 0) { + if ((res = realtime_sqlite3_exec_query_with_handle(db, sql, add_column_name, columns)) < 0) { unref_db(&db); ao2_ref(columns, -1); sqlite3_free(sql); @@ -1075,8 +1209,10 @@ static int realtime_sqlite3_require(const char *database, const char *table, va_ sqlite3_free(sql); - while ((column = va_arg(ap, typeof(column))) && (type = va_arg(ap, typeof(type))) && (sz = va_arg(ap, typeof(sz)))) { + while ((column = va_arg(ap, typeof(column)))) { char *found; + type = va_arg(ap, typeof(type)); + sz = va_arg(ap, typeof(sz)); if (!(found = ao2_find(columns, column, OBJ_POINTER | OBJ_UNLINK))) { if (handle_missing_column(db, table, column, type, sz)) { unref_db(&db); @@ -1184,6 +1320,29 @@ static int unload_module(void) return 0; } +static void discover_sqlite3_caps(void) +{ + /* + * So we cheat a little bit here. SQLite3 added support for the + * 'ESCAPE' keyword in 3.1.0. They added SQLITE_VERSION_NUMBER + * in 3.1.2. So if we run into 3.1.0 or 3.1.1 in the wild, we + * just treat it like < 3.1.0. + * + * For reference: 3.1.0, 3.1.1, and 3.1.2 were all released + * within 30 days of each other in Jan/Feb 2005, so I don't + * imagine we'll be finding something pre-3.1.2 that often in + * practice. + */ +#if defined(SQLITE_VERSION_NUMBER) + has_explicit_like_escaping = 1; +#else + has_explicit_like_escaping = 0; +#endif + + ast_debug(3, "SQLite3 has 'LIKE ... ESCAPE ...' support? %s\n", + has_explicit_like_escaping ? "Yes" : "No"); +} + /*! * \brief Load the module * @@ -1196,6 +1355,8 @@ static int unload_module(void) */ static int load_module(void) { + discover_sqlite3_caps(); + if (!((databases = ao2_container_alloc(DB_BUCKETS, db_hash_fn, db_cmp_fn)))) { return AST_MODULE_LOAD_FAILURE; } diff --git a/res/res_pjsip/pjsip_cli.c b/res/res_pjsip/pjsip_cli.c index e6433f435..56ec191ed 100644 --- a/res/res_pjsip/pjsip_cli.c +++ b/res/res_pjsip/pjsip_cli.c @@ -221,6 +221,8 @@ char *ast_sip_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_ return CLI_SUCCESS; } ao2_callback(container, OBJ_NODATA, formatter_entry->print_body, &context); + ast_str_append(&context.output_buffer, 0, "\nObjects found: %d\n", ao2_container_count(container)); + } else { if (ast_strlen_zero(object_id)) { ast_free(context.output_buffer); diff --git a/res/res_sdp_translator_pjmedia.c b/res/res_sdp_translator_pjmedia.c new file mode 100644 index 000000000..141b97617 --- /dev/null +++ b/res/res_sdp_translator_pjmedia.c @@ -0,0 +1,577 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2017, Digium, Inc. + * + * Mark Michelson <mmichelson@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +#include "asterisk.h" +#include "asterisk/sdp_translator.h" +#include "asterisk/sdp_options.h" +#include "asterisk/sdp_priv.h" +#include "asterisk/vector.h" +#include "asterisk/netsock2.h" +#include "asterisk/utils.h" +#include "asterisk/config.h" +#include "asterisk/test.h" +#include "asterisk/module.h" +#ifdef HAVE_PJPROJECT +#include <pjlib.h> +#include <pjmedia.h> +#endif + +/*** MODULEINFO + <depend>pjproject</depend> + <support_level>core</support_level> + ***/ + +static pj_caching_pool sdp_caching_pool; + + +static void *pjmedia_new(void) +{ + pj_pool_t *pool; + + pool = pj_pool_create(&sdp_caching_pool.factory, "pjmedia sdp translator", 1024, 1024, NULL); + + return pool; +} + +static void pjmedia_free(void *translator_priv) +{ + pj_pool_t *pool = translator_priv; + + pj_pool_release(pool); +} + +static void copy_pj_str(char *dest, const pj_str_t *src, size_t size) +{ + memcpy(dest, pj_strbuf(src), size); + dest[size] = '\0'; +} + +static void dup_pj_str(char **dest, const pj_str_t *src) +{ + *dest = ast_malloc(pj_strlen(src) + 1); + copy_pj_str(*dest, src, pj_strlen(src)); +} + +static void pjmedia_copy_o_line(struct ast_sdp *new_sdp, struct pjmedia_sdp_session * pjmedia_sdp) +{ + dup_pj_str(&new_sdp->o_line.user, &pjmedia_sdp->origin.user); + new_sdp->o_line.id = pjmedia_sdp->origin.id; + new_sdp->o_line.version = pjmedia_sdp->origin.version; + dup_pj_str(&new_sdp->o_line.family, &pjmedia_sdp->origin.addr_type); + dup_pj_str(&new_sdp->o_line.addr, &pjmedia_sdp->origin.addr); +} + +static void pjmedia_copy_s_line(struct ast_sdp *new_sdp, struct pjmedia_sdp_session *pjmedia_sdp) +{ + dup_pj_str(&new_sdp->s_line, &pjmedia_sdp->name); +} + +static void pjmedia_copy_t_line(struct ast_sdp_t_line *new_t_line, struct pjmedia_sdp_session *pjmedia_sdp) +{ + new_t_line->start = pjmedia_sdp->time.start; + new_t_line->end = pjmedia_sdp->time.stop; +} + +static void pjmedia_copy_c_line(struct ast_sdp_c_line *new_c_line, struct pjmedia_sdp_conn *conn) +{ + /* It's perfectly reasonable for a c line not to be present, especially within a media description */ + if (!conn) { + return; + } + + dup_pj_str(&new_c_line->family, &conn->addr_type); + dup_pj_str(&new_c_line->addr, &conn->addr); +} + +static void pjmedia_copy_m_line(struct ast_sdp_m_line *new_m_line, struct pjmedia_sdp_media *pjmedia_m_line) +{ + int i; + + dup_pj_str(&new_m_line->type, &pjmedia_m_line->desc.media); + new_m_line->port = pjmedia_m_line->desc.port; + new_m_line->port_count = pjmedia_m_line->desc.port_count; + dup_pj_str(&new_m_line->profile, &pjmedia_m_line->desc.transport); + pjmedia_copy_c_line(&new_m_line->c_line, pjmedia_m_line->conn); + + AST_VECTOR_INIT(&new_m_line->payloads, pjmedia_m_line->desc.fmt_count); + for (i = 0; i < pjmedia_m_line->desc.fmt_count; ++i) { + ++new_m_line->payloads.current; + dup_pj_str(AST_VECTOR_GET_ADDR(&new_m_line->payloads, i), &pjmedia_m_line->desc.fmt[i]); + } +} + +static void pjmedia_copy_a_lines(struct ast_sdp_a_line_vector *new_a_lines, pjmedia_sdp_attr **attr, unsigned int attr_count) +{ + int i; + + AST_VECTOR_INIT(new_a_lines, attr_count); + + for (i = 0; i < attr_count; ++i) { + struct ast_sdp_a_line *a_line; + + ++new_a_lines->current; + a_line = AST_VECTOR_GET_ADDR(new_a_lines, i); + dup_pj_str(&a_line->name, &attr[i]->name); + dup_pj_str(&a_line->value, &attr[i]->value); + } +} + +static void pjmedia_copy_m_lines(struct ast_sdp *new_sdp, struct pjmedia_sdp_session *pjmedia_sdp) +{ + int i; + + AST_VECTOR_INIT(&new_sdp->m_lines, pjmedia_sdp->media_count); + + for (i = 0; i < pjmedia_sdp->media_count; ++i) { + ++new_sdp->m_lines.current; + + pjmedia_copy_m_line(AST_VECTOR_GET_ADDR(&new_sdp->m_lines, i), pjmedia_sdp->media[i]); + pjmedia_copy_a_lines(&AST_VECTOR_GET_ADDR(&new_sdp->m_lines, i)->a_lines, pjmedia_sdp->media[i]->attr, pjmedia_sdp->media[i]->attr_count); + } +} + +static struct ast_sdp *pjmedia_to_sdp(void *in, void *translator_priv) +{ + struct pjmedia_sdp_session *pjmedia_sdp = in; + + struct ast_sdp *new_sdp = ast_sdp_alloc(); + + pjmedia_copy_o_line(new_sdp, pjmedia_sdp); + pjmedia_copy_s_line(new_sdp, pjmedia_sdp); + pjmedia_copy_t_line(&new_sdp->t_line, pjmedia_sdp); + pjmedia_copy_c_line(&new_sdp->c_line, pjmedia_sdp->conn); + pjmedia_copy_a_lines(&new_sdp->a_lines, pjmedia_sdp->attr, pjmedia_sdp->attr_count); + pjmedia_copy_m_lines(new_sdp, pjmedia_sdp); + + return new_sdp; +} + +static void copy_o_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, struct ast_sdp *sdp) +{ + pjmedia_sdp->origin.id = sdp->o_line.id; + pjmedia_sdp->origin.version = sdp->o_line.version; + pj_strdup2(pool, &pjmedia_sdp->origin.user, sdp->o_line.user); + pj_strdup2(pool, &pjmedia_sdp->origin.addr_type, sdp->o_line.family); + pj_strdup2(pool, &pjmedia_sdp->origin.addr, sdp->o_line.addr); + pj_strdup2(pool, &pjmedia_sdp->origin.net_type, "IN"); +} + +static void copy_s_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, struct ast_sdp *sdp) +{ + pj_strdup2(pool, &pjmedia_sdp->name, sdp->s_line); +} + +static void copy_t_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, struct ast_sdp_t_line *t_line) +{ + pjmedia_sdp->time.start = t_line->start; + pjmedia_sdp->time.stop = t_line->end; +} + +static void copy_c_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_conn **conn, struct ast_sdp_c_line *c_line) +{ + pjmedia_sdp_conn *local_conn; + local_conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn); + pj_strdup2(pool, &local_conn->addr_type, c_line->family); + pj_strdup2(pool, &local_conn->addr, c_line->addr); + pj_strdup2(pool, &local_conn->net_type, "IN"); + + *conn = local_conn; +} + +static void copy_a_lines_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, struct ast_sdp_a_line_vector *a_lines) +{ + int i; + + for (i = 0; i < AST_VECTOR_SIZE(a_lines); ++i) { + pjmedia_sdp_attr *attr; + pj_str_t value; + + pj_strdup2(pool, &value, AST_VECTOR_GET(a_lines, i).value); + attr = pjmedia_sdp_attr_create(pool, AST_VECTOR_GET(a_lines, i).name, &value); + pjmedia_sdp_session_add_attr(pjmedia_sdp, attr); + } +} + +static void copy_a_lines_pjmedia_media(pj_pool_t *pool, pjmedia_sdp_media *media, struct ast_sdp_a_line_vector *a_lines) +{ + int i; + + for (i = 0; i < AST_VECTOR_SIZE(a_lines); ++i) { + pjmedia_sdp_attr *attr; + pj_str_t value; + + pj_strdup2(pool, &value, AST_VECTOR_GET(a_lines, i).value); + attr = pjmedia_sdp_attr_create(pool, AST_VECTOR_GET(a_lines, i).name, &value); + pjmedia_sdp_media_add_attr(media, attr); + } +} + +static void copy_m_line_pjmedia(pj_pool_t *pool, pjmedia_sdp_media *media, struct ast_sdp_m_line *m_line) +{ + int i; + + media->desc.port = m_line->port; + media->desc.port_count = m_line->port_count; + pj_strdup2(pool, &media->desc.transport, m_line->profile); + pj_strdup2(pool, &media->desc.media, m_line->type); + + for (i = 0; i < AST_VECTOR_SIZE(&m_line->payloads); ++i) { + pj_strdup2(pool, &media->desc.fmt[i], AST_VECTOR_GET(&m_line->payloads, i)); + ++media->desc.fmt_count; + } + if (m_line->c_line.addr) { + copy_c_line_pjmedia(pool, &media->conn, &m_line->c_line); + } + copy_a_lines_pjmedia_media(pool, media, &m_line->a_lines); +} + +static void copy_m_lines_pjmedia(pj_pool_t *pool, pjmedia_sdp_session *pjmedia_sdp, struct ast_sdp *sdp) +{ + int i; + + for (i = 0; i < AST_VECTOR_SIZE(&sdp->m_lines); ++i) { + pjmedia_sdp_media *media; + + media = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media); + copy_m_line_pjmedia(pool, media, AST_VECTOR_GET_ADDR(&sdp->m_lines, i)); + pjmedia_sdp->media[pjmedia_sdp->media_count] = media; + ++pjmedia_sdp->media_count; + } +} + +static void *sdp_to_pjmedia(struct ast_sdp *sdp, void *translator_priv) +{ + pj_pool_t *pool = translator_priv; + pjmedia_sdp_session *pjmedia_sdp; + + pjmedia_sdp = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_session); + copy_o_line_pjmedia(pool, pjmedia_sdp, sdp); + copy_s_line_pjmedia(pool, pjmedia_sdp, sdp); + copy_t_line_pjmedia(pool, pjmedia_sdp, &sdp->t_line); + copy_c_line_pjmedia(pool, &pjmedia_sdp->conn, &sdp->c_line); + copy_a_lines_pjmedia(pool, pjmedia_sdp, &sdp->a_lines); + copy_m_lines_pjmedia(pool, pjmedia_sdp, sdp); + return pjmedia_sdp; +} + +static struct ast_sdp_translator_ops pjmedia_translator = { + .repr = AST_SDP_REPR_PJMEDIA, + .translator_new = pjmedia_new, + .translator_free = pjmedia_free, + .to_sdp = pjmedia_to_sdp, + .from_sdp = sdp_to_pjmedia, +}; + +#ifdef TEST_FRAMEWORK + +static int verify_s_line(char *s_line, char *expected) +{ + return strcmp(s_line, expected) == 0; +} + +static int verify_c_line(struct ast_sdp_c_line *c_line, char *family, char *addr) +{ + return strcmp(c_line->family, family) == 0 && strcmp(c_line->addr, addr) == 0; +} + +static int verify_t_line(struct ast_sdp_t_line *t_line, uint32_t start, uint32_t end) +{ + return t_line->start == start && t_line->end == end; +} + +static int verify_m_line(struct ast_sdp *sdp, int index, char *type, int port, int port_count, char *profile, ...) +{ + struct ast_sdp_m_line *m_line; + int res; + va_list ap; + int i; + + m_line = AST_VECTOR_GET_ADDR(&sdp->m_lines, index); + + res = strcmp(m_line->type, type) == 0; + res |= m_line->port == port; + res |= m_line->port_count == port_count; + res |= strcmp(m_line->profile, profile) == 0; + + va_start(ap, profile); + for (i = 0; i < AST_VECTOR_SIZE(&m_line->payloads); ++i) { + char *payload; + + payload = va_arg(ap, char *); + if (!payload) { + res = -1; + break; + } + res |= strcmp(AST_VECTOR_GET(&m_line->payloads, i), payload) == 0; + } + va_end(ap); + return res; +} + +static int verify_a_line(struct ast_sdp *sdp, int m_index, int a_index, char *name, char *value) +{ + struct ast_sdp_m_line *m_line; + struct ast_sdp_a_line *a_line; + + m_line = AST_VECTOR_GET_ADDR(&sdp->m_lines, m_index); + a_line = AST_VECTOR_GET_ADDR(&m_line->a_lines, a_index); + + return strcmp(a_line->name, name) == 0 && strcmp(a_line->value, value) == 0; +} + +AST_TEST_DEFINE(pjmedia_to_sdp_test) +{ + struct ast_sdp_translator *translator; + pj_pool_t *pool; + char *sdp_str = + "v=0\r\n" + "o=alice 2890844526 2890844526 IN IP4 host.atlanta.example.com\r\n" + "s= \r\n" + "c=IN IP4 host.atlanta.example.com\r\n" + "t=0 0\r\n" + "m=audio 49170 RTP/AVP 0 8 97\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:8 PCMA/8000\r\n" + "a=rtpmap:97 iLBC/8000\r\n" + "a=sendrecv\r\n" + "m=video 51372 RTP/AVP 31 32\r\n" + "a=rtpmap:31 H261/90000\r\n" + "a=rtpmap:32 MPV/90000\r\n"; + pjmedia_sdp_session *pjmedia_sdp; + struct ast_sdp *sdp = NULL; + pj_status_t status; + enum ast_test_result_state res = AST_TEST_PASS; + + switch (cmd) { + case TEST_INIT: + info->name = "pjmedia_to_sdp"; + info->category = "/main/sdp/"; + info->summary = "PJMEDIA to SDP unit test"; + info->description = + "Ensures PJMEDIA SDPs are translated correctly"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + pool = pj_pool_create(&sdp_caching_pool.factory, "pjmedia to sdp test", 1024, 1024, NULL); + + translator = ast_sdp_translator_new(AST_SDP_REPR_PJMEDIA); + if (!translator) { + ast_test_status_update(test, "Failed to create SDP translator\n"); + res = AST_TEST_FAIL; + goto cleanup; + } + + status = pjmedia_sdp_parse(pool, sdp_str, strlen(sdp_str), &pjmedia_sdp); + if (status != PJ_SUCCESS) { + ast_test_status_update(test, "Error parsing SDP\n"); + res = AST_TEST_FAIL; + goto cleanup; + } + + sdp = ast_sdp_translator_to_sdp(translator, pjmedia_sdp); + + if (strcmp(sdp->o_line.user, "alice")) { + ast_test_status_update(test, "Unexpected SDP user '%s'\n", sdp->o_line.user); + res = AST_TEST_FAIL; + goto cleanup; + } else if (sdp->o_line.id != 2890844526u) { + ast_test_status_update(test, "Unexpected SDP id '%u'\n", sdp->o_line.id); + res = AST_TEST_FAIL; + goto cleanup; + } else if (sdp->o_line.version != 2890844526u) { + ast_test_status_update(test, "Unexpected SDP version '%u'\n", sdp->o_line.version); + res = AST_TEST_FAIL; + goto cleanup; + } else if (strcmp(sdp->o_line.family, "IP4")) { + ast_test_status_update(test, "Unexpected address family '%s'\n", sdp->o_line.family); + res = AST_TEST_FAIL; + goto cleanup; + } else if (strcmp(sdp->o_line.addr, "host.atlanta.example.com")) { + ast_test_status_update(test, "Unexpected address '%s'\n", sdp->o_line.addr); + res = AST_TEST_FAIL; + goto cleanup; + } + + if (!verify_s_line(sdp->s_line, " ")) { + ast_test_status_update(test, "Bad s line\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_c_line(&sdp->c_line, "IP4", "host.atlanta.example.com")) { + ast_test_status_update(test, "Bad c line\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_t_line(&sdp->t_line, 0, 0)) { + ast_test_status_update(test, "Bad t line\n"); + res = AST_TEST_FAIL; + goto cleanup; + } + + if (!verify_m_line(sdp, 0, "audio", 49170, 1, "RTP/AVP", "0", "8", "97", NULL)) { + ast_test_status_update(test, "Bad m line 1\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_a_line(sdp, 0, 0, "rtpmap", "0 PCMU/8000")) { + ast_test_status_update(test, "Bad a line 1\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_a_line(sdp, 0, 1, "rtpmap", "8 PCMA/8000")) { + ast_test_status_update(test, "Bad a line 2\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_a_line(sdp, 0, 2, "rtpmap", "97 iLBC/8000")) { + ast_test_status_update(test, "Bad a line 3\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_a_line(sdp, 0, 3, "sendrecv", "")) { + ast_test_status_update(test, "Bad a line 3\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_m_line(sdp, 1, "video", 51372, 1, "RTP/AVP", "31", "32", NULL)) { + ast_test_status_update(test, "Bad m line 2\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_a_line(sdp, 1, 0, "rtpmap", "31 H261/90000")) { + ast_test_status_update(test, "Bad a line 4\n"); + res = AST_TEST_FAIL; + goto cleanup; + } else if (!verify_a_line(sdp, 1, 1, "rtpmap", "32 MPV/90000")) { + ast_test_status_update(test, "Bad a line 5\n"); + res = AST_TEST_FAIL; + goto cleanup; + } + +cleanup: + ast_sdp_free(sdp); + ast_sdp_translator_free(translator); + pj_pool_release(pool); + return res; +} + +AST_TEST_DEFINE(sdp_to_pjmedia_test) +{ + struct ast_sdp_translator *translator; + char *sdp_str = + "v=0\r\n" + "o=alice 2890844526 2890844526 IN IP4 host.atlanta.example.com\r\n" + "s= \r\n" + "c=IN IP4 host.atlanta.example.com\r\n" + "t=0 0\r\n" + "m=audio 49170 RTP/AVP 0 8 97\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:8 PCMA/8000\r\n" + "a=rtpmap:97 iLBC/8000\r\n" + "a=sendrecv\r\n" + "m=video 51372 RTP/AVP 31 32\r\n" + "a=rtpmap:31 H261/90000\r\n" + "a=rtpmap:32 MPV/90000\r\n\r\n"; + pj_pool_t *pool; + pjmedia_sdp_session *pjmedia_sdp_orig; + pjmedia_sdp_session *pjmedia_sdp_dup; + struct ast_sdp *sdp = NULL; + pj_status_t status; + enum ast_test_result_state res = AST_TEST_PASS; + + switch (cmd) { + case TEST_INIT: + info->name = "sdp_to_pjmedia"; + info->category = "/main/sdp/"; + info->summary = "SDP to PJMEDIA unit test"; + info->description = + "Ensures PJMEDIA SDPs are translated correctly"; + return AST_TEST_NOT_RUN; + case TEST_EXECUTE: + break; + } + + pool = pj_pool_create(&sdp_caching_pool.factory, "pjmedia to sdp test", 1024, 1024, NULL); + + translator = ast_sdp_translator_new(AST_SDP_REPR_PJMEDIA); + if (!translator) { + ast_test_status_update(test, "Failed to create SDP translator\n"); + res = AST_TEST_FAIL; + goto cleanup; + } + + status = pjmedia_sdp_parse(pool, sdp_str, strlen(sdp_str), &pjmedia_sdp_orig); + if (status != PJ_SUCCESS) { + ast_test_status_update(test, "Error parsing SDP\n"); + res = AST_TEST_FAIL; + goto cleanup; + } + + sdp = ast_sdp_translator_to_sdp(translator, pjmedia_sdp_orig); + pjmedia_sdp_dup = ast_sdp_translator_from_sdp(translator, sdp); + + if ((status = pjmedia_sdp_session_cmp(pjmedia_sdp_orig, pjmedia_sdp_dup, 0)) != PJ_SUCCESS) { + char buf[2048]; + char errbuf[256]; + ast_test_status_update(test, "SDPs aren't equal\n"); + pjmedia_sdp_print(pjmedia_sdp_orig, buf, sizeof(buf)); + ast_log(LOG_NOTICE, "Original SDP is %s\n", buf); + pjmedia_sdp_print(pjmedia_sdp_dup, buf, sizeof(buf)); + ast_log(LOG_NOTICE, "New SDP is %s\n", buf); + pjmedia_strerror(status, errbuf, sizeof(errbuf)); + ast_log(LOG_NOTICE, "PJMEDIA says %d: '%s'\n", status, errbuf); + res = AST_TEST_FAIL; + goto cleanup; + } + +cleanup: + ast_sdp_free(sdp); + ast_sdp_translator_free(translator); + pj_pool_release(pool); + return res; +} + +#endif /* TEST_FRAMEWORK */ + +static int load_module(void) +{ + if (ast_sdp_register_translator(&pjmedia_translator)) { + return AST_MODULE_LOAD_DECLINE; + } + pj_caching_pool_init(&sdp_caching_pool, NULL, 1024 * 1024); + AST_TEST_REGISTER(pjmedia_to_sdp_test); + AST_TEST_REGISTER(sdp_to_pjmedia_test); + + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + ast_sdp_unregister_translator(&pjmedia_translator); + pj_caching_pool_destroy(&sdp_caching_pool); + AST_TEST_UNREGISTER(pjmedia_to_sdp_test); + AST_TEST_REGISTER(sdp_to_pjmedia_test); + return 0; +} + +static int reload_module(void) +{ + return 0; +} + +AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJMEDIA SDP Translator", + .support_level = AST_MODULE_SUPPORT_CORE, + .load = load_module, + .unload = unload_module, + .reload = reload_module, + .load_pri = AST_MODPRI_CHANNEL_DEPEND, +); |