📄 im.c
字号:
fu8_t ck[8]; aim_frame_t *fr; aim_snacid_t snacid; aim_tlvlist_t *tl = NULL, *itl = NULL; int hdrlen, i; fu8_t *hdr; aim_bstream_t hdrbs; if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004))) return -EINVAL; if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 256+strlen(destsn)))) return -ENOMEM; snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0); aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid); /* * Generate a random message cookie * * This cookie needs to be alphanumeric and NULL-terminated to be * TOC-compatible. * * XXX have I mentioned these should be generated in msgcookie.c? * */ for (i = 0; i < 7; i++) ck[i] = 0x30 + ((fu8_t) rand() % 10); ck[7] = '\0'; if (ckret) memcpy(ckret, ck, 8); /* Cookie */ aimbs_putraw(&fr->data, ck, 8); /* Channel */ aimbs_put16(&fr->data, 0x0002); /* Destination SN */ aimbs_put8(&fr->data, strlen(destsn)); aimbs_putraw(&fr->data, destsn, strlen(destsn)); aim_addtlvtochain_noval(&tl, 0x0003); hdrlen = 2+8+16+6+8+6+4; hdr = malloc(hdrlen); aim_bstream_init(&hdrbs, hdr, hdrlen); aimbs_put16(&hdrbs, 0x0000); aimbs_putraw(&hdrbs, ck, 8); aim_putcap(&hdrbs, AIM_CAPS_IMIMAGE); aim_addtlvtochain16(&itl, 0x000a, 0x0001); aim_addtlvtochain_raw(&itl, 0x0003, 4, ip); aim_addtlvtochain16(&itl, 0x0005, port); aim_addtlvtochain_noval(&itl, 0x000f); aim_writetlvchain(&hdrbs, &itl); aim_addtlvtochain_raw(&tl, 0x0005, aim_bstream_curpos(&hdrbs), hdr); aim_writetlvchain(&fr->data, &tl); free(hdr); aim_freetlvchain(&itl); aim_freetlvchain(&tl); aim_tx_enqueue(sess, fr); return 0;}faim_internal int aim_request_sendfile(aim_session_t *sess, const char *sn, const char *filename, fu16_t numfiles, fu32_t totsize, fu8_t *ip, fu16_t port, fu8_t *ckret){ aim_conn_t *conn; int i; fu8_t ck[8]; aim_frame_t *fr; aim_snacid_t snacid; if (!sess || !(conn = aim_conn_findbygroup(sess, 0x0004))) return -EINVAL; if (!sn || !filename) return -EINVAL; if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 10+8+2+1+strlen(sn)+2+2+2+8+16+6+8+6+4+2+2+2+2+4+strlen(filename)+4))) return -ENOMEM; snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0); aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid); for (i = 0; i < 7; i++) aimutil_put8(ck+i, 0x30 + ((fu8_t) rand() % 10)); ck[7] = '\0'; if (ckret) memcpy(ckret, ck, 8); /* * Cookie */ aimbs_putraw(&fr->data, ck, 8); /* * Channel (2) */ aimbs_put16(&fr->data, 0x0002); /* * Dest sn */ aimbs_put8(&fr->data, strlen(sn)); aimbs_putraw(&fr->data, sn, strlen(sn)); /* * TLV t(0005) * * Encompasses everything below. Gee. */ aimbs_put16(&fr->data, 0x0005); aimbs_put16(&fr->data, 2+8+16+6+8+6+4+2+2+2+2+4+strlen(filename)+4); aimbs_put16(&fr->data, 0x0000); aimbs_putraw(&fr->data, ck, 8); aim_putcap(&fr->data, AIM_CAPS_SENDFILE); /* TLV t(000a) */ aimbs_put16(&fr->data, 0x000a); aimbs_put16(&fr->data, 0x0002); aimbs_put16(&fr->data, 0x0001); /* TLV t(0003) (IP) */ aimbs_put16(&fr->data, 0x0003); aimbs_put16(&fr->data, 0x0004); aimbs_putraw(&fr->data, ip, 4); /* TLV t(0005) (port) */ aimbs_put16(&fr->data, 0x0005); aimbs_put16(&fr->data, 0x0002); aimbs_put16(&fr->data, port); /* TLV t(000f) */ aimbs_put16(&fr->data, 0x000f); aimbs_put16(&fr->data, 0x0000); /* TLV t(2711) */ aimbs_put16(&fr->data, 0x2711); aimbs_put16(&fr->data, 2+2+4+strlen(filename)+4); /* ? */ aimbs_put16(&fr->data, 0x0001); aimbs_put16(&fr->data, numfiles); aimbs_put32(&fr->data, totsize); aimbs_putraw(&fr->data, filename, strlen(filename)); /* ? */ aimbs_put32(&fr->data, 0x00000000); aim_tx_enqueue(sess, fr); return 0;}static int outgoingim(aim_session_t *sess, aim_module_t *mod, aim_frame_t *rx, aim_modsnac_t *snac, aim_bstream_t *bs){ int i, ret = 0; aim_rxcallback_t userfunc; fu8_t cookie[8]; fu16_t channel; aim_tlvlist_t *tlvlist; char *sn; int snlen; fu16_t icbmflags = 0; fu8_t flag1 = 0, flag2 = 0; fu8_t *msg = NULL; aim_tlv_t *msgblock; /* ICBM Cookie. */ for (i = 0; i < 8; i++) cookie[i] = aimbs_get8(bs); /* Channel ID */ channel = aimbs_get16(bs); if (channel != 0x01) { faimdprintf(sess, 0, "icbm: ICBM recieved on unsupported channel. Ignoring. (chan = %04x)\n", channel); return 0; } snlen = aimbs_get8(bs); sn = aimbs_getstr(bs, snlen); tlvlist = aim_readtlvchain(bs); if (aim_gettlv(tlvlist, 0x0003, 1)) icbmflags |= AIM_IMFLAGS_ACK; if (aim_gettlv(tlvlist, 0x0004, 1)) icbmflags |= AIM_IMFLAGS_AWAY; if ((msgblock = aim_gettlv(tlvlist, 0x0002, 1))) { aim_bstream_t mbs; int featurelen, msglen; aim_bstream_init(&mbs, msgblock->value, msgblock->length); aimbs_get8(&mbs); aimbs_get8(&mbs); for (featurelen = aimbs_get16(&mbs); featurelen; featurelen--) aimbs_get8(&mbs); aimbs_get8(&mbs); aimbs_get8(&mbs); msglen = aimbs_get16(&mbs) - 4; /* final block length */ flag1 = aimbs_get16(&mbs); flag2 = aimbs_get16(&mbs); msg = aimbs_getstr(&mbs, msglen); } if ((userfunc = aim_callhandler(sess, rx->conn, snac->family, snac->subtype))) ret = userfunc(sess, rx, channel, sn, msg, icbmflags, flag1, flag2); free(sn); aim_freetlvchain(&tlvlist); return ret;}/* * Ahh, the joys of nearly ridiculous over-engineering. * * Not only do AIM ICBM's support multiple channels. Not only do they * support multiple character sets. But they support multiple character * sets / encodings within the same ICBM. * * These multipart messages allow for complex space savings techniques, which * seem utterly unnecessary by today's standards. In fact, there is only * one client still in popular use that still uses this method: AOL for the * Macintosh, Version 5.0. Obscure, yes, I know. * * In modern (non-"legacy") clients, if the user tries to send a character * that is not ISO-8859-1 or ASCII, the client will send the entire message * as UNICODE, meaning that every character in the message will occupy the * full 16 bit UNICODE field, even if the high order byte would be zero. * Multipart messages prevent this wasted space by allowing the client to * only send the characters in UNICODE that need to be sent that way, and * the rest of the message can be sent in whatever the native character * set is (probably ASCII). * * An important note is that sections will be displayed in the order that * they appear in the ICBM. There is no facility for merging or rearranging * sections at run time. So if you have, say, ASCII then UNICODE then ASCII, * you must supply two ASCII sections with a UNICODE in the middle, and incur * the associated overhead. * * Normally I would have laughed and given a firm 'no' to supporting this * seldom-used feature, but something is attracting me to it. In the future, * it may be possible to abuse this to send mixed-media messages to other * open source clients (like encryption or something) -- see faimtest for * examples of how to do this. * * I would definitly recommend avoiding this feature unless you really * know what you are doing, and/or you have something neat to do with it. * */faim_export int aim_mpmsg_init(aim_session_t *sess, aim_mpmsg_t *mpm){ memset(mpm, 0, sizeof(aim_mpmsg_t)); return 0;}static int mpmsg_addsection(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, fu8_t *data, fu16_t datalen){ aim_mpmsg_section_t *sec; if (!(sec = malloc(sizeof(aim_mpmsg_section_t)))) return -1; sec->charset = charset; sec->charsubset = charsubset; sec->data = data; sec->datalen = datalen; sec->next = NULL; if (!mpm->parts) mpm->parts = sec; else { aim_mpmsg_section_t *cur; for (cur = mpm->parts; cur->next; cur = cur->next) ; cur->next = sec; } mpm->numparts++; return 0;}faim_export int aim_mpmsg_addraw(aim_session_t *sess, aim_mpmsg_t *mpm, fu16_t charset, fu16_t charsubset, const fu8_t *data, fu16_t datalen){ fu8_t *dup; if (!(dup = malloc(datalen))) return -1; memcpy(dup, data, datalen); if (mpmsg_addsection(sess, mpm, charset, charsubset, dup, datalen) == -1) { free(dup); return -1; } return 0;}/* XXX should provide a way of saying ISO-8859-1 specifically */faim_export int aim_mpmsg_addascii(aim_session_t *sess, aim_mpmsg_t *mpm, const char *ascii){ fu8_t *dup; if (!(dup = strdup(ascii))) return -1; if (mpmsg_addsection(sess, mpm, 0x0000, 0x0000, dup, strlen(ascii)) == -1) { free(dup); return -1; } return 0;}faim_export int aim_mpmsg_addunicode(aim_session_t *sess, aim_mpmsg_t *mpm, const fu16_t *unicode, fu16_t unicodelen){ fu8_t *buf; aim_bstream_t bs; int i; if (!(buf = malloc(unicodelen * 2))) return -1; aim_bstream_init(&bs, buf, unicodelen * 2); /* We assume unicode is in /host/ byte order -- convert to network */ for (i = 0; i < unicodelen; i++) aimbs_put16(&bs, unicode[i]); if (mpmsg_addsection(sess, mpm, 0x0002, 0x0000, buf, aim_bstream_curpos(&bs)) == -1) { free(buf); return -1; } return 0;}faim_export void aim_mpmsg_free(aim_session_t *sess, aim_mpmsg_t *mpm){ aim_mpmsg_section_t *cur; for (cur = mpm->parts; cur; ) { aim_mpmsg_section_t *tmp; tmp = cur->next; free(cur->data); free(cur); cur = tmp; } mpm->numparts = 0; mpm->parts = NULL; return;}/* * Start by building the multipart structures, then pick the first * human-readable section and stuff it into args->msg so no one gets * suspicious. * */static int incomingim_ch1_parsemsgs(aim_session_t *sess, fu8_t *data, int len, struct aim_incomingim_ch1_args *args){ static const fu16_t charsetpri[] = { 0x0000, /* ASCII first */ 0x0003, /* then ISO-8859-1 */ 0x0002, /* UNICODE as last resort */ }; static const int charsetpricount = 3; int i; aim_bstream_t mbs; aim_mpmsg_section_t *sec; aim_bstream_init(&mbs, data, len); while (aim_bstream_empty(&mbs)) { fu16_t msglen, flag1, flag2; fu8_t *msgbuf; aimbs_get8(&mbs); /* 01 */ aimbs_get8(&mbs); /* 01 */ /* Message string length, including character set info. */ msglen = aimbs_get16(&mbs); /* Character set info */ flag1 = aimbs_get16(&mbs); flag2 = aimbs_get16(&mbs); /* Message. */ msglen -= 4; /* * For now, we don't care what the encoding is. Just copy * it into a multipart struct and deal with it later. However, * always pad the ending with a NULL. This makes it easier * to treat ASCII sections as strings. It won't matter for * UNICODE or binary data, as you should never read past * the specified data length, which will not include the pad. * * XXX There's an API bug here. For sending, the UNICODE is * given in host byte order (aim_mpmsg_addunicode), but here * the received messages are given in network byte order. * */ msgbuf = aimbs_getstr(&mbs, msglen); mpmsg_addsection(sess, &args->mpmsg, flag1, flag2, msgbuf, msglen); } /* while */ args->icbmflags |= AIM_IMFLAGS_MULTIPART; /* always set */ /* * Clients that support multiparts should never use args->msg, as it * will point to an arbitrary section. * * Here, we attempt to provide clients that do not support multipart * messages with something to look at -- hopefully a human-readable * string. But, failing that, a UNICODE message, or nothing at all. * * Which means that even if args->msg is NULL, it does not mean the * message was blank. * */ for (i = 0; i < charsetpricount; i++) { for (sec = args->mpmsg.parts; sec; sec = sec->next) { if (sec->charset != charsetpri[i]) continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -