dns.cpp
Go to the documentation of this file.
1/* -*-mode:c++; c-file-style: "gnu";-*- */
2/*
3 * $Id: dns.cpp,v 1.25 2009/01/03 17:26:43 sebdiaz Exp $
4 *
5 * Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
6 * 2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
7 * Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 3 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
22 */
23
32#include <cstdlib>
33#include <new>
34#include <vector>
35#include <stdexcept>
36#include <iostream>
37#include <stdio.h>
38#include <memory.h>
39#include "cgicc/CgiDefs.h"
40#include "cgicc/Cgicc.h"
42#include "cgicc/HTMLClasses.h"
43
44#if HAVE_SYS_UTSNAME_H
45# include <sys/utsname.h>
46#endif
47
48#if HAVE_SYS_TIME_H
49# include <sys/time.h>
50#endif
51
52#ifdef WIN32
53# include <winsock2.h>
54#else
55# include <sys/types.h>
56# include <sys/socket.h>
57# include <netinet/in.h>
58# include <arpa/inet.h>
59# include <netdb.h>
60#endif /* WIN32 */
61
62#include "styles.h"
63
64using namespace std;
65using namespace cgicc;
66
67// DNS gateway cgi
68int
69main(int /*argc*/,
70 char ** /*argv*/)
71{
72
73 try {
74#if HAVE_GETTIMEOFDAY
75 timeval start;
76 gettimeofday(&start, NULL);
77#endif
78
79 Cgicc cgi;
80
82 cout << html().set("lang","en").set("dir","ltr") << endl;
83
84 // Set up the page; I will put in lfs to ease reading of the
85 // produced HTML. These are optional, and except in <PRE>
86 // tags have no effect on HTML appearance.
87 cout << head() << endl;
88
89 // Output the style sheet portion of the header
90 cout << style() << comment() << endl;
91 cout << styles;
92 cout << comment() << style() << endl;
93
94 cout << title("DNS Gateway") << endl;
95 cout << head() << endl;
96
97 cout << h1() << "GNU cgi" << span("cc").set("class","red")
98 << " DNS Gateway" << h1() << endl;
99
100 form_iterator ip = cgi.getElement("ip");
101 form_iterator name = cgi.getElement("hostname");
102
103 if(ip != (*cgi).end()) {
104 cout << h3() << "Query results for " << **ip << h3() << endl;
105
106 u_long addr;
107 struct hostent *hp;
108 char **p;
109
110 if((int)(addr = inet_addr((**ip).c_str())) == -1) {
111 cout << cgicc::div().set("class", "notice") << endl
112 << strong(span("ERROR").set("class","red"))
113 << " - IP address must be of the form x.x.x.x"
114 << endl << cgicc::div() << endl;
115 }
116 else {
117 hp = gethostbyaddr((char*)&addr, sizeof (addr), AF_INET);
118 if(hp == NULL) {
119 cout << cgicc::div().set("class", "notice") << endl
120 << strong(span("ERROR").set("class","red"))
121 << " - Host information for " << em((**ip)) << " not found."
122 << endl << cgicc::div() << endl;
123 }
124 else {
125 for(p = hp->h_addr_list; *p != 0; p++) {
126 struct in_addr in;
127 //char **q;
128
129 (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
130
131 cout << cgicc::div().set("class", "notice") << endl
132 << span(inet_ntoa(in)).set("class","blue")
133 << " - " << ' ' << hp->h_name;
134 //for(q = hp->h_aliases; *q != 0; q++)
135 // cout << *q << ' ';
136 cout << endl << cgicc::div() << endl;
137 }
138 }
139 }
140 }
141
142
143 if(name != (*cgi).end()) {
144 cout << h3() << "Query results for " << **name << h3() << endl;
145
146 struct hostent *hp;
147 char **p;
148
149 hp = gethostbyname((**name).c_str());
150 if(hp == NULL) {
151 cout << cgicc::div().set("class", "notice") << endl
152 << strong(span("ERROR").set("class","red"))
153 << " - Host information for " << em(**name) << " not found."
154 << endl << cgicc::div() << endl;
155 }
156 else {
157 for(p = hp->h_addr_list; *p != 0; p++) {
158 struct in_addr in;
159 //char **q;
160
161 (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
162
163 cout << cgicc::div().set("class", "notice") << endl
164 << inet_ntoa(in) << " - " << ' '
165 << span(hp->h_name).set("class","blue");
166 // for(q = hp->h_aliases; *q != 0; q++)
167 // cout << *q << ' ';
168 cout << endl << cgicc::div() << endl;
169 }
170 }
171 }
172
173 cout << p("Please enter an IP address or a hostname.") << endl;
174
175 cout << table() << endl;
176
177 cout << "<form method=\"post\" action=\""
178 << cgi.getEnvironment().getScriptName() << "\">" << endl;
179
180 cout << tr() << endl;
181 cout << td(strong("IP Address: ")).set("class", "title") << endl;
182 cout << td().set("class", "data") << "<input type=\"text\" name=\"ip\"";
183 if(ip != (*cgi).end())
184 cout << " value=\"" << **ip << "\">";
185 else
186 cout << ">";
187 cout << td() << tr() << "</form>" << endl;
188
189 cout << "<form method=\"post\" action=\""
190 << cgi.getEnvironment().getScriptName() << "\">" << endl;
191
192 cout << tr() << endl;
193 cout << td(strong("Hostname: ")).set("class", "title") << endl;
194 cout << td().set("class", "data")
195 << "<input type=\"text\" name=\"hostname\"";
196 if(name != (*cgi).end())
197 cout << " value=\"" << **name << "\">";
198 else
199 cout << ">";
200 cout << td() << tr() << endl;
201 cout << "</form>" << table() << p() << endl;
202
203 // Now print cout a footer with some fun info
204 cout << hr(set("class","half")) << endl;
205 cout << cgicc::div().set("align","center").set("class","smaller") << endl;
206 cout << "GNU cgi" << span("cc").set("class","red") << " v"
207 << cgi.getVersion() << br() << endl;
208 cout << "Compiled at " << cgi.getCompileTime()
209 << " on " << cgi.getCompileDate() << br() << endl;
210
211 cout << "Configured for " << cgi.getHost();
212 // I don't know if everyone has uname...
213#if HAVE_UNAME
214 struct utsname info;
215 if(uname(&info) != -1) {
216 cout << ". Running on " << info.sysname;
217 cout << ' ' << info.release << " (";
218 cout << info.nodename << ')' << endl;
219 }
220#else
221 cout << '.' << endl;
222#endif
223
224#if HAVE_GETTIMEOFDAY
225 // Information on this query
226 timeval end;
227 gettimeofday(&end, NULL);
228 long us = ((end.tv_sec - start.tv_sec) * 1000000)
229 + (end.tv_usec - start.tv_usec);
230
231 cout << br() << "Total time for request = " << us << " us";
232 cout << " (" << static_cast<double>(us/1000000.0) << " s)";
233#endif
234
235 // End of document
236 cout << cgicc::div() << endl;
237 cout << body() << html() << endl;
238
239 return EXIT_SUCCESS;
240 }
241
242 catch(const std::exception& e) {
243 return EXIT_FAILURE;
244 }
245}
Platform and operating system specific macro definitions.
The main header file for the GNU cgicc library.
The header file containing HTML output classes.
Shortcut to HTTPContentHeader for text/html.
std::string getScriptName() const
Get the full path to this CGI application.
The main class of the GNU cgicc library.
Definition: Cgicc.h:103
const char * getCompileDate() const
Get the date on which this library was compiled.
const CgiEnvironment & getEnvironment() const
Definition: Cgicc.h:394
const char * getVersion() const
Get the version number of cgicc.
const char * getCompileTime() const
Get the time at which this library was compiled.
const char * getHost() const
Get the platform for which Cgicc was configured.
form_iterator getElement(const std::string &name)
Find a radio button in a radio group, or a selected list item.
Specifies the DTD of the HTML 4 document.
Definition: HTMLDoctype.h:57
HTMLElement & set(const std::string &name)
Set an HTMLAttribute on this HTMLElement.
Shortcut to HTTPContentHeader for text/html.
An HTML comment.
Definition: HTMLClasses.h:93
The namespace containing the cgicc library.
Definition: Cgicc.h:52
std::vector< FormEntry >::iterator form_iterator
A vector of FormEntry objects.
Definition: Cgicc.h:66
HTMLAttributeList set(const std::string &name)
Create a new HTMLAttributeList, and set an HTMLAttribute.

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 Fri Mar 1 2024 08:39:41 for cgicc by doxygen 1.9.6