[svn-commits] mmichelson: branch mmichelson/sip_options r394172 - in /team/mmichelson/sip_o...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Jul 11 16:26:46 CDT 2013


Author: mmichelson
Date: Thu Jul 11 16:26:44 2013
New Revision: 394172

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=394172
Log:
Add "useragent" and "maxforwards" options.

For these options, a new "global" section was created. This is similar
to the "system" section except that reloads will actually pick up new
values from the "global" section.

A new PJSIP module was added in order to facilitate these options. The
module registers handlers that are called on outgoing requests and responses
and adds the configured headers.


Added:
    team/mmichelson/sip_options/res/res_sip/config_global.c   (with props)
Modified:
    team/mmichelson/sip_options/include/asterisk/res_sip.h
    team/mmichelson/sip_options/res/res_sip.c
    team/mmichelson/sip_options/res/res_sip.exports.in
    team/mmichelson/sip_options/res/res_sip/include/res_sip_private.h
    team/mmichelson/sip_options/res/res_sip/sip_configuration.c

Modified: team/mmichelson/sip_options/include/asterisk/res_sip.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/sip_options/include/asterisk/res_sip.h?view=diff&rev=394172&r1=394171&r2=394172
==============================================================================
--- team/mmichelson/sip_options/include/asterisk/res_sip.h (original)
+++ team/mmichelson/sip_options/include/asterisk/res_sip.h Thu Jul 11 16:26:44 2013
@@ -1336,4 +1336,12 @@
  */
 void ast_sip_report_auth_challenge_sent(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_tx_data *tdata);
 
+void ast_sip_initialize_global_headers(void);
+void ast_sip_destroy_global_headers(void);
+
+int ast_sip_add_global_request_header(const char *name, const char *value, int replace);
+int ast_sip_add_global_response_header(const char *name, const char *value, int replace);
+
+int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery);
+
 #endif /* _RES_SIP_H */

Modified: team/mmichelson/sip_options/res/res_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/sip_options/res/res_sip.c?view=diff&rev=394172&r1=394171&r2=394172
==============================================================================
--- team/mmichelson/sip_options/res/res_sip.c (original)
+++ team/mmichelson/sip_options/res/res_sip.c Thu Jul 11 16:26:44 2013
@@ -778,6 +778,19 @@
 					<synopsis>Use the short forms of common SIP header names.</synopsis>
 				</configOption>
 			</configObject>
+			<configObject name="global">
+				<synopsis>Options that apply globally to all SIP communications</synopsis>
+				<description><para>
+					The settings in this section are global. Unlike options in the <literal>system</literal>
+					section, these options can be refreshed by performing a reload.
+				</para></description>
+				<configOption name="maxforwards" default="70">
+					<synopsis>Value used in Max-Forwards header for SIP requests.</synopsis>
+				</configOption>
+				<configOption name="useragent" default="Asterisk &lt;Asterisk Version&gt;">
+					<synopsis>Value used in User-Agent header for SIP requests and Server header for SIP responses.</synopsis>
+				</configOption>
+			</configObject>
 		</configFile>
 	</configInfo>
  ***/
@@ -1547,6 +1560,18 @@
 	return *servant_id == SIP_SERVANT_ID;
 }
 
+static void remove_request_headers(pjsip_endpoint *endpt)
+{
+	const pjsip_hdr *request_headers = pjsip_endpt_get_request_headers(endpt);
+	pjsip_hdr *iter = request_headers->next;
+
+	while (iter != request_headers) {
+		pjsip_hdr *to_erase = iter;
+		iter = iter->next;
+		pj_list_erase(to_erase);
+	}
+}
+
 static int load_module(void)
 {
     /* The third parameter is just copied from
@@ -1584,6 +1609,12 @@
 		ast_log(LOG_ERROR, "Failed to create PJSIP endpoint structure. Aborting load\n");
 		goto error;
 	}
+
+	/* PJSIP will automatically try to add a Max-Forwards header. Since we want to control that,
+	 * we need to stop PJSIP from doing it automatically
+	 */
+	remove_request_headers(ast_pjsip_endpoint);
+
 	memory_pool = pj_pool_create(&caching_pool.factory, "SIP", 1024, 1024, NULL);
 	if (!memory_pool) {
 		ast_log(LOG_ERROR, "Failed to create memory pool for SIP. Aborting load\n");
@@ -1606,6 +1637,8 @@
 		goto error;
 	}
 
+	ast_sip_initialize_global_headers();
+
 	if (ast_res_sip_initialize_configuration()) {
 		ast_log(LOG_ERROR, "Failed to initialize SIP configuration. Aborting load\n");
 		goto error;
@@ -1630,6 +1663,7 @@
 error:
 	ast_sip_destroy_distributor();
 	ast_res_sip_destroy_configuration();
+	ast_sip_destroy_global_headers();
 	if (monitor_thread) {
 		stop_monitor_thread();
 	}
@@ -1673,6 +1707,7 @@
 {
 	ast_sip_destroy_distributor();
 	ast_res_sip_destroy_configuration();
+	ast_sip_destroy_global_headers();
 	if (monitor_thread) {
 		stop_monitor_thread();
 	}

Modified: team/mmichelson/sip_options/res/res_sip.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/sip_options/res/res_sip.exports.in?view=diff&rev=394172&r1=394171&r2=394172
==============================================================================
--- team/mmichelson/sip_options/res/res_sip.exports.in (original)
+++ team/mmichelson/sip_options/res/res_sip.exports.in Thu Jul 11 16:26:44 2013
@@ -60,6 +60,11 @@
 		LINKER_SYMBOL_PREFIXast_sip_report_auth_failed_challenge_response;
 		LINKER_SYMBOL_PREFIXast_sip_report_auth_success;
 		LINKER_SYMBOL_PREFIXast_sip_report_auth_challenge_sent;
+		LINKER_SYMBOL_PREFIXast_sip_initialize_global_headers;
+		LINKER_SYMBOL_PREFIXast_sip_destroy_global_headers;
+		LINKER_SYMBOL_PREFIXast_sip_add_global_request_header;
+		LINKER_SYMBOL_PREFIXast_sip_add_global_response_header;
+		LINKER_SYMBOL_PREFIXast_sip_initialize_sorcery_global;
 	local:
 		*;
 };

Added: team/mmichelson/sip_options/res/res_sip/config_global.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/sip_options/res/res_sip/config_global.c?view=auto&rev=394172
==============================================================================
--- team/mmichelson/sip_options/res/res_sip/config_global.c (added)
+++ team/mmichelson/sip_options/res/res_sip/config_global.c Thu Jul 11 16:26:44 2013
@@ -1,0 +1,90 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Mark Michelson <mmichelson at 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 <pjsip.h>
+#include <pjlib.h>
+
+#include "asterisk/res_sip.h"
+#include "asterisk/sorcery.h"
+#include "asterisk/ast_version.h"
+
+#define DEFAULT_MAX_FORWARDS 70
+#define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
+
+static char default_useragent[128];
+
+struct global_config {
+	SORCERY_OBJECT(details);
+	AST_DECLARE_STRING_FIELDS(
+		AST_STRING_FIELD(useragent);
+	);
+	/* Value to put in Max-Forwards header */
+	unsigned int max_forwards;
+};
+
+static void global_destructor(void *obj)
+{
+	struct global_config *cfg = obj;
+
+	ast_string_field_free_memory(cfg);
+}
+
+static void *global_alloc(const char *name)
+{
+	struct global_config *cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
+
+	if (!cfg || ast_string_field_init(cfg, 64)) {
+		return NULL;
+	}
+
+	return cfg;
+}
+
+static int global_apply(const struct ast_sorcery *sorcery, void *obj)
+{
+	struct global_config *cfg = obj;
+	char max_forwards[10];
+
+	snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
+
+	ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
+	ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
+	ast_sip_add_global_response_header("Server", cfg->useragent, 1);
+	return 0;
+}
+
+int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery)
+{
+	snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
+
+	ast_sorcery_apply_default(sorcery, "global", "config", "res_sip.conf,criteria=type=global");
+
+	if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
+		return -1;
+	}
+
+	ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
+	ast_sorcery_object_field_register(sorcery, "global", "maxforwards", __stringify(DEFAULT_MAX_FORWARDS),
+			OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
+	ast_sorcery_object_field_register(sorcery, "global", "useragent", default_useragent,
+			OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
+
+	return 0;
+}

Propchange: team/mmichelson/sip_options/res/res_sip/config_global.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mmichelson/sip_options/res/res_sip/config_global.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mmichelson/sip_options/res/res_sip/config_global.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/mmichelson/sip_options/res/res_sip/include/res_sip_private.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/sip_options/res/res_sip/include/res_sip_private.h?view=diff&rev=394172&r1=394171&r2=394172
==============================================================================
--- team/mmichelson/sip_options/res/res_sip/include/res_sip_private.h (original)
+++ team/mmichelson/sip_options/res/res_sip/include/res_sip_private.h Thu Jul 11 16:26:44 2013
@@ -63,4 +63,12 @@
  */
 int ast_sip_initialize_system(void);
 
+/*!
+ * \brief Initialize global configuration
+ *
+ * \retval 0 Success
+ * \retval non-zero Failure
+ */
+int ast_sip_initialize_global(void);
+
 #endif /* RES_SIP_PRIVATE_H_ */

Modified: team/mmichelson/sip_options/res/res_sip/sip_configuration.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/sip_options/res/res_sip/sip_configuration.c?view=diff&rev=394172&r1=394171&r2=394172
==============================================================================
--- team/mmichelson/sip_options/res/res_sip/sip_configuration.c (original)
+++ team/mmichelson/sip_options/res/res_sip/sip_configuration.c Thu Jul 11 16:26:44 2013
@@ -708,6 +708,13 @@
 		return -1;
 	}
 
+	if (ast_sip_initialize_sorcery_global(sip_sorcery)) {
+		ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
+		ast_sorcery_unref(sip_sorcery);
+		sip_sorcery = NULL;
+		return -1;
+	}
+
 	ast_sorcery_load(sip_sorcery);
 
 	return 0;




More information about the svn-commits mailing list