Commit some more work on an /etc/rc.conf writing routine for networking.

Not enabled for API calls yet.
This commit is contained in:
Ken Moore
2017-08-10 14:53:30 -04:00
parent 376eaa4e37
commit 4e23691d50
2 changed files with 49 additions and 5 deletions

View File

@@ -433,6 +433,7 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmNetworkRequest(const QJsonVa
ok = true;
for(int i=0; i<devs.length(); i++){
sysadm::NetDevSettings D = sysadm::Network::deviceRCSettings(devs[i]);
if(D.device.isEmpty()){ continue; } //nothing for this device
QJsonObject obj;
//assemble the information about this device into an output object
obj.insert("device", D.device);
@@ -447,7 +448,7 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmNetworkRequest(const QJsonVa
obj.insert("wifi_ssid", D.wifiSSID);
obj.insert("wifi_bssid", D.wifiBSSID);
obj.insert("wifi_channel", D.wifiChannel);
obj.insert("wifi_secure_only", D.wifisecurity ? "true" : "false");
obj.insert("wifi_use_wpa", D.wifisecurity ? "true" : "false");
}
//Add this device info to the main output structure
out->insert(devs[i], obj);

View File

@@ -57,8 +57,9 @@ NetDevSettings Network::deviceRCSettings(QString dev){
if(val.startsWith("\"")){ val = val.remove(1); }
if(val.endsWith("\"")){ val.chop(1); }
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;
if( val.simplified()==dev && (var.startsWith("vlans_") || var.startsWith("wlans_") )){
set.asDevice = var.section("_",1,-1);
if(var.startsWith("wlans_")){ set.wifihost = true; }
}else if(var==("ifconfig_"+dev)){
QStringList vals = val.split(" ",QString::SkipEmptyParts);
//This is the main settings line: lots of things to look for:
@@ -119,8 +120,50 @@ NetDevSettings Network::deviceRCSettings(QString dev){
}*/
//--------------------------------------
bool NetworkRoot::saveRCSettings(NetDevSettings){
return false;
bool NetworkRoot::saveRCSettings(NetDevSettings set){
if(!QFile::exists("/dev/"+set.device)){ return false; } //invalid device
//Create the lines which need to be put info /etc/rc.conf based on the settings
QStringList tags, lines, found;
//Related device lines
if(!set.asDevice.isEmpty()){
if(set.wifihost){ tags << "wlans_"+set.asDevice; lines << "wlans_"+set.asDevice+"=\""+set.device+"\""; }
else{ tags << "vlans_"+set.asDevice; lines << "vlans_"+set.asDevice+"=\""+set.device+"\""; }
}
//Main device line
QString tmp = "ifconfig_"+set.device+"=\"";
if(set.useDHCP){ tmp.append("DHCP"); }
else{
if(!set.staticIPv4.isEmpty()){ tmp.append("inet "+set.staticIPv4+" "); }
if(!set.staticIPv6.isEmpty()){ tmp.append("inet6 "+set.staticIPv6+" "); }
if(!set.staticNetmask.isEmpty()){ tmp.append("netmask "+set.staticNetmask+" "); }
if(!set.staticGateway.isEmpty()){ tmp.append("gateway "+set.staticGateway+" "); }
tmp = tmp.simplified(); //remove any excess whitespace on end
}
QString var = "ifconfig_"+set.device;
tags << var;
if(!tmp.isEmpty()){ lines << var+"=\""+tmp+"\""; }
else{ lines << ""; } //delete the line
//Now read rc.conf and adjust contents as needed
QStringList info = Network::readRcConf();
for(int i=0; i<info.length(); i++){
if(info[i].simplified().isEmpty()){ continue; }
QString var = info[i].section("=",0,0).simplified();
if( tags.contains(var) ){
int index = tags.indexOf(var);
info[i] = lines[index];
lines.removeAt(index);
found << tags.takeAt(index);
}else if(found.contains(var)){
//Duplicate line - remove this since it was already handled
info.removeAt(i); i--;
}
}
//Now add any lines which were not already found to the end of the file
for(int i=0; i<lines.length(); i++){
info << lines[i];
}
return General::writeTextFile("/etc/rc.conf", info, true);
}
//--------------------------------------