// Host Update // // Douglas Thrift // // $Id$ #include "Host.hpp" Host::Host(const string& host, const string& name, const string& address, const string& platform) { #ifdef _WIN32 if (count == 0) { if (WSAStartup(MAKEWORD(2, 0), &data) != 0) { cerr << "Host(): WSAStartup()\n"; exit(1); } } count++; #endif this->address = new struct in_addr; setHost(host); if (name != "") setName(name, address == ""); if (address != "") setAddress(address, name == ""); setPlatform(platform); } Host::~Host() { delete address; #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"; exit(1); } 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 (address == host.address) { return platform == host.platform; } else { return false; } } else { return false; } } else { return false; } } Host Host::operator++() { ifstream fin(host.c_str()); if (fin.is_open()) { getline(fin, name); fin.read((char*)(address), sizeof(in_addr)); getline(fin, platform); fin.close(); } return *this; } Host Host::operator++(int) { Host old = *this; ++*this; return old; } Host Host::operator--() { ofstream fout(host.c_str()); fout << name << '\n'; fout.write((char*)(address), sizeof(in_addr)); fout << platform; fout.close(); return *this; } Host Host::operator--(int) { Host old = *this; --*this; return old; } #ifdef _WIN32 unsigned Host::count = 0; WSADATA Host::data; #endif