Index: channels/chan_iax2.c
===================================================================
--- channels/chan_iax2.c	(revision 131915)
+++ channels/chan_iax2.c	(working copy)
@@ -107,7 +107,6 @@
 static int nochecksums = 0;
 #endif
 
-
 #define PTR_TO_CALLNO(a) ((unsigned short)(unsigned long)(a))
 #define CALLNO_TO_PTR(a) ((void *)(unsigned long)(a))
 
@@ -784,53 +783,6 @@
 	ast_mutex_unlock(lock);
 }
 
-static void iax_debug_output(const char *data)
-{
-	if (iaxdebug)
-		ast_verbose("%s", data);
-}
-
-static void iax_error_output(const char *data)
-{
-	ast_log(LOG_WARNING, "%s", data);
-}
-
-static void __attribute__((format (printf, 1, 2))) jb_error_output(const char *fmt, ...)
-{
-	va_list args;
-	char buf[1024];
-
-	va_start(args, fmt);
-	vsnprintf(buf, sizeof(buf), fmt, args);
-	va_end(args);
-
-	ast_log(LOG_ERROR, "%s", buf);
-}
-
-static void __attribute__((format (printf, 1, 2))) jb_warning_output(const char *fmt, ...)
-{
-	va_list args;
-	char buf[1024];
-
-	va_start(args, fmt);
-	vsnprintf(buf, sizeof(buf), fmt, args);
-	va_end(args);
-
-	ast_log(LOG_WARNING, "%s", buf);
-}
-
-static void __attribute__((format (printf, 1, 2))) jb_debug_output(const char *fmt, ...)
-{
-	va_list args;
-	char buf[1024];
-
-	va_start(args, fmt);
-	vsnprintf(buf, sizeof(buf), fmt, args);
-	va_end(args);
-
-	ast_verbose("%s", buf);
-}
-
 /*!
  * \brief an array of iax2 pvt structures
  *
@@ -886,6 +838,66 @@
    but keeps the division between trunked and non-trunked better. */
 #define TRUNK_CALL_START	ARRAY_LEN(iaxs) / 2
 
+/* Debug routines... */
+static struct sockaddr_in debugaddr;
+
+static void iax_outputframe(struct iax_frame *f, struct ast_iax2_full_hdr *fhi, int rx, struct sockaddr_in *sin, int datalen)
+{
+	if (iaxdebug ||
+	    (sin && debugaddr.sin_addr.s_addr && 
+	     (!ntohs(debugaddr.sin_port) ||
+	      debugaddr.sin_port == sin->sin_port) &&
+	     debugaddr.sin_addr.s_addr == sin->sin_addr.s_addr))
+		iax_showframe(f, fhi, rx, sin, datalen);
+}
+
+static void iax_debug_output(const char *data)
+{
+	if (iaxdebug)
+		ast_verbose("%s", data);
+}
+
+static void iax_error_output(const char *data)
+{
+	ast_log(LOG_WARNING, "%s", data);
+}
+
+static void __attribute__((format (printf, 1, 2))) jb_error_output(const char *fmt, ...)
+{
+	va_list args;
+	char buf[1024];
+
+	va_start(args, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, args);
+	va_end(args);
+
+	ast_log(LOG_ERROR, "%s", buf);
+}
+
+static void __attribute__((format (printf, 1, 2))) jb_warning_output(const char *fmt, ...)
+{
+	va_list args;
+	char buf[1024];
+
+	va_start(args, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, args);
+	va_end(args);
+
+	ast_log(LOG_WARNING, "%s", buf);
+}
+
+static void __attribute__((format (printf, 1, 2))) jb_debug_output(const char *fmt, ...)
+{
+	va_list args;
+	char buf[1024];
+
+	va_start(args, fmt);
+	vsnprintf(buf, sizeof(buf), fmt, args);
+	va_end(args);
+
+	ast_verbose("%s", buf);
+}
+
 static int maxtrunkcall = TRUNK_CALL_START;
 static int maxnontrunkcall = 1;
 
@@ -2213,8 +2225,7 @@
 		addr = &iaxs[callno]->addr;
 	}
 	
-	if (iaxdebug)
-		iax_showframe(f, NULL, 0, addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
+	iax_outputframe(f, NULL, 0, addr, f->datalen - sizeof(struct ast_iax2_full_hdr));
 
 	res = sendto(iaxs[callno]->sockfd, f->data, f->datalen, 0,(struct sockaddr *)addr, 
 		     sizeof(iaxs[callno]->media));
@@ -4790,15 +4801,12 @@
 			pvt->svideoformat = f->subclass & ~0x1;
 		if (ast_test_flag(pvt, IAX_ENCRYPTED)) {
 			if (ast_test_flag(pvt, IAX_KEYPOPULATED)) {
-				if (iaxdebug) {
-					if (fr->transfer)
-						iax_showframe(fr, NULL, 2, &pvt->transfer, fr->datalen - sizeof(struct ast_iax2_full_hdr));
-					else if (fr->media)
-						iax_showframe(fr, NULL, 2, &pvt->media, fr->datalen - sizeof(struct ast_iax2_full_hdr));
-
-					else
-						iax_showframe(fr, NULL, 2, &pvt->addr, fr->datalen - sizeof(struct ast_iax2_full_hdr));
-				}
+				if (fr->transfer)
+					iax_outputframe(fr, NULL, 2, &pvt->transfer, fr->datalen - sizeof(struct ast_iax2_full_hdr));
+				else if (fr->media)
+					iax_outputframe(fr, NULL, 2, &pvt->media, fr->datalen - sizeof(struct ast_iax2_full_hdr));
+				else
+					iax_outputframe(fr, NULL, 2, &pvt->addr, fr->datalen - sizeof(struct ast_iax2_full_hdr));
 				encrypt_frame(&pvt->ecx, fh, pvt->semirand, &fr->datalen);
 			} else
 				ast_log(LOG_WARNING, "Supposed to send packet encrypted, but no key?\n");
@@ -5526,47 +5534,46 @@
 	return CLI_SUCCESS;
 }
 
-static char *handle_cli_iax2_set_debug_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "iax2 set debug [off]";
-		e->usage =
-			"Usage: iax2 set debug [off]\n"
-			"       Enables/Disables dumping of IAX packets for debugging purposes.\n";
-		return NULL;
-	case CLI_GENERATE:
-		return NULL;
-	}
-	if (a->argc < 3 || a->argc > 4)
-		return CLI_SHOWUSAGE;
-	if (a->argc == 3) {
-		iaxdebug = 1;
-		ast_cli(a->fd, "IAX2 Debugging Enabled\n");
-	} else {
-		iaxdebug = 0;
-		ast_cli(a->fd, "IAX2 Debugging Disabled\n");
-	}
-	return CLI_SUCCESS;
-}
 
+
 static char *handle_cli_iax2_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	switch (cmd) {
 	case CLI_INIT:
-		e->command = "iax2 set debug {on|off}";
+		e->command = "iax2 set debug {on|off|peer} [peername]";
 		e->usage =
-			"Usage: iax2 set debug {on|off}\n"
+			"Usage: iax2 set debug {on|off|peer} [peername]\n"
 			"       Enables/Disables dumping of IAX packets for debugging purposes.\n";
 		return NULL;
 	case CLI_GENERATE:
 		return NULL;
 	}
 
-	if (a->argc != e->args)
+	if (a->argc < e->args - 1  || 
+		a->argc > e->args)
 		return CLI_SHOWUSAGE;
 
-	if (!strncasecmp(a->argv[e->args -1], "on", 2)) {
+	if (!strcasecmp(a->argv[e->args-2], "peer")) {
+		struct iax2_peer *peer;
+
+		if (a->argc != e->args)
+			return CLI_SHOWUSAGE;
+
+		peer = find_peer(a->argv[e->args-1], 1);
+
+		if (!peer) {
+			ast_cli(a->fd, "IAX2 peer '%s' does not exist", a->argv[e->args-1]);
+			return CLI_FAILURE;
+		}
+
+		debugaddr.sin_addr = peer->addr.sin_addr;
+		debugaddr.sin_port = peer->addr.sin_port;
+
+		ast_cli(a->fd, "SIP Debugging Enabled for IP: %s:%d\n",
+			ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
+
+		ao2_ref(peer, -1);
+	} else if (!strncasecmp(a->argv[e->args-2], "on", 2)) {
 		iaxdebug = 1;
 		ast_cli(a->fd, "IAX2 Debugging Enabled\n");
 	} else {
@@ -5576,33 +5583,6 @@
 	return CLI_SUCCESS;
 }
 
-
-static char *handle_cli_iax2_set_debug_trunk_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "iax2 set debug trunk [off]";
-		e->usage =
-			"Usage: iax2 set debug trunk [off]\n"
-			"       Enables/Disables debugging of IAX trunking\n";
-		return NULL;
-	case CLI_GENERATE:
-		return NULL;
-	}
-
-	if (a->argc < 4 || a->argc > 5)
-		return CLI_SHOWUSAGE;
-
-	if (a->argc == 4) {
-		iaxtrunkdebug = 1;
-		ast_cli(a->fd, "IAX2 Trunk Debugging Enabled\n");
-	} else {
-		iaxtrunkdebug = 0;
-		ast_cli(a->fd, "IAX2 Trunk Debugging Disabled\n");
-	}
-	return CLI_SUCCESS;
-}
-
 static char *handle_cli_iax2_set_debug_trunk(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	switch (cmd) {
@@ -5629,32 +5609,6 @@
 	return CLI_SUCCESS;
 }
 
-static char *handle_cli_iax2_set_debug_jb_deprecated(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
-{
-	switch (cmd) {
-	case CLI_INIT:
-		e->command = "iax2 set debug jb [off]";
-		e->usage =
-			"Usage: iax2 set debug jb [off]\n"
-			"       Enables/Disables jitterbuffer debugging information\n";
-		return NULL;
-	case CLI_GENERATE:
-		return NULL;
-	}
-
-	if (a->argc < 4 || a->argc > 5)
-		return CLI_SHOWUSAGE;
-	
-	if (a->argc == 4) {
-		jb_setoutput(jb_error_output, jb_warning_output, jb_debug_output);
-		ast_cli(a->fd, "IAX2 Jitterbuffer Debugging Enabled\n");
-	} else {
-		jb_setoutput(jb_error_output, jb_warning_output, NULL);
-		ast_cli(a->fd, "IAX2 Jitterbuffer Debugging Disabled\n");
-	}
-	return CLI_SUCCESS;
-}
-
 static char *handle_cli_iax2_set_debug_jb(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	switch (cmd) {
@@ -6001,8 +5955,7 @@
 	fh.iseqno = 0;
 	fh.type = AST_FRAME_IAX;
 	fh.csub = compress_subclass(IAX_COMMAND_INVAL);
-	if (iaxdebug)
-		 iax_showframe(NULL, &fh, 0, sin, 0);
+	iax_outputframe(NULL, &fh, 0, sin, 0);
 #if 0
 	if (option_debug)
 #endif	
@@ -8017,8 +7970,8 @@
 		return socket_process_meta(res, meta, &sin, fd, fr);
 
 #ifdef DEBUG_SUPPORT
-	if (iaxdebug && (res >= sizeof(*fh)))
-		iax_showframe(NULL, fh, 1, &sin, res - sizeof(*fh));
+	if (res >= sizeof(*fh))
+		iax_outputframe(NULL, fh, 1, &sin, res - sizeof(*fh));
 #endif
 	if (ntohs(mh->callno) & IAX_FLAG_FULL) {
 		if (res < sizeof(*fh)) {
@@ -8094,8 +8047,8 @@
 			return 1;
 		}
 #ifdef DEBUG_SUPPORT
-		else if (iaxdebug)
-			iax_showframe(NULL, fh, 3, &sin, res - sizeof(*fh));
+		else
+			iax_outputframe(NULL, fh, 3, &sin, res - sizeof(*fh));
 #endif
 	}
 
@@ -11301,7 +11254,8 @@
 		prune_peers();
 		prune_users();
 		trunk_timed = trunk_untimed = 0; 
-		trunk_nmaxmtu = trunk_maxmtu = 0; 
+		trunk_nmaxmtu = trunk_maxmtu = 0;
+		memset(&debugaddr, '\0', sizeof(debugaddr));
 
 		AST_LIST_LOCK(&registrations);
 		AST_LIST_TRAVERSE(&registrations, reg, entry)
@@ -11963,18 +11917,14 @@
 #endif
 */
 
-static struct ast_cli_entry cli_iax2_set_debug_deprecated = AST_CLI_DEFINE(handle_cli_iax2_set_debug_deprecated, "Enable/Disable IAX debugging");
-static struct ast_cli_entry cli_iax2_set_debug_trunk_deprecated = AST_CLI_DEFINE(handle_cli_iax2_set_debug_trunk_deprecated, "Enable/Disable IAX debugging");
-static struct ast_cli_entry cli_iax2_set_debug_jb_deprecated = AST_CLI_DEFINE(handle_cli_iax2_set_debug_jb_deprecated, "Enable/Disable IAX debugging");
-
 static struct ast_cli_entry cli_iax2[] = {
 	AST_CLI_DEFINE(handle_cli_iax2_provision,           "Provision an IAX device"),
 	AST_CLI_DEFINE(handle_cli_iax2_prune_realtime,      "Prune a cached realtime lookup"),
 	AST_CLI_DEFINE(handle_cli_iax2_reload,              "Reload IAX configuration"),
 	AST_CLI_DEFINE(handle_cli_iax2_set_mtu,             "Set the IAX systemwide trunking MTU"),
-	AST_CLI_DEFINE(handle_cli_iax2_set_debug,           "Enable/Disable IAX debugging", .deprecate_cmd = &cli_iax2_set_debug_deprecated),
-	AST_CLI_DEFINE(handle_cli_iax2_set_debug_trunk,     "Enable/Disable IAX trunk debugging", .deprecate_cmd = &cli_iax2_set_debug_trunk_deprecated),
-	AST_CLI_DEFINE(handle_cli_iax2_set_debug_jb,        "Enable/Disable IAX jitterbuffer debugging", .deprecate_cmd = &cli_iax2_set_debug_jb_deprecated),
+	AST_CLI_DEFINE(handle_cli_iax2_set_debug,           "Enable/Disable IAX debugging"),
+	AST_CLI_DEFINE(handle_cli_iax2_set_debug_trunk,     "Enable/Disable IAX trunk debugging"),
+	AST_CLI_DEFINE(handle_cli_iax2_set_debug_jb,        "Enable/Disable IAX jitterbuffer debugging"),
 	AST_CLI_DEFINE(handle_cli_iax2_show_cache,          "Display IAX cached dialplan"),
 	AST_CLI_DEFINE(handle_cli_iax2_show_channels,       "List active IAX channels"),
 	AST_CLI_DEFINE(handle_cli_iax2_show_firmware,       "List available IAX firmware"),
