source: genesis/config/pcbased/node/FreeBSD/7-STABLE/IP.pm@ 8955

Last change on this file since 8955 was 8613, checked in by rick, 14 years ago

Moving,moving,moving ...

  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 2.1 KB
Line 
1#!/usr/bin/perl -w
2#
3#Module om ip addresses makkelijk te manipuleren
4#
5#Rick van der Zwet (rick@wirelessleiden.nl)
6package IP;
7
8$nGlobal=0;
9
10
11#module voor het berekenen van de netmask
12sub len2MaskCalc {
13 my $nMask = $_[0];
14 if( $nMask < 1 ) {
15 return 0;
16 }
17 my $nCalc = 0;
18 for( my $nX = 7; $nX > -1; $nX-- ) {
19 $nCalc = $nCalc + 2**$nX;
20 $nMask = $nMask - 1;
21 $nGlobal = $nMask;
22 if( $nMask < 1 ) {
23 return( $nCalc );
24 }
25 }
26 return $nCalc;
27}
28
29
30#convert een netmask naar een subnet
31sub getSubnetAddr {
32 $nGlobal = 0;
33 my @subnet;
34 $subnet[0]=len2MaskCalc( $_[0] );
35 $subnet[1]=len2MaskCalc( $nGlobal );
36 $subnet[2]=len2MaskCalc( $nGlobal );
37 $subnet[3]=len2MaskCalc( $nGlobal );
38 return( join( ".", @subnet) );
39}
40
41
42#convert een subnet naar een netmask
43sub getNetmask {
44 my $cidr = 0;
45 my @subnet = split( /\./, $_[0] );
46 foreach my $part (@subnet) {
47 my $bin = sprintf "%b", $part;
48 $cidr = $cidr + ( $bin =~ tr/1// );
49 }
50 return( $cidr );
51}
52
53
54#netmask en subnet converten naar subnet
55sub toSubnet {
56 my $subnet = $_[0];
57 if ( not $subnet =~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ ) {
58 $subnet = getSubnetAddr($_[0]);
59 }
60 return( $subnet );
61}
62
63
64#reken broadcast address uit (ip, netmask/subnet)
65sub getBroadcastAddr {
66 my @ipAddr = split(/\./,$_[0]);
67 my $subnet = toSubnet($_[1]);
68 my @subnetAddr = split(/\./, $subnet );
69 my @broadcastAddr;
70 for( my $ctr=0; $ctr < 4; $ctr++) {
71 $broadcastAddr[$ctr] = $ipAddr[$ctr] | ($subnetAddr[$ctr] ^ 255);
72 }
73 return( join( ".", @broadcastAddr) );
74}
75
76
77#reken network address uit (ip, netmask/subnet)
78sub getNetworkAddr {
79 my @ipAddr = split(/\./,$_[0]);
80 my $subnet = toSubnet($_[1]);
81 my @subnetAddr = split(/\./, $subnet );
82 my @networkAddr;
83 for( my $ctr=0; $ctr < 4; $ctr++) {
84 $networkAddr[$ctr] = ($ipAddr[$ctr] + 0) & $subnetAddr[$ctr];
85 }
86 return( join( ".", @networkAddr) );
87}
88
89
90#reken uit of een ip nummer in een range valt (ip1, ip2 ,netmask/subnet)
91sub calcOnNetwork {
92 my $subnet = toSubnet($_[2]);
93 my $tmpNetworkAddr1 = getNetworkAddr($_[0], $subnet);
94 my $tmpNetworkAddr2 = getNetworkAddr($_[1], $subnet);
95 if( $tmpNetworkAddr1 eq $tmpNetworkAddr2 ) {
96 return(1);
97 }
98 else {
99 return(0);
100 }
101}
102
1031;
Note: See TracBrowser for help on using the repository browser.