File: Whois.pm

package info (click to toggle)
asused 3.72-7
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 524 kB
  • ctags: 159
  • sloc: perl: 3,494; sh: 129; makefile: 47
file content (555 lines) | stat: -rw-r--r-- 15,186 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# Copyright (c) 2000                    RIPE NCC
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of the author not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
# AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#------------------------------------------------------------------------------
# Module Header
# Filename          : Whois.pm
# Purpose           : Make whois queries
# Author            : Timur Bakeyev <timur@ripe.net>
# Date              : 08082000
# Description       : General purpose, simple client to make whois queries
# Language Version  : Perl5
# OSs Tested        : BSD/OS 3.1
# Command Line      : None
# Input Files       : None
# Output Files      : None
# External Programs : None
# Problems          : None known
# To Do             : Make more consistent(?)
# Comments          :
# $Id: Whois.pm,v 1.8 2001/08/28 14:04:33 timur Exp $
#------------------------------------------------------------------------------
package Whois;

use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
use vars qw($ERRORSTRING $ERRORCODE);

require Exporter;

@ISA = qw(Exporter); 
# External modules
use IO::Socket;
# Error handling
use Carp;

use vars qw(
        $WHOIS_TIMEOUT
	$WHOIS_NO_ENTRY
        $WHOIS_UNKNOWN_QUERY
	$REQUEST_IGNORED
        $HOST_CONNECT_ERROR
	$NO_RESPONSE_FROM_SOCKET
        $CONNECTION_CLOSED
	$ERROR_WRITING_SOCKET
        $NO_RESULT
	$EMPTY_QUERY
        $SOCKET_ERROR
	$NO_HOST_PASSED
        $NO_PORT_PASSED
);

# Error codes
use vars qw(%ERROR);
	
@EXPORT = ();

@EXPORT_OK = qw(%WHOIS_ERROR %ERROR);

%EXPORT_TAGS = (
    'ERROR_CODES' => [qw(
        $WHOIS_TIMEOUT
	$WHOIS_NO_ENTRY
        $WHOIS_UNKNOWN_QUERY
	$REQUEST_IGNORED
        $HOST_CONNECT_ERROR
	$NO_RESPONSE_FROM_SOCKET
        $CONNECTION_CLOSED
	$ERROR_WRITING_SOCKET
        $NO_RESULT
	$EMPTY_QUERY
        $SOCKET_ERROR
	$NO_HOST_PASSED
        $NO_PORT_PASSED
    )]
);

Exporter::export_ok_tags('ERROR_CODES');

$VERSION = '1.04';

#6xx  Other software/system errors
$WHOIS_TIMEOUT		= 602;
$WHOIS_NO_ENTRY		= 603;
$WHOIS_UNKNOWN_QUERY	= 604;
$REQUEST_IGNORED	= 605;
$HOST_CONNECT_ERROR	= 622;
$NO_RESPONSE_FROM_SOCKET= 623;
$CONNECTION_CLOSED	= 624;
$ERROR_WRITING_SOCKET	= 625;
$NO_RESULT		= 626;
$EMPTY_QUERY		= 627;
$SOCKET_ERROR		= 628;
$NO_HOST_PASSED		= 629;
$NO_PORT_PASSED		= 630;

# List of error messages, that correspond to error codes
%ERROR = (
    $NO_RESULT		=> 'No results were returned',
    $NO_HOST_PASSED	=> 'No hostname was passed',
    $NO_PORT_PASSED	=> 'No port was passed',
    $WHOIS_TIMEOUT	=> 'Timeout',
    $WHOIS_NO_ENTRY	=> 'No such entry in the database',
    $WHOIS_UNKNOWN_QUERY=> 'Unknown type of the query',
    $CONNECTION_CLOSED	=> 'Connection closed by foreign host',
    $EMPTY_QUERY	=> 'Query is empty',
);


# Global error variables, work ONLY for constructor
$ERRORSTRING = '';
$ERRORCODE = 0;

# http://www.ripe.net/ripencc/pub-services/db/rpsl/errors.html

# %ERROR:101: no entries found
# %ERROR:102: unknown source
# %ERROR:103: unknown object type
# %ERROR:104: unknown attribute
# %ERROR:105: attribute is not searchable
# %ERROR:106: no search key specified
# 
# %ERROR:201: access denied
# %ERROR:202: access control limit reached 
# %ERROR:203: address passing not allowed
# %ERROR:204: maximum referral lines exceeded
# 
# %ERROR:301: connection has been closed
# %ERROR:302: referral timeout
# %ERROR:303: no referral host
# %ERROR:304: referral host not responding
# 
# %ERROR:401: invalid range: Not within <first>-<last> 
# %ERROR:402: not authorized to mirror the database 
# %ERROR:403: unknown source 


# Default values for the object
my %default = (
    'Port'	=> 'whois(43)',	# Default whois port
    'Timeout'	=> 5,		# Connection timeout
    'Retry'	=> 3,		# Retry count
    'Debug'	=> 0		# Debuging
);

####################################################################
#  DESCRIPTION: Show debug message, if debugging is on
#        INPUT: Debug message
#       OUTPUT: None
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub debug {
    my $self = shift;
    my(@dbgString) = @_;
    
    if($self->{'Debug'}) {
	print STDERR @dbgString, "\n";
    }
}

####################################################################
#  DESCRIPTION: Set an error code and string and return code
#        INPUT: Error code, error string
#       OUTPUT: Error code
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub error {
    my $self = shift;
    my($errCode, $errString) = @_;
    
    $self->{'ErrorString'} = $errString;
    $self->{'ErrorCode'} = $errCode;
    
    if(wantarray()) {
	return ($self->{'ErrorCode'}, $self->{'ErrorString'});
    }
    else {
	return $self->{'ErrorCode'};
    }
}

####################################################################
#  DESCRIPTION: Retrive current error
#        INPUT: None
#       OUTPUT: Last error string
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub GetError {
    my $self = shift;
    
    if(wantarray()) {
	return ($self->{'ErrorCode'}, $self->{'ErrorString'});
    }
    else {
	return $self->{'ErrorCode'};
    }
}

####################################################################
#  DESCRIPTION: Retrive current error code
#        INPUT: None
#       OUTPUT: Last error string
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub GetErrorCode {
    my $self = shift;
    
    return $self->{'ErrorCode'};
}

####################################################################
#  DESCRIPTION: Retrive current error code
#        INPUT: None
#       OUTPUT: Last error string
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub GetErrorString {
    my $self = shift;
    
    return $self->{'ErrorString'};
}

####################################################################
#  DESCRIPTION: Clean error status
#        INPUT: None
#       OUTPUT: None
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub ClearError {
    my $self = shift;
    
    $self->{'ErrorString'} = '';
    $self->{'ErrorCode'} = 0;
}

####################################################################
#  DESCRIPTION: Create new object
#        INPUT: Hash of parameters, can be:
#		host, port, timeout, retry.
#       OUTPUT: Object
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my %param = (@_);
    my $param = '';
    my $self = { };
    
    bless $self, $class;
    
    # Clear global error variables
    $ERRORSTRING = '';
    $ERRORCODE = 0;

    # Turn debugging on if necessary
    $self->{'Debug'} = $default{'Debug'};
    
    $param = delete $param{'Debug'};
    if($param && $param =~ /^\d+$/) {
	$self->{'Debug'} = $param;
    }
    # We can override debuging switch through the environment
    if($ENV{'WHOIS_DEBUG'} && $ENV{'WHOIS_DEBUG'} =~ /^\d+$/) {
	$self->{'Debug'} = $ENV{'WHOIS_DEBUG'};
    }

    # Get server name and port
    $param = delete $param{'Host'};
    $self->{'Host'} = ($param && $param =~ /^\S+$/) ? $param : $default{'Host'};
    # We can override hostname through the environment
    if($ENV{'WHOIS_HOST'} && $ENV{'WHOIS_HOST'} =~ /^\S+$/) {
	$self->{'Host'} = $ENV{'WHOIS_HOST'};
    }
    $self->debug("Host: ", $self->{'Host'});
    
    $param = delete $param{'Port'};
    $self->{'Port'} = ($param && $param =~ /^\S+$/) ? $param : $default{'Port'};
    # We can override port through the environment
    if($ENV{'WHOIS_PORT'} && $ENV{'WHOIS_PORT'} =~ /^\S+$/) {
	$self->{'Port'} = $ENV{'WHOIS_PORT'};
    }
    $self->debug("Port: ", $self->{'Port'});
    
    # Use passed timeout if supplied
    $param = delete $param{'Timeout'};
    $self->{'Timeout'} = ($param && $param =~ /^\d+$/) ? $param : $default{'Timeout'};
    $self->debug("Timeout: ", $self->{'Timeout'});
    
    # Use passed retry count if supplied
    $param = delete $param{'Retry'};
    $self->{'Retry'} = ($param && $param =~ /^\d+$/) ? $param : $default{'Retry'};
    $self->debug("Retry count: ", $self->{'Retry'});
    
    croak("Illeagal attributes: " . join(' ', sort(keys(%param)))) if(%param);

    # Clear errors
    $self->ClearError();
    # Try to open connection and destroy object, if fail.
    if($self->Open()) {
	# Put the connection error into the global variables
	# for farther investigations of the failure
	($ERRORCODE, $ERRORSTRING) = $self->GetError();
	# No object was created
	return;
    }

    $self->debug(ref($self), " object was created");

    return $self;
}

####################################################################
#  DESCRIPTION: Open connection to whois server
#        INPUT: None
#       OUTPUT: Error code on error, 0 on success.
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub Open {
    my $self = shift;
    # Clear error
    $self->ClearError();
    # Check, that we really do have where to connect to
    return $self->error($NO_HOST_PASSED, $ERROR{$NO_HOST_PASSED})
	unless($self->{'Host'});

    return $self->error($NO_PORT_PASSED, $ERROR{$NO_PORT_PASSED})
	unless($self->{'Port'});
    
    # Retry count
    my $retry = 0;
    # Create connection
    do {
	if($retry++ > $self->{'Retry'}) {
	    return $self->error($HOST_CONNECT_ERROR, 
		sprintf("Can't connect to %s:%s after %d attempts",
		$self->{'Host'}, $self->{'Port'}, $retry));
	}
	# Delay between attempts
	sleep($retry);
	# Create connection
	$self->{'Sock'} = IO::Socket->new(
		Domain	 => AF_INET,
		PeerAddr => $self->{'Host'},
		PeerPort => $self->{'Port'},
		Proto    => 'tcp',
		Type	 => SOCK_STREAM,
		Timeout	 => $self->{'Timeout'}
		);
    
	} until($self->{'Sock'});
    # Ok
    return 0;
}

####################################################################
#  DESCRIPTION: Query whois server
#        INPUT: Query, that should be send to the server
#       OUTPUT: Error code on error, 0 on success.
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub Query {
    my $self = shift;
    # Filter out empty paramters
    my @query = grep { /\S+/ } @_;
    # Convert an array of parameters into string
    my $query = join(' ', @query);

    # Make sure we don't have trailing "\r|\n"
    if($query) {
	$query =~ s/\s*$//s;
	$query =~ s/^\s*//s;
    }

    # Clear error
    $self->ClearError();
    
    # If query is empty - fail
    return $self->error($EMPTY_QUERY, $ERROR{$EMPTY_QUERY})
	unless($query);
    
    # Store query for farther references
    $self->{'Query'} = $query;
    
    # Check, that connection exist
    unless(defined($self->{'Sock'}) && $self->{'Sock'}->opened()) {
	# Try to re-open connection
	my $error = $self->Open();
	
	$self->debug("Try to re-open connection");
	# Return, if reconnection failed
	return $error if($error);
    }

    # Make local change of input record separator
    # XXX This is RIPE specific, but, hopefuly, harmless
    local $/ = "\n\n\n";
    
    # Clear old result
    undef($self->{'Result'});
    
    # Just make shortcut to ease access
    my $sock = $self->{'Sock'};
    
    # In case of any other error also terminate
    return $self->error($SOCKET_ERROR, $sock->error())
	if($sock->error());

    $self->debug("Query: ", $self->{'Query'});
    
    # Pass a query to the server
    $sock->printf("%s\x0d\x0a", $self->{'Query'});
    
    # In case of any other error also terminate
    return $self->error($SOCKET_ERROR, $sock->error())
	if($sock->error());
    
    # Obtain results from the server
    $self->{'Result'} = $sock->getline();
    
    $self->debug("Result: ", $self->{'Result'});

    # In case of any other error also terminate
    return $self->error($SOCKET_ERROR, $sock->error())
	if($sock->error());
    
    # Close the connection
    $self->Close();
    
    # Everything is ok
    return 0;
}

####################################################################
#  DESCRIPTION: Return raw result of the query
#        INPUT: None
#       OUTPUT: Scalar with the result
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub GetResult {
    my $self = shift;
    
    return $self->{'Result'};
}

####################################################################
#  DESCRIPTION: Close connection with the whois server
#        INPUT: None
#       OUTPUT: None
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub Close {
    my $self = shift;
    
    if(defined($self->{'Sock'}) && $self->{'Sock'}->opened()) {
	$self->debug("Shutdown the connection");
	$self->{'Sock'}->shutdown(2);
	$self->{'Sock'}->close();
    }
    # Destroy socket
    undef($self->{'Sock'});
}

####################################################################
#  DESCRIPTION: Destructor for the object.
#        INPUT: None
#       OUTPUT: None
# SIDE EFFECTS: None
#       SYNTAX:	None
####################################################################
sub DESTROY {
    my $self = shift;
    
    $self->Close();
    $self->debug(ref($self), " object was destroyed");
}

1;

__END__
# Below is the stub of documentation for your module. You better edit it!

=head1 NAME

Whois - Perl extension to make queries to the whois servers

=head1 SYNOPSIS

  use Whois;
 
  $whois = new Whois('Host' => 'whois.ripe.net');
 
 die("Failed to create object") unless(ref($whois));
 
 die($whois->GetError()) if($whois->GetError());
 
 if($whois->Query("BAT-RIPE")) {
    printf("Query error: %s\n", $whois->GetError());
    exit;
 }
 
 my $result = $whois->GetResult();
 
 if($result) {
    print "$result\n";
 }
 else {
   printf("No results: %s\n", $whois->GetError());
 }
 
 $whois->Close();

=head1 DESCRIPTION

Stub documentation for Whois was created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head1 AUTHOR

Timur Bakeyev <timur@ripe.net>

=head1 SEE ALSO

perl(1).

=cut