1 |
// Host Update |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Host.hpp" |
8 |
|
9 |
Host::Host(const string& host, const string& name, const string& address, const |
10 |
string& platform) |
11 |
{ |
12 |
memset(&this->address, 0, sizeof(in_addr)); |
13 |
|
14 |
#ifdef _WIN32 |
15 |
if (count == 0) |
16 |
{ |
17 |
if (WSAStartup(MAKEWORD(2, 0), &data) != 0) |
18 |
{ |
19 |
cerr << "Host(): WSAStartup()\n"; |
20 |
exit(1); |
21 |
} |
22 |
} |
23 |
|
24 |
count++; |
25 |
#endif |
26 |
|
27 |
setHost(host); |
28 |
if (name != "") setName(name, address == ""); |
29 |
if (address != "") setAddress(address, name == ""); |
30 |
setPlatform(platform); |
31 |
} |
32 |
|
33 |
Host::~Host() |
34 |
{ |
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 |
} |
48 |
|
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 |
} |
69 |
else |
70 |
{ |
71 |
memcpy(&this->address, &value, sizeof(in_addr_t)); |
72 |
} |
73 |
|
74 |
if (lookup) |
75 |
{ |
76 |
struct hostent* ent = gethostbyaddr((char*)(&this->address), |
77 |
sizeof(in_addr), AF_INET); |
78 |
|
79 |
if (ent != NULL) name = ent->h_name; |
80 |
} |
81 |
} |
82 |
|
83 |
bool Host::operator==(const Host& host) const |
84 |
{ |
85 |
if (this->host == host.host) |
86 |
{ |
87 |
if (name == host.name) |
88 |
{ |
89 |
if (string(inet_ntoa(address)) == inet_ntoa(host.address)) |
90 |
{ |
91 |
return platform == host.platform; |
92 |
} |
93 |
else |
94 |
{ |
95 |
return false; |
96 |
} |
97 |
} |
98 |
else |
99 |
{ |
100 |
return false; |
101 |
} |
102 |
} |
103 |
else |
104 |
{ |
105 |
return false; |
106 |
} |
107 |
} |
108 |
|
109 |
Host Host::operator++() |
110 |
{ |
111 |
string file = string("hosts") + slash + host; |
112 |
|
113 |
ifstream fin(file.c_str()); |
114 |
|
115 |
if (fin.is_open()) |
116 |
{ |
117 |
getline(fin, name); |
118 |
|
119 |
string address; |
120 |
|
121 |
getline(fin, address); |
122 |
|
123 |
in_addr_t value = inet_addr(address.c_str()); |
124 |
|
125 |
if (value == INADDR_NONE) |
126 |
{ |
127 |
cerr << "Host.operator++(): INADDR_NONE\n"; |
128 |
} |
129 |
else |
130 |
{ |
131 |
memcpy(&this->address, &value, sizeof(in_addr_t)); |
132 |
} |
133 |
|
134 |
getline(fin, platform); |
135 |
fin.close(); |
136 |
} |
137 |
|
138 |
return *this; |
139 |
} |
140 |
|
141 |
Host Host::operator++(int) |
142 |
{ |
143 |
Host old = *this; |
144 |
|
145 |
++*this; |
146 |
|
147 |
return old; |
148 |
} |
149 |
|
150 |
Host Host::operator--() |
151 |
{ |
152 |
string file = string("hosts") + slash + host; |
153 |
|
154 |
ofstream fout(file.c_str()); |
155 |
|
156 |
fout << name << '\n' |
157 |
<< inet_ntoa(address) << '\n' |
158 |
<< platform << '\n'; |
159 |
|
160 |
fout.close(); |
161 |
|
162 |
return *this; |
163 |
} |
164 |
|
165 |
Host Host::operator--(int) |
166 |
{ |
167 |
Host old = *this; |
168 |
|
169 |
--*this; |
170 |
|
171 |
return old; |
172 |
} |
173 |
|
174 |
#ifdef _WIN32 |
175 |
|
176 |
unsigned Host::count = 0; |
177 |
WSADATA Host::data; |
178 |
|
179 |
#endif |