1 |
/*************************************************************************** |
2 |
* Copyright (C) 2003 by Torben Nielsen * |
3 |
* torben.nielsen@rocketmail.com * |
4 |
* * |
5 |
* This program is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
7 |
* the Free Software Foundation; either version 2 of the License, or * |
8 |
* (at your option) any later version. * |
9 |
***************************************************************************/ |
10 |
#include "serverspy.h" |
11 |
|
12 |
#include "dnshelper.h" |
13 |
|
14 |
|
15 |
#include <qlineedit.h> |
16 |
#include <qlabel.h> |
17 |
#include <qtextstream.h> |
18 |
#include <qstring.h> |
19 |
#include <qregexp.h> |
20 |
|
21 |
Serverspy::Serverspy(QWidget *parent, const char *name, WFlags f) |
22 |
: ServerspyBase(parent, name, f) |
23 |
{ |
24 |
connect(&m_socket, SIGNAL(hostFound()), SLOT(socketHostFound())); |
25 |
connect(&m_socket, SIGNAL(connected()), SLOT(socketConnected())); |
26 |
connect(&m_socket, SIGNAL(error(int)), SLOT(socketError(int))); |
27 |
connect(&m_socket, SIGNAL(readyRead()), SLOT(socketReadyRead())); |
28 |
|
29 |
m_timer = new QTimer(this); |
30 |
connect( m_timer, SIGNAL( timeout() ), SLOT( socketTimeout() ) ); |
31 |
} |
32 |
|
33 |
|
34 |
Serverspy::~Serverspy() |
35 |
{ |
36 |
if (m_socket.state() != QSocket::Idle || m_socket.state() != QSocket::Closing) |
37 |
m_socket.close(); |
38 |
} |
39 |
|
40 |
void Serverspy::findServer() |
41 |
{ |
42 |
QString tmp = m_address->text(); |
43 |
m_address->setText( tmp.stripWhiteSpace() ); |
44 |
|
45 |
if ( m_address->text().isEmpty() ) |
46 |
return; |
47 |
|
48 |
|
49 |
QString ip = DnsHelper::getIp( m_address->text() ); |
50 |
|
51 |
QString hostname = DnsHelper::getHostname( ip ); |
52 |
|
53 |
m_ip->setText(QString("IP-address: ") + ip ); |
54 |
m_hostname->setText(QString("Orig. hostname: ") + hostname ); |
55 |
|
56 |
|
57 |
m_timer->start( 20000, true ); |
58 |
|
59 |
if ( m_socket.state() != QSocket::Idle ) |
60 |
m_socket.close(); |
61 |
|
62 |
m_output->setText("Result: looking up server"); |
63 |
|
64 |
m_socket.connectToHost( m_address->text(), 80 ); |
65 |
} |
66 |
|
67 |
void Serverspy::socketHostFound() |
68 |
{ |
69 |
m_output->setText("Result: connecting to server"); |
70 |
} |
71 |
|
72 |
void Serverspy::socketConnected() |
73 |
{ |
74 |
QString host_header = QString("Host: ").append(m_address->text()).append("\n"); |
75 |
QTextStream out(&m_socket); |
76 |
out << QString("HEAD / HTTP/1.1\n").append(host_header).append("\r\n");; |
77 |
m_output->setText("Result: requesting data"); |
78 |
} |
79 |
|
80 |
void Serverspy::socketError(int err) |
81 |
{ |
82 |
switch(err) { |
83 |
case QSocket::ErrConnectionRefused : |
84 |
m_output->setText("Result: error - connection refused!"); |
85 |
break; |
86 |
case QSocket::ErrHostNotFound : |
87 |
m_output->setText("Result: error - host not found!"); |
88 |
break; |
89 |
case QSocket::ErrSocketRead : |
90 |
m_output->setText("Result: error - could not read from socket"); |
91 |
break; |
92 |
default : |
93 |
m_output->setText("Result: error"); |
94 |
} |
95 |
m_socket.close(); |
96 |
m_timer->stop(); |
97 |
} |
98 |
|
99 |
void Serverspy::socketReadyRead() |
100 |
{ |
101 |
while ( m_socket.canReadLine() ) { |
102 |
QString line = m_socket.readLine(); |
103 |
QRegExp regex("^Server: ", true); |
104 |
|
105 |
if ( regex.search(line) != -1 ) { |
106 |
line = line.remove(regex); |
107 |
line = line.stripWhiteSpace(); |
108 |
m_output->setText( QString("Software: ").append(line) ); |
109 |
m_timer->stop(); |
110 |
m_socket.close(); |
111 |
break; |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
void Serverspy::socketTimeout() |
117 |
{ |
118 |
m_socket.close(); |
119 |
m_output->setText("Result: connection timed out"); |
120 |
} |
121 |
|
122 |
|
123 |
#include "serverspy.moc" |