简体   繁体   中英

How to use asn.1 while transmit data in the internet?

asn.1 = abstract syntax annotation one. I know it can define some types of data and by using asn1c compiler, we can convert Rectangle.asn to some of .c and .h files. Then we can encode or decode from or to different form of data(ber, per, xer, etc.) by using these .c and .h files. But how? I have a file named Rectangle.asn:

RectangleTest DEFINITIONS ::= BEGIN

Rectangle ::= SEQUENCE {
    height INTEGER, -- Height of the rectangle
    width INTEGER -- Width of the rectangle
}

END

and asn1c Rectangle.asn command brings many .c and .h files. The related files such as Rectangle.h and Rectangle.c is not as simple as I am concerned. Why the struct is nested? Why there is a data type named INTEGER_t but not just int? I am so confused and have no idea on how to really use these .c and .h files when I just want to send a serialized rect = {10, 20}? Please help! some code on Rectangle.h and Rectangle.c

/* Rectangle */
typedef struct Rectangle {
    INTEGER_t    height;
    INTEGER_t    width;

    /* Context for parsing across buffer boundaries */
    asn_struct_ctx_t _asn_ctx;
} Rectangle_t;


#include "Rectangle.h"

static asn_TYPE_member_t asn_MBR_Rectangle_1[] = {
    { ATF_NOFLAGS, 0, offsetof(struct Rectangle, height),
        (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
        0,
        &asn_DEF_INTEGER,
        0,  /* Defer constraints checking to the member type */
        0,  /* PER is not compiled, use -gen-PER */
        0,
        "height"
        },
    { ATF_NOFLAGS, 0, offsetof(struct Rectangle, width),
        (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
        0,
        &asn_DEF_INTEGER,
        0,  /* Defer constraints checking to the member type */
        0,  /* PER is not compiled, use -gen-PER */
        0,
        "width"
        },
};
static ber_tlv_tag_t asn_DEF_Rectangle_tags_1[] = {
    (ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static asn_TYPE_tag2member_t asn_MAP_Rectangle_tag2el_1[] = {
    { (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 0, 0, 1 }, /* height at 4 */
    { (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 1, -1, 0 } /* width at 6 */
};
static asn_SEQUENCE_specifics_t asn_SPC_Rectangle_specs_1 = {
    sizeof(struct Rectangle),
    offsetof(struct Rectangle, _asn_ctx),
    asn_MAP_Rectangle_tag2el_1,
    2,  /* Count of tags in the map */
    0, 0, 0,    /* Optional elements (not needed) */
    -1, /* Start extensions */
    -1  /* Stop extensions */
};

You need to read more about ASN.1.

The problem that it solves is that things on different computers are not all the same. If you want to send data from an embedded x86 DOS system (16 bit little endian int) to a SPARC (32 bit big endian int) then you can't just use int on both sides.

There are two main parts to ASN.1:

  • the language description ie, the ASN.1 grammar
  • the encoding rules that apply the grammar to convert host data to and from encoded data

There are some examples on the asn1c web site .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM