From 1a5cdeb138cdb1dc91d0b7f69a1e415cdeef4ad1 Mon Sep 17 00:00:00 2001
From: Steve Murphy <murf@digium.com>
Date: Sat, 17 Nov 2007 17:46:36 +0000
Subject: [PATCH] a quick fix to pbx_dundi.c to make it so it will compile.
 Hope I did the right thing. And some additions to removal of extens to take
 care of hashtab pointers in all cases.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@89388 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/pbx.c      | 30 ++++++++++++++++++++++++++----
 pbx/pbx_dundi.c |  1 +
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/main/pbx.c b/main/pbx.c
index a05a654491..2129898545 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -3630,9 +3630,23 @@ int ast_context_remove_extension2(struct ast_context *con, const char *extension
 				ex.priority = priority;
 				exten2 = ast_hashtab_lookup(exten->peer_tree, &ex);
 				if (exten2) {
+					if (exten2->label) /* if this exten has a label, remove that, too */
+						ast_hashtab_remove_this_object(exten->peer_label_tree,exten2);
 					ast_hashtab_remove_this_object(exten->peer_tree, exten2);
 					ast_log(LOG_NOTICE,"Removed priority %d from extension %s context %s table\n",
 							priority, exten->exten, con->name);
+					if (ast_hashtab_size(exten->peer_tree) == 0) {
+						/* well, if the last priority of an exten is to be removed,
+						   then, the extension is removed, too! */
+						struct match_char *x = add_exten_to_pattern_tree(con, exten);
+						if (x->exten) { /* this test for safety purposes */
+							x->deleted = 1; /* with this marked as deleted, it will never show up in the scoreboard, and therefore never be found */
+							x->exten = 0; /* get rid of what will become a bad pointer */
+							ast_hashtab_remove_this_object(con->root_tree, exten);
+							ast_log(LOG_NOTICE,"Removed extension %s from context %s table\n",
+									exten->exten, con->name);
+						}
+					}
 				} else {
 					ast_log(LOG_ERROR,"Could not find priority %d of exten %s in context %s!\n",
 							priority, exten->exten, con->name);
@@ -3691,13 +3705,21 @@ int ast_context_remove_extension2(struct ast_context *con, const char *extension
 			 * The next node is either the next priority or the next extension
 			 */
 			struct ast_exten *next_node = peer->peer ? peer->peer : peer->next;
-
-			if (!prev_exten)	/* change the root... */
+			if (next_node == peer->peer) {
+				next_node->peer_tree = exten->peer_tree; /* move the priority hash tabs over */
+				exten->peer_tree = 0;
+				next_node->peer_tree = exten->peer_label_tree;
+				exten->peer_label_tree = 0;
+			}
+			if (!prev_exten) {	/* change the root... */
 				con->root = next_node;
-			else
+			} else {
 				prev_exten->next = next_node; /* unlink */
-			if (peer->peer)	/* XXX update the new head of the pri list */
+			}
+			if (peer->peer)	{ /* XXX update the new head of the pri list */
 				peer->peer->next = peer->next;
+			}
+			
 		} else { /* easy, we are not first priority in extension */
 			previous_peer->peer = peer->peer;
 		}
diff --git a/pbx/pbx_dundi.c b/pbx/pbx_dundi.c
index bcbce6e4ea..fb673c78fd 100644
--- a/pbx/pbx_dundi.c
+++ b/pbx/pbx_dundi.c
@@ -36,6 +36,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include <zlib.h>
 #include <sys/signal.h>
 #include <pthread.h>
+#include <net/if.h>
 
 #include "asterisk/file.h"
 #include "asterisk/logger.h"
-- 
GitLab