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 int Read(int fd, u_char *buf, int len)
21 while ((i < len) && (!(i < 0)))
23 j = read(fd, &(buf[i]), len-i);
24 if ( (j < 0) && (errno != EAGAIN))
25 return -errno; /* fail */
27 i += j; /* success, continue */
30 printf("\nRead Block: (%d/%04x)\n", len, len);
32 for (j = 0; j < len; j++)
36 if (buf[j] >= ' ' && buf[j] < 127)
37 printf("%c=%02x ",buf[j], buf[j]);
39 printf("0x%02x ", buf[j]);
47 struct command_struct *
49 struct connection_info struct *,
50 struct command_struct *
53 Grab as many command sequences as we can off the socket, and enqueue
54 each command in the incoming event queue in a seperate struct.
57 int aim_get_command(void)
59 int i, readgood, j, isav, err;
64 struct command_rx_struct *workingStruct = NULL;
65 struct command_rx_struct *workingPtr = NULL;
66 struct aim_conn_t *conn = NULL;
68 printf("Reading generic/unknown response...");
72 /* dont wait at all (ie, never call this unless something is there) */
75 conn = aim_select(&tv);
78 return 0; /* nothing waiting */
87 tv.tv_sec = 0; /* wait, but only for 10us */
95 /* read first 6 bytes (the FLAP header only) off the socket */
96 while ( (select(s+1, &fds, NULL, NULL, &tv) == 1) && (i < 6))
98 if ((err = Read(s, &(generic[i]), 1)) < 0)
100 /* error is probably not recoverable...(must be a pessimistic day) */
101 aim_conn_close(conn);
107 if (generic[i] == 0x2a)
111 printf("%x ", generic[i]);
119 printf("skipping 0x%d ", generic[i]);
128 printf("%x ", generic[i]);
138 if (generic[0] != 0x2a)
140 /* this really shouldn't happen, since the main loop
141 select() should protect us from entering this function
142 without data waiting */
143 printf("Bad incoming data!");
149 /* allocate a new struct */
150 workingStruct = (struct command_rx_struct *) malloc(sizeof(struct command_rx_struct));
151 workingStruct->lock = 1; /* lock the struct */
153 /* store type -- byte 2 */
154 workingStruct->type = (char) generic[1];
156 /* store seqnum -- bytes 3 and 4 */
157 workingStruct->seqnum = ( (( (u_int) generic[2]) & 0xFF) << 8);
158 workingStruct->seqnum += ( (u_int) generic[3]) & 0xFF;
160 /* store commandlen -- bytes 5 and 6 */
161 workingStruct->commandlen = ( (( (u_int) generic[4]) & 0xFF ) << 8);
162 workingStruct->commandlen += ( (u_int) generic[5]) & 0xFF;
164 /* malloc for data portion */
165 workingStruct->data = (char *) malloc(workingStruct->commandlen);
167 /* read the data portion of the packet */
168 i = Read(s, workingStruct->data, workingStruct->commandlen);
171 aim_conn_close(conn);
176 printf(" done. (%db+%db read, %db skipped)\n", isav, i, j);
179 workingStruct->conn = conn;
181 workingStruct->next = NULL; /* this will always be at the bottom */
182 workingStruct->lock = 0; /* unlock */
184 /* enqueue this packet */
185 if (aim_queue_incoming == NULL)
187 aim_queue_incoming = workingStruct;
191 workingPtr = aim_queue_incoming;
192 while (workingPtr->next != NULL)
193 workingPtr = workingPtr->next;
194 workingPtr->next = workingStruct;
198 workingStruct->conn->lastactivity = time(NULL);
206 This is just what it sounds. It purges the receive (rx) queue of
207 all handled commands. This is normally called from inside
208 aim_rxdispatch() after it's processed all the commands in the queue.
211 struct command_rx_struct *aim_purge_rxqueue(struct command_rx_struct *queue)
213 struct command_rx_struct *workingPtr = NULL;
214 struct command_rx_struct *workingPtr2 = NULL;
216 if (queue == (struct command_rx_struct *)NULL)
220 else if (queue->next == (struct command_rx_struct *)NULL)
222 if (queue->handled == 1) {
225 free(workingPtr->data);
231 while (queue->handled == 1)
235 free(workingPtr->data);
241 while (workingPtr->next != (struct command_rx_struct *)NULL)
243 if (workingPtr->next->handled == 1)
245 workingPtr2 = workingPtr->next;
246 workingPtr->next = workingPtr->next->next;
247 free(workingPtr2->data);
250 else /* TODO: rework this so the additional if isn't needed */
252 if (workingPtr->next == (struct command_rx_struct *)NULL)
254 if (workingPtr->handled == 1)
256 workingPtr2 = workingPtr;
258 free(workingPtr2->data);
265 workingPtr = workingPtr->next;