📄 ft.c
字号:
/** * For those times when we want to open up the direct connection channel ourselves. * * You'll want to set up some kind of watcher on this socket. * When the state changes, call aim_handlerendconnection with * the connection returned by this. aim_handlerendconnection * will accept the pending connection and stop listening. * * @param sess The session * @param sn The screen name to connect to. * @return The new connection. */faim_export aim_conn_t *aim_odc_initiate(aim_session_t *sess, const char *sn, int listenfd, const fu8_t *localip, fu16_t port, const fu8_t *mycookie){ aim_conn_t *newconn; aim_msgcookie_t *cookie; struct aim_odc_intdata *priv; fu8_t ck[8]; if (!localip) return NULL; if (mycookie) { memcpy(ck, mycookie, 8); aim_im_sendch2_odcrequest(sess, ck, TRUE, sn, localip, port); } else aim_im_sendch2_odcrequest(sess, ck, FALSE, sn, localip, port); cookie = (aim_msgcookie_t *)calloc(1, sizeof(aim_msgcookie_t)); memcpy(cookie->cookie, ck, 8); cookie->type = AIM_COOKIETYPE_OFTIM; /* this one is for the cookie */ priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); memcpy(priv->cookie, ck, 8); strncpy(priv->sn, sn, sizeof(priv->sn)); cookie->data = priv; aim_cachecookie(sess, cookie); /* XXX - switch to aim_cloneconn()? */ if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER, NULL))) { close(listenfd); return NULL; } /* this one is for the conn */ priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); memcpy(priv->cookie, ck, 8); strncpy(priv->sn, sn, sizeof(priv->sn)); newconn->fd = listenfd; newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; newconn->internal = priv; newconn->lastactivity = time(NULL); return newconn;}/** * Connect directly to the given buddy for directim. * * This is a wrapper for aim_newconn. * * If addr is NULL, the socket is not created, but the connection is * allocated and setup to connect. * * @param sess The Godly session. * @param sn The screen name we're connecting to. I hope it's a girl... * @param addr Address to connect to. * @return The new connection. */faim_export aim_conn_t *aim_odc_connect(aim_session_t *sess, const char *sn, const char *addr, const fu8_t *cookie){ aim_conn_t *newconn; struct aim_odc_intdata *intdata; if (!sess || !sn) return NULL; if (!(intdata = calloc(1, sizeof(struct aim_odc_intdata)))) return NULL; memcpy(intdata->cookie, cookie, 8); strncpy(intdata->sn, sn, sizeof(intdata->sn)); if (addr) strncpy(intdata->ip, addr, sizeof(intdata->ip)); /* XXX - verify that non-blocking connects actually work */ if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, addr))) { free(intdata); return NULL; } newconn->internal = intdata; newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; return newconn;}/** * Sometimes you just don't know with these kinds of people. * * @param sess The session. * @param conn The ODC connection of the incoming data. * @param frr The frame allocated for the incoming data. * @param bs It stands for "bologna sandwich." * @return Return 0 if no errors, otherwise return the error number. */static int handlehdr_odc(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *frr, aim_bstream_t *bs){ aim_frame_t fr; int ret = 0; aim_rxcallback_t userfunc; fu32_t payloadlength; fu16_t flags, encoding; char *snptr = NULL; fr.conn = conn; /* AAA - ugly */ aim_bstream_setpos(bs, 20); payloadlength = aimbs_get32(bs); aim_bstream_setpos(bs, 24); encoding = aimbs_get16(bs); aim_bstream_setpos(bs, 30); flags = aimbs_get16(bs); aim_bstream_setpos(bs, 36); /* XXX - create an aimbs_getnullstr function? */ snptr = aimbs_getstr(bs, 32); /* Next 32 bytes contain the sn, padded with null chars */ faimdprintf(sess, 2, "faim: OFT frame: handlehdr_odc: %04x / %04x / %s\n", payloadlength, flags, snptr); if (flags & 0x0008) { if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) ret = userfunc(sess, &fr, snptr, 2); } else if (flags & 0x0004) { if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) ret = userfunc(sess, &fr, snptr, 1); } else { if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) ret = userfunc(sess, &fr, snptr, 0); } if ((payloadlength != 0) && (payloadlength != UINT_MAX)) { char *msg; int recvd = 0; int i, isawaymsg; isawaymsg = flags & 0x0001; if (!(msg = calloc(1, payloadlength+1))) { free(snptr); return -ENOMEM; } while (payloadlength - recvd) { if (payloadlength - recvd >= 1024) i = aim_recv(conn->fd, &msg[recvd], 1024); else i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd); if (i <= 0) { free(msg); free(snptr); return -1; } recvd = recvd + i; if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER))) ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength); } if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING))) ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg); free(msg); } free(snptr); return ret;}faim_export struct aim_oft_info *aim_oft_createinfo(aim_session_t *sess, const fu8_t *cookie, const char *sn, const char *ip, fu16_t port, fu32_t size, fu32_t modtime, char *filename){ struct aim_oft_info *new; if (!sess) return NULL; if (!(new = (struct aim_oft_info *)calloc(1, sizeof(struct aim_oft_info)))) return NULL; new->sess = sess; if (cookie) memcpy(new->cookie, cookie, 8); if (ip) new->clientip = strdup(ip); if (sn) new->sn = strdup(sn); new->port = port; new->fh.totfiles = 1; new->fh.filesleft = 1; new->fh.totparts = 1; new->fh.partsleft = 1; new->fh.totsize = size; new->fh.size = size; new->fh.modtime = modtime; new->fh.checksum = 0xffff0000; new->fh.rfrcsum = 0xffff0000; new->fh.rfcsum = 0xffff0000; new->fh.recvcsum = 0xffff0000; strncpy(new->fh.idstring, "OFT_Windows ICBMFT V1.1 32", 31); if (filename) { strncpy(new->fh.name, filename, 63); new->fh.name[63] = '\0'; } new->next = sess->oft_info; sess->oft_info = new; return new;}/** * Remove the given oft_info struct from the oft_info linked list, and * then free its memory. * * @param sess The session. * @param oft_info The aim_oft_info struct that we're destroying. * @return Return 0 if no errors, otherwise return the error number. */faim_export int aim_oft_destroyinfo(struct aim_oft_info *oft_info){ aim_session_t *sess; if (!oft_info || !(sess = oft_info->sess)) return -EINVAL; if (sess->oft_info && (sess->oft_info == oft_info)) { sess->oft_info = sess->oft_info->next; } else { struct aim_oft_info *cur; for (cur=sess->oft_info; (cur->next && (cur->next!=oft_info)); cur=cur->next); if (cur->next) cur->next = cur->next->next; } free(oft_info->sn); free(oft_info->proxyip); free(oft_info->clientip); free(oft_info->verifiedip); free(oft_info); return 0;}/** * Creates a listener socket so the other dude can connect to us. * * You'll want to set up some kind of watcher on this socket. * When the state changes, call aim_handlerendconnection with * the connection returned by this. aim_handlerendconnection * will accept the pending connection and stop listening. * * @param sess The session. * @param oft_info File transfer information associated with this * connection. * @return Return 0 if no errors, otherwise return the error number. */faim_export int aim_sendfile_listen(aim_session_t *sess, struct aim_oft_info *oft_info, int listenfd){ if (!oft_info) return -EINVAL; if (!(oft_info->conn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER, NULL))) { close(listenfd); return -ENOMEM; } oft_info->conn->fd = listenfd; oft_info->conn->subtype = AIM_CONN_SUBTYPE_OFT_SENDFILE; oft_info->conn->lastactivity = time(NULL); return 0;}/** * Extract an &aim_fileheader_t from the given buffer. * * @param bs The should be from an incoming rendezvous packet. * @return A pointer to new struct on success, or NULL on error. */static struct aim_fileheader_t *aim_oft_getheader(aim_bstream_t *bs){ struct aim_fileheader_t *fh; if (!(fh = calloc(1, sizeof(struct aim_fileheader_t)))) return NULL; /* The bstream should be positioned after the hdrtype. */ aimbs_getrawbuf(bs, fh->bcookie, 8); fh->encrypt = aimbs_get16(bs); fh->compress = aimbs_get16(bs); fh->totfiles = aimbs_get16(bs); fh->filesleft = aimbs_get16(bs); fh->totparts = aimbs_get16(bs); fh->partsleft = aimbs_get16(bs); fh->totsize = aimbs_get32(bs); fh->size = aimbs_get32(bs); fh->modtime = aimbs_get32(bs); fh->checksum = aimbs_get32(bs); fh->rfrcsum = aimbs_get32(bs); fh->rfsize = aimbs_get32(bs); fh->cretime = aimbs_get32(bs); fh->rfcsum = aimbs_get32(bs); fh->nrecvd = aimbs_get32(bs); fh->recvcsum = aimbs_get32(bs); aimbs_getrawbuf(bs, fh->idstring, 32); fh->flags = aimbs_get8(bs); fh->lnameoffset = aimbs_get8(bs); fh->lsizeoffset = aimbs_get8(bs); aimbs_getrawbuf(bs, fh->dummy, 69); aimbs_getrawbuf(bs, fh->macfileinfo, 16); fh->nencode = aimbs_get16(bs); fh->nlanguage = aimbs_get16(bs); aimbs_getrawbuf(bs, fh->name, 64); /* XXX - filenames longer than 64B */ fh->name[63] = '\0'; return fh;} /** * Fills a buffer with network-order fh data * * @param bs A bstream to fill -- automatically initialized * @param fh A struct aim_fileheader_t to get data from. * @return Return non-zero on error. */static int aim_oft_buildheader(aim_bstream_t *bs, struct aim_fileheader_t *fh){ fu8_t *hdr; if (!bs || !fh) return -EINVAL; if (!(hdr = (unsigned char *)calloc(1, 0x100 - 8))) return -ENOMEM; aim_bstream_init(bs, hdr, 0x100 - 8); aimbs_putraw(bs, fh->bcookie, 8); aimbs_put16(bs, fh->encrypt); aimbs_put16(bs, fh->compress); aimbs_put16(bs, fh->totfiles); aimbs_put16(bs, fh->filesleft); aimbs_put16(bs, fh->totparts); aimbs_put16(bs, fh->partsleft); aimbs_put32(bs, fh->totsize); aimbs_put32(bs, fh->size); aimbs_put32(bs, fh->modtime); aimbs_put32(bs, fh->checksum); aimbs_put32(bs, fh->rfrcsum); aimbs_put32(bs, fh->rfsize); aimbs_put32(bs, fh->cretime); aimbs_put32(bs, fh->rfcsum); aimbs_put32(bs, fh->nrecvd); aimbs_put32(bs, fh->recvcsum); aimbs_putraw(bs, fh->idstring, 32); aimbs_put8(bs, fh->flags); aimbs_put8(bs, fh->lnameoffset); aimbs_put8(bs, fh->lsizeoffset); aimbs_putraw(bs, fh->dummy, 69); aimbs_putraw(bs, fh->macfileinfo, 16); aimbs_put16(bs, fh->nencode); aimbs_put16(bs, fh->nlanguage); aimbs_putraw(bs, fh->name, 64); /* XXX - filenames longer than 64B */ return 0;}/** * Create an OFT packet based on the given information, and send it on its merry way. * * @param sess The session. * @param type The subtype of the OFT packet we're sending. * @param oft_info The aim_oft_info struct with the connection and OFT * info we're sending. * @return Return 0 if no errors, otherwise return the error number. */faim_export int aim_oft_sendheader(aim_session_t *sess, fu16_t type, struct aim_oft_info *oft_info){ aim_frame_t *fr; if (!sess || !oft_info || !oft_info->conn || (oft_info->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) return -EINVAL;#if 0 /* * If you are receiving a file, the cookie should be null, if you are sending a * file, the cookie should be the same as the one used in the ICBM negotiation * SNACs. */ fh->lnameoffset = 0x1a; fh->lsizeoffset = 0x10; /* These should be the same as charset and charsubset in ICBMs */ fh->nencode = 0x0000; fh->nlanguage = 0x0000;#endif aim_oft_dirconvert_tostupid(oft_info->fh.name); if (!(fr = aim_tx_new(sess, oft_info->conn, AIM_FRAMETYPE_OFT, type, 0))) return -ENOMEM; if (aim_oft_buildheader(&fr->data, &oft_info->fh) == -1) { aim_frame_destroy(fr); return -ENOMEM; } memcpy(fr->hdr.rend.magic, "OFT2", 4); fr->hdr.rend.hdrlen = aim_bstream_curpos(&fr->data) + 8; aim_tx_enqueue(sess, fr); return 0;}/** * Handle incoming data on a rendezvous connection. This is analogous to the * consumesnac function in rxhandlers.c, and I really think this should probably * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet. * * @param sess The session. * @param fr The frame allocated for the incoming data. * @return Return 0 if the packet was handled correctly, otherwise return the * error number. */faim_internal int aim_rxdispatch_rendezvous(aim_session_t *sess, aim_frame_t *fr){ aim_conn_t *conn = fr->conn; int ret = 1; if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) { if (fr->hdr.rend.type == 0x0001) ret = handlehdr_odc(sess, conn, fr, &fr->data); else faimdprintf(sess, 0, "faim: ODC directim frame unknown, type is %04x\n", fr->hdr.rend.type); } else { aim_rxcallback_t userfunc; struct aim_fileheader_t *header = aim_oft_getheader(&fr->data); aim_oft_dirconvert_fromstupid(header->name); /* XXX - This should be client-side */ if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, fr->hdr.rend.type))) ret = userfunc(sess, fr, conn, header->bcookie, header); free(header); } if (ret == -1) aim_conn_close(conn); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -