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