[asterisk-commits] file: branch group/dns_pjsip r434211 - in /team/group/dns_pjsip: ./ channels/...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Apr 7 06:11:08 CDT 2015


Author: file
Date: Tue Apr  7 06:11:00 2015
New Revision: 434211

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=434211
Log:
Multiple revisions 434068,434089,434092,434099,434115,434132,434154,434170,434189,434192

........
  r434068 | mmichelson | 2015-04-06 14:05:47 -0300 (Mon, 06 Apr 2015) | 9 lines
  
  Merge NAPTR support into trunk.
  
  This adds NAPTR record allocation and sorting, as well as
  unit tests that verify that NAPTR records are parsed and
  sorted correctly.
  
  Review: https://reviewboard.asterisk.org/r/4542
........
  r434089 | mjordan | 2015-04-06 14:52:31 -0300 (Mon, 06 Apr 2015) | 20 lines
  
  clang compiler warnings: Fix format specified in framehook
  
  This patch fixes an invalid format specifier used in the formatting of an
  ERROR message in the framehook code. The format specifier specifies a
  type of 'unsigned short', but the argument passed to it is of type 'int'.
  The patch changes the format specifier to 'i'.
  
  Review: https://reviewboard.asterisk.org/r/4540
  
  ASTERISK-24917
  Reported by: dkdegroot
  patches:
    rb4535.patch submitted by dkdegroot (License 6600)
  ........
  
  Merged revisions 434087 from http://svn.asterisk.org/svn/asterisk/branches/11
  ........
  
  Merged revisions 434088 from http://svn.asterisk.org/svn/asterisk/branches/13
........
  r434092 | mjordan | 2015-04-06 15:03:54 -0300 (Mon, 06 Apr 2015) | 19 lines
  
  clang compiler warnings: Fix sometimes-uninitialized warning in pbx_config
  
  This patch fixes a warning caught by clang, in which a char pointer could be
  assigned to before it was initialized. The patch re-organizes the code to
  ensure that the pointer is always initialized, even on off nominal paths.
  
  Review: https://reviewboard.asterisk.org/r/4529
  
  ASTERISK-24917
  Reported by: dkdegroot
  patches:
    rb4529.patch submitted by dkdegroot (License 6600)
  ........
  
  Merged revisions 434090 from http://svn.asterisk.org/svn/asterisk/branches/11
  ........
  
  Merged revisions 434091 from http://svn.asterisk.org/svn/asterisk/branches/13
........
  r434099 | mjordan | 2015-04-06 15:18:32 -0300 (Mon, 06 Apr 2015) | 22 lines
  
  clang compiler warnings: Remove large chunks of unused code from extconf
  
  This patch fixes a warning caught by clang, in which it detected that large
  chunks of extconf were unused. Frankly, I wish we could pretend that all of
  extconf was unused, but alas, that is not yet the case.
  
  A few extraneous functions in the parking tests were removed as well, for
  the same reason.
  
  Review: https://reviewboard.asterisk.org/r/4553
  
  ASTERISK-24917
  Reported by: dkdegroot
  patches:
    rb4553.patch submitted by dkdegroot (License 6600)
  ........
  
  Merged revisions 434093 from http://svn.asterisk.org/svn/asterisk/branches/11
  ........
  
  Merged revisions 434097 from http://svn.asterisk.org/svn/asterisk/branches/13
........
  r434115 | gtjoseph | 2015-04-06 16:04:32 -0300 (Mon, 06 Apr 2015) | 26 lines
  
  build: Fixes for gcc 5 compilation
  
  These are fixes for compilation under gcc 5.0...
  
  chan_sip.c:    In parse_request needed to make 'lim' unsigned.
  inline_api.h:  Needed to add a check for '__GNUC_STDC_INLINE__' to detect C99 
                 inline semantics (same as clang).
  ccss.c:        In ast_cc_set_parm, needed to fix weird comparison.
  dsp.c:         Needed to work around a possible compiler bug.  It was throwing 
                 an array-bounds error but neither
                 sgriepentrog, rmudgett nor I could figure out why.
  manager.c:     In action_atxfer, needed to correct an array allocation.
  
  This patch will go to 11, 13, trunk.
  
  Review: https://reviewboard.asterisk.org/r/4581/
  Reported-by: Jeffrey Ollie
  Tested-by: George Joseph
  ASTERISK-24932 #close
  ........
  
  Merged revisions 434113 from http://svn.asterisk.org/svn/asterisk/branches/11
  ........
  
  Merged revisions 434114 from http://svn.asterisk.org/svn/asterisk/branches/13
........
  r434132 | kharwell | 2015-04-06 16:23:57 -0300 (Mon, 06 Apr 2015) | 14 lines
  
  res_pjsip: config option 'timers' can't be set to 'no'
  
  When setting the configuration option 'timers' equal to 'no' the bit flag was
  not properly negated. This patch clears all associated flags and only sets the
  specified one. pjsip will handle any necessary flag combinations. Also went
  ahead and did similar for the '100rel' option.
  
  ASTERISK-24910 #close
  Reported by: Ray Crumrine
  Review: https://reviewboard.asterisk.org/r/4582/
  ........
  
  Merged revisions 434131 from http://svn.asterisk.org/svn/asterisk/branches/13
........
  r434154 | mmichelson | 2015-04-06 18:13:36 -0300 (Mon, 06 Apr 2015) | 3 lines
  
  Add missing DNS NAPTR test file.
........
  r434170 | mmichelson | 2015-04-06 18:54:53 -0300 (Mon, 06 Apr 2015) | 3 lines
  
  Uncomment test case.
........
  r434189 | mjordan | 2015-04-06 23:03:48 -0300 (Mon, 06 Apr 2015) | 19 lines
  
  clang compiler warnings: Fix non-literal-null-conversion warnings
  
  Clang will flag errors when a char pointer is set to '\0', as opposed to a
  value that the char pointer points to. This patch fixes this warning
  in a variety of locations.
  
  Review: https://reviewboard.asterisk.org/r/4551
  
  ASTERISK-24917
  Reported by: dkdegroot
  patches:
    rb4551.patch submitted by dkdegroot (License 6600)
  ........
  
  Merged revisions 434187 from http://svn.asterisk.org/svn/asterisk/branches/11
  ........
  
  Merged revisions 434188 from http://svn.asterisk.org/svn/asterisk/branches/13
........
  r434192 | mjordan | 2015-04-06 23:10:31 -0300 (Mon, 06 Apr 2015) | 20 lines
  
  clang compiler warnings: Fix sometimes-initialized warning in func_math
  
  This patch fixes a bug in a unit test in func_math where a variable could be
  passed to ast_free that wasn't allocated. This patch corrects the issue and
  ensures that we only attempt to free a variable if we previously allocated
  it.
  
  Review: https://reviewboard.asterisk.org/r/4552
  
  ASTERISK-24917
  Reported by: dkdegroot
  patches:
    rb4552.patch submitted by dkdegroot (License 6600)
  ........
  
  Merged revisions 434190 from http://svn.asterisk.org/svn/asterisk/branches/11
  ........
  
  Merged revisions 434191 from http://svn.asterisk.org/svn/asterisk/branches/13
........

Merged revisions 434068,434089,434092,434099,434115,434132,434154,434170,434189,434192 from http://svn.asterisk.org/svn/asterisk/trunk

Added:
    team/group/dns_pjsip/tests/test_dns_naptr.c
      - copied unchanged from r434192, trunk/tests/test_dns_naptr.c
Modified:
    team/group/dns_pjsip/   (props changed)
    team/group/dns_pjsip/channels/chan_sip.c
    team/group/dns_pjsip/channels/chan_skinny.c
    team/group/dns_pjsip/funcs/func_math.c
    team/group/dns_pjsip/include/asterisk/dns_internal.h   (contents, props changed)
    team/group/dns_pjsip/include/asterisk/inline_api.h
    team/group/dns_pjsip/main/ccss.c
    team/group/dns_pjsip/main/dns_core.c
    team/group/dns_pjsip/main/dns_naptr.c
    team/group/dns_pjsip/main/dsp.c
    team/group/dns_pjsip/main/framehook.c
    team/group/dns_pjsip/main/manager.c
    team/group/dns_pjsip/pbx/pbx_config.c
    team/group/dns_pjsip/res/parking/parking_tests.c
    team/group/dns_pjsip/res/res_pjsip.c
    team/group/dns_pjsip/res/res_pjsip/pjsip_configuration.c
    team/group/dns_pjsip/res/res_resolver_unbound.c
    team/group/dns_pjsip/res/stasis_recording/stored.c
    team/group/dns_pjsip/tests/test_dns.c   (props changed)
    team/group/dns_pjsip/tests/test_dns_recurring.c   (props changed)
    team/group/dns_pjsip/utils/extconf.c

Propchange: team/group/dns_pjsip/
------------------------------------------------------------------------------
    automerge = *

Propchange: team/group/dns_pjsip/
------------------------------------------------------------------------------
Binary property 'branch-13-merged' - no diff available.

Propchange: team/group/dns_pjsip/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Tue Apr  7 06:11:00 2015
@@ -1,1 +1,1 @@
-/trunk:1-434048
+/trunk:1-434210

Modified: team/group/dns_pjsip/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/channels/chan_sip.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/channels/chan_sip.c (original)
+++ team/group/dns_pjsip/channels/chan_sip.c Tue Apr  7 06:11:00 2015
@@ -9585,7 +9585,8 @@
 {
 	char *c = ast_str_buffer(req->data);
 	ptrdiff_t *dst = req->header;
-	int i = 0, lim = SIP_MAX_HEADERS - 1;
+	int i = 0;
+	unsigned int lim = SIP_MAX_HEADERS - 1;
 	unsigned int skipping_headers = 0;
 	ptrdiff_t current_header_offset = 0;
 	char *previous_header = "";
@@ -19727,13 +19728,13 @@
 	char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
 
 	while ((oldcontext = strsep(&old, "&"))) {
-		stalecontext = '\0';
+		stalecontext = NULL;
 		ast_copy_string(newlist, new, sizeof(newlist));
 		stringp = newlist;
 		while ((newcontext = strsep(&stringp, "&"))) {
 			if (!strcmp(newcontext, oldcontext)) {
 				/* This is not the context you're looking for */
-				stalecontext = '\0';
+				stalecontext = NULL;
 				break;
 			} else if (strcmp(newcontext, oldcontext)) {
 				stalecontext = oldcontext;

Modified: team/group/dns_pjsip/channels/chan_skinny.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/channels/chan_skinny.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/channels/chan_skinny.c (original)
+++ team/group/dns_pjsip/channels/chan_skinny.c Tue Apr  7 06:11:00 2015
@@ -2185,13 +2185,13 @@
 	char *oldcontext, *newcontext, *stalecontext, *stringp, newlist[AST_MAX_CONTEXT];
 
 	while ((oldcontext = strsep(&old, "&"))) {
-		stalecontext = '\0';
+		stalecontext = NULL;
 		ast_copy_string(newlist, new, sizeof(newlist));
 		stringp = newlist;
 		while ((newcontext = strsep(&stringp, "&"))) {
 			if (strcmp(newcontext, oldcontext) == 0) {
 				/* This is not the context you're looking for */
-				stalecontext = '\0';
+				stalecontext = NULL;
 				break;
 			} else if (strcmp(newcontext, oldcontext)) {
 				stalecontext = oldcontext;

Modified: team/group/dns_pjsip/funcs/func_math.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/funcs/func_math.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/funcs/func_math.c (original)
+++ team/group/dns_pjsip/funcs/func_math.c Tue Apr  7 06:11:00 2015
@@ -482,13 +482,11 @@
 
 	ast_test_status_update(test, "Testing MATH() substitution ...\n");
 
-	if (!(expr = ast_str_create(16)) || !(result = ast_str_create(16))) {
-		if (expr) {
-			ast_free(expr);
-		}
-		if (result) {
-			ast_free(result);
-		}
+	if (!(expr = ast_str_create(16))) {
+		return AST_TEST_FAIL;
+	}
+	if (!(result = ast_str_create(16))) {
+		ast_free(expr);
 		return AST_TEST_FAIL;
 	}
 

Modified: team/group/dns_pjsip/include/asterisk/dns_internal.h
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/include/asterisk/dns_internal.h?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/include/asterisk/dns_internal.h (original)
+++ team/group/dns_pjsip/include/asterisk/dns_internal.h Tue Apr  7 06:11:00 2015
@@ -35,6 +35,14 @@
 	size_t data_len;
 	/*! \brief Linked list information */
 	AST_LIST_ENTRY(ast_dns_record) list;
+	/*! \brief pointer to record-specific data.
+	 *
+	 * For certain "subclasses" of DNS records, the
+	 * location of the raw DNS data will differ from
+	 * the generic case. This pointer will reliably
+	 * be set to point to the raw DNS data, no matter
+	 * where in the structure it may lie.
+	 */
 	char *data_ptr;
 	/*! \brief The raw DNS record */
 	char data[0];
@@ -74,6 +82,13 @@
 	unsigned short order;
 	/*! \brief The preference of the NAPTR record */
 	unsigned short preference;
+	/*! \brief Buffer for NAPTR-specific data
+	 *
+	 * This includes the raw NAPTR record, as well as
+	 * the area where the flags, service, regexp, and
+	 * replacement strings are stored.
+	 */
+	char data[0];
 };
 
 /*! \brief The result of a DNS query */
@@ -174,6 +189,25 @@
 struct ast_sched_context *ast_dns_get_sched(void);
 
 /*!
+ * \brief Allocate and parse a DNS NAPTR record
+ *
+ * \param query The DNS query
+ * \param data This specific NAPTR record
+ * \param size The size of the NAPTR record
+ *
+ * \retval non-NULL success
+ * \retval NULL failure
+ */
+struct ast_dns_record *dns_naptr_alloc(struct ast_dns_query *query, const char *data, const size_t size);
+
+/*!
+ * \brief Sort the NAPTR records on a result
+ *
+ * \param result The DNS result
+ */
+void dns_naptr_sort(struct ast_dns_result *result);
+
+/*!
  * \brief Allocate and parse a DNS SRV record
  *
  * \param query The DNS query

Propchange: team/group/dns_pjsip/include/asterisk/dns_internal.h
------------------------------------------------------------------------------
--- svn:keywords (original)
+++ svn:keywords Tue Apr  7 06:11:00 2015
@@ -1,1 +1,1 @@
-Author Date Id Revision
+'Author Date Id Revision'

Modified: team/group/dns_pjsip/include/asterisk/inline_api.h
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/include/asterisk/inline_api.h?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/include/asterisk/inline_api.h (original)
+++ team/group/dns_pjsip/include/asterisk/inline_api.h Tue Apr  7 06:11:00 2015
@@ -48,7 +48,7 @@
 #if !defined(LOW_MEMORY) && !defined(DISABLE_INLINE)
 
 #if !defined(AST_API_MODULE)
-#if defined(__clang__)
+#if defined(__clang__) || defined(__GNUC_STDC_INLINE__)
 #define AST_INLINE_API(hdr, body) static hdr; static inline hdr body
 #else /* if defined(__clang__) */
 #define AST_INLINE_API(hdr, body) hdr; extern inline hdr body

Modified: team/group/dns_pjsip/main/ccss.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/main/ccss.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/main/ccss.c (original)
+++ team/group/dns_pjsip/main/ccss.c Tue Apr  7 06:11:00 2015
@@ -825,7 +825,7 @@
 		return 0;
 	}
 
-	if (!sscanf(value, "%30u", &value_as_uint) == 1) {
+	if (sscanf(value, "%30u", &value_as_uint) != 1) {
 		return -1;
 	}
 

Modified: team/group/dns_pjsip/main/dns_core.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/main/dns_core.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/main/dns_core.c (original)
+++ team/group/dns_pjsip/main/dns_core.c Tue Apr  7 06:11:00 2015
@@ -478,7 +478,9 @@
 		return -1;
 	}
 
-	if (rr_type == ns_t_srv) {
+	if (rr_type == ns_t_naptr) {
+		record = dns_naptr_alloc(query, data, size);
+	} else if (rr_type == ns_t_srv) {
 		record = ast_dns_srv_alloc(query, data, size);
 	} else {
 		record = generic_record_alloc(query, data, size);
@@ -501,7 +503,9 @@
 
 void ast_dns_resolver_completed(struct ast_dns_query *query)
 {
-	if (ast_dns_query_get_rr_type(query) == ns_t_srv) {
+	if (ast_dns_query_get_rr_type(query) == ns_t_naptr) {
+		dns_naptr_sort(query->result);
+	} else if (ast_dns_query_get_rr_type(query) == ns_t_srv) {
 		ast_dns_srv_sort(query->result);
 	}
 

Modified: team/group/dns_pjsip/main/dns_naptr.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/main/dns_naptr.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/main/dns_naptr.c (original)
+++ team/group/dns_pjsip/main/dns_naptr.c Tue Apr  7 06:11:00 2015
@@ -31,35 +31,675 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
+#include <arpa/nameser.h>
+#include <resolv.h>
+#include <regex.h>
+
 #include "asterisk/dns_core.h"
 #include "asterisk/dns_naptr.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/vector.h"
+#include "asterisk/dns_query_set.h"
+#include "asterisk/dns_internal.h"
+#include "asterisk/utils.h"
+
+/*!
+ * \brief Result of analyzing NAPTR flags on a record
+ */
+enum flags_result {
+	/*! Terminal record, meaning the DDDS algorithm can be stopped */
+	FLAGS_TERMINAL,
+	/*! No flags provided, likely meaning another NAPTR lookup */
+	FLAGS_EMPTY,
+	/*! Unrecognized but valid flags. We cannot conclude what they mean */
+	FLAGS_UNKNOWN,
+	/*! Non-alphanumeric or invalid combination of flags */
+	FLAGS_INVALID,
+};
+
+/*!
+ * \brief Analyze and interpret NAPTR flags as per RFC 3404
+ *
+ * \note The flags string passed into this function is NOT NULL-terminated
+ *
+ * \param flags The flags string from a NAPTR record
+ * \flags_size The size of the flags string in bytes
+ * \return flag result
+ */
+static enum flags_result interpret_flags(const char *flags, uint8_t flags_size)
+{
+	int i;
+	char known_flag_found = 0;
+
+	if (flags_size == 0) {
+		return FLAGS_EMPTY;
+	}
+
+	/* Take care of the most common (and easy) case, one character */
+	if (flags_size == 1) {
+		if (*flags == 's' || *flags == 'S' ||
+				*flags == 'a' || *flags == 'A' ||
+				*flags == 'u' || *flags == 'U') {
+			return FLAGS_TERMINAL;
+		} else if (!isalnum(*flags)) {
+			return FLAGS_INVALID;
+		} else {
+			return FLAGS_UNKNOWN;
+		}
+	}
+
+	/*
+	 * Multiple flags are allowed, but you cannot mix the
+	 * S, A, U, and P flags together.
+	 */
+	for (i = 0; i < flags_size; ++i) {
+		if (!isalnum(flags[i])) {
+			return FLAGS_INVALID;
+		} else if (flags[i] == 's' || flags[i] == 'S') {
+			if (known_flag_found && known_flag_found != 's') {
+				return FLAGS_INVALID;
+			}
+			known_flag_found = 's';
+		} else if (flags[i] == 'u' || flags[i] == 'U') {
+			if (known_flag_found && known_flag_found != 'u') {
+				return FLAGS_INVALID;
+			}
+			known_flag_found = 'u';
+		} else if (flags[i] == 'a' || flags[i] == 'A') {
+			if (known_flag_found && known_flag_found != 'a') {
+				return FLAGS_INVALID;
+			}
+			known_flag_found = 'a';
+		} else if (flags[i] == 'p' || flags[i] == 'P') {
+			if (known_flag_found && known_flag_found != 'p') {
+				return FLAGS_INVALID;
+			}
+			known_flag_found = 'p';
+		}
+	}
+
+	return (!known_flag_found || known_flag_found == 'p') ? FLAGS_UNKNOWN : FLAGS_TERMINAL;
+}
+
+/*!
+ * \brief Analyze NAPTR services for validity as defined by RFC 3404
+ *
+ * \note The services string passed to this function is NOT NULL-terminated
+ * \param services The services string parsed from a NAPTR record
+ * \param services_size The size of the services string
+ * \retval 0 Services are valid
+ * \retval -1 Services are invalid
+ */
+static int services_invalid(const char *services, uint8_t services_size)
+{
+	const char *current_pos = services;
+	const char *end_of_services = services + services_size;
+
+	if (services_size == 0) {
+		return 0;
+	}
+
+	/* Services are broken into sections divided by a + sign. Each section
+	 * must start with an alphabetic character, and then can only contain
+	 * alphanumeric characters. The size of any section is limited to
+	 * 32 characters
+	 */
+	while (1) {
+		char *plus_pos = memchr(current_pos, '+', end_of_services - current_pos);
+		uint8_t current_size = plus_pos ? plus_pos - current_pos : end_of_services - current_pos;
+		int i;
+
+		if (!isalpha(current_pos[0])) {
+			return -1;
+		}
+
+		if (current_size > 32) {
+			return -1;
+		}
+
+		for (i = 1; i < current_size; ++i) {
+			if (!isalnum(current_pos[i])) {
+				return -1;
+			}
+		}
+
+		if (!plus_pos) {
+			break;
+		}
+		current_pos = plus_pos + 1;
+	}
+
+	return 0;
+}
+
+/*!
+ * \brief Determine if flags in the regexp are invalid
+ *
+ * A NAPTR regexp is structured like so
+ * /pattern/repl/FLAGS
+ *
+ * This ensures that the flags on the regexp are valid. Regexp
+ * flags can either be zero or one character long. If the flags
+ * are one character long, that character must be "i" to indicate
+ * the regex evaluation is case-insensitive.
+ *
+ * \note The flags string passed to this function is not NULL-terminated
+ * \param flags The regexp flags from the NAPTR record
+ * \param end A pointer to the end of the flags string
+ * \retval 0 Flags are valid
+ * \retval -1 Flags are invalid
+ */
+static int regexp_flags_invalid(const char *flags, const char *end)
+{
+	if (flags >= end) {
+		return 0;
+	}
+
+	if (end - flags > 1) {
+		return -1;
+	}
+
+	if (*flags != 'i') {
+		return -1;
+	}
+
+	return 0;
+}
+
+/*!
+ * \brief Determine if the replacement in the regexp is invalid
+ *
+ * A NAPTR regexp is structured like so
+ * /pattern/REPL/flags
+ *
+ * This ensures that the replacement on the regexp is valid. The regexp
+ * replacement is free to use any character it wants, plus backreferences
+ * and an escaped regexp delimiter.
+ *
+ * This function does not attempt to ensure that the backreferences refer
+ * to valid portions of the regexp's regex pattern.
+ *
+ * \note The repl string passed to this function is NOT NULL-terminated
+ *
+ * \param repl The regexp replacement string
+ * \param end Pointer to the end of the replacement string
+ * \param delim The delimiter character for the regexp
+ *
+ * \retval 0 Replacement is valid
+ * \retval -1 Replacement is invalid
+ */
+static int regexp_repl_invalid(const char *repl, const char *end, char delim)
+{
+	const char *ptr = repl;
+
+	if (repl == end) {
+		/* Kind of weird, but this is fine */
+		return 0;
+	}
+
+	while (1) {
+		char *backslash_pos = memchr(ptr, '\\', end - ptr);
+		if (!backslash_pos) {
+			break;
+		}
+
+		ast_assert(backslash_pos < end - 1);
+
+		/* XXX RFC 3402 is unclear about whether other backslash-escaped characters
+		 * (such as a backslash-escaped backslash) are legal
+		 */
+		if (!strchr("12345689", backslash_pos[1]) && backslash_pos[1] != delim) {
+			return -1;
+		}
+
+		ptr = backslash_pos + 1;
+	}
+
+	return 0;
+}
+
+/*!
+ * \brief Determine if the pattern in a regexp is invalid
+ *
+ * A NAPTR regexp is structured like so
+ * /PATTERN/repl/flags
+ *
+ * This ensures that the pattern on the regexp is valid. The pattern is
+ * passed to a regex compiler to determine its validity.
+ *
+ * \note The pattern string passed to this function is NOT NULL-terminated
+ *
+ * \param pattern The pattern from the NAPTR record
+ * \param end A pointer to the end of the pattern
+ *
+ * \retval 0 Pattern is valid
+ * \retval non-zero Pattern is invalid
+ */
+static int regexp_pattern_invalid(const char *pattern, const char *end)
+{
+	int pattern_size = end - pattern;
+	char pattern_str[pattern_size + 1];
+	regex_t reg;
+	int res;
+
+	/* regcomp requires a NULL-terminated string */
+	memcpy(pattern_str, pattern, pattern_size);
+	pattern_str[pattern_size] = '\0';
+
+	res = regcomp(&reg, pattern_str, REG_EXTENDED);
+
+	regfree(&reg);
+
+	return res;
+}
+
+/*!
+ * \brief Determine if the regexp in a NAPTR record is invalid
+ *
+ * The goal of this function is to divide the regexp into its
+ * constituent parts and then let validation subroutines determine
+ * if each part is valid. If all parts are valid, then the entire
+ * regexp is valid.
+ *
+ * \note The regexp string passed to this function is NOT NULL-terminated
+ *
+ * \param regexp The regexp from the NAPTR record
+ * \param regexp_size The size of the regexp string
+ *
+ * \retval 0 regexp is valid
+ * \retval non-zero regexp is invalid
+ */
+static int regexp_invalid(const char *regexp, uint8_t regexp_size)
+{
+	char delim;
+	const char *delim2_pos;
+	const char *delim3_pos;
+	const char *ptr = regexp;
+	const char *end_of_regexp = regexp + regexp_size;
+	const char *regex_pos;
+	const char *repl_pos;
+	const char *flags_pos;
+
+	if (regexp_size == 0) {
+		return 0;
+	}
+
+	/* The delimiter will be a ! or / in most cases, but the rules allow
+	 * for the delimiter to be nearly any character. It cannot be 'i' because
+	 * the delimiter cannot be the same as regexp flags. The delimiter cannot
+	 * be 1-9 because the delimiter cannot be a backreference number. RFC
+	 * 2915 specified that backslash was also not allowed as a delimiter, but
+	 * RFC 3402 does not say this. We've gone ahead and made the character
+	 * illegal for our purposes.
+	 */
+	delim = *ptr;
+	if (strchr("123456789\\i", delim)) {
+		return -1;
+	}
+	++ptr;
+	regex_pos = ptr;
+
+	/* Find the other two delimiters. If the delim is escaped with a backslash, it doesn't count */
+	while (1) {
+		delim2_pos = memchr(ptr, delim, end_of_regexp - ptr);
+		if (!delim2_pos) {
+			return -1;
+		}
+		ptr = delim2_pos + 1;
+		if (delim2_pos[-1] != '\\') {
+			break;
+		}
+	}
+
+	if (ptr >= end_of_regexp) {
+		return -1;
+	}
+
+	repl_pos = ptr;
+
+	while (1) {
+		delim3_pos = memchr(ptr, delim, end_of_regexp - ptr);
+		if (!delim3_pos) {
+			return -1;
+		}
+		ptr = delim3_pos + 1;
+		if (delim3_pos[-1] != '\\') {
+			break;
+		}
+	}
+	flags_pos = ptr;
+
+	if (regexp_flags_invalid(flags_pos, end_of_regexp) ||
+			regexp_repl_invalid(repl_pos, delim3_pos, delim) ||
+			regexp_pattern_invalid(regex_pos, delim2_pos)) {
+		return -1;
+	}
+
+	return 0;
+}
+
+#define PAST_END_OF_RECORD ptr >= end_of_record
+
+struct ast_dns_record *dns_naptr_alloc(struct ast_dns_query *query, const char *data, const size_t size)
+{
+	struct ast_dns_naptr_record *naptr;
+	char *ptr = NULL;
+	uint16_t order;
+	uint16_t preference;
+	uint8_t flags_size;
+	char *flags;
+	uint8_t services_size;
+	char *services;
+	uint8_t regexp_size;
+	char *regexp;
+	char replacement[256] = "";
+	int replacement_size;
+	char *naptr_offset;
+	char *naptr_search_base = (char *)query->result->answer;
+	size_t remaining_size = query->result->answer_size;
+	char *end_of_record;
+	enum flags_result flags_res;
+
+	/*
+	 * This is bordering on the hackiest thing I've ever written.
+	 * Part of parsing a NAPTR record is to parse a potential replacement
+	 * domain name. Decoding this domain name requires the use of the
+	 * dn_expand() function. This function requires that the domain you
+	 * pass in be a pointer to within the full DNS answer. Unfortunately,
+	 * libunbound gives its RRs back as copies of data from the DNS answer
+	 * instead of pointers to within the DNS answer. This means that in order
+	 * to be able to parse the domain name correctly, I need to find the
+	 * current NAPTR record inside the DNS answer and operate on it. This
+	 * loop is designed to find the current NAPTR record within the full
+	 * DNS answer and set the "ptr" variable to the beginning of the
+	 * NAPTR RDATA
+	 */
+	while (1) {
+		naptr_offset = memchr(naptr_search_base, data[0], remaining_size);
+
+		/* Since the NAPTR record we have been given came from the DNS answer,
+		 * we should never run into a situation where we can't find ourself
+		 * in the answer
+		 */
+		ast_assert(naptr_offset != NULL);
+		ast_assert(naptr_search_base + remaining_size - naptr_offset >= size);
+
+		/* ... but just to be on the safe side, let's be sure we can break
+		 * out if the assertion doesn't hold
+		 */
+		if (!naptr_offset || naptr_search_base + remaining_size - naptr_offset < size) {
+			ast_log(LOG_ERROR, "Failed to locate NAPTR record within DNS result\n");
+			return NULL;
+		}
+
+		if (!memcmp(naptr_offset, data, size)) {
+			/* BAM! FOUND IT! */
+			ptr = naptr_offset;
+			break;
+		}
+		/* Data didn't match us, so keep looking */
+		remaining_size -= naptr_offset - naptr_search_base;
+		naptr_search_base = naptr_offset + 1;
+	}
+
+	ast_assert(ptr != NULL);
+
+	end_of_record = ptr + size;
+
+	/* ORDER */
+	/* This assignment takes a big-endian 16-bit value and stores it in the
+	 * machine's native byte order. Using this method allows us to avoid potential
+	 * alignment issues in case the order is not on a short-addressable boundary.
+	 * See http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html for
+	 * more information
+	 */
+	order = ((unsigned char)(ptr[1]) << 0) | ((unsigned char)(ptr[0]) << 8);
+	ptr += 2;
+
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+
+	/* PREFERENCE */
+	preference = ((unsigned char) (ptr[1]) << 0) | ((unsigned char)(ptr[0]) << 8);
+	ptr += 2;
+
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+
+	/* FLAGS */
+	flags_size = *ptr;
+	++ptr;
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+	flags = ptr;
+	ptr += flags_size;
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+
+	/* SERVICES */
+	services_size = *ptr;
+	++ptr;
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+	services = ptr;
+	ptr += services_size;
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+
+	/* REGEXP */
+	regexp_size = *ptr;
+	++ptr;
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+	regexp = ptr;
+	ptr += regexp_size;
+	if (PAST_END_OF_RECORD) {
+		return NULL;
+	}
+
+	replacement_size = dn_expand((unsigned char *)query->result->answer, (unsigned char *) end_of_record, (unsigned char *) ptr, replacement, sizeof(replacement) - 1);
+	if (replacement_size < 0) {
+		ast_log(LOG_ERROR, "Failed to expand domain name: %s\n", strerror(errno));
+		return NULL;
+	}
+
+	ptr += replacement_size;
+
+	if (ptr != end_of_record) {
+		ast_log(LOG_ERROR, "NAPTR record gave undersized string indications.\n");
+		return NULL;
+	}
+
+	/* We've validated the size of the NAPTR record. Now we can validate
+	 * the individual parts
+	 */
+	flags_res = interpret_flags(flags, flags_size);
+	if (flags_res == FLAGS_INVALID) {
+		ast_log(LOG_ERROR, "NAPTR Record contained invalid flags %.*s\n", flags_size, flags);
+		return NULL;
+	}
+
+	if (services_invalid(services, services_size)) {
+		ast_log(LOG_ERROR, "NAPTR record contained invalid services %.*s\n", services_size, services);
+		return NULL;
+	}
+
+	if (regexp_invalid(regexp, regexp_size)) {
+		ast_log(LOG_ERROR, "NAPTR record contained invalid regexp %.*s\n", regexp_size, regexp);
+		return NULL;
+	}
+
+	/* replacement_size takes into account the NULL label, so a NAPTR record with no replacement
+	 * will have a replacement_size of 1.
+	 */
+	if (regexp_size && replacement_size > 1) {
+		ast_log(LOG_ERROR, "NAPTR record contained both a regexp and replacement\n");
+		return NULL;
+	}
+
+	naptr = ast_calloc(1, sizeof(*naptr) + size + flags_size + 1 + services_size + 1 + regexp_size + 1 + replacement_size + 1);
+	if (!naptr) {
+		return NULL;
+	}
+
+	naptr->order = order;
+	naptr->preference = preference;
+
+	ptr = naptr->data;
+	ptr += size;
+
+	strncpy(ptr, flags, flags_size);
+	ptr[flags_size] = '\0';
+	naptr->flags = ptr;
+	ptr += flags_size + 1;
+
+	strncpy(ptr, services, services_size);
+	ptr[services_size] = '\0';
+	naptr->service = ptr;
+	ptr += services_size + 1;
+
+	strncpy(ptr, regexp, regexp_size);
+	ptr[regexp_size] = '\0';
+	naptr->regexp = ptr;
+	ptr += regexp_size + 1;
+
+	strcpy(ptr, replacement);
+	naptr->replacement = ptr;
+
+	naptr->generic.data_ptr = naptr->data;
+
+	return (struct ast_dns_record *)naptr;
+}
+
+
+static int compare_order(const void *record1, const void *record2)
+{
+	const struct ast_dns_naptr_record **left = (const struct ast_dns_naptr_record **)record1;
+	const struct ast_dns_naptr_record **right = (const struct ast_dns_naptr_record **)record2;
+
+	if ((*left)->order < (*right)->order) {
+		return -1;
+	} else if ((*left)->order > (*right)->order) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+static int compare_preference(const void *record1, const void *record2)
+{
+	const struct ast_dns_naptr_record **left = (const struct ast_dns_naptr_record **)record1;
+	const struct ast_dns_naptr_record **right = (const struct ast_dns_naptr_record **)record2;
+
+	if ((*left)->preference < (*right)->preference) {
+		return -1;
+	} else if ((*left)->preference > (*right)->preference) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+void dns_naptr_sort(struct ast_dns_result *result)
+{
+	struct ast_dns_record *current;
+	size_t num_records = 0;
+	struct ast_dns_naptr_record **records;
+	int i = 0;
+	int j = 0;
+	int cur_order;
+
+	/* Determine the number of records */
+	AST_LIST_TRAVERSE(&result->records, current, list) {
+		++num_records;
+	}
+
+	/* No point in continuing if there are no records */
+	if (num_records == 0) {
+		return;
+	}
+
+	/* Allocate an array with that number of records */
+	records = ast_alloca(num_records * sizeof(*records));
+
+	/* Move records from the list to the array */
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&result->records, current, list) {
+		records[i++] = (struct ast_dns_naptr_record *) current;
+		AST_LIST_REMOVE_CURRENT(list);
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+
+	/* Sort the array by order */
+	qsort(records, num_records, sizeof(*records), compare_order);
+
+	/* Sort subarrays by preference */
+	for (i = 0; i < num_records; i = j) {
+		cur_order = records[i]->order;
+		for (j = i + 1; j < num_records; ++j) {
+			if (records[j]->order != cur_order) {
+				break;
+			}
+		}
+		qsort(&records[i], j - i, sizeof(*records), compare_preference);
+	}
+
+	/* Place sorted records back into the original list */
+	for (i = 0; i < num_records; ++i) {
+		AST_LIST_INSERT_TAIL(&result->records, (struct ast_dns_record *)(records[i]), list);
+	}
+}
 
 const char *ast_dns_naptr_get_flags(const struct ast_dns_record *record)
 {
-	return NULL;
+	struct ast_dns_naptr_record *naptr = (struct ast_dns_naptr_record *) record;
+
+	ast_assert(ast_dns_record_get_rr_type(record) == ns_t_naptr);
+	return naptr->flags;
 }
 
 const char *ast_dns_naptr_get_service(const struct ast_dns_record *record)
 {
-	return NULL;
+	struct ast_dns_naptr_record *naptr = (struct ast_dns_naptr_record *) record;
+
+	ast_assert(ast_dns_record_get_rr_type(record) == ns_t_naptr);
+	return naptr->service;
 }
 
 const char *ast_dns_naptr_get_regexp(const struct ast_dns_record *record)
 {
-	return NULL;
+	struct ast_dns_naptr_record *naptr = (struct ast_dns_naptr_record *) record;
+
+	ast_assert(ast_dns_record_get_rr_type(record) == ns_t_naptr);
+	return naptr->regexp;
 }
 
 const char *ast_dns_naptr_get_replacement(const struct ast_dns_record *record)
 {
-	return NULL;
+	struct ast_dns_naptr_record *naptr = (struct ast_dns_naptr_record *) record;
+
+	ast_assert(ast_dns_record_get_rr_type(record) == ns_t_naptr);
+	return naptr->replacement;
 }
 
 unsigned short ast_dns_naptr_get_order(const struct ast_dns_record *record)
 {
-	return 0;
+	struct ast_dns_naptr_record *naptr = (struct ast_dns_naptr_record *) record;
+
+	ast_assert(ast_dns_record_get_rr_type(record) == ns_t_naptr);
+	return naptr->order;
 }
 
 unsigned short ast_dns_naptr_get_preference(const struct ast_dns_record *record)
 {
-	return 0;
-}
+	struct ast_dns_naptr_record *naptr = (struct ast_dns_naptr_record *) record;
+
+	ast_assert(ast_dns_record_get_rr_type(record) == ns_t_naptr);
+	return naptr->preference;
+}

Modified: team/group/dns_pjsip/main/dsp.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/main/dsp.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/main/dsp.c (original)
+++ team/group/dns_pjsip/main/dsp.c Tue Apr  7 06:11:00 2015
@@ -112,9 +112,11 @@
 	{ "uk", PROG_MODE_UK },
 };
 
+#define FREQ_ARRAY_SIZE 7
+
 static struct progress {
 	enum gsamp_size size;
-	int freqs[7];
+	int freqs[FREQ_ARRAY_SIZE];
 } modes[] = {
 	{ GSAMP_SIZE_NA, { 350, 440, 480, 620, 950, 1400, 1800 } },	/*!< North America */
 	{ GSAMP_SIZE_CR, { 425 } },					/*!< Costa Rica, Brazil */
@@ -389,7 +391,7 @@
 	struct ast_dsp_busy_pattern busy_cadence;
 	int historicnoise[DSP_HISTORY];
 	int historicsilence[DSP_HISTORY];
-	goertzel_state_t freqs[7];
+	goertzel_state_t freqs[FREQ_ARRAY_SIZE];
 	int freqcount;
 	int gsamps;
 	enum gsamp_size gsamp_size;
@@ -1036,6 +1038,8 @@
 	int pass;
 	int newstate = DSP_TONE_STATE_SILENCE;
 	int res = 0;
+	int freqcount = dsp->freqcount > FREQ_ARRAY_SIZE ? FREQ_ARRAY_SIZE : dsp->freqcount;
+
 	while (len) {
 		/* Take the lesser of the number of samples we need and what we have */
 		pass = len;
@@ -1045,7 +1049,7 @@
 		for (x = 0; x < pass; x++) {
 			samp = s[x];
 			dsp->genergy += (int32_t) samp * (int32_t) samp;
-			for (y = 0; y < dsp->freqcount; y++) {
+			for (y = 0; y < freqcount; y++) {
 				goertzel_sample(&dsp->freqs[y], samp);
 			}
 		}
@@ -1053,8 +1057,8 @@
 		dsp->gsamps += pass;
 		len -= pass;
 		if (dsp->gsamps == dsp->gsamp_size) {
-			float hz[7];
-			for (y = 0; y < 7; y++) {
+			float hz[FREQ_ARRAY_SIZE];
+			for (y = 0; y < FREQ_ARRAY_SIZE; y++) {
 				hz[y] = goertzel_result(&dsp->freqs[y]);
 			}
 			switch (dsp->progmode) {
@@ -1642,7 +1646,7 @@
 
 	dsp->gsamp_size = modes[dsp->progmode].size;
 	dsp->gsamps = 0;
-	for (x = 0; x < ARRAY_LEN(modes[dsp->progmode].freqs); x++) {
+	for (x = 0; x < FREQ_ARRAY_SIZE; x++) {
 		if (modes[dsp->progmode].freqs[x]) {
 			goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->sample_rate);
 			max = x + 1;
@@ -1668,6 +1672,7 @@
 		dsp->digitmode = DSP_DIGITMODE_DTMF;
 		dsp->faxmode = DSP_FAXMODE_DETECT_CNG;
 		dsp->sample_rate = sample_rate;
+		dsp->freqcount = 0;
 		/* Initialize digit detector */
 		ast_digit_detect_init(&dsp->digit_state, dsp->digitmode & DSP_DIGITMODE_MF, dsp->sample_rate);
 		dsp->display_inband_dtmf_warning = 1;

Modified: team/group/dns_pjsip/main/framehook.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/main/framehook.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/main/framehook.c (original)
+++ team/group/dns_pjsip/main/framehook.c Tue Apr  7 06:11:00 2015
@@ -137,7 +137,7 @@
 	struct ast_framehook_list *fh_list;
 	struct ast_frame *frame;
 	if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
-		ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
+		ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%i)\n",
 			i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
 		return -1;
 	}

Modified: team/group/dns_pjsip/main/manager.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/main/manager.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/main/manager.c (original)
+++ team/group/dns_pjsip/main/manager.c Tue Apr  7 06:11:00 2015
@@ -4803,7 +4803,7 @@
 static int check_blacklist(const char *cmd)
 {
 	char *cmd_copy, *cur_cmd;
-	char *cmd_words[MAX_BLACKLIST_CMD_LEN] = { NULL, };
+	char *cmd_words[AST_MAX_CMD_LEN] = { NULL, };
 	int i;
 
 	cmd_copy = ast_strdupa(cmd);

Modified: team/group/dns_pjsip/pbx/pbx_config.c
URL: http://svnview.digium.com/svn/asterisk/team/group/dns_pjsip/pbx/pbx_config.c?view=diff&rev=434211&r1=434210&r2=434211
==============================================================================
--- team/group/dns_pjsip/pbx/pbx_config.c (original)
+++ team/group/dns_pjsip/pbx/pbx_config.c Tue Apr  7 06:11:00 2015
@@ -956,7 +956,9 @@
 					const char *el = ast_get_extension_label(p);
 					char label[128] = "";
 					char *appdata = ast_get_extension_app_data(p);
-					char *escaped;
+
+					int escaped_len = (!ast_strlen_zero(appdata)) ? 2 * strlen(appdata) + 1 : 1;
+					char escaped[escaped_len];
 
 					if (ast_get_extension_matchcid(p)) {
 						sep = "/";
@@ -970,12 +972,9 @@
 					}
 
 					if (!ast_strlen_zero(appdata)) {
-						int escaped_len = 2 * strlen(appdata) + 1;
-						char escaped[escaped_len];
-
 						ast_escape_semicolons(appdata, escaped, escaped_len);
 					} else {
-						escaped = "";
+						escaped[0] = '\0';
 					}
 
 					fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",


[... 706 lines stripped ...]



More information about the asterisk-commits mailing list