如何使用 asn1c 生成的代码解码 GSM-TCAP 消息

How to decode GSM-TCAP messages using asn1c generated code

我正在使用 TCAP 协议规范中的 asn1c 生成的 c 代码(即对应的 ASN1 files)。 我可以通过生成的代码成功地对 TCAP 数据包进行编码。 但是,尝试 "decode" 相关字节流失败。

示例代码如下

// A real byte stream of a TCAP message:
unsigned char packet_bytes[] = {
  0x62, 0x43, 0x48, 0x04, 0x00, 0x18, 0x02, 0x78,
  0x6b, 0x1a, 0x28, 0x18, 0x06, 0x07, 0x00, 0x11,
  0x86, 0x05, 0x01, 0x01, 0x01, 0xa0, 0x0d, 0x60,
  0x0b, 0xa1, 0x09, 0x06, 0x07, 0x04, 0x00, 0x00,
  0x01, 0x00, 0x14, 0x03, 0x6c, 0x1f, 0xa1, 0x1d,
  0x02, 0x01, 0x00, 0x02, 0x01, 0x2d, 0x30, 0x15,
  0x80, 0x07, 0x91, 0x64, 0x21, 0x92, 0x05, 0x31,
  0x74, 0x81, 0x01, 0x01, 0x82, 0x07, 0x91, 0x64,
  0x21, 0x00, 0x00, 0x90, 0x02
};
// Initializing ...
TCAP_TCMessage_t _pdu, *pdu = &_pdu;
memset(pdu, 0, sizeof(*pdu));    

// Decoding:
asn_dec_rval_t dec_ret = ber_decode(NULL, &asn_DEF_TCAP_TCMessage, (void **) &pdu, packet_bytes, sizeof(packet_bytes));

虽然正确检测到消息类型("Begin",在本例中),但未解析其他参数。

使用其他编码规则,即 aper_decode()uper_decode(),也会失败。 如果有人能描述如何使用自动生成的 c 代码解码(解析)TCAP 消息的字节串,我将不胜感激。

I am using the c code generated by asn1c from the TCAP protocol specification

您使用的是哪个 asn1c(git 提交 ID)以及您从哪里获得它,因为那里有相当多的 fork 日志?

While the message type ("Begin", in this case), is correctly detected, but other paramters are not parsed.

你怎么知道 Begin 被正确检测到?

Using other encoding rules, i.e., aper_decode() and uper_decode(), also fails.

尝试其他编码没有意义,因为它们与二进制不兼容。

I would be thankful if anyone can describe how to use the auto-generated c code for decoding (parsing) a byte string of TCAP messages.

您使用正确,可能 BER 解码器某处存在错误。

您可以尝试在 CFLAGS 中使用 -DASN_EMIT_DEBUG=1 进行编译(或 -DEMIT_ASN_DEBUG=1,具体取决于您使用的 asn1c 版本)以获得更多调试消息。

@Vasil,非常感谢你的回答。

Which asn1c are you using (git commit id) and where do you get it from as there are quite a log of forks out there?

我用的是mouse07410's branch.

How do you know that Begin is correctly detected?

来自ber_decode求值的pdu变量的字段present(可以在示例代码中看到pdu类型)。 从这个字节流的 "Wireshark" 输出,我知道消息的正确类型是 Begin.

You could try compiling with -DASN_EMIT_DEBUG=1 in CFLAGS (or -DEMIT_ASN_DEBUG=1 depending on the asn1c version you are using) to get some more debug messages.

感谢提供提示;很有帮助。


问题与我使用的 asn1 文件有关。 我使用 osmocom asn1 files 并通过

编译它们
ASN=../asn
asn1c $ASN/DialoguePDUs.asn $ASN/tcap.asn $ASN/UnidialoguePDUs.asn

其中,DialoguePortion定义如下(注意第一个定义有注释):

--DialoguePortion ::= [APPLICATION 11] EXPLICIT EXTERNAL

-- WS adaptation
DialoguePortion ::= [APPLICATION  11] IMPLICIT DialogueOC
DialogueOC ::= OCTET STRING    

为了能够解码 TCAP 消息, 需要使用以前的定义(如 standard 中的定义),即 DialoguePortion 应定义为

DialoguePortion ::= [APPLICATION 11] EXPLICIT EXTERNAL

在 asn1 文件中使用后一个定义时, 并重新编译asn1文件,问题解决。

P.S.: 也和我的问题有关。