4 * Herein lies all the mangement routines for the transmit (Tx) queue.
11 #include <sys/socket.h>
15 * Allocate a new tx frame.
17 * This is more for looks than anything else.
19 * Right now, that is. If/when we implement a pool of transmit
20 * frames, this will become the request-an-unused-frame part.
22 * framing = AIM_FRAMETYPE_OFT/OSCAR
23 * chan = channel for OSCAR, hdrtype for OFT
26 faim_internal struct command_tx_struct *aim_tx_new(unsigned char framing, int chan, struct aim_conn_t *conn, int datalen)
28 struct command_tx_struct *new;
31 printf("aim_tx_new: ERROR: no connection specified\n");
35 new = (struct command_tx_struct *)malloc(sizeof(struct command_tx_struct));
38 memset(new, 0, sizeof(struct command_tx_struct));
43 new->data = (u_char *)malloc(datalen);
44 new->commandlen = datalen;
48 new->hdrtype = framing;
49 if (new->hdrtype == AIM_FRAMETYPE_OSCAR) {
50 new->hdr.oscar.type = chan;
51 } else if (new->hdrtype == AIM_FRAMETYPE_OFT) {
52 new->hdr.oft.type = chan;
53 new->hdr.oft.hdr2len = 0; /* this will get setup by caller */
55 printf("tx_new: unknown framing\n");
62 * aim_tx_enqeue__queuebased()
64 * The overall purpose here is to enqueue the passed in command struct
65 * into the outgoing (tx) queue. Basically...
66 * 1) Make a scope-irrelevent copy of the struct
68 * 3) Mark as not-sent-yet
69 * 4) Enqueue the struct into the list
70 * 5) Unlock the struct once it's linked in
73 * Note that this is only used when doing queue-based transmitting;
74 * that is, when sess->tx_enqueue is set to &aim_tx_enqueue__queuebased.
77 faim_internal int aim_tx_enqueue__queuebased(struct aim_session_t *sess,
78 struct command_tx_struct *newpacket)
80 struct command_tx_struct *cur;
82 if (newpacket->conn == NULL) {
83 faimdprintf(1, "aim_tx_enqueue: WARNING: enqueueing packet with no connecetion\n");
84 newpacket->conn = aim_getconn_type(sess, AIM_CONN_TYPE_BOS);
87 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR) {
89 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
91 /* set some more fields */
92 newpacket->lock = 1; /* lock */
93 newpacket->sent = 0; /* not sent yet */
94 newpacket->next = NULL; /* always last */
96 /* see overhead note in aim_rxqueue counterpart */
97 if (sess->queue_outgoing == NULL) {
98 sess->queue_outgoing = newpacket;
100 for (cur = sess->queue_outgoing;
104 cur->next = newpacket;
107 newpacket->lock = 0; /* unlock so it can be sent */
110 faimdprintf(2, "calling aim_tx_printqueue()\n");
111 aim_tx_printqueue(sess);
112 faimdprintf(2, "back from aim_tx_printqueue()\n");
119 * aim_tx_enqueue__immediate()
121 * Parallel to aim_tx_enqueue__queuebased, however, this bypasses
122 * the whole queue mess when you want immediate writes to happen.
124 * Basically the same as its __queuebased couterpart, however
125 * instead of doing a list append, it just calls aim_tx_sendframe()
129 faim_internal int aim_tx_enqueue__immediate(struct aim_session_t *sess, struct command_tx_struct *newpacket)
131 if (newpacket->conn == NULL) {
132 faimdprintf(1, "aim_tx_enqueue: ERROR: packet has no connection\n");
134 free(newpacket->data);
139 if (newpacket->hdrtype == AIM_FRAMETYPE_OSCAR)
140 newpacket->hdr.oscar.seqnum = aim_get_next_txseqnum(newpacket->conn);
142 newpacket->lock = 1; /* lock */
143 newpacket->sent = 0; /* not sent yet */
145 aim_tx_sendframe(sess, newpacket);
148 free(newpacket->data);
155 * aim_get_next_txseqnum()
157 * This increments the tx command count, and returns the seqnum
158 * that should be stamped on the next FLAP packet sent. This is
159 * normally called during the final step of packet preparation
160 * before enqueuement (in aim_tx_enqueue()).
163 faim_internal unsigned int aim_get_next_txseqnum(struct aim_conn_t *conn)
167 faim_mutex_lock(&conn->seqnum_lock);
168 ret = ++conn->seqnum;
169 faim_mutex_unlock(&conn->seqnum_lock);
174 * aim_tx_printqueue()
176 * This is basically for debuging purposes only. It dumps all the
177 * records in the tx queue and their current status. Very helpful
178 * if the queue isn't working quite right.
182 faim_internal int aim_tx_printqueue(struct aim_session_t *sess)
184 struct command_tx_struct *cur;
186 faimdprintf(2, "\ncurrent aim_queue_outgoing...\n");
187 faimdprintf(2, "\ttype seqnum len lock sent\n");
189 if (sess->queue_outgoing == NULL)
190 faimdprintf(2, "aim_tx_flushqueue(): queue empty");
192 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
193 faimdprintf(2, "\t %2x %2x %4x %4x %1d %1d\n",
195 (cur->hdrtype==AIM_FRAMETYPE_OFT)?cur->hdr.oft.type:cur->hdr.oscar.type,
196 (cur->hdrtype==AIM_FRAMETYPE_OSCAR)?cur->seqnum:0,
197 cur->commandlen, cur->lock,
202 faimdprintf(2, "\n(done printing queue)\n");
209 * aim_tx_flushqueue()
211 * This the function is responsable for putting the queued commands
212 * onto the wire. This function is critical to the operation of
213 * the queue and therefore is the most prone to brokenness. It
214 * seems to be working quite well at this point.
217 * 1) Traverse the list, only operate on commands that are unlocked
218 * and haven't been sent yet.
220 * 3) Allocate a temporary buffer to store the finished, fully
221 * processed packet in.
222 * 4) Build the packet from the command_tx_struct data.
223 * 5) Write the packet to the socket.
224 * 6) If success, mark the packet sent, if fail report failure, do NOT
225 * mark the packet sent (so it will not get purged and therefore
226 * be attempted again on next call).
227 * 7) Unlock the struct.
228 * 8) Free the temp buffer
229 * 9) Step to next struct in list and go back to 1.
232 faim_internal int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx_struct *cur)
235 unsigned char *curPacket;
238 return -1; /* fatal */
240 cur->lock = 1; /* lock the struct */
242 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR)
243 buflen = cur->commandlen + 6;
244 else if (cur->hdrtype == AIM_FRAMETYPE_OFT)
245 buflen = cur->hdr.oft.hdr2len + 8;
251 /* allocate full-packet buffer */
252 if (!(curPacket = (unsigned char *) malloc(buflen))) {
257 if (cur->hdrtype == AIM_FRAMETYPE_OSCAR) {
261 /* type/family byte */
262 curPacket[1] = cur->hdr.oscar.type;
264 /* bytes 3+4: word: FLAP sequence number */
265 aimutil_put16(curPacket+2, cur->hdr.oscar.seqnum);
267 /* bytes 5+6: word: SNAC len */
268 aimutil_put16(curPacket+4, cur->commandlen);
270 /* bytes 7 and on: raw: SNAC data */ /* XXX: ye gods! get rid of this! */
271 memcpy(&(curPacket[6]), cur->data, cur->commandlen);
273 } else if (cur->hdrtype == AIM_FRAMETYPE_OFT) {
276 z += aimutil_put8(curPacket+z, 0x4f);
277 z += aimutil_put8(curPacket+z, 0x44);
278 z += aimutil_put8(curPacket+z, 0x43);
279 z += aimutil_put8(curPacket+z, 0x32);
281 z += aimutil_put16(curPacket+z, cur->hdr.oft.hdr2len + 8);
282 z += aimutil_put16(curPacket+z, cur->hdr.oft.type);
284 memcpy(curPacket+z, cur->hdr.oft.hdr2, cur->hdr.oft.hdr2len);
288 * For OSCAR, a full image of the raw packet data now in curPacket.
289 * For OFT, an image of just the bloated header is in curPacket,
290 * since OFT allows us to do the data in a different write (yay!).
292 faim_mutex_lock(&cur->conn->active);
293 if (send(cur->conn->fd, curPacket, buflen, 0) != buflen) {
294 faim_mutex_unlock(&cur->conn->active);
296 aim_conn_kill(sess, &cur->conn);
297 return 0; /* bail out */
300 if ((cur->hdrtype == AIM_FRAMETYPE_OFT) && cur->commandlen) {
301 if (send(cur->conn->fd, cur->data, cur->commandlen, 0) != (int)cur->commandlen) {
303 * Theres nothing we can do about this since we've already sent the
304 * header! The connection is unstable.
309 cur->sent = 1; /* mark the struct as sent */
310 cur->conn->lastactivity = time(NULL);
312 faim_mutex_unlock(&cur->conn->active);
315 faimdprintf(2, "\nPacket:");
316 for (i = 0; i < (cur->commandlen + 6); i++) {
318 faimdprintf(2, "\n\t");
320 if (curPacket[i] >= ' ' && curPacket[i]<127) {
321 faimdprintf(2, "%c=%02x ", curPacket[i], curPacket[i]);
323 faimdprintf(2, "0x%2x ", curPacket[i]);
326 faimdprintf(2, "\n");
328 cur->lock = 0; /* unlock the struct */
329 free(curPacket); /* free up full-packet buffer */
331 return 1; /* success */
334 faim_export int aim_tx_flushqueue(struct aim_session_t *sess)
336 struct command_tx_struct *cur;
342 if (sess->queue_outgoing == NULL)
345 faimdprintf(2, "beginning txflush...\n");
346 for (cur = sess->queue_outgoing; cur; cur = cur->next) {
347 /* only process if its unlocked and unsent */
348 if (!cur->lock && !cur->sent) {
351 * And now for the meager attempt to force transmit
352 * latency and avoid missed messages.
354 if ((cur->conn->lastactivity + cur->conn->forcedlatency) >= time(NULL)) {
355 /* FIXME FIXME -- should be a break! we dont want to block the upper layers */
356 sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL));
359 if (aim_tx_sendframe(sess, cur) == -1)
364 /* purge sent commands from queue */
365 aim_tx_purgequeue(sess);
371 * aim_tx_purgequeue()
373 * This is responsable for removing sent commands from the transmit
374 * queue. This is not a required operation, but it of course helps
375 * reduce memory footprint at run time!
378 faim_export void aim_tx_purgequeue(struct aim_session_t *sess)
380 struct command_tx_struct *cur = NULL;
381 struct command_tx_struct *tmp;
383 if (sess->queue_outgoing == NULL)
386 if (sess->queue_outgoing->next == NULL) {
387 if (!sess->queue_outgoing->lock && sess->queue_outgoing->sent) {
388 tmp = sess->queue_outgoing;
389 sess->queue_outgoing = NULL;
390 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
391 free(tmp->hdr.oft.hdr2);
398 for(cur = sess->queue_outgoing; cur->next != NULL; ) {
399 if (!cur->next->lock && cur->next->sent) {
401 cur->next = tmp->next;
402 if (tmp->hdrtype == AIM_FRAMETYPE_OFT)
403 free(tmp->hdr.oft.hdr2);
410 * Be careful here. Because of the way we just
411 * manipulated the pointer, cur may be NULL and
412 * the for() will segfault doing the check unless
413 * we find this case first.