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

dns.cpp

Go to the documentation of this file.
00001 /*
00002  *  $Id: dns.cpp,v 1.17 2002/03/03 17:41:44 sbooth Exp $
00003  *
00004  *  Copyright (C) 1996 - 2002 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   STDNS ofstream gLogFile( "/change_this_path/cgicc.log", STDNS ios::app );
00062 #endif
00063 
00064 #if CGICC_USE_NAMESPACES
00065   using namespace std;
00066   using namespace cgicc;
00067 #else
00068 #  define div div_
00069 #  define link link_
00070 #  define select select_
00071 #endif
00072 
00073 // DNS gateway cgi
00074 int
00075 main(int /*argc*/, 
00076      char ** /*argv*/)
00077 {
00078 
00079   try {
00080 #if HAVE_GETTIMEOFDAY
00081     timeval start;
00082     gettimeofday(&start, NULL);
00083 #endif
00084 
00085     Cgicc cgi;
00086     
00087     cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
00088     cout << html().set("lang","en").set("dir","ltr") << endl;
00089     
00090     // Set up the page; I will put in lfs to ease reading of the
00091     // produced HTML. These are optional, and except in <PRE>
00092     // tags have no effect on HTML appearance.
00093     cout << head() << endl;
00094 
00095     // Output the style sheet portion of the header
00096     cout << style() << comment() << endl;
00097     cout << "body { color: black; background-color: white; }" << endl;
00098     cout << "hr.half { width: 60%; align: center; }" << endl;
00099     cout << "span.red, strong.red { color: red; }" << endl;
00100     cout << "div.smaller { font-size: small; }" << endl;
00101     cout << "div.dns { border: solid thin; margin: 1em 0; "
00102          << "background: #ddd; text-align: center; }" << endl;
00103     cout << "span.blue { color: blue; }" << endl;
00104     cout << "col.title { color: white; background-color: black; ";
00105     cout << "font-weight: bold; text-align: center; }" << endl;
00106     cout << "col.data { background-color: #ddd; text-align: left; }" << endl;
00107     cout << comment() << style() << endl;
00108 
00109     cout << title("DNS Gateway") << endl;
00110     cout << head() << endl;
00111     
00112     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00113          << " DNS Gateway" << h1() << endl;
00114   
00115     form_iterator ip = cgi.getElement("ip");
00116     form_iterator name = cgi.getElement("hostname");
00117 
00118     if(ip != (*cgi).end()) {
00119       cout << h3() << "Query results for " << **ip << h3() << endl;
00120       
00121       u_long addr;
00122       struct hostent *hp;
00123       char **p;
00124       
00125       if((int)(addr = inet_addr((**ip).c_str())) == -1) {
00126         cout << CGICCNS div().set("class", "dns") << endl
00127              << strong(span("ERROR").set("class","red"))
00128              << " - IP address must be of the form x.x.x.x"
00129              << endl << CGICCNS div() << endl;
00130       }
00131       else {
00132         hp = gethostbyaddr((char*)&addr, sizeof (addr), AF_INET);
00133         if(hp == NULL) {
00134           cout << CGICCNS div().set("class", "dns") << endl
00135                << strong(span("ERROR").set("class","red")) 
00136                << " - Host information for " << em((**ip)) << " not found."
00137                << endl << CGICCNS div() << endl;
00138         }
00139         else {
00140           for(p = hp->h_addr_list; *p != 0; p++) {
00141             struct in_addr in;
00142             //char **q;
00143             
00144             (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00145             
00146             cout << CGICCNS div().set("class", "dns") << endl
00147                  << span(inet_ntoa(in)).set("class","blue") 
00148                  << " - " << ' ' << hp->h_name;
00149             //for(q = hp->h_aliases; *q != 0; q++)
00150             //      cout << *q << ' ';
00151             cout << endl << CGICCNS div() << endl;
00152           }
00153         }
00154       }
00155     }
00156     
00157 
00158     if(name != (*cgi).end()) {
00159       cout << h3() << "Query results for " << **name << h3() << endl;
00160       
00161       struct hostent *hp;
00162       char **p;
00163       
00164       hp = gethostbyname((**name).c_str());
00165       if(hp == NULL) {
00166         cout << CGICCNS div().set("class", "dns") << endl
00167              << strong(span("ERROR").set("class","red"))
00168              << " - Host information for " << em(**name) << " not found."
00169              << endl << CGICCNS div() << endl;
00170       }
00171       else {
00172         for(p = hp->h_addr_list; *p != 0; p++) {
00173           struct in_addr in;
00174           //char **q;
00175           
00176           (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00177           
00178           cout << CGICCNS div().set("class", "dns") << endl
00179                << inet_ntoa(in) << " - " << ' ' 
00180                << span(hp->h_name).set("class","blue");
00181           //    for(q = hp->h_aliases; *q != 0; q++)
00182           //      cout << *q << ' ';
00183           cout << endl << CGICCNS div() << endl;
00184         }
00185       }
00186     }
00187     
00188     cout << p("Please enter an IP address or a hostname.") << endl;
00189     
00190     cout << table().set("border","0")
00191                    .set("rules","none")
00192                    .set("frame","void")
00193                    .set("cellspacing","2").set("cellpadding","2") << endl;
00194     cout << colgroup().set("span","2") << endl;
00195     cout << col().set("align","center")
00196                  .set("class","title")
00197                  .set("span","1") << endl;
00198     cout << col().set("align","left")
00199                  .set("class","data")
00200                  .set("span","1") << endl;
00201     cout << colgroup() << endl;
00202     
00203     cout << "<form method=\"post\" action=\"http://"
00204          << cgi.getEnvironment().getServerName();
00205     if(cgi.getEnvironment().getServerPort() != 80)
00206       cout << ":" << cgi.getEnvironment().getServerPort();
00207     cout << cgi.getEnvironment().getScriptName() << "\">" << endl;
00208     
00209     cout << tr() << endl;
00210     cout << td(strong("IP Address: ")) << endl;
00211     cout << td() << "<input type=\"text\" name=\"ip\"";
00212     if(ip != (*cgi).end())
00213       cout << " value=\"" << **ip << "\">";
00214     else
00215       cout << ">";
00216     cout << td() << tr() << "</form>" << endl;
00217     
00218     cout << "<form method=\"post\" action=\"http://"
00219          << cgi.getEnvironment().getServerName();
00220     if(cgi.getEnvironment().getServerPort() != 80)
00221       cout << ":" << cgi.getEnvironment().getServerPort();
00222     cout << cgi.getEnvironment().getScriptName() << "\">" << endl;
00223     
00224     cout << tr() << endl;
00225     cout << td(strong("Hostname: ")) << endl;
00226     cout << td() << "<input type=\"text\" name=\"hostname\"";
00227     if(name != (*cgi).end())
00228       cout << " value=\"" << **name << "\">";
00229     else
00230       cout << ">";
00231     cout << td() << tr() << endl;
00232     cout << "</form>" << table() << p() << endl;
00233     
00234     // Now print cout a footer with some fun info
00235     cout << hr(set("class","half")) << endl;
00236     cout << CGICCNS div().set("align","center").set("class","smaller") << endl;
00237     cout << "GNU cgi" << span("cc").set("class","red") << " v"
00238          << cgi.getVersion() << br() << endl;
00239     cout << "Compiled at " << cgi.getCompileTime() 
00240          << " on " << cgi.getCompileDate() << br() << endl;
00241     
00242     cout << "Configured for " << cgi.getHost();  
00243     // I don't know if everyone has uname...
00244 #if HAVE_UNAME
00245     struct utsname info;
00246     if(uname(&info) != -1) {
00247       cout << ". Running on " << info.sysname;
00248       cout << ' ' << info.release << " (";
00249       cout << info.nodename << ')' << endl;
00250     }
00251 #else
00252     cout << '.' << endl;
00253 #endif
00254     
00255 #if HAVE_GETTIMEOFDAY
00256     // Information on this query
00257     timeval end;
00258     gettimeofday(&end, NULL);
00259     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00260       + (end.tv_usec - start.tv_usec);
00261 
00262     cout << br() << "Total time for request = " << us << " us";
00263     cout << " (" << (double) (us/1000000.0) << " s)";
00264 #endif
00265     
00266     // End of document
00267     cout << CGICCNS div() << endl;
00268     cout << body() << html() << endl;
00269 
00270     return EXIT_SUCCESS;
00271   }
00272 
00273   catch(const STDNS exception& e) {
00274     return EXIT_FAILURE;
00275   }
00276 }

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996 - 2002 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 Sun Mar 17 16:40:57 2002 for cgicc by doxygen 1.2.13.1