b0b.5m1th.f4 has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if I needed to change the following code with my own data and numbers, for example my own DNS data, or if it would be fine leaving it?

use strict; use Net::DNS::Nameserver; # ip to bind dns server to my $BINDIP = "127.0.0.1"; my $BINDPORT = 53; my %spoof = ( 'microsoft.com' => '72.14.179.47', ); my @ns = qw/4.2.2.2 4.2.2.3/; my $hostsfile = "/etc/hosts"; open(HOSTS, "<$hostsfile") || die "Can't read $hostsfile: $!"; while (<HOSTS>) { if (/^\s*(\d+\.\d+\.\d+\.\d+)\s+([^#\s]+)/) { $spoof{lc($2)} = $1; } } close(HOSTS); my $resolver = new Net::DNS::Resolver( nameservers => \@ns, recurse => 1, ); sub uniq {     my %seen;     grep !$seen{$_}++, @_; } sub reply_handler { my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_; my ($rcode, @ans, @auth, @add); my $err = "NOERROR"; print "Received query from $peerhost to ". $conn->{sockhost}. "\n" +; $query->print; if ($spoof{lc($qname)}) { my ($ttl, $rdata) = (3600, $spoof{lc($qname)}); my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata") +; push @ans, $rr; } else { my $ret = $resolver->search($qname); if ($ret) { foreach my $rr ($ret->answer) { next unless $rr->type eq "A";      push @ans, @{$ret->{answer}};      } } else { $err = "NXDOMAIN"; } } @ans = uniq(@ans); return ($err, \@ans, \@auth, \@add, { aa => 1 }); } my $ns = new Net::DNS::Nameserver( LocalPort => $BINDPORT, LocalAddr => $BINDIP, ReplyHandler => \&reply_handler, Verbose => 1 ) || die "couldn't create nameserver object\n"; $ns->main_loop;

Replies are listed 'Best First'.
Re: Perl networking DNS
by Corion (Patriarch) on Mar 06, 2017 at 08:30 UTC

    What do you want to achieve?

    What of that does your code not already do?

      "I was wondering if I needed to change the following code with my own data and numbers, for example my own DNS data, or if it would be fine leaving it?"

        "What do you want to achieve?"

        If you found this code somewhere and are wondering what to do with it, maybe you shouldn't.

        If you want "for example" to supply your own DNS data, you will have to change the code.

        As you haven't told us what you want to achieve, it's hard to give you more concrete advice.

Re: Perl networking DNS
by stevieb (Canon) on Mar 07, 2017 at 02:05 UTC

    Do you know what this code does?

    If not, I would highly recommend not using domain names or IP addresses that are related/assigned to entities that aren't your own in all cases.