// Host Update // // Douglas Thrift // // $Id$ #include "Host.hpp" Host::Host(const string& host, const string& name, const string& address, const string& platform) { memset(&this->address, 0, sizeof(in_addr)); #ifdef _WIN32 if (count == 0) { if (WSAStartup(MAKEWORD(2, 0), &data) != 0) { 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) { cerr << "~Host(): WSACleanup()\n"; exit(1); } } #endif } void Host::setName(const 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 string& address, bool lookup) { in_addr_t value = inet_addr(address.c_str()); if (value == INADDR_NONE) { 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 (string(inet_ntoa(address)) == inet_ntoa(host.address)) { return platform == host.platform; } else { return false; } } else { return false; } } else { return false; } } Host Host::operator++() { string file = string("hosts") + slash + host; ifstream fin(file.c_str()); if (fin.is_open()) { getline(fin, name); string address; getline(fin, address); in_addr_t value = inet_addr(address.c_str()); if (value == INADDR_NONE) { cerr << "Host.operator++(): INADDR_NONE\n"; } else { memcpy(&this->address, &value, sizeof(in_addr_t)); } getline(fin, platform); fin.close(); } return *this; } Host Host::operator++(int) { Host old = *this; ++*this; return old; } Host Host::operator--() { string file = string("hosts") + slash + host; 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