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