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)

In reply to Turning a RIB into a FIB (an example of a recursive IP lookup) by !unlike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.