~ubuntu-branches/ubuntu/trusty/kwlan/trusty

« back to all changes in this revision

Viewing changes to kwlan/kwlaninterface.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2007-08-24 01:02:39 UTC
  • mfrom: (1.1.5 upstream) (0.1.3 lenny)
  • Revision ID: james.westby@ubuntu.com-20070824010239-e2xmgrrehh3n092w
Tags: 0.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
    m_fdSock = -1;
84
84
    m_state = DISABLED;
85
85
    m_isScanning = FALSE;
 
86
    m_isConfiguring = FALSE;
86
87
    KMessageBox::setDontShowAskAgainConfig(mainConfiguration().m_configuration);
87
88
    //get path to wpa_supplicant
88
89
    m_wpaPath = getPath("wpa_supplicant");
121
122
    memset(&m_devInfo, 0, sizeof(m_devInfo));
122
123
    strcpy(m_devInfo.ifr_name, interface.latin1());
123
124
    m_confFile = QString("kwlanrc.") + interface;
124
 
    m_configureInterface = mainConfiguration().readConfigureInterface( interface);
125
125
    // find out if we have a wireless interface
126
126
    m_controlInterface = strdup(interface.ascii());
127
127
    QStringList wirelessInterfaces = getInterfaces();
138
138
        // show wizard
139
139
        newInterfaceWizard(interface);
140
140
    }
141
 
    
 
141
    m_configureInterface = mainConfiguration().readConfigureInterface( interface);
142
142
    QStringList binDirs;
143
143
    if (m_type == ETHERNET)
144
144
    {
467
467
        
468
468
QStringList KwlanInterface::listNonWpaProfiles(QStringList wpaProfiles)
469
469
{
470
 
    bool encrypted;
471
470
    QStringList profiles;
472
471
    if (!m_configuration) return profiles;
473
472
    profiles = m_configuration->getNetworks();
483
482
            continue;
484
483
        }
485
484
            // Check whether the Network can be used without encryption
 
485
        /*
486
486
        m_configuration->readEncryption(*it,encrypted);
487
487
        if (encrypted) {
488
488
            it = profiles.remove(it);
489
489
            continue;
490
490
        }
 
491
        */
491
492
        it++;
492
493
    }
493
494
    //kdDebug() << profiles << endl;
582
583
 
583
584
bool KwlanInterface::profileActivateWpa(QString ssid)
584
585
{
 
586
    if (m_isConfiguring) return TRUE;
585
587
        // select network using wpa_supplicant
586
588
    if (!m_wpaStarted) return FALSE;
587
589
    int ret;
588
590
    char reply[40], cmd[256];
589
591
    size_t reply_len = sizeof(reply);
 
592
    m_ssid = ssid;
590
593
    int id = getWpaId(ssid);
591
594
    if (id == -1 ){
592
 
        KMessageBox::sorry(0, i18n("Could not find network in wpa_supplicant\nconfiguration."));
 
595
        // Seems to be a new network, so configure it.
 
596
        m_isConfiguring = TRUE;
 
597
        for (kwlNetworkList::iterator it = m_availableNetworks.begin();it != m_availableNetworks.end();it++)
 
598
        {
 
599
            if ((*it).m_ssid == ssid)
 
600
            {
 
601
                KProfileConfigDlg *nc = new KProfileConfigDlg();
 
602
                if (nc == NULL)
 
603
                    return FALSE;
 
604
                nc->setInterface(this);
 
605
                nc->paramsFromScanResults((*it));
 
606
                kdDebug() << (*it).m_flags << endl;
 
607
                nc->show();
 
608
                nc->exec();
 
609
                m_isConfiguring = FALSE;
 
610
                return TRUE;
 
611
            }
 
612
        }
 
613
        //KMessageBox::sorry(0, i18n("Could not find network in wpa_supplicant\nconfiguration."));
593
614
        return FALSE;
594
615
    }
595
616
    snprintf(cmd, sizeof(cmd), "DISCONNECT");
615
636
    // store in config
616
637
    if (m_configuration) m_configuration->writeLastSsid( ssid);
617
638
    // set ip data
618
 
    m_ssid = ssid;
619
639
    getIpData();
620
640
    
621
641
    checkWpaConnection();
633
653
        return profileActivateWpa(ssid);
634
654
    }
635
655
    else {
 
656
        // check if wpa_supplicant is needed for the network
 
657
        for (kwlNetworkList::iterator it = m_availableNetworks.begin();it != m_availableNetworks.end();it++)
 
658
        {
 
659
            if ((*it).m_ssid==ssid)
 
660
            {
 
661
                if ((*it).m_flags.contains("WPA") )
 
662
                {
 
663
                    int result = KMessageBox::questionYesNo(0,i18n("To activate this network, wpa_supplicant must be started. Do you want to start wpa_supplicant?"),
 
664
                                              "Kwlan");
 
665
                    if (result == KMessageBox::Yes)
 
666
                    {
 
667
                        KStartSupplicantDlg *startSupplicant = new KStartSupplicantDlg();
 
668
                        QString driver;
 
669
                        m_configuration->readDriver(driver);
 
670
                        if (startSupplicant )
 
671
                        {
 
672
                            QString iface = QString(m_controlInterface);
 
673
                            startSupplicant->setData(&iface,&driver);
 
674
                            startSupplicant->show();
 
675
                            startSupplicant->exec();
 
676
                            QString wpa_supplicant;
 
677
                            if (startSupplicant->m_start)
 
678
                            {
 
679
                                KwlanSuProcess *startWpa= new KwlanSuProcess(NULL,"Start WPA Supplicant");
 
680
                                *startWpa << m_wpaPath << QString("-i%1").arg(QString(m_controlInterface)) << QString ("-D%1").arg(startSupplicant->getDriver()) << QString("-c")+ m_wpaConf << "-B";
 
681
                                startWpa->setDescription(i18n("Start WPA Supplicant"));
 
682
                                startWpa->start();
 
683
                                m_configuration->writeDriver(startSupplicant->getDriver());
 
684
                                m_ssid = ssid;
 
685
                            }
 
686
                            delete startSupplicant;
 
687
                            m_networkChange = true;
 
688
                            return true;
 
689
                        }
 
690
                    }
 
691
                }
 
692
            }
 
693
        }
 
694
 
636
695
        // select network using iwconfig
637
696
        return profileActivateNonWpa(ssid);
638
697
    }
675
734
    }
676
735
    m_releaseProcess->setDescription(i18n("Release IP address"));
677
736
    connect (m_releaseProcess, SIGNAL(processExited(KwlanSuProcess*)), this, SLOT(releaseProcessExited(KwlanSuProcess*)));
678
 
    //m_releaseProcess->start(SuProcessBase::Block);
679
737
    m_releaseProcess->start();
680
 
    //m_interfaceData.ipAddress="";
681
 
    //emit ipChanged( QString(""));
682
738
}
683
739
 
684
740
void KwlanInterface::setDnsSettings()
711
767
        *setNetwork << ";";
712
768
        *setNetwork << getPath("chown") << "root:root" << "/etc/resolv.conf";
713
769
        setNetwork->setDescription(i18n("Set network settings"));
714
 
        //setNetwork->start(SuProcessBase::Block);
715
770
        setNetwork->start();
716
771
    }
717
772
}
732
787
 
733
788
void KwlanInterface::getIpAddress()
734
789
{
735
 
    if ((m_type == PPP)||(m_ssid.isEmpty()) || !m_configureInterface) return;
 
790
    if ((m_type == PPP)||(m_ssid.isEmpty()) || !m_configureInterface || m_isConfiguring) return;
736
791
    if (m_dhcpProcess) 
737
792
    {
738
793
        // We are already running
782
837
{
783
838
    char *pos = message, *pos2;
784
839
    int priority = 2;
785
 
    //kdDebug() << message << endl;
786
840
    if (*pos == '<') {
787
841
        /* skip priority */
788
842
        pos++;
811
865
        pos2 = pos;
812
866
        QString lastmsg = pos2;
813
867
        lastmsg.truncate(40);
814
 
    //textLastMessage->setText(lastmsg);
815
 
    //pingsToStatusUpdate = 0;
816
868
        m_networkChange = true;
817
869
    
818
870
        if (str_match(pos, WPA_CTRL_REQ))
877
929
    *startWpa << m_wpaPath << QString("-i%1").arg(QString(m_controlInterface)) << QString ("-D%1").arg(driver) << QString("-c") +m_wpaConf << "-B";
878
930
    startWpa->setDescription(i18n("Start WPA Supplicant"));
879
931
    startWpa->start();
880
 
        //remember last interface driver
 
932
    //remember last interface driver
881
933
    if (m_configuration) m_configuration->writeDriver(driver);
882
 
    //emit (wpaStatusChanged(TRUE));
883
934
    emit (networksChanged());
884
935
}
885
936
 
917
968
 
918
969
void KwlanInterface::updateData()
919
970
{
920
 
    bool tmpAvailable = m_interfaceData.available;
 
971
    //bool tmpAvailable = m_interfaceData.available;
921
972
    bool tmpUp = m_interfaceData.up;
922
 
    bool tmpConnected = m_interfaceData.connected;
 
973
    //bool tmpConnected = m_interfaceData.connected;
923
974
    FILE* flags_fp = fopen((m_sysClassPath+"flags").latin1(), "r");
924
975
    bool currentStatus;
925
976
    if (!flags_fp)
935
986
        fclose(flags_fp);
936
987
        currentStatus = IFF_UP & flags;
937
988
    }
938
 
    //if (!currentStatus && m_interfaceData.up) { // interface down...
939
989
    if (!currentStatus) { // interface down...
940
990
        // Interface is down and was up before -> set status variables
941
991
        m_interfaceData.up = FALSE;
1000
1050
                m_interfaceData.rxBytes = currentRxBytes;
1001
1051
            }
1002
1052
 
1003
 
            //m_interfaceData.incomingBytes = currentRxBytes - m_interfaceData.prevRxBytes;
1004
 
            //m_interfaceData.prevRxBytes = currentRxBytes;
1005
1053
            m_interfaceData.rxString = KIO::convertSize( m_interfaceData.rxBytes );
1006
1054
        
1007
1055
            unsigned long currentTxBytes = readInterfaceNumValue("statistics/tx_bytes");
1023
1071
                m_interfaceData.txBytes = currentTxBytes;
1024
1072
            }
1025
1073
 
1026
 
            //m_interfaceData.outgoingBytes = currentTxBytes - m_interfaceData.prevTxBytes;
1027
 
            //m_interfaceData.prevTxBytes = currentTxBytes;
1028
1074
            m_interfaceData.txString = KIO::convertSize( m_interfaceData.txBytes );
1029
1075
 
1030
1076
            m_interfaceData.rxPackets = readInterfaceNumValue("statistics/rx_packets");
1081
1127
            }
1082
1128
        }
1083
1129
    }
1084
 
    //if (tmpAvailable != m_interfaceData.available) 
1085
 
    //    emit (connectionStatusChanged (m_interfaceData.available));
1086
1130
    if (tmpUp != m_interfaceData.up)
1087
1131
    {
1088
1132
        emit (interfaceUp(m_interfaceData.up ));
1128
1172
    }
1129
1173
}
1130
1174
 
1131
 
void KwlanInterface::iwconfigProcessExited( KProcess* process )
1132
 
{
1133
 
    if ( process == m_iwconfigProcess )
1134
 
    {
1135
 
        delete m_iwconfigProcess;
1136
 
        m_iwconfigProcess = 0L;
1137
 
        parseIwconfigOutput();
1138
 
    }
1139
 
}
1140
 
 
1141
 
void KwlanInterface::iwconfigProcessStdout( KProcess*, char* buffer, int buflen )
1142
 
{
1143
 
    m_iwconfigStdout += QString::fromLatin1( buffer, buflen );
1144
 
}
1145
 
 
1146
 
void KwlanInterface::parseIwconfigOutput()
1147
 
{
1148
 
    /* mIwconfigStdout contains the complete output of 'iwconfig' which we
1149
 
    * are going to parse here.
1150
 
    */
1151
 
    QMap<QString, QString> configs;
1152
 
    QStringList ifList = QStringList::split( "\n\n", m_iwconfigStdout );
1153
 
    QStringList::Iterator it;
1154
 
    QString key;
1155
 
    for ( it = ifList.begin(); it != ifList.end(); ++it )
1156
 
    {
1157
 
        int index = ( *it ).find( ' ' );
1158
 
        if ( index == -1 )
1159
 
            continue;
1160
 
        key = ( *it ).left( index );
1161
 
        configs[key] = ( *it ).mid( index );
1162
 
    }
1163
 
    if ( configs.find( m_controlInterface) == configs.end() )
1164
 
    {
1165
 
        // The interface does not exist. Meaning the driver
1166
 
        // isn't loaded and/or the interface has not been created.
1167
 
        m_interfaceData.existing = false;
1168
 
        m_interfaceData.available = false;
1169
 
    }
1170
 
 
1171
 
    if ( configs[m_controlInterface].contains( "no wireless extensions" ) )
1172
 
    {
1173
 
        // The interface isn't a wireless device.
1174
 
        m_interfaceData.wirelessDevice = false;
1175
 
    }
1176
 
    else
1177
 
    {
1178
 
        // Update the wireless data of the interface.
1179
 
        m_interfaceData.wirelessDevice = true;
1180
 
        updateWirelessData( configs[m_controlInterface] );
1181
 
    }
1182
 
}
1183
 
 
1184
 
void KwlanInterface::updateWirelessData( QString& config )
1185
 
{
1186
 
    QRegExp regExp( "radio off" );
1187
 
    if ( regExp.search( config ) > -1 ) {
1188
 
        m_wirelessData.radioOff = TRUE;
1189
 
        return; //The rest of the information isn't needed when radio is off.
1190
 
    } else
1191
 
        m_wirelessData.radioOff = FALSE;
1192
 
 
1193
 
    regExp.setPattern( "ESSID:\"?([^\"]*)\"?" );
1194
 
    if ( regExp.search( config ) > -1 )
1195
 
        m_wirelessData.essid = regExp.cap( 1 );
1196
 
 
1197
 
    regExp.setPattern( "Mode:(\\w*)" );
1198
 
    if ( regExp.search( config ) > -1 )
1199
 
        m_wirelessData.mode = regExp.cap( 1 );
1200
 
 
1201
 
    regExp.setPattern( "Frequency:([\\w|\\.]*)" );
1202
 
    if ( regExp.search( config ) > -1 )
1203
 
        m_wirelessData.frequency = regExp.cap( 1 );
1204
 
    else
1205
 
    {
1206
 
        regExp.setPattern( "Channel:(\\d*)" );
1207
 
        if ( regExp.search( config ) > -1 )
1208
 
            m_wirelessData.channel = regExp.cap( 1 );
1209
 
    }
1210
 
 
1211
 
    regExp.setPattern( "Bit Rate[=:]([\\w/]*)" );
1212
 
    if ( regExp.search( config ) > -1 )
1213
 
        m_wirelessData.bitRate = regExp.cap( 1 );
1214
 
 
1215
 
    regExp.setPattern( "Signal level.(-?\\d+\\s*\\w+)" );
1216
 
    if ( regExp.search( config ) > -1 )
1217
 
        m_wirelessData.signal = regExp.cap( 1 );
1218
 
 
1219
 
    regExp.setPattern( "Noise level.(-?\\d+\\s*\\w+)" );
1220
 
    if ( regExp.search( config ) > -1 )
1221
 
        m_wirelessData.noise = regExp.cap( 1 );
1222
 
 
1223
 
    regExp.setPattern( "Link Quality[=:]([\\d/]*)" );
1224
 
    if ( regExp.search( config ) > -1 )
1225
 
        m_wirelessData.linkQuality = regExp.cap( 1 );
1226
 
}
1227
 
 
1228
 
void KwlanInterface::updateInterfaceData( QString& config, int type )
1229
 
{
1230
 
    bool hasIpAddress = TRUE;
1231
 
    if (m_interfaceData.ipAddress.isEmpty()) hasIpAddress = FALSE;
1232
 
    QRegExp regExp( ".*RX.*:(\\d+).*:\\d+.*:\\d+.*:\\d+" );
1233
 
    if ( regExp.search( config ) > -1 )
1234
 
        m_interfaceData.rxPackets = regExp.cap( 1 ).toULong();
1235
 
 
1236
 
    regExp.setPattern( ".*TX.*:(\\d+).*:\\d+.*:\\d+.*:\\d+" );
1237
 
    if ( regExp.search( config ) > -1 )
1238
 
        m_interfaceData.txPackets = regExp.cap( 1 ).toULong();
1239
 
 
1240
 
    regExp.setPattern( "RX bytes:(\\d+)\\s*\\(\\d+\\.\\d+\\s*\\w+\\)" );
1241
 
    if ( regExp.search( config ) > -1 )
1242
 
    {
1243
 
        // We count the traffic on ourself to avoid an overflow after
1244
 
        // 4GB of traffic.
1245
 
        unsigned long currentRxBytes = regExp.cap( 1 ).toULong();
1246
 
        if ( currentRxBytes < m_interfaceData.prevRxBytes )
1247
 
        {
1248
 
            // there was an overflow
1249
 
            m_interfaceData.rxBytes = currentRxBytes;
1250
 
            m_interfaceData.prevRxBytes = 0L;
1251
 
        }
1252
 
        if ( m_interfaceData.rxBytes == 0L )
1253
 
        {
1254
 
            // on startup set to currently received bytes
1255
 
            m_interfaceData.rxBytes = currentRxBytes;
1256
 
            m_interfaceData.prevRxBytes = currentRxBytes;
1257
 
        }
1258
 
        else
1259
 
            // afterwards only add difference to previous number of bytes
1260
 
        {
1261
 
            m_interfaceData.prevRxBytes = m_interfaceData.rxBytes;
1262
 
            m_interfaceData.rxBytes = currentRxBytes;
1263
 
        }
1264
 
 
1265
 
            //m_interfaceData.incomingBytes = currentRxBytes - m_interfaceData.prevRxBytes;
1266
 
            //m_interfaceData.prevRxBytes = currentRxBytes;
1267
 
        m_interfaceData.rxString = KIO::convertSize( m_interfaceData.rxBytes );
1268
 
    }
1269
 
 
1270
 
 
1271
 
    regExp.setPattern( "TX bytes:(\\d+)\\s*\\(\\d+\\.\\d+\\s*\\w+\\)" );
1272
 
    if ( regExp.search( config ) > -1 )
1273
 
    {
1274
 
        // We count the traffic on ourself to avoid an overflow after
1275
 
        // 4GB of traffic.
1276
 
        unsigned long currentTxBytes = regExp.cap( 1 ).toULong();
1277
 
        if ( currentTxBytes < m_interfaceData.prevTxBytes )
1278
 
        {
1279
 
            // there was an overflow
1280
 
            m_interfaceData.txBytes = currentTxBytes;
1281
 
            m_interfaceData.prevTxBytes = 0L;
1282
 
        }
1283
 
        if ( m_interfaceData.txBytes == 0L )
1284
 
        {
1285
 
            // on startup set to currently transmitted bytes
1286
 
            m_interfaceData.txBytes = currentTxBytes;
1287
 
            m_interfaceData.prevTxBytes = currentTxBytes;
1288
 
        }
1289
 
        else
1290
 
        {
1291
 
            m_interfaceData.prevTxBytes = m_interfaceData.txBytes;
1292
 
            m_interfaceData.txBytes = currentTxBytes;
1293
 
        }
1294
 
 
1295
 
            //m_interfaceData.outgoingBytes = currentTxBytes - m_interfaceData.prevTxBytes;
1296
 
            //m_interfaceData.prevTxBytes = currentTxBytes;
1297
 
        m_interfaceData.txString = KIO::convertSize( m_interfaceData.txBytes );
1298
 
    }
1299
 
  
1300
 
 
1301
 
    regExp.setPattern( "inet\\s+\\w+:(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" );
1302
 
    if ( regExp.search( config ) > -1 )
1303
 
    {
1304
 
        m_interfaceData.ipAddress = regExp.cap( 1 );
1305
 
        //if (!hasIpAddress) startScriptAfterConnect();
1306
 
    }
1307
 
    else m_interfaceData.ipAddress = "";
1308
 
    regExp.setPattern( "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}).*(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}).*(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" );
1309
 
    if ( regExp.search( config ) > -1 )
1310
 
    {
1311
 
        m_interfaceData.broadcastAddress = regExp.cap( 2 );
1312
 
        m_interfaceData.subnetMask = regExp.cap( 3 );
1313
 
    }
1314
 
    else {
1315
 
        m_interfaceData.broadcastAddress = "";
1316
 
        m_interfaceData.subnetMask = "";
1317
 
    }
1318
 
    
1319
 
 
1320
 
    if ( type == ETHERNET )
1321
 
    {
1322
 
        regExp.setPattern( "(.{2}:.{2}:.{2}:.{2}:.{2}:.{2})" );
1323
 
        if ( regExp.search( config ) > -1 )
1324
 
            m_interfaceData.hwAddress = regExp.cap( 1 );
1325
 
    }
1326
 
    else if (  type == PPP )
1327
 
    {
1328
 
        regExp.setPattern( "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}).*(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}).*(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})" );
1329
 
        if ( regExp.search( config ) > -1 )
1330
 
            m_interfaceData.ptpAddress = regExp.cap( 2 );
1331
 
    }
1332
 
}
1333
1175
 
1334
1176
void KwlanInterface::logViewer()
1335
1177
{
1511
1353
{
1512
1354
    int res = KMessageBox::warningContinueCancel(0,QString(i18n("New interface found!\n%1\n Click Continue to configure it now.")).arg(interface),"Kwlan",
1513
1355
            KStdGuiItem::cont(),"newinterfacewizard");
1514
 
    KMessageBox::saveDontShowAgainContinue("newinterfacewizard");
1515
1356
    if (res == KMessageBox::Continue)
1516
1357
    {
1517
1358
        //User wants to configure interface