+ int buflen = 0;
+ unsigned char *curPacket;
+
+ if (!cur)
+ return -1; /* fatal */
+
+ cur->lock = 1; /* lock the struct */
+
+ if (cur->hdrtype == AIM_FRAMETYPE_OSCAR)
+ buflen = cur->commandlen + 6;
+ else if (cur->hdrtype == AIM_FRAMETYPE_OFT)
+ buflen = cur->hdr.oft.hdr2len + 8;
+ else {
+ cur->lock = 0;
+ return -1;
+ }
+
+ /* allocate full-packet buffer */
+ if (!(curPacket = (unsigned char *) malloc(buflen))) {
+ cur->lock = 0;
+ return -1;
+ }
+
+ if (cur->hdrtype == AIM_FRAMETYPE_OSCAR) {
+ /* command byte */
+ curPacket[0] = 0x2a;
+
+ /* type/family byte */
+ curPacket[1] = cur->hdr.oscar.type;
+
+ /* bytes 3+4: word: FLAP sequence number */
+ aimutil_put16(curPacket+2, cur->hdr.oscar.seqnum);
+
+ /* bytes 5+6: word: SNAC len */
+ aimutil_put16(curPacket+4, cur->commandlen);
+
+ /* bytes 7 and on: raw: SNAC data */ /* XXX: ye gods! get rid of this! */
+ memcpy(&(curPacket[6]), cur->data, cur->commandlen);
+
+ } else if (cur->hdrtype == AIM_FRAMETYPE_OFT) {
+ int z = 0;
+
+ z += aimutil_put8(curPacket+z, 0x4f);
+ z += aimutil_put8(curPacket+z, 0x44);
+ z += aimutil_put8(curPacket+z, 0x43);
+ z += aimutil_put8(curPacket+z, 0x32);
+
+ z += aimutil_put16(curPacket+z, cur->hdr.oft.hdr2len + 8);
+ z += aimutil_put16(curPacket+z, cur->hdr.oft.type);
+
+ memcpy(curPacket+z, cur->hdr.oft.hdr2, cur->hdr.oft.hdr2len);
+ }
+
+ /*
+ * For OSCAR, a full image of the raw packet data now in curPacket.
+ * For OFT, an image of just the bloated header is in curPacket,
+ * since OFT allows us to do the data in a different write (yay!).
+ */
+ faim_mutex_lock(&cur->conn->active);
+ if (send(cur->conn->fd, curPacket, buflen, 0) != buflen) {
+ faim_mutex_unlock(&cur->conn->active);
+ cur->sent = 1;
+ aim_conn_kill(sess, &cur->conn);
+ return 0; /* bail out */
+ }
+
+ if ((cur->hdrtype == AIM_FRAMETYPE_OFT) && cur->commandlen) {
+ if (send(cur->conn->fd, cur->data, cur->commandlen, 0) != (int)cur->commandlen) {
+ /*
+ * Theres nothing we can do about this since we've already sent the
+ * header! The connection is unstable.
+ */
+ }
+ }
+
+ cur->sent = 1; /* mark the struct as sent */
+ cur->conn->lastactivity = time(NULL);
+
+ faim_mutex_unlock(&cur->conn->active);