4 This file contains the management routines for the receive
5 (incoming packet) queue. The actual packet handlers are in
13 * This is a modified read() to make SURE we get the number
14 * of bytes we are told to, otherwise block.
16 * Modified to count errno (Sébastien Carpe <scarpe@atos-group.com>)
19 int Read(int fd, u_char *buf, int len)
25 while ((i < len) && (!(i < 0)))
27 j = read(fd, &(buf[i]), len-i);
28 if ( (j < 0) && (errno != EAGAIN))
29 return -errno; /* fail */
33 if (err_count> MAX_READ_ERROR) {
35 * Reached maximum number of allowed read errors.
37 * Lets suppose the connection is lost and errno didn't
45 i += j; /* success, continue */
51 * Grab as many command sequences as we can off the socket, and enqueue
52 * each command in the incoming event queue in a seperate struct.
54 int aim_get_command(struct aim_session_t *sess)
56 int i, readgood, j, isav, err;
61 struct command_rx_struct *workingStruct = NULL;
62 struct command_rx_struct *workingPtr = NULL;
63 struct aim_conn_t *conn = NULL;
65 printf("Reading generic/unknown response...");
69 /* dont wait at all (ie, never call this unless something is there) */
72 conn = aim_select(sess, &tv);
75 return 0; /* nothing waiting */
84 tv.tv_sec = 0; /* wait, but only for 10us */
92 /* read first 6 bytes (the FLAP header only) off the socket */
93 while ( (select(s+1, &fds, NULL, NULL, &tv) == 1) && (i < 6))
95 if ((err = Read(s, &(generic[i]), 1)) < 0)
97 /* error is probably not recoverable...(must be a pessimistic day) */
104 if (generic[i] == 0x2a)
108 printf("%x ", generic[i]);
116 printf("skipping 0x%d ", generic[i]);
125 printf("%x ", generic[i]);
135 if (generic[0] != 0x2a)
137 /* this really shouldn't happen, since the main loop
138 select() should protect us from entering this function
139 without data waiting */
140 printf("Bad incoming data!");
146 /* allocate a new struct */
147 workingStruct = (struct command_rx_struct *) malloc(sizeof(struct command_rx_struct));
148 workingStruct->lock = 1; /* lock the struct */
150 /* store channel -- byte 2 */
151 workingStruct->type = (char) generic[1];
153 /* store seqnum -- bytes 3 and 4 */
154 workingStruct->seqnum = aimutil_get16(generic+2);
156 /* store commandlen -- bytes 5 and 6 */
157 workingStruct->commandlen = aimutil_get16(generic+4);
159 /* malloc for data portion */
160 workingStruct->data = (u_char *) malloc(workingStruct->commandlen);
162 /* read the data portion of the packet */
163 i = Read(s, workingStruct->data, workingStruct->commandlen);
166 aim_conn_close(conn);
171 printf(" done. (%db+%db read, %db skipped)\n", isav, i, j);
174 workingStruct->conn = conn;
176 workingStruct->next = NULL; /* this will always be at the bottom */
177 workingStruct->lock = 0; /* unlock */
179 /* enqueue this packet */
180 if (sess->queue_incoming == NULL)
182 sess->queue_incoming = workingStruct;
186 workingPtr = sess->queue_incoming;
187 while (workingPtr->next != NULL)
188 workingPtr = workingPtr->next;
189 workingPtr->next = workingStruct;
193 workingStruct->conn->lastactivity = time(NULL);
201 * This is just what it sounds. It purges the receive (rx) queue of
202 * all handled commands. This is normally called from inside
203 * aim_rxdispatch() after it's processed all the commands in the queue.
206 struct command_rx_struct *aim_purge_rxqueue(struct command_rx_struct *queue)
208 struct command_rx_struct *workingPtr = NULL;
209 struct command_rx_struct *workingPtr2 = NULL;
211 if (queue == (struct command_rx_struct *)NULL)
215 else if (queue->next == (struct command_rx_struct *)NULL)
217 if (queue->handled == 1) {
220 free(workingPtr->data);
226 while (queue->handled == 1)
230 free(workingPtr->data);
236 while (workingPtr->next != (struct command_rx_struct *)NULL)
238 if (workingPtr->next->handled == 1)
240 workingPtr2 = workingPtr->next;
241 workingPtr->next = workingPtr->next->next;
242 free(workingPtr2->data);
245 else /* TODO: rework this so the additional if isn't needed */
247 if (workingPtr->next == (struct command_rx_struct *)NULL)
249 if (workingPtr->handled == 1)
251 workingPtr2 = workingPtr;
253 free(workingPtr2->data);
260 workingPtr = workingPtr->next;