Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

connect.cpp

Go to the documentation of this file.
00001 /*
00002  *            kPPP: A pppd front end for the KDE project
00003  *
00004  *
00005  *            Copyright (C) 1997 Bernd Johannes Wuebben
00006  *                   wuebben@math.cornell.edu
00007  *            Copyright (C) 1998-2001 Harri Porten <porten@kde.org>
00008  *
00009  * based on EzPPP:
00010  * Copyright (C) 1997  Jay Painter
00011  *
00012  * This program is free software; you can redistribute it and/or
00013  * modify it under the terms of the GNU Library General Public
00014  * License as published by the Free Software Foundation; either
00015  * version 2 of the License, or (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020  * Library General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU Library General Public
00023  * License along with this program; if not, write to the Free
00024  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  */
00026 
00027 /* OPIE */
00028 #include <opie2/odebug.h>
00029 using namespace Opie::Core;
00030 
00031 /* QT */
00032 #include <qlayout.h>
00033 #include <qregexp.h>
00034 #include <qapplication.h>
00035 #include <qmessagebox.h>
00036 #include <qpushbutton.h>
00037 
00038 /* STD */
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 //#include "docking.h"
00068 #include "interfaceppp.h"
00069 #include "modem.h"
00070 #include "kpppconfig.h"
00071 #include "pppdata.h"
00072 #include "kpppwidget.h"
00073 //#include "requester.h"
00074 //#include "utils.h"
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 //   int messw = (messg->sizeHint().width() * 12) / 10;
00114 //   messw = QMAX(messw,280);
00115 //   messg->setMinimumWidth(messw);
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 ); // FIXME: disable the log button
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 //   int maxw = QMAX(cancel->sizeHint().width(),
00138 //               debug->sizeHint().width());
00139 //   maxw = QMAX(maxw,65);
00140 //   debug->setFixedWidth(maxw);
00141 //   cancel->setFixedWidth(maxw);
00142   l1->addWidget(debug);
00143   l1->addWidget(cancel);
00144 
00145 //  setFixedSize(sizeHint());
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   // this is all just to keep the GUI nice and snappy ....
00175   // you have to see to believe ...
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 //  stats->totalbytes = 0;
00195   dialnumber = 0;
00196 
00197 //  p_kppp->con_speed = "";
00198 
00199 //  p_kppp->setQuitOnDisconnect (p_kppp->quitOnDisconnect() || _ifaceppp->data()->quit_on_disconnect());
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   // run the "before-connect" command
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 //     int i, status;
00219 
00220 //     do {
00221 //       qApp->processEvents();
00222 //       i = waitpid(id, &status, WNOHANG);
00223 //       usleep(100000);
00224 //     } while (i == 0 && errno == 0);
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; // wait until cancel is pressed
00232     return;
00233   }
00234 
00235   if (lock == -1) {
00236     messg->setText(QObject::tr("Unable to create modem lock file."));
00237     vmain = 20; // wait until cancel is pressed
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       // if we are stuck anywhere we will time out
00254       timeout_timer->start(_ifaceppp->data()->modemTimeout()*1000);
00255 
00256       // this timer will run the script etc.
00257       main_timer_ID = startTimer(10);
00258 
00259       return;
00260     }
00261   }
00262 
00263   // initialization failed
00264   messg->setText(_ifaceppp->modem()->modemMessage());
00265   vmain = 20; // wait until cancel is pressed
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     // first init string ?
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         // send a carriage return and then wait a bit so that the modem will
00291         // let us issue commands.
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); // 0.01 - 3.0 sec
00300     }
00301 
00302     substate++;
00303 
00304     /*
00305      * FIXME after 3.0: Make it possible to disable ATS11 since it
00306      * seems to be incompatible with some ISDN adapters (e.g. DataBox
00307      * Speed Dragon). Even better would be to detect this when doing
00308      * a "Modem Query"
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       // done with all init strings ?
00334       if(substate < PPPData::NumInitStrings) {
00335         vmain = 0;
00336         return;
00337       }
00338       substate = -1;
00339       // skip setting the volume if command is empty
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); // 0.01 - 3.0 sec
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   // dial the number and wait to connect
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   // wait for connect, but redial if BUSY or wait for user cancel
00399   // if NO CARRIER or NO DIALTONE
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   // wait for newline after CONNECT response (so we get the speed)
00453   if(vmain == 101) {
00454     if(!expecting) {
00455       _ifaceppp->modem()->setDataMode(true); // modem will no longer respond to AT commands
00456 
00457       emit startAccounting();
00458 //      p_kppp->con_win->startClock();
00459 
00460       vmain = 2;
00461       scriptTimeout=_ifaceppp->data()->modemTimeout()*1000;
00462       return;
00463     }
00464   }
00465 
00466   // execute the script
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 //        p_kppp->setPW_Edit(scanvar);
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         // replace %USERNAME% and %PASSWORD%
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         // The incrementing of the scriptindex MUST be before the
00541         // call to setExpect otherwise the expect will miss a string that is
00542         // already in the buffer.
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           // the user entered an Id on the main kppp dialog
00610           writeline(idstring);
00611           firstrunID = false;
00612           scriptindex++;
00613         }
00614         else {
00615           // the user didn't enter and Id on the main kppp dialog
00616           // let's query for an ID
00617              /* if not around yet, then post window... */
00618              if (prompt->Consumed()) {
00619                if (!(prompt->isVisible())) {
00620                  prompt->setPrompt(scriptArgument);
00621                  prompt->setEchoModeNormal();
00622                  prompt->show();
00623                }
00624              } else {
00625                /* if prompt withdrawn ... then, */
00626                if(!(prompt->isVisible())) {
00627                  writeline(prompt->text());
00628                  prompt->setConsumed();
00629                  scriptindex++;
00630                  return;
00631                }
00632                /* replace timeout value */
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           // the user entered a password on the main kppp dialog
00646           writeline(pwstring);
00647           firstrunPW = false;
00648           scriptindex++;
00649         }
00650         else {
00651           // the user didn't enter a password on the main kppp dialog
00652           // let's query for a password
00653              /* if not around yet, then post window... */
00654              if (prompt->Consumed()) {
00655                if (!(prompt->isVisible())) {
00656                  prompt->setPrompt(scriptArgument);
00657                  prompt->setEchoModePassword();
00658                  prompt->show();
00659                }
00660              } else {
00661                /* if prompt withdrawn ... then, */
00662                if(!(prompt->isVisible())) {
00663 //               p_kppp->setPW_Edit(prompt->text());
00664                  writeline(prompt->text());
00665                  prompt->setConsumed();
00666                  scriptindex++;
00667                  return;
00668                }
00669                /* replace timeout value */
00670              }
00671         }
00672       }
00673 
00674       if (scriptCommand == "Prompt") {
00675         QString bm = QObject::tr("Prompting %1");
00676 
00677         // if the scriptindex (aka the prompt text) includes a ## marker
00678         // this marker should get substituted with the contents of our stored
00679         // variable (from the subsequent scan).
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         /* if not around yet, then post window... */
00693         if (prompt->Consumed()) {
00694            if (!(prompt->isVisible())) {
00695                 prompt->setPrompt( ts );
00696                 prompt->setEchoModeNormal();
00697                 prompt->show();
00698            }
00699         } else {
00700             /* if prompt withdrawn ... then, */
00701             if (!(prompt->isVisible())) {
00702                 writeline(prompt->text());
00703                 prompt->setConsumed();
00704                 scriptindex++;
00705                 return;
00706             }
00707             /* replace timeout value */
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         /* if not around yet, then post window... */
00717         if (prompt->Consumed()) {
00718            if (!(prompt->isVisible())) {
00719                 prompt->setPrompt(scriptArgument);
00720                 prompt->setEchoModePassword();
00721                 prompt->show();
00722            }
00723         } else {
00724             /* if prompt withdrawn ... then, */
00725             if (!(prompt->isVisible())) {
00726                 writeline(prompt->text());
00727                 prompt->setConsumed();
00728                 scriptindex++;
00729                 return;
00730             }
00731             /* replace timeout value */
00732         }
00733       }
00734 
00735       if (scriptCommand == "LoopStart") {
00736 
00737         QString bm = QObject::tr("Loop Start %1").arg(scriptArgument);
00738 
00739         // The incrementing of the scriptindex MUST be before the
00740         // call to setExpect otherwise the expect will miss a string that is
00741         // already in the buffer.
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                 // NB! The incrementing of the scriptindex MUST be before the
00771                 // call to setExpect otherwise the expect will miss a string
00772                 // that is already in the buffer.
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   // this is a subroutine for the "Answer" script option
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 //     if (termwindow->isVisible())
00800 //       return;
00801 //     if (termwindow->pressedContinue())
00802 //       vmain = 10;
00803 //     else
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(); // better be sure.
00814 
00815       // stop reading of data
00816       _ifaceppp->modem()->stop();
00817 
00818       if(_ifaceppp->data()->authMethod() == AUTH_TERMINAL) {
00819       //     if (termwindow) {
00820 //        delete termwindow;
00821 //        termwindow = 0L;
00822 //        this->show();
00823 //      } else {
00824 //        termwindow = new LoginTerm(0L, 0L);
00825 //        hide();
00826 //        termwindow->show();
00827 //        vmain = 30;
00828 //        return;
00829 //      }
00830       }
00831 
00832       // Close the tty. This prevents the QTimer::singleShot() in
00833       // Modem::readtty() from re-enabling the socket notifier.
00834       // The port is still held open by the helper process.
00835 
00836       /* Er, there _is_ not QTimer::singleShot() in Modem::readtty(),
00837          and closing the thing prevents pppd from using it later. */
00838       //_ifaceppp->modem()->closetty();
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       // find out PPP interface and notify the stats module
00846 //      stats->setUnit(pppInterfaceNumber());
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         // O.K we are done here, let's change over to the if_waiting loop
00860         // where we wait for the ppp if (interface) to come up.
00861 
00862         emit if_waiting_signal();
00863       } else {
00864 
00865         // starting pppd wasn't successful. Error messages were
00866         // handled by execppp();
00867         if_timeout_timer->stop();
00868         this->hide();
00869         messg->setText("");
00870 //      p_kppp->quit_b->setFocus();
00871 //      p_kppp->show();
00872         qApp->processEvents();
00873         _ifaceppp->modem()->hangup();
00874         emit stopAccounting();
00875 //      p_kppp->con_win->stopClock();
00876         _ifaceppp->modem()->closetty();
00877         _ifaceppp->modem()->unlockdevice();
00878 
00879       }
00880 
00881       return;
00882     }
00883   }
00884 
00885   // this is a "wait until cancel" entry
00886 
00887   if(vmain == 20) {
00888   }
00889 }
00890 
00891 
00892 void ConnectWidget::set_con_speed_string() {
00893   // Here we are trying to determine the speed at which we are connected.
00894   // Usually the modem responds after connect with something like
00895   // CONNECT 115200, so all we need to do is find the number after CONNECT
00896   // or whatever the modemConnectResp() is.
00897 //  p_kppp->con_speed = _ifaceppp->modem()->parseModemSpeed(myreadbuffer);
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     // While in scanning mode store each char to the scan buffer
00910     // for use in the prompt command
00911     if( scanning )
00912        scanbuffer += c;
00913 
00914     // add to debug window
00915     emit debugPutChar(c);
00916 
00917     checkBuffers();
00918 }
00919 
00920 
00921 void ConnectWidget::checkBuffers() {
00922   // Let's check if we are finished with scanning:
00923   // The scanstring have to be in the buffer and the latest character
00924   // was a carriage return or an linefeed (depending on modem setup)
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       // Show the Variabel content in the debug window
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       // keep everything after the expected string
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     // notify event loop if expected string was found
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 //   if (termwindow) {
00983 //     delete termwindow;
00984 //     termwindow = 0L;
00985 //     this->show();
00986 //   }
00987 
00988   messg->setText(QObject::tr("One moment please..."));
00989 
00990   // just to be sure
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 //  p_kppp->quit_b->setFocus();
01002 //  p_kppp->show();
01003 //  emit stopAccounting();      // just to be sure
01004 //  p_kppp->con_win->stopClock();
01005   _ifaceppp->modem()->closetty();
01006   _ifaceppp->modem()->unlockdevice();
01007 
01008   //abort prompt window...
01009   if (prompt->isVisible()) {
01010       prompt->hide();
01011   }
01012   prompt->setConsumed();
01013 
01014   _ifaceppp->setStatus( false );
01015   _ifaceppp->refresh();
01016 //  messg->setText(tr("offline"));
01017   refresh();
01018 }
01019 
01020 
01021 void ConnectWidget::script_timed_out() {
01022   if(vmain == 20) { // we are in the 'wait for the user to cancel' state
01023     timeout_timer->stop();
01024     emit stopAccounting();
01025 //    p_kppp->con_win->stopClock();
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 //  p_kppp->con_win->stopClock();
01037 
01038   vmain = 0; // let's try again.
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   // check if the expected string is in the read buffer already.
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   // let's kill the stuck pppd
01074   _ifaceppp->modem()->killPPPDaemon();
01075 
01076   emit stopAccounting();
01077 //  p_kppp->con_win->stopClock();
01078 
01079 
01080   // killing ppp will generate a SIGCHLD which will be caught in pppdie()
01081   // in main.cpp what happens next will depend on the boolean
01082   // reconnect_on_disconnect which is set in ConnectWidget::init();
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 //   if(!stats->ifIsUp()) {
01095 
01096 //     if(_ifaceppp->data()->pppdError() != 0) {
01097 //       // we are here if pppd died immediately after starting it.
01098 //       pppdDied();
01099 //       // error message handled in main.cpp: sigPPPDDied()
01100 //       return;
01101 //     }
01102 
01103 //     if_timer->start(100, TRUE); // single shot
01104 //     return;
01105 //   }
01106 
01107   // O.K the ppp interface is up and running
01108   // give it a few time to come up completly (0.2 seconds)
01109   if_timeout_timer->stop();
01110   if_timer->stop();
01111   usleep(200000);
01112 
01113   if(_ifaceppp->data()->autoDNS())
01114     addpeerdns( _ifaceppp );
01115 
01116   // Close the debugging window. If we are connected, we
01117   // are not really interested in debug output
01118   emit closeDebugWindow();
01119 //  p_kppp->statdlg->take_stats(); // start taking ppp statistics
01120   auto_hostname(_ifaceppp);
01121 
01122   if(!_ifaceppp->data()->command_on_connect().isEmpty()) {
01123     messg->setText(QObject::tr("Running startup command..."));
01124 
01125     // make sure that we don't get any async errors
01126     qApp->flushX();
01127     execute_command(_ifaceppp->data()->command_on_connect());
01128     messg->setText(QObject::tr("Done"));
01129   }
01130 
01131   // remove the authentication file
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 //  p_kppp->con_win->setConnectionSpeed(p_kppp->con_speed);
01139 //  this->hide();
01140 //  messg->setText("");
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   //_ifaceppp->refresh();
01148   //  emit _ifaceppp->updateInterface(_ifaceppp);
01149 
01150   // prepare the con_win so as to have the right size for
01151   // accounting / non-accounting mode
01152 //   if(p_kppp->acct != 0)
01153 //     p_kppp->con_win->accounting(p_kppp->acct->running());
01154 //   else
01155 //     p_kppp->con_win->accounting(false);
01156 
01157 //   if (_ifaceppp->data()->get_dock_into_panel()) {
01158 // //    DockWidget::dock_widget->show();
01159 // //    DockWidget::dock_widget->take_stats();
01160 // //    this->hide();
01161 //   }
01162 //   else {
01163 // //    p_kppp->con_win->show();
01164 
01165 //     if(_ifaceppp->data()->get_iconify_on_connect()) {
01166 //         //    p_kppp->con_win->showMinimized();
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   // as of version 2.3.6 pppd falls back to the real user rights when
01189   // opening a device given in a command line. To avoid permission conflicts
01190   // we'll simply leave this argument away. pppd will then use the default tty
01191   // which is the serial port we connected stdin/stdout to in opener.cpp.
01192   //  command += " ";
01193   //  command += _ifaceppp->data()->modemDevice();
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   // PAP settings
01233   if(_ifaceppp->data()->authMethod() == AUTH_PAP) {
01234     command += " -chap user ";
01235     command = command + _ifaceppp->data()->storedUsername();
01236   }
01237 
01238   // CHAP settings
01239   if(_ifaceppp->data()->authMethod() == AUTH_CHAP) {
01240     command += " -pap user ";
01241     command = command  + _ifaceppp->data()->storedUsername();
01242   }
01243 
01244   // PAP/CHAP settings
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   // check for debug
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; // nonsensically long command which would bust my buffer buf.
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 // Set the hostname and domain from DNS Server
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]; // buffer overflow safe
01305 
01306   gethostname(tmp_str, sizeof(tmp_str));
01307   tmp_str[sizeof(tmp_str)-1]=0; // panic
01308   old_hostname=tmp_str; // copy to QString
01309 
01310   // if (!p_kppp->stats->local_ip_address.isEmpty() && _ifaceppp->data()->autoname()) {
01311   if ( _ifaceppp->data()->autoname()) {
01312 //    local_ip.s_addr=inet_addr(p_kppp->stats->local_ip_address.ascii());
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 // Replace the DNS domain entry in the /etc/resolv.conf file and
01332 // disable the nameserver entries if option is enabled
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 // adds the DNS entries in the /etc/resolv.conf file
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 // remove the dns entries from the /etc/resolv.conf file
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 

Generated on Sat Nov 5 16:17:49 2005 for OPIE by  doxygen 1.4.2