// Host Update // // Douglas Thrift // // $Id$ #include "Host.hpp" Host::Host(const string& host, const string& name, const string& address) { #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 == ""); } 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) { cerr << "Here!\n"; struct hostent* ent = gethostbyaddr((char*)(this->address), sizeof(in_addr), AF_INET); name = ent->h_name; } } void Host::operator++() { ifstream fin(host.c_str()); getline(fin, name); fin.read((char*)(address), sizeof(in_addr)); fin.close(); } void Host::operator--() { ofstream fout(host.c_str()); fout << name << '\n'; fout.write((char*)(address), sizeof(in_addr)); fout.close(); } #ifdef _WIN32 unsigned Host::count = 0; WSADATA Host::data; #endif