xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/ditch.pl (revision e670fd5c413e99c2f6a37901bb21c537fcd322d2)
1#!/usr/bin/perl
2#
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0. If a copy of the MPL was not distributed with this
7# file, you can obtain one at https://mozilla.org/MPL/2.0/.
8#
9# See the COPYRIGHT file distributed with this work for additional
10# information regarding copyright ownership.
11
12# This is a tool for sending queries via UDP to specified address and
13# port, then exiting without waiting for a response.
14#
15# Usage: ditch.pl [-s <address>] [-p <port>] [filename]
16#
17# Input (in filename, if specified, otherwise stdin) is a series of one
18# or more DNS names and types to send as queries, e.g.:
19#
20# www.example.com A
21# www.example.org MX
22#
23# If not specified, address defaults to 127.0.0.1, port to 53.
24
25require 5.006.001;
26
27use strict;
28use Getopt::Std;
29use Net::DNS;
30use Net::DNS::Packet;
31use IO::File;
32use IO::Socket;
33
34sub usage {
35    print ("Usage: ditch.pl [-s address] [-p port] [file]\n");
36    exit 1;
37}
38
39my %options={};
40getopts("s:p:t:", \%options);
41
42my $addr = "127.0.0.1";
43$addr = $options{s} if defined $options{s};
44
45my $port = 53;
46$port = $options{p} if defined $options{p};
47
48my $file = "STDIN";
49if (@ARGV >= 1) {
50    my $filename = shift @ARGV;
51    open FH, "<$filename" or die "$filename: $!";
52    $file = "FH";
53}
54
55my $input = "";
56while (defined(my $line = <$file>) ) {
57    chomp $line;
58    next if ($line =~ m/^ *#/);
59    my @tokens = split (' ', $line);
60
61    my $packet;
62    if ($Net::DNS::VERSION > 0.68) {
63            $packet = new Net::DNS::Packet();
64            $@ and die $@;
65    } else {
66            my $err;
67            ($packet, $err) = new Net::DNS::Packet();
68            $err and die $err;
69    }
70
71    my $q = new Net::DNS::Question($tokens[0], $tokens[1], "IN");
72    $packet->header->rd(1);
73    $packet->push(question => $q);
74
75    my $sock = IO::Socket::INET->new(PeerAddr => $addr, PeerPort => $port,
76                                     Proto => "udp",) or die "$!";
77
78    my $bytes = $sock->send($packet->data);
79    #print ("sent $bytes bytes to $addr:$port:\n");
80    #print ("  ", unpack("H* ", $packet->data), "\n");
81
82    $sock->close;
83}
84
85close $file;
86