[API CHANGE] Add a new action for sysadm/network class:

"action" = "list-settings"
List all the settings for network devices that are saved in /etc/rc.conf
-----------------

REST Request (example):
-------------------------------
PUT /sysadm/network
{
   "action" : "list-settings"
}

WebSocket Request:
-------------------------------
{
   "id" : "fooid",
   "namespace" : "sysadm",
   "args" : {
      "action" : "list-settings"
   },
   "name" : "network"
}

Response:
-------------------------------
{
  "args": {
    "lo0": {
      "associated_device": "",
      "device": "lo0",
      "static_gateway": "",
      "static_ipv4": "",
      "static_ipv6": "",
      "static_netmask": "",
      "use_dhcp": "false"
    },
    "re0": {
      "associated_device": "",
      "device": "re0",
      "static_gateway": "",
      "static_ipv4": "",
      "static_ipv6": "",
      "static_netmask": "",
      "use_dhcp": "true"
    },
    "ue0": {
      "associated_device": "",
      "device": "ue0",
      "static_gateway": "",
      "static_ipv4": "",
      "static_ipv6": "",
      "static_netmask": "",
      "use_dhcp": "true"
    }
  },
  "id": "fooid",
  "name": "response",
  "namespace": "sysadm"
}
This commit is contained in:
Ken Moore
2017-08-10 13:51:35 -04:00
parent 4dded17898
commit 376eaa4e37
4 changed files with 68 additions and 25 deletions

View File

@@ -409,9 +409,9 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmNetworkRequest(const QJsonVa
bool ok = false;
if(keys.contains("action")){
QString act = JsonValueToString(in_args.toObject().value("action"));
QStringList devs = sysadm::NetDevice::listNetDevices();
if(act=="list-devices"){
ok = true;
QStringList devs = sysadm::NetDevice::listNetDevices();
for(int i=0; i<devs.length(); i++){
sysadm::NetDevice D(devs[i]);
QJsonObject obj;
@@ -428,6 +428,30 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmNetworkRequest(const QJsonVa
//Add this device info to the main output structure
out->insert(devs[i], obj);
}
}else if(act=="list-settings"){
ok = true;
for(int i=0; i<devs.length(); i++){
sysadm::NetDevSettings D = sysadm::Network::deviceRCSettings(devs[i]);
QJsonObject obj;
//assemble the information about this device into an output object
obj.insert("device", D.device);
obj.insert("associated_device", D.asDevice);
obj.insert("use_dhcp", D.useDHCP ? "true" : "false" );
obj.insert("static_ipv4", D.staticIPv4);
obj.insert("static_ipv6", D.staticIPv6);
obj.insert("static_netmask", D.staticNetmask);
obj.insert("static_gateway", D.staticGateway);
if(D.wifihost){
obj.insert("wifi_country", D.wifiCountry);
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");
}
//Add this device info to the main output structure
out->insert(devs[i], obj);
}
}
} //end of "action" key usage

View File

@@ -4,6 +4,11 @@
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
// SysAdm source code for TrueOS
// Copyright (c) 2016-2017 TrueOS/iXsystems
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
// Note: This was almost entirely written by Tim McCormick in 2009 for
// the first PC-BSD library, and copied here by Ken Moore in 2015
//===========================================
@@ -15,7 +20,7 @@
using namespace sysadm;
//====================
// STATIC LISTING FUNCTION
// STATIC LISTING FUNCTION
//====================
QStringList NetDevice::listNetDevices(){
QStringList result;
@@ -28,7 +33,7 @@ QStringList NetDevice::listNetDevices(){
if (result.contains(ifName) == 0) result += ifName;
ifap = ifap->ifa_next;
}
//Close the
//Close the structure
freeifaddrs(ifap);
return result;
}
@@ -57,10 +62,10 @@ QString NetDevice::ipAsString(){
strncpy(ifr.ifr_name, name.toLocal8Bit(), IFNAMSIZ);
int s = socket(PF_INET, SOCK_DGRAM, 0);
ioctl(s, SIOCGIFADDR, &ifr);
struct in_addr in = ((sockaddr_in *) &ifr.ifr_addr)->sin_addr;
close(s); //close the file descriptor
return QString(inet_ntoa(in));
}
@@ -86,7 +91,7 @@ QString NetDevice::ipv6AsString(){
//Now get the IPv6 address in string form
char straddr[INET6_ADDRSTRLEN];
int err = getnameinfo(sadd, sadd->sa_len, straddr, sizeof(straddr),NULL, 0, NI_NUMERICHOST);
if(err!=0){
if(err!=0){
qDebug() << "getnameinfo error:" << gai_strerror(err);
return "";
}else{
@@ -102,16 +107,28 @@ QString NetDevice::netmaskAsString(){
strncpy(ifr.ifr_name, name.toLocal8Bit(), IFNAMSIZ);
int s = socket(PF_INET, SOCK_DGRAM, 0);
ioctl(s, SIOCGIFNETMASK, &ifr);
struct in_addr in = ((sockaddr_in *) &ifr.ifr_addr)->sin_addr;
close(s); //close the file descriptor
return QString(inet_ntoa(in));
}
//Returns the description string for the device
QString NetDevice::desc(){
return General::sysctl("dev." + devName() + "." + QString::number(devNum()) + ".%desc");
QString name, num, parent;
if( isWireless() ){ parent = getWifiParent(); }
if(!parent.isEmpty()){
name = num = parent;
uint pos = name.indexOf(QRegExp("[0-9]+$"));
name.truncate(pos);
num.remove(0,pos);
}else{
name = devName();
num = QString::number(devNum());
}
return General::sysctl("dev." + name + "." + num + ".%desc");
}
//Fetch the mac address as a QString
@@ -136,7 +153,7 @@ QString NetDevice::macAsString(){
sdl = (sockaddr_dl *)(((if_msghdr *)buf)+1);
ptr = (char *) LLADDR(sdl);
QString mac;
for (uint i=0; i < 6; i++){
mac += QString::number(*(ptr+i), 16).right(2).rightJustified(2, '0');
@@ -170,6 +187,7 @@ QString NetDevice::mediaStatusAsString(){
if (ifm.ifm_status & IFM_ACTIVE) status = "active";
else status = "no carrier";
}
close(s); //close the file descriptor
return status;
}
@@ -190,8 +208,9 @@ bool NetDevice::isWireless(){
int s = socket(AF_INET, SOCK_DGRAM, 0);
ioctl(s, SIOCGIFMEDIA, &ifm);
return IFM_TYPE(ifm.ifm_active) == IFM_IEEE80211;
bool iswifi = (IFM_TYPE(ifm.ifm_active) == IFM_IEEE80211);
close(s); //close the file descriptor
return iswifi;
}
//Get the parent device (if this is a wireless wlan)
@@ -203,7 +222,7 @@ QString NetDevice::getWifiParent(){
//See if the device is setup to use DHCP
bool NetDevice::usesDHCP(){
//The system does not keep track of how the device's address was assigned
// so the closest we can get to this is to see if the system is setup to use
// so the closest we can get to this is to see if the system is setup to use
// DHCP on startup (in /etc/rc.conf) (Ken Moore - 6/24/15)
return !Network::readRcConf().filter(name).filter("DHCP").isEmpty();
}
@@ -217,8 +236,9 @@ bool NetDevice::isUp(){
int s = socket(AF_INET, SOCK_DGRAM, 0);
ioctl(s, SIOCGIFFLAGS, &ifr);
return (ifr.ifr_flags & IFF_UP) ? 1 : 0;
bool isup = (ifr.ifr_flags & IFF_UP);
close(s); //close the file descriptor
return isup;
}
//Determine the number of packets received by the device

View File

@@ -19,7 +19,7 @@ using namespace sysadm;
//Copy over this data into the output structure
NetworkEntry tmp;
tmp.name = QString::fromLocal8Bit(entry->n_name);
for(int i=0; entry->n_aliases[i] != 0; i++){
for(int i=0; entry->n_aliases[i] != 0; i++){
tmp.aliases << QString::fromLocal8Bit(entry->n_aliases[i]);
}
tmp.netnum = entry->n_net;
@@ -73,13 +73,13 @@ NetDevSettings Network::deviceRCSettings(QString dev){
//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;
@@ -126,4 +126,4 @@ bool NetworkRoot::saveRCSettings(NetDevSettings){
//--------------------------------------
bool NetworkRoot::setIfconfigSettings(NetDevSettings){
return false;
}
}

View File

@@ -38,7 +38,7 @@ struct NetDevSettings{
//General data class for network devices
// Note: Sources in NetDevice.cpp
class NetDevice{
private:
private:
QString name;
public:
NetDevice(QString devName){ name = devName; }
@@ -61,7 +61,7 @@ public:
long packetsTx();
long errorsRx();
long errorsTx();
//Setting Functions (to make changes - requires root)
void setUp(bool up); //Turn device on/off (temporary - not saved globally)
@@ -75,7 +75,6 @@ struct NetWifi{
QString BSSID, SSID;
};
//The general-purpose class that any user/app can utilitize
class Network{
public:
@@ -88,11 +87,11 @@ public:
//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)
};
} //end of pcbsd namespace
#endif
#endif