4 This file contains the management routines for the receive
5 (incoming packet) queue. The actual packet handlers are in
14 * This is a modified read() to make SURE we get the number
15 * of bytes we are told to, otherwise block.
17 * Modified to count errno (Sébastien Carpe <scarpe@atos-group.com>)
20 int aim_failsaferead(int fd, u_char *buf, int len)
26 while ((i < len) && (!(i < 0)))
28 j = read(fd, &(buf[i]), len-i);
29 if ( (j < 0) && (errno != EAGAIN))
30 return -errno; /* fail */
34 if (err_count> MAX_READ_ERROR) {
36 * Reached maximum number of allowed read errors.
38 * Lets suppose the connection is lost and errno didn't
46 i += j; /* success, continue */
52 * Grab as many command sequences as we can off the socket, and enqueue
53 * each command in the incoming event queue in a seperate struct.
55 int aim_get_command(struct aim_session_t *sess)
57 int i, readgood, j, isav, err;
62 struct command_rx_struct *workingStruct = NULL;
63 struct aim_conn_t *conn = NULL;
66 faimdprintf(1, "Reading generic/unknown response...");
68 /* dont wait at all (ie, never call this unless something is there) */
71 conn = aim_select(sess, &tv, &selstat);
74 return 0; /* nothing waiting */
83 tv.tv_sec = 0; /* wait, but only for 10us */
91 /* read first 6 bytes (the FLAP header only) off the socket */
92 while ( (select(s+1, &fds, NULL, NULL, &tv) == 1) && (i < 6))
94 if ((err = aim_failsaferead(s, &(generic[i]), 1)) < 0)
96 /* error is probably not recoverable...(must be a pessimistic day) */
103 if (generic[i] == 0x2a)
106 faimdprintf(1, "%x ", generic[i]);
111 faimdprintf(1, "skipping 0x%d ", generic[i]);
117 faimdprintf(1, "%x ", generic[i]);
127 * This shouldn't happen unless the socket breaks, the server breaks,
128 * or we break. We must handle it just in case.
130 if (generic[0] != 0x2a) {
131 printf("Bad incoming data!");
137 /* allocate a new struct */
138 workingStruct = (struct command_rx_struct *) malloc(sizeof(struct command_rx_struct));
139 memset(workingStruct, 0x00, sizeof(struct command_rx_struct));
141 workingStruct->lock = 1; /* lock the struct */
143 /* store channel -- byte 2 */
144 workingStruct->type = (char) generic[1];
146 /* store seqnum -- bytes 3 and 4 */
147 workingStruct->seqnum = aimutil_get16(generic+2);
149 /* store commandlen -- bytes 5 and 6 */
150 workingStruct->commandlen = aimutil_get16(generic+4);
152 workingStruct->nofree = 0; /* free by default */
154 /* malloc for data portion */
155 workingStruct->data = (u_char *) malloc(workingStruct->commandlen);
157 /* read the data portion of the packet */
158 if (aim_failsaferead(s, workingStruct->data, workingStruct->commandlen) < 0){
159 aim_conn_close(conn);
163 faimdprintf(1, " done. (%db+%db read, %db skipped)\n", isav, i, j);
165 workingStruct->conn = conn;
167 workingStruct->next = NULL; /* this will always be at the bottom */
168 workingStruct->lock = 0; /* unlock */
170 /* enqueue this packet */
171 if (sess->queue_incoming == NULL) {
172 sess->queue_incoming = workingStruct;
174 struct command_rx_struct *cur;
177 * This append operation takes a while. It might be faster
178 * if we maintain a pointer to the last entry in the queue
179 * and just update that. Need to determine if the overhead
180 * to maintain that is lower than the overhead for this loop.
182 for (cur = sess->queue_incoming; cur->next; cur = cur->next)
184 cur->next = workingStruct;
187 workingStruct->conn->lastactivity = time(NULL);
193 * Purge recieve queue of all handled commands (->handled==1). Also
194 * allows for selective freeing using ->nofree so that the client can
195 * keep the data for various purposes.
197 * If ->nofree is nonzero, the frame will be delinked from the global list,
198 * but will not be free'ed. The client _must_ keep a pointer to the
199 * data -- libfaim will not! If the client marks ->nofree but
200 * does not keep a pointer, it's lost forever.
203 void aim_purge_rxqueue(struct aim_session_t *sess)
205 struct command_rx_struct *cur = NULL;
206 struct command_rx_struct *tmp;
208 if (sess->queue_incoming == NULL)
211 if (sess->queue_incoming->next == NULL) {
212 if (sess->queue_incoming->handled) {
213 tmp = sess->queue_incoming;
214 sess->queue_incoming = NULL;
225 for(cur = sess->queue_incoming; cur->next != NULL; ) {
226 if (cur->next->handled) {
228 cur->next = tmp->next;
238 * Be careful here. Because of the way we just
239 * manipulated the pointer, cur may be NULL and
240 * the for() will segfault doing the check unless
241 * we find this case first.