[asterisk-commits] murf: branch murf/bug11210 r108136 - /team/murf/bug11210/channels/chan_sip.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Mar 12 14:58:47 CDT 2008


Author: murf
Date: Wed Mar 12 14:58:47 2008
New Revision: 108136

URL: http://svn.digium.com/view/asterisk?view=rev&rev=108136
Log:
Probably no-one wants this, but I needed it to generate a database for realtime testing. It caused me to study the entire db interface now, so if we toss it, it served its purpose well.

Modified:
    team/murf/bug11210/channels/chan_sip.c

Modified: team/murf/bug11210/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/murf/bug11210/channels/chan_sip.c?view=diff&rev=108136&r1=108135&r2=108136
==============================================================================
--- team/murf/bug11210/channels/chan_sip.c (original)
+++ team/murf/bug11210/channels/chan_sip.c Wed Mar 12 14:58:47 2008
@@ -1925,6 +1925,7 @@
 static int expire_register(const void *data);
 static void *do_monitor(void *data);
 static int restart_monitor(void);
+static void peer_mailboxes_to_str(struct ast_str **mailbox_str, struct sip_peer *peer);
 /* static int sip_addrcmp(char *name, struct sockaddr_in *sin);	Support for peer matching */
 static int sip_refer_allocate(struct sip_pvt *p);
 static void ast_quiet_chan(struct ast_channel *chan);
@@ -1949,6 +1950,8 @@
 static char *sip_show_sched(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static char * _sip_show_peers(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
 static char *sip_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
+static char *_sip_dbdump(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[]);
+static char *sip_dbdump(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static char *sip_show_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 static void  print_group(int fd, ast_group_t group, int crlf);
 static const char *dtmfmode2str(int mode) attribute_const;
@@ -11896,10 +11899,24 @@
 	{ -1,                   NULL}, /* terminator */
 };
 
+static struct _map_x_s natcfgmodes[] = {
+	{ SIP_NAT_NEVER,        "never"},
+	{ SIP_NAT_ROUTE,        "route"},
+	{ SIP_NAT_ALWAYS,       "yes"},
+	{ SIP_NAT_RFC3581,      "no"},
+	{ -1,                   NULL}, /* terminator */
+};
+
 /*! \brief  Convert NAT setting to text string */
 static const char *nat2str(int nat)
 {
 	return map_x_s(natmodes, nat, "Unknown");
+}
+
+/*! \brief  Convert NAT setting to text string appropriate for config files */
+static const char *nat2strconfig(int nat)
+{
+	return map_x_s(natcfgmodes, nat, "Unknown");
 }
 
 /*! \brief  Report Peer status in character string
@@ -12183,6 +12200,291 @@
 	}
 
 	return _sip_show_peers(a->fd, NULL, NULL, NULL, a->argc, (const char **) a->argv);
+}
+
+static char *sip_dbdump(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "sip dbdump";
+		e->usage =
+			"Usage: sip dbdump [<file>]\n"
+			"       dumps user/peer info to the screen in SQL INSERT command form\n"
+			"       Optional file path will be output instead of to console if present.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	return _sip_dbdump(a->fd, NULL, NULL, NULL, a->argc, (const char **) a->argv);
+}
+
+/*! \brief Execute sip show peers command */
+static char *_sip_dbdump(int fd, int *total, struct mansession *s, const struct message *m, int argc, const char *argv[])
+{
+	struct sip_peer *peer;
+	struct sip_user *user;
+	struct ao2_iterator i;
+	char fname[1024];
+	const char *id;
+	FILE *f1;
+	char idtext[256] = "";
+	int realtimepeers;
+
+	realtimepeers = ast_check_realtime("sippeers");
+
+	if (s) {	/* Manager - get ActionID */
+		id = astman_get_header(m, "ActionID");
+		if (!ast_strlen_zero(id))
+			snprintf(idtext, sizeof(idtext), "ActionID: %s\r\n", id);
+	}
+
+	switch (argc) {
+	case 3:
+		strncpy(fname, argv[2], sizeof(fname));
+		f1 = fopen(fname, "w");
+		if (!f1) {
+			ast_cli(fd, "Sorry, could not open %s for writing!", fname);
+			return CLI_SHOWUSAGE;
+		}
+		break;
+	case 2:
+		ast_cli(fd, "Sorry, will only generate a file at the moment. Please run again with a file name to write to.\n");
+	default:
+		return CLI_SHOWUSAGE;
+	}
+
+	i = ao2_iterator_init(peers, 0);
+	while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {	
+
+		user = find_user(peer->name,realtimepeers);
+
+		ao2_lock(peer);
+
+		fprintf(f1, "INSERT INTO sipbuddies (");
+		
+		/* print out the populated field names in order */
+		fprintf(f1,"name");
+		
+		if (peer->host_dynamic)
+			fprintf(f1,",host");
+		if (ast_test_flag(&peer->flags[0], SIP_NAT))
+			fprintf(f1,",nat");
+		fprintf(f1,",type");
+		if (!ast_strlen_zero(peer->accountcode))
+			fprintf(f1,",accountcode");
+		if (peer->amaflags)
+			fprintf(f1,",amaflags");
+		fprintf(f1,",`call-limit`");
+		if (peer->callgroup)
+			fprintf(f1,",callgroup");
+		if (user && !ast_strlen_zero(user->cid_num))
+			fprintf(f1,",callerid");
+		if (ast_test_flag(&peer->flags[0], SIP_REINVITE))
+			fprintf(f1,",canreinvite");
+		if (!ast_strlen_zero(peer->context))
+			fprintf(f1,",context");
+		if (peer->defaddr.sin_addr.s_addr)
+			fprintf(f1,",defaultip");
+		if (ast_test_flag(&peer->flags[0], SIP_DTMF))
+			fprintf(f1,",dtmfmode");
+		if (!ast_strlen_zero(peer->fromuser))
+			fprintf(f1,",fromuser");
+		if (!ast_strlen_zero(peer->fromdomain))
+			fprintf(f1,",fromdomain");
+		if (ast_test_flag(&peer->flags[0], SIP_INSECURE))
+			fprintf(f1,",insecure");
+		if (!ast_strlen_zero(peer->language))
+			fprintf(f1,",language");
+		if (!AST_LIST_EMPTY(&peer->mailboxes)) {
+			fprintf(f1,",mailbox");
+		}
+		if (!ast_strlen_zero(peer->md5secret))
+			fprintf(f1,",md5secret");
+		if (peer->ha) {
+			if (peer->ha->sense == AST_SENSE_DENY) {
+				fprintf(f1,",deny");
+			}
+			if (peer->ha->next && peer->ha->next->sense == AST_SENSE_ALLOW) {
+				fprintf(f1,",permit");
+			}
+		}
+		if (!ast_strlen_zero(peer->mohinterpret))
+			fprintf(f1,",mohinterpret");
+		if (!ast_strlen_zero(peer->mohsuggest))
+			fprintf(f1,",mohsuggest");
+		if (peer->pickupgroup)
+			fprintf(f1,",pickupgroup");
+		if (peer->maxms)
+			fprintf(f1,",qualify");
+		if (!ast_strlen_zero(peer->regexten))
+			fprintf(f1,",regexten");
+		if (peer->rtptimeout)
+			fprintf(f1,",rtptimeout");
+		if (peer->rtpholdtimeout)
+			fprintf(f1,",rtpholdtimeout");
+		if (!ast_strlen_zero(peer->secret))
+			fprintf(f1,",secret");
+		if (peer->chanvars)
+			fprintf(f1,",setvar");
+		if (ast_codec_pref_index(&peer->prefs, 0)) { /* print the codecs wanted in order */
+			fprintf(f1,",allow");
+		}
+		if (!ast_strlen_zero(peer->fullcontact))
+			fprintf(f1,",fullcontact");
+		if (peer->addr.sin_addr.s_addr)
+			fprintf(f1,",ipaddr");
+		if (peer->addr.sin_port)
+			fprintf(f1,",port");
+		if (!ast_strlen_zero(peer->username))
+			fprintf(f1,",username");
+		
+		/* print out the values in order */
+		fprintf(f1, ") VALUES (");
+		
+		fprintf(f1,"'%s'", peer->name);
+		
+		if (peer->host_dynamic)
+			fprintf(f1,",'dynamic'");
+		if (ast_test_flag(&peer->flags[0], SIP_NAT)) {
+			fprintf(f1,",'%s'",nat2strconfig(ast_test_flag(&peer->flags[0], SIP_NAT)));
+		}
+		if (user)
+			fprintf(f1,",'friend'");
+		else
+			fprintf(f1,",'peer'");
+		if (!ast_strlen_zero(peer->accountcode))
+			fprintf(f1,",'%s'", peer->accountcode);
+		if (peer->amaflags)
+			fprintf(f1,",'%s'", ast_cdr_flags2str(peer->amaflags));
+		fprintf(f1,",%d", peer->call_limit);
+		if (peer->callgroup) {
+			char buf[256];
+			
+			fprintf(f1,",'%s'", ast_print_group(buf, sizeof(buf), peer->callgroup));
+		}
+		if (user && !ast_strlen_zero(user->cid_num))
+			fprintf(f1,",\"%s<%s>\"", user->cid_name, user->cid_num);
+		if (ast_test_flag(&peer->flags[0], SIP_REINVITE)) {
+			switch (ast_test_flag(&peer->flags[0], SIP_REINVITE)) {
+			case SIP_REINVITE_NONE:
+				fprintf(f1,",'no'");
+				break;
+			case SIP_CAN_REINVITE:
+				fprintf(f1,",'yes'");
+				break;
+			case SIP_CAN_REINVITE_NAT:
+				fprintf(f1,",'nonat'");
+				break;
+			case SIP_REINVITE_UPDATE:
+				fprintf(f1,",'update'");
+				break;
+			default:
+				fprintf(f1,",'no'");
+				break;
+			}
+		}
+		if (!ast_strlen_zero(peer->context))
+			fprintf(f1,",'%s'",peer->context);
+		if (peer->defaddr.sin_addr.s_addr)
+			fprintf(f1,",'%s'",  ast_inet_ntoa(peer->defaddr.sin_addr));
+		if (ast_test_flag(&peer->flags[0], SIP_DTMF)) {
+			fprintf(f1,",'%s'", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF)));
+		}
+		if (!ast_strlen_zero(peer->fromuser))
+			fprintf(f1,",'%s'", peer->fromuser);
+		if (!ast_strlen_zero(peer->fromdomain))
+			fprintf(f1,",'%s'", peer->fromdomain);
+		if (ast_test_flag(&peer->flags[0], SIP_INSECURE)) {
+			
+			fprintf(f1,",'%s'", insecure2str(ast_test_flag(&peer->flags[0], SIP_INSECURE)));
+		}
+		if (!ast_strlen_zero(peer->language))
+			fprintf(f1,",'%s'", peer->language);
+		
+		if (!AST_LIST_EMPTY(&peer->mailboxes)) {
+			struct ast_str *mailbox_str = ast_str_alloca(512);
+			peer_mailboxes_to_str(&mailbox_str, peer);
+			fprintf(f1,",'%s'", mailbox_str->str);
+		}
+		
+		if (!ast_strlen_zero(peer->md5secret))
+			fprintf(f1,",'%s'", peer->md5secret);
+		if (peer->ha) {
+			if (peer->ha->sense == AST_SENSE_DENY) {
+				fprintf(f1,",'%s/%s'",  ast_inet_ntoa(peer->ha->netaddr), ast_inet_ntoa(peer->ha->netmask));
+			}
+			if (peer->ha->next && peer->ha->next->sense == AST_SENSE_ALLOW) {
+				fprintf(f1,",'%s/%s'",  ast_inet_ntoa(peer->ha->next->netaddr), ast_inet_ntoa(peer->ha->next->netmask));
+			}
+		}
+		
+		if (!ast_strlen_zero(peer->mohinterpret))
+			fprintf(f1,",'%s'", peer->mohinterpret);
+		if (!ast_strlen_zero(peer->mohsuggest))
+			fprintf(f1,",'%s'", peer->mohsuggest);
+		if (peer->pickupgroup) {
+			char buf[256];
+			
+			fprintf(f1,",'%s'", ast_print_group(buf, sizeof(buf), peer->pickupgroup));
+		}
+		if (peer->maxms)
+			fprintf(f1,",'%d'", peer->maxms);
+		if (!ast_strlen_zero(peer->regexten))
+			fprintf(f1,",'%s'", peer->regexten);
+		if (peer->rtptimeout)
+			fprintf(f1,",'%d'", peer->rtptimeout);
+		if (peer->rtpholdtimeout)
+			fprintf(f1,",'%d'", peer->rtpholdtimeout);
+		if (!ast_strlen_zero(peer->secret))
+			fprintf(f1,",'%s'", peer->secret);
+		if (peer->chanvars) {
+			int first=1;
+			struct ast_variable *p1 = peer->chanvars;
+			fprintf(f1,",'");
+			while (p1)
+			{
+				if (!first)
+					fprintf(f1,";");
+				else
+					first = 0;
+					
+				fprintf(f1,"%s=%s", p1->name, p1->value);
+				p1 = p1->next;
+			}
+			fprintf(f1,"'");
+		}
+		
+		if (ast_codec_pref_index(&peer->prefs, 0)) { /* print the codecs wanted in order */
+			/* this code isn't general, it assumes deny=all; but that's pretty common.
+			   people who use this differently will have to modify the results by hand. sorry. */
+			int x, codec;
+			fprintf(f1,",'");
+			for(x = 0; x < 32 ; x++) {
+				codec = ast_codec_pref_index(&peer->prefs, x);
+				if (!codec)
+					break;
+				fprintf(f1, "%s", ast_getformatname(codec));
+				fprintf(f1, ":%d", peer->prefs.framing[x]);
+				if (x < 31 && ast_codec_pref_index(&peer->prefs, x + 1))
+					fprintf(f1, ",");
+			}
+			fprintf(f1,"'");
+		}
+		
+		if (!ast_strlen_zero(peer->fullcontact))
+			fprintf(f1,",'%s'", peer->fullcontact);
+		if (peer->addr.sin_addr.s_addr)
+			fprintf(f1,",'%s'",  ast_inet_ntoa(peer->addr.sin_addr));
+		if (peer->addr.sin_port)
+			fprintf(f1,",%d", peer->addr.sin_port);
+		if (!ast_strlen_zero(peer->username))
+			fprintf(f1,",'%s'", peer->username);
+		
+		fprintf(f1, ");\n");
+	}
+	fclose(f1);
+	return CLI_SUCCESS;
 }
 
 /*! \brief Execute sip show peers command */
@@ -21860,6 +22162,7 @@
 	AST_CLI_DEFINE(sip_show_inuse, "List all inuse/limits"),
 	AST_CLI_DEFINE(sip_show_objects, "List all SIP object allocations"),
 	AST_CLI_DEFINE(sip_show_peers, "List defined SIP peers"),
+	AST_CLI_DEFINE(sip_dbdump, "dump peer info into realtime db sql format"),
 	AST_CLI_DEFINE(sip_show_registry, "List SIP registration status"),
 	AST_CLI_DEFINE(sip_unregister, "Unregister (force expiration) a SIP peer from the registery\n"),
 	AST_CLI_DEFINE(sip_show_settings, "Show SIP global settings"),




More information about the asterisk-commits mailing list