ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/Host.cpp
Revision: 10
Committed: 2003-11-06T20:35:29-08:00 (21 years, 7 months ago) by Douglas Thrift
File size: 1736 byte(s)
Log Message:
Die!

File Contents

# User Rev Content
1 Douglas Thrift 9 // Host Update
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Host.hpp"
8    
9 Douglas Thrift 10 Host::Host(const string& host, const string& name, const string& address)
10 Douglas Thrift 9 {
11 Douglas Thrift 10 #ifdef _WIN32
12     if (count == 0)
13     {
14     if (WSAStartup(MAKEWORD(2, 0), &data) != 0)
15     {
16     cerr << "Host(): WSAStartup()\n";
17     exit(1);
18     }
19     }
20    
21     count++;
22     #endif
23    
24     this->address = new struct in_addr;
25    
26     setHost(host);
27     if (name != "") setName(name, address == "");
28     if (address != "") setAddress(address, name == "");
29 Douglas Thrift 9 }
30    
31     Host::~Host()
32     {
33     delete address;
34 Douglas Thrift 10
35     #ifdef _WIN32
36     count--;
37    
38     if (count == 0)
39     {
40     if (WSACleanup() != 0)
41     {
42     cerr << "~Host(): WSACleanup()\n";
43     exit(1);
44     }
45     }
46     #endif
47 Douglas Thrift 9 }
48 Douglas Thrift 10
49     void Host::setName(const string& name, bool lookup)
50     {
51     this->name = name;
52    
53     if (lookup)
54     {
55     struct hostent* ent = gethostbyname(this->name.c_str());
56    
57     memcpy(address, *(ent->h_addr_list), ent->h_length);
58     }
59     }
60    
61     void Host::setAddress(const string& address, bool lookup)
62     {
63     in_addr_t value = inet_addr(address.c_str());
64    
65     if (value == INADDR_NONE)
66     {
67     cerr << "Host.setAddress(): INADDR_NONE\n";
68     exit(1);
69     }
70     else
71     {
72     memcpy(this->address, &value, sizeof(in_addr_t));
73     }
74    
75     if (lookup)
76     {
77     cerr << "Here!\n";
78     struct hostent* ent = gethostbyaddr((char*)(this->address),
79     sizeof(in_addr), AF_INET);
80    
81     name = ent->h_name;
82     }
83     }
84    
85     void Host::operator++()
86     {
87     ifstream fin(host.c_str());
88    
89     getline(fin, name);
90    
91     fin.read((char*)(address), sizeof(in_addr));
92     fin.close();
93     }
94    
95     void Host::operator--()
96     {
97     ofstream fout(host.c_str());
98    
99     fout << name << '\n';
100    
101     fout.write((char*)(address), sizeof(in_addr));
102     fout.close();
103     }
104    
105     #ifdef _WIN32
106    
107     unsigned Host::count = 0;
108     WSADATA Host::data;
109    
110     #endif