[asterisk-commits] rizzo: branch rizzo/astobj2 r47831 - /team/rizzo/astobj2/channels/chan_sip.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Nov 18 13:53:46 MST 2006


Author: rizzo
Date: Sat Nov 18 14:53:45 2006
New Revision: 47831

URL: http://svn.digium.com/view/asterisk?view=rev&rev=47831
Log:
merge (and fix) the various "sip set debug ..." CLI functions



Modified:
    team/rizzo/astobj2/channels/chan_sip.c

Modified: team/rizzo/astobj2/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/channels/chan_sip.c?view=diff&rev=47831&r1=47830&r2=47831
==============================================================================
--- team/rizzo/astobj2/channels/chan_sip.c (original)
+++ team/rizzo/astobj2/channels/chan_sip.c Sat Nov 18 14:53:45 2006
@@ -1461,7 +1461,6 @@
 static char *complete_sipch(const char *line, const char *word, int pos, int state);
 static char *complete_sip_peer(const char *word, int state, int flags2);
 static char *complete_sip_show_peer(const char *line, const char *word, int pos, int state);
-static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state);
 static char *complete_sip_user(const char *word, int state, int flags2);
 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
@@ -1469,10 +1468,6 @@
 static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state);
 static int sip_show_channel(int fd, int argc, char *argv[]);
 static int sip_show_history(int fd, int argc, char *argv[]);
-static int sip_do_debug_ip(int fd, int argc, char *argv[]);
-static int sip_do_debug_peer(int fd, int argc, char *argv[]);
-static int sip_do_debug(int fd, int argc, char *argv[]);
-static int sip_no_debug(int fd, int argc, char *argv[]);
 static int sip_notify(int fd, int argc, char *argv[]);
 static int sip_do_history(int fd, int argc, char *argv[]);
 static int sip_no_history(int fd, int argc, char *argv[]);
@@ -10890,15 +10885,6 @@
 	return NULL;
 }
 
-/*! \brief Support routine for 'sip debug peer' CLI */
-static char *complete_sip_debug_peer(const char *line, const char *word, int pos, int state)
-{
-	if (pos == 3)
-		return complete_sip_peer(word, state, 0);
-
-	return NULL;
-}
-
 /*! \brief Do completion on user name */
 static char *complete_sip_user(const char *word, int state, int flags2)
 {
@@ -11259,24 +11245,21 @@
 	return;
 }
 
-/*! \brief Enable SIP Debugging in CLI */
-static int sip_do_debug_ip(int fd, int argc, char *argv[])
+/*! \brief enable debugging for a single IP */
+static char *sip_do_debug_ip(int fd, char *arg)
 {
 	struct hostent *hp;
 	struct ast_hostent ahp;
 	int port = 0;
-	char *p, *arg;
-
-	/* sip set debug ip <ip> */
-	if (argc != 5)
-		return RESULT_SHOWUSAGE;
-	p = arg = argv[4];
+	char *p;
+
+	p = arg;
 	strsep(&p, ":");
 	if (p)
 		port = atoi(p);
 	hp = ast_gethostbyname(arg, &ahp);
 	if (hp == NULL)
-		return RESULT_SHOWUSAGE;
+		return CLI_SHOWUSAGE;
 
 	debugaddr.sin_family = AF_INET;
 	memcpy(&debugaddr.sin_addr, hp->h_addr, sizeof(debugaddr.sin_addr));
@@ -11288,49 +11271,69 @@
 
 	ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
 
-	return RESULT_SUCCESS;
-}
-
-/*! \brief  sip_do_debug_peer: Turn on SIP debugging with peer mask */
-static int sip_do_debug_peer(int fd, int argc, char *argv[])
-{
-	struct sip_peer *peer;
-	if (argc != 5)
-		return RESULT_SHOWUSAGE;
-	peer = find_peer(argv[4], NULL, 1);
-	if (peer) {
-		if (peer->addr.sin_addr.s_addr) {
-			debugaddr.sin_family = AF_INET;
-			debugaddr.sin_addr = peer->addr.sin_addr;
-			debugaddr.sin_port = peer->addr.sin_port;
-			ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
+	return CLI_SUCCESS;
+}
+
+/*! \brief  sip_do_debug_peer: Turn on SIP debugging for a given peer */
+static char *sip_do_debug_peer(int fd, char *arg)
+{
+	struct sip_peer *peer = find_peer(arg, NULL, 1);
+	if (!peer)
+		ast_cli(fd, "No such peer '%s'\n", arg);
+	else if (peer->addr.sin_addr.s_addr == 0)
+		ast_cli(fd, "Unable to get IP address of peer '%s'\n", arg);
+	else {
+		debugaddr.sin_family = AF_INET;
+		debugaddr.sin_addr = peer->addr.sin_addr;
+		debugaddr.sin_port = peer->addr.sin_port;
+		ast_cli(fd, "SIP Debugging Enabled for IP: %s:%d\n",
+			ast_inet_ntoa(debugaddr.sin_addr), ntohs(debugaddr.sin_port));
+		ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
+	}
+	if (peer)
+		unref_peer(peer);
+	return CLI_SUCCESS;
+}
+
+/*! \brief Turn on SIP debugging (CLI command) */
+static char *sip_do_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	int oldsipdebug = sipdebug_console;
+	char *what;
+
+	if (cmd == CLI_INIT) {
+		e->command = "sip set debug {on|off|ip|peer}";
+		e->usage =
+			"Usage: sip set debug {off|on|ip addr[:port]|peer peername}\n"
+			"       Globally disables dumping of SIP packets,\n"
+			"       or enables it either globally or for a (single)\n"
+			"       IP address or registered peer.\n";
+		return NULL;
+	} else if (cmd == CLI_GENERATE) {
+		if (a->pos == 4) /* XXX should check on argv too */
+			return complete_sip_peer(a->word, a->n, 0);
+		return NULL;
+	}
+
+	what = a->argv[e->args-1];	/* guaranteed to exist */
+	if (a->argc == e->args) {	/* on/off */
+		if (!strcasecmp(what, "on")) {
 			ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
-		} else
-			ast_cli(fd, "Unable to get IP address of peer '%s'\n", argv[3]);
-		unref_peer(peer);
-	} else
-		ast_cli(fd, "No such peer '%s'\n", argv[4]);
-	return RESULT_SUCCESS;
-}
-
-/*! \brief Turn on SIP debugging (CLI command) */
-static int sip_do_debug(int fd, int argc, char *argv[])
-{
-	int oldsipdebug = sipdebug_console;
-	if (argc != 4) {
-		if (argc != 5) 
-			return RESULT_SHOWUSAGE;
-		else if (strcmp(argv[3], "ip") == 0)
-			return sip_do_debug_ip(fd, argc, argv);
-		else if (strcmp(argv[3], "peer") == 0)
-			return sip_do_debug_peer(fd, argc, argv);
-		else
-			return RESULT_SHOWUSAGE;
-	}
-	ast_set_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
-	memset(&debugaddr, 0, sizeof(debugaddr));
-	ast_cli(fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
-	return RESULT_SUCCESS;
+			memset(&debugaddr, 0, sizeof(debugaddr));
+			ast_cli(a->fd, "SIP Debugging %senabled\n", oldsipdebug ? "re-" : "");
+			return CLI_SUCCESS;
+		} else if (!strcasecmp(what, "off")) {
+			ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
+			ast_cli(a->fd, "SIP Debugging Disabled\n");
+			return CLI_SUCCESS;
+		}
+	} else if (a->argc == e->args +1) {/* ip/peer */
+		if (!strcasecmp(what, "ip"))
+			return sip_do_debug_ip(a->fd, a->argv[e->args]);
+		else if (!strcasecmp(what, "peer"))
+			return sip_do_debug_peer(a->fd, a->argv[e->args]);
+	}
+	return CLI_SHOWUSAGE;	/* default, failure */
 }
 
 /*! \brief Cli command to send SIP notify to peer */
@@ -11387,16 +11390,6 @@
 		pvt_unref(p);
 	}
 
-	return RESULT_SUCCESS;
-}
-
-/*! \brief Disable SIP Debugging in CLI */
-static int sip_no_debug(int fd, int argc, char *argv[])
-{
-	if (argc != 4)
-		return RESULT_SHOWUSAGE;
-	ast_clear_flag(&global_flags[1], SIP_PAGE2_DEBUG_CONSOLE);
-	ast_cli(fd, "SIP Debugging Disabled\n");
 	return RESULT_SUCCESS;
 }
 
@@ -11668,19 +11661,6 @@
 static char show_reg_usage[] =
 "Usage: sip show registry\n"
 "       Lists all registration requests and status.\n";
-
-static char debug_usage[] = 
-"Usage: sip debug\n"
-"       Enables dumping of SIP packets for debugging purposes\n\n"
-"       sip debug ip <host[:PORT]>\n"
-"       Enables dumping of SIP packets to and from host.\n\n"
-"       sip debug peer <peername>\n"
-"       Enables dumping of SIP packets to and from host.\n"
-"       Require peer to be registered.\n";
-
-static char no_debug_usage[] = 
-"Usage: sip debug off\n"
-"       Disables dumping of SIP packets for debugging purposes\n";
 
 static char no_history_usage[] = 
 "Usage: sip history off\n"
@@ -17544,21 +17524,7 @@
 	sip_prune_realtime, "Prune cached Realtime user(s)",
 	prune_realtime_usage, complete_sip_prune_realtime_user },
 
-	{ { "sip", "set", "debug", NULL },
-	sip_do_debug, "Enable SIP debugging",
-	debug_usage },
-
-	{ { "sip", "set", "debug", "ip", NULL },
-	sip_do_debug, "Enable SIP debugging on IP",
-	debug_usage },
-
-	{ { "sip", "set", "debug", "peer", NULL },
-	sip_do_debug, "Enable SIP debugging on Peername",
-	debug_usage, complete_sip_debug_peer },
-
-	{ { "sip", "set", "debug", "off", NULL },
-	sip_no_debug, "Disable SIP debugging",
-	no_debug_usage },
+	NEW_CLI(sip_do_debug, "Enable/Disable SIP debugging"),
 
 	{ { "sip", "history", NULL },
 	sip_do_history, "Enable SIP history",



More information about the asterisk-commits mailing list