Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

dns.cpp

Go to the documentation of this file.
00001 /*
00002  *  $Id: dns_8cpp-source.html,v 1.1.1.3 2004/06/19 04:36:23 chrisb Exp $
00003  *
00004  *  Copyright (C) 1996 - 2003 Stephen F. Booth
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00029 #include <cstdlib>
00030 #include <new>
00031 #include <vector>
00032 #include <stdexcept>
00033 #include <iostream>
00034 
00035 #include "cgicc/CgiDefs.h"
00036 #include "cgicc/Cgicc.h"
00037 #include "cgicc/HTTPHTMLHeader.h"
00038 #include "cgicc/HTMLClasses.h"
00039 
00040 #if HAVE_UNAME
00041 #  include <sys/utsname.h>
00042 #endif
00043 
00044 #if HAVE_SYS_TIME_H
00045 #  include <sys/time.h>
00046 #endif
00047 
00048 #ifdef WIN32
00049 #  include <winsock2.h>
00050 #else
00051 #  include <sys/types.h>
00052 #  include <sys/socket.h>
00053 #  include <netinet/in.h>
00054 #  include <arpa/inet.h>
00055 #  include <netdb.h>
00056 #endif /* WIN32 */
00057 
00058 // To use logging, the variable gLogFile MUST be defined, and it _must_
00059 // be an ofstream
00060 #if DEBUG
00061   std::ofstream gLogFile( "/change_this_path/cgicc.log", std::ios::app );
00062 #endif
00063 
00064 using namespace std;
00065 using namespace cgicc;
00066 
00067 // DNS gateway cgi
00068 int
00069 main(int /*argc*/, 
00070      char ** /*argv*/)
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     // Set up the page; I will put in lfs to ease reading of the
00085     // produced HTML. These are optional, and except in <PRE>
00086     // tags have no effect on HTML appearance.
00087     cout << head() << endl;
00088 
00089     // Output the style sheet portion of the header
00090     cout << style() << comment() << endl;
00091     cout << "body { color: black; background-color: white; }" << endl;
00092     cout << "hr.half { width: 60%; align: center; }" << endl;
00093     cout << "span.red, strong.red { color: red; }" << endl;
00094     cout << "div.smaller { font-size: small; }" << endl;
00095     cout << "div.dns { border: solid thin; margin: 1em 0; "
00096          << "background: #ddd; text-align: center; }" << endl;
00097     cout << "span.blue { color: blue; }" << endl;
00098     cout << "col.title { color: white; background-color: black; ";
00099     cout << "font-weight: bold; text-align: center; }" << endl;
00100     cout << "col.data { background-color: #ddd; text-align: left; }" << endl;
00101     cout << comment() << style() << endl;
00102 
00103     cout << title("DNS Gateway") << endl;
00104     cout << head() << endl;
00105     
00106     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00107          << " DNS Gateway" << h1() << endl;
00108   
00109     form_iterator ip = cgi.getElement("ip");
00110     form_iterator name = cgi.getElement("hostname");
00111 
00112     if(ip != (*cgi).end()) {
00113       cout << h3() << "Query results for " << **ip << h3() << endl;
00114       
00115       u_long addr;
00116       struct hostent *hp;
00117       char **p;
00118       
00119       if((int)(addr = inet_addr((**ip).c_str())) == -1) {
00120         cout << cgicc::div().set("class", "dns") << endl
00121              << strong(span("ERROR").set("class","red"))
00122              << " - IP address must be of the form x.x.x.x"
00123              << endl << cgicc::div() << endl;
00124       }
00125       else {
00126         hp = gethostbyaddr((char*)&addr, sizeof (addr), AF_INET);
00127         if(hp == NULL) {
00128           cout << cgicc::div().set("class", "dns") << endl
00129                << strong(span("ERROR").set("class","red")) 
00130                << " - Host information for " << em((**ip)) << " not found."
00131                << endl << cgicc::div() << endl;
00132         }
00133         else {
00134           for(p = hp->h_addr_list; *p != 0; p++) {
00135             struct in_addr in;
00136             //char **q;
00137             
00138             (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00139             
00140             cout << cgicc::div().set("class", "dns") << endl
00141                  << span(inet_ntoa(in)).set("class","blue") 
00142                  << " - " << ' ' << hp->h_name;
00143             //for(q = hp->h_aliases; *q != 0; q++)
00144             //      cout << *q << ' ';
00145             cout << endl << cgicc::div() << endl;
00146           }
00147         }
00148       }
00149     }
00150     
00151 
00152     if(name != (*cgi).end()) {
00153       cout << h3() << "Query results for " << **name << h3() << endl;
00154       
00155       struct hostent *hp;
00156       char **p;
00157       
00158       hp = gethostbyname((**name).c_str());
00159       if(hp == NULL) {
00160         cout << cgicc::div().set("class", "dns") << endl
00161              << strong(span("ERROR").set("class","red"))
00162              << " - Host information for " << em(**name) << " not found."
00163              << endl << cgicc::div() << endl;
00164       }
00165       else {
00166         for(p = hp->h_addr_list; *p != 0; p++) {
00167           struct in_addr in;
00168           //char **q;
00169           
00170           (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00171           
00172           cout << cgicc::div().set("class", "dns") << endl
00173                << inet_ntoa(in) << " - " << ' ' 
00174                << span(hp->h_name).set("class","blue");
00175           //    for(q = hp->h_aliases; *q != 0; q++)
00176           //      cout << *q << ' ';
00177           cout << endl << cgicc::div() << endl;
00178         }
00179       }
00180     }
00181     
00182     cout << p("Please enter an IP address or a hostname.") << endl;
00183     
00184     cout << table().set("border","0")
00185                    .set("rules","none")
00186                    .set("frame","void")
00187                    .set("cellspacing","2").set("cellpadding","2") << endl;
00188     cout << colgroup().set("span","2") << endl;
00189     cout << col().set("align","center")
00190                  .set("class","title")
00191                  .set("span","1") << endl;
00192     cout << col().set("align","left")
00193                  .set("class","data")
00194                  .set("span","1") << endl;
00195     cout << colgroup() << endl;
00196     
00197     cout << "<form method=\"post\" action=\"http://"
00198          << cgi.getEnvironment().getServerName();
00199     if(cgi.getEnvironment().getServerPort() != 80)
00200       cout << ":" << cgi.getEnvironment().getServerPort();
00201     cout << cgi.getEnvironment().getScriptName() << "\">" << endl;
00202     
00203     cout << tr() << endl;
00204     cout << td(strong("IP Address: ")) << endl;
00205     cout << td() << "<input type=\"text\" name=\"ip\"";
00206     if(ip != (*cgi).end())
00207       cout << " value=\"" << **ip << "\">";
00208     else
00209       cout << ">";
00210     cout << td() << tr() << "</form>" << endl;
00211     
00212     cout << "<form method=\"post\" action=\"http://"
00213          << cgi.getEnvironment().getServerName();
00214     if(cgi.getEnvironment().getServerPort() != 80)
00215       cout << ":" << cgi.getEnvironment().getServerPort();
00216     cout << cgi.getEnvironment().getScriptName() << "\">" << endl;
00217     
00218     cout << tr() << endl;
00219     cout << td(strong("Hostname: ")) << endl;
00220     cout << td() << "<input type=\"text\" name=\"hostname\"";
00221     if(name != (*cgi).end())
00222       cout << " value=\"" << **name << "\">";
00223     else
00224       cout << ">";
00225     cout << td() << tr() << endl;
00226     cout << "</form>" << table() << p() << endl;
00227     
00228     // Now print cout a footer with some fun info
00229     cout << hr(set("class","half")) << endl;
00230     cout << cgicc::div().set("align","center").set("class","smaller") << endl;
00231     cout << "GNU cgi" << span("cc").set("class","red") << " v"
00232          << cgi.getVersion() << br() << endl;
00233     cout << "Compiled at " << cgi.getCompileTime() 
00234          << " on " << cgi.getCompileDate() << br() << endl;
00235     
00236     cout << "Configured for " << cgi.getHost();  
00237     // I don't know if everyone has uname...
00238 #if HAVE_UNAME
00239     struct utsname info;
00240     if(uname(&info) != -1) {
00241       cout << ". Running on " << info.sysname;
00242       cout << ' ' << info.release << " (";
00243       cout << info.nodename << ')' << endl;
00244     }
00245 #else
00246     cout << '.' << endl;
00247 #endif
00248     
00249 #if HAVE_GETTIMEOFDAY
00250     // Information on this query
00251     timeval end;
00252     gettimeofday(&end, NULL);
00253     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00254       + (end.tv_usec - start.tv_usec);
00255 
00256     cout << br() << "Total time for request = " << us << " us";
00257     cout << " (" << (double) (us/1000000.0) << " s)";
00258 #endif
00259     
00260     // End of document
00261     cout << cgicc::div() << endl;
00262     cout << body() << html() << endl;
00263 
00264     return EXIT_SUCCESS;
00265   }
00266 
00267   catch(const std::exception& e) {
00268     return EXIT_FAILURE;
00269   }
00270 }

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996 - 2004 Stephen F. Booth
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front Cover Texts, and with no Back-Cover Texts.
Documentation generated Sat Jun 19 00:36:23 2004 for cgicc by doxygen 1.3.6