itevad – How to write your own protocol and its stack – part 4


In this post, we will continue our Itevad Protocol Binary Ber Decoder. With the help of ANS.1 compiler, not only do we get the structural description of Itevad protocol itself but also the necessary converting functions used to convert between ASN.1 types and C basic data types, as well as the corresponding encoder and decoder function calls per different binary encoding ways, like Per, Ber and Xer. Let’s check the code!

/* Ber decoder for Binary Itevad Protocol */
[root@localhost Itevad2]# cat itevadBerDecoder.c
Itevad Protocol Binary Ber Decoder Example
April 1, 2012
Reference: Using the Open Source ASN.1 Compiler
by Lev Walkin

#include <stdio.h>
#include <sys/types.h>
#include "ItevadMessage.h"

int main( int ac, char **av)
 char buf[ 1024]; /* Temp buffer */
 ItevadMessage_t *itevadMsg = NULL; /* Type to decode */
 asn_dec_rval_t rval; /* Decoder return value */
 FILE *fp; /* Input file handler */
 size_t size; /* Number of bytes read */
 char *filename; /* Input file name */

 /* Require a single filename argument */
 if ( ac != 2)
 fprintf( stderr, "Usage: %s <file.ber>n", av[ 0]);
 exit(64); /* better, EX_USAGE */
 filename = av[ 1];

 /* Open input file as read-only binary */
 fp = fopen( filename, "rb");
 if ( !fp)
 perror( filename);
 exit(66); /* better, EX_NOINPUT */

 /* Read up to the buffer size */
 size = fread( buf, 1, sizeof( buf), fp);
 fclose( fp);
 if ( !size)
 fprintf( stderr, "%s: Empty or brokenn", filename);
 exit(65); /* better, EX_DATAERR */

 /* Decode the input buffer as Itevad type */
 rval = ber_decode( 0, &asn_DEF_ItevadMessage, (void **)&itevadMsg, buf, size);
 if ( rval.code != RC_OK)
 fprintf( stderr, "%s: Broken ItevadMessage decoding at byte %ldn",
 filename, (long)rval.consumed);
 exit(65); /* better, EX_DATAERR */

 /* Print the decoded Itevad type as XML */
 xer_fprint( stdout, &asn_DEF_ItevadMessage, itevadMsg);

 /* Decoding finished successfully */
 return 0;
[root@localhost itevad2]#

/* Generate and run itevadBerDecoderBin */
[root@localhost itevad2]# gcc -I. -o itevadBerDecoderBin *.c
[root@localhost Itevad2]# ./itevadBerDecoderBin itevad_binary.msg
        <address>31 31 31 31</address>
            <askContent>who r you?</askContent>
[root@localhost itevad2]#

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Dave's Tools, H.248/MEGACO/EGCP, Programming, Stuff about Compiler and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s