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