Commit a434453d authored by Dominik Charousset's avatar Dominik Charousset

cleanup and coding style

parent 3ce5f985
...@@ -115,49 +115,43 @@ bool operator!=(const columns_iterator& lhs, const columns_iterator& rhs) { ...@@ -115,49 +115,43 @@ bool operator!=(const columns_iterator& lhs, const columns_iterator& rhs) {
namespace cppa { namespace util { namespace cppa { namespace util {
std::string get_root_uuid() { std::string get_root_uuid() {
char buf[1024] = {0}; int sck = socket(AF_INET, SOCK_DGRAM, 0);
struct ifconf ifc = {0}; if (sck < 0) {
struct ifreq *ifr = NULL;
int sck = 0;
int nInterfaces = 0;
// Get a socket handle.
sck = socket(AF_INET, SOCK_DGRAM, 0);
if(sck < 0) {
perror("socket"); perror("socket");
return ""; return "";
} }
// query available interfaces
// Query available interfaces. char buf[1024];
ifconf ifc;
ifc.ifc_len = sizeof(buf); ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf; ifc.ifc_buf = buf;
if(ioctl(sck, SIOCGIFCONF, &ifc) < 0) { if (ioctl(sck, SIOCGIFCONF, &ifc) < 0) {
perror("ioctl(SIOCGIFCONF)"); perror("ioctl(SIOCGIFCONF)");
return ""; return "";
} }
vector<string> hw_addresses; vector<string> hw_addresses;
auto ctoi = [](char c) -> unsigned { auto ctoi = [](char c) -> unsigned {
return static_cast<unsigned char>(c); return static_cast<unsigned char>(c);
}; };
// Iterate through the list of interfaces. // iterate through interfaces.
ifr = ifc.ifc_req; auto ifr = ifc.ifc_req;
nInterfaces = ifc.ifc_len / sizeof(struct ifreq); size_t num_interfaces = ifc.ifc_len / sizeof(ifreq);
for(int i = 0; i < nInterfaces; i++) { for (size_t i = 0; i < num_interfaces; i++) {
struct ifreq *item = &ifr[i]; auto& item = ifr[i];
// Get the MAC address // get MAC address
if(ioctl(sck, SIOCGIFHWADDR, item) < 0) { if (ioctl(sck, SIOCGIFHWADDR, &item) < 0) {
perror("ioctl(SIOCGIFHWADDR)"); perror("ioctl(SIOCGIFHWADDR)");
return ""; return "";
} }
// convert MAC address to standard string representation
std::ostringstream oss; std::ostringstream oss;
oss << hex; oss << hex;
oss.width(2); oss.width(2);
oss << ctoi(item->ifr_hwaddr.sa_data[0]); oss << ctoi(item.ifr_hwaddr.sa_data[0]);
for (size_t i = 1; i < 6; ++i) { for (size_t i = 1; i < 6; ++i) {
oss << ":"; oss << ":";
oss.width(2); oss.width(2);
oss << ctoi(item->ifr_hwaddr.sa_data[i]); oss << ctoi(item.ifr_hwaddr.sa_data[i]);
} }
auto addr = oss.str(); auto addr = oss.str();
if (addr != "00:00:00:00:00:00") { if (addr != "00:00:00:00:00:00") {
...@@ -188,3 +182,4 @@ std::string get_root_uuid() { ...@@ -188,3 +182,4 @@ std::string get_root_uuid() {
} } // namespace cppa::util } } // namespace cppa::util
#endif // CPPA_LINUX #endif // CPPA_LINUX
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment