Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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

by !unlike (Beadle)
on Jan 22, 2003 at 10:56 UTC ( #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(; my @dests = qw( 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.

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2023-06-06 17:22 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.