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 int listenestablish(fu16_t portnum);
25 static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr);
28 * aim_handlerendconnect - call this to accept OFT connections and set up the requisite structures
30 * @cur: the conn the incoming connection is on
32 * call this when you get an outstanding read on a conn with subtype
33 * AIM_CONN_SUBTYPE_RENDEZVOUS_OUT, it will clone the current
34 * &aim_conn_t and tweak things as appropriate. the new conn and the
35 * listener conn are both returned to the client in the
36 * %AIM_CB_FAM_OFT, %AIM_CB_OFT_<CLASS>INITIATE callback.
38 faim_export int aim_handlerendconnect(aim_session_t *sess, aim_conn_t *cur)
41 struct sockaddr cliaddr;
42 int clilen = sizeof(cliaddr);
46 if ((acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1)
47 return 0; /* not an error */
49 if (cliaddr.sa_family != AF_INET) { /* just in case IPv6 really is happening */
55 if (!(newconn = aim_cloneconn(sess, cur))) {
61 newconn->type = AIM_CONN_TYPE_RENDEZVOUS;
62 newconn->fd = acceptfd;
64 if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) {
65 struct aim_directim_intdata *priv;
66 aim_rxcallback_t userfunc;
68 priv = (struct aim_directim_intdata *)(newconn->internal = cur->internal);
71 snprintf(priv->ip, sizeof(priv->ip), "%s:%u",
72 inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr),
73 ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
75 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE)))
76 ret = userfunc(sess, NULL, newconn, cur);
78 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE) {
80 struct aim_filetransfer_priv *priv;
81 aim_rxcallback_t userfunc;
84 newconn->priv = cur->priv;
86 priv = (struct aim_filetransfer_priv *)newconn->priv;
88 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
90 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE)))
91 ret = userfunc(sess, NULL, newconn, cur);
94 faimdprintf(sess, 1,"Got a Connection on a listener that's not Rendezvous(??!) Closing conn.\n");
95 aim_conn_close(newconn);
103 * aim_send_im_direct - send IM client-to-client over established connection
104 * @sess: session to conn
105 * @conn: directim connection
106 * @msg: null-terminated string to send; if this is NULL, it will send a "typing" notice.
108 * Call this just like you would aim_send_im, to send a directim. You
109 * _must_ have previously established the directim connection.
111 faim_export int aim_send_im_direct(aim_session_t *sess, aim_conn_t *conn, const char *msg)
113 struct aim_directim_intdata *intdata = (struct aim_directim_intdata *)conn->internal;
115 aim_bstream_t hdrbs; /* XXX this should be within aim_frame_t */
117 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS))
120 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x01, strlen(msg))))
123 memcpy(fr->hdr.oft.magic, "ODC2", 4);
125 fr->hdr.oft.hdr2len = 0x44;
127 if (!(fr->hdr.oft.hdr2 = calloc(1, fr->hdr.oft.hdr2len))) {
128 aim_frame_destroy(fr);
132 aim_bstream_init(&hdrbs, fr->hdr.oft.hdr2, fr->hdr.oft.hdr2len);
134 aimbs_put16(&hdrbs, 0x0006);
135 aimbs_put16(&hdrbs, 0x0000);
136 aimbs_putraw(&hdrbs, intdata->cookie, 8);
137 aimbs_put16(&hdrbs, 0x0000);
138 aimbs_put16(&hdrbs, 0x0000);
139 aimbs_put16(&hdrbs, 0x0000);
140 aimbs_put16(&hdrbs, 0x0000);
141 aimbs_put32(&hdrbs, strlen(msg));
142 aimbs_put16(&hdrbs, 0x0000);
143 aimbs_put16(&hdrbs, 0x0000);
144 aimbs_put16(&hdrbs, 0x0000);
146 /* flags -- 0x000e for "typing", 0x0000 for message */
147 aimbs_put16(&hdrbs, msg ? 0x0000 : 0x000e);
149 aimbs_put16(&hdrbs, 0x0000);
150 aimbs_put16(&hdrbs, 0x0000);
151 aimbs_putraw(&hdrbs, sess->sn, strlen(sess->sn));
153 aim_bstream_setpos(&hdrbs, 52); /* bleeehh */
155 aimbs_put8(&hdrbs, 0x00);
156 aimbs_put16(&hdrbs, 0x0000);
157 aimbs_put16(&hdrbs, 0x0000);
158 aimbs_put16(&hdrbs, 0x0000);
159 aimbs_put16(&hdrbs, 0x0000);
160 aimbs_put16(&hdrbs, 0x0000);
161 aimbs_put16(&hdrbs, 0x0000);
162 aimbs_put16(&hdrbs, 0x0000);
167 #if 0 /* XXX this is how you send buddy icon info... */
168 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008);
169 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c);
170 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
171 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466);
172 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001);
173 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f);
174 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e);
175 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8);
177 aimbs_putraw(&fr->data, msg, strlen(msg));
180 aim_tx_enqueue(sess, fr);
185 /* XXX: give the client author the responsibility of setting up a
186 * listener, then we no longer have a libfaim problem with broken
187 * solaris *innocent smile* -jbm */
189 static int getlocalip(fu8_t *ip)
191 struct hostent *hptr;
194 /* XXX if available, use getaddrinfo() */
195 /* XXX allow client to specify which IP to use for multihomed boxes */
197 if (gethostname(localhost, 128) < 0)
200 if (!(hptr = gethostbyname(localhost)))
203 memcpy(ip, hptr->h_addr_list[0], 4);
208 /* XXX this should probably go in im.c */
209 static int aim_request_directim(aim_session_t *sess, aim_conn_t *conn, const char *destsn, fu8_t *ip, fu16_t port, fu8_t *ckret)
214 aim_tlvlist_t *tl = NULL, *itl = NULL;
219 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 256+strlen(destsn))))
222 snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
223 aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
226 * Generate a random message cookie
228 * This cookie needs to be alphanumeric and NULL-terminated to be
231 * XXX have I mentioned these should be generated in msgcookie.c?
234 for (i = 0; i < 7; i++)
235 ck[i] = 0x30 + ((fu8_t) rand() % 10);
239 memcpy(ckret, ck, 8);
242 aimbs_putraw(&fr->data, ck, 8);
245 aimbs_put16(&fr->data, 0x0002);
248 aimbs_put8(&fr->data, strlen(destsn));
249 aimbs_putraw(&fr->data, destsn, strlen(destsn));
251 aim_addtlvtochain_noval(&tl, 0x0003);
253 hdrlen = 2+8+16+6+8+6+4;
254 hdr = malloc(hdrlen);
255 aim_bstream_init(&hdrbs, hdr, hdrlen);
257 aimbs_put16(&hdrbs, 0x0000);
258 aimbs_putraw(&hdrbs, ck, 8);
259 aim_putcap(&hdrbs, AIM_CAPS_IMIMAGE);
261 aim_addtlvtochain16(&itl, 0x000a, 0x0001);
262 aim_addtlvtochain_raw(&itl, 0x0003, 4, ip);
263 aim_addtlvtochain16(&itl, 0x0005, port);
264 aim_addtlvtochain_noval(&itl, 0x000f);
266 aim_writetlvchain(&hdrbs, &itl);
268 aim_addtlvtochain_raw(&tl, 0x0005, aim_bstream_curpos(&hdrbs), hdr);
270 aim_writetlvchain(&fr->data, &tl);
273 aim_freetlvchain(&itl);
274 aim_freetlvchain(&tl);
276 aim_tx_enqueue(sess, fr);
282 * aim_directim_intitiate - For those times when we want to open up the directim channel ourselves.
283 * @sess: your session,
284 * @conn: the BOS conn,
285 * @priv: a dummy priv value (we'll let it get filled in later) (if you pass a %NULL, we alloc one)
286 * @destsn: the SN to connect to.
289 faim_export aim_conn_t *aim_directim_initiate(aim_session_t *sess, aim_conn_t *conn, const char *destsn)
292 aim_msgcookie_t *cookie;
293 struct aim_directim_intdata *priv;
299 if (getlocalip(localip) == -1)
302 if ((listenfd = listenestablish(port)) == -1)
305 aim_request_directim(sess, conn, destsn, localip, port, ck);
307 cookie = (aim_msgcookie_t *)calloc(1, sizeof(aim_msgcookie_t));
308 memcpy(cookie->cookie, ck, 8);
309 cookie->type = AIM_COOKIETYPE_OFTIM;
311 priv = (struct aim_directim_intdata *)calloc(1, sizeof(struct aim_directim_intdata));
313 memcpy(priv->cookie, ck, 8);
314 memcpy(priv->sn, destsn, sizeof(priv->sn));
316 aim_cachecookie(sess, cookie);
318 /* XXX switch to aim_cloneconn()? */
319 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL))) {
324 /* this one is for the conn */
325 priv = (struct aim_directim_intdata *)calloc(1, sizeof(struct aim_directim_intdata));
327 memcpy(priv->cookie, ck, 8);
328 memcpy(priv->sn, destsn, sizeof(priv->sn));
330 newconn->fd = listenfd;
331 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
332 newconn->internal = priv;
333 newconn->lastactivity = time(NULL);
335 faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
342 * unsigned int aim_oft_listener_clean - close up old listeners
343 * @sess: session to clean up in
344 * @age: maximum age in seconds
346 * returns number closed, -1 on error.
348 faim_export unsigned int aim_oft_listener_clean(struct aim_session_t *sess, time_t age)
350 struct aim_conn_t *cur;
352 unsigned int hit = 0;
357 faim_mutex_lock(&sess->connlistlock);
358 for(cur = sess->connlist;cur; cur = cur->next)
359 if (cur->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) {
360 faim_mutex_lock(&cur->active);
361 if (cur->lastactivity < (now - age) ) {
362 faim_mutex_unlock(&cur->active);
366 faim_mutex_unlock(&cur->active);
368 faim_mutex_unlock(&sess->connlistlock);
373 faim_export const char *aim_directim_getsn(aim_conn_t *conn)
375 struct aim_directim_intdata *intdata;
380 if ((conn->type != AIM_CONN_TYPE_RENDEZVOUS) ||
381 (conn->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM))
387 intdata = (struct aim_directim_intdata *)conn->internal;
393 * aim_directim_connect - connect to buddy for directim
394 * @sess: the session to append the conn to,
395 * @sn: the SN we're connecting to
396 * @addr: address to connect to
398 * This is a wrapper for aim_newconn.
400 * If addr is NULL, the socket is not created, but the connection is
401 * allocated and setup to connect.
404 faim_export aim_conn_t *aim_directim_connect(aim_session_t *sess, const char *sn, const char *addr, const fu8_t *cookie)
407 struct aim_directim_intdata *intdata;
412 if (!(intdata = malloc(sizeof(struct aim_directim_intdata))))
414 memset(intdata, 0, sizeof(struct aim_directim_intdata));
416 memcpy(intdata->cookie, cookie, 8);
417 strncpy(intdata->sn, sn, sizeof(intdata->sn));
418 strncpy(intdata->ip, addr, sizeof(intdata->ip));
420 /* XXX verify that non-blocking connects actually work */
421 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, addr))) {
426 if (!newconn || (newconn->fd == -1)) {
431 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
432 newconn->internal = intdata;
438 * aim_directim_getconn - find a directim conn for buddy name
439 * @sess: your session,
440 * @name: the name to get,
442 * returns conn for directim with name, %NULL if none found.
445 faim_export aim_conn_t *aim_directim_getconn(aim_session_t *sess, const char *name)
449 if (!sess || !name || !strlen(name))
452 for (cur = sess->connlist; cur; cur = cur->next) {
453 struct aim_directim_intdata *intdata;
455 if ((cur->type != AIM_CONN_TYPE_RENDEZVOUS) || (cur->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM))
458 intdata = cur->internal;
460 if (aim_sncmp(intdata->sn, name) == 0)
468 * aim_accepttransfer - accept a file transfer request
469 * @sess: the session,
470 * @conn: the BOS conn for the CAP reply
471 * @sn: the screenname to send it to,
472 * @cookie: the cookie used
473 * @ip: the ip to connect to
474 * @listingfiles: number of files to share
475 * @listingtotsize: total size of shared files
476 * @listingsize: length of the listing file(buffer)
477 * @listingchecksum: checksum of the listing
478 * @rendid: capability type (%AIM_CAPS_GETFILE or %AIM_CAPS_SENDFILE)
480 * Returns new connection or %NULL on error.
482 * XXX this should take a struct.
484 faim_export aim_conn_t *aim_accepttransfer(aim_session_t *sess,
486 const char *sn, const fu8_t *cookie,
489 fu16_t listingtotsize,
491 fu32_t listingchecksum,
496 struct command_tx_struct *newpacket, *newoft;
497 struct aim_conn_t *newconn;
498 struct aim_fileheader_t *fh;
499 struct aim_filetransfer_priv *priv;
500 struct aim_msgcookie_t *cachedcook;
503 if (!sess || !conn || !sn || !cookie || !ip) {
507 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, ip);
509 if (!newconn || (newconn->fd == -1)) {
510 perror("aim_newconn");
511 faimdprintf(sess, 2, "could not connect to %s (fd: %i)\n", ip, newconn?newconn->fd:0);
514 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
516 memcpy(priv->cookie, cookie, 8);
518 strncpy(priv->sn, sn, MAXSNLEN);
519 strncpy(priv->ip, ip, sizeof(priv->ip));
520 newconn->priv = (void *)priv;
522 faimdprintf(sess, 2, "faim: connected to peer (fd = %d)\n", newconn->fd);
525 if (rendid == AIM_CAPS_GETFILE) {
526 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
528 faimdprintf(sess, 2, "faim: getfile request accept\n");
530 if (!(newoft = aim_tx_new(sess, newconn, AIM_FRAMETYPE_OFT, 0x1108, 0))) {
531 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
532 /* XXX: conn leak here */
537 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
538 newoft->hdr.oft.hdr2len = 0x100 - 8;
540 if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t)))) {
541 /* XXX: conn leak here */
546 fh->encrypt = 0x0000;
547 fh->compress = 0x0000;
548 fh->totfiles = listingfiles;
549 fh->filesleft = listingfiles; /* is this right -- total parts and parts left?*/
550 fh->totparts = 0x0001;
551 fh->partsleft = 0x0001;
552 fh->totsize = listingtotsize;
553 fh->size = listingsize; /* ls -l listing.txt */
554 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
555 fh->checksum = listingchecksum;
556 fh->rfcsum = 0x00000000;
557 fh->rfsize = 0x00000000;
558 fh->cretime = 0x00000000;
559 fh->rfcsum = 0x00000000;
560 fh->nrecvd = 0x00000000;
561 fh->recvcsum = 0x00000000;
562 memset(fh->idstring, 0, sizeof(fh->idstring));
563 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
565 fh->lnameoffset = 0x1a;
566 fh->lsizeoffset = 0x10;
567 memset(fh->dummy, 0, sizeof(fh->dummy));
568 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
570 /* we need to figure out these encodings for filenames */
571 fh->nencode = 0x0000;
572 fh->nlanguage = 0x0000;
573 memset(fh->name, 0, sizeof(fh->name));
574 memcpy(fh->name, "listing.txt", sizeof(fh->name));
576 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
578 aim_frame_destroy(newoft);
580 perror("calloc (1)");
584 memcpy(fh->bcookie, cookie, 8);
586 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, fh)))
587 faimdprintf(sess, 1, "eek, bh fail!\n");
590 aim_tx_enqueue(sess, newoft);
592 if (!(cachedcook = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) {
593 faimdprintf(sess, 1, "faim: accepttransfer: couldn't calloc cachedcook. yeep!\n");
594 /* XXX: more cleanup, conn leak */
595 perror("calloc (2)");
599 memcpy(&(priv->fh), fh, sizeof(struct aim_fileheader_t));
600 memcpy(cachedcook->cookie, cookie, 8);
602 cachedcook->type = AIM_COOKIETYPE_OFTGET;
603 cachedcook->data = (void *)priv;
605 if (aim_cachecookie(sess, cachedcook) == -1)
606 faimdprintf(sess, 1, "faim: ERROR caching message cookie\n");
610 /* OSCAR CAP accept packet */
612 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sn)+4+2+8+16))) {
620 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
622 for (i = 0; i < 8; i++)
623 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
625 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
626 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
627 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
628 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
629 curbyte += aimutil_put16(newpacket->data+curbyte, 0x001a);
630 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept*/);
632 for (i = 0;i < 8; i++)
633 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
635 curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid);
637 aim_tx_enqueue(sess, newpacket);
644 * aim_getlisting(FILE *file) -- get an aim_fileheader_t for a given FILE*
645 * @file is an opened listing file
647 * returns a pointer to the filled-in fileheader_t
649 * Currently omits checksum. we'll fix this when AOL breaks us, i
653 faim_export struct aim_fileheader_t *aim_getlisting(aim_session_t *sess, FILE *file)
657 struct aim_fileheader_t *fh;
658 u_long totsize = 0, size = 0, checksum = 0xffff0000;
660 char *linebuf, sizebuf[9];
662 int linelength = 1024;
664 /* XXX: if we have a line longer than 1024chars, God help us. */
665 if ( (linebuf = (char *)calloc(1, linelength)) == NULL ) {
666 faimdprintf(sess, 2, "linebuf calloc failed\n");
670 if (fseek(file, 0, SEEK_END) == -1) { /* use this for sanity check */
671 perror("getlisting END1 fseek:");
672 faimdprintf(sess, 2, "getlising fseek END1 error\n");
675 if ((size = ftell(file)) == -1) {
676 perror("getlisting END1 getpos:");
677 faimdprintf(sess, 2, "getlising getpos END1 error\n");
680 if (fseek(file, 0, SEEK_SET) != 0) {
681 perror("getlesting fseek(SET):");
682 faimdprintf(sess, 2, "faim: getlisting: couldn't seek to beginning of listing file\n");
685 memset(linebuf, 0, linelength);
689 while(fgets(linebuf, linelength, file)) {
691 memset(sizebuf, 0, 9);
693 size += strlen(linebuf);
695 if (strlen(linebuf) < 23) {
696 faimdprintf(sess, 2, "line \"%s\" too short. skipping\n", linebuf);
699 if (linebuf[strlen(linebuf)-1] != '\n') {
700 faimdprintf(sess, 2, "faim: OFT: getlisting -- hit EOF or line too long!\n");
703 memcpy(sizebuf, linebuf+17, 8);
705 totsize += strtol(sizebuf, NULL, 10);
706 memset(linebuf, 0, linelength);
709 if (fseek(file, 0, SEEK_SET) == -1) {
710 perror("getlisting END2 fseek:");
711 faimdprintf(sess, 2, "getlising fseek END2 error\n");
716 /* we're going to ignore checksumming the data for now -- that
717 * requires walking the whole listing.txt. it should probably be
718 * done at register time and cached, but, eh. */
720 if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t))))
723 fh->encrypt = 0x0000;
724 fh->compress = 0x0000;
725 fh->totfiles = totfiles;
726 fh->filesleft = totfiles; /* is this right ?*/
727 fh->totparts = 0x0001;
728 fh->partsleft = 0x0001;
729 fh->totsize = totsize;
730 fh->size = size; /* ls -l listing.txt */
731 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
732 fh->checksum = checksum; /* XXX: checksum ! */
733 fh->rfcsum = 0x00000000;
734 fh->rfsize = 0x00000000;
735 fh->cretime = 0x00000000;
736 fh->rfcsum = 0x00000000;
737 fh->nrecvd = 0x00000000;
738 fh->recvcsum = 0x00000000;
740 /* memset(fh->idstring, 0, sizeof(fh->idstring)); */
741 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
742 memset(fh->idstring+strlen(fh->idstring), 0, sizeof(fh->idstring)-strlen(fh->idstring));
745 fh->lnameoffset = 0x1a;
746 fh->lsizeoffset = 0x10;
748 /* memset(fh->dummy, 0, sizeof(fh->dummy)); */
749 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
751 fh->nencode = 0x0000; /* we need to figure out these encodings for filenames */
752 fh->nlanguage = 0x0000;
754 /* memset(fh->name, 0, sizeof(fh->name)); */
755 memcpy(fh->name, "listing.txt", sizeof(fh->name));
756 memset(fh->name+strlen(fh->name), 0, 64-strlen(fh->name));
758 faimdprintf(sess, 2, "faim: OFT: listing fh name %s / %s\n", fh->name, (fh->name+(strlen(fh->name))));
764 * aim_listenestablish - create a listening socket on a port.
765 * @portnum: the port number to bind to.
767 * you need to call accept() when it's connected. returns your fd
770 static int listenestablish(fu16_t portnum)
772 #if defined(__linux__)
773 /* XXX what other OS's support getaddrinfo? */
776 struct addrinfo hints, *res, *ressave;
779 snprintf(serv, sizeof(serv), "%d", portnum);
780 memset(&hints, 0, sizeof(struct addrinfo));
781 hints.ai_flags = AI_PASSIVE;
782 hints.ai_family = AF_UNSPEC;
783 hints.ai_socktype = SOCK_STREAM;
784 if (getaddrinfo(NULL /*any IP*/, serv, &hints, &res) != 0) {
785 perror("getaddrinfo");
790 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
793 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
794 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
798 } while ( (res = res->ai_next) );
803 if (listen(listenfd, 1024)!=0) {
808 freeaddrinfo(ressave);
813 struct sockaddr_in sockin;
815 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
816 perror("socket(listenfd)");
820 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) != 0)) {
821 perror("setsockopt(listenfd)");
826 memset(&sockin, 0, sizeof(struct sockaddr_in));
827 sockin.sin_family = AF_INET;
828 sockin.sin_port = htons(portnum);
830 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
831 perror("bind(listenfd)");
835 if (listen(listenfd, 4) != 0) {
836 perror("listen(listenfd)");
844 static int getcommand_getfile(aim_session_t *sess, aim_conn_t *conn)
847 struct aim_filetransfer_priv *ft;
848 aim_rxcallback_t userfunc;
851 if (ft->state == 2) {
852 /* waiting on listing data */
855 struct command_tx_struct *newoft;
857 if (!(listing = malloc(ft->fh.size)))
861 if (aim_recv(conn->fd, listing, ft->fh.size) != ft->fh.size)
862 faimdprintf(sess, 2, "OFT get: file %s was short. (0x%lx)\n", ft->fh.name, ft->fh.size);
864 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120b, 0))) {
865 faimdprintf(sess, 2, "faim: aim_get_command_rendezvous: getfile listing: tx_new OFT failed\n");
866 faim_mutex_unlock(&conn->active);
868 aim_conn_close(conn);
872 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
873 newoft->hdr.oft.hdr2len = 0x100 - 8;
875 /* Protocol BS - set nrecvd to size of listing, recvcsum to listing checksum, flags to 0 */
877 ft->fh.nrecvd = ft->fh.size;
878 ft->fh.recvcsum = ft->fh.checksum;
881 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
882 aim_frame_destroy(newoft);
887 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh))))
888 faimdprintf(sess, 2, "eek! bh fail listing\n");
891 aim_tx_enqueue(sess, newoft);
892 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTING)) )
893 ret = userfunc(sess, NULL, conn, ft, listing);
899 if (ft->state == 3) {
900 /* waiting on file data */
901 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILERECEIVE)) )
902 return userfunc(sess, NULL, conn, ft);
906 if (ft->state == 4) {
907 if( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILESTATE4)) )
908 return userfunc(sess, NULL, conn);
909 aim_conn_close(conn);
919 static void disconnected_sendfile(aim_session_t *sess, aim_conn_t *conn)
922 aim_rxcallback_t userfunc;
923 aim_msgcookie_t *cook;
924 struct aim_filetransfer_priv *priv = (struct aim_filetransfer_priv *)conn->priv;
926 cook = aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTSEND);
927 aim_cookie_free(sess, cook);
931 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_SENDFILEDISCONNECT)) )
932 userfunc(sess, &fr, priv->sn);
937 static void disconnected_getfile(aim_session_t *sess, aim_conn_t *conn)
940 aim_rxcallback_t userfunc;
941 aim_msgcookie_t *cook;
942 struct aim_filetransfer_priv *priv = (struct aim_filetransfer_priv *)conn->priv;
944 cook = aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTGET);
945 aim_cookie_free(sess, cook);
949 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT)) )
950 userfunc(sess, &fr, priv->sn);
955 static void disconnected_directim(aim_session_t *sess, aim_conn_t *conn)
958 struct aim_directim_intdata *intdata = (struct aim_directim_intdata *)conn->internal;
959 aim_rxcallback_t userfunc;
960 aim_msgcookie_t *cook;
962 cook = aim_uncachecookie(sess, intdata->cookie, AIM_COOKIETYPE_OFTIM);
963 aim_cookie_free(sess, cook);
967 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT)) )
968 userfunc(sess, &fr, intdata->sn);
973 static int handlehdr_directim(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
976 aim_rxcallback_t userfunc;
977 fu32_t payloadlength;
983 payloadlength = aimutil_get32(hdr+22);
984 flags = aimutil_get16(hdr+32);
985 snptr = (char *)hdr+38;
987 faimdprintf(sess, 2, "faim: OFT frame: handlehdr_directim: %04x / %04x / %s\n", payloadlength, flags, snptr);
989 if (flags == 0x000e) {
992 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)))
993 ret = userfunc(sess, &fr, snptr);
997 } else if ((flags == 0x0000) && payloadlength) {
1001 if (!(msg = calloc(1, payloadlength+1)))
1004 if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) {
1009 msg[payloadlength] = '\0';
1011 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) )
1012 ret = userfunc(sess, &fr, snptr, msg);
1022 static int handlehdr_getfile_listing(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1025 struct aim_filetransfer_priv *ft;
1026 struct aim_fileheader_t *fh;
1027 struct aim_msgcookie_t *cook;
1028 struct command_tx_struct *newoft;
1029 aim_rxcallback_t userfunc;
1031 faimdprintf(sess, 2,"faim: rend: fileget 0x1108\n");
1032 fh = aim_oft_getfh(hdr);
1034 faim_mutex_unlock(&conn->active);
1036 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1043 /* we're waaaaiiiting.. for listing.txt */
1046 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1049 if(aim_cachecookie(sess, cook) == -1) {
1050 faimdprintf(sess, 1, "error caching cookie\n");
1054 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x1209, 0))) {
1055 aim_conn_close(conn);
1059 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1060 newoft->hdr.oft.hdr2len = 0x100 - 8;
1062 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1064 aim_frame_destroy(newoft);
1068 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1070 aim_frame_destroy(newoft);
1075 aim_tx_enqueue(sess, newoft);
1080 static int handlehdr_getfile_listing2(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1083 struct aim_filetransfer_priv *ft;
1084 struct aim_fileheader_t *fh;
1085 struct aim_msgcookie_t *cook;
1087 aim_rxcallback_t userfunc;
1089 fh = aim_oft_getfh(hdr);
1091 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET)))
1092 faimdprintf(sess, 2, "shit, no cookie in 0x1209. (%i/%s)going to crash..\n", AIM_COOKIETYPE_OFTGET, fh->bcookie);
1096 if (ft->fh.size != fh->size)
1097 faimdprintf(sess, 2, "hrm. ft->fh.size (%ld) != fh->size (%ld). um. using ft->fh.size\n", ft->fh.size, fh->size);
1099 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ)))
1100 ret = userfunc(sess, NULL, conn, fh);
1102 faimdprintf(sess, 2, "faim: get_command_rendezvous: hit end of 1209\n");
1112 static int handlehdr_getfile_listing3(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1115 struct aim_filetransfer_priv *ft;
1116 struct aim_msgcookie_t *cook;
1117 struct aim_fileheader_t *fh;
1118 aim_rxcallback_t userfunc;
1120 fh = aim_oft_getfh(hdr);
1122 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1131 if (aim_cachecookie(sess, cook) == -1)
1134 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGRXCONFIRM)))
1135 return userfunc(sess, NULL, conn);
1140 static int handlehdr_getfile_request(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1143 struct aim_filetransfer_priv *ft;
1144 struct aim_msgcookie_t *cook;
1145 struct aim_fileheader_t *fh;
1146 struct command_tx_struct *newoft;
1148 aim_rxcallback_t userfunc;
1150 fh = aim_oft_getfh(hdr);
1152 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1158 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1161 aim_cachecookie(sess, cook);
1163 faimdprintf(sess, 2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name);
1165 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) )
1166 i = userfunc(sess, NULL, conn, &(ft->fh), cook->cookie);
1171 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0101, 0))) {
1172 faimdprintf(sess, 2, "faim: send_final_transfer: tx_new OFT failed\n");
1177 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1178 newoft->hdr.oft.hdr2len = 0x100 - 8;
1180 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1181 aim_frame_destroy(newoft);
1185 /* protocol BS: nrecvd, recvcsum to 0, flags to 0x20. */
1187 ft->fh.recvcsum = 0;
1188 ft->fh.flags = 0x20;
1190 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
1193 aim_tx_enqueue(sess, newoft);
1195 faimdprintf(sess, 2, "faim: OFT: OFT file header enqueued.\n");
1203 static int handlehdr_getfile_sending(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1206 struct aim_fileheader_t *fh;
1207 struct aim_filetransfer_priv *ft;
1208 struct aim_msgcookie_t *cook;
1209 struct command_tx_struct *newoft;
1210 aim_rxcallback_t userfunc;
1212 fh = aim_oft_getfh(hdr);
1214 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1225 if (aim_cachecookie(sess, cook) == -1)
1228 faimdprintf(sess, 2, "faim: fileget: %s seems to want to send %s\n", ft->sn, ft->fh.name);
1230 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) {
1231 faimdprintf(sess, 2, "faim: send_final_transfer: tx_new OFT failed\n");
1236 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1238 newoft->hdr.oft.hdr2len = 0x100 - 8;
1240 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1241 aim_frame_destroy(newoft);
1245 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
1248 aim_tx_enqueue(sess, newoft);
1250 faimdprintf(sess, 2, "faim: OFT: OFT 0x0202 enqueued.\n");
1252 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL)
1259 static int handlehdr_getfile_recv(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1262 struct aim_fileheader_t *fh;
1263 struct aim_filetransfer_priv *ft;
1264 struct aim_msgcookie_t *cook;
1266 aim_rxcallback_t userfunc;
1268 fh = aim_oft_getfh(hdr);
1270 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1277 faimdprintf(sess, 2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n");
1279 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) )
1280 ret = userfunc(sess, NULL, conn, fh);
1290 static int handlehdr_getfile_finish(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1293 struct aim_fileheader_t *fh;
1294 aim_rxcallback_t userfunc;
1296 fh = aim_oft_getfh(hdr);
1298 faimdprintf(sess, 2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n");
1300 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) )
1301 userfunc(sess, NULL, conn, fh);
1310 * aim_get_command_rendezvous - OFT equivalent of aim_get_command
1311 * @sess: session to work on
1312 * @conn: conn to pull data from
1314 * this reads and handles data from conn->fd. currently a little rough
1317 faim_internal int aim_get_command_rendezvous(aim_session_t *sess, aim_conn_t *conn)
1321 int hdrlen, hdrtype;
1327 memset(hdrbuf1, 0, sizeof(hdrbuf1));
1329 /* I guess? I didn't understand any of that mess... */
1330 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE)
1331 return getcommand_getfile(sess, conn);
1333 /* XXX fix all the error cases here */
1334 if (aim_recv(conn->fd, hdrbuf1, 6) < 6) {
1336 faimdprintf(sess, 2, "faim: rend: read error (fd: %i)\n", conn->fd);
1338 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)
1339 disconnected_directim(sess, conn);
1340 else if (conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE)
1341 disconnected_getfile(sess, conn);
1342 else if (conn->subtype == AIM_CONN_SUBTYPE_OFT_SENDFILE)
1343 disconnected_sendfile(sess, conn);
1345 aim_conn_close(conn);
1350 hdrlen = aimutil_get16(hdrbuf1+4);
1353 hdr = malloc(hdrlen);
1355 if (aim_recv(conn->fd, hdr, hdrlen) < hdrlen) {
1356 faimdprintf(sess, 2, "faim: rend: read2 error on %d (%d)\n", conn->fd, hdrlen);
1358 aim_conn_close(conn);
1362 hdrtype = aimutil_get16(hdr);
1364 if (hdrtype == 0x0001)
1365 ret = handlehdr_directim(sess, conn, hdr);
1366 else if (hdrtype == 0x1108) /* getfile listing.txt incoming tx->rx */
1367 ret = handlehdr_getfile_listing(sess, conn, hdr);
1368 else if (hdrtype == 0x1209) /* get file listing ack rx->tx */
1369 ret = handlehdr_getfile_listing2(sess, conn, hdr);
1370 else if (hdrtype == 0x120b) /* get file listing rx confirm */
1371 ret = handlehdr_getfile_listing3(sess, conn, hdr);
1372 else if (hdrtype == 0x120c) /* getfile request */
1373 ret = handlehdr_getfile_request(sess, conn, hdr);
1374 else if (hdrtype == 0x0101) /* getfile sending data */
1375 ret = handlehdr_getfile_sending(sess, conn, hdr);
1376 else if (hdrtype == 0x0202) /* getfile recv data */
1377 ret = handlehdr_getfile_recv(sess, conn, hdr);
1378 else if (hdrtype == 0x0204) /* getfile finished */
1379 ret = handlehdr_getfile_finish(sess, conn, hdr);
1381 faimdprintf(sess, 2,"faim: OFT frame: uknown type %04x\n", hdrtype);
1388 aim_conn_close(conn);
1395 * aim_oft_getfh - extracts an &aim_fileheader_t from buffer hdr.
1396 * @hdr: buffer to extract header from
1398 * returns pointer to new struct on success; %NULL on error.
1401 static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr)
1403 struct aim_fileheader_t *fh;
1405 if (!(fh = calloc(1, sizeof(struct aim_fileheader_t))))
1408 /* [0] and [1] are the type. we can ignore those here. */
1410 for(j = 0; j < 8; j++, i++)
1411 fh->bcookie[j] = hdr[i];
1412 fh->encrypt = aimutil_get16(hdr+i);
1414 fh->compress = aimutil_get16(hdr+i);
1416 fh->totfiles = aimutil_get16(hdr+i);
1418 fh->filesleft = aimutil_get16(hdr+i);
1420 fh->totparts = aimutil_get16(hdr+i);
1422 fh->partsleft = aimutil_get16(hdr+i);
1424 fh->totsize = aimutil_get32(hdr+i);
1426 fh->size = aimutil_get32(hdr+i);
1428 fh->modtime = aimutil_get32(hdr+i);
1430 fh->checksum = aimutil_get32(hdr+i);
1432 fh->rfrcsum = aimutil_get32(hdr+i);
1434 fh->rfsize = aimutil_get32(hdr+i);
1436 fh->cretime = aimutil_get32(hdr+i);
1438 fh->rfcsum = aimutil_get32(hdr+i);
1440 fh->nrecvd = aimutil_get32(hdr+i);
1442 fh->recvcsum = aimutil_get32(hdr+i);
1444 memcpy(fh->idstring, hdr+i, 32);
1446 fh->flags = aimutil_get8(hdr+i);
1448 fh->lnameoffset = aimutil_get8(hdr+i);
1450 fh->lsizeoffset = aimutil_get8(hdr+i);
1452 memcpy(fh->dummy, hdr+i, 69);
1454 memcpy(fh->macfileinfo, hdr+i, 16);
1456 fh->nencode = aimutil_get16(hdr+i);
1458 fh->nlanguage = aimutil_get16(hdr+i);
1460 memcpy(fh->name, hdr+i, 64);
1467 * aim_oft_checksum - calculate oft checksum of buffer
1468 * @buffer: buffer of data to checksum
1469 * @bufsize: size of buffer
1470 * @checksum: pointer to integer to place result in (pointer!)
1473 * Note that checksum is a pointer. Checksum should be filled with
1474 * 0xFFFF0000 for each new file; you can have this checksum chunks of
1475 * files in series if you just call it repeatedly in a for(; ; ) loop
1476 * and don't reset the checksum between each call. And you thought we
1477 * didn't care about you and your pathetic client's meomry footprint
1481 * Also, it's been said that this is incorrect as currently
1482 * written. You were warned.
1484 faim_export fu32_t aim_oft_checksum(aim_session_t *sess, const char *buffer, int bufsize, fu32_t *checksum)
1488 fu16_t check0, check1;
1491 check0 = ((*checksum & 0xFF000000) >> 16);
1492 check1 = ((*checksum & 0x00ff0000) >> 16);
1493 for(i = 0; i < bufsize; i++) {
1494 if (i % 2) { /* use check1 -- second byte */
1495 if ( (short)buffer[i] > check1 ) { /* wrapping */
1496 check1 += 0x100; /* this is a cheap way to wrap */
1498 /* if we're wrapping, decrement the other one */
1499 /* XXX: check this corner case */
1505 check1 -= buffer[i];
1506 } else { /* use check0 -- first byte */
1507 if ( (short)buffer[i] > check0 ) { /* wrapping */
1508 check0 += 0x100; /* this is a cheap way to wrap */
1510 /* if we're wrapping, decrement the other one */
1511 /* XXX: check this corner case */
1517 check0 -= buffer[i];
1521 if (check0 > 0xff || check1 > 0xff) {
1522 /* they shouldn't be able to do this. error! */
1523 faimdprintf(sess, 2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1);
1527 /* grab just the lowest byte; this should be clean, but just in
1532 *checksum = ((check0 * 0x1000000) + (check1 * 0x10000));
1539 * aim_oft_buildheader - fills a buffer with network-order fh data
1540 * @dest: buffer to fill -- pre-alloced
1541 * @fh: fh to get data from
1543 * returns length written; -1 on error.
1544 * DOES NOT DO BOUNDS CHECKING!
1547 static int oft_buildheader(unsigned char *dest, struct aim_fileheader_t *fh)
1553 for(i = 0; i < 8; i++)
1554 curbyte += aimutil_put8(dest+curbyte, fh->bcookie[i]);
1555 curbyte += aimutil_put16(dest+curbyte, fh->encrypt);
1556 curbyte += aimutil_put16(dest+curbyte, fh->compress);
1557 curbyte += aimutil_put16(dest+curbyte, fh->totfiles);
1558 curbyte += aimutil_put16(dest+curbyte, fh->filesleft);
1559 curbyte += aimutil_put16(dest+curbyte, fh->totparts);
1560 curbyte += aimutil_put16(dest+curbyte, fh->partsleft);
1561 curbyte += aimutil_put32(dest+curbyte, fh->totsize);
1562 curbyte += aimutil_put32(dest+curbyte, fh->size);
1563 curbyte += aimutil_put32(dest+curbyte, fh->modtime);
1564 curbyte += aimutil_put32(dest+curbyte, fh->checksum);
1565 curbyte += aimutil_put32(dest+curbyte, fh->rfrcsum);
1566 curbyte += aimutil_put32(dest+curbyte, fh->rfsize);
1567 curbyte += aimutil_put32(dest+curbyte, fh->cretime);
1568 curbyte += aimutil_put32(dest+curbyte, fh->rfcsum);
1569 curbyte += aimutil_put32(dest+curbyte, fh->nrecvd);
1570 curbyte += aimutil_put32(dest+curbyte, fh->recvcsum);
1571 memcpy(dest+curbyte, fh->idstring, 32);
1573 curbyte += aimutil_put8(dest+curbyte, fh->flags);
1574 curbyte += aimutil_put8(dest+curbyte, fh->lnameoffset);
1575 curbyte += aimutil_put8(dest+curbyte, fh->lsizeoffset);
1576 memcpy(dest+curbyte, fh->dummy, 69);
1578 memcpy(dest+curbyte, fh->macfileinfo, 16);
1580 curbyte += aimutil_put16(dest+curbyte, fh->nencode);
1581 curbyte += aimutil_put16(dest+curbyte, fh->nlanguage);
1582 memset(dest+curbyte, 0x00, 64);
1583 memcpy(dest+curbyte, fh->name, 64);
1585 /* XXX: Filenames longer than 64B */
1592 * aim_getfile_intitiate - Request an OFT getfile session
1593 * @sess: your session,
1594 * @conn: the BOS conn,
1595 * @destsn is the SN to connect to.
1597 * returns a new &aim_conn_t on success, %NULL on error
1599 faim_export aim_conn_t *aim_getfile_initiate(aim_session_t *sess, aim_conn_t *conn, const char *destsn)
1603 struct command_tx_struct *newpacket;
1604 struct aim_conn_t *newconn;
1605 struct aim_filetransfer_priv *priv;
1606 struct aim_msgcookie_t *cookie;
1607 int curbyte, i, listenfd;
1609 struct hostent *hptr;
1610 struct utsname myname;
1614 /* Open our socket */
1616 if ( (listenfd = aim_listenestablish(port)) == -1)
1619 /* get our local IP */
1621 if (uname(&myname) < 0)
1623 if ( (hptr = gethostbyname(myname.nodename)) == NULL)
1625 memcpy(&d, hptr->h_addr_list[0], 4);
1627 aim_putcap(cap, 16, AIM_CAPS_GETFILE);
1629 /* create the OSCAR packet */
1631 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(destsn)+4+4+0x42)))
1633 newpacket->lock = 1;
1637 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
1639 /* XXX: check the cookie before commiting to using it */
1641 /* Generate a random message cookie
1642 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */
1644 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 10));
1646 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1648 /* grab all the data for cookie caching. */
1650 if (!(cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t))))
1652 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
1653 cookie->type = AIM_COOKIETYPE_OFTGET;
1655 if (!(priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv))))
1657 memcpy(priv->cookie, cookie, 8);
1658 memcpy(priv->sn, destsn, sizeof(priv->sn));
1659 memcpy(priv->fh.name, "listing.txt", strlen("listing.txt"));
1662 cookie->data = priv;
1664 aim_cachecookie(sess, cookie);
1667 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
1669 /* Destination SN (prepended with byte length) */
1670 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
1671 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
1672 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1673 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1676 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1677 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0042);
1679 /* Flag data / ICBM Parameters? */
1680 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1681 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1684 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
1686 /* Capability String */
1687 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
1689 /* 000a/0002 : 0001 */
1690 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
1691 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1692 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
1694 /* 0003/0004: IP address */
1695 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1696 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
1697 for(i = 0; i < 4; i++)
1698 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]);
1700 /* already in network byte order */
1702 /* 0005/0002: Port */
1703 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1704 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1705 curbyte += aimutil_put16(newpacket->data+curbyte, port);
1708 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
1709 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1712 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
1713 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c);
1714 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00120001);
1716 for(i = 0; i < 0x000c - 4; i++)
1717 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1719 newpacket->commandlen = curbyte;
1720 newpacket->lock = 0;
1721 aim_tx_enqueue(sess, newpacket);
1723 /* allocate and set up our connection */
1725 i = fcntl(listenfd, F_GETFL, 0);
1726 fcntl(listenfd, F_SETFL, i | O_NONBLOCK);
1727 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL);
1730 perror("aim_newconn");
1734 newconn->fd = listenfd;
1735 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
1736 newconn->priv = priv;
1737 faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
1744 * aim_oft_getfile_request - request a particular file over an established getfile connection
1745 * @sess: your session
1746 * @conn: the established OFT getfile connection
1747 * @name: filename to request
1748 * @size: size of the file
1751 * returns -1 on error, 0 on successful enqueuing
1753 faim_export int aim_oft_getfile_request(aim_session_t *sess, aim_conn_t *conn, const char *name, int size)
1757 struct command_tx_struct *newoft;
1758 struct aim_filetransfer_priv *ft;
1759 if (!sess || !conn || !conn->priv || !name)
1762 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120c, 0))) {
1763 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1769 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1770 newoft->hdr.oft.hdr2len = 0x100 - 8;
1772 ft = (struct aim_filetransfer_priv *)conn->priv;
1773 ft->fh.filesleft = 1;
1774 ft->fh.totfiles = 1;
1775 ft->fh.totparts = 1;
1776 ft->fh.partsleft = 1;
1777 ft->fh.totsize = size;
1779 ft->fh.checksum = 0;
1780 memcpy(ft->fh.name, name, strlen(name));
1781 memset(ft->fh.name+strlen(name), 0, 1);
1783 if (!(newoft->hdr.oft.hdr2 = (unsigned char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1785 aim_frame_destroy(newoft);
1789 if (!(aim_oft_buildheader(newoft->hdr.oft.hdr2, &(ft->fh)))) {
1791 aim_frame_destroy(newoft);
1797 aim_tx_enqueue(sess, newoft);
1803 * aim_oft_getfile_ack - acknowledge a getfile download as complete
1804 * @sess: your session
1805 * @conn: the getfile conn to send the ack over
1807 * Call this function after you have read all the data in a particular
1808 * filetransfer. Returns -1 on error, 0 on apparent success
1811 faim_export int aim_oft_getfile_ack(aim_session_t *sess, aim_conn_t *conn)
1815 struct command_tx_struct *newoft;
1816 struct aim_filetransfer_priv *ft;
1818 if (!sess || !conn || !conn->priv)
1821 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) {
1822 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1828 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1829 newoft->hdr.oft.hdr2len = 0x100-8;
1831 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1833 aim_frame_destroy(newoft);
1837 ft = (struct aim_filetransfer_priv *)conn->priv;
1839 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1841 aim_frame_destroy(newoft);
1846 aim_tx_enqueue(sess, newoft);
1852 * aim_oft_getfile_end - end a getfile.
1853 * @sess: your session
1854 * @conn: the getfile connection
1856 * call this before you close the getfile connection if you're on the
1857 * receiving/requesting end.
1859 faim_export int aim_oft_getfile_end(aim_session_t *sess, aim_conn_t *conn)
1863 struct command_tx_struct *newoft;
1864 struct aim_filetransfer_priv *ft;
1866 if (!sess || !conn || !conn->priv)
1869 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0204, 0))) {
1870 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1876 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1877 newoft->hdr.oft.hdr2len = 0x100 - 8;
1879 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1881 aim_frame_destroy(newoft);
1885 ft = (struct aim_filetransfer_priv *)conn->priv;
1886 ft->state = 4; /* no longer wanting data */
1887 ft->fh.nrecvd = ft->fh.size;
1888 ft->fh.recvcsum = ft->fh.checksum;
1889 ft->fh.flags = 0x21;
1891 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1893 aim_frame_destroy(newoft);
1898 aim_tx_enqueue(sess, newoft);