6 |
|
|
7 |
|
#include "HostUpdate.hpp" |
8 |
|
#include "Host.hpp" |
9 |
+ |
|
10 |
+ |
#ifdef _WIN32 |
11 |
|
#include <windows.h> |
12 |
+ |
#endif |
13 |
|
|
14 |
|
int main(int argc, char* argv[]) |
15 |
|
{ |
33 |
|
string method = sgetenv("REQUEST_METHOD"); |
34 |
|
|
35 |
|
parse(method); |
36 |
+ |
mode(); |
37 |
|
|
38 |
|
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
39 |
|
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
40 |
|
{ |
41 |
|
update(agent); |
42 |
|
} |
43 |
+ |
else if (agent.find("Host Update Sharp/") == 0 && method == "POST" && |
44 |
+ |
agent.find(" (") != string::npos && agent.find(')') != string::npos) |
45 |
+ |
{ |
46 |
+ |
update(agent); |
47 |
+ |
} |
48 |
|
else |
49 |
|
{ |
50 |
|
display(); |
80 |
|
while (input.good()); |
81 |
|
} |
82 |
|
|
83 |
+ |
void HostUpdate::mode() |
84 |
+ |
{ |
85 |
+ |
host = false, name = false, address = false, platform = false, since = |
86 |
+ |
false; |
87 |
+ |
|
88 |
+ |
for (multimap<string, string>::iterator itor = cgi.find("mode"); itor != |
89 |
+ |
cgi.upper_bound("mode") && itor != cgi.end(); itor++) |
90 |
+ |
{ |
91 |
+ |
string mode = itor->second; |
92 |
+ |
|
93 |
+ |
for (string::iterator itor = mode.begin(); itor != mode.end(); itor++) |
94 |
+ |
{ |
95 |
+ |
char mode = *itor; |
96 |
+ |
|
97 |
+ |
switch (mode) |
98 |
+ |
{ |
99 |
+ |
case 'h': |
100 |
+ |
if (!host) host = true; |
101 |
+ |
break; |
102 |
+ |
case 'n': |
103 |
+ |
if (!name) name = true; |
104 |
+ |
break; |
105 |
+ |
case 'a': |
106 |
+ |
if (!address) address = true; |
107 |
+ |
break; |
108 |
+ |
case 'p': |
109 |
+ |
if (!platform) platform = true; |
110 |
+ |
break; |
111 |
+ |
case 's': |
112 |
+ |
if (!since) since = true; |
113 |
+ |
break; |
114 |
+ |
default: |
115 |
+ |
break; |
116 |
+ |
} |
117 |
+ |
} |
118 |
+ |
} |
119 |
+ |
|
120 |
+ |
if (!host && !name && !address && !platform && !since) host = true, name = |
121 |
+ |
true, address = true, platform = true, since = true; |
122 |
+ |
} |
123 |
+ |
|
124 |
|
void HostUpdate::update(const string& agent) |
125 |
|
{ |
126 |
|
cout << "Content-Type: text/plain\n\n"; |
133 |
|
string host = itor->second, name = sgetenv("REMOTE_HOST"), address = |
134 |
|
sgetenv("REMOTE_ADDR"); |
135 |
|
|
136 |
< |
string::size_type begin = agent.find('(') + 1, end = agent.find(')', |
137 |
< |
begin); |
136 |
> |
string::size_type begin = agent.find('(') + 1, end = agent.rfind(')'); |
137 |
> |
|
138 |
> |
if (begin >= end) return; |
139 |
> |
|
140 |
|
string platform = agent.substr(begin, end - begin); |
141 |
|
|
142 |
|
Host client(host, name, address, platform), saved(host); |
143 |
|
|
92 |
– |
cout << "host=" << client.getHost() << '\n' |
93 |
– |
<< "name=" << client.getName() << '\n' |
94 |
– |
<< "address=" << client.getAddress() << '\n' |
95 |
– |
<< "platform=" << client.getPlatform() << '\n'; |
96 |
– |
|
144 |
|
if (client != ++saved) client--; |
145 |
+ |
|
146 |
+ |
display(client); |
147 |
|
} |
148 |
|
|
149 |
|
void HostUpdate::display() |
150 |
|
{ |
151 |
|
cout << "Content-Type: text/plain\n\n"; |
152 |
|
|
153 |
+ |
{ |
154 |
+ |
bool request = false; |
155 |
+ |
|
156 |
+ |
for (multimap<string, string>::iterator itor = cgi.find("host"); itor != |
157 |
+ |
cgi.upper_bound("host") && itor != cgi.end(); itor++) |
158 |
+ |
{ |
159 |
+ |
if (itor->second != "") |
160 |
+ |
{ |
161 |
+ |
Host host(itor->second); |
162 |
+ |
|
163 |
+ |
display(++host); |
164 |
+ |
|
165 |
+ |
if (!request) request = true; |
166 |
+ |
} |
167 |
+ |
} |
168 |
+ |
|
169 |
+ |
if (request) return; |
170 |
+ |
} |
171 |
+ |
|
172 |
|
set<Host> hosts; |
173 |
|
|
174 |
|
#ifndef _WIN32 |
214 |
|
|
215 |
|
void HostUpdate::display(const Host& host) |
216 |
|
{ |
217 |
< |
cout << "host=" << host.getHost() << '\n' |
218 |
< |
<< "name=" << host.getName() << '\n' |
219 |
< |
<< "address=" << host.getAddress() << '\n' |
220 |
< |
<< "platform=" << host.getPlatform() << '\n'; |
217 |
> |
if (this->host) cout << "host=" << host.getHost() << '\n'; |
218 |
> |
if (name) cout << "name=" << host.getName() << '\n'; |
219 |
> |
if (address) cout << "address=" << host.getAddress() << '\n'; |
220 |
> |
if (platform) cout << "platform=" << host.getPlatform() << '\n'; |
221 |
|
|
222 |
|
string name = string("hosts") + slash + host.getHost(); |
223 |
|
struct stat file; |
224 |
|
|
225 |
< |
if (stat(name.c_str(), &file) == 0) |
225 |
> |
if (since && stat(name.c_str(), &file) == 0) |
226 |
|
{ |
227 |
|
char since[20]; |
228 |
|
|
230 |
|
|
231 |
|
cout << "since=" << since << " GMT\n"; |
232 |
|
} |
233 |
+ |
else if (since) |
234 |
+ |
{ |
235 |
+ |
char since[20]; |
236 |
+ |
time_t* now = new time_t; |
237 |
+ |
|
238 |
+ |
time(now); |
239 |
+ |
strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(now)); |
240 |
+ |
|
241 |
+ |
delete now; |
242 |
+ |
|
243 |
+ |
cout << "since=" << since << " GMT\n"; |
244 |
+ |
} |
245 |
|
} |