Merge "pjsip_distributor.c: Update some debug messages to get transaction name."
authorzuul <zuul@gerrit.asterisk.org>
Wed, 22 Feb 2017 02:01:08 +0000 (20:01 -0600)
committerGerrit Code Review <gerrit2@gerrit.digium.api>
Wed, 22 Feb 2017 02:01:08 +0000 (20:01 -0600)
14 files changed:
addons/res_config_mysql.c
apps/app_voicemail.c
include/asterisk/sdp_options.h [new file with mode: 0644]
include/asterisk/sdp_priv.h [new file with mode: 0644]
include/asterisk/sdp_translator.h [new file with mode: 0644]
main/Makefile
main/sdp_options.c [new file with mode: 0644]
main/sdp_repr.c [new file with mode: 0644]
main/sdp_translator.c [new file with mode: 0644]
main/tcptls.c
res/res_config_ldap.c
res/res_config_pgsql.c
res/res_config_sqlite3.c
res/res_sdp_translator_pjmedia.c [new file with mode: 0644]

index bf38a4e..f2ef949 100644 (file)
@@ -303,6 +303,11 @@ static char *decode_chunk(char *chunk)
        return orig;
 }
 
+#define IS_SQL_LIKE_CLAUSE(x) ((x) && ast_ends_with(x, " LIKE"))
+
+/* MySQL requires us to escape the escape... yo dawg */
+static char *ESCAPE_CLAUSE = " ESCAPE '\\\\'";
+
 static struct ast_variable *realtime_mysql(const char *database, const char *table, const struct ast_variable *rt_fields)
 {
        struct mysql_conn *dbh;
@@ -315,6 +320,7 @@ static struct ast_variable *realtime_mysql(const char *database, const char *tab
        char *stringp;
        char *chunk;
        char *op;
+       char *escape = "";
        const struct ast_variable *field = rt_fields;
        struct ast_variable *var=NULL, *prev=NULL;
 
@@ -345,20 +351,29 @@ static struct ast_variable *realtime_mysql(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 */
 
-       if (!strchr(field->name, ' ')) 
-               op = " ="; 
-       else 
+       if (!strchr(field->name, ' ')) {
+               op = " =";
+       } else {
                op = "";
+               if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                       escape = ESCAPE_CLAUSE;
+               }
+       }
 
        ESCAPE_STRING(buf, field->value);
-       ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'", table, field->name, op, ast_str_buffer(buf));
+       ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'%s", table, field->name, op, ast_str_buffer(buf), escape);
        while ((field = field->next)) {
-               if (!strchr(field->name, ' ')) 
-                       op = " ="; 
-               else
+               escape = "";
+               if (!strchr(field->name, ' ')) {
+                       op = " =";
+               } else {
                        op = "";
+                       if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                               escape = ESCAPE_CLAUSE;
+                       }
+               }
                ESCAPE_STRING(buf, field->value);
-               ast_str_append(&sql, 0, " AND %s%s '%s'", field->name, op, ast_str_buffer(buf));
+               ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(buf), escape);
        }
 
        ast_debug(1, "MySQL RealTime: Retrieve SQL: %s\n", ast_str_buffer(sql));
@@ -416,6 +431,7 @@ static struct ast_config *realtime_multi_mysql(const char *database, const char
        char *stringp;
        char *chunk;
        char *op;
+       char *escape = "";
        const struct ast_variable *field = rt_fields;
        struct ast_variable *var = NULL;
        struct ast_config *cfg = NULL;
@@ -462,17 +478,29 @@ static struct ast_config *realtime_multi_mysql(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
+       } else {
                op = "";
+               if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                       escape = ESCAPE_CLAUSE;
+               }
+       }
 
        ESCAPE_STRING(buf, field->value);
-       ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'", table, field->name, op, ast_str_buffer(buf));
+       ast_str_set(&sql, 0, "SELECT * FROM %s WHERE %s%s '%s'%s", table, field->name, op, ast_str_buffer(buf), escape);
        while ((field = field->next)) {
-               if (!strchr(field->name, ' ')) op = " ="; else op = "";
+               escape = "";
+               if (!strchr(field->name, ' ')) {
+                       op = " =";
+               } else {
+                       op = "";
+                       if (IS_SQL_LIKE_CLAUSE(field->name)) {
+                               escape = ESCAPE_CLAUSE;
+                       }
+               }
                ESCAPE_STRING(buf, field->value);
-               ast_str_append(&sql, 0, " AND %s%s '%s'", field->name, op, ast_str_buffer(buf));
+               ast_str_append(&sql, 0, " AND %s%s '%s'%s", field->name, op, ast_str_buffer(buf), escape);
        }
 
        if (initfield) {
index 91e8104..1d7ffc0 100644 (file)
@@ -11190,7 +11190,7 @@ static int vm_authenticate(struct ast_channel *chan, char *mailbox, int mailbox_
                return -1;
        }
        if (vmu && !skipuser) {
-               memcpy(res_vmu, vmu, sizeof(struct ast_vm_user));
+               *res_vmu = *vmu;
        }
        return 0;
 }
@@ -11352,8 +11352,8 @@ static int vm_execmain(struct ast_channel *chan, const char *data)
        int box;
        int useadsi = 0;
        int skipuser = 0;
-       struct vm_state vms;
-       struct ast_vm_user *vmu = NULL, vmus;
+       struct vm_state vms = {{0}};
+       struct ast_vm_user *vmu = NULL, vmus = {{0}};
        char *context = NULL;
        int silentexit = 0;
        struct ast_flags flags = { 0 };
@@ -11366,12 +11366,8 @@ static int vm_execmain(struct ast_channel *chan, const char *data)
 #endif
 
        /* Add the vm_state to the active list and keep it active */
-       memset(&vms, 0, sizeof(vms));
-
        vms.lastmsg = -1;
 
-       memset(&vmus, 0, sizeof(vmus));
-
        ast_test_suite_event_notify("START", "Message: vm_execmain started");
        if (ast_channel_state(chan) != AST_STATE_UP) {
                ast_debug(1, "Before ast_answer\n");
@@ -12664,7 +12660,7 @@ static struct ast_custom_function vm_info_acf = {
 static int vmauthenticate(struct ast_channel *chan, const char *data)
 {
        char *s, *user = NULL, *context = NULL, mailbox[AST_MAX_EXTENSION] = "";
-       struct ast_vm_user vmus;
+       struct ast_vm_user vmus = {{0}};
        char *options = NULL;
        int silent = 0, skipuser = 0;
        int res = -1;
diff --git a/include/asterisk/sdp_options.h b/include/asterisk/sdp_options.h
new file mode 100644 (file)
index 0000000..a5c2d08
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * 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.
+ */
+
+#ifndef _ASTERISK_SDP_OPTIONS_H
+#define _ASTERISK_SDP_OPTIONS_H
+
+struct ast_sdp_options;
+
+/*!
+ * \since 15.0.0
+ * \brief Allocate a new SDP options structure.
+ *
+ * This will heap-allocate an SDP options structure and
+ * initialize it to a set of default values.
+ *
+ * \retval NULL Allocation failure
+ * \retval non-NULL Newly allocated SDP options
+ */
+struct ast_sdp_options *ast_sdp_options_alloc(void);
+
+/*!
+ * \since 15.0.0
+ * \brief Free an SDP options structure.
+ *
+ * \note This only needs to be called if an error occurs between
+ *       options allocation and a call to ast_sdp_state_alloc()
+ *       Otherwise, the SDP state will take care of freeing the
+ *       options for you.
+ *
+ * \param options The options to free
+ */
+void ast_sdp_options_free(struct ast_sdp_options *options);
+
+/*!
+ * \brief ICE options
+ *
+ * This is an enum because it is predicted that this eventually
+ * support a TRICKLE-ICE option.
+ */
+enum ast_sdp_options_ice {
+       /*! ICE is not enabled on this session */
+       AST_SDP_ICE_DISABLED,
+       /*! Standard ICE is enabled on this session */
+       AST_SDP_ICE_ENABLED_STANDARD,
+};
+
+/*!
+ * \since 15.0.0
+ * \brief Set ICE options
+ *
+ * The default is AST_SDP_ICE_DISABLED
+ */
+int ast_sdp_options_set_ice(struct ast_sdp_options *options,
+       enum ast_sdp_options_ice ice_setting);
+
+/*!
+ * \since 15.0.0
+ * \brief Retrieve ICE options
+ */
+enum ast_sdp_options_ice ast_sdp_options_get_ice(const struct ast_sdp_options *options);
+
+/*!
+ * \since 15.0.0
+ * \brief Enable or disable telephone events.
+ *
+ * A non-zero value indicates telephone events are enabled.
+ * A zero value indicates telephone events are disabled.
+ *
+ * The default is 0
+ */
+int ast_sdp_options_set_telephone_event(struct ast_sdp_options *options,
+       int telephone_event_enabled);
+
+/*!
+ * \since 15.0.0
+ * \brief Retrieve telephone event setting.
+ *
+ * \retval 0 Telephone events are currently disabled.
+ * \retval non-zero Telephone events are currently enabled.
+ */
+int ast_sdp_options_get_telephone_event(const struct ast_sdp_options *options);
+
+/*!
+ * \brief Representation of the SDP
+ *
+ * Users of the SDP API set the representation based on what they
+ * natively handle. This indicates the type of SDP that the API expects
+ * when being given an SDP, and it indicates the type of SDP that the API
+ * returns when asked for one.
+ */
+enum ast_sdp_options_repr {
+       /*! SDP is represented as a string */
+       AST_SDP_REPR_STRING,
+       /*! SDP is represented as a pjmedia_sdp_session */
+       AST_SDP_REPR_PJMEDIA,
+       /*! End of the list */
+       AST_SDP_REPR_END,
+};
+
+/*!
+ * \since 15.0.0
+ * \brief Set the SDP representation
+ *
+ * The default is AST_SDP_REPR_STRING
+ */
+int ast_sdp_options_set_repr(struct ast_sdp_options *options,
+       enum ast_sdp_options_repr repr);
+
+/*!
+ * \since 15.0.0
+ * \brief Get the SDP representation
+ */
+enum ast_sdp_options_repr ast_sdp_options_get_repr(const struct ast_sdp_options *options);
+
+/*!
+ * \brief SDP encryption options
+ */
+enum ast_sdp_options_encryption {
+       /*! No encryption */
+       AST_SDP_ENCRYPTION_DISABLED,
+       /*! SRTP SDES encryption */
+       AST_SDP_ENCRYPTION_SRTP_SDES,
+       /*! DTLS encryption */
+       AST_SDP_ENCRYPTION_DTLS,
+};
+
+/*!
+ * \since 15.0.0
+ * \brief Set the SDP encryption
+ *
+ * The default is AST_SDP_ENCRYPTION_DISABLED
+ */
+int ast_sdp_options_set_encryption(struct ast_sdp_options *options,
+       enum ast_sdp_options_encryption encryption);
+
+/*!
+ * \since 15.0.0
+ * \brief Get the SDP encryption
+ */
+enum ast_sdp_options_encryption ast_sdp_options_get_encryption(const struct ast_sdp_options *options);
+
+#endif /* _ASTERISK_SDP_OPTIONS_H */
diff --git a/include/asterisk/sdp_priv.h b/include/asterisk/sdp_priv.h
new file mode 100644 (file)
index 0000000..000d111
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * 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.
+ */
+
+/* NOTE: It is unlikely that you need to include this file. You probably will only need
+ * this if you are an SDP translator, or if you are an inner part of the SDP API
+ */
+
+#ifndef _SDP_PRIV_H
+#define _SDP_PRIV_H
+
+#include "asterisk/vector.h"
+
+/*!
+ * \brief Structure representing an SDP attribute
+ */
+struct ast_sdp_a_line {
+       /*! Attribute name */
+       char *name;
+       /*! Attribute value. For attributes that have no value, this will be an empty string */
+       char *value;
+};
+
+/*!
+ * \brief Structure representing an SDP connection
+ */
+struct ast_sdp_c_line {
+       /* IP family string (e.g. IP4 or IP6) */
+       char *family;
+       /* Connection address. Can be an IP address or FQDN */
+       char *addr;
+};
+
+/*!
+ * \brief A collection of SDP attributes
+ */
+AST_VECTOR(ast_sdp_a_line_vector, struct ast_sdp_a_line);
+
+/*!
+ * \brief An SDP media stream
+ *
+ * This contains both the m line, as well as its
+ * constituent a lines.
+ */
+struct ast_sdp_m_line {
+       /*! Media type (e.g. "audio" or "video") */
+       char *type;
+       /*! Port number in m line */
+       uint16_t port;
+       /*! Number of ports specified in m line */
+       uint16_t port_count;
+       /*! RTP profile string (e.g. "RTP/AVP") */
+       char *profile;
+       /*! RTP payloads */
+       AST_VECTOR(, char *) payloads;
+       /*! Connection information for this media stream */
+       struct ast_sdp_c_line c_line;
+       /*! The attributes for this media stream */
+       struct ast_sdp_a_line_vector a_lines;
+};
+
+/*!
+ * \brief SDP time information
+ */
+struct ast_sdp_t_line {
+       /*! Session start time */
+       uint32_t start;
+       /*! Session end time */
+       uint32_t end;
+};
+
+/*!
+ * \brief An SDP
+ */
+struct ast_sdp {
+       /*! SDP Origin line */
+       struct {
+               /*! Origin user name */
+               char *user;
+               /*! Origin id */
+               uint32_t id;
+               /*! Origin version */
+               uint32_t version;
+               /*! Origin IP address family (e.g. "IP4" or "IP6") */
+               char *family;
+               /*! Origin address. Can be an IP address or FQDN */
+               char *addr;
+       } o_line;
+       /*! SDP Session name */
+       char *s_line;
+       /*! SDP top-level connection information */
+       struct ast_sdp_c_line c_line;
+       /*! SDP timing information */
+       struct ast_sdp_t_line t_line;
+       /*! SDP top-level attributes */
+       struct ast_sdp_a_line_vector a_lines;
+       /*! SDP media streams */
+       AST_VECTOR(, struct ast_sdp_m_line) m_lines;
+};
+
+/*!
+ * \brief Allocate a new SDP.
+ *
+ * \note This does not perform any initialization.
+ *
+ * \retval NULL FAIL
+ * \retval non-NULL New SDP
+ */
+struct ast_sdp *ast_sdp_alloc(void);
+
+/*!
+ * \brief Free an SDP and all its constituent parts
+ */
+void ast_sdp_free(struct ast_sdp *dead);
+
+#endif /* _SDP_PRIV_H */
diff --git a/include/asterisk/sdp_translator.h b/include/asterisk/sdp_translator.h
new file mode 100644 (file)
index 0000000..62a875e
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * 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.
+ */
+
+#ifndef _ASTERISK_SDP_TRANSLATOR_H
+#define _ASTERISK_SDP_TRANSLATOR_H
+
+#include "asterisk/sdp_options.h"
+
+struct sdp;
+
+/*!
+ * \brief SDP translator operations
+ */
+struct ast_sdp_translator_ops {
+       /*! The SDP representation on which this translator operates */
+       enum ast_sdp_options_repr repr;
+       /*! Allocate new translator private data for a translator */
+       void *(*translator_new)(void);
+       /*! Free translator private data */
+       void (*translator_free)(void *translator_priv);
+       /*! Convert the channel-native SDP into an internal Asterisk SDP */
+       struct ast_sdp *(*to_sdp)(void *repr_sdp, void *translator_priv);
+       /*! Convert an internal Asterisk SDP into a channel-native SDP */
+       void *(*from_sdp)(struct ast_sdp *sdp, void *translator_priv);
+};
+
+/*!
+ * \brief An SDP translator
+ *
+ * An SDP translator is responsible for converting between Asterisk's internal
+ * representation of an SDP and the representation that is native to the channel
+ * driver. Translators are allocated per-use.
+ */
+struct ast_sdp_translator {
+       /*! The operations this translator uses */
+       struct ast_sdp_translator_ops *ops;
+       /*! Private data this translator uses */
+       void *translator_priv;
+};
+
+/*!
+ * \brief Register an SDP translator
+ * \param ops The SDP operations defined by this translator
+ * \retval 0 Success
+ * \retval -1 FAIL
+ */
+int ast_sdp_register_translator(struct ast_sdp_translator_ops *ops);
+
+/*!
+ * \brief Unregister an SDP translator
+ */
+void ast_sdp_unregister_translator(struct ast_sdp_translator_ops *ops);
+
+/*!
+ * \brief Allocate a new SDP translator
+ * \param Representation corresponding to the translator_ops to use
+ * \retval NULL FAIL
+ * \retval non-NULL New SDP translator
+ */
+struct ast_sdp_translator *ast_sdp_translator_new(enum ast_sdp_options_repr repr);
+
+/*!
+ * \brief Free an SDP translator
+ */
+void ast_sdp_translator_free(struct ast_sdp_translator *translator);
+
+/*!
+ * \brief Translate a native SDP to internal Asterisk SDP
+ *
+ * \param translator The translator to use when translating
+ * \param native_sdp The SDP from the channel driver
+ * \retval NULL FAIL
+ * \retval Non-NULL The translated SDP
+ */
+struct ast_sdp *ast_sdp_translator_to_sdp(struct ast_sdp_translator *translator, void *native_sdp);
+
+/*!
+ * \brief Translate an internal Asterisk SDP to a native SDP
+ *
+ * \param translator The translator to use when translating
+ * \param ast_sdp The Asterisk SDP to translate
+ * \retval NULL FAIL
+ * \retval non-NULL The translated SDP
+ */
+void *ast_sdp_translator_from_sdp(struct ast_sdp_translator *translator, struct ast_sdp *ast_sdp);
+
+#endif /* _ASTERISK_SDP_TRANSLATOR_H */
index 4d1b2c4..3c371c6 100644 (file)
@@ -355,7 +355,11 @@ else # Darwin
 endif
 endif
 ifneq ($(LDCONFIG),)
+ifneq ($(DESTDIR),)
        $(LDCONFIG) $(LDCONFIG_FLAGS) "$(DESTDIR)$(ASTLIBDIR)/"
+else
+       $(LDCONFIG)
+endif
 endif
        $(LN) -sf asterisk "$(DESTDIR)$(ASTSBINDIR)/rasterisk"
 
@@ -373,7 +377,11 @@ ifneq ($(ASTPJ_LIB).$(ASTPJ_SO_VERSION),.)
        rm -f "$(DESTDIR)$(ASTLIBDIR)/$(ASTPJ_LIB)"
 endif
 ifneq ($(LDCONFIG),)
+ifneq ($(DESTDIR),)
        $(LDCONFIG) $(LDCONFIG_FLAGS) "$(DESTDIR)$(ASTLIBDIR)/"
+else
+       $(LDCONFIG)
+endif
 endif
 
 clean::
diff --git a/main/sdp_options.c b/main/sdp_options.c
new file mode 100644 (file)
index 0000000..e18dfa5
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * 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/utils.h"
+#include "asterisk/sdp_options.h"
+
+struct ast_sdp_options {
+       enum ast_sdp_options_ice ice;
+       int telephone_event;
+       enum ast_sdp_options_repr repr;
+       enum ast_sdp_options_encryption encryption;
+};
+
+#define DEFAULT_ICE AST_SDP_ICE_DISABLED
+#define DEFAULT_TELEPHONE_EVENT 0
+#define DEFAULT_REPR AST_SDP_REPR_STRING
+#define DEFAULT_ENCRYPTION AST_SDP_ENCRYPTION_DISABLED
+
+static void set_defaults(struct ast_sdp_options *options)
+{
+       options->ice = DEFAULT_ICE;
+       options->telephone_event = DEFAULT_TELEPHONE_EVENT;
+       options->repr = DEFAULT_REPR;
+       options->encryption = DEFAULT_ENCRYPTION;
+}
+
+struct ast_sdp_options *ast_sdp_options_alloc(void)
+{
+       struct ast_sdp_options *options;
+
+       options = ast_calloc(1, sizeof(*options));
+       if (!options) {
+               return NULL;
+       }
+       set_defaults(options);
+       return options;
+}
+
+void ast_sdp_options_free(struct ast_sdp_options *options)
+{
+       ast_free(options);
+}
+
+int ast_sdp_options_set_ice(struct ast_sdp_options *options, enum ast_sdp_options_ice ice_setting)
+{
+       ast_assert(options != NULL);
+
+       options->ice = ice_setting;
+       return 0;
+}
+
+enum ast_sdp_options_ice ast_sdp_options_get_ice(const struct ast_sdp_options *options)
+{
+       ast_assert(options != NULL);
+
+       return options->ice;
+}
+
+int ast_sdp_options_set_telephone_event(struct ast_sdp_options *options, int telephone_event_enabled)
+{
+       ast_assert(options != NULL);
+
+       options->telephone_event = telephone_event_enabled;
+       return 0;
+}
+
+int ast_sdp_options_get_telephone_event(const struct ast_sdp_options *options)
+{
+       ast_assert(options != NULL);
+
+       return options->telephone_event;
+}
+
+int ast_sdp_options_set_repr(struct ast_sdp_options *options, enum ast_sdp_options_repr repr)
+{
+       ast_assert(options != NULL);
+
+       options->repr = repr;
+       return 0;
+}
+
+enum ast_sdp_options_repr ast_sdp_options_get_repr(const struct ast_sdp_options *options)
+{
+       ast_assert(options != NULL);
+
+       return options->repr;
+}
+
+int ast_sdp_options_set_encryption(struct ast_sdp_options *options,
+       enum ast_sdp_options_encryption encryption)
+{
+       ast_assert(options != NULL);
+
+       options->encryption = encryption;
+       return 0;
+}
+
+enum ast_sdp_options_encryption ast_sdp_options_get_encryption(const struct ast_sdp_options *options)
+{
+       ast_assert(options != NULL);
+
+       return options->encryption;
+}
diff --git a/main/sdp_repr.c b/main/sdp_repr.c
new file mode 100644 (file)
index 0000000..6df243b
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * 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_priv.h"
+#include "asterisk/utils.h"
+
+struct ast_sdp *ast_sdp_alloc(void)
+{
+       struct ast_sdp *new_sdp;
+
+       new_sdp = ast_calloc(1, sizeof *new_sdp);
+       return new_sdp;
+}
+
+static void free_o_line(struct ast_sdp *dead)
+{
+       ast_free(dead->o_line.user);
+       ast_free(dead->o_line.family);
+       ast_free(dead->o_line.addr);
+}
+
+static void free_s_line(struct ast_sdp *dead)
+{
+       ast_free(dead->s_line);
+}
+
+static void free_c_line(struct ast_sdp_c_line *c_line)
+{
+       ast_free(c_line->family);
+       ast_free(c_line->addr);
+}
+
+static void free_t_line(struct ast_sdp_t_line *t_line)
+{
+       return;
+}
+
+static void free_a_line(struct ast_sdp_a_line *a_line)
+{
+       ast_free(a_line->name);
+       ast_free(a_line->value);
+}
+
+static void free_a_lines(struct ast_sdp_a_line_vector *a_lines)
+{
+       int i;
+
+       for (i = 0; i < AST_VECTOR_SIZE(a_lines); ++i) {
+               free_a_line(AST_VECTOR_GET_ADDR(a_lines, i));
+       }
+       AST_VECTOR_FREE(a_lines);
+}
+
+static void free_m_line(struct ast_sdp_m_line *m_line)
+{
+       int i;
+
+       ast_free(m_line->type);
+       ast_free(m_line->profile);
+       free_c_line(&m_line->c_line);
+
+       for (i = 0; i < AST_VECTOR_SIZE(&m_line->payloads); ++i) {
+               ast_free(AST_VECTOR_GET(&m_line->payloads, i));
+       }
+       AST_VECTOR_FREE(&m_line->payloads);
+
+       free_a_lines(&m_line->a_lines);
+}
+
+static void free_m_lines(struct ast_sdp *dead)
+{
+       int i;
+
+       for (i = 0; i < AST_VECTOR_SIZE(&dead->m_lines); ++i) {
+               free_m_line(AST_VECTOR_GET_ADDR(&dead->m_lines, i));
+       }
+
+       AST_VECTOR_FREE(&dead->m_lines);
+}
+
+void ast_sdp_free(struct ast_sdp *dead)
+{
+       if (!dead) {
+               return;
+       }
+
+       free_o_line(dead);
+       free_s_line(dead);
+       free_c_line(&dead->c_line);
+       free_t_line(&dead->t_line);
+       free_a_lines(&dead->a_lines);
+       free_m_lines(dead);
+       ast_free(dead);
+}
+
diff --git a/main/sdp_translator.c b/main/sdp_translator.c
new file mode 100644 (file)
index 0000000..5426ae9
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * 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_options.h"
+#include "asterisk/sdp_translator.h"
+#include "asterisk/logger.h"
+#include "asterisk/utils.h"
+#include "asterisk/lock.h"
+
+AST_RWLOCK_DEFINE_STATIC(registered_ops_lock);
+static struct ast_sdp_translator_ops *registered_ops[AST_SDP_REPR_END];
+
+int ast_sdp_register_translator(struct ast_sdp_translator_ops *ops)
+{
+       SCOPED_WRLOCK(lock, &registered_ops_lock);
+
+       if (ops->repr >= AST_SDP_REPR_END) {
+               ast_log(LOG_ERROR, "SDP translator has unrecognized representation\n");
+               return -1;
+       }
+
+       if (registered_ops[ops->repr] != NULL) {
+               ast_log(LOG_ERROR, "SDP_translator with this representation already registered\n");
+               return -1;
+       }
+
+       registered_ops[ops->repr] = ops;
+       ast_log(LOG_NOTICE, "Placed ops %p at slot %d\n", ops, ops->repr);
+       return 0;
+}
+
+void ast_sdp_unregister_translator(struct ast_sdp_translator_ops *ops)
+{
+       SCOPED_WRLOCK(lock, &registered_ops_lock);
+
+       if (ops->repr >= AST_SDP_REPR_END) {
+               return;
+       }
+
+       registered_ops[ops->repr] = NULL;
+}
+
+struct ast_sdp_translator *ast_sdp_translator_new(enum ast_sdp_options_repr repr)
+{
+       struct ast_sdp_translator *translator;
+       SCOPED_RDLOCK(lock, &registered_ops_lock);
+
+       if (registered_ops[repr] == NULL) {
+               ast_log(LOG_NOTICE, "No registered SDP translator with representation %d\n", repr);
+               return NULL;
+       }
+
+       translator = ast_calloc(1, sizeof(*translator));
+       if (!translator) {
+               return NULL;
+       }
+
+       translator->ops = registered_ops[repr];
+
+       translator->translator_priv = translator->ops->translator_new();
+       if (!translator->translator_priv) {
+               ast_free(translator);
+               return NULL;
+       }
+
+       return translator;
+}
+
+void ast_sdp_translator_free(struct ast_sdp_translator *translator)
+{
+       translator->ops->translator_free(translator->translator_priv);
+       ast_free(translator);
+}
+
+struct ast_sdp *ast_sdp_translator_to_sdp(struct ast_sdp_translator *translator, void *native_sdp)
+{
+       return translator->ops->to_sdp(native_sdp, translator->translator_priv);
+}
+
+void *ast_sdp_translator_from_sdp(struct ast_sdp_translator *translator, struct ast_sdp *ast_sdp)
+{
+       return translator->ops->from_sdp(ast_sdp, translator->translator_priv);
+}
index 7f1421d..1377dd7 100644 (file)
@@ -240,20 +240,23 @@ void *ast_tcptls_server_root(void *data)
                }
                tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
                if (!tcptls_session) {
-                       ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
-                       if (close(fd)) {
-                               ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
-                       }
+                       close(fd);
                        continue;
                }
 
                tcptls_session->overflow_buf = ast_str_create(128);
+               if (!tcptls_session->overflow_buf) {
+                       ao2_ref(tcptls_session, -1);
+                       close(fd);
+                       continue;
+               }
                flags = fcntl(fd, F_GETFL);
                fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
 
                tcptls_session->stream = ast_iostream_from_fd(&fd);
                if (!tcptls_session->stream) {
-                       ast_log(LOG_WARNING, "No memory for new session iostream\n");
+                       ao2_ref(tcptls_session, -1);
+                       close(fd);
                        continue;
                }
 
@@ -265,7 +268,6 @@ void *ast_tcptls_server_root(void *data)
                /* This thread is now the only place that controls the single ref to tcptls_session */
                if (ast_pthread_create_detached_background(&launched, NULL, handle_tcptls_connection, tcptls_session)) {
                        ast_log(LOG_ERROR, "Unable to launch helper thread: %s\n", strerror(errno));
-                       ast_tcptls_close_session_file(tcptls_session);
                        ao2_ref(tcptls_session, -1);
                }
        }
@@ -561,11 +563,15 @@ struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_s
                }
        }
 
-       if (!(tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor))) {
+       tcptls_session = ao2_alloc(sizeof(*tcptls_session), session_instance_destructor);
+       if (!tcptls_session) {
                goto error;
        }
 
        tcptls_session->overflow_buf = ast_str_create(128);
+       if (!tcptls_session->overflow_buf) {
+               goto error;
+       }
        tcptls_session->client = 1;
        tcptls_session->stream = ast_iostream_from_fd(&fd);
        if (!tcptls_session->stream) {
@@ -584,9 +590,7 @@ struct ast_tcptls_session_instance *ast_tcptls_client_create(struct ast_tcptls_s
 error:
        close(desc->accept_fd);
        desc->accept_fd = -1;
-       if (tcptls_session) {
-               ao2_ref(tcptls_session, -1);
-       }
+       ao2_cleanup(tcptls_session);
        return NULL;
 }
 
index 5e95853..f6abe63 100644 (file)
@@ -190,7 +190,7 @@ static int semicolon_count_var(struct ast_variable *var)
                return 0;
        }
 
-       ast_debug(2, "LINE(%d) semicolon_count_var: %s\n", __LINE__, var_value->value);
+       ast_debug(2, "semicolon_count_var: %s\n", var_value->value);
 
        return semicolon_count_str(var_value->value);
 }
@@ -330,7 +330,7 @@ static struct ast_variable *realtime_ldap_entry_to_var(struct ldap_table_config
                        for (v = values; *v; v++) {
                                value = *v;
                                valptr = value->bv_val;
-                               ast_debug(2, "LINE(%d) attribute_name: %s LDAP value: %s\n", __LINE__, attribute_name, valptr);
+                               ast_debug(2, "attribute_name: %s LDAP value: %s\n", attribute_name, valptr);
                                if (is_realmed_password_attribute) {
                                        if (!strncasecmp(valptr, "{md5}", 5)) {
                                                valptr += 5;
@@ -422,7 +422,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
         * value in \a variable_value; otherwise, we keep \a vars static and increase the length of the linked list of variables in the array element.
         * This memory must be freed outside of this function.
         */
-       vars = ast_calloc(sizeof(struct ast_variable *), tot_count + 1);
+       vars = ast_calloc(tot_count + 1, sizeof(struct ast_variable *));
 
        ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
 
@@ -467,7 +467,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                delim_value = ast_strdup(valptr);
 
                                                                if ((delim_tot_count = semicolon_count_str(delim_value)) > 0) {
-                                                                       ast_debug(4, "LINE(%d) is delimited %d times: %s\n", __LINE__, delim_tot_count, delim_value);
+                                                                       ast_debug(4, "is delimited %d times: %s\n", delim_tot_count, delim_value);
                                                                        is_delimited = 1;
                                                                }
                                                        }
@@ -477,11 +477,11 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                /* for non-Static RealTime, first */
 
                                                                for (i = pos; !ast_strlen_zero(valptr + i); i++) {
-                                                                       ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
+                                                                       ast_debug(4, "DELIM pos: %d i: %d\n", pos, i);
                                                                        if (delim_value[i] == ';') {
                                                                                delim_value[i] = '\0';
 
-                                                                               ast_debug(2, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
+                                                                               ast_debug(2, "DELIM - attribute_name: %s value: %s pos: %d\n", attribute_name, &delim_value[pos], pos);
 
                                                                                if (prev) {
                                                                                        prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
@@ -499,9 +499,9 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                        }
                                                                }
                                                                if (ast_strlen_zero(valptr + i)) {
-                                                                       ast_debug(4, "LINE(%d) DELIM pos: %d i: %d delim_count: %d\n", __LINE__, pos, i, delim_count);
+                                                                       ast_debug(4, "DELIM pos: %d i: %d delim_count: %d\n", pos, i, delim_count);
                                                                        /* Last delimited value */
-                                                                       ast_debug(4, "LINE(%d) DELIM - attribute_name: %s value: %s pos: %d\n", __LINE__, attribute_name, &delim_value[pos], pos);
+                                                                       ast_debug(4, "DELIM - attribute_name: %s value: %s pos: %d\n", attribute_name, &delim_value[pos], pos);
                                                                        if (prev) {
                                                                                prev->next = ast_variable_new(attribute_name, &delim_value[pos], table_config->table_name);
                                                                                if (prev->next) {
@@ -517,14 +517,14 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                                                ast_free(delim_value);
                                                                delim_value = NULL;
 
-                                                               ast_debug(4, "LINE(%d) DELIM pos: %d i: %d\n", __LINE__, pos, i);
+                                                               ast_debug(4, "DELIM pos: %d i: %d\n", pos, i);
                                                        } else {
                                                                /* not delimited */
                                                                if (delim_value) {
                                                                        ast_free(delim_value);
                                                                        delim_value = NULL;
                                                                }
-                                                               ast_debug(2, "LINE(%d) attribute_name: %s value: %s\n", __LINE__, attribute_name, valptr);
+                                                               ast_debug(2, "attribute_name: %s value: %s\n", attribute_name, valptr);
 
                                                                if (prev) {
                                                                        prev->next = ast_variable_new(attribute_name, valptr, table_config->table_name);
@@ -548,7 +548,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                                        const struct ast_variable *tmpdebug = variable_named(var, "variable_name");
                                        const struct ast_variable *tmpdebug2 = variable_named(var, "variable_value");
                                        if (tmpdebug && tmpdebug2) {
-                                               ast_debug(3, "LINE(%d) Added to vars - %s = %s\n", __LINE__, tmpdebug->value, tmpdebug2->value);
+                                               ast_debug(3, "Added to vars - %s = %s\n", tmpdebug->value, tmpdebug2->value);
                                        }
                                }
                                vars[entry_index++] = var;
@@ -559,7 +559,7 @@ static struct ast_variable **realtime_ldap_result_to_vars(struct ldap_table_conf
                } while (delim_count <= delim_tot_count && static_table_config == table_config);
 
                if (static_table_config != table_config) {
-                       ast_debug(3, "LINE(%d) Added to vars - non static\n", __LINE__);
+                       ast_debug(3, "Added to vars - non static\n");
 
                        vars[entry_index++] = var;
                        prev = NULL;
@@ -926,13 +926,8 @@ static struct ast_variable **realtime_ldap_base_ap(unsigned int *entries_count_p
                }
        }
 
-       if (filter) {
-               ast_free(filter);
-       }
-
-       if (clean_basedn) {
-               ast_free(clean_basedn);
-       }
+       ast_free(filter);
+       ast_free(clean_basedn);
 
        ast_mutex_unlock(&ldap_lock);
 
@@ -1136,7 +1131,7 @@ static struct ast_config *config_ldap(const char *basedn, const char *table_name
         * first, and since the data could easily exceed stack size, this is
         * allocated from the heap.
         */
-       if (!(categories = ast_calloc(sizeof(*categories), vars_count))) {
+       if (!(categories = ast_calloc(vars_count, sizeof(*categories)))) {
                return NULL;
        }
 
@@ -1217,6 +1212,90 @@ static struct ast_config *config_ldap(const char *basedn, const char *table_name
        return cfg;
 }
 
+/*!
+ * \internal
+ * \brief Remove LDAP_MOD_DELETE modifications that will not succeed
+ *
+ * \details
+ * A LDAP_MOD_DELETE operation will fail if the LDAP entry does not already have
+ * the corresponding attribute. Because we may be updating multiple LDAP entries
+ * in a single call to update_ldap(), we may need our own copy of the
+ * modifications array for each one.
+ *
+ * \note
+ * This function dynamically allocates memory. If it returns a non-NULL pointer,
+ * it is up to the caller to free it with ldap_mods_free()
+ *
+ * \returns an LDAPMod * if modifications needed to be removed, NULL otherwise.
+ */
+static LDAPMod **massage_mods_for_entry(LDAPMessage *entry, LDAPMod **mods, size_t count)
+{
+       size_t i;
+       int remove[count];
+       size_t remove_count = 0;
+
+       for (i = 0; i < count; i++) {
+               BerElement *ber = NULL;
+               char *attribute;
+               int exists = 0;
+
+               if (mods[i]->mod_op != LDAP_MOD_DELETE) {
+                       continue;
+               }
+
+               /* If we are deleting something, it has to exist */
+               attribute = ldap_first_attribute(ldapConn, entry, &ber);
+               while (attribute) {
+                       if (!strcasecmp(attribute, mods[i]->mod_type)) {
+                               /* OK, we have the attribute */
+                               exists = 1;
+                               ldap_memfree(attribute);
+                               break;
+                       }
+
+                       ldap_memfree(attribute);
+                       attribute = ldap_next_attribute(ldapConn, entry, ber);
+               }
+
+               if (!exists) {
+                       remove[remove_count++] = i;
+               }
+       }
+
+       if (remove_count) {
+               size_t k, remove_index;
+               LDAPMod **x = ldap_memcalloc(count - remove_count + 1, sizeof(LDAPMod *));
+               for (i = 0, k = 0; i < count; i++) {
+                       int skip = 0;
+                       /* Is this one we have to remove? */
+                       for (remove_index = 0; !skip && remove_index < remove_count; remove_index++) {
+                               skip = (remove[remove_index] == i);
+                       }
+
+                       if (skip) {
+                               ast_debug(3, "Skipping %s deletion because it doesn't exist\n",
+                                               mods[i]->mod_type);
+                               continue;
+                       }
+
+                       x[k] = ldap_memcalloc(1, sizeof(LDAPMod));
+                       x[k]->mod_op = mods[i]->mod_op;
+                       x[k]->mod_type = ldap_strdup(mods[i]->mod_type);
+                       if (mods[i]->mod_values) {
+                               x[k]->mod_values = ldap_memcalloc(2, sizeof(char *));
+                               x[k]->mod_values[0] = ldap_strdup(mods[i]->mod_values[0]);
+                       }
+                       k++;
+               }
+               /* NULL terminate */
+               x[k] = NULL;
+               return x;
+       }
+
+       return NULL;
+}
+
+
 /* \brief Function to update a set of values in ldap static mode
  */
 static int update_ldap(const char *basedn, const char *table_name, const char *attribute,
@@ -1249,7 +1328,7 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
        }
 
        if (!attribute || !lookup) {
-               ast_log(LOG_WARNING, "LINE(%d): search parameters are empty.\n", __LINE__);
+               ast_log(LOG_WARNING, "Search parameters are empty.\n");
                return -1;
        }
        ast_mutex_lock(&ldap_lock);
@@ -1285,19 +1364,22 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
         * one parameter/value pair and delimit them with a semicolon */
        newparam = convert_attribute_name_to_ldap(table_config, field->name);
        if (!newparam) {
-               ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
+               ast_log(LOG_WARNING, "Need at least one parameter to modify.\n");
                return -1;
        }
 
        mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
-       ldap_mods = ldap_memcalloc(sizeof(LDAPMod *), mods_size);
+       ldap_mods = ldap_memcalloc(mods_size, sizeof(LDAPMod *));
        ldap_mods[0] = ldap_memcalloc(1, sizeof(LDAPMod));
-
-       ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
        ldap_mods[0]->mod_type = ldap_strdup(newparam);
 
-       ldap_mods[0]->mod_values = ast_calloc(sizeof(char *), 2);
-       ldap_mods[0]->mod_values[0] = ldap_strdup(field->value);
+       if (strlen(field->value) == 0) {
+               ldap_mods[0]->mod_op = LDAP_MOD_DELETE;
+       } else {
+               ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
+               ldap_mods[0]->mod_values = ldap_memcalloc(2, sizeof(char *));
+               ldap_mods[0]->mod_values[0] = ldap_strdup(field->value);
+       }
 
        while ((field = field->next)) {
                newparam = convert_attribute_name_to_ldap(table_config, field->name);
@@ -1309,7 +1391,7 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
                                ldap_mods[i]->mod_values[0] = ldap_memrealloc(ldap_mods[i]->mod_values[0], sizeof(char) * (strlen(ldap_mods[i]->mod_values[0]) + strlen(field->value) + 2));
                                strcat(ldap_mods[i]->mod_values[0], ";");
                                strcat(ldap_mods[i]->mod_values[0], field->value);
-                               mod_exists = 1; 
+                               mod_exists = 1;
                                break;
                        }
                }
@@ -1318,22 +1400,19 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
                if (!mod_exists) {
                        mods_size++;
                        ldap_mods = ldap_memrealloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
-                       ldap_mods[mods_size - 1] = NULL;
-                       
                        ldap_mods[mods_size - 2] = ldap_memcalloc(1, sizeof(LDAPMod));
-
-                       ldap_mods[mods_size - 2]->mod_type = ldap_memcalloc(sizeof(char), strlen(newparam) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
+                       ldap_mods[mods_size - 2]->mod_type = ldap_strdup(newparam);
 
                        if (strlen(field->value) == 0) {
                                ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_DELETE;
                        } else {
                                ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
-
-                               ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(sizeof(char *), 2);
-                               ldap_mods[mods_size - 2]->mod_values[0] = ldap_memcalloc(sizeof(char), strlen(field->value) + 1);
-                               strcpy(ldap_mods[mods_size - 2]->mod_values[0], field->value);
+                               ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(2, sizeof(char *));
+                               ldap_mods[mods_size - 2]->mod_values[0] = ldap_strdup(field->value);
                        }
+
+                       /* NULL terminate */
+                       ldap_mods[mods_size - 1] = NULL;
                }
        }
        /* freeing ldap_mods further down */
@@ -1366,27 +1445,45 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
                ast_free(filter);
                ast_free(clean_basedn);
                ldap_msgfree(ldap_result_msg);
-               ldap_mods_free(ldap_mods, 0);
+               ldap_mods_free(ldap_mods, 1);
                return -1;
        }
        /* Ready to update */
        if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
-               ast_debug(3, "LINE(%d) Modifying %s=%s hits: %d\n", __LINE__, attribute, lookup, num_entries);
+               ast_debug(3, "Modifying %s=%s hits: %d\n", attribute, lookup, num_entries);
                for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
                        if (ldap_mods[i]->mod_op != LDAP_MOD_DELETE) {
-                               ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
+                               ast_debug(3, "%s=%s\n", ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
                        } else {
-                               ast_debug(3, "LINE(%d) deleting %s \n", __LINE__, ldap_mods[i]->mod_type);
+                               ast_debug(3, "deleting %s\n", ldap_mods[i]->mod_type);
                        }
                }
                ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
 
-               for (i = 0; ldap_entry; i++) { 
+               for (i = 0; ldap_entry; i++) {
+                       LDAPMod **working = ldap_mods;
+                       LDAPMod **massaged = massage_mods_for_entry(ldap_entry, ldap_mods, mods_size - 1);
+
+                       if (massaged) {
+                               /* Did we massage everything out of the list? */
+                               if (massaged[0] == NULL) {
+                                       ast_debug(3, "Nothing left to modify - skipping\n");
+                                       ldap_mods_free(massaged, 1);
+                                       continue;
+                               }
+                               working = massaged;
+                       }
+
                        dn = ldap_get_dn(ldapConn, ldap_entry);
-                       if ((error = ldap_modify_ext_s(ldapConn, dn, ldap_mods, NULL, NULL)) != LDAP_SUCCESS)  {
+                       if ((error = ldap_modify_ext_s(ldapConn, dn, working, NULL, NULL)) != LDAP_SUCCESS)  {
                                ast_log(LOG_ERROR, "Couldn't modify '%s'='%s', dn:%s because %s\n",
                                                attribute, lookup, dn, ldap_err2string(error));
                        }
+
+                       if (massaged) {
+                               ldap_mods_free(massaged, 1);
+                       }
+
                        ldap_memfree(dn);
                        ldap_entry = ldap_next_entry(ldapConn, ldap_entry);
                }
@@ -1396,7 +1493,7 @@ static int update_ldap(const char *basedn, const char *table_name, const char *a
        ast_free(filter);
        ast_free(clean_basedn);
        ldap_msgfree(ldap_result_msg);
-       ldap_mods_free(ldap_mods, 0);
+       ldap_mods_free(ldap_mods, 1);
        return num_entries;
 }
 
@@ -1469,23 +1566,19 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
        field = update_fields;
        newparam = convert_attribute_name_to_ldap(table_config, field->name);
        if (!newparam) {
-               ast_log(LOG_WARNING, "LINE(%d): need at least one parameter to modify.\n", __LINE__);
+               ast_log(LOG_WARNING, "Need at least one parameter to modify.\n");
                ast_free(filter);
                ast_free(clean_basedn);
                return -1;
        }
 
        mods_size = 2; /* one for the first param/value pair and one for the the terminating NULL */
-       ldap_mods = ast_calloc(sizeof(LDAPMod *), mods_size);
-       ldap_mods[0] = ast_calloc(1, sizeof(LDAPMod));
-
+       ldap_mods = ldap_memcalloc(mods_size, sizeof(LDAPMod *));
+       ldap_mods[0] = ldap_memcalloc(1, sizeof(LDAPMod));
        ldap_mods[0]->mod_op = LDAP_MOD_REPLACE;
-       ldap_mods[0]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
-       strcpy(ldap_mods[0]->mod_type, newparam);
-
-       ldap_mods[0]->mod_values = ast_calloc(sizeof(char), 2);
-       ldap_mods[0]->mod_values[0] = ast_calloc(sizeof(char), strlen(field->value) + 1);
-       strcpy(ldap_mods[0]->mod_values[0], field->value);
+       ldap_mods[0]->mod_type = ldap_strdup(newparam);
+       ldap_mods[0]->mod_values = ldap_memcalloc(2, sizeof(char *));
+       ldap_mods[0]->mod_values[0] = ldap_strdup(field->value);
 
        while ((field = field->next)) {
                newparam = convert_attribute_name_to_ldap(table_config, field->name);
@@ -1505,18 +1598,15 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
                /* create new mod */
                if (!mod_exists) {
                        mods_size++;
-                       ldap_mods = ast_realloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
-                       ldap_mods[mods_size - 1] = NULL;
-                       ldap_mods[mods_size - 2] = ast_calloc(1, sizeof(LDAPMod));
-
+                       ldap_mods = ldap_memrealloc(ldap_mods, sizeof(LDAPMod *) * mods_size);
+                       ldap_mods[mods_size - 2] = ldap_memcalloc(1, sizeof(LDAPMod));
                        ldap_mods[mods_size - 2]->mod_op = LDAP_MOD_REPLACE;
+                       ldap_mods[mods_size - 2]->mod_type = ldap_strdup(newparam);
+                       ldap_mods[mods_size - 2]->mod_values = ldap_memcalloc(2, sizeof(char *));
+                       ldap_mods[mods_size - 2]->mod_values[0] = ldap_strdup(field->value);
 
-                       ldap_mods[mods_size - 2]->mod_type = ast_calloc(sizeof(char), strlen(newparam) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_type, newparam);
-
-                       ldap_mods[mods_size - 2]->mod_values = ast_calloc(sizeof(char *), 2);
-                       ldap_mods[mods_size - 2]->mod_values[0] = ast_calloc(sizeof(char), strlen(field->value) + 1);
-                       strcpy(ldap_mods[mods_size - 2]->mod_values[0], field->value);
+                       /* NULL terminate */
+                       ldap_mods[mods_size - 1] = NULL;
                }
        }
        /* freeing ldap_mods further down */
@@ -1550,13 +1640,13 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
                ast_free(filter);
                ast_free(clean_basedn);
                ldap_msgfree(ldap_result_msg);
-               ldap_mods_free(ldap_mods, 0);
+               ldap_mods_free(ldap_mods, 1);
                return -1;
        }
        /* Ready to update */
        if ((num_entries = ldap_count_entries(ldapConn, ldap_result_msg)) > 0) {
                for (i = 0; option_debug > 2 && i < mods_size - 1; i++) {
-                       ast_debug(3, "LINE(%d) %s=%s \n", __LINE__, ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
+                       ast_debug(3, "%s=%s\n", ldap_mods[i]->mod_type, ldap_mods[i]->mod_values[0]);
                }
 
                ldap_entry = ldap_first_entry(ldapConn, ldap_result_msg);
@@ -1572,14 +1662,10 @@ static int update2_ldap(const char *basedn, const char *table_name, const struct
        }
 
        ast_mutex_unlock(&ldap_lock);
-       if (filter) {
-               ast_free(filter);
-       }
-       if (clean_basedn) {
-               ast_free(clean_basedn);
-       }
+       ast_free(filter);
+       ast_free(clean_basedn);
        ldap_msgfree(ldap_result_msg);
-       ldap_mods_free(ldap_mods, 0);
+       ldap_mods_free(ldap_mods, 1);
        return num_entries;
 }
 
@@ -1681,6 +1767,21 @@ static int reload(void)
        return 0;
 }
 
+static int config_can_be_inherited(const char *key)
+{
+       int i;
+       static const char * const config[] = {
+               "basedn", "host", "pass", "port", "protocol", "url", "user", "version", NULL
+       };
+
+       for (i = 0; config[i]; i++) {
+               if (!strcasecmp(key, config[i])) {
+                       return 0;
+               }
+       }
+       return 1;
+}
+
 /*! \brief parse the configuration file
  */
 static int parse_config(void)
@@ -1771,7 +1872,9 @@ static int parse_config(void)
                                if (!strcasecmp(var->name, "additionalFilter")) {
                                        table_config->additional_filter = ast_strdup(var->value);
                                } else {
-                                       ldap_table_config_add_attribute(table_config, var->name, var->value);
+                                       if (!is_general || config_can_be_inherited(var->name)) {
+                                               ldap_table_config_add_attribute(table_config, var->name, var->value);
+                                       }
                                }
                        }
                }
index 25a4827..f085961 100644 (file)
@@ -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) {
index 50d4095..8c514b0 100644 (file)
@@ -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);
@@ -777,6 +779,8 @@ static struct ast_config *realtime_sqlite3_load(const char *database, const char
        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)
 {
@@ -802,6 +806,15 @@ 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) {
@@ -1307,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
  *
@@ -1319,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_sdp_translator_pjmedia.c b/res/res_sdp_translator_pjmedia.c
new file mode 100644 (file)
index 0000000..141b976
--- /dev/null
@@ -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,
+);