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