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) |
10 |
< |
{ |
11 |
< |
#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 |
< |
} |
30 |
< |
|
31 |
< |
Host::~Host() |
32 |
< |
{ |
33 |
< |
delete address; |
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 |
< |
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 |
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 |