]> andersk Git - libfaim.git/blame - deprecated/aim_rxqueue.orig.c
- Thu Feb 8 20:12:39 UTC 2001
[libfaim.git] / deprecated / aim_rxqueue.orig.c
CommitLineData
9de3ca7e 1/*
2 aim_rxqueue.c
3
4 This file contains the management routines for the receive
5 (incoming packet) queue. The actual packet handlers are in
6 aim_rxhandlers.c.
7
8 */
9
10#include "aim.h"
11
12/*
13 This is a modified read() to make SURE we get the number
14 of bytes we are told to, otherwise block.
15 */
16int Read(int fd, u_char *buf, int len)
17{
18 int i = 0;
19 int j = 0;
20
21 while ((i < len) && (!(i < 0)))
22 {
23 j = read(fd, &(buf[i]), len-i);
24 if ( (j < 0) && (errno != EAGAIN))
25 return -errno; /* fail */
26 else
27 i += j; /* success, continue */
28 }
29#if 0
30 printf("\nRead Block: (%d/%04x)\n", len, len);
31 printf("\t");
32 for (j = 0; j < len; j++)
33 {
34 if (j % 8 == 0)
35 printf("\n\t");
36 if (buf[j] >= ' ' && buf[j] < 127)
37 printf("%c=%02x ",buf[j], buf[j]);
38 else
39 printf("0x%02x ", buf[j]);
40 }
41 printf("\n\n");
42#endif
43 return i;
44}
45
46/*
47 struct command_struct *
48 get_generic(
49 struct connection_info struct *,
50 struct command_struct *
51 )
52
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.
55
56*/
57int aim_get_command(void)
58{
59 int i, readgood, j, isav, err;
60 int s;
61 fd_set fds;
62 struct timeval tv;
63 char generic[6];
64 struct command_rx_struct *workingStruct = NULL;
65 struct command_rx_struct *workingPtr = NULL;
66 struct aim_conn_t *conn = NULL;
67#if debug > 0
68 printf("Reading generic/unknown response...");
69#endif
70
71
72 /* dont wait at all (ie, never call this unless something is there) */
73 tv.tv_sec = 0;
74 tv.tv_usec = 0;
75 conn = aim_select(&tv);
76
77 if (conn==NULL)
78 return 0; /* nothing waiting */
79
80 s = conn->fd;
81
82 FD_ZERO(&fds);
83 FD_SET(s, &fds);
84 tv.tv_sec = 0; /* wait, but only for 10us */
85 tv.tv_usec = 10;
86
87 generic[0] = 0x00;
88
89 readgood = 0;
90 i = 0;
91 j = 0;
92 /* read first 6 bytes (the FLAP header only) off the socket */
93 while ( (select(s+1, &fds, NULL, NULL, &tv) == 1) && (i < 6))
94 {
95 if ((err = Read(s, &(generic[i]), 1)) < 0)
96 {
97 /* error is probably not recoverable...(must be a pessimistic day) */
98 aim_conn_close(conn);
99 return err;
100 }
101
102 if (readgood == 0)
103 {
104 if (generic[i] == 0x2a)
105 {
106 readgood = 1;
107#if debug > 1
108 printf("%x ", generic[i]);
109 fflush(stdout);
110#endif
111 i++;
112 }
113 else
114 {
115#if debug > 1
116 printf("skipping 0x%d ", generic[i]);
117 fflush(stdout);
118#endif
119 j++;
120 }
121 }
122 else
123 {
124#if debug > 1
125 printf("%x ", generic[i]);
126#endif
127 i++;
128 }
129 FD_ZERO(&fds);
130 FD_SET(s, &fds);
131 tv.tv_sec= 2;
132 tv.tv_usec= 2;
133 }
134
135 if (generic[0] != 0x2a)
136 {
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!");
141 return -1;
142 }
143
144 isav = i;
145
146 /* allocate a new struct */
147 workingStruct = (struct command_rx_struct *) malloc(sizeof(struct command_rx_struct));
148 workingStruct->lock = 1; /* lock the struct */
149
150 /* store type -- byte 2 */
151 workingStruct->type = (char) generic[1];
152
153 /* store seqnum -- bytes 3 and 4 */
154 workingStruct->seqnum = ( (( (unsigned int) generic[2]) & 0xFF) << 8);
155 workingStruct->seqnum += ( (unsigned int) generic[3]) & 0xFF;
156
157 /* store commandlen -- bytes 5 and 6 */
158 workingStruct->commandlen = ( (( (unsigned int) generic[4]) & 0xFF ) << 8);
159 workingStruct->commandlen += ( (unsigned int) generic[5]) & 0xFF;
160
161 /* malloc for data portion */
162 workingStruct->data = (char *) malloc(workingStruct->commandlen);
163
164 /* read the data portion of the packet */
165 i = Read(s, workingStruct->data, workingStruct->commandlen);
166 if (i < 0)
167 {
168 aim_conn_close(conn);
169 return i;
170 }
171
172#if debug > 0
173 printf(" done. (%db+%db read, %db skipped)\n", isav, i, j);
174#endif
175
176 workingStruct->conn = conn;
177
178 workingStruct->next = NULL; /* this will always be at the bottom */
179 workingStruct->lock = 0; /* unlock */
180
181 /* enqueue this packet */
182 if (aim_queue_incoming == NULL)
183 aim_queue_incoming = workingStruct;
184 else
185 {
186 workingPtr = aim_queue_incoming;
187 while (workingPtr->next != NULL)
188 workingPtr = workingPtr->next;
189 workingPtr->next = workingStruct;
190 }
191
192 return 0;
193}
194
195/*
196 purge_rxqueue()
197
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.
201
202 */
203struct command_rx_struct *aim_purge_rxqueue(struct command_rx_struct *queue)
204{
205 int i = 0;
206 struct command_rx_struct *workingPtr = NULL;
207 struct command_rx_struct *workingPtr2 = NULL;
208
209 workingPtr = queue;
210 if (queue == NULL)
211 {
212 return queue;
213 }
214 else if (queue->next == NULL)
215 {
216 if (queue->handled == 1)
217 {
218 workingPtr2 = queue;
219 queue = NULL;
220 free(workingPtr2->data);
221 free(workingPtr2);
222 }
223 return queue;
224 }
225 else
226 {
227 for (i = 0; workingPtr != NULL; i++)
228 {
229 if (workingPtr->next->handled == 1)
230 {
231 /* save struct */
232 workingPtr2 = workingPtr->next;
233 /* dequeue */
234 workingPtr->next = workingPtr2->next;
235 /* free */
236 free(workingPtr2->data);
237 free(workingPtr2);
238 }
239
240 workingPtr = workingPtr->next;
241 }
242 }
243
244 return queue;
245}
This page took 0.125785 seconds and 5 git commands to generate.