]> andersk Git - libfaim.git/blob - aim_rxqueue.c
Initial revision
[libfaim.git] / aim_rxqueue.c
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  */
16 int 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 */
57 int 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   if (s < 3) 
83     return 0;
84
85   FD_ZERO(&fds);
86   FD_SET(s, &fds);
87   tv.tv_sec = 0;  /* wait, but only for 10us */
88   tv.tv_usec = 10;
89   
90   generic[0] = 0x00;  
91
92   readgood = 0;
93   i = 0;
94   j = 0;
95   /* read first 6 bytes (the FLAP header only) off the socket */
96   while ( (select(s+1, &fds, NULL, NULL, &tv) == 1) && (i < 6))
97     {
98       if ((err = Read(s, &(generic[i]), 1)) < 0)
99         {
100           /* error is probably not recoverable...(must be a pessimistic day) */
101           aim_conn_close(conn);
102           return err;
103         }
104
105       if (readgood == 0)
106         {
107           if (generic[i] == 0x2a)
108           {
109             readgood = 1;
110 #if debug > 1
111             printf("%x ", generic[i]);
112             fflush(stdout);
113 #endif
114             i++;
115           }
116           else
117             {
118 #if debug > 1
119               printf("skipping 0x%d ", generic[i]);
120               fflush(stdout);
121 #endif
122               j++;
123             }
124         }
125       else
126         {
127 #if debug > 1
128           printf("%x ", generic[i]);
129 #endif
130           i++;
131         }
132       FD_ZERO(&fds);
133       FD_SET(s, &fds);
134       tv.tv_sec= 2;
135       tv.tv_usec= 2;
136     }
137
138   if (generic[0] != 0x2a)
139     {
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!");
144       return -1;
145     }
146
147   isav = i;
148
149   /* allocate a new struct */
150   workingStruct = (struct command_rx_struct *) malloc(sizeof(struct command_rx_struct));
151   workingStruct->lock = 1;  /* lock the struct */
152
153   /* store type -- byte 2 */
154   workingStruct->type = (char) generic[1];
155
156   /* store seqnum -- bytes 3 and 4 */
157   workingStruct->seqnum = ( (( (u_int) generic[2]) & 0xFF) << 8);
158   workingStruct->seqnum += ( (u_int) generic[3]) & 0xFF;
159
160   /* store commandlen -- bytes 5 and 6 */
161   workingStruct->commandlen = ( (( (u_int) generic[4]) & 0xFF ) << 8);
162   workingStruct->commandlen += ( (u_int) generic[5]) & 0xFF;
163
164   /* malloc for data portion */
165   workingStruct->data = (char *) malloc(workingStruct->commandlen);
166
167   /* read the data portion of the packet */
168   i = Read(s, workingStruct->data, workingStruct->commandlen);
169   if (i < 0)
170     {
171       aim_conn_close(conn);
172       return i;
173     }
174
175 #if debug > 0
176   printf(" done. (%db+%db read, %db skipped)\n", isav, i, j);
177 #endif
178
179   workingStruct->conn = conn;
180
181   workingStruct->next = NULL;  /* this will always be at the bottom */
182   workingStruct->lock = 0; /* unlock */
183
184   /* enqueue this packet */
185   if (aim_queue_incoming == NULL)
186     {
187       aim_queue_incoming = workingStruct;
188     }
189   else
190     {
191       workingPtr = aim_queue_incoming;
192       while (workingPtr->next != NULL)
193         workingPtr = workingPtr->next;
194       workingPtr->next = workingStruct;
195     }
196   
197   
198   workingStruct->conn->lastactivity = time(NULL);
199
200   return 0;  
201 }
202
203 /*
204   purge_rxqueue()
205
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.
209   
210  */
211 struct command_rx_struct *aim_purge_rxqueue(struct command_rx_struct *queue)
212 {
213   struct command_rx_struct *workingPtr = NULL;
214   struct command_rx_struct *workingPtr2 = NULL;
215
216   if (queue == (struct command_rx_struct *)NULL) 
217     {
218     /* do nothing */
219     }
220   else if (queue->next == (struct command_rx_struct *)NULL)
221     {
222       if (queue->handled == 1) {
223         workingPtr = queue;
224         queue = NULL;
225         free(workingPtr->data);
226         free(workingPtr);
227       }
228     }
229   else 
230     {
231       while (queue->handled == 1) 
232         {
233           workingPtr = queue;
234           queue = queue->next;
235           free(workingPtr->data);
236           free(workingPtr);
237         }
238
239       workingPtr = queue;
240
241       while (workingPtr->next != (struct command_rx_struct *)NULL)
242         {
243           if (workingPtr->next->handled == 1) 
244             {
245               workingPtr2 = workingPtr->next;
246               workingPtr->next = workingPtr->next->next;
247               free(workingPtr2->data);
248               free(workingPtr2);
249             } 
250           else /* TODO: rework this so the additional if isn't needed */
251             {
252               if (workingPtr->next == (struct command_rx_struct *)NULL) 
253                 {
254                   if (workingPtr->handled == 1)
255                     {
256                       workingPtr2 = workingPtr;
257                       workingPtr = NULL;
258                       free(workingPtr2->data);
259                       free(workingPtr2);
260                       return queue;
261                     }
262                 } 
263               else 
264                 {
265                   workingPtr = workingPtr->next;
266                 }
267             }
268         }
269     }
270   return queue;
271 }
This page took 0.081519 seconds and 5 git commands to generate.