Esempio n. 1
0
File: dcc.cpp Progetto: aszrul/znc
void CDCCSock::Disconnected() {
    const CString sStart = ((m_bSend) ? "DCC -> [" : "DCC <- [") +
                           m_sRemoteNick + "][" + m_sFileName + "] - ";

    DEBUG(GetSockName() << " == Disconnected()");

    if (m_uBytesSoFar > m_uFileSize) {
        if (m_bSend) {
            m_pModule->PutModule(t_f("Sending [{1}] to [{2}]: Too much data!")(
                m_sFileName, m_sRemoteNick));
        } else {
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}]: Too much data!")(
                    m_sFileName, m_sRemoteNick));
        }
    } else if (m_uBytesSoFar == m_uFileSize) {
        if (m_bSend) {
            m_pModule->PutModule(
                t_f("Sending [{1}] to [{2}] completed at {3} KiB/s")(
                    m_sFileName, m_sRemoteNick,
                    static_cast<int>(GetAvgWrite() / 1024.0)));
        } else {
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}] completed at {3} KiB/s")(
                    m_sFileName, m_sRemoteNick,
                    static_cast<int>(GetAvgRead() / 1024.0)));
        }
    } else {
        m_pModule->PutModule(sStart + "Incomplete!");
    }
}
Esempio n. 2
0
void CDCCSock::SendPacket() {
	if (!m_pFile) {
		m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - File closed prematurely.");
		Close();
		return;
	}

	if (GetInternalWriteBuffer().size() > 1024 * 1024) {
		// There is still enough data to be written, don't add more
		// stuff to that buffer.
		DEBUG("SendPacket(): Skipping send, buffer still full enough [" << GetInternalWriteBuffer().size() << "]["
				<< m_sRemoteNick << "][" << m_sFileName << "]");
		return;
	}

	char szBuf[4096];
	int iLen = m_pFile->Read(szBuf, 4096);

	if (iLen < 0) {
		m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - Error reading from file.");
		Close();
		return;
	}

	if (iLen > 0) {
		Write(szBuf, iLen);
		m_uBytesSoFar += iLen;
	}
}
Esempio n. 3
0
File: dcc.cpp Progetto: aszrul/znc
void CDCCSock::Timeout() {
    DEBUG(GetSockName() << " == Timeout()");
    if (m_bSend) {
        m_pModule->PutModule(t_f("Sending [{1}] to [{2}]: Timeout.")(
            m_sFileName, m_sRemoteNick));
    } else {
        m_pModule->PutModule(
            t_f("Receiving [{1}] from [{2}]: Timeout.")(
                m_sFileName, m_sRemoteNick));
    }
}
Esempio n. 4
0
File: dcc.cpp Progetto: aszrul/znc
void CDCCSock::ConnectionRefused() {
    DEBUG(GetSockName() << " == ConnectionRefused()");
    if (m_bSend) {
        m_pModule->PutModule(t_f("Sending [{1}] to [{2}]: Connection refused.")(
            m_sFileName, m_sRemoteNick));
    } else {
        m_pModule->PutModule(
            t_f("Receiving [{1}] from [{2}]: Connection refused.")(
                m_sFileName, m_sRemoteNick));
    }
}
Esempio n. 5
0
File: dcc.cpp Progetto: aszrul/znc
void CDCCSock::SockError(int iErrno, const CString& sDescription) {
    DEBUG(GetSockName() << " == SockError(" << iErrno << ", " << sDescription
                        << ")");
    if (m_bSend) {
        m_pModule->PutModule(
            t_f("Sending [{1}] to [{2}]: Socket error {3}: {4}")(
                m_sFileName, m_sRemoteNick, iErrno, sDescription));
    } else {
        m_pModule->PutModule(
            t_f("Receiving [{1}] from [{2}]: Socket error {3}: {4}")(
                m_sFileName, m_sRemoteNick, iErrno, sDescription));
    }
}
Esempio n. 6
0
File: dcc.cpp Progetto: aszrul/znc
void CDCCSock::SendPacket() {
    if (!m_pFile) {
        if (m_bSend) {
            m_pModule->PutModule(
                t_f("Sending [{1}] to [{2}]: File closed prematurely.")(
                    m_sFileName, m_sRemoteNick));
        } else {
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}]: File closed prematurely.")(
                    m_sFileName, m_sRemoteNick));
        }

        Close();
        return;
    }

    if (GetInternalWriteBuffer().size() > 1024 * 1024) {
        // There is still enough data to be written, don't add more
        // stuff to that buffer.
        DEBUG("SendPacket(): Skipping send, buffer still full enough ["
              << GetInternalWriteBuffer().size() << "][" << m_sRemoteNick
              << "][" << m_sFileName << "]");
        return;
    }

    char szBuf[4096];
    ssize_t iLen = m_pFile->Read(szBuf, 4096);

    if (iLen < 0) {
        if (m_bSend) {
            m_pModule->PutModule(
                t_f("Sending [{1}] to [{2}]: Error reading from file.")(
                    m_sFileName, m_sRemoteNick));
        } else {
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}]: Error reading from file.")(
                    m_sFileName, m_sRemoteNick));
        }

        Close();
        return;
    }

    if (iLen > 0) {
        Write(szBuf, iLen);
        m_uBytesSoFar += iLen;
    }
}
Esempio n. 7
0
File: dcc.cpp Progetto: aszrul/znc
void CDCCSock::Connected() {
    DEBUG(GetSockName() << " == Connected(" << GetRemoteIP() << ")");
    if (m_bSend) {
        m_pModule->PutModule(t_f("Sending [{1}] to [{2}]: Transfer started.")(
            m_sFileName, m_sRemoteNick));
    } else {
        m_pModule->PutModule(
            t_f("Receiving [{1}] from [{2}]: Transfer started.")(
                m_sFileName, m_sRemoteNick));
    }

    if (m_bSend) {
        SendPacket();
    }

    SetTimeout(120);
}
Esempio n. 8
0
File: dcc.cpp Progetto: aszrul/znc
void CDCCSock::ReadData(const char* data, size_t len) {
    if (!m_pFile) {
        DEBUG("File not open! closing get.");
        if (m_bSend) {
            m_pModule->PutModule(t_f("Sending [{1}] to [{2}]: File not open!")(
                m_sFileName, m_sRemoteNick));
        } else {
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}]: File not open!")(
                    m_sFileName, m_sRemoteNick));
        }
        Close();
        return;
    }

    // DCC specs says the receiving end sends the number of bytes it
    // received so far as a 4 byte integer in network byte order, so we need
    // uint32_t to do the job portably. This also means that the maximum
    // file that we can transfer is 4 GiB big (see OpenFile()).
    if (m_bSend) {
        m_sSendBuf.append(data, len);

        while (m_sSendBuf.size() >= 4) {
            uint32_t iRemoteSoFar;
            memcpy(&iRemoteSoFar, m_sSendBuf.data(), sizeof(iRemoteSoFar));
            iRemoteSoFar = ntohl(iRemoteSoFar);

            if ((iRemoteSoFar + 65536) >= m_uBytesSoFar) {
                SendPacket();
            }

            m_sSendBuf.erase(0, 4);
        }
    } else {
        m_pFile->Write(data, len);
        m_uBytesSoFar += len;
        uint32_t uSoFar = htonl((uint32_t)m_uBytesSoFar);
        Write((char*)&uSoFar, sizeof(uSoFar));

        if (m_uBytesSoFar >= m_uFileSize) {
            Close();
        }
    }
}
Esempio n. 9
0
void CDCCSock::Disconnected() {
	const CString sStart = ((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - ";

	DEBUG(GetSockName() << " == Disconnected()");

	if (m_uBytesSoFar > m_uFileSize) {
		m_pModule->PutModule(sStart + "TooMuchData!");
	} else if (m_uBytesSoFar == m_uFileSize) {
		if (m_bSend) {
			m_pModule->PutModule(sStart + "Completed! - Sent [" + m_sLocalFile +
					"] at [" + CString((int) (GetAvgWrite() / 1024.0)) + " KiB/s ]");
		} else {
			m_pModule->PutModule(sStart + "Completed! - Saved to [" + m_sLocalFile +
					"] at [" + CString((int) (GetAvgRead() / 1024.0)) + " KiB/s ]");
		}
	} else {
		m_pModule->PutModule(sStart + "Incomplete!");
	}
}
Esempio n. 10
0
void CDCCSock::Connected() {
	DEBUG(GetSockName() << " == Connected(" << GetRemoteIP() << ")");
	m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - Transfer Started.");

	if (m_bSend) {
		SendPacket();
	}

	SetTimeout(120);
}
Esempio n. 11
0
CFile* CDCCSock::OpenFile(bool bWrite) {
	if ((m_pFile) || (m_sLocalFile.empty())) {
		m_pModule->PutModule(((bWrite) ? "DCC <- [" : "DCC -> [") + m_sRemoteNick + "][" + m_sLocalFile + "] - Unable to open file.");
		return NULL;
	}

	m_pFile = new CFile(m_sLocalFile);

	if (bWrite) {
		if (m_pFile->Exists()) {
			delete m_pFile;
			m_pFile = NULL;
			m_pModule->PutModule("DCC <- [" + m_sRemoteNick + "] - File already exists [" + m_sLocalFile + "]");
			return NULL;
		}

		if (!m_pFile->Open(O_WRONLY | O_TRUNC | O_CREAT)) {
			delete m_pFile;
			m_pFile = NULL;
			m_pModule->PutModule("DCC <- [" + m_sRemoteNick + "] - Could not open file [" + m_sLocalFile + "]");
			return NULL;
		}
	} else {
		if (!m_pFile->IsReg()) {
			delete m_pFile;
			m_pFile = NULL;
			m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - Not a file [" + m_sLocalFile + "]");
			return NULL;
		}

		if (!m_pFile->Open()) {
			delete m_pFile;
			m_pFile = NULL;
			m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - Could not open file [" + m_sLocalFile + "]");
			return NULL;
		}

		// The DCC specs only allow file transfers with files smaller
		// than 4GiB (see ReadData()).
		unsigned long long uFileSize = m_pFile->GetSize();
		if (uFileSize > (unsigned long long) 0xffffffff) {
			delete m_pFile;
			m_pFile = NULL;
			m_pModule->PutModule("DCC -> [" + m_sRemoteNick + "] - File too large (>4 GiB) [" + m_sLocalFile + "]");
			return NULL;
		}

		m_uFileSize = (unsigned long) uFileSize;
	}

	m_sFileName = m_pFile->GetShortName();

	return m_pFile;
}
Esempio n. 12
0
File: dcc.cpp Progetto: aszrul/znc
CFile* CDCCSock::OpenFile(bool bWrite) {
    if ((m_pFile) || (m_sLocalFile.empty())) {
        if (m_bSend) {
            m_pModule->PutModule(
                t_f("Sending [{1}] to [{2}]: Unable to open file.")(
                    m_sFileName, m_sRemoteNick));
        } else {
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}]: Unable to open file.")(
                    m_sFileName, m_sRemoteNick));
        }
        return nullptr;
    }

    m_pFile = new CFile(m_sLocalFile);

    if (bWrite) {
        if (m_pFile->Exists()) {
            delete m_pFile;
            m_pFile = nullptr;
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}]: File already exists.")(
                    m_sFileName, m_sRemoteNick));
            return nullptr;
        }

        if (!m_pFile->Open(O_WRONLY | O_TRUNC | O_CREAT)) {
            delete m_pFile;
            m_pFile = nullptr;
            m_pModule->PutModule(
                t_f("Receiving [{1}] from [{2}]: Could not open file.")(
                    m_sFileName, m_sRemoteNick));
            return nullptr;
        }
    } else {
        if (!m_pFile->IsReg()) {
            delete m_pFile;
            m_pFile = nullptr;
            m_pModule->PutModule(
                t_f("Sending [{1}] to [{2}]: Not a file.")(
                    m_sFileName, m_sRemoteNick));
            return nullptr;
        }

        if (!m_pFile->Open()) {
            delete m_pFile;
            m_pFile = nullptr;
            m_pModule->PutModule(
                t_f("Sending [{1}] to [{2}]: Could not open file.")(
                    m_sFileName, m_sRemoteNick));
            return nullptr;
        }

        // The DCC specs only allow file transfers with files smaller
        // than 4GiB (see ReadData()).
        unsigned long long uFileSize = m_pFile->GetSize();
        if (uFileSize > (unsigned long long)0xffffffffULL) {
            delete m_pFile;
            m_pFile = nullptr;
            m_pModule->PutModule(
                t_f("Sending [{1}] to [{2}]: File too large (>4 GiB).")(
                    m_sFileName, m_sRemoteNick));
            return nullptr;
        }

        m_uFileSize = uFileSize;
    }

    m_sFileName = m_pFile->GetShortName();

    return m_pFile;
}
Esempio n. 13
0
void CDCCSock::SockError(int iErrno) {
	DEBUG(GetSockName() << " == SockError(" << iErrno << ")");
	m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - Socket Error [" + CString(iErrno) + "]");
}
Esempio n. 14
0
void CDCCSock::Timeout() {
	DEBUG(GetSockName() << " == Timeout()");
	m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - Timed Out.");
}
Esempio n. 15
0
void CDCCSock::ConnectionRefused() {
	DEBUG(GetSockName() << " == ConnectionRefused()");
	m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - Connection Refused.");
}
Esempio n. 16
0
File: dcc.cpp Progetto: johnfb/znc
void CDCCSock::SockError(int iErrno, const CString& sDescription) {
	DEBUG(GetSockName() << " == SockError(" << iErrno << ", " << sDescription << ")");
	m_pModule->PutModule(((m_bSend) ? "DCC -> [" : "DCC <- [") + m_sRemoteNick + "][" + m_sFileName + "] - Socket Error [" + sDescription + "]");
}