/* * Copyright (C) 2017 Edupals project * * Author: * Enrique Medina Gremaldos * * Source: * https://github.com/edupals/edupals-base-toolkit * * This file is a part of edupals-base-toolkit. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * */ #include #include #include #include #include #include #include #include #include #include #include #include using namespace edupals::network; using namespace edupals::network::exception; using namespace std; namespace fs=std::experimental::filesystem; MAC::MAC(array address) : address(address) { } MAC::MAC(string address) { //TODO: check for errors! string hex=""; int n=5; for (char c:address) { if (c==':') { this->address[n]=stoi(hex,0,16); n--; hex=""; } else { hex+=c; } } this->address[n]=stoi(hex,0,16); } string MAC::to_string() { stringstream s; s<address[0]=address & 0x000000FF; this->address[1]=(address & 0x0000FF00)>>8; this->address[2]=(address & 0x00FF0000)>>16; this->address[3]=(address & 0xFF000000)>>24; } IP4::IP4(array address) : address(address) { } string IP4::to_string() { stringstream s; s<<(int)address[0]<<"."<<(int)address[1]<<"."<<(int)address[2]<<"."<<(int)address[3]; return s.str(); } uint8_t IP4::operator [] (int n) { return address[n]; } uint32_t IP4::get_uint32() { uint32_t tmp; tmp=address[0] | (address[1]<<8) | (address[2]<<16) | (address[3]<<24); return tmp; } Mask4::Mask4(uint32_t address) : IP4(address) { } Mask4::Mask4(array address) : IP4(address) { } int32_t Mask4::bits() { int32_t num=0; bool knee=false; for (int n=3;n>=0;n--) { for (int b=0;b<8;b++) { uint8_t m = 1<name=name; } string Interface::read_str(string prop) { if(!exists()) { throw InterfaceNotFound(name); } ifstream file; string tmp; fs::path sysfs = path; fs::path carrier = sysfs / prop; file.open(carrier); std::getline(file,tmp); file.close(); return tmp; } uint32_t Interface::read_u32(string prop) { string tmp = read_str(prop); return std::stoi(tmp); } bool Interface::carrier() { try { uint32_t value = read_u32("carrier"); return (value==1); } catch (std::exception e) { return false; } } uint32_t Interface::mtu() { return read_u32("mtu"); } uint32_t Interface::type() { return read_u32("type"); } MAC Interface::address() { string tmp = read_str("address"); return MAC(tmp); } IP4 Interface::ip4() { int fd; struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET; std::strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ-1); ioctl(fd, SIOCGIFADDR, &ifr); close(fd); struct sockaddr_in* sin = (struct sockaddr_in*)&(ifr.ifr_addr); return IP4(sin->sin_addr.s_addr); } //TODO: factorize Mask4 Interface::mask4() { int fd; struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET; std::strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ-1); ioctl(fd, SIOCGIFNETMASK, &ifr); close(fd); struct sockaddr_in* sin = (struct sockaddr_in*)&(ifr.ifr_netmask); return Mask4(sin->sin_addr.s_addr); } IP4 Interface::broadcast4() { int fd; struct ifreq ifr; fd = socket(AF_INET, SOCK_DGRAM, 0); ifr.ifr_addr.sa_family = AF_INET; std::strncpy(ifr.ifr_name, name.c_str(), IFNAMSIZ-1); ioctl(fd, SIOCGIFBRDADDR, &ifr); close(fd); struct sockaddr_in* sin = (struct sockaddr_in*)&(ifr.ifr_broadaddr); return IP4(sin->sin_addr.s_addr); } bool Interface::exists() { fs::path sysfs = path; return fs::exists(sysfs); } vector Interface::list() { vector ifaces; fs::path sysfs("/sys/class/net"); for (auto& dev: fs::directory_iterator(sysfs)) { ifaces.push_back(Interface(dev.path().filename())); } return ifaces; }