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 */
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 type -- byte 2 */
151 workingStruct->type = (char) generic[1];
153 /* store seqnum -- bytes 3 and 4 */
154 workingStruct->seqnum = ( (( (unsigned int) generic[2]) & 0xFF) << 8);
155 workingStruct->seqnum += ( (unsigned int) generic[3]) & 0xFF;
157 /* store commandlen -- bytes 5 and 6 */
158 workingStruct->commandlen = ( (( (unsigned int) generic[4]) & 0xFF ) << 8);
159 workingStruct->commandlen += ( (unsigned int) generic[5]) & 0xFF;
161 /* malloc for data portion */
162 workingStruct->data = (char *) malloc(workingStruct->commandlen);
164 /* read the data portion of the packet */
165 i = Read(s, workingStruct->data, workingStruct->commandlen);
168 aim_conn_close(conn);
173 printf(" done. (%db+%db read, %db skipped)\n", isav, i, j);
176 workingStruct->conn = conn;
178 workingStruct->next = NULL; /* this will always be at the bottom */
179 workingStruct->lock = 0; /* unlock */
181 /* enqueue this packet */
182 if (aim_queue_incoming == NULL)
183 aim_queue_incoming = workingStruct;
186 workingPtr = aim_queue_incoming;
187 while (workingPtr->next != NULL)
188 workingPtr = workingPtr->next;
189 workingPtr->next = workingStruct;
198 This is just what it sounds. It purges the receive (rx) queue of
199 all handled commands. This is normally called from inside
200 aim_rxdispatch() after it's processed all the commands in the queue.
203 struct command_rx_struct *aim_purge_rxqueue(struct command_rx_struct *queue)
206 struct command_rx_struct *workingPtr = NULL;
207 struct command_rx_struct *workingPtr2 = NULL;
214 else if (queue->next == NULL)
216 if (queue->handled == 1)
220 free(workingPtr2->data);
227 for (i = 0; workingPtr != NULL; i++)
229 if (workingPtr->next->handled == 1)
232 workingPtr2 = workingPtr->next;
234 workingPtr->next = workingPtr2->next;
236 free(workingPtr2->data);
240 workingPtr = workingPtr->next;