5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <sys/utsname.h> /* for aim_directim_initiate */
8 #include <arpa/inet.h> /* for inet_ntoa */
11 /* aim_msgcookies.c is mostly new. just look at the diff and replace yours, easiest. */
14 function name where i had it
15 aim_send_im_direct aim_im.c
16 aim_directim_initiate aim_im.c
17 aim_filetransfer_accept aim_im.c
18 aim_getlisting aim_misc.c (?!) -- prototype function. can be ignored.
20 aim_get_command_rendezvous aim_r
21 oft_getfh aim_rxqueue.c
24 faim_export int aim_handlerendconnect(struct aim_session_t *sess, struct aim_conn_t *cur)
27 rxcallback_t userfunc;
28 struct sockaddr cliaddr;
29 socklen_t clilen = sizeof(cliaddr);
33 * Listener sockets only have incoming connections. No data.
35 if( (acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1)
38 if (cliaddr.sa_family != AF_INET) /* just in case IPv6 really is happening */
41 switch(cur->subtype) {
42 case AIM_CONN_SUBTYPE_OFT_DIRECTIM: {
43 struct aim_directim_priv *priv;
45 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
47 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
50 cur->priv = priv; /* what happens if there is one?! -- mid */
52 cur->type = AIM_CONN_TYPE_RENDEZVOUS;
53 close(cur->fd); /* should we really do this? seems like the client should decide. maybe clone the connection and keep the listener open. -- mid */
56 if ( (userfunc = aim_callhandler(cur, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE)))
57 ret = userfunc(sess, NULL, cur);
61 case AIM_CONN_SUBTYPE_OFT_GETFILE: {
62 struct aim_filetransfer_priv *priv;
64 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
66 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
71 if ( (userfunc = aim_callhandler(cur, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE)))
72 ret = userfunc(sess, NULL, cur);
86 * conn - directim connection
87 * msg - null-terminated string to send
90 faim_export int aim_send_im_direct(struct aim_session_t *sess,
91 struct aim_conn_t *conn,
94 struct command_tx_struct *newpacket , *newpacket2;
96 /* newpacket contains a real header with data, newpacket2 is just a
97 null packet, with a cookie and a lot of 0x00s. newpacket is the
98 "i'm sending", newpacket2 is the "i'm typing".*/
100 /* uhm. the client should send those as two seperate things -- mid */
102 struct aim_directim_priv *priv = NULL;
105 if (!sess || !conn || !(conn->type) || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !conn->priv || !msg) {
106 printf("faim: directim: invalid arguments\n");
110 if (strlen(msg) >= MAXMSGLEN)
113 priv = (struct aim_directim_priv *)conn->priv;
117 if (!(newpacket2 = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, 0))) {
118 printf("faim: directim: tx_new2 failed\n");
122 newpacket2->lock = 1; /* lock struct */
124 memcpy(newpacket2->hdr.oft.magic, "ODC2", 4);
125 newpacket2->hdr.oft.hdr2len = 0x44;
127 if (!(newpacket2->hdr.oft.hdr2 = calloc(1,newpacket2->hdr.oft.hdr2len))) {
133 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0006);
134 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
136 i += aimutil_putstr(newpacket2->hdr.oft.hdr2+i, (char *)priv->cookie, 8);
138 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
139 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
140 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
141 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
143 i += aimutil_put32(newpacket2->hdr.oft.hdr2+i, 0x00000000);
145 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
146 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
147 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
149 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x000e);
151 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
152 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
154 i += aimutil_putstr(newpacket2->hdr.oft.hdr2+i, sess->logininfo.screen_name, strlen(sess->logininfo.screen_name));
157 i += aimutil_put8(newpacket2->hdr.oft.hdr2+i, 0x00); /* 53 */
158 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); /* 55 */
159 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
160 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
161 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* 61 */
162 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
163 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* 65 */
164 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* end of hdr2 */
166 newpacket2->lock = 0;
167 newpacket2->data = NULL;
169 aim_tx_enqueue(sess, newpacket2);
173 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, strlen(msg)))) {
174 printf("faim: directim: tx_new failed\n");
178 newpacket->lock = 1; /* lock struct */
180 memcpy(newpacket->hdr.oft.magic, "ODC2", 4);
181 newpacket->hdr.oft.hdr2len = 0x54;
183 if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) {
189 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0006);
190 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
192 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, (char *)priv->cookie, 8);
194 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
195 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
196 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
197 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
199 i += aimutil_put32(newpacket->hdr.oft.hdr2+i, strlen(msg));
201 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
202 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
203 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
204 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
205 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
206 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
208 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, sess->logininfo.screen_name, strlen(sess->logininfo.screen_name));
211 i += aimutil_put8(newpacket->hdr.oft.hdr2+i, 0x00); /* 53 */
212 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); /* 55 */
213 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
214 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
215 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 61 */
216 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
217 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 65 */
218 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* end of hdr2 */
220 /* values grabbed from a dump */
221 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008); /* 69 */
222 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c);
223 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 71 */
224 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466);/* 73 */
225 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001);/* 73 */
226 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f);
227 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e);
228 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8);
230 memcpy(newpacket->data, msg, strlen(msg));
234 aim_tx_enqueue(sess, newpacket);
240 * aim_directim_intitiate:
241 * For those times when we want to open up the directim channel ourselves.
242 * sess is your session,
243 * conn is the BOS conn,
244 * priv is a dummy priv value (we'll let it get filled in later) (if
245 * you pass a NULL, we alloc one)
246 * destsn is the SN to connect to.
250 faim_export struct aim_conn_t *aim_directim_initiate(struct aim_session_t *sess,
251 struct aim_conn_t *conn,
252 struct aim_directim_priv *priv,
255 struct command_tx_struct *newpacket;
256 struct aim_conn_t *newconn;
258 struct aim_msgcookie_t *cookie;
260 int curbyte, i, listenfd;
263 struct hostent *hptr;
266 unsigned char cap[16];
267 char d[4]; /* XXX: IPv6. *cough* */
273 if( (listenfd = aim_listenestablish(port)) == -1)
280 if(gethostname(localhost, 128) < 0)
283 if( (hptr = gethostbyname(localhost)) == NULL)
286 memcpy(&d, hptr->h_addr_list[0], 4); /* XXX: this probably isn't quite kosher, but it works */
288 aim_putcap(cap, 16, AIM_CAPS_IMIMAGE);
291 * create the OSCAR packet
294 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x32)))
297 newpacket->lock = 1; /* lock struct */
300 curbyte += aim_putsnac(newpacket->data+curbyte,
301 0x0004, 0x0006, 0x0000, sess->snac_nextid);
304 * Generate a random message cookie
305 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible.
308 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) rand() % 20));
309 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
312 * grab all the data for cookie caching.
314 cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t));
316 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
317 cookie->type = AIM_COOKIETYPE_OFTIM;
320 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
322 memcpy(priv->cookie, cookie, 8);
323 memcpy(priv->sn, destsn, sizeof(priv->sn));
327 aim_cachecookie(sess, cookie); /* cache da cookie */
332 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
335 * Destination SN (prepended with byte length)
337 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
338 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
340 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
341 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
346 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
347 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032);
350 * Flag data / ICBM Parameters?
352 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
353 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
358 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
363 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
368 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
369 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
370 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
373 * 0003/0004: IP address
376 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
377 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
379 for(i = 0; i < 4; i++)
380 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); /* already in network byte order */
386 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
387 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
388 curbyte += aimutil_put16(newpacket->data+curbyte, port);
391 * 000f/0000: umm.. dunno. Zigamorph[1]?
392 * [1]: see esr's TNHD.
395 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
396 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
398 printf("curbyte: 0x%x\n",curbyte);
400 newpacket->commandlen = curbyte;
403 aim_tx_enqueue(sess, newpacket);
406 * allocate and set up our connection
409 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL);
411 perror("aim_newconn");
412 aim_conn_kill(sess, &newconn);
416 newconn->fd = listenfd;
417 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
418 newconn->priv = priv;
419 printf("faim: listening (fd = %d, unconnected)\n", newconn->fd);
422 * XXX We need some way of closing the listener socket after
423 * n seconds of no connection. -- mid
426 #ifdef USE_SNAC_FOR_IMS
428 struct aim_snac_t snac;
430 snac.id = sess->snac_nextid;
431 snac.family = 0x0004;
435 snac.data = malloc(strlen(destsn)+1);
436 memcpy(snac.data, destsn, strlen(destsn)+1);
438 aim_newsnac(sess, &snac);
440 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */
448 * struct aim_conn_t *aim_directim_connect(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_directim_priv *priv)
449 * sess is the session to append the conn to,
450 * conn is the BOS connection,
451 * priv is the filled-in priv data structure for the connection
452 * returns conn if connected, NULL on error
456 faim_export struct aim_conn_t *aim_directim_connect(struct aim_session_t *sess,
457 struct aim_conn_t *conn,
458 struct aim_directim_priv *priv )
460 struct aim_conn_t *newconn = NULL;;
462 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, priv->ip);
463 if (!newconn || (newconn->fd == -1)) {
464 printf("could not connect to %s\n", priv->ip);
465 perror("aim_newconn");
466 aim_conn_kill(sess, &newconn);
469 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
470 newconn->priv = priv;
471 printf("faim: connected to peer (fd = %d)\n", newconn->fd);
478 * struct aim_conn_t *aim_directim_getconn(struct aim_session_t *sess, const char *name)
479 * sess is your session,
480 * name is the name to get,
481 * returns conn for directim with name, NULL if none found.
484 faim_export struct aim_conn_t *aim_directim_getconn(struct aim_session_t *sess, const char *name)
486 struct aim_conn_t *cur;
487 struct aim_directim_priv *priv;
489 faim_mutex_lock(&sess->connlistlock);
490 for (cur = sess->connlist; cur; cur = cur->next) {
491 if (cur->type != AIM_CONN_TYPE_RENDEZVOUS || cur->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM)
495 if (aim_sncmp(priv->sn, name) == 0)
498 faim_mutex_unlock(&sess->connlistlock);
504 * aim_accepttransfer:
505 * accept a file transfer request.
506 * sess is the session,
507 * conn is the BOS conn for the CAP reply
508 * sn is the screenname to send it to,
509 * cookie is the cookie used
510 * ip is the ip to connect to
511 * file is the listing file to use
517 faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess,
518 struct aim_conn_t *conn,
523 unsigned short rendid)
525 struct command_tx_struct *newpacket, *newoft;
527 struct aim_conn_t *newconn;
528 struct aim_fileheader_t *listingfh;
529 struct aim_filetransfer_priv *priv;
530 struct aim_msgcookie_t *cachedcook;
534 if(rendid == AIM_CAPS_GETFILE) {
536 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, ip);
538 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
540 if (!newconn || (newconn->fd == -1)) {
541 perror("aim_newconn");
542 faimdprintf(2, "could not connect to %s (fd: %i)\n", ip, newconn?newconn->fd:0);
543 aim_conn_kill(sess, &newconn);
546 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
547 memcpy(priv->cookie, cookie, 8);
548 strncpy(priv->sn, sn, MAXSNLEN);
549 strncpy(priv->ip, ip, sizeof(priv->ip));
550 newconn->priv = (void *)priv;
551 faimdprintf(2, "faim: connected to peer (fd = %d)\n", newconn->fd);
554 /* cache da cookie. COOOOOKIES! */
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");
558 /* XXX die here, or go on? search for cachedcook for future references */
562 memcpy(cachedcook->cookie, cookie, 8);
564 /* strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name)); */
566 if(cachedcook) { /* see above calloc of cachedcook */
567 cachedcook->type = AIM_COOKIETYPE_OFTGET;
568 cachedcook->data = (void *)priv;
570 if (aim_cachecookie(sess, cachedcook) != 0)
571 faimdprintf(1, "faim: ERROR caching message cookie\n");
574 if(rendid == AIM_CAPS_GETFILE) {
575 faimdprintf(2, "faim: getfile request accept\n");
576 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1108, newconn, 0))) {
577 faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n");
578 /* XXX: what else do we need to clean up here? */
584 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
585 newoft->hdr.oft.hdr2len = 0xf8; /* 0x100 - 8 */
587 if(!(listingfh = aim_getlisting(file))) {
591 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
596 memcpy(listingfh->bcookie, cookie, 8);
598 aim_oft_buildheader((void *)newoft->hdr.oft.hdr2, listingfh);
603 aim_tx_enqueue(sess, newoft);
604 printf("faim: getfile: OFT listing enqueued.\n");
608 /* OSCAR CAP accept packet */
610 if(!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(sn)+4+2+8+16)))
615 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
616 for (i = 0; i < 8; i++)
617 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
618 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
619 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
620 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
621 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
622 curbyte += aimutil_put16(newpacket->data+curbyte, 0x001a);
623 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept */);
624 for (i = 0; i < 8; i++)
625 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
626 curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid);
629 aim_tx_enqueue(sess, newpacket);
637 * aim_getlisting(FILE *file)
639 * file is an opened listing file
640 * returns a pointer to the filled-in fileheader_t
642 * currently omits checksum. we'll fix this when AOL breaks us, i
647 faim_internal struct aim_fileheader_t *aim_getlisting(FILE *file)
649 struct aim_fileheader_t *fh;
650 u_long totsize = 0, size = 0, checksum = 0xffff0000;
652 char *linebuf, sizebuf[9];
654 int linelength = 1024;
656 /* XXX: if we have a line longer than 1024chars, God help us. */
657 if( (linebuf = (char *)calloc(1, linelength)) == NULL ) {
658 faimdprintf(2, "linebuf calloc failed\n");
662 if(fseek(file, 0, SEEK_END) == -1) { /* use this for sanity check */
663 perror("getlisting END1 fseek:");
664 faimdprintf(2, "getlising fseek END1 error\n");
667 if(fgetpos(file, &size) == -1) {
668 perror("getlisting END1 getpos:");
669 faimdprintf(2, "getlising getpos END1 error\n");
672 if(fseek(file, 0, SEEK_SET) != 0) {
673 perror("getlesting fseek(SET):");
674 faimdprintf(2, "faim: getlisting: couldn't seek to beginning of listing file\n");
677 bzero(linebuf, linelength);
681 while(fgets(linebuf, linelength, file)) {
685 size += strlen(linebuf);
687 if(strlen(linebuf) < 23) {
688 faimdprintf(2, "line \"%s\" too short. skipping\n", linebuf);
691 if(linebuf[strlen(linebuf)-1] != '\n') {
692 faimdprintf(2, "faim: OFT: getlisting -- hit EOF or line too long!\n");
695 memcpy(sizebuf, linebuf+17, 8);
697 totsize += strtol(sizebuf, NULL, 10);
698 bzero(linebuf, linelength);
702 faimdprintf(2, "faim: getlisting: size != 0 after while.. %i\n", size);
705 if(fseek(file, 0, SEEK_SET) == -1) {
706 perror("getlisting END2 fseek:");
707 faimdprintf(2, "getlising fseek END2 error\n");
712 /* we're going to ignore checksumming the data for now -- that
713 * requires walking the whole listing.txt. it should probably be
714 * done at register time and cached, but, eh. */
716 if(!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t))))
719 printf( "faim: OFT: getlisting: totfiles: %u, totsize: %lu, size: %lu\n", totfiles, totsize, size);
721 fh->encrypt = 0x0000;
722 fh->compress = 0x0000;
723 fh->totfiles = totfiles;
724 fh->filesleft = totfiles; /* is this right ?*/
725 fh->totparts = 0x0001;
726 fh->partsleft = 0x0001;
727 fh->totsize = totsize;
728 fh->size = size; /* ls -l listing.txt */
729 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
730 fh->checksum = checksum; /* XXX: checksum ! */
731 fh->rfcsum = 0x00000000;
732 fh->rfsize = 0x00000000;
733 fh->cretime = 0x00000000;
734 fh->rfcsum = 0x00000000;
735 fh->nrecvd = 0x00000000;
736 fh->recvcsum = 0x00000000;
738 /* memset(fh->idstring, 0, sizeof(fh->idstring)); */
739 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
740 memset(fh->idstring+strlen(fh->idstring), 0, sizeof(fh->idstring)-strlen(fh->idstring));
743 fh->lnameoffset = 0x1a;
744 fh->lsizeoffset = 0x10;
746 /* memset(fh->dummy, 0, sizeof(fh->dummy)); */
747 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
749 fh->nencode = 0x0000; /* we need to figure out these encodings for filenames */
750 fh->nlanguage = 0x0000;
752 /* memset(fh->name, 0, sizeof(fh->name)); */
753 memcpy(fh->name, "listing.txt", sizeof(fh->name));
754 memset(fh->name+strlen(fh->name), 0, 64-strlen(fh->name));
756 faimdprintf(2, "faim: OFT: listing fh name %s / %s\n", fh->name, (fh->name+(strlen(fh->name))));
761 * establish: create a listening socket on a port. you need to call
762 * accept() when it's connected.
763 * portnum is the port number to bind to.
767 faim_internal int aim_listenestablish(u_short portnum)
769 #if defined(__linux__) /* XXX what other OS's support getaddrinfo? */
772 struct addrinfo hints, *res, *ressave;
774 sprintf(serv, "%d", portnum);
775 memset(&hints, 0, sizeof(struct addrinfo));
776 hints.ai_flags = AI_PASSIVE;
777 hints.ai_family = AF_UNSPEC;
778 hints.ai_socktype = SOCK_STREAM;
779 if (getaddrinfo(NULL/*any IP*/, serv, &hints, &res) != 0) {
780 perror("getaddrinfo");
785 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
788 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
789 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
792 } while ( (res = res->ai_next) );
795 if (listen(listenfd, 1024)!=0) {
799 freeaddrinfo(ressave);
804 struct sockaddr_in sockin;
806 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
807 perror("socket(listenfd)");
810 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) != 0)) {
811 perror("setsockopt(listenfd)");
815 memset(&sockin, 0, sizeof(struct sockaddr_in));
816 sockin.sin_family = AF_INET;
817 sockin.sin_port = htons(portnum);
818 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
819 perror("bind(listenfd)");
823 if (listen(listenfd, 4) != 0) {
824 perror("listen(listenfd)");
833 faim_internal int aim_get_command_rendezvous(struct aim_session_t *sess, struct aim_conn_t *conn)
835 unsigned char hdrbuf1[6];
836 unsigned char *hdr = NULL;
839 rxcallback_t userfunc = NULL;
842 memset(hdrbuf1, 0, sizeof(hdrbuf1));
844 faim_mutex_lock(&conn->active); /* gets locked down for the entirety */
846 if ( (hdrlen = aim_recv(conn->fd, hdrbuf1, 6)) < 6) {
848 faimdprintf(2, "faim: rend: read error (fd: %i) %02x%02x%02x%02x%02x%02x (%i)\n", conn->fd, hdrbuf1[0],hdrbuf1[1],hdrbuf1[0],hdrbuf1[0],hdrbuf1[0],hdrbuf1[0],hdrlen);
849 faim_mutex_unlock(&conn->active);
850 aim_conn_close(conn);
854 else { /* disconnected */
856 switch(conn->subtype) {
857 case AIM_CONN_SUBTYPE_OFT_DIRECTIM: { /* XXX: clean up cookies here ? */
858 struct aim_directim_priv *priv;
859 if(!(priv = (struct aim_directim_priv *)conn->priv) )
860 return -1; /* not much we can do */
862 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT)) )
863 i = userfunc(sess, NULL, conn, priv->sn);
865 aim_conn_kill(sess, &conn);
867 aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTIM);
869 case AIM_CONN_SUBTYPE_OFT_GETFILE: {
870 struct aim_filetransfer_priv *priv;
871 if(!(priv = (struct aim_filetransfer_priv *)conn->priv))
874 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT)) )
875 i = userfunc(sess, NULL, conn, priv->sn);
877 aim_conn_kill(sess, &conn);
879 aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTGET);
881 case AIM_CONN_SUBTYPE_OFT_SENDFILE: {
882 struct aim_filetransfer_priv *priv;
883 if(!(priv = (struct aim_filetransfer_priv *)conn->priv))
886 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_SENDFILEDISCONNECT)) )
887 i = userfunc(sess, NULL, conn, priv->sn);
889 aim_conn_kill(sess, &conn);
892 aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTSEND);
900 hdrlen = aimutil_get16(hdrbuf1+4);
903 if (!(hdr = malloc(hdrlen))) {
904 faim_mutex_unlock(&conn->active);
908 if (aim_recv(conn->fd, hdr, hdrlen) < hdrlen) {
910 faimdprintf(2,"faim: rend: read2 error\n");
912 faim_mutex_unlock(&conn->active);
913 aim_conn_close(conn);
917 hdrtype = aimutil_get16(hdr);
920 case 0x0001: { /* directim */
921 int payloadlength = 0;
923 struct aim_directim_priv *priv;
926 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
928 payloadlength = aimutil_get32(hdr+22);
929 flags = aimutil_get16(hdr+32);
930 snptr = (char *)hdr+38;
932 strncpy(priv->sn, snptr, MAXSNLEN);
934 faimdprintf(2, "faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, snptr);
936 if (flags == 0x000e) {
937 faim_mutex_unlock(&conn->active);
938 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)) )
939 return userfunc(sess, NULL, snptr);
940 } else if ((flags == 0x0000) && payloadlength) {
943 if(! (msg = calloc(1, payloadlength+1)) ) {
944 faim_mutex_unlock(&conn->active);
948 if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) {
950 printf("faim: rend: read3 error\n");
952 faim_mutex_unlock(&conn->active);
953 aim_conn_close(conn);
956 faim_mutex_unlock(&conn->active);
957 msg[payloadlength] = '\0';
958 faimdprintf(2, "faim: directim: %s/%04x/%04x/%s\n", snptr, payloadlength, flags, msg);
961 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) )
962 i = userfunc(sess, NULL, conn, snptr, msg);
970 /* currently experimental to a non-compiling degree */
971 case 0x1108: { /* getfile listing.txt incoming tx->rx */
972 struct aim_filetransfer_priv *ft;
973 struct aim_fileheader_t *fh;
974 struct aim_msgcookie_t *cook;
975 struct aim_conn_type *newoft;
980 faimdprintf(2,"faim: rend: fileget 0x1108\n");
982 if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) {
983 faimdprintf(2, "faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n");
984 faim_mutex_unlock(&conn->active);
988 ft->state = 1; /* we're waaaaiiiting.. */
990 fh = aim_oft_getfh(hdr);
992 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
994 if(!(cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET))) {
995 faim_mutex_unlock(&conn->active);
1004 aim_cachecookie(sess, cook);
1006 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1209, conn, 0))) {
1007 /* XXX: what else do we need to clean up here? */
1008 faim_mutex_unlock(&conn->active);
1013 aim_oft_buildheader((void *)newoft->hdr.oft.hdr2, ft->fh); /* no change */
1015 aim_tx_enqueue(sess, newoft);
1019 case 0x1209: { /* get file listing ack rx->tx */
1020 struct aim_filetransfer_priv *ft;
1021 struct aim_fileheader_t *fh;
1022 struct aim_msgcookie_t *cook;
1027 faimdprintf(2,"faim: rend: fileget 0x1209\n");
1029 if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) {
1030 faimdprintf(2, "faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n");
1031 faim_mutex_unlock(&conn->active);
1035 fh = aim_oft_getfh(hdr);
1037 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1039 cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET);
1041 /* we currently trust the other client to be giving us Valid
1042 * Enough input, else this gets to be a messy function (and we
1043 * won't break like winaim does when it gets bad input =) */
1046 free(cook->data); /* XXX */
1050 aim_cachecookie(sess, cook);
1052 /* XXX: have this send chunks of the file instead of the whole
1053 * file. requires rethinking some code. */
1055 if(fseek(sess->oft.listing, 0, SEEK_SET) != 0) {
1056 perror("get_command_rendezvous 1209 fseek(SET):");
1057 faimdprintf(2, "faim: getlisting: couldn't seek to beginning of listing file\n");
1059 commandlen = ft->fh.size;
1061 if((data = (char *)calloc(1, commandlen)) == NULL) {
1062 faimdprintf(2, "faim: get_command_rendezvous 1209: couldn't malloc data.\n");
1063 faim_mutex_unlock(&conn->active);
1068 for(commandlen = 0; commandlen < ft->fh.size; commandlen++)
1069 if( (data[commandlen] = (unsigned char)fgetc(sess->oft.listing)) == EOF)
1070 faimdprintf(2, "faim: get_command_rendezvous 1209: got early EOF (error?)\n");
1072 commandlen = ft->fh.size;
1074 if (write(conn->fd, data, commandlen) != commandlen) {
1075 perror("listing write error");
1077 faim_mutex_unlock(&conn->active);
1079 faimdprintf(2, "faim: get_command_rendezvous: hit end of 1209\n");
1085 case 0x120b: { /* get file second */
1086 struct aim_filetransfer_priv *ft;
1087 struct aim_msgcookie_t *cook;
1089 struct aim_fileheader_t *fh;
1091 faimdprintf(2, "faim: rend: fileget 120b\n");
1093 if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) {
1094 faimdprintf(2, "faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n");
1095 faim_mutex_unlock(&conn->active);
1099 fh = aim_oft_getfh(hdr);
1101 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1103 if(!(cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET))) {
1104 faim_mutex_unlock(&conn->active);
1109 free(cook->data); /* XXX: integrate cookie caching */
1113 aim_cachecookie(sess, cook);
1115 faim_mutex_unlock(&conn->active);
1117 /* call listing.txt rx confirm */
1121 case 0x120c: { /* yet more get file */
1122 struct aim_filetransfer_priv *ft;
1123 struct aim_msgcookie_t *cook;
1124 struct aim_fileheader_t *listingfh;
1125 struct command_tx_struct *newoft;
1129 rxcallback_t userfunc;
1131 printf("faim: rend: fileget 120c\n");
1133 if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) {
1134 printf("faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n");
1135 faim_mutex_unlock(&conn->active);
1140 printf("faim: fileget_command(120c): um. hdrlen != 0x100..\n");
1142 listingfh = aim_oft_getfh((char *)hdr);
1144 memcpy(&(ft->fh), listingfh, sizeof(struct aim_fileheader_t));
1146 if(!(cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET))) {
1147 faim_mutex_unlock(&conn->active);
1152 free(cook->data); /* XXX */
1156 aim_cachecookie(sess, cook);
1158 faim_mutex_unlock(&conn->active);
1160 faimdprintf(2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name);
1162 if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) )
1163 i = userfunc(sess, NULL, conn, &ft->fh, cook->cookie);
1168 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0101, conn, 0))) {
1169 faimdprintf(2, "faim: send_final_transfer: tx_new OFT failed\n");
1175 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1176 newoft->hdr.oft.hdr2len = 0xf8; /* 0x100 - 8 */
1178 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1180 aim_tx_destroy(newoft);
1184 /* memcpy(listingfh->bcookie, ft->fh.bcookie, 8); */
1186 listingfh->nrecvd = 0; /* these need reset for protocol-correctness */
1187 listingfh->recvcsum = 0;
1189 aim_oft_buildheader((void *)newoft->hdr.oft.hdr2, listingfh);
1192 aim_tx_enqueue(sess, newoft);
1193 faimdprintf(2, "faim: OFT: OFT file enqueued.\n");
1195 if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL)
1198 i = userfunc(sess, NULL, conn, listingfh, listingfh->bcookie);
1206 case 0x0202: { /* get file: ready to recieve data */
1207 struct aim_fileheader_t *fh;
1208 fh = aim_oft_getfh((char *)hdr);
1210 faimdprintf(2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n");
1212 faim_mutex_unlock(&conn->active);
1214 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) == NULL)
1217 return userfunc(sess, NULL, conn, fh);
1223 case 0x0204: { /* get file: finished. close it up */
1226 struct aim_fileheader_t *fh;
1227 fh = aim_oft_getfh((char *)hdr);
1229 faimdprintf(2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n");
1231 faim_mutex_unlock(&conn->active);
1233 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) )
1234 i = userfunc(sess, NULL, conn, fh);
1240 /* not sure where to do this yet, as we need to keep it to allow multiple file sends... bleh */
1246 printf("OFT frame: type %04x\n", hdrtype);
1247 /* data connection may be unreliable here */
1248 faim_mutex_unlock(&conn->active);
1259 * aim_oft_registerlisting()
1261 * file: opened FILE *
1262 * listingdir: the path to listing.txt
1263 * returns -1 on error, 0 on success.
1265 * it's not my problem if the listing fd is already set.
1268 faim_export int aim_oft_registerlisting(struct aim_session_t *sess, FILE *file, char* listingdir)
1273 /* XXX: checksum each file in the listing */
1276 if(sess->oft.listing) {
1277 faimdprintf(1, "We already have a file pointer. Closing and overwriting.\n");
1278 fclose(sess->oft.listing);
1281 sess->oft.listing = file;
1283 if(sess->oft.listingdir) {
1284 faimdprintf(1, "We already have a directory string. Freeing and overwriting\n");
1285 free(sess->oft.listingdir);
1289 if( (sess->oft.listingdir = (char *)calloc(1, strlen(listingdir)+1)) )
1290 memcpy(sess->oft.listingdir, listingdir, strlen(listingdir));
1296 faim_internal struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr)
1298 struct aim_fileheader_t *fh;
1301 if(!(fh = calloc(1, sizeof(struct aim_fileheader_t))))
1304 /* [0] and [1] are the type. we can ignore those here. */
1308 for(j = 0; j < 8; j++, i++)
1309 fh->bcookie[j] = hdr[i];
1310 fh->encrypt = aimutil_get16(hdr+i);
1312 fh->compress = aimutil_get16(hdr+i);
1314 fh->totfiles = aimutil_get16(hdr+i);
1316 fh->filesleft = aimutil_get16(hdr+i);
1318 fh->totparts = aimutil_get16(hdr+i);
1320 fh->partsleft = aimutil_get16(hdr+i);
1322 fh->totsize = aimutil_get32(hdr+i);
1324 fh->size = aimutil_get32(hdr+i);
1326 fh->modtime = aimutil_get32(hdr+i);
1328 fh->checksum = aimutil_get32(hdr+i);
1330 fh->rfrcsum = aimutil_get32(hdr+i);
1332 fh->rfsize = aimutil_get32(hdr+i);
1334 fh->cretime = aimutil_get32(hdr+i);
1336 fh->rfcsum = aimutil_get32(hdr+i);
1338 fh->nrecvd = aimutil_get32(hdr+i);
1340 fh->recvcsum = aimutil_get32(hdr+i);
1343 memcpy(fh->idstring, hdr+i, 32);
1346 fh->flags = aimutil_get8(hdr+i);
1348 fh->lnameoffset = aimutil_get8(hdr+i);
1350 fh->lsizeoffset = aimutil_get8(hdr+i);
1353 memcpy(fh->dummy, hdr+i, 69);
1356 memcpy(fh->macfileinfo, hdr+i, 16);
1359 fh->nencode = aimutil_get16(hdr+i);
1361 fh->nlanguage = aimutil_get16(hdr+i);
1364 memcpy(fh->name, hdr+i, 64);
1370 faim_export int aim_oft_checksum(char *buffer, int bufsize, int *checksum)
1372 short check0, check1;
1375 check0 = ((*checksum & 0xFF000000) >> 16);
1376 check1 = ((*checksum & 0x00ff0000) >> 16);
1378 for(i = 0; i < bufsize; i++) {
1380 if(i % 2) { /* use check1 -- second byte */
1381 if ( (short)buffer[i] > check1 ) { /* wrapping */
1383 check1 += 0x100; /* this is a cheap way to wrap */
1385 /* if we're wrapping, decrement the other one */
1386 if(check0 == 0) /* XXX: check this corner case */
1392 check1 -= buffer[i];
1393 } else { /* use check0 -- first byte */
1394 if ( (short)buffer[i] > check0 ) { /* wrapping */
1396 check0 += 0x100; /* this is a cheap way to wrap */
1398 /* if we're wrapping, decrement the other one */
1399 if(check1 == 0) /* XXX: check this corner case */
1405 check0 -= buffer[i];
1409 if(check0 > 0xff || check1 > 0xff) { /* they shouldn't be able to do this. error! */
1410 faimdprintf(2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1);
1414 check0 &= 0xff; /* grab just the lowest byte */
1415 check1 &= 0xff; /* this should be clean, but just in case */
1417 *checksum = ((check0 * 0x1000000) + (check1 * 0x10000));
1424 * aim_oft_buildheader:
1425 * fills a buffer with network-order fh data.
1426 * returns length written; -1 on error.
1427 * dest: buffer to fill -- pre-alloced
1428 * listingfh: fh to get data from
1430 * DOES NOT DO BOUNDS CHECKING!
1434 faim_internal int aim_oft_buildheader(char *dest,struct aim_fileheader_t *listingfh)
1438 if(!dest || !listingfh)
1442 for(i = 0; i < 8; i++)
1443 curbyte += aimutil_put8(dest+curbyte, listingfh->bcookie[i]);
1444 curbyte += aimutil_put16(dest+curbyte, listingfh->encrypt);
1445 curbyte += aimutil_put16(dest+curbyte, listingfh->compress);
1446 curbyte += aimutil_put16(dest+curbyte, listingfh->totfiles);
1447 curbyte += aimutil_put16(dest+curbyte, listingfh->filesleft);
1448 curbyte += aimutil_put16(dest+curbyte, listingfh->totparts);
1449 curbyte += aimutil_put16(dest+curbyte, listingfh->partsleft);
1450 curbyte += aimutil_put32(dest+curbyte, listingfh->totsize);
1451 curbyte += aimutil_put32(dest+curbyte, listingfh->size);
1452 curbyte += aimutil_put32(dest+curbyte, listingfh->modtime);
1453 curbyte += aimutil_put32(dest+curbyte, listingfh->checksum);
1454 curbyte += aimutil_put32(dest+curbyte, listingfh->rfrcsum);
1455 curbyte += aimutil_put32(dest+curbyte, listingfh->rfsize);
1456 curbyte += aimutil_put32(dest+curbyte, listingfh->cretime);
1457 curbyte += aimutil_put32(dest+curbyte, listingfh->rfcsum);
1458 curbyte += aimutil_put32(dest+curbyte, listingfh->nrecvd);
1459 curbyte += aimutil_put32(dest+curbyte, listingfh->recvcsum);
1461 memcpy(dest+curbyte, listingfh->idstring, 32);
1464 curbyte += aimutil_put8(dest+curbyte, listingfh->flags);
1465 curbyte += aimutil_put8(dest+curbyte, listingfh->lnameoffset);
1466 curbyte += aimutil_put8(dest+curbyte, listingfh->lsizeoffset);
1468 memcpy(dest+curbyte, listingfh->dummy, 69);
1471 memcpy(dest+curbyte, listingfh->macfileinfo, 16);
1474 curbyte += aimutil_put16(dest+curbyte, listingfh->nencode);
1475 curbyte += aimutil_put16(dest+curbyte, listingfh->nlanguage);
1477 memcpy(dest+curbyte, listingfh->name, 64); /* XXX: Filenames longer than 64B */
1484 * int aim_getfile_send(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh)
1485 * conn is the OFT connection to shove the data down,
1486 * tosend is the FILE * for the file to send
1487 * fh is the filled-in fh value
1488 * returns -1 on error, 1 on success.
1491 faim_export int aim_getfile_send(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh)
1494 const int bufsize = 4096;
1498 if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->subtype != AIM_CONN_SUBTYPE_OFT_GETFILE) {
1499 faimdprintf(1, "getfile_send: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype);
1504 faimdprintf(1, "getfile_send: file pointer isn't valid\n");
1509 faimdprintf(1, "getfile_send: fh isn't valid\n");
1515 if(!(buf = (char *)calloc(1, bufsize))) {
1516 perror("faim: getfile_send: calloc:");
1517 faimdprintf(2, "getfile_send calloc error\n");
1523 if( fseek(tosend, 0, SEEK_SET) == -1) {
1524 perror("faim: getfile_send: fseek:");
1525 faimdprintf(2, "getfile_send fseek error\n");
1528 faimdprintf(2, "faim: getfile_send: using %i byte blocks\n", bufsize);
1530 for(pos = 0; pos < fh->size; pos++) {
1531 bufpos = pos % bufsize;
1533 if(bufpos == 0 && pos > 0) { /* filled our buffer. spit it across the wire */
1534 if ( (i = write(conn->fd, buf, bufsize)) != bufsize ) {
1535 perror("faim: getfile_send: write1: ");
1536 faimdprintf(1, "faim: getfile_send: whoopsy, didn't write it all...\n");
1541 if( (buf[bufpos] = fgetc(tosend)) == EOF) {
1542 if(pos != fh->size) {
1543 printf("faim: getfile_send: hrm... apparent early EOF at pos 0x%x of 0x%lx\n", pos, fh->size);
1544 faimdprintf(1, "faim: getfile_send: hrm... apparent early EOF at pos 0x%lx of 0x%lx\n", pos, fh->size);
1553 if( (i = write(conn->fd, buf, bufpos+1)) != (bufpos+1)) {
1554 perror("faim: getfile_send: write2: ");
1555 faimdprintf(1, "faim: getfile_send cleanup: whoopsy, didn't write it all...\n");
1567 * int aim_getfile_send_chunk(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh, int pos, int bufsize)
1568 * conn is the OFT connection to shove the data down,
1569 * tosend is the FILE * for the file to send
1570 * fh is the filled-in fh value
1571 * pos is the position to start at (at beginning should be 0, after 5
1572 * bytes are sent should be 5); -1 for "don't seek"
1573 * bufsize is the size of the chunk to send
1575 * returns -1 on error, new pos on success.
1579 * You don't really have to keep track of pos if you don't want
1580 * to. just always call with -1 for pos, and it'll use the one
1581 * contained within the FILE *.
1583 * if (pos + chunksize > fh->size), we only send as much data as we
1584 * can get (ie: up to fh->size.
1587 faim_export int aim_getfile_send_chunk(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh, int pos, int bufsize)
1593 if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->type != AIM_CONN_SUBTYPE_OFT_GETFILE) {
1594 faimdprintf(1, "faim: getfile_send_chunk: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype);
1599 faimdprintf(1, "faim: getfile_send_chunk: file pointer isn't valid\n");
1604 faimdprintf(1, "faim: getfile_send_chunk: fh isn't valid\n");
1610 if(!(buf = (char *)calloc(1, bufsize))) {
1611 perror("faim: getfile_send_chunk: calloc:");
1612 faimdprintf(2, "faim: getfile_send_chunk calloc error\n");
1617 if( fseek(tosend, pos, SEEK_SET) == -1) {
1618 perror("faim: getfile_send_chunk: fseek:");
1619 faimdprintf(2, "faim: getfile_send_chunk fseek error\n");
1623 faimdprintf(2, "faim: getfile_send_chunk: using %i byte blocks\n", bufsize);
1625 for(bufpos = 0; pos < fh->size; bufpos++, pos++) {
1626 if( (buf[bufpos] = fgetc(tosend)) == EOF) {
1627 if(pos != fh->size) {
1628 faimdprintf(1, "faim: getfile_send_chunk: hrm... apparent early EOF at pos 0x%x of 0x%x\n", pos, fh->size);
1635 if( write(conn->fd, buf, bufpos+1) != (bufpos+1)) {
1636 faimdprintf(1, "faim: getfile_send_chunk cleanup: whoopsy, didn't write it all...\n");
1643 return (pos + bufpos);
1648 * free's tx_command_t's. if command is locked, doesn't free.
1649 * returns -1 on error (locked struct); 0 on success.
1650 * command is the command to free
1653 faim_internal int aim_tx_destroy(struct command_tx_struct *command)
1658 free(command->data);
1666 * aim_getfile_intitiate:
1667 * For those times when we want to open up the directim channel ourselves.
1668 * sess is your session,
1669 * conn is the BOS conn,
1670 * priv is a dummy priv value (we'll let it get filled in later) (if
1671 * you pass a NULL, we alloc one)
1672 * destsn is the SN to connect to.
1676 faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess,
1677 struct aim_conn_t *conn,
1678 struct aim_getfile_priv *priv,
1681 struct command_tx_struct *newpacket;
1682 struct aim_conn_t *newconn;
1684 struct aim_msgcookie_t *cookie;
1686 int curbyte, i, listenfd;
1689 struct hostent *hptr;
1690 struct utsname myname;
1693 char d[4]; /* XXX: IPv6. *cough* */
1699 if( (listenfd = aim_listenestablish(port)) == -1)
1706 if(uname(&myname) < 0)
1709 if( (hptr = gethostbyname(myname.nodename)) == NULL)
1712 memcpy(&d, hptr->h_addr_list[0], 4); /* XXX: this probably isn't quite kosher, but it works */
1714 aim_putcap(cap, 16, AIM_CAPS_IMIMAGE);
1717 * create the OSCAR packet
1720 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x32)))
1723 newpacket->lock = 1; /* lock struct */
1726 curbyte += aim_putsnac(newpacket->data+curbyte,
1727 0x0004, 0x0006, 0x0000, sess->snac_nextid);
1730 * Generate a random message cookie
1731 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible.
1734 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 20));
1735 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1738 * grab all the data for cookie caching.
1740 cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t));
1742 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
1743 cookie->type = AIM_COOKIETYPE_OFTIM;
1746 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
1748 memcpy(priv->cookie, cookie, 8);
1749 memcpy(priv->sn, destsn, sizeof(priv->sn));
1751 cookie->data = priv;
1753 aim_cachecookie(sess, cookie); /* cache da cookie */
1758 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
1761 * Destination SN (prepended with byte length)
1763 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
1764 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
1766 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1767 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1772 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1773 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032);
1776 * Flag data / ICBM Parameters?
1778 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1779 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1784 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
1789 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
1794 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
1795 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1796 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
1799 * 0003/0004: IP address
1802 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1803 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
1805 for(i = 0; i < 4; i++)
1806 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); /* already in network byte order */
1812 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1813 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1814 curbyte += aimutil_put16(newpacket->data+curbyte, port);
1817 * 000f/0000: umm.. dunno. Zigamorph[1]?
1818 * [1]: see esr's TNHD.
1821 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
1822 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
1824 printf("curbyte: 0x%x\n",curbyte);
1826 newpacket->commandlen = curbyte;
1827 newpacket->lock = 0;
1829 aim_tx_enqueue(sess, newpacket);
1832 * allocate and set up our connection
1835 i = fcntl(listenfd, F_GETFL, 0);
1836 fcntl(listenfd, F_SETFL, i | O_NONBLOCK);
1838 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL);
1840 perror("aim_newconn");
1841 aim_conn_kill(sess, &newconn);
1845 newconn->fd = listenfd;
1846 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
1847 newconn->priv = priv;
1848 printf("faim: listening (fd = %d, unconnected)\n", newconn->fd);
1851 * XXX We need some way of closing the listener socket after
1852 * n seconds of no connection. -- mid
1855 #ifdef USE_SNAC_FOR_IMS
1857 struct aim_snac_t snac;
1859 snac.id = sess->snac_nextid;
1860 snac.family = 0x0004;
1862 snac.flags = 0x0000;
1864 snac.data = malloc(strlen(destsn)+1);
1865 memcpy(snac.data, destsn, strlen(destsn)+1);
1867 aim_newsnac(sess, &snac);
1869 aim_cleansnacs(sess, 60); /* clean out all SNACs over 60sec old */