4 * Herein lies all the mangement routines for the transmit (Tx) queue.
12 #include <sys/socket.h>
16 * Allocate a new tx frame.
18 * This is more for looks than anything else.
20 * Right now, that is. If/when we implement a pool of transmit
21 * frames, this will become the request-an-unused-frame part.
23 * framing = AIM_FRAMETYPE_OFT/OSCAR
24 * chan = channel for OSCAR, hdrtype for OFT
27 faim_internal struct command_tx_struct *aim_tx_new(unsigned char framing, int chan, struct aim_conn_t *conn, int datalen)
29 struct command_tx_struct *newtx;
32 printf("aim_tx_new: ERROR: no connection specified\n");
36 newtx = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct));
39 memset(newtx, 0, sizeof(struct command_tx_struct));
44 newtx->data = (unsigned char *)malloc(datalen);
45 newtx->commandlen = datalen;
49 newtx->hdrtype = framing;
50 if (newtx->hdrtype == AIM_FRAMETYPE_OSCAR) {
51 newtx->hdr.oscar.type = chan;
52 } else if (newtx->hdrtype == AIM_FRAMETYPE_OFT) {
53 newtx->hdr.oft.type = chan;
54 newtx->hdr.oft.hdr2len = 0; /* this will get setup by caller */
56 printf("tx_new: unknown framing\n");
63 * aim_tx_enqeue__queuebased()
65 * The overall purpose here is to enqueue the passed in command struct
66 * into the outgoing (tx) queue. Basically...
67 * 1) Make a scope-irrelevent copy of the struct
69 * 3) Mark as not-sent-yet
70 * 4) Enqueue the struct into the list
71 * 5) Unlock the struct once it's linked in
74 * Note that this is only used when doing queue-based transmitting;
75 * that is, when sess->tx_enqueue is set to &aim_tx_enqueue__queuebased.
78 faim_internal int aim_tx_enqueue__queuebased(struct aim_session_t *sess,
79 struct command_tx_struct *newpacket)
81 struct command_tx_struct *cur;
83 if (newpacket->conn == NULL) {
84 faimdprintf(1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n");
85 newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
88 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR) {
90 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
92 /* set some more fields */
93 newpacket->lock = 1; /* lock */
94 newpacket->sent = 0; /* not sent yet */
95 newpacket->next = NULL; /* always last */
97 /* see overhead note in aim_rxqueue counterpart */
98 if (sess->queue_outgoing == NULL) {
99 sess->queue_outgoing = newpacket;
101 for (cur = sess->queue_outgoing;
105 cur->next = newpacket;
108 newpacket->lock = 0; /* unlock so it can be sent */
111 faimdprintf(2, "calling aim_tx_printqueue()\n");
112 aim_tx_printqueue(sess);
113 faimdprintf(2, "back from aim_tx_printqueue()\n");
120 * aim_tx_enqueue__immediate()
122 * Parallel to aim_tx_enqueue__queuebased, however, this bypasses
123 * the whole queue mess when you want immediate writes to happen.
125 * Basically the same as its __queuebased couterpart, however
126 * instead of doing a list append, it just calls aim_tx_sendframe()
130 faim_internal int aim_tx_enqueue__immediate(struct aim_session_t *sess, struct command_tx_struct *newpacket)
132 if (newpacket->conn == NULL) {
133 faimdprintf(1, "aim_tx_enqueue: ERROR: packet has no connection\n");
135 free(newpacket->data);
140 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR)
141 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
143 newpacket->lock = 1; /* lock */
144 newpacket->sent = 0; /* not sent yet */
146 aim_tx_sendframe(sess, newpacket);
149 free(newpacket->data);
155 faim_internal int aim_tx_enqueue(struct aim_session_t *sess, struct command_tx_struct *command)
158 * If we want to send a connection thats inprogress, we have to force
159 * them to use the queue based version. Otherwise, use whatever they
162 if (command && command->conn && (command->conn->status & AIM_CONN_STATUS_INPROGRESS)) {
163 return aim_tx_enqueue__queuebased(sess, command);
165 return (*sess->tx_enqueue)(sess, command);
169 * aim_get_next_txseqnum()
171 * This increments the tx command count, and returns the seqnum
172 * that should be stamped on the next FLAP packet sent. This is
173 * normally called during the final step of packet preparation
174 * before enqueuement (in aim_tx_enqueue()).
177 faim_internal unsigned int aim_get_next_txseqnum(struct aim_conn_t *conn)
181 faim_mutex_lock(&conn->seqnum_lock);
182 ret = ++conn->seqnum;
183 faim_mutex_unlock(&conn->seqnum_lock);
188 * aim_tx_printqueue()
190 * This is basically for debuging purposes only. It dumps all the
191 * records in the tx queue and their current status. Very helpful
192 * if the queue isn't working quite right.
196 faim_internal int aim_tx_printqueue(struct aim_session_t *sess)
198 struct command_tx_struct *cur;
200 faimdprintf(2, "\ncurrent aim_queue_outgoing...\n");
201 faimdprintf(2, "\ttype seqnum len lock sent\n");
203 if (sess->queue_outgoing == NULL)
204 faimdprintf(2, "aim_tx_flushqueue(): queue empty");
206 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
207 faimdprintf(2, "\t %2x %2x %4x %4x %1d %1d\n",
209 (cur->hdrtype==AIM_FRAMETYPE_OFT)?cur->hdr.oft.type:cur->hdr.oscar.type,
210 (cur->hdrtype==AIM_FRAMETYPE_OSCAR)?cur->hdr.oscar.seqnum:0,
211 cur->commandlen, cur->lock,
216 faimdprintf(2, "\n(done printing queue)\n");
223 * aim_tx_flushqueue()
225 * This the function is responsable for putting the queued commands
226 * onto the wire. This function is critical to the operation of
227 * the queue and therefore is the most prone to brokenness. It
228 * seems to be working quite well at this point.
231 * 1) Traverse the list, only operate on commands that are unlocked
232 * and haven't been sent yet.
234 * 3) Allocate a temporary buffer to store the finished, fully
235 * processed packet in.
236 * 4) Build the packet from the command_tx_struct data.
237 * 5) Write the packet to the socket.
238 * 6) If success, mark the packet sent, if fail report failure, do NOT
239 * mark the packet sent (so it will not get purged and therefore
240 * be attempted again on next call).
241 * 7) Unlock the struct.
242 * 8) Free the temp buffer
243 * 9) Step to next struct in list and go back to 1.
246 faim_internal int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx_struct *cur)
249 unsigned char *curPacket;
252 return -1; /* fatal */
254 cur->lock = 1; /* lock the struct */
256 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR)
257 buflen = cur->commandlen + 6;
258 else if (cur->hdrtype == AIM_FRAMETYPE_OFT)
259 buflen = cur->hdr.oft.hdr2len + 8;
265 /* allocate full-packet buffer */
266 if (!(curPacket = (unsigned char *) malloc(buflen))) {
271 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR) {
275 /* type/family byte */
276 curPacket[1] = cur->hdr.oscar.type;
278 /* bytes 3+4: word: FLAP sequence number */
279 aimutil_put16(curPacket+2, cur->hdr.oscar.seqnum);
281 /* bytes 5+6: word: SNAC len */
282 aimutil_put16(curPacket+4, cur->commandlen);
284 /* bytes 7 and on: raw: SNAC data */ /* XXX: ye gods! get rid of this! */
285 memcpy(&(curPacket[6]), cur->data, cur->commandlen);
287 } else if (cur->hdrtype == AIM_FRAMETYPE_OFT) {
290 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[0]);
291 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[1]);
292 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[2]);
293 z += aimutil_put8(curPacket+z, cur->hdr.oft.magic[3]);
295 z += aimutil_put16(curPacket+z, cur->hdr.oft.hdr2len + 8);
296 z += aimutil_put16(curPacket+z, cur->hdr.oft.type);
298 memcpy(curPacket+z, cur->hdr.oft.hdr2, cur->hdr.oft.hdr2len);
302 * For OSCAR, a full image of the raw packet data now in curPacket.
303 * For OFT, an image of just the bloated header is in curPacket,
304 * since OFT allows us to do the data in a different write (yay!).
306 faim_mutex_lock(&cur->conn->active);
307 if (send(cur->conn->fd, curPacket, buflen, 0) != buflen) {
308 faim_mutex_unlock(&cur->conn->active);
310 aim_conn_close(cur->conn);
311 return 0; /* bail out */
314 if ((cur->hdrtype == AIM_FRAMETYPE_OFT) && cur->commandlen) {
316 for(curposi = 0; curposi < cur->commandlen; curposi++)
317 printf("%02x ", cur->data[curposi]);
319 if (send(cur->conn->fd, cur->data, cur->commandlen, 0) != (int)cur->commandlen) {
321 * Theres nothing we can do about this since we've already sent the
322 * header! The connection is unstable.
324 faim_mutex_unlock(&cur->conn->active);
326 aim_conn_close(cur->conn);
327 return 0; /* bail out */
332 cur->sent = 1; /* mark the struct as sent */
333 cur->conn->lastactivity = time(NULL);
335 faim_mutex_unlock(&cur->conn->active);
338 faimdprintf(2, "\nPacket:");
339 for (i = 0; i < (cur->commandlen + 6); i++) {
341 faimdprintf(2, "\n\t");
343 if (curPacket[i] >= ' ' && curPacket[i]<127) {
344 faimdprintf(2, "%c=%02x ", curPacket[i], curPacket[i]);
346 faimdprintf(2, "0x%2x ", curPacket[i]);
349 faimdprintf(2, "\n");
351 cur->lock = 0; /* unlock the struct */
352 free(curPacket); /* free up full-packet buffer */
354 return 1; /* success */
357 faim_export int aim_tx_flushqueue(struct aim_session_t *sess)
359 struct command_tx_struct *cur;
361 if (sess->queue_outgoing == NULL)
364 faimdprintf(2, "beginning txflush...\n");
365 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
366 /* only process if its unlocked and unsent */
367 if (!cur->lock && !cur->sent) {
369 if (cur->conn && (cur->conn->status & AIM_CONN_STATUS_INPROGRESS))
373 * And now for the meager attempt to force transmit
374 * latency and avoid missed messages.
376 if ((cur->conn->lastactivity + cur->conn->forcedlatency) >= time(NULL)) {
377 /* FIXME FIXME -- should be a break! we dont want to block the upper layers */
378 sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL));
381 /* XXX XXX XXX this should call the custom "queuing" function!! */
382 if (aim_tx_sendframe(sess, cur) == -1)
387 /* purge sent commands from queue */
388 aim_tx_purgequeue(sess);
394 * aim_tx_purgequeue()
396 * This is responsable for removing sent commands from the transmit
397 * queue. This is not a required operation, but it of course helps
398 * reduce memory footprint at run time!
401 faim_export void aim_tx_purgequeue(struct aim_session_t *sess)
403 struct command_tx_struct *cur = NULL;
404 struct command_tx_struct *tmp;
406 if (sess->queue_outgoing == NULL)
409 if (sess->queue_outgoing->next == NULL) {
410 if (!sess->queue_outgoing->lock && sess->queue_outgoing->sent) {
411 tmp = sess->queue_outgoing;
412 sess->queue_outgoing = NULL;
413 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
414 free(tmp->hdr.oft.hdr2);
421 for(cur = sess->queue_outgoing; cur->next != NULL; ) {
422 if (!cur->next->lock && cur->next->sent) {
424 cur->next = tmp->next;
425 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
426 free(tmp->hdr.oft.hdr2);
433 * Be careful here. Because of the way we just
434 * manipulated the pointer, cur may be NULL and
435 * the for() will segfault doing the check unless
436 * we find this case first.