]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/kvresp.cc
Merge pull request #14021 from Habbie/auth-lua-join-whitespace
[thirdparty/pdns.git] / pdns / kvresp.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "iputils.hh"
26 #include "sstuff.hh"
27 #include "statbag.hh"
28
29 /* This tool REALLY wants to be rewritten in Python, by ahu does not speak it very well.
30 What it does is provide answers to queries from the Lua generic UDP Question/Answer
31 stuff in kv-example-script.lua */
32
33 StatBag S;
34
35 int main(int argc, char** argv)
36 try
37 {
38 if(argc != 3) {
39 cerr<<"Syntax: kvresp local-address local-port"<<endl;
40 exit(EXIT_FAILURE);
41 }
42
43 ComboAddress local(argv[1], atoi(argv[2]));
44 Socket s(local.sin4.sin_family, SOCK_DGRAM);
45
46 s.bind(local);
47 cout<<"Bound to "<<local.toStringWithPort()<<endl;
48
49 char buffer[1500];
50
51 int len;
52 ComboAddress rem=local;
53 socklen_t socklen = rem.getSocklen();
54 for(;;) {
55 len=recvfrom(s.getHandle(), buffer, sizeof(buffer), 0, (struct sockaddr*)&rem, &socklen);
56 if(len < 0)
57 unixDie("recvfrom");
58 string query(buffer, len);
59 cout<<"Had packet: "<<query<<endl;
60 vector<string> parts;
61 stringtok(parts, query);
62 if(parts.size()<2)
63 continue;
64 string response;
65 if(parts[0]=="DOMAIN")
66 response= (parts[1].find("xxx") != string::npos) ? "1" : "0";
67 else if(parts[0]=="IP")
68 response= (parts[1]=="127.0.0.1") ? "1" : "0";
69 else
70 response= "???";
71
72 cout<<"Our reply: "<<response<<endl;
73 if(sendto(s.getHandle(), response.c_str(), response.length(), 0, (struct sockaddr*)&rem, socklen) < 0)
74 unixDie("sendto");
75 }
76 }
77 catch(std::exception& e)
78 {
79 cerr<<"Fatal error: "<<e.what()<<endl;
80 exit(EXIT_FAILURE);
81 }