3 #include <sys/utsname.h> /* for aim_directim_initiate */
4 #include <arpa/inet.h> /* for inet_ntoa */
6 /* aim_msgcookies.c is mostly new. just look at the diff and replace yours, easiest. */
9 function name where i had it
10 aim_send_im_direct aim_im.c
11 aim_directim_initiate aim_im.c
12 aim_filetransfer_accept aim_im.c
13 aim_getlisting aim_misc.c (?!) -- prototype function. can be ignored.
15 aim_get_command_rendezvous aim_rxqueue.c
16 oft_getfh aim_rxqueue.c
19 int aim_handlerendconnect(struct aim_session_t *sess, struct aim_conn_t *cur)
22 rxcallback_t userfunc;
23 struct sockaddr cliaddr;
24 socklen_t clilen = sizeof(cliaddr);
28 * Listener sockets only have incoming connections. No data.
30 if( (acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1)
33 if (cliaddr.sa_family != AF_INET) /* just in case IPv6 really is happening */
36 switch(cur->subtype) {
37 case AIM_CONN_SUBTYPE_OFT_DIRECTIM: {
38 struct aim_directim_priv *priv;
40 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
42 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
45 cur->priv = priv; /* what happens if there is one?! -- mid */
47 cur->type = AIM_CONN_TYPE_RENDEZVOUS;
48 close(cur->fd); /* should we really do this? seems like the client should decide. maybe clone the connection and keep the listener open. -- mid */
51 if ( (userfunc = aim_callhandler(cur, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE)))
52 ret = userfunc(sess, NULL, cur);
56 case AIM_CONN_SUBTYPE_OFT_GETFILE: {
57 struct aim_filetransfer_priv *priv;
61 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
63 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
68 if ( (userfunc = aim_callhandler(cur, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE)))
69 ret = userfunc(sess, NULL, cur);
83 * conn - directim connection
84 * msg - null-terminated string to send
87 int aim_send_im_direct(struct aim_session_t *sess,
88 struct aim_conn_t *conn,
91 struct command_tx_struct *newpacket , *newpacket2;
93 /* newpacket contains a real header with data, newpacket2 is just a
94 null packet, with a cookie and a lot of 0x00s. newpacket is the
95 "i'm sending", newpacket2 is the "i'm typing".*/
97 /* uhm. the client should send those as two seperate things -- mid */
99 struct aim_directim_priv *priv = NULL;
102 if (strlen(msg) >= MAXMSGLEN)
105 if (!sess || !conn || !(conn->type) || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !conn->priv) {
106 printf("faim: directim: invalid arguments\n");
110 priv = (struct aim_directim_priv *)conn->priv;
114 if (!(newpacket2 = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, 0))) {
115 printf("faim: directim: tx_new2 failed\n");
119 newpacket2->lock = 1; /* lock struct */
121 memcpy(newpacket2->hdr.oft.magic, "ODC2", 4);
122 newpacket2->hdr.oft.hdr2len = 0x44;
124 if (!(newpacket2->hdr.oft.hdr2 = calloc(1,newpacket2->hdr.oft.hdr2len))) {
130 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0006);
131 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
133 i += aimutil_putstr(newpacket2->hdr.oft.hdr2+i, priv->cookie, 8);
135 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
136 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
137 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
138 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
140 i += aimutil_put32(newpacket2->hdr.oft.hdr2+i, 0x00000000);
142 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
143 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
144 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
146 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x000e);
148 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
149 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
151 i += aimutil_putstr(newpacket2->hdr.oft.hdr2+i, sess->logininfo.screen_name, strlen(sess->logininfo.screen_name));
154 i += aimutil_put8(newpacket2->hdr.oft.hdr2+i, 0x00); /* 53 */
155 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); /* 55 */
156 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
157 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
158 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* 61 */
159 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);
160 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* 65 */
161 i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* end of hdr2 */
163 newpacket2->lock = 0;
164 newpacket2->data = NULL;
166 aim_tx_enqueue(sess, newpacket2);
170 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, strlen(msg)))) {
171 printf("faim: directim: tx_new failed\n");
175 newpacket->lock = 1; /* lock struct */
177 memcpy(newpacket->hdr.oft.magic, "ODC2", 4);
178 newpacket->hdr.oft.hdr2len = 0x54;
180 if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) {
186 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0006);
187 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
189 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, priv->cookie, 8);
191 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
192 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
193 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
194 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
196 i += aimutil_put32(newpacket->hdr.oft.hdr2+i, strlen(msg));
198 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
199 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
200 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
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);
205 i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, sess->logininfo.screen_name, strlen(sess->logininfo.screen_name));
208 i += aimutil_put8(newpacket->hdr.oft.hdr2+i, 0x00); /* 53 */
209 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); /* 55 */
210 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
211 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
212 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 61 */
213 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
214 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 65 */
215 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* end of hdr2 */
217 /* values grabbed from a dump */
218 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008); /* 69 */
219 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c);
220 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 71 */
221 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466);/* 73 */
222 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001);/* 73 */
223 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f);
224 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e);
225 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8);
227 memcpy(newpacket->data, msg, strlen(msg));
231 aim_tx_enqueue(sess, newpacket);
237 * aim_directim_intitiate:
238 * For those times when we want to open up the directim channel ourselves.
239 * sess is your session,
240 * conn is the BOS conn,
241 * priv is a dummy priv value (we'll let it get filled in later) (if
242 * you pass a NULL, we alloc one)
243 * destsn is the SN to connect to.
247 struct aim_conn_t *aim_directim_initiate(struct aim_session_t *sess,
248 struct aim_conn_t *conn,
249 struct aim_directim_priv *priv,
252 struct command_tx_struct *newpacket;
253 struct aim_conn_t *newconn;
255 struct aim_msgcookie_t *cookie;
257 int curbyte, i, listenfd;
260 struct hostent *hptr;
261 struct utsname myname;
264 char d[4]; /* XXX: IPv6. *cough* */
270 if( (listenfd = aim_listenestablish(port)) == -1)
277 if(uname(&myname) < 0)
280 if( (hptr = gethostbyname(myname.nodename)) == NULL)
283 memcpy(&d, hptr->h_addr_list[0], 4); /* XXX: this probably isn't quite kosher, but it works */
285 aim_putcap(cap, 16, AIM_CAPS_IMIMAGE);
288 * create the OSCAR packet
291 if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x32)))
294 newpacket->lock = 1; /* lock struct */
297 curbyte += aim_putsnac(newpacket->data+curbyte,
298 0x0004, 0x0006, 0x0000, sess->snac_nextid);
301 * Generate a random message cookie
302 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible.
305 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 20));
306 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
309 * grab all the data for cookie caching.
311 cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t));
313 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
314 cookie->type = AIM_COOKIETYPE_OFTIM;
317 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
319 memcpy(priv->cookie, cookie, 8);
320 memcpy(priv->sn, destsn, sizeof(priv->sn));
324 aim_cachecookie(sess, cookie); /* cache da cookie */
329 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
332 * Destination SN (prepended with byte length)
334 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
335 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
337 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
338 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
343 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
344 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032);
347 * Flag data / ICBM Parameters?
349 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
350 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
355 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
360 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
365 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
366 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
367 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
370 * 0003/0004: IP address
373 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
374 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
376 for(i = 0; i < 4; i++)
377 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); /* already in network byte order */
383 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
384 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
385 curbyte += aimutil_put16(newpacket->data+curbyte, port);
388 * 000f/0000: umm.. dunno. Zigamorph[1]?
389 * [1]: see esr's TNHD.
392 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
393 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
395 printf("curbyte: 0x%x\n",curbyte);
397 newpacket->commandlen = curbyte;
400 aim_tx_enqueue(sess, newpacket);
403 * allocate and set up our connection
406 i = fcntl(listenfd, F_GETFL, 0);
407 fcntl(listenfd, F_SETFL, i | O_NONBLOCK);
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,
449 struct aim_conn_t *conn,
450 struct aim_directim_priv *priv )
452 struct aim_conn_t *newconn = NULL;;
454 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, priv->ip);
455 if (!newconn || (newconn->fd == -1)) {
456 printf("could not connect to %s\n", priv->ip);
457 perror("aim_newconn");
458 aim_conn_kill(sess, &newconn);
461 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
462 newconn->priv = priv;
463 printf("faim: connected to peer (fd = %d)\n", newconn->fd);
469 u_long aim_accepttransfer(struct aim_session_t *sess,
470 struct aim_conn_t *conn,
471 struct aim_conn_t *oftconn,
474 unsigned short rendid)
476 struct command_tx_struct *newpacket, *newoft;
477 struct aim_fileheader_t *listingfh;
479 /* now for the oft bits */
481 if(rendid == AIM_CAPS_GETFILE) {
482 printf("jbm: getfile request accept\n");
483 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1108, oftconn, 0))) {
484 printf("faim: accept_transfer: tx_new OFT failed\n");
490 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
491 newoft->hdr.oft.hdr2len = 0xf8; /* 0x100 - 8 */
493 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
498 listingfh = aim_getlisting(sess);
500 memcpy(listingfh->bcookie, cookie, 8);
504 for(i = 0; i < 8; i++)
505 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, cookie[i]);
506 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->encrypt);
507 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->compress);
508 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->totfiles);
509 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->filesleft);
510 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->totparts);
511 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->partsleft);
512 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->totsize);
513 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->size);
514 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->modtime);
515 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->checksum);
516 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->rfrcsum);
517 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->rfsize);
518 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->cretime);
519 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->rfcsum);
520 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->nrecvd);
521 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->recvcsum);
523 memcpy(newoft->hdr.oft.hdr2+curbyte, listingfh->idstring, 32);
526 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, listingfh->flags);
527 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, listingfh->lnameoffset);
528 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, listingfh->lsizeoffset);
530 memcpy(newoft->hdr.oft.hdr2+curbyte, listingfh->dummy, 69);
533 memcpy(newoft->hdr.oft.hdr2+curbyte, listingfh->macfileinfo, 16);
536 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->nencode);
537 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->nlanguage);
539 memcpy(newoft->hdr.oft.hdr2+curbyte, listingfh->name, 64);
545 aim_tx_enqueue(sess, newoft);
546 printf("faim: getfile: OFT listing enqueued.\n");
551 if(!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(sn)+4+2+8+16)))
556 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
557 for (i = 0; i < 8; i++)
558 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
559 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
560 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
561 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
562 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
563 curbyte += aimutil_put16(newpacket->data+curbyte, 0x001a);
564 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept */);
565 for (i = 0; i < 8; i++)
566 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
567 curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid);
570 aim_tx_enqueue(sess, newpacket);
574 return (sess->snac_nextid++);
580 * Get file listing.txt info. where else to put it? i
581 * dunno. client-side issue for sure tho. for now we just side-step
582 * the issue with a nice default. =)
586 struct aim_fileheader_t *aim_getlisting(struct aim_session_t *sess)
588 struct aim_fileheader_t *fh;
590 if(!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t))))
593 fh->encrypt = 0x0000;
594 fh->compress = 0x0000;
595 fh->totfiles = 0x0001;
596 fh->filesleft = 0x0001;
597 fh->totparts = 0x0001;
598 fh->partsleft = 0x0001;
599 fh->totsize = 0x00000064;
600 fh->size = 0x00000024; /* ls -l listing.txt */
601 fh->modtime = (int)time(NULL); /*0x39441fb4; */
602 fh->checksum = 0xb8350000;
603 fh->rfcsum = 0x00000000;
604 fh->rfsize = 0x00000000;
605 fh->cretime = 0x00000000;
606 fh->rfcsum = 0x00000000;
607 fh->nrecvd = 0x00000000;
608 fh->recvcsum = 0x00000000;
610 memset(fh->idstring, 0, 32/*sizeof(fh->idstring)*/);
611 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", 32/*sizeof(fh->idstring)*/);
612 memset(fh->idstring+strlen(fh->idstring), 0, 32-strlen(fh->idstring)); /* jbm hack */
615 fh->lnameoffset = 0x1a;
616 fh->lsizeoffset = 0x10;
618 memset(fh->dummy, 0, 69/*sizeof(fh->dummy)*/);
621 memset(fh->macfileinfo, 0, 16/*sizeof(fh->macfileinfo)*/);
622 /* fh->macfileinfo = ; */
624 fh->nencode = 0x0000;
625 fh->nlanguage = 0x0000;
627 memset(fh->name, 0, 64/*sizeof(fh->name)*/);
628 memcpy(fh->name, "listing.txt", 64 /*sizeof(fh->name)*/);
629 memset(fh->name+strlen(fh->name), 0, 64-strlen(fh->name)); /* jbm hack */
631 printf("jbm: fh name %s / %s\n", fh->name, (fh->name+(strlen(fh->name))));
636 * establish: create a listening socket on a port. you need to call
637 * accept() when it's connected.
638 * portnum is the port number to bind to.
642 int aim_listenestablish(u_short portnum)
646 struct addrinfo hints, *res, *ressave;
648 sprintf(serv, "%d", portnum);
649 memset(&hints, 0, sizeof(struct addrinfo));
650 hints.ai_flags = AI_PASSIVE;
651 hints.ai_family = AF_UNSPEC;
652 hints.ai_socktype = SOCK_STREAM;
653 if (getaddrinfo(NULL/*any IP*/, serv, &hints, &res) != 0) {
654 perror("getaddrinfo");
659 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
662 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
663 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
666 } while ( (res = res->ai_next) );
669 if (listen(listenfd, 1024)!=0) {
673 freeaddrinfo(ressave);
677 int aim_get_command_rendezvous(struct aim_session_t *sess, struct aim_conn_t *conn)
680 /* XXX: NOT THREAD SAFE RIGHT NOW. the locks are acting up. deal. -- jbm */
682 unsigned char hdrbuf1[6];
683 unsigned char *hdr = NULL;
686 rxcallback_t userfunc = NULL;
689 memset(hdrbuf1, 0, sizeof(hdrbuf1));
691 faim_mutex_lock(&conn->active); /* gets locked down for the entirety */
693 if ( (hdrlen = read(conn->fd, hdrbuf1, 6)) < 6) {
696 printf("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);
697 faim_mutex_unlock(&conn->active);
698 aim_conn_close(conn);
702 hdrlen = aimutil_get16(hdrbuf1+4);
705 if (!(hdr = malloc(hdrlen)))
708 if (read(conn->fd, hdr, hdrlen) < hdrlen) {
710 printf("faim: rend: read2 error\n");
712 faim_mutex_unlock(&conn->active);
713 aim_conn_close(conn);
714 return 0; /* see comment on previous read check */
717 hdrtype = aimutil_get16(hdr);
720 case 0x0001: { /* directim */
721 int payloadlength = 0;
723 struct aim_directim_priv *priv;
726 priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv));
728 payloadlength = aimutil_get32(hdr+22);
729 flags = aimutil_get16(hdr+32);
732 strncpy(priv->sn, snptr, MAXSNLEN);
735 printf("faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, snptr);
738 if (flags == 0x000e) {
739 faim_mutex_unlock(&conn->active);
740 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)) )
741 return userfunc(sess, NULL, snptr);
742 } else if ((flags == 0x0000) && payloadlength) {
745 if(! (msg = calloc(1, payloadlength+1)) ) {
746 faim_mutex_unlock(&conn->active);
750 if (recv(conn->fd, msg, payloadlength, MSG_WAITALL) < payloadlength) {
752 printf("faim: rend: read3 error\n");
754 faim_mutex_unlock(&conn->active);
755 aim_conn_close(conn);
758 faim_mutex_unlock(&conn->active);
759 msg[payloadlength] = '\0';
761 printf("faim: directim: %s/%04x/%04x/%s\n", snptr, payloadlength, flags, msg);
764 if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) )
765 i = userfunc(sess, NULL, conn, snptr, msg);
772 case 0x1209: { /* get file first */
773 struct aim_filetransfer_priv *ft;
774 struct aim_fileheader_t *fh;
775 struct aim_msgcookie_t *cook;
780 printf("faim: rend: fileget 0x1209\n");
783 printf("faim: fileget_command(1209): um. hdrlen != 0x100.. 0x%x\n", hdrlen);
785 if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) {
786 printf("faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n");
787 faim_mutex_unlock(&conn->active);
791 fh = aim_oft_getfh(hdr);
793 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
795 cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET);
798 free(cook->data); /* XXX */
802 aim_cachecookie(sess, cook);
806 data = calloc(1, commandlen);
807 memcpy(data, "01/01/1999 00:00 100 file.txt\r\n", commandlen);
809 if (write(conn->fd, data, commandlen) != commandlen) {
810 perror("listing write error");
812 faim_mutex_unlock(&conn->active);
814 printf("jbm: hit end of 1209\n");
818 case 0x120b: { /* get file second */
819 struct aim_filetransfer_priv *ft;
820 struct aim_msgcookie_t *cook;
822 struct aim_fileheader_t *fh;
824 printf("faim: rend: fileget 120b\n");
826 if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) {
827 printf("faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n");
828 faim_mutex_unlock(&conn->active);
833 printf("faim: fileget_command(120b): um. hdrlen != 0x100..\n");
835 fh = aim_oft_getfh(hdr);
837 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
839 cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET);
842 free(cook->data); /* XXX: integrate cookie caching */
846 aim_cachecookie(sess, cook);
848 faim_mutex_unlock(&conn->active);
852 case 0x120c: { /* yet more get file */
853 struct aim_filetransfer_priv *ft;
854 struct aim_msgcookie_t *cook;
855 struct aim_fileheader_t *listingfh;
856 struct command_tx_struct *newoft;
859 printf("faim: rend: fileget 120c\n");
861 if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) {
862 printf("faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n");
863 faim_mutex_unlock(&conn->active);
868 printf("faim: fileget_command(120c): um. hdrlen != 0x100..\n");
870 listingfh = aim_oft_getfh((char *)hdr);
872 memcpy(&(ft->fh), listingfh, sizeof(struct aim_fileheader_t));
874 cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET);
877 free(cook->data); /* XXX */
881 aim_cachecookie(sess, cook);
883 faim_mutex_unlock(&conn->active);
885 printf("faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name);
887 if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0101, conn, 0/*listingfh->size*/))) {
888 printf("faim: send_final_transfer: tx_new OFT failed\n");
892 /* XXX: actually implement Real Handling of all this */
894 printf("jbm: listingfh->size: 0x%lx\n", listingfh->size);
898 /* if(!(newoft->data = calloc(1, listingfh->size))) {
899 printf("newoft data malloc failed. bombing.\n");
903 if(newoft->commandlen > 0) {
905 bzero(newoft->data, newoft->commandlen);
906 for(i = 0; i < newoft->commandlen; i++)
907 newoft->data[i] = 0x30 + (i%10);
909 // memcpy(newoft->data, "This has been a Test\r\n-josh\r\n", newoft->commandlen);
912 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
913 newoft->hdr.oft.hdr2len = 0xf8; /* 0x100 - 8 */
915 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
917 free(newoft->data); /* XXX: make this into a destructor function */
922 memcpy(listingfh->bcookie, ft->fh.bcookie, 8);
926 for(i = 0; i < 8; i++)
927 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, listingfh->bcookie[i]);
928 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->encrypt);
929 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->compress);
930 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->totfiles);
931 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->filesleft);
932 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->totparts);
933 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->partsleft);
934 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->totsize);
935 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->size);
936 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->modtime);
937 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->checksum);
938 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->rfrcsum);
939 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->rfsize);
940 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->cretime);
941 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, listingfh->rfcsum);
942 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, 0 /*listingfh->nrecvd*/);
943 curbyte += aimutil_put32(newoft->hdr.oft.hdr2+curbyte, 0/*listingfh->recvcsum*/);
945 strncpy(newoft->hdr.oft.hdr2+curbyte, listingfh->idstring, 32);
948 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, 0x20 /*listingfh->flags */);
949 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, listingfh->lnameoffset);
950 curbyte += aimutil_put8(newoft->hdr.oft.hdr2+curbyte, listingfh->lsizeoffset);
952 memcpy(newoft->hdr.oft.hdr2+curbyte, listingfh->dummy, 69);
955 memcpy(newoft->hdr.oft.hdr2+curbyte, listingfh->macfileinfo, 16);
958 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->nencode);
959 curbyte += aimutil_put16(newoft->hdr.oft.hdr2+curbyte, listingfh->nlanguage);
961 strncpy(newoft->hdr.oft.hdr2+curbyte, listingfh->name, 64);
967 aim_tx_enqueue(sess, newoft);
968 printf("jbm: OFT listing enqueued.\n");
972 case 0x0202: { /* get file: ready to recieve data */
976 struct aim_fileheader_t *fh;
977 fh = aim_oft_getfh((char *)hdr);
979 c = (char *)calloc(1, fh->size);
981 printf("looks like we're ready to send data.(oft 0x0202)\n");
985 for(i = 0; i < fh->size; i++)
986 c[i] = 0x30 + (i%10);
988 if ( (i = write(conn->fd, c, fh->size)) != fh->size ) {
989 printf("whoopsy, didn't write it all...\n");
992 faim_mutex_unlock(&conn->active);
996 case 0x0204: { /* get file: finished. close it up */
997 printf("looks like we're done with a transfer (oft 0x0204)\n");
998 faim_mutex_unlock(&conn->active);
999 aim_conn_close(conn);
1003 printf("OFT frame: type %04x\n", hdrtype);
1004 /* data connection may be unreliable here */
1005 faim_mutex_unlock(&conn->active);
1016 * this currently feeds totally bogus data
1019 struct aim_fileheader_t *aim_oft_getfh(char *hdr)
1021 struct aim_fileheader_t *fh;
1024 if(!(fh = calloc(1, sizeof(struct aim_fileheader_t))))
1027 /* [0] and [1] are the type. we can ignore those here. */
1031 for(j = 0; j < 8; j++, i++)
1032 fh->bcookie[j] = hdr[i];
1033 fh->encrypt = aimutil_get16(hdr+i);
1035 fh->compress = aimutil_get16(hdr+i);
1037 fh->totfiles = aimutil_get16(hdr+i);
1039 fh->filesleft = aimutil_get16(hdr+i);
1041 fh->totparts = aimutil_get16(hdr+i);
1043 fh->partsleft = aimutil_get16(hdr+i);
1045 fh->totsize = aimutil_get32(hdr+i);
1047 fh->size = aimutil_get32(hdr+i);
1049 fh->modtime = aimutil_get32(hdr+i);
1051 fh->checksum = aimutil_get32(hdr+i);
1053 fh->rfrcsum = aimutil_get32(hdr+i);
1055 fh->rfsize = aimutil_get32(hdr+i);
1057 fh->cretime = aimutil_get32(hdr+i);
1059 fh->rfcsum = aimutil_get32(hdr+i);
1061 fh->nrecvd = aimutil_get32(hdr+i);
1063 fh->recvcsum = aimutil_get32(hdr+i);
1066 memcpy(fh->idstring, hdr+i, 32);
1069 fh->flags = aimutil_get8(hdr+i);
1071 fh->lnameoffset = aimutil_get8(hdr+i);
1073 fh->lsizeoffset = aimutil_get8(hdr+i);
1076 memcpy(fh->dummy, hdr+i, 69);
1079 memcpy(fh->macfileinfo, hdr+i, 16);
1082 fh->nencode = aimutil_get16(hdr+i);
1084 fh->nlanguage = aimutil_get16(hdr+i);
1087 memcpy(fh->name, hdr+i, 64);