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