diff src/protocols/oscar/conn.c @ 13253:87a7c3077c19

[gaim-migrate @ 15619] More cleaning up of oscar. Renamed some functions to be more clear. Got rid of some stuff that wasn't used. Inlined some small things in conn.c that were only used once. The goals of all this are 1. Non-blocking I/O for all connections 2. p2p stuff won't use the same struct as oscar connections, because that's stupid 3. The oscar PRPL should be less scary committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sun, 12 Feb 2006 21:27:04 +0000
parents 2871c385c45a
children e9802db22b06
line wrap: on
line diff
--- a/src/protocols/oscar/conn.c	Sun Feb 12 19:27:57 2006 +0000
+++ b/src/protocols/oscar/conn.c	Sun Feb 12 21:27:04 2006 +0000
@@ -27,10 +27,6 @@
 
 #include "oscar.h"
 
-/* This is defined in oscar.h, but only when !FAIM_INTERNAL, since the rest of
- * the library is not allowed to call it. */
-faim_export void aim_conn_kill(OscarSession *sess, OscarConnection **deadconn);
-
 #ifndef _WIN32
 #include <netdb.h>
 #include <sys/socket.h>
@@ -44,17 +40,17 @@
 /**
  * In OSCAR, every connection has a set of SNAC groups associated
  * with it.  These are the groups that you can send over this connection
- * without being guaranteed a "Not supported" SNAC error.  
+ * without being guaranteed a "Not supported" SNAC error.
  *
- * The grand theory of things says that these associations transcend 
+ * The grand theory of things says that these associations transcend
  * what libfaim calls "connection types" (conn->type).  You can probably
- * see the elegance here, but since I want to revel in it for a bit, you 
+ * see the elegance here, but since I want to revel in it for a bit, you
  * get to hear it all spelled out.
  *
  * So let us say that you have your core BOS connection running.  One
  * of your modules has just given you a SNAC of the group 0x0004 to send
  * you.  Maybe an IM destined for some twit in Greenland.  So you start
- * at the top of your connection list, looking for a connection that 
+ * at the top of your connection list, looking for a connection that
  * claims to support group 0x0004.  You find one.  Why, that neat BOS
  * connection of yours can do that.  So you send it on its way.
  *
@@ -72,8 +68,8 @@
  * it.  Great, you say.  Now I have something to do.  Off you go, making
  * that connection.  One of the first things you get from this new server
  * is a message saying that indeed it does support the group you were looking
- * for.  So you continue and send rate confirmation and all that.  
- * 
+ * for.  So you continue and send rate confirmation and all that.
+ *
  * Then you remember you had that SNAC to send, and now you have a means to
  * do it, and you do, and everyone is happy.  Except the Greenlander, who is
  * still stuck in the bitter cold.
@@ -86,7 +82,7 @@
  * scheme for quite some time now.  But I still haven't convinced myself
  * to make libfaim work that way.  It would take a fair amount of effort,
  * and probably some client API changes as well.  (Whenever I don't want
- * to do something, I just say it would change the client API.  Then I 
+ * to do something, I just say it would change the client API.  Then I
  * instantly have a couple of supporters of not doing it.)
  *
  * Generally, addgroup is only called by the internal handling of the
@@ -96,32 +92,33 @@
  * about such inane things.
  *
  */
-faim_internal void aim_conn_addgroup(OscarConnection *conn, guint16 group)
+void
+aim_conn_addgroup(OscarConnection *conn, guint16 group)
 {
 	aim_conn_inside_t *ins = (aim_conn_inside_t *)conn->inside;
 	struct snacgroup *sg;
 
-	if (!(sg = malloc(sizeof(struct snacgroup))))
-		return;
+	sg = malloc(sizeof(struct snacgroup));
 
 	gaim_debug_misc("oscar", "adding group 0x%04x\n", group);
 	sg->group = group;
 
 	sg->next = ins->groups;
 	ins->groups = sg;
-
-	return;
 }
 
-faim_export OscarConnection *aim_conn_findbygroup(OscarSession *sess, guint16 group)
+OscarConnection *
+aim_conn_findbygroup(OscarSession *sess, guint16 group)
 {
 	OscarConnection *cur;
 
-	for (cur = sess->connlist; cur; cur = cur->next) {
+	for (cur = sess->connlist; cur; cur = cur->next)
+	{
 		aim_conn_inside_t *ins = (aim_conn_inside_t *)cur->inside;
 		struct snacgroup *sg;
 
-		for (sg = ins->groups; sg; sg = sg->next) {
+		for (sg = ins->groups; sg; sg = sg->next)
+		{
 			if (sg->group == group)
 				return cur;
 		}
@@ -130,28 +127,28 @@
 	return NULL;
 }
 
-static void connkill_snacgroups(struct snacgroup **head)
+static void
+connkill_snacgroups(struct snacgroup *head)
 {
 	struct snacgroup *sg;
 
-	for (sg = *head; sg; ) {
+	for (sg = head; sg; )
+	{
 		struct snacgroup *tmp;
 
 		tmp = sg->next;
 		free(sg);
 		sg = tmp;
 	}
-
-	*head = NULL;
-
-	return;
 }
 
-static void connkill_rates(struct rateclass **head)
+static void
+connkill_rates(struct rateclass *head)
 {
 	struct rateclass *rc;
 
-	for (rc = *head; rc; ) {
+	for (rc = head; rc; )
+	{
 		struct rateclass *tmp;
 		struct snacpair *sp;
 
@@ -168,40 +165,36 @@
 
 		rc = tmp;
 	}
-
-	*head = NULL;
-
-	return;
 }
 
-static void connkill_real(OscarSession *sess, OscarConnection **deadconn)
+static void
+connkill_real(OscarSession *sess, OscarConnection *conn)
 {
 
-	aim_rxqueue_cleanbyconn(sess, *deadconn);
-	aim_tx_cleanqueue(sess, *deadconn);
+	aim_rxqueue_cleanbyconn(sess, conn);
+	aim_tx_cleanqueue(sess, conn);
 
-	if ((*deadconn)->fd != -1)
-		aim_conn_close(*deadconn);
+	if (conn->fd != -1)
+		aim_conn_close(conn);
 
 	/*
 	 * This will free ->internal if it necessary...
 	 */
-	if ((*deadconn)->type == AIM_CONN_TYPE_CHAT)
-		aim_conn_kill_chat(sess, *deadconn);
+	if (conn->type == AIM_CONN_TYPE_CHAT)
+		aim_conn_kill_chat(sess, conn);
 
-	if ((*deadconn)->inside) {
-		aim_conn_inside_t *inside = (aim_conn_inside_t *)(*deadconn)->inside;
+	if (conn->inside)
+	{
+		aim_conn_inside_t *inside = (aim_conn_inside_t *)conn->inside;
 
-		connkill_snacgroups(&inside->groups);
-		connkill_rates(&inside->rates);
+		connkill_snacgroups(inside->groups);
+		connkill_rates(inside->rates);
 
 		free(inside);
 	}
 
-	free(*deadconn);
-	*deadconn = NULL;
-
-	return;
+	gaim_circ_buffer_destroy(conn->buffer_outgoing);
+	free(conn);
 }
 
 /**
@@ -218,7 +211,7 @@
 	if (!sess || !conn)
 		return -EINVAL;
 
-	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x04, 0)))
+	if (!(fr = flap_frame_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x04, 0)))
 		return -ENOMEM;
 
 	aim_tx_enqueue(sess, fr);
@@ -227,83 +220,31 @@
 }
 
 /**
- * Clears out the connection list, killing remaining connections.
- *
- * @param sess Session to be cleared.
- */
-static void aim_connrst(OscarSession *sess)
-{
-
-	if (sess->connlist) {
-		OscarConnection *cur = sess->connlist, *tmp;
-
-		/* Attempt to send the log-off packet */
-		if (cur->type == AIM_CONN_TYPE_BOS)
-			aim_flap_close(sess, cur);
-
-		while (cur) {
-			tmp = cur->next;
-			aim_conn_close(cur);
-			connkill_real(sess, &cur);
-			cur = tmp;
-		}
-	}
-
-	sess->connlist = NULL;
-
-	return;
-}
-
-/**
- * Initializes and/or resets a connection structure to the default values.
- *
- * @param deadconn Connection to be reset.
- */
-static void aim_conn_init(OscarConnection *deadconn)
-{
-
-	if (!deadconn)
-		return;
-
-	deadconn->fd = -1;
-	deadconn->subtype = -1;
-	deadconn->type = -1;
-	deadconn->seqnum = 0;
-	deadconn->lastactivity = 0;
-	deadconn->forcedlatency = 0;
-	deadconn->handlerlist = NULL;
-	deadconn->priv = NULL;
-	memset(deadconn->inside, 0, sizeof(aim_conn_inside_t));
-
-	return;
-}
-
-/**
  * Allocate a new empty connection structure.
  *
- * @param sess Session
+ * @param sess The oscar session associated with this connection.
  * @return Returns the new connection structure.
  */
-static OscarConnection *aim_conn_getnext(OscarSession *sess)
+static OscarConnection *
+aim_conn_getnext(OscarSession *sess)
 {
-	OscarConnection *newconn;
-
-	if (!(newconn = malloc(sizeof(OscarConnection))))
-		return NULL;
-	memset(newconn, 0, sizeof(OscarConnection));
+	OscarConnection *conn;
 
-	if (!(newconn->inside = malloc(sizeof(aim_conn_inside_t)))) {
-		free(newconn);
-		return NULL;
-	}
-	memset(newconn->inside, 0, sizeof(aim_conn_inside_t));
+	conn = g_new0(OscarConnection, 1);
+	conn->inside = g_new0(aim_conn_inside_t, 1);
+	conn->buffer_outgoing = gaim_circ_buffer_new(-1);
+	conn->fd = -1;
+	conn->subtype = -1;
+	conn->type = -1;
+	conn->seqnum = 0;
+	conn->lastactivity = 0;
+	conn->forcedlatency = 0;
+	conn->handlerlist = NULL;
 
-	aim_conn_init(newconn);
+	conn->next = sess->connlist;
+	sess->connlist = conn;
 
-	newconn->next = sess->connlist;
-	sess->connlist = newconn;
-
-	return newconn;
+	return conn;
 }
 
 /**
@@ -313,7 +254,8 @@
  * @param sess Session for the connection.
  * @param deadconn Connection to be freed.
  */
-faim_export void aim_conn_kill(OscarSession *sess, OscarConnection **deadconn)
+void
+aim_conn_kill(OscarSession *sess, OscarConnection **deadconn)
 {
 	OscarConnection *cur, **prev;
 
@@ -331,15 +273,13 @@
 	if (!cur)
 		return; /* oops */
 
-	connkill_real(sess, &cur);
-
-	return;
+	connkill_real(sess, cur);
 }
 
 /**
  * Close (but not free) a connection.
  *
- * This leaves everything untouched except for clearing the 
+ * This leaves everything untouched except for clearing the
  * handler list and setting the fd to -1 (used to recognize
  * dead connections).  It will also remove cookies if necessary.
  *
@@ -348,7 +288,8 @@
  *
  * @param deadconn The connection to close.
  */
-faim_export void aim_conn_close(OscarConnection *deadconn)
+void
+aim_conn_close(OscarConnection *deadconn)
 {
 	aim_rxcallback_t userfunc;
 
@@ -362,12 +303,10 @@
 
 	if (deadconn->handlerlist)
 		aim_clearhandlers(deadconn);
-
-	return;
 }
 
 /**
- * Locates a connection of the specified type in the 
+ * Locates a connection of the specified type in the
  * specified session.
  *
  * XXX - Except for rendezvous, all uses of this should be removed and
@@ -378,7 +317,8 @@
  * @return Returns the first connection found of the given target type,
  *         or NULL if none could be found.
  */
-faim_export OscarConnection *aim_getconn_type(OscarSession *sess, int type)
+OscarConnection *
+aim_getconn_type(OscarSession *sess, int type)
 {
 	OscarConnection *cur;
 
@@ -391,7 +331,8 @@
 	return cur;
 }
 
-faim_export OscarConnection *aim_getconn_type_all(OscarSession *sess, int type)
+OscarConnection *
+aim_getconn_type_all(OscarSession *sess, int type)
 {
 	OscarConnection *cur;
 
@@ -404,7 +345,8 @@
 }
 
 /* If you pass -1 for the fd, you'll get what you ask for.  Gibberish. */
-faim_export OscarConnection *aim_getconn_fd(OscarSession *sess, int fd)
+OscarConnection *
+aim_getconn_fd(OscarSession *sess, int fd)
 {
 	OscarConnection *cur;
 
@@ -420,15 +362,14 @@
  * Clone an OscarConnection.
  *
  * A new connection is allocated, and the values are filled in
- * appropriately. Note that this function sets the new connnection's
- * ->priv pointer to be equal to that of its parent: only the pointer
- * is copied, not the data it points to.
+ * appropriately.
  *
  * @param sess The session containing this connection.
  * @param src The connection to clone.
  * @return Returns a pointer to the new OscarConnection, or %NULL on error.
  */
-faim_internal OscarConnection *aim_cloneconn(OscarSession *sess, OscarConnection *src)
+OscarConnection *
+aim_cloneconn(OscarSession *sess, OscarConnection *src)
 {
 	OscarConnection *conn;
 
@@ -439,7 +380,6 @@
 	conn->type = src->type;
 	conn->subtype = src->subtype;
 	conn->seqnum = src->seqnum;
-	conn->priv = src->priv;
 	conn->internal = src->internal;
 	conn->lastactivity = src->lastactivity;
 	conn->forcedlatency = src->forcedlatency;
@@ -451,7 +391,7 @@
 		 * XXX should clone this section as well, but since currently
 		 * this function only gets called for some of that rendezvous
 		 * crap, and not on SNAC connections, its probably okay for
-		 * now. 
+		 * now.
 		 *
 		 */
 	}
@@ -470,7 +410,8 @@
  * @param sess Session to create connection in
  * @param type Type of connection to create
  */
-faim_export OscarConnection *aim_newconn(OscarSession *sess, int type)
+OscarConnection *
+aim_newconn(OscarSession *sess, int type)
 {
 	OscarConnection *conn;
 
@@ -518,7 +459,8 @@
  * @param newval Number of seconds to force between transmits.
  * @return Returns -1 if the connection does not exist, zero otherwise.
  */
-faim_export int aim_conn_setlatency(OscarConnection *conn, int newval)
+int
+aim_conn_setlatency(OscarConnection *conn, int newval)
 {
 
 	if (!conn)
@@ -531,25 +473,19 @@
 }
 
 /**
- * Initializes a session structure by setting the initial values
- * stuff in the OscarSession struct.
- *
- * @param sess Session to initialize.
- * @param nonblocking Set to true if you want connections to be non-blocking.
+ * Allocates a new OscarSession and initializes it with default values.
  */
-faim_export void aim_session_init(OscarSession *sess, guint8 nonblocking)
+OscarSession *
+oscar_session_new(void)
 {
+	OscarSession *sess;
 
-	if (!sess)
-		return;
+	sess = g_new(OscarSession, 1);
 
-	memset(sess, 0, sizeof(OscarSession));
-	aim_connrst(sess);
 	sess->queue_outgoing = NULL;
 	sess->queue_incoming = NULL;
 	aim_initsnachash(sess);
 	sess->msgcookies = NULL;
-	sess->nonblocking = nonblocking;
 	sess->modlistv = NULL;
 	sess->snacid_next = 0x00000001;
 
@@ -603,7 +539,7 @@
 	aim__registermodule(sess, auth_modfirst);
 	aim__registermodule(sess, email_modfirst);
 
-	return;
+	return sess;
 }
 
 /**
@@ -611,15 +547,30 @@
  *
  * @param sess Session to kill
  */
-faim_export void aim_session_kill(OscarSession *sess)
+void
+oscar_session_destroy(OscarSession *sess)
 {
 	aim_cleansnacs(sess, -1);
 
-	aim_logoff(sess);
+	if (sess->connlist) {
+		OscarConnection *cur = sess->connlist, *tmp;
+
+		/* Attempt to send the log-off packet */
+		if (cur->type == AIM_CONN_TYPE_BOS)
+			aim_flap_close(sess, cur);
+
+		while (cur) {
+			tmp = cur->next;
+			aim_conn_close(cur);
+			connkill_real(sess, cur);
+			cur = tmp;
+		}
+	}
+	sess->connlist = NULL;
 
 	aim__shutdownmodules(sess);
 
-	return;
+	g_free(sess);
 }
 
 /**
@@ -630,7 +581,8 @@
  *         connecting (or if it just completed and
  *         aim_conn_completeconnect() has yet to be called on it).
  */
-faim_export int aim_conn_isconnecting(OscarConnection *conn)
+int
+aim_conn_isconnecting(OscarConnection *conn)
 {
 
 	if (!conn)
@@ -642,7 +594,8 @@
 /*
  * XXX this is nearly as ugly as proxyconnect().
  */
-faim_export int aim_conn_completeconnect(OscarSession *sess, OscarConnection *conn)
+int
+aim_conn_completeconnect(OscarSession *sess, OscarConnection *conn)
 {
 	aim_rxcallback_t userfunc;
 
@@ -665,7 +618,8 @@
 	return 0;
 }
 
-faim_export OscarSession *aim_conn_getsess(OscarConnection *conn)
+OscarSession *
+aim_conn_getsess(OscarConnection *conn)
 {
 
 	if (!conn)
@@ -675,27 +629,15 @@
 }
 
 /**
- * Close -ALL- open connections.
- *
- * @param sess The session.
- * @return Zero.
- */
-faim_export int aim_logoff(OscarSession *sess)
-{
-	aim_connrst(sess);  /* in case we want to connect again */
-
-	return 0;
-}
-
-/**
  * No-op.  This sends an empty channel 5 SNAC.  WinAIM 4.x and higher
  * sends these _every minute_ to keep the connection alive.
  */
-faim_export int aim_flap_nop(OscarSession *sess, OscarConnection *conn)
+int
+aim_flap_nop(OscarSession *sess, OscarConnection *conn)
 {
 	FlapFrame *fr;
 
-	if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x05, 0)))
+	if (!(fr = flap_frame_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x05, 0)))
 		return -ENOMEM;
 
 	aim_tx_enqueue(sess, fr);