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

Previous_Part_1

In this part, we will start to play with ASN.1 and asn1c – installing open source ASN.1 compiler, asn1c, writing ANS.1 description of Itevad and then compiling. Please note we will not cover detailed grammar of ASN.1 or how to install asn1c into your machine. All these info could be found from Lev’s website: http://lionet.info/asn1c/blog/ Here we go:)

/* Install and make sure ASN.1 compiler work */
[root@localhost ItevadBin]# which asn1c
/usr/local/bin/asn1c
[root@localhost ItevadBin]# asn1c -h
ASN.1 Compiler, v0.9.21
Copyright (c) 2003, 2004, 2005, 2006 Lev Walkin <vlm@lionet.info>
Usage: asn1c [options] file ...
Options:
  -E                    Run only the ASN.1 parser and print out the tree
  -F                    During -E operation, also perform tree fixing

  -P                    Concatenate and print the compiled text
  -R                    Restrict output (tables only, no support code)
  -S <dir>              Directory with support (skeleton?) files
                        (Default is "/usr/local/share/asn1c")
  -X                    Generate and print the XML DTD

  -Werror               Treat warnings as errors; abort if any warning
  -Wdebug-lexer         Enable verbose debugging output from lexer
  -Wdebug-fixer         --//-- semantics processor
  -Wdebug-compiler      --//-- compiler

  -fbless-SIZE          Allow SIZE() constraint for INTEGER etc (non-std.)
  -fcompound-names      Disambiguate C's struct NAME's inside top-level types
  -findirect-choice     Compile members of CHOICE as indirect pointers
  -fknown-extern-type=<name>    Pretend the specified type is known
  -fnative-types        Use "long" instead of INTEGER_t whenever possible, etc.
  -fno-constraints      Do not generate constraint checking code
  -fno-include-deps     Do not generate courtesy #includes for dependencies
  -funnamed-unions      Enable unnamed unions in structures
  -fskeletons-copy      Force copying the support files

  -gen-PER              Generate PER support code
  -pdu=auto             Generate PDU table (discover PDUs automatically)

  -print-class-matrix   Print out the collected object class matrix (debug)
  -print-constraints    Explain subtype constraints (debug)
  -print-lines          Generate "-- #line" comments in -E output
[root@localhost ItevadBin]#


/* ASN.1 description for Itevad Protocol */
[root@localhost itevad2]# cat itevad.asn1
/*
 * Itevad - an example protocol
 * which is used to do nothing or nothing
 * Name: itevad.asn1
 * dave.tian@alcatel-lucent.com
 * Jan 6, 2011
 * http://daveti.blog.com
 */

ITEVAD DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

ItevadMessage ::= SEQUENCE
{
 version Version,
 ip4Address IP4Address,
 messageBody MessageBody
}

Version ::= INTEGER(0..99)

IP4Address ::= SEQUENCE
{
 address OCTET STRING (SIZE(4)),
 portNumber INTEGER(0..65536) OPTIONAL
}

MessageBody ::= CHOICE
{
 messageRequest MessageRequest,
 messageReply MessageReply
}

MessageRequest ::= SEQUENCE
{
 transactionId TransactionId,
 askContent AskContent
}

MessageReply ::= SEQUENCE
{
 transactionId TransactionId,
 answerContent AnswerContent
}

TransactionId ::= INTEGER(0..4294967295) -- 32-bit unsigned integer, 0xffffffff

AskContent ::= IA5String(SIZE(1..1000))

AnswerContent ::= IA5String(SIZE(1..1000))

END
[root@localhost itevad2]#


/* Generate structure headers and sources for encoding/decoding Binary Itevad Protocol */
[root@localhost itevad2]# asn1c -fnative-types itevad.asn1
Compiled ItevadMessage.c
Compiled ItevadMessage.h
Compiled Version.c
Compiled Version.h
Compiled IP4Address.c
Compiled IP4Address.h
Compiled MessageBody.c
Compiled MessageBody.h
Compiled MessageRequest.c
Compiled MessageRequest.h
Compiled MessageReply.c
Compiled MessageReply.h
Compiled TransactionId.c
Compiled TransactionId.h
Compiled AskContent.c
Compiled AskContent.h
Compiled AnswerContent.c
Compiled AnswerContent.h
Symlinked /usr/local/share/asn1c/INTEGER.h      -> INTEGER.h
Symlinked /usr/local/share/asn1c/NativeEnumerated.h     -> NativeEnumerated.h
Symlinked /usr/local/share/asn1c/IA5String.h    -> IA5String.h
Symlinked /usr/local/share/asn1c/IA5String.c    -> IA5String.c
Symlinked /usr/local/share/asn1c/INTEGER.c      -> INTEGER.c
Symlinked /usr/local/share/asn1c/NativeEnumerated.c     -> NativeEnumerated.c
Symlinked /usr/local/share/asn1c/NativeInteger.h        -> NativeInteger.h
Symlinked /usr/local/share/asn1c/NativeInteger.c        -> NativeInteger.c
Symlinked /usr/local/share/asn1c/constr_CHOICE.h        -> constr_CHOICE.h
Symlinked /usr/local/share/asn1c/constr_CHOICE.c        -> constr_CHOICE.c
Symlinked /usr/local/share/asn1c/constr_SEQUENCE.h      -> constr_SEQUENCE.h
Symlinked /usr/local/share/asn1c/constr_SEQUENCE.c      -> constr_SEQUENCE.c
Symlinked /usr/local/share/asn1c/asn_application.h      -> asn_application.h
Symlinked /usr/local/share/asn1c/asn_system.h   -> asn_system.h
Symlinked /usr/local/share/asn1c/asn_codecs.h   -> asn_codecs.h
Symlinked /usr/local/share/asn1c/asn_internal.h -> asn_internal.h
Symlinked /usr/local/share/asn1c/OCTET_STRING.h -> OCTET_STRING.h
Symlinked /usr/local/share/asn1c/OCTET_STRING.c -> OCTET_STRING.c
Symlinked /usr/local/share/asn1c/BIT_STRING.h   -> BIT_STRING.h
Symlinked /usr/local/share/asn1c/BIT_STRING.c   -> BIT_STRING.c
Symlinked /usr/local/share/asn1c/asn_codecs_prim.c      -> asn_codecs_prim.c
Symlinked /usr/local/share/asn1c/asn_codecs_prim.h      -> asn_codecs_prim.h
Symlinked /usr/local/share/asn1c/ber_tlv_length.h       -> ber_tlv_length.h
Symlinked /usr/local/share/asn1c/ber_tlv_length.c       -> ber_tlv_length.c
Symlinked /usr/local/share/asn1c/ber_tlv_tag.h  -> ber_tlv_tag.h
Symlinked /usr/local/share/asn1c/ber_tlv_tag.c  -> ber_tlv_tag.c
Symlinked /usr/local/share/asn1c/ber_decoder.h  -> ber_decoder.h
Symlinked /usr/local/share/asn1c/ber_decoder.c  -> ber_decoder.c
Symlinked /usr/local/share/asn1c/der_encoder.h  -> der_encoder.h
Symlinked /usr/local/share/asn1c/der_encoder.c  -> der_encoder.c
Symlinked /usr/local/share/asn1c/constr_TYPE.h  -> constr_TYPE.h
Symlinked /usr/local/share/asn1c/constr_TYPE.c  -> constr_TYPE.c
Symlinked /usr/local/share/asn1c/constraints.h  -> constraints.h
Symlinked /usr/local/share/asn1c/constraints.c  -> constraints.c
Symlinked /usr/local/share/asn1c/xer_support.h  -> xer_support.h
Symlinked /usr/local/share/asn1c/xer_support.c  -> xer_support.c
Symlinked /usr/local/share/asn1c/xer_decoder.h  -> xer_decoder.h
Symlinked /usr/local/share/asn1c/xer_decoder.c  -> xer_decoder.c
Symlinked /usr/local/share/asn1c/xer_encoder.h  -> xer_encoder.h
Symlinked /usr/local/share/asn1c/xer_encoder.c  -> xer_encoder.c
Symlinked /usr/local/share/asn1c/per_support.h  -> per_support.h
Symlinked /usr/local/share/asn1c/per_support.c  -> per_support.c
Symlinked /usr/local/share/asn1c/per_decoder.h  -> per_decoder.h
Symlinked /usr/local/share/asn1c/per_decoder.c  -> per_decoder.c
Symlinked /usr/local/share/asn1c/per_encoder.h  -> per_encoder.h
Symlinked /usr/local/share/asn1c/per_encoder.c  -> per_encoder.c
Symlinked /usr/local/share/asn1c/converter-sample.c     -> converter-sample.c
Generated Makefile.am.sample
[root@localhost itevad2]# rm converter-sample.c
rm: remove symbolic link `converter-sample.c'? yes

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:

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 )

Google+ photo

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

Connecting to %s