[asterisk-commits] oej: branch oej/kill-the-user r101316 - /team/oej/kill-the-user/channels/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Jan 30 13:25:46 CST 2008


Author: oej
Date: Wed Jan 30 13:25:45 2008
New Revision: 101316

URL: http://svn.digium.com/view/asterisk?view=rev&rev=101316
Log:
For realtime support, add type=peer to avoid matching on the name on incoming
calls. This has to be done in the table.

Modified:
    team/oej/kill-the-user/channels/chan_sip.c

Modified: team/oej/kill-the-user/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/oej/kill-the-user/channels/chan_sip.c?view=diff&rev=101316&r1=101315&r2=101316
==============================================================================
--- team/oej/kill-the-user/channels/chan_sip.c (original)
+++ team/oej/kill-the-user/channels/chan_sip.c Wed Jan 30 13:25:45 2008
@@ -1427,6 +1427,7 @@
 	char rt_fromcontact;		/*!< P: copy fromcontact from realtime */
 	char host_dynamic;		/*!< P: Dynamic Peers register with Asterisk */
 	char selfdestruct;		/*!< P: Automatic peers need to destruct themselves */
+	char onlymatchonip;		/*!< P: Only match on IP for incoming calls (old type=peer) */
 
 	int expire;			/*!<  When to expire this peer registration */
 	int capability;			/*!<  Codec capability */
@@ -1828,14 +1829,14 @@
 
 /*--- Device object handling */
 static struct sip_peer *temp_peer(const char *name);
-static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime);
+static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int peeronly);
 static int update_call_counter(struct sip_pvt *fup, int event);
 static void sip_destroy_peer(struct sip_peer *peer);
 static int sip_poke_peer(struct sip_peer *peer);
 static void set_peer_defaults(struct sip_peer *peer);
 static struct sip_peer *temp_peer(const char *name);
 static void register_peer_exten(struct sip_peer *peer, int onoff);
-static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime);
+static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int forcenamematch);
 static int sip_poke_peer_s(const void *data);
 static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *p, struct sip_request *req);
 static void reg_source_db(struct sip_peer *peer);
@@ -3554,7 +3555,7 @@
 
 
 	/* Peer found in realtime, now build it in memory */
-	peer = build_peer(newpeername, var, varregs, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS));
+	peer = build_peer(newpeername, var, varregs, !ast_test_flag(&global_flags[1], SIP_PAGE2_RTCACHEFRIENDS), TRUE);
 	if (!peer) {
 		if(peerlist)
 			ast_config_destroy(peerlist);
@@ -3597,19 +3598,26 @@
 					(p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)));
 }
 
-/*! \brief Locate peer by name or ip address 
- *	This is used on incoming SIP message to find matching peer on ip
-	or outgoing message to find matching peer on name 
+/*! \brief Locate device by name or ip address 
+ *	This is used on find matching device on name or ip/port.
+	If the device was declared as type=peer, we don't match on peer name on incoming INVITEs.
+	
 	\note Avoid using this function in new functions if there's a way to avoid it, i
 	since it causes a database lookup or a traversal of the in-memory peer list.
 */
-static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
+static struct sip_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime, int forcenamematch)
 {
 	struct sip_peer *p = NULL;
 
-	if (peer)
-		p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
-	else
+	if (peer) {	/* Search on name */
+		ASTOBJ_CONTAINER_TRAVERSE(&peerl, !p, do {
+			ASTOBJ_RDLOCK(iterator);
+			if ((forcenamematch || !iterator->onlymatchonip) && !strcasecmp(iterator->name, peer)) {
+				p = iterator;
+			}
+			ASTOBJ_UNLOCK(iterator);
+		} while (0) );
+	} else
 		p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, name, sip_addr_hashfunc, 1, sip_addrcmp);
 
 	if (!p && realtime)
@@ -3784,7 +3792,7 @@
 	return 0;
 }
 
-/*! \brief create address structure from peer name
+/*! \brief create address structure from device name
  *      Or, if peer not found, find it in the global DNS 
  *      returns TRUE (-1) on failure, FALSE on success */
 static int create_addr(struct sip_pvt *dialog, const char *opeer)
@@ -3804,7 +3812,7 @@
 	dialog->sa.sin_family = AF_INET;
 	dialog->timer_t1 = global_t1; /* Default SIP retransmission timer T1 (RFC 3261) */
 	dialog->timer_b = global_timer_b; /* Default SIP transaction timer B (RFC 3261) */
-	peer = find_peer(peername, NULL, 1);
+	peer = find_peer(peername, NULL, TRUE, TRUE);
 
 	if (peer) {
 		int res = create_addr_from_peer(dialog, peer);
@@ -4126,8 +4134,8 @@
 
 	ast_copy_string(name, fup->username, sizeof(name));
 
-	/* Check the list of users only for incoming calls */
-	if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, 1) ) ) { /* Try to find peer */
+	/* Check the list of devices */
+	if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, TRUE, FALSE) ) ) { /* Try to find peer */
 		inuse = &p->inUse;
 		call_limit = &p->call_limit;
 		inringing = &p->inRinging;
@@ -9805,7 +9813,7 @@
 /*! \brief Change onhold state of a peer using a pvt structure */
 static void sip_peer_hold(struct sip_pvt *p, int hold)
 {
-	struct sip_peer *peer = find_peer(p->peername, NULL, 1);
+	struct sip_peer *peer = find_peer(p->peername, NULL, 1, TRUE);
 
 	if (!peer)
 		return;
@@ -9953,7 +9961,7 @@
 
 	ast_string_field_set(p, exten, name);
 	build_contact(p);
-	peer = find_peer(name, NULL, 1);
+	peer = find_peer(name, NULL, TRUE, TRUE);
 	if (!(peer && ast_apply_ha(peer->ha, sin))) {
 		/* Peer fails ACL check */
 		if (peer) {
@@ -10795,7 +10803,7 @@
 	}
 }
 
-/*! \brief Validate peer authentication */
+/*! \brief Validate device authentication */
 static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
 	struct sip_request *req, int sipmethod, struct sockaddr_in *sin,
 	struct sip_peer **authpeer,
@@ -10806,15 +10814,15 @@
 	int debug=sip_debug_test_addr(sin);
 	struct sip_peer *peer;
 
-	/* For subscribes, match on peer name only; for other methods,
+	/* For subscribes, match on device name only; for other methods,
 	 * match on IP address-port of the incoming request.
 	 */
-	/* First find peer on name */
-	peer = find_peer(of, NULL, 1);
-
-	/* Then find peer on IP (if it's not a SUBSCRIBE) */
+	/* First find device on name */
+	peer = find_peer(of, NULL, TRUE, FALSE);
+
+	/* Then find device on IP (if it's not a SUBSCRIBE) */
 	if (sipmethod != SIP_SUBSCRIBE)
-		find_peer(NULL, &p->recv, 1);
+		find_peer(NULL, &p->recv, TRUE, FALSE);
 
 	if (!peer) {
 		if (debug)
@@ -11909,7 +11917,7 @@
 		return CLI_SHOWUSAGE;
 
 	load_realtime = (argc == 5 && !strcmp(argv[4], "load")) ? TRUE : FALSE;
-	peer = find_peer(argv[3], NULL, load_realtime);
+	peer = find_peer(argv[3], NULL, load_realtime, TRUE);
 	if (s) { 	/* Manager */
 		if (peer) {
 			const char *id = astman_get_header(m,"ActionID");
@@ -12192,7 +12200,7 @@
 	if (a->argc != 3)
 		return CLI_SHOWUSAGE;
 	
-	if ((peer = find_peer(a->argv[2], NULL, load_realtime))) {
+	if ((peer = find_peer(a->argv[2], NULL, load_realtime, TRUE))) {
 		if (peer->expire > 0) {
 			expire_register(peer);
 			ast_cli(a->fd, "Unregistered peer \'%s\'\n\n", a->argv[2]);
@@ -13032,7 +13040,7 @@
 /*! \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);
+	struct sip_peer *peer = find_peer(arg, NULL, TRUE, TRUE);
 	if (!peer)
 		ast_cli(fd, "No such peer '%s'\n", arg);
 	else if (peer->addr.sin_addr.s_addr == 0)
@@ -13509,7 +13517,7 @@
 	else
 		colname = "ip";
 
-	if (!(peer = find_peer(data, NULL, 1)))
+	if (!(peer = find_peer(data, NULL, TRUE, TRUE)))
 		return -1;
 
 	if (!strcasecmp(colname, "ip")) {
@@ -17979,7 +17987,7 @@
 			return p->stimer->st_cached_max_se;
 		} else {
 			if (p->peername) {
-				struct sip_peer *pp = find_peer(p->peername, NULL, 1);
+				struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE);
 				if (pp) {
 					p->stimer->st_cached_max_se = pp->stimer.st_max_se;
 					return (p->stimer->st_cached_max_se);
@@ -17993,7 +18001,7 @@
 			return p->stimer->st_cached_min_se;
 		} else {
 			if (p->peername) {
-				struct sip_peer *pp = find_peer(p->peername, NULL, 1);
+				struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE);
 				if (pp) {
 					p->stimer->st_cached_min_se = pp->stimer.st_min_se;
 					return (p->stimer->st_cached_min_se);
@@ -18015,7 +18023,7 @@
 		return p->stimer->st_cached_ref;
 
 	if (p->peername) {
-		struct sip_peer *pp = find_peer(p->peername, NULL, 1);
+		struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE);
 		if (pp) {
 			p->stimer->st_cached_ref = pp->stimer.st_ref;
 			return pp->stimer.st_ref;
@@ -18039,7 +18047,7 @@
 		return p->stimer->st_cached_mode;
 
 	if (p->peername) {
-		struct sip_peer *pp = find_peer(p->peername, NULL, 1);
+		struct sip_peer *pp = find_peer(p->peername, NULL, TRUE, TRUE);
 		if (pp) {
 			p->stimer->st_cached_mode = pp->stimer.st_mode_oper;
 			return pp->stimer.st_mode_oper;
@@ -18185,7 +18193,7 @@
 
 	ast_debug(3, "Checking device state for peer %s\n", host);
 
-	if ((p = find_peer(host, NULL, 1))) {
+	if ((p = find_peer(host, NULL, TRUE, TRUE))) {
 		if (p->addr.sin_addr.s_addr || p->defaddr.sin_addr.s_addr) {
 			/* we have an address for the peer */
 		
@@ -18784,7 +18792,7 @@
 }
 
 /*! \brief Build peer from configuration (file or realtime static/dynamic) */
-static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
+static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int ispeer)
 {
 	struct sip_peer *peer = NULL;
 	struct ast_ha *oldha = NULL;
@@ -18821,6 +18829,9 @@
 			speerobjs++;
 		ASTOBJ_INIT(peer);
 	}
+
+	peer->onlymatchonip = ispeer;		/* If type=peer, don't match on caller ID for incoming calls */
+
 	/* Note that our peer HAS had its reference count incrased */
 	if (firstpass) {
 		peer->lastmsgssent = -1;
@@ -18861,6 +18872,9 @@
 		else if (realtime && !strcasecmp(v->name, "fullcontact")) {
 			ast_copy_string(peer->fullcontact, v->value, sizeof(peer->fullcontact));
 			peer->rt_fromcontact = TRUE;
+		} else if (!strcasecmp(v->name, "type")) {
+			if (!strcasecmp(v->value, "peer")) 
+				peer->onlymatchonip = true;		/* For realtime support, add type=peer in the table */
 		} else if (!strcasecmp(v->name, "secret")) 
 			ast_copy_string(peer->secret, v->value, sizeof(peer->secret));
 		else if (!strcasecmp(v->name, "md5secret")) 
@@ -19691,7 +19705,7 @@
 				hassip = ast_variable_retrieve(ucfg, cat, "hassip");
 				registersip = ast_variable_retrieve(ucfg, cat, "registersip");
 				if (ast_true(hassip) || (!hassip && genhassip)) {
-					peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
+					peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0, FALSE);
 					if (peer) {
 						ast_device_state_changed("SIP/%s", peer->name);
 						ASTOBJ_CONTAINER_LINK(&peerl,peer);
@@ -19748,13 +19762,13 @@
 				ast_log(LOG_WARNING, "type=friend is deprecated. Please use type=peer only.");
 				is_peer = 1;
 			} else if (!strcasecmp(utype, "peer"))
-				is_peer = 1;
+				is_peer = 2;
 			else {
 				ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, "sip.conf");
 				continue;
 			}
 			if (is_peer) {
-				peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
+				peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0, is_peer == 2);
 				if (peer) {
 					ASTOBJ_CONTAINER_LINK(&peerl,peer);
 					unref_peer(peer);




More information about the asterisk-commits mailing list