2 * File transfer (OFT) and DirectIM (ODC).
3 * (OSCAR File Transfer, Oscar Direct Connect(ion?)
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <sys/utsname.h> /* for aim_directim_initiate */
16 #include <arpa/inet.h> /* for inet_ntoa */
21 o look for memory leaks.. there's going to be shitloads, i'm sure.
24 static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr);
27 * aim_handlerendconnect - call this to accept OFT connections and set up the requisite structures
29 * @cur: the conn the incoming connection is on
31 * call this when you get an outstanding read on a conn with subtype
32 * AIM_CONN_SUBTYPE_RENDEZVOUS_OUT, it will clone the current
33 * &aim_conn_t and tweak things as appropriate. the new conn and the
34 * listener conn are both returned to the client in the
35 * %AIM_CB_FAM_OFT, %AIM_CB_OFT_<CLASS>INITIATE callback.
37 faim_export int aim_handlerendconnect(struct aim_session_t *sess, struct aim_conn_t *cur)
40 aim_rxcallback_t userfunc;
41 struct sockaddr cliaddr;
42 int clilen = sizeof(cliaddr);
44 struct aim_conn_t *newconn;
46 if ( (acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1)
48 if (cliaddr.sa_family != AF_INET) { /* just in case IPv6 really is happening */
54 /* safe? maybe cur->priv should be NULLed after this. --mid */
56 /* That would be bad. very bad. we want cur->priv->sn to make it up
57 to the client-level for conn management and such. even though
58 that is abusing the interface --jbm */
60 if (!(newconn = aim_cloneconn(sess, cur))) {
66 newconn->type = AIM_CONN_TYPE_RENDEZVOUS;
67 newconn->fd = acceptfd;
69 switch(newconn->subtype) {
70 case AIM_CONN_SUBTYPE_OFT_DIRECTIM: {
71 struct aim_directim_priv *priv;
75 newconn->priv = cur->priv;
79 snprintf(priv->ip, sizeof(priv->ip), "%s:%u",
80 inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr),
81 ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
83 if ( (userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE)))
84 ret = userfunc(sess, NULL, newconn, cur);
88 case AIM_CONN_SUBTYPE_OFT_GETFILE: {
89 struct aim_filetransfer_priv *priv;
92 newconn->priv = cur->priv;
94 priv = (struct aim_filetransfer_priv *)newconn->priv;
96 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
98 if ( (userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE)))
99 ret = userfunc(sess, NULL, newconn, cur);
104 faimdprintf(sess, 1,"Got a Connection on a listener that's not Rendezvous(??!) Closing conn.\n");
105 aim_conn_close(newconn);
114 * aim_send_im_direct - send IM client-to-client over established connection
115 * @sess: session to conn
116 * @conn: directim connection
117 * @msg: null-terminated string to send; if this is NULL, it will send a "typing" notice.
119 * Call this just like you would aim_send_im, to send a directim. You
120 * _must_ have previously established the directim connection.
122 faim_export int aim_send_im_direct(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg)
124 struct command_tx_struct *newpacket;
125 struct aim_directim_priv *priv = NULL;
128 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !conn->priv) {
129 faimdprintf(sess, 2,"faim: directim: invalid arguments\n");
133 priv = (struct aim_directim_priv *)conn->priv;
135 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0001, strlen(msg)))) {
136 faimdprintf(sess, 2,"faim: directim: tx_new failed\n");
142 /* if msg is non-null, we'resending an IM, else a "typing" notice */
144 if (strlen(msg) >= MAXMSGLEN)
146 newpacket->hdr.oft.hdr2len = 0x54;
147 if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) {
149 aim_tx_destroy(newpacket);
153 newpacket->hdr.oft.hdr2len = 0x44;
154 if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) {
156 aim_tx_destroy(newpacket);
161 memcpy(newpacket->hdr.oft.magic, "ODC2", 4);
162 newpacket->data = NULL;
165 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0006);
166 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
167 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, (char *)priv->cookie, 8);
168 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
169 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
170 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
171 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
172 i += aimutil_put32(newpacket->hdr.oft.hdr2+i, strlen(msg));
173 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
174 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
175 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
177 /* flags -- 0x000e for "typing", 0x0000 for message */
179 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
181 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000e);
183 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
184 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
185 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, sess->sn, strlen(sess->sn));
188 i += aimutil_put8(newpacket->hdr.oft.hdr2+i, 0x00);
189 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
190 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
191 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
192 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
193 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
194 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
195 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
200 /* values grabbed from a dump */
201 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008);
202 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c);
203 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
204 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466);
205 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001);
206 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f);
207 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e);
208 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8);
209 if(!(newpacket->data = strdup(msg)))
213 aim_tx_enqueue(sess, newpacket);
217 /* XXX: give the client author the responsibility of setting up a
218 * listener, then we no longer have a libfaim problem with broken
219 * solaris *innocent smile* -jbm */
222 * aim_directim_intitiate - For those times when we want to open up the directim channel ourselves.
223 * @sess: your session,
224 * @conn: the BOS conn,
225 * @priv: a dummy priv value (we'll let it get filled in later) (if you pass a %NULL, we alloc one)
226 * @destsn: the SN to connect to.
229 faim_export struct aim_conn_t *aim_directim_initiate(struct aim_session_t *sess,
230 struct aim_conn_t *conn,
231 struct aim_directim_priv *priv,
235 struct command_tx_struct *newpacket;
236 struct aim_conn_t *newconn;
237 struct aim_msgcookie_t *cookie;
238 int curbyte, i, listenfd;
240 struct hostent *hptr;
242 unsigned char cap[16];
243 char d[4]; /* IPv6 is a bit bigger... */
245 /* XXX: TLVlist-ize this */
247 /* Open our socket */
249 if ( (listenfd = aim_listenestablish(port)) == -1)
252 /* get our local IP */
253 /* XXX if available, use getaddrinfo() */
254 /* XXX allow client to specify which IP to use for multihomed boxes */
255 if (gethostname(localhost, 128) < 0)
257 if ( (hptr = gethostbyname(localhost)) == NULL)
259 memcpy(&d, hptr->h_addr_list[0], 4);
261 aim_putcap(cap, 16, AIM_CAPS_IMIMAGE);
263 /* create the OSCAR packet */
265 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(destsn)+4+4+0x32)))
270 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
272 /* Generate a random message cookie */
273 /* This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */
275 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) rand() % 20));
277 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
279 /* grab all the data for cookie caching */
280 cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t));
281 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
282 cookie->type = AIM_COOKIETYPE_OFTIM;
286 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
288 memcpy(priv->cookie, cookie, 8);
289 memcpy(priv->sn, destsn, sizeof(priv->sn));
291 aim_cachecookie(sess, cookie);
294 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
296 /* Destination SN (prepended with byte length)*/
297 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
298 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
299 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
300 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
303 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
304 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032);
306 /* Flag data / ICBM Parameters */
307 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
308 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
311 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
313 /*Capability String */
314 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
316 /* 000a/0002 : 0001 */
317 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
318 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
319 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
321 /* 0003/0004: IP address */
322 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
323 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
324 for(i = 0;i < 4; i++)
325 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]);
327 /* 0005/0002: Port */
328 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
329 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
330 curbyte += aimutil_put16(newpacket->data+curbyte, port);
333 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
334 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
335 newpacket->commandlen = curbyte;
337 aim_tx_enqueue(sess, newpacket);
340 /* XXX switch to aim_cloneconn()? */
341 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL)))
344 newconn->fd = listenfd;
345 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
346 newconn->priv = priv;
347 newconn->lastactivity = time(NULL);
349 faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
355 * unsigned int aim_oft_listener_clean - close up old listeners
356 * @sess: session to clean up in
357 * @age: maximum age in seconds
359 * returns number closed, -1 on error.
361 faim_export unsigned int aim_oft_listener_clean(struct aim_session_t *sess, time_t age)
363 struct aim_conn_t *cur;
365 unsigned int hit = 0;
370 faim_mutex_lock(&sess->connlistlock);
371 for(cur = sess->connlist;cur; cur = cur->next)
372 if (cur->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) {
373 faim_mutex_lock(&cur->active);
374 if (cur->lastactivity < (now - age) ) {
375 faim_mutex_unlock(&cur->active);
379 faim_mutex_unlock(&cur->active);
381 faim_mutex_unlock(&sess->connlistlock);
386 * aim_directim_connect - connect to buddy for directim
387 * @sess: the session to append the conn to,
388 * @conn: the BOS connection,
389 * @priv: the filled-in priv data structure for the connection
391 * returns conn if connected, %NULL on error
393 faim_export struct aim_conn_t *aim_directim_connect(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_directim_priv *priv)
395 struct aim_conn_t *newconn = NULL;
397 if (!sess || !conn || !priv)
400 /* XXX verify that non-blocking connects actually work */
401 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, priv->ip);
402 if (!newconn || (newconn->fd == -1)) {
403 faimdprintf(sess, 2, "could not connect to %s\n", priv->ip);
404 perror("aim_newconn");
408 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
409 newconn->priv = priv;
410 faimdprintf(sess, 2, "faim: connected to peer (fd = %d)\n", newconn->fd);
416 * aim_directim_getconn - find a directim conn for buddy name
417 * @sess: your session,
418 * @name: the name to get,
420 * returns conn for directim with name, %NULL if none found.
423 faim_export struct aim_conn_t *aim_directim_getconn(struct aim_session_t *sess, const char *name)
425 struct aim_conn_t *cur;
426 struct aim_directim_priv *priv;
431 faim_mutex_lock(&sess->connlistlock);
433 for (cur = sess->connlist; cur; cur = cur->next) {
434 if (cur->type != AIM_CONN_TYPE_RENDEZVOUS || cur->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM)
437 if (aim_sncmp(priv->sn, name) == 0)
439 } faim_mutex_unlock(&sess->connlistlock);
444 * aim_accepttransfer - accept a file transfer request
445 * @sess: the session,
446 * @conn: the BOS conn for the CAP reply
447 * @sn: the screenname to send it to,
448 * @cookie: the cookie used
449 * @ip: the ip to connect to
450 * @listingfiles: number of files to share
451 * @listingtotsize: total size of shared files
452 * @listingsize: length of the listing file(buffer)
453 * @listingchecksum: checksum of the listing
454 * @rendid: capability type (%AIM_CAPS_GETFILE or %AIM_CAPS_SENDFILE)
456 * Returns new connection or %NULL on error.
458 faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess,
459 struct aim_conn_t *conn,
460 char *sn, char *cookie,
462 unsigned short listingfiles,
463 unsigned short listingtotsize,
464 unsigned short listingsize,
465 unsigned int listingchecksum,
466 unsigned short rendid)
468 struct command_tx_struct *newpacket, *newoft;
469 struct aim_conn_t *newconn;
470 struct aim_fileheader_t *fh;
471 struct aim_filetransfer_priv *priv;
472 struct aim_msgcookie_t *cachedcook;
475 if (!sess || !conn || !sn || !cookie || !ip) {
479 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, ip);
481 if (!newconn || (newconn->fd == -1)) {
482 perror("aim_newconn");
483 faimdprintf(sess, 2, "could not connect to %s (fd: %i)\n", ip, newconn?newconn->fd:0);
486 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
488 memcpy(priv->cookie, cookie, 8);
490 strncpy(priv->sn, sn, MAXSNLEN);
491 strncpy(priv->ip, ip, sizeof(priv->ip));
492 newconn->priv = (void *)priv;
494 faimdprintf(sess, 2, "faim: connected to peer (fd = %d)\n", newconn->fd);
497 if (rendid == AIM_CAPS_GETFILE) {
498 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
500 faimdprintf(sess, 2, "faim: getfile request accept\n");
502 if (!(newoft = aim_tx_new(sess, newconn, AIM_FRAMETYPE_OFT, 0x1108, 0))) {
503 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
504 /* XXX: conn leak here */
509 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
510 newoft->hdr.oft.hdr2len = 0x100 - 8;
512 if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t)))) {
513 /* XXX: conn leak here */
518 fh->encrypt = 0x0000;
519 fh->compress = 0x0000;
520 fh->totfiles = listingfiles;
521 fh->filesleft = listingfiles; /* is this right -- total parts and parts left?*/
522 fh->totparts = 0x0001;
523 fh->partsleft = 0x0001;
524 fh->totsize = listingtotsize;
525 fh->size = listingsize; /* ls -l listing.txt */
526 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
527 fh->checksum = listingchecksum;
528 fh->rfcsum = 0x00000000;
529 fh->rfsize = 0x00000000;
530 fh->cretime = 0x00000000;
531 fh->rfcsum = 0x00000000;
532 fh->nrecvd = 0x00000000;
533 fh->recvcsum = 0x00000000;
534 memset(fh->idstring, 0, sizeof(fh->idstring));
535 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
537 fh->lnameoffset = 0x1a;
538 fh->lsizeoffset = 0x10;
539 memset(fh->dummy, 0, sizeof(fh->dummy));
540 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
542 /* we need to figure out these encodings for filenames */
543 fh->nencode = 0x0000;
544 fh->nlanguage = 0x0000;
545 memset(fh->name, 0, sizeof(fh->name));
546 memcpy(fh->name, "listing.txt", sizeof(fh->name));
548 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
550 aim_tx_destroy(newoft);
552 perror("calloc (1)");
556 memcpy(fh->bcookie, cookie, 8);
558 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, fh)))
559 faimdprintf(sess, 1, "eek, bh fail!\n");
562 aim_tx_enqueue(sess, newoft);
564 if (!(cachedcook = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) {
565 faimdprintf(sess, 1, "faim: accepttransfer: couldn't calloc cachedcook. yeep!\n");
566 /* XXX: more cleanup, conn leak */
567 perror("calloc (2)");
571 memcpy(&(priv->fh), fh, sizeof(struct aim_fileheader_t));
572 memcpy(cachedcook->cookie, cookie, 8);
574 cachedcook->type = AIM_COOKIETYPE_OFTGET;
575 cachedcook->data = (void *)priv;
577 if (aim_cachecookie(sess, cachedcook) == -1)
578 faimdprintf(sess, 1, "faim: ERROR caching message cookie\n");
582 /* OSCAR CAP accept packet */
584 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sn)+4+2+8+16))) {
592 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
594 for (i = 0; i < 8; i++)
595 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
597 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
598 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
599 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
600 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
601 curbyte += aimutil_put16(newpacket->data+curbyte, 0x001a);
602 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept*/);
604 for (i = 0;i < 8; i++)
605 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
607 curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid);
609 aim_tx_enqueue(sess, newpacket);
615 * aim_getlisting(FILE *file) -- get an aim_fileheader_t for a given FILE*
616 * @file is an opened listing file
618 * returns a pointer to the filled-in fileheader_t
620 * Currently omits checksum. we'll fix this when AOL breaks us, i
625 faim_export struct aim_fileheader_t *aim_getlisting(struct aim_session_t *sess, FILE *file)
627 struct aim_fileheader_t *fh;
628 u_long totsize = 0, size = 0, checksum = 0xffff0000;
630 char *linebuf, sizebuf[9];
632 int linelength = 1024;
634 /* XXX: if we have a line longer than 1024chars, God help us. */
635 if ( (linebuf = (char *)calloc(1, linelength)) == NULL ) {
636 faimdprintf(sess, 2, "linebuf calloc failed\n");
640 if (fseek(file, 0, SEEK_END) == -1) { /* use this for sanity check */
641 perror("getlisting END1 fseek:");
642 faimdprintf(sess, 2, "getlising fseek END1 error\n");
645 if ((size = ftell(file)) == -1) {
646 perror("getlisting END1 getpos:");
647 faimdprintf(sess, 2, "getlising getpos END1 error\n");
650 if (fseek(file, 0, SEEK_SET) != 0) {
651 perror("getlesting fseek(SET):");
652 faimdprintf(sess, 2, "faim: getlisting: couldn't seek to beginning of listing file\n");
655 memset(linebuf, 0, linelength);
659 while(fgets(linebuf, linelength, file)) {
661 memset(sizebuf, 0, 9);
663 size += strlen(linebuf);
665 if (strlen(linebuf) < 23) {
666 faimdprintf(sess, 2, "line \"%s\" too short. skipping\n", linebuf);
669 if (linebuf[strlen(linebuf)-1] != '\n') {
670 faimdprintf(sess, 2, "faim: OFT: getlisting -- hit EOF or line too long!\n");
673 memcpy(sizebuf, linebuf+17, 8);
675 totsize += strtol(sizebuf, NULL, 10);
676 memset(linebuf, 0, linelength);
679 if (fseek(file, 0, SEEK_SET) == -1) {
680 perror("getlisting END2 fseek:");
681 faimdprintf(sess, 2, "getlising fseek END2 error\n");
686 /* we're going to ignore checksumming the data for now -- that
687 * requires walking the whole listing.txt. it should probably be
688 * done at register time and cached, but, eh. */
690 if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t))))
693 fh->encrypt = 0x0000;
694 fh->compress = 0x0000;
695 fh->totfiles = totfiles;
696 fh->filesleft = totfiles; /* is this right ?*/
697 fh->totparts = 0x0001;
698 fh->partsleft = 0x0001;
699 fh->totsize = totsize;
700 fh->size = size; /* ls -l listing.txt */
701 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
702 fh->checksum = checksum; /* XXX: checksum ! */
703 fh->rfcsum = 0x00000000;
704 fh->rfsize = 0x00000000;
705 fh->cretime = 0x00000000;
706 fh->rfcsum = 0x00000000;
707 fh->nrecvd = 0x00000000;
708 fh->recvcsum = 0x00000000;
710 /* memset(fh->idstring, 0, sizeof(fh->idstring)); */
711 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
712 memset(fh->idstring+strlen(fh->idstring), 0, sizeof(fh->idstring)-strlen(fh->idstring));
715 fh->lnameoffset = 0x1a;
716 fh->lsizeoffset = 0x10;
718 /* memset(fh->dummy, 0, sizeof(fh->dummy)); */
719 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
721 fh->nencode = 0x0000; /* we need to figure out these encodings for filenames */
722 fh->nlanguage = 0x0000;
724 /* memset(fh->name, 0, sizeof(fh->name)); */
725 memcpy(fh->name, "listing.txt", sizeof(fh->name));
726 memset(fh->name+strlen(fh->name), 0, 64-strlen(fh->name));
728 faimdprintf(sess, 2, "faim: OFT: listing fh name %s / %s\n", fh->name, (fh->name+(strlen(fh->name))));
733 * aim_listenestablish - create a listening socket on a port.
734 * @portnum: the port number to bind to.
736 * you need to call accept() when it's connected. returns your fd
739 faim_export int aim_listenestablish(u_short portnum)
741 #if defined(__linux__)
742 /* XXX what other OS's support getaddrinfo? */
745 struct addrinfo hints, *res, *ressave;
748 snprintf(serv, sizeof(serv), "%d", portnum);
749 memset(&hints, 0, sizeof(struct addrinfo));
750 hints.ai_flags = AI_PASSIVE;
751 hints.ai_family = AF_UNSPEC;
752 hints.ai_socktype = SOCK_STREAM;
753 if (getaddrinfo(NULL /*any IP*/, serv, &hints, &res) != 0) {
754 perror("getaddrinfo");
759 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
762 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
763 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
767 } while ( (res = res->ai_next) );
772 if (listen(listenfd, 1024)!=0) {
777 freeaddrinfo(ressave);
782 struct sockaddr_in sockin;
784 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
785 perror("socket(listenfd)");
789 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) != 0)) {
790 perror("setsockopt(listenfd)");
795 memset(&sockin, 0, sizeof(struct sockaddr_in));
796 sockin.sin_family = AF_INET;
797 sockin.sin_port = htons(portnum);
799 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
800 perror("bind(listenfd)");
804 if (listen(listenfd, 4) != 0) {
805 perror("listen(listenfd)");
814 * aim_get_command_rendezvous - OFT equivalent of aim_get_command
815 * @sess: session to work on
816 * @conn: conn to pull data from
818 * this reads and handles data from conn->fd. currently a little rough
821 faim_internal int aim_get_command_rendezvous(struct aim_session_t *sess, struct aim_conn_t *conn)
823 unsigned char hdrbuf1[6];
824 unsigned char *hdr = NULL;
827 aim_rxcallback_t userfunc = NULL;
829 if (!sess || !conn || !conn->priv)
832 memset(hdrbuf1, 0, sizeof(hdrbuf1));
833 faim_mutex_lock(&conn->active);
835 /* gets locked down for the entirety */
837 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE ) {
838 struct aim_filetransfer_priv *ft;
840 if (ft->state == 2) {
841 /* waiting on listing data */
844 struct command_tx_struct *newoft;
845 if (!(listing = malloc(ft->fh.size))) {
846 faim_mutex_unlock(&conn->active);
851 if (aim_recv(conn->fd, listing, ft->fh.size) != ft->fh.size)
852 faimdprintf(sess, 2, "OFT get: file %s was short. (0x%lx)\n", ft->fh.name, ft->fh.size);
854 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120b, 0))) {
855 faimdprintf(sess, 2, "faim: aim_get_command_rendezvous: getfile listing: tx_new OFT failed\n");
856 faim_mutex_unlock(&conn->active);
858 aim_conn_close(conn);
864 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
865 newoft->hdr.oft.hdr2len = 0x100 - 8;
867 /* Protocol BS - set nrecvd to size of listing, recvcsum to
868 listing checksum, flags to 0 */
870 ft->fh.nrecvd = ft->fh.size;
871 ft->fh.recvcsum = ft->fh.checksum;
874 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
876 aim_tx_destroy(newoft);
878 faim_mutex_unlock(&conn->active);
882 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh))))
883 faimdprintf(sess, 2, "eek! bh fail listing\n");
887 aim_tx_enqueue(sess, newoft);
888 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTING)) )
889 ret = userfunc(sess, NULL, conn, ft, listing);
891 faim_mutex_unlock(&conn->active);
895 if (ft->state == 3) {
896 /* waiting on file data */
897 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILERECEIVE)) ) {
898 faim_mutex_unlock(&conn->active);
899 return userfunc(sess, NULL, conn, ft);
901 faim_mutex_unlock(&conn->active);
905 if( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILESTATE4)) ) {
906 faim_mutex_unlock(&conn->active);
907 return userfunc(sess, NULL, conn);
909 faim_mutex_unlock(&conn->active);
910 aim_conn_close(conn);
915 if ( (hdrlen = aim_recv(conn->fd, hdrbuf1, 6)) < 6) {
916 faimdprintf(sess, 2, "faim: rend: read error (fd: %i) %02x%02x%02x%02x%02x%02x (%i)\n",
917 conn->fd, hdrbuf1[0],hdrbuf1[1],hdrbuf1[2],hdrbuf1[3],hdrbuf1[4],hdrbuf1[5],hdrlen);
918 faim_mutex_unlock(&conn->active);
921 else { /* disconnected */
922 char *screenname = NULL;
924 struct aim_msgcookie_t *cook;
926 switch(conn->subtype) {
927 case AIM_CONN_SUBTYPE_OFT_DIRECTIM: {
928 struct aim_directim_priv *priv = NULL;
929 if (!(priv = (struct aim_directim_priv *)conn->priv) )
932 screenname = strdup(priv->sn);
934 cook = aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTIM);
935 aim_cookie_free(sess, cook);
936 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT)) ) {
937 aim_conn_close(conn);
938 ret = userfunc(sess, NULL, conn, screenname);
944 case AIM_CONN_SUBTYPE_OFT_GETFILE: {
945 struct aim_filetransfer_priv *priv;
946 if (!(priv = (struct aim_filetransfer_priv *)conn->priv))
948 screenname = strdup(priv->sn);
950 cook = aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTGET);
952 aim_cookie_free(sess, cook);
954 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT)) ) {
955 aim_conn_close(conn);
956 ret = userfunc(sess, NULL, conn, screenname);
962 case AIM_CONN_SUBTYPE_OFT_SENDFILE: {
963 struct aim_filetransfer_priv *priv;
964 if (!(priv = (struct aim_filetransfer_priv *)conn->priv))
967 screenname = strdup(priv->sn);
969 cook = aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTSEND);
970 aim_cookie_free(sess, cook);
971 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_SENDFILEDISCONNECT)) ) {
972 aim_conn_close(conn);
973 ret = userfunc(sess, NULL, conn, screenname);
981 aim_conn_close(conn);
986 hdrlen = aimutil_get16(hdrbuf1+4);
989 if (!(hdr = malloc(hdrlen))) {
990 faim_mutex_unlock(&conn->active);
994 if (aim_recv(conn->fd, hdr, hdrlen) < hdrlen) {
996 faimdprintf(sess, 2,"faim: rend: read2 error on %d (%d)\n", conn->fd, hdrlen);
998 faim_mutex_unlock(&conn->active);
999 aim_conn_close(conn);
1002 hdrtype = aimutil_get16(hdr);
1005 case 0x0001: { /* directim */
1006 int payloadlength = 0;
1008 struct aim_directim_priv *priv;
1011 if (!(priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)))) {
1012 faim_mutex_unlock(&conn->active);
1017 payloadlength = aimutil_get32(hdr+22);
1018 flags = aimutil_get16(hdr+32);
1019 snptr = (char *)hdr+38;
1020 strncpy(priv->sn, snptr, MAXSNLEN);
1022 faimdprintf(sess, 2, "faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, priv->sn);
1027 if (flags == 0x000e) {
1028 faim_mutex_unlock(&conn->active);
1029 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)) )
1030 return userfunc(sess, NULL, conn);
1033 if ((flags == 0x0000) && payloadlength) {
1036 if (!(msg = calloc(1, payloadlength+1))) {
1037 faim_mutex_unlock(&conn->active);
1041 if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) {
1043 faimdprintf(sess, 2,"faim: rend: read3 error\n");
1045 faim_mutex_unlock(&conn->active);
1046 aim_conn_close(conn);
1050 faim_mutex_unlock(&conn->active);
1051 msg[payloadlength] = 0x00;
1052 faimdprintf(sess, 2, "faim: directim: %s/%04x/%04x/%s\n", priv->sn, payloadlength, flags, msg);
1054 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) )
1055 i = userfunc(sess, NULL, conn, msg);
1057 faimdprintf(sess, 0, "directim: %s/%04x/%04x/%s\n", priv->sn, payloadlength, flags, msg);
1068 case 0x1108: { /* getfile listing.txt incoming tx->rx */
1069 struct aim_filetransfer_priv *ft;
1070 struct aim_fileheader_t *fh;
1071 struct aim_msgcookie_t *cook;
1072 struct command_tx_struct *newoft;
1074 faimdprintf(sess, 2,"faim: rend: fileget 0x1108\n");
1075 fh = aim_oft_getfh(hdr);
1080 faim_mutex_unlock(&conn->active);
1082 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1083 faim_mutex_unlock(&conn->active);
1090 /* we're waaaaiiiting.. for listing.txt */
1093 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1096 if(aim_cachecookie(sess, cook) == -1) {
1097 faimdprintf(sess, 1, "error caching cookie\n");
1101 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x1209, 0))) {
1102 aim_conn_close(conn);
1106 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1107 newoft->hdr.oft.hdr2len = 0x100 - 8;
1109 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1111 aim_tx_destroy(newoft);
1115 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1117 aim_tx_destroy(newoft);
1122 aim_tx_enqueue(sess, newoft);
1126 case 0x1209: { /* get file listing ack rx->tx */
1127 struct aim_filetransfer_priv *ft;
1128 struct aim_fileheader_t *fh;
1129 struct aim_msgcookie_t *cook;
1132 if(!(fh = aim_oft_getfh(hdr))) {
1141 faim_mutex_unlock(&conn->active);
1143 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET)))
1144 faimdprintf(sess, 2, "shit, no cookie in 0x1209. (%i/%s)going to crash..\n",
1145 AIM_COOKIETYPE_OFTGET, fh->bcookie);
1149 if (ft->fh.size != fh->size)
1150 faimdprintf(sess, 2, "hrm. ft->fh.size (%ld) != fh->size (%ld). um. using ft->fh.size\n",
1151 ft->fh.size, fh->size);
1153 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ)))
1154 ret = userfunc(sess, NULL, conn, fh);
1156 faimdprintf(sess, 2, "faim: get_command_rendezvous: hit end of 1209\n");
1164 case 0x120b: { /* getfile listing.txt rx confirm */
1165 struct aim_filetransfer_priv *ft;
1166 struct aim_msgcookie_t *cook;
1167 struct aim_fileheader_t *fh;
1169 fh = aim_oft_getfh(hdr);
1174 faim_mutex_unlock(&conn->active);
1176 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1185 if (aim_cachecookie(sess, cook) == -1) {
1189 if((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGRXCONFIRM)))
1190 return userfunc(sess, NULL, conn);
1194 case 0x120c: { /* getfile file request */
1195 struct aim_filetransfer_priv *ft;
1196 struct aim_msgcookie_t *cook;
1197 struct aim_fileheader_t *fh;
1198 struct command_tx_struct *newoft;
1201 fh = aim_oft_getfh(hdr);
1206 faim_mutex_unlock(&conn->active);
1208 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1209 faimdprintf(sess, 2, "no cookie in 120c\n");
1214 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1217 aim_cachecookie(sess, cook);
1219 faimdprintf(sess, 2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name);
1221 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) )
1222 i = userfunc(sess, NULL, conn, &(ft->fh), cook->cookie);
1227 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0101, 0))) {
1228 faimdprintf(sess, 2, "faim: send_final_transfer: tx_new OFT failed\n");
1233 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1234 newoft->hdr.oft.hdr2len = 0x100 - 8;
1236 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1238 aim_tx_destroy(newoft);
1242 /* protocol BS: nrecvd, recvcsum to 0, flags to 0x20. */
1244 ft->fh.recvcsum = 0;
1245 ft->fh.flags = 0x20;
1247 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
1250 aim_tx_enqueue(sess, newoft);
1252 faimdprintf(sess, 2, "faim: OFT: OFT file header enqueued.\n");
1258 case 0x0101: { /* getfile: sending data */
1259 struct aim_fileheader_t *fh;
1260 struct aim_filetransfer_priv *ft;
1261 struct aim_msgcookie_t *cook;
1262 struct command_tx_struct *newoft;
1264 fh = aim_oft_getfh(hdr);
1269 faim_mutex_unlock(&conn->active);
1271 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1281 if (aim_cachecookie(sess, cook) == -1) {
1282 perror("aim_cachecookie");
1286 faimdprintf(sess, 2, "faim: fileget: %s seems to want to send %s\n", ft->sn, ft->fh.name);
1288 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) {
1289 aim_conn_close(conn);
1290 faimdprintf(sess, 2, "faim: send_final_transfer: tx_new OFT failed\n");
1295 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1297 newoft->hdr.oft.hdr2len = 0x100 - 8;
1299 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1301 aim_tx_destroy(newoft);
1305 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
1308 aim_tx_enqueue(sess, newoft);
1310 faimdprintf(sess, 2, "faim: OFT: OFT 0x0202 enqueued.\n");
1312 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL)
1317 case 0x0202: { /* get file: ready to receive data */
1318 struct aim_fileheader_t *fh;
1319 struct aim_filetransfer_priv *ft;
1320 struct aim_msgcookie_t *cook;
1323 fh = aim_oft_getfh(hdr);
1328 faim_mutex_unlock(&conn->active);
1330 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1337 faimdprintf(sess, 2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n");
1339 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) )
1340 ret = userfunc(sess, NULL, conn, fh);
1347 case 0x0204: { /* get file: finished. close it up */
1349 struct aim_fileheader_t *fh;
1351 if(!(fh = aim_oft_getfh(hdr)))
1357 faim_mutex_unlock(&conn->active);
1359 faimdprintf(sess, 2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n");
1361 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) )
1362 i = userfunc(sess, NULL, conn, fh);
1367 aim_conn_close(conn);
1377 faimdprintf(sess, 2,"faim: OFT frame: uknown type %04x\n", hdrtype);
1378 faim_mutex_unlock(&conn->active);
1384 faimdprintf(sess, 0, "hdr wasn't freed by a rendezvous switch case (hdrtype: %0x04x)!\n", hdrtype);
1392 * aim_oft_getfh - extracts an &aim_fileheader_t from buffer hdr.
1393 * @hdr: buffer to extract header from
1395 * returns pointer to new struct on success; %NULL on error.
1398 static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr)
1400 struct aim_fileheader_t *fh;
1402 if (!(fh = calloc(1, sizeof(struct aim_fileheader_t))))
1405 /* [0] and [1] are the type. we can ignore those here. */
1407 for(j = 0; j < 8; j++, i++)
1408 fh->bcookie[j] = hdr[i];
1409 fh->encrypt = aimutil_get16(hdr+i);
1411 fh->compress = aimutil_get16(hdr+i);
1413 fh->totfiles = aimutil_get16(hdr+i);
1415 fh->filesleft = aimutil_get16(hdr+i);
1417 fh->totparts = aimutil_get16(hdr+i);
1419 fh->partsleft = aimutil_get16(hdr+i);
1421 fh->totsize = aimutil_get32(hdr+i);
1423 fh->size = aimutil_get32(hdr+i);
1425 fh->modtime = aimutil_get32(hdr+i);
1427 fh->checksum = aimutil_get32(hdr+i);
1429 fh->rfrcsum = aimutil_get32(hdr+i);
1431 fh->rfsize = aimutil_get32(hdr+i);
1433 fh->cretime = aimutil_get32(hdr+i);
1435 fh->rfcsum = aimutil_get32(hdr+i);
1437 fh->nrecvd = aimutil_get32(hdr+i);
1439 fh->recvcsum = aimutil_get32(hdr+i);
1441 memcpy(fh->idstring, hdr+i, 32);
1443 fh->flags = aimutil_get8(hdr+i);
1445 fh->lnameoffset = aimutil_get8(hdr+i);
1447 fh->lsizeoffset = aimutil_get8(hdr+i);
1449 memcpy(fh->dummy, hdr+i, 69);
1451 memcpy(fh->macfileinfo, hdr+i, 16);
1453 fh->nencode = aimutil_get16(hdr+i);
1455 fh->nlanguage = aimutil_get16(hdr+i);
1457 memcpy(fh->name, hdr+i, 64);
1463 * aim_oft_checksum - calculate oft checksum of buffer
1464 * @buffer: buffer of data to checksum
1465 * @bufsize: size of buffer
1466 * @checksum: pointer to integer to place result in (pointer!)
1469 * Note that checksum is a pointer. Checksum should be filled with
1470 * 0xFFFF0000 for each new file; you can have this checksum chunks of
1471 * files in series if you just call it repeatedly in a for(; ; ) loop
1472 * and don't reset the checksum between each call. And you thought we
1473 * didn't care about you and your pathetic client's meomry footprint
1477 * Also, it's been said that this is incorrect as currently
1478 * written. You were warned.
1480 faim_export int aim_oft_checksum(struct aim_session_t *sess, char *buffer, int bufsize, int *checksum)
1482 short check0, check1;
1484 check0 = ((*checksum & 0xFF000000) >> 16);
1485 check1 = ((*checksum & 0x00ff0000) >> 16);
1486 for(i = 0; i < bufsize; i++) {
1487 if (i % 2) { /* use check1 -- second byte */
1488 if ( (short)buffer[i] > check1 ) { /* wrapping */
1489 check1 += 0x100; /* this is a cheap way to wrap */
1491 /* if we're wrapping, decrement the other one */
1492 /* XXX: check this corner case */
1498 check1 -= buffer[i];
1499 } else { /* use check0 -- first byte */
1500 if ( (short)buffer[i] > check0 ) { /* wrapping */
1501 check0 += 0x100; /* this is a cheap way to wrap */
1503 /* if we're wrapping, decrement the other one */
1504 /* XXX: check this corner case */
1510 check0 -= buffer[i];
1514 if (check0 > 0xff || check1 > 0xff) {
1515 /* they shouldn't be able to do this. error! */
1516 faimdprintf(sess, 2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1);
1520 /* grab just the lowest byte; this should be clean, but just in
1525 *checksum = ((check0 * 0x1000000) + (check1 * 0x10000));
1530 * aim_oft_buildheader - fills a buffer with network-order fh data
1531 * @dest: buffer to fill -- pre-alloced
1532 * @fh: fh to get data from
1534 * returns length written; -1 on error.
1535 * DOES NOT DO BOUNDS CHECKING!
1538 faim_internal int aim_oft_buildheader(unsigned char *dest,struct aim_fileheader_t *fh)
1544 for(i = 0; i < 8; i++)
1545 curbyte += aimutil_put8(dest+curbyte, fh->bcookie[i]);
1546 curbyte += aimutil_put16(dest+curbyte, fh->encrypt);
1547 curbyte += aimutil_put16(dest+curbyte, fh->compress);
1548 curbyte += aimutil_put16(dest+curbyte, fh->totfiles);
1549 curbyte += aimutil_put16(dest+curbyte, fh->filesleft);
1550 curbyte += aimutil_put16(dest+curbyte, fh->totparts);
1551 curbyte += aimutil_put16(dest+curbyte, fh->partsleft);
1552 curbyte += aimutil_put32(dest+curbyte, fh->totsize);
1553 curbyte += aimutil_put32(dest+curbyte, fh->size);
1554 curbyte += aimutil_put32(dest+curbyte, fh->modtime);
1555 curbyte += aimutil_put32(dest+curbyte, fh->checksum);
1556 curbyte += aimutil_put32(dest+curbyte, fh->rfrcsum);
1557 curbyte += aimutil_put32(dest+curbyte, fh->rfsize);
1558 curbyte += aimutil_put32(dest+curbyte, fh->cretime);
1559 curbyte += aimutil_put32(dest+curbyte, fh->rfcsum);
1560 curbyte += aimutil_put32(dest+curbyte, fh->nrecvd);
1561 curbyte += aimutil_put32(dest+curbyte, fh->recvcsum);
1562 memcpy(dest+curbyte, fh->idstring, 32);
1564 curbyte += aimutil_put8(dest+curbyte, fh->flags);
1565 curbyte += aimutil_put8(dest+curbyte, fh->lnameoffset);
1566 curbyte += aimutil_put8(dest+curbyte, fh->lsizeoffset);
1567 memcpy(dest+curbyte, fh->dummy, 69);
1569 memcpy(dest+curbyte, fh->macfileinfo, 16);
1571 curbyte += aimutil_put16(dest+curbyte, fh->nencode);
1572 curbyte += aimutil_put16(dest+curbyte, fh->nlanguage);
1573 memset(dest+curbyte, 0x00, 64);
1574 memcpy(dest+curbyte, fh->name, 64);
1576 /* XXX: Filenames longer than 64B */
1583 * aim_tx_destroy - free's tx_command_t's
1584 * @command: the command to free
1586 * if command is locked, doesn't free.
1587 * returns -1 on error (locked struct); 0 on success.
1590 faim_internal int aim_tx_destroy(struct command_tx_struct *command){
1594 free(command->data);
1595 if (command->hdrtype == AIM_FRAMETYPE_OFT && command->hdr.oft.hdr2)
1596 free(command->hdr.oft.hdr2);
1602 * aim_getfile_intitiate - Request an OFT getfile session
1603 * @sess: your session,
1604 * @conn: the BOS conn,
1605 * @destsn is the SN to connect to.
1607 * returns a new &aim_conn_t on success, %NULL on error
1609 faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess, struct aim_conn_t *conn, char *destsn)
1611 struct command_tx_struct *newpacket;
1612 struct aim_conn_t *newconn;
1613 struct aim_filetransfer_priv *priv;
1614 struct aim_msgcookie_t *cookie;
1615 int curbyte, i, listenfd;
1617 struct hostent *hptr;
1618 struct utsname myname;
1622 /* Open our socket */
1624 if ( (listenfd = aim_listenestablish(port)) == -1)
1627 /* get our local IP */
1629 if (uname(&myname) < 0)
1631 if ( (hptr = gethostbyname(myname.nodename)) == NULL)
1633 memcpy(&d, hptr->h_addr_list[0], 4);
1635 aim_putcap(cap, 16, AIM_CAPS_GETFILE);
1637 /* create the OSCAR packet */
1639 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(destsn)+4+4+0x42)))
1641 newpacket->lock = 1;
1645 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
1647 /* XXX: check the cookie before commiting to using it */
1649 /* Generate a random message cookie
1650 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */
1652 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 10));
1654 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1656 /* grab all the data for cookie caching. */
1658 if (!(cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t))))
1660 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
1661 cookie->type = AIM_COOKIETYPE_OFTGET;
1663 if (!(priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv))))
1665 memcpy(priv->cookie, cookie, 8);
1666 memcpy(priv->sn, destsn, sizeof(priv->sn));
1667 memcpy(priv->fh.name, "listing.txt", strlen("listing.txt"));
1670 cookie->data = priv;
1672 aim_cachecookie(sess, cookie);
1675 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
1677 /* Destination SN (prepended with byte length) */
1678 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
1679 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
1680 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1681 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1684 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1685 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0042);
1687 /* Flag data / ICBM Parameters? */
1688 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1689 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1692 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
1694 /* Capability String */
1695 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
1697 /* 000a/0002 : 0001 */
1698 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
1699 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1700 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
1702 /* 0003/0004: IP address */
1703 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1704 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
1705 for(i = 0; i < 4; i++)
1706 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]);
1708 /* already in network byte order */
1710 /* 0005/0002: Port */
1711 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1712 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1713 curbyte += aimutil_put16(newpacket->data+curbyte, port);
1716 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
1717 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1720 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
1721 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c);
1722 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00120001);
1724 for(i = 0; i < 0x000c - 4; i++)
1725 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1727 newpacket->commandlen = curbyte;
1728 newpacket->lock = 0;
1729 aim_tx_enqueue(sess, newpacket);
1731 /* allocate and set up our connection */
1733 i = fcntl(listenfd, F_GETFL, 0);
1734 fcntl(listenfd, F_SETFL, i | O_NONBLOCK);
1735 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL);
1738 perror("aim_newconn");
1742 newconn->fd = listenfd;
1743 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
1744 newconn->priv = priv;
1745 faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
1751 * aim_oft_getfile_request - request a particular file over an established getfile connection
1752 * @sess: your session
1753 * @conn: the established OFT getfile connection
1754 * @name: filename to request
1755 * @size: size of the file
1758 * returns -1 on error, 0 on successful enqueuing
1760 faim_export int aim_oft_getfile_request(struct aim_session_t *sess, struct aim_conn_t *conn, const unsigned char *name, const int size)
1762 struct command_tx_struct *newoft;
1763 struct aim_filetransfer_priv *ft;
1764 if (!sess || !conn || !conn->priv || !name)
1767 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120c, 0))) {
1768 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1774 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1775 newoft->hdr.oft.hdr2len = 0x100 - 8;
1777 ft = (struct aim_filetransfer_priv *)conn->priv;
1778 ft->fh.filesleft = 1;
1779 ft->fh.totfiles = 1;
1780 ft->fh.totparts = 1;
1781 ft->fh.partsleft = 1;
1782 ft->fh.totsize = size;
1784 ft->fh.checksum = 0;
1785 memcpy(ft->fh.name, name, strlen(name));
1786 memset(ft->fh.name+strlen(name), 0, 1);
1788 if (!(newoft->hdr.oft.hdr2 = (unsigned char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1790 aim_tx_destroy(newoft);
1794 if (!(aim_oft_buildheader(newoft->hdr.oft.hdr2, &(ft->fh)))) {
1796 aim_tx_destroy(newoft);
1802 aim_tx_enqueue(sess, newoft);
1807 * aim_oft_getfile_ack - acknowledge a getfile download as complete
1808 * @sess: your session
1809 * @conn: the getfile conn to send the ack over
1811 * Call this function after you have read all the data in a particular
1812 * filetransfer. Returns -1 on error, 0 on apparent success
1815 faim_export int aim_oft_getfile_ack(struct aim_session_t *sess, struct aim_conn_t *conn)
1817 struct command_tx_struct *newoft;
1818 struct aim_filetransfer_priv *ft;
1820 if (!sess || !conn || !conn->priv)
1823 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) {
1824 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1830 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1831 newoft->hdr.oft.hdr2len = 0x100-8;
1833 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1835 aim_tx_destroy(newoft);
1839 ft = (struct aim_filetransfer_priv *)conn->priv;
1841 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1843 aim_tx_destroy(newoft);
1848 aim_tx_enqueue(sess, newoft);
1853 * aim_oft_getfile_end - end a getfile.
1854 * @sess: your session
1855 * @conn: the getfile connection
1857 * call this before you close the getfile connection if you're on the
1858 * receiving/requesting end.
1860 faim_export int aim_oft_getfile_end(struct aim_session_t *sess, struct aim_conn_t *conn)
1862 struct command_tx_struct *newoft;
1863 struct aim_filetransfer_priv *ft;
1865 if (!sess || !conn || !conn->priv)
1868 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0204, 0))) {
1869 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1875 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1876 newoft->hdr.oft.hdr2len = 0x100 - 8;
1878 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1880 aim_tx_destroy(newoft);
1884 ft = (struct aim_filetransfer_priv *)conn->priv;
1885 ft->state = 4; /* no longer wanting data */
1886 ft->fh.nrecvd = ft->fh.size;
1887 ft->fh.recvcsum = ft->fh.checksum;
1888 ft->fh.flags = 0x21;
1890 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1892 aim_tx_destroy(newoft);
1897 aim_tx_enqueue(sess, newoft);