ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
Revision: 192
Committed: 2004-08-22T21:23:29-07:00 (20 years, 10 months ago) by Douglas Thrift
File size: 4738 byte(s)
Log Message:
Meep!

File Contents

# User Rev Content
1 Douglas Thrift 3 // Host Update
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "HostUpdate.hpp"
8 Douglas Thrift 9 #include "Host.hpp"
9 Douglas Thrift 21
10 Douglas Thrift 192 #ifndef _WIN32
11    
12     extern "C"
13     {
14     #include <resolv.h>
15     }
16    
17     #else
18    
19 Douglas Thrift 17 #include <windows.h>
20 Douglas Thrift 192
21 Douglas Thrift 21 #endif
22 Douglas Thrift 3
23     int main(int argc, char* argv[])
24     {
25 Douglas Thrift 192 #ifndef _WIN32
26     res_init();
27    
28     for (short index(0); index < MAXNS; ++index)
29     {
30     if (index + 1 < MAXNS)
31     {
32     memcpy(&_res.nsaddr_list[index].sin_addr,
33     &_res.nsaddr_list[index + 1].sin_addr, sizeof(in_addr_t));
34     }
35     else
36     {
37     memset(&_res.nsaddr_list[index].sin_addr, 0, sizeof(in_addr_t));
38     }
39     }
40     #endif
41    
42 Douglas Thrift 3 HostUpdate update;
43    
44     return 0;
45     }
46    
47 Douglas Thrift 192 HostUpdate::HostUpdate() : host(false), name(false), address(false),
48     platform(false), since(false)
49 Douglas Thrift 3 {
50 Douglas Thrift 12 struct stat* hosts = new struct stat;
51 Douglas Thrift 3
52 Douglas Thrift 12 if (stat("hosts", hosts) != 0)
53     {
54     mkdir("hosts");
55     }
56 Douglas Thrift 3
57 Douglas Thrift 12 delete [] hosts;
58 Douglas Thrift 10
59 Douglas Thrift 15 string agent = sgetenv("HTTP_USER_AGENT");
60     string method = sgetenv("REQUEST_METHOD");
61 Douglas Thrift 10
62 Douglas Thrift 15 parse(method);
63 Douglas Thrift 22 mode();
64 Douglas Thrift 10
65 Douglas Thrift 13 if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (")
66 Douglas Thrift 12 != string::npos && agent.find(") libwww-perl/") != string::npos)
67     {
68 Douglas Thrift 15 update(agent);
69 Douglas Thrift 12 }
70 Douglas Thrift 91 else if (agent.find("Host Update Sharp/") == 0 && method == "POST" &&
71     agent.find(" (") != string::npos && agent.find(')') != string::npos)
72     {
73     update(agent);
74     }
75 Douglas Thrift 12 else
76     {
77 Douglas Thrift 15 display();
78 Douglas Thrift 12 }
79 Douglas Thrift 3 }
80    
81 Douglas Thrift 15 void HostUpdate::parse(const string& method)
82 Douglas Thrift 12 {
83 Douglas Thrift 15 string query;
84 Douglas Thrift 12
85 Douglas Thrift 15 if (method == "POST")
86     {
87     getline(cin, query);
88     }
89     else
90     {
91     query = sgetenv("QUERY_STRING");
92     }
93 Douglas Thrift 12
94 Douglas Thrift 15 if (query == "") return;
95 Douglas Thrift 12
96 Douglas Thrift 15 istringstream input(query);
97 Douglas Thrift 12
98 Douglas Thrift 15 do
99     {
100     string name, value;
101 Douglas Thrift 12
102 Douglas Thrift 15 getline(input, name, '=');
103     getline(input, value, '&');
104    
105     cgi.insert(pair<string, string>(name, value));
106     }
107     while (input.good());
108     }
109    
110 Douglas Thrift 22 void HostUpdate::mode()
111     {
112 Douglas Thrift 192 for (multimap<string, string>::iterator itor(cgi.find("mode"));
113     itor != cgi.upper_bound("mode") && itor != cgi.end(); itor++)
114 Douglas Thrift 22 {
115 Douglas Thrift 192 string mode(itor->second);
116 Douglas Thrift 22
117 Douglas Thrift 192 for (string::iterator itor(mode.begin()); itor != mode.end(); itor++)
118 Douglas Thrift 22 {
119 Douglas Thrift 192 char mode(*itor);
120 Douglas Thrift 22
121     switch (mode)
122     {
123     case 'h':
124     if (!host) host = true;
125     break;
126     case 'n':
127     if (!name) name = true;
128     break;
129     case 'a':
130     if (!address) address = true;
131     break;
132     case 'p':
133     if (!platform) platform = true;
134     break;
135     case 's':
136     if (!since) since = true;
137     break;
138     default:
139     break;
140     }
141     }
142     }
143    
144     if (!host && !name && !address && !platform && !since) host = true, name =
145     true, address = true, platform = true, since = true;
146     }
147    
148 Douglas Thrift 15 void HostUpdate::update(const string& agent)
149     {
150     cout << "Content-Type: text/plain\n\n";
151    
152     multimap<string, string>::iterator itor = cgi.find("host");
153 Douglas Thrift 17
154 Douglas Thrift 15 if (itor == cgi.end()) return;
155     if (itor->second == "") return;
156    
157 Douglas Thrift 192 string host(itor->second), name(sgetenv("REMOTE_HOST")),
158     address(sgetenv("REMOTE_ADDR"));
159 Douglas Thrift 15
160 Douglas Thrift 192 string::size_type begin(agent.find('(') + 1), end(agent.rfind(')'));
161 Douglas Thrift 23
162     if (begin >= end) return;
163    
164 Douglas Thrift 192 string platform(agent.substr(begin, end - begin));
165 Douglas Thrift 13
166     Host client(host, name, address, platform), saved(host);
167    
168 Douglas Thrift 22 if (client != ++saved) client--;
169 Douglas Thrift 15
170 Douglas Thrift 22 display(client);
171 Douglas Thrift 12 }
172    
173 Douglas Thrift 15 void HostUpdate::display()
174 Douglas Thrift 12 {
175 Douglas Thrift 14 cout << "Content-Type: text/plain\n\n";
176    
177 Douglas Thrift 22 {
178 Douglas Thrift 192 bool request(false);
179 Douglas Thrift 22
180 Douglas Thrift 192 for (multimap<string, string>::iterator itor(cgi.find("host"));
181     itor != cgi.upper_bound("host") && itor != cgi.end(); itor++)
182 Douglas Thrift 22 {
183 Douglas Thrift 192 if (!itor->second.empty())
184 Douglas Thrift 22 {
185     Host host(itor->second);
186    
187     display(++host);
188    
189     if (!request) request = true;
190     }
191     }
192    
193     if (request) return;
194     }
195    
196 Douglas Thrift 14 set<Host> hosts;
197 Douglas Thrift 17
198     #ifndef _WIN32
199 Douglas Thrift 192 DIR* dir(opendir("hosts"));
200 Douglas Thrift 14 struct dirent* ent;
201    
202     while ((ent = readdir(dir)) != NULL)
203     {
204 Douglas Thrift 192 string file(ent->d_name);
205 Douglas Thrift 14
206     if (file == "." || file == "..") continue;
207 Douglas Thrift 17
208 Douglas Thrift 14 Host host(file);
209    
210     hosts.insert(++host);
211     }
212 Douglas Thrift 15
213 Douglas Thrift 14 closedir(dir);
214 Douglas Thrift 17 #else
215     WIN32_FIND_DATA found;
216 Douglas Thrift 192 HANDLE find(FindFirstFile("hosts\\*", &found));
217 Douglas Thrift 14
218 Douglas Thrift 17 do
219     {
220 Douglas Thrift 192 string file(found.cFileName);
221 Douglas Thrift 17
222     if (file == "." || file == "..") continue;
223    
224     Host host(file);
225    
226     hosts.insert(++host);
227     }
228     while (FindNextFile(find, &found) != 0);
229    
230     FindClose(find);
231     #endif
232    
233 Douglas Thrift 192 for (set<Host>::iterator itor(hosts.begin()); itor != hosts.end(); itor++)
234 Douglas Thrift 14 {
235 Douglas Thrift 20 display(*itor);
236     }
237     }
238 Douglas Thrift 14
239 Douglas Thrift 20 void HostUpdate::display(const Host& host)
240     {
241 Douglas Thrift 22 if (this->host) cout << "host=" << host.getHost() << '\n';
242     if (name) cout << "name=" << host.getName() << '\n';
243     if (address) cout << "address=" << host.getAddress() << '\n';
244     if (platform) cout << "platform=" << host.getPlatform() << '\n';
245 Douglas Thrift 19
246 Douglas Thrift 192 string name(string("hosts") + slash + host.getHost());
247 Douglas Thrift 20 struct stat file;
248 Douglas Thrift 19
249 Douglas Thrift 22 if (since && stat(name.c_str(), &file) == 0)
250 Douglas Thrift 20 {
251     char since[20];
252 Douglas Thrift 19
253 Douglas Thrift 20 strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&file.st_mtime));
254 Douglas Thrift 19
255 Douglas Thrift 20 cout << "since=" << since << " GMT\n";
256 Douglas Thrift 14 }
257 Douglas Thrift 22 else if (since)
258     {
259     char since[20];
260 Douglas Thrift 192 time_t now(time(NULL));
261 Douglas Thrift 22
262 Douglas Thrift 192 strftime(since, 20, "%m/%d/%Y %H:%M:%S", gmtime(&now));
263 Douglas Thrift 22
264     cout << "since=" << since << " GMT\n";
265     }
266 Douglas Thrift 12 }

Properties

Name Value
svn:eol-style native
svn:keywords Id