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