#!/usr/bin/perl -w # # Make sure the path, above, to your perl interpreter is correct. # # # Copyright (c) 2002 James Raftery. All rights reserved. This program is free # software; you can redistribute it and/or modify it under the same terms as # Perl itself. # # James Raftery # 9th April 2002. # # CHANGES # 1 May 2002: Integrate old query climbing SRV behaviour as ``wacky mode''. use strict; use Net::DNS; use Getopt::Std; use vars qw($opt_b $opt_d $opt_h $opt_p $opt_H $opt_w $opt_s %priority); getopts('bdh:p:s:Hw') or die "getopts() failed!"; my $query = $ARGV[0] || ($opt_H = 1); $opt_H = 1 if ($opt_b && $opt_w); ####################################################################### # # SET DEFAULTS HERE! # # 1. Default server locator name. Change ``whois.ripe.net'' below. $opt_h = $opt_h ? $opt_h : "whois.ripe.net"; # 2. Default symbolic name. Change ``whois'' below. $opt_s = $opt_s ? $opt_s : "whois"; # 3. Uncomment this if you want to barf on obviously non-domain queries. # If you query for IP numbers you'll definitely want to have this # commented. die "Bad query: $query" unless ($opt_H || $query =~ /^([a-z\d\-]+\.)*[a-z]{2,4}$/i); # # NO CHANGES SHOULD BE NEEDED BELOW HERE. # ####################################################################### if ($opt_H) { print < $priority{$b}; } #### sub DoWhois { my($host, $port) = @_; my($sockaddr) = 'S n a4 x8'; print "gethostbyname: $host\n" if $opt_d; my(@hostent) = gethostbyname($host); unless ($hostent[4]) { print "gethostbyname failed: $host: $!\n"; return 0; } print "getservbyname: $opt_s, tcp\n" if $opt_d; my(@servent) = getservbyname($opt_s, "tcp"); unless ($servent[2] || $port =~ /^\d+$/ || $opt_p) { print "getservbyname failed: $host: $!\n"; return 0; } print "port = $opt_p $port $servent[2]\n" if $opt_d; $port = $opt_p ? $opt_p : ($port =~ /^\d+$/ ? $port : $servent[2]); my $t = pack($sockaddr, 2, $port, $hostent[4]); print "whois: $host:$port\n" if $opt_d; unless (socket(WHOIS, 2, 1, 0)) { print "socket failed: $host: $!\n"; return 0; } unless (connect(WHOIS, $t)) { print "connect failed: $host: $!\n"; return 0; } select(WHOIS); $| = 1; print WHOIS "$query\n"; select(STDOUT); print "[$host]\n"; while () { print; last if $. > 10000; # Guard against a bongo server } close(WHOIS); return 1; } sub getSRV { my $target = shift; my @srvlist; my $srv = "_$opt_s._tcp.$target"; my $res = new Net::DNS::Resolver; $res->debug(1) if $opt_d; my $ans = $res->query($srv, "SRV", "IN"); return () unless $ans; foreach ($ans->answer) { if (($_->type eq "SRV") && ($_->name eq $srv)) { if ($opt_d) { print "Target: ". $_->target ." "; print "port: ". $_->port ." "; print "pri: ". $_->priority ." "; print "weight: ". $_->weight ."\n"; } # # Should we null the list if we get a '.'? In theory it # means ``no service here''. ATM we'll try any SRVs we # get that look good, in case some dork has a '.' mixed # in with good SRVs. # next if ($_->target eq "."); $priority{$_->target.":".$_->port} = $_->priority; @srvlist = (@srvlist, $_->target.":".$_->port); } @srvlist = sort ByPriority @srvlist; } return @srvlist; }