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