00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <opie2/odebug.h>
00029 using namespace Opie::Core;
00030
00031
00032 #include <qlayout.h>
00033 #include <qregexp.h>
00034 #include <qapplication.h>
00035 #include <qmessagebox.h>
00036 #include <qpushbutton.h>
00037
00038
00039 #include <unistd.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #include <fcntl.h>
00043 #include <netdb.h>
00044 #include <sys/types.h>
00045 #include <sys/socket.h>
00046 #include <arpa/inet.h>
00047 #include <netinet/in.h>
00048 #include <sys/ioctl.h>
00049 #include <assert.h>
00050
00051 #ifdef _XPG4_2
00052 #define __xnet_connect connect
00053 #endif
00054
00055 #include <errno.h>
00056
00057 #ifdef HAVE_SYS_PARAM_H
00058 #include <sys/param.h>
00059 #endif
00060
00061 #ifdef __linux__
00062 #include "runtests.h"
00063 #endif
00064
00065 #include "auth.h"
00066 #include "connect.h"
00067
00068 #include "interfaceppp.h"
00069 #include "modem.h"
00070 #include "kpppconfig.h"
00071 #include "pppdata.h"
00072 #include "kpppwidget.h"
00073
00074
00075 #define execute_command system
00076
00077 QString old_hostname;
00078 bool modified_hostname;
00079
00080
00081 ConnectWidget::ConnectWidget(InterfacePPP *ifp, QWidget *parent, const char *name)
00082 : QWidget(parent, name),
00083 myreadbuffer(""),
00084 main_timer_ID(0),
00085 vmain(0),
00086 substate(-1),
00087 scriptindex(0),
00088 loopnest(0),
00089 loopend(false),
00090 semaphore(false),
00091 expecting(false),
00092 readbuffer(""),
00093 scanvar(""),
00094 scanning(false),
00095 pausing(false),
00096 dialnumber(0),
00097 _ifaceppp(ifp)
00098 {
00099 modified_hostname = false;
00100
00101 QVBoxLayout *tl = new QVBoxLayout(this, 8, 10);
00102 QString tit = QObject::tr("Connecting to: ");
00103 setCaption(tit);
00104
00105 QHBoxLayout *l0 = new QHBoxLayout(10);
00106 tl->addLayout(l0);
00107 l0->addSpacing(10);
00108 messg = new QLabel(this, "messg");
00109 messg->setFrameStyle(QFrame::Panel|QFrame::Sunken);
00110 messg->setAlignment(AlignCenter);
00111 messg->setText(QObject::tr("Unable to create modem lock file."));
00112 messg->setMinimumHeight(messg->sizeHint().height() + 5);
00113
00114
00115
00116 if (_ifaceppp->getStatus())
00117 messg->setText(QObject::tr("Online"));
00118 else
00119 messg->setText(QObject::tr("Offline"));
00120 l0->addSpacing(10);
00121 l0->addWidget(messg);
00122 l0->addSpacing(10);
00123
00124 QHBoxLayout *l1 = new QHBoxLayout(10);
00125 tl->addLayout(l1);
00126 l1->addStretch(1);
00127
00128 debug = new QPushButton(QObject::tr("Log"), this);
00129 debug->setToggleButton(true);
00130 debug->setEnabled( false );
00131 connect(debug, SIGNAL(clicked()), SIGNAL(toggleDebugWindow()));
00132
00133 cancel = new QPushButton(QObject::tr("Cancel"), this);
00134 cancel->setFocus();
00135 connect(cancel, SIGNAL(clicked()), SLOT(cancelbutton()));
00136
00137
00138
00139
00140
00141
00142 l1->addWidget(debug);
00143 l1->addWidget(cancel);
00144
00145
00146
00147 pausetimer = new QTimer(this);
00148 connect(pausetimer, SIGNAL(timeout()), SLOT(pause()));
00149
00150 qApp->processEvents();
00151
00152 timeout_timer = new QTimer(this);
00153 connect(timeout_timer, SIGNAL(timeout()), SLOT(script_timed_out()));
00154
00155 inittimer = new QTimer(this);
00156 connect(inittimer, SIGNAL(timeout()), SLOT(init()));
00157
00158 if_timeout_timer = new QTimer(this);
00159 connect(if_timeout_timer, SIGNAL(timeout()), SLOT(if_waiting_timed_out()));
00160
00161 connect(this,SIGNAL(if_waiting_signal()),this,SLOT(if_waiting_slot()));
00162
00163 prompt = new PWEntry( this, "pw" );
00164 if_timer = new QTimer(this);
00165 connect(if_timer,SIGNAL(timeout()), SLOT(if_waiting_slot()));
00166 }
00167
00168
00169 ConnectWidget::~ConnectWidget() {
00170 }
00171
00172
00173 void ConnectWidget::preinit() {
00174
00175
00176 messg->setText(QObject::tr("Looking for modem..."));
00177 inittimer->start(100);
00178 }
00179
00180
00181 void ConnectWidget::init() {
00182 _ifaceppp->data()->setpppdError(0);
00183 inittimer->stop();
00184 vmain = 0;
00185 substate = -1;
00186 expecting = false;
00187 pausing = false;
00188 scriptindex = 0;
00189 myreadbuffer = "";
00190 scanning = false;
00191 scanvar = "";
00192 firstrunID = true;
00193 firstrunPW = true;
00194
00195 dialnumber = 0;
00196
00197
00198
00199
00200
00201 comlist = &_ifaceppp->data()->scriptType();
00202 arglist = &_ifaceppp->data()->script();
00203
00204 QString tit = QObject::tr("Connecting to: %1").arg(_ifaceppp->data()->accname());
00205 setCaption(tit);
00206
00207 qApp->processEvents();
00208
00209
00210 if (!_ifaceppp->data()->command_before_connect().isEmpty()) {
00211 messg->setText(QObject::tr("Running pre-startup command..."));
00212 emit debugMessage(QObject::tr("Running pre-startup command..."));
00213
00214 qApp->processEvents();
00215 QApplication::flushX();
00216 (void)execute_command(_ifaceppp->data()->command_before_connect());
00217
00218
00219
00220
00221
00222
00223
00224
00225 }
00226
00227 int lock = _ifaceppp->modem()->lockdevice();
00228
00229 if (lock == 1) {
00230 messg->setText(QObject::tr("Modem device is locked."));
00231 vmain = 20;
00232 return;
00233 }
00234
00235 if (lock == -1) {
00236 messg->setText(QObject::tr("Unable to create modem lock file."));
00237 vmain = 20;
00238 return;
00239 }
00240
00241 if(_ifaceppp->modem()->opentty()) {
00242 messg->setText(_ifaceppp->modem()->modemMessage());
00243 qApp->processEvents();
00244 if(_ifaceppp->modem()->hangup()) {
00245
00246 qApp->processEvents();
00247
00248 semaphore = false;
00249
00250 _ifaceppp->modem()->stop();
00251 _ifaceppp->modem()->notify(this, SLOT(readChar(unsigned char)));
00252
00253
00254 timeout_timer->start(_ifaceppp->data()->modemTimeout()*1000);
00255
00256
00257 main_timer_ID = startTimer(10);
00258
00259 return;
00260 }
00261 }
00262
00263
00264 messg->setText(_ifaceppp->modem()->modemMessage());
00265 vmain = 20;
00266 _ifaceppp->modem()->unlockdevice();
00267 }
00268
00269
00270 void ConnectWidget::timerEvent(QTimerEvent *) {
00271 if (semaphore || pausing)
00272 return;
00273
00274 if(vmain == 0) {
00275 #ifdef DEBUG_WO_DIALING
00276 vmain = 10;
00277 return;
00278 #endif
00279
00280 assert(PPPData::NumInitStrings > 0);
00281
00282 if(substate == -1) {
00283 messg->setText(QObject::tr("Initializing modem..."));
00284 emit debugMessage(QObject::tr("Initializing modem..."));
00285 substate = 0;
00286 }
00287
00288 QString initStr = _ifaceppp->data()->modemInitStr(substate);
00289 if (!initStr.isEmpty()) {
00290
00291
00292 if(_ifaceppp->data()->modemPreInitDelay() > 0) {
00293 usleep(_ifaceppp->data()->modemPreInitDelay() * 5000);
00294 writeline("");
00295 usleep(_ifaceppp->data()->modemPreInitDelay() * 5000);
00296 }
00297 setExpect(_ifaceppp->data()->modemInitResp());
00298 writeline(initStr);
00299 usleep(_ifaceppp->data()->modemInitDelay() * 10000);
00300 }
00301
00302 substate++;
00303
00304
00305
00306
00307
00308
00309
00310 if (MODEM_TONEDURATION != _ifaceppp->data()->modemToneDuration())
00311 vmain = 5;
00312 else
00313 vmain = 3;
00314
00315 return;
00316 }
00317
00318 if (vmain == 5) {
00319 if(!expecting) {
00320 QString sToneDuration = "ATS11=" + QString::number(_ifaceppp->data()->modemToneDuration());
00321 QString msg = QObject::tr("Setting ") + sToneDuration;
00322 messg->setText(msg);
00323 emit debugMessage(msg);
00324 setExpect(_ifaceppp->data()->modemInitResp());
00325 writeline(sToneDuration);
00326 }
00327 vmain = 3;
00328 return;
00329 }
00330
00331 if(vmain == 3) {
00332 if(!expecting) {
00333
00334 if(substate < PPPData::NumInitStrings) {
00335 vmain = 0;
00336 return;
00337 }
00338 substate = -1;
00339
00340 if(_ifaceppp->data()->volumeInitString().isEmpty()) {
00341 vmain = 4;
00342 return;
00343 }
00344 messg->setText(QObject::tr("Setting speaker volume..."));
00345 emit debugMessage(QObject::tr("Setting speaker volume..."));
00346
00347 setExpect(_ifaceppp->data()->modemInitResp());
00348 QString vol("AT");
00349 vol += _ifaceppp->data()->volumeInitString();
00350 writeline(vol);
00351 usleep(_ifaceppp->data()->modemInitDelay() * 10000);
00352 vmain = 4;
00353 return;
00354 }
00355 }
00356
00357 if(vmain == 4) {
00358 if(!expecting) {
00359 if(!_ifaceppp->data()->waitForDialTone()) {
00360 QString msg = QObject::tr("Turning off dial tone waiting...");
00361 messg->setText(msg);
00362 emit debugMessage(msg);
00363 setExpect(_ifaceppp->data()->modemInitResp());
00364 writeline(_ifaceppp->data()->modemNoDialToneDetectionStr());
00365 }
00366 vmain = 1;
00367 return;
00368 }
00369 }
00370
00371
00372 if(vmain == 1) {
00373 if(!expecting) {
00374
00375 timeout_timer->stop();
00376 timeout_timer->start(_ifaceppp->data()->modemTimeout()*1000);
00377
00378 QStringList &plist = _ifaceppp->data()->phonenumbers();
00379 QString bmarg= _ifaceppp->data()->dialPrefix();
00380 bmarg += *plist.at(dialnumber);
00381 QString bm = QObject::tr("Dialing %1").arg(bmarg);
00382 messg->setText(bm);
00383 emit debugMessage(bm);
00384
00385 QString pn = _ifaceppp->data()->modemDialStr();
00386 pn += _ifaceppp->data()->dialPrefix();
00387 pn += *plist.at(dialnumber);
00388 if(++dialnumber >= plist.count())
00389 dialnumber = 0;
00390 writeline(pn);
00391
00392 setExpect(_ifaceppp->data()->modemConnectResp());
00393 vmain = 100;
00394 return;
00395 }
00396 }
00397
00398
00399
00400 if(vmain == 100) {
00401 if(!expecting) {
00402 myreadbuffer = _ifaceppp->data()->modemConnectResp();
00403 setExpect("\n");
00404 vmain = 101;
00405 return;
00406 }
00407
00408 if(readbuffer.contains(_ifaceppp->data()->modemBusyResp())) {
00409 timeout_timer->stop();
00410 timeout_timer->start(_ifaceppp->data()->modemTimeout()*1000);
00411
00412 messg->setText(QObject::tr("Line busy. Hanging up..."));
00413 emit debugPutChar('\n');
00414 _ifaceppp->modem()->hangup();
00415
00416 if(_ifaceppp->data()->busyWait() > 0) {
00417 QString bm = QObject::tr("Line busy. Waiting: %1 seconds").arg(_ifaceppp->data()->busyWait());
00418 messg->setText(bm);
00419 emit debugMessage(bm);
00420
00421 pausing = true;
00422
00423 pausetimer->start(_ifaceppp->data()->busyWait()*1000, true);
00424 timeout_timer->stop();
00425 }
00426
00427 _ifaceppp->modem()->setDataMode(false);
00428 vmain = 0;
00429 substate = -1;
00430 return;
00431 }
00432
00433 if(readbuffer.contains(_ifaceppp->data()->modemNoDialtoneResp())) {
00434 timeout_timer->stop();
00435
00436 messg->setText(QObject::tr("No Dialtone"));
00437 vmain = 20;
00438 _ifaceppp->modem()->unlockdevice();
00439 return;
00440 }
00441
00442 if(readbuffer.contains(_ifaceppp->data()->modemNoCarrierResp())) {
00443 timeout_timer->stop();
00444
00445 messg->setText(QObject::tr("No Carrier"));
00446 vmain = 20;
00447 _ifaceppp->modem()->unlockdevice();
00448 return;
00449 }
00450 }
00451
00452
00453 if(vmain == 101) {
00454 if(!expecting) {
00455 _ifaceppp->modem()->setDataMode(true);
00456
00457 emit startAccounting();
00458
00459
00460 vmain = 2;
00461 scriptTimeout=_ifaceppp->data()->modemTimeout()*1000;
00462 return;
00463 }
00464 }
00465
00466
00467 if(vmain == 2) {
00468 if(!expecting && !pausing && !scanning) {
00469
00470 timeout_timer->stop();
00471 timeout_timer->start(scriptTimeout);
00472
00473 if((unsigned) scriptindex < comlist->count()) {
00474 scriptCommand = *(comlist->at(scriptindex));
00475 scriptArgument = *(arglist->at(scriptindex));
00476 } else {
00477 odebug << "End of script" << oendl;
00478 vmain = 10;
00479 return;
00480 }
00481
00482 if (scriptCommand == "Scan") {
00483 QString bm = QObject::tr("Scanning %1").arg(scriptArgument);
00484 messg->setText(bm);
00485 emit debugMessage(bm);
00486
00487 setScan(scriptArgument);
00488 scriptindex++;
00489 return;
00490 }
00491
00492 if (scriptCommand == "Save") {
00493 QString bm = QObject::tr("Saving %1").arg(scriptArgument);
00494 messg->setText(bm);
00495 emit debugMessage(bm);
00496
00497 if (scriptArgument.lower() == "password") {
00498 _ifaceppp->data()->setPassword(scanvar);
00499
00500 if(_ifaceppp->data()->storePassword())
00501 _ifaceppp->data()->setStoredPassword(scanvar);
00502 firstrunPW = true;
00503 }
00504
00505 scriptindex++;
00506 return;
00507 }
00508
00509
00510 if (scriptCommand == "Send" || scriptCommand == "SendNoEcho") {
00511 QString bm = QObject::tr("Sending %1");
00512
00513
00514 QString arg = scriptArgument;
00515 QRegExp re1("%USERNAME%");
00516 QRegExp re2("%PASSWORD%");
00517 arg = arg.replace(re1, _ifaceppp->data()->storedUsername());
00518 arg = arg.replace(re2, _ifaceppp->data()->storedPassword());
00519
00520 if (scriptCommand == "Send")
00521 bm = bm.arg(scriptArgument);
00522 else {
00523 for(uint i = 0; i < scriptArgument.length(); i++)
00524 bm = bm.arg("*");
00525 }
00526
00527 messg->setText(bm);
00528 emit debugMessage(bm);
00529
00530 writeline(scriptArgument);
00531 scriptindex++;
00532 return;
00533 }
00534
00535 if (scriptCommand == "Expect") {
00536 QString bm = QObject::tr("Expecting %1").arg(scriptArgument);
00537 messg->setText(bm);
00538 emit debugMessage(bm);
00539
00540
00541
00542
00543 scriptindex++;
00544 setExpect(scriptArgument);
00545 return;
00546 }
00547
00548
00549 if (scriptCommand == "Pause") {
00550 QString bm = QObject::tr("Pause %1 seconds").arg(scriptArgument);
00551 messg->setText(bm);
00552 emit debugMessage(bm);
00553
00554 pausing = true;
00555
00556 pausetimer->start(scriptArgument.toInt()*1000, true);
00557 timeout_timer->stop();
00558
00559 scriptindex++;
00560 return;
00561 }
00562
00563 if (scriptCommand == "Timeout") {
00564
00565 timeout_timer->stop();
00566
00567 QString bm = QObject::tr("Timeout %1 seconds").arg(scriptArgument);
00568 messg->setText(bm);
00569 emit debugMessage(bm);
00570
00571 scriptTimeout=scriptArgument.toInt()*1000;
00572 timeout_timer->start(scriptTimeout);
00573
00574 scriptindex++;
00575 return;
00576 }
00577
00578 if (scriptCommand == "Hangup") {
00579 messg->setText(QObject::tr("Hangup"));
00580 emit debugMessage(QObject::tr("Hangup"));
00581
00582 writeline(_ifaceppp->data()->modemHangupStr());
00583 setExpect(_ifaceppp->data()->modemHangupResp());
00584
00585 scriptindex++;
00586 return;
00587 }
00588
00589 if (scriptCommand == "Answer") {
00590
00591 timeout_timer->stop();
00592
00593 messg->setText(QObject::tr("Answer"));
00594 emit debugMessage(QObject::tr("Answer"));
00595
00596 setExpect(_ifaceppp->data()->modemRingResp());
00597 vmain = 150;
00598 return;
00599 }
00600
00601 if (scriptCommand == "ID") {
00602 QString bm = QObject::tr("ID %1").arg(scriptArgument);
00603 messg->setText(bm);
00604 emit debugMessage(bm);
00605
00606 QString idstring = _ifaceppp->data()->password();
00607
00608 if(!idstring.isEmpty() && firstrunID) {
00609
00610 writeline(idstring);
00611 firstrunID = false;
00612 scriptindex++;
00613 }
00614 else {
00615
00616
00617
00618 if (prompt->Consumed()) {
00619 if (!(prompt->isVisible())) {
00620 prompt->setPrompt(scriptArgument);
00621 prompt->setEchoModeNormal();
00622 prompt->show();
00623 }
00624 } else {
00625
00626 if(!(prompt->isVisible())) {
00627 writeline(prompt->text());
00628 prompt->setConsumed();
00629 scriptindex++;
00630 return;
00631 }
00632
00633 }
00634 }
00635 }
00636
00637 if (scriptCommand == "Password") {
00638 QString bm = QObject::tr("Password %1").arg(scriptArgument);
00639 messg->setText(bm);
00640 emit debugMessage(bm);
00641
00642 QString pwstring = _ifaceppp->data()->password();
00643
00644 if(!pwstring.isEmpty() && firstrunPW) {
00645
00646 writeline(pwstring);
00647 firstrunPW = false;
00648 scriptindex++;
00649 }
00650 else {
00651
00652
00653
00654 if (prompt->Consumed()) {
00655 if (!(prompt->isVisible())) {
00656 prompt->setPrompt(scriptArgument);
00657 prompt->setEchoModePassword();
00658 prompt->show();
00659 }
00660 } else {
00661
00662 if(!(prompt->isVisible())) {
00663
00664 writeline(prompt->text());
00665 prompt->setConsumed();
00666 scriptindex++;
00667 return;
00668 }
00669
00670 }
00671 }
00672 }
00673
00674 if (scriptCommand == "Prompt") {
00675 QString bm = QObject::tr("Prompting %1");
00676
00677
00678
00679
00680
00681 QString ts = scriptArgument;
00682 int vstart = ts.find( "##" );
00683 if( vstart != -1 ) {
00684 ts.remove( vstart, 2 );
00685 ts.insert( vstart, scanvar );
00686 }
00687
00688 bm = bm.arg(ts);
00689 messg->setText(bm);
00690 emit debugMessage(bm);
00691
00692
00693 if (prompt->Consumed()) {
00694 if (!(prompt->isVisible())) {
00695 prompt->setPrompt( ts );
00696 prompt->setEchoModeNormal();
00697 prompt->show();
00698 }
00699 } else {
00700
00701 if (!(prompt->isVisible())) {
00702 writeline(prompt->text());
00703 prompt->setConsumed();
00704 scriptindex++;
00705 return;
00706 }
00707
00708 }
00709 }
00710
00711 if (scriptCommand == "PWPrompt") {
00712 QString bm = QObject::tr("PW Prompt %1").arg(scriptArgument);
00713 messg->setText(bm);
00714 emit debugMessage(bm);
00715
00716
00717 if (prompt->Consumed()) {
00718 if (!(prompt->isVisible())) {
00719 prompt->setPrompt(scriptArgument);
00720 prompt->setEchoModePassword();
00721 prompt->show();
00722 }
00723 } else {
00724
00725 if (!(prompt->isVisible())) {
00726 writeline(prompt->text());
00727 prompt->setConsumed();
00728 scriptindex++;
00729 return;
00730 }
00731
00732 }
00733 }
00734
00735 if (scriptCommand == "LoopStart") {
00736
00737 QString bm = QObject::tr("Loop Start %1").arg(scriptArgument);
00738
00739
00740
00741
00742 scriptindex++;
00743
00744 if ( loopnest > (MAXLOOPNEST-2) ) {
00745 bm += QObject::tr("ERROR: Nested too deep, ignored.");
00746 vmain=20;
00747 cancelbutton();
00748 QMessageBox::critical(0, "error", QObject::tr("Loops nested too deeply!"));
00749 } else {
00750 setExpect(scriptArgument);
00751 loopstartindex[loopnest] = scriptindex;
00752 loopstr[loopnest] = scriptArgument;
00753 loopend = false;
00754 loopnest++;
00755 }
00756 messg->setText(bm);
00757 emit debugMessage(bm);
00758
00759 }
00760
00761 if (scriptCommand == "LoopEnd") {
00762 QString bm = QObject::tr("Loop End %1").arg(scriptArgument);
00763 if ( loopnest <= 0 ) {
00764 bm = QObject::tr("LoopEnd without matching Start! Line: %1").arg(bm);
00765 vmain=20;
00766 cancelbutton();
00767 QMessageBox::critical(0, "error", bm);
00768 return;
00769 } else {
00770
00771
00772
00773 scriptindex++;
00774 setExpect(scriptArgument);
00775 loopnest--;
00776 loopend = true;
00777 }
00778 messg->setText(bm);
00779 emit debugMessage(bm);
00780
00781 }
00782 }
00783 }
00784
00785
00786
00787 if(vmain == 150) {
00788 if(!expecting) {
00789 writeline(_ifaceppp->data()->modemAnswerStr());
00790 setExpect(_ifaceppp->data()->modemAnswerResp());
00791
00792 vmain = 2;
00793 scriptindex++;
00794 return;
00795 }
00796 }
00797
00798 if(vmain == 30) {
00799
00800
00801
00802
00803
00804 cancelbutton();
00805 }
00806
00807 if(vmain == 10) {
00808 if(!expecting) {
00809
00810 int result;
00811
00812 timeout_timer->stop();
00813 if_timeout_timer->stop();
00814
00815
00816 _ifaceppp->modem()->stop();
00817
00818 if(_ifaceppp->data()->authMethod() == AUTH_TERMINAL) {
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830 }
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840 killTimer( main_timer_ID );
00841
00842 if_timeout_timer->start(_ifaceppp->data()->pppdTimeout()*1000);
00843 odebug << "started if timeout timer with " << _ifaceppp->data()->pppdTimeout()*1000 << "" << oendl;
00844
00845
00846
00847
00848 qApp->flushX();
00849 semaphore = true;
00850 result = execppp();
00851
00852 emit debugMessage(QObject::tr("Starting pppd..."));
00853 odebug << "execppp() returned with return-code " << result << "" << oendl;
00854
00855 if(result) {
00856 if(!_ifaceppp->data()->autoDNS())
00857 adddns( _ifaceppp );
00858
00859
00860
00861
00862 emit if_waiting_signal();
00863 } else {
00864
00865
00866
00867 if_timeout_timer->stop();
00868 this->hide();
00869 messg->setText("");
00870
00871
00872 qApp->processEvents();
00873 _ifaceppp->modem()->hangup();
00874 emit stopAccounting();
00875
00876 _ifaceppp->modem()->closetty();
00877 _ifaceppp->modem()->unlockdevice();
00878
00879 }
00880
00881 return;
00882 }
00883 }
00884
00885
00886
00887 if(vmain == 20) {
00888 }
00889 }
00890
00891
00892 void ConnectWidget::set_con_speed_string() {
00893
00894
00895
00896
00897
00898 }
00899
00900
00901
00902 void ConnectWidget::readChar(unsigned char c) {
00903 if(semaphore)
00904 return;
00905
00906 readbuffer += c;
00907 myreadbuffer += c;
00908
00909
00910
00911 if( scanning )
00912 scanbuffer += c;
00913
00914
00915 emit debugPutChar(c);
00916
00917 checkBuffers();
00918 }
00919
00920
00921 void ConnectWidget::checkBuffers() {
00922
00923
00924
00925 if( scanning && scanbuffer.contains(scanstr) &&
00926 ( scanbuffer.right(1) == "\n" || scanbuffer.right(1) == "\r") ) {
00927 scanning = false;
00928
00929 int vstart = scanbuffer.find( scanstr ) + scanstr.length();
00930 scanvar = scanbuffer.mid( vstart, scanbuffer.length() - vstart);
00931 scanvar = scanvar.stripWhiteSpace();
00932
00933
00934 QString sv = QObject::tr("Scan Var: %1").arg(scanvar);
00935 emit debugMessage(sv);
00936 }
00937
00938 if(expecting) {
00939 if(readbuffer.contains(expectstr)) {
00940 expecting = false;
00941
00942 readbuffer.remove(0, readbuffer.find(expectstr) + expectstr.length());
00943
00944 QString ts = QObject::tr("Found: %1").arg(expectstr);
00945 emit debugMessage(ts);
00946
00947 if (loopend) {
00948 loopend=false;
00949 }
00950 }
00951
00952 if (loopend && readbuffer.contains(loopstr[loopnest])) {
00953 expecting = false;
00954 readbuffer = "";
00955 QString ts = QObject::tr("Looping: %1").arg(loopstr[loopnest]);
00956 emit debugMessage(ts);
00957 scriptindex = loopstartindex[loopnest];
00958 loopend = false;
00959 loopnest++;
00960 }
00961
00962 if(!expecting)
00963 timerEvent((QTimerEvent *) 0);
00964 }
00965 }
00966
00967
00968
00969 void ConnectWidget::pause() {
00970 pausing = false;
00971 pausetimer->stop();
00972 }
00973
00974
00975 void ConnectWidget::cancelbutton() {
00976 _ifaceppp->modem()->stop();
00977 killTimer(main_timer_ID);
00978 timeout_timer->stop();
00979 if_timer->stop();
00980 if_timeout_timer->stop();
00981
00982
00983
00984
00985
00986
00987
00988 messg->setText(QObject::tr("One moment please..."));
00989
00990
00991 _ifaceppp->modem()->removeSecret(AUTH_PAP);
00992 _ifaceppp->modem()->removeSecret(AUTH_CHAP);
00993 removedns(_ifaceppp);
00994
00995 qApp->processEvents();
00996
00997 _ifaceppp->modem()->killPPPDaemon();
00998 _ifaceppp->modem()->hangup();
00999
01000
01001
01002
01003
01004
01005 _ifaceppp->modem()->closetty();
01006 _ifaceppp->modem()->unlockdevice();
01007
01008
01009 if (prompt->isVisible()) {
01010 prompt->hide();
01011 }
01012 prompt->setConsumed();
01013
01014 _ifaceppp->setStatus( false );
01015 _ifaceppp->refresh();
01016
01017 refresh();
01018 }
01019
01020
01021 void ConnectWidget::script_timed_out() {
01022 if(vmain == 20) {
01023 timeout_timer->stop();
01024 emit stopAccounting();
01025
01026 return;
01027 }
01028
01029 if (prompt->isVisible())
01030 prompt->hide();
01031
01032 prompt->setConsumed();
01033 messg->setText(QObject::tr("Script timed out!"));
01034 _ifaceppp->modem()->hangup();
01035 emit stopAccounting();
01036
01037
01038 vmain = 0;
01039 substate = -1;
01040 }
01041
01042
01043 void ConnectWidget::setScan(const QString &n) {
01044 scanning = true;
01045 scanstr = n;
01046 scanbuffer = "";
01047
01048 QString ts = QObject::tr("Scanning: %1").arg(n);
01049 emit debugMessage(ts);
01050 }
01051
01052
01053 void ConnectWidget::setExpect(const QString &n) {
01054 expecting = true;
01055 expectstr = n;
01056
01057 QString ts = QObject::tr("Expecting: %1").arg(n);
01058 ts.replace(QRegExp("\n"), "<LF>");
01059 emit debugMessage(ts);
01060
01061
01062 checkBuffers();
01063 }
01064
01065
01066 void ConnectWidget::if_waiting_timed_out() {
01067 if_timer->stop();
01068 if_timeout_timer->stop();
01069 odebug << "if_waiting_timed_out()" << oendl;
01070
01071 _ifaceppp->data()->setpppdError(E_IF_TIMEOUT);
01072
01073
01074 _ifaceppp->modem()->killPPPDaemon();
01075
01076 emit stopAccounting();
01077
01078
01079
01080
01081
01082
01083 }
01084
01085 void ConnectWidget::pppdDied()
01086 {
01087 if_timer->stop();
01088 if_timeout_timer->stop();
01089 }
01090
01091 void ConnectWidget::if_waiting_slot() {
01092 messg->setText(QObject::tr("Logging on to network..."));
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109 if_timeout_timer->stop();
01110 if_timer->stop();
01111 usleep(200000);
01112
01113 if(_ifaceppp->data()->autoDNS())
01114 addpeerdns( _ifaceppp );
01115
01116
01117
01118 emit closeDebugWindow();
01119
01120 auto_hostname(_ifaceppp);
01121
01122 if(!_ifaceppp->data()->command_on_connect().isEmpty()) {
01123 messg->setText(QObject::tr("Running startup command..."));
01124
01125
01126 qApp->flushX();
01127 execute_command(_ifaceppp->data()->command_on_connect());
01128 messg->setText(QObject::tr("Done"));
01129 }
01130
01131
01132 _ifaceppp->modem()->removeSecret(AUTH_PAP);
01133 _ifaceppp->modem()->removeSecret(AUTH_CHAP);
01134
01135 emit debugMessage(QObject::tr("Done"));
01136 set_con_speed_string();
01137
01138
01139
01140
01141
01142 _ifaceppp->setStatus( true );
01143
01144 m_refreshTimer = new QTimer( this );
01145 connect( m_refreshTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
01146 m_refreshTimer->start( 3000 );
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170 _ifaceppp->modem()->closetty();
01171 }
01172
01173 void ConnectWidget::refresh() {
01174 _ifaceppp->refresh();
01175 if ( _ifaceppp->getStatus() ) {
01176 messg->setText(QObject::tr("Online"));
01177 } else {
01178 messg->setText(QObject::tr("Offline"));
01179 }
01180 }
01181
01182
01183 bool ConnectWidget::execppp() {
01184 QString command;
01185
01186 command = "pppd";
01187
01188
01189
01190
01191
01192
01193
01194
01195 command += " " + _ifaceppp->data()->speed();
01196
01197 command += " -detach";
01198
01199 if(_ifaceppp->data()->ipaddr() != "0.0.0.0" ||
01200 _ifaceppp->data()->gateway() != "0.0.0.0") {
01201 if(_ifaceppp->data()->ipaddr() != "0.0.0.0") {
01202 command += " ";
01203 command += _ifaceppp->data()->ipaddr();
01204 command += ":";
01205 }
01206 else {
01207 command += " ";
01208 command += ":";
01209 }
01210
01211 if(_ifaceppp->data()->gateway() != "0.0.0.0")
01212 command += _ifaceppp->data()->gateway();
01213 }
01214
01215 if(_ifaceppp->data()->subnetmask() != "0.0.0.0")
01216 command += " netmask " + _ifaceppp->data()->subnetmask();
01217
01218 if(_ifaceppp->data()->flowcontrol() != PPPData::FlowNone) {
01219 if(_ifaceppp->data()->flowcontrol() == PPPData::FlowHardware)
01220 command += " crtscts";
01221 else
01222 command += " xonxoff";
01223 }
01224
01225 if(_ifaceppp->data()->defaultroute())
01226 command += " defaultroute";
01227
01228 if(_ifaceppp->data()->autoDNS())
01229 command += " usepeerdns";
01230
01231
01232
01233 if(_ifaceppp->data()->authMethod() == AUTH_PAP) {
01234 command += " -chap user ";
01235 command = command + _ifaceppp->data()->storedUsername();
01236 }
01237
01238
01239 if(_ifaceppp->data()->authMethod() == AUTH_CHAP) {
01240 command += " -pap user ";
01241 command = command + _ifaceppp->data()->storedUsername();
01242 }
01243
01244
01245 if(_ifaceppp->data()->authMethod() == AUTH_PAPCHAP) {
01246 QString tmpName = _ifaceppp->data()->storedUsername();
01247 if ( !tmpName.isEmpty() ) {
01248 command += " user ";
01249 command = command + tmpName;
01250 }
01251 }
01252
01253
01254 if(_ifaceppp->data()->getPPPDebug())
01255 command += " debug";
01256
01257 QStringList &arglist = _ifaceppp->data()->pppdArgument();
01258 for ( QStringList::Iterator it = arglist.begin();
01259 it != arglist.end();
01260 ++it )
01261 {
01262 command += " " + *it;
01263 }
01264
01265 command += " call opie-kppp logfd 11";
01266
01267 if (command.length() > MAX_CMDLEN) {
01268 QMessageBox::critical(this, "error", QObject::tr(
01269 "pppd command + command-line arguments exceed "
01270 "2024 characters in length."
01271 ));
01272
01273 return false;
01274 }
01275
01276 owarn << "Command IS: " << command.latin1() << "" << oendl;
01277
01278 qApp->flushX();
01279
01280 return _ifaceppp->modem()->execPPPDaemon(command);
01281 }
01282
01283
01284 void ConnectWidget::closeEvent( QCloseEvent *e ) {
01285 e->ignore();
01286 emit cancelbutton();
01287 }
01288
01289
01290 void ConnectWidget::setMsg(const QString &msg) {
01291 messg->setText(msg);
01292 }
01293
01294 void ConnectWidget::writeline(const QString &s) {
01295 _ifaceppp->modem()->writeLine(s.local8Bit());
01296 }
01297
01298
01299 void auto_hostname(InterfacePPP *_ifaceppp) {
01300 struct in_addr local_ip;
01301 struct hostent *hostname_entry;
01302 QString new_hostname;
01303 int dot;
01304 char tmp_str[100];
01305
01306 gethostname(tmp_str, sizeof(tmp_str));
01307 tmp_str[sizeof(tmp_str)-1]=0;
01308 old_hostname=tmp_str;
01309
01310
01311 if ( _ifaceppp->data()->autoname()) {
01312
01313 hostname_entry=gethostbyaddr((const char *)&local_ip,sizeof(in_addr),AF_INET);
01314
01315 if (hostname_entry != 0L) {
01316 new_hostname=hostname_entry->h_name;
01317 dot=new_hostname.find('.');
01318 new_hostname=new_hostname.remove(dot,new_hostname.length()-dot);
01319 _ifaceppp->modem()->setHostname(new_hostname);
01320 modified_hostname = TRUE;
01321
01322 new_hostname=hostname_entry->h_name;
01323 new_hostname.remove(0,dot+1);
01324
01325 add_domain(new_hostname, _ifaceppp);
01326 }
01327 }
01328
01329 }
01330
01331
01332
01333 void add_domain(const QString &domain, InterfacePPP *_ifaceppp) {
01334
01335 int fd;
01336 char c;
01337 QString resolv[MAX_RESOLVCONF_LINES];
01338
01339 if (domain.isEmpty())
01340 return;
01341
01342 if((fd = _ifaceppp->modem()->openResolv(O_RDONLY)) >= 0) {
01343
01344 int i=0;
01345 while((read(fd, &c, 1) == 1) && (i < MAX_RESOLVCONF_LINES)) {
01346 if(c == '\n') {
01347 i++;
01348 }
01349 else {
01350 resolv[i] += c;
01351 }
01352 }
01353 close(fd);
01354 if ((c != '\n') && (i < MAX_RESOLVCONF_LINES)) i++;
01355
01356 if((fd = _ifaceppp->modem()->openResolv(O_WRONLY|O_TRUNC)) >= 0) {
01357 QCString tmp = "domain " + domain.local8Bit() +
01358 " \t\t#kppp temp entry\n";
01359 write(fd, tmp.data(), tmp.length());
01360
01361 for(int j=0; j < i; j++) {
01362 if((resolv[j].contains("domain") ||
01363 ( resolv[j].contains("nameserver")
01364 && !resolv[j].contains("#kppp temp entry")
01365 && _ifaceppp->data()->exDNSDisabled()))
01366 && !resolv[j].contains("#entry disabled by kppp")) {
01367 QCString tmp = "# " + resolv[j].local8Bit() +
01368 " \t#entry disabled by kppp\n";
01369 write(fd, tmp, tmp.length());
01370 }
01371 else {
01372 QCString tmp = resolv[j].local8Bit() + "\n";
01373 write(fd, tmp, tmp.length());
01374 }
01375 }
01376 }
01377 close(fd);
01378 }
01379 }
01380
01381
01382
01383 void adddns( InterfacePPP *_ifaceppp)
01384 {
01385 int fd;
01386
01387 if ((fd = _ifaceppp->modem()->openResolv(O_WRONLY|O_APPEND)) >= 0) {
01388 QStringList &dnslist = _ifaceppp->data()->dns();
01389 for ( QStringList::Iterator it = dnslist.begin();
01390 it != dnslist.end();
01391 ++it )
01392 {
01393 QCString dns = "nameserver " + (*it).local8Bit() +
01394 " \t#kppp temp entry\n";
01395 write(fd, dns.data(), dns.length());
01396 }
01397 close(fd);
01398 }
01399 add_domain(_ifaceppp->data()->domain(), _ifaceppp);
01400 }
01401
01402 void addpeerdns(InterfacePPP *_ifaceppp) {
01403 int fd, fd2;
01404
01405 if((fd = _ifaceppp->modem()->openResolv(O_WRONLY|O_APPEND)) >= 0) {
01406 if((fd2 = open("/etc/ppp/resolv.conf", O_RDONLY)) >= 0) {
01407 char c;
01408 int i = 0;
01409 while(i++ < 100 && read(fd2, &c, 1) == 1) {
01410 if(c == '\n')
01411 write(fd, "\t#kppp temp entry\n", 18);
01412 else
01413 write(fd, &c, 1);
01414 }
01415 close(fd2);
01416 } else
01417 fprintf(stderr, "failed to read from /etc/ppp/resolv.conf\n");
01418 close(fd);
01419 }
01420 add_domain(_ifaceppp->data()->domain(), _ifaceppp);
01421 }
01422
01423
01424 void removedns(InterfacePPP *_ifaceppp) {
01425
01426 int fd;
01427 char c;
01428 QString resolv[MAX_RESOLVCONF_LINES];
01429
01430 if((fd = _ifaceppp->modem()->openResolv(O_RDONLY)) >= 0) {
01431
01432 int i=0;
01433 while(read(fd, &c, 1) == 1 && i < MAX_RESOLVCONF_LINES) {
01434 if(c == '\n') {
01435 i++;
01436 }
01437 else {
01438 resolv[i] += c;
01439 }
01440 }
01441 close(fd);
01442
01443 if((fd = _ifaceppp->modem()->openResolv(O_WRONLY|O_TRUNC)) >= 0) {
01444 for(int j=0; j < i; j++) {
01445 if(resolv[j].contains("#kppp temp entry")) continue;
01446 if(resolv[j].contains("#entry disabled by kppp")) {
01447 QCString tmp = resolv[j].local8Bit();
01448 write(fd, tmp.data()+2, tmp.length() - 27);
01449 write(fd, "\n", 1);
01450 }
01451 else {
01452 QCString tmp = resolv[j].local8Bit() + "\n";
01453 write(fd, tmp, tmp.length());
01454 }
01455 }
01456 }
01457 close(fd);
01458
01459 }
01460
01461 if ( modified_hostname ) {
01462 _ifaceppp->modem()->setHostname(old_hostname);
01463 modified_hostname = FALSE;
01464 }
01465
01466 }
01467
01468