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