2 * File transfer (OFT) and DirectIM.
5 #define FAIM_INTERNAL_FT
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <sys/utsname.h> /* for aim_directim_initiate */
15 #include <arpa/inet.h> /* for inet_ntoa */
20 o fix misspelling of receive
21 o double-check mutex states on error returns
22 o look for memory leaks.. there's going to be shitloads, i'm sure.
23 o change listing FILE to listing filesend callback
24 o change send_file and send_chunk over to buffers.
27 static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr);
30 * aim_handlerendconnect - call this to accept OFT connections and set up the requisite structures
32 * @cur: the conn the incoming connection is on
34 * call this when you get an outstanding read on a conn with subtype
35 * AIM_CONN_SUBTYPE_RENDEZVOUS_OUT, it will close the current fd and
36 * call a callback with the new, accept'd fd to the other person. then we
37 * get to start the fun that is OFT!
39 faim_export int aim_handlerendconnect(struct aim_session_t *sess, struct aim_conn_t *cur)
42 rxcallback_t userfunc;
43 struct sockaddr cliaddr;
44 socklen_t clilen = sizeof(cliaddr);
46 struct aim_conn_t *newconn;
48 if( (acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1)
50 if (cliaddr.sa_family != AF_INET) { /* just in case IPv6 really is happening */
56 /* clone it and then close the original */
57 /* XXX safe? maybe cur->priv should be NULLed after this */
58 if (!(newconn = aim_cloneconn(sess, cur))) {
65 newconn->type = AIM_CONN_TYPE_RENDEZVOUS;
66 newconn->fd = acceptfd;
68 switch(newconn->subtype) {
69 case AIM_CONN_SUBTYPE_OFT_DIRECTIM: {
70 struct aim_directim_priv *priv;
73 newconn->priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
74 priv = (struct aim_directim_priv *)newconn->priv;
76 snprintf(priv->ip, sizeof(priv->ip), "%s:%u",
77 inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr),
78 ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
80 /* should we really do this? seems like the client should decide. maybe clone the connection and keep the listener open. -- mid */
81 if ( (userfunc = aim_callhandler(newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE)))
82 ret = userfunc(sess, NULL, newconn);
86 case AIM_CONN_SUBTYPE_OFT_GETFILE: {
87 struct aim_filetransfer_priv *priv;
90 newconn->priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
91 priv = (struct aim_filetransfer_priv *)newconn->priv;
93 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
95 if ( (userfunc = aim_callhandler(newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE)))
96 ret = userfunc(sess, NULL, newconn);
101 faimdprintf(1,"Got a Connection on a listener that's not Rendezvous(??!) Killing conn.\n");
102 aim_conn_close(newconn);
111 * aim_send_im_direct - send IM client-to-client over established connection
112 * @sess: session to conn
113 * @conn: directim connection
114 * @msg: null-terminated string to send; if this is NULL, it will send a "typing" notice.
117 faim_export int aim_send_im_direct(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg)
120 struct command_tx_struct *newpacket;
121 struct aim_directim_priv *priv = NULL;
123 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !conn->priv) {
124 faimdprintf(2,"faim: directim: invalid arguments\n");
128 priv = (struct aim_directim_priv *)conn->priv;
130 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, strlen(msg)))) {
131 faimdprintf(2,"faim: directim: tx_new failed\n");
137 /* if msg is non-null, we'resending an IM, else a "typing" notice */
139 if (strlen(msg) >= MAXMSGLEN)
141 newpacket->hdr.oft.hdr2len = 0x54;
142 if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) {
144 aim_tx_destroy(newpacket);
148 newpacket->hdr.oft.hdr2len = 0x44;
149 if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) {
151 aim_tx_destroy(newpacket);
156 memcpy(newpacket->hdr.oft.magic, "ODC2", 4);
157 newpacket->data = NULL;
160 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0006);
161 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
162 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, (char *)priv->cookie, 8);
163 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
164 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
165 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
166 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
167 i += aimutil_put32(newpacket->hdr.oft.hdr2+i, 0x00000000);
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);
172 /* flags -- 0x000e for "typing", 0x0000 for message */
174 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
176 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000e);
178 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
179 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
180 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, sess->sn, strlen(sess->sn));
183 i += aimutil_put8(newpacket->hdr.oft.hdr2+i, 0x00);
184 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
185 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
186 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
187 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
188 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
189 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
190 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
195 /* values grabbed from a dump */
196 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008);
197 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c);
198 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
199 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466);
200 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001);
201 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f);
202 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e);
203 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8);
204 memcpy(newpacket->data, msg, strlen(msg));
207 aim_tx_enqueue(sess, newpacket);
212 * aim_directim_intitiate - For those times when we want to open up the directim channel ourselves.
213 * @sess: your session,
214 * @conn: the BOS conn,
215 * @priv: a dummy priv value (we'll let it get filled in later) (if you pass a NULL, we alloc one)
216 * @destsn: the SN to connect to.
219 faim_export struct aim_conn_t *aim_directim_initiate(struct aim_session_t *sess,
220 struct aim_conn_t *conn,
221 struct aim_directim_priv *priv,
225 struct command_tx_struct *newpacket;
226 struct aim_conn_t *newconn;
227 struct aim_msgcookie_t *cookie;
228 int curbyte, i, listenfd;
230 struct hostent *hptr;
232 unsigned char cap[16];
233 char d[4]; /* IPv6 is a bit bigger... */
235 /* XXX: TLVlist-ize this */
237 /* Open our socket */
239 if( (listenfd = aim_listenestablish(port)) == -1)
242 /* get our local IP */
243 /* XXX if available, use getaddrinfo() */
244 /* XXX allow client to specify which IP to use for multihomed boxes */
245 if(gethostname(localhost, 128) < 0)
247 if( (hptr = gethostbyname(localhost)) == NULL)
249 memcpy(&d, hptr->h_addr_list[0], 4);
251 /* XXX: this probably isn't quite kosher, but it works */
252 aim_putcap(cap, 16, AIM_CAPS_IMIMAGE);
254 /* create the OSCAR packet */
256 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x32)))
261 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
263 /* Generate a random message cookie */
264 /* This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */
266 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) rand() % 20));
268 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
270 /* grab all the data for cookie caching */
271 cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t));
272 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
273 cookie->type = AIM_COOKIETYPE_OFTIM;
277 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
279 memcpy(priv->cookie, cookie, 8);
280 memcpy(priv->sn, destsn, sizeof(priv->sn));
282 aim_cachecookie(sess, cookie);
285 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
287 /* Destination SN (prepended with byte length)*/
288 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
289 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
290 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
291 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
294 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
295 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032);
297 /* Flag data / ICBM Parameters */
298 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
299 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
302 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
304 /*Capability String */
305 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
307 /* 000a/0002 : 0001 */
308 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
309 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
310 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
312 /* 0003/0004: IP address */
313 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
314 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
315 for(i = 0;i < 4; i++)
316 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]);
318 /* 0005/0002: Port */
319 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
320 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
321 curbyte += aimutil_put16(newpacket->data+curbyte, port);
324 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
325 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
326 newpacket->commandlen = curbyte;
328 aim_tx_enqueue(sess, newpacket);
331 /* XXX switch to aim_cloneconn()? */
332 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL)))
335 newconn->fd = listenfd;
336 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
337 newconn->priv = priv;
338 newconn->lastactivity = time(NULL);
340 faimdprintf(2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
346 * unsigned long aim_oft_listener_clean - close up old listeners
347 * @sess: session to clean up in
348 * @age: maximum age in seconds
350 * returns number closed, -1 on error.
352 faim_export unsigned int aim_oft_listener_clean(struct aim_session_t *sess, time_t age)
354 struct aim_conn_t *cur;
356 unsigned int hit = 0;
361 faim_mutex_lock(&sess->connlistlock);
362 for(cur = sess->connlist;cur; cur = cur->next)
363 if(cur->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) {
364 faim_mutex_lock(&cur->active);
365 if (cur->lastactivity < (now - age) ) {
366 faim_mutex_unlock(&cur->active);
370 faim_mutex_unlock(&cur->active);
372 faim_mutex_unlock(&sess->connlistlock);
377 * aim_directim_connect - connect to buddy for directim
378 * @sess: the session to append the conn to,
379 * @conn: the BOS connection,
380 * @priv: the filled-in priv data structure for the connection
382 * returns conn if connected, %NULL on error
384 faim_export struct aim_conn_t *aim_directim_connect(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_directim_priv *priv)
386 struct aim_conn_t *newconn = NULL;
388 if(!sess || !conn || !priv)
391 /* XXX verify that non-blocking connects actually work */
392 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, priv->ip);
393 if (!newconn || (newconn->fd == -1)) {
394 faimdprintf(2, "could not connect to %s\n", priv->ip);
395 perror("aim_newconn");
396 /* Its safe to call conn_kill here since we created the connection */
398 aim_conn_kill(sess, &newconn);
402 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
403 newconn->priv = priv;
404 faimdprintf(2, "faim: connected to peer (fd = %d)\n", newconn->fd);
410 * aim_directim_getconn - find a directim conn for buddy name
411 * @sess: your session,
412 * @name: the name to get,
414 * returns conn for directim with name, %NULL if none found.
417 faim_export struct aim_conn_t *aim_directim_getconn(struct aim_session_t *sess, const char *name)
419 struct aim_conn_t *cur;
420 struct aim_directim_priv *priv;
425 faim_mutex_lock(&sess->connlistlock);
427 for (cur = sess->connlist; cur; cur = cur->next) {
428 if (cur->type != AIM_CONN_TYPE_RENDEZVOUS || cur->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM)
431 if (aim_sncmp(priv->sn, name) == 0)
433 } faim_mutex_unlock(&sess->connlistlock);
438 * aim_accepttransfer - accept a file transfer request
439 * @sess: the session,
440 * @conn: the BOS conn for the CAP reply
441 * @sn: the screenname to send it to,
442 * @cookie: the cookie used
443 * @ip: the ip to connect to
444 * @listingfiles: number of files to share
445 * @listingtotsize: total size of shared files
446 * @listingsize: length of the listing file(buffer)
447 * @listingchecksum: checksum of the listing
448 * @rendid: capability type (%AIM_CAPS_GETFILE or %AIM_CAPS_SENDFILE)
450 * returns connection or %NULL on error.
452 faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess,
453 struct aim_conn_t *conn,
454 char *sn, char *cookie,
456 unsigned short listingfiles,
457 unsigned short listingtotsize,
458 unsigned short listingsize,
459 unsigned int listingchecksum,
460 unsigned short rendid)
462 struct command_tx_struct *newpacket, *newoft;
463 struct aim_conn_t *newconn;
464 struct aim_fileheader_t *fh;
465 struct aim_filetransfer_priv *priv;
466 struct aim_msgcookie_t *cachedcook;
469 if(!sess || !conn || !sn || !cookie || !ip)
472 if(rendid == AIM_CAPS_GETFILE) {
473 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, ip);
474 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
476 if (!newconn || (newconn->fd == -1)) {
477 perror("aim_newconn");
478 faimdprintf(2, "could not connect to %s (fd: %i)\n", ip, newconn?newconn->fd:0);
479 /* Its safe to call conn_kill here since we created the connection */
481 aim_conn_kill(sess, &newconn);
484 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
486 memcpy(priv->cookie, cookie, 8);
488 strncpy(priv->sn, sn, MAXSNLEN);
489 strncpy(priv->ip, ip, sizeof(priv->ip));
490 newconn->priv = (void *)priv;
492 faimdprintf(2, "faim: connected to peer (fd = %d)\n", newconn->fd);
495 if(rendid == AIM_CAPS_GETFILE) {
496 faimdprintf(2, "faim: getfile request accept\n");
498 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1108, newconn, 0))) {
499 faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n");
500 /* Its safe to call conn_kill here since we created the connection */
501 aim_conn_kill(sess, &newconn);
506 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
507 newoft->hdr.oft.hdr2len = 0x100 - 8;
509 if(!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t))))
512 fh->encrypt = 0x0000;
513 fh->compress = 0x0000;
514 fh->totfiles = listingfiles;
515 fh->filesleft = listingfiles; /* is this right -- total parts and parts left?*/
516 fh->totparts = 0x0001;
517 fh->partsleft = 0x0001;
518 fh->totsize = listingtotsize;
519 fh->size = listingsize; /* ls -l listing.txt */
520 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
521 fh->checksum = listingchecksum;
522 fh->rfcsum = 0x00000000;
523 fh->rfsize = 0x00000000;
524 fh->cretime = 0x00000000;
525 fh->rfcsum = 0x00000000;
526 fh->nrecvd = 0x00000000;
527 fh->recvcsum = 0x00000000;
528 memset(fh->idstring, 0, sizeof(fh->idstring));
529 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
531 fh->lnameoffset = 0x1a;
532 fh->lsizeoffset = 0x10;
533 memset(fh->dummy, 0, sizeof(fh->dummy));
534 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
536 /* we need to figure out these encodings for filenames */
537 fh->nencode = 0x0000;
538 fh->nlanguage = 0x0000;
539 memset(fh->name, 0, sizeof(fh->name));
540 memcpy(fh->name, "listing.txt", sizeof(fh->name));
542 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
544 aim_tx_destroy(newoft);
548 memcpy(fh->bcookie, cookie, 8);
550 if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, fh)))
551 printf("eek, bh fail!\n");
554 aim_tx_enqueue(sess, newoft);
556 if(!(cachedcook = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) {
557 faimdprintf(1, "faim: accepttransfer: couldn't calloc cachedcook. yeep!\n");
562 memcpy(&(priv->fh), fh, sizeof(struct aim_fileheader_t));
563 memcpy(cachedcook->cookie, cookie, 8);
565 cachedcook->type = AIM_COOKIETYPE_OFTGET;
566 cachedcook->data = (void *)priv;
568 if (aim_cachecookie(sess, cachedcook) != 0)
569 faimdprintf(1, "faim: ERROR caching message cookie\n");
574 /* OSCAR CAP accept packet */
576 if(!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(sn)+4+2+8+16)))
580 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
582 for (i = 0; i < 8; i++)
583 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
585 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
586 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
587 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
588 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
589 curbyte += aimutil_put16(newpacket->data+curbyte, 0x001a);
590 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept*/);
592 for (i = 0;i < 8; i++)
593 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
595 curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid);
597 aim_tx_enqueue(sess, newpacket);
604 * aim_getlisting(FILE *file) -- get an aim_fileheader_t for a given FILE*
605 * @file is an opened listing file
607 * returns a pointer to the filled-in fileheader_t
609 * currently omits checksum. we'll fix this when AOL breaks us, i
614 faim_export struct aim_fileheader_t *aim_getlisting(FILE *file)
616 struct aim_fileheader_t *fh;
617 u_long totsize = 0, size = 0, checksum = 0xffff0000;
619 char *linebuf, sizebuf[9];
621 int linelength = 1024;
623 /* XXX: if we have a line longer than 1024chars, God help us. */
624 if( (linebuf = (char *)calloc(1, linelength)) == NULL ) {
625 faimdprintf(2, "linebuf calloc failed\n");
629 if(fseek(file, 0, SEEK_END) == -1) { /* use this for sanity check */
630 perror("getlisting END1 fseek:");
631 faimdprintf(2, "getlising fseek END1 error\n");
634 if(fgetpos(file, &size) == -1) {
635 perror("getlisting END1 getpos:");
636 faimdprintf(2, "getlising getpos END1 error\n");
639 if(fseek(file, 0, SEEK_SET) != 0) {
640 perror("getlesting fseek(SET):");
641 faimdprintf(2, "faim: getlisting: couldn't seek to beginning of listing file\n");
644 memset(linebuf, 0, linelength);
648 while(fgets(linebuf, linelength, file)) {
650 memset(sizebuf, 0, 9);
652 size += strlen(linebuf);
654 if(strlen(linebuf) < 23) {
655 faimdprintf(2, "line \"%s\" too short. skipping\n", linebuf);
658 if(linebuf[strlen(linebuf)-1] != '\n') {
659 faimdprintf(2, "faim: OFT: getlisting -- hit EOF or line too long!\n");
662 memcpy(sizebuf, linebuf+17, 8);
664 totsize += strtol(sizebuf, NULL, 10);
665 memset(linebuf, 0, linelength);
668 if(fseek(file, 0, SEEK_SET) == -1) {
669 perror("getlisting END2 fseek:");
670 faimdprintf(2, "getlising fseek END2 error\n");
675 /* we're going to ignore checksumming the data for now -- that
676 * requires walking the whole listing.txt. it should probably be
677 * done at register time and cached, but, eh. */
679 if(!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t))))
682 printf( "faim: OFT: getlisting: totfiles: %u, totsize: %lu, size: %lu\n", totfiles, totsize, size);
684 fh->encrypt = 0x0000;
685 fh->compress = 0x0000;
686 fh->totfiles = totfiles;
687 fh->filesleft = totfiles; /* is this right ?*/
688 fh->totparts = 0x0001;
689 fh->partsleft = 0x0001;
690 fh->totsize = totsize;
691 fh->size = size; /* ls -l listing.txt */
692 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
693 fh->checksum = checksum; /* XXX: checksum ! */
694 fh->rfcsum = 0x00000000;
695 fh->rfsize = 0x00000000;
696 fh->cretime = 0x00000000;
697 fh->rfcsum = 0x00000000;
698 fh->nrecvd = 0x00000000;
699 fh->recvcsum = 0x00000000;
701 /* memset(fh->idstring, 0, sizeof(fh->idstring)); */
702 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
703 memset(fh->idstring+strlen(fh->idstring), 0, sizeof(fh->idstring)-strlen(fh->idstring));
706 fh->lnameoffset = 0x1a;
707 fh->lsizeoffset = 0x10;
709 /* memset(fh->dummy, 0, sizeof(fh->dummy)); */
710 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
712 fh->nencode = 0x0000; /* we need to figure out these encodings for filenames */
713 fh->nlanguage = 0x0000;
715 /* memset(fh->name, 0, sizeof(fh->name)); */
716 memcpy(fh->name, "listing.txt", sizeof(fh->name));
717 memset(fh->name+strlen(fh->name), 0, 64-strlen(fh->name));
719 faimdprintf(2, "faim: OFT: listing fh name %s / %s\n", fh->name, (fh->name+(strlen(fh->name))));
724 * aim_listenestablish - create a listening socket on a port.
725 * @portnum: the port number to bind to.
727 * you need to call accept() when it's connected. returns your fd
730 faim_internal int aim_listenestablish(u_short portnum)
732 #if defined(__linux__)
733 /* XXX what other OS's support getaddrinfo? */
736 struct addrinfo hints, *res, *ressave;
738 sprintf(serv, "%d", portnum);
739 memset(&hints, 0, sizeof(struct addrinfo));
740 hints.ai_flags = AI_PASSIVE;
741 hints.ai_family = AF_UNSPEC;
742 hints.ai_socktype = SOCK_STREAM;
743 if (getaddrinfo(NULL /*any IP*/, serv, &hints, &res) != 0) {
744 perror("getaddrinfo");
749 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
752 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
753 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
757 } while ( (res = res->ai_next) );
762 if (listen(listenfd, 1024)!=0) {
767 freeaddrinfo(ressave);
772 struct sockaddr_in sockin;
773 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
774 perror("socket(listenfd)");
778 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) != 0)) {
779 perror("setsockopt(listenfd)");
784 memset(&sockin, 0, sizeof(struct sockaddr_in));
785 sockin.sin_family = AF_INET;
786 sockin.sin_port = htons(portnum);
788 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
789 perror("bind(listenfd)");
793 if (listen(listenfd, 4) != 0) {
794 perror("listen(listenfd)");
802 * aim_get_command_rendezvous - OFT equivalent of aim_get_command
803 * @sess: session to work on
804 * @conn: conn to pull data from
806 * this reads and handles data from conn->fd. currently a little rough
809 faim_internal int aim_get_command_rendezvous(struct aim_session_t *sess, struct aim_conn_t *conn)
811 unsigned char hdrbuf1[6];
812 unsigned char *hdr = NULL;
815 rxcallback_t userfunc = NULL;
817 memset(hdrbuf1, 0, sizeof(hdrbuf1));
818 faim_mutex_lock(&conn->active);
820 /* gets locked down for the entirety */
822 if(conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE ) {
823 struct aim_filetransfer_priv *ft;
826 /* waiting on listing data */
829 struct command_tx_struct *newoft;
830 if(!(listing = malloc(ft->fh.size))) {
831 faim_mutex_unlock(&conn->active);
836 if(aim_recv(conn->fd, listing, ft->fh.size) != ft->fh.size)
837 faimdprintf(2, "OFT get: file %s was short. (0x%lx)\n", ft->fh.name, ft->fh.size);
839 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x120b, conn, 0))) {
840 faimdprintf(2, "faim: aim_get_command_rendezvous: getfile listing: tx_new OFT failed\n");
841 faim_mutex_unlock(&conn->active);
843 aim_conn_close(conn);
849 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
850 newoft->hdr.oft.hdr2len = 0x100 - 8;
853 ft->fh.nrecvd = ft->fh.size;
854 ft->fh.recvcsum = ft->fh.checksum;
857 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
859 aim_tx_destroy(newoft);
861 faim_mutex_unlock(&conn->active);
865 if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh))))
866 printf("eek! bh fail listing\n");
870 aim_tx_enqueue(sess, newoft);
871 if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTING)) )
872 ret = userfunc(sess, NULL, conn, ft, listing);
874 faim_mutex_unlock(&conn->active);
879 /* waiting on file data */
880 if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILERECIEVE)) ) {
881 faim_mutex_unlock(&conn->active);
882 return userfunc(sess, NULL, conn, ft);
884 faim_mutex_unlock(&conn->active);
889 if ( (hdrlen = aim_recv(conn->fd, hdrbuf1, 6)) < 6) {
890 faimdprintf(2, "faim: rend: read error (fd: %i) %02x%02x%02x%02x%02x%02x (%i)\n",
891 conn->fd, hdrbuf1[0],hdrbuf1[1],hdrbuf1[2],hdrbuf1[3],hdrbuf1[4],hdrbuf1[5],hdrlen);
892 faim_mutex_unlock(&conn->active);
895 else { /* disconnected */
896 switch(conn->subtype) {
897 case AIM_CONN_SUBTYPE_OFT_DIRECTIM: {
898 /* XXX: clean up cookies here ? */
899 struct aim_directim_priv *priv = NULL;
900 if(!(priv = (struct aim_directim_priv *)conn->priv) )
903 aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTIM);
904 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT)) ) {
905 aim_conn_close(conn);
906 return userfunc(sess, NULL, conn, priv->sn);
910 case AIM_CONN_SUBTYPE_OFT_GETFILE: {
911 struct aim_filetransfer_priv *priv;
912 if(!(priv = (struct aim_filetransfer_priv *)conn->priv))
914 aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTGET);
915 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT)) ) {
916 aim_conn_close(conn);
917 return userfunc(sess, NULL, conn, priv->sn);
921 case AIM_CONN_SUBTYPE_OFT_SENDFILE: {
922 struct aim_filetransfer_priv *priv;
923 if(!(priv = (struct aim_filetransfer_priv *)conn->priv))
925 aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTSEND);
926 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_SENDFILEDISCONNECT)) ) {
927 aim_conn_close(conn);
928 return userfunc(sess, NULL, conn, priv->sn);
934 aim_conn_close(conn);
939 hdrlen = aimutil_get16(hdrbuf1+4);
942 if (!(hdr = malloc(hdrlen))) {
943 faim_mutex_unlock(&conn->active);
947 if (aim_recv(conn->fd, hdr, hdrlen) < hdrlen) {
949 faimdprintf(2,"faim: rend: read2 error on %d (%d)\n", conn->fd, hdrlen);
951 faim_mutex_unlock(&conn->active);
952 aim_conn_close(conn);
955 hdrtype = aimutil_get16(hdr);
956 printf("\tgot 0x%04x", hdrtype);
958 case 0x0001: { /* directim */
959 int payloadlength = 0;
961 struct aim_directim_priv *priv;
964 if(!(priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)))) {
965 faim_mutex_unlock(&conn->active);
969 payloadlength = aimutil_get32(hdr+22);
970 flags = aimutil_get16(hdr+32);
971 snptr = (char *)hdr+38;
972 strncpy(priv->sn, snptr, MAXSNLEN);
974 faimdprintf(2, "faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, snptr);
976 if (flags == 0x000e) {
977 faim_mutex_unlock(&conn->active);
978 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)) )
979 return userfunc(sess, NULL, snptr);
981 if ((flags == 0x0000) && payloadlength) {
983 if(! (msg = calloc(1, payloadlength+1)) ) {
984 faim_mutex_unlock(&conn->active);
987 if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) {
989 printf("faim: rend: read3 error\n");
991 faim_mutex_unlock(&conn->active);
992 aim_conn_close(conn);
995 faim_mutex_unlock(&conn->active);
996 msg[payloadlength] = 0x00;
997 faimdprintf(2, "faim: directim: %s/%04x/%04x/%s\n", snptr, payloadlength, flags, msg);
998 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) )
999 i = userfunc(sess, NULL, conn, snptr, msg);
1005 case 0x1108: { /* getfile listing.txt incoming tx->rx */
1006 struct aim_filetransfer_priv *ft;
1007 struct aim_fileheader_t *fh;
1008 struct aim_msgcookie_t *cook;
1009 struct command_tx_struct *newoft;
1010 faimdprintf(2,"faim: rend: fileget 0x1108\n");
1011 fh = aim_oft_getfh(hdr);
1012 faim_mutex_unlock(&conn->active);
1013 if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1014 faim_mutex_unlock(&conn->active);
1021 /* we're waaaaiiiting.. for listing.txt */
1023 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1024 aim_cachecookie(sess, cook);
1027 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1209, conn, 0))) {
1028 aim_conn_close(conn);
1032 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1033 newoft->hdr.oft.hdr2len = 0x100 - 8;
1035 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1037 aim_tx_destroy(newoft);
1041 if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1043 aim_tx_destroy(newoft);
1048 aim_tx_enqueue(sess, newoft);
1052 case 0x1209: { /* get file listing ack rx->tx */
1053 struct aim_filetransfer_priv *ft;
1054 struct aim_fileheader_t *fh;
1055 struct aim_msgcookie_t *cook;
1058 fh = aim_oft_getfh(hdr);
1059 faim_mutex_unlock(&conn->active);
1061 if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET)))
1062 faimdprintf(2, "shit, no cookie in 0x1209. (%i/%s)going to crash..\n",
1063 AIM_COOKIETYPE_OFTGET, fh->bcookie);
1067 if(ft->fh.size != fh->size)
1068 faimdprintf(2, "hrm. ft->fh.size (%ld) != fh->size (%ld). um. using ft->fh.size\n",
1069 ft->fh.size, fh->size);
1072 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ)))
1073 ret = userfunc(sess, NULL, conn, fh);
1075 faimdprintf(2, "faim: get_command_rendezvous: hit end of 1209\n");
1083 case 0x120b: { /* getfile listing.txt rx confirm */
1084 struct aim_filetransfer_priv *ft;
1085 struct aim_msgcookie_t *cook;
1086 struct aim_fileheader_t *fh;
1088 fh = aim_oft_getfh(hdr);
1089 faim_mutex_unlock(&conn->active);
1091 if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1097 aim_cachecookie(sess, cook);
1100 /* XXX: call listing.txt rx confirm */
1103 case 0x120c: { /* getfile file request */
1104 struct aim_filetransfer_priv *ft;
1105 struct aim_msgcookie_t *cook;
1106 struct aim_fileheader_t *fh;
1107 struct command_tx_struct *newoft;
1110 fh = aim_oft_getfh(hdr);
1111 faim_mutex_unlock(&conn->active);
1113 if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET)))
1117 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1120 aim_cachecookie(sess, cook);
1122 faimdprintf(2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name);
1124 if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) )
1125 i = userfunc(sess, NULL, conn, &(ft->fh), cook->cookie);
1130 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0101, conn, 0))) {
1131 faimdprintf(2, "faim: send_final_transfer: tx_new OFT failed\n");
1136 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1137 newoft->hdr.oft.hdr2len = 0x100 - 8;
1139 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1141 aim_tx_destroy(newoft);
1145 /* protocol BS: nrecvd, recvcsum to 0, flags to 0x20. */
1147 ft->fh.recvcsum = 0;
1148 ft->fh.flags = 0x20;
1150 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
1153 aim_tx_enqueue(sess, newoft);
1155 faimdprintf(2, "faim: OFT: OFT file header enqueued.\n");
1161 case 0x0101: { /* getfile: sending data */
1162 struct aim_fileheader_t *fh;
1163 struct aim_filetransfer_priv *ft;
1164 struct aim_msgcookie_t *cook;
1165 struct command_tx_struct *newoft;
1167 fh = aim_oft_getfh(hdr);
1168 faim_mutex_unlock(&conn->active);
1170 if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1171 printf("fucking no cookie\n");
1180 aim_cachecookie(sess, cook);
1182 faimdprintf(2, "faim: fileget: %s seems to want to send %s\n", ft->sn, ft->fh.name);
1184 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0202, conn, 0))) {
1185 aim_conn_close(conn);
1186 faimdprintf(2, "faim: send_final_transfer: tx_new OFT failed\n");
1191 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1193 newoft->hdr.oft.hdr2len = 0x100 - 8;
1195 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1197 aim_tx_destroy(newoft);
1201 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
1204 aim_tx_enqueue(sess, newoft);
1206 faimdprintf(2, "faim: OFT: OFT 0x0202 enqueued.\n");
1208 if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL)
1213 case 0x0202: { /* get file: ready to recieve data */
1214 struct aim_fileheader_t *fh;
1215 struct aim_filetransfer_priv *ft;
1216 struct aim_msgcookie_t *cook;
1219 fh = aim_oft_getfh(hdr);
1220 faim_mutex_unlock(&conn->active);
1222 if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1229 faimdprintf(2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n");
1231 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) )
1232 ret = userfunc(sess, NULL, conn, fh);
1239 case 0x0204: { /* get file: finished. close it up */
1241 struct aim_fileheader_t *fh;
1243 fh = aim_oft_getfh(hdr);
1244 faim_mutex_unlock(&conn->active);
1246 faimdprintf(2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n");
1248 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) )
1249 i = userfunc(sess, NULL, conn, fh);
1254 aim_conn_close(conn);
1262 faimdprintf(2,"faim: OFT frame: uknown type %04x\n", hdrtype);
1263 faim_mutex_unlock(&conn->active);
1272 * aim_oft_getfh - extracts a aim_fileheader_t from buffer hdr.
1273 * @hdr: buffer to extract header from
1275 * returns pointer to struct on success; %NULL on error.
1278 static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr)
1280 struct aim_fileheader_t *fh;
1282 if(!(fh = calloc(1, sizeof(struct aim_fileheader_t))))
1285 /* [0] and [1] are the type. we can ignore those here. */
1287 for(j = 0; j < 8; j++, i++)
1288 fh->bcookie[j] = hdr[i];
1289 fh->encrypt = aimutil_get16(hdr+i);
1291 fh->compress = aimutil_get16(hdr+i);
1293 fh->totfiles = aimutil_get16(hdr+i);
1295 fh->filesleft = aimutil_get16(hdr+i);
1297 fh->totparts = aimutil_get16(hdr+i);
1299 fh->partsleft = aimutil_get16(hdr+i);
1301 fh->totsize = aimutil_get32(hdr+i);
1303 fh->size = aimutil_get32(hdr+i);
1305 fh->modtime = aimutil_get32(hdr+i);
1307 fh->checksum = aimutil_get32(hdr+i);
1309 fh->rfrcsum = aimutil_get32(hdr+i);
1311 fh->rfsize = aimutil_get32(hdr+i);
1313 fh->cretime = aimutil_get32(hdr+i);
1315 fh->rfcsum = aimutil_get32(hdr+i);
1317 fh->nrecvd = aimutil_get32(hdr+i);
1319 fh->recvcsum = aimutil_get32(hdr+i);
1321 memcpy(fh->idstring, hdr+i, 32);
1323 fh->flags = aimutil_get8(hdr+i);
1325 fh->lnameoffset = aimutil_get8(hdr+i);
1327 fh->lsizeoffset = aimutil_get8(hdr+i);
1329 memcpy(fh->dummy, hdr+i, 69);
1331 memcpy(fh->macfileinfo, hdr+i, 16);
1333 fh->nencode = aimutil_get16(hdr+i);
1335 fh->nlanguage = aimutil_get16(hdr+i);
1337 memcpy(fh->name, hdr+i, 64);
1343 * aim_oft_checksum - calculate oft checksum of buffer
1344 * @buffer: buffer of data to checksum
1345 * @bufsize: size of buffer
1346 * @checksum: pointer to integer to place result in (pointer!)
1349 * note that checksum is a pointer. checksum should be filled with
1350 * 0xFFFF0000 for each new file; you can have this checksum chunks of
1351 * files in series if you just call it repeatedly in a for(; ; ) loop
1352 * and don't reset the checksum between each call. and you thought we
1353 * didn't care about you and your pathetic client's meomry footprint
1357 faim_export int aim_oft_checksum(char *buffer, int bufsize, int *checksum){ short check0, check1;
1359 check0 = ((*checksum & 0xFF000000) >> 16);
1360 check1 = ((*checksum & 0x00ff0000) >> 16);
1361 for(i = 0; i < bufsize; i++) {
1362 if(i % 2) { /* use check1 -- second byte */
1363 if ( (short)buffer[i] > check1 ) { /* wrapping */
1364 check1 += 0x100; /* this is a cheap way to wrap */
1366 /* if we're wrapping, decrement the other one */
1367 /* XXX: check this corner case */
1373 check1 -= buffer[i];
1374 } else { /* use check0 -- first byte */
1375 if ( (short)buffer[i] > check0 ) { /* wrapping */
1376 check0 += 0x100; /* this is a cheap way to wrap */
1378 /* if we're wrapping, decrement the other one */
1379 /* XXX: check this corner case */
1385 check0 -= buffer[i];
1389 if(check0 > 0xff || check1 > 0xff) {
1390 /* they shouldn't be able to do this. error! */
1391 faimdprintf(2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1);
1395 /* grab just the lowest byte; this should be clean, but just in
1400 *checksum = ((check0 * 0x1000000) + (check1 * 0x10000));
1405 * aim_oft_buildheader - fills a buffer with network-order fh data
1406 * @dest: buffer to fill -- pre-alloced
1407 * @fh: fh to get data from
1409 * returns length written; -1 on error.
1410 * DOES NOT DO BOUNDS CHECKING!
1413 faim_internal int aim_oft_buildheader(unsigned char *dest,struct aim_fileheader_t *fh)
1419 for(i = 0; i < 8; i++)
1420 curbyte += aimutil_put8(dest+curbyte, fh->bcookie[i]);
1421 curbyte += aimutil_put16(dest+curbyte, fh->encrypt);
1422 curbyte += aimutil_put16(dest+curbyte, fh->compress);
1423 curbyte += aimutil_put16(dest+curbyte, fh->totfiles);
1424 curbyte += aimutil_put16(dest+curbyte, fh->filesleft);
1425 curbyte += aimutil_put16(dest+curbyte, fh->totparts);
1426 curbyte += aimutil_put16(dest+curbyte, fh->partsleft);
1427 curbyte += aimutil_put32(dest+curbyte, fh->totsize);
1428 curbyte += aimutil_put32(dest+curbyte, fh->size);
1429 curbyte += aimutil_put32(dest+curbyte, fh->modtime);
1430 curbyte += aimutil_put32(dest+curbyte, fh->checksum);
1431 curbyte += aimutil_put32(dest+curbyte, fh->rfrcsum);
1432 curbyte += aimutil_put32(dest+curbyte, fh->rfsize);
1433 curbyte += aimutil_put32(dest+curbyte, fh->cretime);
1434 curbyte += aimutil_put32(dest+curbyte, fh->rfcsum);
1435 curbyte += aimutil_put32(dest+curbyte, fh->nrecvd);
1436 curbyte += aimutil_put32(dest+curbyte, fh->recvcsum);
1437 memcpy(dest+curbyte, fh->idstring, 32);
1439 curbyte += aimutil_put8(dest+curbyte, fh->flags);
1440 curbyte += aimutil_put8(dest+curbyte, fh->lnameoffset);
1441 curbyte += aimutil_put8(dest+curbyte, fh->lsizeoffset);
1442 memcpy(dest+curbyte, fh->dummy, 69);
1444 memcpy(dest+curbyte, fh->macfileinfo, 16);
1446 curbyte += aimutil_put16(dest+curbyte, fh->nencode);
1447 curbyte += aimutil_put16(dest+curbyte, fh->nlanguage);
1448 memset(dest+curbyte, 0x00, 64);
1449 memcpy(dest+curbyte, fh->name, 64);
1451 /* XXX: Filenames longer than 64B */
1457 * aim_getfile_send - send a FULL file down (ie: blocking; that is: DON'T USE IT, use send_chunk below.)
1458 * @conn: the OFT connection to shove the data down,
1459 * @tosend: is the FILE
1460 * for the file to send
1461 * @fh: the filled-in fh value
1464 * returns -1 on error, 1 on success. This sends the whold file and
1465 * blocks, so it will totally mess you up if you use it. use the
1466 * send_chunk below and work out a schduling setup or some such.
1470 faim_export int aim_getfile_send(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh)
1473 const int bufsize = 4096;
1479 if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->subtype != AIM_CONN_SUBTYPE_OFT_GETFILE)
1480 { faimdprintf(1, "getfile_send: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype);
1484 { faimdprintf(1, "getfile_send: file pointer isn't valid\n");
1488 { faimdprintf(1, "getfile_send: fh isn't valid\n");
1494 if(!(buf = (char *)calloc(1, bufsize)))
1495 { perror("faim: getfile_send: calloc:");
1496 faimdprintf(2, "getfile_send calloc error\n");
1499 if( fseek(tosend, 0, SEEK_SET) == -1)
1500 { perror("faim: getfile_send: fseek:");
1501 faimdprintf(2, "getfile_send fseek error\n");
1502 } faimdprintf(2, "faim: getfile_send: using %i byte blocks\n", bufsize);
1505 pos++) { bufpos = pos % bufsize;
1506 if(bufpos == 0 && pos > 0)
1508 /* filled our buffer. spit it across the wire
1511 if ( (i = send(conn->fd, buf, bufsize, 0)) != bufsize )
1512 { perror("faim: getfile_send: write1: ");
1513 faimdprintf(1, "faim: getfile_send: whoopsy, didn't write it all...\n");
1517 if( (buf[bufpos] = fgetc(tosend)) == EOF)
1520 { printf("faim: getfile_send: hrm... apparent early EOF at pos 0x%x of 0x%lx\n", pos, fh->size);
1521 faimdprintf(1, "faim: getfile_send: hrm... apparent early EOF at pos 0x%lx of 0x%lx\n", pos, fh->size);
1525 if( (i = send(conn->fd, buf, bufpos+1, 0)) != (bufpos+1))
1526 { perror("faim: getfile_send: write2: ");
1527 faimdprintf(1, "faim: getfile_send cleanup: whoopsy, didn't write it all...\n");
1536 * aim_getfile_send_chunk - send a chunk of a file down a conn
1537 * @conn: the OFT connection to shove the data down,
1538 * @tosend: the buffer to send
1539 * @fh: the filled-in fh value
1540 * @pos: the position to start at (see below)
1541 * @bufsize: the size of the chunk to send
1544 * returns -1 on error, new pos on success.
1546 * You don't really have to keep track of pos if you don't want
1547 * to. just always call with -1 for pos, and it'll use the one
1548 * contained within the FILE
1552 * if (pos + chunksize > fh->size), we only send as much data as we
1553 * can get (ie: up to fh->size.
1556 * the value of pos: at beginning should be 0, after 5 bytes are sent
1557 * should be 5); -1 for "don't seek"
1560 * GOING TO BE CHANGED TO USE unsigned char
1563 faim_export int aim_getfile_send_chunk(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh, int pos, int bufsize)
1571 if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->type != AIM_CONN_SUBTYPE_OFT_GETFILE)
1572 { faimdprintf(1, "faim: getfile_send_chunk: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype);
1576 { faimdprintf(1, "faim: getfile_send_chunk: file pointer isn't valid\n");
1580 { faimdprintf(1, "faim: getfile_send_chunk: fh isn't valid\n");
1586 if(!(buf = (char *)calloc(1, bufsize)))
1587 { perror("faim: getfile_send_chunk: calloc:");
1588 faimdprintf(2, "faim: getfile_send_chunk calloc error\n");
1593 if( fseek(tosend, pos, SEEK_SET) == -1)
1594 { perror("faim: getfile_send_chunk: fseek:");
1595 faimdprintf(2, "faim: getfile_send_chunk fseek error\n");
1596 } } faimdprintf(2, "faim: getfile_send_chunk: using %i byte blocks\n", bufsize);
1600 if( (buf[bufpos] = fgetc(tosend)) == EOF)
1603 { faimdprintf(1, "faim: getfile_send_chunk: hrm... apparent early EOF at pos 0x%x of 0x%x\n", pos, fh->size);
1607 if( send(conn->fd, buf, bufpos+1, 0) != (bufpos+1))
1608 { faimdprintf(1, "faim: getfile_send_chunk cleanup: whoopsy, didn't write it all...\n");
1612 return (pos + bufpos);
1616 * aim_tx_destroy - free's tx_command_t's
1617 * @command: the command to free
1620 * if command is locked, doesn't free.
1621 * returns -1 on error (locked struct); 0 on success.
1625 faim_internal int aim_tx_destroy(struct command_tx_struct *command){
1629 free(command->data);
1630 if (command->hdrtype == AIM_FRAMETYPE_OFT && command->hdr.oft.hdr2)
1631 free(command->hdr.oft.hdr2);
1637 * aim_getfile_intitiate - For those times when we want to open up the getfile dialog ourselves.
1638 * @sess: your session,
1639 * @conn: the BOS conn,
1640 * @destsn is the SN to connect to.
1642 faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess, struct aim_conn_t *conn, char *destsn)
1644 struct command_tx_struct *newpacket;
1645 struct aim_conn_t *newconn;
1646 struct aim_filetransfer_priv *priv;
1647 struct aim_msgcookie_t *cookie;
1648 int curbyte, i, listenfd;
1650 struct hostent *hptr;
1651 struct utsname myname;
1655 /* Open our socket */
1657 if( (listenfd = aim_listenestablish(port)) == -1)
1660 /* get our local IP */
1662 if(uname(&myname) < 0)
1664 if( (hptr = gethostbyname(myname.nodename)) == NULL)
1666 memcpy(&d, hptr->h_addr_list[0], 4);
1668 /* XXX: this probably isn't quite kosher, but it works */
1669 aim_putcap(cap, 16, AIM_CAPS_GETFILE);
1671 /* create the OSCAR packet */
1673 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x42)))
1675 newpacket->lock = 1;
1679 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
1681 /* XXX: check the cookie before commiting to using it */
1683 /* Generate a random message cookie
1684 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */
1686 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 10));
1688 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1690 /* grab all the data for cookie caching. */
1692 cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t));
1693 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
1694 cookie->type = AIM_COOKIETYPE_OFTGET;
1696 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
1697 memcpy(priv->cookie, cookie, 8);
1698 memcpy(priv->sn, destsn, sizeof(priv->sn));
1699 memcpy(priv->fh.name, "listing.txt", strlen("listing.txt"));
1701 cookie->data = priv;
1703 aim_cachecookie(sess, cookie);
1705 /* cache da cookie */
1708 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
1710 /* Destination SN (prepended with byte length) */
1711 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
1712 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
1713 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1714 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1717 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1718 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0042);
1720 /* Flag data / ICBM Parameters? */
1721 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1722 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1725 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
1727 /* Capability String */
1728 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
1730 /* 000a/0002 : 0001 */
1731 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
1732 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1733 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
1735 /* 0003/0004: IP address */
1736 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1737 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
1738 for(i = 0; i < 4; i++)
1739 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]);
1741 /* already in network byte order */
1743 /* 0005/0002: Port */
1744 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1745 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1746 curbyte += aimutil_put16(newpacket->data+curbyte, port);
1749 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
1750 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1753 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
1754 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c);
1755 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00120001);
1757 for(i = 0; i < 0x000c - 4; i++)
1758 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1760 newpacket->commandlen = curbyte;
1761 newpacket->lock = 0;
1762 aim_tx_enqueue(sess, newpacket);
1764 /* allocate and set up our connection */
1766 i = fcntl(listenfd, F_GETFL, 0);
1767 fcntl(listenfd, F_SETFL, i | O_NONBLOCK);
1768 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL);
1771 perror("aim_newconn");
1775 newconn->fd = listenfd;
1776 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
1777 newconn->priv = priv;
1778 faimdprintf(2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
1784 * aim_oft_getfile_request - request a particular file over an established getfile connection
1785 * @sess: your session
1786 * @conn: the established OFT getfile connection
1787 * @name: filename to request
1788 * @size: size of the file
1791 faim_export int aim_oft_getfile_request(struct aim_session_t *sess, struct aim_conn_t *conn, const unsigned char *name, const int size)
1793 struct command_tx_struct *newoft;
1794 struct aim_filetransfer_priv *ft;
1795 if(!sess || !conn || !conn->priv || !name)
1798 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x120c, conn, 0))) {
1799 faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1805 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1806 newoft->hdr.oft.hdr2len = 0x100 - 8;
1808 ft = (struct aim_filetransfer_priv *)conn->priv;
1809 ft->fh.filesleft = 1;
1810 ft->fh.totfiles = 1;
1811 ft->fh.totparts = 1;
1812 ft->fh.partsleft = 1;
1813 ft->fh.totsize = size;
1815 ft->fh.checksum = 0;
1816 memcpy(ft->fh.name, name, strlen(name));
1817 memset(ft->fh.name+strlen(name), 0, 1);
1819 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1821 aim_tx_destroy(newoft);
1825 if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1827 aim_tx_destroy(newoft);
1833 aim_tx_enqueue(sess, newoft);
1838 * aim_oft_getfile_ack - acknowledge a getfile download as complete
1839 * @sess: your session
1840 * @conn: the getfile conn to send the ack over
1842 * call this function after you have read all the data in a particular
1846 faim_export int aim_oft_getfile_ack(struct aim_session_t *sess, struct aim_conn_t *conn) {
1847 struct command_tx_struct *newoft;
1848 struct aim_filetransfer_priv *ft;
1850 if(!sess || !conn || !conn->priv)
1853 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0202, conn, 0))) {
1854 faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1860 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1861 newoft->hdr.oft.hdr2len = 0x100-8;
1863 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1865 aim_tx_destroy(newoft);
1869 ft = (struct aim_filetransfer_priv *)conn->priv;
1871 if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1873 aim_tx_destroy(newoft);
1878 aim_tx_enqueue(sess, newoft);
1883 * aim_oft_getfile_end - end a getfile.
1884 * @sess: your session
1885 * @conn: the getfile connection
1887 * call this before you close the getfile connection if you're on the
1888 * receiving/requesting end.
1890 faim_export int aim_oft_getfile_end(struct aim_session_t *sess, struct aim_conn_t *conn)
1892 struct command_tx_struct *newoft;
1893 struct aim_filetransfer_priv *ft;
1895 if(!sess || !conn || !conn->priv)
1898 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0204, conn, 0))) {
1899 faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1905 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1906 newoft->hdr.oft.hdr2len = 0x100 - 8;
1908 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1910 aim_tx_destroy(newoft);
1914 ft = (struct aim_filetransfer_priv *)conn->priv;
1915 ft->state = 4; /* no longer wanting data */
1916 ft->fh.nrecvd = ft->fh.size;
1917 ft->fh.recvcsum = ft->fh.checksum;
1918 ft->fh.flags = 0x21;
1920 if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1922 aim_tx_destroy(newoft);
1927 aim_tx_enqueue(sess, newoft);