diff src/protocols/oscar/rxqueue.c @ 7806:c86f075b269a

[gaim-migrate @ 8453] I modified some of the libfaim connection stuff. I dunno, it seems cleaner this way. I like to be comfortable with what's going on. It's possible that FT and DC won't work now (although, they should), but I'll get to making them suck less pretty soon. Also upgraded the Doxyfile to 1 version newer. It mostly just moved some things around, but also removed the CGI_BLEH lines from the end. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 08 Dec 2003 05:41:04 +0000
parents 04dc7fe68889
children 92cbf9713795
line wrap: on
line diff
--- a/src/protocols/oscar/rxqueue.c	Mon Dec 08 04:58:07 2003 +0000
+++ b/src/protocols/oscar/rxqueue.c	Mon Dec 08 05:41:04 2003 +0000
@@ -64,11 +64,10 @@
 }
 
 /**
- * aim_frame_destroy - free aim_frame_t
- * @frame: the frame to free
+ * Free an aim_frame_t
  *
- * returns -1 on error; 0 on success.
- *
+ * @param frame The frame to free.
+ * @return -1 on error; 0 on success.
  */
 faim_internal void aim_frame_destroy(aim_frame_t *frame)
 {
@@ -80,88 +79,94 @@
 }
 
 /*
- * Read a FLAP header from conn into fr, and return the number of bytes in the payload.
+ * Read a FLAP header from conn into fr, and return the number of 
+ * bytes in the payload.
+ *
+ * @return -1 on error, otherwise return the length of the payload.
  */
 static int aim_get_command_flap(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *fr)
 {
-	fu8_t flaphdr_raw[6];
-	aim_bstream_t flaphdr;
-	fu16_t payloadlen;
+	fu8_t hdr_raw[6];
+	aim_bstream_t hdr;
 	
-	aim_bstream_init(&flaphdr, flaphdr_raw, sizeof(flaphdr_raw));
+	fr->hdrtype = AIM_FRAMETYPE_FLAP;
 
 	/*
-	 * Read FLAP header.  Six bytes:
-	 *   0 char  -- Always 0x2a
-	 *   1 char  -- Channel ID.  Usually 2 -- 1 and 4 are used during login.
-	 *   2 short -- Sequence number
-	 *   4 short -- Number of data bytes that follow.
+	 * Read FLAP header.  Six bytes total.
+	 *
+	 *   Byte # | Description
+	 *   -------|-------------
+	 *    0x00  | Always 0x2a
+	 *    0x01  | Channel number, usually "2."  "1" is used during login,
+	 *          |   4 is used during logoff.
+	 *    0x02  | Sequence number, 2 bytes.
+	 *    0x04  | Number of data bytes that follow, 2 bytes.
 	 */
-	if (aim_bstream_recv(&flaphdr, conn->fd, 6) < 6) {
+	aim_bstream_init(&hdr, hdr_raw, sizeof(hdr_raw));
+	if (aim_bstream_recv(&hdr, conn->fd, 6) < 6) {
 		aim_conn_close(conn);
 		return -1;
 	}
 
-	aim_bstream_rewind(&flaphdr);
+	aim_bstream_rewind(&hdr);
 
 	/*
 	 * This shouldn't happen unless the socket breaks, the server breaks,
 	 * or we break.  We must handle it just in case.
 	 */
-	if (aimbs_get8(&flaphdr) != 0x2a) {
-		fu8_t start;
-
-		aim_bstream_rewind(&flaphdr);
-		start = aimbs_get8(&flaphdr);
-		faimdprintf(sess, 0, "FLAP framing disrupted (0x%02x)", start);
+	if (aimbs_get8(&hdr) != 0x2a) {
+		faimdprintf(sess, 0, "Invalid FLAP frame received on FLAP connection!");
 		aim_conn_close(conn);
 		return -1;
 	}	
 
-	/* we're doing FLAP if we're here */
-	fr->hdrtype = AIM_FRAMETYPE_FLAP;
+	fr->hdr.flap.channel = aimbs_get8(&hdr);
+	fr->hdr.flap.seqnum = aimbs_get16(&hdr);
 
-	fr->hdr.flap.type = aimbs_get8(&flaphdr);
-	fr->hdr.flap.seqnum = aimbs_get16(&flaphdr);
-	payloadlen = aimbs_get16(&flaphdr); /* length of payload */
-
-	return payloadlen;
+	return aimbs_get16(&hdr);
 }
 
 /*
- * Read a rendezvous header from conn into fr, and return the number of bytes in the payload.
+ * Read a rendezvous header from conn into fr, and return the number of 
+ * bytes in the payload.
+ *
+ * @return -1 on error, otherwise return the length of the payload.
  */
 static int aim_get_command_rendezvous(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *fr)
 {
-	fu8_t rendhdr_raw[8];
-	aim_bstream_t rendhdr;
+	fu8_t hdr_raw[8];
+	aim_bstream_t hdr;
+
+	fr->hdrtype = AIM_FRAMETYPE_OFT;
 
-	aim_bstream_init(&rendhdr, rendhdr_raw, sizeof(rendhdr_raw));
-
-	if (aim_bstream_recv(&rendhdr, conn->fd, 8) < 8) {
+	/*
+	 * Read rendezvous header
+	 */
+	aim_bstream_init(&hdr, hdr_raw, sizeof(hdr_raw));
+	if (aim_bstream_recv(&hdr, conn->fd, 8) < 8) {
 		aim_conn_close(conn);
 		return -1;
 	}
 
-	aim_bstream_rewind(&rendhdr);
-
-	fr->hdrtype = AIM_FRAMETYPE_OFT; /* a misnomer--rendezvous */
+	aim_bstream_rewind(&hdr);
 
-	aimbs_getrawbuf(&rendhdr, fr->hdr.rend.magic, 4);
-	fr->hdr.rend.hdrlen = aimbs_get16(&rendhdr) - 8;
-	fr->hdr.rend.type = aimbs_get16(&rendhdr);
+	aimbs_getrawbuf(&hdr, fr->hdr.rend.magic, 4);
+	fr->hdr.rend.hdrlen = aimbs_get16(&hdr);
+	fr->hdr.rend.type = aimbs_get16(&hdr);
 
-	return fr->hdr.rend.hdrlen;
+	return fr->hdr.rend.hdrlen - 8;
 }
 
 /*
  * Grab a single command sequence off the socket, and enqueue it in the incoming event queue 
  * in a separate struct.
+ *
+ * @return 0 on success, otherwise return the error number.
  */
 faim_export int aim_get_command(aim_session_t *sess, aim_conn_t *conn)
 {
-	aim_frame_t *newrx;
-	fu16_t payloadlen;
+	aim_frame_t *fr;
+	int payloadlen;
 
 	if (!sess || !conn)
 		return -EINVAL;
@@ -175,81 +180,67 @@
 	if (conn->status & AIM_CONN_STATUS_INPROGRESS)
 		return aim_conn_completeconnect(sess, conn);
 
-	if (!(newrx = (aim_frame_t *)calloc(sizeof(aim_frame_t), 1)))
+	if (!(fr = (aim_frame_t *)calloc(sizeof(aim_frame_t), 1)))
 		return -ENOMEM;
 
 	/*
 	 * Rendezvous (client to client) connections do not speak FLAP, so this 
 	 * function will break on them.
 	 */
-	if (conn->type == AIM_CONN_TYPE_RENDEZVOUS) {
-		int ret = aim_get_command_rendezvous(sess, conn, newrx);
+	if (conn->type == AIM_CONN_TYPE_RENDEZVOUS)
+		payloadlen = aim_get_command_rendezvous(sess, conn, fr);
+	else if (conn->type == AIM_CONN_TYPE_LISTENER) {
+		faimdprintf(sess, 0, "AIM_CONN_TYPE_LISTENER on fd %d\n", conn->fd);
+		free(fr);
+		return -1;
+	} else
+		payloadlen = aim_get_command_flap(sess, conn, fr);
 
-		if (ret < 0) {
-			free(newrx);
+	if (payloadlen < 0) {
+		free(fr);
+		return -1;
+	}
+
+	if (payloadlen > 0) {
+		fu8_t *payload = NULL;
+
+		if (!(payload = (fu8_t *) malloc(payloadlen))) {
+			aim_frame_destroy(fr);
 			return -1;
 		}
 
-		payloadlen = ret;
-	} else if (conn->type == AIM_CONN_TYPE_LISTENER) {
-		faimdprintf(sess, 0, "AIM_CONN_TYPE_LISTENER on fd %d\n", conn->fd);
-		free(newrx);
-		return -1;
-	} else
-		payloadlen = aim_get_command_flap(sess, conn, newrx);
-
-	newrx->nofree = 0; /* free by default */
-
-	if (payloadlen) {
-		fu8_t *payload = NULL;
-
-		if (!(payload = (fu8_t *) malloc(payloadlen))) {
-			aim_frame_destroy(newrx);
-			return -1;
-		}
-
-		aim_bstream_init(&newrx->data, payload, payloadlen);
+		aim_bstream_init(&fr->data, payload, payloadlen);
 
 		/* read the payload */
-		if (aim_bstream_recv(&newrx->data, conn->fd, payloadlen) < payloadlen) {
-			aim_frame_destroy(newrx); /* free's payload */
+		if (aim_bstream_recv(&fr->data, conn->fd, payloadlen) < payloadlen) {
+			aim_frame_destroy(fr); /* free's payload */
 			aim_conn_close(conn);
 			return -1;
 		}
 	} else
-		aim_bstream_init(&newrx->data, NULL, 0);
+		aim_bstream_init(&fr->data, NULL, 0);
 
-
-	aim_bstream_rewind(&newrx->data);
+	aim_bstream_rewind(&fr->data);
 
-	newrx->conn = conn;
+	fr->conn = conn;
 
-	newrx->next = NULL;  /* this will always be at the bottom */
-
-	if (!sess->queue_incoming)
-		sess->queue_incoming = newrx;
+	/* Enqueue this puppy */
+	fr->next = NULL;  /* this will always be at the bottom */
+	if (sess->queue_incoming == NULL)
+		sess->queue_incoming = fr;
 	else {
 		aim_frame_t *cur;
-
-		for (cur = sess->queue_incoming; cur->next; cur = cur->next)
-			;
-		cur->next = newrx;
+		for (cur = sess->queue_incoming; cur->next; cur = cur->next);
+		cur->next = fr;
 	}
 
-	newrx->conn->lastactivity = time(NULL);
+	fr->conn->lastactivity = time(NULL);
 
 	return 0;  
 }
 
 /*
- * Purge recieve queue of all handled commands (->handled==1).  Also
- * allows for selective freeing using ->nofree so that the client can
- * keep the data for various purposes.  
- *
- * If ->nofree is nonzero, the frame will be delinked from the global list, 
- * but will not be free'ed.  The client _must_ keep a pointer to the
- * data -- libfaim will not!  If the client marks ->nofree but
- * does not keep a pointer, it's lost forever.
+ * Purge recieve queue of all handled commands (->handled==1).
  *
  */
 faim_export void aim_purge_rxqueue(aim_session_t *sess)
@@ -258,12 +249,8 @@
 
 	for (prev = &sess->queue_incoming; (cur = *prev); ) {
 		if (cur->handled) {
-
 			*prev = cur->next;
-			
-			if (!cur->nofree)
-				aim_frame_destroy(cur);
-
+			aim_frame_destroy(cur);
 		} else
 			prev = &cur->next;
 	}
@@ -286,5 +273,6 @@
 		if ((!currx->handled) && (currx->conn == conn))
 			currx->handled = 1;
 	}	
+
 	return;
 }