title: How can I parse a raw SNMP trap in Perl?
body: <p>A few weeks ago I wrote an SNMP relayer for our ops group.  They have some dumb devices that can only send traps to a single IP, and we have a monitoring system that listens on multiple IPs for availability.  The code's dead simple, and essentially:</p>
  
  <pre><code>while (recv($packet)) {
    foreach $target (@targets) {
      send($target, $packet);
    }
  }
  </code></pre>
  
  <p>It's worked, basically, but now the obvious short coming that it doesn't include the originator IP is an issue (apparently the first class of device included the info as a varbind and some new class does not).</p>
  
  <p>What I would like to do is change my code to something like this:</p>
  
  <pre><code>while ($server-&gt;recv($packet)) {
    my $obj = decompile($packet)
    if (!$obj-&gt;{varbind}{snmpTrapAddress}) {
      $obj-&gt;{varbind}{snmpTrapAddress} = inet_ntoa($server-&gt;peeraddr());
    }
    $packet = compile($obj);
    foreach $target (@targets) {
      send($target, $packet);
    }
  }
  </code></pre>
  
  <p>In other words, if my sender isn't including snmpTrapAddress, add it.  The problem is that every SNMP package I've looked at for Perl seems very heavily focused on the infrastructure of receiving traps and performing gets.</p>
  
  <p>So:  Is there a simple Perl module that will allow me to say "here's a blob of data representing an snmp trap.  decode it into something I can easily manipulate, then recompile it back into a blob I can throw over the network"?</p>
  
  <p>If the answer you give is "use SNMP dummy", can you provide examples of this?  I may just be blind, but from the output of <a href="http://search.cpan.org/perldoc/SNMP" rel="nofollow noreferrer">perldoc SNMP</a> it's not obvious to me how to use it in this manner.</p>
  
  <p>EDIT:</p>
  
  <p>Turns out after looking around a bit that "SNMP encoding" is really ASN.1 BER (Basic Encoding Rules).  Based on this I'm having a go with Convert::BER.  I would still welcome any easy break down/edit/rebuild tips for SNMP.</p>