ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/HostUpdate.cpp
(Generate patch)

Comparing HostUpdate/HostUpdate.cpp (file contents):
Revision 192 by Douglas Thrift, 2004-08-22T21:23:29-07:00 vs.
Revision 900 by douglas, 2007-04-29T02:26:40-07:00

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines