Commit 262255ee authored by Tao Wang's avatar Tao Wang Committed by Mike Perry
Browse files

Use Optimistic Data SOCKS variant.

This patch alters Firefox's SOCKS handshake to preemptively send data before
it is actually connected. This allows us to save a round trip during
connection setup.

See:
https://gitweb.torproject.org/torspec.git/blob/HEAD:/proposals/181-optimistic-data-client.txt
parent cd45f68f
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -1594,9 +1594,25 @@ nsSocketTransport::OnSocketReady(PRFileDesc *fd, int16_t outFlags)
        // Update poll timeout in case it was changed
        mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
    }
    else if (mState == STATE_CONNECTING) {

//STATE_SENDINGGET: handshake proceeded to state "sent connect"
//one more poll to OnSocketReady will trigger the get request, and state STATE_SENTGET
//STATE_SENTGET: continue and finish handshake
    else if (mState == STATE_SENDINGGET) {
        if ((mPollFlags & PR_POLL_WRITE) && (outFlags & ~PR_POLL_READ)) {
            mOutput.OnSocketReady(NS_OK);
        }
        mPollTimeout = mTimeouts[TIMEOUT_READ_WRITE];
        mState = STATE_SENTGET;
    }

    else if (mState == STATE_CONNECTING || mState == STATE_SENTGET) {
        PRStatus status = PR_ConnectContinue(fd, outFlags);
        if (status == PR_SUCCESS) {
        if (status == PR_SUCCESS && mState == STATE_CONNECTING) {
            OnSocketConnected();
            mState = STATE_SENDINGGET;
        }
        else if (status == PR_SUCCESS && mState == STATE_SENTGET) {
            //
            // we are connected!
            //
+3 −1
Original line number Diff line number Diff line
@@ -163,7 +163,9 @@ private:
        STATE_IDLE,
        STATE_RESOLVING,
        STATE_CONNECTING,
        STATE_TRANSFERRING
        STATE_TRANSFERRING,
        STATE_SENDINGGET,
        STATE_SENTGET
    };

    //-------------------------------------------------------------------------
+3 −1
Original line number Diff line number Diff line
@@ -80,7 +80,9 @@ public:
    void SetConnectTimeout(PRIntervalTime to);
    PRStatus DoHandshake(PRFileDesc *fd, int16_t oflags = -1);
    int16_t GetPollFlags() const;
    bool IsConnected() const { return mState == SOCKS_CONNECTED; }
    bool IsConnected() const { return (mState == SOCKS_CONNECTED ||
                                       mState == SOCKS5_READ_CONNECT_RESPONSE_TOP); }
 
    void ForgetFD() { mFD = nullptr; }

private: