parsing abnf grammar using perl

444 Views Asked by At

Thanks for the inputs for question posted at link Parse::ABNF perl usage . I am still facing difficulty in resolving my problem. Request to check my problem below and provide pointers towards solution.

For now I have created the sip grammar in ABNF format in a file(named it as sip.abnf). I have sip messages with headers in a file like below (recd_message.txt):

From: <sip:[email protected]:5060;user=phone>;tag=1526438727-1338998848384-
To: "govoice-lab2-aokanlawon"<sip:[email protected]>
Contact: <sip:[email protected]:5070;transport=udp>
Allow: ACK,BYE,CANCEL,INFO,INVITE,OPTIONS,PRACK,REFER,NOTIFY
Accept: multipart/mixed,application/media_control+xml,application/sdp

I created a Perl program to use the ABNF grammar to validate the above headers messages named it as testSipHeader.pl with below content:

use strict; use warnings;
use File::Slurp;
use Parse::ABNF ;
use Data::Dumper;

my $grammar_file = shift;
my $messages = shift;
my @header_name;
my $header_status;

my $grammar = scalar read_file( $grammar_file );
$grammar =~ s/\x0d\x0a/\n/g;
$grammar =~ s/^\s+(?=[\w-]+\s*=)//mg;

$grammar = Parse::ABNF->new->parse(  scalar read_file( $grammar_file ) );
if (defined $grammar) {
    print "Grammar is now defined...\n";
    my $header;
    open ( $header , "<", $messages) or die "Could not open $messages file";
    while(<$header>) {
        print "Processing the message $_\n";
        @header_name = split(': ', $_);
        if ($header_name[0] eq "From") {
            $header_status = $grammar->From($_) ;
        } elsif ($header_name[0] eq "To") {
            $header_status = $grammar->To($_) ;
        } elsif ($header_name[0] eq "Contact") {
            $header_status = $grammar->Contact($_) ;
        } elsif ($header_name[0] eq "Allow") {
            $header_status = $grammar->Allow($_) ;
        } elsif ($header_name[0] eq "Accept") {
            $header_status = $grammar->Accept($_) ;
        } else {
            print "Error: Unsupported header $header_name[0] received\n";
        }
    }
} else {
        print "Error: grammar is not defined\n";
}

Perl invokation and output/error is below

$> perl -I. testSipHeader.pl sip.abnf recd_messages.txt
Grammar is now defined...
Processing the message From: <sip:[email protected]:5060;user=phone>;tag=1526438727-

Can't call method "From" on unblessed reference at testSipHeader.pl line 21, <$header> line 1.

Note: Currently I have C program that generates the SIP headers, and I am trying to validate the header's content by this perl functions. And I am trying to use similar to usage as given at link Parse::RecDescent grammar not working as expected

I had to modify a little in your script to include Parse::ABNF and handle input file and after that the output received is present in path https://drive.google.com/file/d/0B8KDQDXsCKR_ZERzV3IyY1M2NW8/edit?usp=sharing

1

There are 1 best solutions below

0
On

I have rewrited you script a bit, what is the output?

use Data::Dumper;
my $grammar_rules;
{
    local $/=undef;
    open(my $fh,'<',$grammar_file) or die $grammar_file,$!;
    $grammar_rules = <$fh>;
    $grammar_rules =~ s/\x0d\x0a/\n/g;
    $grammar_rules =~ s/^\s+(?=[\w-]+\s*=)//mg;
}
print Dumper('rules',$grammar_rules);
my $grammar = Parse::ABNF->new->parse(  $grammar_rules );
print Dumper('grammar',$grammar);
die "Error: grammar is not defined" if ! defined $grammer;
print "Grammar is now defined...\n";

open ( my $header_fh , "<", $messages) or die "Could not open $messages file, $!";
while(my $line = <$header_fh>) {
    chomp($line);
    print "Processing the message $line\n";
    @header_name = split(': ', $line);
    if ($header_name[0] eq "From") {
        $header_status = $grammar->From($line) ;
    } elsif ($header_name[0] eq "To") {
        $header_status = $grammar->To($line) ;
    } elsif ($header_name[0] eq "Contact") {
        $header_status = $grammar->Contact($line) ;
    } elsif ($header_name[0] eq "Allow") {
        $header_status = $grammar->Allow($line) ;
    } elsif ($header_name[0] eq "Accept") {
        $header_status = $grammar->Accept($line) ;
    } else {
        print "Error: Unsupported header $header_name[0] received\n";
    }
}