Beispiel #1
0
int main()
{
        int action = 0;
        const char* prompt = "\nPlease enter action:\n"
                             "0: Install driver\n"
                             "1: Read data\n"
                             "2: Write data\n"
                             "3: IoControl driver\n"
                             "4: Uninstall driver";
        puts(prompt);
        
        const TCHAR* drv_name = _T("ObjNameGetter");

        while (EOF != scanf("%d", &action)) {
                switch (action) {
                case Install:
                        InstallDriver(drv_name);
                        break;
                case Read:
                        ReadDriver();
                        break;
                case Write:
                        WriteDriver();
                        break;
                case Uninstall:
                        UninstallDriver(drv_name);
                        break;
                case IoControl:
                        IoControlDriver();
                        break;
                default:
                        ;
                }

                puts(prompt);
        }
        return 0;
}
//-----------------------------------------------------------------------------
bool HoneDumpcap::CapturePackets(void)
{
	while (m_captureState != CaptureStateDone) {

#ifdef WIN32
		if (m_signalPipeHandle != InvalidFileHandle) {
			DWORD bytesAvailable;
			const BOOL rc = ::PeekNamedPipe(m_signalPipeHandle, NULL, 0, NULL, &bytesAvailable, NULL);
			if (!rc || (bytesAvailable > 0)) {
				Log(QString("Parent process %1 is closing us").arg(m_parentPid));
				m_markCleanup = true;
			}
		}
#else // #ifdef WIN32
		fd_set readfds;
#endif // #ifdef WIN32

		if (m_markCleanup && (m_captureState != CaptureStateCleanUp)) {
			if (!MarkRestart()) {
				return false;
			}
			m_captureState = CaptureStateCleanUp;
			m_markCleanup  = false;
		}
		if (m_markRotate && (m_captureState == CaptureStateNormal)) {
			if (!MarkRestart()) {
				return false;
			}
			m_captureState = CaptureStateRotate;
			m_markRotate       = false;
		}

		quint32 bytesRead;
		if (!ReadDriver(bytesRead)) {
			return false;
		}

		if (bytesRead) {
			const qint32 packetCount  = CountPackets(bytesRead);
			const qint64 bytesWritten = m_captureFile.write(m_captureData.data(), bytesRead);
			if (bytesWritten == -1) {
				return LogError(QString("Cannot write %L1 bytes to %2: %3").arg(bytesRead)
						.arg(m_captureFile.fileName(), m_captureFile.errorString()));
			}
			if (bytesRead != bytesWritten) {
				return LogError(QString("Only wrote %L1 of %L2 bytes to %3").arg(bytesWritten).arg(bytesRead)
						.arg(m_captureFile.fileName()));
			}
			if (!m_captureFile.flush()) {
				return LogError(QString("Cannot flush %1: %2").arg(m_captureFile.fileName(),m_captureFile.errorString()));
			}

			m_captureFileSize    += bytesWritten;
			m_packetCount += packetCount;
			if (m_parentPid.isEmpty()) {
				Log(QString("\rPackets: %1").arg(m_packetCount), false);
			} else {
				WriteCommand('P', QString::number(packetCount));
			}

			// Handle stop and rotate conditions
			if (
					(m_autoStopFileCount    && (m_captureFileCount   >= m_autoStopFileCount  )) ||
					(m_autoStopFileSize     && (m_captureFileSize    >= m_autoStopFileSize   )) ||
					(m_autoStopPacketCount  && (m_packetCount >= m_autoStopPacketCount)) ||
					(m_autoStopMilliseconds && ((QDateTime::currentMSecsSinceEpoch() - m_captureStart) > m_autoStopMilliseconds))) {
				m_markCleanup = true;
			} else if (
					(m_autoRotateFileSize     && (m_captureFileSize >= m_autoRotateFileSize)) ||
					(m_autoRotateMilliseconds && ((QDateTime::currentMSecsSinceEpoch() - m_captureStart) > m_autoRotateMilliseconds))) {
				m_markRotate = true;
			}
		} else { // No data to read
			switch (m_captureState) {
			case CaptureStateCleanUp:
#ifndef WIN32
				if (::ioctl(m_driverHandle, HEIO_GET_AT_HEAD) <= 0) {
					break;
				}
#endif // #ifdef WIN32
				m_captureState = CaptureStateDone;
				break;
			case CaptureStateDone:
				break;
			case CaptureStateNormal:
#ifdef WIN32
				::Sleep(500);
#else // #ifdef WIN32
				FD_ZERO(&readfds);
				FD_SET(m_driverHandle, &readfds);
				if (-1 == ::select(m_driverHandle+1, &readfds, NULL, NULL, NULL)) {
					if (errno == EINTR) {
						m_markCleanup = true;
					} else {
						return LogError("Cannot check for data from the driver", true);
					}
				}
#endif // #ifdef WIN32
				break;
			case CaptureStateRotate:
#ifndef WIN32
				if (::ioctl(m_driverHandle, HEIO_GET_AT_HEAD) <= 0) {
					break;
				}
#endif // #ifdef WIN32
				if (!OpenCaptureFile()) {
					return false;
				}
				m_captureState = CaptureStateNormal;
				break;
			}
		}
	}

	return true;
}