From: mid Date: Thu, 8 Feb 2001 05:57:32 +0000 (+0000) Subject: - Thu Feb 8 02:31:25 UTC 2001 X-Git-Tag: rel_0_99_2~77 X-Git-Url: http://andersk.mit.edu/gitweb/libfaim.git/commitdiff_plain/37ee990eab0e60fadbc1555d3f4a7e85d2026297 - Thu Feb 8 02:31:25 UTC 2001 - Remove aim_purgecookies() -- never used anyway - Clean up a few bits of aim_msgcookie.c - Remove AIM_CONN_MAX constant (hasn't been relevent in ages) - Change commentary on synchro in aim.h - Add NOPLOCKS and make them default - Make faim/aim.h more of a mess; try to keep interfaces hiddenish. - Add #define FAIM_INTERNAL to top of every file - Remove aim_countconn (no one uses it anyway) - Add aim_cloneconn() (cringe.) - Throw in josh's getfile patch (and then munge it enough to break it) - Add getopt() to faimtest. - Remove aim_buildcode.h/mkbuildinfo.sh crap. - Clean up aim_cleansnacs() - There may have been other stuff too. As always, the diff is the most accurate changelog there is. - *** FT is currently more broken than it was before. Also, this patch introduces even more memory leaks. You've been warned. (about 6300loc in all.) --- diff --git a/CHANGES b/CHANGES index 79d5983..efda289 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,25 @@ No release numbers ------------------ + - Thu Feb 8 02:31:25 UTC 2001 + - Remove aim_purgecookies() -- never used anyway + - Clean up a few bits of aim_msgcookie.c + - Remove AIM_CONN_MAX constant (hasn't been relevent in ages) + - Change commentary on synchro in aim.h + - Add NOPLOCKS and make them default + - Make faim/aim.h more of a mess; try to keep interfaces hiddenish. + - Add #define FAIM_INTERNAL to top of every file + - Remove aim_countconn (no one uses it anyway) + - Add aim_cloneconn() (cringe.) + - Throw in josh's getfile patch (and then munge it enough to break it) + - Add getopt() to faimtest. + - Remove aim_buildcode.h/mkbuildinfo.sh crap. + - Clean up aim_cleansnacs() + - There may have been other stuff too. As always, the diff is the + most accurate changelog there is. + - *** FT is currently more broken than it was before. Also, this patch + introduces even more memory leaks. You've been warned. + - Sun Dec 17 07:19:04 UTC 2000 - Update the capability block list diff --git a/Makefile b/Makefile index 5a65b66..8e0dd89 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ include Makefile.rules +CFLAGS += -DAIM_BUILDDATE=\"`date +%Y%m%d`\" -DAIM_BUILDTIME=\"`date +%H%M%S`\" SONAME = libfaim.so SOFILENAME = libfaim.so.0.90 # used for installation @@ -30,10 +31,7 @@ LIBFAIM_OBJECTS = \ all: libfaim allutils -mkbuildinfo: - sh $$PWD/mkbuildinfo.sh - -libfaim: mkbuildinfo $(LIBFAIM_OBJECTS) +libfaim: $(LIBFAIM_OBJECTS) $(AR) cru libfaim.a $(LIBFAIM_OBJECTS) $(RANLIB) libfaim.a ifdef SOLARIS diff --git a/aim_ads.c b/aim_ads.c index f07cc66..dc82715 100644 --- a/aim_ads.c +++ b/aim_ads.c @@ -3,6 +3,7 @@ * */ +#define FAIM_INTERNAL #include faim_export unsigned long aim_ads_clientready(struct aim_session_t *sess, diff --git a/aim_auth.c b/aim_auth.c index d110451..47f4129 100644 --- a/aim_auth.c +++ b/aim_auth.c @@ -5,6 +5,7 @@ */ +#define FAIM_INTERNAL #include /* this just pushes the passed cookie onto the passed connection -- NO SNAC! */ diff --git a/aim_buddylist.c b/aim_buddylist.c index a9b9ef8..ecd5af1 100644 --- a/aim_buddylist.c +++ b/aim_buddylist.c @@ -1,4 +1,5 @@ +#define FAIM_INTERNAL #include /* @@ -6,6 +7,8 @@ * * Adds a single buddy to your buddy list after login. * + * XXX this should just be an extension of setbuddylist() + * */ faim_export unsigned long aim_add_buddy(struct aim_session_t *sess, struct aim_conn_t *conn, @@ -33,6 +36,11 @@ faim_export unsigned long aim_add_buddy(struct aim_session_t *sess, return sess->snac_nextid; } +/* + * XXX generalise to support removing multiple buddies (basically, its + * the same as setbuddylist() but with a different snac subtype). + * + */ faim_export unsigned long aim_remove_buddy(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn ) diff --git a/aim_chat.c b/aim_chat.c index 5af5728..cf57449 100644 --- a/aim_chat.c +++ b/aim_chat.c @@ -5,6 +5,7 @@ * */ +#define FAIM_INTERNAL #include faim_export char *aim_chat_getname(struct aim_conn_t *conn) @@ -57,6 +58,7 @@ faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess, int curbyte,i; struct command_tx_struct *newpacket; + struct aim_msgcookie_t *cookie; if (!sess || !conn || !msg) return 0; @@ -76,7 +78,10 @@ faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess, for (i=0;i<8;i++) curbyte += aimutil_put8(newpacket->data+curbyte, (u_char) rand()); - aim_cachecookie(sess, aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL)); + cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL); + cookie->data = strdup(conn->priv); /* chat hack dependent */ + + aim_cachecookie(sess, cookie); /* * metaTLV start. -- i assume this is a metaTLV. it could be the @@ -554,6 +559,8 @@ faim_export unsigned long aim_chat_invite(struct aim_session_t *sess, { struct command_tx_struct *newpacket; int i,curbyte=0; + struct aim_msgcookie_t *cookie; + struct aim_invite_priv *priv; if (!sess || !conn || !sn || !msg || !roomname) return -1; @@ -575,7 +582,16 @@ faim_export unsigned long aim_chat_invite(struct aim_session_t *sess, curbyte += aimutil_put8(newpacket->data+curbyte, (u_char)rand()); /* XXX this should get uncached by the unwritten 'invite accept' handler */ - aim_cachecookie(sess, aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_CHAT, NULL)); + if(!(priv = calloc(sizeof(struct aim_invite_priv), 1))) + return -1; + priv->sn = strdup(sn); + priv->roomname = strdup(roomname); + priv->exchange = exchange; + priv->instance = instance; + + if(!(cookie = aim_mkcookie(newpacket->data+curbyte-8, AIM_COOKIETYPE_INVITE, priv))) + return -1; + aim_cachecookie(sess, cookie); /* * Channel (2) diff --git a/aim_chatnav.c b/aim_chatnav.c index 6d4ad42..92f9db8 100644 --- a/aim_chatnav.c +++ b/aim_chatnav.c @@ -1,13 +1,15 @@ /* + * Handle ChatNav. * - * - * + * [The ChatNav(igation) service does various things to keep chat + * alive. It provides room information, room searching and creating, + * as well as giving users the right ("permission") to use chat.] * */ +#define FAIM_INTERNAL #include - /* * conn must be a chatnav connection! */ diff --git a/aim_conn.c b/aim_conn.c index 836c0d4..944708e 100644 --- a/aim_conn.c +++ b/aim_conn.c @@ -6,6 +6,7 @@ * */ +#define FAIM_INTERNAL #include #ifndef _WIN32 @@ -176,8 +177,8 @@ faim_export void aim_conn_close(struct aim_conn_t *deadconn) * type found. * */ -faim_internal struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess, - int type) +faim_export struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess, + int type) { struct aim_conn_t *cur; @@ -358,6 +359,37 @@ static int aim_proxyconnect(struct aim_session_t *sess, return fd; } +faim_internal struct aim_conn_t *aim_cloneconn(struct aim_session_t *sess, + struct aim_conn_t *src) +{ + struct aim_conn_t *conn; + struct aim_rxcblist_t *cur; + + if (!(conn = aim_conn_getnext(sess))) + return NULL; + + + faim_mutex_lock(&conn->active); + + conn->fd = src->fd; + conn->type = src->type; + conn->subtype = src->subtype; + conn->seqnum = src->seqnum; + conn->priv = src->priv; + conn->lastactivity = src->lastactivity; + conn->forcedlatency = src->forcedlatency; + + /* clone handler list */ + for (cur = src->handlerlist; cur; cur = cur->next) { + aim_conn_addhandler(sess, conn, cur->family, cur->type, + cur->handler, cur->flags); + } + + faim_mutex_unlock(&conn->active); + + return conn; +} + /** * aim_newconn - Open a new connection * @sess: Session to create connection in @@ -453,26 +485,6 @@ faim_export int aim_conngetmaxfd(struct aim_session_t *sess) return j; } -/** - * aim_countconn - Return the number of open connections in the session - * @sess: Session to look at - * - * Returns the number of number connections in @sess. - * - */ -static int aim_countconn(struct aim_session_t *sess) -{ - int cnt = 0; - struct aim_conn_t *cur; - - faim_mutex_lock(&sess->connlistlock); - for (cur = sess->connlist; cur; cur = cur->next) - cnt++; - faim_mutex_unlock(&sess->connlistlock); - - return cnt; -} - /** * aim_conn_in_sess - Predicate to test the precense of a connection in a sess * @sess: Session to look in diff --git a/aim_ft.c b/aim_ft.c index 84ce4df..3db49c3 100644 --- a/aim_ft.c +++ b/aim_ft.c @@ -1,3 +1,9 @@ +/* + * File transfer (OFT) and DirectIM. + */ + +#define FAIM_INTERNAL_FT +#define FAIM_INTERNAL #include #ifndef _WIN32 @@ -5,620 +11,599 @@ #include #include #include /* for aim_directim_initiate */ + #include /* for inet_ntoa */ + #endif -/* aim_msgcookies.c is mostly new. just look at the diff and replace yours, easiest. */ - -/* - function name where i had it - aim_send_im_direct aim_im.c - aim_directim_initiate aim_im.c - aim_filetransfer_accept aim_im.c - aim_getlisting aim_misc.c (?!) -- prototype function. can be ignored. - establish aim_misc.c - aim_get_command_rendezvous aim_r - oft_getfh aim_rxqueue.c +/* TODO: + o fix misspelling of receive + o double-check mutex states on error returns + o look for memory leaks.. there's going to be shitloads, i'm sure. + o change listing FILE to listing filesend callback + o change send_file and send_chunk over to buffers. */ +static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr); + +/** + * aim_handlerendconnect - call this to accept OFT connections and set up the requisite structures + * @sess: the session + * @cur: the conn the incoming connection is on + * + * call this when you get an outstanding read on a conn with subtype + * AIM_CONN_SUBTYPE_RENDEZVOUS_OUT, it will close the current fd and + * call a callback with the new, accept'd fd to the other person. then we + * get to start the fun that is OFT! + */ faim_export int aim_handlerendconnect(struct aim_session_t *sess, struct aim_conn_t *cur) -{ +{ int acceptfd = 0; rxcallback_t userfunc; struct sockaddr cliaddr; socklen_t clilen = sizeof(cliaddr); int ret = 0; + struct aim_conn_t *newconn; - /* - * Listener sockets only have incoming connections. No data. - */ if( (acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1) return -1; + if (cliaddr.sa_family != AF_INET) { /* just in case IPv6 really is happening */ + close(acceptfd); + aim_conn_close(cur); + return -1; + } - if (cliaddr.sa_family != AF_INET) /* just in case IPv6 really is happening */ + /* clone it and then close the original */ + /* XXX safe? maybe cur->priv should be NULLed after this */ + if (!(newconn = aim_cloneconn(sess, cur))) { + close(acceptfd); + aim_conn_close(cur); return -1; + } + aim_conn_close(cur); + + newconn->type = AIM_CONN_TYPE_RENDEZVOUS; + newconn->fd = acceptfd; - switch(cur->subtype) { - case AIM_CONN_SUBTYPE_OFT_DIRECTIM: { + switch(newconn->subtype) { + case AIM_CONN_SUBTYPE_OFT_DIRECTIM: { struct aim_directim_priv *priv; - - priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); - snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port)); + if(!newconn->priv) + newconn->priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); + priv = (struct aim_directim_priv *)newconn->priv; - if(!cur->priv) - cur->priv = priv; /* what happens if there is one?! -- mid */ + snprintf(priv->ip, sizeof(priv->ip), "%s:%u", + inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), + ntohs(((struct sockaddr_in *)&cliaddr)->sin_port)); - cur->type = AIM_CONN_TYPE_RENDEZVOUS; - close(cur->fd); /* should we really do this? seems like the client should decide. maybe clone the connection and keep the listener open. -- mid */ - cur->fd = acceptfd; + /* should we really do this? seems like the client should decide. maybe clone the connection and keep the listener open. -- mid */ + if ( (userfunc = aim_callhandler(newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE))) + ret = userfunc(sess, NULL, newconn); - if ( (userfunc = aim_callhandler(cur, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE))) - ret = userfunc(sess, NULL, cur); - break; - } - case AIM_CONN_SUBTYPE_OFT_GETFILE: { + } + case AIM_CONN_SUBTYPE_OFT_GETFILE: { struct aim_filetransfer_priv *priv; - priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)); + if(!newconn->priv) + newconn->priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)); + priv = (struct aim_filetransfer_priv *)newconn->priv; snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port)); - if(!cur->priv) - cur->priv = priv; + if ( (userfunc = aim_callhandler(newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE))) + ret = userfunc(sess, NULL, newconn); - if ( (userfunc = aim_callhandler(cur, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE))) - ret = userfunc(sess, NULL, cur); break; } - default: { - /* XXX */ + default: { + faimdprintf(1,"Got a Connection on a listener that's not Rendezvous(??!) Killing conn.\n"); + aim_conn_close(newconn); + break; } } + return ret; } - - + /* - * aim_send_im_direct: - * sess - session - * conn - directim connection - * msg - null-terminated string to send + * aim_send_im_direct - send IM client-to-client over established connection + * @sess: session to conn + * @conn: directim connection + * @msg: null-terminated string to send; if this is NULL, it will send a "typing" notice. + * */ +faim_export int aim_send_im_direct(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg) +{ -faim_export int aim_send_im_direct(struct aim_session_t *sess, - struct aim_conn_t *conn, - char *msg) -{ - struct command_tx_struct *newpacket , *newpacket2; - - /* newpacket contains a real header with data, newpacket2 is just a - null packet, with a cookie and a lot of 0x00s. newpacket is the - "i'm sending", newpacket2 is the "i'm typing".*/ - - /* uhm. the client should send those as two seperate things -- mid */ - + struct command_tx_struct *newpacket; struct aim_directim_priv *priv = NULL; int i; - - if (!sess || !conn || !(conn->type) || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !conn->priv || !msg) { - printf("faim: directim: invalid arguments\n"); - return -1; - }; - - if (strlen(msg) >= MAXMSGLEN) + if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !conn->priv) { + faimdprintf(2,"faim: directim: invalid arguments\n"); return -1; + } priv = (struct aim_directim_priv *)conn->priv; - /* NULLish Header */ - - if (!(newpacket2 = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, 0))) { - printf("faim: directim: tx_new2 failed\n"); - return -1; - } - - newpacket2->lock = 1; /* lock struct */ - - memcpy(newpacket2->hdr.oft.magic, "ODC2", 4); - newpacket2->hdr.oft.hdr2len = 0x44; - - if (!(newpacket2->hdr.oft.hdr2 = calloc(1,newpacket2->hdr.oft.hdr2len))) { - free(newpacket2); + if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, strlen(msg)))) { + faimdprintf(2,"faim: directim: tx_new failed\n"); return -1; - } - - i = 0; - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0006); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - - i += aimutil_putstr(newpacket2->hdr.oft.hdr2+i, (char *)priv->cookie, 8); - - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - - i += aimutil_put32(newpacket2->hdr.oft.hdr2+i, 0x00000000); - - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x000e); - - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - - i += aimutil_putstr(newpacket2->hdr.oft.hdr2+i, sess->sn, strlen(sess->sn)); - - i = 52; /* 0x34 */ - i += aimutil_put8(newpacket2->hdr.oft.hdr2+i, 0x00); /* 53 */ - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); /* 55 */ - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* 61 */ - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* 65 */ - i += aimutil_put16(newpacket2->hdr.oft.hdr2+i, 0x0000);/* end of hdr2 */ - - newpacket2->lock = 0; - newpacket2->data = NULL; - - aim_tx_enqueue(sess, newpacket2); - - /* Header packet */ + } - if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0001, conn, strlen(msg)))) { - printf("faim: directim: tx_new failed\n"); - return -1; - } + newpacket->lock = 1; - newpacket->lock = 1; /* lock struct */ + /* if msg is non-null, we'resending an IM, else a "typing" notice */ + if(msg) { + if (strlen(msg) >= MAXMSGLEN) + return -1; + newpacket->hdr.oft.hdr2len = 0x54; + if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) { + newpacket->lock = 0; + aim_tx_destroy(newpacket); + return -1; + } + } else { + newpacket->hdr.oft.hdr2len = 0x44; + if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) { + newpacket->lock = 0; + aim_tx_destroy(newpacket); + return -1; + } + } memcpy(newpacket->hdr.oft.magic, "ODC2", 4); - newpacket->hdr.oft.hdr2len = 0x54; - - if (!(newpacket->hdr.oft.hdr2 = calloc(1,newpacket->hdr.oft.hdr2len))) { - free(newpacket); - return -1; - } + newpacket->data = NULL; i = 0; i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0006); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); - i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, (char *)priv->cookie, 8); - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); + i += aimutil_put32(newpacket->hdr.oft.hdr2+i, 0x00000000); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put32(newpacket->hdr.oft.hdr2+i, strlen(msg)); + /* flags -- 0x000e for "typing", 0x0000 for message */ + if(msg) + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); + else + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000e); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); + i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, sess->sn, strlen(sess->sn)); + i = 52; + + i += aimutil_put8(newpacket->hdr.oft.hdr2+i, 0x00); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); - - i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, sess->sn, strlen(sess->sn)); - - i = 52; /* 0x34 */ - i += aimutil_put8(newpacket->hdr.oft.hdr2+i, 0x00); /* 53 */ - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); /* 55 */ i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 61 */ i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 65 */ - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* end of hdr2 */ - - /* values grabbed from a dump */ - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008); /* 69 */ - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c); - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);/* 71 */ - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466);/* 73 */ - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001);/* 73 */ - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f); - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e); - i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8); - - memcpy(newpacket->data, msg, strlen(msg)); + /* end of hdr2 */ + + if(msg) { + /* values grabbed from a dump */ + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e); + i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8); + memcpy(newpacket->data, msg, strlen(msg)); + } newpacket->lock = 0; - aim_tx_enqueue(sess, newpacket); - return 0; -} +} /* - * aim_directim_intitiate: - * For those times when we want to open up the directim channel ourselves. - * sess is your session, - * conn is the BOS conn, - * priv is a dummy priv value (we'll let it get filled in later) (if - * you pass a NULL, we alloc one) - * destsn is the SN to connect to. + * aim_directim_intitiate - For those times when we want to open up the directim channel ourselves. + * @sess: your session, + * @conn: the BOS conn, + * @priv: a dummy priv value (we'll let it get filled in later) (if you pass a NULL, we alloc one) + * @destsn: the SN to connect to. + * */ - - -faim_export struct aim_conn_t *aim_directim_initiate(struct aim_session_t *sess, - struct aim_conn_t *conn, - struct aim_directim_priv *priv, +faim_export struct aim_conn_t *aim_directim_initiate(struct aim_session_t *sess, + struct aim_conn_t *conn, + struct aim_directim_priv *priv, char *destsn) -{ +{ + struct command_tx_struct *newpacket; struct aim_conn_t *newconn; - struct aim_msgcookie_t *cookie; - int curbyte, i, listenfd; short port = 4443; - struct hostent *hptr; char localhost[129]; - unsigned char cap[16]; - char d[4]; /* XXX: IPv6. *cough* */ + char d[4]; /* IPv6 is a bit bigger... */ - /* - * Open our socket - */ + /* XXX: TLVlist-ize this */ + + /* Open our socket */ if( (listenfd = aim_listenestablish(port)) == -1) return NULL; - /* - * get our local IP - */ - + /* get our local IP */ + /* XXX if available, use getaddrinfo() */ + /* XXX allow client to specify which IP to use for multihomed boxes */ if(gethostname(localhost, 128) < 0) return NULL; - if( (hptr = gethostbyname(localhost)) == NULL) return NULL; + memcpy(&d, hptr->h_addr_list[0], 4); - memcpy(&d, hptr->h_addr_list[0], 4); /* XXX: this probably isn't quite kosher, but it works */ - + /* XXX: this probably isn't quite kosher, but it works */ aim_putcap(cap, 16, AIM_CAPS_IMIMAGE); - /* - * create the OSCAR packet - */ + /* create the OSCAR packet */ if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x32))) return NULL; + newpacket->lock = 1; - newpacket->lock = 1; /* lock struct */ + curbyte = 0; + curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid); - curbyte = 0; - curbyte += aim_putsnac(newpacket->data+curbyte, - 0x0004, 0x0006, 0x0000, sess->snac_nextid); - - /* - * Generate a random message cookie - * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. - */ - for (i=0;i<7;i++) + /* Generate a random message cookie */ + /* This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */ + for (i=0; i<7; i++) curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) rand() % 20)); + curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); - /* - * grab all the data for cookie caching. - */ + /* grab all the data for cookie caching */ cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)); - memcpy(cookie->cookie, newpacket->data+curbyte-8, 8); cookie->type = AIM_COOKIETYPE_OFTIM; - + priv = cookie->data; + if(!priv) priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); - + memcpy(priv->cookie, cookie, 8); memcpy(priv->sn, destsn, sizeof(priv->sn)); - cookie->data = priv; + aim_cachecookie(sess, cookie); - aim_cachecookie(sess, cookie); /* cache da cookie */ - - /* - * Channel ID - */ + /* Channel ID */ curbyte += aimutil_put16(newpacket->data+curbyte,0x0002); - /* - * Destination SN (prepended with byte length) - */ + /* Destination SN (prepended with byte length)*/ curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn)); curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn)); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); - /* - * enTLV start - */ + /* enTLV start */ curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032); - /* - * Flag data / ICBM Parameters? - */ + /* Flag data / ICBM Parameters */ curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); - /* - * Cookie - */ + /* Cookie */ curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8); - /* - * Capability String - */ + /*Capability String */ curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10); - /* - * 000a/0002 : 0001 - */ + /* 000a/0002 : 0001 */ curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001); - /* - * 0003/0004: IP address - */ - + /* 0003/0004: IP address */ curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004); - - for(i = 0; i < 4; i++) - curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); /* already in network byte order */ - - /* - * 0005/0002: Port - */ - + for(i = 0;i < 4; i++) + curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); + + /* 0005/0002: Port */ curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); curbyte += aimutil_put16(newpacket->data+curbyte, port); - /* - * 000f/0000: umm.. dunno. Zigamorph[1]? - * [1]: see esr's TNHD. - */ - + /* 000f/0000: ?? */ curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); - - printf("curbyte: 0x%x\n",curbyte); - newpacket->commandlen = curbyte; newpacket->lock = 0; - aim_tx_enqueue(sess, newpacket); - /* - * allocate and set up our connection - */ - newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL); - if (!newconn) { - perror("aim_newconn"); - aim_conn_kill(sess, &newconn); + /* XXX switch to aim_cloneconn()? */ + if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL))) return NULL; - } newconn->fd = listenfd; newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; newconn->priv = priv; - printf("faim: listening (fd = %d, unconnected)\n", newconn->fd); + newconn->lastactivity = time(NULL); - /* - * XXX We need some way of closing the listener socket after - * n seconds of no connection. -- mid - */ + faimdprintf(2,"faim: listening (fd = %d, unconnected)\n", newconn->fd); - return (newconn); + return newconn; } /* - * struct aim_conn_t *aim_directim_connect(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_directim_priv *priv) - * sess is the session to append the conn to, - * conn is the BOS connection, - * priv is the filled-in priv data structure for the connection - * returns conn if connected, NULL on error + * unsigned long aim_oft_listener_clean - close up old listeners + * @sess: session to clean up in + * @age: maximum age in seconds + * + * returns number closed, -1 on error. */ +faim_export unsigned int aim_oft_listener_clean(struct aim_session_t *sess, time_t age) +{ + struct aim_conn_t *cur; + time_t now; + unsigned int hit = 0; + + if(!sess) + return -1; + now = time(NULL); + faim_mutex_lock(&sess->connlistlock); + for(cur = sess->connlist;cur; cur = cur->next) + if(cur->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) { + faim_mutex_lock(&cur->active); + if (cur->lastactivity < (now - age) ) { + faim_mutex_unlock(&cur->active); + aim_conn_close(cur); + hit++; + } else + faim_mutex_unlock(&cur->active); + } + faim_mutex_unlock(&sess->connlistlock); + return hit; +} +/* + * aim_directim_connect - connect to buddy for directim + * @sess: the session to append the conn to, + * @conn: the BOS connection, + * @priv: the filled-in priv data structure for the connection + * + * returns conn if connected, %NULL on error + */ +faim_export struct aim_conn_t *aim_directim_connect(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_directim_priv *priv) +{ + struct aim_conn_t *newconn = NULL; -faim_export struct aim_conn_t *aim_directim_connect(struct aim_session_t *sess, - struct aim_conn_t *conn, - struct aim_directim_priv *priv ) -{ - struct aim_conn_t *newconn = NULL;; - + if(!sess || !conn || !priv) + return NULL; + + /* XXX verify that non-blocking connects actually work */ newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, priv->ip); if (!newconn || (newconn->fd == -1)) { - printf("could not connect to %s\n", priv->ip); + faimdprintf(2, "could not connect to %s\n", priv->ip); perror("aim_newconn"); - aim_conn_kill(sess, &newconn); + /* Its safe to call conn_kill here since we created the connection */ + if (newconn) + aim_conn_kill(sess, &newconn); return NULL; - } else { - newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; - newconn->priv = priv; - printf("faim: connected to peer (fd = %d)\n", newconn->fd); - return newconn; } + + newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; + newconn->priv = priv; + faimdprintf(2, "faim: connected to peer (fd = %d)\n", newconn->fd); + return newconn; -} +} /* - * struct aim_conn_t *aim_directim_getconn(struct aim_session_t *sess, const char *name) - * sess is your session, - * name is the name to get, - * returns conn for directim with name, NULL if none found. + * aim_directim_getconn - find a directim conn for buddy name + * @sess: your session, + * @name: the name to get, + * + * returns conn for directim with name, %NULL if none found. + * */ - faim_export struct aim_conn_t *aim_directim_getconn(struct aim_session_t *sess, const char *name) { struct aim_conn_t *cur; struct aim_directim_priv *priv; - + + if(!sess || !name) + return NULL; + faim_mutex_lock(&sess->connlistlock); + for (cur = sess->connlist; cur; cur = cur->next) { if (cur->type != AIM_CONN_TYPE_RENDEZVOUS || cur->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM) continue; - priv = cur->priv; if (aim_sncmp(priv->sn, name) == 0) break; - } - faim_mutex_unlock(&sess->connlistlock); - + } faim_mutex_unlock(&sess->connlistlock); return cur; -} +} /* - * aim_accepttransfer: - * accept a file transfer request. - * sess is the session, - * conn is the BOS conn for the CAP reply - * sn is the screenname to send it to, - * cookie is the cookie used - * ip is the ip to connect to - * file is the listing file to use - * rendid is CAP type + * aim_accepttransfer - accept a file transfer request + * @sess: the session, + * @conn: the BOS conn for the CAP reply + * @sn: the screenname to send it to, + * @cookie: the cookie used + * @ip: the ip to connect to + * @listingfiles: number of files to share + * @listingtotsize: total size of shared files + * @listingsize: length of the listing file(buffer) + * @listingchecksum: checksum of the listing + * @rendid: capability type (%AIM_CAPS_GETFILE or %AIM_CAPS_SENDFILE) * - * returns connection + * returns connection or %NULL on error. */ - -faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess, - struct aim_conn_t *conn, - char *sn, - char *cookie, - char *ip, - FILE *file, - unsigned short rendid) -{ +faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess, + struct aim_conn_t *conn, + char *sn, char *cookie, + char *ip, + unsigned short listingfiles, + unsigned short listingtotsize, + unsigned short listingsize, + unsigned int listingchecksum, + unsigned short rendid) +{ struct command_tx_struct *newpacket, *newoft; - struct aim_conn_t *newconn; - struct aim_fileheader_t *listingfh; + struct aim_fileheader_t *fh; struct aim_filetransfer_priv *priv; struct aim_msgcookie_t *cachedcook; - int curbyte, i; - if(rendid == AIM_CAPS_GETFILE) { + if(!sess || !conn || !sn || !cookie || !ip) + return NULL; + if(rendid == AIM_CAPS_GETFILE) { newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, ip); - newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE; - + if (!newconn || (newconn->fd == -1)) { perror("aim_newconn"); faimdprintf(2, "could not connect to %s (fd: %i)\n", ip, newconn?newconn->fd:0); - aim_conn_kill(sess, &newconn); + /* Its safe to call conn_kill here since we created the connection */ + if (newconn) + aim_conn_kill(sess, &newconn); return NULL; } else { priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)); + memcpy(priv->cookie, cookie, 8); + priv->state = 0; strncpy(priv->sn, sn, MAXSNLEN); strncpy(priv->ip, ip, sizeof(priv->ip)); newconn->priv = (void *)priv; - faimdprintf(2, "faim: connected to peer (fd = %d)\n", newconn->fd); - } - - /* cache da cookie. COOOOOKIES! */ - if(!(cachedcook = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) { - faimdprintf(1, "faim: accepttransfer: couldn't calloc cachedcook. yeep!\n"); - /* XXX die here, or go on? search for cachedcook for future references */ + faimdprintf(2, "faim: connected to peer (fd = %d)\n", newconn->fd); } - if(cachedcook) - memcpy(cachedcook->cookie, cookie, 8); - - /* strncpy(ft->fh.name, miscinfo->value+8, sizeof(ft->fh.name)); */ - - if(cachedcook) { /* see above calloc of cachedcook */ - cachedcook->type = AIM_COOKIETYPE_OFTGET; - cachedcook->data = (void *)priv; - - if (aim_cachecookie(sess, cachedcook) != 0) - faimdprintf(1, "faim: ERROR caching message cookie\n"); - } - - if(rendid == AIM_CAPS_GETFILE) { + if(rendid == AIM_CAPS_GETFILE) { faimdprintf(2, "faim: getfile request accept\n"); - if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1108, newconn, 0))) { + + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1108, newconn, 0))) { faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n"); - /* XXX: what else do we need to clean up here? */ + /* Its safe to call conn_kill here since we created the connection */ + aim_conn_kill(sess, &newconn); return NULL; - } - + } + newoft->lock = 1; - memcpy(newoft->hdr.oft.magic, "OFT2", 4); - newoft->hdr.oft.hdr2len = 0xf8; /* 0x100 - 8 */ - - if(!(listingfh = aim_getlisting(file))) { - return NULL; - } + newoft->hdr.oft.hdr2len = 0x100 - 8; - if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { - free(newoft); + if(!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t)))) return NULL; - } - memcpy(listingfh->bcookie, cookie, 8); + fh->encrypt = 0x0000; + fh->compress = 0x0000; + fh->totfiles = listingfiles; + fh->filesleft = listingfiles; /* is this right -- total parts and parts left?*/ + fh->totparts = 0x0001; + fh->partsleft = 0x0001; + fh->totsize = listingtotsize; + fh->size = listingsize; /* ls -l listing.txt */ + fh->modtime = (int)time(NULL); /* we'll go with current time for now */ + fh->checksum = listingchecksum; + fh->rfcsum = 0x00000000; + fh->rfsize = 0x00000000; + fh->cretime = 0x00000000; + fh->rfcsum = 0x00000000; + fh->nrecvd = 0x00000000; + fh->recvcsum = 0x00000000; + memset(fh->idstring, 0, sizeof(fh->idstring)); + memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring)); + fh->flags = 0x02; + fh->lnameoffset = 0x1a; + fh->lsizeoffset = 0x10; + memset(fh->dummy, 0, sizeof(fh->dummy)); + memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo)); + + /* we need to figure out these encodings for filenames */ + fh->nencode = 0x0000; + fh->nlanguage = 0x0000; + memset(fh->name, 0, sizeof(fh->name)); + memcpy(fh->name, "listing.txt", sizeof(fh->name)); + + if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return NULL; + } - aim_oft_buildheader((void *)newoft->hdr.oft.hdr2, listingfh); + memcpy(fh->bcookie, cookie, 8); - free(listingfh); + if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, fh))) + printf("eek, bh fail!\n"); newoft->lock = 0; aim_tx_enqueue(sess, newoft); - printf("faim: getfile: OFT listing enqueued.\n"); - - } + + if(!(cachedcook = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) { + faimdprintf(1, "faim: accepttransfer: couldn't calloc cachedcook. yeep!\n"); + /* more cleanup */ + return NULL; + } - /* OSCAR CAP accept packet */ + memcpy(&(priv->fh), fh, sizeof(struct aim_fileheader_t)); + memcpy(cachedcook->cookie, cookie, 8); + + cachedcook->type = AIM_COOKIETYPE_OFTGET; + cachedcook->data = (void *)priv; + if (aim_cachecookie(sess, cachedcook) != 0) + faimdprintf(1, "faim: ERROR caching message cookie\n"); + + free(fh); + } + + /* OSCAR CAP accept packet */ + if(!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(sn)+4+2+8+16))) return NULL; - + newpacket->lock = 1; - curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid); - for (i = 0; i < 8; i++) + + for (i = 0; i < 8; i++) curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn)); curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn)); curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); curbyte += aimutil_put16(newpacket->data+curbyte, 0x001a); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept */); - for (i = 0; i < 8; i++) + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept*/); + + for (i = 0;i < 8; i++) curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]); - curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid); + curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid); newpacket->lock = 0; aim_tx_enqueue(sess, newpacket); return newconn; - } - return NULL; + } return NULL; } /* - * aim_getlisting(FILE *file) + * aim_getlisting(FILE *file) -- get an aim_fileheader_t for a given FILE* + * @file is an opened listing file * - * file is an opened listing file * returns a pointer to the filled-in fileheader_t * * currently omits checksum. we'll fix this when AOL breaks us, i @@ -626,7 +611,7 @@ faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess, * */ -faim_internal struct aim_fileheader_t *aim_getlisting(FILE *file) +faim_export struct aim_fileheader_t *aim_getlisting(FILE *file) { struct aim_fileheader_t *fh; u_long totsize = 0, size = 0, checksum = 0xffff0000; @@ -656,13 +641,13 @@ faim_internal struct aim_fileheader_t *aim_getlisting(FILE *file) faimdprintf(2, "faim: getlisting: couldn't seek to beginning of listing file\n"); } - bzero(linebuf, linelength); + memset(linebuf, 0, linelength); size = 0; while(fgets(linebuf, linelength, file)) { totfiles++; - bzero(sizebuf, 9); + memset(sizebuf, 0, 9); size += strlen(linebuf); @@ -677,13 +662,9 @@ faim_internal struct aim_fileheader_t *aim_getlisting(FILE *file) memcpy(sizebuf, linebuf+17, 8); totsize += strtol(sizebuf, NULL, 10); - bzero(linebuf, linelength); + memset(linebuf, 0, linelength); } - /* if(size != 0) { - faimdprintf(2, "faim: getlisting: size != 0 after while.. %i\n", size); - }*/ - if(fseek(file, 0, SEEK_SET) == -1) { perror("getlisting END2 fseek:"); faimdprintf(2, "getlising fseek END2 error\n"); @@ -740,15 +721,16 @@ faim_internal struct aim_fileheader_t *aim_getlisting(FILE *file) } /* - * establish: create a listening socket on a port. you need to call - * accept() when it's connected. - * portnum is the port number to bind to. - * returns your fd + * aim_listenestablish - create a listening socket on a port. + * @portnum: the port number to bind to. + * + * you need to call accept() when it's connected. returns your fd + * */ - faim_internal int aim_listenestablish(u_short portnum) { -#if defined(__linux__) /* XXX what other OS's support getaddrinfo? */ +#if defined(__linux__) + /* XXX what other OS's support getaddrinfo? */ int listenfd; const int on = 1; struct addrinfo hints, *res, *ressave; @@ -758,45 +740,51 @@ faim_internal int aim_listenestablish(u_short portnum) hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - if (getaddrinfo(NULL/*any IP*/, serv, &hints, &res) != 0) { + if (getaddrinfo(NULL /*any IP*/, serv, &hints, &res) != 0) { perror("getaddrinfo"); return -1; - } + } ressave = res; - do { - listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + do { + listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (listenfd < 0) continue; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0) - break; /* success */ + break; + /* success */ close(listenfd); } while ( (res = res->ai_next) ); + if (!res) return -1; - if (listen(listenfd, 1024)!=0) { + + if (listen(listenfd, 1024)!=0) { perror("listen"); return -1; - } + } + freeaddrinfo(ressave); return listenfd; -#else +#else int listenfd; const int on = 1; struct sockaddr_in sockin; - if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket(listenfd)"); return -1; - } + } + if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) != 0)) { perror("setsockopt(listenfd)"); close(listenfd); return -1; - } + } + memset(&sockin, 0, sizeof(struct sockaddr_in)); sockin.sin_family = AF_INET; sockin.sin_port = htons(portnum); + if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) { perror("bind(listenfd)"); close(listenfd); @@ -807,11 +795,17 @@ faim_internal int aim_listenestablish(u_short portnum) close(listenfd); return -1; } - return listenfd; #endif -} - +} +/* + * aim_get_command_rendezvous - OFT equivalent of aim_get_command + * @sess: session to work on + * @conn: conn to pull data from + * + * this reads and handles data from conn->fd. currently a little rough + * around the edges + */ faim_internal int aim_get_command_rendezvous(struct aim_session_t *sess, struct aim_conn_t *conn) { unsigned char hdrbuf1[6]; @@ -819,482 +813,477 @@ faim_internal int aim_get_command_rendezvous(struct aim_session_t *sess, struct int hdrlen, hdrtype; int flags = 0; rxcallback_t userfunc = NULL; - - + memset(hdrbuf1, 0, sizeof(hdrbuf1)); + faim_mutex_lock(&conn->active); + + /* gets locked down for the entirety */ - faim_mutex_lock(&conn->active); /* gets locked down for the entirety */ + if(conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE ) { + struct aim_filetransfer_priv *ft; + ft = conn->priv; + if(ft->state == 2) { + /* waiting on listing data */ + int ret = 0; + char *listing; + struct command_tx_struct *newoft; + if(!(listing = malloc(ft->fh.size))) { + faim_mutex_unlock(&conn->active); + return -1; + } - if ( (hdrlen = aim_recv(conn->fd, hdrbuf1, 6)) < 6) { + ft->state = 0; + if(aim_recv(conn->fd, listing, ft->fh.size) != ft->fh.size) + faimdprintf(2, "OFT get: file %s was short. (0x%lx)\n", ft->fh.name, ft->fh.size); + + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x120b, conn, 0))) { + faimdprintf(2, "faim: aim_get_command_rendezvous: getfile listing: tx_new OFT failed\n"); + faim_mutex_unlock(&conn->active); + free(listing); + aim_conn_close(conn); + return -1; + } + + newoft->lock = 1; + + memcpy(newoft->hdr.oft.magic, "OFT2", 4); + newoft->hdr.oft.hdr2len = 0x100 - 8; + + /* this is icky. */ + ft->fh.nrecvd = ft->fh.size; + ft->fh.recvcsum = ft->fh.checksum; + ft->fh.flags = 0; + + if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + free(listing); + faim_mutex_unlock(&conn->active); + return -1; + } + + if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) + printf("eek! bh fail listing\n"); + + /* send the 120b */ + newoft->lock = 0; + aim_tx_enqueue(sess, newoft); + if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTING)) ) + ret = userfunc(sess, NULL, conn, ft, listing); + + faim_mutex_unlock(&conn->active); + free(listing); + return ret; + } + if(ft->state == 3) { + /* waiting on file data */ + if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILERECIEVE)) ) { + faim_mutex_unlock(&conn->active); + return userfunc(sess, NULL, conn, ft); + } + faim_mutex_unlock(&conn->active); + return 0; + } + } - 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); - faim_mutex_unlock(&conn->active); - - if(hdrlen < 0) - perror("read"); - else { /* disconnected */ - switch(conn->subtype) { - case AIM_CONN_SUBTYPE_OFT_DIRECTIM: { /* XXX: clean up cookies here ? */ - struct aim_directim_priv *priv = NULL; - if(!(priv = (struct aim_directim_priv *)conn->priv) ) - return -1; /* not much we can do */ - aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTIM); - - - if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT)) ) { - aim_conn_close(conn); - return userfunc(sess, NULL, conn, priv->sn); - } + if ( (hdrlen = aim_recv(conn->fd, hdrbuf1, 6)) < 6) { + faimdprintf(2, "faim: rend: read error (fd: %i) %02x%02x%02x%02x%02x%02x (%i)\n", + conn->fd, hdrbuf1[0],hdrbuf1[1],hdrbuf1[2],hdrbuf1[3],hdrbuf1[4],hdrbuf1[5],hdrlen); + faim_mutex_unlock(&conn->active); + if(hdrlen < 0) + perror("read"); + else { /* disconnected */ + switch(conn->subtype) { + case AIM_CONN_SUBTYPE_OFT_DIRECTIM: { + /* XXX: clean up cookies here ? */ + struct aim_directim_priv *priv = NULL; + if(!(priv = (struct aim_directim_priv *)conn->priv) ) + return -1; + + aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTIM); + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT)) ) { + aim_conn_close(conn); + return userfunc(sess, NULL, conn, priv->sn); + } + break; + } + case AIM_CONN_SUBTYPE_OFT_GETFILE: { + struct aim_filetransfer_priv *priv; + if(!(priv = (struct aim_filetransfer_priv *)conn->priv)) + return -1; + aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTGET); + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT)) ) { + aim_conn_close(conn); + return userfunc(sess, NULL, conn, priv->sn); + } + break; + } + case AIM_CONN_SUBTYPE_OFT_SENDFILE: { + struct aim_filetransfer_priv *priv; + if(!(priv = (struct aim_filetransfer_priv *)conn->priv)) + return -1; + aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTSEND); + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_SENDFILEDISCONNECT)) ) { + aim_conn_close(conn); + return userfunc(sess, NULL, conn, priv->sn); + } + break; + } + } + + aim_conn_close(conn); + return -1; + } + } + + hdrlen = aimutil_get16(hdrbuf1+4); + hdrlen -= 6; + + if (!(hdr = malloc(hdrlen))) { + faim_mutex_unlock(&conn->active); + return -1; + } + + if (aim_recv(conn->fd, hdr, hdrlen) < hdrlen) { + perror("read"); + faimdprintf(2,"faim: rend: read2 error on %d (%d)\n", conn->fd, hdrlen); + free(hdr); + faim_mutex_unlock(&conn->active); + aim_conn_close(conn); + return -1; + } + hdrtype = aimutil_get16(hdr); + printf("\tgot 0x%04x", hdrtype); + switch (hdrtype) { + case 0x0001: { /* directim */ + int payloadlength = 0; + char *snptr = NULL; + struct aim_directim_priv *priv; + int i; + + if(!(priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)))) { + faim_mutex_unlock(&conn->active); + return -1; + } + + payloadlength = aimutil_get32(hdr+22); + flags = aimutil_get16(hdr+32); + snptr = (char *)hdr+38; + strncpy(priv->sn, snptr, MAXSNLEN); + + faimdprintf(2, "faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, snptr); + + if (flags == 0x000e) { + faim_mutex_unlock(&conn->active); + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)) ) + return userfunc(sess, NULL, snptr); + } else + if ((flags == 0x0000) && payloadlength) { + unsigned char *msg; + if(! (msg = calloc(1, payloadlength+1)) ) { + faim_mutex_unlock(&conn->active); + return -1; + } + if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) { + perror("read"); + printf("faim: rend: read3 error\n"); + free(msg); + faim_mutex_unlock(&conn->active); + aim_conn_close(conn); + return -1; + } + faim_mutex_unlock(&conn->active); + msg[payloadlength] = 0x00; + faimdprintf(2, "faim: directim: %s/%04x/%04x/%s\n", snptr, payloadlength, flags, msg); + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) ) + i = userfunc(sess, NULL, conn, snptr, msg); + free(msg); + return i; + } + break; + } + case 0x1108: { /* getfile listing.txt incoming tx->rx */ + struct aim_filetransfer_priv *ft; + struct aim_fileheader_t *fh; + struct aim_msgcookie_t *cook; + struct command_tx_struct *newoft; + faimdprintf(2,"faim: rend: fileget 0x1108\n"); + fh = aim_oft_getfh(hdr); + faim_mutex_unlock(&conn->active); + if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { + faim_mutex_unlock(&conn->active); + free(fh); + return -1; + } + ft = cook->data; + ft->state = 2; + + /* we're waaaaiiiting.. for listing.txt */ + + memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t)); + aim_cachecookie(sess, cook); + free(fh); + + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1209, conn, 0))) { + aim_conn_close(conn); + return -1; + } + + memcpy(newoft->hdr.oft.magic, "OFT2", 4); + newoft->hdr.oft.hdr2len = 0x100 - 8; + + if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } + + if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } + + newoft->lock = 0; + aim_tx_enqueue(sess, newoft); + break; + + } + case 0x1209: { /* get file listing ack rx->tx */ + struct aim_filetransfer_priv *ft; + struct aim_fileheader_t *fh; + struct aim_msgcookie_t *cook; + int ret; + + fh = aim_oft_getfh(hdr); + faim_mutex_unlock(&conn->active); + + if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) + faimdprintf(2, "shit, no cookie in 0x1209. (%i/%s)going to crash..\n", + AIM_COOKIETYPE_OFTGET, fh->bcookie); + + ft = cook->data; + + if(ft->fh.size != fh->size) + faimdprintf(2, "hrm. ft->fh.size (%ld) != fh->size (%ld). um. using ft->fh.size\n", + ft->fh.size, fh->size); + + + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ))) + ret = userfunc(sess, NULL, conn, fh); + + faimdprintf(2, "faim: get_command_rendezvous: hit end of 1209\n"); + + free(fh); + + return ret; + + break; + } + case 0x120b: { /* getfile listing.txt rx confirm */ + struct aim_filetransfer_priv *ft; + struct aim_msgcookie_t *cook; + struct aim_fileheader_t *fh; + + fh = aim_oft_getfh(hdr); + faim_mutex_unlock(&conn->active); + + if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { + free(fh); + return -1; + } + + ft = cook->data; + aim_cachecookie(sess, cook); + free(fh); + + /* XXX: call listing.txt rx confirm */ + break; + } + case 0x120c: { /* getfile file request */ + struct aim_filetransfer_priv *ft; + struct aim_msgcookie_t *cook; + struct aim_fileheader_t *fh; + struct command_tx_struct *newoft; + int i; + + fh = aim_oft_getfh(hdr); + faim_mutex_unlock(&conn->active); - break; - } + if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) + return -1; - case AIM_CONN_SUBTYPE_OFT_GETFILE: { - struct aim_filetransfer_priv *priv; - if(!(priv = (struct aim_filetransfer_priv *)conn->priv)) - return -1; + ft = cook->data; + memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t)); + free(fh); - aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTGET); + aim_cachecookie(sess, cook); - if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT)) ) { - aim_conn_close(conn); - return userfunc(sess, NULL, conn, priv->sn); - } + faimdprintf(2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name); - break; - } + if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) ) + i = userfunc(sess, NULL, conn, &(ft->fh), cook->cookie); - case AIM_CONN_SUBTYPE_OFT_SENDFILE: { - struct aim_filetransfer_priv *priv; - if(!(priv = (struct aim_filetransfer_priv *)conn->priv)) - return -1; + if(i < 0) + return -1; - aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTSEND); + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0101, conn, 0))) { + faimdprintf(2, "faim: send_final_transfer: tx_new OFT failed\n"); + return -1; + } - if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_SENDFILEDISCONNECT)) ) { - aim_conn_close(conn); - return userfunc(sess, NULL, conn, priv->sn); - } + newoft->lock = 1; + memcpy(newoft->hdr.oft.magic, "OFT2", 4); + newoft->hdr.oft.hdr2len = 0x100 - 8; - break; - } - } + if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } - aim_conn_close(conn); - aim_conn_kill(sess, &conn); - - return -1; - } - } - - hdrlen = aimutil_get16(hdrbuf1+4); - - hdrlen -= 6; - if (!(hdr = malloc(hdrlen))) { - faim_mutex_unlock(&conn->active); - return -1; - } - - if (aim_recv(conn->fd, hdr, hdrlen) < hdrlen) { - perror("read"); - faimdprintf(2,"faim: rend: read2 error\n"); - free(hdr); - faim_mutex_unlock(&conn->active); - aim_conn_close(conn); - return -1; - } - - hdrtype = aimutil_get16(hdr); - - switch (hdrtype) { - case 0x0001: { /* directim */ - int payloadlength = 0; - char *snptr = NULL; - struct aim_directim_priv *priv; - int i; - - priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); - - payloadlength = aimutil_get32(hdr+22); - flags = aimutil_get16(hdr+32); - snptr = (char *)hdr+38; - - strncpy(priv->sn, snptr, MAXSNLEN); - - faimdprintf(2, "faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, snptr); - - if (flags == 0x000e) { - faim_mutex_unlock(&conn->active); - if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)) ) - return userfunc(sess, NULL, snptr); - } else if ((flags == 0x0000) && payloadlength) { - unsigned char *msg; - - if(! (msg = calloc(1, payloadlength+1)) ) { - faim_mutex_unlock(&conn->active); - return -1; - } - - if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) { - perror("read"); - printf("faim: rend: read3 error\n"); - free(msg); - faim_mutex_unlock(&conn->active); - aim_conn_close(conn); - return -1; - } - faim_mutex_unlock(&conn->active); - msg[payloadlength] = '\0'; - faimdprintf(2, "faim: directim: %s/%04x/%04x/%s\n", snptr, payloadlength, flags, msg); - - - if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) ) - i = userfunc(sess, NULL, conn, snptr, msg); - - free(msg); - return i; - } - break; - } -#if 0 - /* currently experimental to a non-compiling degree */ - case 0x1108: { /* getfile listing.txt incoming tx->rx */ - struct aim_filetransfer_priv *ft; - struct aim_fileheader_t *fh; - struct aim_msgcookie_t *cook; - struct aim_conn_type *newoft; - - int commandlen; - char *data; - - faimdprintf(2,"faim: rend: fileget 0x1108\n"); - - if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) { - faimdprintf(2, "faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n"); - faim_mutex_unlock(&conn->active); - return -1; - } - - ft->state = 1; /* we're waaaaiiiting.. */ - - fh = aim_oft_getfh(hdr); - - memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t)); - - if(!(cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET))) { - faim_mutex_unlock(&conn->active); - return -1; - } - - if(cook->data) - free(cook->data); - - cook->data = ft; - - aim_cachecookie(sess, cook); - - if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x1209, conn, 0))) { - /* XXX: what else do we need to clean up here? */ - faim_mutex_unlock(&conn->active); - return -1; - } + /* protocol BS: nrecvd, recvcsum to 0, flags to 0x20. */ + ft->fh.nrecvd = 0; + ft->fh.recvcsum = 0; + ft->fh.flags = 0x20; + aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)); - aim_oft_buildheader((void *)newoft->hdr.oft.hdr2, ft->fh); /* no change */ - newoft->lock = 0; - aim_tx_enqueue(sess, newoft); - break; - } -#endif - case 0x1209: { /* get file listing ack rx->tx */ - struct aim_filetransfer_priv *ft; - struct aim_fileheader_t *fh; - struct aim_msgcookie_t *cook; - - int commandlen; - char *data; - - faimdprintf(2,"faim: rend: fileget 0x1209\n"); - - if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) { - faimdprintf(2, "faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n"); - faim_mutex_unlock(&conn->active); - return -1; - } - - fh = aim_oft_getfh(hdr); - - memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t)); - - cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET); - - /* we currently trust the other client to be giving us Valid - * Enough input, else this gets to be a messy function (and we - * won't break like winaim does when it gets bad input =) */ - - if(cook->data) - free(cook->data); /* XXX */ - - cook->data = ft; - - aim_cachecookie(sess, cook); - - /* XXX: have this send chunks of the file instead of the whole - * file. requires rethinking some code. */ - - if(fseek(sess->oft.listing, 0, SEEK_SET) != 0) { - perror("get_command_rendezvous 1209 fseek(SET):"); - faimdprintf(2, "faim: getlisting: couldn't seek to beginning of listing file\n"); - } - commandlen = ft->fh.size; - - if((data = (char *)calloc(1, commandlen)) == NULL) { - faimdprintf(2, "faim: get_command_rendezvous 1209: couldn't malloc data.\n"); - faim_mutex_unlock(&conn->active); - return -1; - - } + newoft->lock = 0; + aim_tx_enqueue(sess, newoft); - for(commandlen = 0; commandlen < ft->fh.size; commandlen++) - if( (data[commandlen] = (unsigned char)fgetc(sess->oft.listing)) == EOF) - faimdprintf(2, "faim: get_command_rendezvous 1209: got early EOF (error?)\n"); + faimdprintf(2, "faim: OFT: OFT file header enqueued.\n"); - commandlen = ft->fh.size; + return i; - if (write(conn->fd, data, commandlen) != commandlen) { - perror("listing write error"); - } - faim_mutex_unlock(&conn->active); - - faimdprintf(2, "faim: get_command_rendezvous: hit end of 1209\n"); - - free(data); - - break; - } - case 0x120b: { /* get file second */ - struct aim_filetransfer_priv *ft; - struct aim_msgcookie_t *cook; - - struct aim_fileheader_t *fh; - - faimdprintf(2, "faim: rend: fileget 120b\n"); - - if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) { - faimdprintf(2, "faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n"); - faim_mutex_unlock(&conn->active); - return -1; - } + break; + } + case 0x0101: { /* getfile: sending data */ + struct aim_fileheader_t *fh; + struct aim_filetransfer_priv *ft; + struct aim_msgcookie_t *cook; + struct command_tx_struct *newoft; - fh = aim_oft_getfh(hdr); - - memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t)); - - if(!(cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET))) { - faim_mutex_unlock(&conn->active); - return -1; - } - - if(cook->data) - free(cook->data); /* XXX: integrate cookie caching */ - - cook->data = ft; - - aim_cachecookie(sess, cook); - - faim_mutex_unlock(&conn->active); - - /* call listing.txt rx confirm */ - - break; - } - case 0x120c: { /* yet more get file */ - struct aim_filetransfer_priv *ft; - struct aim_msgcookie_t *cook; - struct aim_fileheader_t *listingfh; - struct command_tx_struct *newoft; + fh = aim_oft_getfh(hdr); + faim_mutex_unlock(&conn->active); - int i; + if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { + printf("fucking no cookie\n"); + free(fh); + return -1; + } + free(fh); - rxcallback_t userfunc; - - printf("faim: rend: fileget 120c\n"); + ft = cook->data; - if(!(ft = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) { - printf("faim: couldn't malloc ft. um. bad. bad bad. file transfer will likely fail, sorry.\n"); - faim_mutex_unlock(&conn->active); - return -1; - } + ft->state = 3; + aim_cachecookie(sess, cook); - if(hdrlen != 0x100) - printf("faim: fileget_command(120c): um. hdrlen != 0x100..\n"); + faimdprintf(2, "faim: fileget: %s seems to want to send %s\n", ft->sn, ft->fh.name); - listingfh = aim_oft_getfh((char *)hdr); + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0202, conn, 0))) { + aim_conn_close(conn); + faimdprintf(2, "faim: send_final_transfer: tx_new OFT failed\n"); + return -1; + } - memcpy(&(ft->fh), listingfh, sizeof(struct aim_fileheader_t)); - - if(!(cook = aim_checkcookie(sess, ft->fh.bcookie, AIM_COOKIETYPE_OFTGET))) { - faim_mutex_unlock(&conn->active); - return -1; - } - - if(cook->data) - free(cook->data); /* XXX */ + newoft->lock = 1; + memcpy(newoft->hdr.oft.magic, "OFT2", 4); - cook->data = ft; - - aim_cachecookie(sess, cook); + newoft->hdr.oft.hdr2len = 0x100 - 8; - faim_mutex_unlock(&conn->active); + if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } - faimdprintf(2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name); + aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)); - if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) ) - i = userfunc(sess, NULL, conn, &ft->fh, cook->cookie); - - if(i < 0) - return -1; - - if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0101, conn, 0))) { - faimdprintf(2, "faim: send_final_transfer: tx_new OFT failed\n"); - return -1; - } - - newoft->lock = 1; - - memcpy(newoft->hdr.oft.magic, "OFT2", 4); - newoft->hdr.oft.hdr2len = 0xf8; /* 0x100 - 8 */ - - if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) { - newoft->lock = 0; - aim_tx_destroy(newoft); - return -1; - } - - /* memcpy(listingfh->bcookie, ft->fh.bcookie, 8); */ - - listingfh->nrecvd = 0; /* these need reset for protocol-correctness */ - listingfh->recvcsum = 0; + newoft->lock = 0; + aim_tx_enqueue(sess, newoft); - aim_oft_buildheader((void *)newoft->hdr.oft.hdr2, listingfh); - - newoft->lock = 0; - aim_tx_enqueue(sess, newoft); - faimdprintf(2, "faim: OFT: OFT file enqueued.\n"); - - if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL) - return 1; - - i = userfunc(sess, NULL, conn, listingfh, listingfh->bcookie); + faimdprintf(2, "faim: OFT: OFT 0x0202 enqueued.\n"); - free(listingfh); + if( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL) + return 1; - return i; + break; + } + case 0x0202: { /* get file: ready to recieve data */ + struct aim_fileheader_t *fh; + struct aim_filetransfer_priv *ft; + struct aim_msgcookie_t *cook; + int ret = 1; - break; - } - case 0x0202: { /* get file: ready to recieve data */ - struct aim_fileheader_t *fh; - fh = aim_oft_getfh((char *)hdr); + fh = aim_oft_getfh(hdr); + faim_mutex_unlock(&conn->active); - faimdprintf(2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n"); + if(!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { + free(fh); + return -1; + } + + ft = cook->data; - faim_mutex_unlock(&conn->active); - - if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) == NULL) - return 1; + faimdprintf(2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n"); - return userfunc(sess, NULL, conn, fh); + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) ) + ret = userfunc(sess, NULL, conn, fh); - free(fh); + free(fh); - break; - } - case 0x0204: { /* get file: finished. close it up */ - int i; + return ret; + break; + } + case 0x0204: { /* get file: finished. close it up */ + int i; + struct aim_fileheader_t *fh; - struct aim_fileheader_t *fh; - fh = aim_oft_getfh((char *)hdr); + fh = aim_oft_getfh(hdr); + faim_mutex_unlock(&conn->active); - faimdprintf(2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n"); + faimdprintf(2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n"); - faim_mutex_unlock(&conn->active); - - if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) ) - i = userfunc(sess, NULL, conn, fh); - else - i = 1; + if ( (userfunc = aim_callhandler(conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) ) + i = userfunc(sess, NULL, conn, fh); + else + i = 1; - /* - free(fh); */ - /* not sure where to do this yet, as we need to keep it to allow multiple file sends... bleh */ + if(conn) + aim_conn_close(conn); - return i; - break; - } - default: { - printf("OFT frame: type %04x\n", hdrtype); - /* data connection may be unreliable here */ - faim_mutex_unlock(&conn->active); - break; - } - } /* switch */ + free(fh); - free(hdr); - - return 0; + return i; + break; + } + default: { + faimdprintf(2,"faim: OFT frame: uknown type %04x\n", hdrtype); + faim_mutex_unlock(&conn->active); + break; + } + } /* switch */ + free(hdr); + return 0; } - + /* - * aim_oft_registerlisting() - * sess: aim session - * file: opened FILE * - * listingdir: the path to listing.txt - * returns -1 on error, 0 on success. + * aim_oft_getfh - extracts a aim_fileheader_t from buffer hdr. + * @hdr: buffer to extract header from + * + * returns pointer to struct on success; %NULL on error. * - * it's not my problem if the listing fd is already set. */ - -faim_export int aim_oft_registerlisting(struct aim_session_t *sess, FILE *file, char* listingdir) -{ - if(!sess) - return -1; - - /* XXX: checksum each file in the listing */ - -#if 0 - if(sess->oft.listing) { - faimdprintf(1, "We already have a file pointer. Closing and overwriting.\n"); - fclose(sess->oft.listing); - } -#endif - sess->oft.listing = file; -#if 0 - if(sess->oft.listingdir) { - faimdprintf(1, "We already have a directory string. Freeing and overwriting\n"); - free(sess->oft.listingdir); - } -#endif - - if( (sess->oft.listingdir = (char *)calloc(1, strlen(listingdir)+1)) ) - memcpy(sess->oft.listingdir, listingdir, strlen(listingdir)); - else - return -1; - return 0; -} - -faim_internal struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr) +static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr) { struct aim_fileheader_t *fh; int i, j; - if(!(fh = calloc(1, sizeof(struct aim_fileheader_t)))) return NULL; - + /* [0] and [1] are the type. we can ignore those here. */ - i = 2; - for(j = 0; j < 8; j++, i++) fh->bcookie[j] = hdr[i]; fh->encrypt = aimutil_get16(hdr+i); @@ -1329,519 +1318,613 @@ faim_internal struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr) i += 4; fh->recvcsum = aimutil_get32(hdr+i); i += 4; - memcpy(fh->idstring, hdr+i, 32); i += 32; - fh->flags = aimutil_get8(hdr+i); i += 1; fh->lnameoffset = aimutil_get8(hdr+i); i += 1; fh->lsizeoffset = aimutil_get8(hdr+i); i += 1; - memcpy(fh->dummy, hdr+i, 69); i += 69; - memcpy(fh->macfileinfo, hdr+i, 16); i += 16; - fh->nencode = aimutil_get16(hdr+i); i += 2; fh->nlanguage = aimutil_get16(hdr+i); i += 2; - memcpy(fh->name, hdr+i, 64); i += 64; - return fh; -} - -faim_export int aim_oft_checksum(char *buffer, int bufsize, int *checksum) -{ - short check0, check1; - int i; - - check0 = ((*checksum & 0xFF000000) >> 16); - check1 = ((*checksum & 0x00ff0000) >> 16); - - for(i = 0; i < bufsize; i++) { - - if(i % 2) { /* use check1 -- second byte */ - if ( (short)buffer[i] > check1 ) { /* wrapping */ - - check1 += 0x100; /* this is a cheap way to wrap */ - - /* if we're wrapping, decrement the other one */ - if(check0 == 0) /* XXX: check this corner case */ - check0 = 0x00ff; - else - check0--; - } - - check1 -= buffer[i]; - } else { /* use check0 -- first byte */ - if ( (short)buffer[i] > check0 ) { /* wrapping */ - - check0 += 0x100; /* this is a cheap way to wrap */ - - /* if we're wrapping, decrement the other one */ - if(check1 == 0) /* XXX: check this corner case */ - check1 = 0x00ff; - else - check1--; - } - - check0 -= buffer[i]; - } - } +} - if(check0 > 0xff || check1 > 0xff) { /* they shouldn't be able to do this. error! */ - faimdprintf(2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1); - return -1; - } +/* + * aim_oft_checksum - calculate oft checksum of buffer + * @buffer: buffer of data to checksum + * @bufsize: size of buffer + * @checksum: pointer to integer to place result in (pointer!) + + * + * note that checksum is a pointer. checksum should be filled with + * 0xFFFF0000 for each new file; you can have this checksum chunks of + * files in series if you just call it repeatedly in a for(; ; ) loop + * and don't reset the checksum between each call. and you thought we + * didn't care about you and your pathetic client's meomry footprint + * ;^) + * + */ +faim_export int aim_oft_checksum(char *buffer, int bufsize, int *checksum){ short check0, check1; + int i; + check0 = ((*checksum & 0xFF000000) >> 16); + check1 = ((*checksum & 0x00ff0000) >> 16); + for(i = 0; i < bufsize; i++) { + if(i % 2) { /* use check1 -- second byte */ + if ( (short)buffer[i] > check1 ) { /* wrapping */ + check1 += 0x100; /* this is a cheap way to wrap */ + + /* if we're wrapping, decrement the other one */ + /* XXX: check this corner case */ + if(check0 == 0) + check0 = 0x00ff; + else + check0--; + } + check1 -= buffer[i]; + } else { /* use check0 -- first byte */ + if ( (short)buffer[i] > check0 ) { /* wrapping */ + check0 += 0x100; /* this is a cheap way to wrap */ - check0 &= 0xff; /* grab just the lowest byte */ - check1 &= 0xff; /* this should be clean, but just in case */ - - *checksum = ((check0 * 0x1000000) + (check1 * 0x10000)); - - return *checksum; -} - + /* if we're wrapping, decrement the other one */ + /* XXX: check this corner case */ + if(check1 == 0) + check1 = 0x00ff; + else + check1--; + } + check0 -= buffer[i]; + } + } + + if(check0 > 0xff || check1 > 0xff) { + /* they shouldn't be able to do this. error! */ + faimdprintf(2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1); + return -1; + } + + /* grab just the lowest byte; this should be clean, but just in + case */ + check0 &= 0xff; + check1 &= 0xff; + + *checksum = ((check0 * 0x1000000) + (check1 * 0x10000)); + return *checksum; +} /* - * aim_oft_buildheader: - * fills a buffer with network-order fh data. - * returns length written; -1 on error. - * dest: buffer to fill -- pre-alloced - * listingfh: fh to get data from + * aim_oft_buildheader - fills a buffer with network-order fh data + * @dest: buffer to fill -- pre-alloced + * @fh: fh to get data from * + * returns length written; -1 on error. * DOES NOT DO BOUNDS CHECKING! + * */ - - -faim_internal int aim_oft_buildheader(char *dest,struct aim_fileheader_t *listingfh) -{ +faim_internal int aim_oft_buildheader(unsigned char *dest,struct aim_fileheader_t *fh) +{ int i, curbyte; - - if(!dest || !listingfh) + if(!dest || !fh) return -1; - - curbyte = 0; - for(i = 0; i < 8; i++) - curbyte += aimutil_put8(dest+curbyte, listingfh->bcookie[i]); - curbyte += aimutil_put16(dest+curbyte, listingfh->encrypt); - curbyte += aimutil_put16(dest+curbyte, listingfh->compress); - curbyte += aimutil_put16(dest+curbyte, listingfh->totfiles); - curbyte += aimutil_put16(dest+curbyte, listingfh->filesleft); - curbyte += aimutil_put16(dest+curbyte, listingfh->totparts); - curbyte += aimutil_put16(dest+curbyte, listingfh->partsleft); - curbyte += aimutil_put32(dest+curbyte, listingfh->totsize); - curbyte += aimutil_put32(dest+curbyte, listingfh->size); - curbyte += aimutil_put32(dest+curbyte, listingfh->modtime); - curbyte += aimutil_put32(dest+curbyte, listingfh->checksum); - curbyte += aimutil_put32(dest+curbyte, listingfh->rfrcsum); - curbyte += aimutil_put32(dest+curbyte, listingfh->rfsize); - curbyte += aimutil_put32(dest+curbyte, listingfh->cretime); - curbyte += aimutil_put32(dest+curbyte, listingfh->rfcsum); - curbyte += aimutil_put32(dest+curbyte, listingfh->nrecvd); - curbyte += aimutil_put32(dest+curbyte, listingfh->recvcsum); - - memcpy(dest+curbyte, listingfh->idstring, 32); + curbyte = 0; + for(i = 0; i < 8; i++) + curbyte += aimutil_put8(dest+curbyte, fh->bcookie[i]); + curbyte += aimutil_put16(dest+curbyte, fh->encrypt); + curbyte += aimutil_put16(dest+curbyte, fh->compress); + curbyte += aimutil_put16(dest+curbyte, fh->totfiles); + curbyte += aimutil_put16(dest+curbyte, fh->filesleft); + curbyte += aimutil_put16(dest+curbyte, fh->totparts); + curbyte += aimutil_put16(dest+curbyte, fh->partsleft); + curbyte += aimutil_put32(dest+curbyte, fh->totsize); + curbyte += aimutil_put32(dest+curbyte, fh->size); + curbyte += aimutil_put32(dest+curbyte, fh->modtime); + curbyte += aimutil_put32(dest+curbyte, fh->checksum); + curbyte += aimutil_put32(dest+curbyte, fh->rfrcsum); + curbyte += aimutil_put32(dest+curbyte, fh->rfsize); + curbyte += aimutil_put32(dest+curbyte, fh->cretime); + curbyte += aimutil_put32(dest+curbyte, fh->rfcsum); + curbyte += aimutil_put32(dest+curbyte, fh->nrecvd); + curbyte += aimutil_put32(dest+curbyte, fh->recvcsum); + memcpy(dest+curbyte, fh->idstring, 32); curbyte += 32; - - curbyte += aimutil_put8(dest+curbyte, listingfh->flags); - curbyte += aimutil_put8(dest+curbyte, listingfh->lnameoffset); - curbyte += aimutil_put8(dest+curbyte, listingfh->lsizeoffset); - - memcpy(dest+curbyte, listingfh->dummy, 69); + curbyte += aimutil_put8(dest+curbyte, fh->flags); + curbyte += aimutil_put8(dest+curbyte, fh->lnameoffset); + curbyte += aimutil_put8(dest+curbyte, fh->lsizeoffset); + memcpy(dest+curbyte, fh->dummy, 69); curbyte += 69; - - memcpy(dest+curbyte, listingfh->macfileinfo, 16); + memcpy(dest+curbyte, fh->macfileinfo, 16); curbyte += 16; + curbyte += aimutil_put16(dest+curbyte, fh->nencode); + curbyte += aimutil_put16(dest+curbyte, fh->nlanguage); + memset(dest+curbyte, 0x00, 64); + memcpy(dest+curbyte, fh->name, 64); - curbyte += aimutil_put16(dest+curbyte, listingfh->nencode); - curbyte += aimutil_put16(dest+curbyte, listingfh->nlanguage); - - memcpy(dest+curbyte, listingfh->name, 64); /* XXX: Filenames longer than 64B */ + /* XXX: Filenames longer than 64B */ curbyte += 64; - return curbyte; } - +#if 0 /* - * int aim_getfile_send(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh) - * conn is the OFT connection to shove the data down, - * tosend is the FILE * for the file to send - * fh is the filled-in fh value - * returns -1 on error, 1 on success. - */ - +* aim_getfile_send - send a FULL file down (ie: blocking; that is: DON'T USE IT, use send_chunk below.) +* @conn: the OFT connection to shove the data down, +* @tosend: is the FILE +* for the file to send +* @fh: the filled-in fh value + +* +* returns -1 on error, 1 on success. This sends the whold file and +* blocks, so it will totally mess you up if you use it. use the +* send_chunk below and work out a schduling setup or some such. + +* +*/ faim_export int aim_getfile_send(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh) -{ - int pos, bufpos, i; +{ + int pos, bufpos, i; const int bufsize = 4096; char *buf; - - /* sanity checks */ - if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->subtype != AIM_CONN_SUBTYPE_OFT_GETFILE) { - faimdprintf(1, "getfile_send: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype); - return -1; - } - - if(!tosend) { - faimdprintf(1, "getfile_send: file pointer isn't valid\n"); - return -1; - } - - if(!fh) { - faimdprintf(1, "getfile_send: fh isn't valid\n"); - return -1; - } - - /* real code */ - - if(!(buf = (char *)calloc(1, bufsize))) { - perror("faim: getfile_send: calloc:"); - faimdprintf(2, "getfile_send calloc error\n"); - return -1; - } - - pos = 0; - - if( fseek(tosend, 0, SEEK_SET) == -1) { - perror("faim: getfile_send: fseek:"); - faimdprintf(2, "getfile_send fseek error\n"); - } - - faimdprintf(2, "faim: getfile_send: using %i byte blocks\n", bufsize); - - for(pos = 0; pos < fh->size; pos++) { - bufpos = pos % bufsize; - - if(bufpos == 0 && pos > 0) { /* filled our buffer. spit it across the wire */ - if ( (i = write(conn->fd, buf, bufsize)) != bufsize ) { - perror("faim: getfile_send: write1: "); - faimdprintf(1, "faim: getfile_send: whoopsy, didn't write it all...\n"); - free(buf); - return -1; - } - } - if( (buf[bufpos] = fgetc(tosend)) == EOF) { - if(pos != fh->size) { - printf("faim: getfile_send: hrm... apparent early EOF at pos 0x%x of 0x%lx\n", pos, fh->size); - faimdprintf(1, "faim: getfile_send: hrm... apparent early EOF at pos 0x%lx of 0x%lx\n", pos, fh->size); - free(buf); - return -1; - } - } - - - } - - if( (i = write(conn->fd, buf, bufpos+1)) != (bufpos+1)) { - perror("faim: getfile_send: write2: "); - faimdprintf(1, "faim: getfile_send cleanup: whoopsy, didn't write it all...\n"); - free(buf); - return -1; - } - - free(buf); - fclose(tosend); - return 1; + /* sanity checks + */ + + if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->subtype != AIM_CONN_SUBTYPE_OFT_GETFILE) + { faimdprintf(1, "getfile_send: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype); + return -1; + } + if(!tosend) + { faimdprintf(1, "getfile_send: file pointer isn't valid\n"); + return -1; + } + if(!fh) + { faimdprintf(1, "getfile_send: fh isn't valid\n"); + return -1; + } + /* real code + */ + + if(!(buf = (char *)calloc(1, bufsize))) + { perror("faim: getfile_send: calloc:"); + faimdprintf(2, "getfile_send calloc error\n"); + return -1; + } pos = 0; + if( fseek(tosend, 0, SEEK_SET) == -1) + { perror("faim: getfile_send: fseek:"); + faimdprintf(2, "getfile_send fseek error\n"); + } faimdprintf(2, "faim: getfile_send: using %i byte blocks\n", bufsize); + for(pos = 0; + pos < fh->size; + pos++) { bufpos = pos % bufsize; + if(bufpos == 0 && pos > 0) + { + /* filled our buffer. spit it across the wire + */ + + if ( (i = send(conn->fd, buf, bufsize, 0)) != bufsize ) + { perror("faim: getfile_send: write1: "); + faimdprintf(1, "faim: getfile_send: whoopsy, didn't write it all...\n"); + free(buf); + return -1; + } } + if( (buf[bufpos] = fgetc(tosend)) == EOF) + { + if(pos != fh->size) + { printf("faim: getfile_send: hrm... apparent early EOF at pos 0x%x of 0x%lx\n", pos, fh->size); + faimdprintf(1, "faim: getfile_send: hrm... apparent early EOF at pos 0x%lx of 0x%lx\n", pos, fh->size); + free(buf); + return -1; + } } } + if( (i = send(conn->fd, buf, bufpos+1, 0)) != (bufpos+1)) + { perror("faim: getfile_send: write2: "); + faimdprintf(1, "faim: getfile_send cleanup: whoopsy, didn't write it all...\n"); + free(buf); + return -1; + } free(buf); + fclose(tosend); + return 1; } - + /* - * int aim_getfile_send_chunk(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh, int pos, int bufsize) - * conn is the OFT connection to shove the data down, - * tosend is the FILE * for the file to send - * fh is the filled-in fh value - * pos is the position to start at (at beginning should be 0, after 5 - * bytes are sent should be 5); -1 for "don't seek" - * bufsize is the size of the chunk to send - * - * returns -1 on error, new pos on success. - * - * - * Notes on usage: - * You don't really have to keep track of pos if you don't want - * to. just always call with -1 for pos, and it'll use the one - * contained within the FILE *. - * - * if (pos + chunksize > fh->size), we only send as much data as we - * can get (ie: up to fh->size. - */ +* aim_getfile_send_chunk - send a chunk of a file down a conn +* @conn: the OFT connection to shove the data down, +* @tosend: the buffer to send +* @fh: the filled-in fh value +* @pos: the position to start at (see below) +* @bufsize: the size of the chunk to send + +* +* returns -1 on error, new pos on success. +* Notes on usage: +* You don't really have to keep track of pos if you don't want +* to. just always call with -1 for pos, and it'll use the one +* contained within the FILE +*. + +* +* if (pos + chunksize > fh->size), we only send as much data as we +* can get (ie: up to fh->size. + +* +* the value of pos: at beginning should be 0, after 5 bytes are sent +* should be 5); -1 for "don't seek" + +* +* GOING TO BE CHANGED TO USE unsigned char +* BUFFERS!!! +*/ faim_export int aim_getfile_send_chunk(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh, int pos, int bufsize) { - int bufpos; - char *buf; - - /* sanity checks */ - if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->type != AIM_CONN_SUBTYPE_OFT_GETFILE) { - faimdprintf(1, "faim: getfile_send_chunk: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype); - return -1; - } - - if(!tosend) { - faimdprintf(1, "faim: getfile_send_chunk: file pointer isn't valid\n"); - return -1; - } - - if(!fh) { - faimdprintf(1, "faim: getfile_send_chunk: fh isn't valid\n"); - return -1; - } - - /* real code */ - - if(!(buf = (char *)calloc(1, bufsize))) { - perror("faim: getfile_send_chunk: calloc:"); - faimdprintf(2, "faim: getfile_send_chunk calloc error\n"); - return -1; - } - - if(pos != -1) { - if( fseek(tosend, pos, SEEK_SET) == -1) { - perror("faim: getfile_send_chunk: fseek:"); - faimdprintf(2, "faim: getfile_send_chunk fseek error\n"); - } - } - - faimdprintf(2, "faim: getfile_send_chunk: using %i byte blocks\n", bufsize); - - for(bufpos = 0; pos < fh->size; bufpos++, pos++) { - if( (buf[bufpos] = fgetc(tosend)) == EOF) { - if(pos != fh->size) { - faimdprintf(1, "faim: getfile_send_chunk: hrm... apparent early EOF at pos 0x%x of 0x%x\n", pos, fh->size); - free(buf); - return -1; - } - } - } - - if( write(conn->fd, buf, bufpos+1) != (bufpos+1)) { - faimdprintf(1, "faim: getfile_send_chunk cleanup: whoopsy, didn't write it all...\n"); - free(buf); - return -1; - } - - free(buf); - - return (pos + bufpos); + int bufpos; + char *buf; + + /* sanity checks + */ + + if(conn->type != AIM_CONN_TYPE_RENDEZVOUS || conn->type != AIM_CONN_SUBTYPE_OFT_GETFILE) + { faimdprintf(1, "faim: getfile_send_chunk: conn->type(0x%04x) != RENDEZVOUS or conn->subtype(0x%04x) != GETFILE\n", conn->type, conn->subtype); + return -1; + } + if(!tosend) + { faimdprintf(1, "faim: getfile_send_chunk: file pointer isn't valid\n"); + return -1; + } + if(!fh) + { faimdprintf(1, "faim: getfile_send_chunk: fh isn't valid\n"); + return -1; + } + /* real code + */ + + if(!(buf = (char *)calloc(1, bufsize))) + { perror("faim: getfile_send_chunk: calloc:"); + faimdprintf(2, "faim: getfile_send_chunk calloc error\n"); + return -1; + } + if(pos != -1) + { + if( fseek(tosend, pos, SEEK_SET) == -1) + { perror("faim: getfile_send_chunk: fseek:"); + faimdprintf(2, "faim: getfile_send_chunk fseek error\n"); + } } faimdprintf(2, "faim: getfile_send_chunk: using %i byte blocks\n", bufsize); + for(bufpos = 0; + pos < fh->size; + bufpos++, pos++) { + if( (buf[bufpos] = fgetc(tosend)) == EOF) + { + if(pos != fh->size) + { faimdprintf(1, "faim: getfile_send_chunk: hrm... apparent early EOF at pos 0x%x of 0x%x\n", pos, fh->size); + free(buf); + return -1; + } } } + if( send(conn->fd, buf, bufpos+1, 0) != (bufpos+1)) + { faimdprintf(1, "faim: getfile_send_chunk cleanup: whoopsy, didn't write it all...\n"); + free(buf); + return -1; + } free(buf); + return (pos + bufpos); } - +#endif /* - * aim_tx_destroy: - * free's tx_command_t's. if command is locked, doesn't free. - * returns -1 on error (locked struct); 0 on success. - * command is the command to free - */ +* aim_tx_destroy - free's tx_command_t's +* @command: the command to free -faim_internal int aim_tx_destroy(struct command_tx_struct *command) -{ +* +* if command is locked, doesn't free. +* returns -1 on error (locked struct); 0 on success. + +* +*/ +faim_internal int aim_tx_destroy(struct command_tx_struct *command){ if(command->lock) return -1; if(command->data) free(command->data); + if (command->hdrtype == AIM_FRAMETYPE_OFT && command->hdr.oft.hdr2) + free(command->hdr.oft.hdr2); free(command); - return 0; -} +} -#if 0 /* - * aim_getfile_intitiate: - * For those times when we want to open up the directim channel ourselves. - * sess is your session, - * conn is the BOS conn, - * priv is a dummy priv value (we'll let it get filled in later) (if - * you pass a NULL, we alloc one) - * destsn is the SN to connect to. + * aim_getfile_intitiate - For those times when we want to open up the getfile dialog ourselves. + * @sess: your session, + * @conn: the BOS conn, + * @destsn is the SN to connect to. */ - - -faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess, - struct aim_conn_t *conn, - struct aim_getfile_priv *priv, - char *destsn) -{ +faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess, struct aim_conn_t *conn, char *destsn) +{ struct command_tx_struct *newpacket; - struct aim_conn_t *newconn; - - struct aim_msgcookie_t *cookie; - - int curbyte, i, listenfd; - short port = 4443; - - struct hostent *hptr; - struct utsname myname; - - char cap[16]; - char d[4]; /* XXX: IPv6. *cough* */ - - /* - * Open our socket - */ + struct aim_conn_t *newconn; + struct aim_filetransfer_priv *priv; + struct aim_msgcookie_t *cookie; + int curbyte, i, listenfd; + short port = 4443; + struct hostent *hptr; + struct utsname myname; + char cap[16]; + char d[4]; + + /* Open our socket */ - if( (listenfd = aim_listenestablish(port)) == -1) - return NULL; + if( (listenfd = aim_listenestablish(port)) == -1) + return NULL; - /* - * get our local IP - */ + /* get our local IP */ - if(uname(&myname) < 0) - return NULL; + if(uname(&myname) < 0) + return NULL; + if( (hptr = gethostbyname(myname.nodename)) == NULL) + return NULL; + memcpy(&d, hptr->h_addr_list[0], 4); - if( (hptr = gethostbyname(myname.nodename)) == NULL) - return NULL; + /* XXX: this probably isn't quite kosher, but it works */ + aim_putcap(cap, 16, AIM_CAPS_GETFILE); - memcpy(&d, hptr->h_addr_list[0], 4); /* XXX: this probably isn't quite kosher, but it works */ + /* create the OSCAR packet */ - aim_putcap(cap, 16, AIM_CAPS_IMIMAGE); + if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x42))) + return NULL; + newpacket->lock = 1; - /* - * create the OSCAR packet - */ + /* lock struct */ + curbyte = 0; + curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid); - if (!(newpacket = aim_tx_new(AIM_FRAMETYPE_OSCAR, 0x0002, conn, 10+8+2+1+strlen(destsn)+4+4+0x32))) - return NULL; + /* XXX: check the cookie before commiting to using it */ - newpacket->lock = 1; /* lock struct */ + /* Generate a random message cookie + * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */ + for (i=0; i<7; i++) + curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 10)); - curbyte = 0; - curbyte += aim_putsnac(newpacket->data+curbyte, - 0x0004, 0x0006, 0x0000, sess->snac_nextid); + curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); - /* - * Generate a random message cookie - * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. - */ - for (i=0;i<7;i++) - curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 20)); - curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); + /* grab all the data for cookie caching. */ - /* - * grab all the data for cookie caching. - */ - cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)); + cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)); + memcpy(cookie->cookie, newpacket->data+curbyte-8, 8); + cookie->type = AIM_COOKIETYPE_OFTGET; - memcpy(cookie->cookie, newpacket->data+curbyte-8, 8); - cookie->type = AIM_COOKIETYPE_OFTIM; - - if(!priv) - priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); + priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)); + memcpy(priv->cookie, cookie, 8); + memcpy(priv->sn, destsn, sizeof(priv->sn)); + memcpy(priv->fh.name, "listing.txt", strlen("listing.txt")); + priv->state = 1; + cookie->data = priv; + conn->priv = priv; + aim_cachecookie(sess, cookie); - memcpy(priv->cookie, cookie, 8); - memcpy(priv->sn, destsn, sizeof(priv->sn)); + /* cache da cookie */ - cookie->data = priv; + /* Channel ID */ + curbyte += aimutil_put16(newpacket->data+curbyte,0x0002); - aim_cachecookie(sess, cookie); /* cache da cookie */ + /* Destination SN (prepended with byte length) */ + curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn)); + curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn)); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); - /* - * Channel ID - */ - curbyte += aimutil_put16(newpacket->data+curbyte,0x0002); + /* enTLV start */ + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0042); - /* - * Destination SN (prepended with byte length) - */ - curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn)); - curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn)); + /* Flag data / ICBM Parameters? */ + curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); + curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); + /* Cookie */ + curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8); - /* - * enTLV start - */ - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032); + /* Capability String */ + curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10); - /* - * Flag data / ICBM Parameters? - */ - curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); - curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); + /* 000a/0002 : 0001 */ + curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001); - /* - * Cookie - */ - curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8); + /* 0003/0004: IP address */ + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004); + for(i = 0; i < 4; i++) + curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); - /* - * Capability String - */ - curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10); + /* already in network byte order */ + + /* 0005/0002: Port */ + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); + curbyte += aimutil_put16(newpacket->data+curbyte, port); - /* - * 000a/0002 : 0001 - */ - curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001); + /* 000f/0000: ?? */ + curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); - /* - * 0003/0004: IP address - */ + /* 2711/000c: ?? */ + curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711); + curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c); + curbyte += aimutil_put32(newpacket->data+curbyte, 0x00120001); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004); + for(i = 0; i < 0x000c - 4; i++) + curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); - for(i = 0; i < 4; i++) - curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); /* already in network byte order */ + newpacket->commandlen = curbyte; + newpacket->lock = 0; + aim_tx_enqueue(sess, newpacket); - /* - * 0005/0002: Port - */ + /* allocate and set up our connection */ - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); - curbyte += aimutil_put16(newpacket->data+curbyte, port); + i = fcntl(listenfd, F_GETFL, 0); + fcntl(listenfd, F_SETFL, i | O_NONBLOCK); + newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL); - /* - * 000f/0000: umm.. dunno. Zigamorph[1]? - * [1]: see esr's TNHD. - */ + if (!newconn){ + perror("aim_newconn"); + return NULL; + } - curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f); - curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); + newconn->fd = listenfd; + newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE; + newconn->priv = priv; + faimdprintf(2,"faim: listening (fd = %d, unconnected)\n", newconn->fd); - printf("curbyte: 0x%x\n",curbyte); + return newconn; +} + +/* + * aim_oft_getfile_request - request a particular file over an established getfile connection + * @sess: your session + * @conn: the established OFT getfile connection + * @name: filename to request + * @size: size of the file + * + */ +faim_export int aim_oft_getfile_request(struct aim_session_t *sess, struct aim_conn_t *conn, const unsigned char *name, const int size) +{ + struct command_tx_struct *newoft; + struct aim_filetransfer_priv *ft; + if(!sess || !conn || !conn->priv || !name) + return 0; + + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x120c, conn, 0))) { + faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n"); + return -1; + } + + newoft->lock = 1; + + memcpy(newoft->hdr.oft.magic, "OFT2", 4); + newoft->hdr.oft.hdr2len = 0x100 - 8; + + ft = (struct aim_filetransfer_priv *)conn->priv; + ft->fh.filesleft = 1; + ft->fh.totfiles = 1; + ft->fh.totparts = 1; + ft->fh.partsleft = 1; + ft->fh.totsize = size; + ft->fh.size = size; + ft->fh.checksum = 0; + memcpy(ft->fh.name, name, strlen(name)); + memset(ft->fh.name+strlen(name), 0, 1); + + if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } + + if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } + + newoft->lock = 0; + + aim_tx_enqueue(sess, newoft); + return 0; +} + +/* + * aim_oft_getfile_ack - acknowledge a getfile download as complete + * @sess: your session + * @conn: the getfile conn to send the ack over + * + * call this function after you have read all the data in a particular + * filetransfer + * + */ +faim_export int aim_oft_getfile_ack(struct aim_session_t *sess, struct aim_conn_t *conn) { + struct command_tx_struct *newoft; + struct aim_filetransfer_priv *ft; - newpacket->commandlen = curbyte; - newpacket->lock = 0; + if(!sess || !conn || !conn->priv) + return -1; - aim_tx_enqueue(sess, newpacket); + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0202, conn, 0))) { + faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n"); + return -1; + } - /* - * allocate and set up our connection - */ + newoft->lock = 1; - i = fcntl(listenfd, F_GETFL, 0); - fcntl(listenfd, F_SETFL, i | O_NONBLOCK); + memcpy(newoft->hdr.oft.magic, "OFT2", 4); + newoft->hdr.oft.hdr2len = 0x100-8; - newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL); - if (!newconn) { - perror("aim_newconn"); - aim_conn_kill(sess, &newconn); - return NULL; - } + if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } - newconn->fd = listenfd; - newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; - newconn->priv = priv; - printf("faim: listening (fd = %d, unconnected)\n", newconn->fd); + ft = (struct aim_filetransfer_priv *)conn->priv; - /* - * XXX We need some way of closing the listener socket after - * n seconds of no connection. -- mid - */ + if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } - return newconn; + newoft->lock = 0; + aim_tx_enqueue(sess, newoft); + return 0; } + +/* + * aim_oft_getfile_end - end a getfile. + * @sess: your session + * @conn: the getfile connection + * + * call this before you close the getfile connection if you're on the + * receiving/requesting end. + */ +faim_export int aim_oft_getfile_end(struct aim_session_t *sess, struct aim_conn_t *conn) +{ + struct command_tx_struct *newoft; + struct aim_filetransfer_priv *ft; + + if(!sess || !conn || !conn->priv) + return -1; -#endif + if(!(newoft = aim_tx_new(AIM_FRAMETYPE_OFT, 0x0204, conn, 0))) { + faimdprintf(2, "faim: aim_accepttransfer: tx_new OFT failed\n"); + return -1; + } + + newoft->lock = 1; + + memcpy(newoft->hdr.oft.magic, "OFT2", 4); + newoft->hdr.oft.hdr2len = 0x100 - 8; + + if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } + + ft = (struct aim_filetransfer_priv *)conn->priv; + ft->state = 4; /* no longer wanting data */ + ft->fh.nrecvd = ft->fh.size; + ft->fh.recvcsum = ft->fh.checksum; + ft->fh.flags = 0x21; + + if(!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { + newoft->lock = 0; + aim_tx_destroy(newoft); + return -1; + } + + newoft->lock = 0; + aim_tx_enqueue(sess, newoft); + + return 0; +} diff --git a/aim_im.c b/aim_im.c index 9d0cbd9..89b2c5f 100644 --- a/aim_im.c +++ b/aim_im.c @@ -5,6 +5,7 @@ * */ +#define FAIM_INTERNAL #include /* @@ -441,14 +442,16 @@ faim_internal int aim_parse_incoming_im_middle(struct aim_session_t *sess, type = aim_msgcookie_gettype(reqclass); /* XXX: fix this shitty code */ - if ((cook = aim_uncachecookie(sess, cookie, type)) == NULL) { - printf("faim: non-data rendezvous thats not in cache!\n"); - aim_freetlvchain(&list2); - aim_freetlvchain(&tlvlist); - return 1; + if(type != 17) { + if ((cook = aim_uncachecookie(sess, cookie, type)) == NULL) { + printf("faim: non-data rendezvous thats not in cache!\n"); + aim_freetlvchain(&list2); + aim_freetlvchain(&tlvlist); + return 1; + } } - if (cook->type == AIM_CAPS_SENDFILE) { + if (cook->type == AIM_COOKIETYPE_OFTGET) { struct aim_filetransfer_priv *ft; if (cook->data) { @@ -456,25 +459,22 @@ faim_internal int aim_parse_incoming_im_middle(struct aim_session_t *sess, ft = (struct aim_filetransfer_priv *)cook->data; - if (aim_gettlv(list2, 0x000b, 1)) - errorcode = aim_gettlv16(list2, 0x000b, 1); - - if (errorcode) { - printf("faim: transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode); - } else if (status == 0x0002) { /* connection accepted */ - printf("faim: transfer from %s (%s) for %s accepted\n", ft->sn, ft->ip, ft->fh.name); + if(status != 0x0002) { + if (aim_gettlv(list2, 0x000b, 1)) + errorcode = aim_gettlv16(list2, 0x000b, 1); + + if (errorcode) + printf("faim: transfer from %s (%s) for %s cancelled (error code %d)\n", ft->sn, ft->ip, ft->fh.name, errorcode); } - free(cook->data); } else { - printf("faim: not data attached to file transfer\n"); + printf("faim: no data attached to file transfer\n"); } } else if (cook->type == AIM_CAPS_VOICE) { printf("faim: voice request cancelled\n"); } else { printf("faim: unknown cookie cache type %d\n", cook->type); } - - free(cook); + if (list2) aim_freetlvchain(&list2); aim_freetlvchain(&tlvlist); diff --git a/aim_info.c b/aim_info.c index 09bb4a2..1eb31b1 100644 --- a/aim_info.c +++ b/aim_info.c @@ -6,7 +6,7 @@ * */ - +#define FAIM_INTERNAL #include struct aim_priv_inforeq { diff --git a/aim_login.c b/aim_login.c index 8f880b1..10e1f79 100644 --- a/aim_login.c +++ b/aim_login.c @@ -5,6 +5,7 @@ * */ +#define FAIM_INTERNAL #include #include "md5.h" diff --git a/aim_logoff.c b/aim_logoff.c index f5d0375..0ea3f00 100644 --- a/aim_logoff.c +++ b/aim_logoff.c @@ -2,8 +2,11 @@ * aim_logoff.c * * + * XXX remove this excuse for a file. + * */ +#define FAIM_INTERNAL #include /* @@ -12,7 +15,7 @@ * Closes -ALL- open connections. * */ -int aim_logoff(struct aim_session_t *sess) +faim_export int aim_logoff(struct aim_session_t *sess) { aim_connrst(sess); /* in case we want to connect again */ diff --git a/aim_meta.c b/aim_meta.c index 67a24cf..dd7b2b0 100644 --- a/aim_meta.c +++ b/aim_meta.c @@ -5,7 +5,6 @@ */ #include -#include /* generated by mkbuildinfo.sh */ faim_export char *aim_getbuilddate(void) { diff --git a/aim_misc.c b/aim_misc.c index f5e78d7..a98047b 100644 --- a/aim_misc.c +++ b/aim_misc.c @@ -11,6 +11,7 @@ * */ +#define FAIM_INTERNAL #include /* diff --git a/aim_msgcookie.c b/aim_msgcookie.c index fb457ee..8978b9c 100644 --- a/aim_msgcookie.c +++ b/aim_msgcookie.c @@ -13,15 +13,15 @@ * wrong, we get quirky behavior when cookies step on each others' toes. */ +#define FAIM_INTERNAL #include /* * aim_cachecookie: * appends a cookie to the cookie list for sess. - * - if cookie->cookie for type cookie->type is found, addtime is updated. + * - if cookie->cookie for type cookie->type is found, -1 is returned * - copies cookie struct; you need to free() it afterwards; * - cookie->data is not copied, but passed along. don't free it. - * - newcook->addtime is updated accordingly; * - cookie->type is just passed across. * * returns -1 on error, 0 on success. @@ -31,29 +31,25 @@ faim_internal int aim_cachecookie(struct aim_session_t *sess, { struct aim_msgcookie_t *newcook; - if (!cookie) + if (!sess || !cookie) return -1; - if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) { - newcook->addtime = time(NULL); - if(cookie->data != newcook->data) { - - printf("faim: cachecookie: matching cookie/type pair " - "%x%x%x%x%x%x%x%x/%x has different *data. free()ing cookie copy..\n", - cookie->cookie[0], cookie->cookie[1], cookie->cookie[2], - cookie->cookie[3], cookie->cookie[4], cookie->cookie[5], - cookie->cookie[6], cookie->cookie[7], cookie->type); - - free(cookie->data); - } + printf("\t\tCC cache %d %s", cookie->type, cookie->cookie); + if(cookie->type == AIM_COOKIETYPE_OFTGET) { + struct aim_filetransfer_priv *priv; + priv = cookie->data; + printf("%s\n", priv->sn); + } else + printf("\n"); - return 0; + if( (newcook = aim_checkcookie(sess, cookie->cookie, cookie->type)) ) { + printf("aim_cachecookie: cookie already cached\n"); + return -1; } if (!(newcook = malloc(sizeof(struct aim_msgcookie_t)))) return -1; memcpy(newcook, cookie, sizeof(struct aim_msgcookie_t)); - newcook->addtime = time(NULL); newcook->next = sess->msgcookies; sess->msgcookies = newcook; @@ -69,78 +65,25 @@ faim_internal int aim_cachecookie(struct aim_session_t *sess, */ faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type) { - struct aim_msgcookie_t *cur; + struct aim_msgcookie_t *cur, **prev; if (!cookie || !sess->msgcookies) return NULL; - if ((sess->msgcookies->type == type) && - (memcmp(sess->msgcookies->cookie, cookie, 8) == 0)) { - struct aim_msgcookie_t *tmp; + printf("\t\tCC uncache %d %s\n", type, cookie); - tmp = sess->msgcookies; - sess->msgcookies = sess->msgcookies->next; - - return tmp; - } - - for (cur = sess->msgcookies; cur->next; cur = cur->next) { - if ((cur->next->type == type) && - (memcmp(cur->next->cookie, cookie, 8) == 0)) { - struct aim_msgcookie_t *tmp; - - tmp = cur->next; - cur->next = cur->next->next; - - return tmp; + for (prev = &sess->msgcookies; (cur = *prev); ) { + if ((cur->type == type) && + (memcmp(cur->cookie, cookie, 8) == 0)) { + *prev = cur->next; + return cur; } + prev = &cur->next; } return NULL; } -/* - * aim_purgecookies: - * purge out old cookies - * - * finds old cookies, calls uncache on them. - * - * this is highly inefficient, but It Works. and i don't feel like - * totally rewriting this. it might have some concurrency issues as - * well, if i rewrite it. - * - * i'll avoid the puns. - */ - -faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage) -{ - struct aim_msgcookie_t *cur; - time_t curtime; - - curtime = time(NULL); - - for (cur = sess->msgcookies; cur; cur = cur->next) { - if (cur->addtime > (time(NULL) - maxage)) { - struct aim_msgcookie_t *remed = NULL; - -#if 1 - printf("aimmsgcookie: WARNING purged obsolete message cookie %x%x%x%x %x%x%x%x\n", - cur->cookie[0], cur->cookie[1], cur->cookie[2], cur->cookie[3], - cur->cookie[4], cur->cookie[5], cur->cookie[6], cur->cookie[7]); -#endif - - remed = aim_uncachecookie(sess, cur->cookie, cur->type); - if (remed) { - if (remed->data) - free(remed->data); - free(remed); - } - } - } - - return 0; -} - faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, void *data) { struct aim_msgcookie_t *cookie; @@ -158,20 +101,42 @@ faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *c, int type, v return cookie; } -faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, unsigned char *cookie, int type) +faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *sess, const unsigned char *cookie, const int type) { struct aim_msgcookie_t *cur; - + + printf("\t\tCC check %d %s\n", type, cookie); + for (cur = sess->msgcookies; cur; cur = cur->next) { if ((cur->type == type) && (memcmp(cur->cookie, cookie, 8) == 0)) - return cur; + return cur; } return NULL; } -static int aim_freecookie(struct aim_msgcookie_t *cookie) { +faim_internal int aim_freecookie(struct aim_session_t *sess, struct aim_msgcookie_t *cookie) { + struct aim_msgcookie_t *cur, **prev; + + if (!sess || !cookie) + return -1; + + /* + * Make sure its not in the list somewhere still. + * + * If this actually happens, theres been a major coding failure + * on my part. However, that does not reduce its occurance likelyhood. + */ + for (prev = &sess->msgcookies; (cur = *prev); ) { + if (cur == cookie) { + *prev = cur->next; + } else + prev = &cur->next; + } + + free(cookie); + return 0; } diff --git a/aim_rxhandlers.c b/aim_rxhandlers.c index 7840167..8e55514 100644 --- a/aim_rxhandlers.c +++ b/aim_rxhandlers.c @@ -7,6 +7,7 @@ * */ +#define FAIM_INTERNAL #include /* @@ -515,10 +516,10 @@ faim_export int aim_rxdispatch(struct aim_session_t *sess) case 0x000a: /* Family: User lookup */ switch (subtype) { case 0x0001: - workingPtr->handled = aim_callhandler_noparam(sess, workingPtr->conn, 0x000a, 0x0001, workingPtr); + workingPtr->handled = aim_parse_searcherror(sess, workingPtr); break; case 0x0003: - workingPtr->handled = aim_callhandler_noparam(sess, workingPtr->conn, 0x000a, 0x0003, workingPtr); + workingPtr->handled = aim_parse_searchreply(sess, workingPtr); break; default: workingPtr->handled = aim_callhandler_noparam(sess, workingPtr->conn, AIM_CB_FAM_LOK, AIM_CB_LOK_DEFAULT, workingPtr); diff --git a/aim_rxqueue.c b/aim_rxqueue.c index bbdb207..6cc1806 100644 --- a/aim_rxqueue.c +++ b/aim_rxqueue.c @@ -6,6 +6,7 @@ * aim_rxhandlers.c. */ +#define FAIM_INTERNAL #include #ifndef _WIN32 @@ -72,8 +73,10 @@ faim_export int aim_get_command(struct aim_session_t *sess, struct aim_conn_t *c */ if (conn->type == AIM_CONN_TYPE_RENDEZVOUS) return aim_get_command_rendezvous(sess, conn); - if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) + if (conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) { + printf("out on fd %d\n", conn->fd); return 0; + } /* * Read FLAP header. Six bytes: diff --git a/aim_search.c b/aim_search.c index a8c4e1c..d1cba01 100644 --- a/aim_search.c +++ b/aim_search.c @@ -6,6 +6,7 @@ * */ +#define FAIM_INTERNAL #include faim_export unsigned long aim_usersearch_address(struct aim_session_t *sess, @@ -33,3 +34,89 @@ faim_export unsigned long aim_usersearch_address(struct aim_session_t *sess, return sess->snac_nextid; } + +faim_internal unsigned long aim_parse_searcherror(struct aim_session_t *sess, struct command_rx_struct *command) +{ + u_int i, ret; + int snacid; + rxcallback_t userfunc; + struct aim_snac_t *snac; + + i = 6; + + snacid = aimutil_get32(command->data+i); + i += 4; + + if(!(snac = aim_remsnac(sess, snacid))) { + faimdprintf(2, "faim: couldn't get a snac for %d, probably should crash.\n", snacid); + return 0; + } + + if((userfunc = aim_callhandler(command->conn, 0x000a, 0x0001))) + ret = userfunc(sess, command, snac->data /* address */); + else + ret = 0; + + if(snac) { + if(snac->data) + free(snac->data); + free(snac); + } + + return ret; +} + + +faim_internal unsigned long aim_parse_searchreply(struct aim_session_t *sess, struct command_rx_struct *command) +{ + u_int i, j, m, ret; + int snacid; + struct aim_tlvlist_t *tlvlist; + char *cur = NULL, *buf = NULL; + rxcallback_t userfunc; + struct aim_snac_t *snac; + + i = 6; + + snacid = aimutil_get32(command->data+i); + i += 4; + + if(!(snac = aim_remsnac(sess, snacid))) { + faimdprintf(2, "faim: couldn't get a snac for %d, probably should crash.\n", snacid); + return 0; + } + + tlvlist = aim_readtlvchain(command->data+i, command->commandlen-i); + + j = 0; + + m = aim_counttlvchain(&tlvlist); + + while((cur = aim_gettlv_str(tlvlist, 0x0001, j+1)) && j < m) { + if(!(buf = realloc(buf, (j+1) * (MAXSNLEN+1)))) + faimdprintf(2, "faim: couldn't realloc buf. oh well.\n"); + + strncpy(&buf[j * (MAXSNLEN+1)], cur, MAXSNLEN); + free(cur); + + j++; + } + + aim_freetlvchain(&tlvlist); + + if((userfunc = aim_callhandler(command->conn, 0x000a, 0x0003))) + ret = userfunc(sess, command, snac->data /* address */, j, buf); + else + ret = 0; + + if(snac) { + if(snac->data) + free(snac->data); + free(snac); + } + + if(buf) + free(buf); + + return ret; +} diff --git a/aim_snac.c b/aim_snac.c index ae46e47..08db68e 100644 --- a/aim_snac.c +++ b/aim_snac.c @@ -1,4 +1,3 @@ - /* * * Various SNAC-related dodads... @@ -13,6 +12,7 @@ * */ +#define FAIM_INTERNAL #include /* @@ -131,7 +131,7 @@ faim_internal int aim_cleansnacs(struct aim_session_t *sess, int i; for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { - struct aim_snac_t *cur = NULL, *next = NULL, *prev = NULL; + struct aim_snac_t *cur, **prev; time_t curtime; faim_mutex_lock(&sess->snac_hash_locks[i]); @@ -142,24 +142,18 @@ faim_internal int aim_cleansnacs(struct aim_session_t *sess, curtime = time(NULL); /* done here in case we waited for the lock */ - cur = sess->snac_hash[i]; - while (cur) { - next = cur->next; + for (prev = &sess->snac_hash[i]; (cur = *prev); ) { if ((curtime - cur->issuetime) > maxage) { - if (sess->snac_hash[i] == cur) - prev = sess->snac_hash[i] = next; - else - prev->next = next; + + *prev = cur->next; /* XXX should we have destructors here? */ if (cur->data) free(cur->data); free(cur); - } else { - prev = cur; - } - cur = next; + } else + prev = &cur->next; } faim_mutex_unlock(&sess->snac_hash_locks[i]); diff --git a/aim_tlv.c b/aim_tlv.c index 5279367..8b8a526 100644 --- a/aim_tlv.c +++ b/aim_tlv.c @@ -1,3 +1,5 @@ + +#define FAIM_INTERNAL #include /** diff --git a/aim_txqueue.c b/aim_txqueue.c index bc5eab3..3a12757 100644 --- a/aim_txqueue.c +++ b/aim_txqueue.c @@ -5,6 +5,7 @@ * */ +#define FAIM_INTERNAL #include #ifndef _WIN32 @@ -206,7 +207,7 @@ faim_internal int aim_tx_printqueue(struct aim_session_t *sess) faimdprintf(2, "\t %2x %2x %4x %4x %1d %1d\n", cur->hdrtype, (cur->hdrtype==AIM_FRAMETYPE_OFT)?cur->hdr.oft.type:cur->hdr.oscar.type, - (cur->hdrtype==AIM_FRAMETYPE_OSCAR)?cur->seqnum:0, + (cur->hdrtype==AIM_FRAMETYPE_OSCAR)?cur->hdr.oscar.seqnum:0, cur->commandlen, cur->lock, cur->sent); } @@ -311,12 +312,21 @@ faim_internal int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx } if ((cur->hdrtype == AIM_FRAMETYPE_OFT) && cur->commandlen) { + int curposi; + for(curposi = 0; curposi < cur->commandlen; curposi++) + printf("%02x ", cur->data[curposi]); + if (send(cur->conn->fd, cur->data, cur->commandlen, 0) != (int)cur->commandlen) { /* * Theres nothing we can do about this since we've already sent the * header! The connection is unstable. */ + faim_mutex_unlock(&cur->conn->active); + cur->sent = 1; + aim_conn_close(cur->conn); + return 0; /* bail out */ } + } cur->sent = 1; /* mark the struct as sent */ @@ -348,10 +358,6 @@ faim_export int aim_tx_flushqueue(struct aim_session_t *sess) { struct command_tx_struct *cur; -#if debug > 1 - int i = 0; -#endif - if (sess->queue_outgoing == NULL) return 0; @@ -372,6 +378,7 @@ faim_export int aim_tx_flushqueue(struct aim_session_t *sess) sleep((cur->conn->lastactivity + cur->conn->forcedlatency) - time(NULL)); } + /* XXX XXX XXX this should call the custom "queuing" function!! */ if (aim_tx_sendframe(sess, cur) == -1) break; } diff --git a/faim/aim.h b/faim/aim.h index d1f3616..044410a 100644 --- a/faim/aim.h +++ b/faim/aim.h @@ -11,13 +11,13 @@ #define FAIM_VERSION_MAJOR 0 #define FAIM_VERSION_MINOR 99 -#define FAIM_VERSION_MINORMINOR 0 +#define FAIM_VERSION_MINORMINOR 1 #include #include -#if !defined(FAIM_USEPTHREADS) && !defined(FAIM_USEFAKELOCKS) -#error pthreads or fakelocks are currently required. +#if !defined(FAIM_USEPTHREADS) && !defined(FAIM_USEFAKELOCKS) && !defined(FAIM_USENOPLOCKS) +#error pthreads, fakelocks, or noplocks are currently required. #endif #include @@ -36,6 +36,7 @@ #include #include #include +#include #endif #ifdef FAIM_USEPTHREADS @@ -55,9 +56,15 @@ */ #define faim_mutex_t char #define faim_mutex_init(x) *x = 0 -#define faim_mutex_lock(x) *x = 1; -#define faim_mutex_unlock(x) *x = 0; -#define faim_mutex_destroy(x) *x = 0; +#define faim_mutex_lock(x) while(*x != 0) {/* spin */}; *x = 1; +#define faim_mutex_unlock(x) while(*x != 0) {/* spin spin spin */}; *x = 0; +#define faim_mutex_destroy(x) while(*x != 0) {/* spiiiinnn */}; *x = 0; +#elif defined(FAIM_USENOPLOCKS) +#define faim_mutex_t char +#define faim_mutex_init(x) +#define faim_mutex_lock(x) +#define faim_mutex_unlock(x) +#define faim_mutex_destroy(x) #endif /* Portability stuff (DMP) */ @@ -269,8 +276,7 @@ struct command_tx_struct { * */ struct aim_oft_session_t { - FILE *listing; - char *listingdir; + }; /* @@ -334,7 +340,7 @@ struct aim_session_t { */ struct aim_snac_t *snac_hash[FAIM_SNAC_HASH_SIZE]; faim_mutex_t snac_hash_locks[FAIM_SNAC_HASH_SIZE]; - u_long snac_nextid; + unsigned long snac_nextid; struct { char server[128]; @@ -382,6 +388,8 @@ struct aim_userinfo_s { #define AIM_FLAG_ALLUSERS 0x001f + +#ifdef FAIM_INTERNAL /* * TLV handling */ @@ -412,7 +420,7 @@ faim_internal unsigned long aim_gettlv32(struct aim_tlvlist_t *list, unsigned sh faim_internal int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv); faim_internal struct aim_tlv_t *aim_createtlv(void); faim_internal int aim_freetlv(struct aim_tlv_t **oldtlv); -faim_export int aim_puttlv_8(unsigned char *buf, unsigned short t, unsigned char v); +faim_internal int aim_puttlv_8(unsigned char *buf, unsigned short t, unsigned char v); faim_internal int aim_puttlv_16(u_char *, u_short, u_short); faim_internal int aim_puttlv_32(u_char *, u_short, u_long); faim_internal int aim_puttlv_str(u_char *buf, u_short t, int l, char *v); @@ -423,6 +431,7 @@ faim_internal int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned sh faim_internal int aim_addtlvtochain_caps(struct aim_tlvlist_t **list, unsigned short type, unsigned short caps); faim_internal int aim_addtlvtochain_noval(struct aim_tlvlist_t **list, unsigned short type); faim_internal int aim_counttlvchain(struct aim_tlvlist_t **list); +#endif /* FAIM_INTERNAL */ /* * Get command from connections / Dispatch commands @@ -433,21 +442,20 @@ int aim_rxdispatch(struct aim_session_t *); faim_export unsigned long aim_debugconn_sendconnect(struct aim_session_t *sess, struct aim_conn_t *conn); -int aim_logoff(struct aim_session_t *); +faim_export int aim_logoff(struct aim_session_t *); +#if !defined(FAIM_INTERNAL) || defined(FAIM_INTERNAL_FT) +/* the library should never call aim_conn_kill except in aim_ft.c */ faim_export void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn); +#endif /* ndef FAIM_INTERNAL */ typedef int (*rxcallback_t)(struct aim_session_t *, struct command_rx_struct *, ...); -int aim_register_callbacks(rxcallback_t *); +#ifdef FAIM_INTERNAL faim_internal unsigned long aim_genericreq_n(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype); faim_internal unsigned long aim_genericreq_l(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_long *); faim_internal unsigned long aim_genericreq_s(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short subtype, u_short *); - -faim_internal struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr); -faim_export int aim_oft_registerlisting(struct aim_session_t *sess, FILE *file, char* listingdir); -faim_export int aim_getfile_send(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh); -faim_export int aim_getfile_send_chunk(struct aim_conn_t *conn, FILE *tosend, struct aim_fileheader_t *fh, int pos, int bufsize); +#endif /* aim_login.c */ faim_export int aim_sendconnack(struct aim_session_t *sess, struct aim_conn_t *conn); @@ -456,29 +464,30 @@ faim_export int aim_send_login (struct aim_session_t *, struct aim_conn_t *, cha faim_export unsigned long aim_sendauthresp(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn, int errorcode, char *errorurl, char *bosip, char *cookie, char *email, int regstatus); faim_export int aim_gencookie(unsigned char *buf); faim_export int aim_sendserverready(struct aim_session_t *sess, struct aim_conn_t *conn); -faim_internal int aim_authkeyparse(struct aim_session_t *sess, struct command_rx_struct *command); faim_export unsigned long aim_sendredirect(struct aim_session_t *sess, struct aim_conn_t *conn, unsigned short servid, char *ip, char *cookie); faim_export void aim_purge_rxqueue(struct aim_session_t *); + +#ifdef FAIM_INTERNAL +faim_internal int aim_authkeyparse(struct aim_session_t *sess, struct command_rx_struct *command); faim_internal void aim_rxqueue_cleanbyconn(struct aim_session_t *sess, struct aim_conn_t *conn); faim_internal int aim_recv(int fd, void *buf, size_t count); -int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *command, ...); -int aim_parse_missed_im(struct aim_session_t *, struct command_rx_struct *, ...); -int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...); +faim_internal int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *command, ...); faim_internal int aim_get_command_rendezvous(struct aim_session_t *sess, struct aim_conn_t *conn); +faim_internal int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx_struct *cur); +faim_internal unsigned int aim_get_next_txseqnum(struct aim_conn_t *); faim_internal struct command_tx_struct *aim_tx_new(unsigned char framing, int chan, struct aim_conn_t *conn, int datalen); faim_internal int aim_tx_enqueue__queuebased(struct aim_session_t *, struct command_tx_struct *); faim_internal int aim_tx_enqueue__immediate(struct aim_session_t *, struct command_tx_struct *); faim_internal int aim_tx_enqueue(struct aim_session_t *, struct command_tx_struct *); -faim_internal int aim_tx_sendframe(struct aim_session_t *sess, struct command_tx_struct *cur); -faim_internal unsigned int aim_get_next_txseqnum(struct aim_conn_t *); -faim_export int aim_tx_flushqueue(struct aim_session_t *); faim_internal int aim_tx_printqueue(struct aim_session_t *); -faim_export void aim_tx_purgequeue(struct aim_session_t *); faim_internal int aim_parse_hostonline(struct aim_session_t *sess, struct command_rx_struct *command, ...); faim_internal int aim_parse_hostversions(struct aim_session_t *sess, struct command_rx_struct *command, ...); +#endif /* FAIM_INTERNAL */ +faim_export int aim_tx_flushqueue(struct aim_session_t *); +faim_export void aim_tx_purgequeue(struct aim_session_t *); struct aim_rxcblist_t { u_short family; @@ -491,9 +500,13 @@ struct aim_rxcblist_t { faim_export int aim_conn_setlatency(struct aim_conn_t *conn, int newval); faim_export int aim_conn_addhandler(struct aim_session_t *, struct aim_conn_t *conn, u_short family, u_short type, rxcallback_t newhandler, u_short flags); -faim_internal rxcallback_t aim_callhandler(struct aim_conn_t *conn, u_short family, u_short type); faim_export int aim_clearhandlers(struct aim_conn_t *conn); +#ifdef FAIM_INTERNAL +faim_internal rxcallback_t aim_callhandler(struct aim_conn_t *conn, u_short family, u_short type); +#endif + +#ifdef FAIM_INTERNAL /* * Generic SNAC structure. Rarely if ever used. */ @@ -512,12 +525,15 @@ faim_internal unsigned long aim_cachesnac(struct aim_session_t *sess, const unsi faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *, u_long id); faim_internal int aim_cleansnacs(struct aim_session_t *, int maxage); faim_internal int aim_putsnac(u_char *, int, int, int, u_long); +#endif /* FAIM_INTERNAL */ - +#ifdef FAIM_INTERNAL faim_internal void aim_connrst(struct aim_session_t *); faim_internal struct aim_conn_t *aim_conn_getnext(struct aim_session_t *); +faim_internal struct aim_conn_t *aim_cloneconn(struct aim_session_t *sess, struct aim_conn_t *src); +#endif /* FAIM_INTERNAL */ + faim_export void aim_conn_close(struct aim_conn_t *deadconn); -faim_internal struct aim_conn_t *aim_getconn_type(struct aim_session_t *, int type); faim_export struct aim_conn_t *aim_newconn(struct aim_session_t *, int type, char *dest); faim_export int aim_conngetmaxfd(struct aim_session_t *); faim_export struct aim_conn_t *aim_select(struct aim_session_t *, struct timeval *, int *); @@ -527,6 +543,7 @@ faim_export int aim_conn_completeconnect(struct aim_session_t *sess, struct aim_ faim_export int aim_conn_isconnecting(struct aim_conn_t *conn); faim_export void aim_session_init(struct aim_session_t *, unsigned long flags); faim_export void aim_setupproxy(struct aim_session_t *sess, char *server, char *username, char *password); +faim_export struct aim_conn_t *aim_getconn_type(struct aim_session_t *, int type); /* aim_misc.c */ @@ -564,31 +581,38 @@ faim_export unsigned long aim_setdirectoryinfo(struct aim_session_t *sess, struc faim_export unsigned long aim_setuserinterests(struct aim_session_t *sess, struct aim_conn_t *conn, char *interest1, char *interest2, char *interest3, char *interest4, char *interest5, unsigned short privacy); faim_export unsigned long aim_icq_setstatus(struct aim_session_t *sess, struct aim_conn_t *conn, unsigned long status); -faim_internal struct aim_fileheader_t *aim_getlisting(FILE *); -faim_internal int aim_oft_buildheader(char *,struct aim_fileheader_t *); +faim_export struct aim_fileheader_t *aim_getlisting(FILE *); + +#ifdef FAIM_INTERNAL +faim_internal int aim_oft_buildheader(unsigned char *,struct aim_fileheader_t *); faim_internal int aim_listenestablish(u_short); faim_internal int aim_tx_destroy(struct command_tx_struct *); +#endif /* FAIM_INTERNAL */ /* aim_rxhandlers.c */ faim_export int aim_rxdispatch(struct aim_session_t *); + +#ifdef FAIM_INTERNAL faim_internal int aim_authparse(struct aim_session_t *, struct command_rx_struct *); faim_internal int aim_handleredirect_middle(struct aim_session_t *, struct command_rx_struct *, ...); faim_internal int aim_parse_unknown(struct aim_session_t *, struct command_rx_struct *, ...); -int aim_parse_last_bad(struct aim_session_t *, struct command_rx_struct *, ...); faim_internal int aim_parse_generalerrs(struct aim_session_t *, struct command_rx_struct *command, ...); faim_internal int aim_parsemotd_middle(struct aim_session_t *sess, struct command_rx_struct *command, ...); +#endif /* FAIM_INTERNAL */ #define AIM_RATE_CODE_CHANGE 0x0001 #define AIM_RATE_CODE_WARNING 0x0002 #define AIM_RATE_CODE_LIMIT 0x0003 #define AIM_RATE_CODE_CLEARLIMIT 0x0004 +faim_export unsigned long aim_ads_clientready(struct aim_session_t *sess, struct aim_conn_t *conn); +faim_export unsigned long aim_ads_requestads(struct aim_session_t *sess, struct aim_conn_t *conn); + +#ifdef FAIM_INTERNAL faim_internal int aim_parse_ratechange_middle(struct aim_session_t *sess, struct command_rx_struct *command); faim_internal int aim_parse_evilnotify_middle(struct aim_session_t *sess, struct command_rx_struct *command); faim_internal int aim_parse_msgack_middle(struct aim_session_t *sess, struct command_rx_struct *command); - -faim_export unsigned long aim_ads_clientready(struct aim_session_t *sess, struct aim_conn_t *conn); -faim_export unsigned long aim_ads_requestads(struct aim_session_t *sess, struct aim_conn_t *conn); +#endif /* FAIM_INTERNAL */ /* aim_im.c */ struct aim_directim_priv { @@ -597,47 +621,6 @@ struct aim_directim_priv { char ip[30]; }; -#define AIM_IMFLAGS_AWAY 0x01 /* mark as an autoreply */ -#define AIM_IMFLAGS_ACK 0x02 /* request a receipt notice */ - -faim_export unsigned long aim_send_im(struct aim_session_t *, struct aim_conn_t *, char *, u_int, char *); -faim_internal int aim_parse_incoming_im_middle(struct aim_session_t *, struct command_rx_struct *); -faim_internal int aim_parse_outgoing_im_middle(struct aim_session_t *, struct command_rx_struct *); -faim_export unsigned long aim_seticbmparam(struct aim_session_t *, struct aim_conn_t *conn); -faim_internal int aim_parse_msgerror_middle(struct aim_session_t *, struct command_rx_struct *); -faim_internal int aim_negchan_middle(struct aim_session_t *sess, struct command_rx_struct *command); -faim_internal int aim_parse_bosrights(struct aim_session_t *sess, struct command_rx_struct *command, ...); -faim_internal int aim_parse_missedcall(struct aim_session_t *sess, struct command_rx_struct *command); - -faim_export struct aim_conn_t * aim_directim_initiate(struct aim_session_t *, struct aim_conn_t *, struct aim_directim_priv *, char *); -faim_export int aim_send_im_direct(struct aim_session_t *, struct aim_conn_t *, char *); -faim_export struct aim_conn_t *aim_directim_connect(struct aim_session_t *, struct aim_conn_t *, struct aim_directim_priv *); - -/* aim_info.c */ -#define AIM_CAPS_BUDDYICON 0x01 -#define AIM_CAPS_VOICE 0x02 -#define AIM_CAPS_IMIMAGE 0x04 -#define AIM_CAPS_CHAT 0x08 -#define AIM_CAPS_GETFILE 0x10 -#define AIM_CAPS_SENDFILE 0x20 -#define AIM_CAPS_GAMES 0x40 -#define AIM_CAPS_SAVESTOCKS 0x80 - -extern u_char aim_caps[8][16]; -faim_internal unsigned short aim_getcap(unsigned char *capblock, int buflen); -faim_internal int aim_putcap(unsigned char *capblock, int buflen, u_short caps); - -#define AIM_GETINFO_GENERALINFO 0x00001 -#define AIM_GETINFO_AWAYMESSAGE 0x00003 - -struct aim_msgcookie_t { - unsigned char cookie[8]; - int type; - void *data; - time_t addtime; - struct aim_msgcookie_t *next; -}; - struct aim_fileheader_t { #if 0 char magic[4]; /* 0 */ @@ -681,6 +664,64 @@ struct aim_filetransfer_priv { struct aim_fileheader_t fh; }; + +#define AIM_IMFLAGS_AWAY 0x01 /* mark as an autoreply */ +#define AIM_IMFLAGS_ACK 0x02 /* request a receipt notice */ + +faim_export unsigned long aim_send_im(struct aim_session_t *, struct aim_conn_t *, char *, u_int, char *); +faim_export int aim_send_im_direct(struct aim_session_t *, struct aim_conn_t *, char *); +faim_export struct aim_conn_t * aim_directim_initiate(struct aim_session_t *, struct aim_conn_t *, struct aim_directim_priv *, char *destsn); +faim_export struct aim_conn_t *aim_directim_connect(struct aim_session_t *, struct aim_conn_t *, struct aim_directim_priv *); +faim_export unsigned long aim_seticbmparam(struct aim_session_t *, struct aim_conn_t *conn); + +#ifdef FAIM_INTERNAL +faim_internal int aim_parse_incoming_im_middle(struct aim_session_t *, struct command_rx_struct *); +faim_internal int aim_parse_outgoing_im_middle(struct aim_session_t *, struct command_rx_struct *); +faim_internal int aim_parse_msgerror_middle(struct aim_session_t *, struct command_rx_struct *); +faim_internal int aim_negchan_middle(struct aim_session_t *sess, struct command_rx_struct *command); +faim_internal int aim_parse_bosrights(struct aim_session_t *sess, struct command_rx_struct *command, ...); +faim_internal int aim_parse_missedcall(struct aim_session_t *sess, struct command_rx_struct *command); +#endif /* FAIM_INTERNAL */ + +faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess, struct aim_conn_t *conn, char *destsn); +faim_export int aim_oft_getfile_request(struct aim_session_t *sess, struct aim_conn_t *conn, const unsigned char *name, const int size); +faim_export int aim_oft_getfile_ack(struct aim_session_t *sess, struct aim_conn_t *conn); +faim_export int aim_oft_getfile_end(struct aim_session_t *sess, struct aim_conn_t *conn); + +/* aim_info.c */ +#define AIM_CAPS_BUDDYICON 0x01 +#define AIM_CAPS_VOICE 0x02 +#define AIM_CAPS_IMIMAGE 0x04 +#define AIM_CAPS_CHAT 0x08 +#define AIM_CAPS_GETFILE 0x10 +#define AIM_CAPS_SENDFILE 0x20 +#define AIM_CAPS_GAMES 0x40 +#define AIM_CAPS_SAVESTOCKS 0x80 + +#ifdef FAIM_INTERNAL +extern u_char aim_caps[8][16]; +faim_internal unsigned short aim_getcap(unsigned char *capblock, int buflen); +faim_internal int aim_putcap(unsigned char *capblock, int buflen, u_short caps); +#endif /* FAIM_INTERNAL */ + + +#define AIM_GETINFO_GENERALINFO 0x00001 +#define AIM_GETINFO_AWAYMESSAGE 0x00003 + +struct aim_msgcookie_t { + unsigned char cookie[8]; + int type; + void *data; + struct aim_msgcookie_t *next; +}; + +struct aim_invite_priv { + char *sn; + char *roomname; + int exchange; + int instance; +}; + #define AIM_COOKIETYPE_UNKNOWN 0x00 #define AIM_COOKIETYPE_ICBM 0x01 #define AIM_COOKIETYPE_ADS 0x02 @@ -688,6 +729,7 @@ struct aim_filetransfer_priv { #define AIM_COOKIETYPE_IM 0x04 #define AIM_COOKIETYPE_CHAT 0x05 #define AIM_COOKIETYPE_CHATNAV 0x06 +#define AIM_COOKIETYPE_INVITE 0x07 /* we'll move OFT up a bit to give breathing room. not like it really * matters. */ #define AIM_COOKIETYPE_OFTIM 0x10 @@ -697,12 +739,14 @@ struct aim_filetransfer_priv { #define AIM_COOKIETYPE_OFTIMAGE 0x14 #define AIM_COOKIETYPE_OFTICON 0x15 +#ifdef FAIM_INTERNAL faim_internal int aim_cachecookie(struct aim_session_t *sess, struct aim_msgcookie_t *cookie); -faim_export int aim_purgecookies(struct aim_session_t *sess, int maxage); faim_internal struct aim_msgcookie_t *aim_uncachecookie(struct aim_session_t *sess, unsigned char *cookie, int type); faim_internal struct aim_msgcookie_t *aim_mkcookie(unsigned char *, int, void *); -faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *, unsigned char *, int); +faim_internal struct aim_msgcookie_t *aim_checkcookie(struct aim_session_t *, const unsigned char *, const int); +faim_internal int aim_freecookie(struct aim_session_t *sess, struct aim_msgcookie_t *cookie); faim_internal int aim_msgcookie_gettype(int reqclass); +#endif /* FAIM_INTERNAL */ faim_export int aim_handlerendconnect(struct aim_session_t *sess, struct aim_conn_t *cur); @@ -710,18 +754,21 @@ faim_export int aim_handlerendconnect(struct aim_session_t *sess, struct aim_con #define AIM_TRANSFER_DENY_DECLINE 0x0001 #define AIM_TRANSFER_DENY_NOTACCEPTING 0x0002 faim_export unsigned long aim_denytransfer(struct aim_session_t *sess, struct aim_conn_t *conn, char *sender, char *cookie, unsigned short code); -faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn,char *cookie,char *ip, FILE *file, unsigned short rendid); - +faim_export struct aim_conn_t *aim_accepttransfer(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn, char *cookie, char *ip, unsigned short listingfiles, unsigned short listingtotsize, unsigned short listingsize, unsigned int listingchecksum, unsigned short rendid); -faim_export unsigned long aim_getinfo(struct aim_session_t *, struct aim_conn_t *, const char *, unsigned short); +#ifdef FAIM_INTERNAL faim_internal int aim_extractuserinfo(u_char *, struct aim_userinfo_s *); faim_internal int aim_parse_userinfo_middle(struct aim_session_t *, struct command_rx_struct *); faim_internal int aim_parse_oncoming_middle(struct aim_session_t *, struct command_rx_struct *); faim_internal int aim_parse_offgoing_middle(struct aim_session_t *, struct command_rx_struct *); faim_internal int aim_putuserinfo(u_char *buf, int buflen, struct aim_userinfo_s *info); +faim_internal int aim_parse_locateerr(struct aim_session_t *sess, struct command_rx_struct *command); +#endif /* FAIM_INTERNAL */ + +faim_export unsigned long aim_getinfo(struct aim_session_t *, struct aim_conn_t *, const char *, unsigned short); faim_export int aim_sendbuddyoncoming(struct aim_session_t *sess, struct aim_conn_t *conn, struct aim_userinfo_s *info); faim_export int aim_sendbuddyoffgoing(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn); -faim_internal int aim_parse_locateerr(struct aim_session_t *sess, struct command_rx_struct *command); + /* aim_auth.c */ faim_export int aim_auth_sendcookie(struct aim_session_t *, struct aim_conn_t *, u_char *); @@ -731,22 +778,34 @@ faim_export unsigned long aim_auth_changepasswd(struct aim_session_t *, struct a /* aim_buddylist.c */ faim_export unsigned long aim_add_buddy(struct aim_session_t *, struct aim_conn_t *, char *); faim_export unsigned long aim_remove_buddy(struct aim_session_t *, struct aim_conn_t *, char *); + +#ifdef FAIM_INTERNAL faim_internal int aim_parse_buddyrights(struct aim_session_t *sess, struct command_rx_struct *command, ...); +#endif /* FAIM_INTERNAL */ /* aim_search.c */ faim_export u_long aim_usersearch_address(struct aim_session_t *, struct aim_conn_t *, char *); +#ifdef FAIM_INTERNAL +faim_internal unsigned long aim_parse_searcherror(struct aim_session_t *, struct command_rx_struct *); +faim_internal unsigned long aim_parse_searchreply(struct aim_session_t *, struct command_rx_struct *); +#endif struct aim_chat_roominfo { u_short exchange; char *name; u_short instance; }; -faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo); -faim_internal int aim_chat_parse_infoupdate(struct aim_session_t *sess, struct command_rx_struct *command); -faim_internal int aim_chat_parse_joined(struct aim_session_t *sess, struct command_rx_struct *command); -faim_internal int aim_chat_parse_leave(struct aim_session_t *sess, struct command_rx_struct *command); -faim_internal int aim_chat_parse_incoming(struct aim_session_t *sess, struct command_rx_struct *command); + +struct aim_chat_exchangeinfo { + u_short number; + char *name; + char *charset1; + char *lang1; + char *charset2; + char *lang2; +}; + faim_export unsigned long aim_chat_send_im(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg); faim_export unsigned long aim_chat_join(struct aim_session_t *sess, struct aim_conn_t *conn, u_short exchange, const char *roomname); faim_export unsigned long aim_chat_clientready(struct aim_session_t *sess, struct aim_conn_t *conn); @@ -759,18 +818,18 @@ faim_export unsigned long aim_chatnav_clientready(struct aim_session_t *sess, st faim_export unsigned long aim_chat_invite(struct aim_session_t *sess, struct aim_conn_t *conn, char *sn, char *msg, u_short exchange, char *roomname, u_short instance); -struct aim_chat_exchangeinfo { - u_short number; - char *name; - char *charset1; - char *lang1; - char *charset2; - char *lang2; -}; -faim_internal int aim_chatnav_parse_info(struct aim_session_t *sess, struct command_rx_struct *command); faim_export u_long aim_chatnav_createroom(struct aim_session_t *sess, struct aim_conn_t *conn, char *name, u_short exchange); faim_export int aim_chat_leaveroom(struct aim_session_t *sess, char *name); +#ifdef FAIM_INTERNAL +faim_internal int aim_chat_readroominfo(u_char *buf, struct aim_chat_roominfo *outinfo); +faim_internal int aim_chat_parse_infoupdate(struct aim_session_t *sess, struct command_rx_struct *command); +faim_internal int aim_chat_parse_joined(struct aim_session_t *sess, struct command_rx_struct *command); +faim_internal int aim_chat_parse_leave(struct aim_session_t *sess, struct command_rx_struct *command); +faim_internal int aim_chat_parse_incoming(struct aim_session_t *sess, struct command_rx_struct *command); +faim_internal int aim_chatnav_parse_info(struct aim_session_t *sess, struct command_rx_struct *command); +#endif /* FAIM_INTERNAL */ + /* aim_util.c */ #ifdef AIMUTIL_USEMACROS /* @@ -818,7 +877,10 @@ faim_export char *aim_strsep(char **pp, const char *delim); faim_export char *aim_getbuilddate(void); faim_export char *aim_getbuildtime(void); faim_export char *aim_getbuildstring(void); + +#ifdef FAIM_INTERNAL faim_internal void faimdprintf(int dlevel, const char *format, ...); +#endif /* FAIM_INTERNAL */ #endif /* __AIM_H__ */ diff --git a/faim/aim_cbtypes.h b/faim/aim_cbtypes.h index a18be3f..1caa997 100644 --- a/faim/aim_cbtypes.h +++ b/faim/aim_cbtypes.h @@ -195,11 +195,14 @@ #define AIM_CB_OFT_DIRECTIMINITIATE 0x0005 #define AIM_CB_OFT_GETFILECONNECTREQ 0x0006 /* connect request -- actually an OSCAR CAP*/ +#define AIM_CB_OFT_GETFILELISTINGREQ 0x000e /* OFT listing.txt request */ #define AIM_CB_OFT_GETFILEFILEREQ 0x0007 /* recieved file request */ #define AIM_CB_OFT_GETFILEFILESEND 0x0008 /* recieved file request confirm -- send data */ #define AIM_CB_OFT_GETFILECOMPLETE 0x0009 /* recieved file send complete*/ #define AIM_CB_OFT_GETFILEINITIATE 0x000a /* request for file get acknowledge */ #define AIM_CB_OFT_GETFILEDISCONNECT 0x000b /* OFT connection disconnected.*/ +#define AIM_CB_OFT_GETFILELISTING 0x000c /* OFT listing.txt recieved.*/ +#define AIM_CB_OFT_GETFILERECIEVE 0x000d /* OFT file incoming.*/ #define AIM_CB_OFT_SENDFILEDISCONNECT 0x000c /* OFT connection disconnected.*/ diff --git a/faim/faimconfig.h b/faim/faimconfig.h index 5beb92f..f439f1e 100644 --- a/faim/faimconfig.h +++ b/faim/faimconfig.h @@ -20,25 +20,6 @@ */ #define debug 0 -/* - * Maximum number of connections the library can simultaneously - * handle per session structure. Five is fairly arbitrary. - * - * Normally, only one connection gets used at a time. However, if - * chat is used, its easily possible for several connections to be - * open simultaneously. - * - * Normal connection list looks like this: - * 1 -- used for authentication at login (closed after login) - * 1 -- used for BOS (general messaging) (stays open for entire session) - * 1 -- used for chatnav (room creation, etc) (opened at random) - * 1n -- used for n connected chat rooms (AOL limits to three) - * - * Default: 7 - * - */ -#define AIM_CONN_MAX 7 - /* * USE_SNAC_FOR_IMS is an old feature that allowed better * tracking of error messages by caching SNAC IDs of outgoing @@ -81,19 +62,29 @@ #define AIMUTIL_USEMACROS /* - * Select whether or not to use POSIX thread functionality. + * What type of synchronisation to use. * - * We don't actually use threads, but we do use the POSIX mutex + * We don't actually use threads, but can use the POSIX mutex * in order to maintain thread safety. You can use the fake locking - * if you really don't like pthreads or you don't have it. + * if you really don't like pthreads (which I don't) or if you don't + * have it. + * + * USEPTHREADS - Use POSIX mutecies + * USEFAKELOCKS - Use little stub spinners to help find locking bugs + * USENOPLOCKS - No-op out all synchro calls at compile time + * + * Default: use noplocks by default. + * + * !!!NOTE: Even with USEPTHREADS turned on, libfaim is not fully thread + * safe. It will still take some effort to add locking calls to + * the places that need them. In fact, this feature in general + * is in danger of being officially deprecated and removed from + * the code. * - * Default: defined on Linux, otherwise use fake locks. */ -#ifdef __linux__ -#define FAIM_USEPTHREADS -#else -#define FAIM_USEFAKELOCKS -#endif +#undef FAIM_USEPTHREADS +#undef FAIM_USEFAKELOCKS +#define FAIM_USENOPLOCKS /* * Size of the SNAC caching hash. diff --git a/utils/faimtest/faimtest.c b/utils/faimtest/faimtest.c index 04e3110..4e364d5 100644 --- a/utils/faimtest/faimtest.c +++ b/utils/faimtest/faimtest.c @@ -41,11 +41,41 @@ /* Current status: - */ #include +static char *dprintf_ctime(void) +{ + static char retbuf[64]; + struct tm *lt; + struct timeval tv; + struct timezone tz; + + gettimeofday(&tv, &tz); + lt = localtime((time_t *)&tv.tv_sec); + strftime(retbuf, 64, "%a %b %e %H:%M:%S %Z %Y", lt); + return retbuf; +} + +#define dprintf(x) { \ + printf("%s %s: " x, dprintf_ctime(), "faimtest"); \ + fflush(stdout); \ +} +#define dvprintf(x, y...) { \ + printf("%s %s: " x, dprintf_ctime(), "faimtest", y); \ + fflush(stdout); \ +} +#define dinlineprintf(x) { \ + printf(x); \ + fflush(stdout); \ +} +#define dvinlineprintf(x, y...) { \ + printf(x, y); \ + fflush(stdout); \ +} +#define dperror(x) dvprintf("%s: %s\n", x, strerror(errno)); + int faimtest_parse_oncoming(struct aim_session_t *, struct command_rx_struct *, ...); int faimtest_parse_offgoing(struct aim_session_t *, struct command_rx_struct *, ...); int faimtest_parse_login_phase3d_f(struct aim_session_t *, struct command_rx_struct *, ...); @@ -81,10 +111,16 @@ int faimtest_getfile_filereq(struct aim_session_t *sess, struct command_rx_struc int faimtest_getfile_filesend(struct aim_session_t *sess, struct command_rx_struct *command, ...); int faimtest_getfile_complete(struct aim_session_t *sess, struct command_rx_struct *command, ...); int faimtest_getfile_disconnect(struct aim_session_t *sess, struct command_rx_struct *command, ...); +int faimtest_getfile_initiate(struct aim_session_t *sess, struct command_rx_struct *command, ...); +int faimtest_getfile_listing(struct aim_session_t *sess, struct command_rx_struct *command, ...); +int faimtest_getfile_listingreq(struct aim_session_t *sess, struct command_rx_struct *command, ...); +int faimtest_getfile_recieve(struct aim_session_t *sess, struct command_rx_struct *command, ...); #endif int faimtest_parse_ratechange(struct aim_session_t *sess, struct command_rx_struct *command, ...); int faimtest_parse_evilnotify(struct aim_session_t *sess, struct command_rx_struct *command, ...); +int faimtest_parse_searcherror(struct aim_session_t *sess, struct command_rx_struct *command, ...); +int faimtest_parse_searchreply(struct aim_session_t *sess, struct command_rx_struct *command, ...); int faimtest_parse_msgerr(struct aim_session_t *sess, struct command_rx_struct *command, ...); int faimtest_parse_buddyrights(struct aim_session_t *sess, struct command_rx_struct *command, ...); int faimtest_parse_locerr(struct aim_session_t *sess, struct command_rx_struct *command, ...); @@ -119,21 +155,27 @@ static char *msgerrreasons[] = { static int msgerrreasonslen = 25; static char *screenname,*password,*server=NULL; +static char *ohcaptainmycaptain = NULL; static int connected = 0; +#ifdef FILESUPPORT +FILE *listingfile; +char *listingpath; +#endif + int faimtest_reportinterval(struct aim_session_t *sess, struct command_rx_struct *command, ...) { if (command->data) { - printf("aim: minimum report interval: %d (seconds?)\n", aimutil_get16(command->data+10)); + dvprintf("aim: minimum report interval: %d (seconds?)\n", aimutil_get16(command->data+10)); } else - printf("aim: NULL minimum report interval!\n"); + dprintf("aim: NULL minimum report interval!\n"); return 1; } int faimtest_flapversion(struct aim_session_t *sess, struct command_rx_struct *command, ...) { - printf("faimtest: using FLAP version %u\n", aimutil_get32(command->data)); + dvprintf("faimtest: using FLAP version %u\n", aimutil_get32(command->data)); #if 0 /* @@ -143,7 +185,7 @@ int faimtest_flapversion(struct aim_session_t *sess, struct command_rx_struct *c if (command->conn->type == AIM_CONN_TYPE_AUTH) { /* do NOT send a connack/flapversion, request_login will send it if needed */ aim_request_login(sess, command->conn, screenname); - printf("faimtest: login request sent\n"); + dprintf("faimtest: login request sent\n"); } #endif @@ -168,7 +210,7 @@ int faimtest_conncomplete(struct aim_session_t *sess, struct command_rx_struct * va_end(ap); if (conn) - printf("faimtest: connection on %d completed\n", conn->fd); + dvprintf("faimtest: connection on %d completed\n", conn->fd); return 1; } @@ -190,30 +232,18 @@ int initwsa(void) } #endif /* _WIN32 */ -int main(void) +int main(int argc, char **argv) { struct aim_session_t aimsess; struct aim_conn_t *authconn = NULL, *waitingconn = NULL; int keepgoing = 1; char *proxy, *proxyusername, *proxypass; - - char *listingpath; - + int i; int selstat = 0; -#ifdef FILESUPPORT - FILE *listingfile; -#endif - - if ( !(screenname = getenv("SCREENNAME")) || - !(password = getenv("PASSWORD"))) - { - printf("Must specify SCREENAME and PASSWORD in environment.\n"); - return -1; - } - + screenname = getenv("SCREENNAME"); + password = getenv("PASSWORD"); server = getenv("AUTHSERVER"); - proxy = getenv("SOCKSPROXY"); proxyusername = getenv("SOCKSNAME"); proxypass = getenv("SOCKSPASS"); @@ -222,9 +252,39 @@ int main(void) listingpath = getenv("LISTINGPATH"); #endif + while ((i = getopt(argc, argv, "u:p:a:U:P:A:l:c:h")) != EOF) { + switch (i) { + case 'u': screenname = optarg; break; + case 'p': password = optarg; break; + case 'a': server = optarg; break; + case 'U': proxyusername = optarg; break; + case 'P': proxypass = optarg; break; + case 'A': proxy = optarg; break; + case 'l': listingpath = optarg; break; + case 'c': ohcaptainmycaptain = optarg; break; + case 'h': + default: + usage: + printf("faimtest\n"); + printf(" Options: \n"); + printf(" -u name Screen name ($SCREENNAME)\n"); + printf(" -p passwd Password ($PASSWORD)\n"); + printf(" -a host:port Authorizer ($AUTHSERVER)\n"); + printf(" -U name Proxy user name ($SOCKSPROXY)\n"); + printf(" -P passwd Proxy password ($SOCKSNAME)\n"); + printf(" -A host:port Proxy host ($SOCKSPASS)\n"); + printf(" -l path Path to listing file ($LISTINGPATH)\n"); + printf(" -c name Screen name of owner\n"); + exit(0); + } + } + + if (!screenname || !password) + goto usage; + #ifdef _WIN32 if (initwsa() != 0) { - printf("faimtest: could not initialize windows sockets\n"); + dprintf("faimtest: could not initialize windows sockets\n"); return -1; } #endif /* _WIN32 */ @@ -236,17 +296,15 @@ int main(void) if(listingpath) { char *listingname; if(!(listingname = (char *)calloc(1, strlen(listingpath)+strlen("/listing.txt")))) { - perror("listingname calloc."); + dperror("listingname calloc"); exit(-1); } sprintf(listingname, "%s/listing.txt", listingpath); if( (listingfile = fopen(listingname, "r")) == NULL) { - printf("Couldn't open %s... bombing.\n", listingname); + dvprintf("Couldn't open %s... bombing.\n", listingname); exit(-1); } - aim_oft_registerlisting(&aimsess, listingfile, listingpath); - free(listingname); } #endif @@ -257,13 +315,14 @@ int main(void) authconn = aim_newconn(&aimsess, AIM_CONN_TYPE_AUTH, server?server:FAIM_LOGIN_SERVER); if (authconn == NULL) { - fprintf(stderr, "faimtest: internal connection error while in aim_login. bailing out.\n"); + dprintf("faimtest: internal connection error while in aim_login. bailing out.\n"); return -1; } else if (authconn->fd == -1) { - if (authconn->status & AIM_CONN_STATUS_RESOLVERR) - fprintf(stderr, "faimtest: could not resolve authorizer name\n"); - else if (authconn->status & AIM_CONN_STATUS_CONNERR) - fprintf(stderr, "faimtest: could not connect to authorizer\n"); + if (authconn->status & AIM_CONN_STATUS_RESOLVERR) { + dprintf("faimtest: could not resolve authorizer name\n"); + } else if (authconn->status & AIM_CONN_STATUS_CONNERR) { + dprintf("faimtest: could not connect to authorizer\n"); + } aim_conn_kill(&aimsess, &authconn); return -1; } @@ -277,7 +336,7 @@ int main(void) /* If the connection is in progress, this will just be queued */ aim_request_login(&aimsess, authconn, screenname); - printf("faimtest: login request sent\n"); + dprintf("faimtest: login request sent\n"); while (keepgoing) { waitingconn = aim_select(&aimsess, NULL, &selstat); @@ -297,34 +356,37 @@ int main(void) case 2: /* incoming data pending */ if (waitingconn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) { if (aim_handlerendconnect(&aimsess, waitingconn) < 0) { - printf("connection error (rend out)\n"); + dprintf("connection error (rend out)\n"); + aim_conn_kill(&aimsess, &waitingconn); } + break; + } + + if (aim_get_command(&aimsess, waitingconn) >= 0) { + aim_rxdispatch(&aimsess); } else { - if (aim_get_command(&aimsess, waitingconn) >= 0) { - aim_rxdispatch(&aimsess); - } else { - printf("connection error (type 0x%04x:0x%04x)\n", waitingconn->type, waitingconn->subtype); - if(waitingconn->type == AIM_CONN_TYPE_RENDEZVOUS) { - /* we should have callbacks for all these, else the library will do the conn_kill for us. */ - printf("connection error: rendezvous connection. you forgot register a disconnect callback, right?\n"); - } - else - aim_conn_kill(&aimsess, &waitingconn); - if (!aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS)) { - printf("major connection error\n"); - keepgoing = 0; - } + dvprintf("connection error (type 0x%04x:0x%04x)\n", waitingconn->type, waitingconn->subtype); + /* we should have callbacks for all these, else the library will do the conn_kill for us. */ + if(waitingconn->type == AIM_CONN_TYPE_RENDEZVOUS) { + dprintf("connection error: rendezvous connection. you forgot register a disconnect callback, right?\n"); + aim_conn_kill(&aimsess, &waitingconn); + } else + aim_conn_kill(&aimsess, &waitingconn); + if (!aim_getconn_type(&aimsess, AIM_CONN_TYPE_BOS)) { + dprintf("major connection error\n"); + keepgoing = 0; } } - break; + break; + default: break; /* invalid */ } } /* Close up */ - printf("AIM just decided we didn't need to be here anymore, closing up...\n"); + dprintf("AIM just decided we didn't need to be here anymore, closing up...\n"); /* close up all connections, dead or no */ aim_logoff(&aimsess); @@ -339,9 +401,13 @@ int faimtest_rateresp(struct aim_session_t *sess, struct command_rx_struct *comm switch(command->conn->type) { case AIM_CONN_TYPE_BOS: { /* this is the new buddy list */ - char buddies[] = "Buddy1&Buddy2&ThisHereIsAName2&midendian&ewarmenhoven&"; + char buddies[128]; /* this is the new profile */ - char profile[] = "Hello"; + char profile[256]; + + /* Caution: Buddy1 and Buddy2 are real people! (who I don't know) */ + snprintf(buddies, sizeof(buddies), "Buddy1&Buddy2&%s&", ohcaptainmycaptain?ohcaptainmycaptain:"blah"); + snprintf(profile, sizeof(profile), "Hello.
My captain is %s. They were dumb enough to leave this message in their client, or they are using faimtest. Shame on them.", ohcaptainmycaptain); aim_bos_ackrateresp(sess, command->conn); /* ack rate info response */ aim_bos_reqpersonalinfo(sess, command->conn); @@ -365,7 +431,7 @@ int faimtest_rateresp(struct aim_session_t *sess, struct command_rx_struct *comm } default: - printf("faimtest: got rate response for unhandled connection type %04x\n", command->conn->type); + dvprintf("faimtest: got rate response for unhandled connection type %04x\n", command->conn->type); break; } @@ -383,12 +449,12 @@ int faimtest_hostversions(struct aim_session_t *sess, struct command_rx_struct * versions = va_arg(ap, unsigned char *); va_end(ap); - printf("faimtest: SNAC versions supported by this host: "); + dprintf("faimtest: SNAC versions supported by this host: "); for (i = 0; i < vercount*4; i += 4) - printf("0x%04x:0x%04x ", - aimutil_get16(versions+i), /* SNAC family */ - aimutil_get16(versions+i+2) /* Version number */); - printf("\n"); + dvinlineprintf("0x%04x:0x%04x ", + aimutil_get16(versions+i), /* SNAC family */ + aimutil_get16(versions+i+2) /* Version number */); + dinlineprintf("\n"); return 1; } @@ -404,10 +470,10 @@ int faimtest_serverready(struct aim_session_t *sess, struct command_rx_struct *c families = va_arg(ap, unsigned short *); va_end(ap); - printf("faimtest: SNAC families supported by this host (type %d): ", command->conn->type); + dvprintf("faimtest: SNAC families supported by this host (type %d): ", command->conn->type); for (i = 0; i < famcount; i++) - printf("0x%04x ", families[i]); - printf("\n"); + dvinlineprintf("0x%04x ", families[i]); + dinlineprintf("\n"); switch (command->conn->type) { case AIM_CONN_TYPE_BOS: @@ -415,11 +481,11 @@ int faimtest_serverready(struct aim_session_t *sess, struct command_rx_struct *c aim_setversions(sess, command->conn); aim_bos_reqrate(sess, command->conn); /* request rate info */ - fprintf(stderr, "faimtest: done with BOS ServerReady\n"); + dprintf("faimtest: done with BOS ServerReady\n"); break; case AIM_CONN_TYPE_CHATNAV: - fprintf(stderr, "faimtest: chatnav: got server ready\n"); + dprintf("faimtest: chatnav: got server ready\n"); aim_conn_addhandler(sess, command->conn, AIM_CB_FAM_CTN, AIM_CB_CTN_INFO, faimtest_chatnav_info, 0); aim_bos_reqrate(sess, command->conn); aim_bos_ackrateresp(sess, command->conn); @@ -442,7 +508,7 @@ int faimtest_serverready(struct aim_session_t *sess, struct command_rx_struct *c break; default: - fprintf(stderr, "faimtest: unknown connection type on Server Ready\n"); + dprintf("faimtest: unknown connection type on Server Ready\n"); } return 1; @@ -458,7 +524,7 @@ int faimtest_parse_buddyrights(struct aim_session_t *sess, struct command_rx_str maxwatchers = va_arg(ap, int); va_end(ap); - printf("faimtest: buddy list rights: Max buddies = %d / Max watchers = %d\n", maxbuddies, maxwatchers); + dvprintf("faimtest: buddy list rights: Max buddies = %d / Max watchers = %d\n", maxbuddies, maxwatchers); return 1; } @@ -473,11 +539,29 @@ int faimtest_bosrights(struct aim_session_t *sess, struct command_rx_struct *com maxdenies = va_arg(ap, int); va_end(ap); - printf("faimtest: BOS rights: Max permit = %d / Max deny = %d\n", maxpermits, maxdenies); + dvprintf("faimtest: BOS rights: Max permit = %d / Max deny = %d\n", maxpermits, maxdenies); aim_bos_clientready(sess, command->conn); - printf("faimtest: officially connected to BOS.\n"); + dprintf("faimtest: officially connected to BOS.\n"); + + return 1; +} + +int faimtest_parse_unknown(struct aim_session_t *sess, struct command_rx_struct *command, ...) +{ + int i = 0; + + if (!sess || !command) + return 1; + + dprintf("\nRecieved unknown packet:"); + for (i = 0; i < command->commandlen; i++) { + if ((i % 8) == 0) + dinlineprintf("\n\t"); + dvinlineprintf("0x%2x ", command->data[i]); + } + dinlineprintf("\n\n"); return 1; } @@ -516,9 +600,9 @@ int faimtest_handleredirect(struct aim_session_t *sess, struct command_rx_struct struct aim_conn_t *tstconn; /* Open a connection to the Auth */ tstconn = aim_newconn(sess, AIM_CONN_TYPE_AUTH, ip); - if ( (tstconn==NULL) || (tstconn->status & AIM_CONN_STATUS_RESOLVERR) ) - fprintf(stderr, "faimtest: unable to reconnect with authorizer\n"); - else { + if ((tstconn==NULL) || (tstconn->status & AIM_CONN_STATUS_RESOLVERR)) { + dprintf("faimtest: unable to reconnect with authorizer\n"); + } else { aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_FLAPVER, faimtest_flapversion, 0); aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE, faimtest_conncomplete, 0); /* Send the cookie to the Auth */ @@ -532,18 +616,18 @@ int faimtest_handleredirect(struct aim_session_t *sess, struct command_rx_struct struct aim_conn_t *tstconn = NULL; tstconn = aim_newconn(sess, AIM_CONN_TYPE_CHATNAV, ip); if ( (tstconn==NULL) || (tstconn->status & AIM_CONN_STATUS_RESOLVERR)) { - fprintf(stderr, "faimtest: unable to connect to chatnav server\n"); + dprintf("faimtest: unable to connect to chatnav server\n"); if (tstconn) aim_conn_kill(sess, &tstconn); return 1; } #if 0 - aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_CTN, AIM_CB_SPECIAL_DEFAULT, aim_parse_unknown, 0); - aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_GEN, AIM_CB_SPECIAL_DEFAULT, aim_parse_unknown, 0); + aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_CTN, AIM_CB_SPECIAL_DEFAULT, faimtest_parse_unknown, 0); + aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_GEN, AIM_CB_SPECIAL_DEFAULT, faimtest_parse_unknown, 0); #endif aim_conn_addhandler(sess, tstconn, 0x0001, 0x0003, faimtest_serverready, 0); aim_conn_addhandler(sess, tstconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE, faimtest_conncomplete, 0); aim_auth_sendcookie(sess, tstconn, cookie); - fprintf(stderr, "\achatnav: connected\n"); + dprintf("\achatnav: connected\n"); } break; case 0x000e: /* Chat */ @@ -558,11 +642,11 @@ int faimtest_handleredirect(struct aim_session_t *sess, struct command_rx_struct tstconn = aim_newconn(sess, AIM_CONN_TYPE_CHAT, ip); if ( (tstconn==NULL) || (tstconn->status & AIM_CONN_STATUS_RESOLVERR)) { - fprintf(stderr, "faimtest: unable to connect to chat server\n"); + dprintf("faimtest: unable to connect to chat server\n"); if (tstconn) aim_conn_kill(sess, &tstconn); return 1; } - printf("faimtest: chat: connected to %s on exchange %d\n", roomname, exchange); + dvprintf("faimtest: chat: connected to %s on exchange %d\n", roomname, exchange); /* * We must do this to attach the stored name to the connection! @@ -575,7 +659,7 @@ int faimtest_handleredirect(struct aim_session_t *sess, struct command_rx_struct } break; default: - printf("uh oh... got redirect for unknown service 0x%04x!!\n", serviceid); + dvprintf("uh oh... got redirect for unknown service 0x%04x!!\n", serviceid); /* dunno */ } @@ -617,35 +701,35 @@ int faimtest_parse_authresp(struct aim_session_t *sess, struct command_rx_struct va_end(ap); - printf("Screen name: %s\n", sn); + dvprintf("Screen name: %s\n", sn); /* * Check for error. */ if (errorcode || !bosip || !cookie) { - printf("Login Error Code 0x%04x\n", errorcode); - printf("Error URL: %s\n", errurl); + dvprintf("Login Error Code 0x%04x\n", errorcode); + dvprintf("Error URL: %s\n", errurl); aim_conn_kill(sess, &command->conn); return 1; } - printf("Reg status: %2d\n", regstatus); - printf("Email: %s\n", email); - printf("BOS IP: %s\n", bosip); + dvprintf("Reg status: %2d\n", regstatus); + dvprintf("Email: %s\n", email); + dvprintf("BOS IP: %s\n", bosip); if (latestbeta) - printf("Latest beta version: %s, build %d, at %s (more info at %s)\n", latestbeta, latestbetabuild, latestbetaurl, latestbetainfo); + dvprintf("Latest beta version: %s, build %d, at %s (more info at %s)\n", latestbeta, latestbetabuild, latestbetaurl, latestbetainfo); if (latestrelease) - printf("Latest released version: %s, build %d, at %s (more info at %s)\n", latestrelease, latestbuild, latestreleaseurl, latestreleaseinfo); + dvprintf("Latest released version: %s, build %d, at %s (more info at %s)\n", latestrelease, latestbuild, latestreleaseurl, latestreleaseinfo); - printf("Closing auth connection...\n"); + dprintf("Closing auth connection...\n"); aim_conn_kill(sess, &command->conn); if (!(bosconn = aim_newconn(sess, AIM_CONN_TYPE_BOS, bosip))) { - fprintf(stderr, "faimtest: could not connect to BOS: internal error\n"); + dprintf("faimtest: could not connect to BOS: internal error\n"); return 1; } else if (bosconn->status & AIM_CONN_STATUS_CONNERR) { - fprintf(stderr, "faimtest: could not connect to BOS\n"); + dprintf("faimtest: could not connect to BOS\n"); aim_conn_kill(sess, &bosconn); return 1; } @@ -667,12 +751,14 @@ int faimtest_parse_authresp(struct aim_session_t *sess, struct command_rx_struct aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_MISSEDCALL, faimtest_parse_misses, 0); aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_RATECHANGE, faimtest_parse_ratechange, 0); aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_EVIL, faimtest_parse_evilnotify, 0); + aim_conn_addhandler(sess, bosconn, 0x000a, 0x0001, faimtest_parse_searcherror, 0); + aim_conn_addhandler(sess, bosconn, 0x000a, 0x0003, faimtest_parse_searchreply, 0); aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_ERROR, faimtest_parse_msgerr, 0); aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_LOC, AIM_CB_LOC_USERINFO, faimtest_parse_userinfo, 0); aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_MSG, AIM_CB_MSG_ACK, faimtest_parse_msgack, 0); - aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_CTN, AIM_CB_CTN_DEFAULT, aim_parse_unknown, 0); - aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_DEFAULT, aim_parse_unknown, 0); + aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_CTN, AIM_CB_CTN_DEFAULT, faimtest_parse_unknown, 0); + aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_DEFAULT, faimtest_parse_unknown, 0); aim_conn_addhandler(sess, bosconn, AIM_CB_FAM_GEN, AIM_CB_GEN_MOTD, faimtest_parse_motd, 0); aim_conn_addhandler(sess, bosconn, 0x0001, 0x0001, faimtest_parse_genericerr, 0); @@ -690,21 +776,21 @@ int faimtest_parse_authresp(struct aim_session_t *sess, struct command_rx_struct static void printuserflags(unsigned short flags) { if (flags & AIM_FLAG_UNCONFIRMED) - printf("UNCONFIRMED "); + dinlineprintf("UNCONFIRMED "); if (flags & AIM_FLAG_ADMINISTRATOR) - printf("ADMINISTRATOR "); + dinlineprintf("ADMINISTRATOR "); if (flags & AIM_FLAG_AOL) - printf("AOL "); + dinlineprintf("AOL "); if (flags & AIM_FLAG_OSCAR_PAY) - printf("OSCAR_PAY "); + dinlineprintf("OSCAR_PAY "); if (flags & AIM_FLAG_FREE) - printf("FREE "); + dinlineprintf("FREE "); if (flags & AIM_FLAG_AWAY) - printf("AWAY "); + dinlineprintf("AWAY "); if (flags & AIM_FLAG_UNKNOWN40) - printf("ICQ? "); + dinlineprintf("ICQ? "); if (flags & AIM_FLAG_UNKNOWN80) - printf("UNKNOWN80 "); + dinlineprintf("UNKNOWN80 "); return; } @@ -723,24 +809,24 @@ int faimtest_parse_userinfo(struct aim_session_t *sess, struct command_rx_struct inforeq = va_arg(ap, int); va_end(ap); - printf("faimtest: userinfo: sn: %s\n", userinfo->sn); - printf("faimtest: userinfo: warnlevel: 0x%04x\n", userinfo->warnlevel); - printf("faimtest: userinfo: flags: 0x%04x = ", userinfo->flags); + dvprintf("faimtest: userinfo: sn: %s\n", userinfo->sn); + dvprintf("faimtest: userinfo: warnlevel: 0x%04x\n", userinfo->warnlevel); + dvprintf("faimtest: userinfo: flags: 0x%04x = ", userinfo->flags); printuserflags(userinfo->flags); - printf("\n"); + dinlineprintf("\n"); - printf("faimtest: userinfo: membersince: %lu\n", userinfo->membersince); - printf("faimtest: userinfo: onlinesince: %lu\n", userinfo->onlinesince); - printf("faimtest: userinfo: idletime: 0x%04x\n", userinfo->idletime); + dvprintf("faimtest: userinfo: membersince: %lu\n", userinfo->membersince); + dvprintf("faimtest: userinfo: onlinesince: %lu\n", userinfo->onlinesince); + dvprintf("faimtest: userinfo: idletime: 0x%04x\n", userinfo->idletime); if (inforeq == AIM_GETINFO_GENERALINFO) { - printf("faimtest: userinfo: profile_encoding: %s\n", prof_encoding ? prof_encoding : "[none]"); - printf("faimtest: userinfo: prof: %s\n", prof ? prof : "[none]"); + dvprintf("faimtest: userinfo: profile_encoding: %s\n", prof_encoding ? prof_encoding : "[none]"); + dvprintf("faimtest: userinfo: prof: %s\n", prof ? prof : "[none]"); } else if (inforeq == AIM_GETINFO_AWAYMESSAGE) { - printf("faimtest: userinfo: awaymsg_encoding: %s\n", prof_encoding ? prof_encoding : "[none]"); - printf("faimtest: userinfo: awaymsg: %s\n", prof ? prof : "[none]"); + dvprintf("faimtest: userinfo: awaymsg_encoding: %s\n", prof_encoding ? prof_encoding : "[none]"); + dvprintf("faimtest: userinfo: awaymsg: %s\n", prof ? prof : "[none]"); } else - printf("faimtest: userinfo: unknown info request\n"); + dprintf("faimtest: userinfo: unknown info request\n"); return 1; } @@ -785,27 +871,27 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str flag2 = va_arg(ap, int); va_end(ap); - printf("faimtest: icbm: sn = \"%s\"\n", userinfo->sn); - printf("faimtest: icbm: warnlevel = 0x%04x\n", userinfo->warnlevel); - printf("faimtest: icbm: flags = 0x%04x = ", userinfo->flags); + dvprintf("faimtest: icbm: sn = \"%s\"\n", userinfo->sn); + dvprintf("faimtest: icbm: warnlevel = 0x%04x\n", userinfo->warnlevel); + dvprintf("faimtest: icbm: flags = 0x%04x = ", userinfo->flags); printuserflags(userinfo->flags); - printf("\n"); + dinlineprintf("\n"); - printf("faimtest: icbm: membersince = %lu\n", userinfo->membersince); - printf("faimtest: icbm: onlinesince = %lu\n", userinfo->onlinesince); - printf("faimtest: icbm: idletime = 0x%04x\n", userinfo->idletime); - printf("faimtest: icbm: capabilities = 0x%04x\n", userinfo->capabilities); + dvprintf("faimtest: icbm: membersince = %lu\n", userinfo->membersince); + dvprintf("faimtest: icbm: onlinesince = %lu\n", userinfo->onlinesince); + dvprintf("faimtest: icbm: idletime = 0x%04x\n", userinfo->idletime); + dvprintf("faimtest: icbm: capabilities = 0x%04x\n", userinfo->capabilities); - printf("faimtest: icbm: icbmflags = "); + dprintf("faimtest: icbm: icbmflags = "); if (icbmflags & AIM_IMFLAGS_AWAY) - printf("away "); + dinlineprintf("away "); if (icbmflags & AIM_IMFLAGS_ACK) - printf("ackrequest "); - printf("\n"); + dinlineprintf("ackrequest "); + dinlineprintf("\n"); - printf("faimtest: icbm: encoding flags = {%04x, %04x}\n", flag1, flag2); + dvprintf("faimtest: icbm: encoding flags = {%04x, %04x}\n", flag1, flag2); - printf("faimtest: icbm: message: %s\n", msg); + dvprintf("faimtest: icbm: message: %s\n", msg); if (msg) { int i = 0; @@ -819,30 +905,39 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str } tmpstr = msg+i; - printf("tmpstr = %s\n", tmpstr); + dvprintf("tmpstr = %s\n", tmpstr); if ( (strlen(tmpstr) >= 10) && (!strncmp(tmpstr, "disconnect", 10)) ) { - aim_send_im(sess, command->conn, "midendian", 0, "ta ta..."); - aim_logoff(sess); + if (ohcaptainmycaptain) + aim_send_im(sess, command->conn, ohcaptainmycaptain, 0, "ta ta..."); + aim_logoff(sess); } else if (strstr(tmpstr, "goodday")) { - printf("faimtest: icbm: sending response\n"); + dprintf("faimtest: icbm: sending response\n"); aim_send_im(sess, command->conn, userinfo->sn, AIM_IMFLAGS_ACK, "Good day to you too."); } else if (strstr(tmpstr, "warnme")) { - printf("faimtest: icbm: sending non-anon warning\n"); + dprintf("faimtest: icbm: sending non-anon warning\n"); aim_send_warning(sess, command->conn, userinfo->sn, 0); } else if (strstr(tmpstr, "anonwarn")) { - printf("faimtest: icbm: sending anon warning\n"); + dprintf("faimtest: icbm: sending anon warning\n"); aim_send_warning(sess, command->conn, userinfo->sn, AIM_WARN_ANON); } else if (strstr(tmpstr, "setdirectoryinfo")) { - printf("faimtest: icbm: sending backwards profile data\n"); + dprintf("faimtest: icbm: sending backwards profile data\n"); aim_setdirectoryinfo(sess, command->conn, "tsrif", "elddim", "tsal", "nediam", "emankcin", "teerts", "ytic", "etats", "piz", 0, 1); } else if (strstr(tmpstr, "setinterests")) { - printf("faimtest: icbm: setting fun interests\n"); + dprintf("faimtest: icbm: setting fun interests\n"); aim_setuserinterests(sess, command->conn, "interest1", "interest2", "interest3", "interest4", "interest5", 1); + } else if (!strncmp(tmpstr, "getfile", 7)) { + if (!ohcaptainmycaptain) { + aim_send_im(sess, command->conn, userinfo->sn, AIM_IMFLAGS_ACK, "I have no owner!"); + } else { + struct aim_conn_t *newconn; + newconn = aim_getfile_initiate(sess, command->conn, (strlen(tmpstr) < 8)?ohcaptainmycaptain:tmpstr+8); + dvprintf("faimtest: getting file listing from %s\n", (strlen(tmpstr) < 8)?ohcaptainmycaptain:tmpstr+8); + aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE, faimtest_getfile_initiate,0); + } } else if (!strncmp(tmpstr, "open chatnav", 12)) { aim_bos_reqservice(sess, command->conn, AIM_CONN_TYPE_CHATNAV); - //aim_chat_join(sess, command->conn, "thishereisaname2_chat85"); } else if (!strncmp(tmpstr, "create", 6)) { aim_chatnav_createroom(sess,aim_getconn_type(sess, AIM_CONN_TYPE_CHATNAV), (strlen(tmpstr) < 7)?"WorldDomination":tmpstr+7, 0x0004); } else if (!strncmp(tmpstr, "close chatnav", 13)) { @@ -859,12 +954,10 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str } else if (!strncmp(tmpstr, "open directim", 13)) { struct aim_conn_t *newconn; newconn = aim_directim_initiate(sess, command->conn, NULL, userinfo->sn); - } else if (!strncmp(tmpstr, "openauth", 8)) { - aim_bos_reqservice(sess, command->conn, AIM_CONN_TYPE_AUTH); - } else if (!strncmp(tmpstr, "auth", 4)) { - aim_genericreq_n(sess, aim_getconn_type(sess, AIM_CONN_TYPE_AUTH), 0x0007, 0x0002); + } else if(!(strncmp(tmpstr, "lookup", 6))) { + aim_usersearch_address(sess, command->conn, tmpstr+7); } else if (!strncmp(tmpstr, "reqsendmsg", 10)) { - aim_send_im(sess, command->conn, "vaxherder", 0, "sendmsg 7900"); + aim_send_im(sess, command->conn, ohcaptainmycaptain, 0, "sendmsg 7900"); } else if (!strncmp(tmpstr, "sendmsg", 7)) { int i; i = atoi(tmpstr+8); @@ -881,7 +974,7 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str free(newbuf); } } else { - printf("unknown command.\n"); + dprintf("unknown command.\n"); aim_add_buddy(sess, command->conn, userinfo->sn); } @@ -900,15 +993,15 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str userinfo = va_arg(ap, struct aim_userinfo_s *); va_end(ap); - printf("faimtest: voice invitation: source sn = %s\n", userinfo->sn); - printf("faimtest: voice invitation: \twarnlevel = 0x%04x\n", userinfo->warnlevel); - printf("faimtest: voice invitation: \tclass = 0x%04x = ", userinfo->flags); + dvprintf("faimtest: voice invitation: source sn = %s\n", userinfo->sn); + dvprintf("faimtest: voice invitation: \twarnlevel = 0x%04x\n", userinfo->warnlevel); + dvprintf("faimtest: voice invitation: \tclass = 0x%04x = ", userinfo->flags); printuserflags(userinfo->flags); - printf("\n"); + dinlineprintf("\n"); /* we dont get membersince on chat invites! */ - printf("faimtest: voice invitation: \tonlinesince = %lu\n", userinfo->onlinesince); - printf("faimtest: voice invitation: \tidletime = 0x%04x\n", userinfo->idletime); + dvprintf("faimtest: voice invitation: \tonlinesince = %lu\n", userinfo->onlinesince); + dvprintf("faimtest: voice invitation: \tidletime = 0x%04x\n", userinfo->idletime); break; } @@ -916,33 +1009,38 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str #ifdef FILESUPPORT char *ip, *cookie; struct aim_conn_t *newconn; + struct aim_fileheader_t *fh; userinfo = va_arg(ap, struct aim_userinfo_s *); ip = va_arg(ap, char *); cookie = va_arg(ap, char *); va_end(ap); - printf("faimtest: get file request from %s (at %s)\n", userinfo->sn, ip); + dvprintf("faimtest: get file request from %s (at %s)\n", userinfo->sn, ip); - sleep(1); + fh = aim_getlisting(listingfile); - if( (newconn = aim_accepttransfer(sess, command->conn, userinfo->sn, cookie, ip, sess->oft.listing, reqclass)) == NULL ) { - printf("faimtest: getfile: requestconn: apparent error in accepttransfer\n"); + if( (newconn = aim_accepttransfer(sess, command->conn, userinfo->sn, cookie, ip, fh->totfiles, fh->totsize, fh->size, fh->checksum, reqclass)) == NULL ) { + dprintf("faimtest: getfile: requestconn: apparent error in accepttransfer\n"); break; } - + + free(fh); + + aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ, faimtest_getfile_listingreq, 0); aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ, faimtest_getfile_filereq, 0); aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND, faimtest_getfile_filesend, 0); aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE, faimtest_getfile_complete, 0); + aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT, faimtest_getfile_disconnect, 0); - printf("faimtest: getfile connect succeeded, handlers added.\n"); + dprintf("faimtest: getfile connect succeeded, handlers added.\n"); break; #endif } case AIM_CAPS_SENDFILE: { - printf("faimtest: send file!\n"); + dprintf("faimtest: send file!\n"); break; } case AIM_CAPS_CHAT: { @@ -956,23 +1054,23 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str lang = va_arg(ap, char *); va_end(ap); - printf("faimtest: chat invitation: source sn = %s\n", userinfo->sn); - printf("faimtest: chat invitation: \twarnlevel = 0x%04x\n", userinfo->warnlevel); - printf("faimtest: chat invitation: \tclass = 0x%04x = ", userinfo->flags); + dvprintf("faimtest: chat invitation: source sn = %s\n", userinfo->sn); + dvprintf("faimtest: chat invitation: \twarnlevel = 0x%04x\n", userinfo->warnlevel); + dvprintf("faimtest: chat invitation: \tclass = 0x%04x = ", userinfo->flags); printuserflags(userinfo->flags); - printf("\n"); + dinlineprintf("\n"); /* we dont get membersince on chat invites! */ - printf("faimtest: chat invitation: \tonlinesince = %lu\n", userinfo->onlinesince); - printf("faimtest: chat invitation: \tidletime = 0x%04x\n", userinfo->idletime); + dvprintf("faimtest: chat invitation: \tonlinesince = %lu\n", userinfo->onlinesince); + dvprintf("faimtest: chat invitation: \tidletime = 0x%04x\n", userinfo->idletime); - printf("faimtest: chat invitation: message = %s\n", msg); - printf("faimtest: chat invitation: room name = %s\n", roominfo->name); - printf("faimtest: chat invitation: encoding = %s\n", encoding); - printf("faimtest: chat invitation: language = %s\n", lang); - printf("faimtest: chat invitation: exchange = 0x%04x\n", roominfo->exchange); - printf("faimtest: chat invitation: instance = 0x%04x\n", roominfo->instance); - printf("faimtest: chat invitiation: autojoining %s...\n", roominfo->name); + dvprintf("faimtest: chat invitation: message = %s\n", msg); + dvprintf("faimtest: chat invitation: room name = %s\n", roominfo->name); + dvprintf("faimtest: chat invitation: encoding = %s\n", encoding); + dvprintf("faimtest: chat invitation: language = %s\n", lang); + dvprintf("faimtest: chat invitation: exchange = 0x%04x\n", roominfo->exchange); + dvprintf("faimtest: chat invitation: instance = 0x%04x\n", roominfo->instance); + dvprintf("faimtest: chat invitiation: autojoining %s...\n", roominfo->name); /* * Automatically join room... */ @@ -983,34 +1081,34 @@ int faimtest_parse_incoming_im(struct aim_session_t *sess, struct command_rx_str struct aim_directim_priv *priv; struct aim_conn_t *newconn; - printf("faimtest: icbm: rendezvous imimage\n"); + dprintf("faimtest: icbm: rendezvous imimage\n"); userinfo = va_arg(ap, struct aim_userinfo_s *); priv = va_arg(ap, struct aim_directim_priv *); va_end(ap); - printf("faimtest: OFT: DirectIM: request from %s (%s)\n", userinfo->sn, priv->ip); + dvprintf("faimtest: OFT: DirectIM: request from %s (%s)\n", userinfo->sn, priv->ip); if (!(newconn = aim_directim_connect(sess, command->conn, priv))) { - printf("faimtest: icbm: imimage: could not connect\n"); + dprintf("faimtest: icbm: imimage: could not connect\n"); break; } aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING, faimtest_directim_incoming, 0); aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT, faimtest_directim_disconnect, 0); aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING, faimtest_directim_typing, 0); - printf("faimtest: OFT: DirectIM: connected to %s\n", userinfo->sn); + dvprintf("faimtest: OFT: DirectIM: connected to %s\n", userinfo->sn); aim_send_im_direct(sess, newconn, "goodday"); break; } default: - printf("faimtest: icbm: unknown reqclass (%d)\n", reqclass); + dvprintf("faimtest: icbm: unknown reqclass (%d)\n", reqclass); } /* switch */ } else - printf("faimtest does not support channels > 2 (chan = %02x)\n", channel); - printf("faimtest: icbm: done with ICBM handling\n"); + dvprintf("faimtest does not support channels > 2 (chan = %02x)\n", channel); + dprintf("faimtest: icbm: done with ICBM handling\n"); return 1; } @@ -1027,7 +1125,7 @@ int faimtest_directim_initiate(struct aim_session_t *sess, struct command_rx_str priv = (struct aim_directim_priv *)newconn->priv; - printf("faimtest: OFT: DirectIM: intitiate success to %s\n", priv->ip); + dvprintf("faimtest: OFT: DirectIM: intitiate success to %s\n", priv->ip); aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING, faimtest_directim_incoming, 0); aim_conn_addhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMDISCONNECT, faimtest_directim_disconnect, 0); @@ -1035,7 +1133,7 @@ int faimtest_directim_initiate(struct aim_session_t *sess, struct command_rx_str aim_send_im_direct(sess, newconn, "goodday"); - printf("faimtest: OFT: DirectIM: connected to %s\n", priv->sn); + dvprintf("faimtest: OFT: DirectIM: connected to %s\n", priv->sn); return 1; } @@ -1050,7 +1148,7 @@ int faimtest_directim_connect(struct aim_session_t *sess, struct command_rx_stru va_end(ap); - printf("faimtest: directim_connect\n"); + dprintf("faimtest: directim_connect\n"); return 1; } @@ -1067,7 +1165,7 @@ int faimtest_directim_incoming(struct aim_session_t *sess, struct command_rx_str msg = va_arg(ap, char *); va_end(ap); - printf("faimtest: Directim from %s: %s\n", sn, msg); + dvprintf("faimtest: Directim from %s: %s\n", sn, msg); if (!strncmp(msg, "sendmsg", 7)) { int i; i = atoi(msg+8); @@ -1104,7 +1202,7 @@ int faimtest_directim_disconnect(struct aim_session_t *sess, struct command_rx_s sn = va_arg(ap, char *); va_end(ap); - printf("faimtest: directim: disconnected from %s\n", sn); + dvprintf("faimtest: directim: disconnected from %s\n", sn); aim_conn_kill(sess, &conn); return 1; @@ -1119,13 +1217,13 @@ int faimtest_directim_typing(struct aim_session_t *sess, struct command_rx_struc sn = va_arg(ap, char *); va_end(ap); - printf("faimtest: ohmigod! %s has started typing (DirectIM). He's going to send you a message! *squeal*\n", sn); + dvprintf("faimtest: ohmigod! %s has started typing (DirectIM). He's going to send you a message! *squeal*\n", sn); return 1; } int faimtest_authsvrready(struct aim_session_t *sess, struct command_rx_struct *command, ...) { - printf("faimtest_authsvrready: called (contype: %d)\n", command->conn->type); + dvprintf("faimtest_authsvrready: called (contype: %d)\n", command->conn->type); sleep(10); /* should just be able to tell it we're ready too... */ aim_auth_clientready(sess, command->conn); @@ -1145,7 +1243,7 @@ int faimtest_authsvrready(struct aim_session_t *sess, struct command_rx_struct * int faimtest_pwdchngdone(struct aim_session_t *sess, struct command_rx_struct *command, ...) { - printf("PASSWORD CHANGE SUCCESSFUL!!!\n"); + dprintf("PASSWORD CHANGE SUCCESSFUL!!!\n"); return 1; } @@ -1158,7 +1256,7 @@ int faimtest_parse_oncoming(struct aim_session_t *sess, struct command_rx_struct userinfo = va_arg(ap, struct aim_userinfo_s *); va_end(ap); - printf("%ld %s is now online (flags: %04x = %s%s%s%s%s%s%s%s) (caps = 0x%04x)\n", + dvprintf("%ld %s is now online (flags: %04x = %s%s%s%s%s%s%s%s) (caps = 0x%04x)\n", time(NULL), userinfo->sn, userinfo->flags, (userinfo->flags&AIM_FLAG_UNCONFIRMED)?" UNCONFIRMED":"", @@ -1182,7 +1280,7 @@ int faimtest_parse_offgoing(struct aim_session_t *sess, struct command_rx_struct sn = va_arg(ap, char *); va_end(ap); - printf("\n%s has left\n", sn); + dvprintf("\n%s has left\n", sn); return 1; } @@ -1206,8 +1304,8 @@ int faimtest_parse_motd(struct aim_session_t *sess, struct command_rx_struct *co msg = va_arg(ap, char *); va_end(ap); - printf("faimtest: motd: %s (%d / %s)\n", msg, id, - (id < codeslen)?codes[id]:"unknown"); + dvprintf("faimtest: motd: %s (%d / %s)\n", msg, id, + (id < codeslen)?codes[id]:"unknown"); if (!connected) connected++; @@ -1224,7 +1322,7 @@ int faimtest_parse_genericerr(struct aim_session_t *sess, struct command_rx_stru reason = va_arg(ap, int); va_end(ap); - printf("faimtest: snac threw error (reason 0x%04x: %s)\n", reason, (reasonsn, reason, (reasonsn, reason, (reasonconn->priv); + dvprintf("faimtest: chat: %s: New occupants have joined:\n", (char *)command->conn->priv); while (i < count) - printf("faimtest: chat: %s: \t%s\n", (char *)command->conn->priv, userinfo[i++].sn); + dvprintf("faimtest: chat: %s: \t%s\n", (char *)command->conn->priv, userinfo[i++].sn); return 1; } @@ -1331,9 +1429,10 @@ int faimtest_chat_leave(struct aim_session_t *sess, struct command_rx_struct *co userinfo = va_arg(ap, struct aim_userinfo_s *); va_end(ap); - printf("faimtest: chat: %s: Some occupants have left:\n", (char *)command->conn->priv); - while (i < count) - printf("faimtest: chat: %s: \t%s\n", (char *)command->conn->priv, userinfo[i++].sn); + dvprintf("faimtest: chat: %s: Some occupants have left:\n", (char *)command->conn->priv); + + for (i = 0; i < count; ) + dvprintf("faimtest: chat: %s: \t%s\n", (char *)command->conn->priv, userinfo[i++].sn); return 1; } @@ -1362,25 +1461,24 @@ int faimtest_chat_infoupdate(struct aim_session_t *sess, struct command_rx_struc unknown_d5 = va_arg(ap, int); va_end(ap); - printf("faimtest: chat: %s: info update:\n", (char *)command->conn->priv); - printf("faimtest: chat: %s: \tRoominfo: {%04x, %s, %04x}\n", + dvprintf("faimtest: chat: %s: info update:\n", (char *)command->conn->priv); + dvprintf("faimtest: chat: %s: \tRoominfo: {%04x, %s, %04x}\n", (char *)command->conn->priv, roominfo->exchange, roominfo->name, roominfo->instance); - printf("faimtest: chat: %s: \tRoomname: %s\n", (char *)command->conn->priv, roomname); - printf("faimtest: chat: %s: \tRoomdesc: %s\n", (char *)command->conn->priv, roomdesc); - printf("faimtest: chat: %s: \tOccupants: (%d)\n", (char *)command->conn->priv, usercount); + dvprintf("faimtest: chat: %s: \tRoomname: %s\n", (char *)command->conn->priv, roomname); + dvprintf("faimtest: chat: %s: \tRoomdesc: %s\n", (char *)command->conn->priv, roomdesc); + dvprintf("faimtest: chat: %s: \tOccupants: (%d)\n", (char *)command->conn->priv, usercount); - i = 0; - while (i < usercount) - printf("faimtest: chat: %s: \t\t%s\n", (char *)command->conn->priv, userinfo[i++].sn); + for (i = 0; i < usercount; ) + dvprintf("faimtest: chat: %s: \t\t%s\n", (char *)command->conn->priv, userinfo[i++].sn); - printf("faimtest: chat: %s: \tUnknown_c9: 0x%04x\n", (char *)command->conn->priv, unknown_c9); - printf("faimtest: chat: %s: \tCreation time: %lu (time_t)\n", (char *)command->conn->priv, creationtime); - printf("faimtest: chat: %s: \tMax message length: %d bytes\n", (char *)command->conn->priv, maxmsglen); - printf("faimtest: chat: %s: \tUnknown_d2: 0x%04x\n", (char *)command->conn->priv, unknown_d2); - printf("faimtest: chat: %s: \tUnknown_d5: 0x%02x\n", (char *)command->conn->priv, unknown_d5); + dvprintf("faimtest: chat: %s: \tUnknown_c9: 0x%04x\n", (char *)command->conn->priv, unknown_c9); + dvprintf("faimtest: chat: %s: \tCreation time: %lu (time_t)\n", (char *)command->conn->priv, creationtime); + dvprintf("faimtest: chat: %s: \tMax message length: %d bytes\n", (char *)command->conn->priv, maxmsglen); + dvprintf("faimtest: chat: %s: \tUnknown_d2: 0x%04x\n", (char *)command->conn->priv, unknown_d2); + dvprintf("faimtest: chat: %s: \tUnknown_d5: 0x%02x\n", (char *)command->conn->priv, unknown_d5); return 1; } @@ -1397,7 +1495,7 @@ int faimtest_chat_incomingmsg(struct aim_session_t *sess, struct command_rx_stru msg = va_arg(ap, char *); va_end(ap); - printf("faimtest: chat: %s: incoming msg from %s: %s\n", (char *)command->conn->priv, userinfo->sn, msg); + dvprintf("faimtest: chat: %s: incoming msg from %s: %s\n", (char *)command->conn->priv, userinfo->sn, msg); /* * Do an echo for testing purposes. But not for ourselves ("oops!") @@ -1430,17 +1528,16 @@ int faimtest_chatnav_info(struct aim_session_t *sess, struct command_rx_struct * exchanges = va_arg(ap, struct aim_chat_exchangeinfo *); va_end(ap); - printf("faimtest: chat info: Chat Rights:\n"); - printf("faimtest: chat info: \tMax Concurrent Rooms: %d\n", maxrooms); + dprintf("faimtest: chat info: Chat Rights:\n"); + dvprintf("faimtest: chat info: \tMax Concurrent Rooms: %d\n", maxrooms); - printf("faimtest: chat info: \tExchange List: (%d total)\n", exchangecount); - while (i < exchangecount) { - printf("faimtest: chat info: \t\t%x: %s (%s/%s)\n", - exchanges[i].number, - exchanges[i].name, - exchanges[i].charset1, - exchanges[i].lang1); - i++; + dvprintf("faimtest: chat info: \tExchange List: (%d total)\n", exchangecount); + for (i = 0; i < exchangecount; i++) { + dvprintf("faimtest: chat info: \t\t%x: %s (%s/%s)\n", + exchanges[i].number, + exchanges[i].name, + exchanges[i].charset1, + exchanges[i].lang1); } } @@ -1464,12 +1561,12 @@ int faimtest_chatnav_info(struct aim_session_t *sess, struct command_rx_struct * ck = va_arg(ap, char *); va_end(ap); - printf("faimtest: recieved room create reply for %s/0x%04x\n", fqcn, exchange); + dvprintf("faimtest: recieved room create reply for %s/0x%04x\n", fqcn, exchange); } break; default: va_end(ap); - printf("faimtest: chatnav info: unknown type (%04x)\n", type); + dvprintf("faimtest: chatnav info: unknown type (%04x)\n", type); } return 1; } @@ -1485,7 +1582,7 @@ int faimtest_parse_connerr(struct aim_session_t *sess, struct command_rx_struct msg = va_arg(ap, char *); va_end(ap); - printf("faimtest: connerr: Code 0x%04x: %s\n", code, msg); + dvprintf("faimtest: connerr: Code 0x%04x: %s\n", code, msg); aim_conn_kill(sess, &command->conn); /* this will break the main loop */ return 1; @@ -1493,7 +1590,7 @@ int faimtest_parse_connerr(struct aim_session_t *sess, struct command_rx_struct int faimtest_debugconn_connect(struct aim_session_t *sess, struct command_rx_struct *command, ...) { - printf("faimtest: connecting to an aimdebugd!\n"); + dprintf("faimtest: connecting to an aimdebugd!\n"); /* convert the authorizer connection to a BOS connection */ command->conn->type = AIM_CONN_TYPE_BOS; @@ -1521,7 +1618,7 @@ int faimtest_parse_msgack(struct aim_session_t *sess, struct command_rx_struct * sn = va_arg(ap, char *); va_end(ap); - printf("faimtest: msgack: 0x%04x / %s\n", type, sn); + dvprintf("faimtest: msgack: 0x%04x / %s\n", type, sn); return 1; } @@ -1540,7 +1637,7 @@ int faimtest_getfile_filereq(struct aim_session_t *ses, struct command_rx_struct cookie = va_arg(ap, char *); va_end(ap); - printf("faimtest: request for file %s.\n", fh->name); + dvprintf("faimtest: request for file %s.\n", fh->name); return 1; } @@ -1552,6 +1649,8 @@ int faimtest_getfile_filesend(struct aim_session_t *sess, struct command_rx_stru struct aim_conn_t *oftconn; struct aim_fileheader_t *fh; char *path, *cookie; + int pos, bufpos, bufsize = 2048, i; + char buf[2048]; FILE *file; @@ -1561,28 +1660,51 @@ int faimtest_getfile_filesend(struct aim_session_t *sess, struct command_rx_stru cookie = va_arg(ap, char *); va_end(ap); - printf("faimtest: sending file %s.\n", fh->name); + dvprintf("faimtest: sending file %s(%ld).\n", fh->name, fh->size); - if( (path = (char *)calloc(1, strlen(sess->oft.listingdir) +strlen(fh->name)+2)) == NULL) { - perror("calloc:"); - printf("faimtest: error in calloc of path\n"); + if( (path = (char *)calloc(1, strlen(listingpath) +strlen(fh->name)+2)) == NULL) { + dperror("calloc"); + dprintf("faimtest: error in calloc of path\n"); return 0; /* XXX: no idea what winaim expects here =) */ } - snprintf(path, strlen(sess->oft.listingdir)+strlen(fh->name)+2, "%s/%s", sess->oft.listingdir, fh->name); + snprintf(path, strlen(listingpath)+strlen(fh->name)+2, "%s/%s", listingpath, fh->name); if( (file = fopen(path, "r")) == NULL) { - printf("faimtest: getfile_send fopen failed for %s. damn.\n", path); + dvprintf("faimtest: getfile_send fopen failed for %s. damn.\n", path); return 0; } + + for(pos = 0; pos < fh->size; pos++) { + bufpos = pos % bufsize; + + if(bufpos == 0 && pos > 0) { /* filled our buffer. spit it across the wire */ + if ( (i = send(oftconn->fd, buf, bufsize, 0)) != bufsize ) { + dperror("faim: getfile_send: write1"); + dprintf("faim: getfile_send: whoopsy, didn't write it all...\n"); + free(buf); + return -1; + } + } + if( (buf[bufpos] = fgetc(file)) == EOF) { + if(pos != fh->size) { + dvprintf("faim: getfile_send: hrm... apparent early EOF at pos 0x%x of 0x%lx\n", pos, fh->size); + free(buf); + return -1; + } + } + dvprintf("%c(0x%02x) ", buf[pos], buf[pos]); + } - if (aim_getfile_send(oftconn, file, fh) == -1) { - printf("faimtest: getfile_send failed. damn.\n"); - } else { - printf("faimtest: looks like getfile went clean\n"); + if( (i = send(oftconn->fd, buf, bufpos+1, 0)) != (bufpos+1)) { + dperror("faim: getfile_send: write2"); + dprintf("faim: getfile_send cleanup: whoopsy, didn't write it all...\n"); + free(buf); + return -1; } + free(fh); return 1; } @@ -1598,9 +1720,10 @@ int faimtest_getfile_complete(struct aim_session_t *sess, struct command_rx_stru fh = va_arg(ap, struct aim_fileheader_t *); va_end(ap); - printf("faimtest: completed file transfer for %s.\n", fh->name); + dvprintf("faimtest: completed file transfer for %s.\n", fh->name); - /* aim_conn_kill(sess, &conn); */ /* we'll let winaim close the conn */ + aim_conn_close(conn); + aim_conn_kill(sess, &conn); return 1; } @@ -1617,11 +1740,151 @@ int faimtest_getfile_disconnect(struct aim_session_t *sess, struct command_rx_st aim_conn_kill(sess, &conn); - printf("faimtest: getfile: disconnected from %s\n", sn); + dvprintf("faimtest: getfile: disconnected from %s\n", sn); + return 1; +} +int faimtest_getfile_initiate(struct aim_session_t *sess, struct command_rx_struct *command, ...) +{ + va_list ap; + struct aim_conn_t *conn; + char *sn; + struct aim_filetransfer_priv *priv; + + va_start(ap, command); + conn = va_arg(ap, struct aim_conn_t *); + sn = va_arg(ap, char *); + va_end(ap); + + aim_conn_addhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ, faimtest_getfile_filereq, 0); + aim_conn_addhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND, faimtest_getfile_filesend, 0); + aim_conn_addhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE, faimtest_getfile_complete, 0); + aim_conn_addhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEDISCONNECT, faimtest_getfile_disconnect, 0); + aim_conn_addhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTING, faimtest_getfile_listing, 0); + aim_conn_addhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ, faimtest_getfile_listingreq, 0); + aim_conn_addhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILERECIEVE, faimtest_getfile_recieve, 0); + + priv = (struct aim_filetransfer_priv *)conn->priv; + + dvprintf("faimtest: getfile: %s (%s) connected to us on %d\n", sn, priv->ip, conn->fd); return 1; } -#endif +int faimtest_getfile_listing(struct aim_session_t *sess, struct command_rx_struct *command, ...) +{ + va_list ap; + struct aim_conn_t *conn; + char *listing; + struct aim_filetransfer_priv *ft; + char *filename, *nameend, *sizec; + int filesize, namelen; + + va_start(ap, command); + conn = va_arg(ap, struct aim_conn_t *); + ft = va_arg(ap, struct aim_filetransfer_priv *); + listing = va_arg(ap, char *); + va_end(ap); + + dvprintf("listing on %d==================\n%s\n===========\n", conn->fd, listing); + + nameend = strstr(listing+0x1a, "\r"); + + namelen = nameend - (listing + 0x1a); + + filename = malloc(namelen + 1); + strncpy(filename, listing+0x1a, namelen); + filename[namelen] = 0x00; + + sizec = malloc(8+1); + memcpy(sizec, listing + 0x11, 8); + sizec[8] = 0x00; + + filesize = strtol(sizec, (char **)NULL, 10); + + dvprintf("faimtest: requesting %d %s(%d long)\n", namelen, filename, filesize); + + aim_oft_getfile_request(sess, conn, filename, filesize); + + free(filename); + free(sizec); + + return 0; +} + +int faimtest_getfile_listingreq(struct aim_session_t *sess, struct command_rx_struct *command, ...) +{ + va_list ap; + struct aim_conn_t *oftconn; + struct aim_fileheader_t *fh; + int pos, bufpos, bufsize = 2048, i; + char buf[2048]; + + + va_start(ap, command); + oftconn = va_arg(ap, struct aim_conn_t *); + fh = va_arg(ap, struct aim_fileheader_t *); + va_end(ap); + + dvprintf("faimtest: sending listing of size %ld\n", fh->size); + + for(pos = 0; pos < fh->size; pos++) { + bufpos = pos % bufsize; + + if(bufpos == 0 && pos > 0) { /* filled our buffer. spit it across the wire */ + if ( (i = send(oftconn->fd, buf, bufsize, 0)) != bufsize ) { + dperror("faim: getfile_send: write1"); + dprintf("faim: getfile_send: whoopsy, didn't write it all...\n"); + free(buf); + return -1; + } + } + if( (buf[bufpos] = fgetc(listingfile)) == EOF) { + if(pos != fh->size) { + dvprintf("faim: getfile_send: hrm... apparent early EOF at pos 0x%x of 0x%lx\n", pos, fh->size); + free(buf); + return -1; + } + } + } + + if( (i = send(oftconn->fd, buf, bufpos+1, 0)) != (bufpos+1)) { + dperror("faim: getfile_send: write2"); + dprintf("faim: getfile_send cleanup: whoopsy, didn't write it all...\n"); + free(buf); + return -1; + } + + dprintf("faimtest: sent listing\n"); + return 0; +} + +int faimtest_getfile_recieve(struct aim_session_t *sess, struct command_rx_struct *command, ...) +{ + va_list ap; + struct aim_conn_t *conn; + struct aim_filetransfer_priv *ft; + unsigned char data; + int pos; + + va_start(ap, command); + conn = va_arg(ap, struct aim_conn_t *); + ft = va_arg(ap, struct aim_filetransfer_priv *); + va_end(ap); + + dvprintf("faimtest: receiving %ld bytes of file data for %s:\n\t", ft->fh.size, ft->fh.name); + + for(pos = 0; pos < ft->fh.size; pos++) { + read(conn->fd, &data, 1); + dvprintf("%c(%02x) ", data, data); + } + + dprintf("\n"); + + aim_oft_getfile_ack(sess, conn); + aim_oft_getfile_end(sess, conn); + + return 0; +} +#endif int faimtest_parse_ratechange(struct aim_session_t *sess, struct command_rx_struct *command, ...) { static char *codes[5] = {"invalid", @@ -1662,7 +1925,7 @@ int faimtest_parse_ratechange(struct aim_session_t *sess, struct command_rx_stru va_end(ap); - printf("faimtest: rate %s (rate class 0x%04lx): curavg = %ld, maxavg = %ld, alert at %ld, clear warning at %ld, limit at %ld, disconnect at %ld (window size = %ld)\n", + dvprintf("faimtest: rate %s (rate class 0x%04lx): curavg = %ld, maxavg = %ld, alert at %ld, clear warning at %ld, limit at %ld, disconnect at %ld (window size = %ld)\n", (code < 5)?codes[code]:"invalid", rateclass, currentavg, maxavg, @@ -1724,7 +1987,42 @@ int faimtest_parse_evilnotify(struct aim_session_t *sess, struct command_rx_stru * newevil is passed as an int representing the new evil value times * ten. */ - printf("faimtest: evil level change: new value = %2.1f%% (caused by %s)\n", ((float)newevil)/10, (userinfo && strlen(userinfo->sn))?userinfo->sn:"anonymous"); + dvprintf("faimtest: evil level change: new value = %2.1f%% (caused by %s)\n", ((float)newevil)/10, (userinfo && strlen(userinfo->sn))?userinfo->sn:"anonymous"); + + return 1; +} + +int faimtest_parse_searchreply(struct aim_session_t *sess, struct command_rx_struct *command, ...) +{ + va_list ap; + char *address, *SNs; + int i, num; + + va_start(ap, command); + address = va_arg(ap, char *); + num = va_arg(ap, int); + SNs = va_arg(ap, char *); + va_end(ap); + + dvprintf("faimtest: E-Mail Search Results for %s: ", address); + + for(i = 0; i < num; i++) + dvinlineprintf("%s, ", &SNs[i*(MAXSNLEN+1)]); + dinlineprintf("\n"); + + return 1; +} + +int faimtest_parse_searcherror(struct aim_session_t *sess, struct command_rx_struct *command, ...) +{ + va_list ap; + char *address; + + va_start(ap, command); + address = va_arg(ap, char *); + va_end(ap); + + dvprintf("faimtest: E-Mail Search Results for %s: No Results or Invalid Email\n", address); return 1; }