*/
-#include "aim.h"
+#include <faim/aim.h>
/*
- This is a modified read() to make SURE we get the number
- of bytes we are told to, otherwise block.
- */
+ * This is a modified read() to make SURE we get the number
+ * of bytes we are told to, otherwise block.
+ *
+ * Modified to count errno (Sébastien Carpe <scarpe@atos-group.com>)
+ *
+*/
int Read(int fd, u_char *buf, int len)
{
int i = 0;
int j = 0;
-
+ int err_count=0;
+
while ((i < len) && (!(i < 0)))
{
j = read(fd, &(buf[i]), len-i);
if ( (j < 0) && (errno != EAGAIN))
- return -errno; /* fail */
- else
- i += j; /* success, continue */
- }
-#if 0
- printf("\nRead Block: (%d/%04x)\n", len, len);
- printf("\t");
- for (j = 0; j < len; j++)
- {
- if (j % 8 == 0)
- printf("\n\t");
- if (buf[j] >= ' ' && buf[j] < 127)
- printf("%c=%02x ",buf[j], buf[j]);
+ return -errno; /* fail */
+ else if (j==0)
+ {
+ err_count++;
+ if (err_count> MAX_READ_ERROR) {
+ /*
+ * Reached maximum number of allowed read errors.
+ *
+ * Lets suppose the connection is lost and errno didn't
+ * know it.
+ *
+ */
+ return (-1);
+ }
+ }
else
- printf("0x%02x ", buf[j]);
+ i += j; /* success, continue */
}
- printf("\n\n");
-#endif
return i;
}
-/*
- struct command_struct *
- get_generic(
- struct connection_info struct *,
- struct command_struct *
- )
-
- Grab as many command sequences as we can off the socket, and enqueue
- each command in the incoming event queue in a seperate struct.
-
-*/
-int aim_get_command(void)
+/*
+ * Grab as many command sequences as we can off the socket, and enqueue
+ * each command in the incoming event queue in a seperate struct.
+ */
+int aim_get_command(struct aim_session_t *sess)
{
int i, readgood, j, isav, err;
int s;
/* dont wait at all (ie, never call this unless something is there) */
tv.tv_sec = 0;
tv.tv_usec = 0;
- conn = aim_select(&tv);
+ conn = aim_select(sess, &tv);
if (conn==NULL)
return 0; /* nothing waiting */
workingStruct = (struct command_rx_struct *) malloc(sizeof(struct command_rx_struct));
workingStruct->lock = 1; /* lock the struct */
- /* store type -- byte 2 */
+ /* store channel -- byte 2 */
workingStruct->type = (char) generic[1];
/* store seqnum -- bytes 3 and 4 */
- workingStruct->seqnum = ( (( (u_int) generic[2]) & 0xFF) << 8);
- workingStruct->seqnum += ( (u_int) generic[3]) & 0xFF;
+ workingStruct->seqnum = aimutil_get16(generic+2);
/* store commandlen -- bytes 5 and 6 */
- workingStruct->commandlen = ( (( (u_int) generic[4]) & 0xFF ) << 8);
- workingStruct->commandlen += ( (u_int) generic[5]) & 0xFF;
+ workingStruct->commandlen = aimutil_get16(generic+4);
/* malloc for data portion */
- workingStruct->data = (char *) malloc(workingStruct->commandlen);
+ workingStruct->data = (u_char *) malloc(workingStruct->commandlen);
/* read the data portion of the packet */
i = Read(s, workingStruct->data, workingStruct->commandlen);
workingStruct->lock = 0; /* unlock */
/* enqueue this packet */
- if (aim_queue_incoming == NULL)
+ if (sess->queue_incoming == NULL)
{
- aim_queue_incoming = workingStruct;
+ sess->queue_incoming = workingStruct;
}
else
{
- workingPtr = aim_queue_incoming;
+ workingPtr = sess->queue_incoming;
while (workingPtr->next != NULL)
workingPtr = workingPtr->next;
workingPtr->next = workingStruct;
}
/*
- purge_rxqueue()
-
- This is just what it sounds. It purges the receive (rx) queue of
- all handled commands. This is normally called from inside
- aim_rxdispatch() after it's processed all the commands in the queue.
-
+ * purge_rxqueue()
+ *
+ * This is just what it sounds. It purges the receive (rx) queue of
+ * all handled commands. This is normally called from inside
+ * aim_rxdispatch() after it's processed all the commands in the queue.
+ *
*/
struct command_rx_struct *aim_purge_rxqueue(struct command_rx_struct *queue)
{