mirror of
https://github.com/outbackdingo/sysadm.git
synced 2026-01-27 10:20:26 +00:00
Add a bit for rc.conf read support to sysadm.
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
int main( int argc, char ** argv )
|
||||
{
|
||||
//Run a simple test of all the sysadm backend functions
|
||||
qDebug() << "**** Test Network Devices ****";
|
||||
QStringList devs = sysadm::NetDevice::listNetDevices();
|
||||
qDebug() <<"Devices:" << devs;
|
||||
for(int i=0; i<devs.length(); i++){
|
||||
@@ -18,6 +19,7 @@ int main( int argc, char ** argv )
|
||||
qDebug() << " - netmask:" << D.netmaskAsString();
|
||||
qDebug() << " - description:" << D.desc();
|
||||
qDebug() << " - MAC:" << D.macAsString();
|
||||
qDebug() << " - Uses DHCP:" << D.usesDHCP();
|
||||
//qDebug() << " - Media Type:" << D.mediaTypeAsString();
|
||||
qDebug() << " - Media Status:" << D.mediaStatusAsString();
|
||||
bool iswifi = D.isWireless();
|
||||
@@ -29,5 +31,22 @@ int main( int argc, char ** argv )
|
||||
qDebug() << " - Packets Rx:" << D.packetsRx() << "Errors:" << D.errorsRx();
|
||||
qDebug() << " - Packets Tx:" << D.packetsTx() << "Errors:" << D.errorsTx();
|
||||
}
|
||||
//Now run tests on the other network functionality
|
||||
qDebug() << "**** Test Network Entries ****";
|
||||
//QList<NetworkEntry> entries = sysadm::Network::listNetworkEntries();
|
||||
for(int i=0; i<devs.length(); i++){
|
||||
//qDebug() << "Entry:" << entries[i].name << "Aliases:"<< entries[i].aliases << "Number:" << entries[i].netnum;
|
||||
sysadm::NetDevSettings set = sysadm::Network::deviceRCSettings(devs[i]);
|
||||
qDebug() << "RC Settings:";
|
||||
qDebug() << " - Device:" << set.device;
|
||||
qDebug() << " - Associated Device:" << set.asDevice;
|
||||
qDebug() << " - Use DHCP:" << set.useDHCP;
|
||||
qDebug() << " - Static IPv4:" << set.staticIPv4;
|
||||
qDebug() << " - Static IPv6:" << set.staticIPv6;
|
||||
qDebug() << " - Static Netmask:" << set.staticNetmask;
|
||||
qDebug() << " - Static Gateway:" << set.staticGateway;
|
||||
qDebug() << " - Allow Secure Wifi:" << set.wifisecurity;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ using namespace sysadm;
|
||||
//=====================
|
||||
// NETWORK FUNCTIONS
|
||||
//=====================
|
||||
QList<NetworkEntry> Network::listNetworkEntries(){
|
||||
/*QList<NetworkEntry> Network::listNetworkEntries(){
|
||||
QList<NetworkEntry> out;
|
||||
netent *entry = getnetent();
|
||||
while(entry!=0){
|
||||
@@ -29,7 +29,7 @@ QList<NetworkEntry> Network::listNetworkEntries(){
|
||||
}
|
||||
endnetent(); //make sure to close the file since we are finished reading it
|
||||
return out;
|
||||
}
|
||||
}*/
|
||||
|
||||
//---------------------------------------
|
||||
QStringList Network::readRcConf(){
|
||||
@@ -45,44 +45,48 @@ QStringList Network::readRcConf(){
|
||||
//---------------------------------------
|
||||
NetDevSettings Network::deviceRCSettings(QString dev){
|
||||
QStringList info = Network::readRcConf().filter(dev);
|
||||
//Setup the default structure/values
|
||||
NetDevSettings set;
|
||||
if(info.isEmpty()){ return set; } //no settings
|
||||
set.device = dev;
|
||||
set.wifihost = false;
|
||||
set.wifihost = set.useDHCP = set.wifisecurity = false;
|
||||
if(info.isEmpty()){ return set; } //no settings
|
||||
//Now load any info associated with this device
|
||||
for(int i=0; i<info.length(); i++){
|
||||
QString var = info[i].section("=",0,0).simplified();
|
||||
QString val = info[i].section("=",1,100).simplified();
|
||||
if(val.startsWith("\"")){ val = val.remove(1); }
|
||||
if(val.endsWith("\"")){ val.chop(1); }
|
||||
val.prepend(" "); //just to make additional parsing easier later - each "variable" should have whitespace on both sides
|
||||
if(var==("ifconfig_"+dev)){
|
||||
val.prepend(" "); val.append(" "); //just to make additional parsing easier later - each "variable" should have whitespace on both sides
|
||||
if( (var=="vlans_"+dev) || (var=="wlans_"+dev) ){
|
||||
set.asDevice = val;
|
||||
}else if(var==("ifconfig_"+dev)){
|
||||
QStringList vals = val.split(" ",QString::SkipEmptyParts);
|
||||
//HOSTAP set.wifihost = true;
|
||||
|
||||
//This is the main settings line: lots of things to look for:
|
||||
if(!val.contains("DHCP")){
|
||||
//Look for the static networking values
|
||||
set.useDHCP = false;
|
||||
if(val.contains(" netmask ")){ set.staticNetmask = val.section(" netmask ",1,1).section(" ",0,0); }
|
||||
if(val.contains(" gateway ")){ set.staticGateway = val.section(" gateway ",1,1).section(" ",0,0); }
|
||||
if(val.contains(" inet ")){ set.staticIPv4 = val.section(" inet ",1,1).section(" ",0,0); }
|
||||
if(val.contains(" inet6 ")){ set.staticIPv6 = val.section(" inet6 ",1,1).section(" ",0,0); }
|
||||
if(set.staticIPv4.isEmpty() || set.staticIPv6.isEmpty()){
|
||||
//IPv4/6 address can sometimes not have the "inet(6)" identifier - look through the first few values to as well
|
||||
/*if(set.staticIPv4.isEmpty() || set.staticIPv6.isEmpty()){
|
||||
//IPv4/6 address can sometimes not have the "inet(6)" identifier - look through the first few values as well
|
||||
QStringList vals = val.split(" ",QString::SkipEmptyParts);
|
||||
|
||||
for(int v=0; v<vals.length(); v++){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}else{ set.useDHCP=true; } //end of DHCP check
|
||||
//Wifi Checks
|
||||
if(vals.contains("WPA")){ set.wifisecurity=true; }
|
||||
|
||||
} //end variable checks
|
||||
} //end loop over rc.conf lines
|
||||
return set;
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
NetDevSettings Network::deviceIfconfigSettings(QString dev){
|
||||
/*NetDevSettings Network::deviceIfconfigSettings(QString dev){
|
||||
QString info = General::RunCommand("ifconfig "+dev);
|
||||
NetDevSettings set;
|
||||
if(info.isEmpty() || info.contains("interface "+dev+"does not exist")){ return set; } //empty stucture
|
||||
@@ -95,14 +99,14 @@ NetDevSettings Network::deviceIfconfigSettings(QString dev){
|
||||
if(info.contains(" ssid ")){ set.wifiSSID = info.section(" ssid ",1,1).section(" ",0,0); }
|
||||
if(info.contains(" bssid ")){ set.wifiBSSID = info.section(" bssid ",1,1).section(" ",0,0); }
|
||||
if(info.contains(" country ")){ set.wifiCountry = info.section(" country ",1,1).section(" ",0,0); }
|
||||
if(info.contains(" channel ")){ set.staticIPv4 = info.section(" channel ",1,1).section(" ",0,0); }
|
||||
if(info.contains(" channel ")){ set.wifiChannel = info.section(" channel ",1,1).section(" ",0,0); }
|
||||
return set;
|
||||
}
|
||||
}*/
|
||||
|
||||
//=====================
|
||||
// NETWORK-ROOT FUNCTIONS
|
||||
//=====================
|
||||
bool NetworkRoot::saveNetworkEntry(NetworkEntry data){
|
||||
/*bool NetworkRoot::saveNetworkEntry(NetworkEntry data){
|
||||
netent *entry = getnetbyname(data.name.toLocal8Bit());
|
||||
if(entry==0){
|
||||
//This entry does not exist yet - need to add it
|
||||
@@ -112,7 +116,7 @@ bool NetworkRoot::saveNetworkEntry(NetworkEntry data){
|
||||
endnetent(); //Make sure to close the file when finished
|
||||
return false; //not implemented yet
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
//--------------------------------------
|
||||
bool NetworkRoot::saveRCSettings(NetDevSettings){
|
||||
|
||||
@@ -21,16 +21,16 @@ struct NetworkEntry{
|
||||
unsigned int netnum;
|
||||
};
|
||||
|
||||
//Data structure for rc.conf settings
|
||||
struct NetDevSettings{
|
||||
QString device; //the device these settings correspond with (usually unset if there was an error)
|
||||
QString asDevice; //associated device (Example: wifi device -> wlan<num> device)
|
||||
//Wifi
|
||||
QString wifiCountry, wifiSSID, wifiBSSID, wifichannel;
|
||||
//Wifi settings
|
||||
QString wifiCountry, wifiSSID, wifiBSSID, wifiChannel;
|
||||
bool wifihost;
|
||||
//General
|
||||
QString etherMac;
|
||||
//NOTE: All the "static" variables are only used for rc.conf settings
|
||||
// For the current IP/IPv6 information use the "NetDevice" class below
|
||||
bool wifisecurity; //always recommended for wifi settings - otherwise can't connect to secure access points
|
||||
//Addressing
|
||||
bool useDHCP;
|
||||
QString staticIPv4, staticNetmask, staticIPv6; //assumes DHCP if none are set
|
||||
QString staticGateway;
|
||||
};
|
||||
@@ -72,22 +72,23 @@ public:
|
||||
//General data structure for wifi access points (local or available)
|
||||
struct NetWifi{
|
||||
NetCrypt encryption;
|
||||
QString BSSID, SSID;
|
||||
};
|
||||
|
||||
|
||||
//The general-purpose class that any user/app can utilitize
|
||||
class Network{
|
||||
public:
|
||||
static QList<NetworkEntry> listNetworkEntries();
|
||||
//static QList<NetworkEntry> listNetworkEntries();
|
||||
static QStringList readRcConf(); //use this when reading /etc/rc.conf for network stuff - prevents opening the file repeatedly
|
||||
static NetDevSettings deviceRCSettings(QString dev); //settings in rc.conf (bootup)
|
||||
static NetDevSettings deviceIfconfigSettings(QString dev); //settings currently running
|
||||
//static NetDevSettings deviceIfconfigSettings(QString dev); //settings currently running
|
||||
};
|
||||
|
||||
//The class that requires overarching root permissions (usually for changes to system)
|
||||
class NetworkRoot{
|
||||
public:
|
||||
static bool saveNetworkEntry(NetworkEntry); //**Not implemented yet**
|
||||
//static bool saveNetworkEntry(NetworkEntry); //**Not implemented yet**
|
||||
static bool saveRCSettings(NetDevSettings); //rc.conf settings (bootup)
|
||||
static bool setIfconfigSettings(NetDevSettings); //ifconfig settings (temporary session)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user