[asterisk-commits] file: trunk r370152 - in /trunk: include/asterisk/xmpp.h res/res_xmpp.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jul 17 11:32:22 CDT 2012


Author: file
Date: Tue Jul 17 11:32:10 2012
New Revision: 370152

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=370152
Log:
Fix a crash as a result of propagating MWI or device state over XMPP when the client is disconnected.

The MWI and device state propagation code wrongly assumes that an XMPP client connection will remain established at all times. This fix corrects that by making the lifetime of the subscription the same as the lifetime of the connection itself. As the connection is established and disconnected the subscription itself is created and destroyed.

(closes issue ASTERISK-18078)
Reported by: elguero

Modified:
    trunk/include/asterisk/xmpp.h
    trunk/res/res_xmpp.c

Modified: trunk/include/asterisk/xmpp.h
URL: http://svnview.digium.com/svn/asterisk/trunk/include/asterisk/xmpp.h?view=diff&rev=370152&r1=370151&r2=370152
==============================================================================
--- trunk/include/asterisk/xmpp.h (original)
+++ trunk/include/asterisk/xmpp.h Tue Jul 17 11:32:10 2012
@@ -134,6 +134,8 @@
         pthread_t thread;
 	int timeout;
 	unsigned int reconnect:1; /*!< Reconnect this client */
+	struct ast_event_sub *mwi_sub; /*!< If distributing event information the MWI subscription */
+	struct ast_event_sub *device_state_sub; /*!< If distributing event information the device state subscription */
 };
 
 /*!

Modified: trunk/res/res_xmpp.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_xmpp.c?view=diff&rev=370152&r1=370151&r2=370152
==============================================================================
--- trunk/res/res_xmpp.c (original)
+++ trunk/res/res_xmpp.c Tue Jul 17 11:32:10 2012
@@ -387,9 +387,6 @@
 static const char *app_ajijoin = "JabberJoin";
 static const char *app_ajileave = "JabberLeave";
 
-static struct ast_event_sub *mwi_sub = NULL;
-static struct ast_event_sub *device_state_sub = NULL;
-
 static ast_cond_t message_received_condition;
 static ast_mutex_t messagelock;
 
@@ -1379,18 +1376,24 @@
 		return;
 	}
 
-	if (!mwi_sub) {
-		mwi_sub = ast_event_subscribe(AST_EVENT_MWI, xmpp_pubsub_mwi_cb, "xmpp_pubsub_mwi_subscription",
-					      client, AST_EVENT_IE_END);
-	}
-	if (!device_state_sub) {
-		if (ast_enable_distributed_devstate()) {
-			return;
-		}
-		device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
-						       xmpp_pubsub_devstate_cb, "xmpp_pubsub_devstate_subscription", client, AST_EVENT_IE_END);
-		ast_event_dump_cache(device_state_sub);
-	}
+	if (!(client->mwi_sub = ast_event_subscribe(AST_EVENT_MWI, xmpp_pubsub_mwi_cb, "xmpp_pubsub_mwi_subscription",
+						    client, AST_EVENT_IE_END))) {
+		return;
+	}
+
+	if (ast_enable_distributed_devstate()) {
+		return;
+	}
+	
+
+	if (!(client->device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
+							     xmpp_pubsub_devstate_cb, "xmpp_pubsub_devstate_subscription", client, AST_EVENT_IE_END))) {
+		ast_event_unsubscribe(client->mwi_sub);
+		client->mwi_sub = NULL;
+		return;
+	}
+
+	ast_event_dump_cache(client->device_state_sub);
 
 	xmpp_pubsub_subscribe(client, "device_state");
 	xmpp_pubsub_subscribe(client, "message_waiting");
@@ -3278,10 +3281,20 @@
 
 int ast_xmpp_client_disconnect(struct ast_xmpp_client *client)
 {
-	if (client->thread != AST_PTHREADT_NULL) {
+	if ((client->thread != AST_PTHREADT_NULL) && !pthread_equal(pthread_self(), client->thread)) {
 		client->state = XMPP_STATE_DISCONNECTING;
 		pthread_join(client->thread, NULL);
 		client->thread = AST_PTHREADT_NULL;
+	}
+
+	if (client->mwi_sub) {
+		ast_event_unsubscribe(client->mwi_sub);
+		client->mwi_sub = NULL;
+	}
+
+	if (client->device_state_sub) {
+		ast_event_unsubscribe(client->device_state_sub);
+		client->device_state_sub = NULL;
 	}
 
 #ifdef HAVE_OPENSSL
@@ -3290,6 +3303,8 @@
 		SSL_CTX_free(client->ssl_context);
 		SSL_free(client->ssl_session);
 	}
+
+	client->stream_flags = 0;
 #endif
 
 	if (client->parser) {
@@ -3318,11 +3333,8 @@
 		return -1;
 	}
 
-#ifdef HAVE_OPENSSL
-	client->stream_flags = 0;
-#endif
-
-	client->state = XMPP_STATE_DISCONNECTED;
+	ast_xmpp_client_disconnect(client);
+
 	client->timeout = 50;
 	iks_parser_reset(client->parser);
 
@@ -3459,6 +3471,7 @@
 			ast_debug(3, "Connecting client '%s'\n", client->name);
 			if ((res = xmpp_client_reconnect(client)) != IKS_OK) {
 				sleep(4);
+				res = IKS_NET_RWERR;
 			}
 			continue;
 		}
@@ -4141,12 +4154,6 @@
 	ast_unregister_application(app_ajileave);
 	ast_manager_unregister("JabberSend");
 	ast_custom_function_unregister(&jabberstatus_function);
-	if (mwi_sub) {
-		ast_event_unsubscribe(mwi_sub);
-	}
-	if (device_state_sub) {
-		ast_event_unsubscribe(device_state_sub);
-	}
 	ast_custom_function_unregister(&jabberreceive_function);
 
 	ast_cond_destroy(&message_received_condition);




More information about the asterisk-commits mailing list