Skip to content
Snippets Groups Projects
Commit 0fdf9a99 authored by Diego Giovane Pasqualin's avatar Diego Giovane Pasqualin
Browse files

Merge branch 'issue/15' into 'development'

Issue #15: Check all possible names for the network interface

Check all possible names for the network interface

See merge request !29
parents ce01ec4d b0b02150
No related branches found
No related tags found
2 merge requests!51Merge development to master,!29Issue #15: Check all possible names for the network interface
Pipeline #
......@@ -25,17 +25,42 @@ std::string get_macaddr() {
std::fstream file;
std::string macaddr;
file.open("/sys/class/net/eth0/address", std::ifstream::in);
/* #FIXME The way we are breaking the getline to get the network
* interface is not very efficient, because we create a char
* array and a string to the same thing.
*/
if (!file.is_open()) {
file.open("/sys/class/net/eth1/address", std::ifstream::in);
if (!file.is_open()) {
throw std::string("No address file found.");
char line[40];
char iface[20];
char dest[20];
std::string Iface;
std::string Destination;
std::string file_path;
FILE *fp = fopen("/proc/net/route", "r");
fgets (line, sizeof(line), fp); // Skip the first line (column headers).
/* Loop to found the default network interface*/
while (fgets(line, sizeof(line), fp)) {
/* Found the interface name and check that it is default*/
sscanf(line, "%s %s\n", iface, dest);
Iface = iface;
Destination = dest;
if (Destination == "00000000") { // found default network interface
/* found macaddr with the default network interface*/
file_path = "/sys/class/net/"+Iface+"/address";
file.open(file_path, std::ifstream::in);
if (!file.is_open()) {
throw std::string("No address file found.");
}
getline(file, macaddr); // get macaddr
file.close();
}
}
getline(file, macaddr);
file.close();
fclose(fp);
return macaddr;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment