[asterisk-commits] russell: branch group/issue_11972 r105839 - in /team/group/issue_11972: chann...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 4 17:03:12 CST 2008


Author: russell
Date: Tue Mar  4 17:03:11 2008
New Revision: 105839

URL: http://svn.digium.com/view/asterisk?view=rev&rev=105839
Log:
First pass at making the server instance object reference counted to fix issues
in chan_sip
(related to issue #11972)

Modified:
    team/group/issue_11972/channels/chan_sip.c
    team/group/issue_11972/include/asterisk/tcptls.h
    team/group/issue_11972/main/http.c
    team/group/issue_11972/main/manager.c
    team/group/issue_11972/main/tcptls.c

Modified: team/group/issue_11972/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/group/issue_11972/channels/chan_sip.c?view=diff&rev=105839&r1=105838&r2=105839
==============================================================================
--- team/group/issue_11972/channels/chan_sip.c (original)
+++ team/group/issue_11972/channels/chan_sip.c Tue Mar  4 17:03:11 2008
@@ -818,6 +818,7 @@
 	char *header[SIP_MAX_HEADERS];
 	char *line[SIP_MAX_LINES];
 	char data[SIP_MAX_PACKET];
+	/* XXX Do we need to unref socket.ser when the request goes away? */
 	struct sip_socket socket;	/*!< The socket used for this request */
 };
 
@@ -2133,7 +2134,7 @@
 static void *sip_tcp_helper_thread(void *data)
 {
 	struct sip_pvt *pvt = data;
-	struct ast_tcptls_server_instance *ser = pvt->socket.ser;
+	struct ast_tcptls_server_instance *ser = ({ ao2_ref(pvt->socket.ser, +1); (ser); });
 
 	return _sip_tcp_helper_thread(pvt, ser);
 }
@@ -2225,12 +2226,15 @@
 	AST_LIST_LOCK(&threadl);
 	AST_LIST_REMOVE(&threadl, me, list);
 	AST_LIST_UNLOCK(&threadl);
+	ao2_ref(me->ser, -1);
 	ast_free(me);
 cleanup2:
 	fclose(ser->f);
-
-	/* XXX this cleanup needs to happen somewhere else. */
-	ser = ast_tcptls_server_instance_destroy(ser);
+	ser->f = NULL;
+	ser->fd = -1;
+
+	ao2_ref(ser, -1);
+	ser = NULL;
 
 	return NULL;
 }
@@ -3466,6 +3470,12 @@
 	if (peer->dnsmgr)
 		ast_dnsmgr_release(peer->dnsmgr);
 	clear_peer_mailboxes(peer);
+
+	if (peer->socket.ser) {
+		ao2_ref(peer->socket.ser, -1);
+		peer->socket.ser = NULL;
+	}
+
 	ast_free(peer);
 }
 
@@ -3862,6 +3872,20 @@
 	}
 }
 
+static void copy_socket_data(struct sip_socket *to_sock, const struct sip_socket *from_sock)
+{
+	if (to_sock->ser) {
+		ao2_ref(to_sock->ser, -1);
+		to_sock->ser = NULL;
+	}
+
+	if (from_sock->ser) {
+		ao2_ref(from_sock->ser, +1);
+	}
+
+	*to_sock = *from_sock;
+}
+
 /*! \brief Create address structure from peer reference.
  *	This function copies data from peer to the dialog, so we don't have to look up the peer
  *	again from memory or database during the life time of the dialog.
@@ -3870,7 +3894,7 @@
  */
 static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
 {
-	dialog->socket = peer->socket;
+	copy_socket_data(&dialog->socket, &peer->socket);
 
 	if ((peer->addr.sin_addr.s_addr || peer->defaddr.sin_addr.s_addr) &&
 	    (!peer->maxms || ((peer->lastms >= 0)  && (peer->lastms <= peer->maxms)))) {
@@ -4317,6 +4341,11 @@
 	ast_mutex_destroy(&p->pvt_lock);
 
 	ast_string_field_free_memory(p);
+
+	if (p->socket.ser) {
+		ao2_ref(p->socket.ser, -1);
+		p->socket.ser = NULL;
+	}
 
 	ast_free(p);
 }
@@ -7443,7 +7472,7 @@
 	build_via(p);
 	ast_string_field_set(p, callid, callid);
 
-	p->socket = req->socket;
+	copy_socket_data(&p->socket, &req->socket);
 
 	/* Use this temporary pvt structure to send the message */
 	__transmit_response(p, msg, req, XMIT_UNRELIABLE);
@@ -9659,7 +9688,8 @@
 		}
 	}
 
-	pvt->socket = peer->socket = req->socket;
+	copy_socket_data(&peer->socket, &req->socket);
+	copy_socket_data(&pvt->socket, &peer->socket);
 
 	/* Look for brackets */
 	curi = contact;
@@ -17989,7 +18019,7 @@
 			return 1;
 		}
 
-		p->socket = req->socket;
+		copy_socket_data(&p->socket, &req->socket);
 
 		/* Go ahead and lock the owner if it has one -- we may need it */
 		/* becaues this is deadlock-prone, we need to try and unlock if failed */
@@ -18084,7 +18114,7 @@
 
 	if ((ser = sip_tcp_locate(&ca.sin))) {
 		s->fd = ser->fd;
-		s->ser = ser;
+		s->ser = ({ ao2_ref(ser, +1); (ser); });
 		return s->fd;
 	}
 
@@ -18100,7 +18130,7 @@
 				ast_copy_string(ca.hostname, p->tohost, sizeof(ca.hostname));
 		}
 	}
-	s->ser = (!s->ser) ? ast_tcptls_client_start(&ca) : s->ser;
+	s->ser = (!s->ser) ? ast_tcptls_client_start(&ca) : ({ ao2_ref(s->ser, +1); (s->ser); });
 
 	if (!s->ser) {
 		if (ca.tls_cfg)

Modified: team/group/issue_11972/include/asterisk/tcptls.h
URL: http://svn.digium.com/view/asterisk/team/group/issue_11972/include/asterisk/tcptls.h?view=diff&rev=105839&r1=105838&r2=105839
==============================================================================
--- team/group/issue_11972/include/asterisk/tcptls.h (original)
+++ team/group/issue_11972/include/asterisk/tcptls.h Tue Mar  4 17:03:11 2008
@@ -50,6 +50,7 @@
 #define _ASTERISK_SERVER_H
 
 #include "asterisk/utils.h"
+#include "asterisk/astobj2.h"
 
 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
 #define DO_SSL  /* comment in/out if you want to support ssl */
@@ -167,11 +168,4 @@
 HOOK_T ast_tcptls_server_read(struct ast_tcptls_server_instance *ser, void *buf, size_t count);
 HOOK_T ast_tcptls_server_write(struct ast_tcptls_server_instance *ser, void *buf, size_t count);
 
-/*!
- * \brief Destroy a server instance
- *
- * \return NULL for convenience
- */
-struct ast_tcptls_server_instance *ast_tcptls_server_instance_destroy(struct ast_tcptls_server_instance *i);
-
 #endif /* _ASTERISK_SERVER_H */

Modified: team/group/issue_11972/main/http.c
URL: http://svn.digium.com/view/asterisk/team/group/issue_11972/main/http.c?view=diff&rev=105839&r1=105838&r2=105839
==============================================================================
--- team/group/issue_11972/main/http.c (original)
+++ team/group/issue_11972/main/http.c Tue Mar  4 17:03:11 2008
@@ -827,7 +827,8 @@
 
 done:
 	fclose(ser->f);
-	ser = ast_tcptls_server_instance_destroy(ser);
+	ao2_ref(ser, -1);
+	ser = NULL;
 	return NULL;
 }
 

Modified: team/group/issue_11972/main/manager.c
URL: http://svn.digium.com/view/asterisk/team/group/issue_11972/main/manager.c?view=diff&rev=105839&r1=105838&r2=105839
==============================================================================
--- team/group/issue_11972/main/manager.c (original)
+++ team/group/issue_11972/main/manager.c Tue Mar  4 17:03:11 2008
@@ -2882,7 +2882,8 @@
 	destroy_session(s);
 
 done:
-	ser = ast_tcptls_server_instance_destroy(ser);
+	ao2_ref(ser, -1);
+	ser = NULL;
 	return NULL;
 }
 

Modified: team/group/issue_11972/main/tcptls.c
URL: http://svn.digium.com/view/asterisk/team/group/issue_11972/main/tcptls.c?view=diff&rev=105839&r1=105838&r2=105839
==============================================================================
--- team/group/issue_11972/main/tcptls.c (original)
+++ team/group/issue_11972/main/tcptls.c Tue Mar  4 17:03:11 2008
@@ -99,6 +99,12 @@
 	return write(ser->fd, buf, count);
 }
 
+static void server_instance_destructor(void *obj)
+{
+	struct ast_tcptls_server_instance *i = obj;
+	ast_mutex_destroy(&i->lock);
+}
+
 void *ast_tcptls_server_root(void *data)
 {
 	struct server_args *desc = data;
@@ -123,7 +129,7 @@
 				ast_log(LOG_WARNING, "Accept failed: %s\n", strerror(errno));
 			continue;
 		}
-		ser = ast_calloc(1, sizeof(*ser));
+		ser = ao2_alloc(sizeof(*ser), server_instance_destructor);
 		if (!ser) {
 			ast_log(LOG_WARNING, "No memory for new session: %s\n", strerror(errno));
 			close(fd);
@@ -143,7 +149,7 @@
 		if (ast_pthread_create_detached_background(&launched, NULL, ast_make_file_from_fd, ser)) {
 			ast_log(LOG_WARNING, "Unable to launch helper thread: %s\n", strerror(errno));
 			close(ser->fd);
-			ast_free(ser);
+			ao2_ref(ser, -1);
 		}
 	}
 	return NULL;
@@ -238,7 +244,7 @@
 		goto error;
 	}
 
-	if (!(ser = ast_calloc(1, sizeof(*ser))))
+	if (!(ser = ao2_alloc(sizeof(*ser), server_instance_destructor)))
 		goto error;
 
 	ast_mutex_init(&ser->lock);
@@ -267,7 +273,7 @@
 	close(desc->accept_fd);
 	desc->accept_fd = -1;
 	if (ser)
-		ast_free(ser);
+		ao2_ref(ser, -1);
 	return NULL;
 }
 
@@ -452,9 +458,4 @@
 		return ser;
 }
 
-struct ast_tcptls_server_instance *ast_tcptls_server_instance_destroy(struct ast_tcptls_server_instance *i)
-{
-	ast_mutex_destroy(&i->lock);
-	ast_free(i);
-	return NULL;
-}
+




More information about the asterisk-commits mailing list