dns.cpp

Go to the documentation of this file.
00001 /* -*-mode:c++; c-file-style: "gnu";-*- */
00002 /*
00003  *  $Id: dns_8cpp-source.html,v 1.3 2008/01/19 21:53:46 sebdiaz Exp $
00004  *
00005  *  Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
00006  *                       2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
00007  *  Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Lesser General Public
00011  *  License as published by the Free Software Foundation; either
00012  *  version 3 of the License, or (at your option) any later version.
00013  *
00014  *  This library is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  *  Lesser General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU Lesser General Public
00020  *  License along with this library; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA 
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 /* WIN32 */
00060 
00061 #include "styles.h"
00062 
00063 using namespace std;
00064 using namespace cgicc;
00065 
00066 // DNS gateway cgi
00067 int
00068 main(int /*argc*/, 
00069      char ** /*argv*/)
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     // Set up the page; I will put in lfs to ease reading of the
00084     // produced HTML. These are optional, and except in <PRE>
00085     // tags have no effect on HTML appearance.
00086     cout << head() << endl;
00087 
00088     // Output the style sheet portion of the header
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             //char **q;
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             //for(q = hp->h_aliases; *q != 0; q++)
00134             //      cout << *q << ' ';
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           //char **q;
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           //    for(q = hp->h_aliases; *q != 0; q++)
00166           //      cout << *q << ' ';
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     // Now print cout a footer with some fun info
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     // I don't know if everyone has uname...
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     // Information on this query
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     // End of document
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 }

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 Jan 19 21:15:58 2008 for cgicc by doxygen 1.5.1