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(struct aim_session_t *sess, struct aim_conn_t *conn, unsigned char framing, int chan, int datalen)
29 struct command_tx_struct *newtx;
32 faimdprintf(sess, 0, "aim_tx_new: ERROR: no connection specified\n");
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");
43 if (framing != AIM_FRAMETYPE_OSCAR) {
44 faimdprintf(sess, 0, "aim_tx_new: attempted to allocate inappropriate frame type for FLAP connection\n");
49 newtx = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct));
52 memset(newtx, 0, sizeof(struct command_tx_struct));
57 newtx->data = (unsigned char *)malloc(datalen);
58 newtx->commandlen = datalen;
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 */
69 faimdprintf(sess, 0, "tx_new: unknown framing\n");
76 * aim_tx_enqeue__queuebased()
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
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
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.
91 static int aim_tx_enqueue__queuebased(struct aim_session_t *sess, struct command_tx_struct *newpacket)
93 struct command_tx_struct *cur;
95 if (newpacket->conn == NULL) {
96 faimdprintf(sess, 1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n");
97 newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
100 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR) {
102 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
104 /* set some more fields */
105 newpacket->lock = 1; /* lock */
106 newpacket->sent = 0; /* not sent yet */
107 newpacket->next = NULL; /* always last */
109 /* see overhead note in aim_rxqueue counterpart */
110 if (sess->queue_outgoing == NULL) {
111 sess->queue_outgoing = newpacket;
113 for (cur = sess->queue_outgoing;
117 cur->next = newpacket;
120 newpacket->lock = 0; /* unlock so it can be sent */
126 * aim_tx_enqueue__immediate()
128 * Parallel to aim_tx_enqueue__queuebased, however, this bypasses
129 * the whole queue mess when you want immediate writes to happen.
131 * Basically the same as its __queuebased couterpart, however
132 * instead of doing a list append, it just calls aim_tx_sendframe()
136 static int aim_tx_enqueue__immediate(struct aim_session_t *sess, struct command_tx_struct *newpacket)
138 if (newpacket->conn == NULL) {
139 faimdprintf(sess, 1, "aim_tx_enqueue: ERROR: packet has no connection\n");
141 free(newpacket->data);
146 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR)
147 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
149 newpacket->lock = 1; /* lock */
150 newpacket->sent = 0; /* not sent yet */
152 aim_tx_sendframe(sess, newpacket);
155 free(newpacket->data);
161 faim_export int aim_tx_setenqueue(struct aim_session_t *sess, int what, int (*func)(struct aim_session_t *, struct command_tx_struct *))
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) {
173 sess->tx_enqueue = func;
175 return -1; /* unknown action */
180 faim_internal int aim_tx_enqueue(struct aim_session_t *sess, struct command_tx_struct *command)
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
187 if (command && command->conn && (command->conn->status & AIM_CONN_STATUS_INPROGRESS)) {
188 return aim_tx_enqueue__queuebased(sess, command);
190 return (*sess->tx_enqueue)(sess, command);
194 * aim_get_next_txseqnum()
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()).
202 faim_internal unsigned int aim_get_next_txseqnum(struct aim_conn_t *conn)
206 faim_mutex_lock(&conn->seqnum_lock);
207 ret = ++conn->seqnum;
208 faim_mutex_unlock(&conn->seqnum_lock);
213 * aim_tx_flushqueue()
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.
221 * 1) Traverse the list, only operate on commands that are unlocked
222 * and haven't been sent yet.
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.
236 faim_internal int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx_struct *cur)
239 unsigned char *curPacket;
242 return -1; /* fatal */
244 cur->lock = 1; /* lock the struct */
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;
255 /* allocate full-packet buffer */
256 if (!(curPacket = (unsigned char *) malloc(buflen))) {
261 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR) {
265 /* type/family byte */
266 curPacket[1] = cur->hdr.oscar.type;
268 /* bytes 3+4: word: FLAP sequence number */
269 aimutil_put16(curPacket+2, cur->hdr.oscar.seqnum);
271 /* bytes 5+6: word: SNAC len */
272 aimutil_put16(curPacket+4, cur->commandlen);
274 /* bytes 7 and on: raw: SNAC data */ /* XXX: ye gods! get rid of this! */
275 memcpy(&(curPacket[6]), cur->data, cur->commandlen);
277 } else if (cur->hdrtype == AIM_FRAMETYPE_OFT) {
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]);
285 z += aimutil_put16(curPacket+z, cur->hdr.oft.hdr2len + 8);
286 z += aimutil_put16(curPacket+z, cur->hdr.oft.type);
288 memcpy(curPacket+z, cur->hdr.oft.hdr2, cur->hdr.oft.hdr2len);
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!).
296 faim_mutex_lock(&cur->conn->active);
297 if (send(cur->conn->fd, curPacket, buflen, 0) != buflen) {
298 faim_mutex_unlock(&cur->conn->active);
300 aim_conn_close(cur->conn);
301 return 0; /* bail out */
304 if ((cur->hdrtype == AIM_FRAMETYPE_OFT) && cur->commandlen) {
306 for(curposi = 0; curposi < cur->commandlen; curposi++)
307 faimdprintf(sess, 0, "%02x ", cur->data[curposi]);
309 if (send(cur->conn->fd, cur->data, cur->commandlen, 0) != (int)cur->commandlen) {
311 * Theres nothing we can do about this since we've already sent the
312 * header! The connection is unstable.
314 faim_mutex_unlock(&cur->conn->active);
316 aim_conn_close(cur->conn);
317 return 0; /* bail out */
322 cur->sent = 1; /* mark the struct as sent */
323 cur->conn->lastactivity = time(NULL);
325 faim_mutex_unlock(&cur->conn->active);
327 if (sess->debug >= 2) {
330 faimdprintf(sess, 2, "\nOutgoing packet: (only valid for OSCAR)");
331 for (i = 0; i < buflen; i++) {
333 faimdprintf(sess, 2, "\n\t");
334 faimdprintf(sess, 2, "0x%02x ", curPacket[i]);
336 faimdprintf(sess, 2, "\n");
339 cur->lock = 0; /* unlock the struct */
341 free(curPacket); /* free up full-packet buffer */
343 return 1; /* success */
346 faim_export int aim_tx_flushqueue(struct aim_session_t *sess)
348 struct command_tx_struct *cur;
350 if (sess->queue_outgoing == NULL)
353 faimdprintf(sess, 2, "beginning txflush...\n");
354 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
355 /* only process if its unlocked and unsent */
356 if (!cur->lock && !cur->sent) {
358 if (cur->conn && (cur->conn->status & AIM_CONN_STATUS_INPROGRESS))
362 * And now for the meager attempt to force transmit
363 * latency and avoid missed messages.
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));
370 /* XXX XXX XXX this should call the custom "queuing" function!! */
371 if (aim_tx_sendframe(sess, cur) == -1)
376 /* purge sent commands from queue */
377 aim_tx_purgequeue(sess);
383 * aim_tx_purgequeue()
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!
390 faim_export void aim_tx_purgequeue(struct aim_session_t *sess)
392 struct command_tx_struct *cur = NULL;
393 struct command_tx_struct *tmp;
395 if (sess->queue_outgoing == NULL)
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;
402 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
403 free(tmp->hdr.oft.hdr2);
410 for(cur = sess->queue_outgoing; cur->next != NULL; ) {
411 if (!cur->next->lock && cur->next->sent) {
413 cur->next = tmp->next;
414 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
415 free(tmp->hdr.oft.hdr2);
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.