| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 } | 554 } |
| 555 } | 555 } |
| 556 | 556 |
| 557 void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) { | 557 void AsyncSocksProxySocket::OnConnectEvent(AsyncSocket* socket) { |
| 558 SendHello(); | 558 SendHello(); |
| 559 } | 559 } |
| 560 | 560 |
| 561 void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { | 561 void AsyncSocksProxySocket::ProcessInput(char* data, size_t* len) { |
| 562 ASSERT(state_ < SS_TUNNEL); | 562 ASSERT(state_ < SS_TUNNEL); |
| 563 | 563 |
| 564 ByteBuffer response(data, *len); | 564 ByteBufferReader response(data, *len); |
| 565 | 565 |
| 566 if (state_ == SS_HELLO) { | 566 if (state_ == SS_HELLO) { |
| 567 uint8_t ver, method; | 567 uint8_t ver, method; |
| 568 if (!response.ReadUInt8(&ver) || | 568 if (!response.ReadUInt8(&ver) || |
| 569 !response.ReadUInt8(&method)) | 569 !response.ReadUInt8(&method)) |
| 570 return; | 570 return; |
| 571 | 571 |
| 572 if (ver != 5) { | 572 if (ver != 5) { |
| 573 Error(0); | 573 Error(0); |
| 574 return; | 574 return; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 } else { | 631 } else { |
| 632 Error(0); | 632 Error(0); |
| 633 return; | 633 return; |
| 634 } | 634 } |
| 635 | 635 |
| 636 state_ = SS_TUNNEL; | 636 state_ = SS_TUNNEL; |
| 637 } | 637 } |
| 638 | 638 |
| 639 // Consume parsed data | 639 // Consume parsed data |
| 640 *len = response.Length(); | 640 *len = response.Length(); |
| 641 memcpy(data, response.Data(), *len); | 641 memmove(data, response.Data(), *len); |
| 642 | 642 |
| 643 if (state_ != SS_TUNNEL) | 643 if (state_ != SS_TUNNEL) |
| 644 return; | 644 return; |
| 645 | 645 |
| 646 bool remainder = (*len > 0); | 646 bool remainder = (*len > 0); |
| 647 BufferInput(false); | 647 BufferInput(false); |
| 648 SignalConnectEvent(this); | 648 SignalConnectEvent(this); |
| 649 | 649 |
| 650 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble | 650 // FIX: if SignalConnect causes the socket to be destroyed, we are in trouble |
| 651 if (remainder) | 651 if (remainder) |
| 652 SignalReadEvent(this); // TODO: signal this?? | 652 SignalReadEvent(this); // TODO: signal this?? |
| 653 } | 653 } |
| 654 | 654 |
| 655 void AsyncSocksProxySocket::SendHello() { | 655 void AsyncSocksProxySocket::SendHello() { |
| 656 ByteBuffer request; | 656 ByteBufferWriter request; |
| 657 request.WriteUInt8(5); // Socks Version | 657 request.WriteUInt8(5); // Socks Version |
| 658 if (user_.empty()) { | 658 if (user_.empty()) { |
| 659 request.WriteUInt8(1); // Authentication Mechanisms | 659 request.WriteUInt8(1); // Authentication Mechanisms |
| 660 request.WriteUInt8(0); // No authentication | 660 request.WriteUInt8(0); // No authentication |
| 661 } else { | 661 } else { |
| 662 request.WriteUInt8(2); // Authentication Mechanisms | 662 request.WriteUInt8(2); // Authentication Mechanisms |
| 663 request.WriteUInt8(0); // No authentication | 663 request.WriteUInt8(0); // No authentication |
| 664 request.WriteUInt8(2); // Username/Password | 664 request.WriteUInt8(2); // Username/Password |
| 665 } | 665 } |
| 666 DirectSend(request.Data(), request.Length()); | 666 DirectSend(request.Data(), request.Length()); |
| 667 state_ = SS_HELLO; | 667 state_ = SS_HELLO; |
| 668 } | 668 } |
| 669 | 669 |
| 670 void AsyncSocksProxySocket::SendAuth() { | 670 void AsyncSocksProxySocket::SendAuth() { |
| 671 ByteBuffer request; | 671 ByteBufferWriter request; |
| 672 request.WriteUInt8(1); // Negotiation Version | 672 request.WriteUInt8(1); // Negotiation Version |
| 673 request.WriteUInt8(static_cast<uint8_t>(user_.size())); | 673 request.WriteUInt8(static_cast<uint8_t>(user_.size())); |
| 674 request.WriteString(user_); // Username | 674 request.WriteString(user_); // Username |
| 675 request.WriteUInt8(static_cast<uint8_t>(pass_.GetLength())); | 675 request.WriteUInt8(static_cast<uint8_t>(pass_.GetLength())); |
| 676 size_t len = pass_.GetLength() + 1; | 676 size_t len = pass_.GetLength() + 1; |
| 677 char * sensitive = new char[len]; | 677 char * sensitive = new char[len]; |
| 678 pass_.CopyTo(sensitive, true); | 678 pass_.CopyTo(sensitive, true); |
| 679 request.WriteString(sensitive); // Password | 679 request.WriteString(sensitive); // Password |
| 680 memset(sensitive, 0, len); | 680 memset(sensitive, 0, len); |
| 681 delete [] sensitive; | 681 delete [] sensitive; |
| 682 DirectSend(request.Data(), request.Length()); | 682 DirectSend(request.Data(), request.Length()); |
| 683 state_ = SS_AUTH; | 683 state_ = SS_AUTH; |
| 684 } | 684 } |
| 685 | 685 |
| 686 void AsyncSocksProxySocket::SendConnect() { | 686 void AsyncSocksProxySocket::SendConnect() { |
| 687 ByteBuffer request; | 687 ByteBufferWriter request; |
| 688 request.WriteUInt8(5); // Socks Version | 688 request.WriteUInt8(5); // Socks Version |
| 689 request.WriteUInt8(1); // CONNECT | 689 request.WriteUInt8(1); // CONNECT |
| 690 request.WriteUInt8(0); // Reserved | 690 request.WriteUInt8(0); // Reserved |
| 691 if (dest_.IsUnresolvedIP()) { | 691 if (dest_.IsUnresolvedIP()) { |
| 692 std::string hostname = dest_.hostname(); | 692 std::string hostname = dest_.hostname(); |
| 693 request.WriteUInt8(3); // DOMAINNAME | 693 request.WriteUInt8(3); // DOMAINNAME |
| 694 request.WriteUInt8(static_cast<uint8_t>(hostname.size())); | 694 request.WriteUInt8(static_cast<uint8_t>(hostname.size())); |
| 695 request.WriteString(hostname); // Destination Hostname | 695 request.WriteString(hostname); // Destination Hostname |
| 696 } else { | 696 } else { |
| 697 request.WriteUInt8(1); // IPV4 | 697 request.WriteUInt8(1); // IPV4 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 712 | 712 |
| 713 AsyncSocksProxyServerSocket::AsyncSocksProxyServerSocket(AsyncSocket* socket) | 713 AsyncSocksProxyServerSocket::AsyncSocksProxyServerSocket(AsyncSocket* socket) |
| 714 : AsyncProxyServerSocket(socket, kBufferSize), state_(SS_HELLO) { | 714 : AsyncProxyServerSocket(socket, kBufferSize), state_(SS_HELLO) { |
| 715 BufferInput(true); | 715 BufferInput(true); |
| 716 } | 716 } |
| 717 | 717 |
| 718 void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) { | 718 void AsyncSocksProxyServerSocket::ProcessInput(char* data, size_t* len) { |
| 719 // TODO: See if the whole message has arrived | 719 // TODO: See if the whole message has arrived |
| 720 ASSERT(state_ < SS_CONNECT_PENDING); | 720 ASSERT(state_ < SS_CONNECT_PENDING); |
| 721 | 721 |
| 722 ByteBuffer response(data, *len); | 722 ByteBufferReader response(data, *len); |
| 723 if (state_ == SS_HELLO) { | 723 if (state_ == SS_HELLO) { |
| 724 HandleHello(&response); | 724 HandleHello(&response); |
| 725 } else if (state_ == SS_AUTH) { | 725 } else if (state_ == SS_AUTH) { |
| 726 HandleAuth(&response); | 726 HandleAuth(&response); |
| 727 } else if (state_ == SS_CONNECT) { | 727 } else if (state_ == SS_CONNECT) { |
| 728 HandleConnect(&response); | 728 HandleConnect(&response); |
| 729 } | 729 } |
| 730 | 730 |
| 731 // Consume parsed data | 731 // Consume parsed data |
| 732 *len = response.Length(); | 732 *len = response.Length(); |
| 733 memcpy(data, response.Data(), *len); | 733 memmove(data, response.Data(), *len); |
| 734 } | 734 } |
| 735 | 735 |
| 736 void AsyncSocksProxyServerSocket::DirectSend(const ByteBuffer& buf) { | 736 void AsyncSocksProxyServerSocket::DirectSend(const ByteBufferWriter& buf) { |
| 737 BufferedReadAdapter::DirectSend(buf.Data(), buf.Length()); | 737 BufferedReadAdapter::DirectSend(buf.Data(), buf.Length()); |
| 738 } | 738 } |
| 739 | 739 |
| 740 void AsyncSocksProxyServerSocket::HandleHello(ByteBuffer* request) { | 740 void AsyncSocksProxyServerSocket::HandleHello(ByteBufferReader* request) { |
| 741 uint8_t ver, num_methods; | 741 uint8_t ver, num_methods; |
| 742 if (!request->ReadUInt8(&ver) || | 742 if (!request->ReadUInt8(&ver) || |
| 743 !request->ReadUInt8(&num_methods)) { | 743 !request->ReadUInt8(&num_methods)) { |
| 744 Error(0); | 744 Error(0); |
| 745 return; | 745 return; |
| 746 } | 746 } |
| 747 | 747 |
| 748 if (ver != 5) { | 748 if (ver != 5) { |
| 749 Error(0); | 749 Error(0); |
| 750 return; | 750 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 762 if (method == 0) { | 762 if (method == 0) { |
| 763 state_ = SS_CONNECT; | 763 state_ = SS_CONNECT; |
| 764 } else if (method == 2) { | 764 } else if (method == 2) { |
| 765 state_ = SS_AUTH; | 765 state_ = SS_AUTH; |
| 766 } else { | 766 } else { |
| 767 state_ = SS_ERROR; | 767 state_ = SS_ERROR; |
| 768 } | 768 } |
| 769 } | 769 } |
| 770 | 770 |
| 771 void AsyncSocksProxyServerSocket::SendHelloReply(uint8_t method) { | 771 void AsyncSocksProxyServerSocket::SendHelloReply(uint8_t method) { |
| 772 ByteBuffer response; | 772 ByteBufferWriter response; |
| 773 response.WriteUInt8(5); // Socks Version | 773 response.WriteUInt8(5); // Socks Version |
| 774 response.WriteUInt8(method); // Auth method | 774 response.WriteUInt8(method); // Auth method |
| 775 DirectSend(response); | 775 DirectSend(response); |
| 776 } | 776 } |
| 777 | 777 |
| 778 void AsyncSocksProxyServerSocket::HandleAuth(ByteBuffer* request) { | 778 void AsyncSocksProxyServerSocket::HandleAuth(ByteBufferReader* request) { |
| 779 uint8_t ver, user_len, pass_len; | 779 uint8_t ver, user_len, pass_len; |
| 780 std::string user, pass; | 780 std::string user, pass; |
| 781 if (!request->ReadUInt8(&ver) || | 781 if (!request->ReadUInt8(&ver) || |
| 782 !request->ReadUInt8(&user_len) || | 782 !request->ReadUInt8(&user_len) || |
| 783 !request->ReadString(&user, user_len) || | 783 !request->ReadString(&user, user_len) || |
| 784 !request->ReadUInt8(&pass_len) || | 784 !request->ReadUInt8(&pass_len) || |
| 785 !request->ReadString(&pass, pass_len)) { | 785 !request->ReadString(&pass, pass_len)) { |
| 786 Error(0); | 786 Error(0); |
| 787 return; | 787 return; |
| 788 } | 788 } |
| 789 | 789 |
| 790 // TODO: Allow for checking of credentials. | 790 // TODO: Allow for checking of credentials. |
| 791 SendAuthReply(0); | 791 SendAuthReply(0); |
| 792 state_ = SS_CONNECT; | 792 state_ = SS_CONNECT; |
| 793 } | 793 } |
| 794 | 794 |
| 795 void AsyncSocksProxyServerSocket::SendAuthReply(uint8_t result) { | 795 void AsyncSocksProxyServerSocket::SendAuthReply(uint8_t result) { |
| 796 ByteBuffer response; | 796 ByteBufferWriter response; |
| 797 response.WriteUInt8(1); // Negotiation Version | 797 response.WriteUInt8(1); // Negotiation Version |
| 798 response.WriteUInt8(result); | 798 response.WriteUInt8(result); |
| 799 DirectSend(response); | 799 DirectSend(response); |
| 800 } | 800 } |
| 801 | 801 |
| 802 void AsyncSocksProxyServerSocket::HandleConnect(ByteBuffer* request) { | 802 void AsyncSocksProxyServerSocket::HandleConnect(ByteBufferReader* request) { |
| 803 uint8_t ver, command, reserved, addr_type; | 803 uint8_t ver, command, reserved, addr_type; |
| 804 uint32_t ip; | 804 uint32_t ip; |
| 805 uint16_t port; | 805 uint16_t port; |
| 806 if (!request->ReadUInt8(&ver) || | 806 if (!request->ReadUInt8(&ver) || |
| 807 !request->ReadUInt8(&command) || | 807 !request->ReadUInt8(&command) || |
| 808 !request->ReadUInt8(&reserved) || | 808 !request->ReadUInt8(&reserved) || |
| 809 !request->ReadUInt8(&addr_type) || | 809 !request->ReadUInt8(&addr_type) || |
| 810 !request->ReadUInt32(&ip) || | 810 !request->ReadUInt32(&ip) || |
| 811 !request->ReadUInt16(&port)) { | 811 !request->ReadUInt16(&port)) { |
| 812 Error(0); | 812 Error(0); |
| 813 return; | 813 return; |
| 814 } | 814 } |
| 815 | 815 |
| 816 if (ver != 5 || command != 1 || | 816 if (ver != 5 || command != 1 || |
| 817 reserved != 0 || addr_type != 1) { | 817 reserved != 0 || addr_type != 1) { |
| 818 Error(0); | 818 Error(0); |
| 819 return; | 819 return; |
| 820 } | 820 } |
| 821 | 821 |
| 822 SignalConnectRequest(this, SocketAddress(ip, port)); | 822 SignalConnectRequest(this, SocketAddress(ip, port)); |
| 823 state_ = SS_CONNECT_PENDING; | 823 state_ = SS_CONNECT_PENDING; |
| 824 } | 824 } |
| 825 | 825 |
| 826 void AsyncSocksProxyServerSocket::SendConnectResult(int result, | 826 void AsyncSocksProxyServerSocket::SendConnectResult(int result, |
| 827 const SocketAddress& addr) { | 827 const SocketAddress& addr) { |
| 828 if (state_ != SS_CONNECT_PENDING) | 828 if (state_ != SS_CONNECT_PENDING) |
| 829 return; | 829 return; |
| 830 | 830 |
| 831 ByteBuffer response; | 831 ByteBufferWriter response; |
| 832 response.WriteUInt8(5); // Socks version | 832 response.WriteUInt8(5); // Socks version |
| 833 response.WriteUInt8((result != 0)); // 0x01 is generic error | 833 response.WriteUInt8((result != 0)); // 0x01 is generic error |
| 834 response.WriteUInt8(0); // reserved | 834 response.WriteUInt8(0); // reserved |
| 835 response.WriteUInt8(1); // IPv4 address | 835 response.WriteUInt8(1); // IPv4 address |
| 836 response.WriteUInt32(addr.ip()); | 836 response.WriteUInt32(addr.ip()); |
| 837 response.WriteUInt16(addr.port()); | 837 response.WriteUInt16(addr.port()); |
| 838 DirectSend(response); | 838 DirectSend(response); |
| 839 BufferInput(false); | 839 BufferInput(false); |
| 840 state_ = SS_TUNNEL; | 840 state_ = SS_TUNNEL; |
| 841 } | 841 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 void LoggingSocketAdapter::OnCloseEvent(AsyncSocket * socket, int err) { | 903 void LoggingSocketAdapter::OnCloseEvent(AsyncSocket * socket, int err) { |
| 904 LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_); | 904 LogMultiline(level_, label_.c_str(), false, NULL, 0, hex_mode_, &lms_); |
| 905 LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_); | 905 LogMultiline(level_, label_.c_str(), true, NULL, 0, hex_mode_, &lms_); |
| 906 LOG_V(level_) << label_ << " Closed with error: " << err; | 906 LOG_V(level_) << label_ << " Closed with error: " << err; |
| 907 AsyncSocketAdapter::OnCloseEvent(socket, err); | 907 AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 908 } | 908 } |
| 909 | 909 |
| 910 /////////////////////////////////////////////////////////////////////////////// | 910 /////////////////////////////////////////////////////////////////////////////// |
| 911 | 911 |
| 912 } // namespace rtc | 912 } // namespace rtc |
| OLD | NEW |