Dear Monks,

I am humble in your presence and ask for forgiveness for writing such poor code. I humbly request 5 minutes of your meditation time.

I am currently trying to decode OSPF LSA's that I am retrieving from the OSPF MIB (ospfLsdbAdvertisement) via SNMP.

I have two networks running OSPF to test against and appear to have sucesfully managed to decode the LSA 20-byte header and Network LSA's using unpack.
(I must add that I am fairly new to unpack/pack).

However, I am experiencing some very odd behaviour and wondered if you could give me any hints or tips.

Here is the 20-byte OSPF LSA Header according to the RFC:

# 0 1 2 3 # 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 +1 # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ++-+ # | LS age | Options | LS type + | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ++-+ # | Link State ID + | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ++-+ # | Advertising Router + | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ++-+ # | LS sequence number + | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ++-+ # | LS checksum | length + | # +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- ++-+

Here is the function I am using to decode the header:**
** Note that it does do some enumeration and calls a couple of other functions that aren't shown.

sub decode_header { my $buff = shift; my %lsa; print dump_hex(substr($buff,0,20)); ($lsa{age}, $lsa{options}, $lsa{type}, $lsa{lsid}, $lsa{advRouter}, $lsa{seqNo}, $lsa{checksum}, $lsa{length}) = unpack('nCCNNNnn',$buff); $lsa{lsid} = N_to_dottedQuad($lsa{lsid}); $lsa{advRouter} = N_to_dottedQuad($lsa{advRouter}); $lsa{type} = $OSPF{LSTYPE}{$lsa{type}}; $lsa{options} = decode_lsa_options($lsa{options}); return \%lsa; }

If the results of the function are Data::Dumped I get something like the following:

Dump: [000] 00 05 22 01 AC 32 00 02 AC 32 00 02 80 20 FF 99 [016] 05 90 00 48 $VAR1 = { 'seqNo' => '2149646233', 'length' => 72, 'checksum' => 1424, 'advRouter' => '172.50.0.2', 'options' => '*|-|DC|-|-|-|E|*', 'lsid' => '172.50.0.2', 'type' => 'Router-LSA', 'age' => 5 };

This looks okay, some sniffing with Ethereal and checking of the standard mib variables have shown that this LSA appears to contain the correct checksum, advertising router and lsid etc. Oddly though, age never seems to be right for some reason...

However, this is where things start to get a little strange. A couple of our routers are using IP addresses with 10 as one of the octets. If the decoder comes across such a value it converts the 10 into 32 for some reason. Other octets within the IP address are represented correctly.
Consider the following example:-

[000] 00 00 22 02 20 01 01 02 20 01 FE 01 80 00 00 10 [016] 41 A5 00 20 $VAR1 = { 'seqNo' => '2147483664', 'length' => 32, 'checksum' => 16805, 'advRouter' => '32.1.254.1', 'options' => '*|-|DC|-|-|-|E|*', 'lsid' => '32.1.1.2', 'type' => 'Network-LSA', 'age' => '0' };

Both the LSID and advertising router should have 10 as their first octet. I've tried a number of things:

  • Renumbering the interfaces and re-checking, it is fine with all IP addresses other than ones with .10 in them.
  • Trying different routers from both Cisco and Juniper, again, the same result.
  • Trying a different network altogether, again, the same result.

    In light of the tests I presume it must be something with my unpack of the packet. If anyone can make any suggestions or give any advice that would be very much appreciated.

    Regards, Matt

    In reply to Strange results from unpack when trying to decode OSPF LSA's from the OSPF MIB by mattedug

    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.