Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00032 #include <cstdlib>
00033 #include <new>
00034 #include <vector>
00035 #include <stdexcept>
00036 #include <iostream>
00037 #include <stdio.h>
00038 #include <memory.h>
00039 #include "cgicc/CgiDefs.h"
00040 #include "cgicc/Cgicc.h"
00041 #include "cgicc/HTTPHTMLHeader.h"
00042 #include "cgicc/HTMLClasses.h"
00043
00044 #if HAVE_SYS_UTSNAME_H
00045 # include <sys/utsname.h>
00046 #endif
00047
00048 #if HAVE_SYS_TIME_H
00049 # include <sys/time.h>
00050 #endif
00051
00052 #ifdef WIN32
00053 # include <winsock2.h>
00054 #else
00055 # include <sys/types.h>
00056 # include <sys/socket.h>
00057 # include <netinet/in.h>
00058 # include <arpa/inet.h>
00059 # include <netdb.h>
00060 #endif
00061
00062 #include "styles.h"
00063
00064 using namespace std;
00065 using namespace cgicc;
00066
00067
00068 int
00069 main(int ,
00070 char ** )
00071 {
00072
00073 try {
00074 #if HAVE_GETTIMEOFDAY
00075 timeval start;
00076 gettimeofday(&start, NULL);
00077 #endif
00078
00079 Cgicc cgi;
00080
00081 cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00082 cout << html().set("lang","en").set("dir","ltr") << endl;
00083
00084
00085
00086
00087 cout << head() << endl;
00088
00089
00090 cout << style() << comment() << endl;
00091 cout << styles;
00092 cout << comment() << style() << endl;
00093
00094 cout << title("DNS Gateway") << endl;
00095 cout << head() << endl;
00096
00097 cout << h1() << "GNU cgi" << span("cc").set("class","red")
00098 << " DNS Gateway" << h1() << endl;
00099
00100 form_iterator ip = cgi.getElement("ip");
00101 form_iterator name = cgi.getElement("hostname");
00102
00103 if(ip != (*cgi).end()) {
00104 cout << h3() << "Query results for " << **ip << h3() << endl;
00105
00106 u_long addr;
00107 struct hostent *hp;
00108 char **p;
00109
00110 if((int)(addr = inet_addr((**ip).c_str())) == -1) {
00111 cout << cgicc::div().set("class", "notice") << endl
00112 << strong(span("ERROR").set("class","red"))
00113 << " - IP address must be of the form x.x.x.x"
00114 << endl << cgicc::div() << endl;
00115 }
00116 else {
00117 hp = gethostbyaddr((char*)&addr, sizeof (addr), AF_INET);
00118 if(hp == NULL) {
00119 cout << cgicc::div().set("class", "notice") << endl
00120 << strong(span("ERROR").set("class","red"))
00121 << " - Host information for " << em((**ip)) << " not found."
00122 << endl << cgicc::div() << endl;
00123 }
00124 else {
00125 for(p = hp->h_addr_list; *p != 0; p++) {
00126 struct in_addr in;
00127
00128
00129 (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00130
00131 cout << cgicc::div().set("class", "notice") << endl
00132 << span(inet_ntoa(in)).set("class","blue")
00133 << " - " << ' ' << hp->h_name;
00134
00135
00136 cout << endl << cgicc::div() << endl;
00137 }
00138 }
00139 }
00140 }
00141
00142
00143 if(name != (*cgi).end()) {
00144 cout << h3() << "Query results for " << **name << h3() << endl;
00145
00146 struct hostent *hp;
00147 char **p;
00148
00149 hp = gethostbyname((**name).c_str());
00150 if(hp == NULL) {
00151 cout << cgicc::div().set("class", "notice") << endl
00152 << strong(span("ERROR").set("class","red"))
00153 << " - Host information for " << em(**name) << " not found."
00154 << endl << cgicc::div() << endl;
00155 }
00156 else {
00157 for(p = hp->h_addr_list; *p != 0; p++) {
00158 struct in_addr in;
00159
00160
00161 (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00162
00163 cout << cgicc::div().set("class", "notice") << endl
00164 << inet_ntoa(in) << " - " << ' '
00165 << span(hp->h_name).set("class","blue");
00166
00167
00168 cout << endl << cgicc::div() << endl;
00169 }
00170 }
00171 }
00172
00173 cout << p("Please enter an IP address or a hostname.") << endl;
00174
00175 cout << table() << endl;
00176
00177 cout << "<form method=\"post\" action=\""
00178 << cgi.getEnvironment().getScriptName() << "\">" << endl;
00179
00180 cout << tr() << endl;
00181 cout << td(strong("IP Address: ")).set("class", "title") << endl;
00182 cout << td().set("class", "data") << "<input type=\"text\" name=\"ip\"";
00183 if(ip != (*cgi).end())
00184 cout << " value=\"" << **ip << "\">";
00185 else
00186 cout << ">";
00187 cout << td() << tr() << "</form>" << endl;
00188
00189 cout << "<form method=\"post\" action=\""
00190 << cgi.getEnvironment().getScriptName() << "\">" << endl;
00191
00192 cout << tr() << endl;
00193 cout << td(strong("Hostname: ")).set("class", "title") << endl;
00194 cout << td().set("class", "data")
00195 << "<input type=\"text\" name=\"hostname\"";
00196 if(name != (*cgi).end())
00197 cout << " value=\"" << **name << "\">";
00198 else
00199 cout << ">";
00200 cout << td() << tr() << endl;
00201 cout << "</form>" << table() << p() << endl;
00202
00203
00204 cout << hr(set("class","half")) << endl;
00205 cout << cgicc::div().set("align","center").set("class","smaller") << endl;
00206 cout << "GNU cgi" << span("cc").set("class","red") << " v"
00207 << cgi.getVersion() << br() << endl;
00208 cout << "Compiled at " << cgi.getCompileTime()
00209 << " on " << cgi.getCompileDate() << br() << endl;
00210
00211 cout << "Configured for " << cgi.getHost();
00212
00213 #if HAVE_UNAME
00214 struct utsname info;
00215 if(uname(&info) != -1) {
00216 cout << ". Running on " << info.sysname;
00217 cout << ' ' << info.release << " (";
00218 cout << info.nodename << ')' << endl;
00219 }
00220 #else
00221 cout << '.' << endl;
00222 #endif
00223
00224 #if HAVE_GETTIMEOFDAY
00225
00226 timeval end;
00227 gettimeofday(&end, NULL);
00228 long us = ((end.tv_sec - start.tv_sec) * 1000000)
00229 + (end.tv_usec - start.tv_usec);
00230
00231 cout << br() << "Total time for request = " << us << " us";
00232 cout << " (" << static_cast<double>(us/1000000.0) << " s)";
00233 #endif
00234
00235
00236 cout << cgicc::div() << endl;
00237 cout << body() << html() << endl;
00238
00239 return EXIT_SUCCESS;
00240 }
00241
00242 catch(const std::exception& e) {
00243 return EXIT_FAILURE;
00244 }
00245 }