]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/notify.cc
Merge remote-tracking branch 'origin/master' into after-notify-commit
[thirdparty/pdns.git] / pdns / notify.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 <bitset>
26 #include "dnsparser.hh"
27 #include "iputils.hh"
28 #include <boost/program_options.hpp>
29
30 #include <boost/format.hpp>
31 #include <boost/utility.hpp>
32 #include <boost/multi_index_container.hpp>
33 #include <boost/multi_index/ordered_index.hpp>
34 #include <boost/multi_index/key_extractors.hpp>
35
36 #include "mplexer.hh"
37 #include "statbag.hh"
38 #include "arguments.hh"
39 #include "version.hh"
40 #include "namespaces.hh"
41 using namespace ::boost::multi_index;
42 #include "namespaces.hh"
43
44 namespace po = boost::program_options;
45 po::variables_map g_vm;
46
47 StatBag S;
48 ArgvMap &arg()
49 {
50 static ArgvMap arg;
51 return arg;
52 }
53
54 void usage() {
55 cerr<<"Syntax: pdns_notify IP_ADDRESS/HOSTNAME[:PORT] DOMAIN"<<endl;
56 }
57
58 int main(int argc, char** argv)
59 try
60 {
61 set<ComboAddress> addrs;
62
63 for(int n=1 ; n < argc; ++n) {
64 if ((string) argv[n] == "--help") {
65 usage();
66 return EXIT_SUCCESS;
67 }
68
69 if ((string) argv[n] == "--version") {
70 cerr<<"notify "<<VERSION<<endl;
71 return EXIT_SUCCESS;
72 }
73 }
74
75 if(argc!=3) {
76 usage();
77 exit(1);
78 }
79
80 int sock = -1;
81
82 // ComboAddress local("127.0.0.1", (int)0);
83 // if(::bind(sock, (struct sockaddr*) &local, local.getSocklen()) < 0)
84 // throw runtime_error("Failed to bind local socket to address "+local.toString()+": "+stringerror());
85
86 try {
87 addrs.emplace(ComboAddress{argv[1], 53});
88 } catch (PDNSException &ex) {
89 /* needs resolving, maybe */
90 struct addrinfo *info;
91 vector<string> parts;
92 boost::split(parts, argv[1], [](char c){return c == ':';});
93 if (parts.size() == 1)
94 parts.push_back("domain");
95 else if (parts.size() != 2)
96 throw runtime_error("Invalid hostname:port syntax");
97 if (getaddrinfo(parts[0].c_str(), parts[1].c_str(), NULL, &info) < 0)
98 throw runtime_error("Cannot resolve '" + string(argv[1]) +"'");
99 for(auto ptr = info; ptr != NULL; ptr = ptr->ai_next)
100 addrs.emplace(ComboAddress{ptr->ai_addr, ptr->ai_addrlen});
101 }
102
103 for(const auto &addr: addrs) {
104 if (sock > -1)
105 (void)close(sock);
106 sock = socket(addr.sin4.sin_family, SOCK_DGRAM, 0);
107 if(sock < 0)
108 throw runtime_error("Creating socket for incoming packets: "+stringerror());
109 if(connect(sock, (struct sockaddr*)&addr, addr.getSocklen()) < 0) {
110 cerr<<"Failed to connect to address "+addr.toStringWithPort()+": "+stringerror()<<endl;
111 continue;
112 }
113 vector<uint8_t> outpacket;
114 DNSPacketWriter pw(outpacket, DNSName(argv[2]), QType::SOA, 1, Opcode::Notify);
115 pw.getHeader()->id = random();
116
117 if(send(sock, &outpacket[0], outpacket.size(), 0) < 0) {
118 cerr<<"Unable to send notify to "<<addr.toStringWithPort()<<": "+stringerror()<<endl;
119 continue;
120 }
121
122 char buffer[1500];
123 fd_set rfds;
124 FD_ZERO(&rfds);
125 FD_SET(sock, &rfds);
126 int len;
127 struct timeval tv;
128 bool timeout = true;
129
130 for(int tries=0; tries<60; tries++) {
131 tv.tv_sec = 1;
132 tv.tv_usec = 0;
133 if ((len = select(sock+1, &rfds, NULL, &rfds, &tv)) > 0) {
134 len = recv(sock, buffer, sizeof(buffer), 0);
135 timeout = false;
136 break;
137 }
138 }
139
140 if(len < 0) {
141 cerr<<"Unable to receive notification response from "<<addr.toStringWithPort()<<": "+stringerror()<<endl;
142 continue;
143 } else if (timeout) {
144 cerr<<"Unable to receive notification response from "<<addr.toStringWithPort()<<": Timed out"<<endl;
145 continue;
146 } else if (len == 0) {
147 cerr<<"Unable to receive notification response from "<<addr.toStringWithPort()<<": EOF"<<endl;
148 continue;
149 }
150
151 string packet(buffer, len);
152 MOADNSParser mdp(false, packet);
153
154 if (mdp.d_header.rcode == 0)
155 cerr<<"Successfully notified "<<addr.toStringWithPort()<<endl;
156 else
157 cerr<<"Received notification response with error from "<<addr.toStringWithPort()<<": "<<RCode::to_s(mdp.d_header.rcode)<<endl;
158 cerr<<"For: '"<<mdp.d_qname<<"'"<<endl;
159 }
160 }
161 catch(std::exception& e)
162 {
163 cerr<<"Fatal: "<<e.what()<<endl;
164 }
165