it is still long way to go

stop holding your self and move forward to the never ending journey

ASN.1 Decoder part 1

with 2 comments

Every day i am dealing with this encoding decoding thing because i am working in a telco company and my position is a Software Architech in billing departement so what i am doing is design and develop billing system. The data that billed by billing is data that come from network elements like Mobile Switching Center (MSC) orServing GPRS Support Node (SGSN).All data or call detail record come from network element is encoded in ASN.1 format. My billing system should read this ASN.1 format.From this data i have to get information about the calling number, called number, event time, duration, volume (gprs only), trunk (for MSC), cgi, MSC id (for msc), sgsn address (for sgsn).
The challenge of ASN.1 is that this format is in hexadecimal format. If we open this ASN.1 file we will get hexadecimal number like this:
a0 82 01 30 a4 82 01 2c  9f 21 01 03 8d 03 00 00 36 8a 03 0a 01 04 94 0f  49 44 30 32 4d 53 4d 44 33 20 42 2f 31 33 36 8e  03 00 00 00 82 03 b5 44 e9 92 02 00 01 93 01 00  8b 03 17 30 2e 8c 03 17 31 28 9f 66 01 04 9f 42  02 24 22 86 08 15 10 01 05 72 00 85 f9 9b 07 15  f0 11 56 d0 77 1b 9f 74 08 53 57 89 00 56 87 20  60 87 08 53 57 89 00 56 87 20 f0 9a 03 17 30 28  88 07 11 26 18 08 41 58 84 9f 22 01 00 9d 01 11  9f 44 01 06 95 07 11 26 18 48 54 47 f9 99 08 11  26 18 48 54 47 29 f8 9f 49 05 30 06 f0 23 c3 9f  41 07 11 26 18 48 54 08 f9 96 06 4f 55 48 42 39  31 85 08 11 26 78 68 58 35 73 f2 9f 75 07 00 22  24 7d d5 5e 84 84 07 41 78 68 58 38 25 f7 97 06  49 41 48 4e 4d 31 9f 39 01 30 9f 26 01 00 81 03  75 92 4f 83 01 01 9f 3a 01 00 80 03 01 02 0e 9f  3f 01 00 91 01 00 90 01 00 8f 03 00 00 0b 9f 23  02 03 03 9f 20 01 01 9f 3d 01 05 9f 3e 05 02 01  00 05 03 9c 07 15 f0 11 56 d0 77 1b 9f 4e 06 4f  55 48 42 39 31 9f 4a 01 02 89 01 00

a0 81 a2 a5  81 9f 87 03 0a 01 04 8b 0f 49 44 30 32 4d 53 4d  44 33 20 42 2f 31 33 36 82 03 b5 44 ea 88 03 17  31 29 9f 66 01 04 9f 1f 02 24 22 81 03 75 e3 f2  89 01 00 85 08 15 10 11 12 96 13 07 f3 8e 07 15  f0 11 56 cf 77 11 8f 01 22 80 03 03 02 09 83 01  01 86 08 53 28 96 30 19 83 01 f0 91 08 53 28 96  30 19 83 01 03 8d 06 49 55 48 42 4d 39 8a 01 00  84 08 11 26 78 68 09 37 19 f6 99 01 06 8c 07 11  26 18 48 54 47 f9 90 07 11 26 18 48 54 00 f9 9f  21 01 42 9f 22 01 04 9f 20 07 21 80 31 57 42 70  96

Above is a sample of call detail record from msc.Ofcourse all encoded format have rule to decode it. ASN.1 have a grammar that describe the rule to encode and decode it. Every hexadecimal number have meaning that described in asn.1 grammar.
My company have a application that can decode the CDR into text file. But the decoder is very slow to decode the cdr. It takes about 15 to 30 minutes just to decode one cdr files with size 16 MB. I need a fast decoder because the billing must decode at least 14.000 cdr files a day andthe billing system should be near real time. Then i am googling to search faster ASN.1 decoder. Because the limited budget my company didn’twant to spend a lot for this decoder so i search for open source ASN.1 decoder. I found http://lionet.info/asn1c/blog/ but it is not working when asn1c compile my ASN.1 grammar so i decide to write my own decoder.
My decoder doesn’t use the grammar at all. But it solve the TLV pattern of ASN.1. I see that ASN.1 constructed in TLV (tag length value). Tag is the tag number of record, length is the length of value and the value is the value of tag number. The value may contains another TLV that it called nested TLV
T L VT L [ T L V ]T L [ T L [ T L V ] ]
My method is just simply read each byte of ASN.1 file then decide what tag that will be decoded and write the value into appropriate type.
below is my code to get each CDR types like mobile originating call, mobile teminating call, roaming call forwarding call, call forwarding call, transit call, sms originating and sms terminating.

#include <stdio.h>

#include <stdlib.h>

int main (int argc, const char * argv[]) {

// insert code here…

FILE *erifile;

erifile = fopen(“/Users/gentur/Documents/eridecoder/EB_MDN3_100104017257.DAT”, “rb”);

if( erifile == NULL ) {

exit(0);

}

int byte_read;

int y = 0;

while( feof(erifile) == 0 ) {

byte_read = getc(erifile);

if( byte_read != 0×00 ) {

if( byte_read == 0xA0 ) {

printf(“<%02d>\n”, y);

int length = getc(erifile);

int length_flag = (length & 0×80 ) >> 7;

if( length_flag == 1 ) {

int num_len_byte = (length & 0x7F );

int i;

length = 0;

for( i=0; i<num_len_byte ; i++ ) {

if( i>0 ) {

length <<= 8;

}

length = (getc(erifile) | length);

}

}

char *cdr_value = malloc(length);

fread(cdr_value, sizeof(char), length, erifile);

int j;

for( j=0; j<length; j++ ) {

if((j%16)==0) printf(“\n”);

printf(” %02X “, cdr_value[j]&0xFF);

}

printf(“\n===================================================\n”);

free(cdr_value);

y++;

}

}

}

fclose(erifile);

return 0;

}

i think this is enough for to day :)

Advertisement

Written by gentur

December 25, 2010 at 3:54 pm

Posted in C, Programming

2 Responses

Subscribe to comments with RSS.

  1. Hi gentur,
    is this code can decode huwawi cdrs files ASN.1?
    also,is there any where can get more codes which explaining how to parse the cdrs?

    aimen

    March 7, 2011 at 1:15 pm

    • Hi aimen,
      I wrote a decoder for specific vendor. I’ve tried http://lionet.info/asn1c/blog/ but it didn’t work for me. The decoder from this website works by parsing the ASN.1 grammar (specification) into c header file. Then you can use the header file to decode your data.But sometimes the binary data has different structure with its grammar that is why i write my own decoder. My decoder doesn’t use asn.1 grammar. My method is to read the TLV pattern and give a meaning for specific TAG.
      I send you a message on your mail.

      gentur

      March 7, 2011 at 2:09 pm


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.