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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* This file is part of the KDE libraries
* Copyright (c) 2001-2002 Michael Goffioul <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
**/
#define USE_QSOCKET
#include "networkscanner.h"
#include <tqprogressbar.h>
#include <kpushbutton.h>
#include <tqlayout.h>
#include <tqtimer.h>
#include <tqlabel.h>
#include <tqcombobox.h>
#include <tqlineedit.h>
#include <tqregexp.h>
#include <tqsocket.h>
#include <tdelocale.h>
#include <kextendedsocket.h>
#include <tdemessagebox.h>
#include <knumvalidator.h>
#include <kdebug.h>
#include <unistd.h>
class NetworkScanner::NetworkScannerPrivate
{
public:
int port;
TQString prefixaddress;
int currentaddress;
int timeout;
bool scanning;
TQPtrList<NetworkScanner::SocketInfo> printers;
TQProgressBar *bar;
KPushButton *scan, *settings;
TQLabel *subnetlab;
TQTimer *timer;
#ifdef USE_QSOCKET
TQSocket *socket;
#else
KExtendedSocket *socket;
#endif
NetworkScannerPrivate( int portvalue ) : port( portvalue )
{
prefixaddress = localPrefix();
currentaddress = 1;
timeout = 50;
scanning = false;
printers.setAutoDelete( true );
}
TQString localPrefix();
TQString scanString();
};
TQString NetworkScanner::NetworkScannerPrivate::localPrefix()
{
char buf[256];
buf[0] = '\0';
if (!gethostname(buf, sizeof(buf)))
buf[sizeof(buf)-1] = '\0';
TQPtrList<KAddressInfo> infos = KExtendedSocket::lookup(buf, TQString::null);
infos.setAutoDelete(true);
if (infos.count() > 0)
{
TQString IPstr = infos.first()->address()->nodeName();
int p = IPstr.findRev('.');
IPstr.truncate(p);
return IPstr;
}
return TQString::null;
}
TQString NetworkScanner::NetworkScannerPrivate::scanString()
{
TQString s = prefixaddress + ".*";
if ( port != -1 )
s.append( ":" ).append( TQString::number( port ) );
return s;
}
NetworkScanner::NetworkScanner( int port, TQWidget *parent, const char *name )
: TQWidget( parent, name )
{
d = new NetworkScannerPrivate( port );
d->bar = new TQProgressBar( 256, this );
d->settings = new KPushButton( KGuiItem( i18n( "&Settings" ), "configure" ), this );
d->scan = new KPushButton( KGuiItem( i18n( "Sc&an" ), "viewmag" ), this );
d->timer = new TQTimer( this );
#ifdef USE_QSOCKET
d->socket = new TQSocket( TQT_TQOBJECT(this) );
#else
d->socket = new KExtendedSocket();
#endif
TQLabel *label = new TQLabel( i18n( "Network scan:" ), this );
d->subnetlab = new TQLabel( i18n( "Subnet: %1" ).arg( d->scanString() ), this );
TQGridLayout *l0 = new TQGridLayout( this, 4, 2, 0, 10 );
l0->addMultiCellWidget( label, 0, 0, 0, 1 );
l0->addMultiCellWidget( d->bar, 1, 1, 0, 1 );
l0->addMultiCellWidget( d->subnetlab, 2, 2, 0, 1 );
l0->addWidget( d->settings, 3, 0 );
l0->addWidget( d->scan, 3, 1 );
connect( d->timer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotTimeout() ) );
connect( d->settings, TQT_SIGNAL( clicked() ), TQT_SLOT( slotSettingsClicked() ) );
connect( d->scan, TQT_SIGNAL( clicked() ), TQT_SLOT( slotScanClicked() ) );
#ifdef USE_QSOCKET
connect( d->socket, TQT_SIGNAL( connected() ), TQT_SLOT( slotConnectionSuccess() ) );
connect( d->socket, TQT_SIGNAL( error( int ) ), TQT_SLOT( slotConnectionFailed( int ) ) );
#else
connect( d->socket, TQT_SIGNAL( connectionSuccess() ), TQT_SLOT( slotConnectionSuccess() ) );
connect( d->socket, TQT_SIGNAL( connectionFailed( int ) ), TQT_SLOT( slotConnectionFailed( int ) ) );
#endif
}
NetworkScanner::~NetworkScanner()
{
#ifndef USE_QSOCKET
delete d->socket;
#endif
delete d;
}
void NetworkScanner::start()
{
if ( d->scanning )
return;
d->printers.clear();
emit scanStarted();
d->settings->setEnabled( false );
d->scan->setGuiItem( KGuiItem( i18n( "&Abort" ), "process-stop" ) );
d->currentaddress = -1;
d->scanning = true;
next();
}
void NetworkScanner::slotScanClicked()
{
if ( !d->scanning )
{
if ( d->localPrefix() == d->prefixaddress ||
KMessageBox::warningContinueCancel( this->parentWidget(),
i18n( "You are about to scan a subnet (%1.*) that does not "
"correspond to the current subnet of this computer (%2.*). Do you want "
"to scan the specified subnet anyway?" ).arg( d->prefixaddress ).arg( d->localPrefix() ),
TQString::null, KGuiItem( i18n( "&Scan" ), "viewmag" ), "askForScan" ) == KMessageBox::Continue )
start();
}
else
{
#ifdef USE_QSOCKET
d->socket->close();
#else
d->socket->cancelAsyncConnect();
#endif
finish();
}
}
void NetworkScanner::finish()
{
if ( !d->scanning )
return;
d->settings->setEnabled( true );
d->scan->setGuiItem( KGuiItem( i18n( "Sc&an" ), "viewmag" ) );
d->bar->reset();
d->scanning = false;
emit scanFinished();
}
void NetworkScanner::slotSettingsClicked()
{
NetworkScannerConfig dlg( this );
dlg.exec();
}
void NetworkScanner::slotNext()
{
if ( !d->scanning )
return;
d->timer->stop();
#ifdef USE_QSOCKET
d->socket->connectToHost( d->prefixaddress + "." + TQString::number( d->currentaddress ), d->port );
kdDebug() << "Address: " << d->socket->peerName() << ", Port: " << d->socket->peerPort() << endl;
#else
d->socket->setAddress( d->prefixaddress + "." + TQString::number( d->currentaddress ), d->port );
d->socket->startAsyncLookup();
kdDebug() << "Address: " << d->socket->host() << ", Port: " << d->socket->port() << endl;
#endif
d->timer->start( d->timeout, true );
}
void NetworkScanner::next()
{
//kdDebug() << "Next" << endl;
d->currentaddress++;
if ( d->currentaddress >= 256 )
finish();
else
{
d->bar->setProgress( d->currentaddress );
TQTimer::singleShot( 0, this, TQT_SLOT( slotNext() ) );
}
}
void NetworkScanner::slotTimeout()
{
kdDebug() << "Timeout" << endl;
if ( !d->scanning )
return;
#ifdef USE_QSOCKET
d->socket->close();
#else
d->socket->cancelAsyncConnect();
#endif
next();
}
void NetworkScanner::slotConnectionSuccess()
{
kdDebug() << "Success" << endl;
#ifdef USE_QSOCKET
TDESocketAddress *addr = KExtendedSocket::peerAddress( d->socket->socket() );
#else
TDESocketAddress *addr = const_cast<TDESocketAddress*>( d->socket->peerAddress() );
#endif
kdDebug() << "Connection success: " << ( addr ? addr->pretty() : TQString( "ERROR" ) ) << endl;
kdDebug() << "Socket: " << d->socket->socket() << endl;
if ( addr )
{
SocketInfo *info = new SocketInfo;
#ifdef USE_QSOCKET
info->IP = d->socket->peerName();
#else
info->IP = d->socket->host();
#endif
info->Port = d->port;
TQString portname;
KExtendedSocket::resolve( addr, info->Name, portname );
d->printers.append( info );
d->socket->close();
delete addr;
}
else
kdDebug() << "Unconnected socket, skipping" << endl;
next();
}
void NetworkScanner::slotConnectionFailed( int )
{
kdDebug() << "Failure" << endl;
next();
}
const TQPtrList<NetworkScanner::SocketInfo>* NetworkScanner::printerList()
{
return &( d->printers );
}
int NetworkScanner::timeout() const
{
return d->timeout;
}
void NetworkScanner::setTimeout( int to )
{
d->timeout = to;
}
TQString NetworkScanner::subnet() const
{
return d->prefixaddress;
}
void NetworkScanner::setSubnet( const TQString& sn )
{
d->prefixaddress = sn;
d->subnetlab->setText( i18n( "Subnet: %1" ).arg( d->scanString() ) );
}
int NetworkScanner::port() const
{
return d->port;
}
void NetworkScanner::setPort( int p )
{
d->port = p;
d->subnetlab->setText( i18n( "Subnet: %1" ).arg( d->scanString() ) );
}
bool NetworkScanner::checkPrinter( const TQString& host, int port )
{
// try first to find it in the SocketInfo list
TQPtrListIterator<NetworkScanner::SocketInfo> it( d->printers );
for ( ; it.current(); ++it )
{
if ( port == it.current()->Port && ( host == it.current()->IP ||
host == it.current()->Name ) )
return true;
}
// not found in SocketInfo list, try to establish connection
KExtendedSocket extsock( host, port );
extsock.setBlockingMode( false );
extsock.setTimeout( 0, d->timeout * 1000 );
return ( extsock.connect() == 0 );
}
NetworkScannerConfig::NetworkScannerConfig(NetworkScanner *scanner, const char *name)
: KDialogBase(scanner, name, true, TQString::null, Ok|Cancel, Ok, true)
{
scanner_ = scanner;
TQWidget *dummy = new TQWidget(this);
setMainWidget(dummy);
KIntValidator *val = new KIntValidator( this );
TQLabel *masklabel = new TQLabel(i18n("&Subnetwork:"),dummy);
TQLabel *portlabel = new TQLabel(i18n("&Port:"),dummy);
TQLabel *toutlabel = new TQLabel(i18n("&Timeout (ms):"),dummy);
TQLineEdit *mm = new TQLineEdit(dummy);
mm->setText(TQString::fromLatin1(".[0-255]"));
mm->setReadOnly(true);
mm->setFixedWidth(fontMetrics().width(mm->text())+10);
mask_ = new TQLineEdit(dummy);
mask_->setAlignment(TQt::AlignRight);
port_ = new TQComboBox(true,dummy);
if ( port_->lineEdit() )
port_->lineEdit()->setValidator( val );
tout_ = new TQLineEdit(dummy);
tout_->setValidator( val );
masklabel->setBuddy(mask_);
portlabel->setBuddy(port_);
toutlabel->setBuddy(tout_);
mask_->setText(scanner_->subnet());
port_->insertItem("631");
port_->insertItem("9100");
port_->insertItem("9101");
port_->insertItem("9102");
port_->setEditText(TQString::number(scanner_->port()));
tout_->setText(TQString::number(scanner_->timeout()));
TQGridLayout *main_ = new TQGridLayout(dummy, 3, 2, 0, 10);
TQHBoxLayout *lay1 = new TQHBoxLayout(0, 0, 5);
main_->addWidget(masklabel, 0, 0);
main_->addWidget(portlabel, 1, 0);
main_->addWidget(toutlabel, 2, 0);
main_->addLayout(lay1, 0, 1);
main_->addWidget(port_, 1, 1);
main_->addWidget(tout_, 2, 1);
lay1->addWidget(mask_,1);
lay1->addWidget(mm,0);
resize(250,130);
setCaption(i18n("Scan Configuration"));
}
NetworkScannerConfig::~NetworkScannerConfig()
{
}
void NetworkScannerConfig::slotOk()
{
TQString msg;
TQRegExp re("(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})");
if (!re.exactMatch(mask_->text()))
msg = i18n("Wrong subnetwork specification.");
else
{
for (int i=1; i<=3; i++)
if (re.cap(i).toInt() >= 255)
{
msg = i18n("Wrong subnetwork specification.");
break;
}
}
bool ok(false);
int v = tout_->text().toInt(&ok);
if (!ok || v <= 0)
msg = i18n("Wrong timeout specification.");
v = port_->currentText().toInt(&ok);
if (!ok || v <= 0)
msg = i18n("Wrong port specification.");
if (!msg.isEmpty())
{
KMessageBox::error(this,msg);
return;
}
scanner_->setTimeout( tout_->text().toInt() );
scanner_->setSubnet( mask_->text() );
scanner_->setPort( port_->currentText().toInt() );
KDialogBase::slotOk();
}
#include "networkscanner.moc"
|