Main Page   Namespace List   Class Hierarchy   Alphabetical List   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.15 2001/09/03 16:14:26 sbooth Exp $
00003  *
00004  *  Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 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/HTTPHeaders.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 << meta().set("name", "author")
00111                   .set("content", "Stephen F. Booth") << endl;
00112     cout << head() << endl;
00113     
00114     cout << h1() << "GNU cgi" << span("cc").set("class","red")
00115          << " DNS Gateway" << h1() << endl;
00116   
00117     form_iterator ip = cgi.getElement("ip");
00118     form_iterator name = cgi.getElement("hostname");
00119 
00120     if(ip != (*cgi).end()) {
00121       cout << h3() << "Query results for " << **ip << h3() << endl;
00122       
00123       u_long addr;
00124       struct hostent *hp;
00125       char **p;
00126       
00127       if((int)(addr = inet_addr((**ip).c_str())) == -1) {
00128         cout << CGICCNS div().set("class", "dns") << endl
00129              << strong(span("ERROR").set("class","red"))
00130              << " - IP address must be of the form x.x.x.x"
00131              << endl << CGICCNS div() << endl;
00132       }
00133       else {
00134         hp = gethostbyaddr((char*)&addr, sizeof (addr), AF_INET);
00135         if(hp == NULL) {
00136           cout << CGICCNS div().set("class", "dns") << endl
00137                << strong(span("ERROR").set("class","red")) 
00138                << " - Host information for " << em((**ip)) << " not found."
00139                << endl << CGICCNS div() << endl;
00140         }
00141         else {
00142           for(p = hp->h_addr_list; *p != 0; p++) {
00143             struct in_addr in;
00144             //char **q;
00145             
00146             (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00147             
00148             cout << CGICCNS div().set("class", "dns") << endl
00149                  << span(inet_ntoa(in)).set("class","blue") 
00150                  << " - " << ' ' << hp->h_name;
00151             //for(q = hp->h_aliases; *q != 0; q++)
00152             //      cout << *q << ' ';
00153             cout << endl << CGICCNS div() << endl;
00154           }
00155         }
00156       }
00157     }
00158     
00159 
00160     if(name != (*cgi).end()) {
00161       cout << h3() << "Query results for " << **name << h3() << endl;
00162       
00163       struct hostent *hp;
00164       char **p;
00165       
00166       hp = gethostbyname((**name).c_str());
00167       if(hp == NULL) {
00168         cout << CGICCNS div().set("class", "dns") << endl
00169              << strong(span("ERROR").set("class","red"))
00170              << " - Host information for " << em(**name) << " not found."
00171              << endl << CGICCNS div() << endl;
00172       }
00173       else {
00174         for(p = hp->h_addr_list; *p != 0; p++) {
00175           struct in_addr in;
00176           //char **q;
00177           
00178           (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
00179           
00180           cout << CGICCNS div().set("class", "dns") << endl
00181                << inet_ntoa(in) << " - " << ' ' 
00182                << span(hp->h_name).set("class","blue");
00183           //    for(q = hp->h_aliases; *q != 0; q++)
00184           //      cout << *q << ' ';
00185           cout << endl << CGICCNS div() << endl;
00186         }
00187       }
00188     }
00189     
00190     cout << p("Please enter an IP address or a hostname.") << endl;
00191     
00192     cout << table().set("border","0")
00193                    .set("rules","none")
00194                    .set("frame","void")
00195                    .set("cellspacing","2").set("cellpadding","2") << endl;
00196     cout << colgroup().set("span","2") << endl;
00197     cout << col().set("align","center")
00198                  .set("class","title")
00199                  .set("span","1") << endl;
00200     cout << col().set("align","left")
00201                  .set("class","data")
00202                  .set("span","1") << endl;
00203     cout << colgroup() << endl;
00204     
00205     cout << "<form method=\"post\" action=\"http://"
00206          << cgi.getEnvironment().getServerName();
00207     if(cgi.getEnvironment().getServerPort() != 80)
00208       cout << ":" << cgi.getEnvironment().getServerPort();
00209     cout << cgi.getEnvironment().getScriptName() << "\">" << endl;
00210     
00211     cout << tr() << endl;
00212     cout << td(strong("IP Address: ")) << endl;
00213     cout << td() << "<input type=\"text\" name=\"ip\"";
00214     if(ip != (*cgi).end())
00215       cout << " value=\"" << **ip << "\">";
00216     else
00217       cout << ">";
00218     cout << td() << tr() << "</form>" << endl;
00219     
00220     cout << "<form method=\"post\" action=\"http://"
00221          << cgi.getEnvironment().getServerName();
00222     if(cgi.getEnvironment().getServerPort() != 80)
00223       cout << ":" << cgi.getEnvironment().getServerPort();
00224     cout << cgi.getEnvironment().getScriptName() << "\">" << endl;
00225     
00226     cout << tr() << endl;
00227     cout << td(strong("Hostname: ")) << endl;
00228     cout << td() << "<input type=\"text\" name=\"hostname\"";
00229     if(name != (*cgi).end())
00230       cout << " value=\"" << **name << "\">";
00231     else
00232       cout << ">";
00233     cout << td() << tr() << endl;
00234     cout << "</form>" << table() << p() << endl;
00235     
00236     // Now print cout a footer with some fun info
00237     cout << hr(set("class","half")) << endl;
00238     cout << CGICCNS div().set("align","center").set("class","smaller") << endl;
00239     cout << "GNU cgi" << span("cc").set("class","red") << " v"
00240          << cgi.getVersion();
00241     cout << " by " << a("Stephen F. Booth")
00242       .set("href", "mailto:sbooth@gnu.org") << br() << endl;
00243     cout << "Compiled at " << cgi.getCompileTime() 
00244          << " on " << cgi.getCompileDate() << br() << endl;
00245     
00246     cout << "Configured for " << cgi.getHost();  
00247     // I don't know if everyone has uname...
00248 #if HAVE_UNAME
00249     struct utsname info;
00250     if(uname(&info) != -1) {
00251       cout << ". Running on " << info.sysname;
00252       cout << ' ' << info.release << " (";
00253       cout << info.nodename << ')' << endl;
00254     }
00255 #else
00256     cout << '.' << endl;
00257 #endif
00258     
00259 #if HAVE_GETTIMEOFDAY
00260     // Information on this query
00261     timeval end;
00262     gettimeofday(&end, NULL);
00263     long us = ((end.tv_sec - start.tv_sec) * 1000000)
00264       + (end.tv_usec - start.tv_usec);
00265 
00266     cout << br() << "Total time for request = " << us << " us";
00267     cout << " (" << (double) (us/1000000.0) << " s)";
00268 #endif
00269     
00270     // End of document
00271     cout << CGICCNS div() << endl;
00272     cout << body() << html() << endl;
00273 
00274     return EXIT_SUCCESS;
00275   }
00276 
00277   catch(const STDNS exception& e) {
00278     return EXIT_FAILURE;
00279   }
00280 }

GNU cgicc - A C++ class library for writing CGI applications
Copyright © 1996, 1997, 1998, 1999, 2000, 2001, 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 Wed Jan 9 12:31:26 2002 for cgicc by doxygen 1.2.13.1