// Host Update // // Douglas Thrift // // $Id$ #include "Host.hpp" Host::Host(const std::string& host, const std::string& name, const std::string& address, const std::string& platform) { ::memset(&this->address, 0, sizeof(in_addr)); #ifdef _WIN32 if (count == 0) if (::WSAStartup(MAKEWORD(2, 0), &data) != 0) { std::cerr << "Host(): WSAStartup()\n"; ::exit(1); } count++; #endif setHost(host); if (name != "") setName(name, address == ""); if (address != "") setAddress(address, name == ""); setPlatform(platform); } Host::~Host() { #ifdef _WIN32 count--; if (count == 0) if (::WSACleanup() != 0) { std::cerr << "~Host(): WSACleanup()\n"; ::exit(1); } #endif } void Host::setName(const std::string& name, bool lookup) { this->name = name; if (lookup) { struct ::hostent* ent(::gethostbyname(this->name.c_str())); ::memcpy(&address, *(ent->h_addr_list), ent->h_length); } } void Host::setAddress(const std::string& address, bool lookup) { ::in_addr_t value = ::inet_addr(address.c_str()); if (value == INADDR_NONE) std::cerr << "Host.setAddress(): INADDR_NONE\n"; else ::memcpy(&this->address, &value, sizeof(::in_addr_t)); if (lookup) { struct ::hostent* ent(::gethostbyaddr((char*)(&this->address), sizeof(in_addr), AF_INET)); if (ent != NULL) name = ent->h_name; } } bool Host::operator==(const Host& host) const { if (this->host == host.host) if (name == host.name) if (std::string(::inet_ntoa(address)) == ::inet_ntoa(host.address)) return platform == host.platform; else return false; else return false; else return false; } Host Host::operator++() { std::string file(std::string("hosts") + slash + host); std::ifstream fin(file.c_str()); if (fin.is_open()) { std::getline(fin, name); std::string address; std::getline(fin, address); ::in_addr_t value(::inet_addr(address.c_str())); if (value == INADDR_NONE) std::cerr << "Host.operator++(): INADDR_NONE\n"; else ::memcpy(&this->address, &value, sizeof(in_addr_t)); std::getline(fin, platform); fin.close(); } return *this; } Host Host::operator++(int) { Host old = *this; ++*this; return old; } Host Host::operator--() { std::string file = std::string("hosts") + slash + host; std::ofstream fout(file.c_str()); fout << name << '\n' << ::inet_ntoa(address) << '\n' << platform << '\n'; fout.close(); return *this; } Host Host::operator--(int) { Host old = *this; --*this; return old; } #ifdef _WIN32 unsigned Host::count(0); WSADATA Host::data; #endif