File: dns.cpp

package info (click to toggle)
libcgicc 3.2.19-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,020 kB
  • sloc: cpp: 5,023; sh: 4,438; makefile: 169; sed: 1
file content (245 lines) | stat: -rw-r--r-- 7,198 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* -*-mode:c++; c-file-style: "gnu";-*- */
/*
 *  $Id: dns.cpp,v 1.25 2009/01/03 17:26:43 sebdiaz Exp $
 *
 *  Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
 *                       2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
 *  Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 3 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA 
 */

/*! \file dns.cpp
 * \brief A WWW to DNS gateway
 *
 * A sample CGI application using the GNU %cgicc library.  This script 
 * allows users to lookup the IP address corresponding to a hostname, 
 * or vice-versa.
 */

#include <cstdlib>
#include <new>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include "cgicc/CgiDefs.h"
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"

#if HAVE_SYS_UTSNAME_H
#  include <sys/utsname.h>
#endif

#if HAVE_SYS_TIME_H
#  include <sys/time.h>
#endif

#ifdef WIN32
#  include <winsock2.h>
#else
#  include <sys/types.h>
#  include <sys/socket.h>
#  include <netinet/in.h>
#  include <arpa/inet.h>
#  include <netdb.h>
#endif /* WIN32 */

#include "styles.h"

using namespace std;
using namespace cgicc;

// DNS gateway cgi
int
main(int /*argc*/, 
     char ** /*argv*/)
{

  try {
#if HAVE_GETTIMEOFDAY
    timeval start;
    gettimeofday(&start, NULL);
#endif

    Cgicc cgi;
    
    cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl;
    cout << html().set("lang","en").set("dir","ltr") << endl;
    
    // Set up the page; I will put in lfs to ease reading of the
    // produced HTML. These are optional, and except in <PRE>
    // tags have no effect on HTML appearance.
    cout << head() << endl;

    // Output the style sheet portion of the header
    cout << style() << comment() << endl;
    cout << styles;
    cout << comment() << style() << endl;

    cout << title("DNS Gateway") << endl;
    cout << head() << endl;
    
    cout << h1() << "GNU cgi" << span("cc").set("class","red")
	 << " DNS Gateway" << h1() << endl;
  
    form_iterator ip = cgi.getElement("ip");
    form_iterator name = cgi.getElement("hostname");

    if(ip != (*cgi).end()) {
      cout << h3() << "Query results for " << **ip << h3() << endl;
      
      u_long addr;
      struct hostent *hp;
      char **p;
      
      if((int)(addr = inet_addr((**ip).c_str())) == -1) {
	cout << cgicc::div().set("class", "notice") << endl
	     << strong(span("ERROR").set("class","red"))
	     << " - IP address must be of the form x.x.x.x"
	     << endl << cgicc::div() << endl;
      }
      else {
	hp = gethostbyaddr((char*)&addr, sizeof (addr), AF_INET);
	if(hp == NULL) {
	  cout << cgicc::div().set("class", "notice") << endl
	       << strong(span("ERROR").set("class","red")) 
	       << " - Host information for " << em((**ip)) << " not found."
	       << endl << cgicc::div() << endl;
	}
	else {
	  for(p = hp->h_addr_list; *p != 0; p++) {
	    struct in_addr in;
	    //char **q;
	    
	    (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
	    
	    cout << cgicc::div().set("class", "notice") << endl
		 << span(inet_ntoa(in)).set("class","blue") 
		 << " - " << ' ' << hp->h_name;
	    //for(q = hp->h_aliases; *q != 0; q++)
	    //	    cout << *q << ' ';
	    cout << endl << cgicc::div() << endl;
	  }
	}
      }
    }
    

    if(name != (*cgi).end()) {
      cout << h3() << "Query results for " << **name << h3() << endl;
      
      struct hostent *hp;
      char **p;
      
      hp = gethostbyname((**name).c_str());
      if(hp == NULL) {
	cout << cgicc::div().set("class", "notice") << endl
	     << strong(span("ERROR").set("class","red"))
	     << " - Host information for " << em(**name) << " not found."
	     << endl << cgicc::div() << endl;
      }
      else {
	for(p = hp->h_addr_list; *p != 0; p++) {
	  struct in_addr in;
	  //char **q;
	  
	  (void) memcpy(&in.s_addr, *p, sizeof(in.s_addr));
	  
	  cout << cgicc::div().set("class", "notice") << endl
	       << inet_ntoa(in) << " - " << ' ' 
	       << span(hp->h_name).set("class","blue");
	  //	for(q = hp->h_aliases; *q != 0; q++)
	  //	  cout << *q << ' ';
	  cout << endl << cgicc::div() << endl;
	}
      }
    }
    
    cout << p("Please enter an IP address or a hostname.") << endl;
    
    cout << table() << endl;
    
    cout << "<form method=\"post\" action=\""
	 << cgi.getEnvironment().getScriptName() << "\">" << endl;
    
    cout << tr() << endl;
    cout << td(strong("IP Address: ")).set("class", "title") << endl;
    cout << td().set("class", "data") << "<input type=\"text\" name=\"ip\"";
    if(ip != (*cgi).end())
      cout << " value=\"" << **ip << "\">";
    else
      cout << ">";
    cout << td() << tr() << "</form>" << endl;
    
    cout << "<form method=\"post\" action=\""
	 << cgi.getEnvironment().getScriptName() << "\">" << endl;
    
    cout << tr() << endl;
    cout << td(strong("Hostname: ")).set("class", "title") << endl;
    cout << td().set("class", "data") 
	 << "<input type=\"text\" name=\"hostname\"";
    if(name != (*cgi).end())
      cout << " value=\"" << **name << "\">";
    else
      cout << ">";
    cout << td() << tr() << endl;
    cout << "</form>" << table() << p() << endl;
    
    // Now print cout a footer with some fun info
    cout << hr(set("class","half")) << endl;
    cout << cgicc::div().set("align","center").set("class","smaller") << endl;
    cout << "GNU cgi" << span("cc").set("class","red") << " v"
	 << cgi.getVersion() << br() << endl;
    cout << "Compiled at " << cgi.getCompileTime() 
	 << " on " << cgi.getCompileDate() << br() << endl;
    
    cout << "Configured for " << cgi.getHost();  
    // I don't know if everyone has uname...
#if HAVE_UNAME
    struct utsname info;
    if(uname(&info) != -1) {
      cout << ". Running on " << info.sysname;
      cout << ' ' << info.release << " (";
      cout << info.nodename << ')' << endl;
    }
#else
    cout << '.' << endl;
#endif
    
#if HAVE_GETTIMEOFDAY
    // Information on this query
    timeval end;
    gettimeofday(&end, NULL);
    long us = ((end.tv_sec - start.tv_sec) * 1000000)
      + (end.tv_usec - start.tv_usec);

    cout << br() << "Total time for request = " << us << " us";
    cout << " (" << static_cast<double>(us/1000000.0) << " s)";
#endif
    
    // End of document
    cout << cgicc::div() << endl;
    cout << body() << html() << endl;

    return EXIT_SUCCESS;
  }

  catch(const std::exception& e) {
    return EXIT_FAILURE;
  }
}