]> andersk Git - libfaim.git/blob - aim_rxqueue.c
- Mon Sep 4 22:08:35 GMT 2000
[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 #include <faim/aim.h> 
10 #include <sys/socket.h>
11
12 /*
13  * Since not all implementations support MSG_WAITALL, define
14  * an alternate guarenteed read function...
15  *
16  * We keep recv() for systems that can do it because it means
17  * a single system call for the entire packet, where read may
18  * take more for a badly fragmented packet.
19  *
20  */
21 static int aim_recv(int fd, void *buf, size_t count)
22 {
23 #ifdef MSG_WAITALL
24   return recv(fd, buf, count, MSG_WAITALL);
25 #else
26   int left, ret, cur = 0; 
27
28   left = count;
29
30   while (left) {
31     ret = read(fd, ((unsigned char *)buf)+cur, left);
32     if (ret == -1)
33       return -1;
34     if (ret == 0)
35       return cur;
36     
37     cur += ret;
38     left -= ret;
39   }
40
41   return cur;
42 #endif
43 }
44
45 /*
46  * Grab a single command sequence off the socket, and enqueue
47  * it in the incoming event queue in a seperate struct.
48  */
49 faim_export int aim_get_command(struct aim_session_t *sess, struct aim_conn_t *conn)
50 {
51   unsigned char generic[6]; 
52   struct command_rx_struct *newrx = NULL;
53
54   if (!sess || !conn)
55     return 0;
56
57   if (conn->fd < 3)  /* can happen when people abuse the interface */
58     return 0;
59
60   /*
61    * Rendezvous (client-client) connections do not speak
62    * FLAP, so this function will break on them.
63    */
64   if (conn->type == AIM_CONN_TYPE_RENDEZVOUS) 
65     return aim_get_command_rendezvous(sess, conn);
66   if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) 
67     return 0; 
68
69   /*
70    * Read FLAP header.  Six bytes:
71    *    
72    *   0 char  -- Always 0x2a
73    *   1 char  -- Channel ID.  Usually 2 -- 1 and 4 are used during login.
74    *   2 short -- Sequence number 
75    *   4 short -- Number of data bytes that follow.
76    */
77   faim_mutex_lock(&conn->active);
78   if (aim_recv(conn->fd, generic, 6) < 6){
79     aim_conn_close(conn);
80     faim_mutex_unlock(&conn->active);
81     return -1;
82   }
83
84   /*
85    * This shouldn't happen unless the socket breaks, the server breaks,
86    * or we break.  We must handle it just in case.
87    */
88   if (generic[0] != 0x2a) {
89     faimdprintf(1, "Bad incoming data!");
90     aim_conn_close(conn);
91     faim_mutex_unlock(&conn->active);
92     return -1;
93   }     
94
95   /* allocate a new struct */
96   if (!(newrx = (struct command_rx_struct *)malloc(sizeof(struct command_rx_struct)))) {
97     faim_mutex_unlock(&conn->active);
98     return -1;
99   }
100   memset(newrx, 0x00, sizeof(struct command_rx_struct));
101
102   newrx->lock = 1;  /* lock the struct */
103
104   /* we're doing OSCAR if we're here */
105   newrx->hdrtype = AIM_FRAMETYPE_OSCAR;
106
107   /* store channel -- byte 2 */
108   newrx->hdr.oscar.type = (char) generic[1];
109
110   /* store seqnum -- bytes 3 and 4 */
111   newrx->hdr.oscar.seqnum = aimutil_get16(generic+2);
112
113   /* store commandlen -- bytes 5 and 6 */
114   newrx->commandlen = aimutil_get16(generic+4);
115
116   newrx->nofree = 0; /* free by default */
117
118   /* malloc for data portion */
119   if (!(newrx->data = (u_char *) malloc(newrx->commandlen))) {
120     free(newrx);
121     faim_mutex_unlock(&conn->active);
122     return -1;
123   }
124
125   /* read the data portion of the packet */
126   if (aim_recv(conn->fd, newrx->data, newrx->commandlen) < newrx->commandlen){
127     free(newrx->data);
128     free(newrx);
129     aim_conn_close(conn);
130     faim_mutex_unlock(&conn->active);
131     return -1;
132   }
133   faim_mutex_unlock(&conn->active);
134
135   newrx->conn = conn;
136
137   newrx->next = NULL;  /* this will always be at the bottom */
138   newrx->lock = 0; /* unlock */
139
140   /* enqueue this packet */
141   if (sess->queue_incoming == NULL) {
142     sess->queue_incoming = newrx;
143   } else {
144     struct command_rx_struct *cur;
145
146     /*
147      * This append operation takes a while.  It might be faster
148      * if we maintain a pointer to the last entry in the queue
149      * and just update that.  Need to determine if the overhead
150      * to maintain that is lower than the overhead for this loop.
151      */
152     for (cur = sess->queue_incoming; cur->next; cur = cur->next)
153       ;
154     cur->next = newrx;
155   }
156   
157   newrx->conn->lastactivity = time(NULL);
158
159   return 0;  
160 }
161
162 /*
163  * Purge recieve queue of all handled commands (->handled==1).  Also
164  * allows for selective freeing using ->nofree so that the client can
165  * keep the data for various purposes.  
166  *
167  * If ->nofree is nonzero, the frame will be delinked from the global list, 
168  * but will not be free'ed.  The client _must_ keep a pointer to the
169  * data -- libfaim will not!  If the client marks ->nofree but
170  * does not keep a pointer, it's lost forever.
171  *
172  */
173 faim_export void aim_purge_rxqueue(struct aim_session_t *sess)
174 {
175   struct command_rx_struct *cur = NULL;
176   struct command_rx_struct *tmp;
177
178   if (sess->queue_incoming == NULL)
179     return;
180   
181   if (sess->queue_incoming->next == NULL) {
182     if (sess->queue_incoming->handled) {
183       tmp = sess->queue_incoming;
184       sess->queue_incoming = NULL;
185
186       if (!tmp->nofree) {
187         if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
188           free(tmp->hdr.oft.hdr2);
189         free(tmp->data);
190         free(tmp);
191       } else
192         tmp->next = NULL;
193     }
194     return;
195   }
196
197   for(cur = sess->queue_incoming; cur->next != NULL; ) {
198     if (cur->next->handled) {
199       tmp = cur->next;
200       cur->next = tmp->next;
201       if (!tmp->nofree) {
202         if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
203           free(tmp->hdr.oft.hdr2);
204         free(tmp->data);
205         free(tmp);
206       } else
207         tmp->next = NULL;
208     }   
209     cur = cur->next;
210
211     /* 
212      * Be careful here.  Because of the way we just
213      * manipulated the pointer, cur may be NULL and 
214      * the for() will segfault doing the check unless
215      * we find this case first.
216      */
217     if (cur == NULL)    
218       break;
219   }
220
221   return;
222 }
223
224 /*
225  * Since aim_get_command will aim_conn_kill dead connections, we need
226  * to clean up the rxqueue of unprocessed connections on that socket.
227  *
228  * XXX: this is something that was handled better in the old connection
229  * handling method, but eh.
230  */
231 faim_internal void aim_rxqueue_cleanbyconn(struct aim_session_t *sess, struct aim_conn_t *conn)
232 {
233   struct command_rx_struct *currx;
234
235   for (currx = sess->queue_incoming; currx; currx = currx->next) {
236     if ((!currx->handled) && (currx->conn == conn))
237       currx->handled = 1;
238   }     
239   return;
240 }
This page took 0.064131 seconds and 5 git commands to generate.