summaryrefslogtreecommitdiffstats
path: root/src/scanmonitor.cpp
blob: 91c66f78f1eb9c63e1a0d41c9a3014f9e5ff9c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***************************************************************************
 *                                                                         *
 *   Copyright (C) 2005 by Kevin Gilbert                                   *
 *   [email protected]                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 ***************************************************************************/

#include <errno.h> 
#include <stdio.h> 
#include <string.h> 
#include <unistd.h>
#include <sys/select.h> 
#include <sys/time.h> 

#include <ntqapplication.h>

#include <tdelocale.h>

#include "nmapoutputbuffer.h"
#include "scanmonitor.h"
#include "scanmonitorevent.h"
#include "scanwidget.h"

//	constructor
//	===========

ScanMonitor::ScanMonitor( ScanWidget* parent )
: TQThread( ),
  m_buffer( NULL ),
  m_nmapBufferSize( 16384 ),
  m_parent( parent )
{	m_buffers.setAutoDelete( true );
}

//	getBuffer
//	=========

void ScanMonitor::getBuffer( )
{	for( m_buffer = m_buffers.first( ); m_buffer != NULL; m_buffer = m_buffers.next( ))
		if( m_buffer->isFree( ))
		{	m_buffer->setBusy( );
			m_buffers.take( );
			m_buffers.append( m_buffer );
			return;
		}

	m_buffer = new NmapOutputBuffer( m_nmapBufferSize );
	m_buffers.append( m_buffer );
}

//	readNmapOutput
//	==============

void ScanMonitor::readNmapOutput( const int pipeFD, const ScanMonitorEventType type )
{	while( true )
	{	if( m_buffer == NULL )
			getBuffer( );
		else
			m_buffer->setBusy( );

		int bytesRead = ::read( pipeFD, m_buffer->buffer( ), m_nmapBufferSize );

		if( bytesRead > 0 )
		{	m_buffer->length( bytesRead );
			TQApplication::postEvent( m_parent, new ScanMonitorEvent( type, m_buffer ));
			m_buffer = NULL;
			continue;
		}

		if( !bytesRead || errno == EAGAIN || errno == EBADF )
		{	m_buffer->setFree( );
			break;
		}

		TQString text = TQString( i18n( "error reading nmap output from pipe: %2\n" )).arg( strerror( errno ));
		strcpy( m_buffer->buffer( ), text.ascii( ));
		m_buffer->length( text.length( ));
		TQApplication::postEvent( m_parent, new ScanMonitorEvent( StderrType, m_buffer ));
		m_buffer = NULL;
		break;
	}
}

//	run
//	===

void ScanMonitor::run( )
{	fd_set efds;
	fd_set rfds;
	struct timeval timeout;

	while( true )
	{	FD_ZERO( &efds );
		FD_ZERO( &rfds );

		FD_SET( m_pipeStderrFD, &efds);
		FD_SET( m_pipeStdoutFD, &efds);

		FD_SET( m_pipeStderrFD, &rfds);
		FD_SET( m_pipeStdoutFD, &rfds);

		timeout.tv_sec  = 2;
		timeout.tv_usec = 0;

		if( !select( _max_( m_pipeStderrFD, m_pipeStdoutFD ) + 1, &rfds, NULL, &efds, &timeout ))
			continue;

		if( FD_ISSET( m_pipeStderrFD, &efds ) || FD_ISSET( m_pipeStdoutFD, &efds ))
			break;

		if( FD_ISSET( m_pipeStderrFD, &rfds ))
			readNmapOutput( m_pipeStderrFD, StderrType );

		if( FD_ISSET( m_pipeStdoutFD, &rfds ))
			readNmapOutput( m_pipeStdoutFD, StdoutType );
	}
}

//	setPipeFDs
//	==========

void ScanMonitor::setPipeFDs( const int pipeStderrFD, const int pipeStdoutFD )
{	m_pipeStderrFD = pipeStderrFD;
	m_pipeStdoutFD = pipeStdoutFD;
}