1 |
// Host Update |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
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 |
{ |
16 |
HostUpdate update; |
17 |
|
18 |
return 0; |
19 |
} |
20 |
|
21 |
HostUpdate::HostUpdate() |
22 |
{ |
23 |
struct stat* hosts = new struct stat; |
24 |
|
25 |
if (stat("hosts", hosts) != 0) |
26 |
{ |
27 |
mkdir("hosts"); |
28 |
} |
29 |
|
30 |
delete [] hosts; |
31 |
|
32 |
string agent = sgetenv("HTTP_USER_AGENT"); |
33 |
string method = sgetenv("REQUEST_METHOD"); |
34 |
|
35 |
parse(method); |
36 |
|
37 |
if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") |
38 |
!= string::npos && agent.find(") libwww-perl/") != string::npos) |
39 |
{ |
40 |
update(agent); |
41 |
} |
42 |
else |
43 |
{ |
44 |
display(); |
45 |
} |
46 |
} |
47 |
|
48 |
void HostUpdate::parse(const string& method) |
49 |
{ |
50 |
string query; |
51 |
|
52 |
if (method == "POST") |
53 |
{ |
54 |
getline(cin, query); |
55 |
} |
56 |
else |
57 |
{ |
58 |
query = sgetenv("QUERY_STRING"); |
59 |
} |
60 |
|
61 |
if (query == "") return; |
62 |
|
63 |
istringstream input(query); |
64 |
|
65 |
do |
66 |
{ |
67 |
string name, value; |
68 |
|
69 |
getline(input, name, '='); |
70 |
getline(input, value, '&'); |
71 |
|
72 |
cgi.insert(pair<string, string>(name, value)); |
73 |
} |
74 |
while (input.good()); |
75 |
} |
76 |
|
77 |
void HostUpdate::update(const string& agent) |
78 |
{ |
79 |
cout << "Content-Type: text/plain\n\n"; |
80 |
|
81 |
multimap<string, string>::iterator itor = cgi.find("host"); |
82 |
|
83 |
if (itor == cgi.end()) return; |
84 |
if (itor->second == "") return; |
85 |
|
86 |
string host = itor->second, name = sgetenv("REMOTE_HOST"), address = |
87 |
sgetenv("REMOTE_ADDR"); |
88 |
|
89 |
string::size_type begin = agent.find('(') + 1, end = agent.find(')', |
90 |
begin); |
91 |
string platform = agent.substr(begin, end - begin); |
92 |
|
93 |
Host client(host, name, address, platform), saved(host); |
94 |
|
95 |
cout << "host=" << client.getHost() << '\n' |
96 |
<< "name=" << client.getName() << '\n' |
97 |
<< "address=" << client.getAddress() << '\n' |
98 |
<< "platform=" << client.getPlatform() << '\n'; |
99 |
|
100 |
if (client != ++saved) client--; |
101 |
} |
102 |
|
103 |
void HostUpdate::display() |
104 |
{ |
105 |
cout << "Content-Type: text/plain\n\n"; |
106 |
|
107 |
set<Host> hosts; |
108 |
|
109 |
#ifndef _WIN32 |
110 |
DIR* dir = opendir("hosts"); |
111 |
struct dirent* ent; |
112 |
|
113 |
while ((ent = readdir(dir)) != NULL) |
114 |
{ |
115 |
string file = ent->d_name; |
116 |
|
117 |
if (file == "." || file == "..") continue; |
118 |
|
119 |
Host host(file); |
120 |
|
121 |
hosts.insert(++host); |
122 |
} |
123 |
|
124 |
closedir(dir); |
125 |
#else |
126 |
WIN32_FIND_DATA found; |
127 |
HANDLE find = FindFirstFile("hosts\\*", &found); |
128 |
|
129 |
do |
130 |
{ |
131 |
string file = found.cFileName; |
132 |
|
133 |
if (file == "." || file == "..") continue; |
134 |
|
135 |
Host host(file); |
136 |
|
137 |
hosts.insert(++host); |
138 |
} |
139 |
while (FindNextFile(find, &found) != 0); |
140 |
|
141 |
FindClose(find); |
142 |
#endif |
143 |
|
144 |
for (set<Host>::iterator itor = hosts.begin(); itor != hosts.end(); itor++) |
145 |
{ |
146 |
display(*itor); |
147 |
} |
148 |
} |
149 |
|
150 |
void HostUpdate::display(const Host& host) |
151 |
{ |
152 |
cout << "host=" << host.getHost() << '\n' |
153 |
<< "name=" << host.getName() << '\n' |
154 |
<< "address=" << host.getAddress() << '\n' |
155 |
<< "platform=" << host.getPlatform() << '\n'; |
156 |
|
157 |
string name = string("hosts") + slash + host.getHost(); |
158 |
struct stat file; |
159 |
|
160 |
if (stat(name.c_str(), &file) == 0) |
161 |
{ |
162 |
char since[20]; |
163 |
|
164 |
strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime)); |
165 |
|
166 |
cout << "since=" << since << " GMT\n"; |
167 |
} |
168 |
} |