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

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 Mon Jul 30 2012 03:22:28 for cgicc by doxygen 1.8.0