PromptUtil.pm(Chapters 8and9)


 
Network Programming with Perl
By Lincoln  D.  Stein
Slots : 1
Table of Contents
Appendix A.   Additonal Source Code

    Content

Net::NetmaskLite (Chapter 3)

This module contains utilities for working with odd- sized netmasks . With it you can easily determine the appropriate broadcast and network addresses for any combination of netmask and IP address. Examine the hostpart() , netpart() , network() , and broadcast() methods to learn the numeric relationships among these parts of the IP address and its netmask.

David Sharnoff's Net::Netmask module, available on CPAN, provides more functionality and is recommended for production work.

 0   package Net::NetmaskLite;   1   # file: Net/NetmaskLite.pm;      2   use strict;   3   use Carp 'croak';   4   use overload '""'=>netmask;      5   sub new {   6     my $pack = shift;   7     my $mask = shift or croak "Usage: Netmask->new($dotted_IP_addr)\n";   8     my $num = ($mask =~ /^\d+$/ && $mask <= 32)   9               ? _tomask($mask)  10               : _tonum($mask);  11     bless $num,$pack;  12   }    13   sub hostpart {  14     my $mask = shift;  15     my $addr = _tonum(shift)                    or croak "Usage: $netmask->hostpart($dotted_IP_addr)\n";  16     _toaddr($addr & ~$$mask);  17   }  18   sub netpart{  19     my $mask = shift;  20     my $addr = _tonum(shift)                    or croak "Usage: $netmask->hostpart($dotted_IP_addr)\n";  21     _toaddr($addr & $$mask);  22   }    23   sub broadcast {  24     my $mask = shift;  25     my $addr = _tonum(shift)                    or croak "Usage: $netmask->hostpart($dotted_IP_addr)\n";  26     _toaddr($addr  ($$mask ^ 0xffffffff));  27   }    28   sub network {  29     my $mask = shift;  30     my $addr = _tonum(shift)                    or croak "Usage: $netmask->hostpart($dotted_IP_addr)\n";  31     _toaddr($addr & ($$mask & 0xffffffff));  32   }    33   sub netmask {  _toaddr(${shift()}); }    34   # utilities  35   sub _tomask {  36     my $ones   = shift;  37     unpack "L",pack "b*",('1' x $ones) . ('0' x (32-$ones));  38   }  39   sub _tonum  { unpack "L",pack("C4",split /\./,shift) }  40   sub _toaddr { join '.',unpack("C4",pack("L",shift))   }    41   1;    42   __END__    43   =head1 NAME    44   Net::NetmaskLite - IP address netmask utility    45   =head1 SYNOPSIS    46     use Net::NetmaskLite;    47     $mask = Net::NetmaskLite->new('255.255.255.248');  48     $broadcast = $mask->broadcast('64.7.3.42');  49     $network   = $mask->network('64.7.3.42');    50     $hostpart  =  $mask->hostpart('64.7.3.42');  51     $netpart   =  $mask->netpart('64.7.3.42');    52   =head1 DESCRIPTION    53   This package provides an object that can be used for deriving the  54   broadcast and network addresses given an Internet netmask.    55   =head1 CONSTRUCTOR    56   =over 4    57   =item $mask = Net::NetmaskLite->new($mask)    58   The new() constructor creates a new netmask.  C<$mask> is the desired  59   mask.  You may use either dotted decimal form (255.255.255.0) or  60   bitcount form (24) for the mask.    61   The constructor returns a Net::NetmaskLite object, which can be used for  62   further manipulation.    63   =back    64   =head1 METHODS    65   =over 4    66   =item $bcast = $mask->broadcast($addr)    67   Given an IP address in dotted decimal form, the broadcast() method  68   returns the proper broadcast address, also in dotted decimal form.    69   =item $network = $mask->network($addr)    70   Given an IP address in dotted decimal form, the network() method  71   returns the proper network address in dotted decimal form.    72   =item $addr = $mask->hostpart($addr)    73   Given an IP address in dotted decimal form, the hostpart() method  74   returns the host part of the address in dotted decimal form.    75   =item $addr = $mask->netpart($addr)    76   Given an IP address in dotted decimal form, the hostpart() method  77   returns the network part of the address in dotted decimal form.    78   =item $addr = $mask->netmask    79   This just returns the original netmask in dotted decimal form.  The  80   quote operator is overloaded to call netmask() when the object is used  81   in a string context.    82   =back    83   =head2 Example:    84   Given a netmask of 255.255.255.248 and an IP address of 64.7.3.42, the  85   following values are returned:    86    netmask:    255.255.255.248  87    broadcast:  64.7.3.47  88    network:    64.7.3.40  89    hostpart:   0.0.0.2  90    netpart:    64.7.3.40    91   =head1 SEE ALSO    92  L<Socket>  93  L<perl>    94  =head1 AUTHOR    95  Lincoln Stein <lstein@cshl.org>    96  =head1 COPYRIGHT    97  Copyright (c) 2000 Lincoln Stein. All rights reserved. This program is  98  free software; you can redistribute it and/or modify it under the same  99  terms as Perl itself.   100  =cut 

   
Top


Network Programming with Perl
Network Programming with Perl
ISBN: 0201615711
EAN: 2147483647
Year: 2000
Pages: 173

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net