]> andersk Git - libfaim.git/blame - src/txqueue.c
- Mon Mar 5 01:19:48 UTC 2001
[libfaim.git] / src / txqueue.c
CommitLineData
9de3ca7e 1/*
24286d93 2 * aim_txqueue.c
3 *
4 * Herein lies all the mangement routines for the transmit (Tx) queue.
5 *
9de3ca7e 6 */
7
37ee990e 8#define FAIM_INTERNAL
dd60ff8b 9#include <aim.h>
9de3ca7e 10
5ac21963 11#ifndef _WIN32
12#include <sys/socket.h>
13#endif
14
f1a5efe0 15/*
16 * Allocate a new tx frame.
17 *
18 * This is more for looks than anything else.
5b79dc93 19 *
20 * Right now, that is. If/when we implement a pool of transmit
21 * frames, this will become the request-an-unused-frame part.
b69540e3 22 *
23 * framing = AIM_FRAMETYPE_OFT/OSCAR
24 * chan = channel for OSCAR, hdrtype for OFT
25 *
f1a5efe0 26 */
646c6b52 27faim_internal struct command_tx_struct *aim_tx_new(struct aim_session_t *sess, struct aim_conn_t *conn, unsigned char framing, int chan, int datalen)
f1a5efe0 28{
a15d82b1 29 struct command_tx_struct *newtx;
f1a5efe0 30
e88ba395 31 if (!conn) {
646c6b52 32 faimdprintf(sess, 0, "aim_tx_new: ERROR: no connection specified\n");
5b79dc93 33 return NULL;
e88ba395 34 }
5b79dc93 35
646c6b52 36 /* For sanity... */
37 if ((conn->type == AIM_CONN_TYPE_RENDEZVOUS) || (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT)) {
38 if (framing != AIM_FRAMETYPE_OFT) {
39 faimdprintf(sess, 0, "aim_tx_new: attempted to allocate inappropriate frame type for rendezvous connection\n");
40 return NULL;
41 }
42 } else {
43 if (framing != AIM_FRAMETYPE_OSCAR) {
44 faimdprintf(sess, 0, "aim_tx_new: attempted to allocate inappropriate frame type for FLAP connection\n");
45 return NULL;
46 }
47 }
48
a15d82b1 49 newtx = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct));
50 if (!newtx)
f1a5efe0 51 return NULL;
a15d82b1 52 memset(newtx, 0, sizeof(struct command_tx_struct));
f1a5efe0 53
a15d82b1 54 newtx->conn = conn;
5b79dc93 55
56 if(datalen) {
a15d82b1 57 newtx->data = (unsigned char *)malloc(datalen);
58 newtx->commandlen = datalen;
b69540e3 59 } else
a15d82b1 60 newtx->data = NULL;
b69540e3 61
a15d82b1 62 newtx->hdrtype = framing;
63 if (newtx->hdrtype == AIM_FRAMETYPE_OSCAR) {
64 newtx->hdr.oscar.type = chan;
65 } else if (newtx->hdrtype == AIM_FRAMETYPE_OFT) {
66 newtx->hdr.oft.type = chan;
67 newtx->hdr.oft.hdr2len = 0; /* this will get setup by caller */
b69540e3 68 } else {
646c6b52 69 faimdprintf(sess, 0, "tx_new: unknown framing\n");
5b79dc93 70 }
71
a15d82b1 72 return newtx;
f1a5efe0 73}
74
9de3ca7e 75/*
e88ba395 76 * aim_tx_enqeue__queuebased()
24286d93 77 *
78 * The overall purpose here is to enqueue the passed in command struct
79 * into the outgoing (tx) queue. Basically...
80 * 1) Make a scope-irrelevent copy of the struct
81 * 2) Lock the struct
82 * 3) Mark as not-sent-yet
83 * 4) Enqueue the struct into the list
84 * 5) Unlock the struct once it's linked in
85 * 6) Return
86 *
e88ba395 87 * Note that this is only used when doing queue-based transmitting;
88 * that is, when sess->tx_enqueue is set to &aim_tx_enqueue__queuebased.
89 *
9de3ca7e 90 */
b8c79ca7 91static int aim_tx_enqueue__queuebased(struct aim_session_t *sess, struct command_tx_struct *newpacket)
9de3ca7e 92{
b8d0da45 93 struct command_tx_struct *cur;
9de3ca7e 94
b8d0da45 95 if (newpacket->conn == NULL) {
646c6b52 96 faimdprintf(sess, 1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n");
a25832e6 97 newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
b8d0da45 98 }
9de3ca7e 99
b69540e3 100 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR) {
101 /* assign seqnum */
102 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
103 }
9de3ca7e 104 /* set some more fields */
5b79dc93 105 newpacket->lock = 1; /* lock */
106 newpacket->sent = 0; /* not sent yet */
107 newpacket->next = NULL; /* always last */
9de3ca7e 108
b8d0da45 109 /* see overhead note in aim_rxqueue counterpart */
110 if (sess->queue_outgoing == NULL) {
5b79dc93 111 sess->queue_outgoing = newpacket;
b8d0da45 112 } else {
113 for (cur = sess->queue_outgoing;
114 cur->next;
115 cur = cur->next)
116 ;
5b79dc93 117 cur->next = newpacket;
b8d0da45 118 }
9de3ca7e 119
5b79dc93 120 newpacket->lock = 0; /* unlock so it can be sent */
9de3ca7e 121
9de3ca7e 122 return 0;
123}
124
e88ba395 125/*
126 * aim_tx_enqueue__immediate()
127 *
128 * Parallel to aim_tx_enqueue__queuebased, however, this bypasses
129 * the whole queue mess when you want immediate writes to happen.
130 *
131 * Basically the same as its __queuebased couterpart, however
132 * instead of doing a list append, it just calls aim_tx_sendframe()
133 * right here.
134 *
135 */
b8c79ca7 136static int aim_tx_enqueue__immediate(struct aim_session_t *sess, struct command_tx_struct *newpacket)
e88ba395 137{
138 if (newpacket->conn == NULL) {
646c6b52 139 faimdprintf(sess, 1, "aim_tx_enqueue: ERROR: packet has no connection\n");
e88ba395 140 if (newpacket->data)
141 free(newpacket->data);
142 free(newpacket);
143 return -1;
144 }
145
b69540e3 146 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR)
147 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
e88ba395 148
149 newpacket->lock = 1; /* lock */
150 newpacket->sent = 0; /* not sent yet */
151
68ac63c2 152 aim_tx_sendframe(sess, newpacket);
e88ba395 153
154 if (newpacket->data)
155 free(newpacket->data);
156 free(newpacket);
157
158 return 0;
159}
160
b8c79ca7 161faim_export int aim_tx_setenqueue(struct aim_session_t *sess, int what, int (*func)(struct aim_session_t *, struct command_tx_struct *))
162{
163 if (!sess)
164 return -1;
165
166 if (what == AIM_TX_QUEUED)
167 sess->tx_enqueue = &aim_tx_enqueue__queuebased;
168 else if (what == AIM_TX_IMMEDIATE)
169 sess->tx_enqueue = &aim_tx_enqueue__immediate;
170 else if (what == AIM_TX_USER) {
171 if (!func)
172 return -1;
173 sess->tx_enqueue = func;
174 } else
175 return -1; /* unknown action */
176
177 return 0;
178}
179
22517493 180faim_internal int aim_tx_enqueue(struct aim_session_t *sess, struct command_tx_struct *command)
181{
182 /*
183 * If we want to send a connection thats inprogress, we have to force
184 * them to use the queue based version. Otherwise, use whatever they
185 * want.
186 */
187 if (command && command->conn && (command->conn->status & AIM_CONN_STATUS_INPROGRESS)) {
188 return aim_tx_enqueue__queuebased(sess, command);
189 }
190 return (*sess->tx_enqueue)(sess, command);
191}
192
9de3ca7e 193/*
a25832e6 194 * aim_get_next_txseqnum()
195 *
196 * This increments the tx command count, and returns the seqnum
197 * that should be stamped on the next FLAP packet sent. This is
198 * normally called during the final step of packet preparation
199 * before enqueuement (in aim_tx_enqueue()).
200 *
9de3ca7e 201 */
78b3fb13 202faim_internal unsigned int aim_get_next_txseqnum(struct aim_conn_t *conn)
9de3ca7e 203{
df5a99fb 204 u_int ret;
205
206 faim_mutex_lock(&conn->seqnum_lock);
207 ret = ++conn->seqnum;
208 faim_mutex_unlock(&conn->seqnum_lock);
209 return ret;
9de3ca7e 210}
211
9de3ca7e 212/*
a25832e6 213 * aim_tx_flushqueue()
214 *
215 * This the function is responsable for putting the queued commands
216 * onto the wire. This function is critical to the operation of
217 * the queue and therefore is the most prone to brokenness. It
218 * seems to be working quite well at this point.
219 *
220 * Procedure:
221 * 1) Traverse the list, only operate on commands that are unlocked
222 * and haven't been sent yet.
223 * 2) Lock the struct
224 * 3) Allocate a temporary buffer to store the finished, fully
225 * processed packet in.
226 * 4) Build the packet from the command_tx_struct data.
227 * 5) Write the packet to the socket.
228 * 6) If success, mark the packet sent, if fail report failure, do NOT
229 * mark the packet sent (so it will not get purged and therefore
230 * be attempted again on next call).
231 * 7) Unlock the struct.
232 * 8) Free the temp buffer
233 * 9) Step to next struct in list and go back to 1.
234 *
9de3ca7e 235 */
78b3fb13 236faim_internal int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx_struct *cur)
e88ba395 237{
b69540e3 238 int buflen = 0;
239 unsigned char *curPacket;
e88ba395 240
241 if (!cur)
242 return -1; /* fatal */
243
244 cur->lock = 1; /* lock the struct */
245
b69540e3 246 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR)
247 buflen = cur->commandlen + 6;
248 else if (cur->hdrtype == AIM_FRAMETYPE_OFT)
249 buflen = cur->hdr.oft.hdr2len + 8;
250 else {
251 cur->lock = 0;
252 return -1;
253 }
254
e88ba395 255 /* allocate full-packet buffer */
b69540e3 256 if (!(curPacket = (unsigned char *) malloc(buflen))) {
257 cur->lock = 0;
258 return -1;
259 }
e88ba395 260
b69540e3 261 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR) {
262 /* command byte */
263 curPacket[0] = 0x2a;
e88ba395 264
b69540e3 265 /* type/family byte */
266 curPacket[1] = cur->hdr.oscar.type;
e88ba395 267
b69540e3 268 /* bytes 3+4: word: FLAP sequence number */
269 aimutil_put16(curPacket+2, cur->hdr.oscar.seqnum);
e88ba395 270
b69540e3 271 /* bytes 5+6: word: SNAC len */
272 aimutil_put16(curPacket+4, cur->commandlen);
e88ba395 273
b69540e3 274 /* bytes 7 and on: raw: SNAC data */ /* XXX: ye gods! get rid of this! */
275 memcpy(&(curPacket[6]), cur->data, cur->commandlen);
276
277 } else if (cur->hdrtype == AIM_FRAMETYPE_OFT) {
278 int z = 0;
279
871e2fd0 280 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[0]);
281 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[1]);
282 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[2]);
283 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[3]);
b69540e3 284
285 z += aimutil_put16(curPacket+z, cur->hdr.oft.hdr2len + 8);
286 z += aimutil_put16(curPacket+z, cur->hdr.oft.type);
287
288 memcpy(curPacket+z, cur->hdr.oft.hdr2, cur->hdr.oft.hdr2len);
289 }
290
291 /*
292 * For OSCAR, a full image of the raw packet data now in curPacket.
293 * For OFT, an image of just the bloated header is in curPacket,
294 * since OFT allows us to do the data in a different write (yay!).
295 */
e88ba395 296 faim_mutex_lock(&cur->conn->active);
5ac21963 297 if (send(cur->conn->fd, curPacket, buflen, 0) != buflen) {
e88ba395 298 faim_mutex_unlock(&cur->conn->active);
68ac63c2 299 cur->sent = 1;
9d2a3582 300 aim_conn_close(cur->conn);
68ac63c2 301 return 0; /* bail out */
e88ba395 302 }
b69540e3 303
304 if ((cur->hdrtype == AIM_FRAMETYPE_OFT) && cur->commandlen) {
37ee990e 305 int curposi;
306 for(curposi = 0; curposi < cur->commandlen; curposi++)
646c6b52 307 faimdprintf(sess, 0, "%02x ", cur->data[curposi]);
37ee990e 308
5ac21963 309 if (send(cur->conn->fd, cur->data, cur->commandlen, 0) != (int)cur->commandlen) {
b69540e3 310 /*
311 * Theres nothing we can do about this since we've already sent the
312 * header! The connection is unstable.
313 */
37ee990e 314 faim_mutex_unlock(&cur->conn->active);
315 cur->sent = 1;
316 aim_conn_close(cur->conn);
317 return 0; /* bail out */
b69540e3 318 }
37ee990e 319
b69540e3 320 }
321
322 cur->sent = 1; /* mark the struct as sent */
323 cur->conn->lastactivity = time(NULL);
324
e88ba395 325 faim_mutex_unlock(&cur->conn->active);
326
646c6b52 327 if (sess->debug >= 2) {
328 int i;
329
330 faimdprintf(sess, 2, "\nOutgoing packet: (only valid for OSCAR)");
331 for (i = 0; i < buflen; i++) {
332 if (!(i % 8))
333 faimdprintf(sess, 2, "\n\t");
334 faimdprintf(sess, 2, "0x%02x ", curPacket[i]);
e88ba395 335 }
646c6b52 336 faimdprintf(sess, 2, "\n");
e88ba395 337 }
646c6b52 338
e88ba395 339 cur->lock = 0; /* unlock the struct */
646c6b52 340
e88ba395 341 free(curPacket); /* free up full-packet buffer */
342
343 return 1; /* success */
344}
345
78b3fb13 346faim_export int aim_tx_flushqueue(struct aim_session_t *sess)
9de3ca7e 347{
b8d0da45 348 struct command_tx_struct *cur;
e88ba395 349
b8d0da45 350 if (sess->queue_outgoing == NULL)
351 return 0;
a25832e6 352
646c6b52 353 faimdprintf(sess, 2, "beginning txflush...\n");
b8d0da45 354 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
355 /* only process if its unlocked and unsent */
356 if (!cur->lock && !cur->sent) {
a25832e6 357
22517493 358 if (cur->conn && (cur->conn->status & AIM_CONN_STATUS_INPROGRESS))
359 continue;
360
b8d0da45 361 /*
362 * And now for the meager attempt to force transmit
363 * latency and avoid missed messages.
364 */
365 if ((cur->conn->lastactivity + cur->conn->forcedlatency) >= time(NULL)) {
366 /* FIXME FIXME -- should be a break! we dont want to block the upper layers */
367 sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL));
368 }
a25832e6 369
37ee990e 370 /* XXX XXX XXX this should call the custom "queuing" function!! */
68ac63c2 371 if (aim_tx_sendframe(sess, cur) == -1)
e88ba395 372 break;
9de3ca7e 373 }
b8d0da45 374 }
9de3ca7e 375
376 /* purge sent commands from queue */
a25832e6 377 aim_tx_purgequeue(sess);
9de3ca7e 378
379 return 0;
380}
381
382/*
a25832e6 383 * aim_tx_purgequeue()
384 *
385 * This is responsable for removing sent commands from the transmit
386 * queue. This is not a required operation, but it of course helps
387 * reduce memory footprint at run time!
388 *
9de3ca7e 389 */
78b3fb13 390faim_export void aim_tx_purgequeue(struct aim_session_t *sess)
9de3ca7e 391{
b8d0da45 392 struct command_tx_struct *cur = NULL;
393 struct command_tx_struct *tmp;
394
a25832e6 395 if (sess->queue_outgoing == NULL)
b8d0da45 396 return;
397
398 if (sess->queue_outgoing->next == NULL) {
399 if (!sess->queue_outgoing->lock && sess->queue_outgoing->sent) {
400 tmp = sess->queue_outgoing;
401 sess->queue_outgoing = NULL;
b69540e3 402 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
403 free(tmp->hdr.oft.hdr2);
b8d0da45 404 free(tmp->data);
405 free(tmp);
9de3ca7e 406 }
b8d0da45 407 return;
408 }
409
410 for(cur = sess->queue_outgoing; cur->next != NULL; ) {
411 if (!cur->next->lock && cur->next->sent) {
412 tmp = cur->next;
413 cur->next = tmp->next;
b69540e3 414 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
415 free(tmp->hdr.oft.hdr2);
b8d0da45 416 free(tmp->data);
417 free(tmp);
418 }
419 cur = cur->next;
9de3ca7e 420
b8d0da45 421 /*
422 * Be careful here. Because of the way we just
423 * manipulated the pointer, cur may be NULL and
424 * the for() will segfault doing the check unless
425 * we find this case first.
426 */
427 if (cur == NULL)
428 break;
429 }
430 return;
9de3ca7e 431}
This page took 0.145445 seconds and 5 git commands to generate.