Mercurial > pidgin
comparison src/protocols/oscar/ft.c @ 11369:ab0fa7cd61cc
[gaim-migrate @ 13593]
Bring all the changes to OSCAR file transfers from oldstatus to HEAD.
Add documentation resulting from my Summer of Code project to CVS including
the original editable files (in OpenOffice.org 2.0 format).
committer: Tailor Script <tailor@pidgin.im>
author | Jonathan Clark <ardentlygnarly> |
---|---|
date | Tue, 30 Aug 2005 05:21:58 +0000 |
parents | 7d31d61e6438 |
children | 48244c196228 |
comparison
equal
deleted
inserted
replaced
11368:26e316f9e136 | 11369:ab0fa7cd61cc |
---|---|
1 /* | 1 /* |
2 * Oscar File transfer (OFT) and Oscar Direct Connect (ODC). | 2 * Oscar File transfer (OFT) and Oscar Direct Connect (ODC). |
3 * (ODC is also referred to as DirectIM and IM Image.) | 3 * (ODC is also referred to as DirectIM and IM Image.) |
4 * | 4 * |
5 * There are a few static helper functions at the top, then | 5 * There are a few static helper functions at the top, then |
6 * ODC stuff, then ft stuff. | 6 * ODC stuff, then ft stuff. |
7 * | 7 * |
8 * I feel like this is a good place to explain OFT, so I'm going to | 8 * I feel like this is a good place to explain OFT, so I'm going to |
9 * do just that. Each OFT packet has a header type. I guess this | 9 * do just that. Each OFT packet has a header type. I guess this |
10 * is pretty similar to the subtype of a SNAC packet. The type | 10 * is pretty similar to the subtype of a SNAC packet. The type |
11 * basically tells the other client the meaning of the OFT packet. | 11 * basically tells the other client the meaning of the OFT packet. |
12 * There are two distinct types of file transfer, which I usually | 12 * There are two distinct types of file transfer, which I usually |
13 * call "sendfile" and "getfile." Sendfile is when you send a file | 13 * call "sendfile" and "getfile." Sendfile is when you send a file |
14 * to another AIM user. Getfile is when you share a group of files, | 14 * to another AIM user. Getfile is when you share a group of files, |
15 * and other users request that you send them the files. | 15 * and other users request that you send them the files. |
16 * | 16 * |
17 * A typical sendfile file transfer goes like this: | 17 * A typical sendfile file transfer goes like this: |
18 * 1) Sender sends a channel 2 ICBM telling the other user that | 18 * 1) Sender sends a channel 2 ICBM telling the other user that |
19 * we want to send them a file. At the same time, we open a | 19 * we want to send them a file. At the same time, we open a |
20 * listener socket (this should be done before sending the | 20 * listener socket (this should be done before sending the |
21 * ICBM) on some port, and wait for them to connect to us. | 21 * ICBM) on some port, and wait for them to connect to us. |
22 * The ICBM we sent should contain our IP address and the port | 22 * The ICBM we sent should contain our IP address and the port |
23 * number that we're listening on. | 23 * number that we're listening on. |
24 * 2) The receiver connects to the sender on the given IP address | 24 * 2) The receiver connects to the sender on the given IP address |
25 * and port. After the connection is established, the receiver | 25 * and port. After the connection is established, the receiver |
26 * sends an ICBM signifying that we are ready and waiting. | 26 * sends an ICBM signifying that we are ready and waiting. |
27 * 3) The sender sends an OFT PROMPT message over the OFT | 27 * 3) The sender sends an OFT PROMPT message over the OFT |
28 * connection. | 28 * connection. |
29 * 4) The receiver of the file sends back an exact copy of this | 29 * 4) The receiver of the file sends back an exact copy of this |
30 * OFT packet, except the cookie is filled in with the cookie | 30 * OFT packet, except the cookie is filled in with the cookie |
31 * from the ICBM. I think this might be an attempt to verify | 31 * from the ICBM. I think this might be an attempt to verify |
32 * that the user that is connected is actually the guy that | 32 * that the user that is connected is actually the guy that |
33 * we sent the ICBM to. Oh, I've been calling this the ACK. | 33 * we sent the ICBM to. Oh, I've been calling this the ACK. |
34 * 5) The sender starts sending raw data across the connection | 34 * 5) The sender starts sending raw data across the connection |
35 * until the entire file has been sent. | 35 * until the entire file has been sent. |
36 * 6) The receiver knows the file is finished because the sender | 36 * 6) The receiver knows the file is finished because the sender |
37 * sent the file size in an earlier OFT packet. So then the | 37 * sent the file size in an earlier OFT packet. So then the |
38 * receiver sends the DONE thingy (after filling in the | 38 * receiver sends the DONE thingy (after filling in the |
39 * "received" checksum and size) and closes the connection. | 39 * "received" checksum and size) and closes the connection. |
40 */ | 40 */ |
41 | 41 |
42 #define FAIM_INTERNAL | 42 #define FAIM_INTERNAL |
43 #ifdef HAVE_CONFIG_H | 43 #ifdef HAVE_CONFIG_H |
110 } | 110 } |
111 | 111 |
112 /** | 112 /** |
113 * Calculate oft checksum of buffer | 113 * Calculate oft checksum of buffer |
114 * | 114 * |
115 * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The | 115 * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The |
116 * checksum is kind of a rolling checksum thing, so each time you get bytes | 116 * checksum is kind of a rolling checksum thing, so each time you get bytes |
117 * of a file you just call this puppy and it updates the checksum. You can | 117 * of a file you just call this puppy and it updates the checksum. You can |
118 * calculate the checksum of an entire file by calling this in a while or a | 118 * calculate the checksum of an entire file by calling this in a while or a |
119 * for loop, or something. | 119 * for loop, or something. |
120 * | 120 * |
121 * Thanks to Graham Booker for providing this improved checksum routine, | 121 * Thanks to Graham Booker for providing this improved checksum routine, |
122 * which is simpler and should be more accurate than Josh Myer's original | 122 * which is simpler and should be more accurate than Josh Myer's original |
123 * code. -- wtm | 123 * code. -- wtm |
124 * | 124 * |
125 * This algorithm works every time I have tried it. The other fails | 125 * This algorithm works every time I have tried it. The other fails |
126 * sometimes. So, AOL who thought this up? It has got to be the weirdest | 126 * sometimes. So, AOL who thought this up? It has got to be the weirdest |
127 * checksum I have ever seen. | 127 * checksum I have ever seen. |
128 * | 128 * |
129 * @param buffer Buffer of data to checksum. Man I'd like to buff her... | 129 * @param buffer Buffer of data to checksum. Man I'd like to buff her... |
130 * @param bufsize Size of buffer. | 130 * @param bufsize Size of buffer. |
131 * @param prevcheck Previous checksum. | 131 * @param prevcheck Previous checksum. |
142 val = buffer[i]; | 142 val = buffer[i]; |
143 else | 143 else |
144 val = buffer[i] << 8; | 144 val = buffer[i] << 8; |
145 check -= val; | 145 check -= val; |
146 /* | 146 /* |
147 * The following appears to be necessary.... It happens | 147 * The following appears to be necessary.... It happens |
148 * every once in a while and the checksum doesn't fail. | 148 * every once in a while and the checksum doesn't fail. |
149 */ | 149 */ |
150 if (check > oldcheck) | 150 if (check > oldcheck) |
151 check--; | 151 check--; |
152 } | 152 } |
242 /** | 242 /** |
243 * Send client-to-client typing notification over an established direct connection. | 243 * Send client-to-client typing notification over an established direct connection. |
244 * | 244 * |
245 * @param sess The session. | 245 * @param sess The session. |
246 * @param conn The already-connected ODC connection. | 246 * @param conn The already-connected ODC connection. |
247 * @param typing If 0x0002, sends a "typing" message, 0x0001 sends "typed," and | 247 * @param typing If 0x0002, sends a "typing" message, 0x0001 sends "typed," and |
248 * 0x0000 sends "stopped." | 248 * 0x0000 sends "stopped." |
249 * @return Return 0 if no errors, otherwise return the error number. | 249 * @return Return 0 if no errors, otherwise return the error number. |
250 */ | 250 */ |
251 faim_export int aim_odc_send_typing(aim_session_t *sess, aim_conn_t *conn, int typing) | 251 faim_export int aim_odc_send_typing(aim_session_t *sess, aim_conn_t *conn, int typing) |
252 { | 252 { |
315 } | 315 } |
316 | 316 |
317 /** | 317 /** |
318 * Send client-to-client IM over an established direct connection. | 318 * Send client-to-client IM over an established direct connection. |
319 * Call this just like you would aim_send_im, to send a directim. | 319 * Call this just like you would aim_send_im, to send a directim. |
320 * | 320 * |
321 * @param sess The session. | 321 * @param sess The session. |
322 * @param conn The already-connected ODC connection. | 322 * @param conn The already-connected ODC connection. |
323 * @param msg Null-terminated string to send. | 323 * @param msg Null-terminated string to send. |
324 * @param len The length of the message to send, including binary data. | 324 * @param len The length of the message to send, including binary data. |
325 * @param encoding See the AIM_CHARSET_* defines in aim.h | 325 * @param encoding See the AIM_CHARSET_* defines in aim.h |
382 aimbs_put16(hdrbs, 0x0000); | 382 aimbs_put16(hdrbs, 0x0000); |
383 aimbs_put8(hdrbs, 0x00); | 383 aimbs_put8(hdrbs, 0x00); |
384 | 384 |
385 /* end of hdr2 */ | 385 /* end of hdr2 */ |
386 | 386 |
387 #if 0 /* XXX - this is how you send buddy icon info... */ | 387 #if 0 /* XXX - this is how you send buddy icon info... */ |
388 aimbs_put16(hdrbs, 0x0008); | 388 aimbs_put16(hdrbs, 0x0008); |
389 aimbs_put16(hdrbs, 0x000c); | 389 aimbs_put16(hdrbs, 0x000c); |
390 aimbs_put16(hdrbs, 0x0000); | 390 aimbs_put16(hdrbs, 0x0000); |
391 aimbs_put16(hdrbs, 0x1466); | 391 aimbs_put16(hdrbs, 0x1466); |
392 aimbs_put16(hdrbs, 0x0001); | 392 aimbs_put16(hdrbs, 0x0001); |
444 /** | 444 /** |
445 * Find the conn of a direct connection with the given buddy. | 445 * Find the conn of a direct connection with the given buddy. |
446 * | 446 * |
447 * @param sess The session. | 447 * @param sess The session. |
448 * @param sn The screen name of the buddy whose direct connection you want to find. | 448 * @param sn The screen name of the buddy whose direct connection you want to find. |
449 * @return The conn for the direct connection with the given buddy, or NULL if no | 449 * @return The conn for the direct connection with the given buddy, or NULL if no |
450 * connection was found. | 450 * connection was found. |
451 */ | 451 */ |
452 faim_export aim_conn_t *aim_odc_getconn(aim_session_t *sess, const char *sn) | 452 faim_export aim_conn_t *aim_odc_getconn(aim_session_t *sess, const char *sn) |
453 { | 453 { |
454 aim_conn_t *cur; | 454 aim_conn_t *cur; |
532 /** | 532 /** |
533 * Connect directly to the given buddy for directim. | 533 * Connect directly to the given buddy for directim. |
534 * | 534 * |
535 * This is a wrapper for aim_newconn. | 535 * This is a wrapper for aim_newconn. |
536 * | 536 * |
537 * If addr is NULL, the socket is not created, but the connection is | 537 * If addr is NULL, the socket is not created, but the connection is |
538 * allocated and setup to connect. | 538 * allocated and setup to connect. |
539 * | 539 * |
540 * @param sess The Godly session. | 540 * @param sess The Godly session. |
541 * @param sn The screen name we're connecting to. I hope it's a girl... | 541 * @param sn The screen name we're connecting to. I hope it's a girl... |
542 * @param addr Address to connect to. | 542 * @param addr Address to connect to. |
629 } | 629 } |
630 | 630 |
631 while (payloadlength - recvd) { | 631 while (payloadlength - recvd) { |
632 if (payloadlength - recvd >= 1024) | 632 if (payloadlength - recvd >= 1024) |
633 i = aim_recv(conn->fd, &msg[recvd], 1024); | 633 i = aim_recv(conn->fd, &msg[recvd], 1024); |
634 else | 634 else |
635 i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd); | 635 i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd); |
636 if (i <= 0) { | 636 if (i <= 0) { |
637 free(msg); | 637 free(msg); |
638 free(snptr); | 638 free(snptr); |
639 return -1; | 639 return -1; |
640 } | 640 } |
641 recvd = recvd + i; | 641 recvd = recvd + i; |
642 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER))) | 642 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER))) |
643 ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength); | 643 ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength); |
644 } | 644 } |
645 | 645 |
646 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING))) | 646 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING))) |
647 ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg); | 647 ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg); |
648 | 648 |
649 free(msg); | 649 free(msg); |
650 } | 650 } |
652 free(snptr); | 652 free(snptr); |
653 | 653 |
654 return ret; | 654 return ret; |
655 } | 655 } |
656 | 656 |
657 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) | 657 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, int send_or_recv, int method, int stage) |
658 { | 658 { |
659 struct aim_oft_info *new; | 659 struct aim_oft_info *new; |
660 | 660 |
661 if (!sess) | 661 if (!sess) |
662 return NULL; | 662 return NULL; |
665 return NULL; | 665 return NULL; |
666 | 666 |
667 new->sess = sess; | 667 new->sess = sess; |
668 if (cookie) | 668 if (cookie) |
669 memcpy(new->cookie, cookie, 8); | 669 memcpy(new->cookie, cookie, 8); |
670 else | |
671 aim_im_makecookie(new->cookie); | |
670 if (ip) | 672 if (ip) |
671 new->clientip = strdup(ip); | 673 new->clientip = strdup(ip); |
674 else | |
675 new->clientip = NULL; | |
672 if (sn) | 676 if (sn) |
673 new->sn = strdup(sn); | 677 new->sn = strdup(sn); |
678 else | |
679 new->sn = NULL; | |
680 new->method = method; | |
681 new->send_or_recv = send_or_recv; | |
682 new->stage = stage; | |
674 new->port = port; | 683 new->port = port; |
684 new->xfer_reffed = FALSE; | |
675 new->success = FALSE; | 685 new->success = FALSE; |
676 new->fh.totfiles = 1; | 686 new->fh.totfiles = 1; |
677 new->fh.filesleft = 1; | 687 new->fh.filesleft = 1; |
678 new->fh.totparts = 1; | 688 new->fh.totparts = 1; |
679 new->fh.partsleft = 1; | 689 new->fh.partsleft = 1; |
694 sess->oft_info = new; | 704 sess->oft_info = new; |
695 | 705 |
696 return new; | 706 return new; |
697 } | 707 } |
698 | 708 |
699 /** | 709 faim_export struct aim_rv_proxy_info *aim_rv_proxy_createinfo(aim_session_t *sess, const fu8_t *cookie, |
700 * Remove the given oft_info struct from the oft_info linked list, and | 710 fu16_t port) |
711 { | |
712 struct aim_rv_proxy_info *proxy_info; | |
713 | |
714 if (!(proxy_info = (struct aim_rv_proxy_info*)calloc(1, sizeof(struct aim_rv_proxy_info)))) | |
715 return NULL; | |
716 | |
717 proxy_info->sess = sess; | |
718 proxy_info->port = port; | |
719 proxy_info->packet_ver = AIM_RV_PROXY_PACKETVER_DFLT; | |
720 proxy_info->unknownA = AIM_RV_PROXY_UNKNOWNA_DFLT; | |
721 | |
722 if (cookie) | |
723 memcpy(proxy_info->cookie, cookie, 8); | |
724 | |
725 return proxy_info; | |
726 } | |
727 | |
728 /** | |
729 * Remove the given oft_info struct from the oft_info linked list, and | |
701 * then free its memory. | 730 * then free its memory. |
702 * | 731 * |
703 * @param sess The session. | 732 * @param sess The session. |
704 * @param oft_info The aim_oft_info struct that we're destroying. | 733 * @param oft_info The aim_oft_info struct that we're destroying. |
705 * @return Return 0 if no errors, otherwise return the error number. | 734 * @return Return 0 if no errors, otherwise return the error number. |
730 } | 759 } |
731 | 760 |
732 /** | 761 /** |
733 * Creates a listener socket so the other dude can connect to us. | 762 * Creates a listener socket so the other dude can connect to us. |
734 * | 763 * |
735 * You'll want to set up some kind of watcher on this socket. | 764 * You'll want to set up some kind of watcher on this socket. |
736 * When the state changes, call aim_handlerendconnection with | 765 * When the state changes, call aim_handlerendconnection with |
737 * the connection returned by this. aim_handlerendconnection | 766 * the connection returned by this. aim_handlerendconnection |
738 * will accept the pending connection and stop listening. | 767 * will accept the pending connection and stop listening. |
739 * | 768 * |
740 * @param sess The session. | 769 * @param sess The session. |
741 * @param oft_info File transfer information associated with this | 770 * @param oft_info File transfer information associated with this |
742 * connection. | 771 * connection. |
743 * @return Return 0 if no errors, otherwise return the error number. | 772 * @return Return 0 if no errors, otherwise return the error number. |
744 */ | 773 */ |
745 faim_export int aim_sendfile_listen(aim_session_t *sess, struct aim_oft_info *oft_info, int listenfd) | 774 faim_export int aim_sendfile_listen(aim_session_t *sess, struct aim_oft_info *oft_info, int listenfd) |
746 { | 775 { |
800 fh->nlanguage = aimbs_get16(bs); | 829 fh->nlanguage = aimbs_get16(bs); |
801 aimbs_getrawbuf(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */ | 830 aimbs_getrawbuf(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */ |
802 fh->name[63] = '\0'; | 831 fh->name[63] = '\0'; |
803 | 832 |
804 return fh; | 833 return fh; |
805 } | 834 } |
806 | 835 |
807 /** | 836 /** |
808 * Fills a buffer with network-order fh data | 837 * Fills a buffer with network-order fh data |
809 * | 838 * |
810 * @param bs A bstream to fill -- automatically initialized | 839 * @param bs A bstream to fill -- automatically initialized |
811 * @param fh A struct aim_fileheader_t to get data from. | 840 * @param fh A struct aim_fileheader_t to get data from. |
812 * @return Return non-zero on error. | 841 * @return Return non-zero on error. |
813 */ | 842 */ |
814 static int aim_oft_buildheader(aim_bstream_t *bs, struct aim_fileheader_t *fh) | 843 static int aim_oft_buildheader(aim_bstream_t *bs, struct aim_fileheader_t *fh) |
815 { | 844 { |
816 fu8_t *hdr; | 845 fu8_t *hdr; |
817 | 846 |
818 if (!bs || !fh) | 847 if (!bs || !fh) |
819 return -EINVAL; | 848 return -EINVAL; |
820 | 849 |
855 /** | 884 /** |
856 * Create an OFT packet based on the given information, and send it on its merry way. | 885 * Create an OFT packet based on the given information, and send it on its merry way. |
857 * | 886 * |
858 * @param sess The session. | 887 * @param sess The session. |
859 * @param type The subtype of the OFT packet we're sending. | 888 * @param type The subtype of the OFT packet we're sending. |
860 * @param oft_info The aim_oft_info struct with the connection and OFT | 889 * @param oft_info The aim_oft_info struct with the connection and OFT |
861 * info we're sending. | 890 * info we're sending. |
862 * @return Return 0 if no errors, otherwise return the error number. | 891 * @return Return 0 if no errors, otherwise return the error number. |
863 */ | 892 */ |
864 faim_export int aim_oft_sendheader(aim_session_t *sess, fu16_t type, struct aim_oft_info *oft_info) | 893 faim_export int aim_oft_sendheader(aim_session_t *sess, fu16_t type, struct aim_oft_info *oft_info) |
865 { | 894 { |
868 if (!sess || !oft_info || !oft_info->conn || (oft_info->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) | 897 if (!sess || !oft_info || !oft_info->conn || (oft_info->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) |
869 return -EINVAL; | 898 return -EINVAL; |
870 | 899 |
871 #if 0 | 900 #if 0 |
872 /* | 901 /* |
873 * If you are receiving a file, the cookie should be null, if you are sending a | 902 * If you are receiving a file, the cookie should be null, if you are sending a |
874 * file, the cookie should be the same as the one used in the ICBM negotiation | 903 * file, the cookie should be the same as the one used in the ICBM negotiation |
875 * SNACs. | 904 * SNACs. |
876 */ | 905 */ |
877 fh->lnameoffset = 0x1a; | 906 fh->lnameoffset = 0x1a; |
878 fh->lsizeoffset = 0x10; | 907 fh->lsizeoffset = 0x10; |
879 | 908 |
899 | 928 |
900 return 0; | 929 return 0; |
901 } | 930 } |
902 | 931 |
903 /** | 932 /** |
904 * Handle incoming data on a rendezvous connection. This is analogous to the | 933 * Create a rendezvous "init recv" packet and send it on its merry way. |
905 * consumesnac function in rxhandlers.c, and I really think this should probably | 934 * This is the first packet sent to the proxy server by the second client |
935 * involved in this rendezvous proxy session. | |
936 * | |
937 * @param sess The session. | |
938 * @param proxy_info Changable pieces of data for this packet | |
939 * @return Return 0 if no errors, otherwise return the error number. | |
940 */ | |
941 faim_export int aim_rv_proxy_init_recv(struct aim_rv_proxy_info *proxy_info) | |
942 { | |
943 //aim_tlvlist_t *tlvlist_sendfile; | |
944 aim_bstream_t bs; | |
945 fu8_t *bs_raw; | |
946 fu16_t packet_len; | |
947 fu8_t sn_len; | |
948 int err; | |
949 | |
950 err = 0; | |
951 | |
952 if (!proxy_info) | |
953 return -EINVAL; | |
954 | |
955 sn_len = strlen(proxy_info->sess->sn); | |
956 packet_len = 2 + 2 /* packet_len, packet_ver */ | |
957 + 2 + 4 /* cmd_type, unknownA */ | |
958 + 2 /* flags */ | |
959 + 1 + sn_len /* Length/value pair for screenname */ | |
960 + 8 /* ICBM Cookie */ | |
961 + 2 /* port */ | |
962 + 2 + 2 + 16; /* TLV for Filesend capability block */ | |
963 | |
964 if (!(bs_raw = malloc(packet_len))) | |
965 return -ENOMEM; | |
966 | |
967 aim_bstream_init(&bs, bs_raw, packet_len); | |
968 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */ | |
969 aimbs_put16(&bs, proxy_info->packet_ver); | |
970 aimbs_put16(&bs, AIM_RV_PROXY_INIT_RECV); | |
971 aimbs_put32(&bs, proxy_info->unknownA); | |
972 aimbs_put16(&bs, proxy_info->flags); | |
973 aimbs_put8(&bs, sn_len); | |
974 aimbs_putraw(&bs, proxy_info->sess->sn, sn_len); | |
975 aimbs_put16(&bs, proxy_info->port); | |
976 aimbs_putraw(&bs, proxy_info->cookie, 8); | |
977 | |
978 aimbs_put16(&bs, 0x0001); /* Type */ | |
979 aimbs_put16(&bs, 16); /* Length */ | |
980 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */ | |
981 | |
982 // TODO: Use built-in TLV | |
983 //aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE); | |
984 //aim_tlvlist_write(&bs, &tlvlist_sendfile); | |
985 | |
986 aim_bstream_rewind(&bs); | |
987 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len) | |
988 err = errno; | |
989 proxy_info->conn->lastactivity = time(NULL); | |
990 | |
991 //aim_tlvlist_free(tlvlist_sendfile); | |
992 free(bs_raw); | |
993 | |
994 return err; | |
995 } | |
996 | |
997 | |
998 /** | |
999 * Create a rendezvous "init send" packet and send it on its merry way. | |
1000 * This is the first packet sent to the proxy server by the client | |
1001 * first indicating that this will be a proxied connection | |
1002 * | |
1003 * @param sess The session. | |
1004 * @param proxy_info Changable pieces of data for this packet | |
1005 * @return Return 0 if no errors, otherwise return the error number. | |
1006 */ | |
1007 faim_export int aim_rv_proxy_init_send(struct aim_rv_proxy_info *proxy_info) | |
1008 { | |
1009 //aim_tlvlist_t *tlvlist_sendfile; | |
1010 aim_bstream_t bs; | |
1011 fu8_t *bs_raw; | |
1012 fu16_t packet_len; | |
1013 fu8_t sn_len; | |
1014 int err; | |
1015 | |
1016 err = 0; | |
1017 | |
1018 if (!proxy_info) | |
1019 return -EINVAL; | |
1020 | |
1021 sn_len = strlen(proxy_info->sess->sn); | |
1022 packet_len = 2 + 2 /* packet_len, packet_ver */ | |
1023 + 2 + 4 /* cmd_type, unknownA */ | |
1024 + 2 /* flags */ | |
1025 + 1 + sn_len /* Length/value pair for screenname */ | |
1026 + 8 /* ICBM Cookie */ | |
1027 + 2 + 2 + 16; /* TLV for Filesend capability block */ | |
1028 | |
1029 if (!(bs_raw = malloc(packet_len))) | |
1030 return -ENOMEM; | |
1031 | |
1032 aim_bstream_init(&bs, bs_raw, packet_len); | |
1033 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */ | |
1034 aimbs_put16(&bs, proxy_info->packet_ver); | |
1035 aimbs_put16(&bs, AIM_RV_PROXY_INIT_SEND); | |
1036 aimbs_put32(&bs, proxy_info->unknownA); | |
1037 aimbs_put16(&bs, proxy_info->flags); | |
1038 aimbs_put8(&bs, sn_len); | |
1039 aimbs_putraw(&bs, proxy_info->sess->sn, sn_len); | |
1040 aimbs_putraw(&bs, proxy_info->cookie, 8); | |
1041 | |
1042 aimbs_put16(&bs, 0x0001); /* Type */ | |
1043 aimbs_put16(&bs, 16); /* Length */ | |
1044 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */ | |
1045 | |
1046 // TODO: Use built-in TLV | |
1047 //aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE); | |
1048 //aim_tlvlist_write(&bs, &tlvlist_sendfile); | |
1049 | |
1050 aim_bstream_rewind(&bs); | |
1051 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len) | |
1052 err = errno; | |
1053 proxy_info->conn->lastactivity = time(NULL); | |
1054 | |
1055 //aim_tlvlist_free(tlvlist_sendfile); | |
1056 free(bs_raw); | |
1057 | |
1058 return err; | |
1059 } | |
1060 | |
1061 /** | |
1062 * Handle incoming data on a rendezvous connection. This is analogous to the | |
1063 * consumesnac function in rxhandlers.c, and I really think this should probably | |
906 * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet. | 1064 * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet. |
907 * | 1065 * |
908 * @param sess The session. | 1066 * @param sess The session. |
909 * @param fr The frame allocated for the incoming data. | 1067 * @param fr The frame allocated for the incoming data. |
910 * @return Return 0 if the packet was handled correctly, otherwise return the | 1068 * @return Return 0 if the packet was handled correctly, otherwise return the |
911 * error number. | 1069 * error number. |
912 */ | 1070 */ |
913 faim_internal int aim_rxdispatch_rendezvous(aim_session_t *sess, aim_frame_t *fr) | 1071 faim_internal int aim_rxdispatch_rendezvous(aim_session_t *sess, aim_frame_t *fr) |
914 { | 1072 { |
915 aim_conn_t *conn = fr->conn; | 1073 aim_conn_t *conn = fr->conn; |
935 if (ret == -1) | 1093 if (ret == -1) |
936 aim_conn_close(conn); | 1094 aim_conn_close(conn); |
937 | 1095 |
938 return ret; | 1096 return ret; |
939 } | 1097 } |
1098 | |
1099 /** | |
1100 * Handle incoming data on a rendezvous proxy connection. This is similar to | |
1101 * aim_rxdispatch_rendezvous above and should probably be kept with that function. | |
1102 * | |
1103 * @param sess The session. | |
1104 * @param fr The frame allocated for the incoming data. | |
1105 * @return Return 0 if the packet was handled correctly, otherwise return the | |
1106 * error number. | |
1107 */ | |
1108 faim_internal struct aim_rv_proxy_info *aim_rv_proxy_read(aim_session_t *sess, aim_conn_t *conn) | |
1109 { | |
1110 aim_bstream_t bs_hdr; | |
1111 fu8_t hdr_buf[AIM_RV_PROXY_HDR_LEN]; | |
1112 aim_bstream_t bs_body; /* The body (everything but the header) of the packet */ | |
1113 fu8_t *body_buf = NULL; | |
1114 fu8_t body_len; | |
1115 | |
1116 char str_ip[30] = {""}; | |
1117 fu8_t ip_temp[4]; | |
1118 | |
1119 fu16_t len; | |
1120 struct aim_rv_proxy_info *proxy_info; | |
1121 | |
1122 if(!(proxy_info = malloc(sizeof(struct aim_rv_proxy_info)))) | |
1123 return NULL; | |
1124 | |
1125 aim_bstream_init(&bs_hdr, hdr_buf, AIM_RV_PROXY_HDR_LEN); | |
1126 if (aim_bstream_recv(&bs_hdr, conn->fd, AIM_RV_PROXY_HDR_LEN) == AIM_RV_PROXY_HDR_LEN) { | |
1127 aim_bstream_rewind(&bs_hdr); | |
1128 len = aimbs_get16(&bs_hdr); | |
1129 proxy_info->packet_ver = aimbs_get16(&bs_hdr); | |
1130 proxy_info->cmd_type = aimbs_get16(&bs_hdr); | |
1131 proxy_info->unknownA = aimbs_get32(&bs_hdr); | |
1132 proxy_info->flags = aimbs_get16(&bs_hdr); | |
1133 if(proxy_info->cmd_type == AIM_RV_PROXY_READY) { | |
1134 /* Do a little victory dance | |
1135 * A ready packet contains no additional information */ | |
1136 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ERROR) { | |
1137 if(len == AIM_RV_PROXY_ERROR_LEN - 2) { | |
1138 body_len = AIM_RV_PROXY_ERROR_LEN - AIM_RV_PROXY_HDR_LEN; | |
1139 body_buf = malloc(body_len); | |
1140 aim_bstream_init(&bs_body, body_buf, body_len); | |
1141 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) { | |
1142 aim_bstream_rewind(&bs_body); | |
1143 proxy_info->err_code = aimbs_get16(&bs_body); | |
1144 } else { | |
1145 gaim_debug_warning("oscar","error reading rv proxy error packet\n"); | |
1146 aim_conn_close(conn); | |
1147 free(proxy_info); | |
1148 proxy_info = NULL; | |
1149 } | |
1150 } else { | |
1151 gaim_debug_warning("oscar","invalid length for proxy error packet\n"); | |
1152 free(proxy_info); | |
1153 proxy_info = NULL; | |
1154 } | |
1155 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ACK) { | |
1156 if(len == AIM_RV_PROXY_ACK_LEN - 2) { | |
1157 body_len = AIM_RV_PROXY_ACK_LEN - AIM_RV_PROXY_HDR_LEN; | |
1158 body_buf = malloc(body_len); | |
1159 aim_bstream_init(&bs_body, body_buf, body_len); | |
1160 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) { | |
1161 aim_bstream_rewind(&bs_body); | |
1162 proxy_info->port = aimbs_get16(&bs_body); | |
1163 int i; | |
1164 for(i=0; i<4; i++) | |
1165 ip_temp[i] = aimbs_get8(&bs_body); | |
1166 snprintf(str_ip, sizeof(str_ip), "%hhu.%hhu.%hhu.%hhu", | |
1167 ip_temp[0], ip_temp[1], | |
1168 ip_temp[2], ip_temp[3]); | |
1169 proxy_info->ip = strdup(str_ip); | |
1170 } else { | |
1171 gaim_debug_warning("oscar","error reading rv proxy error packet\n"); | |
1172 aim_conn_close(conn); | |
1173 free(proxy_info); | |
1174 proxy_info = NULL; | |
1175 } | |
1176 } else { | |
1177 gaim_debug_warning("oscar","invalid length for proxy error packet\n"); | |
1178 free(proxy_info); | |
1179 proxy_info = NULL; | |
1180 } | |
1181 } else { | |
1182 gaim_debug_warning("oscar","unknown type for aim rendezvous proxy packet\n"); | |
1183 } | |
1184 } else { | |
1185 gaim_debug_warning("oscar","error reading header of rv proxy packet\n"); | |
1186 aim_conn_close(conn); | |
1187 free(proxy_info); | |
1188 proxy_info = NULL; | |
1189 } | |
1190 if(body_buf) { | |
1191 free(body_buf); | |
1192 body_buf = NULL; | |
1193 } | |
1194 return proxy_info; | |
1195 } |