simple-socket library 1.9.0
Loading...
Searching...
No Matches
mini_ifconfig.cpp

Very basic network configuration using the libraries built-in functions.

Output without arguments lists all interfaces:

lo:
        mtu 65536
        inet 127.0.0.1 netmask 255.0.0.0 broadcast 0.0.0.0
        ether
enp12s0:
        mtu 1500
        inet 192.168.178.15 netmask 255.255.255.0 broadcast 192.168.178.255
        ether 30:c5:99:5b:ea:63
#include <iostream>
#include <sstream>
const char usageString[] = " [ <interface> <setting> <value> ]\n"
" \n"
" If called without parameters, lists interfaces and their current settings.\n"
" Changes said settings if called with parameters.\n"
" \n"
" Parameters:\n"
" interface name of the interface to configure\n"
" setting name of the setting to change\n"
" value new value for said setting\n"
" \n"
" Settings:\n"
" mtu maximum transmission unit\n"
" address IPv4 interface address\n"
" broadcast IPv4 broadcast address\n"
" netmask IPV4 netmask\n"
" destination IPv4 destination address\n";
void usage();
int print();
int configure(const std::string& interface, const std::string& setting, const std::string& val);
char* progName;
int main( int argc, char* argv[])
{
progName = argv[0];
if(argc == 1) return print();
if(argc == 4) {
return configure(argv[1], argv[2], argv[3]);
}
usage();
return -1;
}
void usage()
{
std::cout << progName << usageString;
}
int print()
{
// go through all settings and print them
for(const auto& i : ifs)
{
std::cout << i.name() << ":" << "\n";
std::cout << "\t" << "mtu " << i.getMTU() << "\n";
std::cout << "\t" << "inet " << i.getInterfaceAddress() << " ";
std::cout << "netmask " << i.getNetmask() << " ";
std::cout << "broadcast " << i.getBroadcastAddress() << "\n";
std::cout << "\t" << "ether " << i.getHardwareAddress() << std::endl;
}
return 0;
}
int configure(const std::string& interface, const std::string& setting, const std::string& val)
{
// check which setting we want to configure
// and set the value appropriately
try {
NET::NetworkInterface nwi(interface);
if(setting == "mtu") {
nwi.setMTU(std::stoi(val));
} else if(setting == "address") {
nwi.setInterfaceAddress(val);
} else if(setting == "broadcast") {
nwi.setBroadcastAddress(val);
} else if(setting == "netmask") {
nwi.setNetmask(val);
} else if(setting == "destination") {
nwi.setDestinationAddress(val);
} else {
usage();
return -1;
}
return 0;
}
// since any of the calls might throw, catch errors
// here and report them to the user
catch(const NET::SocketException& e)
{
std::cerr << e.what() << " (" << e.errorCode() << ")" << std::endl;
return -1;
}
}
Signals a problem with the execution of a socket call.
const char * what() const noexcept
Returns a C-string describing the cause of the current error.
int errorCode() const noexcept
Returns the glibc errno code of the current error.
std::vector< NetworkInterface > getNetworkInterfaces()
Returns list of all network interfaces regardless of their state.