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