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