#!/usr/bin/perl -w

=head1 NAME

dnstelnet - Wrapper script for IO::Socket::DNS

=head1 SYNOPSIS

  dnstelnet <suffix> <host> <port>

=head1 DESCRIPTION

This is mostly just a tester utility to make sure that dnsd is
configured and running properly. This will tunnel a TCP
connection through the DNS proxy server to any TCP server
using the telnet command.

=head1 EXAMPLE

  dnstelnet d.example.com cpan.mx.develooper.com 25

=head1 SEE ALSO

dnsc, dnsd, telnet

=head1 AUTHOR

Rob Brown, E<lt>bbb@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2011 by Rob Brown

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.9 or,
at your option, any later version of Perl 5 you may have available.

=cut

use strict;
use IO::Socket;

my $suffix = shift || $ENV{DNS_SUFFIX} or die "Tunnel DNS Suffix must be specified\n";
my $host = shift or die "Specify the host or IP to connect to.\n";
my $port = shift;
if (!$port) {
    $port = $1 if $host =~ s/:(\d+)$//;
}

my $dnsc = $0;
if ($dnsc =~ s/dnstelnet/dnsc/) {
    if (-x $dnsc) {
        print "Using proxy program: $dnsc ...\n";
    }
    else {
        die "Unable to find dnsc client proxy software\n";
    }
}
else {
    die "$dnsc: Unimplemented invocation.\n";
}

# Just choose any available ephemeral port and reuse it
my $proxy_port = IO::Socket::INET->new(Listen => 1)->sockport
    or die "Unable to choose a proxy port: $!\n";
print "Using proxy_port $proxy_port ...\n";
my $proxy_pid = fork;
if (!$proxy_pid) {
    # Child process #1
    # Start up the proxy client invisibly
    open STDIN,  "</dev/null";
    open STDOUT, ">/dev/null";
    open STDERR, ">/dev/null";
    exec $dnsc, "--suffix",$suffix, "--listen",$proxy_port, $host, $port or die "exec: $dnsc: $!";
}

# Wait a bit for the proxy server to turn on
sleep 2;
# Run the actual telnet program
exec telnet => localhost => $proxy_port;
