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