Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Turning a RIB into a FIB (an example of a recursive IP lookup)

by !unlike (Beadle)
on Jan 22, 2003 at 10:56 UTC ( [id://228987]=perlmeditation: print w/replies, xml ) Need Help??

I work for an ISP in the UK. I have recently been given a task that requires a Perl script to do one of the tasks of a router. That is turn a routing table (RIB) into a forwarding table (FIB). I *could* use a router to do this task, but for various reasons that I won't go into here, it is "better" that I do this within a Perl script.

I was amazed, neigh, astounded that there is not already an example of how to do this. Either that or my searches have not been thorough enough. If there are already examples then please enlighten me.

Either way here's an example of how to do this. Thoughts/comments welcome on all aspects of the code, particularly the lookup sub:
use strict; use NetAddr::IP; # Sample data my @sources = qw(192.168.0.19/32 192.168.0.20/32 192.168.0.21/32 192.168.0.22/32 192.168.1.72/30 192.168.1.76/30 192.168.1.80/30 192.168.1.84/30); my @dests = qw(192.168.1.73 192.168.1.77 192.168.1.81 192.168.1.85 int1 int2 int3 int4); my @types = qw(static static static static direct direct direct direct); sub lookup { my ($ip_obj, @RIB) = @_; for(my $i = 0; $i < @RIB; $i++) { next if $ip_obj == $RIB[$i]->{dest}; # don't check self if ($ip_obj->within($RIB[$i]->{source})) { return $RIB[$i]->{dest} if $RIB[$i]->{type} eq 'direct' return lookup($RIB[$i]->{dest}, @RIB); } } } my @RIB; # routing table. AoH my %FIB; # forwarding table. H # Produce 'useful' data struct from data for (my $i = 0; $i < @sources; $i++) { $RIB[$i]->{source} = new NetAddr::IP($sources[$i]); # Only create NetAddr::IP obj if not a 'direct' route $RIB[$i]->{dest} = $types[$i] eq 'direct' ? $dests[$i] : new NetAddr::IP($dests[$i]); $RIB[$i]->{type} = $types[$i]; } for (my $i = 0; $i < @RIB; $i++) { next if $RIB[$i]->{type} ne 'static'; # only interested in # static routes $FIB{$RIB[$i]->{source}->addr()} = lookup($RIB[$i]->{dest}, @RIB); } print map{ "$_ -> $FIB{$_}\n" } sort keys %FIB;

If people think this is useful I'm happy to write a tutorial.

!unlike
"The price if ignorance, which is of course that you must learn from those who know." Scorates (paraphrased)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://228987]
Approved by claree0
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 06:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found